blob: e3d812d68d02e170cb919e1daeb23304fb99902e [file] [log] [blame]
Daniel Dunbar3ede8d02009-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 Dunbar3ede8d02009-03-02 19:59:07 +000010#include "clang/Driver/Driver.h"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000011
Daniel Dunbar53ec5522009-03-12 07:58:46 +000012#include "clang/Driver/Action.h"
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
15#include "clang/Driver/Compilation.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000017#include "clang/Driver/HostInfo.h"
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000018#include "clang/Driver/Job.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000019#include "clang/Driver/Option.h"
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000020#include "clang/Driver/Options.h"
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000021#include "clang/Driver/Tool.h"
22#include "clang/Driver/ToolChain.h"
Daniel Dunbar53ec5522009-03-12 07:58:46 +000023#include "clang/Driver/Types.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000024
Douglas Gregorab41e632009-04-27 22:23:34 +000025#include "clang/Basic/Version.h"
26
Daniel Dunbar13689542009-03-13 20:33:35 +000027#include "llvm/ADT/StringSet.h"
Daniel Dunbar8f25c792009-03-18 01:38:48 +000028#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000029#include "llvm/Support/raw_ostream.h"
Daniel Dunbar53ec5522009-03-12 07:58:46 +000030#include "llvm/System/Path.h"
Daniel Dunbar632f50e2009-03-18 21:34:08 +000031#include "llvm/System/Program.h"
Daniel Dunbarba102132009-03-13 12:19:02 +000032
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000033#include "InputInfo.h"
34
Daniel Dunbarba102132009-03-13 12:19:02 +000035#include <map>
36
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000037using namespace clang::driver;
Chris Lattner92b36992009-03-26 05:56:24 +000038using namespace clang;
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000039
Daniel Dunbar4a124082009-08-23 18:42:54 +000040// Used to set values for "production" clang, for releases.
Daniel Dunbar8bde5052009-08-23 19:41:53 +000041// #define USE_PRODUCTION_CLANG
Daniel Dunbar4a124082009-08-23 18:42:54 +000042
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000043Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000044 const char *_DefaultHostTriple,
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000045 const char *_DefaultImageName,
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000046 Diagnostic &_Diags)
47 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000048 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000049 DefaultImageName(_DefaultImageName),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000050 Host(0),
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +000051 CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false),
Daniel Dunbar4a124082009-08-23 18:42:54 +000052 CCCGenericGCCName("gcc"), CCCUseClang(true),
53#ifdef USE_PRODUCTION_CLANG
54 CCCUseClangCXX(false),
55#else
56 CCCUseClangCXX(true),
57#endif
Douglas Gregor214e8722009-04-28 22:44:02 +000058 CCCUseClangCPP(true), CCCUsePCH(true),
Daniel Dunbar8b1604e2009-03-13 00:17:48 +000059 SuppressMissingInputWarning(false)
Daniel Dunbar365c02f2009-03-10 20:52:46 +000060{
Daniel Dunbar4a124082009-08-23 18:42:54 +000061#ifdef USE_PRODUCTION_CLANG
62 // Only use clang on i386 and x86_64 by default, in a "production" build.
63 CCCClangArchs.insert("i386");
64 CCCClangArchs.insert("x86_64");
Mike Stump4f711c42009-09-03 01:30:36 +000065 CCCClangArchs.insert("arm");
66 CCCClangArchs.insert("armv5");
67 CCCClangArchs.insert("armv6");
68 CCCClangArchs.insert("armv7");
Daniel Dunbar4a124082009-08-23 18:42:54 +000069#endif
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000070}
71
72Driver::~Driver() {
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000073 delete Opts;
Daniel Dunbar7e4534d2009-03-18 01:09:40 +000074 delete Host;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000075}
76
Daniel Dunbarf3cad362009-03-25 04:13:45 +000077InputArgList *Driver::ParseArgStrings(const char **ArgBegin,
78 const char **ArgEnd) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +000079 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbarf3cad362009-03-25 04:13:45 +000080 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
Daniel Dunbar06482622009-03-05 06:38:47 +000081
Daniel Dunbarad2a9af2009-03-13 11:38:42 +000082 // FIXME: Handle '@' args (or at least error on them).
83
Daniel Dunbar06482622009-03-05 06:38:47 +000084 unsigned Index = 0, End = ArgEnd - ArgBegin;
85 while (Index < End) {
Daniel Dunbar41393402009-03-13 01:01:44 +000086 // gcc's handling of empty arguments doesn't make
87 // sense, but this is not a common use case. :)
88 //
89 // We just ignore them here (note that other things may
90 // still take them as arguments).
91 if (Args->getArgString(Index)[0] == '\0') {
92 ++Index;
93 continue;
94 }
95
Daniel Dunbar06482622009-03-05 06:38:47 +000096 unsigned Prev = Index;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000097 Arg *A = getOpts().ParseOneArg(*Args, Index);
98 assert(Index > Prev && "Parser failed to consume argument.");
Daniel Dunbar53ec5522009-03-12 07:58:46 +000099
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000100 // Check for missing argument error.
101 if (!A) {
102 assert(Index >= End && "Unexpected parser error.");
103 Diag(clang::diag::err_drv_missing_argument)
104 << Args->getArgString(Prev)
105 << (Index - Prev - 1);
106 break;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000107 }
Daniel Dunbar06482622009-03-05 06:38:47 +0000108
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000109 if (A->getOption().isUnsupported()) {
110 Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
111 continue;
112 }
113 Args->append(A);
Daniel Dunbar06482622009-03-05 06:38:47 +0000114 }
115
116 return Args;
117}
118
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000119Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000120 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
121
Daniel Dunbarcb881672009-03-13 00:51:18 +0000122 // FIXME: Handle environment options which effect driver behavior,
123 // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH,
124 // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS.
125
126 // FIXME: What are we going to do with -V and -b?
127
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000128 // FIXME: This stuff needs to go into the Compilation, not the
129 // driver.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000130 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar06482622009-03-05 06:38:47 +0000131
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000132 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000133 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000134
135 // Read -ccc args.
136 //
137 // FIXME: We need to figure out where this behavior should
138 // live. Most of it should be outside in the client; the parts that
139 // aren't should have proper options, either by introducing new ones
140 // or by overloading gcc ones like -V or -b.
141 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
142 const char *Opt = *Start + 5;
143
144 if (!strcmp(Opt, "print-options")) {
145 CCCPrintOptions = true;
146 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000147 CCCPrintActions = true;
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000148 } else if (!strcmp(Opt, "print-bindings")) {
149 CCCPrintBindings = true;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000150 } else if (!strcmp(Opt, "cxx")) {
151 CCCIsCXX = true;
152 } else if (!strcmp(Opt, "echo")) {
153 CCCEcho = true;
154
Daniel Dunbar78d8a082009-04-01 23:34:41 +0000155 } else if (!strcmp(Opt, "gcc-name")) {
156 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
157 CCCGenericGCCName = *++Start;
158
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000159 } else if (!strcmp(Opt, "clang-cxx")) {
160 CCCUseClangCXX = true;
Daniel Dunbardfaf4b32009-07-23 17:48:59 +0000161 } else if (!strcmp(Opt, "no-clang-cxx")) {
162 CCCUseClangCXX = false;
Douglas Gregordf91ef32009-04-18 00:34:01 +0000163 } else if (!strcmp(Opt, "pch-is-pch")) {
164 CCCUsePCH = true;
165 } else if (!strcmp(Opt, "pch-is-pth")) {
166 CCCUsePCH = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000167 } else if (!strcmp(Opt, "no-clang")) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000168 CCCUseClang = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000169 } else if (!strcmp(Opt, "no-clang-cpp")) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000170 CCCUseClangCPP = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000171 } else if (!strcmp(Opt, "clang-archs")) {
172 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
173 const char *Cur = *++Start;
174
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000175 CCCClangArchs.clear();
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000176 for (;;) {
177 const char *Next = strchr(Cur, ',');
178
179 if (Next) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000180 if (Cur != Next)
181 CCCClangArchs.insert(std::string(Cur, Next));
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000182 Cur = Next + 1;
183 } else {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000184 if (*Cur != '\0')
185 CCCClangArchs.insert(std::string(Cur));
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000186 break;
187 }
188 }
189
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000190 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000191 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000192 HostTriple = *++Start;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000193
Daniel Dunbarda641412009-09-04 18:35:03 +0000194 } else if (!strcmp(Opt, "install-dir")) {
195 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
196 Dir = *++Start;
197
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000198 } else {
199 // FIXME: Error handling.
200 llvm::errs() << "invalid option: " << *Start << "\n";
201 exit(1);
202 }
203 }
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000204
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000205 InputArgList *Args = ParseArgStrings(Start, End);
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000206
Daniel Dunbare5049522009-03-17 20:45:45 +0000207 Host = GetHostInfo(HostTriple);
Daniel Dunbarcb881672009-03-13 00:51:18 +0000208
Daniel Dunbar586dc232009-03-16 06:42:30 +0000209 // The compilation takes ownership of Args.
Daniel Dunbare530ad42009-03-18 22:16:03 +0000210 Compilation *C = new Compilation(*this, *Host->getToolChain(*Args), Args);
Daniel Dunbar21549232009-03-18 02:55:38 +0000211
212 // FIXME: This behavior shouldn't be here.
213 if (CCCPrintOptions) {
214 PrintOptions(C->getArgs());
215 return C;
216 }
217
218 if (!HandleImmediateArgs(*C))
219 return C;
220
221 // Construct the list of abstract actions to perform for this
222 // compilation. We avoid passing a Compilation here simply to
223 // enforce the abstraction that pipelining is not host or toolchain
224 // dependent (other than the driver driver test).
225 if (Host->useDriverDriver())
226 BuildUniversalActions(C->getArgs(), C->getActions());
227 else
228 BuildActions(C->getArgs(), C->getActions());
229
230 if (CCCPrintActions) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000231 PrintActions(*C);
Daniel Dunbar21549232009-03-18 02:55:38 +0000232 return C;
233 }
234
235 BuildJobs(*C);
Daniel Dunbar8d2554a2009-03-15 01:38:15 +0000236
237 return C;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000238}
239
Daniel Dunbarc88a88f2009-07-01 20:03:04 +0000240int Driver::ExecuteCompilation(const Compilation &C) const {
241 // Just print if -### was present.
242 if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
243 C.PrintJob(llvm::errs(), C.getJobs(), "\n", true);
244 return 0;
245 }
246
247 // If there were errors building the compilation, quit now.
248 if (getDiags().getNumErrors())
249 return 1;
250
251 const Command *FailingCommand = 0;
252 int Res = C.ExecuteJob(C.getJobs(), FailingCommand);
253
254 // Remove temp files.
255 C.CleanupFileList(C.getTempFiles());
256
257 // If the compilation failed, remove result files as well.
258 if (Res != 0 && !C.getArgs().hasArg(options::OPT_save_temps))
259 C.CleanupFileList(C.getResultFiles(), true);
260
261 // Print extra information about abnormal failures, if possible.
262 if (Res) {
263 // This is ad-hoc, but we don't want to be excessively noisy. If the result
264 // status was 1, assume the command failed normally. In particular, if it
265 // was the compiler then assume it gave a reasonable error code. Failures in
266 // other tools are less common, and they generally have worse diagnostics,
267 // so always print the diagnostic there.
268 const Action &Source = FailingCommand->getSource();
269 bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) ||
270 isa<PrecompileJobAction>(Source) ||
271 isa<AnalyzeJobAction>(Source) ||
272 isa<CompileJobAction>(Source));
273
274 if (!IsFriendlyTool || Res != 1) {
275 // FIXME: See FIXME above regarding result code interpretation.
276 if (Res < 0)
277 Diag(clang::diag::err_drv_command_signalled)
278 << Source.getClassName() << -Res;
279 else
280 Diag(clang::diag::err_drv_command_failed)
281 << Source.getClassName() << Res;
282 }
283 }
284
285 return Res;
286}
287
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000288void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar06482622009-03-05 06:38:47 +0000289 unsigned i = 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000290 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar06482622009-03-05 06:38:47 +0000291 it != ie; ++it, ++i) {
292 Arg *A = *it;
293 llvm::errs() << "Option " << i << " - "
294 << "Name: \"" << A->getOption().getName() << "\", "
295 << "Values: {";
296 for (unsigned j = 0; j < A->getNumValues(); ++j) {
297 if (j)
298 llvm::errs() << ", ";
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000299 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar06482622009-03-05 06:38:47 +0000300 }
301 llvm::errs() << "}\n";
Daniel Dunbar06482622009-03-05 06:38:47 +0000302 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000303}
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000304
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000305static std::string getOptionHelpName(const OptTable &Opts, options::ID Id) {
306 std::string Name = Opts.getOptionName(Id);
307
308 // Add metavar, if used.
309 switch (Opts.getOptionKind(Id)) {
310 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
311 assert(0 && "Invalid option with help text.");
312
313 case Option::MultiArgClass: case Option::JoinedAndSeparateClass:
314 assert(0 && "Cannot print metavar for this kind of option.");
315
316 case Option::FlagClass:
317 break;
318
319 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
320 Name += ' ';
321 // FALLTHROUGH
322 case Option::JoinedClass: case Option::CommaJoinedClass:
323 Name += Opts.getOptionMetaVar(Id);
324 break;
325 }
326
327 return Name;
328}
329
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000330void Driver::PrintHelp(bool ShowHidden) const {
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000331 llvm::raw_ostream &OS = llvm::outs();
332
333 OS << "OVERVIEW: clang \"gcc-compatible\" driver\n";
334 OS << '\n';
335 OS << "USAGE: " << Name << " [options] <input files>\n";
336 OS << '\n';
337 OS << "OPTIONS:\n";
338
339 // Render help text into (option, help) pairs.
340 std::vector< std::pair<std::string, const char*> > OptionHelp;
341
342 for (unsigned i = options::OPT_INPUT, e = options::LastOption; i != e; ++i) {
343 options::ID Id = (options::ID) i;
344 if (const char *Text = getOpts().getOptionHelpText(Id))
345 OptionHelp.push_back(std::make_pair(getOptionHelpName(getOpts(), Id),
346 Text));
347 }
348
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000349 if (ShowHidden) {
350 OptionHelp.push_back(std::make_pair("\nDRIVER OPTIONS:",""));
351 OptionHelp.push_back(std::make_pair("-ccc-cxx",
352 "Act as a C++ driver"));
353 OptionHelp.push_back(std::make_pair("-ccc-gcc-name",
354 "Name for native GCC compiler"));
355 OptionHelp.push_back(std::make_pair("-ccc-clang-cxx",
Daniel Dunbar5915fbf2009-09-01 16:57:46 +0000356 "Enable the clang compiler for C++"));
357 OptionHelp.push_back(std::make_pair("-ccc-no-clang-cxx",
358 "Disable the clang compiler for C++"));
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000359 OptionHelp.push_back(std::make_pair("-ccc-no-clang",
Daniel Dunbar5915fbf2009-09-01 16:57:46 +0000360 "Disable the clang compiler"));
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000361 OptionHelp.push_back(std::make_pair("-ccc-no-clang-cpp",
Daniel Dunbar5915fbf2009-09-01 16:57:46 +0000362 "Disable the clang preprocessor"));
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000363 OptionHelp.push_back(std::make_pair("-ccc-clang-archs",
364 "Comma separate list of architectures "
365 "to use the clang compiler for"));
Douglas Gregordf91ef32009-04-18 00:34:01 +0000366 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pch",
367 "Use lazy PCH for precompiled headers"));
368 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pth",
369 "Use pretokenized headers for precompiled headers"));
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000370
371 OptionHelp.push_back(std::make_pair("\nDEBUG/DEVELOPMENT OPTIONS:",""));
372 OptionHelp.push_back(std::make_pair("-ccc-host-triple",
Daniel Dunbarda641412009-09-04 18:35:03 +0000373 "Simulate running on the given target"));
374 OptionHelp.push_back(std::make_pair("-ccc-install-dir",
375 "Simulate installation in the given directory"));
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000376 OptionHelp.push_back(std::make_pair("-ccc-print-options",
377 "Dump parsed command line arguments"));
378 OptionHelp.push_back(std::make_pair("-ccc-print-phases",
379 "Dump list of actions to perform"));
380 OptionHelp.push_back(std::make_pair("-ccc-print-bindings",
381 "Show bindings of tools to actions"));
382 OptionHelp.push_back(std::make_pair("CCC_ADD_ARGS",
383 "(ENVIRONMENT VARIABLE) Comma separated list of "
384 "arguments to prepend to the command line"));
385 }
386
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000387 // Find the maximum option length.
388 unsigned OptionFieldWidth = 0;
389 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000390 // Skip titles.
391 if (!OptionHelp[i].second)
392 continue;
393
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000394 // Limit the amount of padding we are willing to give up for
395 // alignment.
396 unsigned Length = OptionHelp[i].first.size();
397 if (Length <= 23)
398 OptionFieldWidth = std::max(OptionFieldWidth, Length);
399 }
400
401 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
402 const std::string &Option = OptionHelp[i].first;
403 OS << " " << Option;
404 for (int j = Option.length(), e = OptionFieldWidth; j < e; ++j)
405 OS << ' ';
406 OS << ' ' << OptionHelp[i].second << '\n';
407 }
408
409 OS.flush();
410}
411
Daniel Dunbar79300722009-07-21 20:06:58 +0000412void Driver::PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const {
Mike Stump5d023c32009-03-18 14:00:02 +0000413 static char buf[] = "$URL$";
414 char *zap = strstr(buf, "/lib/Driver");
415 if (zap)
416 *zap = 0;
417 zap = strstr(buf, "/clang/tools/clang");
418 if (zap)
419 *zap = 0;
Mike Stumpe70295b2009-03-18 15:19:35 +0000420 const char *vers = buf+6;
Mike Stump8944c382009-03-18 18:45:55 +0000421 // FIXME: Add cmake support and remove #ifdef
422#ifdef SVN_REVISION
423 const char *revision = SVN_REVISION;
424#else
425 const char *revision = "";
426#endif
Daniel Dunbarcb881672009-03-13 00:51:18 +0000427 // FIXME: The following handlers should use a callback mechanism, we
428 // don't know what the client would like to do.
Daniel Dunbar79300722009-07-21 20:06:58 +0000429 OS << "clang version " CLANG_VERSION_STRING " ("
Daniel Dunbar3ee96ba2009-06-16 23:32:58 +0000430 << vers << " " << revision << ")" << '\n';
Daniel Dunbar70c8db12009-03-26 16:09:13 +0000431
432 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbar79300722009-07-21 20:06:58 +0000433 OS << "Target: " << TC.getTripleString() << '\n';
Daniel Dunbar3ee96ba2009-06-16 23:32:58 +0000434
435 // Print the threading model.
436 //
437 // FIXME: Implement correctly.
Daniel Dunbar79300722009-07-21 20:06:58 +0000438 OS << "Thread model: " << "posix" << '\n';
Daniel Dunbarcb881672009-03-13 00:51:18 +0000439}
440
Daniel Dunbar21549232009-03-18 02:55:38 +0000441bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000442 // The order these options are handled in in gcc is all over the
443 // place, but we don't expect inconsistencies w.r.t. that to matter
444 // in practice.
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000445
Daniel Dunbare06dc212009-04-04 05:17:38 +0000446 if (C.getArgs().hasArg(options::OPT_dumpversion)) {
Douglas Gregorab41e632009-04-27 22:23:34 +0000447 llvm::outs() << CLANG_VERSION_STRING "\n";
Daniel Dunbare06dc212009-04-04 05:17:38 +0000448 return false;
449 }
450
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000451 if (C.getArgs().hasArg(options::OPT__help) ||
452 C.getArgs().hasArg(options::OPT__help_hidden)) {
453 PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden));
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000454 return false;
455 }
456
Daniel Dunbar6cc73de2009-04-02 15:05:41 +0000457 if (C.getArgs().hasArg(options::OPT__version)) {
Daniel Dunbar79300722009-07-21 20:06:58 +0000458 // Follow gcc behavior and use stdout for --version and stderr for -v
459 PrintVersion(C, llvm::outs());
Daniel Dunbar6cc73de2009-04-02 15:05:41 +0000460 return false;
461 }
462
Daniel Dunbar21549232009-03-18 02:55:38 +0000463 if (C.getArgs().hasArg(options::OPT_v) ||
464 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbar79300722009-07-21 20:06:58 +0000465 PrintVersion(C, llvm::errs());
Daniel Dunbarcb881672009-03-13 00:51:18 +0000466 SuppressMissingInputWarning = true;
467 }
468
Daniel Dunbar21549232009-03-18 02:55:38 +0000469 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbarca3459e2009-03-20 04:37:21 +0000470 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
471 llvm::outs() << "programs: =";
472 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
473 ie = TC.getProgramPaths().end(); it != ie; ++it) {
474 if (it != TC.getProgramPaths().begin())
475 llvm::outs() << ':';
476 llvm::outs() << *it;
477 }
478 llvm::outs() << "\n";
479 llvm::outs() << "libraries: =";
480 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
481 ie = TC.getFilePaths().end(); it != ie; ++it) {
482 if (it != TC.getFilePaths().begin())
483 llvm::outs() << ':';
484 llvm::outs() << *it;
485 }
486 llvm::outs() << "\n";
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000487 return false;
Daniel Dunbarca3459e2009-03-20 04:37:21 +0000488 }
489
Daniel Dunbarcb881672009-03-13 00:51:18 +0000490 // FIXME: The following handlers should use a callback mechanism, we
491 // don't know what the client would like to do.
Daniel Dunbar21549232009-03-18 02:55:38 +0000492 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000493 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).str()
Daniel Dunbar21549232009-03-18 02:55:38 +0000494 << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000495 return false;
496 }
497
Daniel Dunbar21549232009-03-18 02:55:38 +0000498 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000499 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).str()
Daniel Dunbar21549232009-03-18 02:55:38 +0000500 << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000501 return false;
502 }
503
Daniel Dunbar21549232009-03-18 02:55:38 +0000504 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000505 llvm::outs() << GetFilePath("libgcc.a", TC).str() << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000506 return false;
507 }
508
Daniel Dunbar12cfe032009-06-16 23:25:22 +0000509 if (C.getArgs().hasArg(options::OPT_print_multi_lib)) {
510 // FIXME: We need tool chain support for this.
511 llvm::outs() << ".;\n";
512
513 switch (C.getDefaultToolChain().getTriple().getArch()) {
514 default:
515 break;
516
517 case llvm::Triple::x86_64:
518 llvm::outs() << "x86_64;@m64" << "\n";
519 break;
520
521 case llvm::Triple::ppc64:
522 llvm::outs() << "ppc64;@m64" << "\n";
523 break;
524 }
525 return false;
526 }
527
528 // FIXME: What is the difference between print-multi-directory and
529 // print-multi-os-directory?
530 if (C.getArgs().hasArg(options::OPT_print_multi_directory) ||
531 C.getArgs().hasArg(options::OPT_print_multi_os_directory)) {
532 switch (C.getDefaultToolChain().getTriple().getArch()) {
533 default:
534 case llvm::Triple::x86:
535 case llvm::Triple::ppc:
536 llvm::outs() << "." << "\n";
537 break;
538
539 case llvm::Triple::x86_64:
540 llvm::outs() << "x86_64" << "\n";
541 break;
542
543 case llvm::Triple::ppc64:
544 llvm::outs() << "ppc64" << "\n";
545 break;
546 }
547 return false;
548 }
549
Daniel Dunbarcb881672009-03-13 00:51:18 +0000550 return true;
551}
552
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000553static unsigned PrintActions1(const Compilation &C,
Daniel Dunbarba102132009-03-13 12:19:02 +0000554 Action *A,
555 std::map<Action*, unsigned> &Ids) {
556 if (Ids.count(A))
557 return Ids[A];
558
559 std::string str;
560 llvm::raw_string_ostream os(str);
561
562 os << Action::getClassName(A->getKind()) << ", ";
563 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000564 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbarba102132009-03-13 12:19:02 +0000565 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000566 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
567 C.getDefaultToolChain().getArchName()) << '"'
568 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbarba102132009-03-13 12:19:02 +0000569 } else {
570 os << "{";
571 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000572 os << PrintActions1(C, *it, Ids);
Daniel Dunbarba102132009-03-13 12:19:02 +0000573 ++it;
574 if (it != ie)
575 os << ", ";
576 }
577 os << "}";
578 }
579
580 unsigned Id = Ids.size();
581 Ids[A] = Id;
Daniel Dunbarb269c322009-03-13 17:20:20 +0000582 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbarba102132009-03-13 12:19:02 +0000583 << types::getTypeName(A->getType()) << "\n";
584
585 return Id;
586}
587
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000588void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbarba102132009-03-13 12:19:02 +0000589 std::map<Action*, unsigned> Ids;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000590 for (ActionList::const_iterator it = C.getActions().begin(),
591 ie = C.getActions().end(); it != ie; ++it)
592 PrintActions1(C, *it, Ids);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000593}
594
Daniel Dunbar21549232009-03-18 02:55:38 +0000595void Driver::BuildUniversalActions(const ArgList &Args,
596 ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000597 llvm::PrettyStackTraceString CrashInfo("Building actions for universal build");
Daniel Dunbar13689542009-03-13 20:33:35 +0000598 // Collect the list of architectures. Duplicates are allowed, but
599 // should only be handled once (in the order seen).
600 llvm::StringSet<> ArchNames;
601 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000602 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
603 it != ie; ++it) {
604 Arg *A = *it;
605
606 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbar13689542009-03-13 20:33:35 +0000607 const char *Name = A->getValue(Args);
608
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000609 // FIXME: We need to handle canonicalization of the specified
610 // arch?
611
Daniel Dunbar75877192009-03-19 07:55:12 +0000612 A->claim();
Daniel Dunbar13689542009-03-13 20:33:35 +0000613 if (ArchNames.insert(Name))
614 Archs.push_back(Name);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000615 }
616 }
617
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000618 // When there is no explicit arch for this platform, make sure we
619 // still bind the architecture (to the default) so that -Xarch_ is
620 // handled correctly.
621 if (!Archs.size())
622 Archs.push_back(0);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000623
624 // FIXME: We killed off some others but these aren't yet detected in
625 // a functional manner. If we added information to jobs about which
626 // "auxiliary" files they wrote then we could detect the conflict
627 // these cause downstream.
628 if (Archs.size() > 1) {
629 // No recovery needed, the point of this is just to prevent
630 // overwriting the same files.
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000631 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
632 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000633 << A->getAsString(Args);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000634 }
635
636 ActionList SingleActions;
637 BuildActions(Args, SingleActions);
638
639 // Add in arch binding and lipo (if necessary) for every top level
640 // action.
641 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
642 Action *Act = SingleActions[i];
643
644 // Make sure we can lipo this kind of output. If not (and it is an
645 // actual output) then we disallow, since we can't create an
646 // output file with the right name without overwriting it. We
647 // could remove this oddity by just changing the output names to
648 // include the arch, which would also fix
649 // -save-temps. Compatibility wins for now.
650
Daniel Dunbar3dbd6c52009-03-13 17:46:02 +0000651 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000652 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
653 << types::getTypeName(Act->getType());
654
655 ActionList Inputs;
Daniel Dunbar75877192009-03-19 07:55:12 +0000656 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbar13689542009-03-13 20:33:35 +0000657 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000658
659 // Lipo if necessary, We do it this way because we need to set the
660 // arch flag so that -Xarch_ gets overwritten.
661 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
662 Actions.append(Inputs.begin(), Inputs.end());
663 else
664 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
665 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000666}
667
Daniel Dunbar21549232009-03-18 02:55:38 +0000668void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000669 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000670 // Start by constructing the list of inputs and their types.
671
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000672 // Track the current user specified (-x) input. We also explicitly
673 // track the argument used to set the type; we only want to claim
674 // the type when we actually use it, so we warn about unused -x
675 // arguments.
676 types::ID InputType = types::TY_Nothing;
677 Arg *InputTypeArg = 0;
678
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000679 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
680 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
681 it != ie; ++it) {
682 Arg *A = *it;
683
684 if (isa<InputOption>(A->getOption())) {
685 const char *Value = A->getValue(Args);
686 types::ID Ty = types::TY_INVALID;
687
688 // Infer the input type if necessary.
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000689 if (InputType == types::TY_Nothing) {
690 // If there was an explicit arg for this, claim it.
691 if (InputTypeArg)
692 InputTypeArg->claim();
693
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000694 // stdin must be handled specially.
695 if (memcmp(Value, "-", 2) == 0) {
696 // If running with -E, treat as a C input (this changes the
697 // builtin macros, for example). This may be overridden by
698 // -ObjC below.
699 //
700 // Otherwise emit an error but still use a valid type to
701 // avoid spurious errors (e.g., no inputs).
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000702 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000703 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000704 Ty = types::TY_C;
705 } else {
706 // Otherwise lookup by extension, and fallback to ObjectType
Daniel Dunbare33bea42009-03-20 23:39:23 +0000707 // if not found. We use a host hook here because Darwin at
708 // least has its own idea of what .s is.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000709 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbare33bea42009-03-20 23:39:23 +0000710 Ty = Host->lookupTypeForExtension(Ext + 1);
711
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000712 if (Ty == types::TY_INVALID)
713 Ty = types::TY_Object;
714 }
715
Daniel Dunbar683ca382009-05-18 21:47:54 +0000716 // -ObjC and -ObjC++ override the default language, but only for "source
717 // files". We just treat everything that isn't a linker input as a
718 // source file.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000719 //
Daniel Dunbar683ca382009-05-18 21:47:54 +0000720 // FIXME: Clean this up if we move the phase sequence into the type.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000721 if (Ty != types::TY_Object) {
722 if (Args.hasArg(options::OPT_ObjC))
723 Ty = types::TY_ObjC;
724 else if (Args.hasArg(options::OPT_ObjCXX))
725 Ty = types::TY_ObjCXX;
726 }
727 } else {
728 assert(InputTypeArg && "InputType set w/o InputTypeArg");
729 InputTypeArg->claim();
730 Ty = InputType;
731 }
732
733 // Check that the file exists. It isn't clear this is worth
734 // doing, since the tool presumably does this anyway, and this
735 // just adds an extra stat to the equation, but this is gcc
736 // compatible.
737 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000738 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000739 else
740 Inputs.push_back(std::make_pair(Ty, A));
741
742 } else if (A->getOption().isLinkerInput()) {
743 // Just treat as object type, we could make a special type for
744 // this if necessary.
745 Inputs.push_back(std::make_pair(types::TY_Object, A));
746
747 } else if (A->getOption().getId() == options::OPT_x) {
748 InputTypeArg = A;
749 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
750
751 // Follow gcc behavior and treat as linker input for invalid -x
752 // options. Its not clear why we shouldn't just revert to
753 // unknown; but this isn't very important, we might as well be
754 // bug comatible.
755 if (!InputType) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000756 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000757 InputType = types::TY_Object;
758 }
759 }
760 }
761
Daniel Dunbar8b1604e2009-03-13 00:17:48 +0000762 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000763 Diag(clang::diag::err_drv_no_input_files);
764 return;
765 }
766
767 // Determine which compilation mode we are in. We look for options
768 // which affect the phase, starting with the earliest phases, and
769 // record which option we used to determine the final phase.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000770 Arg *FinalPhaseArg = 0;
771 phases::ID FinalPhase;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000772
773 // -{E,M,MM} only run the preprocessor.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000774 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
775 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
776 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
777 FinalPhase = phases::Preprocess;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000778
Daniel Dunbar5915fbf2009-09-01 16:57:46 +0000779 // -{fsyntax-only,-analyze,emit-ast,S} only run up to the compiler.
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000780 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
Daniel Dunbar63be57a2009-05-06 02:12:32 +0000781 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze,
782 options::OPT__analyze_auto)) ||
Daniel Dunbar5915fbf2009-09-01 16:57:46 +0000783 (FinalPhaseArg = Args.getLastArg(options::OPT_emit_ast)) ||
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000784 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
785 FinalPhase = phases::Compile;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000786
787 // -c only runs up to the assembler.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000788 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
789 FinalPhase = phases::Assemble;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000790
791 // Otherwise do everything.
792 } else
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000793 FinalPhase = phases::Link;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000794
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000795 // Reject -Z* at the top level, these options should never have been
796 // exposed by gcc.
Daniel Dunbard7b88c22009-03-26 16:12:09 +0000797 if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000798 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000799
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000800 // Construct the actions to perform.
801 ActionList LinkerInputs;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000802 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000803 types::ID InputType = Inputs[i].first;
804 const Arg *InputArg = Inputs[i].second;
805
806 unsigned NumSteps = types::getNumCompilationPhases(InputType);
807 assert(NumSteps && "Invalid number of steps!");
808
809 // If the first step comes after the final phase we are doing as
810 // part of this compilation, warn the user about it.
811 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
812 if (InitialPhase > FinalPhase) {
Daniel Dunbar05494a72009-03-19 07:57:08 +0000813 // Claim here to avoid the more general unused warning.
814 InputArg->claim();
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000815 Diag(clang::diag::warn_drv_input_file_unused)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000816 << InputArg->getAsString(Args)
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000817 << getPhaseName(InitialPhase)
818 << FinalPhaseArg->getOption().getName();
819 continue;
820 }
821
822 // Build the pipeline for this file.
823 Action *Current = new InputAction(*InputArg, InputType);
824 for (unsigned i = 0; i != NumSteps; ++i) {
825 phases::ID Phase = types::getCompilationPhase(InputType, i);
826
827 // We are done if this step is past what the user requested.
828 if (Phase > FinalPhase)
829 break;
830
831 // Queue linker inputs.
832 if (Phase == phases::Link) {
833 assert(i + 1 == NumSteps && "linking must be final compilation step.");
834 LinkerInputs.push_back(Current);
835 Current = 0;
836 break;
837 }
838
Daniel Dunbar337a6272009-03-24 20:17:30 +0000839 // Some types skip the assembler phase (e.g., llvm-bc), but we
840 // can't encode this in the steps because the intermediate type
841 // depends on arguments. Just special case here.
842 if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
843 continue;
844
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000845 // Otherwise construct the appropriate action.
846 Current = ConstructPhaseAction(Args, Phase, Current);
847 if (Current->getType() == types::TY_Nothing)
848 break;
849 }
850
851 // If we ended with something, add to the output list.
852 if (Current)
853 Actions.push_back(Current);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000854 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000855
856 // Add a link action if necessary.
857 if (!LinkerInputs.empty())
858 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
859}
860
861Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
862 Action *Input) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000863 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000864 // Build the appropriate action.
865 switch (Phase) {
866 case phases::Link: assert(0 && "link action invalid here.");
867 case phases::Preprocess: {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +0000868 types::ID OutputTy;
869 // -{M, MM} alter the output type.
870 if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) {
871 OutputTy = types::TY_Dependencies;
872 } else {
873 OutputTy = types::getPreprocessedType(Input->getType());
874 assert(OutputTy != types::TY_INVALID &&
875 "Cannot preprocess this input type!");
876 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000877 return new PreprocessJobAction(Input, OutputTy);
878 }
879 case phases::Precompile:
880 return new PrecompileJobAction(Input, types::TY_PCH);
881 case phases::Compile: {
882 if (Args.hasArg(options::OPT_fsyntax_only)) {
883 return new CompileJobAction(Input, types::TY_Nothing);
Daniel Dunbar63be57a2009-05-06 02:12:32 +0000884 } else if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000885 return new AnalyzeJobAction(Input, types::TY_Plist);
Daniel Dunbar5915fbf2009-09-01 16:57:46 +0000886 } else if (Args.hasArg(options::OPT_emit_ast)) {
887 return new CompileJobAction(Input, types::TY_AST);
Daniel Dunbar337a6272009-03-24 20:17:30 +0000888 } else if (Args.hasArg(options::OPT_emit_llvm) ||
889 Args.hasArg(options::OPT_flto) ||
890 Args.hasArg(options::OPT_O4)) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000891 types::ID Output =
892 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
893 return new CompileJobAction(Input, Output);
894 } else {
895 return new CompileJobAction(Input, types::TY_PP_Asm);
896 }
897 }
898 case phases::Assemble:
899 return new AssembleJobAction(Input, types::TY_Object);
900 }
901
902 assert(0 && "invalid phase in ConstructPhaseAction");
903 return 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000904}
905
Daniel Dunbar21549232009-03-18 02:55:38 +0000906void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000907 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000908 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
909 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000910
911 // FIXME: Pipes are forcibly disabled until we support executing
912 // them.
913 if (!CCCPrintBindings)
914 UsePipes = false;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000915
916 // -save-temps inhibits pipes.
917 if (SaveTemps && UsePipes) {
918 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
919 UsePipes = true;
920 }
921
922 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
923
924 // It is an error to provide a -o option if we are making multiple
925 // output files.
926 if (FinalOutput) {
927 unsigned NumOutputs = 0;
Daniel Dunbar21549232009-03-18 02:55:38 +0000928 for (ActionList::const_iterator it = C.getActions().begin(),
929 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000930 if ((*it)->getType() != types::TY_Nothing)
931 ++NumOutputs;
932
933 if (NumOutputs > 1) {
934 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
935 FinalOutput = 0;
936 }
937 }
938
Daniel Dunbar21549232009-03-18 02:55:38 +0000939 for (ActionList::const_iterator it = C.getActions().begin(),
940 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000941 Action *A = *it;
942
943 // If we are linking an image for multiple archs then the linker
944 // wants -arch_multiple and -final_output <final image
945 // name>. Unfortunately, this doesn't fit in cleanly because we
946 // have to pass this information down.
947 //
948 // FIXME: This is a hack; find a cleaner way to integrate this
949 // into the process.
950 const char *LinkingOutput = 0;
Daniel Dunbard7b88c22009-03-26 16:12:09 +0000951 if (isa<LipoJobAction>(A)) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000952 if (FinalOutput)
953 LinkingOutput = FinalOutput->getValue(C.getArgs());
954 else
955 LinkingOutput = DefaultImageName.c_str();
956 }
957
958 InputInfo II;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000959 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000960 /*CanAcceptPipe*/ true,
961 /*AtTopLevel*/ true,
962 /*LinkingOutput*/ LinkingOutput,
963 II);
964 }
Daniel Dunbar586dc232009-03-16 06:42:30 +0000965
Daniel Dunbarbf4a6762009-04-03 22:09:23 +0000966 // If the user passed -Qunused-arguments or there were errors, don't
967 // warn about any unused arguments.
Daniel Dunbar1e23f5f2009-04-07 19:04:18 +0000968 if (Diags.getNumErrors() ||
969 C.getArgs().hasArg(options::OPT_Qunused_arguments))
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000970 return;
971
Daniel Dunbara2094e72009-03-29 22:24:54 +0000972 // Claim -### here.
973 (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
974
Daniel Dunbar586dc232009-03-16 06:42:30 +0000975 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
976 it != ie; ++it) {
977 Arg *A = *it;
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000978
Daniel Dunbar586dc232009-03-16 06:42:30 +0000979 // FIXME: It would be nice to be able to send the argument to the
980 // Diagnostic, so that extra values, position, and so on could be
981 // printed.
Daniel Dunbar4f53b292009-04-04 00:52:26 +0000982 if (!A->isClaimed()) {
Daniel Dunbar1e23f5f2009-04-07 19:04:18 +0000983 if (A->getOption().hasNoArgumentUnused())
984 continue;
985
Daniel Dunbar4f53b292009-04-04 00:52:26 +0000986 // Suppress the warning automatically if this is just a flag,
987 // and it is an instance of an argument we already claimed.
988 const Option &Opt = A->getOption();
989 if (isa<FlagOption>(Opt)) {
990 bool DuplicateClaimed = false;
991
992 // FIXME: Use iterator.
993 for (ArgList::const_iterator it = C.getArgs().begin(),
994 ie = C.getArgs().end(); it != ie; ++it) {
995 if ((*it)->isClaimed() && (*it)->getOption().matches(Opt.getId())) {
996 DuplicateClaimed = true;
997 break;
998 }
999 }
1000
1001 if (DuplicateClaimed)
1002 continue;
1003 }
1004
Daniel Dunbar586dc232009-03-16 06:42:30 +00001005 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +00001006 << A->getAsString(C.getArgs());
Daniel Dunbar4f53b292009-04-04 00:52:26 +00001007 }
Daniel Dunbar586dc232009-03-16 06:42:30 +00001008 }
Daniel Dunbar57b704d2009-03-13 22:12:33 +00001009}
1010
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001011void Driver::BuildJobsForAction(Compilation &C,
1012 const Action *A,
1013 const ToolChain *TC,
1014 bool CanAcceptPipe,
1015 bool AtTopLevel,
1016 const char *LinkingOutput,
1017 InputInfo &Result) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +00001018 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action");
Daniel Dunbar60ccc762009-03-18 23:18:19 +00001019
1020 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
1021 // FIXME: Pipes are forcibly disabled until we support executing
1022 // them.
1023 if (!CCCPrintBindings)
1024 UsePipes = false;
1025
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001026 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar115a7922009-03-19 07:29:38 +00001027 // FIXME: It would be nice to not claim this here; maybe the old
1028 // scheme of just using Args was better?
1029 const Arg &Input = IA->getInputArg();
1030 Input.claim();
1031 if (isa<PositionalArg>(Input)) {
1032 const char *Name = Input.getValue(C.getArgs());
1033 Result = InputInfo(Name, A->getType(), Name);
1034 } else
1035 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001036 return;
1037 }
1038
1039 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
1040 const char *ArchName = BAA->getArchName();
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001041 std::string Arch;
1042 if (!ArchName) {
1043 Arch = C.getDefaultToolChain().getArchName();
1044 ArchName = Arch.c_str();
1045 }
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001046 BuildJobsForAction(C,
1047 *BAA->begin(),
1048 Host->getToolChain(C.getArgs(), ArchName),
1049 CanAcceptPipe,
1050 AtTopLevel,
1051 LinkingOutput,
1052 Result);
1053 return;
1054 }
1055
1056 const JobAction *JA = cast<JobAction>(A);
1057 const Tool &T = TC->SelectTool(C, *JA);
1058
1059 // See if we should use an integrated preprocessor. We do so when we
1060 // have exactly one input, since this is the only use case we care
1061 // about (irrelevant since we don't support combine yet).
1062 bool UseIntegratedCPP = false;
1063 const ActionList *Inputs = &A->getInputs();
1064 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
1065 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
1066 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
1067 !C.getArgs().hasArg(options::OPT_save_temps) &&
1068 T.hasIntegratedCPP()) {
1069 UseIntegratedCPP = true;
1070 Inputs = &(*Inputs)[0]->getInputs();
1071 }
1072 }
1073
1074 // Only use pipes when there is exactly one input.
1075 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001076 InputInfoList InputInfos;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001077 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
1078 it != ie; ++it) {
1079 InputInfo II;
1080 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
1081 /*AtTopLevel*/false,
1082 LinkingOutput,
1083 II);
1084 InputInfos.push_back(II);
1085 }
1086
1087 // Determine if we should output to a pipe.
1088 bool OutputToPipe = false;
1089 if (CanAcceptPipe && T.canPipeOutput()) {
1090 // Some actions default to writing to a pipe if they are the top
1091 // level phase and there was no user override.
1092 //
1093 // FIXME: Is there a better way to handle this?
1094 if (AtTopLevel) {
1095 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
1096 OutputToPipe = true;
Daniel Dunbar60ccc762009-03-18 23:18:19 +00001097 } else if (UsePipes)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001098 OutputToPipe = true;
1099 }
1100
1101 // Figure out where to put the job (pipes).
1102 Job *Dest = &C.getJobs();
1103 if (InputInfos[0].isPipe()) {
Daniel Dunbar441d0602009-03-17 17:53:55 +00001104 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001105 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
1106 Dest = &InputInfos[0].getPipe();
1107 }
1108
1109 // Always use the first input as the base input.
1110 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar441d0602009-03-17 17:53:55 +00001111
1112 // Determine the place to write output to (nothing, pipe, or
1113 // filename) and where to put the new job.
Daniel Dunbar441d0602009-03-17 17:53:55 +00001114 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001115 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001116 } else if (OutputToPipe) {
1117 // Append to current piped job or create a new one as appropriate.
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001118 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
1119 if (!PJ) {
1120 PJ = new PipedJob();
Daniel Dunbarb7b61b22009-03-20 00:11:04 +00001121 // FIXME: Temporary hack so that -ccc-print-bindings work until
1122 // we have pipe support. Please remove later.
1123 if (!CCCPrintBindings)
1124 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar871adcf2009-03-18 07:06:02 +00001125 Dest = PJ;
Daniel Dunbar441d0602009-03-17 17:53:55 +00001126 }
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001127 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001128 } else {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001129 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
1130 A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001131 }
1132
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001133 if (CCCPrintBindings) {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00001134 llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"'
1135 << " - \"" << T.getName() << "\", inputs: [";
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001136 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
1137 llvm::errs() << InputInfos[i].getAsString();
1138 if (i + 1 != e)
1139 llvm::errs() << ", ";
1140 }
1141 llvm::errs() << "], output: " << Result.getAsString() << "\n";
1142 } else {
Daniel Dunbarf3cad362009-03-25 04:13:45 +00001143 T.ConstructJob(C, *JA, *Dest, Result, InputInfos,
1144 C.getArgsForToolChain(TC), LinkingOutput);
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001145 }
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001146}
1147
Daniel Dunbar441d0602009-03-17 17:53:55 +00001148const char *Driver::GetNamedOutputPath(Compilation &C,
1149 const JobAction &JA,
1150 const char *BaseInput,
1151 bool AtTopLevel) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +00001152 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar441d0602009-03-17 17:53:55 +00001153 // Output to a user requested destination?
1154 if (AtTopLevel) {
1155 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
1156 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
1157 }
1158
1159 // Output to a temporary file?
1160 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbar214399e2009-03-18 19:34:39 +00001161 std::string TmpName =
1162 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
1163 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar441d0602009-03-17 17:53:55 +00001164 }
1165
1166 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbar5796bf42009-03-18 02:00:31 +00001167 std::string BaseName(BasePath.getLast());
Daniel Dunbar441d0602009-03-17 17:53:55 +00001168
1169 // Determine what the derived output name should be.
1170 const char *NamedOutput;
1171 if (JA.getType() == types::TY_Image) {
1172 NamedOutput = DefaultImageName.c_str();
1173 } else {
1174 const char *Suffix = types::getTypeTempSuffix(JA.getType());
1175 assert(Suffix && "All types used for output should have a suffix.");
1176
1177 std::string::size_type End = std::string::npos;
1178 if (!types::appendSuffixForType(JA.getType()))
1179 End = BaseName.rfind('.');
1180 std::string Suffixed(BaseName.substr(0, End));
1181 Suffixed += '.';
1182 Suffixed += Suffix;
1183 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
1184 }
1185
1186 // As an annoying special case, PCH generation doesn't strip the
1187 // pathname.
1188 if (JA.getType() == types::TY_PCH) {
1189 BasePath.eraseComponent();
Daniel Dunbar56c55942009-03-18 09:58:30 +00001190 if (BasePath.isEmpty())
1191 BasePath = NamedOutput;
1192 else
1193 BasePath.appendComponent(NamedOutput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001194 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
1195 } else {
1196 return C.addResultFile(NamedOutput);
1197 }
1198}
1199
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +00001200llvm::sys::Path Driver::GetFilePath(const char *Name,
Daniel Dunbar21549232009-03-18 02:55:38 +00001201 const ToolChain &TC) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001202 const ToolChain::path_list &List = TC.getFilePaths();
1203 for (ToolChain::path_list::const_iterator
1204 it = List.begin(), ie = List.end(); it != ie; ++it) {
1205 llvm::sys::Path P(*it);
1206 P.appendComponent(Name);
1207 if (P.exists())
1208 return P;
1209 }
1210
Daniel Dunbarcb881672009-03-13 00:51:18 +00001211 return llvm::sys::Path(Name);
1212}
1213
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +00001214llvm::sys::Path Driver::GetProgramPath(const char *Name,
Mike Stump950bedd2009-03-27 00:40:20 +00001215 const ToolChain &TC,
1216 bool WantFile) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001217 const ToolChain::path_list &List = TC.getProgramPaths();
1218 for (ToolChain::path_list::const_iterator
1219 it = List.begin(), ie = List.end(); it != ie; ++it) {
1220 llvm::sys::Path P(*it);
1221 P.appendComponent(Name);
Mike Stump950bedd2009-03-27 00:40:20 +00001222 if (WantFile ? P.exists() : P.canExecute())
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001223 return P;
1224 }
1225
Daniel Dunbarc50b00d2009-03-23 16:15:50 +00001226 // If all else failed, search the path.
1227 llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
Daniel Dunbar632f50e2009-03-18 21:34:08 +00001228 if (!P.empty())
1229 return P;
1230
Daniel Dunbarcb881672009-03-13 00:51:18 +00001231 return llvm::sys::Path(Name);
1232}
1233
Daniel Dunbar214399e2009-03-18 19:34:39 +00001234std::string Driver::GetTemporaryPath(const char *Suffix) const {
1235 // FIXME: This is lame; sys::Path should provide this function (in
1236 // particular, it should know how to find the temporary files dir).
1237 std::string Error;
Daniel Dunbarb03417f2009-04-20 20:28:21 +00001238 const char *TmpDir = ::getenv("TMPDIR");
1239 if (!TmpDir)
1240 TmpDir = ::getenv("TEMP");
1241 if (!TmpDir)
Daniel Dunbar3ca7ee92009-04-21 00:25:10 +00001242 TmpDir = ::getenv("TMP");
1243 if (!TmpDir)
Daniel Dunbarb03417f2009-04-20 20:28:21 +00001244 TmpDir = "/tmp";
1245 llvm::sys::Path P(TmpDir);
Daniel Dunbarf60c63a2009-04-20 17:32:49 +00001246 P.appendComponent("cc");
Daniel Dunbar214399e2009-03-18 19:34:39 +00001247 if (P.makeUnique(false, &Error)) {
1248 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
1249 return "";
1250 }
1251
Daniel Dunbar84603bc2009-03-18 23:08:52 +00001252 // FIXME: Grumble, makeUnique sometimes leaves the file around!?
1253 // PR3837.
1254 P.eraseFromDisk(false, 0);
1255
Daniel Dunbar214399e2009-03-18 19:34:39 +00001256 P.appendSuffix(Suffix);
Chris Lattnerd57a7ef2009-08-23 22:45:33 +00001257 return P.str();
Daniel Dunbar214399e2009-03-18 19:34:39 +00001258}
1259
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001260const HostInfo *Driver::GetHostInfo(const char *TripleStr) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +00001261 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001262 llvm::Triple Triple(TripleStr);
Daniel Dunbardd98e2c2009-03-10 23:41:59 +00001263
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001264 switch (Triple.getOS()) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001265 case llvm::Triple::AuroraUX:
1266 return createAuroraUXHostInfo(*this, Triple);
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001267 case llvm::Triple::Darwin:
1268 return createDarwinHostInfo(*this, Triple);
1269 case llvm::Triple::DragonFly:
1270 return createDragonFlyHostInfo(*this, Triple);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001271 case llvm::Triple::OpenBSD:
1272 return createOpenBSDHostInfo(*this, Triple);
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001273 case llvm::Triple::FreeBSD:
1274 return createFreeBSDHostInfo(*this, Triple);
Eli Friedman6b3454a2009-05-26 07:52:18 +00001275 case llvm::Triple::Linux:
1276 return createLinuxHostInfo(*this, Triple);
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001277 default:
1278 return createUnknownHostInfo(*this, Triple);
1279 }
Daniel Dunbardd98e2c2009-03-10 23:41:59 +00001280}
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001281
1282bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
Daniel Dunbarbf54a062009-04-01 20:33:11 +00001283 const std::string &ArchNameStr) const {
1284 // FIXME: Remove this hack.
1285 const char *ArchName = ArchNameStr.c_str();
1286 if (ArchNameStr == "powerpc")
1287 ArchName = "ppc";
1288 else if (ArchNameStr == "powerpc64")
1289 ArchName = "ppc64";
1290
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001291 // Check if user requested no clang, or clang doesn't understand
1292 // this type (we only handle single inputs for now).
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00001293 if (!CCCUseClang || JA.size() != 1 ||
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001294 !types::isAcceptedByClang((*JA.begin())->getType()))
1295 return false;
1296
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001297 // Otherwise make sure this is an action clang understands.
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001298 if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbar6256d362009-03-24 19:14:56 +00001299 if (!CCCUseClangCPP) {
1300 Diag(clang::diag::warn_drv_not_using_clang_cpp);
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001301 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001302 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001303 } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
1304 return false;
1305
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001306 // Use clang for C++?
Daniel Dunbar6256d362009-03-24 19:14:56 +00001307 if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
1308 Diag(clang::diag::warn_drv_not_using_clang_cxx);
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001309 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001310 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001311
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00001312 // Always use clang for precompiling and AST generation, regardless of
1313 // archs.
1314 if (isa<PrecompileJobAction>(JA) || JA.getType() == types::TY_AST)
Daniel Dunbarfec26bd2009-04-16 23:10:13 +00001315 return true;
1316
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001317 // Finally, don't use clang if this isn't one of the user specified
1318 // archs to build.
Daniel Dunbar6256d362009-03-24 19:14:56 +00001319 if (!CCCClangArchs.empty() && !CCCClangArchs.count(ArchName)) {
1320 Diag(clang::diag::warn_drv_not_using_clang_arch) << ArchName;
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001321 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001322 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001323
1324 return true;
1325}
Daniel Dunbard73fe9b2009-03-26 15:58:36 +00001326
1327/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and
1328/// return the grouped values as integers. Numbers which are not
1329/// provided are set to 0.
1330///
1331/// \return True if the entire string was parsed (9.2), or all groups
1332/// were parsed (10.3.5extrastuff).
1333bool Driver::GetReleaseVersion(const char *Str, unsigned &Major,
1334 unsigned &Minor, unsigned &Micro,
1335 bool &HadExtra) {
1336 HadExtra = false;
1337
1338 Major = Minor = Micro = 0;
1339 if (*Str == '\0')
1340 return true;
1341
1342 char *End;
1343 Major = (unsigned) strtol(Str, &End, 10);
1344 if (*Str != '\0' && *End == '\0')
1345 return true;
1346 if (*End != '.')
1347 return false;
1348
1349 Str = End+1;
1350 Minor = (unsigned) strtol(Str, &End, 10);
1351 if (*Str != '\0' && *End == '\0')
1352 return true;
1353 if (*End != '.')
1354 return false;
1355
1356 Str = End+1;
1357 Micro = (unsigned) strtol(Str, &End, 10);
1358 if (*Str != '\0' && *End == '\0')
1359 return true;
1360 if (Str == End)
1361 return false;
1362 HadExtra = true;
1363 return true;
1364}