15 #include "clang/Config/config.h" 23 #include "llvm/ADT/STLExtras.h" 24 #include "llvm/ADT/SmallString.h" 25 #include "llvm/ADT/StringRef.h" 26 #include "llvm/ADT/Triple.h" 27 #include "llvm/ADT/Twine.h" 28 #include "llvm/Config/llvm-config.h" 29 #include "llvm/MC/MCTargetOptions.h" 30 #include "llvm/Option/Arg.h" 31 #include "llvm/Option/ArgList.h" 32 #include "llvm/Option/OptTable.h" 33 #include "llvm/Option/Option.h" 34 #include "llvm/Support/ErrorHandling.h" 35 #include "llvm/Support/FileSystem.h" 36 #include "llvm/Support/Path.h" 37 #include "llvm/Support/TargetParser.h" 38 #include "llvm/Support/TargetRegistry.h" 39 #include "llvm/Support/VersionTuple.h" 40 #include "llvm/Support/VirtualFileSystem.h" 46 using namespace clang;
47 using namespace driver;
48 using namespace tools;
53 return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext,
54 options::OPT_fno_rtti, options::OPT_frtti);
58 const llvm::Triple &Triple,
59 const Arg *CachedRTTIArg) {
62 if (CachedRTTIArg->getOption().matches(options::OPT_frtti))
85 if (
getVFS().exists(CandidateLibPath))
90 Triple.setEnvironment(Env);
91 if (EffectiveTriple != llvm::Triple())
92 EffectiveTriple.setEnvironment(Env);
102 return Args.hasFlag(options::OPT_fintegrated_as,
103 options::OPT_fno_integrated_as,
108 return ENABLE_X86_RELAX_RELOCATIONS;
116 if (!SanitizerArguments.get())
118 return *SanitizerArguments.get();
122 if (!XRayArguments.get())
123 XRayArguments.reset(
new XRayArgs(*
this, Args));
124 return *XRayArguments.get();
129 struct DriverSuffix {
131 const char *ModeFlag;
140 static const DriverSuffix DriverSuffixes[] = {
142 {
"clang++",
"--driver-mode=g++"},
143 {
"clang-c++",
"--driver-mode=g++"},
144 {
"clang-cc",
nullptr},
145 {
"clang-cpp",
"--driver-mode=cpp"},
146 {
"clang-g++",
"--driver-mode=g++"},
147 {
"clang-gcc",
nullptr},
148 {
"clang-cl",
"--driver-mode=cl"},
150 {
"cpp",
"--driver-mode=cpp"},
151 {
"cl",
"--driver-mode=cl"},
152 {
"++",
"--driver-mode=g++"},
155 for (
size_t i = 0;
i < llvm::array_lengthof(DriverSuffixes); ++
i) {
156 StringRef Suffix(DriverSuffixes[
i].Suffix);
157 if (ProgName.endswith(Suffix)) {
158 Pos = ProgName.size() - Suffix.size();
159 return &DriverSuffixes[
i];
168 std::string ProgName = llvm::sys::path::stem(Argv0);
171 std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(), ::tolower);
189 ProgName = ProgName.rtrim(
"0123456789.");
196 ProgName = ProgName.slice(0, ProgName.rfind(
'-'));
209 size_t SuffixEnd = SuffixPos + strlen(DS->Suffix);
211 size_t LastComponent = ProgName.rfind(
'-', SuffixPos);
212 if (LastComponent == std::string::npos)
214 std::string ModeSuffix = ProgName.substr(LastComponent + 1,
215 SuffixEnd - LastComponent - 1);
218 StringRef Prefix(ProgName);
219 Prefix = Prefix.slice(0, LastComponent);
220 std::string IgnoredError;
221 bool IsRegistered = llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError);
222 return ParsedClangName{Prefix, ModeSuffix, DS->ModeFlag, IsRegistered};
230 switch (Triple.getArch()) {
231 case llvm::Triple::ppc:
233 case llvm::Triple::ppc64:
235 case llvm::Triple::ppc64le:
238 return Triple.getArchName();
250 Tool *ToolChain::getClang()
const {
261 llvm_unreachable(
"Linking is not supported by this toolchain");
264 Tool *ToolChain::getAssemble()
const {
267 return Assemble.get();
270 Tool *ToolChain::getClangAs()
const {
273 return Assemble.get();
276 Tool *ToolChain::getLink()
const {
282 Tool *ToolChain::getOffloadBundler()
const {
291 return getAssemble();
302 llvm_unreachable(
"Invalid tool kind.");
316 return getOffloadBundler();
319 llvm_unreachable(
"Invalid tool kind.");
323 const ArgList &Args) {
324 const llvm::Triple &Triple = TC.
getTriple();
325 bool IsWindows = Triple.isOSWindows();
327 if (TC.
getArch() == llvm::Triple::arm || TC.
getArch() == llvm::Triple::armeb)
333 if (TC.
getArch() == llvm::Triple::x86 && Triple.isAndroid())
336 return llvm::Triple::getArchTypeName(TC.
getArch());
340 switch (Triple.getOS()) {
341 case llvm::Triple::FreeBSD:
343 case llvm::Triple::NetBSD:
345 case llvm::Triple::OpenBSD:
347 case llvm::Triple::Solaris:
356 if (Triple.isOSUnknown()) {
357 llvm::sys::path::append(Path,
"lib");
367 bool IsITANMSVCWindows =
368 TT.isWindowsMSVCEnvironment() || TT.isWindowsItaniumEnvironment();
375 Suffix = IsITANMSVCWindows ?
".obj" :
".o";
378 Suffix = IsITANMSVCWindows ?
".lib" :
".a";
381 Suffix = Triple.isOSWindows()
382 ? (Triple.isWindowsGNUEnvironment() ?
".dll.a" :
".lib")
389 llvm::sys::path::append(P, Prefix + Twine(
"clang_rt.") + Component + Suffix);
395 const char *Env = TT.isAndroid() ?
"-android" :
"";
397 llvm::sys::path::append(Path, Prefix + Twine(
"clang_rt.") + Component +
"-" +
398 Arch + Env + Suffix);
405 return Args.MakeArgString(
getCompilerRT(Args, Component, Type));
413 P.assign(D.ResourceDir);
414 llvm::sys::path::append(P,
"lib", D.getTargetTriple());
419 P.assign(D.ResourceDir);
420 llvm::sys::path::append(P,
"lib", Triple.str());
432 llvm::sys::path::append(P,
"..",
"lib", D.getTargetTriple(),
"c++");
438 llvm::sys::path::append(P,
"..",
"lib", Triple.str(),
"c++");
448 llvm::Triple::getArchTypeName(
getArch()));
453 if (Args.hasArg(options::OPT_noprofilelib))
457 Args.hasArg(options::OPT_fprofile_generate) ||
458 Args.hasArg(options::OPT_fprofile_generate_EQ) ||
459 Args.hasArg(options::OPT_fcs_profile_generate) ||
460 Args.hasArg(options::OPT_fcs_profile_generate_EQ) ||
461 Args.hasArg(options::OPT_fprofile_instr_generate) ||
462 Args.hasArg(options::OPT_fprofile_instr_generate_EQ) ||
463 Args.hasArg(options::OPT_fcreate_profile) ||
464 Args.hasArg(options::OPT_forder_file_instrumentation))
471 return Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
473 Args.hasArg(options::OPT_coverage);
477 if (
getDriver().ShouldUseClangCompiler(JA))
return getClang();
485 return D.GetFilePath(Name, *
this);
489 return D.GetProgramPath(Name, *
this);
493 const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ);
494 StringRef UseLinker = A ? A->getValue() : CLANG_DEFAULT_LINKER;
496 if (llvm::sys::path::is_absolute(UseLinker)) {
499 if (llvm::sys::fs::can_execute(UseLinker))
501 }
else if (UseLinker.empty() || UseLinker ==
"ld") {
507 if (Triple.isOSDarwin())
508 LinkerName.append(
"ld64.");
510 LinkerName.append(
"ld.");
511 LinkerName.append(UseLinker);
514 if (llvm::sys::fs::can_execute(LinkerPath))
519 getDriver().
Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
533 llvm::Triple HostTriple(LLVM_HOST_TRIPLE);
534 switch (HostTriple.getArch()) {
537 case llvm::Triple::arm:
538 case llvm::Triple::armeb:
539 case llvm::Triple::thumb:
540 case llvm::Triple::thumbeb:
541 return getArch() != llvm::Triple::arm &&
getArch() != llvm::Triple::thumb &&
542 getArch() != llvm::Triple::armeb &&
getArch() != llvm::Triple::thumbeb;
544 return HostTriple.getArch() !=
getArch();
553 llvm::ExceptionHandling
559 if (Model ==
"single") {
561 return Triple.getArch() == llvm::Triple::arm ||
562 Triple.getArch() == llvm::Triple::armeb ||
563 Triple.getArch() == llvm::Triple::thumb ||
564 Triple.getArch() == llvm::Triple::thumbeb ||
565 Triple.getArch() == llvm::Triple::wasm32 ||
566 Triple.getArch() == llvm::Triple::wasm64;
567 }
else if (Model ==
"posix")
579 case llvm::Triple::x86_64: {
581 if (!Triple.isOSBinFormatMachO())
584 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
587 StringRef MArch = A->getValue();
588 if (MArch ==
"x86_64h")
589 Triple.setArchName(MArch);
591 return Triple.getTriple();
593 case llvm::Triple::aarch64: {
595 if (!Triple.isOSBinFormatMachO())
601 Triple.setArchName(
"arm64");
602 return Triple.getTriple();
604 case llvm::Triple::arm:
605 case llvm::Triple::armeb:
606 case llvm::Triple::thumb:
607 case llvm::Triple::thumbeb: {
610 bool IsBigEndian =
getTriple().getArch() == llvm::Triple::armeb ||
611 getTriple().getArch() == llvm::Triple::thumbeb;
615 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
616 options::OPT_mbig_endian)) {
617 IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian);
623 StringRef MCPU, MArch;
624 if (
const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
625 MCPU = A->getValue();
626 if (
const Arg *A = Args.getLastArg(options::OPT_march_EQ))
627 MArch = A->getValue();
629 Triple.isOSBinFormatMachO()
634 bool IsMProfile = ARM::parseArchProfile(Suffix) == ARM::ProfileKind::M;
635 bool ThumbDefault = IsMProfile || (ARM::parseArchVersion(Suffix) == 7 &&
640 std::string ArchName;
648 bool ARMModeRequested = !Args.hasFlag(options::OPT_mthumb,
649 options::OPT_mno_thumb, ThumbDefault);
650 if (IsMProfile && ARMModeRequested) {
652 getDriver().
Diag(diag::err_cpu_unsupported_isa) << CPU <<
"ARM";
661 bool IsThumb =
false;
662 if (InputType != types::TY_PP_Asm)
663 IsThumb = Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb,
671 Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
672 for (StringRef
Value : A->getValues()) {
673 if (
Value ==
"-mthumb")
681 if (IsThumb || IsMProfile ||
getTriple().isOSWindows()) {
683 ArchName =
"thumbeb";
687 Triple.setArchName(ArchName + Suffix.str());
689 return Triple.getTriple();
700 ArgStringList &CC1Args)
const {
705 const ArgList &DriverArgs, ArgStringList &CC1Args,
711 llvm::opt::ArgStringList &CmdArgs)
const {
718 const ArgList &Args)
const {
719 const Arg* A = Args.getLastArg(options::OPT_rtlib_EQ);
720 StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_RTLIB;
723 if (LibName ==
"compiler-rt")
725 else if (LibName ==
"libgcc")
727 else if (LibName ==
"platform")
731 getDriver().
Diag(diag::err_drv_invalid_rtlib_name) << A->getAsString(Args);
737 const ArgList &Args)
const {
738 const Arg *A = Args.getLastArg(options::OPT_unwindlib_EQ);
739 StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_UNWINDLIB;
741 if (LibName ==
"none")
743 else if (LibName ==
"platform" || LibName ==
"") {
749 }
else if (LibName ==
"libunwind") {
753 }
else if (LibName ==
"libgcc")
758 << A->getAsString(Args);
764 const Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
765 StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_CXX_STDLIB;
768 if (LibName ==
"libc++")
770 else if (LibName ==
"libstdc++")
772 else if (LibName ==
"platform")
776 getDriver().
Diag(diag::err_drv_invalid_stdlib_name) << A->getAsString(Args);
783 ArgStringList &CC1Args,
785 CC1Args.push_back(
"-internal-isystem");
786 CC1Args.push_back(DriverArgs.MakeArgString(Path));
798 ArgStringList &CC1Args,
800 CC1Args.push_back(
"-internal-externc-isystem");
801 CC1Args.push_back(DriverArgs.MakeArgString(Path));
805 ArgStringList &CC1Args,
807 if (llvm::sys::fs::exists(Path))
813 ArgStringList &CC1Args,
815 for (
const auto Path : Paths) {
816 CC1Args.push_back(
"-internal-isystem");
817 CC1Args.push_back(DriverArgs.MakeArgString(Path));
822 ArgStringList &CC1Args)
const {
832 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
837 !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
838 options::OPT_nostdlibxx);
842 ArgStringList &CmdArgs)
const {
843 assert(!Args.hasArg(options::OPT_nostdlibxx) &&
844 "should not have called this");
849 CmdArgs.push_back(
"-lc++");
853 CmdArgs.push_back(
"-lstdc++");
859 ArgStringList &CmdArgs)
const {
861 if(LibPath.length() > 0)
862 CmdArgs.push_back(Args.MakeArgString(StringRef(
"-L") + LibPath));
866 ArgStringList &CmdArgs)
const {
867 CmdArgs.push_back(
"-lcc_kext");
871 ArgStringList &CmdArgs)
const {
877 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
878 options::OPT_funsafe_math_optimizations,
879 options::OPT_fno_unsafe_math_optimizations);
881 if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
882 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
887 if (Path ==
"crtfastmath.o")
890 CmdArgs.push_back(Args.MakeArgString(Path));
900 (SanitizerKind::CFI & ~SanitizerKind::CFIICall) |
901 SanitizerKind::CFICastStrict |
902 SanitizerKind::FloatDivideByZero |
903 SanitizerKind::UnsignedIntegerOverflow |
904 SanitizerKind::ImplicitConversion |
906 if (
getTriple().getArch() == llvm::Triple::x86 ||
907 getTriple().getArch() == llvm::Triple::x86_64 ||
908 getTriple().getArch() == llvm::Triple::arm ||
909 getTriple().getArch() == llvm::Triple::aarch64 ||
910 getTriple().getArch() == llvm::Triple::wasm32 ||
911 getTriple().getArch() == llvm::Triple::wasm64)
912 Res |= SanitizerKind::CFIICall;
913 if (
getTriple().getArch() == llvm::Triple::x86_64 ||
914 getTriple().getArch() == llvm::Triple::aarch64)
915 Res |= SanitizerKind::ShadowCallStack;
920 ArgStringList &CC1Args)
const {}
923 ArgStringList &CC1Args)
const {}
927 return VersionTuple(Version);
930 return VersionTuple(Version / 100, Version % 100);
932 unsigned Build = 0, Factor = 1;
933 for (; Version > 10000; Version = Version / 10, Factor = Factor * 10)
934 Build = Build + (Version % 10) * Factor;
935 return VersionTuple(Version / 100, Version % 100, Build);
940 const llvm::opt::ArgList &Args)
const {
941 const Arg *MSCVersion = Args.getLastArg(options::OPT_fmsc_version);
942 const Arg *MSCompatibilityVersion =
943 Args.getLastArg(options::OPT_fms_compatibility_version);
945 if (MSCVersion && MSCompatibilityVersion) {
947 D->
Diag(diag::err_drv_argument_not_allowed_with)
948 << MSCVersion->getAsString(Args)
949 << MSCompatibilityVersion->getAsString(Args);
950 return VersionTuple();
953 if (MSCompatibilityVersion) {
955 if (MSVT.tryParse(MSCompatibilityVersion->getValue())) {
957 D->
Diag(diag::err_drv_invalid_value)
958 << MSCompatibilityVersion->getAsString(Args)
959 << MSCompatibilityVersion->getValue();
966 unsigned Version = 0;
967 if (StringRef(MSCVersion->getValue()).getAsInteger(10, Version)) {
969 D->
Diag(diag::err_drv_invalid_value)
970 << MSCVersion->getAsString(Args) << MSCVersion->getValue();
976 return VersionTuple();
980 const llvm::opt::DerivedArgList &Args,
bool SameTripleAsHost,
982 DerivedArgList *DAL =
new DerivedArgList(Args.getBaseArgs());
984 bool Modified =
false;
987 for (
auto *A : Args) {
992 if (A->getOption().matches(options::OPT_m_Group)) {
993 if (SameTripleAsHost)
1002 bool XOpenMPTargetNoTriple =
1003 A->getOption().matches(options::OPT_Xopenmp_target);
1005 if (A->getOption().matches(options::OPT_Xopenmp_target_EQ)) {
1008 Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
1011 }
else if (XOpenMPTargetNoTriple) {
1013 Index = Args.getBaseArgs().MakeIndex(A->getValue(0));
1021 std::unique_ptr<Arg> XOpenMPTargetArg(Opts.ParseOneArg(Args, Index));
1022 if (!XOpenMPTargetArg || Index > Prev + 1) {
1023 getDriver().
Diag(diag::err_drv_invalid_Xopenmp_target_with_args)
1024 << A->getAsString(Args);
1027 if (XOpenMPTargetNoTriple && XOpenMPTargetArg &&
1028 Args.getAllArgValues(options::OPT_fopenmp_targets_EQ).size() != 1) {
1029 getDriver().
Diag(diag::err_drv_Xopenmp_target_missing_triple);
1032 XOpenMPTargetArg->setBaseArg(A);
1033 A = XOpenMPTargetArg.release();
1034 AllocatedArgs.push_back(A);
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.
ID lookupTypeForExtension(llvm::StringRef Ext)
lookupTypeForExtension - Lookup the type to use for the file extension Ext.
const llvm::opt::OptTable & getOpts() const