blob: 672c8905490d327fe3eec5518b6a897f7a157a99 [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);
127 CCCIsCXX = Args->hasArg(options::OPT_ccc_cxx);
128 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,
132 options::OPT_ccc_no_clang_cxx);
133 CCCUsePCH = Args->hasFlag(options::OPT_ccc_pch_is_pch,
134 options::OPT_ccc_pch_is_pth);
135 CCCUseClang = !Args->hasArg(options::OPT_ccc_no_clang);
136 CCCUseClangCPP = !Args->hasArg(options::OPT_ccc_no_clang_cpp);
137 if (const Arg *A = Args->getLastArg(options::OPT_ccc_clang_archs)) {
138 llvm::StringRef Cur = A->getValue(*Args);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000139
Daniel Dunbaracd69572009-12-04 21:55:23 +0000140 CCCClangArchs.clear();
141 while (!Cur.empty()) {
142 std::pair<llvm::StringRef, llvm::StringRef> Split = Cur.split(',');
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000143
Daniel Dunbaracd69572009-12-04 21:55:23 +0000144 if (!Split.first.empty()) {
145 llvm::Triple::ArchType Arch =
146 llvm::Triple(Split.first, "", "").getArch();
Daniel Dunbar7803c952009-04-01 23:34:41 +0000147
Daniel Dunbaracd69572009-12-04 21:55:23 +0000148 if (Arch == llvm::Triple::UnknownArch) {
149 Diag(clang::diag::err_drv_invalid_arch_name) << Arch;
150 continue;
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000151 }
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000152
Daniel Dunbaracd69572009-12-04 21:55:23 +0000153 CCCClangArchs.insert(Arch);
Daniel Dunbar953b8d12009-09-08 23:36:55 +0000154 }
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000155
Daniel Dunbaracd69572009-12-04 21:55:23 +0000156 Cur = Split.second;
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000157 }
158 }
Daniel Dunbaracd69572009-12-04 21:55:23 +0000159 if (const Arg *A = Args->getLastArg(options::OPT_ccc_host_triple))
160 HostTriple = A->getValue(*Args);
161 if (const Arg *A = Args->getLastArg(options::OPT_ccc_install_dir))
162 Dir = A->getValue(*Args);
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000163
Daniel Dunbar52e0c702009-03-17 20:45:45 +0000164 Host = GetHostInfo(HostTriple);
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000165
Daniel Dunbar3ce436d2009-03-16 06:42:30 +0000166 // The compilation takes ownership of Args.
Daniel Dunbar1ef3f2a2009-09-08 23:37:19 +0000167 Compilation *C = new Compilation(*this, *Host->CreateToolChain(*Args), Args);
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000168
169 // FIXME: This behavior shouldn't be here.
170 if (CCCPrintOptions) {
171 PrintOptions(C->getArgs());
172 return C;
173 }
174
175 if (!HandleImmediateArgs(*C))
176 return C;
177
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000178 // Construct the list of abstract actions to perform for this compilation. We
179 // avoid passing a Compilation here simply to enforce the abstraction that
180 // pipelining is not host or toolchain dependent (other than the driver driver
181 // test).
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000182 if (Host->useDriverDriver())
183 BuildUniversalActions(C->getArgs(), C->getActions());
184 else
185 BuildActions(C->getArgs(), C->getActions());
186
187 if (CCCPrintActions) {
Daniel Dunbareb843be2009-03-18 03:13:20 +0000188 PrintActions(*C);
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000189 return C;
190 }
191
192 BuildJobs(*C);
Daniel Dunbaradc91e62009-03-15 01:38:15 +0000193
194 return C;
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000195}
196
Daniel Dunbar38bfda62009-07-01 20:03:04 +0000197int Driver::ExecuteCompilation(const Compilation &C) const {
198 // Just print if -### was present.
199 if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
200 C.PrintJob(llvm::errs(), C.getJobs(), "\n", true);
201 return 0;
202 }
203
204 // If there were errors building the compilation, quit now.
205 if (getDiags().getNumErrors())
206 return 1;
207
208 const Command *FailingCommand = 0;
209 int Res = C.ExecuteJob(C.getJobs(), FailingCommand);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000210
Daniel Dunbar38bfda62009-07-01 20:03:04 +0000211 // Remove temp files.
212 C.CleanupFileList(C.getTempFiles());
213
214 // If the compilation failed, remove result files as well.
215 if (Res != 0 && !C.getArgs().hasArg(options::OPT_save_temps))
216 C.CleanupFileList(C.getResultFiles(), true);
217
218 // Print extra information about abnormal failures, if possible.
219 if (Res) {
220 // This is ad-hoc, but we don't want to be excessively noisy. If the result
221 // status was 1, assume the command failed normally. In particular, if it
222 // was the compiler then assume it gave a reasonable error code. Failures in
223 // other tools are less common, and they generally have worse diagnostics,
224 // so always print the diagnostic there.
225 const Action &Source = FailingCommand->getSource();
226 bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) ||
227 isa<PrecompileJobAction>(Source) ||
228 isa<AnalyzeJobAction>(Source) ||
229 isa<CompileJobAction>(Source));
230
231 if (!IsFriendlyTool || Res != 1) {
232 // FIXME: See FIXME above regarding result code interpretation.
233 if (Res < 0)
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000234 Diag(clang::diag::err_drv_command_signalled)
Daniel Dunbar38bfda62009-07-01 20:03:04 +0000235 << Source.getClassName() << -Res;
236 else
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000237 Diag(clang::diag::err_drv_command_failed)
Daniel Dunbar38bfda62009-07-01 20:03:04 +0000238 << Source.getClassName() << Res;
239 }
240 }
241
242 return Res;
243}
244
Daniel Dunbar446f6842009-03-12 18:24:49 +0000245void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000246 unsigned i = 0;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000247 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000248 it != ie; ++it, ++i) {
249 Arg *A = *it;
250 llvm::errs() << "Option " << i << " - "
251 << "Name: \"" << A->getOption().getName() << "\", "
252 << "Values: {";
253 for (unsigned j = 0; j < A->getNumValues(); ++j) {
254 if (j)
255 llvm::errs() << ", ";
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000256 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000257 }
258 llvm::errs() << "}\n";
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000259 }
Daniel Dunbar544ecd12009-03-02 19:59:07 +0000260}
Daniel Dunbar4dff6a42009-03-10 23:41:59 +0000261
Daniel Dunbar65b99522009-12-03 07:01:38 +0000262// FIXME: Move -ccc options to real options in the .td file (or eliminate), and
263// then move to using OptTable::PrintHelp.
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000264void Driver::PrintHelp(bool ShowHidden) const {
Daniel Dunbaracd69572009-12-04 21:55:23 +0000265 getOpts().PrintHelp(llvm::outs(), Name.c_str(),
266 "clang \"gcc-compatible\" driver", ShowHidden);
Daniel Dunbar7c925282009-03-31 21:38:17 +0000267}
268
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000269void Driver::PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000270 // FIXME: The following handlers should use a callback mechanism, we don't
271 // know what the client would like to do.
Mike Stump727170d2009-10-09 17:31:54 +0000272#ifdef CLANG_VENDOR
273 OS << CLANG_VENDOR;
274#endif
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000275 OS << "clang version " CLANG_VERSION_STRING " ("
Douglas Gregor1b7035d2009-10-05 20:33:49 +0000276 << getClangSubversionPath();
277 if (unsigned Revision = getClangSubversionRevision())
278 OS << " " << Revision;
279 OS << ")" << '\n';
Daniel Dunbarb10248802009-03-26 16:09:13 +0000280
281 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000282 OS << "Target: " << TC.getTripleString() << '\n';
Daniel Dunbar10978e42009-06-16 23:32:58 +0000283
284 // Print the threading model.
285 //
286 // FIXME: Implement correctly.
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000287 OS << "Thread model: " << "posix" << '\n';
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000288}
289
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000290bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000291 // The order these options are handled in in gcc is all over the place, but we
292 // don't expect inconsistencies w.r.t. that to matter in practice.
Daniel Dunbar7c925282009-03-31 21:38:17 +0000293
Daniel Dunbara9bbcfa2009-04-04 05:17:38 +0000294 if (C.getArgs().hasArg(options::OPT_dumpversion)) {
Douglas Gregor7b71e632009-04-27 22:23:34 +0000295 llvm::outs() << CLANG_VERSION_STRING "\n";
Daniel Dunbara9bbcfa2009-04-04 05:17:38 +0000296 return false;
297 }
298
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000299 if (C.getArgs().hasArg(options::OPT__help) ||
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000300 C.getArgs().hasArg(options::OPT__help_hidden)) {
301 PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden));
Daniel Dunbar7c925282009-03-31 21:38:17 +0000302 return false;
303 }
304
Daniel Dunbarb0006ae2009-04-02 15:05:41 +0000305 if (C.getArgs().hasArg(options::OPT__version)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000306 // Follow gcc behavior and use stdout for --version and stderr for -v.
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000307 PrintVersion(C, llvm::outs());
Daniel Dunbarb0006ae2009-04-02 15:05:41 +0000308 return false;
309 }
310
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000311 if (C.getArgs().hasArg(options::OPT_v) ||
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000312 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000313 PrintVersion(C, llvm::errs());
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000314 SuppressMissingInputWarning = true;
315 }
316
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000317 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbard972e222009-03-20 04:37:21 +0000318 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
319 llvm::outs() << "programs: =";
320 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
321 ie = TC.getProgramPaths().end(); it != ie; ++it) {
322 if (it != TC.getProgramPaths().begin())
323 llvm::outs() << ':';
324 llvm::outs() << *it;
325 }
326 llvm::outs() << "\n";
327 llvm::outs() << "libraries: =";
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000328 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
Daniel Dunbard972e222009-03-20 04:37:21 +0000329 ie = TC.getFilePaths().end(); it != ie; ++it) {
330 if (it != TC.getFilePaths().begin())
331 llvm::outs() << ':';
332 llvm::outs() << *it;
333 }
334 llvm::outs() << "\n";
Daniel Dunbar7c925282009-03-31 21:38:17 +0000335 return false;
Daniel Dunbard972e222009-03-20 04:37:21 +0000336 }
337
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000338 // FIXME: The following handlers should use a callback mechanism, we don't
339 // know what the client would like to do.
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000340 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
Daniel Dunbar1ce81532009-09-09 22:33:00 +0000341 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC) << "\n";
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000342 return false;
343 }
344
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000345 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
Daniel Dunbar1ce81532009-09-09 22:33:00 +0000346 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC) << "\n";
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000347 return false;
348 }
349
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000350 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbar1ce81532009-09-09 22:33:00 +0000351 llvm::outs() << GetFilePath("libgcc.a", TC) << "\n";
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000352 return false;
353 }
354
Daniel Dunbar1b3ec3a2009-06-16 23:25:22 +0000355 if (C.getArgs().hasArg(options::OPT_print_multi_lib)) {
356 // FIXME: We need tool chain support for this.
357 llvm::outs() << ".;\n";
358
359 switch (C.getDefaultToolChain().getTriple().getArch()) {
360 default:
361 break;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000362
Daniel Dunbar1b3ec3a2009-06-16 23:25:22 +0000363 case llvm::Triple::x86_64:
364 llvm::outs() << "x86_64;@m64" << "\n";
365 break;
366
367 case llvm::Triple::ppc64:
368 llvm::outs() << "ppc64;@m64" << "\n";
369 break;
370 }
371 return false;
372 }
373
374 // FIXME: What is the difference between print-multi-directory and
375 // print-multi-os-directory?
376 if (C.getArgs().hasArg(options::OPT_print_multi_directory) ||
377 C.getArgs().hasArg(options::OPT_print_multi_os_directory)) {
378 switch (C.getDefaultToolChain().getTriple().getArch()) {
379 default:
380 case llvm::Triple::x86:
381 case llvm::Triple::ppc:
382 llvm::outs() << "." << "\n";
383 break;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000384
Daniel Dunbar1b3ec3a2009-06-16 23:25:22 +0000385 case llvm::Triple::x86_64:
386 llvm::outs() << "x86_64" << "\n";
387 break;
388
389 case llvm::Triple::ppc64:
390 llvm::outs() << "ppc64" << "\n";
391 break;
392 }
393 return false;
394 }
395
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000396 return true;
397}
398
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000399static unsigned PrintActions1(const Compilation &C, Action *A,
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000400 std::map<Action*, unsigned> &Ids) {
401 if (Ids.count(A))
402 return Ids[A];
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000403
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000404 std::string str;
405 llvm::raw_string_ostream os(str);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000406
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000407 os << Action::getClassName(A->getKind()) << ", ";
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000408 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbareb843be2009-03-18 03:13:20 +0000409 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000410 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000411 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
Daniel Dunbareb843be2009-03-18 03:13:20 +0000412 C.getDefaultToolChain().getArchName()) << '"'
413 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000414 } else {
415 os << "{";
416 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbareb843be2009-03-18 03:13:20 +0000417 os << PrintActions1(C, *it, Ids);
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000418 ++it;
419 if (it != ie)
420 os << ", ";
421 }
422 os << "}";
423 }
424
425 unsigned Id = Ids.size();
426 Ids[A] = Id;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000427 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000428 << types::getTypeName(A->getType()) << "\n";
429
430 return Id;
431}
432
Daniel Dunbareb843be2009-03-18 03:13:20 +0000433void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000434 std::map<Action*, unsigned> Ids;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000435 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbareb843be2009-03-18 03:13:20 +0000436 ie = C.getActions().end(); it != ie; ++it)
437 PrintActions1(C, *it, Ids);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000438}
439
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000440void Driver::BuildUniversalActions(const ArgList &Args,
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000441 ActionList &Actions) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000442 llvm::PrettyStackTraceString CrashInfo("Building universal build actions");
443 // Collect the list of architectures. Duplicates are allowed, but should only
444 // be handled once (in the order seen).
Daniel Dunbare5dc4822009-03-13 20:33:35 +0000445 llvm::StringSet<> ArchNames;
446 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000447 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbarf479c122009-03-12 18:40:18 +0000448 it != ie; ++it) {
449 Arg *A = *it;
450
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000451 if (A->getOption().matches(options::OPT_arch)) {
Daniel Dunbar9c3f7c42009-09-08 23:37:30 +0000452 // Validate the option here; we don't save the type here because its
453 // particular spelling may participate in other driver choices.
454 llvm::Triple::ArchType Arch =
455 llvm::Triple::getArchTypeForDarwinArchName(A->getValue(Args));
456 if (Arch == llvm::Triple::UnknownArch) {
457 Diag(clang::diag::err_drv_invalid_arch_name)
458 << A->getAsString(Args);
459 continue;
460 }
461
Daniel Dunbar2da02722009-03-19 07:55:12 +0000462 A->claim();
Daniel Dunbar7b574042009-09-08 23:37:02 +0000463 if (ArchNames.insert(A->getValue(Args)))
464 Archs.push_back(A->getValue(Args));
Daniel Dunbarf479c122009-03-12 18:40:18 +0000465 }
466 }
467
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000468 // When there is no explicit arch for this platform, make sure we still bind
469 // the architecture (to the default) so that -Xarch_ is handled correctly.
Daniel Dunbareb843be2009-03-18 03:13:20 +0000470 if (!Archs.size())
471 Archs.push_back(0);
Daniel Dunbarf479c122009-03-12 18:40:18 +0000472
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000473 // FIXME: We killed off some others but these aren't yet detected in a
474 // functional manner. If we added information to jobs about which "auxiliary"
475 // files they wrote then we could detect the conflict these cause downstream.
Daniel Dunbarf479c122009-03-12 18:40:18 +0000476 if (Archs.size() > 1) {
477 // No recovery needed, the point of this is just to prevent
478 // overwriting the same files.
Daniel Dunbarf479c122009-03-12 18:40:18 +0000479 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000480 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar0e759942009-03-20 06:14:23 +0000481 << A->getAsString(Args);
Daniel Dunbarf479c122009-03-12 18:40:18 +0000482 }
483
484 ActionList SingleActions;
485 BuildActions(Args, SingleActions);
486
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000487 // Add in arch binding and lipo (if necessary) for every top level action.
Daniel Dunbarf479c122009-03-12 18:40:18 +0000488 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
489 Action *Act = SingleActions[i];
490
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000491 // Make sure we can lipo this kind of output. If not (and it is an actual
492 // output) then we disallow, since we can't create an output file with the
493 // right name without overwriting it. We could remove this oddity by just
494 // changing the output names to include the arch, which would also fix
Daniel Dunbarf479c122009-03-12 18:40:18 +0000495 // -save-temps. Compatibility wins for now.
496
Daniel Dunbare2ca3bd2009-03-13 17:46:02 +0000497 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbarf479c122009-03-12 18:40:18 +0000498 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
499 << types::getTypeName(Act->getType());
500
501 ActionList Inputs;
Daniel Dunbar2da02722009-03-19 07:55:12 +0000502 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbare5dc4822009-03-13 20:33:35 +0000503 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbarf479c122009-03-12 18:40:18 +0000504
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000505 // Lipo if necessary, we do it this way because we need to set the arch flag
506 // so that -Xarch_ gets overwritten.
Daniel Dunbarf479c122009-03-12 18:40:18 +0000507 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
508 Actions.append(Inputs.begin(), Inputs.end());
509 else
510 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
511 }
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000512}
513
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000514void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +0000515 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000516 // Start by constructing the list of inputs and their types.
517
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000518 // Track the current user specified (-x) input. We also explicitly track the
519 // argument used to set the type; we only want to claim the type when we
520 // actually use it, so we warn about unused -x arguments.
Daniel Dunbarc5a5ac52009-03-13 17:57:10 +0000521 types::ID InputType = types::TY_Nothing;
522 Arg *InputTypeArg = 0;
523
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000524 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000525 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000526 it != ie; ++it) {
527 Arg *A = *it;
528
529 if (isa<InputOption>(A->getOption())) {
530 const char *Value = A->getValue(Args);
531 types::ID Ty = types::TY_INVALID;
532
533 // Infer the input type if necessary.
Daniel Dunbarc5a5ac52009-03-13 17:57:10 +0000534 if (InputType == types::TY_Nothing) {
535 // If there was an explicit arg for this, claim it.
536 if (InputTypeArg)
537 InputTypeArg->claim();
538
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000539 // stdin must be handled specially.
540 if (memcmp(Value, "-", 2) == 0) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000541 // If running with -E, treat as a C input (this changes the builtin
542 // macros, for example). This may be overridden by -ObjC below.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000543 //
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000544 // Otherwise emit an error but still use a valid type to avoid
545 // spurious errors (e.g., no inputs).
Daniel Dunbarfffd1812009-11-19 04:00:53 +0000546 if (!Args.hasArgNoClaim(options::OPT_E))
Daniel Dunbar5cd1e412009-03-12 09:13:48 +0000547 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000548 Ty = types::TY_C;
549 } else {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000550 // Otherwise lookup by extension, and fallback to ObjectType if not
551 // found. We use a host hook here because Darwin at least has its own
552 // idea of what .s is.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000553 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbarea9f0322009-03-20 23:39:23 +0000554 Ty = Host->lookupTypeForExtension(Ext + 1);
555
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000556 if (Ty == types::TY_INVALID)
557 Ty = types::TY_Object;
558 }
559
Daniel Dunbar82b22102009-05-18 21:47:54 +0000560 // -ObjC and -ObjC++ override the default language, but only for "source
561 // files". We just treat everything that isn't a linker input as a
562 // source file.
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000563 //
Daniel Dunbar82b22102009-05-18 21:47:54 +0000564 // FIXME: Clean this up if we move the phase sequence into the type.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000565 if (Ty != types::TY_Object) {
566 if (Args.hasArg(options::OPT_ObjC))
567 Ty = types::TY_ObjC;
568 else if (Args.hasArg(options::OPT_ObjCXX))
569 Ty = types::TY_ObjCXX;
570 }
571 } else {
572 assert(InputTypeArg && "InputType set w/o InputTypeArg");
573 InputTypeArg->claim();
574 Ty = InputType;
575 }
576
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000577 // Check that the file exists. It isn't clear this is worth doing, since
578 // the tool presumably does this anyway, and this just adds an extra stat
579 // to the equation, but this is gcc compatible.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000580 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbar5cd1e412009-03-12 09:13:48 +0000581 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000582 else
583 Inputs.push_back(std::make_pair(Ty, A));
584
585 } else if (A->getOption().isLinkerInput()) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000586 // Just treat as object type, we could make a special type for this if
587 // necessary.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000588 Inputs.push_back(std::make_pair(types::TY_Object, A));
589
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000590 } else if (A->getOption().matches(options::OPT_x)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000591 InputTypeArg = A;
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000592 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
593
594 // Follow gcc behavior and treat as linker input for invalid -x
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000595 // options. Its not clear why we shouldn't just revert to unknown; but
596 // this isn't very important, we might as well be bug comatible.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000597 if (!InputType) {
Daniel Dunbar5cd1e412009-03-12 09:13:48 +0000598 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000599 InputType = types::TY_Object;
600 }
601 }
602 }
603
Daniel Dunbar34c41872009-03-13 00:17:48 +0000604 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000605 Diag(clang::diag::err_drv_no_input_files);
606 return;
607 }
608
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000609 // Determine which compilation mode we are in. We look for options which
610 // affect the phase, starting with the earliest phases, and record which
611 // option we used to determine the final phase.
Daniel Dunbar65229332009-03-13 11:38:42 +0000612 Arg *FinalPhaseArg = 0;
613 phases::ID FinalPhase;
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000614
615 // -{E,M,MM} only run the preprocessor.
Daniel Dunbar65229332009-03-13 11:38:42 +0000616 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
617 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
618 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
619 FinalPhase = phases::Preprocess;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000620
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +0000621 // -{fsyntax-only,-analyze,emit-ast,S} only run up to the compiler.
Daniel Dunbarae0e55e2009-03-15 00:48:16 +0000622 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
Daniel Dunbar5e051f92009-05-06 02:12:32 +0000623 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze,
624 options::OPT__analyze_auto)) ||
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +0000625 (FinalPhaseArg = Args.getLastArg(options::OPT_emit_ast)) ||
Daniel Dunbar65229332009-03-13 11:38:42 +0000626 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
627 FinalPhase = phases::Compile;
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000628
629 // -c only runs up to the assembler.
Daniel Dunbar65229332009-03-13 11:38:42 +0000630 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
631 FinalPhase = phases::Assemble;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000632
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000633 // Otherwise do everything.
634 } else
Daniel Dunbar65229332009-03-13 11:38:42 +0000635 FinalPhase = phases::Link;
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000636
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000637 // Reject -Z* at the top level, these options should never have been exposed
638 // by gcc.
Daniel Dunbardd765242009-03-26 16:12:09 +0000639 if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
Daniel Dunbar0e759942009-03-20 06:14:23 +0000640 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000641
Daniel Dunbar65229332009-03-13 11:38:42 +0000642 // Construct the actions to perform.
643 ActionList LinkerInputs;
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000644 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbar65229332009-03-13 11:38:42 +0000645 types::ID InputType = Inputs[i].first;
646 const Arg *InputArg = Inputs[i].second;
647
648 unsigned NumSteps = types::getNumCompilationPhases(InputType);
649 assert(NumSteps && "Invalid number of steps!");
650
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000651 // If the first step comes after the final phase we are doing as part of
652 // this compilation, warn the user about it.
Daniel Dunbar65229332009-03-13 11:38:42 +0000653 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
654 if (InitialPhase > FinalPhase) {
Daniel Dunbaradc5c7c2009-03-19 07:57:08 +0000655 // Claim here to avoid the more general unused warning.
656 InputArg->claim();
Daniel Dunbar07806ca2009-09-17 04:13:26 +0000657
658 // Special case '-E' warning on a previously preprocessed file to make
659 // more sense.
660 if (InitialPhase == phases::Compile && FinalPhase == phases::Preprocess &&
661 getPreprocessedType(InputType) == types::TY_INVALID)
662 Diag(clang::diag::warn_drv_preprocessed_input_file_unused)
663 << InputArg->getAsString(Args)
664 << FinalPhaseArg->getOption().getName();
665 else
666 Diag(clang::diag::warn_drv_input_file_unused)
667 << InputArg->getAsString(Args)
668 << getPhaseName(InitialPhase)
669 << FinalPhaseArg->getOption().getName();
Daniel Dunbar65229332009-03-13 11:38:42 +0000670 continue;
671 }
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000672
Daniel Dunbar65229332009-03-13 11:38:42 +0000673 // Build the pipeline for this file.
674 Action *Current = new InputAction(*InputArg, InputType);
675 for (unsigned i = 0; i != NumSteps; ++i) {
676 phases::ID Phase = types::getCompilationPhase(InputType, i);
677
678 // We are done if this step is past what the user requested.
679 if (Phase > FinalPhase)
680 break;
681
682 // Queue linker inputs.
683 if (Phase == phases::Link) {
684 assert(i + 1 == NumSteps && "linking must be final compilation step.");
685 LinkerInputs.push_back(Current);
686 Current = 0;
687 break;
688 }
689
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000690 // Some types skip the assembler phase (e.g., llvm-bc), but we can't
691 // encode this in the steps because the intermediate type depends on
692 // arguments. Just special case here.
Daniel Dunbar13864952009-03-24 20:17:30 +0000693 if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
694 continue;
695
Daniel Dunbar65229332009-03-13 11:38:42 +0000696 // Otherwise construct the appropriate action.
697 Current = ConstructPhaseAction(Args, Phase, Current);
698 if (Current->getType() == types::TY_Nothing)
699 break;
700 }
701
702 // If we ended with something, add to the output list.
703 if (Current)
704 Actions.push_back(Current);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000705 }
Daniel Dunbar65229332009-03-13 11:38:42 +0000706
707 // Add a link action if necessary.
708 if (!LinkerInputs.empty())
709 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
710}
711
712Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
713 Action *Input) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +0000714 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbar65229332009-03-13 11:38:42 +0000715 // Build the appropriate action.
716 switch (Phase) {
717 case phases::Link: assert(0 && "link action invalid here.");
718 case phases::Preprocess: {
Daniel Dunbard67a3222009-03-30 06:36:42 +0000719 types::ID OutputTy;
720 // -{M, MM} alter the output type.
721 if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) {
722 OutputTy = types::TY_Dependencies;
723 } else {
724 OutputTy = types::getPreprocessedType(Input->getType());
725 assert(OutputTy != types::TY_INVALID &&
726 "Cannot preprocess this input type!");
727 }
Daniel Dunbar65229332009-03-13 11:38:42 +0000728 return new PreprocessJobAction(Input, OutputTy);
729 }
730 case phases::Precompile:
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000731 return new PrecompileJobAction(Input, types::TY_PCH);
Daniel Dunbar65229332009-03-13 11:38:42 +0000732 case phases::Compile: {
733 if (Args.hasArg(options::OPT_fsyntax_only)) {
734 return new CompileJobAction(Input, types::TY_Nothing);
Daniel Dunbar5e051f92009-05-06 02:12:32 +0000735 } else if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) {
Daniel Dunbar65229332009-03-13 11:38:42 +0000736 return new AnalyzeJobAction(Input, types::TY_Plist);
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +0000737 } else if (Args.hasArg(options::OPT_emit_ast)) {
738 return new CompileJobAction(Input, types::TY_AST);
Daniel Dunbar13864952009-03-24 20:17:30 +0000739 } else if (Args.hasArg(options::OPT_emit_llvm) ||
740 Args.hasArg(options::OPT_flto) ||
741 Args.hasArg(options::OPT_O4)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000742 types::ID Output =
Daniel Dunbar65229332009-03-13 11:38:42 +0000743 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
744 return new CompileJobAction(Input, Output);
745 } else {
746 return new CompileJobAction(Input, types::TY_PP_Asm);
747 }
748 }
749 case phases::Assemble:
750 return new AssembleJobAction(Input, types::TY_Object);
751 }
752
753 assert(0 && "invalid phase in ConstructPhaseAction");
754 return 0;
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000755}
756
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000757void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +0000758 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbare75d8342009-03-16 06:56:51 +0000759 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
760 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +0000761
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000762 // FIXME: Pipes are forcibly disabled until we support executing them.
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +0000763 if (!CCCPrintBindings)
764 UsePipes = false;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000765
Daniel Dunbare75d8342009-03-16 06:56:51 +0000766 // -save-temps inhibits pipes.
767 if (SaveTemps && UsePipes) {
768 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
769 UsePipes = true;
770 }
771
772 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
773
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000774 // It is an error to provide a -o option if we are making multiple output
775 // files.
Daniel Dunbare75d8342009-03-16 06:56:51 +0000776 if (FinalOutput) {
777 unsigned NumOutputs = 0;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000778 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000779 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbare75d8342009-03-16 06:56:51 +0000780 if ((*it)->getType() != types::TY_Nothing)
781 ++NumOutputs;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000782
Daniel Dunbare75d8342009-03-16 06:56:51 +0000783 if (NumOutputs > 1) {
784 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
785 FinalOutput = 0;
786 }
787 }
788
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000789 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000790 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbare75d8342009-03-16 06:56:51 +0000791 Action *A = *it;
792
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000793 // If we are linking an image for multiple archs then the linker wants
794 // -arch_multiple and -final_output <final image name>. Unfortunately, this
795 // doesn't fit in cleanly because we have to pass this information down.
Daniel Dunbare75d8342009-03-16 06:56:51 +0000796 //
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000797 // FIXME: This is a hack; find a cleaner way to integrate this into the
798 // process.
Daniel Dunbare75d8342009-03-16 06:56:51 +0000799 const char *LinkingOutput = 0;
Daniel Dunbardd765242009-03-26 16:12:09 +0000800 if (isa<LipoJobAction>(A)) {
Daniel Dunbare75d8342009-03-16 06:56:51 +0000801 if (FinalOutput)
802 LinkingOutput = FinalOutput->getValue(C.getArgs());
803 else
804 LinkingOutput = DefaultImageName.c_str();
805 }
806
807 InputInfo II;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000808 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +0000809 /*BoundArch*/0,
Daniel Dunbare75d8342009-03-16 06:56:51 +0000810 /*CanAcceptPipe*/ true,
811 /*AtTopLevel*/ true,
812 /*LinkingOutput*/ LinkingOutput,
813 II);
814 }
Daniel Dunbar3ce436d2009-03-16 06:42:30 +0000815
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000816 // If the user passed -Qunused-arguments or there were errors, don't warn
817 // about any unused arguments.
818 if (Diags.getNumErrors() ||
Daniel Dunbara3cfbe32009-04-07 19:04:18 +0000819 C.getArgs().hasArg(options::OPT_Qunused_arguments))
Daniel Dunbard175d972009-03-18 18:03:46 +0000820 return;
821
Daniel Dunbar58399ae2009-03-29 22:24:54 +0000822 // Claim -### here.
823 (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000824
Daniel Dunbar3ce436d2009-03-16 06:42:30 +0000825 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
826 it != ie; ++it) {
827 Arg *A = *it;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000828
Daniel Dunbar3ce436d2009-03-16 06:42:30 +0000829 // FIXME: It would be nice to be able to send the argument to the
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000830 // Diagnostic, so that extra values, position, and so on could be printed.
Daniel Dunbar90dd6f42009-04-04 00:52:26 +0000831 if (!A->isClaimed()) {
Daniel Dunbara3cfbe32009-04-07 19:04:18 +0000832 if (A->getOption().hasNoArgumentUnused())
833 continue;
834
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000835 // Suppress the warning automatically if this is just a flag, and it is an
836 // instance of an argument we already claimed.
Daniel Dunbar90dd6f42009-04-04 00:52:26 +0000837 const Option &Opt = A->getOption();
838 if (isa<FlagOption>(Opt)) {
839 bool DuplicateClaimed = false;
840
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000841 for (arg_iterator it = C.getArgs().filtered_begin(&Opt),
842 ie = C.getArgs().filtered_end(); it != ie; ++it) {
843 if ((*it)->isClaimed()) {
Daniel Dunbar90dd6f42009-04-04 00:52:26 +0000844 DuplicateClaimed = true;
845 break;
846 }
847 }
848
849 if (DuplicateClaimed)
850 continue;
851 }
852
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000853 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar0e759942009-03-20 06:14:23 +0000854 << A->getAsString(C.getArgs());
Daniel Dunbar90dd6f42009-04-04 00:52:26 +0000855 }
Daniel Dunbar3ce436d2009-03-16 06:42:30 +0000856 }
Daniel Dunbar95e6b192009-03-13 22:12:33 +0000857}
858
Daniel Dunbare75d8342009-03-16 06:56:51 +0000859void Driver::BuildJobsForAction(Compilation &C,
860 const Action *A,
861 const ToolChain *TC,
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +0000862 const char *BoundArch,
Daniel Dunbare75d8342009-03-16 06:56:51 +0000863 bool CanAcceptPipe,
864 bool AtTopLevel,
865 const char *LinkingOutput,
866 InputInfo &Result) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000867 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +0000868
869 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000870 // FIXME: Pipes are forcibly disabled until we support executing them.
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +0000871 if (!CCCPrintBindings)
872 UsePipes = false;
873
Daniel Dunbare75d8342009-03-16 06:56:51 +0000874 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000875 // FIXME: It would be nice to not claim this here; maybe the old scheme of
876 // just using Args was better?
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +0000877 const Arg &Input = IA->getInputArg();
878 Input.claim();
879 if (isa<PositionalArg>(Input)) {
880 const char *Name = Input.getValue(C.getArgs());
881 Result = InputInfo(Name, A->getType(), Name);
882 } else
883 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbare75d8342009-03-16 06:56:51 +0000884 return;
885 }
886
887 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar1ef3f2a2009-09-08 23:37:19 +0000888 const ToolChain *TC = &C.getDefaultToolChain();
889
Daniel Dunbar51c7f972009-05-22 02:53:45 +0000890 std::string Arch;
Daniel Dunbar1ef3f2a2009-09-08 23:37:19 +0000891 if (BAA->getArchName())
892 TC = Host->CreateToolChain(C.getArgs(), BAA->getArchName());
893
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +0000894 BuildJobsForAction(C, *BAA->begin(), TC, BAA->getArchName(),
895 CanAcceptPipe, AtTopLevel, LinkingOutput, Result);
Daniel Dunbare75d8342009-03-16 06:56:51 +0000896 return;
897 }
898
899 const JobAction *JA = cast<JobAction>(A);
900 const Tool &T = TC->SelectTool(C, *JA);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000901
902 // See if we should use an integrated preprocessor. We do so when we have
903 // exactly one input, since this is the only use case we care about
904 // (irrelevant since we don't support combine yet).
Daniel Dunbare75d8342009-03-16 06:56:51 +0000905 bool UseIntegratedCPP = false;
906 const ActionList *Inputs = &A->getInputs();
907 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
908 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
909 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
910 !C.getArgs().hasArg(options::OPT_save_temps) &&
911 T.hasIntegratedCPP()) {
912 UseIntegratedCPP = true;
913 Inputs = &(*Inputs)[0]->getInputs();
914 }
915 }
916
917 // Only use pipes when there is exactly one input.
918 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar1a093d22009-03-18 06:00:36 +0000919 InputInfoList InputInfos;
Daniel Dunbare75d8342009-03-16 06:56:51 +0000920 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
921 it != ie; ++it) {
922 InputInfo II;
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +0000923 BuildJobsForAction(C, *it, TC, BoundArch, TryToUsePipeInput,
924 /*AtTopLevel*/false, LinkingOutput, II);
Daniel Dunbare75d8342009-03-16 06:56:51 +0000925 InputInfos.push_back(II);
926 }
927
928 // Determine if we should output to a pipe.
929 bool OutputToPipe = false;
930 if (CanAcceptPipe && T.canPipeOutput()) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000931 // Some actions default to writing to a pipe if they are the top level phase
932 // and there was no user override.
Daniel Dunbare75d8342009-03-16 06:56:51 +0000933 //
934 // FIXME: Is there a better way to handle this?
935 if (AtTopLevel) {
936 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
937 OutputToPipe = true;
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +0000938 } else if (UsePipes)
Daniel Dunbare75d8342009-03-16 06:56:51 +0000939 OutputToPipe = true;
940 }
941
942 // Figure out where to put the job (pipes).
943 Job *Dest = &C.getJobs();
944 if (InputInfos[0].isPipe()) {
Daniel Dunbar7a178a42009-03-17 17:53:55 +0000945 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbare75d8342009-03-16 06:56:51 +0000946 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
947 Dest = &InputInfos[0].getPipe();
948 }
949
950 // Always use the first input as the base input.
951 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar7a178a42009-03-17 17:53:55 +0000952
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000953 // Determine the place to write output to (nothing, pipe, or filename) and
954 // where to put the new job.
Daniel Dunbar7a178a42009-03-17 17:53:55 +0000955 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbarb39cc522009-03-17 22:47:06 +0000956 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar7a178a42009-03-17 17:53:55 +0000957 } else if (OutputToPipe) {
958 // Append to current piped job or create a new one as appropriate.
Daniel Dunbarb39cc522009-03-17 22:47:06 +0000959 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
960 if (!PJ) {
961 PJ = new PipedJob();
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000962 // FIXME: Temporary hack so that -ccc-print-bindings work until we have
963 // pipe support. Please remove later.
Daniel Dunbar1fc898c2009-03-20 00:11:04 +0000964 if (!CCCPrintBindings)
965 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +0000966 Dest = PJ;
Daniel Dunbar7a178a42009-03-17 17:53:55 +0000967 }
Daniel Dunbarb39cc522009-03-17 22:47:06 +0000968 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar7a178a42009-03-17 17:53:55 +0000969 } else {
Daniel Dunbarb39cc522009-03-17 22:47:06 +0000970 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
971 A->getType(), BaseInput);
Daniel Dunbar7a178a42009-03-17 17:53:55 +0000972 }
973
Daniel Dunbarb39cc522009-03-17 22:47:06 +0000974 if (CCCPrintBindings) {
Daniel Dunbard67a3222009-03-30 06:36:42 +0000975 llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"'
976 << " - \"" << T.getName() << "\", inputs: [";
Daniel Dunbarb39cc522009-03-17 22:47:06 +0000977 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
978 llvm::errs() << InputInfos[i].getAsString();
979 if (i + 1 != e)
980 llvm::errs() << ", ";
981 }
982 llvm::errs() << "], output: " << Result.getAsString() << "\n";
983 } else {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000984 T.ConstructJob(C, *JA, *Dest, Result, InputInfos,
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +0000985 C.getArgsForToolChain(TC, BoundArch), LinkingOutput);
Daniel Dunbarb39cc522009-03-17 22:47:06 +0000986 }
Daniel Dunbare75d8342009-03-16 06:56:51 +0000987}
988
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000989const char *Driver::GetNamedOutputPath(Compilation &C,
Daniel Dunbar7a178a42009-03-17 17:53:55 +0000990 const JobAction &JA,
991 const char *BaseInput,
992 bool AtTopLevel) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +0000993 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar7a178a42009-03-17 17:53:55 +0000994 // Output to a user requested destination?
995 if (AtTopLevel) {
996 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
997 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
998 }
999
1000 // Output to a temporary file?
1001 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001002 std::string TmpName =
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001003 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
1004 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001005 }
1006
1007 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbard00978b2009-03-18 02:00:31 +00001008 std::string BaseName(BasePath.getLast());
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001009
1010 // Determine what the derived output name should be.
1011 const char *NamedOutput;
1012 if (JA.getType() == types::TY_Image) {
1013 NamedOutput = DefaultImageName.c_str();
1014 } else {
1015 const char *Suffix = types::getTypeTempSuffix(JA.getType());
1016 assert(Suffix && "All types used for output should have a suffix.");
1017
1018 std::string::size_type End = std::string::npos;
1019 if (!types::appendSuffixForType(JA.getType()))
1020 End = BaseName.rfind('.');
1021 std::string Suffixed(BaseName.substr(0, End));
1022 Suffixed += '.';
1023 Suffixed += Suffix;
1024 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
1025 }
1026
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001027 // As an annoying special case, PCH generation doesn't strip the pathname.
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001028 if (JA.getType() == types::TY_PCH) {
1029 BasePath.eraseComponent();
Daniel Dunbare6c83192009-03-18 09:58:30 +00001030 if (BasePath.isEmpty())
1031 BasePath = NamedOutput;
1032 else
1033 BasePath.appendComponent(NamedOutput);
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001034 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
1035 } else {
1036 return C.addResultFile(NamedOutput);
1037 }
1038}
1039
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001040std::string Driver::GetFilePath(const char *Name, const ToolChain &TC) const {
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001041 const ToolChain::path_list &List = TC.getFilePaths();
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001042 for (ToolChain::path_list::const_iterator
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001043 it = List.begin(), ie = List.end(); it != ie; ++it) {
1044 llvm::sys::Path P(*it);
1045 P.appendComponent(Name);
1046 if (P.exists())
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001047 return P.str();
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001048 }
1049
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001050 return Name;
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +00001051}
1052
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001053std::string Driver::GetProgramPath(const char *Name, const ToolChain &TC,
1054 bool WantFile) const {
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001055 const ToolChain::path_list &List = TC.getProgramPaths();
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001056 for (ToolChain::path_list::const_iterator
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001057 it = List.begin(), ie = List.end(); it != ie; ++it) {
1058 llvm::sys::Path P(*it);
1059 P.appendComponent(Name);
Mike Stumpdb657372009-03-27 00:40:20 +00001060 if (WantFile ? P.exists() : P.canExecute())
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001061 return P.str();
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001062 }
1063
Daniel Dunbar76ce7412009-03-23 16:15:50 +00001064 // If all else failed, search the path.
1065 llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
Daniel Dunbar6f668772009-03-18 21:34:08 +00001066 if (!P.empty())
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001067 return P.str();
Daniel Dunbar6f668772009-03-18 21:34:08 +00001068
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001069 return Name;
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +00001070}
1071
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001072std::string Driver::GetTemporaryPath(const char *Suffix) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001073 // FIXME: This is lame; sys::Path should provide this function (in particular,
1074 // it should know how to find the temporary files dir).
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001075 std::string Error;
Daniel Dunbarc35694d2009-04-20 20:28:21 +00001076 const char *TmpDir = ::getenv("TMPDIR");
1077 if (!TmpDir)
1078 TmpDir = ::getenv("TEMP");
1079 if (!TmpDir)
Daniel Dunbar712e4de2009-04-21 00:25:10 +00001080 TmpDir = ::getenv("TMP");
1081 if (!TmpDir)
Daniel Dunbarc35694d2009-04-20 20:28:21 +00001082 TmpDir = "/tmp";
1083 llvm::sys::Path P(TmpDir);
Daniel Dunbarab8ce7c2009-04-20 17:32:49 +00001084 P.appendComponent("cc");
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001085 if (P.makeUnique(false, &Error)) {
1086 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
1087 return "";
1088 }
1089
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001090 // FIXME: Grumble, makeUnique sometimes leaves the file around!? PR3837.
Daniel Dunbar18d69de2009-03-18 23:08:52 +00001091 P.eraseFromDisk(false, 0);
1092
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001093 P.appendSuffix(Suffix);
Chris Lattner3441b4f2009-08-23 22:45:33 +00001094 return P.str();
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001095}
1096
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001097const HostInfo *Driver::GetHostInfo(const char *TripleStr) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +00001098 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001099 llvm::Triple Triple(TripleStr);
Daniel Dunbar4dff6a42009-03-10 23:41:59 +00001100
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001101 switch (Triple.getOS()) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001102 case llvm::Triple::AuroraUX:
1103 return createAuroraUXHostInfo(*this, Triple);
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001104 case llvm::Triple::Darwin:
1105 return createDarwinHostInfo(*this, Triple);
1106 case llvm::Triple::DragonFly:
1107 return createDragonFlyHostInfo(*this, Triple);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001108 case llvm::Triple::OpenBSD:
1109 return createOpenBSDHostInfo(*this, Triple);
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001110 case llvm::Triple::FreeBSD:
1111 return createFreeBSDHostInfo(*this, Triple);
Eli Friedman5cd659f2009-05-26 07:52:18 +00001112 case llvm::Triple::Linux:
1113 return createLinuxHostInfo(*this, Triple);
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001114 default:
1115 return createUnknownHostInfo(*this, Triple);
1116 }
Daniel Dunbar4dff6a42009-03-10 23:41:59 +00001117}
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001118
1119bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
Daniel Dunbar953b8d12009-09-08 23:36:55 +00001120 const llvm::Triple &Triple) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001121 // Check if user requested no clang, or clang doesn't understand this type (we
1122 // only handle single inputs for now).
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00001123 if (!CCCUseClang || JA.size() != 1 ||
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001124 !types::isAcceptedByClang((*JA.begin())->getType()))
1125 return false;
1126
Daniel Dunbar88f356e2009-03-24 19:02:31 +00001127 // Otherwise make sure this is an action clang understands.
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001128 if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001129 if (!CCCUseClangCPP) {
1130 Diag(clang::diag::warn_drv_not_using_clang_cpp);
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001131 return false;
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001132 }
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001133 } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
1134 return false;
1135
Daniel Dunbar88f356e2009-03-24 19:02:31 +00001136 // Use clang for C++?
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001137 if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
1138 Diag(clang::diag::warn_drv_not_using_clang_cxx);
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001139 return false;
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001140 }
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001141
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001142 // Always use clang for precompiling and AST generation, regardless of archs.
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00001143 if (isa<PrecompileJobAction>(JA) || JA.getType() == types::TY_AST)
Daniel Dunbarf2df7c22009-04-16 23:10:13 +00001144 return true;
1145
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001146 // Finally, don't use clang if this isn't one of the user specified archs to
1147 // build.
Daniel Dunbar953b8d12009-09-08 23:36:55 +00001148 if (!CCCClangArchs.empty() && !CCCClangArchs.count(Triple.getArch())) {
1149 Diag(clang::diag::warn_drv_not_using_clang_arch) << Triple.getArchName();
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001150 return false;
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001151 }
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001152
1153 return true;
1154}
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001155
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001156/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the
1157/// grouped values as integers. Numbers which are not provided are set to 0.
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001158///
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001159/// \return True if the entire string was parsed (9.2), or all groups were
1160/// parsed (10.3.5extrastuff).
1161bool Driver::GetReleaseVersion(const char *Str, unsigned &Major,
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001162 unsigned &Minor, unsigned &Micro,
1163 bool &HadExtra) {
1164 HadExtra = false;
1165
1166 Major = Minor = Micro = 0;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001167 if (*Str == '\0')
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001168 return true;
1169
1170 char *End;
1171 Major = (unsigned) strtol(Str, &End, 10);
1172 if (*Str != '\0' && *End == '\0')
1173 return true;
1174 if (*End != '.')
1175 return false;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001176
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001177 Str = End+1;
1178 Minor = (unsigned) strtol(Str, &End, 10);
1179 if (*Str != '\0' && *End == '\0')
1180 return true;
1181 if (*End != '.')
1182 return false;
1183
1184 Str = End+1;
1185 Micro = (unsigned) strtol(Str, &End, 10);
1186 if (*Str != '\0' && *End == '\0')
1187 return true;
1188 if (Str == End)
1189 return false;
1190 HadExtra = true;
1191 return true;
1192}