Driver.cpp revision 195341
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" 19193326Sed#include "clang/Driver/Option.h" 20193326Sed#include "clang/Driver/Options.h" 21193326Sed#include "clang/Driver/Tool.h" 22193326Sed#include "clang/Driver/ToolChain.h" 23193326Sed#include "clang/Driver/Types.h" 24193326Sed 25193326Sed#include "clang/Basic/Version.h" 26193326Sed 27193326Sed#include "llvm/ADT/StringSet.h" 28193326Sed#include "llvm/Support/PrettyStackTrace.h" 29193326Sed#include "llvm/Support/raw_ostream.h" 30193326Sed#include "llvm/System/Path.h" 31193326Sed#include "llvm/System/Program.h" 32193326Sed 33193326Sed#include "InputInfo.h" 34193326Sed 35193326Sed#include <map> 36193326Sed 37193326Sedusing namespace clang::driver; 38193326Sedusing namespace clang; 39193326Sed 40193326SedDriver::Driver(const char *_Name, const char *_Dir, 41193326Sed const char *_DefaultHostTriple, 42193326Sed const char *_DefaultImageName, 43193326Sed Diagnostic &_Diags) 44193326Sed : Opts(new OptTable()), Diags(_Diags), 45193326Sed Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple), 46193326Sed DefaultImageName(_DefaultImageName), 47193326Sed Host(0), 48193326Sed CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false), 49193326Sed CCCGenericGCCName("gcc"), CCCUseClang(true), CCCUseClangCXX(false), 50193326Sed CCCUseClangCPP(true), CCCUsePCH(true), 51193326Sed SuppressMissingInputWarning(false) 52193326Sed{ 53193326Sed // Only use clang on i386 and x86_64 by default. 54193326Sed CCCClangArchs.insert("i386"); 55193326Sed CCCClangArchs.insert("x86_64"); 56193326Sed} 57193326Sed 58193326SedDriver::~Driver() { 59193326Sed delete Opts; 60193326Sed delete Host; 61193326Sed} 62193326Sed 63193326SedInputArgList *Driver::ParseArgStrings(const char **ArgBegin, 64193326Sed const char **ArgEnd) { 65193326Sed llvm::PrettyStackTraceString CrashInfo("Command line argument parsing"); 66193326Sed InputArgList *Args = new InputArgList(ArgBegin, ArgEnd); 67193326Sed 68193326Sed // FIXME: Handle '@' args (or at least error on them). 69193326Sed 70193326Sed unsigned Index = 0, End = ArgEnd - ArgBegin; 71193326Sed while (Index < End) { 72193326Sed // gcc's handling of empty arguments doesn't make 73193326Sed // sense, but this is not a common use case. :) 74193326Sed // 75193326Sed // We just ignore them here (note that other things may 76193326Sed // still take them as arguments). 77193326Sed if (Args->getArgString(Index)[0] == '\0') { 78193326Sed ++Index; 79193326Sed continue; 80193326Sed } 81193326Sed 82193326Sed unsigned Prev = Index; 83193326Sed Arg *A = getOpts().ParseOneArg(*Args, Index); 84193326Sed assert(Index > Prev && "Parser failed to consume argument."); 85193326Sed 86193326Sed // Check for missing argument error. 87193326Sed if (!A) { 88193326Sed assert(Index >= End && "Unexpected parser error."); 89193326Sed Diag(clang::diag::err_drv_missing_argument) 90193326Sed << Args->getArgString(Prev) 91193326Sed << (Index - Prev - 1); 92193326Sed break; 93193326Sed } 94193326Sed 95193326Sed if (A->getOption().isUnsupported()) { 96193326Sed Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args); 97193326Sed continue; 98193326Sed } 99193326Sed Args->append(A); 100193326Sed } 101193326Sed 102193326Sed return Args; 103193326Sed} 104193326Sed 105193326SedCompilation *Driver::BuildCompilation(int argc, const char **argv) { 106193326Sed llvm::PrettyStackTraceString CrashInfo("Compilation construction"); 107193326Sed 108193326Sed // FIXME: Handle environment options which effect driver behavior, 109193326Sed // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH, 110193326Sed // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS. 111193326Sed 112193326Sed // FIXME: What are we going to do with -V and -b? 113193326Sed 114193326Sed // FIXME: This stuff needs to go into the Compilation, not the 115193326Sed // driver. 116193326Sed bool CCCPrintOptions = false, CCCPrintActions = false; 117193326Sed 118193326Sed const char **Start = argv + 1, **End = argv + argc; 119193326Sed const char *HostTriple = DefaultHostTriple.c_str(); 120193326Sed 121193326Sed // Read -ccc args. 122193326Sed // 123193326Sed // FIXME: We need to figure out where this behavior should 124193326Sed // live. Most of it should be outside in the client; the parts that 125193326Sed // aren't should have proper options, either by introducing new ones 126193326Sed // or by overloading gcc ones like -V or -b. 127193326Sed for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) { 128193326Sed const char *Opt = *Start + 5; 129193326Sed 130193326Sed if (!strcmp(Opt, "print-options")) { 131193326Sed CCCPrintOptions = true; 132193326Sed } else if (!strcmp(Opt, "print-phases")) { 133193326Sed CCCPrintActions = true; 134193326Sed } else if (!strcmp(Opt, "print-bindings")) { 135193326Sed CCCPrintBindings = true; 136193326Sed } else if (!strcmp(Opt, "cxx")) { 137193326Sed CCCIsCXX = true; 138193326Sed } else if (!strcmp(Opt, "echo")) { 139193326Sed CCCEcho = true; 140193326Sed 141193326Sed } else if (!strcmp(Opt, "gcc-name")) { 142193326Sed assert(Start+1 < End && "FIXME: -ccc- argument handling."); 143193326Sed CCCGenericGCCName = *++Start; 144193326Sed 145193326Sed } else if (!strcmp(Opt, "clang-cxx")) { 146193326Sed CCCUseClangCXX = true; 147193326Sed } else if (!strcmp(Opt, "pch-is-pch")) { 148193326Sed CCCUsePCH = true; 149193326Sed } else if (!strcmp(Opt, "pch-is-pth")) { 150193326Sed CCCUsePCH = false; 151193326Sed } else if (!strcmp(Opt, "no-clang")) { 152193326Sed CCCUseClang = false; 153193326Sed } else if (!strcmp(Opt, "no-clang-cpp")) { 154193326Sed CCCUseClangCPP = false; 155193326Sed } else if (!strcmp(Opt, "clang-archs")) { 156193326Sed assert(Start+1 < End && "FIXME: -ccc- argument handling."); 157193326Sed const char *Cur = *++Start; 158193326Sed 159193326Sed CCCClangArchs.clear(); 160193326Sed for (;;) { 161193326Sed const char *Next = strchr(Cur, ','); 162193326Sed 163193326Sed if (Next) { 164193326Sed if (Cur != Next) 165193326Sed CCCClangArchs.insert(std::string(Cur, Next)); 166193326Sed Cur = Next + 1; 167193326Sed } else { 168193326Sed if (*Cur != '\0') 169193326Sed CCCClangArchs.insert(std::string(Cur)); 170193326Sed break; 171193326Sed } 172193326Sed } 173193326Sed 174193326Sed } else if (!strcmp(Opt, "host-triple")) { 175193326Sed assert(Start+1 < End && "FIXME: -ccc- argument handling."); 176193326Sed HostTriple = *++Start; 177193326Sed 178193326Sed } else { 179193326Sed // FIXME: Error handling. 180193326Sed llvm::errs() << "invalid option: " << *Start << "\n"; 181193326Sed exit(1); 182193326Sed } 183193326Sed } 184193326Sed 185193326Sed InputArgList *Args = ParseArgStrings(Start, End); 186193326Sed 187193326Sed Host = GetHostInfo(HostTriple); 188193326Sed 189193326Sed // The compilation takes ownership of Args. 190193326Sed Compilation *C = new Compilation(*this, *Host->getToolChain(*Args), Args); 191193326Sed 192193326Sed // FIXME: This behavior shouldn't be here. 193193326Sed if (CCCPrintOptions) { 194193326Sed PrintOptions(C->getArgs()); 195193326Sed return C; 196193326Sed } 197193326Sed 198193326Sed if (!HandleImmediateArgs(*C)) 199193326Sed return C; 200193326Sed 201193326Sed // Construct the list of abstract actions to perform for this 202193326Sed // compilation. We avoid passing a Compilation here simply to 203193326Sed // enforce the abstraction that pipelining is not host or toolchain 204193326Sed // dependent (other than the driver driver test). 205193326Sed if (Host->useDriverDriver()) 206193326Sed BuildUniversalActions(C->getArgs(), C->getActions()); 207193326Sed else 208193326Sed BuildActions(C->getArgs(), C->getActions()); 209193326Sed 210193326Sed if (CCCPrintActions) { 211193326Sed PrintActions(*C); 212193326Sed return C; 213193326Sed } 214193326Sed 215193326Sed BuildJobs(*C); 216193326Sed 217193326Sed return C; 218193326Sed} 219193326Sed 220195341Sedint Driver::ExecuteCompilation(const Compilation &C) const { 221195341Sed // Just print if -### was present. 222195341Sed if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { 223195341Sed C.PrintJob(llvm::errs(), C.getJobs(), "\n", true); 224195341Sed return 0; 225195341Sed } 226195341Sed 227195341Sed // If there were errors building the compilation, quit now. 228195341Sed if (getDiags().getNumErrors()) 229195341Sed return 1; 230195341Sed 231195341Sed const Command *FailingCommand = 0; 232195341Sed int Res = C.ExecuteJob(C.getJobs(), FailingCommand); 233195341Sed 234195341Sed // Remove temp files. 235195341Sed C.CleanupFileList(C.getTempFiles()); 236195341Sed 237195341Sed // If the compilation failed, remove result files as well. 238195341Sed if (Res != 0 && !C.getArgs().hasArg(options::OPT_save_temps)) 239195341Sed C.CleanupFileList(C.getResultFiles(), true); 240195341Sed 241195341Sed // Print extra information about abnormal failures, if possible. 242195341Sed if (Res) { 243195341Sed // This is ad-hoc, but we don't want to be excessively noisy. If the result 244195341Sed // status was 1, assume the command failed normally. In particular, if it 245195341Sed // was the compiler then assume it gave a reasonable error code. Failures in 246195341Sed // other tools are less common, and they generally have worse diagnostics, 247195341Sed // so always print the diagnostic there. 248195341Sed const Action &Source = FailingCommand->getSource(); 249195341Sed bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) || 250195341Sed isa<PrecompileJobAction>(Source) || 251195341Sed isa<AnalyzeJobAction>(Source) || 252195341Sed isa<CompileJobAction>(Source)); 253195341Sed 254195341Sed if (!IsFriendlyTool || Res != 1) { 255195341Sed // FIXME: See FIXME above regarding result code interpretation. 256195341Sed if (Res < 0) 257195341Sed Diag(clang::diag::err_drv_command_signalled) 258195341Sed << Source.getClassName() << -Res; 259195341Sed else 260195341Sed Diag(clang::diag::err_drv_command_failed) 261195341Sed << Source.getClassName() << Res; 262195341Sed } 263195341Sed } 264195341Sed 265195341Sed return Res; 266195341Sed} 267195341Sed 268193326Sedvoid Driver::PrintOptions(const ArgList &Args) const { 269193326Sed unsigned i = 0; 270193326Sed for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); 271193326Sed it != ie; ++it, ++i) { 272193326Sed Arg *A = *it; 273193326Sed llvm::errs() << "Option " << i << " - " 274193326Sed << "Name: \"" << A->getOption().getName() << "\", " 275193326Sed << "Values: {"; 276193326Sed for (unsigned j = 0; j < A->getNumValues(); ++j) { 277193326Sed if (j) 278193326Sed llvm::errs() << ", "; 279193326Sed llvm::errs() << '"' << A->getValue(Args, j) << '"'; 280193326Sed } 281193326Sed llvm::errs() << "}\n"; 282193326Sed } 283193326Sed} 284193326Sed 285193326Sedstatic std::string getOptionHelpName(const OptTable &Opts, options::ID Id) { 286193326Sed std::string Name = Opts.getOptionName(Id); 287193326Sed 288193326Sed // Add metavar, if used. 289193326Sed switch (Opts.getOptionKind(Id)) { 290193326Sed case Option::GroupClass: case Option::InputClass: case Option::UnknownClass: 291193326Sed assert(0 && "Invalid option with help text."); 292193326Sed 293193326Sed case Option::MultiArgClass: case Option::JoinedAndSeparateClass: 294193326Sed assert(0 && "Cannot print metavar for this kind of option."); 295193326Sed 296193326Sed case Option::FlagClass: 297193326Sed break; 298193326Sed 299193326Sed case Option::SeparateClass: case Option::JoinedOrSeparateClass: 300193326Sed Name += ' '; 301193326Sed // FALLTHROUGH 302193326Sed case Option::JoinedClass: case Option::CommaJoinedClass: 303193326Sed Name += Opts.getOptionMetaVar(Id); 304193326Sed break; 305193326Sed } 306193326Sed 307193326Sed return Name; 308193326Sed} 309193326Sed 310193326Sedvoid Driver::PrintHelp(bool ShowHidden) const { 311193326Sed llvm::raw_ostream &OS = llvm::outs(); 312193326Sed 313193326Sed OS << "OVERVIEW: clang \"gcc-compatible\" driver\n"; 314193326Sed OS << '\n'; 315193326Sed OS << "USAGE: " << Name << " [options] <input files>\n"; 316193326Sed OS << '\n'; 317193326Sed OS << "OPTIONS:\n"; 318193326Sed 319193326Sed // Render help text into (option, help) pairs. 320193326Sed std::vector< std::pair<std::string, const char*> > OptionHelp; 321193326Sed 322193326Sed for (unsigned i = options::OPT_INPUT, e = options::LastOption; i != e; ++i) { 323193326Sed options::ID Id = (options::ID) i; 324193326Sed if (const char *Text = getOpts().getOptionHelpText(Id)) 325193326Sed OptionHelp.push_back(std::make_pair(getOptionHelpName(getOpts(), Id), 326193326Sed Text)); 327193326Sed } 328193326Sed 329193326Sed if (ShowHidden) { 330193326Sed OptionHelp.push_back(std::make_pair("\nDRIVER OPTIONS:","")); 331193326Sed OptionHelp.push_back(std::make_pair("-ccc-cxx", 332193326Sed "Act as a C++ driver")); 333193326Sed OptionHelp.push_back(std::make_pair("-ccc-gcc-name", 334193326Sed "Name for native GCC compiler")); 335193326Sed OptionHelp.push_back(std::make_pair("-ccc-clang-cxx", 336193326Sed "Use the clang compiler for C++")); 337193326Sed OptionHelp.push_back(std::make_pair("-ccc-no-clang", 338193326Sed "Never use the clang compiler")); 339193326Sed OptionHelp.push_back(std::make_pair("-ccc-no-clang-cpp", 340193326Sed "Never use the clang preprocessor")); 341193326Sed OptionHelp.push_back(std::make_pair("-ccc-clang-archs", 342193326Sed "Comma separate list of architectures " 343193326Sed "to use the clang compiler for")); 344193326Sed OptionHelp.push_back(std::make_pair("-ccc-pch-is-pch", 345193326Sed "Use lazy PCH for precompiled headers")); 346193326Sed OptionHelp.push_back(std::make_pair("-ccc-pch-is-pth", 347193326Sed "Use pretokenized headers for precompiled headers")); 348193326Sed 349193326Sed OptionHelp.push_back(std::make_pair("\nDEBUG/DEVELOPMENT OPTIONS:","")); 350193326Sed OptionHelp.push_back(std::make_pair("-ccc-host-triple", 351193326Sed "Simulate running on the given target")); 352193326Sed OptionHelp.push_back(std::make_pair("-ccc-print-options", 353193326Sed "Dump parsed command line arguments")); 354193326Sed OptionHelp.push_back(std::make_pair("-ccc-print-phases", 355193326Sed "Dump list of actions to perform")); 356193326Sed OptionHelp.push_back(std::make_pair("-ccc-print-bindings", 357193326Sed "Show bindings of tools to actions")); 358193326Sed OptionHelp.push_back(std::make_pair("CCC_ADD_ARGS", 359193326Sed "(ENVIRONMENT VARIABLE) Comma separated list of " 360193326Sed "arguments to prepend to the command line")); 361193326Sed } 362193326Sed 363193326Sed // Find the maximum option length. 364193326Sed unsigned OptionFieldWidth = 0; 365193326Sed for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { 366193326Sed // Skip titles. 367193326Sed if (!OptionHelp[i].second) 368193326Sed continue; 369193326Sed 370193326Sed // Limit the amount of padding we are willing to give up for 371193326Sed // alignment. 372193326Sed unsigned Length = OptionHelp[i].first.size(); 373193326Sed if (Length <= 23) 374193326Sed OptionFieldWidth = std::max(OptionFieldWidth, Length); 375193326Sed } 376193326Sed 377193326Sed for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { 378193326Sed const std::string &Option = OptionHelp[i].first; 379193326Sed OS << " " << Option; 380193326Sed for (int j = Option.length(), e = OptionFieldWidth; j < e; ++j) 381193326Sed OS << ' '; 382193326Sed OS << ' ' << OptionHelp[i].second << '\n'; 383193326Sed } 384193326Sed 385193326Sed OS.flush(); 386193326Sed} 387193326Sed 388193326Sedvoid Driver::PrintVersion(const Compilation &C) const { 389193326Sed static char buf[] = "$URL: https://ed@llvm.org/svn/llvm-project/cfe/trunk/lib/Driver/Driver.cpp $"; 390193326Sed char *zap = strstr(buf, "/lib/Driver"); 391193326Sed if (zap) 392193326Sed *zap = 0; 393193326Sed zap = strstr(buf, "/clang/tools/clang"); 394193326Sed if (zap) 395193326Sed *zap = 0; 396193326Sed const char *vers = buf+6; 397193326Sed // FIXME: Add cmake support and remove #ifdef 398193326Sed#ifdef SVN_REVISION 399193326Sed const char *revision = SVN_REVISION; 400193326Sed#else 401193326Sed const char *revision = ""; 402193326Sed#endif 403193326Sed // FIXME: The following handlers should use a callback mechanism, we 404193326Sed // don't know what the client would like to do. 405193326Sed 406193326Sed llvm::errs() << "clang version " CLANG_VERSION_STRING " (" 407194613Sed << vers << " " << revision << ")" << '\n'; 408193326Sed 409193326Sed const ToolChain &TC = C.getDefaultToolChain(); 410193326Sed llvm::errs() << "Target: " << TC.getTripleString() << '\n'; 411194613Sed 412194613Sed // Print the threading model. 413194613Sed // 414194613Sed // FIXME: Implement correctly. 415194613Sed llvm::errs() << "Thread model: " << "posix" << '\n'; 416193326Sed} 417193326Sed 418193326Sedbool Driver::HandleImmediateArgs(const Compilation &C) { 419193326Sed // The order these options are handled in in gcc is all over the 420193326Sed // place, but we don't expect inconsistencies w.r.t. that to matter 421193326Sed // in practice. 422193326Sed 423193326Sed if (C.getArgs().hasArg(options::OPT_dumpversion)) { 424193326Sed llvm::outs() << CLANG_VERSION_STRING "\n"; 425193326Sed return false; 426193326Sed } 427193326Sed 428193326Sed if (C.getArgs().hasArg(options::OPT__help) || 429193326Sed C.getArgs().hasArg(options::OPT__help_hidden)) { 430193326Sed PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden)); 431193326Sed return false; 432193326Sed } 433193326Sed 434193326Sed if (C.getArgs().hasArg(options::OPT__version)) { 435193326Sed PrintVersion(C); 436193326Sed return false; 437193326Sed } 438193326Sed 439193326Sed if (C.getArgs().hasArg(options::OPT_v) || 440193326Sed C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { 441193326Sed PrintVersion(C); 442193326Sed SuppressMissingInputWarning = true; 443193326Sed } 444193326Sed 445193326Sed const ToolChain &TC = C.getDefaultToolChain(); 446193326Sed if (C.getArgs().hasArg(options::OPT_print_search_dirs)) { 447193326Sed llvm::outs() << "programs: ="; 448193326Sed for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(), 449193326Sed ie = TC.getProgramPaths().end(); it != ie; ++it) { 450193326Sed if (it != TC.getProgramPaths().begin()) 451193326Sed llvm::outs() << ':'; 452193326Sed llvm::outs() << *it; 453193326Sed } 454193326Sed llvm::outs() << "\n"; 455193326Sed llvm::outs() << "libraries: ="; 456193326Sed for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(), 457193326Sed ie = TC.getFilePaths().end(); it != ie; ++it) { 458193326Sed if (it != TC.getFilePaths().begin()) 459193326Sed llvm::outs() << ':'; 460193326Sed llvm::outs() << *it; 461193326Sed } 462193326Sed llvm::outs() << "\n"; 463193326Sed return false; 464193326Sed } 465193326Sed 466193326Sed // FIXME: The following handlers should use a callback mechanism, we 467193326Sed // don't know what the client would like to do. 468193326Sed if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) { 469193326Sed llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).toString() 470193326Sed << "\n"; 471193326Sed return false; 472193326Sed } 473193326Sed 474193326Sed if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) { 475193326Sed llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).toString() 476193326Sed << "\n"; 477193326Sed return false; 478193326Sed } 479193326Sed 480193326Sed if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) { 481193326Sed llvm::outs() << GetFilePath("libgcc.a", TC).toString() << "\n"; 482193326Sed return false; 483193326Sed } 484193326Sed 485194613Sed if (C.getArgs().hasArg(options::OPT_print_multi_lib)) { 486194613Sed // FIXME: We need tool chain support for this. 487194613Sed llvm::outs() << ".;\n"; 488194613Sed 489194613Sed switch (C.getDefaultToolChain().getTriple().getArch()) { 490194613Sed default: 491194613Sed break; 492194613Sed 493194613Sed case llvm::Triple::x86_64: 494194613Sed llvm::outs() << "x86_64;@m64" << "\n"; 495194613Sed break; 496194613Sed 497194613Sed case llvm::Triple::ppc64: 498194613Sed llvm::outs() << "ppc64;@m64" << "\n"; 499194613Sed break; 500194613Sed } 501194613Sed return false; 502194613Sed } 503194613Sed 504194613Sed // FIXME: What is the difference between print-multi-directory and 505194613Sed // print-multi-os-directory? 506194613Sed if (C.getArgs().hasArg(options::OPT_print_multi_directory) || 507194613Sed C.getArgs().hasArg(options::OPT_print_multi_os_directory)) { 508194613Sed switch (C.getDefaultToolChain().getTriple().getArch()) { 509194613Sed default: 510194613Sed case llvm::Triple::x86: 511194613Sed case llvm::Triple::ppc: 512194613Sed llvm::outs() << "." << "\n"; 513194613Sed break; 514194613Sed 515194613Sed case llvm::Triple::x86_64: 516194613Sed llvm::outs() << "x86_64" << "\n"; 517194613Sed break; 518194613Sed 519194613Sed case llvm::Triple::ppc64: 520194613Sed llvm::outs() << "ppc64" << "\n"; 521194613Sed break; 522194613Sed } 523194613Sed return false; 524194613Sed } 525194613Sed 526193326Sed return true; 527193326Sed} 528193326Sed 529193326Sedstatic unsigned PrintActions1(const Compilation &C, 530193326Sed Action *A, 531193326Sed std::map<Action*, unsigned> &Ids) { 532193326Sed if (Ids.count(A)) 533193326Sed return Ids[A]; 534193326Sed 535193326Sed std::string str; 536193326Sed llvm::raw_string_ostream os(str); 537193326Sed 538193326Sed os << Action::getClassName(A->getKind()) << ", "; 539193326Sed if (InputAction *IA = dyn_cast<InputAction>(A)) { 540193326Sed os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\""; 541193326Sed } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) { 542193326Sed os << '"' << (BIA->getArchName() ? BIA->getArchName() : 543193326Sed C.getDefaultToolChain().getArchName()) << '"' 544193326Sed << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}"; 545193326Sed } else { 546193326Sed os << "{"; 547193326Sed for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) { 548193326Sed os << PrintActions1(C, *it, Ids); 549193326Sed ++it; 550193326Sed if (it != ie) 551193326Sed os << ", "; 552193326Sed } 553193326Sed os << "}"; 554193326Sed } 555193326Sed 556193326Sed unsigned Id = Ids.size(); 557193326Sed Ids[A] = Id; 558193326Sed llvm::errs() << Id << ": " << os.str() << ", " 559193326Sed << types::getTypeName(A->getType()) << "\n"; 560193326Sed 561193326Sed return Id; 562193326Sed} 563193326Sed 564193326Sedvoid Driver::PrintActions(const Compilation &C) const { 565193326Sed std::map<Action*, unsigned> Ids; 566193326Sed for (ActionList::const_iterator it = C.getActions().begin(), 567193326Sed ie = C.getActions().end(); it != ie; ++it) 568193326Sed PrintActions1(C, *it, Ids); 569193326Sed} 570193326Sed 571193326Sedvoid Driver::BuildUniversalActions(const ArgList &Args, 572193326Sed ActionList &Actions) const { 573193326Sed llvm::PrettyStackTraceString CrashInfo("Building actions for universal build"); 574193326Sed // Collect the list of architectures. Duplicates are allowed, but 575193326Sed // should only be handled once (in the order seen). 576193326Sed llvm::StringSet<> ArchNames; 577193326Sed llvm::SmallVector<const char *, 4> Archs; 578193326Sed for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); 579193326Sed it != ie; ++it) { 580193326Sed Arg *A = *it; 581193326Sed 582193326Sed if (A->getOption().getId() == options::OPT_arch) { 583193326Sed const char *Name = A->getValue(Args); 584193326Sed 585193326Sed // FIXME: We need to handle canonicalization of the specified 586193326Sed // arch? 587193326Sed 588193326Sed A->claim(); 589193326Sed if (ArchNames.insert(Name)) 590193326Sed Archs.push_back(Name); 591193326Sed } 592193326Sed } 593193326Sed 594193326Sed // When there is no explicit arch for this platform, make sure we 595193326Sed // still bind the architecture (to the default) so that -Xarch_ is 596193326Sed // handled correctly. 597193326Sed if (!Archs.size()) 598193326Sed Archs.push_back(0); 599193326Sed 600193326Sed // FIXME: We killed off some others but these aren't yet detected in 601193326Sed // a functional manner. If we added information to jobs about which 602193326Sed // "auxiliary" files they wrote then we could detect the conflict 603193326Sed // these cause downstream. 604193326Sed if (Archs.size() > 1) { 605193326Sed // No recovery needed, the point of this is just to prevent 606193326Sed // overwriting the same files. 607193326Sed if (const Arg *A = Args.getLastArg(options::OPT_save_temps)) 608193326Sed Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs) 609193326Sed << A->getAsString(Args); 610193326Sed } 611193326Sed 612193326Sed ActionList SingleActions; 613193326Sed BuildActions(Args, SingleActions); 614193326Sed 615193326Sed // Add in arch binding and lipo (if necessary) for every top level 616193326Sed // action. 617193326Sed for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) { 618193326Sed Action *Act = SingleActions[i]; 619193326Sed 620193326Sed // Make sure we can lipo this kind of output. If not (and it is an 621193326Sed // actual output) then we disallow, since we can't create an 622193326Sed // output file with the right name without overwriting it. We 623193326Sed // could remove this oddity by just changing the output names to 624193326Sed // include the arch, which would also fix 625193326Sed // -save-temps. Compatibility wins for now. 626193326Sed 627193326Sed if (Archs.size() > 1 && !types::canLipoType(Act->getType())) 628193326Sed Diag(clang::diag::err_drv_invalid_output_with_multiple_archs) 629193326Sed << types::getTypeName(Act->getType()); 630193326Sed 631193326Sed ActionList Inputs; 632193326Sed for (unsigned i = 0, e = Archs.size(); i != e; ++i) 633193326Sed Inputs.push_back(new BindArchAction(Act, Archs[i])); 634193326Sed 635193326Sed // Lipo if necessary, We do it this way because we need to set the 636193326Sed // arch flag so that -Xarch_ gets overwritten. 637193326Sed if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing) 638193326Sed Actions.append(Inputs.begin(), Inputs.end()); 639193326Sed else 640193326Sed Actions.push_back(new LipoJobAction(Inputs, Act->getType())); 641193326Sed } 642193326Sed} 643193326Sed 644193326Sedvoid Driver::BuildActions(const ArgList &Args, ActionList &Actions) const { 645193326Sed llvm::PrettyStackTraceString CrashInfo("Building compilation actions"); 646193326Sed // Start by constructing the list of inputs and their types. 647193326Sed 648193326Sed // Track the current user specified (-x) input. We also explicitly 649193326Sed // track the argument used to set the type; we only want to claim 650193326Sed // the type when we actually use it, so we warn about unused -x 651193326Sed // arguments. 652193326Sed types::ID InputType = types::TY_Nothing; 653193326Sed Arg *InputTypeArg = 0; 654193326Sed 655193326Sed llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs; 656193326Sed for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); 657193326Sed it != ie; ++it) { 658193326Sed Arg *A = *it; 659193326Sed 660193326Sed if (isa<InputOption>(A->getOption())) { 661193326Sed const char *Value = A->getValue(Args); 662193326Sed types::ID Ty = types::TY_INVALID; 663193326Sed 664193326Sed // Infer the input type if necessary. 665193326Sed if (InputType == types::TY_Nothing) { 666193326Sed // If there was an explicit arg for this, claim it. 667193326Sed if (InputTypeArg) 668193326Sed InputTypeArg->claim(); 669193326Sed 670193326Sed // stdin must be handled specially. 671193326Sed if (memcmp(Value, "-", 2) == 0) { 672193326Sed // If running with -E, treat as a C input (this changes the 673193326Sed // builtin macros, for example). This may be overridden by 674193326Sed // -ObjC below. 675193326Sed // 676193326Sed // Otherwise emit an error but still use a valid type to 677193326Sed // avoid spurious errors (e.g., no inputs). 678193326Sed if (!Args.hasArg(options::OPT_E, false)) 679193326Sed Diag(clang::diag::err_drv_unknown_stdin_type); 680193326Sed Ty = types::TY_C; 681193326Sed } else { 682193326Sed // Otherwise lookup by extension, and fallback to ObjectType 683193326Sed // if not found. We use a host hook here because Darwin at 684193326Sed // least has its own idea of what .s is. 685193326Sed if (const char *Ext = strrchr(Value, '.')) 686193326Sed Ty = Host->lookupTypeForExtension(Ext + 1); 687193326Sed 688193326Sed if (Ty == types::TY_INVALID) 689193326Sed Ty = types::TY_Object; 690193326Sed } 691193326Sed 692193326Sed // -ObjC and -ObjC++ override the default language, but only for "source 693193326Sed // files". We just treat everything that isn't a linker input as a 694193326Sed // source file. 695193326Sed // 696193326Sed // FIXME: Clean this up if we move the phase sequence into the type. 697193326Sed if (Ty != types::TY_Object) { 698193326Sed if (Args.hasArg(options::OPT_ObjC)) 699193326Sed Ty = types::TY_ObjC; 700193326Sed else if (Args.hasArg(options::OPT_ObjCXX)) 701193326Sed Ty = types::TY_ObjCXX; 702193326Sed } 703193326Sed } else { 704193326Sed assert(InputTypeArg && "InputType set w/o InputTypeArg"); 705193326Sed InputTypeArg->claim(); 706193326Sed Ty = InputType; 707193326Sed } 708193326Sed 709193326Sed // Check that the file exists. It isn't clear this is worth 710193326Sed // doing, since the tool presumably does this anyway, and this 711193326Sed // just adds an extra stat to the equation, but this is gcc 712193326Sed // compatible. 713193326Sed if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists()) 714193326Sed Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args); 715193326Sed else 716193326Sed Inputs.push_back(std::make_pair(Ty, A)); 717193326Sed 718193326Sed } else if (A->getOption().isLinkerInput()) { 719193326Sed // Just treat as object type, we could make a special type for 720193326Sed // this if necessary. 721193326Sed Inputs.push_back(std::make_pair(types::TY_Object, A)); 722193326Sed 723193326Sed } else if (A->getOption().getId() == options::OPT_x) { 724193326Sed InputTypeArg = A; 725193326Sed InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args)); 726193326Sed 727193326Sed // Follow gcc behavior and treat as linker input for invalid -x 728193326Sed // options. Its not clear why we shouldn't just revert to 729193326Sed // unknown; but this isn't very important, we might as well be 730193326Sed // bug comatible. 731193326Sed if (!InputType) { 732193326Sed Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args); 733193326Sed InputType = types::TY_Object; 734193326Sed } 735193326Sed } 736193326Sed } 737193326Sed 738193326Sed if (!SuppressMissingInputWarning && Inputs.empty()) { 739193326Sed Diag(clang::diag::err_drv_no_input_files); 740193326Sed return; 741193326Sed } 742193326Sed 743193326Sed // Determine which compilation mode we are in. We look for options 744193326Sed // which affect the phase, starting with the earliest phases, and 745193326Sed // record which option we used to determine the final phase. 746193326Sed Arg *FinalPhaseArg = 0; 747193326Sed phases::ID FinalPhase; 748193326Sed 749193326Sed // -{E,M,MM} only run the preprocessor. 750193326Sed if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) || 751193326Sed (FinalPhaseArg = Args.getLastArg(options::OPT_M)) || 752193326Sed (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) { 753193326Sed FinalPhase = phases::Preprocess; 754193326Sed 755193326Sed // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler. 756193326Sed } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) || 757193326Sed (FinalPhaseArg = Args.getLastArg(options::OPT__analyze, 758193326Sed options::OPT__analyze_auto)) || 759193326Sed (FinalPhaseArg = Args.getLastArg(options::OPT_S))) { 760193326Sed FinalPhase = phases::Compile; 761193326Sed 762193326Sed // -c only runs up to the assembler. 763193326Sed } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) { 764193326Sed FinalPhase = phases::Assemble; 765193326Sed 766193326Sed // Otherwise do everything. 767193326Sed } else 768193326Sed FinalPhase = phases::Link; 769193326Sed 770193326Sed // Reject -Z* at the top level, these options should never have been 771193326Sed // exposed by gcc. 772193326Sed if (Arg *A = Args.getLastArg(options::OPT_Z_Joined)) 773193326Sed Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args); 774193326Sed 775193326Sed // Construct the actions to perform. 776193326Sed ActionList LinkerInputs; 777193326Sed for (unsigned i = 0, e = Inputs.size(); i != e; ++i) { 778193326Sed types::ID InputType = Inputs[i].first; 779193326Sed const Arg *InputArg = Inputs[i].second; 780193326Sed 781193326Sed unsigned NumSteps = types::getNumCompilationPhases(InputType); 782193326Sed assert(NumSteps && "Invalid number of steps!"); 783193326Sed 784193326Sed // If the first step comes after the final phase we are doing as 785193326Sed // part of this compilation, warn the user about it. 786193326Sed phases::ID InitialPhase = types::getCompilationPhase(InputType, 0); 787193326Sed if (InitialPhase > FinalPhase) { 788193326Sed // Claim here to avoid the more general unused warning. 789193326Sed InputArg->claim(); 790193326Sed Diag(clang::diag::warn_drv_input_file_unused) 791193326Sed << InputArg->getAsString(Args) 792193326Sed << getPhaseName(InitialPhase) 793193326Sed << FinalPhaseArg->getOption().getName(); 794193326Sed continue; 795193326Sed } 796193326Sed 797193326Sed // Build the pipeline for this file. 798193326Sed Action *Current = new InputAction(*InputArg, InputType); 799193326Sed for (unsigned i = 0; i != NumSteps; ++i) { 800193326Sed phases::ID Phase = types::getCompilationPhase(InputType, i); 801193326Sed 802193326Sed // We are done if this step is past what the user requested. 803193326Sed if (Phase > FinalPhase) 804193326Sed break; 805193326Sed 806193326Sed // Queue linker inputs. 807193326Sed if (Phase == phases::Link) { 808193326Sed assert(i + 1 == NumSteps && "linking must be final compilation step."); 809193326Sed LinkerInputs.push_back(Current); 810193326Sed Current = 0; 811193326Sed break; 812193326Sed } 813193326Sed 814193326Sed // Some types skip the assembler phase (e.g., llvm-bc), but we 815193326Sed // can't encode this in the steps because the intermediate type 816193326Sed // depends on arguments. Just special case here. 817193326Sed if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm) 818193326Sed continue; 819193326Sed 820193326Sed // Otherwise construct the appropriate action. 821193326Sed Current = ConstructPhaseAction(Args, Phase, Current); 822193326Sed if (Current->getType() == types::TY_Nothing) 823193326Sed break; 824193326Sed } 825193326Sed 826193326Sed // If we ended with something, add to the output list. 827193326Sed if (Current) 828193326Sed Actions.push_back(Current); 829193326Sed } 830193326Sed 831193326Sed // Add a link action if necessary. 832193326Sed if (!LinkerInputs.empty()) 833193326Sed Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image)); 834193326Sed} 835193326Sed 836193326SedAction *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase, 837193326Sed Action *Input) const { 838193326Sed llvm::PrettyStackTraceString CrashInfo("Constructing phase actions"); 839193326Sed // Build the appropriate action. 840193326Sed switch (Phase) { 841193326Sed case phases::Link: assert(0 && "link action invalid here."); 842193326Sed case phases::Preprocess: { 843193326Sed types::ID OutputTy; 844193326Sed // -{M, MM} alter the output type. 845193326Sed if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) { 846193326Sed OutputTy = types::TY_Dependencies; 847193326Sed } else { 848193326Sed OutputTy = types::getPreprocessedType(Input->getType()); 849193326Sed assert(OutputTy != types::TY_INVALID && 850193326Sed "Cannot preprocess this input type!"); 851193326Sed } 852193326Sed return new PreprocessJobAction(Input, OutputTy); 853193326Sed } 854193326Sed case phases::Precompile: 855193326Sed return new PrecompileJobAction(Input, types::TY_PCH); 856193326Sed case phases::Compile: { 857193326Sed if (Args.hasArg(options::OPT_fsyntax_only)) { 858193326Sed return new CompileJobAction(Input, types::TY_Nothing); 859193326Sed } else if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) { 860193326Sed return new AnalyzeJobAction(Input, types::TY_Plist); 861193326Sed } else if (Args.hasArg(options::OPT_emit_llvm) || 862193326Sed Args.hasArg(options::OPT_flto) || 863193326Sed Args.hasArg(options::OPT_O4)) { 864193326Sed types::ID Output = 865193326Sed Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC; 866193326Sed return new CompileJobAction(Input, Output); 867193326Sed } else { 868193326Sed return new CompileJobAction(Input, types::TY_PP_Asm); 869193326Sed } 870193326Sed } 871193326Sed case phases::Assemble: 872193326Sed return new AssembleJobAction(Input, types::TY_Object); 873193326Sed } 874193326Sed 875193326Sed assert(0 && "invalid phase in ConstructPhaseAction"); 876193326Sed return 0; 877193326Sed} 878193326Sed 879193326Sedvoid Driver::BuildJobs(Compilation &C) const { 880193326Sed llvm::PrettyStackTraceString CrashInfo("Building compilation jobs"); 881193326Sed bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps); 882193326Sed bool UsePipes = C.getArgs().hasArg(options::OPT_pipe); 883193326Sed 884193326Sed // FIXME: Pipes are forcibly disabled until we support executing 885193326Sed // them. 886193326Sed if (!CCCPrintBindings) 887193326Sed UsePipes = false; 888193326Sed 889193326Sed // -save-temps inhibits pipes. 890193326Sed if (SaveTemps && UsePipes) { 891193326Sed Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps); 892193326Sed UsePipes = true; 893193326Sed } 894193326Sed 895193326Sed Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o); 896193326Sed 897193326Sed // It is an error to provide a -o option if we are making multiple 898193326Sed // output files. 899193326Sed if (FinalOutput) { 900193326Sed unsigned NumOutputs = 0; 901193326Sed for (ActionList::const_iterator it = C.getActions().begin(), 902193326Sed ie = C.getActions().end(); it != ie; ++it) 903193326Sed if ((*it)->getType() != types::TY_Nothing) 904193326Sed ++NumOutputs; 905193326Sed 906193326Sed if (NumOutputs > 1) { 907193326Sed Diag(clang::diag::err_drv_output_argument_with_multiple_files); 908193326Sed FinalOutput = 0; 909193326Sed } 910193326Sed } 911193326Sed 912193326Sed for (ActionList::const_iterator it = C.getActions().begin(), 913193326Sed ie = C.getActions().end(); it != ie; ++it) { 914193326Sed Action *A = *it; 915193326Sed 916193326Sed // If we are linking an image for multiple archs then the linker 917193326Sed // wants -arch_multiple and -final_output <final image 918193326Sed // name>. Unfortunately, this doesn't fit in cleanly because we 919193326Sed // have to pass this information down. 920193326Sed // 921193326Sed // FIXME: This is a hack; find a cleaner way to integrate this 922193326Sed // into the process. 923193326Sed const char *LinkingOutput = 0; 924193326Sed if (isa<LipoJobAction>(A)) { 925193326Sed if (FinalOutput) 926193326Sed LinkingOutput = FinalOutput->getValue(C.getArgs()); 927193326Sed else 928193326Sed LinkingOutput = DefaultImageName.c_str(); 929193326Sed } 930193326Sed 931193326Sed InputInfo II; 932193326Sed BuildJobsForAction(C, A, &C.getDefaultToolChain(), 933193326Sed /*CanAcceptPipe*/ true, 934193326Sed /*AtTopLevel*/ true, 935193326Sed /*LinkingOutput*/ LinkingOutput, 936193326Sed II); 937193326Sed } 938193326Sed 939193326Sed // If the user passed -Qunused-arguments or there were errors, don't 940193326Sed // warn about any unused arguments. 941193326Sed if (Diags.getNumErrors() || 942193326Sed C.getArgs().hasArg(options::OPT_Qunused_arguments)) 943193326Sed return; 944193326Sed 945193326Sed // Claim -### here. 946193326Sed (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH); 947193326Sed 948193326Sed for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end(); 949193326Sed it != ie; ++it) { 950193326Sed Arg *A = *it; 951193326Sed 952193326Sed // FIXME: It would be nice to be able to send the argument to the 953193326Sed // Diagnostic, so that extra values, position, and so on could be 954193326Sed // printed. 955193326Sed if (!A->isClaimed()) { 956193326Sed if (A->getOption().hasNoArgumentUnused()) 957193326Sed continue; 958193326Sed 959193326Sed // Suppress the warning automatically if this is just a flag, 960193326Sed // and it is an instance of an argument we already claimed. 961193326Sed const Option &Opt = A->getOption(); 962193326Sed if (isa<FlagOption>(Opt)) { 963193326Sed bool DuplicateClaimed = false; 964193326Sed 965193326Sed // FIXME: Use iterator. 966193326Sed for (ArgList::const_iterator it = C.getArgs().begin(), 967193326Sed ie = C.getArgs().end(); it != ie; ++it) { 968193326Sed if ((*it)->isClaimed() && (*it)->getOption().matches(Opt.getId())) { 969193326Sed DuplicateClaimed = true; 970193326Sed break; 971193326Sed } 972193326Sed } 973193326Sed 974193326Sed if (DuplicateClaimed) 975193326Sed continue; 976193326Sed } 977193326Sed 978193326Sed Diag(clang::diag::warn_drv_unused_argument) 979193326Sed << A->getAsString(C.getArgs()); 980193326Sed } 981193326Sed } 982193326Sed} 983193326Sed 984193326Sedvoid Driver::BuildJobsForAction(Compilation &C, 985193326Sed const Action *A, 986193326Sed const ToolChain *TC, 987193326Sed bool CanAcceptPipe, 988193326Sed bool AtTopLevel, 989193326Sed const char *LinkingOutput, 990193326Sed InputInfo &Result) const { 991193326Sed llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action"); 992193326Sed 993193326Sed bool UsePipes = C.getArgs().hasArg(options::OPT_pipe); 994193326Sed // FIXME: Pipes are forcibly disabled until we support executing 995193326Sed // them. 996193326Sed if (!CCCPrintBindings) 997193326Sed UsePipes = false; 998193326Sed 999193326Sed if (const InputAction *IA = dyn_cast<InputAction>(A)) { 1000193326Sed // FIXME: It would be nice to not claim this here; maybe the old 1001193326Sed // scheme of just using Args was better? 1002193326Sed const Arg &Input = IA->getInputArg(); 1003193326Sed Input.claim(); 1004193326Sed if (isa<PositionalArg>(Input)) { 1005193326Sed const char *Name = Input.getValue(C.getArgs()); 1006193326Sed Result = InputInfo(Name, A->getType(), Name); 1007193326Sed } else 1008193326Sed Result = InputInfo(&Input, A->getType(), ""); 1009193326Sed return; 1010193326Sed } 1011193326Sed 1012193326Sed if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) { 1013193326Sed const char *ArchName = BAA->getArchName(); 1014193326Sed std::string Arch; 1015193326Sed if (!ArchName) { 1016193326Sed Arch = C.getDefaultToolChain().getArchName(); 1017193326Sed ArchName = Arch.c_str(); 1018193326Sed } 1019193326Sed BuildJobsForAction(C, 1020193326Sed *BAA->begin(), 1021193326Sed Host->getToolChain(C.getArgs(), ArchName), 1022193326Sed CanAcceptPipe, 1023193326Sed AtTopLevel, 1024193326Sed LinkingOutput, 1025193326Sed Result); 1026193326Sed return; 1027193326Sed } 1028193326Sed 1029193326Sed const JobAction *JA = cast<JobAction>(A); 1030193326Sed const Tool &T = TC->SelectTool(C, *JA); 1031193326Sed 1032193326Sed // See if we should use an integrated preprocessor. We do so when we 1033193326Sed // have exactly one input, since this is the only use case we care 1034193326Sed // about (irrelevant since we don't support combine yet). 1035193326Sed bool UseIntegratedCPP = false; 1036193326Sed const ActionList *Inputs = &A->getInputs(); 1037193326Sed if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) { 1038193326Sed if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) && 1039193326Sed !C.getArgs().hasArg(options::OPT_traditional_cpp) && 1040193326Sed !C.getArgs().hasArg(options::OPT_save_temps) && 1041193326Sed T.hasIntegratedCPP()) { 1042193326Sed UseIntegratedCPP = true; 1043193326Sed Inputs = &(*Inputs)[0]->getInputs(); 1044193326Sed } 1045193326Sed } 1046193326Sed 1047193326Sed // Only use pipes when there is exactly one input. 1048193326Sed bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput(); 1049193326Sed InputInfoList InputInfos; 1050193326Sed for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end(); 1051193326Sed it != ie; ++it) { 1052193326Sed InputInfo II; 1053193326Sed BuildJobsForAction(C, *it, TC, TryToUsePipeInput, 1054193326Sed /*AtTopLevel*/false, 1055193326Sed LinkingOutput, 1056193326Sed II); 1057193326Sed InputInfos.push_back(II); 1058193326Sed } 1059193326Sed 1060193326Sed // Determine if we should output to a pipe. 1061193326Sed bool OutputToPipe = false; 1062193326Sed if (CanAcceptPipe && T.canPipeOutput()) { 1063193326Sed // Some actions default to writing to a pipe if they are the top 1064193326Sed // level phase and there was no user override. 1065193326Sed // 1066193326Sed // FIXME: Is there a better way to handle this? 1067193326Sed if (AtTopLevel) { 1068193326Sed if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o)) 1069193326Sed OutputToPipe = true; 1070193326Sed } else if (UsePipes) 1071193326Sed OutputToPipe = true; 1072193326Sed } 1073193326Sed 1074193326Sed // Figure out where to put the job (pipes). 1075193326Sed Job *Dest = &C.getJobs(); 1076193326Sed if (InputInfos[0].isPipe()) { 1077193326Sed assert(TryToUsePipeInput && "Unrequested pipe!"); 1078193326Sed assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs."); 1079193326Sed Dest = &InputInfos[0].getPipe(); 1080193326Sed } 1081193326Sed 1082193326Sed // Always use the first input as the base input. 1083193326Sed const char *BaseInput = InputInfos[0].getBaseInput(); 1084193326Sed 1085193326Sed // Determine the place to write output to (nothing, pipe, or 1086193326Sed // filename) and where to put the new job. 1087193326Sed if (JA->getType() == types::TY_Nothing) { 1088193326Sed Result = InputInfo(A->getType(), BaseInput); 1089193326Sed } else if (OutputToPipe) { 1090193326Sed // Append to current piped job or create a new one as appropriate. 1091193326Sed PipedJob *PJ = dyn_cast<PipedJob>(Dest); 1092193326Sed if (!PJ) { 1093193326Sed PJ = new PipedJob(); 1094193326Sed // FIXME: Temporary hack so that -ccc-print-bindings work until 1095193326Sed // we have pipe support. Please remove later. 1096193326Sed if (!CCCPrintBindings) 1097193326Sed cast<JobList>(Dest)->addJob(PJ); 1098193326Sed Dest = PJ; 1099193326Sed } 1100193326Sed Result = InputInfo(PJ, A->getType(), BaseInput); 1101193326Sed } else { 1102193326Sed Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel), 1103193326Sed A->getType(), BaseInput); 1104193326Sed } 1105193326Sed 1106193326Sed if (CCCPrintBindings) { 1107193326Sed llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"' 1108193326Sed << " - \"" << T.getName() << "\", inputs: ["; 1109193326Sed for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) { 1110193326Sed llvm::errs() << InputInfos[i].getAsString(); 1111193326Sed if (i + 1 != e) 1112193326Sed llvm::errs() << ", "; 1113193326Sed } 1114193326Sed llvm::errs() << "], output: " << Result.getAsString() << "\n"; 1115193326Sed } else { 1116193326Sed T.ConstructJob(C, *JA, *Dest, Result, InputInfos, 1117193326Sed C.getArgsForToolChain(TC), LinkingOutput); 1118193326Sed } 1119193326Sed} 1120193326Sed 1121193326Sedconst char *Driver::GetNamedOutputPath(Compilation &C, 1122193326Sed const JobAction &JA, 1123193326Sed const char *BaseInput, 1124193326Sed bool AtTopLevel) const { 1125193326Sed llvm::PrettyStackTraceString CrashInfo("Computing output path"); 1126193326Sed // Output to a user requested destination? 1127193326Sed if (AtTopLevel) { 1128193326Sed if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) 1129193326Sed return C.addResultFile(FinalOutput->getValue(C.getArgs())); 1130193326Sed } 1131193326Sed 1132193326Sed // Output to a temporary file? 1133193326Sed if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) { 1134193326Sed std::string TmpName = 1135193326Sed GetTemporaryPath(types::getTypeTempSuffix(JA.getType())); 1136193326Sed return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str())); 1137193326Sed } 1138193326Sed 1139193326Sed llvm::sys::Path BasePath(BaseInput); 1140193326Sed std::string BaseName(BasePath.getLast()); 1141193326Sed 1142193326Sed // Determine what the derived output name should be. 1143193326Sed const char *NamedOutput; 1144193326Sed if (JA.getType() == types::TY_Image) { 1145193326Sed NamedOutput = DefaultImageName.c_str(); 1146193326Sed } else { 1147193326Sed const char *Suffix = types::getTypeTempSuffix(JA.getType()); 1148193326Sed assert(Suffix && "All types used for output should have a suffix."); 1149193326Sed 1150193326Sed std::string::size_type End = std::string::npos; 1151193326Sed if (!types::appendSuffixForType(JA.getType())) 1152193326Sed End = BaseName.rfind('.'); 1153193326Sed std::string Suffixed(BaseName.substr(0, End)); 1154193326Sed Suffixed += '.'; 1155193326Sed Suffixed += Suffix; 1156193326Sed NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str()); 1157193326Sed } 1158193326Sed 1159193326Sed // As an annoying special case, PCH generation doesn't strip the 1160193326Sed // pathname. 1161193326Sed if (JA.getType() == types::TY_PCH) { 1162193326Sed BasePath.eraseComponent(); 1163193326Sed if (BasePath.isEmpty()) 1164193326Sed BasePath = NamedOutput; 1165193326Sed else 1166193326Sed BasePath.appendComponent(NamedOutput); 1167193326Sed return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str())); 1168193326Sed } else { 1169193326Sed return C.addResultFile(NamedOutput); 1170193326Sed } 1171193326Sed} 1172193326Sed 1173193326Sedllvm::sys::Path Driver::GetFilePath(const char *Name, 1174193326Sed const ToolChain &TC) const { 1175193326Sed const ToolChain::path_list &List = TC.getFilePaths(); 1176193326Sed for (ToolChain::path_list::const_iterator 1177193326Sed it = List.begin(), ie = List.end(); it != ie; ++it) { 1178193326Sed llvm::sys::Path P(*it); 1179193326Sed P.appendComponent(Name); 1180193326Sed if (P.exists()) 1181193326Sed return P; 1182193326Sed } 1183193326Sed 1184193326Sed return llvm::sys::Path(Name); 1185193326Sed} 1186193326Sed 1187193326Sedllvm::sys::Path Driver::GetProgramPath(const char *Name, 1188193326Sed const ToolChain &TC, 1189193326Sed bool WantFile) const { 1190193326Sed const ToolChain::path_list &List = TC.getProgramPaths(); 1191193326Sed for (ToolChain::path_list::const_iterator 1192193326Sed it = List.begin(), ie = List.end(); it != ie; ++it) { 1193193326Sed llvm::sys::Path P(*it); 1194193326Sed P.appendComponent(Name); 1195193326Sed if (WantFile ? P.exists() : P.canExecute()) 1196193326Sed return P; 1197193326Sed } 1198193326Sed 1199193326Sed // If all else failed, search the path. 1200193326Sed llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name)); 1201193326Sed if (!P.empty()) 1202193326Sed return P; 1203193326Sed 1204193326Sed return llvm::sys::Path(Name); 1205193326Sed} 1206193326Sed 1207193326Sedstd::string Driver::GetTemporaryPath(const char *Suffix) const { 1208193326Sed // FIXME: This is lame; sys::Path should provide this function (in 1209193326Sed // particular, it should know how to find the temporary files dir). 1210193326Sed std::string Error; 1211193326Sed const char *TmpDir = ::getenv("TMPDIR"); 1212193326Sed if (!TmpDir) 1213193326Sed TmpDir = ::getenv("TEMP"); 1214193326Sed if (!TmpDir) 1215193326Sed TmpDir = ::getenv("TMP"); 1216193326Sed if (!TmpDir) 1217193326Sed TmpDir = "/tmp"; 1218193326Sed llvm::sys::Path P(TmpDir); 1219193326Sed P.appendComponent("cc"); 1220193326Sed if (P.makeUnique(false, &Error)) { 1221193326Sed Diag(clang::diag::err_drv_unable_to_make_temp) << Error; 1222193326Sed return ""; 1223193326Sed } 1224193326Sed 1225193326Sed // FIXME: Grumble, makeUnique sometimes leaves the file around!? 1226193326Sed // PR3837. 1227193326Sed P.eraseFromDisk(false, 0); 1228193326Sed 1229193326Sed P.appendSuffix(Suffix); 1230193326Sed return P.toString(); 1231193326Sed} 1232193326Sed 1233193326Sedconst HostInfo *Driver::GetHostInfo(const char *TripleStr) const { 1234193326Sed llvm::PrettyStackTraceString CrashInfo("Constructing host"); 1235193326Sed llvm::Triple Triple(TripleStr); 1236193326Sed 1237193326Sed // Normalize Arch a bit. 1238193326Sed // 1239193326Sed // FIXME: We shouldn't need to do this once everything goes through the triple 1240193326Sed // interface. 1241193326Sed if (Triple.getArchName() == "i686") 1242193326Sed Triple.setArchName("i386"); 1243193326Sed else if (Triple.getArchName() == "amd64") 1244193326Sed Triple.setArchName("x86_64"); 1245193326Sed else if (Triple.getArchName() == "ppc" || 1246193326Sed Triple.getArchName() == "Power Macintosh") 1247193326Sed Triple.setArchName("powerpc"); 1248193326Sed else if (Triple.getArchName() == "ppc64") 1249193326Sed Triple.setArchName("powerpc64"); 1250193326Sed 1251193326Sed switch (Triple.getOS()) { 1252193326Sed case llvm::Triple::Darwin: 1253193326Sed return createDarwinHostInfo(*this, Triple); 1254193326Sed case llvm::Triple::DragonFly: 1255193326Sed return createDragonFlyHostInfo(*this, Triple); 1256195341Sed case llvm::Triple::OpenBSD: 1257195341Sed return createOpenBSDHostInfo(*this, Triple); 1258193326Sed case llvm::Triple::FreeBSD: 1259193326Sed return createFreeBSDHostInfo(*this, Triple); 1260193326Sed case llvm::Triple::Linux: 1261193326Sed return createLinuxHostInfo(*this, Triple); 1262193326Sed default: 1263193326Sed return createUnknownHostInfo(*this, Triple); 1264193326Sed } 1265193326Sed} 1266193326Sed 1267193326Sedbool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA, 1268193326Sed const std::string &ArchNameStr) const { 1269193326Sed // FIXME: Remove this hack. 1270193326Sed const char *ArchName = ArchNameStr.c_str(); 1271193326Sed if (ArchNameStr == "powerpc") 1272193326Sed ArchName = "ppc"; 1273193326Sed else if (ArchNameStr == "powerpc64") 1274193326Sed ArchName = "ppc64"; 1275193326Sed 1276193326Sed // Check if user requested no clang, or clang doesn't understand 1277193326Sed // this type (we only handle single inputs for now). 1278193326Sed if (!CCCUseClang || JA.size() != 1 || 1279193326Sed !types::isAcceptedByClang((*JA.begin())->getType())) 1280193326Sed return false; 1281193326Sed 1282193326Sed // Otherwise make sure this is an action clang understands. 1283193326Sed if (isa<PreprocessJobAction>(JA)) { 1284193326Sed if (!CCCUseClangCPP) { 1285193326Sed Diag(clang::diag::warn_drv_not_using_clang_cpp); 1286193326Sed return false; 1287193326Sed } 1288193326Sed } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA)) 1289193326Sed return false; 1290193326Sed 1291193326Sed // Use clang for C++? 1292193326Sed if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) { 1293193326Sed Diag(clang::diag::warn_drv_not_using_clang_cxx); 1294193326Sed return false; 1295193326Sed } 1296193326Sed 1297193326Sed // Always use clang for precompiling, regardless of archs. PTH is 1298193326Sed // platform independent, and this allows the use of the static 1299193326Sed // analyzer on platforms we don't have full IRgen support for. 1300193326Sed if (isa<PrecompileJobAction>(JA)) 1301193326Sed return true; 1302193326Sed 1303193326Sed // Finally, don't use clang if this isn't one of the user specified 1304193326Sed // archs to build. 1305193326Sed if (!CCCClangArchs.empty() && !CCCClangArchs.count(ArchName)) { 1306193326Sed Diag(clang::diag::warn_drv_not_using_clang_arch) << ArchName; 1307193326Sed return false; 1308193326Sed } 1309193326Sed 1310193326Sed return true; 1311193326Sed} 1312193326Sed 1313193326Sed/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and 1314193326Sed/// return the grouped values as integers. Numbers which are not 1315193326Sed/// provided are set to 0. 1316193326Sed/// 1317193326Sed/// \return True if the entire string was parsed (9.2), or all groups 1318193326Sed/// were parsed (10.3.5extrastuff). 1319193326Sedbool Driver::GetReleaseVersion(const char *Str, unsigned &Major, 1320193326Sed unsigned &Minor, unsigned &Micro, 1321193326Sed bool &HadExtra) { 1322193326Sed HadExtra = false; 1323193326Sed 1324193326Sed Major = Minor = Micro = 0; 1325193326Sed if (*Str == '\0') 1326193326Sed return true; 1327193326Sed 1328193326Sed char *End; 1329193326Sed Major = (unsigned) strtol(Str, &End, 10); 1330193326Sed if (*Str != '\0' && *End == '\0') 1331193326Sed return true; 1332193326Sed if (*End != '.') 1333193326Sed return false; 1334193326Sed 1335193326Sed Str = End+1; 1336193326Sed Minor = (unsigned) strtol(Str, &End, 10); 1337193326Sed if (*Str != '\0' && *End == '\0') 1338193326Sed return true; 1339193326Sed if (*End != '.') 1340193326Sed return false; 1341193326Sed 1342193326Sed Str = End+1; 1343193326Sed Micro = (unsigned) strtol(Str, &End, 10); 1344193326Sed if (*Str != '\0' && *End == '\0') 1345193326Sed return true; 1346193326Sed if (Str == End) 1347193326Sed return false; 1348193326Sed HadExtra = true; 1349193326Sed return true; 1350193326Sed} 1351