16 #include "clang/Config/config.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/Option/Arg.h"
25 #include "llvm/Option/ArgList.h"
26 #include "llvm/Option/Option.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/TargetParser.h"
31 #include "llvm/Support/TargetRegistry.h"
33 using namespace clang::driver;
34 using namespace clang::driver::tools;
35 using namespace clang;
37 using namespace llvm::opt;
40 return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext,
41 options::OPT_fno_rtti, options::OPT_frtti);
45 const llvm::Triple &Triple,
46 const Arg *CachedRTTIArg) {
49 if (CachedRTTIArg->getOption().matches(options::OPT_frtti))
56 if (!Triple.isPS4CPU())
61 Arg *Exceptions = Args.getLastArgNoClaim(
62 options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions,
63 options::OPT_fexceptions, options::OPT_fno_exceptions);
65 (Exceptions->getOption().matches(options::OPT_fexceptions) ||
66 Exceptions->getOption().matches(options::OPT_fcxx_exceptions)))
77 if (Arg *A = Args.getLastArg(options::OPT_mthread_model))
79 D.
Diag(diag::err_drv_invalid_thread_model_for_target)
80 << A->getValue() << A->getAsString(Args);
83 if (
getVFS().exists(CandidateLibPath))
93 return Args.hasFlag(options::OPT_fintegrated_as,
94 options::OPT_fno_integrated_as,
99 if (!SanitizerArguments.get())
101 return *SanitizerArguments.get();
105 if (!XRayArguments.get())
106 XRayArguments.reset(
new XRayArgs(*
this, Args));
107 return *XRayArguments.get();
111 struct DriverSuffix {
113 const char *ModeFlag;
116 const DriverSuffix *FindDriverSuffix(StringRef ProgName) {
120 static const DriverSuffix DriverSuffixes[] = {
122 {
"clang++",
"--driver-mode=g++"},
123 {
"clang-c++",
"--driver-mode=g++"},
124 {
"clang-cc",
nullptr},
125 {
"clang-cpp",
"--driver-mode=cpp"},
126 {
"clang-g++",
"--driver-mode=g++"},
127 {
"clang-gcc",
nullptr},
128 {
"clang-cl",
"--driver-mode=cl"},
130 {
"cpp",
"--driver-mode=cpp"},
131 {
"cl",
"--driver-mode=cl"},
132 {
"++",
"--driver-mode=g++"},
135 for (
size_t i = 0; i < llvm::array_lengthof(DriverSuffixes); ++i)
136 if (ProgName.endswith(DriverSuffixes[i].Suffix))
137 return &DriverSuffixes[i];
143 std::string normalizeProgramName(llvm::StringRef Argv0) {
144 std::string ProgName = llvm::sys::path::stem(Argv0);
147 std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(), ::tolower);
152 const DriverSuffix *parseDriverSuffix(StringRef ProgName) {
160 const DriverSuffix *DS = FindDriverSuffix(ProgName);
165 ProgName = ProgName.rtrim(
"0123456789.");
166 DS = FindDriverSuffix(ProgName);
172 ProgName = ProgName.slice(0, ProgName.rfind(
'-'));
173 DS = FindDriverSuffix(ProgName);
179 std::pair<std::string, std::string>
181 std::string ProgName = normalizeProgramName(PN);
182 const DriverSuffix *DS = parseDriverSuffix(ProgName);
184 return std::make_pair(
"",
"");
185 std::string ModeFlag = DS->ModeFlag ==
nullptr ?
"" : DS->ModeFlag;
187 std::string::size_type LastComponent =
188 ProgName.rfind(
'-', ProgName.size() - strlen(DS->Suffix));
189 if (LastComponent == std::string::npos)
190 return std::make_pair(
"", ModeFlag);
193 StringRef Prefix(ProgName);
194 Prefix = Prefix.slice(0, LastComponent);
195 std::string IgnoredError;
197 if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) {
200 return std::make_pair(Target, ModeFlag);
208 switch (Triple.getArch()) {
209 case llvm::Triple::ppc:
211 case llvm::Triple::ppc64:
213 case llvm::Triple::ppc64le:
216 return Triple.getArchName();
224 Tool *ToolChain::getClang()
const {
235 llvm_unreachable(
"Linking is not supported by this toolchain");
238 Tool *ToolChain::getAssemble()
const {
241 return Assemble.get();
244 Tool *ToolChain::getClangAs()
const {
247 return Assemble.get();
250 Tool *ToolChain::getLink()
const {
256 Tool *ToolChain::getOffloadBundler()
const {
265 return getAssemble();
276 llvm_unreachable(
"Invalid tool kind.");
289 return getOffloadBundler();
292 llvm_unreachable(
"Invalid tool kind.");
296 const ArgList &Args) {
297 const llvm::Triple &Triple = TC.
getTriple();
298 bool IsWindows = Triple.isOSWindows();
300 if (Triple.isWindowsMSVCEnvironment() && TC.
getArch() == llvm::Triple::x86)
303 if (TC.
getArch() == llvm::Triple::arm || TC.
getArch() == llvm::Triple::armeb)
314 const char *Env = TT.isAndroid() ?
"-android" :
"";
315 bool IsITANMSVCWindows =
316 TT.isWindowsMSVCEnvironment() || TT.isWindowsItaniumEnvironment();
319 const char *Prefix = IsITANMSVCWindows ?
"" :
"lib";
320 const char *Suffix = Shared ? (Triple.isOSWindows() ?
".dll" :
".so")
321 : (IsITANMSVCWindows ?
".lib" :
".a");
324 StringRef OSLibName = Triple.isOSFreeBSD() ?
"freebsd" :
getOS();
325 llvm::sys::path::append(Path,
"lib", OSLibName);
326 llvm::sys::path::append(Path, Prefix + Twine(
"clang_rt.") + Component +
"-" +
327 Arch + Env + Suffix);
334 return Args.MakeArgString(
getCompilerRT(Args, Component, Shared));
339 StringRef OSLibName =
getTriple().isOSFreeBSD() ?
"freebsd" :
getOS();
340 llvm::sys::path::append(Path,
"lib", OSLibName,
341 llvm::Triple::getArchTypeName(
getArch()));
346 if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
348 Args.hasArg(options::OPT_fprofile_generate) ||
349 Args.hasArg(options::OPT_fprofile_generate_EQ) ||
350 Args.hasArg(options::OPT_fprofile_instr_generate) ||
351 Args.hasArg(options::OPT_fprofile_instr_generate_EQ) ||
352 Args.hasArg(options::OPT_fcreate_profile) ||
353 Args.hasArg(options::OPT_coverage))
360 if (
getDriver().ShouldUseClangCompiler(JA))
return getClang();
376 const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ);
377 StringRef UseLinker = A ? A->getValue() : CLANG_DEFAULT_LINKER;
379 if (llvm::sys::path::is_absolute(UseLinker)) {
382 if (llvm::sys::fs::exists(UseLinker))
384 }
else if (UseLinker.empty() || UseLinker ==
"ld") {
390 LinkerName.append(UseLinker);
393 if (llvm::sys::fs::exists(LinkerPath))
398 getDriver().
Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
412 llvm::Triple HostTriple(LLVM_HOST_TRIPLE);
413 switch (HostTriple.getArch()) {
416 case llvm::Triple::arm:
417 case llvm::Triple::armeb:
418 case llvm::Triple::thumb:
419 case llvm::Triple::thumbeb:
420 return getArch() != llvm::Triple::arm &&
getArch() != llvm::Triple::thumb &&
421 getArch() != llvm::Triple::armeb &&
getArch() != llvm::Triple::thumbeb;
423 return HostTriple.getArch() !=
getArch();
433 if (Model ==
"single") {
435 return Triple.getArch() == llvm::Triple::arm ||
436 Triple.getArch() == llvm::Triple::armeb ||
437 Triple.getArch() == llvm::Triple::thumb ||
438 Triple.getArch() == llvm::Triple::thumbeb ||
439 Triple.getArch() == llvm::Triple::wasm32 ||
440 Triple.getArch() == llvm::Triple::wasm64;
441 }
else if (Model ==
"posix")
453 case llvm::Triple::x86_64: {
455 if (!Triple.isOSBinFormatMachO())
458 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
461 StringRef MArch = A->getValue();
462 if (MArch ==
"x86_64h")
463 Triple.setArchName(MArch);
465 return Triple.getTriple();
467 case llvm::Triple::aarch64: {
469 if (!Triple.isOSBinFormatMachO())
475 Triple.setArchName(
"arm64");
476 return Triple.getTriple();
478 case llvm::Triple::arm:
479 case llvm::Triple::armeb:
480 case llvm::Triple::thumb:
481 case llvm::Triple::thumbeb: {
484 bool IsBigEndian =
getTriple().getArch() == llvm::Triple::armeb ||
485 getTriple().getArch() == llvm::Triple::thumbeb;
489 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
490 options::OPT_mbig_endian)) {
491 IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian);
497 StringRef MCPU, MArch;
498 if (
const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
499 MCPU = A->getValue();
500 if (
const Arg *A = Args.getLastArg(options::OPT_march_EQ))
501 MArch = A->getValue();
503 Triple.isOSBinFormatMachO()
508 bool IsMProfile = ARM::parseArchProfile(Suffix) == ARM::PK_M;
509 bool ThumbDefault = IsMProfile || (ARM::parseArchVersion(Suffix) == 7 &&
514 std::string ArchName;
522 if ((InputType != types::TY_PP_Asm && Args.hasFlag(options::OPT_mthumb,
523 options::OPT_mno_thumb, ThumbDefault)) || IsMProfile ||
526 ArchName =
"thumbeb";
530 Triple.setArchName(ArchName + Suffix.str());
532 return Triple.getTriple();
543 ArgStringList &CC1Args)
const {
548 const ArgList &DriverArgs, ArgStringList &CC1Args,
554 llvm::opt::ArgStringList &CmdArgs)
const {
561 const ArgList &Args)
const {
562 const Arg* A = Args.getLastArg(options::OPT_rtlib_EQ);
563 StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_RTLIB;
566 if (LibName ==
"compiler-rt")
568 else if (LibName ==
"libgcc")
570 else if (LibName ==
"platform")
574 getDriver().
Diag(diag::err_drv_invalid_rtlib_name) << A->getAsString(Args);
580 const Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
581 StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_CXX_STDLIB;
584 if (LibName ==
"libc++")
586 else if (LibName ==
"libstdc++")
588 else if (LibName ==
"platform")
592 getDriver().
Diag(diag::err_drv_invalid_stdlib_name) << A->getAsString(Args);
599 ArgStringList &CC1Args,
601 CC1Args.push_back(
"-internal-isystem");
602 CC1Args.push_back(DriverArgs.MakeArgString(Path));
614 ArgStringList &CC1Args,
616 CC1Args.push_back(
"-internal-externc-isystem");
617 CC1Args.push_back(DriverArgs.MakeArgString(Path));
621 ArgStringList &CC1Args,
623 if (llvm::sys::fs::exists(Path))
629 ArgStringList &CC1Args,
631 for (StringRef Path : Paths) {
632 CC1Args.push_back(
"-internal-isystem");
633 CC1Args.push_back(DriverArgs.MakeArgString(Path));
638 ArgStringList &CC1Args)
const {
648 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
652 ArgStringList &CmdArgs)
const {
657 CmdArgs.push_back(
"-lc++");
661 CmdArgs.push_back(
"-lstdc++");
667 ArgStringList &CmdArgs)
const {
669 if(LibPath.length() > 0)
670 CmdArgs.push_back(Args.MakeArgString(StringRef(
"-L") + LibPath));
674 ArgStringList &CmdArgs)
const {
675 CmdArgs.push_back(
"-lcc_kext");
679 ArgStringList &CmdArgs)
const {
685 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
686 options::OPT_funsafe_math_optimizations,
687 options::OPT_fno_unsafe_math_optimizations);
689 if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
690 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
695 if (Path ==
"crtfastmath.o")
698 CmdArgs.push_back(Args.MakeArgString(Path));
705 using namespace SanitizerKind;
706 SanitizerMask Res = (Undefined & ~Vptr & ~Function) | (CFI & ~CFIICall) |
707 CFICastStrict | UnsignedIntegerOverflow |
Nullability |
709 if (
getTriple().getArch() == llvm::Triple::x86 ||
710 getTriple().getArch() == llvm::Triple::x86_64 ||
711 getTriple().getArch() == llvm::Triple::arm ||
712 getTriple().getArch() == llvm::Triple::aarch64 ||
713 getTriple().getArch() == llvm::Triple::wasm32 ||
714 getTriple().getArch() == llvm::Triple::wasm64)
720 ArgStringList &CC1Args)
const {}
723 ArgStringList &CC1Args)
const {}
732 unsigned Build = 0, Factor = 1;
733 for (; Version > 10000; Version = Version / 10, Factor = Factor * 10)
734 Build = Build + (Version % 10) * Factor;
735 return VersionTuple(Version / 100, Version % 100, Build);
740 const llvm::opt::ArgList &Args)
const {
741 const Arg *MSCVersion = Args.getLastArg(options::OPT_fmsc_version);
742 const Arg *MSCompatibilityVersion =
743 Args.getLastArg(options::OPT_fms_compatibility_version);
745 if (MSCVersion && MSCompatibilityVersion) {
747 D->
Diag(diag::err_drv_argument_not_allowed_with)
748 << MSCVersion->getAsString(Args)
749 << MSCompatibilityVersion->getAsString(Args);
753 if (MSCompatibilityVersion) {
755 if (MSVT.
tryParse(MSCompatibilityVersion->getValue())) {
757 D->
Diag(diag::err_drv_invalid_value)
758 << MSCompatibilityVersion->getAsString(Args)
759 << MSCompatibilityVersion->getValue();
766 unsigned Version = 0;
767 if (StringRef(MSCVersion->getValue()).getAsInteger(10, Version)) {
769 D->
Diag(diag::err_drv_invalid_value)
770 << MSCVersion->getAsString(Args) << MSCVersion->getValue();
Represents a version number in the form major[.minor[.subminor[.build]]].
std::string GetProgramPath(StringRef Name, const ToolChain &TC) const
GetProgramPath - Lookup Name in the list of program search paths.
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 ...
The virtual file system interface.
ActionClass getKind() const
bool isOptimizationLevelFast(const llvm::opt::ArgList &Args)
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
vfs::FileSystem & getVFS() const
'gnustep' is the modern non-fragile GNUstep runtime.
bool tryParse(StringRef string)
Try to parse the given string as a version number.
The basic abstraction for the target Objective-C runtime.
Defines the virtual file system interface vfs::FileSystem.
std::string GetFilePath(StringRef Name, const ToolChain &TC) const
GetFilePath - Lookup Name in the list of file search paths.
ID lookupTypeForExtension(llvm::StringRef Ext)
lookupTypeForExtension - Lookup the type to use for the file extension Ext.