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