ToolChain.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. //===--- ToolChain.cpp - Collections of tools for one platform ------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "Tools.h"
  10. #include "clang/Basic/ObjCRuntime.h"
  11. #include "clang/Driver/Action.h"
  12. #include "clang/Driver/Driver.h"
  13. #include "clang/Driver/DriverDiagnostic.h"
  14. #include "clang/Driver/Options.h"
  15. #include "clang/Driver/SanitizerArgs.h"
  16. #include "clang/Driver/ToolChain.h"
  17. #include "llvm/ADT/SmallString.h"
  18. #include "llvm/ADT/StringSwitch.h"
  19. #include "llvm/Option/Arg.h"
  20. #include "llvm/Option/ArgList.h"
  21. #include "llvm/Option/Option.h"
  22. #include "llvm/Support/ErrorHandling.h"
  23. #include "llvm/Support/FileSystem.h"
  24. using namespace clang::driver;
  25. using namespace clang;
  26. using namespace llvm::opt;
  27. static llvm::opt::Arg *GetRTTIArgument(const ArgList &Args) {
  28. return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext,
  29. options::OPT_fno_rtti, options::OPT_frtti);
  30. }
  31. static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args,
  32. const llvm::Triple &Triple,
  33. const Arg *CachedRTTIArg) {
  34. // Explicit rtti/no-rtti args
  35. if (CachedRTTIArg) {
  36. if (CachedRTTIArg->getOption().matches(options::OPT_frtti))
  37. return ToolChain::RM_EnabledExplicitly;
  38. else
  39. return ToolChain::RM_DisabledExplicitly;
  40. }
  41. // -frtti is default, except for the PS4 CPU.
  42. if (!Triple.isPS4CPU())
  43. return ToolChain::RM_EnabledImplicitly;
  44. // On the PS4, turning on c++ exceptions turns on rtti.
  45. // We're assuming that, if we see -fexceptions, rtti gets turned on.
  46. Arg *Exceptions = Args.getLastArgNoClaim(
  47. options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions,
  48. options::OPT_fexceptions, options::OPT_fno_exceptions);
  49. if (Exceptions &&
  50. (Exceptions->getOption().matches(options::OPT_fexceptions) ||
  51. Exceptions->getOption().matches(options::OPT_fcxx_exceptions)))
  52. return ToolChain::RM_EnabledImplicitly;
  53. return ToolChain::RM_DisabledImplicitly;
  54. }
  55. ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
  56. const ArgList &Args)
  57. : D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)),
  58. CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)) {
  59. if (Arg *A = Args.getLastArg(options::OPT_mthread_model))
  60. if (!isThreadModelSupported(A->getValue()))
  61. D.Diag(diag::err_drv_invalid_thread_model_for_target)
  62. << A->getValue() << A->getAsString(Args);
  63. }
  64. ToolChain::~ToolChain() {
  65. }
  66. const Driver &ToolChain::getDriver() const {
  67. return D;
  68. }
  69. bool ToolChain::useIntegratedAs() const {
  70. return Args.hasFlag(options::OPT_fintegrated_as,
  71. options::OPT_fno_integrated_as,
  72. IsIntegratedAssemblerDefault());
  73. }
  74. const SanitizerArgs& ToolChain::getSanitizerArgs() const {
  75. if (!SanitizerArguments.get())
  76. SanitizerArguments.reset(new SanitizerArgs(*this, Args));
  77. return *SanitizerArguments.get();
  78. }
  79. StringRef ToolChain::getDefaultUniversalArchName() const {
  80. // In universal driver terms, the arch name accepted by -arch isn't exactly
  81. // the same as the ones that appear in the triple. Roughly speaking, this is
  82. // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the
  83. // only interesting special case is powerpc.
  84. switch (Triple.getArch()) {
  85. case llvm::Triple::ppc:
  86. return "ppc";
  87. case llvm::Triple::ppc64:
  88. return "ppc64";
  89. case llvm::Triple::ppc64le:
  90. return "ppc64le";
  91. default:
  92. return Triple.getArchName();
  93. }
  94. }
  95. bool ToolChain::IsUnwindTablesDefault() const {
  96. return false;
  97. }
  98. Tool *ToolChain::getClang() const {
  99. if (!Clang)
  100. Clang.reset(new tools::Clang(*this));
  101. return Clang.get();
  102. }
  103. Tool *ToolChain::buildAssembler() const {
  104. return new tools::ClangAs(*this);
  105. }
  106. Tool *ToolChain::buildLinker() const {
  107. llvm_unreachable("Linking is not supported by this toolchain");
  108. }
  109. Tool *ToolChain::getAssemble() const {
  110. if (!Assemble)
  111. Assemble.reset(buildAssembler());
  112. return Assemble.get();
  113. }
  114. Tool *ToolChain::getClangAs() const {
  115. if (!Assemble)
  116. Assemble.reset(new tools::ClangAs(*this));
  117. return Assemble.get();
  118. }
  119. Tool *ToolChain::getLink() const {
  120. if (!Link)
  121. Link.reset(buildLinker());
  122. return Link.get();
  123. }
  124. Tool *ToolChain::getTool(Action::ActionClass AC) const {
  125. switch (AC) {
  126. case Action::AssembleJobClass:
  127. return getAssemble();
  128. case Action::LinkJobClass:
  129. return getLink();
  130. case Action::InputClass:
  131. case Action::BindArchClass:
  132. case Action::CudaDeviceClass:
  133. case Action::CudaHostClass:
  134. case Action::LipoJobClass:
  135. case Action::DsymutilJobClass:
  136. case Action::VerifyDebugInfoJobClass:
  137. llvm_unreachable("Invalid tool kind.");
  138. case Action::CompileJobClass:
  139. case Action::PrecompileJobClass:
  140. case Action::PreprocessJobClass:
  141. case Action::AnalyzeJobClass:
  142. case Action::MigrateJobClass:
  143. case Action::VerifyPCHJobClass:
  144. case Action::BackendJobClass:
  145. return getClang();
  146. }
  147. llvm_unreachable("Invalid tool kind.");
  148. }
  149. Tool *ToolChain::SelectTool(const JobAction &JA) const {
  150. if (getDriver().ShouldUseClangCompiler(JA))
  151. return getClang();
  152. Action::ActionClass AC = JA.getKind();
  153. if (AC == Action::AssembleJobClass && useIntegratedAs())
  154. return getClangAs();
  155. return getTool(AC);
  156. }
  157. std::string ToolChain::GetFilePath(const char *Name) const {
  158. return D.GetFilePath(Name, *this);
  159. }
  160. std::string ToolChain::GetProgramPath(const char *Name) const {
  161. return D.GetProgramPath(Name, *this);
  162. }
  163. std::string ToolChain::GetLinkerPath() const {
  164. if (Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
  165. StringRef Suffix = A->getValue();
  166. // If we're passed -fuse-ld= with no argument, or with the argument ld,
  167. // then use whatever the default system linker is.
  168. if (Suffix.empty() || Suffix == "ld")
  169. return GetProgramPath("ld");
  170. llvm::SmallString<8> LinkerName("ld.");
  171. LinkerName.append(Suffix);
  172. std::string LinkerPath(GetProgramPath(LinkerName.c_str()));
  173. if (llvm::sys::fs::exists(LinkerPath))
  174. return LinkerPath;
  175. getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
  176. return "";
  177. }
  178. return GetProgramPath("ld");
  179. }
  180. types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
  181. return types::lookupTypeForExtension(Ext);
  182. }
  183. bool ToolChain::HasNativeLLVMSupport() const {
  184. return false;
  185. }
  186. bool ToolChain::isCrossCompiling() const {
  187. llvm::Triple HostTriple(LLVM_HOST_TRIPLE);
  188. switch (HostTriple.getArch()) {
  189. // The A32/T32/T16 instruction sets are not separate architectures in this
  190. // context.
  191. case llvm::Triple::arm:
  192. case llvm::Triple::armeb:
  193. case llvm::Triple::thumb:
  194. case llvm::Triple::thumbeb:
  195. return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb &&
  196. getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb;
  197. default:
  198. return HostTriple.getArch() != getArch();
  199. }
  200. }
  201. ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
  202. return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
  203. VersionTuple());
  204. }
  205. bool ToolChain::isThreadModelSupported(const StringRef Model) const {
  206. if (Model == "single") {
  207. // FIXME: 'single' is only supported on ARM so far.
  208. return Triple.getArch() == llvm::Triple::arm ||
  209. Triple.getArch() == llvm::Triple::armeb ||
  210. Triple.getArch() == llvm::Triple::thumb ||
  211. Triple.getArch() == llvm::Triple::thumbeb;
  212. } else if (Model == "posix")
  213. return true;
  214. return false;
  215. }
  216. std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
  217. types::ID InputType) const {
  218. switch (getTriple().getArch()) {
  219. default:
  220. return getTripleString();
  221. case llvm::Triple::x86_64: {
  222. llvm::Triple Triple = getTriple();
  223. if (!Triple.isOSBinFormatMachO())
  224. return getTripleString();
  225. if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
  226. // x86_64h goes in the triple. Other -march options just use the
  227. // vanilla triple we already have.
  228. StringRef MArch = A->getValue();
  229. if (MArch == "x86_64h")
  230. Triple.setArchName(MArch);
  231. }
  232. return Triple.getTriple();
  233. }
  234. case llvm::Triple::aarch64: {
  235. llvm::Triple Triple = getTriple();
  236. if (!Triple.isOSBinFormatMachO())
  237. return getTripleString();
  238. // FIXME: older versions of ld64 expect the "arm64" component in the actual
  239. // triple string and query it to determine whether an LTO file can be
  240. // handled. Remove this when we don't care any more.
  241. Triple.setArchName("arm64");
  242. return Triple.getTriple();
  243. }
  244. case llvm::Triple::arm:
  245. case llvm::Triple::armeb:
  246. case llvm::Triple::thumb:
  247. case llvm::Triple::thumbeb: {
  248. // FIXME: Factor into subclasses.
  249. llvm::Triple Triple = getTriple();
  250. bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb ||
  251. getTriple().getArch() == llvm::Triple::thumbeb;
  252. // Handle pseudo-target flags '-mlittle-endian'/'-EL' and
  253. // '-mbig-endian'/'-EB'.
  254. if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
  255. options::OPT_mbig_endian)) {
  256. IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian);
  257. }
  258. // Thumb2 is the default for V7 on Darwin.
  259. //
  260. // FIXME: Thumb should just be another -target-feaure, not in the triple.
  261. StringRef MCPU, MArch;
  262. if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
  263. MCPU = A->getValue();
  264. if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
  265. MArch = A->getValue();
  266. std::string CPU = Triple.isOSBinFormatMachO()
  267. ? tools::arm::getARMCPUForMArch(MArch, Triple)
  268. : tools::arm::getARMTargetCPU(MCPU, MArch, Triple);
  269. StringRef Suffix =
  270. tools::arm::getLLVMArchSuffixForARM(CPU,
  271. tools::arm::getARMArch(MArch, Triple));
  272. bool ThumbDefault = Suffix.startswith("v6m") || Suffix.startswith("v7m") ||
  273. Suffix.startswith("v7em") ||
  274. (Suffix.startswith("v7") && getTriple().isOSBinFormatMachO());
  275. // FIXME: this is invalid for WindowsCE
  276. if (getTriple().isOSWindows())
  277. ThumbDefault = true;
  278. std::string ArchName;
  279. if (IsBigEndian)
  280. ArchName = "armeb";
  281. else
  282. ArchName = "arm";
  283. // Assembly files should start in ARM mode.
  284. if (InputType != types::TY_PP_Asm &&
  285. Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
  286. {
  287. if (IsBigEndian)
  288. ArchName = "thumbeb";
  289. else
  290. ArchName = "thumb";
  291. }
  292. Triple.setArchName(ArchName + Suffix.str());
  293. return Triple.getTriple();
  294. }
  295. }
  296. }
  297. std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
  298. types::ID InputType) const {
  299. return ComputeLLVMTriple(Args, InputType);
  300. }
  301. void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
  302. ArgStringList &CC1Args) const {
  303. // Each toolchain should provide the appropriate include flags.
  304. }
  305. void ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
  306. ArgStringList &CC1Args) const {
  307. }
  308. void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {}
  309. ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
  310. const ArgList &Args) const
  311. {
  312. if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
  313. StringRef Value = A->getValue();
  314. if (Value == "compiler-rt")
  315. return ToolChain::RLT_CompilerRT;
  316. if (Value == "libgcc")
  317. return ToolChain::RLT_Libgcc;
  318. getDriver().Diag(diag::err_drv_invalid_rtlib_name)
  319. << A->getAsString(Args);
  320. }
  321. return GetDefaultRuntimeLibType();
  322. }
  323. ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
  324. if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
  325. StringRef Value = A->getValue();
  326. if (Value == "libc++")
  327. return ToolChain::CST_Libcxx;
  328. if (Value == "libstdc++")
  329. return ToolChain::CST_Libstdcxx;
  330. getDriver().Diag(diag::err_drv_invalid_stdlib_name)
  331. << A->getAsString(Args);
  332. }
  333. return ToolChain::CST_Libstdcxx;
  334. }
  335. /// \brief Utility function to add a system include directory to CC1 arguments.
  336. /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
  337. ArgStringList &CC1Args,
  338. const Twine &Path) {
  339. CC1Args.push_back("-internal-isystem");
  340. CC1Args.push_back(DriverArgs.MakeArgString(Path));
  341. }
  342. /// \brief Utility function to add a system include directory with extern "C"
  343. /// semantics to CC1 arguments.
  344. ///
  345. /// Note that this should be used rarely, and only for directories that
  346. /// historically and for legacy reasons are treated as having implicit extern
  347. /// "C" semantics. These semantics are *ignored* by and large today, but its
  348. /// important to preserve the preprocessor changes resulting from the
  349. /// classification.
  350. /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
  351. ArgStringList &CC1Args,
  352. const Twine &Path) {
  353. CC1Args.push_back("-internal-externc-isystem");
  354. CC1Args.push_back(DriverArgs.MakeArgString(Path));
  355. }
  356. void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
  357. ArgStringList &CC1Args,
  358. const Twine &Path) {
  359. if (llvm::sys::fs::exists(Path))
  360. addExternCSystemInclude(DriverArgs, CC1Args, Path);
  361. }
  362. /// \brief Utility function to add a list of system include directories to CC1.
  363. /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
  364. ArgStringList &CC1Args,
  365. ArrayRef<StringRef> Paths) {
  366. for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end();
  367. I != E; ++I) {
  368. CC1Args.push_back("-internal-isystem");
  369. CC1Args.push_back(DriverArgs.MakeArgString(*I));
  370. }
  371. }
  372. void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
  373. ArgStringList &CC1Args) const {
  374. // Header search paths should be handled by each of the subclasses.
  375. // Historically, they have not been, and instead have been handled inside of
  376. // the CC1-layer frontend. As the logic is hoisted out, this generic function
  377. // will slowly stop being called.
  378. //
  379. // While it is being called, replicate a bit of a hack to propagate the
  380. // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
  381. // header search paths with it. Once all systems are overriding this
  382. // function, the CC1 flag and this line can be removed.
  383. DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
  384. }
  385. void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
  386. ArgStringList &CmdArgs) const {
  387. CXXStdlibType Type = GetCXXStdlibType(Args);
  388. switch (Type) {
  389. case ToolChain::CST_Libcxx:
  390. CmdArgs.push_back("-lc++");
  391. break;
  392. case ToolChain::CST_Libstdcxx:
  393. CmdArgs.push_back("-lstdc++");
  394. break;
  395. }
  396. }
  397. void ToolChain::AddCCKextLibArgs(const ArgList &Args,
  398. ArgStringList &CmdArgs) const {
  399. CmdArgs.push_back("-lcc_kext");
  400. }
  401. bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args,
  402. ArgStringList &CmdArgs) const {
  403. // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed
  404. // (to keep the linker options consistent with gcc and clang itself).
  405. if (!isOptimizationLevelFast(Args)) {
  406. // Check if -ffast-math or -funsafe-math.
  407. Arg *A =
  408. Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
  409. options::OPT_funsafe_math_optimizations,
  410. options::OPT_fno_unsafe_math_optimizations);
  411. if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
  412. A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
  413. return false;
  414. }
  415. // If crtfastmath.o exists add it to the arguments.
  416. std::string Path = GetFilePath("crtfastmath.o");
  417. if (Path == "crtfastmath.o") // Not found.
  418. return false;
  419. CmdArgs.push_back(Args.MakeArgString(Path));
  420. return true;
  421. }
  422. SanitizerMask ToolChain::getSupportedSanitizers() const {
  423. // Return sanitizers which don't require runtime support and are not
  424. // platform or architecture-dependent.
  425. using namespace SanitizerKind;
  426. return (Undefined & ~Vptr & ~Function) | CFI | CFICastStrict |
  427. UnsignedIntegerOverflow | LocalBounds;
  428. }