Driver.cpp revision 206084
1193326Sed//===--- Driver.cpp - Clang GCC Compatible Driver -----------------------*-===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9193326Sed 10193326Sed#include "clang/Driver/Driver.h" 11193326Sed 12193326Sed#include "clang/Driver/Action.h" 13193326Sed#include "clang/Driver/Arg.h" 14193326Sed#include "clang/Driver/ArgList.h" 15193326Sed#include "clang/Driver/Compilation.h" 16193326Sed#include "clang/Driver/DriverDiagnostic.h" 17193326Sed#include "clang/Driver/HostInfo.h" 18193326Sed#include "clang/Driver/Job.h" 19199512Srdivacky#include "clang/Driver/OptTable.h" 20193326Sed#include "clang/Driver/Option.h" 21193326Sed#include "clang/Driver/Options.h" 22193326Sed#include "clang/Driver/Tool.h" 23193326Sed#include "clang/Driver/ToolChain.h" 24193326Sed#include "clang/Driver/Types.h" 25193326Sed 26193326Sed#include "clang/Basic/Version.h" 27193326Sed 28193326Sed#include "llvm/ADT/StringSet.h" 29202879Srdivacky#include "llvm/ADT/OwningPtr.h" 30193326Sed#include "llvm/Support/PrettyStackTrace.h" 31193326Sed#include "llvm/Support/raw_ostream.h" 32193326Sed#include "llvm/System/Path.h" 33193326Sed#include "llvm/System/Program.h" 34193326Sed 35193326Sed#include "InputInfo.h" 36193326Sed 37193326Sed#include <map> 38193326Sed 39193326Sedusing namespace clang::driver; 40193326Sedusing namespace clang; 41193326Sed 42198092Srdivacky// Used to set values for "production" clang, for releases. 43198092Srdivacky// #define USE_PRODUCTION_CLANG 44198092Srdivacky 45200583SrdivackyDriver::Driver(llvm::StringRef _Name, llvm::StringRef _Dir, 46200583Srdivacky llvm::StringRef _DefaultHostTriple, 47200583Srdivacky llvm::StringRef _DefaultImageName, 48206084Srdivacky bool IsProduction, bool CXXIsProduction, 49206084Srdivacky Diagnostic &_Diags) 50199512Srdivacky : Opts(createDriverOptTable()), Diags(_Diags), 51193326Sed Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple), 52193326Sed DefaultImageName(_DefaultImageName), 53204643Srdivacky DriverTitle("clang \"gcc-compatible\" driver"), 54193326Sed Host(0), 55205408Srdivacky CCCGenericGCCName("gcc"), CCPrintOptionsFilename(0), CCCIsCXX(false), 56205408Srdivacky CCCEcho(false), CCCPrintBindings(false), CCPrintOptions(false), 57205408Srdivacky CheckInputsExist(true), CCCUseClang(true), CCCUseClangCXX(true), 58205408Srdivacky CCCUseClangCPP(true), CCCUsePCH(true), SuppressMissingInputWarning(false) { 59198092Srdivacky if (IsProduction) { 60198092Srdivacky // In a "production" build, only use clang on architectures we expect to 61198092Srdivacky // work, and don't use clang C++. 62198092Srdivacky // 63198092Srdivacky // During development its more convenient to always have the driver use 64198092Srdivacky // clang, but we don't want users to be confused when things don't work, or 65198092Srdivacky // to file bugs for things we don't support. 66198092Srdivacky CCCClangArchs.insert(llvm::Triple::x86); 67198092Srdivacky CCCClangArchs.insert(llvm::Triple::x86_64); 68198092Srdivacky CCCClangArchs.insert(llvm::Triple::arm); 69198092Srdivacky 70206084Srdivacky if (!CXXIsProduction) 71206084Srdivacky CCCUseClangCXX = false; 72198092Srdivacky } 73202879Srdivacky 74202879Srdivacky // Compute the path to the resource directory. 75202879Srdivacky llvm::sys::Path P(Dir); 76202879Srdivacky P.eraseComponent(); // Remove /bin from foo/bin 77202879Srdivacky P.appendComponent("lib"); 78202879Srdivacky P.appendComponent("clang"); 79202879Srdivacky P.appendComponent(CLANG_VERSION_STRING); 80202879Srdivacky ResourceDir = P.str(); 81193326Sed} 82193326Sed 83193326SedDriver::~Driver() { 84193326Sed delete Opts; 85193326Sed delete Host; 86193326Sed} 87193326Sed 88198092SrdivackyInputArgList *Driver::ParseArgStrings(const char **ArgBegin, 89193326Sed const char **ArgEnd) { 90193326Sed llvm::PrettyStackTraceString CrashInfo("Command line argument parsing"); 91199512Srdivacky unsigned MissingArgIndex, MissingArgCount; 92199512Srdivacky InputArgList *Args = getOpts().ParseArgs(ArgBegin, ArgEnd, 93199512Srdivacky MissingArgIndex, MissingArgCount); 94198092Srdivacky 95199512Srdivacky // Check for missing argument error. 96199512Srdivacky if (MissingArgCount) 97199512Srdivacky Diag(clang::diag::err_drv_missing_argument) 98199512Srdivacky << Args->getArgString(MissingArgIndex) << MissingArgCount; 99193326Sed 100199512Srdivacky // Check for unsupported options. 101199512Srdivacky for (ArgList::const_iterator it = Args->begin(), ie = Args->end(); 102199512Srdivacky it != ie; ++it) { 103199512Srdivacky Arg *A = *it; 104193326Sed if (A->getOption().isUnsupported()) { 105193326Sed Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args); 106193326Sed continue; 107193326Sed } 108193326Sed } 109193326Sed 110193326Sed return Args; 111193326Sed} 112193326Sed 113193326SedCompilation *Driver::BuildCompilation(int argc, const char **argv) { 114193326Sed llvm::PrettyStackTraceString CrashInfo("Compilation construction"); 115193326Sed 116198092Srdivacky // FIXME: Handle environment options which effect driver behavior, somewhere 117198092Srdivacky // (client?). GCC_EXEC_PREFIX, COMPILER_PATH, LIBRARY_PATH, LPATH, 118198092Srdivacky // CC_PRINT_OPTIONS. 119193326Sed 120193326Sed // FIXME: What are we going to do with -V and -b? 121193326Sed 122198092Srdivacky // FIXME: This stuff needs to go into the Compilation, not the driver. 123193326Sed bool CCCPrintOptions = false, CCCPrintActions = false; 124193326Sed 125193326Sed const char **Start = argv + 1, **End = argv + argc; 126193326Sed const char *HostTriple = DefaultHostTriple.c_str(); 127193326Sed 128200583Srdivacky InputArgList *Args = ParseArgStrings(Start, End); 129200583Srdivacky 130200583Srdivacky // -no-canonical-prefixes is used very early in main. 131200583Srdivacky Args->ClaimAllArgs(options::OPT_no_canonical_prefixes); 132200583Srdivacky 133200583Srdivacky // Extract -ccc args. 134193326Sed // 135198092Srdivacky // FIXME: We need to figure out where this behavior should live. Most of it 136198092Srdivacky // should be outside in the client; the parts that aren't should have proper 137198092Srdivacky // options, either by introducing new ones or by overloading gcc ones like -V 138198092Srdivacky // or -b. 139200583Srdivacky CCCPrintOptions = Args->hasArg(options::OPT_ccc_print_options); 140200583Srdivacky CCCPrintActions = Args->hasArg(options::OPT_ccc_print_phases); 141200583Srdivacky CCCPrintBindings = Args->hasArg(options::OPT_ccc_print_bindings); 142200583Srdivacky CCCIsCXX = Args->hasArg(options::OPT_ccc_cxx) || CCCIsCXX; 143200583Srdivacky CCCEcho = Args->hasArg(options::OPT_ccc_echo); 144200583Srdivacky if (const Arg *A = Args->getLastArg(options::OPT_ccc_gcc_name)) 145200583Srdivacky CCCGenericGCCName = A->getValue(*Args); 146200583Srdivacky CCCUseClangCXX = Args->hasFlag(options::OPT_ccc_clang_cxx, 147200583Srdivacky options::OPT_ccc_no_clang_cxx, 148200583Srdivacky CCCUseClangCXX); 149200583Srdivacky CCCUsePCH = Args->hasFlag(options::OPT_ccc_pch_is_pch, 150200583Srdivacky options::OPT_ccc_pch_is_pth); 151200583Srdivacky CCCUseClang = !Args->hasArg(options::OPT_ccc_no_clang); 152200583Srdivacky CCCUseClangCPP = !Args->hasArg(options::OPT_ccc_no_clang_cpp); 153200583Srdivacky if (const Arg *A = Args->getLastArg(options::OPT_ccc_clang_archs)) { 154200583Srdivacky llvm::StringRef Cur = A->getValue(*Args); 155198092Srdivacky 156200583Srdivacky CCCClangArchs.clear(); 157200583Srdivacky while (!Cur.empty()) { 158200583Srdivacky std::pair<llvm::StringRef, llvm::StringRef> Split = Cur.split(','); 159198092Srdivacky 160200583Srdivacky if (!Split.first.empty()) { 161200583Srdivacky llvm::Triple::ArchType Arch = 162200583Srdivacky llvm::Triple(Split.first, "", "").getArch(); 163193326Sed 164203955Srdivacky if (Arch == llvm::Triple::UnknownArch) 165203955Srdivacky Diag(clang::diag::err_drv_invalid_arch_name) << Split.first; 166198092Srdivacky 167200583Srdivacky CCCClangArchs.insert(Arch); 168193326Sed } 169193326Sed 170200583Srdivacky Cur = Split.second; 171193326Sed } 172193326Sed } 173200583Srdivacky if (const Arg *A = Args->getLastArg(options::OPT_ccc_host_triple)) 174200583Srdivacky HostTriple = A->getValue(*Args); 175200583Srdivacky if (const Arg *A = Args->getLastArg(options::OPT_ccc_install_dir)) 176200583Srdivacky Dir = A->getValue(*Args); 177206084Srdivacky if (const Arg *A = Args->getLastArg(options::OPT_B)) 178206084Srdivacky PrefixDir = A->getValue(*Args); 179193326Sed 180193326Sed Host = GetHostInfo(HostTriple); 181193326Sed 182193326Sed // The compilation takes ownership of Args. 183198092Srdivacky Compilation *C = new Compilation(*this, *Host->CreateToolChain(*Args), Args); 184193326Sed 185193326Sed // FIXME: This behavior shouldn't be here. 186193326Sed if (CCCPrintOptions) { 187193326Sed PrintOptions(C->getArgs()); 188193326Sed return C; 189193326Sed } 190193326Sed 191193326Sed if (!HandleImmediateArgs(*C)) 192193326Sed return C; 193193326Sed 194198092Srdivacky // Construct the list of abstract actions to perform for this compilation. We 195198092Srdivacky // avoid passing a Compilation here simply to enforce the abstraction that 196198092Srdivacky // pipelining is not host or toolchain dependent (other than the driver driver 197198092Srdivacky // test). 198193326Sed if (Host->useDriverDriver()) 199193326Sed BuildUniversalActions(C->getArgs(), C->getActions()); 200193326Sed else 201193326Sed BuildActions(C->getArgs(), C->getActions()); 202193326Sed 203193326Sed if (CCCPrintActions) { 204193326Sed PrintActions(*C); 205193326Sed return C; 206193326Sed } 207193326Sed 208193326Sed BuildJobs(*C); 209193326Sed 210193326Sed return C; 211193326Sed} 212193326Sed 213195341Sedint Driver::ExecuteCompilation(const Compilation &C) const { 214195341Sed // Just print if -### was present. 215195341Sed if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { 216195341Sed C.PrintJob(llvm::errs(), C.getJobs(), "\n", true); 217195341Sed return 0; 218195341Sed } 219195341Sed 220195341Sed // If there were errors building the compilation, quit now. 221195341Sed if (getDiags().getNumErrors()) 222195341Sed return 1; 223195341Sed 224195341Sed const Command *FailingCommand = 0; 225195341Sed int Res = C.ExecuteJob(C.getJobs(), FailingCommand); 226198092Srdivacky 227195341Sed // Remove temp files. 228195341Sed C.CleanupFileList(C.getTempFiles()); 229195341Sed 230195341Sed // If the compilation failed, remove result files as well. 231195341Sed if (Res != 0 && !C.getArgs().hasArg(options::OPT_save_temps)) 232195341Sed C.CleanupFileList(C.getResultFiles(), true); 233195341Sed 234195341Sed // Print extra information about abnormal failures, if possible. 235195341Sed if (Res) { 236195341Sed // This is ad-hoc, but we don't want to be excessively noisy. If the result 237195341Sed // status was 1, assume the command failed normally. In particular, if it 238195341Sed // was the compiler then assume it gave a reasonable error code. Failures in 239195341Sed // other tools are less common, and they generally have worse diagnostics, 240195341Sed // so always print the diagnostic there. 241195341Sed const Action &Source = FailingCommand->getSource(); 242195341Sed bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) || 243195341Sed isa<PrecompileJobAction>(Source) || 244195341Sed isa<AnalyzeJobAction>(Source) || 245195341Sed isa<CompileJobAction>(Source)); 246195341Sed 247195341Sed if (!IsFriendlyTool || Res != 1) { 248195341Sed // FIXME: See FIXME above regarding result code interpretation. 249195341Sed if (Res < 0) 250198092Srdivacky Diag(clang::diag::err_drv_command_signalled) 251195341Sed << Source.getClassName() << -Res; 252195341Sed else 253198092Srdivacky Diag(clang::diag::err_drv_command_failed) 254195341Sed << Source.getClassName() << Res; 255195341Sed } 256195341Sed } 257195341Sed 258195341Sed return Res; 259195341Sed} 260195341Sed 261193326Sedvoid Driver::PrintOptions(const ArgList &Args) const { 262193326Sed unsigned i = 0; 263198092Srdivacky for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); 264193326Sed it != ie; ++it, ++i) { 265193326Sed Arg *A = *it; 266193326Sed llvm::errs() << "Option " << i << " - " 267193326Sed << "Name: \"" << A->getOption().getName() << "\", " 268193326Sed << "Values: {"; 269193326Sed for (unsigned j = 0; j < A->getNumValues(); ++j) { 270193326Sed if (j) 271193326Sed llvm::errs() << ", "; 272193326Sed llvm::errs() << '"' << A->getValue(Args, j) << '"'; 273193326Sed } 274193326Sed llvm::errs() << "}\n"; 275193326Sed } 276193326Sed} 277193326Sed 278200583Srdivacky// FIXME: Move -ccc options to real options in the .td file (or eliminate), and 279200583Srdivacky// then move to using OptTable::PrintHelp. 280193326Sedvoid Driver::PrintHelp(bool ShowHidden) const { 281204643Srdivacky getOpts().PrintHelp(llvm::outs(), Name.c_str(), DriverTitle.c_str(), 282204643Srdivacky ShowHidden); 283193326Sed} 284193326Sed 285198092Srdivackyvoid Driver::PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const { 286198092Srdivacky // FIXME: The following handlers should use a callback mechanism, we don't 287198092Srdivacky // know what the client would like to do. 288202879Srdivacky OS << getClangFullVersion() << '\n'; 289193326Sed const ToolChain &TC = C.getDefaultToolChain(); 290198092Srdivacky OS << "Target: " << TC.getTripleString() << '\n'; 291194613Sed 292194613Sed // Print the threading model. 293194613Sed // 294194613Sed // FIXME: Implement correctly. 295198092Srdivacky OS << "Thread model: " << "posix" << '\n'; 296193326Sed} 297193326Sed 298193326Sedbool Driver::HandleImmediateArgs(const Compilation &C) { 299198092Srdivacky // The order these options are handled in in gcc is all over the place, but we 300198092Srdivacky // don't expect inconsistencies w.r.t. that to matter in practice. 301193326Sed 302193326Sed if (C.getArgs().hasArg(options::OPT_dumpversion)) { 303193326Sed llvm::outs() << CLANG_VERSION_STRING "\n"; 304193326Sed return false; 305193326Sed } 306193326Sed 307198092Srdivacky if (C.getArgs().hasArg(options::OPT__help) || 308193326Sed C.getArgs().hasArg(options::OPT__help_hidden)) { 309193326Sed PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden)); 310193326Sed return false; 311193326Sed } 312193326Sed 313193326Sed if (C.getArgs().hasArg(options::OPT__version)) { 314198092Srdivacky // Follow gcc behavior and use stdout for --version and stderr for -v. 315198092Srdivacky PrintVersion(C, llvm::outs()); 316193326Sed return false; 317193326Sed } 318193326Sed 319198092Srdivacky if (C.getArgs().hasArg(options::OPT_v) || 320193326Sed C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { 321198092Srdivacky PrintVersion(C, llvm::errs()); 322193326Sed SuppressMissingInputWarning = true; 323193326Sed } 324193326Sed 325193326Sed const ToolChain &TC = C.getDefaultToolChain(); 326193326Sed if (C.getArgs().hasArg(options::OPT_print_search_dirs)) { 327193326Sed llvm::outs() << "programs: ="; 328193326Sed for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(), 329193326Sed ie = TC.getProgramPaths().end(); it != ie; ++it) { 330193326Sed if (it != TC.getProgramPaths().begin()) 331193326Sed llvm::outs() << ':'; 332193326Sed llvm::outs() << *it; 333193326Sed } 334193326Sed llvm::outs() << "\n"; 335193326Sed llvm::outs() << "libraries: ="; 336198092Srdivacky for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(), 337193326Sed ie = TC.getFilePaths().end(); it != ie; ++it) { 338193326Sed if (it != TC.getFilePaths().begin()) 339193326Sed llvm::outs() << ':'; 340193326Sed llvm::outs() << *it; 341193326Sed } 342193326Sed llvm::outs() << "\n"; 343193326Sed return false; 344193326Sed } 345193326Sed 346198092Srdivacky // FIXME: The following handlers should use a callback mechanism, we don't 347198092Srdivacky // know what the client would like to do. 348193326Sed if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) { 349198092Srdivacky llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC) << "\n"; 350193326Sed return false; 351193326Sed } 352193326Sed 353193326Sed if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) { 354198092Srdivacky llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC) << "\n"; 355193326Sed return false; 356193326Sed } 357193326Sed 358193326Sed if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) { 359198092Srdivacky llvm::outs() << GetFilePath("libgcc.a", TC) << "\n"; 360193326Sed return false; 361193326Sed } 362193326Sed 363194613Sed if (C.getArgs().hasArg(options::OPT_print_multi_lib)) { 364194613Sed // FIXME: We need tool chain support for this. 365194613Sed llvm::outs() << ".;\n"; 366194613Sed 367194613Sed switch (C.getDefaultToolChain().getTriple().getArch()) { 368194613Sed default: 369194613Sed break; 370198092Srdivacky 371194613Sed case llvm::Triple::x86_64: 372194613Sed llvm::outs() << "x86_64;@m64" << "\n"; 373194613Sed break; 374194613Sed 375194613Sed case llvm::Triple::ppc64: 376194613Sed llvm::outs() << "ppc64;@m64" << "\n"; 377194613Sed break; 378194613Sed } 379194613Sed return false; 380194613Sed } 381194613Sed 382194613Sed // FIXME: What is the difference between print-multi-directory and 383194613Sed // print-multi-os-directory? 384194613Sed if (C.getArgs().hasArg(options::OPT_print_multi_directory) || 385194613Sed C.getArgs().hasArg(options::OPT_print_multi_os_directory)) { 386194613Sed switch (C.getDefaultToolChain().getTriple().getArch()) { 387194613Sed default: 388194613Sed case llvm::Triple::x86: 389194613Sed case llvm::Triple::ppc: 390194613Sed llvm::outs() << "." << "\n"; 391194613Sed break; 392198092Srdivacky 393194613Sed case llvm::Triple::x86_64: 394194613Sed llvm::outs() << "x86_64" << "\n"; 395194613Sed break; 396194613Sed 397194613Sed case llvm::Triple::ppc64: 398194613Sed llvm::outs() << "ppc64" << "\n"; 399194613Sed break; 400194613Sed } 401194613Sed return false; 402194613Sed } 403194613Sed 404193326Sed return true; 405193326Sed} 406193326Sed 407198092Srdivackystatic unsigned PrintActions1(const Compilation &C, Action *A, 408193326Sed std::map<Action*, unsigned> &Ids) { 409193326Sed if (Ids.count(A)) 410193326Sed return Ids[A]; 411198092Srdivacky 412193326Sed std::string str; 413193326Sed llvm::raw_string_ostream os(str); 414198092Srdivacky 415193326Sed os << Action::getClassName(A->getKind()) << ", "; 416198092Srdivacky if (InputAction *IA = dyn_cast<InputAction>(A)) { 417193326Sed os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\""; 418193326Sed } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) { 419198092Srdivacky os << '"' << (BIA->getArchName() ? BIA->getArchName() : 420193326Sed C.getDefaultToolChain().getArchName()) << '"' 421193326Sed << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}"; 422193326Sed } else { 423193326Sed os << "{"; 424193326Sed for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) { 425193326Sed os << PrintActions1(C, *it, Ids); 426193326Sed ++it; 427193326Sed if (it != ie) 428193326Sed os << ", "; 429193326Sed } 430193326Sed os << "}"; 431193326Sed } 432193326Sed 433193326Sed unsigned Id = Ids.size(); 434193326Sed Ids[A] = Id; 435198092Srdivacky llvm::errs() << Id << ": " << os.str() << ", " 436193326Sed << types::getTypeName(A->getType()) << "\n"; 437193326Sed 438193326Sed return Id; 439193326Sed} 440193326Sed 441193326Sedvoid Driver::PrintActions(const Compilation &C) const { 442193326Sed std::map<Action*, unsigned> Ids; 443198092Srdivacky for (ActionList::const_iterator it = C.getActions().begin(), 444193326Sed ie = C.getActions().end(); it != ie; ++it) 445193326Sed PrintActions1(C, *it, Ids); 446193326Sed} 447193326Sed 448198092Srdivackyvoid Driver::BuildUniversalActions(const ArgList &Args, 449193326Sed ActionList &Actions) const { 450198092Srdivacky llvm::PrettyStackTraceString CrashInfo("Building universal build actions"); 451198092Srdivacky // Collect the list of architectures. Duplicates are allowed, but should only 452198092Srdivacky // be handled once (in the order seen). 453193326Sed llvm::StringSet<> ArchNames; 454193326Sed llvm::SmallVector<const char *, 4> Archs; 455198092Srdivacky for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); 456193326Sed it != ie; ++it) { 457193326Sed Arg *A = *it; 458193326Sed 459199512Srdivacky if (A->getOption().matches(options::OPT_arch)) { 460198092Srdivacky // Validate the option here; we don't save the type here because its 461198092Srdivacky // particular spelling may participate in other driver choices. 462198092Srdivacky llvm::Triple::ArchType Arch = 463198092Srdivacky llvm::Triple::getArchTypeForDarwinArchName(A->getValue(Args)); 464198092Srdivacky if (Arch == llvm::Triple::UnknownArch) { 465198092Srdivacky Diag(clang::diag::err_drv_invalid_arch_name) 466198092Srdivacky << A->getAsString(Args); 467198092Srdivacky continue; 468198092Srdivacky } 469193326Sed 470193326Sed A->claim(); 471198092Srdivacky if (ArchNames.insert(A->getValue(Args))) 472198092Srdivacky Archs.push_back(A->getValue(Args)); 473193326Sed } 474193326Sed } 475193326Sed 476198092Srdivacky // When there is no explicit arch for this platform, make sure we still bind 477198092Srdivacky // the architecture (to the default) so that -Xarch_ is handled correctly. 478193326Sed if (!Archs.size()) 479193326Sed Archs.push_back(0); 480193326Sed 481198092Srdivacky // FIXME: We killed off some others but these aren't yet detected in a 482198092Srdivacky // functional manner. If we added information to jobs about which "auxiliary" 483198092Srdivacky // files they wrote then we could detect the conflict these cause downstream. 484193326Sed if (Archs.size() > 1) { 485193326Sed // No recovery needed, the point of this is just to prevent 486193326Sed // overwriting the same files. 487193326Sed if (const Arg *A = Args.getLastArg(options::OPT_save_temps)) 488198092Srdivacky Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs) 489193326Sed << A->getAsString(Args); 490193326Sed } 491193326Sed 492193326Sed ActionList SingleActions; 493193326Sed BuildActions(Args, SingleActions); 494193326Sed 495198092Srdivacky // Add in arch binding and lipo (if necessary) for every top level action. 496193326Sed for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) { 497193326Sed Action *Act = SingleActions[i]; 498193326Sed 499198092Srdivacky // Make sure we can lipo this kind of output. If not (and it is an actual 500198092Srdivacky // output) then we disallow, since we can't create an output file with the 501198092Srdivacky // right name without overwriting it. We could remove this oddity by just 502198092Srdivacky // changing the output names to include the arch, which would also fix 503193326Sed // -save-temps. Compatibility wins for now. 504193326Sed 505193326Sed if (Archs.size() > 1 && !types::canLipoType(Act->getType())) 506193326Sed Diag(clang::diag::err_drv_invalid_output_with_multiple_archs) 507193326Sed << types::getTypeName(Act->getType()); 508193326Sed 509193326Sed ActionList Inputs; 510205219Srdivacky for (unsigned i = 0, e = Archs.size(); i != e; ++i) { 511193326Sed Inputs.push_back(new BindArchAction(Act, Archs[i])); 512205219Srdivacky if (i != 0) 513205219Srdivacky Inputs.back()->setOwnsInputs(false); 514205219Srdivacky } 515193326Sed 516198092Srdivacky // Lipo if necessary, we do it this way because we need to set the arch flag 517198092Srdivacky // so that -Xarch_ gets overwritten. 518193326Sed if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing) 519193326Sed Actions.append(Inputs.begin(), Inputs.end()); 520193326Sed else 521193326Sed Actions.push_back(new LipoJobAction(Inputs, Act->getType())); 522193326Sed } 523193326Sed} 524193326Sed 525193326Sedvoid Driver::BuildActions(const ArgList &Args, ActionList &Actions) const { 526193326Sed llvm::PrettyStackTraceString CrashInfo("Building compilation actions"); 527193326Sed // Start by constructing the list of inputs and their types. 528193326Sed 529198092Srdivacky // Track the current user specified (-x) input. We also explicitly track the 530198092Srdivacky // argument used to set the type; we only want to claim the type when we 531198092Srdivacky // actually use it, so we warn about unused -x arguments. 532193326Sed types::ID InputType = types::TY_Nothing; 533193326Sed Arg *InputTypeArg = 0; 534193326Sed 535193326Sed llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs; 536198092Srdivacky for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); 537193326Sed it != ie; ++it) { 538193326Sed Arg *A = *it; 539193326Sed 540193326Sed if (isa<InputOption>(A->getOption())) { 541193326Sed const char *Value = A->getValue(Args); 542193326Sed types::ID Ty = types::TY_INVALID; 543193326Sed 544193326Sed // Infer the input type if necessary. 545193326Sed if (InputType == types::TY_Nothing) { 546193326Sed // If there was an explicit arg for this, claim it. 547193326Sed if (InputTypeArg) 548193326Sed InputTypeArg->claim(); 549193326Sed 550193326Sed // stdin must be handled specially. 551193326Sed if (memcmp(Value, "-", 2) == 0) { 552198092Srdivacky // If running with -E, treat as a C input (this changes the builtin 553198092Srdivacky // macros, for example). This may be overridden by -ObjC below. 554193326Sed // 555198092Srdivacky // Otherwise emit an error but still use a valid type to avoid 556198092Srdivacky // spurious errors (e.g., no inputs). 557199512Srdivacky if (!Args.hasArgNoClaim(options::OPT_E)) 558193326Sed Diag(clang::diag::err_drv_unknown_stdin_type); 559193326Sed Ty = types::TY_C; 560193326Sed } else { 561198092Srdivacky // Otherwise lookup by extension, and fallback to ObjectType if not 562198092Srdivacky // found. We use a host hook here because Darwin at least has its own 563198092Srdivacky // idea of what .s is. 564193326Sed if (const char *Ext = strrchr(Value, '.')) 565193326Sed Ty = Host->lookupTypeForExtension(Ext + 1); 566193326Sed 567193326Sed if (Ty == types::TY_INVALID) 568193326Sed Ty = types::TY_Object; 569204643Srdivacky 570204643Srdivacky // If the driver is invoked as C++ compiler (like clang++ or c++) it 571204643Srdivacky // should autodetect some input files as C++ for g++ compatibility. 572204643Srdivacky if (CCCIsCXX) { 573204643Srdivacky types::ID OldTy = Ty; 574204643Srdivacky Ty = types::lookupCXXTypeForCType(Ty); 575204643Srdivacky 576204643Srdivacky if (Ty != OldTy) 577204643Srdivacky Diag(clang::diag::warn_drv_treating_input_as_cxx) 578204643Srdivacky << getTypeName(OldTy) << getTypeName(Ty); 579204643Srdivacky } 580193326Sed } 581193326Sed 582193326Sed // -ObjC and -ObjC++ override the default language, but only for "source 583193326Sed // files". We just treat everything that isn't a linker input as a 584193326Sed // source file. 585198092Srdivacky // 586193326Sed // FIXME: Clean this up if we move the phase sequence into the type. 587193326Sed if (Ty != types::TY_Object) { 588193326Sed if (Args.hasArg(options::OPT_ObjC)) 589193326Sed Ty = types::TY_ObjC; 590193326Sed else if (Args.hasArg(options::OPT_ObjCXX)) 591193326Sed Ty = types::TY_ObjCXX; 592193326Sed } 593193326Sed } else { 594193326Sed assert(InputTypeArg && "InputType set w/o InputTypeArg"); 595193326Sed InputTypeArg->claim(); 596193326Sed Ty = InputType; 597193326Sed } 598193326Sed 599203955Srdivacky // Check that the file exists, if enabled. 600203955Srdivacky if (CheckInputsExist && memcmp(Value, "-", 2) != 0 && 601203955Srdivacky !llvm::sys::Path(Value).exists()) 602193326Sed Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args); 603193326Sed else 604193326Sed Inputs.push_back(std::make_pair(Ty, A)); 605193326Sed 606193326Sed } else if (A->getOption().isLinkerInput()) { 607198092Srdivacky // Just treat as object type, we could make a special type for this if 608198092Srdivacky // necessary. 609193326Sed Inputs.push_back(std::make_pair(types::TY_Object, A)); 610193326Sed 611199512Srdivacky } else if (A->getOption().matches(options::OPT_x)) { 612198092Srdivacky InputTypeArg = A; 613193326Sed InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args)); 614193326Sed 615193326Sed // Follow gcc behavior and treat as linker input for invalid -x 616198092Srdivacky // options. Its not clear why we shouldn't just revert to unknown; but 617198092Srdivacky // this isn't very important, we might as well be bug comatible. 618193326Sed if (!InputType) { 619193326Sed Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args); 620193326Sed InputType = types::TY_Object; 621193326Sed } 622193326Sed } 623193326Sed } 624193326Sed 625193326Sed if (!SuppressMissingInputWarning && Inputs.empty()) { 626193326Sed Diag(clang::diag::err_drv_no_input_files); 627193326Sed return; 628193326Sed } 629193326Sed 630198092Srdivacky // Determine which compilation mode we are in. We look for options which 631198092Srdivacky // affect the phase, starting with the earliest phases, and record which 632198092Srdivacky // option we used to determine the final phase. 633193326Sed Arg *FinalPhaseArg = 0; 634193326Sed phases::ID FinalPhase; 635193326Sed 636193326Sed // -{E,M,MM} only run the preprocessor. 637193326Sed if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) || 638193326Sed (FinalPhaseArg = Args.getLastArg(options::OPT_M)) || 639193326Sed (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) { 640193326Sed FinalPhase = phases::Preprocess; 641198092Srdivacky 642198092Srdivacky // -{fsyntax-only,-analyze,emit-ast,S} only run up to the compiler. 643193326Sed } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) || 644203955Srdivacky (FinalPhaseArg = Args.getLastArg(options::OPT_rewrite_objc)) || 645193326Sed (FinalPhaseArg = Args.getLastArg(options::OPT__analyze, 646193326Sed options::OPT__analyze_auto)) || 647198092Srdivacky (FinalPhaseArg = Args.getLastArg(options::OPT_emit_ast)) || 648193326Sed (FinalPhaseArg = Args.getLastArg(options::OPT_S))) { 649193326Sed FinalPhase = phases::Compile; 650193326Sed 651193326Sed // -c only runs up to the assembler. 652193326Sed } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) { 653193326Sed FinalPhase = phases::Assemble; 654198092Srdivacky 655193326Sed // Otherwise do everything. 656193326Sed } else 657193326Sed FinalPhase = phases::Link; 658193326Sed 659198092Srdivacky // Reject -Z* at the top level, these options should never have been exposed 660198092Srdivacky // by gcc. 661193326Sed if (Arg *A = Args.getLastArg(options::OPT_Z_Joined)) 662193326Sed Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args); 663193326Sed 664193326Sed // Construct the actions to perform. 665193326Sed ActionList LinkerInputs; 666193326Sed for (unsigned i = 0, e = Inputs.size(); i != e; ++i) { 667193326Sed types::ID InputType = Inputs[i].first; 668193326Sed const Arg *InputArg = Inputs[i].second; 669193326Sed 670193326Sed unsigned NumSteps = types::getNumCompilationPhases(InputType); 671193326Sed assert(NumSteps && "Invalid number of steps!"); 672193326Sed 673198092Srdivacky // If the first step comes after the final phase we are doing as part of 674198092Srdivacky // this compilation, warn the user about it. 675193326Sed phases::ID InitialPhase = types::getCompilationPhase(InputType, 0); 676193326Sed if (InitialPhase > FinalPhase) { 677193326Sed // Claim here to avoid the more general unused warning. 678193326Sed InputArg->claim(); 679198092Srdivacky 680198092Srdivacky // Special case '-E' warning on a previously preprocessed file to make 681198092Srdivacky // more sense. 682198092Srdivacky if (InitialPhase == phases::Compile && FinalPhase == phases::Preprocess && 683198092Srdivacky getPreprocessedType(InputType) == types::TY_INVALID) 684198092Srdivacky Diag(clang::diag::warn_drv_preprocessed_input_file_unused) 685198092Srdivacky << InputArg->getAsString(Args) 686198092Srdivacky << FinalPhaseArg->getOption().getName(); 687198092Srdivacky else 688198092Srdivacky Diag(clang::diag::warn_drv_input_file_unused) 689198092Srdivacky << InputArg->getAsString(Args) 690198092Srdivacky << getPhaseName(InitialPhase) 691198092Srdivacky << FinalPhaseArg->getOption().getName(); 692193326Sed continue; 693193326Sed } 694198092Srdivacky 695193326Sed // Build the pipeline for this file. 696202879Srdivacky llvm::OwningPtr<Action> Current(new InputAction(*InputArg, InputType)); 697193326Sed for (unsigned i = 0; i != NumSteps; ++i) { 698193326Sed phases::ID Phase = types::getCompilationPhase(InputType, i); 699193326Sed 700193326Sed // We are done if this step is past what the user requested. 701193326Sed if (Phase > FinalPhase) 702193326Sed break; 703193326Sed 704193326Sed // Queue linker inputs. 705193326Sed if (Phase == phases::Link) { 706193326Sed assert(i + 1 == NumSteps && "linking must be final compilation step."); 707202879Srdivacky LinkerInputs.push_back(Current.take()); 708193326Sed break; 709193326Sed } 710193326Sed 711198092Srdivacky // Some types skip the assembler phase (e.g., llvm-bc), but we can't 712198092Srdivacky // encode this in the steps because the intermediate type depends on 713198092Srdivacky // arguments. Just special case here. 714193326Sed if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm) 715193326Sed continue; 716193326Sed 717193326Sed // Otherwise construct the appropriate action. 718202879Srdivacky Current.reset(ConstructPhaseAction(Args, Phase, Current.take())); 719193326Sed if (Current->getType() == types::TY_Nothing) 720193326Sed break; 721193326Sed } 722193326Sed 723193326Sed // If we ended with something, add to the output list. 724193326Sed if (Current) 725202879Srdivacky Actions.push_back(Current.take()); 726193326Sed } 727193326Sed 728193326Sed // Add a link action if necessary. 729193326Sed if (!LinkerInputs.empty()) 730193326Sed Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image)); 731201361Srdivacky 732201361Srdivacky // If we are linking, claim any options which are obviously only used for 733201361Srdivacky // compilation. 734201361Srdivacky if (FinalPhase == phases::Link) 735201361Srdivacky Args.ClaimAllArgs(options::OPT_CompileOnly_Group); 736193326Sed} 737193326Sed 738193326SedAction *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase, 739193326Sed Action *Input) const { 740193326Sed llvm::PrettyStackTraceString CrashInfo("Constructing phase actions"); 741193326Sed // Build the appropriate action. 742193326Sed switch (Phase) { 743193326Sed case phases::Link: assert(0 && "link action invalid here."); 744193326Sed case phases::Preprocess: { 745193326Sed types::ID OutputTy; 746193326Sed // -{M, MM} alter the output type. 747193326Sed if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) { 748193326Sed OutputTy = types::TY_Dependencies; 749193326Sed } else { 750193326Sed OutputTy = types::getPreprocessedType(Input->getType()); 751193326Sed assert(OutputTy != types::TY_INVALID && 752193326Sed "Cannot preprocess this input type!"); 753193326Sed } 754193326Sed return new PreprocessJobAction(Input, OutputTy); 755193326Sed } 756193326Sed case phases::Precompile: 757198092Srdivacky return new PrecompileJobAction(Input, types::TY_PCH); 758193326Sed case phases::Compile: { 759201361Srdivacky bool HasO4 = false; 760201361Srdivacky if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) 761201361Srdivacky HasO4 = A->getOption().matches(options::OPT_O4); 762201361Srdivacky 763193326Sed if (Args.hasArg(options::OPT_fsyntax_only)) { 764193326Sed return new CompileJobAction(Input, types::TY_Nothing); 765203955Srdivacky } else if (Args.hasArg(options::OPT_rewrite_objc)) { 766203955Srdivacky return new CompileJobAction(Input, types::TY_RewrittenObjC); 767193326Sed } else if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) { 768193326Sed return new AnalyzeJobAction(Input, types::TY_Plist); 769198092Srdivacky } else if (Args.hasArg(options::OPT_emit_ast)) { 770198092Srdivacky return new CompileJobAction(Input, types::TY_AST); 771193326Sed } else if (Args.hasArg(options::OPT_emit_llvm) || 772201361Srdivacky Args.hasArg(options::OPT_flto) || HasO4) { 773198092Srdivacky types::ID Output = 774193326Sed Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC; 775193326Sed return new CompileJobAction(Input, Output); 776193326Sed } else { 777193326Sed return new CompileJobAction(Input, types::TY_PP_Asm); 778193326Sed } 779193326Sed } 780193326Sed case phases::Assemble: 781193326Sed return new AssembleJobAction(Input, types::TY_Object); 782193326Sed } 783193326Sed 784193326Sed assert(0 && "invalid phase in ConstructPhaseAction"); 785193326Sed return 0; 786193326Sed} 787193326Sed 788193326Sedvoid Driver::BuildJobs(Compilation &C) const { 789193326Sed llvm::PrettyStackTraceString CrashInfo("Building compilation jobs"); 790193326Sed bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps); 791193326Sed bool UsePipes = C.getArgs().hasArg(options::OPT_pipe); 792193326Sed 793198092Srdivacky // FIXME: Pipes are forcibly disabled until we support executing them. 794193326Sed if (!CCCPrintBindings) 795193326Sed UsePipes = false; 796198092Srdivacky 797193326Sed // -save-temps inhibits pipes. 798201361Srdivacky if (SaveTemps && UsePipes) 799193326Sed Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps); 800193326Sed 801193326Sed Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o); 802193326Sed 803198092Srdivacky // It is an error to provide a -o option if we are making multiple output 804198092Srdivacky // files. 805193326Sed if (FinalOutput) { 806193326Sed unsigned NumOutputs = 0; 807198092Srdivacky for (ActionList::const_iterator it = C.getActions().begin(), 808193326Sed ie = C.getActions().end(); it != ie; ++it) 809193326Sed if ((*it)->getType() != types::TY_Nothing) 810193326Sed ++NumOutputs; 811198092Srdivacky 812193326Sed if (NumOutputs > 1) { 813193326Sed Diag(clang::diag::err_drv_output_argument_with_multiple_files); 814193326Sed FinalOutput = 0; 815193326Sed } 816193326Sed } 817193326Sed 818198092Srdivacky for (ActionList::const_iterator it = C.getActions().begin(), 819193326Sed ie = C.getActions().end(); it != ie; ++it) { 820193326Sed Action *A = *it; 821193326Sed 822198092Srdivacky // If we are linking an image for multiple archs then the linker wants 823198092Srdivacky // -arch_multiple and -final_output <final image name>. Unfortunately, this 824198092Srdivacky // doesn't fit in cleanly because we have to pass this information down. 825193326Sed // 826198092Srdivacky // FIXME: This is a hack; find a cleaner way to integrate this into the 827198092Srdivacky // process. 828193326Sed const char *LinkingOutput = 0; 829193326Sed if (isa<LipoJobAction>(A)) { 830193326Sed if (FinalOutput) 831193326Sed LinkingOutput = FinalOutput->getValue(C.getArgs()); 832193326Sed else 833193326Sed LinkingOutput = DefaultImageName.c_str(); 834193326Sed } 835193326Sed 836193326Sed InputInfo II; 837198092Srdivacky BuildJobsForAction(C, A, &C.getDefaultToolChain(), 838198092Srdivacky /*BoundArch*/0, 839193326Sed /*CanAcceptPipe*/ true, 840193326Sed /*AtTopLevel*/ true, 841193326Sed /*LinkingOutput*/ LinkingOutput, 842193326Sed II); 843193326Sed } 844193326Sed 845198092Srdivacky // If the user passed -Qunused-arguments or there were errors, don't warn 846198092Srdivacky // about any unused arguments. 847198092Srdivacky if (Diags.getNumErrors() || 848193326Sed C.getArgs().hasArg(options::OPT_Qunused_arguments)) 849193326Sed return; 850193326Sed 851193326Sed // Claim -### here. 852193326Sed (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH); 853198092Srdivacky 854193326Sed for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end(); 855193326Sed it != ie; ++it) { 856193326Sed Arg *A = *it; 857198092Srdivacky 858193326Sed // FIXME: It would be nice to be able to send the argument to the 859198092Srdivacky // Diagnostic, so that extra values, position, and so on could be printed. 860193326Sed if (!A->isClaimed()) { 861193326Sed if (A->getOption().hasNoArgumentUnused()) 862193326Sed continue; 863193326Sed 864198092Srdivacky // Suppress the warning automatically if this is just a flag, and it is an 865198092Srdivacky // instance of an argument we already claimed. 866193326Sed const Option &Opt = A->getOption(); 867193326Sed if (isa<FlagOption>(Opt)) { 868193326Sed bool DuplicateClaimed = false; 869193326Sed 870199990Srdivacky for (arg_iterator it = C.getArgs().filtered_begin(&Opt), 871199990Srdivacky ie = C.getArgs().filtered_end(); it != ie; ++it) { 872199990Srdivacky if ((*it)->isClaimed()) { 873193326Sed DuplicateClaimed = true; 874193326Sed break; 875193326Sed } 876193326Sed } 877193326Sed 878193326Sed if (DuplicateClaimed) 879193326Sed continue; 880193326Sed } 881193326Sed 882198092Srdivacky Diag(clang::diag::warn_drv_unused_argument) 883193326Sed << A->getAsString(C.getArgs()); 884193326Sed } 885193326Sed } 886193326Sed} 887193326Sed 888203955Srdivackystatic const Tool &SelectToolForJob(Compilation &C, const ToolChain *TC, 889203955Srdivacky const JobAction *JA, 890203955Srdivacky const ActionList *&Inputs) { 891203955Srdivacky const Tool *ToolForJob = 0; 892203955Srdivacky 893203955Srdivacky // See if we should look for a compiler with an integrated assembler. We match 894203955Srdivacky // bottom up, so what we are actually looking for is an assembler job with a 895203955Srdivacky // compiler input. 896203955Srdivacky if (C.getArgs().hasArg(options::OPT_integrated_as, 897203955Srdivacky options::OPT_no_integrated_as, 898203955Srdivacky TC->IsIntegratedAssemblerDefault()) && 899203955Srdivacky !C.getArgs().hasArg(options::OPT_save_temps) && 900203955Srdivacky isa<AssembleJobAction>(JA) && 901203955Srdivacky Inputs->size() == 1 && isa<CompileJobAction>(*Inputs->begin())) { 902203955Srdivacky const Tool &Compiler = TC->SelectTool(C,cast<JobAction>(**Inputs->begin())); 903203955Srdivacky if (Compiler.hasIntegratedAssembler()) { 904203955Srdivacky Inputs = &(*Inputs)[0]->getInputs(); 905203955Srdivacky ToolForJob = &Compiler; 906203955Srdivacky } 907203955Srdivacky } 908203955Srdivacky 909203955Srdivacky // Otherwise use the tool for the current job. 910203955Srdivacky if (!ToolForJob) 911203955Srdivacky ToolForJob = &TC->SelectTool(C, *JA); 912203955Srdivacky 913203955Srdivacky // See if we should use an integrated preprocessor. We do so when we have 914203955Srdivacky // exactly one input, since this is the only use case we care about 915203955Srdivacky // (irrelevant since we don't support combine yet). 916203955Srdivacky if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin()) && 917203955Srdivacky !C.getArgs().hasArg(options::OPT_no_integrated_cpp) && 918203955Srdivacky !C.getArgs().hasArg(options::OPT_traditional_cpp) && 919203955Srdivacky !C.getArgs().hasArg(options::OPT_save_temps) && 920203955Srdivacky ToolForJob->hasIntegratedCPP()) 921203955Srdivacky Inputs = &(*Inputs)[0]->getInputs(); 922203955Srdivacky 923203955Srdivacky return *ToolForJob; 924203955Srdivacky} 925203955Srdivacky 926193326Sedvoid Driver::BuildJobsForAction(Compilation &C, 927193326Sed const Action *A, 928193326Sed const ToolChain *TC, 929198092Srdivacky const char *BoundArch, 930193326Sed bool CanAcceptPipe, 931193326Sed bool AtTopLevel, 932193326Sed const char *LinkingOutput, 933193326Sed InputInfo &Result) const { 934198092Srdivacky llvm::PrettyStackTraceString CrashInfo("Building compilation jobs"); 935193326Sed 936193326Sed bool UsePipes = C.getArgs().hasArg(options::OPT_pipe); 937198092Srdivacky // FIXME: Pipes are forcibly disabled until we support executing them. 938193326Sed if (!CCCPrintBindings) 939193326Sed UsePipes = false; 940193326Sed 941193326Sed if (const InputAction *IA = dyn_cast<InputAction>(A)) { 942198092Srdivacky // FIXME: It would be nice to not claim this here; maybe the old scheme of 943198092Srdivacky // just using Args was better? 944193326Sed const Arg &Input = IA->getInputArg(); 945193326Sed Input.claim(); 946193326Sed if (isa<PositionalArg>(Input)) { 947193326Sed const char *Name = Input.getValue(C.getArgs()); 948193326Sed Result = InputInfo(Name, A->getType(), Name); 949193326Sed } else 950193326Sed Result = InputInfo(&Input, A->getType(), ""); 951193326Sed return; 952193326Sed } 953193326Sed 954193326Sed if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) { 955198092Srdivacky const ToolChain *TC = &C.getDefaultToolChain(); 956198092Srdivacky 957193326Sed std::string Arch; 958198092Srdivacky if (BAA->getArchName()) 959198092Srdivacky TC = Host->CreateToolChain(C.getArgs(), BAA->getArchName()); 960198092Srdivacky 961198092Srdivacky BuildJobsForAction(C, *BAA->begin(), TC, BAA->getArchName(), 962198092Srdivacky CanAcceptPipe, AtTopLevel, LinkingOutput, Result); 963193326Sed return; 964193326Sed } 965193326Sed 966203955Srdivacky const ActionList *Inputs = &A->getInputs(); 967203955Srdivacky 968193326Sed const JobAction *JA = cast<JobAction>(A); 969203955Srdivacky const Tool &T = SelectToolForJob(C, TC, JA, Inputs); 970198092Srdivacky 971193326Sed // Only use pipes when there is exactly one input. 972193326Sed bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput(); 973193326Sed InputInfoList InputInfos; 974193326Sed for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end(); 975193326Sed it != ie; ++it) { 976193326Sed InputInfo II; 977198092Srdivacky BuildJobsForAction(C, *it, TC, BoundArch, TryToUsePipeInput, 978198092Srdivacky /*AtTopLevel*/false, LinkingOutput, II); 979193326Sed InputInfos.push_back(II); 980193326Sed } 981193326Sed 982193326Sed // Determine if we should output to a pipe. 983193326Sed bool OutputToPipe = false; 984193326Sed if (CanAcceptPipe && T.canPipeOutput()) { 985198092Srdivacky // Some actions default to writing to a pipe if they are the top level phase 986198092Srdivacky // and there was no user override. 987193326Sed // 988193326Sed // FIXME: Is there a better way to handle this? 989193326Sed if (AtTopLevel) { 990193326Sed if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o)) 991193326Sed OutputToPipe = true; 992193326Sed } else if (UsePipes) 993193326Sed OutputToPipe = true; 994193326Sed } 995193326Sed 996193326Sed // Figure out where to put the job (pipes). 997193326Sed Job *Dest = &C.getJobs(); 998193326Sed if (InputInfos[0].isPipe()) { 999193326Sed assert(TryToUsePipeInput && "Unrequested pipe!"); 1000193326Sed assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs."); 1001193326Sed Dest = &InputInfos[0].getPipe(); 1002193326Sed } 1003193326Sed 1004193326Sed // Always use the first input as the base input. 1005193326Sed const char *BaseInput = InputInfos[0].getBaseInput(); 1006193326Sed 1007198092Srdivacky // Determine the place to write output to (nothing, pipe, or filename) and 1008198092Srdivacky // where to put the new job. 1009193326Sed if (JA->getType() == types::TY_Nothing) { 1010193326Sed Result = InputInfo(A->getType(), BaseInput); 1011193326Sed } else if (OutputToPipe) { 1012193326Sed // Append to current piped job or create a new one as appropriate. 1013193326Sed PipedJob *PJ = dyn_cast<PipedJob>(Dest); 1014193326Sed if (!PJ) { 1015193326Sed PJ = new PipedJob(); 1016198092Srdivacky // FIXME: Temporary hack so that -ccc-print-bindings work until we have 1017198092Srdivacky // pipe support. Please remove later. 1018193326Sed if (!CCCPrintBindings) 1019193326Sed cast<JobList>(Dest)->addJob(PJ); 1020193326Sed Dest = PJ; 1021193326Sed } 1022193326Sed Result = InputInfo(PJ, A->getType(), BaseInput); 1023193326Sed } else { 1024193326Sed Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel), 1025193326Sed A->getType(), BaseInput); 1026193326Sed } 1027193326Sed 1028193326Sed if (CCCPrintBindings) { 1029193326Sed llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"' 1030193326Sed << " - \"" << T.getName() << "\", inputs: ["; 1031193326Sed for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) { 1032193326Sed llvm::errs() << InputInfos[i].getAsString(); 1033193326Sed if (i + 1 != e) 1034193326Sed llvm::errs() << ", "; 1035193326Sed } 1036193326Sed llvm::errs() << "], output: " << Result.getAsString() << "\n"; 1037193326Sed } else { 1038198092Srdivacky T.ConstructJob(C, *JA, *Dest, Result, InputInfos, 1039198092Srdivacky C.getArgsForToolChain(TC, BoundArch), LinkingOutput); 1040193326Sed } 1041193326Sed} 1042193326Sed 1043198092Srdivackyconst char *Driver::GetNamedOutputPath(Compilation &C, 1044193326Sed const JobAction &JA, 1045193326Sed const char *BaseInput, 1046193326Sed bool AtTopLevel) const { 1047193326Sed llvm::PrettyStackTraceString CrashInfo("Computing output path"); 1048193326Sed // Output to a user requested destination? 1049193326Sed if (AtTopLevel) { 1050193326Sed if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) 1051193326Sed return C.addResultFile(FinalOutput->getValue(C.getArgs())); 1052193326Sed } 1053193326Sed 1054193326Sed // Output to a temporary file? 1055193326Sed if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) { 1056198092Srdivacky std::string TmpName = 1057193326Sed GetTemporaryPath(types::getTypeTempSuffix(JA.getType())); 1058193326Sed return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str())); 1059193326Sed } 1060193326Sed 1061193326Sed llvm::sys::Path BasePath(BaseInput); 1062193326Sed std::string BaseName(BasePath.getLast()); 1063193326Sed 1064193326Sed // Determine what the derived output name should be. 1065193326Sed const char *NamedOutput; 1066193326Sed if (JA.getType() == types::TY_Image) { 1067193326Sed NamedOutput = DefaultImageName.c_str(); 1068193326Sed } else { 1069193326Sed const char *Suffix = types::getTypeTempSuffix(JA.getType()); 1070193326Sed assert(Suffix && "All types used for output should have a suffix."); 1071193326Sed 1072193326Sed std::string::size_type End = std::string::npos; 1073193326Sed if (!types::appendSuffixForType(JA.getType())) 1074193326Sed End = BaseName.rfind('.'); 1075193326Sed std::string Suffixed(BaseName.substr(0, End)); 1076193326Sed Suffixed += '.'; 1077193326Sed Suffixed += Suffix; 1078193326Sed NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str()); 1079193326Sed } 1080193326Sed 1081198092Srdivacky // As an annoying special case, PCH generation doesn't strip the pathname. 1082193326Sed if (JA.getType() == types::TY_PCH) { 1083193326Sed BasePath.eraseComponent(); 1084193326Sed if (BasePath.isEmpty()) 1085193326Sed BasePath = NamedOutput; 1086193326Sed else 1087193326Sed BasePath.appendComponent(NamedOutput); 1088193326Sed return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str())); 1089193326Sed } else { 1090193326Sed return C.addResultFile(NamedOutput); 1091193326Sed } 1092193326Sed} 1093193326Sed 1094198092Srdivackystd::string Driver::GetFilePath(const char *Name, const ToolChain &TC) const { 1095206084Srdivacky // Respect a limited subset of the '-Bprefix' functionality in GCC by 1096206084Srdivacky // attempting to use this prefix when lokup up program paths. 1097206084Srdivacky if (!PrefixDir.empty()) { 1098206084Srdivacky llvm::sys::Path P(PrefixDir); 1099206084Srdivacky P.appendComponent(Name); 1100206084Srdivacky if (P.exists()) 1101206084Srdivacky return P.str(); 1102206084Srdivacky } 1103206084Srdivacky 1104193326Sed const ToolChain::path_list &List = TC.getFilePaths(); 1105198092Srdivacky for (ToolChain::path_list::const_iterator 1106193326Sed it = List.begin(), ie = List.end(); it != ie; ++it) { 1107193326Sed llvm::sys::Path P(*it); 1108193326Sed P.appendComponent(Name); 1109193326Sed if (P.exists()) 1110198092Srdivacky return P.str(); 1111193326Sed } 1112193326Sed 1113198092Srdivacky return Name; 1114193326Sed} 1115193326Sed 1116198092Srdivackystd::string Driver::GetProgramPath(const char *Name, const ToolChain &TC, 1117198092Srdivacky bool WantFile) const { 1118206084Srdivacky // Respect a limited subset of the '-Bprefix' functionality in GCC by 1119206084Srdivacky // attempting to use this prefix when lokup up program paths. 1120206084Srdivacky if (!PrefixDir.empty()) { 1121206084Srdivacky llvm::sys::Path P(PrefixDir); 1122206084Srdivacky P.appendComponent(Name); 1123206084Srdivacky if (WantFile ? P.exists() : P.canExecute()) 1124206084Srdivacky return P.str(); 1125206084Srdivacky } 1126206084Srdivacky 1127193326Sed const ToolChain::path_list &List = TC.getProgramPaths(); 1128198092Srdivacky for (ToolChain::path_list::const_iterator 1129193326Sed it = List.begin(), ie = List.end(); it != ie; ++it) { 1130193326Sed llvm::sys::Path P(*it); 1131193326Sed P.appendComponent(Name); 1132193326Sed if (WantFile ? P.exists() : P.canExecute()) 1133198092Srdivacky return P.str(); 1134193326Sed } 1135193326Sed 1136193326Sed // If all else failed, search the path. 1137193326Sed llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name)); 1138193326Sed if (!P.empty()) 1139198092Srdivacky return P.str(); 1140193326Sed 1141198092Srdivacky return Name; 1142193326Sed} 1143193326Sed 1144193326Sedstd::string Driver::GetTemporaryPath(const char *Suffix) const { 1145198092Srdivacky // FIXME: This is lame; sys::Path should provide this function (in particular, 1146198092Srdivacky // it should know how to find the temporary files dir). 1147193326Sed std::string Error; 1148193326Sed const char *TmpDir = ::getenv("TMPDIR"); 1149193326Sed if (!TmpDir) 1150193326Sed TmpDir = ::getenv("TEMP"); 1151193326Sed if (!TmpDir) 1152193326Sed TmpDir = ::getenv("TMP"); 1153193326Sed if (!TmpDir) 1154193326Sed TmpDir = "/tmp"; 1155193326Sed llvm::sys::Path P(TmpDir); 1156193326Sed P.appendComponent("cc"); 1157193326Sed if (P.makeUnique(false, &Error)) { 1158193326Sed Diag(clang::diag::err_drv_unable_to_make_temp) << Error; 1159193326Sed return ""; 1160193326Sed } 1161193326Sed 1162198092Srdivacky // FIXME: Grumble, makeUnique sometimes leaves the file around!? PR3837. 1163193326Sed P.eraseFromDisk(false, 0); 1164193326Sed 1165193326Sed P.appendSuffix(Suffix); 1166198092Srdivacky return P.str(); 1167193326Sed} 1168193326Sed 1169193326Sedconst HostInfo *Driver::GetHostInfo(const char *TripleStr) const { 1170193326Sed llvm::PrettyStackTraceString CrashInfo("Constructing host"); 1171193326Sed llvm::Triple Triple(TripleStr); 1172193326Sed 1173204793Srdivacky // TCE is an osless target 1174204793Srdivacky if (Triple.getArchName() == "tce") 1175204793Srdivacky return createTCEHostInfo(*this, Triple); 1176204793Srdivacky 1177193326Sed switch (Triple.getOS()) { 1178198092Srdivacky case llvm::Triple::AuroraUX: 1179198092Srdivacky return createAuroraUXHostInfo(*this, Triple); 1180193326Sed case llvm::Triple::Darwin: 1181193326Sed return createDarwinHostInfo(*this, Triple); 1182193326Sed case llvm::Triple::DragonFly: 1183193326Sed return createDragonFlyHostInfo(*this, Triple); 1184195341Sed case llvm::Triple::OpenBSD: 1185195341Sed return createOpenBSDHostInfo(*this, Triple); 1186193326Sed case llvm::Triple::FreeBSD: 1187193326Sed return createFreeBSDHostInfo(*this, Triple); 1188193326Sed case llvm::Triple::Linux: 1189193326Sed return createLinuxHostInfo(*this, Triple); 1190193326Sed default: 1191193326Sed return createUnknownHostInfo(*this, Triple); 1192193326Sed } 1193193326Sed} 1194193326Sed 1195193326Sedbool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA, 1196198092Srdivacky const llvm::Triple &Triple) const { 1197198092Srdivacky // Check if user requested no clang, or clang doesn't understand this type (we 1198198092Srdivacky // only handle single inputs for now). 1199198092Srdivacky if (!CCCUseClang || JA.size() != 1 || 1200193326Sed !types::isAcceptedByClang((*JA.begin())->getType())) 1201193326Sed return false; 1202193326Sed 1203193326Sed // Otherwise make sure this is an action clang understands. 1204193326Sed if (isa<PreprocessJobAction>(JA)) { 1205193326Sed if (!CCCUseClangCPP) { 1206193326Sed Diag(clang::diag::warn_drv_not_using_clang_cpp); 1207193326Sed return false; 1208193326Sed } 1209193326Sed } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA)) 1210193326Sed return false; 1211193326Sed 1212193326Sed // Use clang for C++? 1213193326Sed if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) { 1214193326Sed Diag(clang::diag::warn_drv_not_using_clang_cxx); 1215193326Sed return false; 1216193326Sed } 1217193326Sed 1218203955Srdivacky // Always use clang for precompiling, AST generation, and rewriting, 1219203955Srdivacky // regardless of archs. 1220203955Srdivacky if (isa<PrecompileJobAction>(JA) || JA.getType() == types::TY_AST || 1221203955Srdivacky JA.getType() == types::TY_RewrittenObjC) 1222193326Sed return true; 1223193326Sed 1224198092Srdivacky // Finally, don't use clang if this isn't one of the user specified archs to 1225198092Srdivacky // build. 1226198092Srdivacky if (!CCCClangArchs.empty() && !CCCClangArchs.count(Triple.getArch())) { 1227198092Srdivacky Diag(clang::diag::warn_drv_not_using_clang_arch) << Triple.getArchName(); 1228193326Sed return false; 1229193326Sed } 1230193326Sed 1231193326Sed return true; 1232193326Sed} 1233193326Sed 1234198092Srdivacky/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the 1235198092Srdivacky/// grouped values as integers. Numbers which are not provided are set to 0. 1236193326Sed/// 1237198092Srdivacky/// \return True if the entire string was parsed (9.2), or all groups were 1238198092Srdivacky/// parsed (10.3.5extrastuff). 1239198092Srdivackybool Driver::GetReleaseVersion(const char *Str, unsigned &Major, 1240193326Sed unsigned &Minor, unsigned &Micro, 1241193326Sed bool &HadExtra) { 1242193326Sed HadExtra = false; 1243193326Sed 1244193326Sed Major = Minor = Micro = 0; 1245198092Srdivacky if (*Str == '\0') 1246193326Sed return true; 1247193326Sed 1248193326Sed char *End; 1249193326Sed Major = (unsigned) strtol(Str, &End, 10); 1250193326Sed if (*Str != '\0' && *End == '\0') 1251193326Sed return true; 1252193326Sed if (*End != '.') 1253193326Sed return false; 1254198092Srdivacky 1255193326Sed Str = End+1; 1256193326Sed Minor = (unsigned) strtol(Str, &End, 10); 1257193326Sed if (*Str != '\0' && *End == '\0') 1258193326Sed return true; 1259193326Sed if (*End != '.') 1260193326Sed return false; 1261193326Sed 1262193326Sed Str = End+1; 1263193326Sed Micro = (unsigned) strtol(Str, &End, 10); 1264193326Sed if (*Str != '\0' && *End == '\0') 1265193326Sed return true; 1266193326Sed if (Str == End) 1267193326Sed return false; 1268193326Sed HadExtra = true; 1269193326Sed return true; 1270193326Sed} 1271