17 #include "clang/Config/config.h" 25 #include "llvm/ADT/STLExtras.h" 26 #include "llvm/ADT/SmallString.h" 27 #include "llvm/ADT/StringRef.h" 28 #include "llvm/ADT/Triple.h" 29 #include "llvm/ADT/Twine.h" 30 #include "llvm/Config/llvm-config.h" 31 #include "llvm/MC/MCTargetOptions.h" 32 #include "llvm/Option/Arg.h" 33 #include "llvm/Option/ArgList.h" 34 #include "llvm/Option/OptTable.h" 35 #include "llvm/Option/Option.h" 36 #include "llvm/Support/ErrorHandling.h" 37 #include "llvm/Support/FileSystem.h" 38 #include "llvm/Support/Path.h" 39 #include "llvm/Support/TargetParser.h" 40 #include "llvm/Support/TargetRegistry.h" 41 #include "llvm/Support/VersionTuple.h" 42 #include "llvm/Support/VirtualFileSystem.h" 48 using namespace clang;
49 using namespace driver;
50 using namespace tools;
55 return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext,
56 options::OPT_fno_rtti, options::OPT_frtti);
60 const llvm::Triple &Triple,
61 const Arg *CachedRTTIArg) {
64 if (CachedRTTIArg->getOption().matches(options::OPT_frtti))
87 if (
getVFS().exists(CandidateLibPath))
92 Triple.setEnvironment(Env);
93 if (EffectiveTriple != llvm::Triple())
94 EffectiveTriple.setEnvironment(Env);
104 return Args.hasFlag(options::OPT_fintegrated_as,
105 options::OPT_fno_integrated_as,
110 return ENABLE_X86_RELAX_RELOCATIONS;
118 if (!SanitizerArguments.get())
120 return *SanitizerArguments.get();
124 if (!XRayArguments.get())
125 XRayArguments.reset(
new XRayArgs(*
this, Args));
126 return *XRayArguments.get();
131 struct DriverSuffix {
133 const char *ModeFlag;
142 static const DriverSuffix DriverSuffixes[] = {
144 {
"clang++",
"--driver-mode=g++"},
145 {
"clang-c++",
"--driver-mode=g++"},
146 {
"clang-cc",
nullptr},
147 {
"clang-cpp",
"--driver-mode=cpp"},
148 {
"clang-g++",
"--driver-mode=g++"},
149 {
"clang-gcc",
nullptr},
150 {
"clang-cl",
"--driver-mode=cl"},
152 {
"cpp",
"--driver-mode=cpp"},
153 {
"cl",
"--driver-mode=cl"},
154 {
"++",
"--driver-mode=g++"},
155 {
"flang",
"--driver-mode=flang"},
158 for (
size_t i = 0; i < llvm::array_lengthof(DriverSuffixes); ++i) {
159 StringRef Suffix(DriverSuffixes[i].Suffix);
160 if (ProgName.endswith(Suffix)) {
161 Pos = ProgName.size() - Suffix.size();
162 return &DriverSuffixes[i];
171 std::string ProgName = llvm::sys::path::stem(Argv0);
174 std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(), ::tolower);
192 ProgName = ProgName.rtrim(
"0123456789.");
199 ProgName = ProgName.slice(0, ProgName.rfind(
'-'));
212 size_t SuffixEnd = SuffixPos + strlen(DS->Suffix);
214 size_t LastComponent = ProgName.rfind(
'-', SuffixPos);
215 if (LastComponent == std::string::npos)
217 std::string ModeSuffix = ProgName.substr(LastComponent + 1,
218 SuffixEnd - LastComponent - 1);
221 StringRef Prefix(ProgName);
222 Prefix = Prefix.slice(0, LastComponent);
223 std::string IgnoredError;
224 bool IsRegistered = llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError);
225 return ParsedClangName{Prefix, ModeSuffix, DS->ModeFlag, IsRegistered};
233 switch (Triple.getArch()) {
234 case llvm::Triple::ppc:
236 case llvm::Triple::ppc64:
238 case llvm::Triple::ppc64le:
241 return Triple.getArchName();
253 Tool *ToolChain::getClang()
const {
259 Tool *ToolChain::getFlang()
const {
270 llvm_unreachable(
"Linking is not supported by this toolchain");
273 Tool *ToolChain::getAssemble()
const {
276 return Assemble.get();
279 Tool *ToolChain::getClangAs()
const {
282 return Assemble.get();
285 Tool *ToolChain::getLink()
const {
291 Tool *ToolChain::getIfsMerge()
const {
294 return IfsMerge.get();
297 Tool *ToolChain::getOffloadBundler()
const {
303 Tool *ToolChain::getOffloadWrapper()
const {
312 return getAssemble();
315 return getIfsMerge();
326 llvm_unreachable(
"Invalid tool kind.");
340 return getOffloadBundler();
343 return getOffloadWrapper();
346 llvm_unreachable(
"Invalid tool kind.");
350 const ArgList &Args) {
351 const llvm::Triple &Triple = TC.
getTriple();
352 bool IsWindows = Triple.isOSWindows();
354 if (TC.
getArch() == llvm::Triple::arm || TC.
getArch() == llvm::Triple::armeb)
360 if (TC.
getArch() == llvm::Triple::x86 && Triple.isAndroid())
363 return llvm::Triple::getArchTypeName(TC.
getArch());
367 switch (Triple.getOS()) {
368 case llvm::Triple::FreeBSD:
370 case llvm::Triple::NetBSD:
372 case llvm::Triple::OpenBSD:
374 case llvm::Triple::Solaris:
383 if (Triple.isOSUnknown()) {
384 llvm::sys::path::append(Path,
"lib");
394 bool IsITANMSVCWindows =
395 TT.isWindowsMSVCEnvironment() || TT.isWindowsItaniumEnvironment();
402 Suffix = IsITANMSVCWindows ?
".obj" :
".o";
405 Suffix = IsITANMSVCWindows ?
".lib" :
".a";
408 Suffix = Triple.isOSWindows()
409 ? (Triple.isWindowsGNUEnvironment() ?
".dll.a" :
".lib")
416 llvm::sys::path::append(P, Prefix + Twine(
"clang_rt.") + Component + Suffix);
422 const char *Env = TT.isAndroid() ?
"-android" :
"";
424 llvm::sys::path::append(Path, Prefix + Twine(
"clang_rt.") + Component +
"-" +
425 Arch + Env + Suffix);
432 return Args.MakeArgString(
getCompilerRT(Args, Component, Type));
440 P.assign(D.ResourceDir);
441 llvm::sys::path::append(P,
"lib", D.getTargetTriple());
446 P.assign(D.ResourceDir);
447 llvm::sys::path::append(P,
"lib", Triple.str());
459 llvm::sys::path::append(P,
"..",
"lib", D.getTargetTriple(),
"c++");
465 llvm::sys::path::append(P,
"..",
"lib", Triple.str(),
"c++");
475 llvm::Triple::getArchTypeName(
getArch()));
480 if (Args.hasArg(options::OPT_noprofilelib))
484 Args.hasArg(options::OPT_fprofile_generate) ||
485 Args.hasArg(options::OPT_fprofile_generate_EQ) ||
486 Args.hasArg(options::OPT_fcs_profile_generate) ||
487 Args.hasArg(options::OPT_fcs_profile_generate_EQ) ||
488 Args.hasArg(options::OPT_fprofile_instr_generate) ||
489 Args.hasArg(options::OPT_fprofile_instr_generate_EQ) ||
490 Args.hasArg(options::OPT_fcreate_profile) ||
491 Args.hasArg(options::OPT_forder_file_instrumentation))
498 return Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
500 Args.hasArg(options::OPT_coverage);
513 return D.GetFilePath(Name, *
this);
517 return D.GetProgramPath(Name, *
this);
521 const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ);
522 StringRef UseLinker = A ? A->getValue() : CLANG_DEFAULT_LINKER;
524 if (llvm::sys::path::is_absolute(UseLinker)) {
527 if (llvm::sys::fs::can_execute(UseLinker))
529 }
else if (UseLinker.empty() || UseLinker ==
"ld") {
535 if (Triple.isOSDarwin())
536 LinkerName.append(
"ld64.");
538 LinkerName.append(
"ld.");
539 LinkerName.append(UseLinker);
542 if (llvm::sys::fs::can_execute(LinkerPath))
547 getDriver().
Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
558 if (D.IsFlangMode() &&
id == types::TY_PP_Fortran)
559 id = types::TY_Fortran;
569 llvm::Triple HostTriple(LLVM_HOST_TRIPLE);
570 switch (HostTriple.getArch()) {
573 case llvm::Triple::arm:
574 case llvm::Triple::armeb:
575 case llvm::Triple::thumb:
576 case llvm::Triple::thumbeb:
577 return getArch() != llvm::Triple::arm &&
getArch() != llvm::Triple::thumb &&
578 getArch() != llvm::Triple::armeb &&
getArch() != llvm::Triple::thumbeb;
580 return HostTriple.getArch() !=
getArch();
589 llvm::ExceptionHandling
595 if (Model ==
"single") {
597 return Triple.getArch() == llvm::Triple::arm ||
598 Triple.getArch() == llvm::Triple::armeb ||
599 Triple.getArch() == llvm::Triple::thumb ||
600 Triple.getArch() == llvm::Triple::thumbeb ||
601 Triple.getArch() == llvm::Triple::wasm32 ||
602 Triple.getArch() == llvm::Triple::wasm64;
603 }
else if (Model ==
"posix")
615 case llvm::Triple::x86_64: {
617 if (!Triple.isOSBinFormatMachO())
620 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
623 StringRef MArch = A->getValue();
624 if (MArch ==
"x86_64h")
625 Triple.setArchName(MArch);
627 return Triple.getTriple();
629 case llvm::Triple::aarch64: {
631 if (!Triple.isOSBinFormatMachO())
637 Triple.setArchName(
"arm64");
638 return Triple.getTriple();
640 case llvm::Triple::aarch64_32:
642 case llvm::Triple::arm:
643 case llvm::Triple::armeb:
644 case llvm::Triple::thumb:
645 case llvm::Triple::thumbeb: {
648 bool IsBigEndian =
getTriple().getArch() == llvm::Triple::armeb ||
649 getTriple().getArch() == llvm::Triple::thumbeb;
653 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
654 options::OPT_mbig_endian)) {
655 IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian);
661 StringRef MCPU, MArch;
662 if (
const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
663 MCPU = A->getValue();
664 if (
const Arg *A = Args.getLastArg(options::OPT_march_EQ))
665 MArch = A->getValue();
667 Triple.isOSBinFormatMachO()
672 bool IsMProfile = ARM::parseArchProfile(Suffix) == ARM::ProfileKind::M;
673 bool ThumbDefault = IsMProfile || (ARM::parseArchVersion(Suffix) == 7 &&
678 std::string ArchName;
686 bool ARMModeRequested = !Args.hasFlag(options::OPT_mthumb,
687 options::OPT_mno_thumb, ThumbDefault);
688 if (IsMProfile && ARMModeRequested) {
690 getDriver().
Diag(diag::err_cpu_unsupported_isa) << CPU <<
"ARM";
699 bool IsThumb =
false;
700 if (InputType != types::TY_PP_Asm)
701 IsThumb = Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb,
709 Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
710 for (StringRef
Value : A->getValues()) {
711 if (
Value ==
"-mthumb")
719 if (IsThumb || IsMProfile ||
getTriple().isOSWindows()) {
721 ArchName =
"thumbeb";
725 Triple.setArchName(ArchName + Suffix.str());
727 return Triple.getTriple();
738 ArgStringList &CC1Args)
const {
743 const ArgList &DriverArgs, ArgStringList &CC1Args,
749 llvm::opt::ArgStringList &CmdArgs)
const {
756 const ArgList &Args)
const {
757 const Arg* A = Args.getLastArg(options::OPT_rtlib_EQ);
758 StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_RTLIB;
761 if (LibName ==
"compiler-rt")
763 else if (LibName ==
"libgcc")
765 else if (LibName ==
"platform")
769 getDriver().
Diag(diag::err_drv_invalid_rtlib_name) << A->getAsString(Args);
775 const ArgList &Args)
const {
776 const Arg *A = Args.getLastArg(options::OPT_unwindlib_EQ);
777 StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_UNWINDLIB;
779 if (LibName ==
"none")
781 else if (LibName ==
"platform" || LibName ==
"") {
787 }
else if (LibName ==
"libunwind") {
791 }
else if (LibName ==
"libgcc")
796 << A->getAsString(Args);
802 const Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
803 StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_CXX_STDLIB;
806 if (LibName ==
"libc++")
808 else if (LibName ==
"libstdc++")
810 else if (LibName ==
"platform")
814 getDriver().
Diag(diag::err_drv_invalid_stdlib_name) << A->getAsString(Args);
821 ArgStringList &CC1Args,
823 CC1Args.push_back(
"-internal-isystem");
824 CC1Args.push_back(DriverArgs.MakeArgString(Path));
836 ArgStringList &CC1Args,
838 CC1Args.push_back(
"-internal-externc-isystem");
839 CC1Args.push_back(DriverArgs.MakeArgString(Path));
843 ArgStringList &CC1Args,
845 if (llvm::sys::fs::exists(Path))
851 ArgStringList &CC1Args,
853 for (
const auto &Path : Paths) {
854 CC1Args.push_back(
"-internal-isystem");
855 CC1Args.push_back(DriverArgs.MakeArgString(Path));
860 ArgStringList &CC1Args)
const {
870 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
874 const llvm::opt::ArgList &DriverArgs,
875 llvm::opt::ArgStringList &CC1Args)
const {
876 DriverArgs.ClaimAllArgs(options::OPT_stdlibxx_isystem);
877 if (!DriverArgs.hasArg(options::OPT_nostdincxx))
879 DriverArgs.getAllArgValues(options::OPT_stdlibxx_isystem))
885 !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
886 options::OPT_nostdlibxx);
890 ArgStringList &CmdArgs)
const {
891 assert(!Args.hasArg(options::OPT_nostdlibxx) &&
892 "should not have called this");
897 CmdArgs.push_back(
"-lc++");
901 CmdArgs.push_back(
"-lstdc++");
907 ArgStringList &CmdArgs)
const {
909 if(LibPath.length() > 0)
910 CmdArgs.push_back(Args.MakeArgString(StringRef(
"-L") + LibPath));
914 ArgStringList &CmdArgs)
const {
915 CmdArgs.push_back(
"-lcc_kext");
919 ArgStringList &CmdArgs)
const {
925 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
926 options::OPT_funsafe_math_optimizations,
927 options::OPT_fno_unsafe_math_optimizations);
929 if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
930 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
935 if (Path ==
"crtfastmath.o")
938 CmdArgs.push_back(Args.MakeArgString(Path));
948 (SanitizerKind::CFI & ~SanitizerKind::CFIICall) |
949 SanitizerKind::CFICastStrict |
950 SanitizerKind::FloatDivideByZero |
951 SanitizerKind::UnsignedIntegerOverflow |
952 SanitizerKind::ImplicitConversion |
954 if (
getTriple().getArch() == llvm::Triple::x86 ||
955 getTriple().getArch() == llvm::Triple::x86_64 ||
956 getTriple().getArch() == llvm::Triple::arm ||
957 getTriple().getArch() == llvm::Triple::aarch64 ||
958 getTriple().getArch() == llvm::Triple::wasm32 ||
959 getTriple().getArch() == llvm::Triple::wasm64)
960 Res |= SanitizerKind::CFIICall;
961 if (
getTriple().getArch() == llvm::Triple::x86_64 ||
962 getTriple().getArch() == llvm::Triple::aarch64)
963 Res |= SanitizerKind::ShadowCallStack;
964 if (
getTriple().getArch() == llvm::Triple::aarch64 ||
965 getTriple().getArch() == llvm::Triple::aarch64_be)
966 Res |= SanitizerKind::MemTag;
971 ArgStringList &CC1Args)
const {}
974 ArgStringList &CC1Args)
const {}
978 return VersionTuple(Version);
981 return VersionTuple(Version / 100, Version % 100);
983 unsigned Build = 0, Factor = 1;
984 for (; Version > 10000; Version = Version / 10, Factor = Factor * 10)
985 Build = Build + (Version % 10) * Factor;
986 return VersionTuple(Version / 100, Version % 100, Build);
991 const llvm::opt::ArgList &Args)
const {
992 const Arg *MSCVersion = Args.getLastArg(options::OPT_fmsc_version);
993 const Arg *MSCompatibilityVersion =
994 Args.getLastArg(options::OPT_fms_compatibility_version);
996 if (MSCVersion && MSCompatibilityVersion) {
998 D->
Diag(diag::err_drv_argument_not_allowed_with)
999 << MSCVersion->getAsString(Args)
1000 << MSCompatibilityVersion->getAsString(Args);
1001 return VersionTuple();
1004 if (MSCompatibilityVersion) {
1006 if (MSVT.tryParse(MSCompatibilityVersion->getValue())) {
1008 D->
Diag(diag::err_drv_invalid_value)
1009 << MSCompatibilityVersion->getAsString(Args)
1010 << MSCompatibilityVersion->getValue();
1017 unsigned Version = 0;
1018 if (StringRef(MSCVersion->getValue()).getAsInteger(10, Version)) {
1020 D->
Diag(diag::err_drv_invalid_value)
1021 << MSCVersion->getAsString(Args) << MSCVersion->getValue();
1027 return VersionTuple();
1031 const llvm::opt::DerivedArgList &Args,
bool SameTripleAsHost,
1033 DerivedArgList *DAL =
new DerivedArgList(Args.getBaseArgs());
1035 bool Modified =
false;
1038 for (
auto *A : Args) {
1043 if (A->getOption().matches(options::OPT_m_Group)) {
1044 if (SameTripleAsHost)
1053 bool XOpenMPTargetNoTriple =
1054 A->getOption().matches(options::OPT_Xopenmp_target);
1056 if (A->getOption().matches(options::OPT_Xopenmp_target_EQ)) {
1059 Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
1062 }
else if (XOpenMPTargetNoTriple) {
1064 Index = Args.getBaseArgs().MakeIndex(A->getValue(0));
1072 std::unique_ptr<Arg> XOpenMPTargetArg(Opts.ParseOneArg(Args, Index));
1073 if (!XOpenMPTargetArg || Index > Prev + 1) {
1074 getDriver().
Diag(diag::err_drv_invalid_Xopenmp_target_with_args)
1075 << A->getAsString(Args);
1078 if (XOpenMPTargetNoTriple && XOpenMPTargetArg &&
1079 Args.getAllArgValues(options::OPT_fopenmp_targets_EQ).size() != 1) {
1080 getDriver().
Diag(diag::err_drv_Xopenmp_target_missing_triple);
1083 XOpenMPTargetArg->setBaseArg(A);
1084 A = XOpenMPTargetArg.release();
1085 AllocatedArgs.push_back(A);
bool ShouldUseFlangCompiler(const JobAction &JA) const
ShouldUseFlangCompiler - Should the flang compiler be used to handle this action. ...
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Defines types useful for describing an Objective-C runtime.
The base class of the type hierarchy.
DiagnosticBuilder Diag(unsigned DiagID) const
'gcc' is the Objective-C runtime shipped with GCC, implementing a fragile Objective-C ABI ...
Defines the clang::SanitizerKind enum.
bool isOptimizationLevelFast(const llvm::opt::ArgList &Args)
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
llvm::vfs::FileSystem & getVFS() const
ActionClass getKind() const
'gnustep' is the modern non-fragile GNUstep runtime.
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
constexpr XRayInstrMask None
Helper structure used to pass information extracted from clang executable name such as i686-linux-and...
Dataflow Directional Tag Classes.
The basic abstraction for the target Objective-C runtime.
bool ShouldUseClangCompiler(const JobAction &JA) const
ShouldUseClangCompiler - Should the clang compiler be used to handle this action. ...
ID lookupTypeForExtension(llvm::StringRef Ext)
lookupTypeForExtension - Lookup the type to use for the file extension Ext.
const llvm::opt::OptTable & getOpts() const