18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/Option/Arg.h"
21 #include "llvm/Option/ArgList.h"
22 #include "llvm/Option/Option.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/TargetRegistry.h"
26 #include "llvm/Support/TargetParser.h"
28 using namespace clang::driver;
29 using namespace clang::driver::tools;
30 using namespace clang;
32 using namespace llvm::opt;
35 return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext,
36 options::OPT_fno_rtti, options::OPT_frtti);
40 const llvm::Triple &Triple,
41 const Arg *CachedRTTIArg) {
44 if (CachedRTTIArg->getOption().matches(options::OPT_frtti))
51 if (!Triple.isPS4CPU())
56 Arg *Exceptions = Args.getLastArgNoClaim(
57 options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions,
58 options::OPT_fexceptions, options::OPT_fno_exceptions);
60 (Exceptions->getOption().matches(options::OPT_fexceptions) ||
61 Exceptions->getOption().matches(options::OPT_fcxx_exceptions)))
71 if (Arg *A = Args.getLastArg(options::OPT_mthread_model))
73 D.
Diag(diag::err_drv_invalid_thread_model_for_target)
74 << A->getValue() << A->getAsString(Args);
83 return Args.hasFlag(options::OPT_fintegrated_as,
84 options::OPT_fno_integrated_as,
89 if (!SanitizerArguments.get())
91 return *SanitizerArguments.get();
100 const DriverSuffix *FindDriverSuffix(StringRef ProgName) {
104 static const DriverSuffix DriverSuffixes[] = {
106 {
"clang++",
"--driver-mode=g++"},
107 {
"clang-c++",
"--driver-mode=g++"},
108 {
"clang-cc",
nullptr},
109 {
"clang-cpp",
"--driver-mode=cpp"},
110 {
"clang-g++",
"--driver-mode=g++"},
111 {
"clang-gcc",
nullptr},
112 {
"clang-cl",
"--driver-mode=cl"},
114 {
"cpp",
"--driver-mode=cpp"},
115 {
"cl",
"--driver-mode=cl"},
116 {
"++",
"--driver-mode=g++"},
119 for (
size_t i = 0; i < llvm::array_lengthof(DriverSuffixes); ++i)
120 if (ProgName.endswith(DriverSuffixes[i].Suffix))
121 return &DriverSuffixes[i];
127 std::string normalizeProgramName(llvm::StringRef Argv0) {
128 std::string ProgName = llvm::sys::path::stem(Argv0);
131 std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(), ::tolower);
136 const DriverSuffix *parseDriverSuffix(StringRef ProgName) {
144 const DriverSuffix *DS = FindDriverSuffix(ProgName);
149 ProgName = ProgName.rtrim(
"0123456789.");
150 DS = FindDriverSuffix(ProgName);
156 ProgName = ProgName.slice(0, ProgName.rfind(
'-'));
157 DS = FindDriverSuffix(ProgName);
163 std::pair<std::string, std::string>
165 std::string ProgName = normalizeProgramName(PN);
166 const DriverSuffix *DS = parseDriverSuffix(ProgName);
168 return std::make_pair(
"",
"");
169 std::string ModeFlag = DS->ModeFlag ==
nullptr ?
"" : DS->ModeFlag;
171 std::string::size_type LastComponent =
172 ProgName.rfind(
'-', ProgName.size() - strlen(DS->Suffix));
173 if (LastComponent == std::string::npos)
174 return std::make_pair(
"", ModeFlag);
177 StringRef Prefix(ProgName);
178 Prefix = Prefix.slice(0, LastComponent);
179 std::string IgnoredError;
181 if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) {
184 return std::make_pair(Target, ModeFlag);
192 switch (Triple.getArch()) {
193 case llvm::Triple::ppc:
195 case llvm::Triple::ppc64:
197 case llvm::Triple::ppc64le:
200 return Triple.getArchName();
208 Tool *ToolChain::getClang()
const {
219 llvm_unreachable(
"Linking is not supported by this toolchain");
222 Tool *ToolChain::getAssemble()
const {
225 return Assemble.get();
228 Tool *ToolChain::getClangAs()
const {
231 return Assemble.get();
234 Tool *ToolChain::getLink()
const {
243 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 Suffix = A->getValue();
348 if (Suffix.empty() || Suffix ==
"ld")
352 LinkerName.append(Suffix);
355 if (llvm::sys::fs::exists(LinkerPath))
358 getDriver().
Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
374 llvm::Triple HostTriple(LLVM_HOST_TRIPLE);
375 switch (HostTriple.getArch()) {
378 case llvm::Triple::arm:
379 case llvm::Triple::armeb:
380 case llvm::Triple::thumb:
381 case llvm::Triple::thumbeb:
382 return getArch() != llvm::Triple::arm &&
getArch() != llvm::Triple::thumb &&
383 getArch() != llvm::Triple::armeb &&
getArch() != llvm::Triple::thumbeb;
385 return HostTriple.getArch() !=
getArch();
395 if (Model ==
"single") {
397 return Triple.getArch() == llvm::Triple::arm ||
398 Triple.getArch() == llvm::Triple::armeb ||
399 Triple.getArch() == llvm::Triple::thumb ||
400 Triple.getArch() == llvm::Triple::thumbeb ||
401 Triple.getArch() == llvm::Triple::wasm32 ||
402 Triple.getArch() == llvm::Triple::wasm64;
403 }
else if (Model ==
"posix")
415 case llvm::Triple::x86_64: {
417 if (!Triple.isOSBinFormatMachO())
420 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
423 StringRef MArch = A->getValue();
424 if (MArch ==
"x86_64h")
425 Triple.setArchName(MArch);
427 return Triple.getTriple();
429 case llvm::Triple::aarch64: {
431 if (!Triple.isOSBinFormatMachO())
437 Triple.setArchName(
"arm64");
438 return Triple.getTriple();
440 case llvm::Triple::arm:
441 case llvm::Triple::armeb:
442 case llvm::Triple::thumb:
443 case llvm::Triple::thumbeb: {
446 bool IsBigEndian =
getTriple().getArch() == llvm::Triple::armeb ||
447 getTriple().getArch() == llvm::Triple::thumbeb;
451 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
452 options::OPT_mbig_endian)) {
453 IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian);
459 StringRef MCPU, MArch;
460 if (
const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
461 MCPU = A->getValue();
462 if (
const Arg *A = Args.getLastArg(options::OPT_march_EQ))
463 MArch = A->getValue();
465 Triple.isOSBinFormatMachO()
470 bool IsMProfile = ARM::parseArchProfile(Suffix) == ARM::PK_M;
471 bool ThumbDefault = IsMProfile || (ARM::parseArchVersion(Suffix) == 7 &&
476 std::string ArchName;
483 if ((InputType != types::TY_PP_Asm && Args.hasFlag(options::OPT_mthumb,
484 options::OPT_mno_thumb, ThumbDefault)) || IsMProfile) {
486 ArchName =
"thumbeb";
490 Triple.setArchName(ArchName + Suffix.str());
492 return Triple.getTriple();
503 ArgStringList &CC1Args)
const {
508 ArgStringList &CC1Args)
const {
514 llvm::opt::ArgStringList &CmdArgs)
const {
522 const ArgList &Args)
const {
523 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
524 StringRef
Value = A->getValue();
525 if (Value ==
"compiler-rt")
527 if (Value ==
"libgcc")
530 << A->getAsString(Args);
537 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
538 StringRef
Value = A->getValue();
539 if (Value ==
"libc++")
541 if (Value ==
"libstdc++")
544 << A->getAsString(Args);
552 ArgStringList &CC1Args,
554 CC1Args.push_back(
"-internal-isystem");
555 CC1Args.push_back(DriverArgs.MakeArgString(Path));
567 ArgStringList &CC1Args,
569 CC1Args.push_back(
"-internal-externc-isystem");
570 CC1Args.push_back(DriverArgs.MakeArgString(Path));
574 ArgStringList &CC1Args,
576 if (llvm::sys::fs::exists(Path))
582 ArgStringList &CC1Args,
584 for (StringRef Path : Paths) {
585 CC1Args.push_back(
"-internal-isystem");
586 CC1Args.push_back(DriverArgs.MakeArgString(Path));
591 ArgStringList &CC1Args)
const {
601 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
605 ArgStringList &CmdArgs)
const {
610 CmdArgs.push_back(
"-lc++");
614 CmdArgs.push_back(
"-lstdc++");
620 ArgStringList &CmdArgs)
const {
622 if(LibPath.length() > 0)
623 CmdArgs.push_back(Args.MakeArgString(StringRef(
"-L") + LibPath));
627 ArgStringList &CmdArgs)
const {
628 CmdArgs.push_back(
"-lcc_kext");
632 ArgStringList &CmdArgs)
const {
638 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
639 options::OPT_funsafe_math_optimizations,
640 options::OPT_fno_unsafe_math_optimizations);
642 if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
643 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
648 if (Path ==
"crtfastmath.o")
651 CmdArgs.push_back(Args.MakeArgString(Path));
658 using namespace SanitizerKind;
659 SanitizerMask Res = (Undefined & ~Vptr & ~Function) | (CFI & ~CFIICall) |
660 CFICastStrict | UnsignedIntegerOverflow | LocalBounds;
661 if (
getTriple().getArch() == llvm::Triple::x86 ||
662 getTriple().getArch() == llvm::Triple::x86_64)
668 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.