blob: c404e762b8dcc897f55ec89fed9548fcea1d0fbc [file] [log] [blame]
Daniel Dunbar544ecd12009-03-02 19:59:07 +00001//===--- Driver.cpp - Clang GCC Compatible Driver -----------------------*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Dunbar544ecd12009-03-02 19:59:07 +000010#include "clang/Driver/Driver.h"
Daniel Dunbar544ecd12009-03-02 19:59:07 +000011
Daniel Dunbar1688f1a2009-03-12 07:58:46 +000012#include "clang/Driver/Action.h"
Daniel Dunbarb2cd66b2009-03-04 20:49:20 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
15#include "clang/Driver/Compilation.h"
Daniel Dunbarc0b3e952009-03-12 08:55:43 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar4dff6a42009-03-10 23:41:59 +000017#include "clang/Driver/HostInfo.h"
Daniel Dunbare75d8342009-03-16 06:56:51 +000018#include "clang/Driver/Job.h"
Daniel Dunbaraa767372009-11-19 00:15:11 +000019#include "clang/Driver/OptTable.h"
Daniel Dunbard02cb1d2009-03-05 06:38:47 +000020#include "clang/Driver/Option.h"
Daniel Dunbarb2cd66b2009-03-04 20:49:20 +000021#include "clang/Driver/Options.h"
Daniel Dunbare75d8342009-03-16 06:56:51 +000022#include "clang/Driver/Tool.h"
23#include "clang/Driver/ToolChain.h"
Daniel Dunbar1688f1a2009-03-12 07:58:46 +000024#include "clang/Driver/Types.h"
Daniel Dunbard02cb1d2009-03-05 06:38:47 +000025
Douglas Gregor7b71e632009-04-27 22:23:34 +000026#include "clang/Basic/Version.h"
27
Daniel Dunbare5dc4822009-03-13 20:33:35 +000028#include "llvm/ADT/StringSet.h"
Daniel Dunbar2608c542009-03-18 01:38:48 +000029#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbard02cb1d2009-03-05 06:38:47 +000030#include "llvm/Support/raw_ostream.h"
Daniel Dunbar1688f1a2009-03-12 07:58:46 +000031#include "llvm/System/Path.h"
Daniel Dunbar6f668772009-03-18 21:34:08 +000032#include "llvm/System/Program.h"
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +000033
Daniel Dunbare75d8342009-03-16 06:56:51 +000034#include "InputInfo.h"
35
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +000036#include <map>
37
Daniel Dunbarb2cd66b2009-03-04 20:49:20 +000038using namespace clang::driver;
Chris Lattnerada1c912009-03-26 05:56:24 +000039using namespace clang;
Daniel Dunbarb2cd66b2009-03-04 20:49:20 +000040
Daniel Dunbarb5bcd6b2009-08-23 18:42:54 +000041// Used to set values for "production" clang, for releases.
Daniel Dunbarccc60da2009-08-23 19:41:53 +000042// #define USE_PRODUCTION_CLANG
Daniel Dunbarb5bcd6b2009-08-23 18:42:54 +000043
Daniel Dunbar4dff6a42009-03-10 23:41:59 +000044Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbarc0b3e952009-03-12 08:55:43 +000045 const char *_DefaultHostTriple,
Daniel Dunbare75d8342009-03-16 06:56:51 +000046 const char *_DefaultImageName,
Daniel Dunbar5564ba72009-09-22 22:31:13 +000047 bool IsProduction, Diagnostic &_Diags)
Daniel Dunbar26228a02009-11-18 20:19:36 +000048 : Opts(createDriverOptTable()), Diags(_Diags),
Daniel Dunbar4dff6a42009-03-10 23:41:59 +000049 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
Daniel Dunbare75d8342009-03-16 06:56:51 +000050 DefaultImageName(_DefaultImageName),
Daniel Dunbar4dff6a42009-03-10 23:41:59 +000051 Host(0),
Daniel Dunbarb39cc522009-03-17 22:47:06 +000052 CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false),
Daniel Dunbarb5bcd6b2009-08-23 18:42:54 +000053 CCCGenericGCCName("gcc"), CCCUseClang(true),
Daniel Dunbar5564ba72009-09-22 22:31:13 +000054 CCCUseClangCXX(true), CCCUseClangCPP(true), CCCUsePCH(true),
Mike Stump11289f42009-09-09 15:08:12 +000055 SuppressMissingInputWarning(false) {
Daniel Dunbar5564ba72009-09-22 22:31:13 +000056 if (IsProduction) {
57 // In a "production" build, only use clang on architectures we expect to
58 // work, and don't use clang C++.
59 //
60 // During development its more convenient to always have the driver use
61 // clang, but we don't want users to be confused when things don't work, or
62 // to file bugs for things we don't support.
63 CCCClangArchs.insert(llvm::Triple::x86);
64 CCCClangArchs.insert(llvm::Triple::x86_64);
65 CCCClangArchs.insert(llvm::Triple::arm);
66
67 CCCUseClangCXX = false;
68 }
Daniel Dunbar544ecd12009-03-02 19:59:07 +000069}
70
71Driver::~Driver() {
Daniel Dunbarb2cd66b2009-03-04 20:49:20 +000072 delete Opts;
Daniel Dunbar01601722009-03-18 01:09:40 +000073 delete Host;
Daniel Dunbar544ecd12009-03-02 19:59:07 +000074}
75
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +000076InputArgList *Driver::ParseArgStrings(const char **ArgBegin,
Daniel Dunbardac54a82009-03-25 04:13:45 +000077 const char **ArgEnd) {
Daniel Dunbar2608c542009-03-18 01:38:48 +000078 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbar52ed5fe2009-11-19 06:35:06 +000079 unsigned MissingArgIndex, MissingArgCount;
80 InputArgList *Args = getOpts().ParseArgs(ArgBegin, ArgEnd,
81 MissingArgIndex, MissingArgCount);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +000082
Daniel Dunbar52ed5fe2009-11-19 06:35:06 +000083 // Check for missing argument error.
84 if (MissingArgCount)
85 Diag(clang::diag::err_drv_missing_argument)
86 << Args->getArgString(MissingArgIndex) << MissingArgCount;
Daniel Dunbar65229332009-03-13 11:38:42 +000087
Daniel Dunbar52ed5fe2009-11-19 06:35:06 +000088 // Check for unsupported options.
89 for (ArgList::const_iterator it = Args->begin(), ie = Args->end();
90 it != ie; ++it) {
91 Arg *A = *it;
Daniel Dunbard8500f32009-03-22 23:26:43 +000092 if (A->getOption().isUnsupported()) {
93 Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
94 continue;
95 }
Daniel Dunbard02cb1d2009-03-05 06:38:47 +000096 }
97
98 return Args;
99}
100
Daniel Dunbar544ecd12009-03-02 19:59:07 +0000101Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar2608c542009-03-18 01:38:48 +0000102 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
103
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000104 // FIXME: Handle environment options which effect driver behavior, somewhere
105 // (client?). GCC_EXEC_PREFIX, COMPILER_PATH, LIBRARY_PATH, LPATH,
106 // CC_PRINT_OPTIONS.
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000107
108 // FIXME: What are we going to do with -V and -b?
109
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000110 // FIXME: This stuff needs to go into the Compilation, not the driver.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000111 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000112
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000113 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbar4dff6a42009-03-10 23:41:59 +0000114 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000115
Daniel Dunbaracd69572009-12-04 21:55:23 +0000116 InputArgList *Args = ParseArgStrings(Start, End);
117
118 // Extract -ccc args.
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000119 //
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000120 // FIXME: We need to figure out where this behavior should live. Most of it
121 // should be outside in the client; the parts that aren't should have proper
122 // options, either by introducing new ones or by overloading gcc ones like -V
123 // or -b.
Daniel Dunbaracd69572009-12-04 21:55:23 +0000124 CCCPrintOptions = Args->hasArg(options::OPT_ccc_print_options);
125 CCCPrintActions = Args->hasArg(options::OPT_ccc_print_phases);
126 CCCPrintBindings = Args->hasArg(options::OPT_ccc_print_bindings);
Daniel Dunbar1b52d8c2009-12-05 00:13:59 +0000127 CCCIsCXX = Args->hasArg(options::OPT_ccc_cxx) || CCCIsCXX;
Daniel Dunbaracd69572009-12-04 21:55:23 +0000128 CCCEcho = Args->hasArg(options::OPT_ccc_echo);
129 if (const Arg *A = Args->getLastArg(options::OPT_ccc_gcc_name))
130 CCCGenericGCCName = A->getValue(*Args);
131 CCCUseClangCXX = Args->hasFlag(options::OPT_ccc_clang_cxx,
Daniel Dunbar1b52d8c2009-12-05 00:13:59 +0000132 options::OPT_ccc_no_clang_cxx,
133 CCCUseClangCXX);
Daniel Dunbaracd69572009-12-04 21:55:23 +0000134 CCCUsePCH = Args->hasFlag(options::OPT_ccc_pch_is_pch,
135 options::OPT_ccc_pch_is_pth);
136 CCCUseClang = !Args->hasArg(options::OPT_ccc_no_clang);
137 CCCUseClangCPP = !Args->hasArg(options::OPT_ccc_no_clang_cpp);
138 if (const Arg *A = Args->getLastArg(options::OPT_ccc_clang_archs)) {
139 llvm::StringRef Cur = A->getValue(*Args);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000140
Daniel Dunbaracd69572009-12-04 21:55:23 +0000141 CCCClangArchs.clear();
142 while (!Cur.empty()) {
143 std::pair<llvm::StringRef, llvm::StringRef> Split = Cur.split(',');
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000144
Daniel Dunbaracd69572009-12-04 21:55:23 +0000145 if (!Split.first.empty()) {
146 llvm::Triple::ArchType Arch =
147 llvm::Triple(Split.first, "", "").getArch();
Daniel Dunbar7803c952009-04-01 23:34:41 +0000148
Daniel Dunbaracd69572009-12-04 21:55:23 +0000149 if (Arch == llvm::Triple::UnknownArch) {
150 Diag(clang::diag::err_drv_invalid_arch_name) << Arch;
151 continue;
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000152 }
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000153
Daniel Dunbaracd69572009-12-04 21:55:23 +0000154 CCCClangArchs.insert(Arch);
Daniel Dunbar953b8d12009-09-08 23:36:55 +0000155 }
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000156
Daniel Dunbaracd69572009-12-04 21:55:23 +0000157 Cur = Split.second;
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000158 }
159 }
Daniel Dunbaracd69572009-12-04 21:55:23 +0000160 if (const Arg *A = Args->getLastArg(options::OPT_ccc_host_triple))
161 HostTriple = A->getValue(*Args);
162 if (const Arg *A = Args->getLastArg(options::OPT_ccc_install_dir))
163 Dir = A->getValue(*Args);
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000164
Daniel Dunbar52e0c702009-03-17 20:45:45 +0000165 Host = GetHostInfo(HostTriple);
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000166
Daniel Dunbar3ce436d2009-03-16 06:42:30 +0000167 // The compilation takes ownership of Args.
Daniel Dunbar1ef3f2a2009-09-08 23:37:19 +0000168 Compilation *C = new Compilation(*this, *Host->CreateToolChain(*Args), Args);
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000169
170 // FIXME: This behavior shouldn't be here.
171 if (CCCPrintOptions) {
172 PrintOptions(C->getArgs());
173 return C;
174 }
175
176 if (!HandleImmediateArgs(*C))
177 return C;
178
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000179 // Construct the list of abstract actions to perform for this compilation. We
180 // avoid passing a Compilation here simply to enforce the abstraction that
181 // pipelining is not host or toolchain dependent (other than the driver driver
182 // test).
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000183 if (Host->useDriverDriver())
184 BuildUniversalActions(C->getArgs(), C->getActions());
185 else
186 BuildActions(C->getArgs(), C->getActions());
187
188 if (CCCPrintActions) {
Daniel Dunbareb843be2009-03-18 03:13:20 +0000189 PrintActions(*C);
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000190 return C;
191 }
192
193 BuildJobs(*C);
Daniel Dunbaradc91e62009-03-15 01:38:15 +0000194
195 return C;
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000196}
197
Daniel Dunbar38bfda62009-07-01 20:03:04 +0000198int Driver::ExecuteCompilation(const Compilation &C) const {
199 // Just print if -### was present.
200 if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
201 C.PrintJob(llvm::errs(), C.getJobs(), "\n", true);
202 return 0;
203 }
204
205 // If there were errors building the compilation, quit now.
206 if (getDiags().getNumErrors())
207 return 1;
208
209 const Command *FailingCommand = 0;
210 int Res = C.ExecuteJob(C.getJobs(), FailingCommand);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000211
Daniel Dunbar38bfda62009-07-01 20:03:04 +0000212 // Remove temp files.
213 C.CleanupFileList(C.getTempFiles());
214
215 // If the compilation failed, remove result files as well.
216 if (Res != 0 && !C.getArgs().hasArg(options::OPT_save_temps))
217 C.CleanupFileList(C.getResultFiles(), true);
218
219 // Print extra information about abnormal failures, if possible.
220 if (Res) {
221 // This is ad-hoc, but we don't want to be excessively noisy. If the result
222 // status was 1, assume the command failed normally. In particular, if it
223 // was the compiler then assume it gave a reasonable error code. Failures in
224 // other tools are less common, and they generally have worse diagnostics,
225 // so always print the diagnostic there.
226 const Action &Source = FailingCommand->getSource();
227 bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) ||
228 isa<PrecompileJobAction>(Source) ||
229 isa<AnalyzeJobAction>(Source) ||
230 isa<CompileJobAction>(Source));
231
232 if (!IsFriendlyTool || Res != 1) {
233 // FIXME: See FIXME above regarding result code interpretation.
234 if (Res < 0)
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000235 Diag(clang::diag::err_drv_command_signalled)
Daniel Dunbar38bfda62009-07-01 20:03:04 +0000236 << Source.getClassName() << -Res;
237 else
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000238 Diag(clang::diag::err_drv_command_failed)
Daniel Dunbar38bfda62009-07-01 20:03:04 +0000239 << Source.getClassName() << Res;
240 }
241 }
242
243 return Res;
244}
245
Daniel Dunbar446f6842009-03-12 18:24:49 +0000246void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000247 unsigned i = 0;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000248 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000249 it != ie; ++it, ++i) {
250 Arg *A = *it;
251 llvm::errs() << "Option " << i << " - "
252 << "Name: \"" << A->getOption().getName() << "\", "
253 << "Values: {";
254 for (unsigned j = 0; j < A->getNumValues(); ++j) {
255 if (j)
256 llvm::errs() << ", ";
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000257 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000258 }
259 llvm::errs() << "}\n";
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000260 }
Daniel Dunbar544ecd12009-03-02 19:59:07 +0000261}
Daniel Dunbar4dff6a42009-03-10 23:41:59 +0000262
Daniel Dunbar65b99522009-12-03 07:01:38 +0000263// FIXME: Move -ccc options to real options in the .td file (or eliminate), and
264// then move to using OptTable::PrintHelp.
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000265void Driver::PrintHelp(bool ShowHidden) const {
Daniel Dunbaracd69572009-12-04 21:55:23 +0000266 getOpts().PrintHelp(llvm::outs(), Name.c_str(),
267 "clang \"gcc-compatible\" driver", ShowHidden);
Daniel Dunbar7c925282009-03-31 21:38:17 +0000268}
269
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000270void Driver::PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000271 // FIXME: The following handlers should use a callback mechanism, we don't
272 // know what the client would like to do.
Mike Stump727170d2009-10-09 17:31:54 +0000273#ifdef CLANG_VENDOR
274 OS << CLANG_VENDOR;
275#endif
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000276 OS << "clang version " CLANG_VERSION_STRING " ("
Douglas Gregor1b7035d2009-10-05 20:33:49 +0000277 << getClangSubversionPath();
278 if (unsigned Revision = getClangSubversionRevision())
279 OS << " " << Revision;
280 OS << ")" << '\n';
Daniel Dunbarb10248802009-03-26 16:09:13 +0000281
282 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000283 OS << "Target: " << TC.getTripleString() << '\n';
Daniel Dunbar10978e42009-06-16 23:32:58 +0000284
285 // Print the threading model.
286 //
287 // FIXME: Implement correctly.
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000288 OS << "Thread model: " << "posix" << '\n';
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000289}
290
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000291bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000292 // The order these options are handled in in gcc is all over the place, but we
293 // don't expect inconsistencies w.r.t. that to matter in practice.
Daniel Dunbar7c925282009-03-31 21:38:17 +0000294
Daniel Dunbara9bbcfa2009-04-04 05:17:38 +0000295 if (C.getArgs().hasArg(options::OPT_dumpversion)) {
Douglas Gregor7b71e632009-04-27 22:23:34 +0000296 llvm::outs() << CLANG_VERSION_STRING "\n";
Daniel Dunbara9bbcfa2009-04-04 05:17:38 +0000297 return false;
298 }
299
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000300 if (C.getArgs().hasArg(options::OPT__help) ||
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000301 C.getArgs().hasArg(options::OPT__help_hidden)) {
302 PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden));
Daniel Dunbar7c925282009-03-31 21:38:17 +0000303 return false;
304 }
305
Daniel Dunbarb0006ae2009-04-02 15:05:41 +0000306 if (C.getArgs().hasArg(options::OPT__version)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000307 // Follow gcc behavior and use stdout for --version and stderr for -v.
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000308 PrintVersion(C, llvm::outs());
Daniel Dunbarb0006ae2009-04-02 15:05:41 +0000309 return false;
310 }
311
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000312 if (C.getArgs().hasArg(options::OPT_v) ||
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000313 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000314 PrintVersion(C, llvm::errs());
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000315 SuppressMissingInputWarning = true;
316 }
317
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000318 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbard972e222009-03-20 04:37:21 +0000319 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
320 llvm::outs() << "programs: =";
321 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
322 ie = TC.getProgramPaths().end(); it != ie; ++it) {
323 if (it != TC.getProgramPaths().begin())
324 llvm::outs() << ':';
325 llvm::outs() << *it;
326 }
327 llvm::outs() << "\n";
328 llvm::outs() << "libraries: =";
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000329 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
Daniel Dunbard972e222009-03-20 04:37:21 +0000330 ie = TC.getFilePaths().end(); it != ie; ++it) {
331 if (it != TC.getFilePaths().begin())
332 llvm::outs() << ':';
333 llvm::outs() << *it;
334 }
335 llvm::outs() << "\n";
Daniel Dunbar7c925282009-03-31 21:38:17 +0000336 return false;
Daniel Dunbard972e222009-03-20 04:37:21 +0000337 }
338
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000339 // FIXME: The following handlers should use a callback mechanism, we don't
340 // know what the client would like to do.
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000341 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
Daniel Dunbar1ce81532009-09-09 22:33:00 +0000342 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC) << "\n";
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000343 return false;
344 }
345
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000346 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
Daniel Dunbar1ce81532009-09-09 22:33:00 +0000347 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC) << "\n";
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000348 return false;
349 }
350
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000351 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbar1ce81532009-09-09 22:33:00 +0000352 llvm::outs() << GetFilePath("libgcc.a", TC) << "\n";
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000353 return false;
354 }
355
Daniel Dunbar1b3ec3a2009-06-16 23:25:22 +0000356 if (C.getArgs().hasArg(options::OPT_print_multi_lib)) {
357 // FIXME: We need tool chain support for this.
358 llvm::outs() << ".;\n";
359
360 switch (C.getDefaultToolChain().getTriple().getArch()) {
361 default:
362 break;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000363
Daniel Dunbar1b3ec3a2009-06-16 23:25:22 +0000364 case llvm::Triple::x86_64:
365 llvm::outs() << "x86_64;@m64" << "\n";
366 break;
367
368 case llvm::Triple::ppc64:
369 llvm::outs() << "ppc64;@m64" << "\n";
370 break;
371 }
372 return false;
373 }
374
375 // FIXME: What is the difference between print-multi-directory and
376 // print-multi-os-directory?
377 if (C.getArgs().hasArg(options::OPT_print_multi_directory) ||
378 C.getArgs().hasArg(options::OPT_print_multi_os_directory)) {
379 switch (C.getDefaultToolChain().getTriple().getArch()) {
380 default:
381 case llvm::Triple::x86:
382 case llvm::Triple::ppc:
383 llvm::outs() << "." << "\n";
384 break;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000385
Daniel Dunbar1b3ec3a2009-06-16 23:25:22 +0000386 case llvm::Triple::x86_64:
387 llvm::outs() << "x86_64" << "\n";
388 break;
389
390 case llvm::Triple::ppc64:
391 llvm::outs() << "ppc64" << "\n";
392 break;
393 }
394 return false;
395 }
396
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000397 return true;
398}
399
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000400static unsigned PrintActions1(const Compilation &C, Action *A,
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000401 std::map<Action*, unsigned> &Ids) {
402 if (Ids.count(A))
403 return Ids[A];
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000404
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000405 std::string str;
406 llvm::raw_string_ostream os(str);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000407
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000408 os << Action::getClassName(A->getKind()) << ", ";
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000409 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbareb843be2009-03-18 03:13:20 +0000410 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000411 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000412 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
Daniel Dunbareb843be2009-03-18 03:13:20 +0000413 C.getDefaultToolChain().getArchName()) << '"'
414 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000415 } else {
416 os << "{";
417 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbareb843be2009-03-18 03:13:20 +0000418 os << PrintActions1(C, *it, Ids);
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000419 ++it;
420 if (it != ie)
421 os << ", ";
422 }
423 os << "}";
424 }
425
426 unsigned Id = Ids.size();
427 Ids[A] = Id;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000428 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000429 << types::getTypeName(A->getType()) << "\n";
430
431 return Id;
432}
433
Daniel Dunbareb843be2009-03-18 03:13:20 +0000434void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000435 std::map<Action*, unsigned> Ids;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000436 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbareb843be2009-03-18 03:13:20 +0000437 ie = C.getActions().end(); it != ie; ++it)
438 PrintActions1(C, *it, Ids);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000439}
440
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000441void Driver::BuildUniversalActions(const ArgList &Args,
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000442 ActionList &Actions) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000443 llvm::PrettyStackTraceString CrashInfo("Building universal build actions");
444 // Collect the list of architectures. Duplicates are allowed, but should only
445 // be handled once (in the order seen).
Daniel Dunbare5dc4822009-03-13 20:33:35 +0000446 llvm::StringSet<> ArchNames;
447 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000448 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbarf479c122009-03-12 18:40:18 +0000449 it != ie; ++it) {
450 Arg *A = *it;
451
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000452 if (A->getOption().matches(options::OPT_arch)) {
Daniel Dunbar9c3f7c42009-09-08 23:37:30 +0000453 // Validate the option here; we don't save the type here because its
454 // particular spelling may participate in other driver choices.
455 llvm::Triple::ArchType Arch =
456 llvm::Triple::getArchTypeForDarwinArchName(A->getValue(Args));
457 if (Arch == llvm::Triple::UnknownArch) {
458 Diag(clang::diag::err_drv_invalid_arch_name)
459 << A->getAsString(Args);
460 continue;
461 }
462
Daniel Dunbar2da02722009-03-19 07:55:12 +0000463 A->claim();
Daniel Dunbar7b574042009-09-08 23:37:02 +0000464 if (ArchNames.insert(A->getValue(Args)))
465 Archs.push_back(A->getValue(Args));
Daniel Dunbarf479c122009-03-12 18:40:18 +0000466 }
467 }
468
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000469 // When there is no explicit arch for this platform, make sure we still bind
470 // the architecture (to the default) so that -Xarch_ is handled correctly.
Daniel Dunbareb843be2009-03-18 03:13:20 +0000471 if (!Archs.size())
472 Archs.push_back(0);
Daniel Dunbarf479c122009-03-12 18:40:18 +0000473
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000474 // FIXME: We killed off some others but these aren't yet detected in a
475 // functional manner. If we added information to jobs about which "auxiliary"
476 // files they wrote then we could detect the conflict these cause downstream.
Daniel Dunbarf479c122009-03-12 18:40:18 +0000477 if (Archs.size() > 1) {
478 // No recovery needed, the point of this is just to prevent
479 // overwriting the same files.
Daniel Dunbarf479c122009-03-12 18:40:18 +0000480 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000481 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar0e759942009-03-20 06:14:23 +0000482 << A->getAsString(Args);
Daniel Dunbarf479c122009-03-12 18:40:18 +0000483 }
484
485 ActionList SingleActions;
486 BuildActions(Args, SingleActions);
487
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000488 // Add in arch binding and lipo (if necessary) for every top level action.
Daniel Dunbarf479c122009-03-12 18:40:18 +0000489 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
490 Action *Act = SingleActions[i];
491
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000492 // Make sure we can lipo this kind of output. If not (and it is an actual
493 // output) then we disallow, since we can't create an output file with the
494 // right name without overwriting it. We could remove this oddity by just
495 // changing the output names to include the arch, which would also fix
Daniel Dunbarf479c122009-03-12 18:40:18 +0000496 // -save-temps. Compatibility wins for now.
497
Daniel Dunbare2ca3bd2009-03-13 17:46:02 +0000498 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbarf479c122009-03-12 18:40:18 +0000499 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
500 << types::getTypeName(Act->getType());
501
502 ActionList Inputs;
Daniel Dunbar2da02722009-03-19 07:55:12 +0000503 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbare5dc4822009-03-13 20:33:35 +0000504 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbarf479c122009-03-12 18:40:18 +0000505
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000506 // Lipo if necessary, we do it this way because we need to set the arch flag
507 // so that -Xarch_ gets overwritten.
Daniel Dunbarf479c122009-03-12 18:40:18 +0000508 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
509 Actions.append(Inputs.begin(), Inputs.end());
510 else
511 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
512 }
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000513}
514
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000515void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +0000516 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000517 // Start by constructing the list of inputs and their types.
518
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000519 // Track the current user specified (-x) input. We also explicitly track the
520 // argument used to set the type; we only want to claim the type when we
521 // actually use it, so we warn about unused -x arguments.
Daniel Dunbarc5a5ac52009-03-13 17:57:10 +0000522 types::ID InputType = types::TY_Nothing;
523 Arg *InputTypeArg = 0;
524
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000525 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000526 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000527 it != ie; ++it) {
528 Arg *A = *it;
529
530 if (isa<InputOption>(A->getOption())) {
531 const char *Value = A->getValue(Args);
532 types::ID Ty = types::TY_INVALID;
533
534 // Infer the input type if necessary.
Daniel Dunbarc5a5ac52009-03-13 17:57:10 +0000535 if (InputType == types::TY_Nothing) {
536 // If there was an explicit arg for this, claim it.
537 if (InputTypeArg)
538 InputTypeArg->claim();
539
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000540 // stdin must be handled specially.
541 if (memcmp(Value, "-", 2) == 0) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000542 // If running with -E, treat as a C input (this changes the builtin
543 // macros, for example). This may be overridden by -ObjC below.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000544 //
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000545 // Otherwise emit an error but still use a valid type to avoid
546 // spurious errors (e.g., no inputs).
Daniel Dunbarfffd1812009-11-19 04:00:53 +0000547 if (!Args.hasArgNoClaim(options::OPT_E))
Daniel Dunbar5cd1e412009-03-12 09:13:48 +0000548 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000549 Ty = types::TY_C;
550 } else {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000551 // Otherwise lookup by extension, and fallback to ObjectType if not
552 // found. We use a host hook here because Darwin at least has its own
553 // idea of what .s is.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000554 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbarea9f0322009-03-20 23:39:23 +0000555 Ty = Host->lookupTypeForExtension(Ext + 1);
556
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000557 if (Ty == types::TY_INVALID)
558 Ty = types::TY_Object;
559 }
560
Daniel Dunbar82b22102009-05-18 21:47:54 +0000561 // -ObjC and -ObjC++ override the default language, but only for "source
562 // files". We just treat everything that isn't a linker input as a
563 // source file.
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000564 //
Daniel Dunbar82b22102009-05-18 21:47:54 +0000565 // FIXME: Clean this up if we move the phase sequence into the type.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000566 if (Ty != types::TY_Object) {
567 if (Args.hasArg(options::OPT_ObjC))
568 Ty = types::TY_ObjC;
569 else if (Args.hasArg(options::OPT_ObjCXX))
570 Ty = types::TY_ObjCXX;
571 }
572 } else {
573 assert(InputTypeArg && "InputType set w/o InputTypeArg");
574 InputTypeArg->claim();
575 Ty = InputType;
576 }
577
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000578 // Check that the file exists. It isn't clear this is worth doing, since
579 // the tool presumably does this anyway, and this just adds an extra stat
580 // to the equation, but this is gcc compatible.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000581 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbar5cd1e412009-03-12 09:13:48 +0000582 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000583 else
584 Inputs.push_back(std::make_pair(Ty, A));
585
586 } else if (A->getOption().isLinkerInput()) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000587 // Just treat as object type, we could make a special type for this if
588 // necessary.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000589 Inputs.push_back(std::make_pair(types::TY_Object, A));
590
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000591 } else if (A->getOption().matches(options::OPT_x)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000592 InputTypeArg = A;
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000593 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
594
595 // Follow gcc behavior and treat as linker input for invalid -x
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000596 // options. Its not clear why we shouldn't just revert to unknown; but
597 // this isn't very important, we might as well be bug comatible.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000598 if (!InputType) {
Daniel Dunbar5cd1e412009-03-12 09:13:48 +0000599 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000600 InputType = types::TY_Object;
601 }
602 }
603 }
604
Daniel Dunbar34c41872009-03-13 00:17:48 +0000605 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000606 Diag(clang::diag::err_drv_no_input_files);
607 return;
608 }
609
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000610 // Determine which compilation mode we are in. We look for options which
611 // affect the phase, starting with the earliest phases, and record which
612 // option we used to determine the final phase.
Daniel Dunbar65229332009-03-13 11:38:42 +0000613 Arg *FinalPhaseArg = 0;
614 phases::ID FinalPhase;
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000615
616 // -{E,M,MM} only run the preprocessor.
Daniel Dunbar65229332009-03-13 11:38:42 +0000617 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
618 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
619 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
620 FinalPhase = phases::Preprocess;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000621
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +0000622 // -{fsyntax-only,-analyze,emit-ast,S} only run up to the compiler.
Daniel Dunbarae0e55e2009-03-15 00:48:16 +0000623 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
Daniel Dunbar5e051f92009-05-06 02:12:32 +0000624 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze,
625 options::OPT__analyze_auto)) ||
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +0000626 (FinalPhaseArg = Args.getLastArg(options::OPT_emit_ast)) ||
Daniel Dunbar65229332009-03-13 11:38:42 +0000627 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
628 FinalPhase = phases::Compile;
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000629
630 // -c only runs up to the assembler.
Daniel Dunbar65229332009-03-13 11:38:42 +0000631 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
632 FinalPhase = phases::Assemble;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000633
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000634 // Otherwise do everything.
635 } else
Daniel Dunbar65229332009-03-13 11:38:42 +0000636 FinalPhase = phases::Link;
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000637
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000638 // Reject -Z* at the top level, these options should never have been exposed
639 // by gcc.
Daniel Dunbardd765242009-03-26 16:12:09 +0000640 if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
Daniel Dunbar0e759942009-03-20 06:14:23 +0000641 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000642
Daniel Dunbar65229332009-03-13 11:38:42 +0000643 // Construct the actions to perform.
644 ActionList LinkerInputs;
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000645 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbar65229332009-03-13 11:38:42 +0000646 types::ID InputType = Inputs[i].first;
647 const Arg *InputArg = Inputs[i].second;
648
649 unsigned NumSteps = types::getNumCompilationPhases(InputType);
650 assert(NumSteps && "Invalid number of steps!");
651
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000652 // If the first step comes after the final phase we are doing as part of
653 // this compilation, warn the user about it.
Daniel Dunbar65229332009-03-13 11:38:42 +0000654 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
655 if (InitialPhase > FinalPhase) {
Daniel Dunbaradc5c7c2009-03-19 07:57:08 +0000656 // Claim here to avoid the more general unused warning.
657 InputArg->claim();
Daniel Dunbar07806ca2009-09-17 04:13:26 +0000658
659 // Special case '-E' warning on a previously preprocessed file to make
660 // more sense.
661 if (InitialPhase == phases::Compile && FinalPhase == phases::Preprocess &&
662 getPreprocessedType(InputType) == types::TY_INVALID)
663 Diag(clang::diag::warn_drv_preprocessed_input_file_unused)
664 << InputArg->getAsString(Args)
665 << FinalPhaseArg->getOption().getName();
666 else
667 Diag(clang::diag::warn_drv_input_file_unused)
668 << InputArg->getAsString(Args)
669 << getPhaseName(InitialPhase)
670 << FinalPhaseArg->getOption().getName();
Daniel Dunbar65229332009-03-13 11:38:42 +0000671 continue;
672 }
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000673
Daniel Dunbar65229332009-03-13 11:38:42 +0000674 // Build the pipeline for this file.
675 Action *Current = new InputAction(*InputArg, InputType);
676 for (unsigned i = 0; i != NumSteps; ++i) {
677 phases::ID Phase = types::getCompilationPhase(InputType, i);
678
679 // We are done if this step is past what the user requested.
680 if (Phase > FinalPhase)
681 break;
682
683 // Queue linker inputs.
684 if (Phase == phases::Link) {
685 assert(i + 1 == NumSteps && "linking must be final compilation step.");
686 LinkerInputs.push_back(Current);
687 Current = 0;
688 break;
689 }
690
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000691 // Some types skip the assembler phase (e.g., llvm-bc), but we can't
692 // encode this in the steps because the intermediate type depends on
693 // arguments. Just special case here.
Daniel Dunbar13864952009-03-24 20:17:30 +0000694 if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
695 continue;
696
Daniel Dunbar65229332009-03-13 11:38:42 +0000697 // Otherwise construct the appropriate action.
698 Current = ConstructPhaseAction(Args, Phase, Current);
699 if (Current->getType() == types::TY_Nothing)
700 break;
701 }
702
703 // If we ended with something, add to the output list.
704 if (Current)
705 Actions.push_back(Current);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000706 }
Daniel Dunbar65229332009-03-13 11:38:42 +0000707
708 // Add a link action if necessary.
709 if (!LinkerInputs.empty())
710 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
711}
712
713Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
714 Action *Input) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +0000715 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbar65229332009-03-13 11:38:42 +0000716 // Build the appropriate action.
717 switch (Phase) {
718 case phases::Link: assert(0 && "link action invalid here.");
719 case phases::Preprocess: {
Daniel Dunbard67a3222009-03-30 06:36:42 +0000720 types::ID OutputTy;
721 // -{M, MM} alter the output type.
722 if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) {
723 OutputTy = types::TY_Dependencies;
724 } else {
725 OutputTy = types::getPreprocessedType(Input->getType());
726 assert(OutputTy != types::TY_INVALID &&
727 "Cannot preprocess this input type!");
728 }
Daniel Dunbar65229332009-03-13 11:38:42 +0000729 return new PreprocessJobAction(Input, OutputTy);
730 }
731 case phases::Precompile:
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000732 return new PrecompileJobAction(Input, types::TY_PCH);
Daniel Dunbar65229332009-03-13 11:38:42 +0000733 case phases::Compile: {
734 if (Args.hasArg(options::OPT_fsyntax_only)) {
735 return new CompileJobAction(Input, types::TY_Nothing);
Daniel Dunbar5e051f92009-05-06 02:12:32 +0000736 } else if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) {
Daniel Dunbar65229332009-03-13 11:38:42 +0000737 return new AnalyzeJobAction(Input, types::TY_Plist);
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +0000738 } else if (Args.hasArg(options::OPT_emit_ast)) {
739 return new CompileJobAction(Input, types::TY_AST);
Daniel Dunbar13864952009-03-24 20:17:30 +0000740 } else if (Args.hasArg(options::OPT_emit_llvm) ||
741 Args.hasArg(options::OPT_flto) ||
742 Args.hasArg(options::OPT_O4)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000743 types::ID Output =
Daniel Dunbar65229332009-03-13 11:38:42 +0000744 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
745 return new CompileJobAction(Input, Output);
746 } else {
747 return new CompileJobAction(Input, types::TY_PP_Asm);
748 }
749 }
750 case phases::Assemble:
751 return new AssembleJobAction(Input, types::TY_Object);
752 }
753
754 assert(0 && "invalid phase in ConstructPhaseAction");
755 return 0;
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000756}
757
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000758void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +0000759 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbare75d8342009-03-16 06:56:51 +0000760 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
761 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +0000762
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000763 // FIXME: Pipes are forcibly disabled until we support executing them.
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +0000764 if (!CCCPrintBindings)
765 UsePipes = false;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000766
Daniel Dunbare75d8342009-03-16 06:56:51 +0000767 // -save-temps inhibits pipes.
768 if (SaveTemps && UsePipes) {
769 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
770 UsePipes = true;
771 }
772
773 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
774
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000775 // It is an error to provide a -o option if we are making multiple output
776 // files.
Daniel Dunbare75d8342009-03-16 06:56:51 +0000777 if (FinalOutput) {
778 unsigned NumOutputs = 0;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000779 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000780 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbare75d8342009-03-16 06:56:51 +0000781 if ((*it)->getType() != types::TY_Nothing)
782 ++NumOutputs;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000783
Daniel Dunbare75d8342009-03-16 06:56:51 +0000784 if (NumOutputs > 1) {
785 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
786 FinalOutput = 0;
787 }
788 }
789
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000790 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000791 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbare75d8342009-03-16 06:56:51 +0000792 Action *A = *it;
793
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000794 // If we are linking an image for multiple archs then the linker wants
795 // -arch_multiple and -final_output <final image name>. Unfortunately, this
796 // doesn't fit in cleanly because we have to pass this information down.
Daniel Dunbare75d8342009-03-16 06:56:51 +0000797 //
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000798 // FIXME: This is a hack; find a cleaner way to integrate this into the
799 // process.
Daniel Dunbare75d8342009-03-16 06:56:51 +0000800 const char *LinkingOutput = 0;
Daniel Dunbardd765242009-03-26 16:12:09 +0000801 if (isa<LipoJobAction>(A)) {
Daniel Dunbare75d8342009-03-16 06:56:51 +0000802 if (FinalOutput)
803 LinkingOutput = FinalOutput->getValue(C.getArgs());
804 else
805 LinkingOutput = DefaultImageName.c_str();
806 }
807
808 InputInfo II;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000809 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +0000810 /*BoundArch*/0,
Daniel Dunbare75d8342009-03-16 06:56:51 +0000811 /*CanAcceptPipe*/ true,
812 /*AtTopLevel*/ true,
813 /*LinkingOutput*/ LinkingOutput,
814 II);
815 }
Daniel Dunbar3ce436d2009-03-16 06:42:30 +0000816
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000817 // If the user passed -Qunused-arguments or there were errors, don't warn
818 // about any unused arguments.
819 if (Diags.getNumErrors() ||
Daniel Dunbara3cfbe32009-04-07 19:04:18 +0000820 C.getArgs().hasArg(options::OPT_Qunused_arguments))
Daniel Dunbard175d972009-03-18 18:03:46 +0000821 return;
822
Daniel Dunbar58399ae2009-03-29 22:24:54 +0000823 // Claim -### here.
824 (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000825
Daniel Dunbar3ce436d2009-03-16 06:42:30 +0000826 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
827 it != ie; ++it) {
828 Arg *A = *it;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000829
Daniel Dunbar3ce436d2009-03-16 06:42:30 +0000830 // FIXME: It would be nice to be able to send the argument to the
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000831 // Diagnostic, so that extra values, position, and so on could be printed.
Daniel Dunbar90dd6f42009-04-04 00:52:26 +0000832 if (!A->isClaimed()) {
Daniel Dunbara3cfbe32009-04-07 19:04:18 +0000833 if (A->getOption().hasNoArgumentUnused())
834 continue;
835
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000836 // Suppress the warning automatically if this is just a flag, and it is an
837 // instance of an argument we already claimed.
Daniel Dunbar90dd6f42009-04-04 00:52:26 +0000838 const Option &Opt = A->getOption();
839 if (isa<FlagOption>(Opt)) {
840 bool DuplicateClaimed = false;
841
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000842 for (arg_iterator it = C.getArgs().filtered_begin(&Opt),
843 ie = C.getArgs().filtered_end(); it != ie; ++it) {
844 if ((*it)->isClaimed()) {
Daniel Dunbar90dd6f42009-04-04 00:52:26 +0000845 DuplicateClaimed = true;
846 break;
847 }
848 }
849
850 if (DuplicateClaimed)
851 continue;
852 }
853
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000854 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar0e759942009-03-20 06:14:23 +0000855 << A->getAsString(C.getArgs());
Daniel Dunbar90dd6f42009-04-04 00:52:26 +0000856 }
Daniel Dunbar3ce436d2009-03-16 06:42:30 +0000857 }
Daniel Dunbar95e6b192009-03-13 22:12:33 +0000858}
859
Daniel Dunbare75d8342009-03-16 06:56:51 +0000860void Driver::BuildJobsForAction(Compilation &C,
861 const Action *A,
862 const ToolChain *TC,
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +0000863 const char *BoundArch,
Daniel Dunbare75d8342009-03-16 06:56:51 +0000864 bool CanAcceptPipe,
865 bool AtTopLevel,
866 const char *LinkingOutput,
867 InputInfo &Result) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000868 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +0000869
870 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000871 // FIXME: Pipes are forcibly disabled until we support executing them.
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +0000872 if (!CCCPrintBindings)
873 UsePipes = false;
874
Daniel Dunbare75d8342009-03-16 06:56:51 +0000875 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000876 // FIXME: It would be nice to not claim this here; maybe the old scheme of
877 // just using Args was better?
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +0000878 const Arg &Input = IA->getInputArg();
879 Input.claim();
880 if (isa<PositionalArg>(Input)) {
881 const char *Name = Input.getValue(C.getArgs());
882 Result = InputInfo(Name, A->getType(), Name);
883 } else
884 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbare75d8342009-03-16 06:56:51 +0000885 return;
886 }
887
888 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar1ef3f2a2009-09-08 23:37:19 +0000889 const ToolChain *TC = &C.getDefaultToolChain();
890
Daniel Dunbar51c7f972009-05-22 02:53:45 +0000891 std::string Arch;
Daniel Dunbar1ef3f2a2009-09-08 23:37:19 +0000892 if (BAA->getArchName())
893 TC = Host->CreateToolChain(C.getArgs(), BAA->getArchName());
894
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +0000895 BuildJobsForAction(C, *BAA->begin(), TC, BAA->getArchName(),
896 CanAcceptPipe, AtTopLevel, LinkingOutput, Result);
Daniel Dunbare75d8342009-03-16 06:56:51 +0000897 return;
898 }
899
900 const JobAction *JA = cast<JobAction>(A);
901 const Tool &T = TC->SelectTool(C, *JA);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000902
903 // See if we should use an integrated preprocessor. We do so when we have
904 // exactly one input, since this is the only use case we care about
905 // (irrelevant since we don't support combine yet).
Daniel Dunbare75d8342009-03-16 06:56:51 +0000906 bool UseIntegratedCPP = false;
907 const ActionList *Inputs = &A->getInputs();
908 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
909 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
910 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
911 !C.getArgs().hasArg(options::OPT_save_temps) &&
912 T.hasIntegratedCPP()) {
913 UseIntegratedCPP = true;
914 Inputs = &(*Inputs)[0]->getInputs();
915 }
916 }
917
918 // Only use pipes when there is exactly one input.
919 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar1a093d22009-03-18 06:00:36 +0000920 InputInfoList InputInfos;
Daniel Dunbare75d8342009-03-16 06:56:51 +0000921 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
922 it != ie; ++it) {
923 InputInfo II;
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +0000924 BuildJobsForAction(C, *it, TC, BoundArch, TryToUsePipeInput,
925 /*AtTopLevel*/false, LinkingOutput, II);
Daniel Dunbare75d8342009-03-16 06:56:51 +0000926 InputInfos.push_back(II);
927 }
928
929 // Determine if we should output to a pipe.
930 bool OutputToPipe = false;
931 if (CanAcceptPipe && T.canPipeOutput()) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000932 // Some actions default to writing to a pipe if they are the top level phase
933 // and there was no user override.
Daniel Dunbare75d8342009-03-16 06:56:51 +0000934 //
935 // FIXME: Is there a better way to handle this?
936 if (AtTopLevel) {
937 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
938 OutputToPipe = true;
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +0000939 } else if (UsePipes)
Daniel Dunbare75d8342009-03-16 06:56:51 +0000940 OutputToPipe = true;
941 }
942
943 // Figure out where to put the job (pipes).
944 Job *Dest = &C.getJobs();
945 if (InputInfos[0].isPipe()) {
Daniel Dunbar7a178a42009-03-17 17:53:55 +0000946 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbare75d8342009-03-16 06:56:51 +0000947 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
948 Dest = &InputInfos[0].getPipe();
949 }
950
951 // Always use the first input as the base input.
952 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar7a178a42009-03-17 17:53:55 +0000953
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000954 // Determine the place to write output to (nothing, pipe, or filename) and
955 // where to put the new job.
Daniel Dunbar7a178a42009-03-17 17:53:55 +0000956 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbarb39cc522009-03-17 22:47:06 +0000957 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar7a178a42009-03-17 17:53:55 +0000958 } else if (OutputToPipe) {
959 // Append to current piped job or create a new one as appropriate.
Daniel Dunbarb39cc522009-03-17 22:47:06 +0000960 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
961 if (!PJ) {
962 PJ = new PipedJob();
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000963 // FIXME: Temporary hack so that -ccc-print-bindings work until we have
964 // pipe support. Please remove later.
Daniel Dunbar1fc898c2009-03-20 00:11:04 +0000965 if (!CCCPrintBindings)
966 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +0000967 Dest = PJ;
Daniel Dunbar7a178a42009-03-17 17:53:55 +0000968 }
Daniel Dunbarb39cc522009-03-17 22:47:06 +0000969 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar7a178a42009-03-17 17:53:55 +0000970 } else {
Daniel Dunbarb39cc522009-03-17 22:47:06 +0000971 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
972 A->getType(), BaseInput);
Daniel Dunbar7a178a42009-03-17 17:53:55 +0000973 }
974
Daniel Dunbarb39cc522009-03-17 22:47:06 +0000975 if (CCCPrintBindings) {
Daniel Dunbard67a3222009-03-30 06:36:42 +0000976 llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"'
977 << " - \"" << T.getName() << "\", inputs: [";
Daniel Dunbarb39cc522009-03-17 22:47:06 +0000978 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
979 llvm::errs() << InputInfos[i].getAsString();
980 if (i + 1 != e)
981 llvm::errs() << ", ";
982 }
983 llvm::errs() << "], output: " << Result.getAsString() << "\n";
984 } else {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000985 T.ConstructJob(C, *JA, *Dest, Result, InputInfos,
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +0000986 C.getArgsForToolChain(TC, BoundArch), LinkingOutput);
Daniel Dunbarb39cc522009-03-17 22:47:06 +0000987 }
Daniel Dunbare75d8342009-03-16 06:56:51 +0000988}
989
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000990const char *Driver::GetNamedOutputPath(Compilation &C,
Daniel Dunbar7a178a42009-03-17 17:53:55 +0000991 const JobAction &JA,
992 const char *BaseInput,
993 bool AtTopLevel) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +0000994 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar7a178a42009-03-17 17:53:55 +0000995 // Output to a user requested destination?
996 if (AtTopLevel) {
997 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
998 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
999 }
1000
1001 // Output to a temporary file?
1002 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001003 std::string TmpName =
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001004 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
1005 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001006 }
1007
1008 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbard00978b2009-03-18 02:00:31 +00001009 std::string BaseName(BasePath.getLast());
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001010
1011 // Determine what the derived output name should be.
1012 const char *NamedOutput;
1013 if (JA.getType() == types::TY_Image) {
1014 NamedOutput = DefaultImageName.c_str();
1015 } else {
1016 const char *Suffix = types::getTypeTempSuffix(JA.getType());
1017 assert(Suffix && "All types used for output should have a suffix.");
1018
1019 std::string::size_type End = std::string::npos;
1020 if (!types::appendSuffixForType(JA.getType()))
1021 End = BaseName.rfind('.');
1022 std::string Suffixed(BaseName.substr(0, End));
1023 Suffixed += '.';
1024 Suffixed += Suffix;
1025 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
1026 }
1027
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001028 // As an annoying special case, PCH generation doesn't strip the pathname.
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001029 if (JA.getType() == types::TY_PCH) {
1030 BasePath.eraseComponent();
Daniel Dunbare6c83192009-03-18 09:58:30 +00001031 if (BasePath.isEmpty())
1032 BasePath = NamedOutput;
1033 else
1034 BasePath.appendComponent(NamedOutput);
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001035 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
1036 } else {
1037 return C.addResultFile(NamedOutput);
1038 }
1039}
1040
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001041std::string Driver::GetFilePath(const char *Name, const ToolChain &TC) const {
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001042 const ToolChain::path_list &List = TC.getFilePaths();
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001043 for (ToolChain::path_list::const_iterator
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001044 it = List.begin(), ie = List.end(); it != ie; ++it) {
1045 llvm::sys::Path P(*it);
1046 P.appendComponent(Name);
1047 if (P.exists())
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001048 return P.str();
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001049 }
1050
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001051 return Name;
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +00001052}
1053
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001054std::string Driver::GetProgramPath(const char *Name, const ToolChain &TC,
1055 bool WantFile) const {
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001056 const ToolChain::path_list &List = TC.getProgramPaths();
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001057 for (ToolChain::path_list::const_iterator
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001058 it = List.begin(), ie = List.end(); it != ie; ++it) {
1059 llvm::sys::Path P(*it);
1060 P.appendComponent(Name);
Mike Stumpdb657372009-03-27 00:40:20 +00001061 if (WantFile ? P.exists() : P.canExecute())
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001062 return P.str();
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001063 }
1064
Daniel Dunbar76ce7412009-03-23 16:15:50 +00001065 // If all else failed, search the path.
1066 llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
Daniel Dunbar6f668772009-03-18 21:34:08 +00001067 if (!P.empty())
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001068 return P.str();
Daniel Dunbar6f668772009-03-18 21:34:08 +00001069
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001070 return Name;
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +00001071}
1072
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001073std::string Driver::GetTemporaryPath(const char *Suffix) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001074 // FIXME: This is lame; sys::Path should provide this function (in particular,
1075 // it should know how to find the temporary files dir).
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001076 std::string Error;
Daniel Dunbarc35694d2009-04-20 20:28:21 +00001077 const char *TmpDir = ::getenv("TMPDIR");
1078 if (!TmpDir)
1079 TmpDir = ::getenv("TEMP");
1080 if (!TmpDir)
Daniel Dunbar712e4de2009-04-21 00:25:10 +00001081 TmpDir = ::getenv("TMP");
1082 if (!TmpDir)
Daniel Dunbarc35694d2009-04-20 20:28:21 +00001083 TmpDir = "/tmp";
1084 llvm::sys::Path P(TmpDir);
Daniel Dunbarab8ce7c2009-04-20 17:32:49 +00001085 P.appendComponent("cc");
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001086 if (P.makeUnique(false, &Error)) {
1087 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
1088 return "";
1089 }
1090
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001091 // FIXME: Grumble, makeUnique sometimes leaves the file around!? PR3837.
Daniel Dunbar18d69de2009-03-18 23:08:52 +00001092 P.eraseFromDisk(false, 0);
1093
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001094 P.appendSuffix(Suffix);
Chris Lattner3441b4f2009-08-23 22:45:33 +00001095 return P.str();
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001096}
1097
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001098const HostInfo *Driver::GetHostInfo(const char *TripleStr) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +00001099 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001100 llvm::Triple Triple(TripleStr);
Daniel Dunbar4dff6a42009-03-10 23:41:59 +00001101
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001102 switch (Triple.getOS()) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001103 case llvm::Triple::AuroraUX:
1104 return createAuroraUXHostInfo(*this, Triple);
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001105 case llvm::Triple::Darwin:
1106 return createDarwinHostInfo(*this, Triple);
1107 case llvm::Triple::DragonFly:
1108 return createDragonFlyHostInfo(*this, Triple);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001109 case llvm::Triple::OpenBSD:
1110 return createOpenBSDHostInfo(*this, Triple);
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001111 case llvm::Triple::FreeBSD:
1112 return createFreeBSDHostInfo(*this, Triple);
Eli Friedman5cd659f2009-05-26 07:52:18 +00001113 case llvm::Triple::Linux:
1114 return createLinuxHostInfo(*this, Triple);
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001115 default:
1116 return createUnknownHostInfo(*this, Triple);
1117 }
Daniel Dunbar4dff6a42009-03-10 23:41:59 +00001118}
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001119
1120bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
Daniel Dunbar953b8d12009-09-08 23:36:55 +00001121 const llvm::Triple &Triple) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001122 // Check if user requested no clang, or clang doesn't understand this type (we
1123 // only handle single inputs for now).
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00001124 if (!CCCUseClang || JA.size() != 1 ||
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001125 !types::isAcceptedByClang((*JA.begin())->getType()))
1126 return false;
1127
Daniel Dunbar88f356e2009-03-24 19:02:31 +00001128 // Otherwise make sure this is an action clang understands.
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001129 if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001130 if (!CCCUseClangCPP) {
1131 Diag(clang::diag::warn_drv_not_using_clang_cpp);
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001132 return false;
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001133 }
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001134 } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
1135 return false;
1136
Daniel Dunbar88f356e2009-03-24 19:02:31 +00001137 // Use clang for C++?
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001138 if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
1139 Diag(clang::diag::warn_drv_not_using_clang_cxx);
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001140 return false;
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001141 }
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001142
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001143 // Always use clang for precompiling and AST generation, regardless of archs.
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00001144 if (isa<PrecompileJobAction>(JA) || JA.getType() == types::TY_AST)
Daniel Dunbarf2df7c22009-04-16 23:10:13 +00001145 return true;
1146
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001147 // Finally, don't use clang if this isn't one of the user specified archs to
1148 // build.
Daniel Dunbar953b8d12009-09-08 23:36:55 +00001149 if (!CCCClangArchs.empty() && !CCCClangArchs.count(Triple.getArch())) {
1150 Diag(clang::diag::warn_drv_not_using_clang_arch) << Triple.getArchName();
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001151 return false;
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001152 }
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001153
1154 return true;
1155}
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001156
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001157/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the
1158/// grouped values as integers. Numbers which are not provided are set to 0.
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001159///
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001160/// \return True if the entire string was parsed (9.2), or all groups were
1161/// parsed (10.3.5extrastuff).
1162bool Driver::GetReleaseVersion(const char *Str, unsigned &Major,
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001163 unsigned &Minor, unsigned &Micro,
1164 bool &HadExtra) {
1165 HadExtra = false;
1166
1167 Major = Minor = Micro = 0;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001168 if (*Str == '\0')
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001169 return true;
1170
1171 char *End;
1172 Major = (unsigned) strtol(Str, &End, 10);
1173 if (*Str != '\0' && *End == '\0')
1174 return true;
1175 if (*End != '.')
1176 return false;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001177
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001178 Str = End+1;
1179 Minor = (unsigned) strtol(Str, &End, 10);
1180 if (*Str != '\0' && *End == '\0')
1181 return true;
1182 if (*End != '.')
1183 return false;
1184
1185 Str = End+1;
1186 Micro = (unsigned) strtol(Str, &End, 10);
1187 if (*Str != '\0' && *End == '\0')
1188 return true;
1189 if (Str == End)
1190 return false;
1191 HadExtra = true;
1192 return true;
1193}