12 #include "clang/Config/config.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/Option/Arg.h"
22 #include "llvm/Option/ArgList.h"
23 #include "llvm/Option/Option.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/TargetRegistry.h"
27 #include "llvm/Support/TargetParser.h"
29 using namespace clang::driver;
30 using namespace clang::driver::tools;
31 using namespace clang;
33 using namespace llvm::opt;
36 return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext,
37 options::OPT_fno_rtti, options::OPT_frtti);
41 const llvm::Triple &Triple,
42 const Arg *CachedRTTIArg) {
45 if (CachedRTTIArg->getOption().matches(options::OPT_frtti))
52 if (!Triple.isPS4CPU())
57 Arg *Exceptions = Args.getLastArgNoClaim(
58 options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions,
59 options::OPT_fexceptions, options::OPT_fno_exceptions);
61 (Exceptions->getOption().matches(options::OPT_fexceptions) ||
62 Exceptions->getOption().matches(options::OPT_fcxx_exceptions)))
72 if (Arg *A = Args.getLastArg(options::OPT_mthread_model))
74 D.
Diag(diag::err_drv_invalid_thread_model_for_target)
75 << A->getValue() << A->getAsString(Args);
84 return Args.hasFlag(options::OPT_fintegrated_as,
85 options::OPT_fno_integrated_as,
90 if (!SanitizerArguments.get())
92 return *SanitizerArguments.get();
101 const DriverSuffix *FindDriverSuffix(StringRef ProgName) {
105 static const DriverSuffix DriverSuffixes[] = {
107 {
"clang++",
"--driver-mode=g++"},
108 {
"clang-c++",
"--driver-mode=g++"},
109 {
"clang-cc",
nullptr},
110 {
"clang-cpp",
"--driver-mode=cpp"},
111 {
"clang-g++",
"--driver-mode=g++"},
112 {
"clang-gcc",
nullptr},
113 {
"clang-cl",
"--driver-mode=cl"},
115 {
"cpp",
"--driver-mode=cpp"},
116 {
"cl",
"--driver-mode=cl"},
117 {
"++",
"--driver-mode=g++"},
120 for (
size_t i = 0; i < llvm::array_lengthof(DriverSuffixes); ++i)
121 if (ProgName.endswith(DriverSuffixes[i].Suffix))
122 return &DriverSuffixes[i];
128 std::string normalizeProgramName(llvm::StringRef Argv0) {
129 std::string ProgName = llvm::sys::path::stem(Argv0);
132 std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(), ::tolower);
137 const DriverSuffix *parseDriverSuffix(StringRef ProgName) {
145 const DriverSuffix *DS = FindDriverSuffix(ProgName);
150 ProgName = ProgName.rtrim(
"0123456789.");
151 DS = FindDriverSuffix(ProgName);
157 ProgName = ProgName.slice(0, ProgName.rfind(
'-'));
158 DS = FindDriverSuffix(ProgName);
164 std::pair<std::string, std::string>
166 std::string ProgName = normalizeProgramName(PN);
167 const DriverSuffix *DS = parseDriverSuffix(ProgName);
169 return std::make_pair(
"",
"");
170 std::string ModeFlag = DS->ModeFlag ==
nullptr ?
"" : DS->ModeFlag;
172 std::string::size_type LastComponent =
173 ProgName.rfind(
'-', ProgName.size() - strlen(DS->Suffix));
174 if (LastComponent == std::string::npos)
175 return std::make_pair(
"", ModeFlag);
178 StringRef Prefix(ProgName);
179 Prefix = Prefix.slice(0, LastComponent);
180 std::string IgnoredError;
182 if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) {
185 return std::make_pair(Target, ModeFlag);
193 switch (Triple.getArch()) {
194 case llvm::Triple::ppc:
196 case llvm::Triple::ppc64:
198 case llvm::Triple::ppc64le:
201 return Triple.getArchName();
209 Tool *ToolChain::getClang()
const {
220 llvm_unreachable(
"Linking is not supported by this toolchain");
223 Tool *ToolChain::getAssemble()
const {
226 return Assemble.get();
229 Tool *ToolChain::getClangAs()
const {
232 return Assemble.get();
235 Tool *ToolChain::getLink()
const {
244 return getAssemble();
255 llvm_unreachable(
"Invalid tool kind.");
267 llvm_unreachable(
"Invalid tool kind.");
271 const ArgList &Args) {
272 const llvm::Triple &Triple = TC.
getTriple();
273 bool IsWindows = Triple.isOSWindows();
275 if (Triple.isWindowsMSVCEnvironment() && TC.
getArch() == llvm::Triple::x86)
278 if (TC.
getArch() == llvm::Triple::arm || TC.
getArch() == llvm::Triple::armeb)
289 const char *Env = TT.isAndroid() ?
"-android" :
"";
290 bool IsITANMSVCWindows =
291 TT.isWindowsMSVCEnvironment() || TT.isWindowsItaniumEnvironment();
294 const char *Prefix = IsITANMSVCWindows ?
"" :
"lib";
295 const char *Suffix = Shared ? (Triple.isOSWindows() ?
".dll" :
".so")
296 : (IsITANMSVCWindows ?
".lib" :
".a");
299 StringRef OSLibName = Triple.isOSFreeBSD() ?
"freebsd" :
getOS();
300 llvm::sys::path::append(Path,
"lib", OSLibName);
301 llvm::sys::path::append(Path, Prefix + Twine(
"clang_rt.") + Component +
"-" +
302 Arch + Env + Suffix);
309 return Args.MakeArgString(
getCompilerRT(Args, Component, Shared));
313 if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
315 Args.hasArg(options::OPT_fprofile_generate) ||
316 Args.hasArg(options::OPT_fprofile_generate_EQ) ||
317 Args.hasArg(options::OPT_fprofile_instr_generate) ||
318 Args.hasArg(options::OPT_fprofile_instr_generate_EQ) ||
319 Args.hasArg(options::OPT_fcreate_profile) ||
320 Args.hasArg(options::OPT_coverage))
327 if (
getDriver().ShouldUseClangCompiler(JA))
return getClang();
343 if (Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
344 StringRef UseLinker = A->getValue();
346 if (llvm::sys::path::is_absolute(UseLinker)) {
349 if (llvm::sys::fs::exists(UseLinker))
354 if (UseLinker.empty() || UseLinker ==
"ld")
358 LinkerName.append(UseLinker);
361 if (llvm::sys::fs::exists(LinkerPath))
365 getDriver().
Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
381 llvm::Triple HostTriple(LLVM_HOST_TRIPLE);
382 switch (HostTriple.getArch()) {
385 case llvm::Triple::arm:
386 case llvm::Triple::armeb:
387 case llvm::Triple::thumb:
388 case llvm::Triple::thumbeb:
389 return getArch() != llvm::Triple::arm &&
getArch() != llvm::Triple::thumb &&
390 getArch() != llvm::Triple::armeb &&
getArch() != llvm::Triple::thumbeb;
392 return HostTriple.getArch() !=
getArch();
402 if (Model ==
"single") {
404 return Triple.getArch() == llvm::Triple::arm ||
405 Triple.getArch() == llvm::Triple::armeb ||
406 Triple.getArch() == llvm::Triple::thumb ||
407 Triple.getArch() == llvm::Triple::thumbeb ||
408 Triple.getArch() == llvm::Triple::wasm32 ||
409 Triple.getArch() == llvm::Triple::wasm64;
410 }
else if (Model ==
"posix")
422 case llvm::Triple::x86_64: {
424 if (!Triple.isOSBinFormatMachO())
427 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
430 StringRef MArch = A->getValue();
431 if (MArch ==
"x86_64h")
432 Triple.setArchName(MArch);
434 return Triple.getTriple();
436 case llvm::Triple::aarch64: {
438 if (!Triple.isOSBinFormatMachO())
444 Triple.setArchName(
"arm64");
445 return Triple.getTriple();
447 case llvm::Triple::arm:
448 case llvm::Triple::armeb:
449 case llvm::Triple::thumb:
450 case llvm::Triple::thumbeb: {
453 bool IsBigEndian =
getTriple().getArch() == llvm::Triple::armeb ||
454 getTriple().getArch() == llvm::Triple::thumbeb;
458 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
459 options::OPT_mbig_endian)) {
460 IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian);
466 StringRef MCPU, MArch;
467 if (
const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
468 MCPU = A->getValue();
469 if (
const Arg *A = Args.getLastArg(options::OPT_march_EQ))
470 MArch = A->getValue();
472 Triple.isOSBinFormatMachO()
477 bool IsMProfile = ARM::parseArchProfile(Suffix) == ARM::PK_M;
478 bool ThumbDefault = IsMProfile || (ARM::parseArchVersion(Suffix) == 7 &&
483 std::string ArchName;
490 if ((InputType != types::TY_PP_Asm && Args.hasFlag(options::OPT_mthumb,
491 options::OPT_mno_thumb, ThumbDefault)) || IsMProfile) {
493 ArchName =
"thumbeb";
497 Triple.setArchName(ArchName + Suffix.str());
499 return Triple.getTriple();
510 ArgStringList &CC1Args)
const {
515 ArgStringList &CC1Args)
const {
521 llvm::opt::ArgStringList &CmdArgs)
const {
528 const ArgList &Args)
const {
529 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
530 StringRef
Value = A->getValue();
531 if (Value ==
"compiler-rt")
533 if (Value ==
"libgcc")
536 << A->getAsString(Args);
544 if (Name ==
"libc++")
546 else if (Name ==
"libstdc++")
556 bool HasValidType =
false;
557 bool ForcePlatformDefault =
false;
559 const Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
561 StringRef
Value = A->getValue();
565 if (Value ==
"platform")
566 ForcePlatformDefault =
true;
567 else if (!HasValidType)
569 << A->getAsString(Args);
572 if (!HasValidType && (ForcePlatformDefault ||
581 ArgStringList &CC1Args,
583 CC1Args.push_back(
"-internal-isystem");
584 CC1Args.push_back(DriverArgs.MakeArgString(Path));
596 ArgStringList &CC1Args,
598 CC1Args.push_back(
"-internal-externc-isystem");
599 CC1Args.push_back(DriverArgs.MakeArgString(Path));
603 ArgStringList &CC1Args,
605 if (llvm::sys::fs::exists(Path))
611 ArgStringList &CC1Args,
613 for (StringRef Path : Paths) {
614 CC1Args.push_back(
"-internal-isystem");
615 CC1Args.push_back(DriverArgs.MakeArgString(Path));
620 ArgStringList &CC1Args)
const {
630 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
634 ArgStringList &CmdArgs)
const {
639 CmdArgs.push_back(
"-lc++");
643 CmdArgs.push_back(
"-lstdc++");
649 ArgStringList &CmdArgs)
const {
651 if(LibPath.length() > 0)
652 CmdArgs.push_back(Args.MakeArgString(StringRef(
"-L") + LibPath));
656 ArgStringList &CmdArgs)
const {
657 CmdArgs.push_back(
"-lcc_kext");
661 ArgStringList &CmdArgs)
const {
667 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
668 options::OPT_funsafe_math_optimizations,
669 options::OPT_fno_unsafe_math_optimizations);
671 if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
672 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
677 if (Path ==
"crtfastmath.o")
680 CmdArgs.push_back(Args.MakeArgString(Path));
687 using namespace SanitizerKind;
688 SanitizerMask Res = (Undefined & ~Vptr & ~Function) | (CFI & ~CFIICall) |
689 CFICastStrict | UnsignedIntegerOverflow | LocalBounds;
690 if (
getTriple().getArch() == llvm::Triple::x86 ||
691 getTriple().getArch() == llvm::Triple::x86_64)
697 ArgStringList &CC1Args)
const {}
700 ArgStringList &CC1Args)
const {}
ID lookupTypeForExtension(const char *Ext)
lookupTypeForExtension - Lookup the type to use for the file extension Ext.
Represents a version number in the form major[.minor[.subminor[.build]]].
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.
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
ActionClass getKind() const
std::string GetFilePath(const char *Name, const ToolChain &TC) const
GetFilePath - Lookup Name in the list of file search paths.
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.
std::string GetProgramPath(const char *Name, const ToolChain &TC) const
GetProgramPath - Lookup Name in the list of program search paths.
The basic abstraction for the target Objective-C runtime.