Driver.cpp revision 199512
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" 29193326Sed#include "llvm/Support/PrettyStackTrace.h" 30193326Sed#include "llvm/Support/raw_ostream.h" 31193326Sed#include "llvm/System/Path.h" 32193326Sed#include "llvm/System/Program.h" 33193326Sed 34193326Sed#include "InputInfo.h" 35193326Sed 36193326Sed#include <map> 37193326Sed 38193326Sedusing namespace clang::driver; 39193326Sedusing namespace clang; 40193326Sed 41198092Srdivacky// Used to set values for "production" clang, for releases. 42198092Srdivacky// #define USE_PRODUCTION_CLANG 43198092Srdivacky 44193326SedDriver::Driver(const char *_Name, const char *_Dir, 45193326Sed const char *_DefaultHostTriple, 46193326Sed const char *_DefaultImageName, 47198092Srdivacky bool IsProduction, Diagnostic &_Diags) 48199512Srdivacky : Opts(createDriverOptTable()), Diags(_Diags), 49193326Sed Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple), 50193326Sed DefaultImageName(_DefaultImageName), 51193326Sed Host(0), 52193326Sed CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false), 53198092Srdivacky CCCGenericGCCName("gcc"), CCCUseClang(true), 54198092Srdivacky CCCUseClangCXX(true), CCCUseClangCPP(true), CCCUsePCH(true), 55198092Srdivacky SuppressMissingInputWarning(false) { 56198092Srdivacky if (IsProduction) { 57198092Srdivacky // In a "production" build, only use clang on architectures we expect to 58198092Srdivacky // work, and don't use clang C++. 59198092Srdivacky // 60198092Srdivacky // During development its more convenient to always have the driver use 61198092Srdivacky // clang, but we don't want users to be confused when things don't work, or 62198092Srdivacky // to file bugs for things we don't support. 63198092Srdivacky CCCClangArchs.insert(llvm::Triple::x86); 64198092Srdivacky CCCClangArchs.insert(llvm::Triple::x86_64); 65198092Srdivacky CCCClangArchs.insert(llvm::Triple::arm); 66198092Srdivacky 67198092Srdivacky CCCUseClangCXX = false; 68198092Srdivacky } 69193326Sed} 70193326Sed 71193326SedDriver::~Driver() { 72193326Sed delete Opts; 73193326Sed delete Host; 74193326Sed} 75193326Sed 76198092SrdivackyInputArgList *Driver::ParseArgStrings(const char **ArgBegin, 77193326Sed const char **ArgEnd) { 78193326Sed llvm::PrettyStackTraceString CrashInfo("Command line argument parsing"); 79199512Srdivacky unsigned MissingArgIndex, MissingArgCount; 80199512Srdivacky InputArgList *Args = getOpts().ParseArgs(ArgBegin, ArgEnd, 81199512Srdivacky MissingArgIndex, MissingArgCount); 82198092Srdivacky 83199512Srdivacky // Check for missing argument error. 84199512Srdivacky if (MissingArgCount) 85199512Srdivacky Diag(clang::diag::err_drv_missing_argument) 86199512Srdivacky << Args->getArgString(MissingArgIndex) << MissingArgCount; 87193326Sed 88199512Srdivacky // Check for unsupported options. 89199512Srdivacky for (ArgList::const_iterator it = Args->begin(), ie = Args->end(); 90199512Srdivacky it != ie; ++it) { 91199512Srdivacky Arg *A = *it; 92193326Sed if (A->getOption().isUnsupported()) { 93193326Sed Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args); 94193326Sed continue; 95193326Sed } 96193326Sed } 97193326Sed 98193326Sed return Args; 99193326Sed} 100193326Sed 101193326SedCompilation *Driver::BuildCompilation(int argc, const char **argv) { 102193326Sed llvm::PrettyStackTraceString CrashInfo("Compilation construction"); 103193326Sed 104198092Srdivacky // FIXME: Handle environment options which effect driver behavior, somewhere 105198092Srdivacky // (client?). GCC_EXEC_PREFIX, COMPILER_PATH, LIBRARY_PATH, LPATH, 106198092Srdivacky // CC_PRINT_OPTIONS. 107193326Sed 108193326Sed // FIXME: What are we going to do with -V and -b? 109193326Sed 110198092Srdivacky // FIXME: This stuff needs to go into the Compilation, not the driver. 111193326Sed bool CCCPrintOptions = false, CCCPrintActions = false; 112193326Sed 113193326Sed const char **Start = argv + 1, **End = argv + argc; 114193326Sed const char *HostTriple = DefaultHostTriple.c_str(); 115193326Sed 116198092Srdivacky // Read -ccc args. 117193326Sed // 118198092Srdivacky // FIXME: We need to figure out where this behavior should live. Most of it 119198092Srdivacky // should be outside in the client; the parts that aren't should have proper 120198092Srdivacky // options, either by introducing new ones or by overloading gcc ones like -V 121198092Srdivacky // or -b. 122193326Sed for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) { 123193326Sed const char *Opt = *Start + 5; 124198092Srdivacky 125193326Sed if (!strcmp(Opt, "print-options")) { 126193326Sed CCCPrintOptions = true; 127193326Sed } else if (!strcmp(Opt, "print-phases")) { 128193326Sed CCCPrintActions = true; 129193326Sed } else if (!strcmp(Opt, "print-bindings")) { 130193326Sed CCCPrintBindings = true; 131193326Sed } else if (!strcmp(Opt, "cxx")) { 132193326Sed CCCIsCXX = true; 133193326Sed } else if (!strcmp(Opt, "echo")) { 134193326Sed CCCEcho = true; 135198092Srdivacky 136193326Sed } else if (!strcmp(Opt, "gcc-name")) { 137193326Sed assert(Start+1 < End && "FIXME: -ccc- argument handling."); 138193326Sed CCCGenericGCCName = *++Start; 139193326Sed 140193326Sed } else if (!strcmp(Opt, "clang-cxx")) { 141193326Sed CCCUseClangCXX = true; 142198092Srdivacky } else if (!strcmp(Opt, "no-clang-cxx")) { 143198092Srdivacky CCCUseClangCXX = false; 144193326Sed } else if (!strcmp(Opt, "pch-is-pch")) { 145193326Sed CCCUsePCH = true; 146193326Sed } else if (!strcmp(Opt, "pch-is-pth")) { 147193326Sed CCCUsePCH = false; 148193326Sed } else if (!strcmp(Opt, "no-clang")) { 149193326Sed CCCUseClang = false; 150193326Sed } else if (!strcmp(Opt, "no-clang-cpp")) { 151193326Sed CCCUseClangCPP = false; 152193326Sed } else if (!strcmp(Opt, "clang-archs")) { 153193326Sed assert(Start+1 < End && "FIXME: -ccc- argument handling."); 154198092Srdivacky llvm::StringRef Cur = *++Start; 155198092Srdivacky 156193326Sed CCCClangArchs.clear(); 157198092Srdivacky while (!Cur.empty()) { 158198092Srdivacky std::pair<llvm::StringRef, llvm::StringRef> Split = Cur.split(','); 159193326Sed 160198092Srdivacky if (!Split.first.empty()) { 161198092Srdivacky llvm::Triple::ArchType Arch = 162198092Srdivacky llvm::Triple(Split.first, "", "").getArch(); 163198092Srdivacky 164198092Srdivacky if (Arch == llvm::Triple::UnknownArch) { 165198092Srdivacky // FIXME: Error handling. 166198092Srdivacky llvm::errs() << "invalid arch name: " << Split.first << "\n"; 167198092Srdivacky exit(1); 168198092Srdivacky } 169198092Srdivacky 170198092Srdivacky CCCClangArchs.insert(Arch); 171193326Sed } 172198092Srdivacky 173198092Srdivacky Cur = Split.second; 174193326Sed } 175193326Sed } else if (!strcmp(Opt, "host-triple")) { 176193326Sed assert(Start+1 < End && "FIXME: -ccc- argument handling."); 177193326Sed HostTriple = *++Start; 178193326Sed 179198092Srdivacky } else if (!strcmp(Opt, "install-dir")) { 180198092Srdivacky assert(Start+1 < End && "FIXME: -ccc- argument handling."); 181198092Srdivacky Dir = *++Start; 182198092Srdivacky 183193326Sed } else { 184193326Sed // FIXME: Error handling. 185193326Sed llvm::errs() << "invalid option: " << *Start << "\n"; 186193326Sed exit(1); 187193326Sed } 188193326Sed } 189193326Sed 190193326Sed InputArgList *Args = ParseArgStrings(Start, End); 191193326Sed 192193326Sed Host = GetHostInfo(HostTriple); 193193326Sed 194193326Sed // The compilation takes ownership of Args. 195198092Srdivacky Compilation *C = new Compilation(*this, *Host->CreateToolChain(*Args), Args); 196193326Sed 197193326Sed // FIXME: This behavior shouldn't be here. 198193326Sed if (CCCPrintOptions) { 199193326Sed PrintOptions(C->getArgs()); 200193326Sed return C; 201193326Sed } 202193326Sed 203193326Sed if (!HandleImmediateArgs(*C)) 204193326Sed return C; 205193326Sed 206198092Srdivacky // Construct the list of abstract actions to perform for this compilation. We 207198092Srdivacky // avoid passing a Compilation here simply to enforce the abstraction that 208198092Srdivacky // pipelining is not host or toolchain dependent (other than the driver driver 209198092Srdivacky // test). 210193326Sed if (Host->useDriverDriver()) 211193326Sed BuildUniversalActions(C->getArgs(), C->getActions()); 212193326Sed else 213193326Sed BuildActions(C->getArgs(), C->getActions()); 214193326Sed 215193326Sed if (CCCPrintActions) { 216193326Sed PrintActions(*C); 217193326Sed return C; 218193326Sed } 219193326Sed 220193326Sed BuildJobs(*C); 221193326Sed 222193326Sed return C; 223193326Sed} 224193326Sed 225195341Sedint Driver::ExecuteCompilation(const Compilation &C) const { 226195341Sed // Just print if -### was present. 227195341Sed if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { 228195341Sed C.PrintJob(llvm::errs(), C.getJobs(), "\n", true); 229195341Sed return 0; 230195341Sed } 231195341Sed 232195341Sed // If there were errors building the compilation, quit now. 233195341Sed if (getDiags().getNumErrors()) 234195341Sed return 1; 235195341Sed 236195341Sed const Command *FailingCommand = 0; 237195341Sed int Res = C.ExecuteJob(C.getJobs(), FailingCommand); 238198092Srdivacky 239195341Sed // Remove temp files. 240195341Sed C.CleanupFileList(C.getTempFiles()); 241195341Sed 242195341Sed // If the compilation failed, remove result files as well. 243195341Sed if (Res != 0 && !C.getArgs().hasArg(options::OPT_save_temps)) 244195341Sed C.CleanupFileList(C.getResultFiles(), true); 245195341Sed 246195341Sed // Print extra information about abnormal failures, if possible. 247195341Sed if (Res) { 248195341Sed // This is ad-hoc, but we don't want to be excessively noisy. If the result 249195341Sed // status was 1, assume the command failed normally. In particular, if it 250195341Sed // was the compiler then assume it gave a reasonable error code. Failures in 251195341Sed // other tools are less common, and they generally have worse diagnostics, 252195341Sed // so always print the diagnostic there. 253195341Sed const Action &Source = FailingCommand->getSource(); 254195341Sed bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) || 255195341Sed isa<PrecompileJobAction>(Source) || 256195341Sed isa<AnalyzeJobAction>(Source) || 257195341Sed isa<CompileJobAction>(Source)); 258195341Sed 259195341Sed if (!IsFriendlyTool || Res != 1) { 260195341Sed // FIXME: See FIXME above regarding result code interpretation. 261195341Sed if (Res < 0) 262198092Srdivacky Diag(clang::diag::err_drv_command_signalled) 263195341Sed << Source.getClassName() << -Res; 264195341Sed else 265198092Srdivacky Diag(clang::diag::err_drv_command_failed) 266195341Sed << Source.getClassName() << Res; 267195341Sed } 268195341Sed } 269195341Sed 270195341Sed return Res; 271195341Sed} 272195341Sed 273193326Sedvoid Driver::PrintOptions(const ArgList &Args) const { 274193326Sed unsigned i = 0; 275198092Srdivacky for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); 276193326Sed it != ie; ++it, ++i) { 277193326Sed Arg *A = *it; 278193326Sed llvm::errs() << "Option " << i << " - " 279193326Sed << "Name: \"" << A->getOption().getName() << "\", " 280193326Sed << "Values: {"; 281193326Sed for (unsigned j = 0; j < A->getNumValues(); ++j) { 282193326Sed if (j) 283193326Sed llvm::errs() << ", "; 284193326Sed llvm::errs() << '"' << A->getValue(Args, j) << '"'; 285193326Sed } 286193326Sed llvm::errs() << "}\n"; 287193326Sed } 288193326Sed} 289193326Sed 290193326Sedstatic std::string getOptionHelpName(const OptTable &Opts, options::ID Id) { 291193326Sed std::string Name = Opts.getOptionName(Id); 292198092Srdivacky 293193326Sed // Add metavar, if used. 294193326Sed switch (Opts.getOptionKind(Id)) { 295198092Srdivacky case Option::GroupClass: case Option::InputClass: case Option::UnknownClass: 296193326Sed assert(0 && "Invalid option with help text."); 297193326Sed 298193326Sed case Option::MultiArgClass: case Option::JoinedAndSeparateClass: 299193326Sed assert(0 && "Cannot print metavar for this kind of option."); 300193326Sed 301193326Sed case Option::FlagClass: 302193326Sed break; 303193326Sed 304193326Sed case Option::SeparateClass: case Option::JoinedOrSeparateClass: 305193326Sed Name += ' '; 306193326Sed // FALLTHROUGH 307193326Sed case Option::JoinedClass: case Option::CommaJoinedClass: 308193326Sed Name += Opts.getOptionMetaVar(Id); 309193326Sed break; 310193326Sed } 311193326Sed 312193326Sed return Name; 313193326Sed} 314193326Sed 315193326Sedvoid Driver::PrintHelp(bool ShowHidden) const { 316193326Sed llvm::raw_ostream &OS = llvm::outs(); 317193326Sed 318193326Sed OS << "OVERVIEW: clang \"gcc-compatible\" driver\n"; 319193326Sed OS << '\n'; 320193326Sed OS << "USAGE: " << Name << " [options] <input files>\n"; 321193326Sed OS << '\n'; 322193326Sed OS << "OPTIONS:\n"; 323193326Sed 324193326Sed // Render help text into (option, help) pairs. 325193326Sed std::vector< std::pair<std::string, const char*> > OptionHelp; 326193326Sed 327199512Srdivacky for (unsigned i = 0, e = getOpts().getNumOptions(); i != e; ++i) { 328199512Srdivacky options::ID Id = (options::ID) (i + 1); 329193326Sed if (const char *Text = getOpts().getOptionHelpText(Id)) 330193326Sed OptionHelp.push_back(std::make_pair(getOptionHelpName(getOpts(), Id), 331193326Sed Text)); 332193326Sed } 333193326Sed 334193326Sed if (ShowHidden) { 335193326Sed OptionHelp.push_back(std::make_pair("\nDRIVER OPTIONS:","")); 336193326Sed OptionHelp.push_back(std::make_pair("-ccc-cxx", 337193326Sed "Act as a C++ driver")); 338193326Sed OptionHelp.push_back(std::make_pair("-ccc-gcc-name", 339193326Sed "Name for native GCC compiler")); 340193326Sed OptionHelp.push_back(std::make_pair("-ccc-clang-cxx", 341198092Srdivacky "Enable the clang compiler for C++")); 342198092Srdivacky OptionHelp.push_back(std::make_pair("-ccc-no-clang-cxx", 343198092Srdivacky "Disable the clang compiler for C++")); 344193326Sed OptionHelp.push_back(std::make_pair("-ccc-no-clang", 345198092Srdivacky "Disable the clang compiler")); 346193326Sed OptionHelp.push_back(std::make_pair("-ccc-no-clang-cpp", 347198092Srdivacky "Disable the clang preprocessor")); 348193326Sed OptionHelp.push_back(std::make_pair("-ccc-clang-archs", 349193326Sed "Comma separate list of architectures " 350193326Sed "to use the clang compiler for")); 351193326Sed OptionHelp.push_back(std::make_pair("-ccc-pch-is-pch", 352193326Sed "Use lazy PCH for precompiled headers")); 353193326Sed OptionHelp.push_back(std::make_pair("-ccc-pch-is-pth", 354193326Sed "Use pretokenized headers for precompiled headers")); 355193326Sed 356193326Sed OptionHelp.push_back(std::make_pair("\nDEBUG/DEVELOPMENT OPTIONS:","")); 357193326Sed OptionHelp.push_back(std::make_pair("-ccc-host-triple", 358198092Srdivacky "Simulate running on the given target")); 359198092Srdivacky OptionHelp.push_back(std::make_pair("-ccc-install-dir", 360198092Srdivacky "Simulate installation in the given directory")); 361193326Sed OptionHelp.push_back(std::make_pair("-ccc-print-options", 362193326Sed "Dump parsed command line arguments")); 363193326Sed OptionHelp.push_back(std::make_pair("-ccc-print-phases", 364193326Sed "Dump list of actions to perform")); 365193326Sed OptionHelp.push_back(std::make_pair("-ccc-print-bindings", 366193326Sed "Show bindings of tools to actions")); 367193326Sed OptionHelp.push_back(std::make_pair("CCC_ADD_ARGS", 368193326Sed "(ENVIRONMENT VARIABLE) Comma separated list of " 369193326Sed "arguments to prepend to the command line")); 370193326Sed } 371193326Sed 372198092Srdivacky // Find the maximum option length. 373193326Sed unsigned OptionFieldWidth = 0; 374193326Sed for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { 375193326Sed // Skip titles. 376193326Sed if (!OptionHelp[i].second) 377193326Sed continue; 378193326Sed 379198092Srdivacky // Limit the amount of padding we are willing to give up for alignment. 380193326Sed unsigned Length = OptionHelp[i].first.size(); 381193326Sed if (Length <= 23) 382193326Sed OptionFieldWidth = std::max(OptionFieldWidth, Length); 383193326Sed } 384193326Sed 385193326Sed for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { 386193326Sed const std::string &Option = OptionHelp[i].first; 387193326Sed OS << " " << Option; 388193326Sed for (int j = Option.length(), e = OptionFieldWidth; j < e; ++j) 389193326Sed OS << ' '; 390193326Sed OS << ' ' << OptionHelp[i].second << '\n'; 391193326Sed } 392193326Sed 393193326Sed OS.flush(); 394193326Sed} 395193326Sed 396198092Srdivackyvoid Driver::PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const { 397198092Srdivacky // FIXME: The following handlers should use a callback mechanism, we don't 398198092Srdivacky // know what the client would like to do. 399198092Srdivacky#ifdef CLANG_VENDOR 400198092Srdivacky OS << CLANG_VENDOR; 401193326Sed#endif 402198092Srdivacky OS << "clang version " CLANG_VERSION_STRING " (" 403198092Srdivacky << getClangSubversionPath(); 404198092Srdivacky if (unsigned Revision = getClangSubversionRevision()) 405198092Srdivacky OS << " " << Revision; 406198092Srdivacky OS << ")" << '\n'; 407193326Sed 408193326Sed const ToolChain &TC = C.getDefaultToolChain(); 409198092Srdivacky OS << "Target: " << TC.getTripleString() << '\n'; 410194613Sed 411194613Sed // Print the threading model. 412194613Sed // 413194613Sed // FIXME: Implement correctly. 414198092Srdivacky OS << "Thread model: " << "posix" << '\n'; 415193326Sed} 416193326Sed 417193326Sedbool Driver::HandleImmediateArgs(const Compilation &C) { 418198092Srdivacky // The order these options are handled in in gcc is all over the place, but we 419198092Srdivacky // don't expect inconsistencies w.r.t. that to matter in practice. 420193326Sed 421193326Sed if (C.getArgs().hasArg(options::OPT_dumpversion)) { 422193326Sed llvm::outs() << CLANG_VERSION_STRING "\n"; 423193326Sed return false; 424193326Sed } 425193326Sed 426198092Srdivacky if (C.getArgs().hasArg(options::OPT__help) || 427193326Sed C.getArgs().hasArg(options::OPT__help_hidden)) { 428193326Sed PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden)); 429193326Sed return false; 430193326Sed } 431193326Sed 432193326Sed if (C.getArgs().hasArg(options::OPT__version)) { 433198092Srdivacky // Follow gcc behavior and use stdout for --version and stderr for -v. 434198092Srdivacky PrintVersion(C, llvm::outs()); 435193326Sed return false; 436193326Sed } 437193326Sed 438198092Srdivacky if (C.getArgs().hasArg(options::OPT_v) || 439193326Sed C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { 440198092Srdivacky PrintVersion(C, llvm::errs()); 441193326Sed SuppressMissingInputWarning = true; 442193326Sed } 443193326Sed 444193326Sed const ToolChain &TC = C.getDefaultToolChain(); 445193326Sed if (C.getArgs().hasArg(options::OPT_print_search_dirs)) { 446193326Sed llvm::outs() << "programs: ="; 447193326Sed for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(), 448193326Sed ie = TC.getProgramPaths().end(); it != ie; ++it) { 449193326Sed if (it != TC.getProgramPaths().begin()) 450193326Sed llvm::outs() << ':'; 451193326Sed llvm::outs() << *it; 452193326Sed } 453193326Sed llvm::outs() << "\n"; 454193326Sed llvm::outs() << "libraries: ="; 455198092Srdivacky for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(), 456193326Sed ie = TC.getFilePaths().end(); it != ie; ++it) { 457193326Sed if (it != TC.getFilePaths().begin()) 458193326Sed llvm::outs() << ':'; 459193326Sed llvm::outs() << *it; 460193326Sed } 461193326Sed llvm::outs() << "\n"; 462193326Sed return false; 463193326Sed } 464193326Sed 465198092Srdivacky // FIXME: The following handlers should use a callback mechanism, we don't 466198092Srdivacky // know what the client would like to do. 467193326Sed if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) { 468198092Srdivacky llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC) << "\n"; 469193326Sed return false; 470193326Sed } 471193326Sed 472193326Sed if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) { 473198092Srdivacky llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC) << "\n"; 474193326Sed return false; 475193326Sed } 476193326Sed 477193326Sed if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) { 478198092Srdivacky llvm::outs() << GetFilePath("libgcc.a", TC) << "\n"; 479193326Sed return false; 480193326Sed } 481193326Sed 482194613Sed if (C.getArgs().hasArg(options::OPT_print_multi_lib)) { 483194613Sed // FIXME: We need tool chain support for this. 484194613Sed llvm::outs() << ".;\n"; 485194613Sed 486194613Sed switch (C.getDefaultToolChain().getTriple().getArch()) { 487194613Sed default: 488194613Sed break; 489198092Srdivacky 490194613Sed case llvm::Triple::x86_64: 491194613Sed llvm::outs() << "x86_64;@m64" << "\n"; 492194613Sed break; 493194613Sed 494194613Sed case llvm::Triple::ppc64: 495194613Sed llvm::outs() << "ppc64;@m64" << "\n"; 496194613Sed break; 497194613Sed } 498194613Sed return false; 499194613Sed } 500194613Sed 501194613Sed // FIXME: What is the difference between print-multi-directory and 502194613Sed // print-multi-os-directory? 503194613Sed if (C.getArgs().hasArg(options::OPT_print_multi_directory) || 504194613Sed C.getArgs().hasArg(options::OPT_print_multi_os_directory)) { 505194613Sed switch (C.getDefaultToolChain().getTriple().getArch()) { 506194613Sed default: 507194613Sed case llvm::Triple::x86: 508194613Sed case llvm::Triple::ppc: 509194613Sed llvm::outs() << "." << "\n"; 510194613Sed break; 511198092Srdivacky 512194613Sed case llvm::Triple::x86_64: 513194613Sed llvm::outs() << "x86_64" << "\n"; 514194613Sed break; 515194613Sed 516194613Sed case llvm::Triple::ppc64: 517194613Sed llvm::outs() << "ppc64" << "\n"; 518194613Sed break; 519194613Sed } 520194613Sed return false; 521194613Sed } 522194613Sed 523193326Sed return true; 524193326Sed} 525193326Sed 526198092Srdivackystatic unsigned PrintActions1(const Compilation &C, Action *A, 527193326Sed std::map<Action*, unsigned> &Ids) { 528193326Sed if (Ids.count(A)) 529193326Sed return Ids[A]; 530198092Srdivacky 531193326Sed std::string str; 532193326Sed llvm::raw_string_ostream os(str); 533198092Srdivacky 534193326Sed os << Action::getClassName(A->getKind()) << ", "; 535198092Srdivacky if (InputAction *IA = dyn_cast<InputAction>(A)) { 536193326Sed os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\""; 537193326Sed } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) { 538198092Srdivacky os << '"' << (BIA->getArchName() ? BIA->getArchName() : 539193326Sed C.getDefaultToolChain().getArchName()) << '"' 540193326Sed << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}"; 541193326Sed } else { 542193326Sed os << "{"; 543193326Sed for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) { 544193326Sed os << PrintActions1(C, *it, Ids); 545193326Sed ++it; 546193326Sed if (it != ie) 547193326Sed os << ", "; 548193326Sed } 549193326Sed os << "}"; 550193326Sed } 551193326Sed 552193326Sed unsigned Id = Ids.size(); 553193326Sed Ids[A] = Id; 554198092Srdivacky llvm::errs() << Id << ": " << os.str() << ", " 555193326Sed << types::getTypeName(A->getType()) << "\n"; 556193326Sed 557193326Sed return Id; 558193326Sed} 559193326Sed 560193326Sedvoid Driver::PrintActions(const Compilation &C) const { 561193326Sed std::map<Action*, unsigned> Ids; 562198092Srdivacky for (ActionList::const_iterator it = C.getActions().begin(), 563193326Sed ie = C.getActions().end(); it != ie; ++it) 564193326Sed PrintActions1(C, *it, Ids); 565193326Sed} 566193326Sed 567198092Srdivackyvoid Driver::BuildUniversalActions(const ArgList &Args, 568193326Sed ActionList &Actions) const { 569198092Srdivacky llvm::PrettyStackTraceString CrashInfo("Building universal build actions"); 570198092Srdivacky // Collect the list of architectures. Duplicates are allowed, but should only 571198092Srdivacky // be handled once (in the order seen). 572193326Sed llvm::StringSet<> ArchNames; 573193326Sed llvm::SmallVector<const char *, 4> Archs; 574198092Srdivacky for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); 575193326Sed it != ie; ++it) { 576193326Sed Arg *A = *it; 577193326Sed 578199512Srdivacky if (A->getOption().matches(options::OPT_arch)) { 579198092Srdivacky // Validate the option here; we don't save the type here because its 580198092Srdivacky // particular spelling may participate in other driver choices. 581198092Srdivacky llvm::Triple::ArchType Arch = 582198092Srdivacky llvm::Triple::getArchTypeForDarwinArchName(A->getValue(Args)); 583198092Srdivacky if (Arch == llvm::Triple::UnknownArch) { 584198092Srdivacky Diag(clang::diag::err_drv_invalid_arch_name) 585198092Srdivacky << A->getAsString(Args); 586198092Srdivacky continue; 587198092Srdivacky } 588193326Sed 589193326Sed A->claim(); 590198092Srdivacky if (ArchNames.insert(A->getValue(Args))) 591198092Srdivacky Archs.push_back(A->getValue(Args)); 592193326Sed } 593193326Sed } 594193326Sed 595198092Srdivacky // When there is no explicit arch for this platform, make sure we still bind 596198092Srdivacky // the architecture (to the default) so that -Xarch_ is handled correctly. 597193326Sed if (!Archs.size()) 598193326Sed Archs.push_back(0); 599193326Sed 600198092Srdivacky // FIXME: We killed off some others but these aren't yet detected in a 601198092Srdivacky // functional manner. If we added information to jobs about which "auxiliary" 602198092Srdivacky // files they wrote then we could detect the conflict these cause downstream. 603193326Sed if (Archs.size() > 1) { 604193326Sed // No recovery needed, the point of this is just to prevent 605193326Sed // overwriting the same files. 606193326Sed if (const Arg *A = Args.getLastArg(options::OPT_save_temps)) 607198092Srdivacky Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs) 608193326Sed << A->getAsString(Args); 609193326Sed } 610193326Sed 611193326Sed ActionList SingleActions; 612193326Sed BuildActions(Args, SingleActions); 613193326Sed 614198092Srdivacky // Add in arch binding and lipo (if necessary) for every top level action. 615193326Sed for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) { 616193326Sed Action *Act = SingleActions[i]; 617193326Sed 618198092Srdivacky // Make sure we can lipo this kind of output. If not (and it is an actual 619198092Srdivacky // output) then we disallow, since we can't create an output file with the 620198092Srdivacky // right name without overwriting it. We could remove this oddity by just 621198092Srdivacky // changing the output names to include the arch, which would also fix 622193326Sed // -save-temps. Compatibility wins for now. 623193326Sed 624193326Sed if (Archs.size() > 1 && !types::canLipoType(Act->getType())) 625193326Sed Diag(clang::diag::err_drv_invalid_output_with_multiple_archs) 626193326Sed << types::getTypeName(Act->getType()); 627193326Sed 628193326Sed ActionList Inputs; 629193326Sed for (unsigned i = 0, e = Archs.size(); i != e; ++i) 630193326Sed Inputs.push_back(new BindArchAction(Act, Archs[i])); 631193326Sed 632198092Srdivacky // Lipo if necessary, we do it this way because we need to set the arch flag 633198092Srdivacky // so that -Xarch_ gets overwritten. 634193326Sed if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing) 635193326Sed Actions.append(Inputs.begin(), Inputs.end()); 636193326Sed else 637193326Sed Actions.push_back(new LipoJobAction(Inputs, Act->getType())); 638193326Sed } 639193326Sed} 640193326Sed 641193326Sedvoid Driver::BuildActions(const ArgList &Args, ActionList &Actions) const { 642193326Sed llvm::PrettyStackTraceString CrashInfo("Building compilation actions"); 643193326Sed // Start by constructing the list of inputs and their types. 644193326Sed 645198092Srdivacky // Track the current user specified (-x) input. We also explicitly track the 646198092Srdivacky // argument used to set the type; we only want to claim the type when we 647198092Srdivacky // actually use it, so we warn about unused -x arguments. 648193326Sed types::ID InputType = types::TY_Nothing; 649193326Sed Arg *InputTypeArg = 0; 650193326Sed 651193326Sed llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs; 652198092Srdivacky for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); 653193326Sed it != ie; ++it) { 654193326Sed Arg *A = *it; 655193326Sed 656193326Sed if (isa<InputOption>(A->getOption())) { 657193326Sed const char *Value = A->getValue(Args); 658193326Sed types::ID Ty = types::TY_INVALID; 659193326Sed 660193326Sed // Infer the input type if necessary. 661193326Sed if (InputType == types::TY_Nothing) { 662193326Sed // If there was an explicit arg for this, claim it. 663193326Sed if (InputTypeArg) 664193326Sed InputTypeArg->claim(); 665193326Sed 666193326Sed // stdin must be handled specially. 667193326Sed if (memcmp(Value, "-", 2) == 0) { 668198092Srdivacky // If running with -E, treat as a C input (this changes the builtin 669198092Srdivacky // macros, for example). This may be overridden by -ObjC below. 670193326Sed // 671198092Srdivacky // Otherwise emit an error but still use a valid type to avoid 672198092Srdivacky // spurious errors (e.g., no inputs). 673199512Srdivacky if (!Args.hasArgNoClaim(options::OPT_E)) 674193326Sed Diag(clang::diag::err_drv_unknown_stdin_type); 675193326Sed Ty = types::TY_C; 676193326Sed } else { 677198092Srdivacky // Otherwise lookup by extension, and fallback to ObjectType if not 678198092Srdivacky // found. We use a host hook here because Darwin at least has its own 679198092Srdivacky // idea of what .s is. 680193326Sed if (const char *Ext = strrchr(Value, '.')) 681193326Sed Ty = Host->lookupTypeForExtension(Ext + 1); 682193326Sed 683193326Sed if (Ty == types::TY_INVALID) 684193326Sed Ty = types::TY_Object; 685193326Sed } 686193326Sed 687193326Sed // -ObjC and -ObjC++ override the default language, but only for "source 688193326Sed // files". We just treat everything that isn't a linker input as a 689193326Sed // source file. 690198092Srdivacky // 691193326Sed // FIXME: Clean this up if we move the phase sequence into the type. 692193326Sed if (Ty != types::TY_Object) { 693193326Sed if (Args.hasArg(options::OPT_ObjC)) 694193326Sed Ty = types::TY_ObjC; 695193326Sed else if (Args.hasArg(options::OPT_ObjCXX)) 696193326Sed Ty = types::TY_ObjCXX; 697193326Sed } 698193326Sed } else { 699193326Sed assert(InputTypeArg && "InputType set w/o InputTypeArg"); 700193326Sed InputTypeArg->claim(); 701193326Sed Ty = InputType; 702193326Sed } 703193326Sed 704198092Srdivacky // Check that the file exists. It isn't clear this is worth doing, since 705198092Srdivacky // the tool presumably does this anyway, and this just adds an extra stat 706198092Srdivacky // to the equation, but this is gcc compatible. 707193326Sed if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists()) 708193326Sed Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args); 709193326Sed else 710193326Sed Inputs.push_back(std::make_pair(Ty, A)); 711193326Sed 712193326Sed } else if (A->getOption().isLinkerInput()) { 713198092Srdivacky // Just treat as object type, we could make a special type for this if 714198092Srdivacky // necessary. 715193326Sed Inputs.push_back(std::make_pair(types::TY_Object, A)); 716193326Sed 717199512Srdivacky } else if (A->getOption().matches(options::OPT_x)) { 718198092Srdivacky InputTypeArg = A; 719193326Sed InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args)); 720193326Sed 721193326Sed // Follow gcc behavior and treat as linker input for invalid -x 722198092Srdivacky // options. Its not clear why we shouldn't just revert to unknown; but 723198092Srdivacky // this isn't very important, we might as well be bug comatible. 724193326Sed if (!InputType) { 725193326Sed Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args); 726193326Sed InputType = types::TY_Object; 727193326Sed } 728193326Sed } 729193326Sed } 730193326Sed 731193326Sed if (!SuppressMissingInputWarning && Inputs.empty()) { 732193326Sed Diag(clang::diag::err_drv_no_input_files); 733193326Sed return; 734193326Sed } 735193326Sed 736198092Srdivacky // Determine which compilation mode we are in. We look for options which 737198092Srdivacky // affect the phase, starting with the earliest phases, and record which 738198092Srdivacky // option we used to determine the final phase. 739193326Sed Arg *FinalPhaseArg = 0; 740193326Sed phases::ID FinalPhase; 741193326Sed 742193326Sed // -{E,M,MM} only run the preprocessor. 743193326Sed if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) || 744193326Sed (FinalPhaseArg = Args.getLastArg(options::OPT_M)) || 745193326Sed (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) { 746193326Sed FinalPhase = phases::Preprocess; 747198092Srdivacky 748198092Srdivacky // -{fsyntax-only,-analyze,emit-ast,S} only run up to the compiler. 749193326Sed } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) || 750193326Sed (FinalPhaseArg = Args.getLastArg(options::OPT__analyze, 751193326Sed options::OPT__analyze_auto)) || 752198092Srdivacky (FinalPhaseArg = Args.getLastArg(options::OPT_emit_ast)) || 753193326Sed (FinalPhaseArg = Args.getLastArg(options::OPT_S))) { 754193326Sed FinalPhase = phases::Compile; 755193326Sed 756193326Sed // -c only runs up to the assembler. 757193326Sed } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) { 758193326Sed FinalPhase = phases::Assemble; 759198092Srdivacky 760193326Sed // Otherwise do everything. 761193326Sed } else 762193326Sed FinalPhase = phases::Link; 763193326Sed 764198092Srdivacky // Reject -Z* at the top level, these options should never have been exposed 765198092Srdivacky // by gcc. 766193326Sed if (Arg *A = Args.getLastArg(options::OPT_Z_Joined)) 767193326Sed Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args); 768193326Sed 769193326Sed // Construct the actions to perform. 770193326Sed ActionList LinkerInputs; 771193326Sed for (unsigned i = 0, e = Inputs.size(); i != e; ++i) { 772193326Sed types::ID InputType = Inputs[i].first; 773193326Sed const Arg *InputArg = Inputs[i].second; 774193326Sed 775193326Sed unsigned NumSteps = types::getNumCompilationPhases(InputType); 776193326Sed assert(NumSteps && "Invalid number of steps!"); 777193326Sed 778198092Srdivacky // If the first step comes after the final phase we are doing as part of 779198092Srdivacky // this compilation, warn the user about it. 780193326Sed phases::ID InitialPhase = types::getCompilationPhase(InputType, 0); 781193326Sed if (InitialPhase > FinalPhase) { 782193326Sed // Claim here to avoid the more general unused warning. 783193326Sed InputArg->claim(); 784198092Srdivacky 785198092Srdivacky // Special case '-E' warning on a previously preprocessed file to make 786198092Srdivacky // more sense. 787198092Srdivacky if (InitialPhase == phases::Compile && FinalPhase == phases::Preprocess && 788198092Srdivacky getPreprocessedType(InputType) == types::TY_INVALID) 789198092Srdivacky Diag(clang::diag::warn_drv_preprocessed_input_file_unused) 790198092Srdivacky << InputArg->getAsString(Args) 791198092Srdivacky << FinalPhaseArg->getOption().getName(); 792198092Srdivacky else 793198092Srdivacky Diag(clang::diag::warn_drv_input_file_unused) 794198092Srdivacky << InputArg->getAsString(Args) 795198092Srdivacky << getPhaseName(InitialPhase) 796198092Srdivacky << FinalPhaseArg->getOption().getName(); 797193326Sed continue; 798193326Sed } 799198092Srdivacky 800193326Sed // Build the pipeline for this file. 801193326Sed Action *Current = new InputAction(*InputArg, InputType); 802193326Sed for (unsigned i = 0; i != NumSteps; ++i) { 803193326Sed phases::ID Phase = types::getCompilationPhase(InputType, i); 804193326Sed 805193326Sed // We are done if this step is past what the user requested. 806193326Sed if (Phase > FinalPhase) 807193326Sed break; 808193326Sed 809193326Sed // Queue linker inputs. 810193326Sed if (Phase == phases::Link) { 811193326Sed assert(i + 1 == NumSteps && "linking must be final compilation step."); 812193326Sed LinkerInputs.push_back(Current); 813193326Sed Current = 0; 814193326Sed break; 815193326Sed } 816193326Sed 817198092Srdivacky // Some types skip the assembler phase (e.g., llvm-bc), but we can't 818198092Srdivacky // encode this in the steps because the intermediate type depends on 819198092Srdivacky // arguments. Just special case here. 820193326Sed if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm) 821193326Sed continue; 822193326Sed 823193326Sed // Otherwise construct the appropriate action. 824193326Sed Current = ConstructPhaseAction(Args, Phase, Current); 825193326Sed if (Current->getType() == types::TY_Nothing) 826193326Sed break; 827193326Sed } 828193326Sed 829193326Sed // If we ended with something, add to the output list. 830193326Sed if (Current) 831193326Sed Actions.push_back(Current); 832193326Sed } 833193326Sed 834193326Sed // Add a link action if necessary. 835193326Sed if (!LinkerInputs.empty()) 836193326Sed Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image)); 837193326Sed} 838193326Sed 839193326SedAction *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase, 840193326Sed Action *Input) const { 841193326Sed llvm::PrettyStackTraceString CrashInfo("Constructing phase actions"); 842193326Sed // Build the appropriate action. 843193326Sed switch (Phase) { 844193326Sed case phases::Link: assert(0 && "link action invalid here."); 845193326Sed case phases::Preprocess: { 846193326Sed types::ID OutputTy; 847193326Sed // -{M, MM} alter the output type. 848193326Sed if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) { 849193326Sed OutputTy = types::TY_Dependencies; 850193326Sed } else { 851193326Sed OutputTy = types::getPreprocessedType(Input->getType()); 852193326Sed assert(OutputTy != types::TY_INVALID && 853193326Sed "Cannot preprocess this input type!"); 854193326Sed } 855193326Sed return new PreprocessJobAction(Input, OutputTy); 856193326Sed } 857193326Sed case phases::Precompile: 858198092Srdivacky return new PrecompileJobAction(Input, types::TY_PCH); 859193326Sed case phases::Compile: { 860193326Sed if (Args.hasArg(options::OPT_fsyntax_only)) { 861193326Sed return new CompileJobAction(Input, types::TY_Nothing); 862193326Sed } else if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) { 863193326Sed return new AnalyzeJobAction(Input, types::TY_Plist); 864198092Srdivacky } else if (Args.hasArg(options::OPT_emit_ast)) { 865198092Srdivacky return new CompileJobAction(Input, types::TY_AST); 866193326Sed } else if (Args.hasArg(options::OPT_emit_llvm) || 867193326Sed Args.hasArg(options::OPT_flto) || 868193326Sed Args.hasArg(options::OPT_O4)) { 869198092Srdivacky types::ID Output = 870193326Sed Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC; 871193326Sed return new CompileJobAction(Input, Output); 872193326Sed } else { 873193326Sed return new CompileJobAction(Input, types::TY_PP_Asm); 874193326Sed } 875193326Sed } 876193326Sed case phases::Assemble: 877193326Sed return new AssembleJobAction(Input, types::TY_Object); 878193326Sed } 879193326Sed 880193326Sed assert(0 && "invalid phase in ConstructPhaseAction"); 881193326Sed return 0; 882193326Sed} 883193326Sed 884193326Sedvoid Driver::BuildJobs(Compilation &C) const { 885193326Sed llvm::PrettyStackTraceString CrashInfo("Building compilation jobs"); 886193326Sed bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps); 887193326Sed bool UsePipes = C.getArgs().hasArg(options::OPT_pipe); 888193326Sed 889198092Srdivacky // FIXME: Pipes are forcibly disabled until we support executing them. 890193326Sed if (!CCCPrintBindings) 891193326Sed UsePipes = false; 892198092Srdivacky 893193326Sed // -save-temps inhibits pipes. 894193326Sed if (SaveTemps && UsePipes) { 895193326Sed Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps); 896193326Sed UsePipes = true; 897193326Sed } 898193326Sed 899193326Sed Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o); 900193326Sed 901198092Srdivacky // It is an error to provide a -o option if we are making multiple output 902198092Srdivacky // files. 903193326Sed if (FinalOutput) { 904193326Sed unsigned NumOutputs = 0; 905198092Srdivacky for (ActionList::const_iterator it = C.getActions().begin(), 906193326Sed ie = C.getActions().end(); it != ie; ++it) 907193326Sed if ((*it)->getType() != types::TY_Nothing) 908193326Sed ++NumOutputs; 909198092Srdivacky 910193326Sed if (NumOutputs > 1) { 911193326Sed Diag(clang::diag::err_drv_output_argument_with_multiple_files); 912193326Sed FinalOutput = 0; 913193326Sed } 914193326Sed } 915193326Sed 916198092Srdivacky for (ActionList::const_iterator it = C.getActions().begin(), 917193326Sed ie = C.getActions().end(); it != ie; ++it) { 918193326Sed Action *A = *it; 919193326Sed 920198092Srdivacky // If we are linking an image for multiple archs then the linker wants 921198092Srdivacky // -arch_multiple and -final_output <final image name>. Unfortunately, this 922198092Srdivacky // doesn't fit in cleanly because we have to pass this information down. 923193326Sed // 924198092Srdivacky // FIXME: This is a hack; find a cleaner way to integrate this into the 925198092Srdivacky // process. 926193326Sed const char *LinkingOutput = 0; 927193326Sed if (isa<LipoJobAction>(A)) { 928193326Sed if (FinalOutput) 929193326Sed LinkingOutput = FinalOutput->getValue(C.getArgs()); 930193326Sed else 931193326Sed LinkingOutput = DefaultImageName.c_str(); 932193326Sed } 933193326Sed 934193326Sed InputInfo II; 935198092Srdivacky BuildJobsForAction(C, A, &C.getDefaultToolChain(), 936198092Srdivacky /*BoundArch*/0, 937193326Sed /*CanAcceptPipe*/ true, 938193326Sed /*AtTopLevel*/ true, 939193326Sed /*LinkingOutput*/ LinkingOutput, 940193326Sed II); 941193326Sed } 942193326Sed 943198092Srdivacky // If the user passed -Qunused-arguments or there were errors, don't warn 944198092Srdivacky // about any unused arguments. 945198092Srdivacky if (Diags.getNumErrors() || 946193326Sed C.getArgs().hasArg(options::OPT_Qunused_arguments)) 947193326Sed return; 948193326Sed 949193326Sed // Claim -### here. 950193326Sed (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH); 951198092Srdivacky 952193326Sed for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end(); 953193326Sed it != ie; ++it) { 954193326Sed Arg *A = *it; 955198092Srdivacky 956193326Sed // FIXME: It would be nice to be able to send the argument to the 957198092Srdivacky // Diagnostic, so that extra values, position, and so on could be printed. 958193326Sed if (!A->isClaimed()) { 959193326Sed if (A->getOption().hasNoArgumentUnused()) 960193326Sed continue; 961193326Sed 962198092Srdivacky // Suppress the warning automatically if this is just a flag, and it is an 963198092Srdivacky // instance of an argument we already claimed. 964193326Sed const Option &Opt = A->getOption(); 965193326Sed if (isa<FlagOption>(Opt)) { 966193326Sed bool DuplicateClaimed = false; 967193326Sed 968193326Sed // FIXME: Use iterator. 969198092Srdivacky for (ArgList::const_iterator it = C.getArgs().begin(), 970193326Sed ie = C.getArgs().end(); it != ie; ++it) { 971199512Srdivacky if ((*it)->isClaimed() && (*it)->getOption().matches(&Opt)) { 972193326Sed DuplicateClaimed = true; 973193326Sed break; 974193326Sed } 975193326Sed } 976193326Sed 977193326Sed if (DuplicateClaimed) 978193326Sed continue; 979193326Sed } 980193326Sed 981198092Srdivacky Diag(clang::diag::warn_drv_unused_argument) 982193326Sed << A->getAsString(C.getArgs()); 983193326Sed } 984193326Sed } 985193326Sed} 986193326Sed 987193326Sedvoid Driver::BuildJobsForAction(Compilation &C, 988193326Sed const Action *A, 989193326Sed const ToolChain *TC, 990198092Srdivacky const char *BoundArch, 991193326Sed bool CanAcceptPipe, 992193326Sed bool AtTopLevel, 993193326Sed const char *LinkingOutput, 994193326Sed InputInfo &Result) const { 995198092Srdivacky llvm::PrettyStackTraceString CrashInfo("Building compilation jobs"); 996193326Sed 997193326Sed bool UsePipes = C.getArgs().hasArg(options::OPT_pipe); 998198092Srdivacky // FIXME: Pipes are forcibly disabled until we support executing them. 999193326Sed if (!CCCPrintBindings) 1000193326Sed UsePipes = false; 1001193326Sed 1002193326Sed if (const InputAction *IA = dyn_cast<InputAction>(A)) { 1003198092Srdivacky // FIXME: It would be nice to not claim this here; maybe the old scheme of 1004198092Srdivacky // just using Args was better? 1005193326Sed const Arg &Input = IA->getInputArg(); 1006193326Sed Input.claim(); 1007193326Sed if (isa<PositionalArg>(Input)) { 1008193326Sed const char *Name = Input.getValue(C.getArgs()); 1009193326Sed Result = InputInfo(Name, A->getType(), Name); 1010193326Sed } else 1011193326Sed Result = InputInfo(&Input, A->getType(), ""); 1012193326Sed return; 1013193326Sed } 1014193326Sed 1015193326Sed if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) { 1016198092Srdivacky const ToolChain *TC = &C.getDefaultToolChain(); 1017198092Srdivacky 1018193326Sed std::string Arch; 1019198092Srdivacky if (BAA->getArchName()) 1020198092Srdivacky TC = Host->CreateToolChain(C.getArgs(), BAA->getArchName()); 1021198092Srdivacky 1022198092Srdivacky BuildJobsForAction(C, *BAA->begin(), TC, BAA->getArchName(), 1023198092Srdivacky CanAcceptPipe, AtTopLevel, LinkingOutput, Result); 1024193326Sed return; 1025193326Sed } 1026193326Sed 1027193326Sed const JobAction *JA = cast<JobAction>(A); 1028193326Sed const Tool &T = TC->SelectTool(C, *JA); 1029198092Srdivacky 1030198092Srdivacky // See if we should use an integrated preprocessor. We do so when we have 1031198092Srdivacky // exactly one input, since this is the only use case we care about 1032198092Srdivacky // (irrelevant since we don't support combine yet). 1033193326Sed bool UseIntegratedCPP = false; 1034193326Sed const ActionList *Inputs = &A->getInputs(); 1035193326Sed if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) { 1036193326Sed if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) && 1037193326Sed !C.getArgs().hasArg(options::OPT_traditional_cpp) && 1038193326Sed !C.getArgs().hasArg(options::OPT_save_temps) && 1039193326Sed T.hasIntegratedCPP()) { 1040193326Sed UseIntegratedCPP = true; 1041193326Sed Inputs = &(*Inputs)[0]->getInputs(); 1042193326Sed } 1043193326Sed } 1044193326Sed 1045193326Sed // Only use pipes when there is exactly one input. 1046193326Sed bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput(); 1047193326Sed InputInfoList InputInfos; 1048193326Sed for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end(); 1049193326Sed it != ie; ++it) { 1050193326Sed InputInfo II; 1051198092Srdivacky BuildJobsForAction(C, *it, TC, BoundArch, TryToUsePipeInput, 1052198092Srdivacky /*AtTopLevel*/false, LinkingOutput, II); 1053193326Sed InputInfos.push_back(II); 1054193326Sed } 1055193326Sed 1056193326Sed // Determine if we should output to a pipe. 1057193326Sed bool OutputToPipe = false; 1058193326Sed if (CanAcceptPipe && T.canPipeOutput()) { 1059198092Srdivacky // Some actions default to writing to a pipe if they are the top level phase 1060198092Srdivacky // and there was no user override. 1061193326Sed // 1062193326Sed // FIXME: Is there a better way to handle this? 1063193326Sed if (AtTopLevel) { 1064193326Sed if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o)) 1065193326Sed OutputToPipe = true; 1066193326Sed } else if (UsePipes) 1067193326Sed OutputToPipe = true; 1068193326Sed } 1069193326Sed 1070193326Sed // Figure out where to put the job (pipes). 1071193326Sed Job *Dest = &C.getJobs(); 1072193326Sed if (InputInfos[0].isPipe()) { 1073193326Sed assert(TryToUsePipeInput && "Unrequested pipe!"); 1074193326Sed assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs."); 1075193326Sed Dest = &InputInfos[0].getPipe(); 1076193326Sed } 1077193326Sed 1078193326Sed // Always use the first input as the base input. 1079193326Sed const char *BaseInput = InputInfos[0].getBaseInput(); 1080193326Sed 1081198092Srdivacky // Determine the place to write output to (nothing, pipe, or filename) and 1082198092Srdivacky // where to put the new job. 1083193326Sed if (JA->getType() == types::TY_Nothing) { 1084193326Sed Result = InputInfo(A->getType(), BaseInput); 1085193326Sed } else if (OutputToPipe) { 1086193326Sed // Append to current piped job or create a new one as appropriate. 1087193326Sed PipedJob *PJ = dyn_cast<PipedJob>(Dest); 1088193326Sed if (!PJ) { 1089193326Sed PJ = new PipedJob(); 1090198092Srdivacky // FIXME: Temporary hack so that -ccc-print-bindings work until we have 1091198092Srdivacky // pipe support. Please remove later. 1092193326Sed if (!CCCPrintBindings) 1093193326Sed cast<JobList>(Dest)->addJob(PJ); 1094193326Sed Dest = PJ; 1095193326Sed } 1096193326Sed Result = InputInfo(PJ, A->getType(), BaseInput); 1097193326Sed } else { 1098193326Sed Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel), 1099193326Sed A->getType(), BaseInput); 1100193326Sed } 1101193326Sed 1102193326Sed if (CCCPrintBindings) { 1103193326Sed llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"' 1104193326Sed << " - \"" << T.getName() << "\", inputs: ["; 1105193326Sed for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) { 1106193326Sed llvm::errs() << InputInfos[i].getAsString(); 1107193326Sed if (i + 1 != e) 1108193326Sed llvm::errs() << ", "; 1109193326Sed } 1110193326Sed llvm::errs() << "], output: " << Result.getAsString() << "\n"; 1111193326Sed } else { 1112198092Srdivacky T.ConstructJob(C, *JA, *Dest, Result, InputInfos, 1113198092Srdivacky C.getArgsForToolChain(TC, BoundArch), LinkingOutput); 1114193326Sed } 1115193326Sed} 1116193326Sed 1117198092Srdivackyconst char *Driver::GetNamedOutputPath(Compilation &C, 1118193326Sed const JobAction &JA, 1119193326Sed const char *BaseInput, 1120193326Sed bool AtTopLevel) const { 1121193326Sed llvm::PrettyStackTraceString CrashInfo("Computing output path"); 1122193326Sed // Output to a user requested destination? 1123193326Sed if (AtTopLevel) { 1124193326Sed if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) 1125193326Sed return C.addResultFile(FinalOutput->getValue(C.getArgs())); 1126193326Sed } 1127193326Sed 1128193326Sed // Output to a temporary file? 1129193326Sed if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) { 1130198092Srdivacky std::string TmpName = 1131193326Sed GetTemporaryPath(types::getTypeTempSuffix(JA.getType())); 1132193326Sed return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str())); 1133193326Sed } 1134193326Sed 1135193326Sed llvm::sys::Path BasePath(BaseInput); 1136193326Sed std::string BaseName(BasePath.getLast()); 1137193326Sed 1138193326Sed // Determine what the derived output name should be. 1139193326Sed const char *NamedOutput; 1140193326Sed if (JA.getType() == types::TY_Image) { 1141193326Sed NamedOutput = DefaultImageName.c_str(); 1142193326Sed } else { 1143193326Sed const char *Suffix = types::getTypeTempSuffix(JA.getType()); 1144193326Sed assert(Suffix && "All types used for output should have a suffix."); 1145193326Sed 1146193326Sed std::string::size_type End = std::string::npos; 1147193326Sed if (!types::appendSuffixForType(JA.getType())) 1148193326Sed End = BaseName.rfind('.'); 1149193326Sed std::string Suffixed(BaseName.substr(0, End)); 1150193326Sed Suffixed += '.'; 1151193326Sed Suffixed += Suffix; 1152193326Sed NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str()); 1153193326Sed } 1154193326Sed 1155198092Srdivacky // As an annoying special case, PCH generation doesn't strip the pathname. 1156193326Sed if (JA.getType() == types::TY_PCH) { 1157193326Sed BasePath.eraseComponent(); 1158193326Sed if (BasePath.isEmpty()) 1159193326Sed BasePath = NamedOutput; 1160193326Sed else 1161193326Sed BasePath.appendComponent(NamedOutput); 1162193326Sed return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str())); 1163193326Sed } else { 1164193326Sed return C.addResultFile(NamedOutput); 1165193326Sed } 1166193326Sed} 1167193326Sed 1168198092Srdivackystd::string Driver::GetFilePath(const char *Name, const ToolChain &TC) const { 1169193326Sed const ToolChain::path_list &List = TC.getFilePaths(); 1170198092Srdivacky for (ToolChain::path_list::const_iterator 1171193326Sed it = List.begin(), ie = List.end(); it != ie; ++it) { 1172193326Sed llvm::sys::Path P(*it); 1173193326Sed P.appendComponent(Name); 1174193326Sed if (P.exists()) 1175198092Srdivacky return P.str(); 1176193326Sed } 1177193326Sed 1178198092Srdivacky return Name; 1179193326Sed} 1180193326Sed 1181198092Srdivackystd::string Driver::GetProgramPath(const char *Name, const ToolChain &TC, 1182198092Srdivacky bool WantFile) const { 1183193326Sed const ToolChain::path_list &List = TC.getProgramPaths(); 1184198092Srdivacky for (ToolChain::path_list::const_iterator 1185193326Sed it = List.begin(), ie = List.end(); it != ie; ++it) { 1186193326Sed llvm::sys::Path P(*it); 1187193326Sed P.appendComponent(Name); 1188193326Sed if (WantFile ? P.exists() : P.canExecute()) 1189198092Srdivacky return P.str(); 1190193326Sed } 1191193326Sed 1192193326Sed // If all else failed, search the path. 1193193326Sed llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name)); 1194193326Sed if (!P.empty()) 1195198092Srdivacky return P.str(); 1196193326Sed 1197198092Srdivacky return Name; 1198193326Sed} 1199193326Sed 1200193326Sedstd::string Driver::GetTemporaryPath(const char *Suffix) const { 1201198092Srdivacky // FIXME: This is lame; sys::Path should provide this function (in particular, 1202198092Srdivacky // it should know how to find the temporary files dir). 1203193326Sed std::string Error; 1204193326Sed const char *TmpDir = ::getenv("TMPDIR"); 1205193326Sed if (!TmpDir) 1206193326Sed TmpDir = ::getenv("TEMP"); 1207193326Sed if (!TmpDir) 1208193326Sed TmpDir = ::getenv("TMP"); 1209193326Sed if (!TmpDir) 1210193326Sed TmpDir = "/tmp"; 1211193326Sed llvm::sys::Path P(TmpDir); 1212193326Sed P.appendComponent("cc"); 1213193326Sed if (P.makeUnique(false, &Error)) { 1214193326Sed Diag(clang::diag::err_drv_unable_to_make_temp) << Error; 1215193326Sed return ""; 1216193326Sed } 1217193326Sed 1218198092Srdivacky // FIXME: Grumble, makeUnique sometimes leaves the file around!? PR3837. 1219193326Sed P.eraseFromDisk(false, 0); 1220193326Sed 1221193326Sed P.appendSuffix(Suffix); 1222198092Srdivacky return P.str(); 1223193326Sed} 1224193326Sed 1225193326Sedconst HostInfo *Driver::GetHostInfo(const char *TripleStr) const { 1226193326Sed llvm::PrettyStackTraceString CrashInfo("Constructing host"); 1227193326Sed llvm::Triple Triple(TripleStr); 1228193326Sed 1229193326Sed switch (Triple.getOS()) { 1230198092Srdivacky case llvm::Triple::AuroraUX: 1231198092Srdivacky return createAuroraUXHostInfo(*this, Triple); 1232193326Sed case llvm::Triple::Darwin: 1233193326Sed return createDarwinHostInfo(*this, Triple); 1234193326Sed case llvm::Triple::DragonFly: 1235193326Sed return createDragonFlyHostInfo(*this, Triple); 1236195341Sed case llvm::Triple::OpenBSD: 1237195341Sed return createOpenBSDHostInfo(*this, Triple); 1238193326Sed case llvm::Triple::FreeBSD: 1239193326Sed return createFreeBSDHostInfo(*this, Triple); 1240193326Sed case llvm::Triple::Linux: 1241193326Sed return createLinuxHostInfo(*this, Triple); 1242193326Sed default: 1243193326Sed return createUnknownHostInfo(*this, Triple); 1244193326Sed } 1245193326Sed} 1246193326Sed 1247193326Sedbool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA, 1248198092Srdivacky const llvm::Triple &Triple) const { 1249198092Srdivacky // Check if user requested no clang, or clang doesn't understand this type (we 1250198092Srdivacky // only handle single inputs for now). 1251198092Srdivacky if (!CCCUseClang || JA.size() != 1 || 1252193326Sed !types::isAcceptedByClang((*JA.begin())->getType())) 1253193326Sed return false; 1254193326Sed 1255193326Sed // Otherwise make sure this is an action clang understands. 1256193326Sed if (isa<PreprocessJobAction>(JA)) { 1257193326Sed if (!CCCUseClangCPP) { 1258193326Sed Diag(clang::diag::warn_drv_not_using_clang_cpp); 1259193326Sed return false; 1260193326Sed } 1261193326Sed } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA)) 1262193326Sed return false; 1263193326Sed 1264193326Sed // Use clang for C++? 1265193326Sed if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) { 1266193326Sed Diag(clang::diag::warn_drv_not_using_clang_cxx); 1267193326Sed return false; 1268193326Sed } 1269193326Sed 1270198092Srdivacky // Always use clang for precompiling and AST generation, regardless of archs. 1271198092Srdivacky if (isa<PrecompileJobAction>(JA) || JA.getType() == types::TY_AST) 1272193326Sed return true; 1273193326Sed 1274198092Srdivacky // Finally, don't use clang if this isn't one of the user specified archs to 1275198092Srdivacky // build. 1276198092Srdivacky if (!CCCClangArchs.empty() && !CCCClangArchs.count(Triple.getArch())) { 1277198092Srdivacky Diag(clang::diag::warn_drv_not_using_clang_arch) << Triple.getArchName(); 1278193326Sed return false; 1279193326Sed } 1280193326Sed 1281193326Sed return true; 1282193326Sed} 1283193326Sed 1284198092Srdivacky/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the 1285198092Srdivacky/// grouped values as integers. Numbers which are not provided are set to 0. 1286193326Sed/// 1287198092Srdivacky/// \return True if the entire string was parsed (9.2), or all groups were 1288198092Srdivacky/// parsed (10.3.5extrastuff). 1289198092Srdivackybool Driver::GetReleaseVersion(const char *Str, unsigned &Major, 1290193326Sed unsigned &Minor, unsigned &Micro, 1291193326Sed bool &HadExtra) { 1292193326Sed HadExtra = false; 1293193326Sed 1294193326Sed Major = Minor = Micro = 0; 1295198092Srdivacky if (*Str == '\0') 1296193326Sed return true; 1297193326Sed 1298193326Sed char *End; 1299193326Sed Major = (unsigned) strtol(Str, &End, 10); 1300193326Sed if (*Str != '\0' && *End == '\0') 1301193326Sed return true; 1302193326Sed if (*End != '.') 1303193326Sed return false; 1304198092Srdivacky 1305193326Sed Str = End+1; 1306193326Sed Minor = (unsigned) strtol(Str, &End, 10); 1307193326Sed if (*Str != '\0' && *End == '\0') 1308193326Sed return true; 1309193326Sed if (*End != '.') 1310193326Sed return false; 1311193326Sed 1312193326Sed Str = End+1; 1313193326Sed Micro = (unsigned) strtol(Str, &End, 10); 1314193326Sed if (*Str != '\0' && *End == '\0') 1315193326Sed return true; 1316193326Sed if (Str == End) 1317193326Sed return false; 1318193326Sed HadExtra = true; 1319193326Sed return true; 1320193326Sed} 1321