blob: c4136df56bf78c6f054dbe025409a973232ee798 [file] [log] [blame]
Daniel Dunbar63c4da92009-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 Dunbar63c4da92009-03-02 19:59:07 +000010#include "clang/Driver/Driver.h"
Daniel Dunbar63c4da92009-03-02 19:59:07 +000011
Daniel Dunbardb62cc32009-03-12 07:58:46 +000012#include "clang/Driver/Action.h"
Daniel Dunbard6f0e372009-03-04 20:49:20 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
15#include "clang/Driver/Compilation.h"
Daniel Dunbar93468492009-03-12 08:55:43 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbard25acaa2009-03-10 23:41:59 +000017#include "clang/Driver/HostInfo.h"
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000018#include "clang/Driver/Job.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000019#include "clang/Driver/Option.h"
Daniel Dunbard6f0e372009-03-04 20:49:20 +000020#include "clang/Driver/Options.h"
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000021#include "clang/Driver/Tool.h"
22#include "clang/Driver/ToolChain.h"
Daniel Dunbardb62cc32009-03-12 07:58:46 +000023#include "clang/Driver/Types.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000024
Douglas Gregorb7064742009-04-27 22:23:34 +000025#include "clang/Basic/Version.h"
26
Daniel Dunbarb1873cd2009-03-13 20:33:35 +000027#include "llvm/ADT/StringSet.h"
Daniel Dunbar16e04ff2009-03-18 01:38:48 +000028#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000029#include "llvm/Support/raw_ostream.h"
Daniel Dunbardb62cc32009-03-12 07:58:46 +000030#include "llvm/System/Path.h"
Daniel Dunbarcb84b9a2009-03-18 21:34:08 +000031#include "llvm/System/Program.h"
Daniel Dunbar494646b2009-03-13 12:19:02 +000032
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000033#include "InputInfo.h"
34
Daniel Dunbar494646b2009-03-13 12:19:02 +000035#include <map>
36
Daniel Dunbard6f0e372009-03-04 20:49:20 +000037using namespace clang::driver;
Chris Lattnerc272d262009-03-26 05:56:24 +000038using namespace clang;
Daniel Dunbard6f0e372009-03-04 20:49:20 +000039
Daniel Dunbard25acaa2009-03-10 23:41:59 +000040Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar93468492009-03-12 08:55:43 +000041 const char *_DefaultHostTriple,
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000042 const char *_DefaultImageName,
Daniel Dunbar93468492009-03-12 08:55:43 +000043 Diagnostic &_Diags)
44 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbard25acaa2009-03-10 23:41:59 +000045 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000046 DefaultImageName(_DefaultImageName),
Daniel Dunbard25acaa2009-03-10 23:41:59 +000047 Host(0),
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +000048 CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false),
Daniel Dunbar126da5e2009-04-01 23:34:41 +000049 CCCGenericGCCName("gcc"), CCCUseClang(true), CCCUseClangCXX(false),
Douglas Gregorf93696d2009-04-28 22:44:02 +000050 CCCUseClangCPP(true), CCCUsePCH(true),
Daniel Dunbar5a5ec5c2009-03-13 00:17:48 +000051 SuppressMissingInputWarning(false)
Daniel Dunbarb282ced2009-03-10 20:52:46 +000052{
Daniel Dunbar0ea85f72009-03-24 19:02:31 +000053 // Only use clang on i386 and x86_64 by default.
54 CCCClangArchs.insert("i386");
55 CCCClangArchs.insert("x86_64");
Daniel Dunbar63c4da92009-03-02 19:59:07 +000056}
57
58Driver::~Driver() {
Daniel Dunbard6f0e372009-03-04 20:49:20 +000059 delete Opts;
Daniel Dunbar2f8b37e2009-03-18 01:09:40 +000060 delete Host;
Daniel Dunbar63c4da92009-03-02 19:59:07 +000061}
62
Daniel Dunbara16e4fe2009-03-25 04:13:45 +000063InputArgList *Driver::ParseArgStrings(const char **ArgBegin,
64 const char **ArgEnd) {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +000065 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbara16e4fe2009-03-25 04:13:45 +000066 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000067
Daniel Dunbar85cb3592009-03-13 11:38:42 +000068 // FIXME: Handle '@' args (or at least error on them).
69
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000070 unsigned Index = 0, End = ArgEnd - ArgBegin;
71 while (Index < End) {
Daniel Dunbarb043ebd2009-03-13 01:01:44 +000072 // gcc's handling of empty arguments doesn't make
73 // sense, but this is not a common use case. :)
74 //
75 // We just ignore them here (note that other things may
76 // still take them as arguments).
77 if (Args->getArgString(Index)[0] == '\0') {
78 ++Index;
79 continue;
80 }
81
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000082 unsigned Prev = Index;
Daniel Dunbarfb88ce02009-03-22 23:26:43 +000083 Arg *A = getOpts().ParseOneArg(*Args, Index);
84 assert(Index > Prev && "Parser failed to consume argument.");
Daniel Dunbardb62cc32009-03-12 07:58:46 +000085
Daniel Dunbarfb88ce02009-03-22 23:26:43 +000086 // Check for missing argument error.
87 if (!A) {
88 assert(Index >= End && "Unexpected parser error.");
89 Diag(clang::diag::err_drv_missing_argument)
90 << Args->getArgString(Prev)
91 << (Index - Prev - 1);
92 break;
Daniel Dunbardb62cc32009-03-12 07:58:46 +000093 }
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000094
Daniel Dunbarfb88ce02009-03-22 23:26:43 +000095 if (A->getOption().isUnsupported()) {
96 Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
97 continue;
98 }
99 Args->append(A);
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000100 }
101
102 return Args;
103}
104
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000105Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000106 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
107
Daniel Dunbarcc006892009-03-13 00:51:18 +0000108 // FIXME: Handle environment options which effect driver behavior,
109 // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH,
110 // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS.
111
112 // FIXME: What are we going to do with -V and -b?
113
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000114 // FIXME: This stuff needs to go into the Compilation, not the
115 // driver.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000116 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000117
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000118 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000119 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000120
121 // Read -ccc args.
122 //
123 // FIXME: We need to figure out where this behavior should
124 // live. Most of it should be outside in the client; the parts that
125 // aren't should have proper options, either by introducing new ones
126 // or by overloading gcc ones like -V or -b.
127 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
128 const char *Opt = *Start + 5;
129
130 if (!strcmp(Opt, "print-options")) {
131 CCCPrintOptions = true;
132 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000133 CCCPrintActions = true;
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000134 } else if (!strcmp(Opt, "print-bindings")) {
135 CCCPrintBindings = true;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000136 } else if (!strcmp(Opt, "cxx")) {
137 CCCIsCXX = true;
138 } else if (!strcmp(Opt, "echo")) {
139 CCCEcho = true;
140
Daniel Dunbar126da5e2009-04-01 23:34:41 +0000141 } else if (!strcmp(Opt, "gcc-name")) {
142 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
143 CCCGenericGCCName = *++Start;
144
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000145 } else if (!strcmp(Opt, "clang-cxx")) {
146 CCCUseClangCXX = true;
Douglas Gregor95734502009-04-18 00:34:01 +0000147 } else if (!strcmp(Opt, "pch-is-pch")) {
148 CCCUsePCH = true;
149 } else if (!strcmp(Opt, "pch-is-pth")) {
150 CCCUsePCH = false;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000151 } else if (!strcmp(Opt, "no-clang")) {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000152 CCCUseClang = false;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000153 } else if (!strcmp(Opt, "no-clang-cpp")) {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000154 CCCUseClangCPP = false;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000155 } else if (!strcmp(Opt, "clang-archs")) {
156 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
157 const char *Cur = *++Start;
158
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000159 CCCClangArchs.clear();
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000160 for (;;) {
161 const char *Next = strchr(Cur, ',');
162
163 if (Next) {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000164 if (Cur != Next)
165 CCCClangArchs.insert(std::string(Cur, Next));
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000166 Cur = Next + 1;
167 } else {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000168 if (*Cur != '\0')
169 CCCClangArchs.insert(std::string(Cur));
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000170 break;
171 }
172 }
173
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000174 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000175 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000176 HostTriple = *++Start;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000177
178 } else {
179 // FIXME: Error handling.
180 llvm::errs() << "invalid option: " << *Start << "\n";
181 exit(1);
182 }
183 }
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000184
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000185 InputArgList *Args = ParseArgStrings(Start, End);
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000186
Daniel Dunbar08966ca2009-03-17 20:45:45 +0000187 Host = GetHostInfo(HostTriple);
Daniel Dunbarcc006892009-03-13 00:51:18 +0000188
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000189 // The compilation takes ownership of Args.
Daniel Dunbar31a76e32009-03-18 22:16:03 +0000190 Compilation *C = new Compilation(*this, *Host->getToolChain(*Args), Args);
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000191
192 // FIXME: This behavior shouldn't be here.
193 if (CCCPrintOptions) {
194 PrintOptions(C->getArgs());
195 return C;
196 }
197
198 if (!HandleImmediateArgs(*C))
199 return C;
200
201 // Construct the list of abstract actions to perform for this
202 // compilation. We avoid passing a Compilation here simply to
203 // enforce the abstraction that pipelining is not host or toolchain
204 // dependent (other than the driver driver test).
205 if (Host->useDriverDriver())
206 BuildUniversalActions(C->getArgs(), C->getActions());
207 else
208 BuildActions(C->getArgs(), C->getActions());
209
210 if (CCCPrintActions) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000211 PrintActions(*C);
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000212 return C;
213 }
214
215 BuildJobs(*C);
Daniel Dunbarc413f822009-03-15 01:38:15 +0000216
217 return C;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000218}
219
Daniel Dunbar9fbdc882009-07-01 20:03:04 +0000220int Driver::ExecuteCompilation(const Compilation &C) const {
221 // Just print if -### was present.
222 if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
223 C.PrintJob(llvm::errs(), C.getJobs(), "\n", true);
224 return 0;
225 }
226
227 // If there were errors building the compilation, quit now.
228 if (getDiags().getNumErrors())
229 return 1;
230
231 const Command *FailingCommand = 0;
232 int Res = C.ExecuteJob(C.getJobs(), FailingCommand);
233
234 // Remove temp files.
235 C.CleanupFileList(C.getTempFiles());
236
237 // If the compilation failed, remove result files as well.
238 if (Res != 0 && !C.getArgs().hasArg(options::OPT_save_temps))
239 C.CleanupFileList(C.getResultFiles(), true);
240
241 // Print extra information about abnormal failures, if possible.
242 if (Res) {
243 // This is ad-hoc, but we don't want to be excessively noisy. If the result
244 // status was 1, assume the command failed normally. In particular, if it
245 // was the compiler then assume it gave a reasonable error code. Failures in
246 // other tools are less common, and they generally have worse diagnostics,
247 // so always print the diagnostic there.
248 const Action &Source = FailingCommand->getSource();
249 bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) ||
250 isa<PrecompileJobAction>(Source) ||
251 isa<AnalyzeJobAction>(Source) ||
252 isa<CompileJobAction>(Source));
253
254 if (!IsFriendlyTool || Res != 1) {
255 // FIXME: See FIXME above regarding result code interpretation.
256 if (Res < 0)
257 Diag(clang::diag::err_drv_command_signalled)
258 << Source.getClassName() << -Res;
259 else
260 Diag(clang::diag::err_drv_command_failed)
261 << Source.getClassName() << Res;
262 }
263 }
264
265 return Res;
266}
267
Daniel Dunbara790d372009-03-12 18:24:49 +0000268void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000269 unsigned i = 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000270 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000271 it != ie; ++it, ++i) {
272 Arg *A = *it;
273 llvm::errs() << "Option " << i << " - "
274 << "Name: \"" << A->getOption().getName() << "\", "
275 << "Values: {";
276 for (unsigned j = 0; j < A->getNumValues(); ++j) {
277 if (j)
278 llvm::errs() << ", ";
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000279 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000280 }
281 llvm::errs() << "}\n";
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000282 }
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000283}
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000284
Daniel Dunbare627b682009-03-31 21:38:17 +0000285static std::string getOptionHelpName(const OptTable &Opts, options::ID Id) {
286 std::string Name = Opts.getOptionName(Id);
287
288 // Add metavar, if used.
289 switch (Opts.getOptionKind(Id)) {
290 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
291 assert(0 && "Invalid option with help text.");
292
293 case Option::MultiArgClass: case Option::JoinedAndSeparateClass:
294 assert(0 && "Cannot print metavar for this kind of option.");
295
296 case Option::FlagClass:
297 break;
298
299 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
300 Name += ' ';
301 // FALLTHROUGH
302 case Option::JoinedClass: case Option::CommaJoinedClass:
303 Name += Opts.getOptionMetaVar(Id);
304 break;
305 }
306
307 return Name;
308}
309
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000310void Driver::PrintHelp(bool ShowHidden) const {
Daniel Dunbare627b682009-03-31 21:38:17 +0000311 llvm::raw_ostream &OS = llvm::outs();
312
313 OS << "OVERVIEW: clang \"gcc-compatible\" driver\n";
314 OS << '\n';
315 OS << "USAGE: " << Name << " [options] <input files>\n";
316 OS << '\n';
317 OS << "OPTIONS:\n";
318
319 // Render help text into (option, help) pairs.
320 std::vector< std::pair<std::string, const char*> > OptionHelp;
321
322 for (unsigned i = options::OPT_INPUT, e = options::LastOption; i != e; ++i) {
323 options::ID Id = (options::ID) i;
324 if (const char *Text = getOpts().getOptionHelpText(Id))
325 OptionHelp.push_back(std::make_pair(getOptionHelpName(getOpts(), Id),
326 Text));
327 }
328
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000329 if (ShowHidden) {
330 OptionHelp.push_back(std::make_pair("\nDRIVER OPTIONS:",""));
331 OptionHelp.push_back(std::make_pair("-ccc-cxx",
332 "Act as a C++ driver"));
333 OptionHelp.push_back(std::make_pair("-ccc-gcc-name",
334 "Name for native GCC compiler"));
335 OptionHelp.push_back(std::make_pair("-ccc-clang-cxx",
336 "Use the clang compiler for C++"));
337 OptionHelp.push_back(std::make_pair("-ccc-no-clang",
338 "Never use the clang compiler"));
339 OptionHelp.push_back(std::make_pair("-ccc-no-clang-cpp",
340 "Never use the clang preprocessor"));
341 OptionHelp.push_back(std::make_pair("-ccc-clang-archs",
342 "Comma separate list of architectures "
343 "to use the clang compiler for"));
Douglas Gregor95734502009-04-18 00:34:01 +0000344 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pch",
345 "Use lazy PCH for precompiled headers"));
346 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pth",
347 "Use pretokenized headers for precompiled headers"));
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000348
349 OptionHelp.push_back(std::make_pair("\nDEBUG/DEVELOPMENT OPTIONS:",""));
350 OptionHelp.push_back(std::make_pair("-ccc-host-triple",
351 "Simulate running on the given target"));
352 OptionHelp.push_back(std::make_pair("-ccc-print-options",
353 "Dump parsed command line arguments"));
354 OptionHelp.push_back(std::make_pair("-ccc-print-phases",
355 "Dump list of actions to perform"));
356 OptionHelp.push_back(std::make_pair("-ccc-print-bindings",
357 "Show bindings of tools to actions"));
358 OptionHelp.push_back(std::make_pair("CCC_ADD_ARGS",
359 "(ENVIRONMENT VARIABLE) Comma separated list of "
360 "arguments to prepend to the command line"));
361 }
362
Daniel Dunbare627b682009-03-31 21:38:17 +0000363 // Find the maximum option length.
364 unsigned OptionFieldWidth = 0;
365 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000366 // Skip titles.
367 if (!OptionHelp[i].second)
368 continue;
369
Daniel Dunbare627b682009-03-31 21:38:17 +0000370 // Limit the amount of padding we are willing to give up for
371 // alignment.
372 unsigned Length = OptionHelp[i].first.size();
373 if (Length <= 23)
374 OptionFieldWidth = std::max(OptionFieldWidth, Length);
375 }
376
377 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
378 const std::string &Option = OptionHelp[i].first;
379 OS << " " << Option;
380 for (int j = Option.length(), e = OptionFieldWidth; j < e; ++j)
381 OS << ' ';
382 OS << ' ' << OptionHelp[i].second << '\n';
383 }
384
385 OS.flush();
386}
387
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000388void Driver::PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const {
Mike Stump74d94472009-03-18 14:00:02 +0000389 static char buf[] = "$URL$";
390 char *zap = strstr(buf, "/lib/Driver");
391 if (zap)
392 *zap = 0;
393 zap = strstr(buf, "/clang/tools/clang");
394 if (zap)
395 *zap = 0;
Mike Stumpbc927292009-03-18 15:19:35 +0000396 const char *vers = buf+6;
Mike Stump3fc58f02009-03-18 18:45:55 +0000397 // FIXME: Add cmake support and remove #ifdef
398#ifdef SVN_REVISION
399 const char *revision = SVN_REVISION;
400#else
401 const char *revision = "";
402#endif
Daniel Dunbarcc006892009-03-13 00:51:18 +0000403 // FIXME: The following handlers should use a callback mechanism, we
404 // don't know what the client would like to do.
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000405 OS << "clang version " CLANG_VERSION_STRING " ("
Daniel Dunbar41c34b32009-06-16 23:32:58 +0000406 << vers << " " << revision << ")" << '\n';
Daniel Dunbar0ded7de2009-03-26 16:09:13 +0000407
408 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000409 OS << "Target: " << TC.getTripleString() << '\n';
Daniel Dunbar41c34b32009-06-16 23:32:58 +0000410
411 // Print the threading model.
412 //
413 // FIXME: Implement correctly.
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000414 OS << "Thread model: " << "posix" << '\n';
Daniel Dunbarcc006892009-03-13 00:51:18 +0000415}
416
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000417bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbarcc006892009-03-13 00:51:18 +0000418 // The order these options are handled in in gcc is all over the
419 // place, but we don't expect inconsistencies w.r.t. that to matter
420 // in practice.
Daniel Dunbare627b682009-03-31 21:38:17 +0000421
Daniel Dunbar9a553c42009-04-04 05:17:38 +0000422 if (C.getArgs().hasArg(options::OPT_dumpversion)) {
Douglas Gregorb7064742009-04-27 22:23:34 +0000423 llvm::outs() << CLANG_VERSION_STRING "\n";
Daniel Dunbar9a553c42009-04-04 05:17:38 +0000424 return false;
425 }
426
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000427 if (C.getArgs().hasArg(options::OPT__help) ||
428 C.getArgs().hasArg(options::OPT__help_hidden)) {
429 PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden));
Daniel Dunbare627b682009-03-31 21:38:17 +0000430 return false;
431 }
432
Daniel Dunbar3b7c84c2009-04-02 15:05:41 +0000433 if (C.getArgs().hasArg(options::OPT__version)) {
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000434 // Follow gcc behavior and use stdout for --version and stderr for -v
435 PrintVersion(C, llvm::outs());
Daniel Dunbar3b7c84c2009-04-02 15:05:41 +0000436 return false;
437 }
438
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000439 if (C.getArgs().hasArg(options::OPT_v) ||
440 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000441 PrintVersion(C, llvm::errs());
Daniel Dunbarcc006892009-03-13 00:51:18 +0000442 SuppressMissingInputWarning = true;
443 }
444
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000445 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbar91b9e202009-03-20 04:37:21 +0000446 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
447 llvm::outs() << "programs: =";
448 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
449 ie = TC.getProgramPaths().end(); it != ie; ++it) {
450 if (it != TC.getProgramPaths().begin())
451 llvm::outs() << ':';
452 llvm::outs() << *it;
453 }
454 llvm::outs() << "\n";
455 llvm::outs() << "libraries: =";
456 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
457 ie = TC.getFilePaths().end(); it != ie; ++it) {
458 if (it != TC.getFilePaths().begin())
459 llvm::outs() << ':';
460 llvm::outs() << *it;
461 }
462 llvm::outs() << "\n";
Daniel Dunbare627b682009-03-31 21:38:17 +0000463 return false;
Daniel Dunbar91b9e202009-03-20 04:37:21 +0000464 }
465
Daniel Dunbarcc006892009-03-13 00:51:18 +0000466 // FIXME: The following handlers should use a callback mechanism, we
467 // don't know what the client would like to do.
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000468 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
469 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).toString()
470 << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000471 return false;
472 }
473
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000474 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
475 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).toString()
476 << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000477 return false;
478 }
479
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000480 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbaref68dd32009-03-27 14:26:33 +0000481 llvm::outs() << GetFilePath("libgcc.a", TC).toString() << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000482 return false;
483 }
484
Daniel Dunbar329aa992009-06-16 23:25:22 +0000485 if (C.getArgs().hasArg(options::OPT_print_multi_lib)) {
486 // FIXME: We need tool chain support for this.
487 llvm::outs() << ".;\n";
488
489 switch (C.getDefaultToolChain().getTriple().getArch()) {
490 default:
491 break;
492
493 case llvm::Triple::x86_64:
494 llvm::outs() << "x86_64;@m64" << "\n";
495 break;
496
497 case llvm::Triple::ppc64:
498 llvm::outs() << "ppc64;@m64" << "\n";
499 break;
500 }
501 return false;
502 }
503
504 // FIXME: What is the difference between print-multi-directory and
505 // print-multi-os-directory?
506 if (C.getArgs().hasArg(options::OPT_print_multi_directory) ||
507 C.getArgs().hasArg(options::OPT_print_multi_os_directory)) {
508 switch (C.getDefaultToolChain().getTriple().getArch()) {
509 default:
510 case llvm::Triple::x86:
511 case llvm::Triple::ppc:
512 llvm::outs() << "." << "\n";
513 break;
514
515 case llvm::Triple::x86_64:
516 llvm::outs() << "x86_64" << "\n";
517 break;
518
519 case llvm::Triple::ppc64:
520 llvm::outs() << "ppc64" << "\n";
521 break;
522 }
523 return false;
524 }
525
Daniel Dunbarcc006892009-03-13 00:51:18 +0000526 return true;
527}
528
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000529static unsigned PrintActions1(const Compilation &C,
Daniel Dunbar494646b2009-03-13 12:19:02 +0000530 Action *A,
531 std::map<Action*, unsigned> &Ids) {
532 if (Ids.count(A))
533 return Ids[A];
534
535 std::string str;
536 llvm::raw_string_ostream os(str);
537
538 os << Action::getClassName(A->getKind()) << ", ";
539 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000540 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbar494646b2009-03-13 12:19:02 +0000541 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000542 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
543 C.getDefaultToolChain().getArchName()) << '"'
544 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbar494646b2009-03-13 12:19:02 +0000545 } else {
546 os << "{";
547 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000548 os << PrintActions1(C, *it, Ids);
Daniel Dunbar494646b2009-03-13 12:19:02 +0000549 ++it;
550 if (it != ie)
551 os << ", ";
552 }
553 os << "}";
554 }
555
556 unsigned Id = Ids.size();
557 Ids[A] = Id;
Daniel Dunbar9dc28b82009-03-13 17:20:20 +0000558 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbar494646b2009-03-13 12:19:02 +0000559 << types::getTypeName(A->getType()) << "\n";
560
561 return Id;
562}
563
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000564void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbar494646b2009-03-13 12:19:02 +0000565 std::map<Action*, unsigned> Ids;
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000566 for (ActionList::const_iterator it = C.getActions().begin(),
567 ie = C.getActions().end(); it != ie; ++it)
568 PrintActions1(C, *it, Ids);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000569}
570
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000571void Driver::BuildUniversalActions(const ArgList &Args,
572 ActionList &Actions) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000573 llvm::PrettyStackTraceString CrashInfo("Building actions for universal build");
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000574 // Collect the list of architectures. Duplicates are allowed, but
575 // should only be handled once (in the order seen).
576 llvm::StringSet<> ArchNames;
577 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000578 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
579 it != ie; ++it) {
580 Arg *A = *it;
581
582 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000583 const char *Name = A->getValue(Args);
584
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000585 // FIXME: We need to handle canonicalization of the specified
586 // arch?
587
Daniel Dunbara345a442009-03-19 07:55:12 +0000588 A->claim();
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000589 if (ArchNames.insert(Name))
590 Archs.push_back(Name);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000591 }
592 }
593
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000594 // When there is no explicit arch for this platform, make sure we
595 // still bind the architecture (to the default) so that -Xarch_ is
596 // handled correctly.
597 if (!Archs.size())
598 Archs.push_back(0);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000599
600 // FIXME: We killed off some others but these aren't yet detected in
601 // a functional manner. If we added information to jobs about which
602 // "auxiliary" files they wrote then we could detect the conflict
603 // these cause downstream.
604 if (Archs.size() > 1) {
605 // No recovery needed, the point of this is just to prevent
606 // overwriting the same files.
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000607 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
608 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar73225932009-03-20 06:14:23 +0000609 << A->getAsString(Args);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000610 }
611
612 ActionList SingleActions;
613 BuildActions(Args, SingleActions);
614
615 // Add in arch binding and lipo (if necessary) for every top level
616 // action.
617 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
618 Action *Act = SingleActions[i];
619
620 // Make sure we can lipo this kind of output. If not (and it is an
621 // actual output) then we disallow, since we can't create an
622 // output file with the right name without overwriting it. We
623 // could remove this oddity by just changing the output names to
624 // include the arch, which would also fix
625 // -save-temps. Compatibility wins for now.
626
Daniel Dunbardd863aa2009-03-13 17:46:02 +0000627 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000628 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
629 << types::getTypeName(Act->getType());
630
631 ActionList Inputs;
Daniel Dunbara345a442009-03-19 07:55:12 +0000632 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000633 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000634
635 // Lipo if necessary, We do it this way because we need to set the
636 // arch flag so that -Xarch_ gets overwritten.
637 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
638 Actions.append(Inputs.begin(), Inputs.end());
639 else
640 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
641 }
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000642}
643
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000644void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000645 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000646 // Start by constructing the list of inputs and their types.
647
Daniel Dunbar5cb75d62009-03-13 17:57:10 +0000648 // Track the current user specified (-x) input. We also explicitly
649 // track the argument used to set the type; we only want to claim
650 // the type when we actually use it, so we warn about unused -x
651 // arguments.
652 types::ID InputType = types::TY_Nothing;
653 Arg *InputTypeArg = 0;
654
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000655 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
656 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
657 it != ie; ++it) {
658 Arg *A = *it;
659
660 if (isa<InputOption>(A->getOption())) {
661 const char *Value = A->getValue(Args);
662 types::ID Ty = types::TY_INVALID;
663
664 // Infer the input type if necessary.
Daniel Dunbar5cb75d62009-03-13 17:57:10 +0000665 if (InputType == types::TY_Nothing) {
666 // If there was an explicit arg for this, claim it.
667 if (InputTypeArg)
668 InputTypeArg->claim();
669
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000670 // stdin must be handled specially.
671 if (memcmp(Value, "-", 2) == 0) {
672 // If running with -E, treat as a C input (this changes the
673 // builtin macros, for example). This may be overridden by
674 // -ObjC below.
675 //
676 // Otherwise emit an error but still use a valid type to
677 // avoid spurious errors (e.g., no inputs).
Daniel Dunbare9c70fa2009-03-15 00:48:16 +0000678 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbard724e332009-03-12 09:13:48 +0000679 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000680 Ty = types::TY_C;
681 } else {
682 // Otherwise lookup by extension, and fallback to ObjectType
Daniel Dunbar7b6dfbd2009-03-20 23:39:23 +0000683 // if not found. We use a host hook here because Darwin at
684 // least has its own idea of what .s is.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000685 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbar7b6dfbd2009-03-20 23:39:23 +0000686 Ty = Host->lookupTypeForExtension(Ext + 1);
687
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000688 if (Ty == types::TY_INVALID)
689 Ty = types::TY_Object;
690 }
691
Daniel Dunbar84266db2009-05-18 21:47:54 +0000692 // -ObjC and -ObjC++ override the default language, but only for "source
693 // files". We just treat everything that isn't a linker input as a
694 // source file.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000695 //
Daniel Dunbar84266db2009-05-18 21:47:54 +0000696 // FIXME: Clean this up if we move the phase sequence into the type.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000697 if (Ty != types::TY_Object) {
698 if (Args.hasArg(options::OPT_ObjC))
699 Ty = types::TY_ObjC;
700 else if (Args.hasArg(options::OPT_ObjCXX))
701 Ty = types::TY_ObjCXX;
702 }
703 } else {
704 assert(InputTypeArg && "InputType set w/o InputTypeArg");
705 InputTypeArg->claim();
706 Ty = InputType;
707 }
708
709 // Check that the file exists. It isn't clear this is worth
710 // doing, since the tool presumably does this anyway, and this
711 // just adds an extra stat to the equation, but this is gcc
712 // compatible.
713 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbard724e332009-03-12 09:13:48 +0000714 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000715 else
716 Inputs.push_back(std::make_pair(Ty, A));
717
718 } else if (A->getOption().isLinkerInput()) {
719 // Just treat as object type, we could make a special type for
720 // this if necessary.
721 Inputs.push_back(std::make_pair(types::TY_Object, A));
722
723 } else if (A->getOption().getId() == options::OPT_x) {
724 InputTypeArg = A;
725 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
726
727 // Follow gcc behavior and treat as linker input for invalid -x
728 // options. Its not clear why we shouldn't just revert to
729 // unknown; but this isn't very important, we might as well be
730 // bug comatible.
731 if (!InputType) {
Daniel Dunbard724e332009-03-12 09:13:48 +0000732 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000733 InputType = types::TY_Object;
734 }
735 }
736 }
737
Daniel Dunbar5a5ec5c2009-03-13 00:17:48 +0000738 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000739 Diag(clang::diag::err_drv_no_input_files);
740 return;
741 }
742
743 // Determine which compilation mode we are in. We look for options
744 // which affect the phase, starting with the earliest phases, and
745 // record which option we used to determine the final phase.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000746 Arg *FinalPhaseArg = 0;
747 phases::ID FinalPhase;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000748
749 // -{E,M,MM} only run the preprocessor.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000750 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
751 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
752 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
753 FinalPhase = phases::Preprocess;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000754
Daniel Dunbare9c70fa2009-03-15 00:48:16 +0000755 // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler.
756 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
Daniel Dunbar6e3de3d2009-05-06 02:12:32 +0000757 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze,
758 options::OPT__analyze_auto)) ||
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000759 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
760 FinalPhase = phases::Compile;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000761
762 // -c only runs up to the assembler.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000763 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
764 FinalPhase = phases::Assemble;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000765
766 // Otherwise do everything.
767 } else
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000768 FinalPhase = phases::Link;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000769
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000770 // Reject -Z* at the top level, these options should never have been
771 // exposed by gcc.
Daniel Dunbar55e34e32009-03-26 16:12:09 +0000772 if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
Daniel Dunbar73225932009-03-20 06:14:23 +0000773 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000774
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000775 // Construct the actions to perform.
776 ActionList LinkerInputs;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000777 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000778 types::ID InputType = Inputs[i].first;
779 const Arg *InputArg = Inputs[i].second;
780
781 unsigned NumSteps = types::getNumCompilationPhases(InputType);
782 assert(NumSteps && "Invalid number of steps!");
783
784 // If the first step comes after the final phase we are doing as
785 // part of this compilation, warn the user about it.
786 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
787 if (InitialPhase > FinalPhase) {
Daniel Dunbar923e7032009-03-19 07:57:08 +0000788 // Claim here to avoid the more general unused warning.
789 InputArg->claim();
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000790 Diag(clang::diag::warn_drv_input_file_unused)
Daniel Dunbar73225932009-03-20 06:14:23 +0000791 << InputArg->getAsString(Args)
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000792 << getPhaseName(InitialPhase)
793 << FinalPhaseArg->getOption().getName();
794 continue;
795 }
796
797 // Build the pipeline for this file.
798 Action *Current = new InputAction(*InputArg, InputType);
799 for (unsigned i = 0; i != NumSteps; ++i) {
800 phases::ID Phase = types::getCompilationPhase(InputType, i);
801
802 // We are done if this step is past what the user requested.
803 if (Phase > FinalPhase)
804 break;
805
806 // Queue linker inputs.
807 if (Phase == phases::Link) {
808 assert(i + 1 == NumSteps && "linking must be final compilation step.");
809 LinkerInputs.push_back(Current);
810 Current = 0;
811 break;
812 }
813
Daniel Dunbardec14452009-03-24 20:17:30 +0000814 // Some types skip the assembler phase (e.g., llvm-bc), but we
815 // can't encode this in the steps because the intermediate type
816 // depends on arguments. Just special case here.
817 if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
818 continue;
819
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000820 // Otherwise construct the appropriate action.
821 Current = ConstructPhaseAction(Args, Phase, Current);
822 if (Current->getType() == types::TY_Nothing)
823 break;
824 }
825
826 // If we ended with something, add to the output list.
827 if (Current)
828 Actions.push_back(Current);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000829 }
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000830
831 // Add a link action if necessary.
832 if (!LinkerInputs.empty())
833 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
834}
835
836Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
837 Action *Input) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000838 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000839 // Build the appropriate action.
840 switch (Phase) {
841 case phases::Link: assert(0 && "link action invalid here.");
842 case phases::Preprocess: {
Daniel Dunbar049b7242009-03-30 06:36:42 +0000843 types::ID OutputTy;
844 // -{M, MM} alter the output type.
845 if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) {
846 OutputTy = types::TY_Dependencies;
847 } else {
848 OutputTy = types::getPreprocessedType(Input->getType());
849 assert(OutputTy != types::TY_INVALID &&
850 "Cannot preprocess this input type!");
851 }
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000852 return new PreprocessJobAction(Input, OutputTy);
853 }
854 case phases::Precompile:
855 return new PrecompileJobAction(Input, types::TY_PCH);
856 case phases::Compile: {
857 if (Args.hasArg(options::OPT_fsyntax_only)) {
858 return new CompileJobAction(Input, types::TY_Nothing);
Daniel Dunbar6e3de3d2009-05-06 02:12:32 +0000859 } else if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) {
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000860 return new AnalyzeJobAction(Input, types::TY_Plist);
Daniel Dunbardec14452009-03-24 20:17:30 +0000861 } else if (Args.hasArg(options::OPT_emit_llvm) ||
862 Args.hasArg(options::OPT_flto) ||
863 Args.hasArg(options::OPT_O4)) {
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000864 types::ID Output =
865 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
866 return new CompileJobAction(Input, Output);
867 } else {
868 return new CompileJobAction(Input, types::TY_PP_Asm);
869 }
870 }
871 case phases::Assemble:
872 return new AssembleJobAction(Input, types::TY_Object);
873 }
874
875 assert(0 && "invalid phase in ConstructPhaseAction");
876 return 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000877}
878
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000879void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000880 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000881 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
882 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbarbac6b642009-03-18 23:18:19 +0000883
884 // FIXME: Pipes are forcibly disabled until we support executing
885 // them.
886 if (!CCCPrintBindings)
887 UsePipes = false;
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000888
889 // -save-temps inhibits pipes.
890 if (SaveTemps && UsePipes) {
891 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
892 UsePipes = true;
893 }
894
895 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
896
897 // It is an error to provide a -o option if we are making multiple
898 // output files.
899 if (FinalOutput) {
900 unsigned NumOutputs = 0;
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000901 for (ActionList::const_iterator it = C.getActions().begin(),
902 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000903 if ((*it)->getType() != types::TY_Nothing)
904 ++NumOutputs;
905
906 if (NumOutputs > 1) {
907 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
908 FinalOutput = 0;
909 }
910 }
911
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000912 for (ActionList::const_iterator it = C.getActions().begin(),
913 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000914 Action *A = *it;
915
916 // If we are linking an image for multiple archs then the linker
917 // wants -arch_multiple and -final_output <final image
918 // name>. Unfortunately, this doesn't fit in cleanly because we
919 // have to pass this information down.
920 //
921 // FIXME: This is a hack; find a cleaner way to integrate this
922 // into the process.
923 const char *LinkingOutput = 0;
Daniel Dunbar55e34e32009-03-26 16:12:09 +0000924 if (isa<LipoJobAction>(A)) {
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000925 if (FinalOutput)
926 LinkingOutput = FinalOutput->getValue(C.getArgs());
927 else
928 LinkingOutput = DefaultImageName.c_str();
929 }
930
931 InputInfo II;
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000932 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000933 /*CanAcceptPipe*/ true,
934 /*AtTopLevel*/ true,
935 /*LinkingOutput*/ LinkingOutput,
936 II);
937 }
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000938
Daniel Dunbar025de202009-04-03 22:09:23 +0000939 // If the user passed -Qunused-arguments or there were errors, don't
940 // warn about any unused arguments.
Daniel Dunbar1cfd8912009-04-07 19:04:18 +0000941 if (Diags.getNumErrors() ||
942 C.getArgs().hasArg(options::OPT_Qunused_arguments))
Daniel Dunbar46c70822009-03-18 18:03:46 +0000943 return;
944
Daniel Dunbar1e6f9ff2009-03-29 22:24:54 +0000945 // Claim -### here.
946 (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
947
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000948 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
949 it != ie; ++it) {
950 Arg *A = *it;
Daniel Dunbar46c70822009-03-18 18:03:46 +0000951
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000952 // FIXME: It would be nice to be able to send the argument to the
953 // Diagnostic, so that extra values, position, and so on could be
954 // printed.
Daniel Dunbar6c6e3f72009-04-04 00:52:26 +0000955 if (!A->isClaimed()) {
Daniel Dunbar1cfd8912009-04-07 19:04:18 +0000956 if (A->getOption().hasNoArgumentUnused())
957 continue;
958
Daniel Dunbar6c6e3f72009-04-04 00:52:26 +0000959 // Suppress the warning automatically if this is just a flag,
960 // and it is an instance of an argument we already claimed.
961 const Option &Opt = A->getOption();
962 if (isa<FlagOption>(Opt)) {
963 bool DuplicateClaimed = false;
964
965 // FIXME: Use iterator.
966 for (ArgList::const_iterator it = C.getArgs().begin(),
967 ie = C.getArgs().end(); it != ie; ++it) {
968 if ((*it)->isClaimed() && (*it)->getOption().matches(Opt.getId())) {
969 DuplicateClaimed = true;
970 break;
971 }
972 }
973
974 if (DuplicateClaimed)
975 continue;
976 }
977
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000978 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar73225932009-03-20 06:14:23 +0000979 << A->getAsString(C.getArgs());
Daniel Dunbar6c6e3f72009-04-04 00:52:26 +0000980 }
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000981 }
Daniel Dunbar47d762e2009-03-13 22:12:33 +0000982}
983
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000984void Driver::BuildJobsForAction(Compilation &C,
985 const Action *A,
986 const ToolChain *TC,
987 bool CanAcceptPipe,
988 bool AtTopLevel,
989 const char *LinkingOutput,
990 InputInfo &Result) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000991 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action");
Daniel Dunbarbac6b642009-03-18 23:18:19 +0000992
993 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
994 // FIXME: Pipes are forcibly disabled until we support executing
995 // them.
996 if (!CCCPrintBindings)
997 UsePipes = false;
998
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000999 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbarc3be67b2009-03-19 07:29:38 +00001000 // FIXME: It would be nice to not claim this here; maybe the old
1001 // scheme of just using Args was better?
1002 const Arg &Input = IA->getInputArg();
1003 Input.claim();
1004 if (isa<PositionalArg>(Input)) {
1005 const char *Name = Input.getValue(C.getArgs());
1006 Result = InputInfo(Name, A->getType(), Name);
1007 } else
1008 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001009 return;
1010 }
1011
1012 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
1013 const char *ArchName = BAA->getArchName();
Daniel Dunbar08303652009-05-22 02:53:45 +00001014 std::string Arch;
1015 if (!ArchName) {
1016 Arch = C.getDefaultToolChain().getArchName();
1017 ArchName = Arch.c_str();
1018 }
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001019 BuildJobsForAction(C,
1020 *BAA->begin(),
1021 Host->getToolChain(C.getArgs(), ArchName),
1022 CanAcceptPipe,
1023 AtTopLevel,
1024 LinkingOutput,
1025 Result);
1026 return;
1027 }
1028
1029 const JobAction *JA = cast<JobAction>(A);
1030 const Tool &T = TC->SelectTool(C, *JA);
1031
1032 // See if we should use an integrated preprocessor. We do so when we
1033 // have exactly one input, since this is the only use case we care
1034 // about (irrelevant since we don't support combine yet).
1035 bool UseIntegratedCPP = false;
1036 const ActionList *Inputs = &A->getInputs();
1037 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
1038 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
1039 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
1040 !C.getArgs().hasArg(options::OPT_save_temps) &&
1041 T.hasIntegratedCPP()) {
1042 UseIntegratedCPP = true;
1043 Inputs = &(*Inputs)[0]->getInputs();
1044 }
1045 }
1046
1047 // Only use pipes when there is exactly one input.
1048 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar00fd3792009-03-18 06:00:36 +00001049 InputInfoList InputInfos;
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001050 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
1051 it != ie; ++it) {
1052 InputInfo II;
1053 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
1054 /*AtTopLevel*/false,
1055 LinkingOutput,
1056 II);
1057 InputInfos.push_back(II);
1058 }
1059
1060 // Determine if we should output to a pipe.
1061 bool OutputToPipe = false;
1062 if (CanAcceptPipe && T.canPipeOutput()) {
1063 // Some actions default to writing to a pipe if they are the top
1064 // level phase and there was no user override.
1065 //
1066 // FIXME: Is there a better way to handle this?
1067 if (AtTopLevel) {
1068 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
1069 OutputToPipe = true;
Daniel Dunbarbac6b642009-03-18 23:18:19 +00001070 } else if (UsePipes)
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001071 OutputToPipe = true;
1072 }
1073
1074 // Figure out where to put the job (pipes).
1075 Job *Dest = &C.getJobs();
1076 if (InputInfos[0].isPipe()) {
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001077 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001078 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
1079 Dest = &InputInfos[0].getPipe();
1080 }
1081
1082 // Always use the first input as the base input.
1083 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001084
1085 // Determine the place to write output to (nothing, pipe, or
1086 // filename) and where to put the new job.
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001087 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001088 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001089 } else if (OutputToPipe) {
1090 // Append to current piped job or create a new one as appropriate.
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001091 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
1092 if (!PJ) {
1093 PJ = new PipedJob();
Daniel Dunbar8eeb5a32009-03-20 00:11:04 +00001094 // FIXME: Temporary hack so that -ccc-print-bindings work until
1095 // we have pipe support. Please remove later.
1096 if (!CCCPrintBindings)
1097 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar933ac452009-03-18 07:06:02 +00001098 Dest = PJ;
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001099 }
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001100 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001101 } else {
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001102 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
1103 A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001104 }
1105
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001106 if (CCCPrintBindings) {
Daniel Dunbar049b7242009-03-30 06:36:42 +00001107 llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"'
1108 << " - \"" << T.getName() << "\", inputs: [";
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001109 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
1110 llvm::errs() << InputInfos[i].getAsString();
1111 if (i + 1 != e)
1112 llvm::errs() << ", ";
1113 }
1114 llvm::errs() << "], output: " << Result.getAsString() << "\n";
1115 } else {
Daniel Dunbara16e4fe2009-03-25 04:13:45 +00001116 T.ConstructJob(C, *JA, *Dest, Result, InputInfos,
1117 C.getArgsForToolChain(TC), LinkingOutput);
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001118 }
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001119}
1120
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001121const char *Driver::GetNamedOutputPath(Compilation &C,
1122 const JobAction &JA,
1123 const char *BaseInput,
1124 bool AtTopLevel) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +00001125 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001126 // Output to a user requested destination?
1127 if (AtTopLevel) {
1128 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
1129 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
1130 }
1131
1132 // Output to a temporary file?
1133 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001134 std::string TmpName =
1135 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
1136 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001137 }
1138
1139 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbarfb5e3332009-03-18 02:00:31 +00001140 std::string BaseName(BasePath.getLast());
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001141
1142 // Determine what the derived output name should be.
1143 const char *NamedOutput;
1144 if (JA.getType() == types::TY_Image) {
1145 NamedOutput = DefaultImageName.c_str();
1146 } else {
1147 const char *Suffix = types::getTypeTempSuffix(JA.getType());
1148 assert(Suffix && "All types used for output should have a suffix.");
1149
1150 std::string::size_type End = std::string::npos;
1151 if (!types::appendSuffixForType(JA.getType()))
1152 End = BaseName.rfind('.');
1153 std::string Suffixed(BaseName.substr(0, End));
1154 Suffixed += '.';
1155 Suffixed += Suffix;
1156 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
1157 }
1158
1159 // As an annoying special case, PCH generation doesn't strip the
1160 // pathname.
1161 if (JA.getType() == types::TY_PCH) {
1162 BasePath.eraseComponent();
Daniel Dunbar58a51262009-03-18 09:58:30 +00001163 if (BasePath.isEmpty())
1164 BasePath = NamedOutput;
1165 else
1166 BasePath.appendComponent(NamedOutput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001167 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
1168 } else {
1169 return C.addResultFile(NamedOutput);
1170 }
1171}
1172
Daniel Dunbare1cef7d2009-03-16 05:25:36 +00001173llvm::sys::Path Driver::GetFilePath(const char *Name,
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +00001174 const ToolChain &TC) const {
Daniel Dunbarcc7600c2009-03-18 20:26:19 +00001175 const ToolChain::path_list &List = TC.getFilePaths();
1176 for (ToolChain::path_list::const_iterator
1177 it = List.begin(), ie = List.end(); it != ie; ++it) {
1178 llvm::sys::Path P(*it);
1179 P.appendComponent(Name);
1180 if (P.exists())
1181 return P;
1182 }
1183
Daniel Dunbarcc006892009-03-13 00:51:18 +00001184 return llvm::sys::Path(Name);
1185}
1186
Daniel Dunbare1cef7d2009-03-16 05:25:36 +00001187llvm::sys::Path Driver::GetProgramPath(const char *Name,
Mike Stump32b1bbe2009-03-27 00:40:20 +00001188 const ToolChain &TC,
1189 bool WantFile) const {
Daniel Dunbarcc7600c2009-03-18 20:26:19 +00001190 const ToolChain::path_list &List = TC.getProgramPaths();
1191 for (ToolChain::path_list::const_iterator
1192 it = List.begin(), ie = List.end(); it != ie; ++it) {
1193 llvm::sys::Path P(*it);
1194 P.appendComponent(Name);
Mike Stump32b1bbe2009-03-27 00:40:20 +00001195 if (WantFile ? P.exists() : P.canExecute())
Daniel Dunbarcc7600c2009-03-18 20:26:19 +00001196 return P;
1197 }
1198
Daniel Dunbar49a35de2009-03-23 16:15:50 +00001199 // If all else failed, search the path.
1200 llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
Daniel Dunbarcb84b9a2009-03-18 21:34:08 +00001201 if (!P.empty())
1202 return P;
1203
Daniel Dunbarcc006892009-03-13 00:51:18 +00001204 return llvm::sys::Path(Name);
1205}
1206
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001207std::string Driver::GetTemporaryPath(const char *Suffix) const {
1208 // FIXME: This is lame; sys::Path should provide this function (in
1209 // particular, it should know how to find the temporary files dir).
1210 std::string Error;
Daniel Dunbar02e396a2009-04-20 20:28:21 +00001211 const char *TmpDir = ::getenv("TMPDIR");
1212 if (!TmpDir)
1213 TmpDir = ::getenv("TEMP");
1214 if (!TmpDir)
Daniel Dunbara77aa002009-04-21 00:25:10 +00001215 TmpDir = ::getenv("TMP");
1216 if (!TmpDir)
Daniel Dunbar02e396a2009-04-20 20:28:21 +00001217 TmpDir = "/tmp";
1218 llvm::sys::Path P(TmpDir);
Daniel Dunbar121f2512009-04-20 17:32:49 +00001219 P.appendComponent("cc");
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001220 if (P.makeUnique(false, &Error)) {
1221 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
1222 return "";
1223 }
1224
Daniel Dunbar9083abe2009-03-18 23:08:52 +00001225 // FIXME: Grumble, makeUnique sometimes leaves the file around!?
1226 // PR3837.
1227 P.eraseFromDisk(false, 0);
1228
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001229 P.appendSuffix(Suffix);
1230 return P.toString();
1231}
1232
Daniel Dunbar08303652009-05-22 02:53:45 +00001233const HostInfo *Driver::GetHostInfo(const char *TripleStr) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +00001234 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbar08303652009-05-22 02:53:45 +00001235 llvm::Triple Triple(TripleStr);
Daniel Dunbard25acaa2009-03-10 23:41:59 +00001236
Daniel Dunbar7424b8a2009-03-17 19:00:50 +00001237 // Normalize Arch a bit.
1238 //
Daniel Dunbar08303652009-05-22 02:53:45 +00001239 // FIXME: We shouldn't need to do this once everything goes through the triple
1240 // interface.
1241 if (Triple.getArchName() == "i686")
1242 Triple.setArchName("i386");
1243 else if (Triple.getArchName() == "amd64")
1244 Triple.setArchName("x86_64");
1245 else if (Triple.getArchName() == "ppc" ||
1246 Triple.getArchName() == "Power Macintosh")
1247 Triple.setArchName("powerpc");
1248 else if (Triple.getArchName() == "ppc64")
1249 Triple.setArchName("powerpc64");
Daniel Dunbar46363f52009-05-02 18:28:39 +00001250
Daniel Dunbar08303652009-05-22 02:53:45 +00001251 switch (Triple.getOS()) {
1252 case llvm::Triple::Darwin:
1253 return createDarwinHostInfo(*this, Triple);
1254 case llvm::Triple::DragonFly:
1255 return createDragonFlyHostInfo(*this, Triple);
Daniel Dunbar64c77a12009-06-29 20:52:51 +00001256 case llvm::Triple::OpenBSD:
1257 return createOpenBSDHostInfo(*this, Triple);
Daniel Dunbar08303652009-05-22 02:53:45 +00001258 case llvm::Triple::FreeBSD:
1259 return createFreeBSDHostInfo(*this, Triple);
Eli Friedman8cac4352009-05-26 07:52:18 +00001260 case llvm::Triple::Linux:
1261 return createLinuxHostInfo(*this, Triple);
Daniel Dunbar08303652009-05-22 02:53:45 +00001262 default:
1263 return createUnknownHostInfo(*this, Triple);
1264 }
Daniel Dunbard25acaa2009-03-10 23:41:59 +00001265}
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001266
1267bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
Daniel Dunbarb7daa2d2009-04-01 20:33:11 +00001268 const std::string &ArchNameStr) const {
1269 // FIXME: Remove this hack.
1270 const char *ArchName = ArchNameStr.c_str();
1271 if (ArchNameStr == "powerpc")
1272 ArchName = "ppc";
1273 else if (ArchNameStr == "powerpc64")
1274 ArchName = "ppc64";
1275
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001276 // Check if user requested no clang, or clang doesn't understand
1277 // this type (we only handle single inputs for now).
Daniel Dunbar0ea85f72009-03-24 19:02:31 +00001278 if (!CCCUseClang || JA.size() != 1 ||
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001279 !types::isAcceptedByClang((*JA.begin())->getType()))
1280 return false;
1281
Daniel Dunbar0ea85f72009-03-24 19:02:31 +00001282 // Otherwise make sure this is an action clang understands.
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001283 if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001284 if (!CCCUseClangCPP) {
1285 Diag(clang::diag::warn_drv_not_using_clang_cpp);
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001286 return false;
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001287 }
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001288 } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
1289 return false;
1290
Daniel Dunbar0ea85f72009-03-24 19:02:31 +00001291 // Use clang for C++?
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001292 if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
1293 Diag(clang::diag::warn_drv_not_using_clang_cxx);
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001294 return false;
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001295 }
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001296
Daniel Dunbar5a34ca82009-04-16 23:10:13 +00001297 // Always use clang for precompiling, regardless of archs. PTH is
1298 // platform independent, and this allows the use of the static
1299 // analyzer on platforms we don't have full IRgen support for.
1300 if (isa<PrecompileJobAction>(JA))
1301 return true;
1302
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001303 // Finally, don't use clang if this isn't one of the user specified
1304 // archs to build.
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001305 if (!CCCClangArchs.empty() && !CCCClangArchs.count(ArchName)) {
1306 Diag(clang::diag::warn_drv_not_using_clang_arch) << ArchName;
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001307 return false;
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001308 }
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001309
1310 return true;
1311}
Daniel Dunbar9cdd4d42009-03-26 15:58:36 +00001312
1313/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and
1314/// return the grouped values as integers. Numbers which are not
1315/// provided are set to 0.
1316///
1317/// \return True if the entire string was parsed (9.2), or all groups
1318/// were parsed (10.3.5extrastuff).
1319bool Driver::GetReleaseVersion(const char *Str, unsigned &Major,
1320 unsigned &Minor, unsigned &Micro,
1321 bool &HadExtra) {
1322 HadExtra = false;
1323
1324 Major = Minor = Micro = 0;
1325 if (*Str == '\0')
1326 return true;
1327
1328 char *End;
1329 Major = (unsigned) strtol(Str, &End, 10);
1330 if (*Str != '\0' && *End == '\0')
1331 return true;
1332 if (*End != '.')
1333 return false;
1334
1335 Str = End+1;
1336 Minor = (unsigned) strtol(Str, &End, 10);
1337 if (*Str != '\0' && *End == '\0')
1338 return true;
1339 if (*End != '.')
1340 return false;
1341
1342 Str = End+1;
1343 Micro = (unsigned) strtol(Str, &End, 10);
1344 if (*Str != '\0' && *End == '\0')
1345 return true;
1346 if (Str == End)
1347 return false;
1348 HadExtra = true;
1349 return true;
1350}