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 case Action::BackendJobClass: 133 return getClang(); 134 } 135 136 llvm_unreachable("Invalid tool kind."); 137 } 138 139 Tool *ToolChain::SelectTool(const JobAction &JA) const { 140 if (getDriver().ShouldUseClangCompiler(JA)) 141 return getClang(); 142 Action::ActionClass AC = JA.getKind(); 143 if (AC == Action::AssembleJobClass && useIntegratedAs()) 144 return getClangAs(); 145 return getTool(AC); 146 } 147 148 std::string ToolChain::GetFilePath(const char *Name) const { 149 return D.GetFilePath(Name, *this); 150 151 } 152 153 std::string ToolChain::GetProgramPath(const char *Name) const { 154 return D.GetProgramPath(Name, *this); 155 } 156 157 std::string ToolChain::GetLinkerPath() const { 158 if (Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) { 159 StringRef Suffix = A->getValue(); 160 161 // If we're passed -fuse-ld= with no argument, or with the argument ld, 162 // then use whatever the default system linker is. 163 if (Suffix.empty() || Suffix == "ld") 164 return GetProgramPath("ld"); 165 166 llvm::SmallString<8> LinkerName("ld."); 167 LinkerName.append(Suffix); 168 169 std::string LinkerPath(GetProgramPath(LinkerName.c_str())); 170 if (llvm::sys::fs::exists(LinkerPath)) 171 return LinkerPath; 172 173 getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args); 174 return ""; 175 } 176 177 return GetProgramPath("ld"); 178 } 179 180 181 types::ID ToolChain::LookupTypeForExtension(const char *Ext) const { 182 return types::lookupTypeForExtension(Ext); 183 } 184 185 bool ToolChain::HasNativeLLVMSupport() const { 186 return false; 187 } 188 189 bool ToolChain::isCrossCompiling() const { 190 llvm::Triple HostTriple(LLVM_HOST_TRIPLE); 191 switch (HostTriple.getArch()) { 192 // The A32/T32/T16 instruction sets are not separate architectures in this 193 // context. 194 case llvm::Triple::arm: 195 case llvm::Triple::armeb: 196 case llvm::Triple::thumb: 197 case llvm::Triple::thumbeb: 198 return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb && 199 getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb; 200 default: 201 return HostTriple.getArch() != getArch(); 202 } 203 } 204 205 ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const { 206 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC, 207 VersionTuple()); 208 } 209 210 bool ToolChain::isThreadModelSupported(const StringRef Model) const { 211 if (Model == "single") { 212 // FIXME: 'single' is only supported on ARM so far. 213 return Triple.getArch() == llvm::Triple::arm || 214 Triple.getArch() == llvm::Triple::armeb || 215 Triple.getArch() == llvm::Triple::thumb || 216 Triple.getArch() == llvm::Triple::thumbeb; 217 } else if (Model == "posix") 218 return true; 219 220 return false; 221 } 222 223 std::string ToolChain::ComputeLLVMTriple(const ArgList &Args, 224 types::ID InputType) const { 225 switch (getTriple().getArch()) { 226 default: 227 return getTripleString(); 228 229 case llvm::Triple::x86_64: { 230 llvm::Triple Triple = getTriple(); 231 if (!Triple.isOSBinFormatMachO()) 232 return getTripleString(); 233 234 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) { 235 // x86_64h goes in the triple. Other -march options just use the 236 // vanilla triple we already have. 237 StringRef MArch = A->getValue(); 238 if (MArch == "x86_64h") 239 Triple.setArchName(MArch); 240 } 241 return Triple.getTriple(); 242 } 243 case llvm::Triple::aarch64: { 244 llvm::Triple Triple = getTriple(); 245 if (!Triple.isOSBinFormatMachO()) 246 return getTripleString(); 247 248 // FIXME: older versions of ld64 expect the "arm64" component in the actual 249 // triple string and query it to determine whether an LTO file can be 250 // handled. Remove this when we don't care any more. 251 Triple.setArchName("arm64"); 252 return Triple.getTriple(); 253 } 254 case llvm::Triple::arm: 255 case llvm::Triple::armeb: 256 case llvm::Triple::thumb: 257 case llvm::Triple::thumbeb: { 258 // FIXME: Factor into subclasses. 259 llvm::Triple Triple = getTriple(); 260 bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb || 261 getTriple().getArch() == llvm::Triple::thumbeb; 262 263 // Handle pseudo-target flags '-mlittle-endian'/'-EL' and 264 // '-mbig-endian'/'-EB'. 265 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian, 266 options::OPT_mbig_endian)) { 267 if (A->getOption().matches(options::OPT_mlittle_endian)) 268 IsBigEndian = false; 269 else 270 IsBigEndian = true; 271 } 272 273 // Thumb2 is the default for V7 on Darwin. 274 // 275 // FIXME: Thumb should just be another -target-feaure, not in the triple. 276 StringRef Suffix = Triple.isOSBinFormatMachO() 277 ? tools::arm::getLLVMArchSuffixForARM(tools::arm::getARMCPUForMArch(Args, Triple)) 278 : tools::arm::getLLVMArchSuffixForARM(tools::arm::getARMTargetCPU(Args, Triple)); 279 bool ThumbDefault = Suffix.startswith("v6m") || Suffix.startswith("v7m") || 280 Suffix.startswith("v7em") || 281 (Suffix.startswith("v7") && getTriple().isOSBinFormatMachO()); 282 // FIXME: this is invalid for WindowsCE 283 if (getTriple().isOSWindows()) 284 ThumbDefault = true; 285 std::string ArchName; 286 if (IsBigEndian) 287 ArchName = "armeb"; 288 else 289 ArchName = "arm"; 290 291 // Assembly files should start in ARM mode. 292 if (InputType != types::TY_PP_Asm && 293 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault)) 294 { 295 if (IsBigEndian) 296 ArchName = "thumbeb"; 297 else 298 ArchName = "thumb"; 299 } 300 Triple.setArchName(ArchName + Suffix.str()); 301 302 return Triple.getTriple(); 303 } 304 } 305 } 306 307 std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args, 308 types::ID InputType) const { 309 return ComputeLLVMTriple(Args, InputType); 310 } 311 312 void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 313 ArgStringList &CC1Args) const { 314 // Each toolchain should provide the appropriate include flags. 315 } 316 317 void ToolChain::addClangTargetOptions(const ArgList &DriverArgs, 318 ArgStringList &CC1Args) const { 319 } 320 321 void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {} 322 323 ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType( 324 const ArgList &Args) const 325 { 326 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) { 327 StringRef Value = A->getValue(); 328 if (Value == "compiler-rt") 329 return ToolChain::RLT_CompilerRT; 330 if (Value == "libgcc") 331 return ToolChain::RLT_Libgcc; 332 getDriver().Diag(diag::err_drv_invalid_rtlib_name) 333 << A->getAsString(Args); 334 } 335 336 return GetDefaultRuntimeLibType(); 337 } 338 339 ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{ 340 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { 341 StringRef Value = A->getValue(); 342 if (Value == "libc++") 343 return ToolChain::CST_Libcxx; 344 if (Value == "libstdc++") 345 return ToolChain::CST_Libstdcxx; 346 getDriver().Diag(diag::err_drv_invalid_stdlib_name) 347 << A->getAsString(Args); 348 } 349 350 return ToolChain::CST_Libstdcxx; 351 } 352 353 /// \brief Utility function to add a system include directory to CC1 arguments. 354 /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs, 355 ArgStringList &CC1Args, 356 const Twine &Path) { 357 CC1Args.push_back("-internal-isystem"); 358 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 359 } 360 361 /// \brief Utility function to add a system include directory with extern "C" 362 /// semantics to CC1 arguments. 363 /// 364 /// Note that this should be used rarely, and only for directories that 365 /// historically and for legacy reasons are treated as having implicit extern 366 /// "C" semantics. These semantics are *ignored* by and large today, but its 367 /// important to preserve the preprocessor changes resulting from the 368 /// classification. 369 /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs, 370 ArgStringList &CC1Args, 371 const Twine &Path) { 372 CC1Args.push_back("-internal-externc-isystem"); 373 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 374 } 375 376 void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs, 377 ArgStringList &CC1Args, 378 const Twine &Path) { 379 if (llvm::sys::fs::exists(Path)) 380 addExternCSystemInclude(DriverArgs, CC1Args, Path); 381 } 382 383 /// \brief Utility function to add a list of system include directories to CC1. 384 /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs, 385 ArgStringList &CC1Args, 386 ArrayRef<StringRef> Paths) { 387 for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end(); 388 I != E; ++I) { 389 CC1Args.push_back("-internal-isystem"); 390 CC1Args.push_back(DriverArgs.MakeArgString(*I)); 391 } 392 } 393 394 void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 395 ArgStringList &CC1Args) const { 396 // Header search paths should be handled by each of the subclasses. 397 // Historically, they have not been, and instead have been handled inside of 398 // the CC1-layer frontend. As the logic is hoisted out, this generic function 399 // will slowly stop being called. 400 // 401 // While it is being called, replicate a bit of a hack to propagate the 402 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++ 403 // header search paths with it. Once all systems are overriding this 404 // function, the CC1 flag and this line can be removed. 405 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ); 406 } 407 408 void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args, 409 ArgStringList &CmdArgs) const { 410 CXXStdlibType Type = GetCXXStdlibType(Args); 411 412 switch (Type) { 413 case ToolChain::CST_Libcxx: 414 CmdArgs.push_back("-lc++"); 415 break; 416 417 case ToolChain::CST_Libstdcxx: 418 CmdArgs.push_back("-lstdc++"); 419 break; 420 } 421 } 422 423 void ToolChain::AddCCKextLibArgs(const ArgList &Args, 424 ArgStringList &CmdArgs) const { 425 CmdArgs.push_back("-lcc_kext"); 426 } 427 428 bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args, 429 ArgStringList &CmdArgs) const { 430 // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed 431 // (to keep the linker options consistent with gcc and clang itself). 432 if (!isOptimizationLevelFast(Args)) { 433 // Check if -ffast-math or -funsafe-math. 434 Arg *A = 435 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math, 436 options::OPT_funsafe_math_optimizations, 437 options::OPT_fno_unsafe_math_optimizations); 438 439 if (!A || A->getOption().getID() == options::OPT_fno_fast_math || 440 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations) 441 return false; 442 } 443 // If crtfastmath.o exists add it to the arguments. 444 std::string Path = GetFilePath("crtfastmath.o"); 445 if (Path == "crtfastmath.o") // Not found. 446 return false; 447 448 CmdArgs.push_back(Args.MakeArgString(Path)); 449 return true; 450 } 451