ToolChain.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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/StringSwitch.h"
  18. #include "llvm/Option/Arg.h"
  19. #include "llvm/Option/ArgList.h"
  20. #include "llvm/Option/Option.h"
  21. #include "llvm/Support/ErrorHandling.h"
  22. #include "llvm/Support/FileSystem.h"
  23. using namespace clang::driver;
  24. using namespace clang;
  25. using namespace llvm::opt;
  26. ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
  27. const ArgList &A)
  28. : D(D), Triple(T), Args(A) {
  29. }
  30. ToolChain::~ToolChain() {
  31. }
  32. const Driver &ToolChain::getDriver() const {
  33. return D;
  34. }
  35. bool ToolChain::useIntegratedAs() const {
  36. return Args.hasFlag(options::OPT_fintegrated_as,
  37. options::OPT_fno_integrated_as,
  38. IsIntegratedAssemblerDefault());
  39. }
  40. const SanitizerArgs& ToolChain::getSanitizerArgs() const {
  41. if (!SanitizerArguments.get())
  42. SanitizerArguments.reset(new SanitizerArgs(*this, Args));
  43. return *SanitizerArguments.get();
  44. }
  45. std::string ToolChain::getDefaultUniversalArchName() const {
  46. // In universal driver terms, the arch name accepted by -arch isn't exactly
  47. // the same as the ones that appear in the triple. Roughly speaking, this is
  48. // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the
  49. // only interesting special case is powerpc.
  50. switch (Triple.getArch()) {
  51. case llvm::Triple::ppc:
  52. return "ppc";
  53. case llvm::Triple::ppc64:
  54. return "ppc64";
  55. case llvm::Triple::ppc64le:
  56. return "ppc64le";
  57. default:
  58. return Triple.getArchName();
  59. }
  60. }
  61. bool ToolChain::IsUnwindTablesDefault() const {
  62. return false;
  63. }
  64. Tool *ToolChain::getClang() const {
  65. if (!Clang)
  66. Clang.reset(new tools::Clang(*this));
  67. return Clang.get();
  68. }
  69. Tool *ToolChain::buildAssembler() const {
  70. return new tools::ClangAs(*this);
  71. }
  72. Tool *ToolChain::buildLinker() const {
  73. llvm_unreachable("Linking is not supported by this toolchain");
  74. }
  75. Tool *ToolChain::getAssemble() const {
  76. if (!Assemble)
  77. Assemble.reset(buildAssembler());
  78. return Assemble.get();
  79. }
  80. Tool *ToolChain::getClangAs() const {
  81. if (!Assemble)
  82. Assemble.reset(new tools::ClangAs(*this));
  83. return Assemble.get();
  84. }
  85. Tool *ToolChain::getLink() const {
  86. if (!Link)
  87. Link.reset(buildLinker());
  88. return Link.get();
  89. }
  90. Tool *ToolChain::getTool(Action::ActionClass AC) const {
  91. switch (AC) {
  92. case Action::AssembleJobClass:
  93. return getAssemble();
  94. case Action::LinkJobClass:
  95. return getLink();
  96. case Action::InputClass:
  97. case Action::BindArchClass:
  98. case Action::LipoJobClass:
  99. case Action::DsymutilJobClass:
  100. case Action::VerifyDebugInfoJobClass:
  101. llvm_unreachable("Invalid tool kind.");
  102. case Action::CompileJobClass:
  103. case Action::PrecompileJobClass:
  104. case Action::PreprocessJobClass:
  105. case Action::AnalyzeJobClass:
  106. case Action::MigrateJobClass:
  107. case Action::VerifyPCHJobClass:
  108. return getClang();
  109. }
  110. llvm_unreachable("Invalid tool kind.");
  111. }
  112. Tool *ToolChain::SelectTool(const JobAction &JA) const {
  113. if (getDriver().ShouldUseClangCompiler(JA))
  114. return getClang();
  115. Action::ActionClass AC = JA.getKind();
  116. if (AC == Action::AssembleJobClass && useIntegratedAs())
  117. return getClangAs();
  118. return getTool(AC);
  119. }
  120. std::string ToolChain::GetFilePath(const char *Name) const {
  121. return D.GetFilePath(Name, *this);
  122. }
  123. std::string ToolChain::GetProgramPath(const char *Name) const {
  124. return D.GetProgramPath(Name, *this);
  125. }
  126. types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
  127. return types::lookupTypeForExtension(Ext);
  128. }
  129. bool ToolChain::HasNativeLLVMSupport() const {
  130. return false;
  131. }
  132. bool ToolChain::isCrossCompiling() const {
  133. llvm::Triple HostTriple(LLVM_HOST_TRIPLE);
  134. switch (HostTriple.getArch()) {
  135. // The A32/T32/T16 instruction sets are not separate architectures in this
  136. // context.
  137. case llvm::Triple::arm:
  138. case llvm::Triple::armeb:
  139. case llvm::Triple::thumb:
  140. case llvm::Triple::thumbeb:
  141. return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb &&
  142. getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb;
  143. default:
  144. return HostTriple.getArch() != getArch();
  145. }
  146. }
  147. ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
  148. return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
  149. VersionTuple());
  150. }
  151. std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
  152. types::ID InputType) const {
  153. switch (getTriple().getArch()) {
  154. default:
  155. return getTripleString();
  156. case llvm::Triple::x86_64: {
  157. llvm::Triple Triple = getTriple();
  158. if (!Triple.isOSBinFormatMachO())
  159. return getTripleString();
  160. if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
  161. // x86_64h goes in the triple. Other -march options just use the
  162. // vanilla triple we already have.
  163. StringRef MArch = A->getValue();
  164. if (MArch == "x86_64h")
  165. Triple.setArchName(MArch);
  166. }
  167. return Triple.getTriple();
  168. }
  169. case llvm::Triple::arm:
  170. case llvm::Triple::armeb:
  171. case llvm::Triple::thumb:
  172. case llvm::Triple::thumbeb: {
  173. // FIXME: Factor into subclasses.
  174. llvm::Triple Triple = getTriple();
  175. bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb ||
  176. getTriple().getArch() == llvm::Triple::thumbeb;
  177. // Thumb2 is the default for V7 on Darwin.
  178. //
  179. // FIXME: Thumb should just be another -target-feaure, not in the triple.
  180. StringRef Suffix = Triple.isOSBinFormatMachO()
  181. ? tools::arm::getLLVMArchSuffixForARM(tools::arm::getARMCPUForMArch(Args, Triple))
  182. : tools::arm::getLLVMArchSuffixForARM(tools::arm::getARMTargetCPU(Args, Triple));
  183. bool ThumbDefault = Suffix.startswith("v6m") || Suffix.startswith("v7m") ||
  184. Suffix.startswith("v7em") ||
  185. (Suffix.startswith("v7") && getTriple().isOSBinFormatMachO());
  186. std::string ArchName;
  187. if (IsBigEndian)
  188. ArchName = "armeb";
  189. else
  190. ArchName = "arm";
  191. // Assembly files should start in ARM mode.
  192. if (InputType != types::TY_PP_Asm &&
  193. Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
  194. {
  195. if (IsBigEndian)
  196. ArchName = "thumbeb";
  197. else
  198. ArchName = "thumb";
  199. }
  200. Triple.setArchName(ArchName + Suffix.str());
  201. return Triple.getTriple();
  202. }
  203. }
  204. }
  205. std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
  206. types::ID InputType) const {
  207. return ComputeLLVMTriple(Args, InputType);
  208. }
  209. void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
  210. ArgStringList &CC1Args) const {
  211. // Each toolchain should provide the appropriate include flags.
  212. }
  213. void ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
  214. ArgStringList &CC1Args) const {
  215. }
  216. ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
  217. const ArgList &Args) const
  218. {
  219. if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
  220. StringRef Value = A->getValue();
  221. if (Value == "compiler-rt")
  222. return ToolChain::RLT_CompilerRT;
  223. if (Value == "libgcc")
  224. return ToolChain::RLT_Libgcc;
  225. getDriver().Diag(diag::err_drv_invalid_rtlib_name)
  226. << A->getAsString(Args);
  227. }
  228. return GetDefaultRuntimeLibType();
  229. }
  230. ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
  231. if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
  232. StringRef Value = A->getValue();
  233. if (Value == "libc++")
  234. return ToolChain::CST_Libcxx;
  235. if (Value == "libstdc++")
  236. return ToolChain::CST_Libstdcxx;
  237. getDriver().Diag(diag::err_drv_invalid_stdlib_name)
  238. << A->getAsString(Args);
  239. }
  240. return ToolChain::CST_Libstdcxx;
  241. }
  242. /// \brief Utility function to add a system include directory to CC1 arguments.
  243. /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
  244. ArgStringList &CC1Args,
  245. const Twine &Path) {
  246. CC1Args.push_back("-internal-isystem");
  247. CC1Args.push_back(DriverArgs.MakeArgString(Path));
  248. }
  249. /// \brief Utility function to add a system include directory with extern "C"
  250. /// semantics to CC1 arguments.
  251. ///
  252. /// Note that this should be used rarely, and only for directories that
  253. /// historically and for legacy reasons are treated as having implicit extern
  254. /// "C" semantics. These semantics are *ignored* by and large today, but its
  255. /// important to preserve the preprocessor changes resulting from the
  256. /// classification.
  257. /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
  258. ArgStringList &CC1Args,
  259. const Twine &Path) {
  260. CC1Args.push_back("-internal-externc-isystem");
  261. CC1Args.push_back(DriverArgs.MakeArgString(Path));
  262. }
  263. void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
  264. ArgStringList &CC1Args,
  265. const Twine &Path) {
  266. if (llvm::sys::fs::exists(Path))
  267. addExternCSystemInclude(DriverArgs, CC1Args, Path);
  268. }
  269. /// \brief Utility function to add a list of system include directories to CC1.
  270. /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
  271. ArgStringList &CC1Args,
  272. ArrayRef<StringRef> Paths) {
  273. for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end();
  274. I != E; ++I) {
  275. CC1Args.push_back("-internal-isystem");
  276. CC1Args.push_back(DriverArgs.MakeArgString(*I));
  277. }
  278. }
  279. void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
  280. ArgStringList &CC1Args) const {
  281. // Header search paths should be handled by each of the subclasses.
  282. // Historically, they have not been, and instead have been handled inside of
  283. // the CC1-layer frontend. As the logic is hoisted out, this generic function
  284. // will slowly stop being called.
  285. //
  286. // While it is being called, replicate a bit of a hack to propagate the
  287. // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
  288. // header search paths with it. Once all systems are overriding this
  289. // function, the CC1 flag and this line can be removed.
  290. DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
  291. }
  292. void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
  293. ArgStringList &CmdArgs) const {
  294. CXXStdlibType Type = GetCXXStdlibType(Args);
  295. switch (Type) {
  296. case ToolChain::CST_Libcxx:
  297. CmdArgs.push_back("-lc++");
  298. break;
  299. case ToolChain::CST_Libstdcxx:
  300. CmdArgs.push_back("-lstdc++");
  301. break;
  302. }
  303. }
  304. void ToolChain::AddCCKextLibArgs(const ArgList &Args,
  305. ArgStringList &CmdArgs) const {
  306. CmdArgs.push_back("-lcc_kext");
  307. }
  308. bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args,
  309. ArgStringList &CmdArgs) const {
  310. // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed
  311. // (to keep the linker options consistent with gcc and clang itself).
  312. if (!isOptimizationLevelFast(Args)) {
  313. // Check if -ffast-math or -funsafe-math.
  314. Arg *A =
  315. Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
  316. options::OPT_funsafe_math_optimizations,
  317. options::OPT_fno_unsafe_math_optimizations);
  318. if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
  319. A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
  320. return false;
  321. }
  322. // If crtfastmath.o exists add it to the arguments.
  323. std::string Path = GetFilePath("crtfastmath.o");
  324. if (Path == "crtfastmath.o") // Not found.
  325. return false;
  326. CmdArgs.push_back(Args.MakeArgString(Path));
  327. return true;
  328. }