Driver.cpp revision 64952508c2b0c8bffb45c8b410f0af3d2457f59b
13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===--- Driver.cpp - Clang GCC Compatible Driver -----------------------*-===//
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//                     The LLVM Compiler Infrastructure
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// This file is distributed under the University of Illinois Open Source
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// License. See LICENSE.TXT for details.
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===----------------------------------------------------------------------===//
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Driver/Driver.h"
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Driver/Action.h"
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Driver/Arg.h"
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Driver/ArgList.h"
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Driver/Compilation.h"
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Driver/DriverDiagnostic.h"
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Driver/HostInfo.h"
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Driver/Job.h"
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Driver/OptTable.h"
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Driver/Option.h"
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Driver/Options.h"
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Driver/Tool.h"
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Driver/ToolChain.h"
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Driver/Types.h"
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Basic/Version.h"
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/ADT/StringSet.h"
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/ADT/OwningPtr.h"
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/Support/PrettyStackTrace.h"
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/Support/raw_ostream.h"
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/System/Path.h"
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/System/Program.h"
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "InputInfo.h"
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <map>
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing namespace clang::driver;
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing namespace clang;
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Used to set values for "production" clang, for releases.
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// #define USE_PRODUCTION_CLANG
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
453c827367444ee418f129b2c238299f49d3264554Jarkko PoyryDriver::Driver(llvm::StringRef _Name, llvm::StringRef _Dir,
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry               llvm::StringRef _DefaultHostTriple,
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry               llvm::StringRef _DefaultImageName,
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry               bool IsProduction, Diagnostic &_Diags)
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  : Opts(createDriverOptTable()), Diags(_Diags),
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    DefaultImageName(_DefaultImageName),
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Host(0),
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    CCCGenericGCCName("gcc"), CCCIsCXX(false), CCCEcho(false),
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    CCCPrintBindings(false), CheckInputsExist(true), CCCUseClang(true),
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    CCCUseClangCXX(true), CCCUseClangCPP(true), CCCUsePCH(true),
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    SuppressMissingInputWarning(false) {
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (IsProduction) {
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // In a "production" build, only use clang on architectures we expect to
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // work, and don't use clang C++.
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    //
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // During development its more convenient to always have the driver use
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // clang, but we don't want users to be confused when things don't work, or
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // to file bugs for things we don't support.
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    CCCClangArchs.insert(llvm::Triple::x86);
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    CCCClangArchs.insert(llvm::Triple::x86_64);
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    CCCClangArchs.insert(llvm::Triple::arm);
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    CCCUseClangCXX = false;
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Compute the path to the resource directory.
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  llvm::sys::Path P(Dir);
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  P.eraseComponent(); // Remove /bin from foo/bin
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  P.appendComponent("lib");
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  P.appendComponent("clang");
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  P.appendComponent(CLANG_VERSION_STRING);
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  ResourceDir = P.str();
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
803c827367444ee418f129b2c238299f49d3264554Jarkko PoyryDriver::~Driver() {
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  delete Opts;
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  delete Host;
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
853c827367444ee418f129b2c238299f49d3264554Jarkko PoyryInputArgList *Driver::ParseArgStrings(const char **ArgBegin,
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                      const char **ArgEnd) {
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  unsigned MissingArgIndex, MissingArgCount;
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  InputArgList *Args = getOpts().ParseArgs(ArgBegin, ArgEnd,
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                           MissingArgIndex, MissingArgCount);
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Check for missing argument error.
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (MissingArgCount)
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Diag(clang::diag::err_drv_missing_argument)
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      << Args->getArgString(MissingArgIndex) << MissingArgCount;
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Check for unsupported options.
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  for (ArgList::const_iterator it = Args->begin(), ie = Args->end();
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry       it != ie; ++it) {
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Arg *A = *it;
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (A->getOption().isUnsupported()) {
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      continue;
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return Args;
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1091bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
1101bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko PöyryCompilation *Driver::BuildCompilation(int argc, const char **argv) {
1111bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  llvm::PrettyStackTraceString CrashInfo("Compilation construction");
1121bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
1131bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  // FIXME: Handle environment options which effect driver behavior, somewhere
1141bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  // (client?). GCC_EXEC_PREFIX, COMPILER_PATH, LIBRARY_PATH, LPATH,
1151bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  // CC_PRINT_OPTIONS.
1161bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
1171bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  // FIXME: What are we going to do with -V and -b?
1181bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
1191bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  // FIXME: This stuff needs to go into the Compilation, not the driver.
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  bool CCCPrintOptions = false, CCCPrintActions = false;
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const char **Start = argv + 1, **End = argv + argc;
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const char *HostTriple = DefaultHostTriple.c_str();
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  InputArgList *Args = ParseArgStrings(Start, End);
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // -no-canonical-prefixes is used very early in main.
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Args->ClaimAllArgs(options::OPT_no_canonical_prefixes);
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Extract -ccc args.
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  //
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // FIXME: We need to figure out where this behavior should live. Most of it
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // should be outside in the client; the parts that aren't should have proper
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // options, either by introducing new ones or by overloading gcc ones like -V
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // or -b.
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  CCCPrintOptions = Args->hasArg(options::OPT_ccc_print_options);
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  CCCPrintActions = Args->hasArg(options::OPT_ccc_print_phases);
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  CCCPrintBindings = Args->hasArg(options::OPT_ccc_print_bindings);
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  CCCIsCXX = Args->hasArg(options::OPT_ccc_cxx) || CCCIsCXX;
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  CCCEcho = Args->hasArg(options::OPT_ccc_echo);
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (const Arg *A = Args->getLastArg(options::OPT_ccc_gcc_name))
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    CCCGenericGCCName = A->getValue(*Args);
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  CCCUseClangCXX = Args->hasFlag(options::OPT_ccc_clang_cxx,
1441bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry                                 options::OPT_ccc_no_clang_cxx,
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                 CCCUseClangCXX);
1461bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  CCCUsePCH = Args->hasFlag(options::OPT_ccc_pch_is_pch,
1471bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry                            options::OPT_ccc_pch_is_pth);
1481bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  CCCUseClang = !Args->hasArg(options::OPT_ccc_no_clang);
1491bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  CCCUseClangCPP = !Args->hasArg(options::OPT_ccc_no_clang_cpp);
1501bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  if (const Arg *A = Args->getLastArg(options::OPT_ccc_clang_archs)) {
1511bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    llvm::StringRef Cur = A->getValue(*Args);
1521bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
1531bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    CCCClangArchs.clear();
1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    while (!Cur.empty()) {
1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      std::pair<llvm::StringRef, llvm::StringRef> Split = Cur.split(',');
1561bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
1571bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      if (!Split.first.empty()) {
1581bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry        llvm::Triple::ArchType Arch =
1591bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry          llvm::Triple(Split.first, "", "").getArch();
1601bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
1611bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry        if (Arch == llvm::Triple::UnknownArch)
1621bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry          Diag(clang::diag::err_drv_invalid_arch_name) << Split.first;
1631bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
1641bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry        CCCClangArchs.insert(Arch);
1651bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      }
1661bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
1671bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      Cur = Split.second;
1681bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    }
1691bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  }
1701bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  if (const Arg *A = Args->getLastArg(options::OPT_ccc_host_triple))
1711bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    HostTriple = A->getValue(*Args);
1721bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  if (const Arg *A = Args->getLastArg(options::OPT_ccc_install_dir))
1731bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    Dir = A->getValue(*Args);
1741bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
1751bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  Host = GetHostInfo(HostTriple);
1761bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
1771bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  // The compilation takes ownership of Args.
1781bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  Compilation *C = new Compilation(*this, *Host->CreateToolChain(*Args), Args);
1791bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
1801bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  // FIXME: This behavior shouldn't be here.
1811bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  if (CCCPrintOptions) {
1821bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    PrintOptions(C->getArgs());
1831bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    return C;
1841bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  }
1851bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
1861bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  if (!HandleImmediateArgs(*C))
1873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return C;
1883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Construct the list of abstract actions to perform for this compilation. We
1903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // avoid passing a Compilation here simply to enforce the abstraction that
1913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // pipelining is not host or toolchain dependent (other than the driver driver
1923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // test).
1933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (Host->useDriverDriver())
1943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    BuildUniversalActions(C->getArgs(), C->getActions());
1953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  else
1963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    BuildActions(C->getArgs(), C->getActions());
1973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (CCCPrintActions) {
1993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    PrintActions(*C);
2003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return C;
2013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
2023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  BuildJobs(*C);
2043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return C;
2063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2083c827367444ee418f129b2c238299f49d3264554Jarkko Poyryint Driver::ExecuteCompilation(const Compilation &C) const {
2093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Just print if -### was present.
2103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
2113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    C.PrintJob(llvm::errs(), C.getJobs(), "\n", true);
2123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return 0;
2133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
2143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // If there were errors building the compilation, quit now.
2163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (getDiags().getNumErrors())
2173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return 1;
2183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const Command *FailingCommand = 0;
2203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  int Res = C.ExecuteJob(C.getJobs(), FailingCommand);
2213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Remove temp files.
2233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  C.CleanupFileList(C.getTempFiles());
2243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // If the compilation failed, remove result files as well.
2263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (Res != 0 && !C.getArgs().hasArg(options::OPT_save_temps))
2273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    C.CleanupFileList(C.getResultFiles(), true);
2283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Print extra information about abnormal failures, if possible.
2303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (Res) {
2313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // This is ad-hoc, but we don't want to be excessively noisy. If the result
2323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // status was 1, assume the command failed normally. In particular, if it
2333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // was the compiler then assume it gave a reasonable error code. Failures in
2343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // other tools are less common, and they generally have worse diagnostics,
2353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // so always print the diagnostic there.
2363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    const Action &Source = FailingCommand->getSource();
2373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) ||
2383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                           isa<PrecompileJobAction>(Source) ||
2393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                           isa<AnalyzeJobAction>(Source) ||
2403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                           isa<CompileJobAction>(Source));
2413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (!IsFriendlyTool || Res != 1) {
2433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // FIXME: See FIXME above regarding result code interpretation.
2443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (Res < 0)
2453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        Diag(clang::diag::err_drv_command_signalled)
2463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          << Source.getClassName() << -Res;
2473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      else
2483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        Diag(clang::diag::err_drv_command_failed)
2493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          << Source.getClassName() << Res;
2503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
2513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
2523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return Res;
2543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2563c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid Driver::PrintOptions(const ArgList &Args) const {
2573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  unsigned i = 0;
2583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
2593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry       it != ie; ++it, ++i) {
2603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Arg *A = *it;
2613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    llvm::errs() << "Option " << i << " - "
2623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                 << "Name: \"" << A->getOption().getName() << "\", "
2633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                 << "Values: {";
2643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    for (unsigned j = 0; j < A->getNumValues(); ++j) {
2653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (j)
2663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        llvm::errs() << ", ";
2673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      llvm::errs() << '"' << A->getValue(Args, j) << '"';
2683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
2693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    llvm::errs() << "}\n";
2703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
2711bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry}
2723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// FIXME: Move -ccc options to real options in the .td file (or eliminate), and
2743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// then move to using OptTable::PrintHelp.
2751bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyryvoid Driver::PrintHelp(bool ShowHidden) const {
2761bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  getOpts().PrintHelp(llvm::outs(), Name.c_str(),
2773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                      "clang \"gcc-compatible\" driver", ShowHidden);
2783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2803c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid Driver::PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const {
2811bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  // FIXME: The following handlers should use a callback mechanism, we don't
2821bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  // know what the client would like to do.
2833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  OS << getClangFullVersion() << '\n';
2843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const ToolChain &TC = C.getDefaultToolChain();
2853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  OS << "Target: " << TC.getTripleString() << '\n';
2863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Print the threading model.
2883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  //
2893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // FIXME: Implement correctly.
2903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  OS << "Thread model: " << "posix" << '\n';
2913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2933c827367444ee418f129b2c238299f49d3264554Jarkko Poyrybool Driver::HandleImmediateArgs(const Compilation &C) {
2941bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  // The order these options are handled in in gcc is all over the place, but we
2953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // don't expect inconsistencies w.r.t. that to matter in practice.
2963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2971bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  if (C.getArgs().hasArg(options::OPT_dumpversion)) {
2983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    llvm::outs() << CLANG_VERSION_STRING "\n";
2991bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    return false;
3003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
3013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (C.getArgs().hasArg(options::OPT__help) ||
3033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      C.getArgs().hasArg(options::OPT__help_hidden)) {
3043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden));
3051bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    return false;
3061bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  }
3071bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
3081bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  if (C.getArgs().hasArg(options::OPT__version)) {
3091bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    // Follow gcc behavior and use stdout for --version and stderr for -v.
3101bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    PrintVersion(C, llvm::outs());
3111bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    return false;
3121bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  }
3131bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
3141bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  if (C.getArgs().hasArg(options::OPT_v) ||
3151bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
3161bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    PrintVersion(C, llvm::errs());
3171bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    SuppressMissingInputWarning = true;
3181bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  }
3191bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
3201bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  const ToolChain &TC = C.getDefaultToolChain();
3211bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
3221bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    llvm::outs() << "programs: =";
3231bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
3243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry           ie = TC.getProgramPaths().end(); it != ie; ++it) {
3253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (it != TC.getProgramPaths().begin())
3263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        llvm::outs() << ':';
3273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      llvm::outs() << *it;
3283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
3293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    llvm::outs() << "\n";
3303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    llvm::outs() << "libraries: =";
3313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
3323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry           ie = TC.getFilePaths().end(); it != ie; ++it) {
3333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (it != TC.getFilePaths().begin())
3341bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry        llvm::outs() << ':';
3351bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      llvm::outs() << *it;
3361bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    }
3371bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    llvm::outs() << "\n";
3381bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    return false;
3391bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  }
3401bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
3411bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  // FIXME: The following handlers should use a callback mechanism, we don't
3421bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  // know what the client would like to do.
3431bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
3441bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC) << "\n";
3451bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    return false;
3461bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  }
3471bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
3481bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
3493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC) << "\n";
3503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return false;
3513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
3523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
3543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    llvm::outs() << GetFilePath("libgcc.a", TC) << "\n";
3553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return false;
3563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
3571bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
3583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (C.getArgs().hasArg(options::OPT_print_multi_lib)) {
3591bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    // FIXME: We need tool chain support for this.
3603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    llvm::outs() << ".;\n";
3613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    switch (C.getDefaultToolChain().getTriple().getArch()) {
3633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    default:
3643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      break;
3653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case llvm::Triple::x86_64:
3673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      llvm::outs() << "x86_64;@m64" << "\n";
3683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      break;
3693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case llvm::Triple::ppc64:
3713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      llvm::outs() << "ppc64;@m64" << "\n";
3723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      break;
3733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
3743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return false;
3753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
3763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // FIXME: What is the difference between print-multi-directory and
3783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // print-multi-os-directory?
3793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (C.getArgs().hasArg(options::OPT_print_multi_directory) ||
3803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      C.getArgs().hasArg(options::OPT_print_multi_os_directory)) {
3813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    switch (C.getDefaultToolChain().getTriple().getArch()) {
3823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    default:
3833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case llvm::Triple::x86:
3843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case llvm::Triple::ppc:
3853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      llvm::outs() << "." << "\n";
3863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      break;
3873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case llvm::Triple::x86_64:
3893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      llvm::outs() << "x86_64" << "\n";
3903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      break;
3913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case llvm::Triple::ppc64:
3933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      llvm::outs() << "ppc64" << "\n";
3943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      break;
3953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
3963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return false;
3973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
3983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return true;
4003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4023c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic unsigned PrintActions1(const Compilation &C, Action *A,
4033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                              std::map<Action*, unsigned> &Ids) {
4043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (Ids.count(A))
4053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return Ids[A];
4063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  std::string str;
4083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  llvm::raw_string_ostream os(str);
4093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  os << Action::getClassName(A->getKind()) << ", ";
4113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (InputAction *IA = dyn_cast<InputAction>(A)) {
4123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
4133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
4143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    os << '"' << (BIA->getArchName() ? BIA->getArchName() :
4153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                  C.getDefaultToolChain().getArchName()) << '"'
4163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry       << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
4173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  } else {
4183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    os << "{";
4193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
4203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      os << PrintActions1(C, *it, Ids);
4213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      ++it;
4223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (it != ie)
4233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        os << ", ";
4243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
4253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    os << "}";
4263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
4273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  unsigned Id = Ids.size();
4293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Ids[A] = Id;
4303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  llvm::errs() << Id << ": " << os.str() << ", "
4313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry               << types::getTypeName(A->getType()) << "\n";
4323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return Id;
4343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4363c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid Driver::PrintActions(const Compilation &C) const {
4373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  std::map<Action*, unsigned> Ids;
4383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  for (ActionList::const_iterator it = C.getActions().begin(),
4393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry         ie = C.getActions().end(); it != ie; ++it)
4403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    PrintActions1(C, *it, Ids);
4413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4433c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid Driver::BuildUniversalActions(const ArgList &Args,
4443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                   ActionList &Actions) const {
4453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  llvm::PrettyStackTraceString CrashInfo("Building universal build actions");
4463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Collect the list of architectures. Duplicates are allowed, but should only
4473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // be handled once (in the order seen).
4483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  llvm::StringSet<> ArchNames;
4493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  llvm::SmallVector<const char *, 4> Archs;
4501bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
4513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry       it != ie; ++it) {
4523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Arg *A = *it;
4533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (A->getOption().matches(options::OPT_arch)) {
4551bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      // Validate the option here; we don't save the type here because its
4561bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      // particular spelling may participate in other driver choices.
4571bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      llvm::Triple::ArchType Arch =
4581bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry        llvm::Triple::getArchTypeForDarwinArchName(A->getValue(Args));
4591bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      if (Arch == llvm::Triple::UnknownArch) {
4601bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry        Diag(clang::diag::err_drv_invalid_arch_name)
4613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          << A->getAsString(Args);
4623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        continue;
4633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      }
4643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      A->claim();
4663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (ArchNames.insert(A->getValue(Args)))
4673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        Archs.push_back(A->getValue(Args));
4683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
4693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
4703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // When there is no explicit arch for this platform, make sure we still bind
4723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // the architecture (to the default) so that -Xarch_ is handled correctly.
4733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (!Archs.size())
4743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Archs.push_back(0);
4753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // FIXME: We killed off some others but these aren't yet detected in a
4773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // functional manner. If we added information to jobs about which "auxiliary"
4783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // files they wrote then we could detect the conflict these cause downstream.
4793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (Archs.size() > 1) {
4803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // No recovery needed, the point of this is just to prevent
4813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // overwriting the same files.
4823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
4833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
4843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        << A->getAsString(Args);
4853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
4863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  ActionList SingleActions;
4883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  BuildActions(Args, SingleActions);
4893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Add in arch binding and lipo (if necessary) for every top level action.
4911bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
4923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Action *Act = SingleActions[i];
4933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // Make sure we can lipo this kind of output. If not (and it is an actual
4953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // output) then we disallow, since we can't create an output file with the
4963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // right name without overwriting it. We could remove this oddity by just
4973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // changing the output names to include the arch, which would also fix
4983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // -save-temps. Compatibility wins for now.
4993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
5013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
5023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        << types::getTypeName(Act->getType());
5033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    ActionList Inputs;
5053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    for (unsigned i = 0, e = Archs.size(); i != e; ++i)
5063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      Inputs.push_back(new BindArchAction(Act, Archs[i]));
5073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // Lipo if necessary, we do it this way because we need to set the arch flag
5093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // so that -Xarch_ gets overwritten.
5103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
5113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      Actions.append(Inputs.begin(), Inputs.end());
5123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    else
5133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
5143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
5153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
5163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5173c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
5183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
5193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Start by constructing the list of inputs and their types.
5201bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
5213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Track the current user specified (-x) input. We also explicitly track the
5221bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  // argument used to set the type; we only want to claim the type when we
5233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // actually use it, so we warn about unused -x arguments.
5243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  types::ID InputType = types::TY_Nothing;
5253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Arg *InputTypeArg = 0;
5263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
5283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
5293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry       it != ie; ++it) {
5303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Arg *A = *it;
5313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (isa<InputOption>(A->getOption())) {
5333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      const char *Value = A->getValue(Args);
5343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      types::ID Ty = types::TY_INVALID;
5353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // Infer the input type if necessary.
5373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (InputType == types::TY_Nothing) {
5383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        // If there was an explicit arg for this, claim it.
5393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        if (InputTypeArg)
5403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          InputTypeArg->claim();
5413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        // stdin must be handled specially.
5433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        if (memcmp(Value, "-", 2) == 0) {
5443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          // If running with -E, treat as a C input (this changes the builtin
5453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          // macros, for example). This may be overridden by -ObjC below.
5463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          //
5473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          // Otherwise emit an error but still use a valid type to avoid
5483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          // spurious errors (e.g., no inputs).
5493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          if (!Args.hasArgNoClaim(options::OPT_E))
5503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            Diag(clang::diag::err_drv_unknown_stdin_type);
5513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          Ty = types::TY_C;
5523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        } else {
5533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          // Otherwise lookup by extension, and fallback to ObjectType if not
5543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          // found. We use a host hook here because Darwin at least has its own
5553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          // idea of what .s is.
5563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          if (const char *Ext = strrchr(Value, '.'))
5573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            Ty = Host->lookupTypeForExtension(Ext + 1);
5583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          if (Ty == types::TY_INVALID)
5603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            Ty = types::TY_Object;
5613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        }
5623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        // -ObjC and -ObjC++ override the default language, but only for "source
5643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        // files". We just treat everything that isn't a linker input as a
5653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        // source file.
5663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        //
5673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        // FIXME: Clean this up if we move the phase sequence into the type.
5683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        if (Ty != types::TY_Object) {
5693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          if (Args.hasArg(options::OPT_ObjC))
5703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            Ty = types::TY_ObjC;
5713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          else if (Args.hasArg(options::OPT_ObjCXX))
5723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            Ty = types::TY_ObjCXX;
5733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        }
5743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      } else {
5753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        assert(InputTypeArg && "InputType set w/o InputTypeArg");
5763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        InputTypeArg->claim();
5773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        Ty = InputType;
5783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      }
5793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // Check that the file exists, if enabled.
5813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (CheckInputsExist && memcmp(Value, "-", 2) != 0 &&
5823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          !llvm::sys::Path(Value).exists())
5833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
5843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      else
5853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        Inputs.push_back(std::make_pair(Ty, A));
5863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    } else if (A->getOption().isLinkerInput()) {
5883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // Just treat as object type, we could make a special type for this if
5893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // necessary.
5903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      Inputs.push_back(std::make_pair(types::TY_Object, A));
5913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    } else if (A->getOption().matches(options::OPT_x)) {
5931bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      InputTypeArg = A;
5943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
5953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // Follow gcc behavior and treat as linker input for invalid -x
5973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // options. Its not clear why we shouldn't just revert to unknown; but
5983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // this isn't very important, we might as well be bug comatible.
5993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (!InputType) {
6003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
6013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        InputType = types::TY_Object;
6023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      }
6033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
6043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
6053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (!SuppressMissingInputWarning && Inputs.empty()) {
6073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Diag(clang::diag::err_drv_no_input_files);
6083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return;
6093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
6103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Determine which compilation mode we are in. We look for options which
6123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // affect the phase, starting with the earliest phases, and record which
6133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // option we used to determine the final phase.
6143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Arg *FinalPhaseArg = 0;
6153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  phases::ID FinalPhase;
6163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // -{E,M,MM} only run the preprocessor.
6183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
6193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
6203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
6213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    FinalPhase = phases::Preprocess;
6223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // -{fsyntax-only,-analyze,emit-ast,S} only run up to the compiler.
6243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
6253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry             (FinalPhaseArg = Args.getLastArg(options::OPT_rewrite_objc)) ||
6263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry             (FinalPhaseArg = Args.getLastArg(options::OPT__analyze,
6273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                              options::OPT__analyze_auto)) ||
6283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry             (FinalPhaseArg = Args.getLastArg(options::OPT_emit_ast)) ||
6293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry             (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
6303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    FinalPhase = phases::Compile;
6313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // -c only runs up to the assembler.
6333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
6343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    FinalPhase = phases::Assemble;
6353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // Otherwise do everything.
6373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  } else
6383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    FinalPhase = phases::Link;
6393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Reject -Z* at the top level, these options should never have been exposed
6413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // by gcc.
6423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
6433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
6443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Construct the actions to perform.
6463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  ActionList LinkerInputs;
6473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
6483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    types::ID InputType = Inputs[i].first;
6493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    const Arg *InputArg = Inputs[i].second;
6503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    unsigned NumSteps = types::getNumCompilationPhases(InputType);
6523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    assert(NumSteps && "Invalid number of steps!");
6533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // If the first step comes after the final phase we are doing as part of
6553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // this compilation, warn the user about it.
6563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
6573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (InitialPhase > FinalPhase) {
6583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // Claim here to avoid the more general unused warning.
6593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      InputArg->claim();
6603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // Special case '-E' warning on a previously preprocessed file to make
6623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // more sense.
6633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (InitialPhase == phases::Compile && FinalPhase == phases::Preprocess &&
6643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          getPreprocessedType(InputType) == types::TY_INVALID)
6653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        Diag(clang::diag::warn_drv_preprocessed_input_file_unused)
6663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          << InputArg->getAsString(Args)
6673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          << FinalPhaseArg->getOption().getName();
6683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      else
6693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        Diag(clang::diag::warn_drv_input_file_unused)
6703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          << InputArg->getAsString(Args)
6713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          << getPhaseName(InitialPhase)
6723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          << FinalPhaseArg->getOption().getName();
6733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      continue;
6743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
6753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // Build the pipeline for this file.
6773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    llvm::OwningPtr<Action> Current(new InputAction(*InputArg, InputType));
6783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    for (unsigned i = 0; i != NumSteps; ++i) {
6793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      phases::ID Phase = types::getCompilationPhase(InputType, i);
6803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // We are done if this step is past what the user requested.
6823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (Phase > FinalPhase)
6833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        break;
6843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // Queue linker inputs.
6863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (Phase == phases::Link) {
6873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        assert(i + 1 == NumSteps && "linking must be final compilation step.");
6883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        LinkerInputs.push_back(Current.take());
6891bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry        break;
6901bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      }
6913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // Some types skip the assembler phase (e.g., llvm-bc), but we can't
6933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // encode this in the steps because the intermediate type depends on
6943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // arguments. Just special case here.
6953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
6963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        continue;
6973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // Otherwise construct the appropriate action.
6993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      Current.reset(ConstructPhaseAction(Args, Phase, Current.take()));
7003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (Current->getType() == types::TY_Nothing)
7013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        break;
7023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
7033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // If we ended with something, add to the output list.
7053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (Current)
7063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      Actions.push_back(Current.take());
7073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
7083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Add a link action if necessary.
7103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (!LinkerInputs.empty())
7113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
7123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // If we are linking, claim any options which are obviously only used for
7143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // compilation.
7153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (FinalPhase == phases::Link)
7163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Args.ClaimAllArgs(options::OPT_CompileOnly_Group);
7173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
7183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7193c827367444ee418f129b2c238299f49d3264554Jarkko PoyryAction *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
7203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                     Action *Input) const {
7213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
7223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Build the appropriate action.
7233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  switch (Phase) {
7241bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  case phases::Link: assert(0 && "link action invalid here.");
7251bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  case phases::Preprocess: {
7261bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    types::ID OutputTy;
7271bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    // -{M, MM} alter the output type.
7283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) {
7291bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      OutputTy = types::TY_Dependencies;
7301bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    } else {
7311bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      OutputTy = types::getPreprocessedType(Input->getType());
7321bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      assert(OutputTy != types::TY_INVALID &&
7333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry             "Cannot preprocess this input type!");
7343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
7353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return new PreprocessJobAction(Input, OutputTy);
7363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
7373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  case phases::Precompile:
7383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return new PrecompileJobAction(Input, types::TY_PCH);
7393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  case phases::Compile: {
7403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    bool HasO4 = false;
7413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (const Arg *A = Args.getLastArg(options::OPT_O_Group))
7423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      HasO4 = A->getOption().matches(options::OPT_O4);
7433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (Args.hasArg(options::OPT_fsyntax_only)) {
7453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      return new CompileJobAction(Input, types::TY_Nothing);
7463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    } else if (Args.hasArg(options::OPT_rewrite_objc)) {
7473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      return new CompileJobAction(Input, types::TY_RewrittenObjC);
7483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    } else if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) {
7493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      return new AnalyzeJobAction(Input, types::TY_Plist);
7503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    } else if (Args.hasArg(options::OPT_emit_ast)) {
7518852c82a1ffa4760985c17cc6875d5d521daf343Jarkko Poyry      return new CompileJobAction(Input, types::TY_AST);
7528852c82a1ffa4760985c17cc6875d5d521daf343Jarkko Poyry    } else if (Args.hasArg(options::OPT_emit_llvm) ||
7538852c82a1ffa4760985c17cc6875d5d521daf343Jarkko Poyry               Args.hasArg(options::OPT_flto) || HasO4) {
7548852c82a1ffa4760985c17cc6875d5d521daf343Jarkko Poyry      types::ID Output =
7553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
7563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      return new CompileJobAction(Input, Output);
7573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    } else {
7583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      return new CompileJobAction(Input, types::TY_PP_Asm);
7593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
7603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
7613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  case phases::Assemble:
7623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return new AssembleJobAction(Input, types::TY_Object);
7631bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  }
7641bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
7653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  assert(0 && "invalid phase in ConstructPhaseAction");
7663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return 0;
767f6981ac458964bbfe296b497bff57c8fee8eb589Pyry Haulos}
7681bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
7691bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyryvoid Driver::BuildJobs(Compilation &C) const {
7701bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
7711bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
7721bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
7731bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
7741bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  // FIXME: Pipes are forcibly disabled until we support executing them.
775f6981ac458964bbfe296b497bff57c8fee8eb589Pyry Haulos  if (!CCCPrintBindings)
776f6981ac458964bbfe296b497bff57c8fee8eb589Pyry Haulos    UsePipes = false;
7773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // -save-temps inhibits pipes.
7793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (SaveTemps && UsePipes)
7801bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
7811bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
7821bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
7831bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
7841bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  // It is an error to provide a -o option if we are making multiple output
7851bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  // files.
7861bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  if (FinalOutput) {
7871bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    unsigned NumOutputs = 0;
7881bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    for (ActionList::const_iterator it = C.getActions().begin(),
7891bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry           ie = C.getActions().end(); it != ie; ++it)
7901bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      if ((*it)->getType() != types::TY_Nothing)
7911bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry        ++NumOutputs;
7921bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
7931bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    if (NumOutputs > 1) {
7941bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      Diag(clang::diag::err_drv_output_argument_with_multiple_files);
7951bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      FinalOutput = 0;
7961bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    }
7973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
7983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  for (ActionList::const_iterator it = C.getActions().begin(),
8003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry         ie = C.getActions().end(); it != ie; ++it) {
8013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Action *A = *it;
8023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // If we are linking an image for multiple archs then the linker wants
8043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // -arch_multiple and -final_output <final image name>. Unfortunately, this
8053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // doesn't fit in cleanly because we have to pass this information down.
8063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    //
8073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // FIXME: This is a hack; find a cleaner way to integrate this into the
8083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // process.
8093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    const char *LinkingOutput = 0;
8103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (isa<LipoJobAction>(A)) {
8113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (FinalOutput)
8123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        LinkingOutput = FinalOutput->getValue(C.getArgs());
8133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      else
8143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        LinkingOutput = DefaultImageName.c_str();
8153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
8163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    InputInfo II;
8183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    BuildJobsForAction(C, A, &C.getDefaultToolChain(),
8193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                       /*BoundArch*/0,
8203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                       /*CanAcceptPipe*/ true,
8213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                       /*AtTopLevel*/ true,
8223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                       /*LinkingOutput*/ LinkingOutput,
8233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                       II);
8243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
8253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // If the user passed -Qunused-arguments or there were errors, don't warn
8273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // about any unused arguments.
8283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (Diags.getNumErrors() ||
8293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      C.getArgs().hasArg(options::OPT_Qunused_arguments))
8303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return;
8313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Claim -### here.
8333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
8343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
8363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry       it != ie; ++it) {
8373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Arg *A = *it;
8383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // FIXME: It would be nice to be able to send the argument to the
8403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // Diagnostic, so that extra values, position, and so on could be printed.
8411bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    if (!A->isClaimed()) {
8421bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      if (A->getOption().hasNoArgumentUnused())
8431bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry        continue;
8443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // Suppress the warning automatically if this is just a flag, and it is an
8461bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      // instance of an argument we already claimed.
8473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      const Option &Opt = A->getOption();
8483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (isa<FlagOption>(Opt)) {
8491bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry        bool DuplicateClaimed = false;
8501bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
8511bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry        for (arg_iterator it = C.getArgs().filtered_begin(&Opt),
8523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry               ie = C.getArgs().filtered_end(); it != ie; ++it) {
8533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          if ((*it)->isClaimed()) {
8541bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry            DuplicateClaimed = true;
8553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            break;
8563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          }
8573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        }
8583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        if (DuplicateClaimed)
8603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          continue;
8613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      }
8623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      Diag(clang::diag::warn_drv_unused_argument)
8643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        << A->getAsString(C.getArgs());
8653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
8668852c82a1ffa4760985c17cc6875d5d521daf343Jarkko Poyry  }
8673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
8683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8693c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic const Tool &SelectToolForJob(Compilation &C, const ToolChain *TC,
8703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                    const JobAction *JA,
8713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                    const ActionList *&Inputs) {
8723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const Tool *ToolForJob = 0;
8733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // See if we should look for a compiler with an integrated assembler. We match
8753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // bottom up, so what we are actually looking for is an assembler job with a
8763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // compiler input.
8773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (C.getArgs().hasArg(options::OPT_integrated_as,
8783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                         options::OPT_no_integrated_as,
8793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                         TC->IsIntegratedAssemblerDefault()) &&
8803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      !C.getArgs().hasArg(options::OPT_save_temps) &&
8813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      isa<AssembleJobAction>(JA) &&
8823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      Inputs->size() == 1 && isa<CompileJobAction>(*Inputs->begin())) {
8833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    const Tool &Compiler = TC->SelectTool(C,cast<JobAction>(**Inputs->begin()));
8843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (Compiler.hasIntegratedAssembler()) {
8851bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      Inputs = &(*Inputs)[0]->getInputs();
8861bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry      ToolForJob = &Compiler;
8873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
8883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
8893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Otherwise use the tool for the current job.
8913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (!ToolForJob)
8921bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    ToolForJob = &TC->SelectTool(C, *JA);
8931bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry
8943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // See if we should use an integrated preprocessor. We do so when we have
8958852c82a1ffa4760985c17cc6875d5d521daf343Jarkko Poyry  // exactly one input, since this is the only use case we care about
8968852c82a1ffa4760985c17cc6875d5d521daf343Jarkko Poyry  // (irrelevant since we don't support combine yet).
8978852c82a1ffa4760985c17cc6875d5d521daf343Jarkko Poyry  if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin()) &&
8988852c82a1ffa4760985c17cc6875d5d521daf343Jarkko Poyry      !C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
8998852c82a1ffa4760985c17cc6875d5d521daf343Jarkko Poyry      !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
9003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      !C.getArgs().hasArg(options::OPT_save_temps) &&
9013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      ToolForJob->hasIntegratedCPP())
9023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Inputs = &(*Inputs)[0]->getInputs();
9033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return *ToolForJob;
9053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
9063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9073c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid Driver::BuildJobsForAction(Compilation &C,
9083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                const Action *A,
9093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                const ToolChain *TC,
9103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                const char *BoundArch,
9113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                bool CanAcceptPipe,
9123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                bool AtTopLevel,
9133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                const char *LinkingOutput,
9143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                InputInfo &Result) const {
9153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
9163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
9183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // FIXME: Pipes are forcibly disabled until we support executing them.
9193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (!CCCPrintBindings)
9203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    UsePipes = false;
9213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (const InputAction *IA = dyn_cast<InputAction>(A)) {
9233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // FIXME: It would be nice to not claim this here; maybe the old scheme of
9243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // just using Args was better?
9253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    const Arg &Input = IA->getInputArg();
9263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Input.claim();
9273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (isa<PositionalArg>(Input)) {
9283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      const char *Name = Input.getValue(C.getArgs());
9293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      Result = InputInfo(Name, A->getType(), Name);
9303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    } else
9313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      Result = InputInfo(&Input, A->getType(), "");
9323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return;
9333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
9343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
9363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    const ToolChain *TC = &C.getDefaultToolChain();
9373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    std::string Arch;
9393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (BAA->getArchName())
9403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      TC = Host->CreateToolChain(C.getArgs(), BAA->getArchName());
9413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    BuildJobsForAction(C, *BAA->begin(), TC, BAA->getArchName(),
9433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                       CanAcceptPipe, AtTopLevel, LinkingOutput, Result);
9443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return;
9453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
9463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const ActionList *Inputs = &A->getInputs();
9483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const JobAction *JA = cast<JobAction>(A);
9503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const Tool &T = SelectToolForJob(C, TC, JA, Inputs);
9513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Only use pipes when there is exactly one input.
9533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
9543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  InputInfoList InputInfos;
9553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
9563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry       it != ie; ++it) {
9573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    InputInfo II;
9583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    BuildJobsForAction(C, *it, TC, BoundArch, TryToUsePipeInput,
9593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                       /*AtTopLevel*/false, LinkingOutput, II);
9603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    InputInfos.push_back(II);
9613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
9623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Determine if we should output to a pipe.
9643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  bool OutputToPipe = false;
9653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (CanAcceptPipe && T.canPipeOutput()) {
9663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // Some actions default to writing to a pipe if they are the top level phase
9673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // and there was no user override.
9683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    //
9693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // FIXME: Is there a better way to handle this?
9703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (AtTopLevel) {
9713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
9723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        OutputToPipe = true;
9733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    } else if (UsePipes)
9743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      OutputToPipe = true;
9753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
9763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Figure out where to put the job (pipes).
9783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Job *Dest = &C.getJobs();
9793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (InputInfos[0].isPipe()) {
9803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    assert(TryToUsePipeInput && "Unrequested pipe!");
9813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
9823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Dest = &InputInfos[0].getPipe();
9833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
9843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Always use the first input as the base input.
9863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const char *BaseInput = InputInfos[0].getBaseInput();
9873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Determine the place to write output to (nothing, pipe, or filename) and
9893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // where to put the new job.
9903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (JA->getType() == types::TY_Nothing) {
9913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Result = InputInfo(A->getType(), BaseInput);
9923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  } else if (OutputToPipe) {
9933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // Append to current piped job or create a new one as appropriate.
9943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    PipedJob *PJ = dyn_cast<PipedJob>(Dest);
9953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (!PJ) {
9963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      PJ = new PipedJob();
9973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // FIXME: Temporary hack so that -ccc-print-bindings work until we have
9983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // pipe support. Please remove later.
9993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (!CCCPrintBindings)
10003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        cast<JobList>(Dest)->addJob(PJ);
10013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      Dest = PJ;
10023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
10033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Result = InputInfo(PJ, A->getType(), BaseInput);
10043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  } else {
10053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
10063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                       A->getType(), BaseInput);
10073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
10083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (CCCPrintBindings) {
10103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"'
10113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                 << " - \"" << T.getName() << "\", inputs: [";
10123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
10133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      llvm::errs() << InputInfos[i].getAsString();
10143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (i + 1 != e)
10153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        llvm::errs() << ", ";
10163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
10173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    llvm::errs() << "], output: " << Result.getAsString() << "\n";
10183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  } else {
10193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    T.ConstructJob(C, *JA, *Dest, Result, InputInfos,
10203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                   C.getArgsForToolChain(TC, BoundArch), LinkingOutput);
10213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
10223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
10233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10243c827367444ee418f129b2c238299f49d3264554Jarkko Poyryconst char *Driver::GetNamedOutputPath(Compilation &C,
10253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                       const JobAction &JA,
10263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                       const char *BaseInput,
10273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                       bool AtTopLevel) const {
10283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  llvm::PrettyStackTraceString CrashInfo("Computing output path");
10293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Output to a user requested destination?
10303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (AtTopLevel) {
10313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
10323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      return C.addResultFile(FinalOutput->getValue(C.getArgs()));
10333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
10343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Output to a temporary file?
10363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
10373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    std::string TmpName =
10383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
10393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
10403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
10413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  llvm::sys::Path BasePath(BaseInput);
10433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  std::string BaseName(BasePath.getLast());
10443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Determine what the derived output name should be.
10463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const char *NamedOutput;
10473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (JA.getType() == types::TY_Image) {
10483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    NamedOutput = DefaultImageName.c_str();
10493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  } else {
10503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    const char *Suffix = types::getTypeTempSuffix(JA.getType());
10513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    assert(Suffix && "All types used for output should have a suffix.");
10523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    std::string::size_type End = std::string::npos;
10543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (!types::appendSuffixForType(JA.getType()))
10553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      End = BaseName.rfind('.');
10563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    std::string Suffixed(BaseName.substr(0, End));
10573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Suffixed += '.';
10583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Suffixed += Suffix;
10593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
10603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
10613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // As an annoying special case, PCH generation doesn't strip the pathname.
10633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (JA.getType() == types::TY_PCH) {
10643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    BasePath.eraseComponent();
10653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (BasePath.isEmpty())
10663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      BasePath = NamedOutput;
10673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    else
10683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      BasePath.appendComponent(NamedOutput);
10693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
10703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  } else {
10713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return C.addResultFile(NamedOutput);
10723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
10733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
10743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10753c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystd::string Driver::GetFilePath(const char *Name, const ToolChain &TC) const {
10763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const ToolChain::path_list &List = TC.getFilePaths();
10773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  for (ToolChain::path_list::const_iterator
10788852c82a1ffa4760985c17cc6875d5d521daf343Jarkko Poyry         it = List.begin(), ie = List.end(); it != ie; ++it) {
10791bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry    llvm::sys::Path P(*it);
10803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    P.appendComponent(Name);
10813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (P.exists())
10823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      return P.str();
10833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
10843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return Name;
10863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
10873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10883c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystd::string Driver::GetProgramPath(const char *Name, const ToolChain &TC,
10893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                   bool WantFile) const {
10903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const ToolChain::path_list &List = TC.getProgramPaths();
10913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  for (ToolChain::path_list::const_iterator
10923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry         it = List.begin(), ie = List.end(); it != ie; ++it) {
10933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    llvm::sys::Path P(*it);
10943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    P.appendComponent(Name);
10953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (WantFile ? P.exists() : P.canExecute())
10963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      return P.str();
10973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
10983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // If all else failed, search the path.
11003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
11013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (!P.empty())
11023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return P.str();
11033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
11043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return Name;
11053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
11063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
11073c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystd::string Driver::GetTemporaryPath(const char *Suffix) const {
11083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // FIXME: This is lame; sys::Path should provide this function (in particular,
11093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // it should know how to find the temporary files dir).
11103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  std::string Error;
11113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const char *TmpDir = ::getenv("TMPDIR");
11123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (!TmpDir)
11133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    TmpDir = ::getenv("TEMP");
11143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (!TmpDir)
11153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    TmpDir = ::getenv("TMP");
11163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (!TmpDir)
11173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    TmpDir = "/tmp";
11183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  llvm::sys::Path P(TmpDir);
11193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  P.appendComponent("cc");
11203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (P.makeUnique(false, &Error)) {
11213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
11223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return "";
11233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
11243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
11253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // FIXME: Grumble, makeUnique sometimes leaves the file around!?  PR3837.
11263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  P.eraseFromDisk(false, 0);
11273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
11283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  P.appendSuffix(Suffix);
11293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return P.str();
11303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
11313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
11323c827367444ee418f129b2c238299f49d3264554Jarkko Poyryconst HostInfo *Driver::GetHostInfo(const char *TripleStr) const {
11331bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  llvm::PrettyStackTraceString CrashInfo("Constructing host");
11343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  llvm::Triple Triple(TripleStr);
11353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
11363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  switch (Triple.getOS()) {
11373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  case llvm::Triple::AuroraUX:
11383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return createAuroraUXHostInfo(*this, Triple);
11393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  case llvm::Triple::Darwin:
11403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return createDarwinHostInfo(*this, Triple);
11413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  case llvm::Triple::DragonFly:
11423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return createDragonFlyHostInfo(*this, Triple);
11433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  case llvm::Triple::OpenBSD:
11443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return createOpenBSDHostInfo(*this, Triple);
11453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  case llvm::Triple::FreeBSD:
11463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return createFreeBSDHostInfo(*this, Triple);
11473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  case llvm::Triple::Linux:
11483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return createLinuxHostInfo(*this, Triple);
11493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  default:
11503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return createUnknownHostInfo(*this, Triple);
11513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
11523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
11533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
11543c827367444ee418f129b2c238299f49d3264554Jarkko Poyrybool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
11553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                    const llvm::Triple &Triple) const {
11563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Check if user requested no clang, or clang doesn't understand this type (we
11573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // only handle single inputs for now).
11583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (!CCCUseClang || JA.size() != 1 ||
11593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      !types::isAcceptedByClang((*JA.begin())->getType()))
11603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return false;
11613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
11623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Otherwise make sure this is an action clang understands.
11633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (isa<PreprocessJobAction>(JA)) {
11643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (!CCCUseClangCPP) {
11653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      Diag(clang::diag::warn_drv_not_using_clang_cpp);
11663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      return false;
11673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
11683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
11693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return false;
11703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
11713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Use clang for C++?
11723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
11733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Diag(clang::diag::warn_drv_not_using_clang_cxx);
11743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return false;
11753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
11763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
11773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Always use clang for precompiling, AST generation, and rewriting,
11783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // regardless of archs.
11793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (isa<PrecompileJobAction>(JA) || JA.getType() == types::TY_AST ||
11803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      JA.getType() == types::TY_RewrittenObjC)
11813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return true;
11823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
11831bfa102df7c2678fa4ac421fa29780848c2c12f3Jarkko Pöyry  // Finally, don't use clang if this isn't one of the user specified archs to
11843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // build.
11853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (!CCCClangArchs.empty() && !CCCClangArchs.count(Triple.getArch())) {
11863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Diag(clang::diag::warn_drv_not_using_clang_arch) << Triple.getArchName();
11873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return false;
11883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
11893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
11903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return true;
11913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
11923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
11933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the
11943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// grouped values as integers. Numbers which are not provided are set to 0.
11953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry///
11963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// \return True if the entire string was parsed (9.2), or all groups were
11973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// parsed (10.3.5extrastuff).
11983c827367444ee418f129b2c238299f49d3264554Jarkko Poyrybool Driver::GetReleaseVersion(const char *Str, unsigned &Major,
11993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                               unsigned &Minor, unsigned &Micro,
12003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                               bool &HadExtra) {
12013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  HadExtra = false;
12023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
12033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Major = Minor = Micro = 0;
12043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (*Str == '\0')
12053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return true;
12063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
12073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  char *End;
12083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Major = (unsigned) strtol(Str, &End, 10);
12093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (*Str != '\0' && *End == '\0')
12103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return true;
12113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (*End != '.')
12123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return false;
12133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
12143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Str = End+1;
12153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Minor = (unsigned) strtol(Str, &End, 10);
12163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (*Str != '\0' && *End == '\0')
12173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return true;
12183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (*End != '.')
12193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return false;
12203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
12213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Str = End+1;
12223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Micro = (unsigned) strtol(Str, &End, 10);
12233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (*Str != '\0' && *End == '\0')
12243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return true;
12253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (Str == End)
12263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return false;
12273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  HadExtra = true;
12283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return true;
12293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
12303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry