blob: 8845b9b58c5d84e10e4c9da7ce8f12bb2d4ed740 [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.
41#define USE_PRODUCTION_CLANG
42
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");
65#endif
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000066}
67
68Driver::~Driver() {
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000069 delete Opts;
Daniel Dunbar7e4534d2009-03-18 01:09:40 +000070 delete Host;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000071}
72
Daniel Dunbarf3cad362009-03-25 04:13:45 +000073InputArgList *Driver::ParseArgStrings(const char **ArgBegin,
74 const char **ArgEnd) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +000075 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbarf3cad362009-03-25 04:13:45 +000076 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
Daniel Dunbar06482622009-03-05 06:38:47 +000077
Daniel Dunbarad2a9af2009-03-13 11:38:42 +000078 // FIXME: Handle '@' args (or at least error on them).
79
Daniel Dunbar06482622009-03-05 06:38:47 +000080 unsigned Index = 0, End = ArgEnd - ArgBegin;
81 while (Index < End) {
Daniel Dunbar41393402009-03-13 01:01:44 +000082 // gcc's handling of empty arguments doesn't make
83 // sense, but this is not a common use case. :)
84 //
85 // We just ignore them here (note that other things may
86 // still take them as arguments).
87 if (Args->getArgString(Index)[0] == '\0') {
88 ++Index;
89 continue;
90 }
91
Daniel Dunbar06482622009-03-05 06:38:47 +000092 unsigned Prev = Index;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000093 Arg *A = getOpts().ParseOneArg(*Args, Index);
94 assert(Index > Prev && "Parser failed to consume argument.");
Daniel Dunbar53ec5522009-03-12 07:58:46 +000095
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000096 // Check for missing argument error.
97 if (!A) {
98 assert(Index >= End && "Unexpected parser error.");
99 Diag(clang::diag::err_drv_missing_argument)
100 << Args->getArgString(Prev)
101 << (Index - Prev - 1);
102 break;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000103 }
Daniel Dunbar06482622009-03-05 06:38:47 +0000104
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000105 if (A->getOption().isUnsupported()) {
106 Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
107 continue;
108 }
109 Args->append(A);
Daniel Dunbar06482622009-03-05 06:38:47 +0000110 }
111
112 return Args;
113}
114
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000115Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000116 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
117
Daniel Dunbarcb881672009-03-13 00:51:18 +0000118 // FIXME: Handle environment options which effect driver behavior,
119 // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH,
120 // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS.
121
122 // FIXME: What are we going to do with -V and -b?
123
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000124 // FIXME: This stuff needs to go into the Compilation, not the
125 // driver.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000126 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar06482622009-03-05 06:38:47 +0000127
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000128 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000129 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000130
131 // Read -ccc args.
132 //
133 // FIXME: We need to figure out where this behavior should
134 // live. Most of it should be outside in the client; the parts that
135 // aren't should have proper options, either by introducing new ones
136 // or by overloading gcc ones like -V or -b.
137 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
138 const char *Opt = *Start + 5;
139
140 if (!strcmp(Opt, "print-options")) {
141 CCCPrintOptions = true;
142 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000143 CCCPrintActions = true;
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000144 } else if (!strcmp(Opt, "print-bindings")) {
145 CCCPrintBindings = true;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000146 } else if (!strcmp(Opt, "cxx")) {
147 CCCIsCXX = true;
148 } else if (!strcmp(Opt, "echo")) {
149 CCCEcho = true;
150
Daniel Dunbar78d8a082009-04-01 23:34:41 +0000151 } else if (!strcmp(Opt, "gcc-name")) {
152 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
153 CCCGenericGCCName = *++Start;
154
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000155 } else if (!strcmp(Opt, "clang-cxx")) {
156 CCCUseClangCXX = true;
Daniel Dunbardfaf4b32009-07-23 17:48:59 +0000157 } else if (!strcmp(Opt, "no-clang-cxx")) {
158 CCCUseClangCXX = false;
Douglas Gregordf91ef32009-04-18 00:34:01 +0000159 } else if (!strcmp(Opt, "pch-is-pch")) {
160 CCCUsePCH = true;
161 } else if (!strcmp(Opt, "pch-is-pth")) {
162 CCCUsePCH = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000163 } else if (!strcmp(Opt, "no-clang")) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000164 CCCUseClang = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000165 } else if (!strcmp(Opt, "no-clang-cpp")) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000166 CCCUseClangCPP = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000167 } else if (!strcmp(Opt, "clang-archs")) {
168 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
169 const char *Cur = *++Start;
170
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000171 CCCClangArchs.clear();
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000172 for (;;) {
173 const char *Next = strchr(Cur, ',');
174
175 if (Next) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000176 if (Cur != Next)
177 CCCClangArchs.insert(std::string(Cur, Next));
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000178 Cur = Next + 1;
179 } else {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000180 if (*Cur != '\0')
181 CCCClangArchs.insert(std::string(Cur));
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000182 break;
183 }
184 }
185
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000186 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000187 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000188 HostTriple = *++Start;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000189
190 } else {
191 // FIXME: Error handling.
192 llvm::errs() << "invalid option: " << *Start << "\n";
193 exit(1);
194 }
195 }
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000196
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000197 InputArgList *Args = ParseArgStrings(Start, End);
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000198
Daniel Dunbare5049522009-03-17 20:45:45 +0000199 Host = GetHostInfo(HostTriple);
Daniel Dunbarcb881672009-03-13 00:51:18 +0000200
Daniel Dunbar586dc232009-03-16 06:42:30 +0000201 // The compilation takes ownership of Args.
Daniel Dunbare530ad42009-03-18 22:16:03 +0000202 Compilation *C = new Compilation(*this, *Host->getToolChain(*Args), Args);
Daniel Dunbar21549232009-03-18 02:55:38 +0000203
204 // FIXME: This behavior shouldn't be here.
205 if (CCCPrintOptions) {
206 PrintOptions(C->getArgs());
207 return C;
208 }
209
210 if (!HandleImmediateArgs(*C))
211 return C;
212
213 // Construct the list of abstract actions to perform for this
214 // compilation. We avoid passing a Compilation here simply to
215 // enforce the abstraction that pipelining is not host or toolchain
216 // dependent (other than the driver driver test).
217 if (Host->useDriverDriver())
218 BuildUniversalActions(C->getArgs(), C->getActions());
219 else
220 BuildActions(C->getArgs(), C->getActions());
221
222 if (CCCPrintActions) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000223 PrintActions(*C);
Daniel Dunbar21549232009-03-18 02:55:38 +0000224 return C;
225 }
226
227 BuildJobs(*C);
Daniel Dunbar8d2554a2009-03-15 01:38:15 +0000228
229 return C;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000230}
231
Daniel Dunbarc88a88f2009-07-01 20:03:04 +0000232int Driver::ExecuteCompilation(const Compilation &C) const {
233 // Just print if -### was present.
234 if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
235 C.PrintJob(llvm::errs(), C.getJobs(), "\n", true);
236 return 0;
237 }
238
239 // If there were errors building the compilation, quit now.
240 if (getDiags().getNumErrors())
241 return 1;
242
243 const Command *FailingCommand = 0;
244 int Res = C.ExecuteJob(C.getJobs(), FailingCommand);
245
246 // Remove temp files.
247 C.CleanupFileList(C.getTempFiles());
248
249 // If the compilation failed, remove result files as well.
250 if (Res != 0 && !C.getArgs().hasArg(options::OPT_save_temps))
251 C.CleanupFileList(C.getResultFiles(), true);
252
253 // Print extra information about abnormal failures, if possible.
254 if (Res) {
255 // This is ad-hoc, but we don't want to be excessively noisy. If the result
256 // status was 1, assume the command failed normally. In particular, if it
257 // was the compiler then assume it gave a reasonable error code. Failures in
258 // other tools are less common, and they generally have worse diagnostics,
259 // so always print the diagnostic there.
260 const Action &Source = FailingCommand->getSource();
261 bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) ||
262 isa<PrecompileJobAction>(Source) ||
263 isa<AnalyzeJobAction>(Source) ||
264 isa<CompileJobAction>(Source));
265
266 if (!IsFriendlyTool || Res != 1) {
267 // FIXME: See FIXME above regarding result code interpretation.
268 if (Res < 0)
269 Diag(clang::diag::err_drv_command_signalled)
270 << Source.getClassName() << -Res;
271 else
272 Diag(clang::diag::err_drv_command_failed)
273 << Source.getClassName() << Res;
274 }
275 }
276
277 return Res;
278}
279
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000280void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar06482622009-03-05 06:38:47 +0000281 unsigned i = 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000282 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar06482622009-03-05 06:38:47 +0000283 it != ie; ++it, ++i) {
284 Arg *A = *it;
285 llvm::errs() << "Option " << i << " - "
286 << "Name: \"" << A->getOption().getName() << "\", "
287 << "Values: {";
288 for (unsigned j = 0; j < A->getNumValues(); ++j) {
289 if (j)
290 llvm::errs() << ", ";
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000291 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar06482622009-03-05 06:38:47 +0000292 }
293 llvm::errs() << "}\n";
Daniel Dunbar06482622009-03-05 06:38:47 +0000294 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000295}
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000296
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000297static std::string getOptionHelpName(const OptTable &Opts, options::ID Id) {
298 std::string Name = Opts.getOptionName(Id);
299
300 // Add metavar, if used.
301 switch (Opts.getOptionKind(Id)) {
302 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
303 assert(0 && "Invalid option with help text.");
304
305 case Option::MultiArgClass: case Option::JoinedAndSeparateClass:
306 assert(0 && "Cannot print metavar for this kind of option.");
307
308 case Option::FlagClass:
309 break;
310
311 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
312 Name += ' ';
313 // FALLTHROUGH
314 case Option::JoinedClass: case Option::CommaJoinedClass:
315 Name += Opts.getOptionMetaVar(Id);
316 break;
317 }
318
319 return Name;
320}
321
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000322void Driver::PrintHelp(bool ShowHidden) const {
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000323 llvm::raw_ostream &OS = llvm::outs();
324
325 OS << "OVERVIEW: clang \"gcc-compatible\" driver\n";
326 OS << '\n';
327 OS << "USAGE: " << Name << " [options] <input files>\n";
328 OS << '\n';
329 OS << "OPTIONS:\n";
330
331 // Render help text into (option, help) pairs.
332 std::vector< std::pair<std::string, const char*> > OptionHelp;
333
334 for (unsigned i = options::OPT_INPUT, e = options::LastOption; i != e; ++i) {
335 options::ID Id = (options::ID) i;
336 if (const char *Text = getOpts().getOptionHelpText(Id))
337 OptionHelp.push_back(std::make_pair(getOptionHelpName(getOpts(), Id),
338 Text));
339 }
340
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000341 if (ShowHidden) {
342 OptionHelp.push_back(std::make_pair("\nDRIVER OPTIONS:",""));
343 OptionHelp.push_back(std::make_pair("-ccc-cxx",
344 "Act as a C++ driver"));
345 OptionHelp.push_back(std::make_pair("-ccc-gcc-name",
346 "Name for native GCC compiler"));
347 OptionHelp.push_back(std::make_pair("-ccc-clang-cxx",
348 "Use the clang compiler for C++"));
349 OptionHelp.push_back(std::make_pair("-ccc-no-clang",
350 "Never use the clang compiler"));
351 OptionHelp.push_back(std::make_pair("-ccc-no-clang-cpp",
352 "Never use the clang preprocessor"));
353 OptionHelp.push_back(std::make_pair("-ccc-clang-archs",
354 "Comma separate list of architectures "
355 "to use the clang compiler for"));
Douglas Gregordf91ef32009-04-18 00:34:01 +0000356 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pch",
357 "Use lazy PCH for precompiled headers"));
358 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pth",
359 "Use pretokenized headers for precompiled headers"));
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000360
361 OptionHelp.push_back(std::make_pair("\nDEBUG/DEVELOPMENT OPTIONS:",""));
362 OptionHelp.push_back(std::make_pair("-ccc-host-triple",
363 "Simulate running on the given target"));
364 OptionHelp.push_back(std::make_pair("-ccc-print-options",
365 "Dump parsed command line arguments"));
366 OptionHelp.push_back(std::make_pair("-ccc-print-phases",
367 "Dump list of actions to perform"));
368 OptionHelp.push_back(std::make_pair("-ccc-print-bindings",
369 "Show bindings of tools to actions"));
370 OptionHelp.push_back(std::make_pair("CCC_ADD_ARGS",
371 "(ENVIRONMENT VARIABLE) Comma separated list of "
372 "arguments to prepend to the command line"));
373 }
374
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000375 // Find the maximum option length.
376 unsigned OptionFieldWidth = 0;
377 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000378 // Skip titles.
379 if (!OptionHelp[i].second)
380 continue;
381
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000382 // Limit the amount of padding we are willing to give up for
383 // alignment.
384 unsigned Length = OptionHelp[i].first.size();
385 if (Length <= 23)
386 OptionFieldWidth = std::max(OptionFieldWidth, Length);
387 }
388
389 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
390 const std::string &Option = OptionHelp[i].first;
391 OS << " " << Option;
392 for (int j = Option.length(), e = OptionFieldWidth; j < e; ++j)
393 OS << ' ';
394 OS << ' ' << OptionHelp[i].second << '\n';
395 }
396
397 OS.flush();
398}
399
Daniel Dunbar79300722009-07-21 20:06:58 +0000400void Driver::PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const {
Mike Stump5d023c32009-03-18 14:00:02 +0000401 static char buf[] = "$URL$";
402 char *zap = strstr(buf, "/lib/Driver");
403 if (zap)
404 *zap = 0;
405 zap = strstr(buf, "/clang/tools/clang");
406 if (zap)
407 *zap = 0;
Mike Stumpe70295b2009-03-18 15:19:35 +0000408 const char *vers = buf+6;
Mike Stump8944c382009-03-18 18:45:55 +0000409 // FIXME: Add cmake support and remove #ifdef
410#ifdef SVN_REVISION
411 const char *revision = SVN_REVISION;
412#else
413 const char *revision = "";
414#endif
Daniel Dunbarcb881672009-03-13 00:51:18 +0000415 // FIXME: The following handlers should use a callback mechanism, we
416 // don't know what the client would like to do.
Daniel Dunbar79300722009-07-21 20:06:58 +0000417 OS << "clang version " CLANG_VERSION_STRING " ("
Daniel Dunbar3ee96ba2009-06-16 23:32:58 +0000418 << vers << " " << revision << ")" << '\n';
Daniel Dunbar70c8db12009-03-26 16:09:13 +0000419
420 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbar79300722009-07-21 20:06:58 +0000421 OS << "Target: " << TC.getTripleString() << '\n';
Daniel Dunbar3ee96ba2009-06-16 23:32:58 +0000422
423 // Print the threading model.
424 //
425 // FIXME: Implement correctly.
Daniel Dunbar79300722009-07-21 20:06:58 +0000426 OS << "Thread model: " << "posix" << '\n';
Daniel Dunbarcb881672009-03-13 00:51:18 +0000427}
428
Daniel Dunbar21549232009-03-18 02:55:38 +0000429bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000430 // The order these options are handled in in gcc is all over the
431 // place, but we don't expect inconsistencies w.r.t. that to matter
432 // in practice.
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000433
Daniel Dunbare06dc212009-04-04 05:17:38 +0000434 if (C.getArgs().hasArg(options::OPT_dumpversion)) {
Douglas Gregorab41e632009-04-27 22:23:34 +0000435 llvm::outs() << CLANG_VERSION_STRING "\n";
Daniel Dunbare06dc212009-04-04 05:17:38 +0000436 return false;
437 }
438
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000439 if (C.getArgs().hasArg(options::OPT__help) ||
440 C.getArgs().hasArg(options::OPT__help_hidden)) {
441 PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden));
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000442 return false;
443 }
444
Daniel Dunbar6cc73de2009-04-02 15:05:41 +0000445 if (C.getArgs().hasArg(options::OPT__version)) {
Daniel Dunbar79300722009-07-21 20:06:58 +0000446 // Follow gcc behavior and use stdout for --version and stderr for -v
447 PrintVersion(C, llvm::outs());
Daniel Dunbar6cc73de2009-04-02 15:05:41 +0000448 return false;
449 }
450
Daniel Dunbar21549232009-03-18 02:55:38 +0000451 if (C.getArgs().hasArg(options::OPT_v) ||
452 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbar79300722009-07-21 20:06:58 +0000453 PrintVersion(C, llvm::errs());
Daniel Dunbarcb881672009-03-13 00:51:18 +0000454 SuppressMissingInputWarning = true;
455 }
456
Daniel Dunbar21549232009-03-18 02:55:38 +0000457 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbarca3459e2009-03-20 04:37:21 +0000458 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
459 llvm::outs() << "programs: =";
460 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
461 ie = TC.getProgramPaths().end(); it != ie; ++it) {
462 if (it != TC.getProgramPaths().begin())
463 llvm::outs() << ':';
464 llvm::outs() << *it;
465 }
466 llvm::outs() << "\n";
467 llvm::outs() << "libraries: =";
468 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
469 ie = TC.getFilePaths().end(); it != ie; ++it) {
470 if (it != TC.getFilePaths().begin())
471 llvm::outs() << ':';
472 llvm::outs() << *it;
473 }
474 llvm::outs() << "\n";
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000475 return false;
Daniel Dunbarca3459e2009-03-20 04:37:21 +0000476 }
477
Daniel Dunbarcb881672009-03-13 00:51:18 +0000478 // FIXME: The following handlers should use a callback mechanism, we
479 // don't know what the client would like to do.
Daniel Dunbar21549232009-03-18 02:55:38 +0000480 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
481 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).toString()
482 << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000483 return false;
484 }
485
Daniel Dunbar21549232009-03-18 02:55:38 +0000486 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
487 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).toString()
488 << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000489 return false;
490 }
491
Daniel Dunbar21549232009-03-18 02:55:38 +0000492 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbar08c65e02009-03-27 14:26:33 +0000493 llvm::outs() << GetFilePath("libgcc.a", TC).toString() << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000494 return false;
495 }
496
Daniel Dunbar12cfe032009-06-16 23:25:22 +0000497 if (C.getArgs().hasArg(options::OPT_print_multi_lib)) {
498 // FIXME: We need tool chain support for this.
499 llvm::outs() << ".;\n";
500
501 switch (C.getDefaultToolChain().getTriple().getArch()) {
502 default:
503 break;
504
505 case llvm::Triple::x86_64:
506 llvm::outs() << "x86_64;@m64" << "\n";
507 break;
508
509 case llvm::Triple::ppc64:
510 llvm::outs() << "ppc64;@m64" << "\n";
511 break;
512 }
513 return false;
514 }
515
516 // FIXME: What is the difference between print-multi-directory and
517 // print-multi-os-directory?
518 if (C.getArgs().hasArg(options::OPT_print_multi_directory) ||
519 C.getArgs().hasArg(options::OPT_print_multi_os_directory)) {
520 switch (C.getDefaultToolChain().getTriple().getArch()) {
521 default:
522 case llvm::Triple::x86:
523 case llvm::Triple::ppc:
524 llvm::outs() << "." << "\n";
525 break;
526
527 case llvm::Triple::x86_64:
528 llvm::outs() << "x86_64" << "\n";
529 break;
530
531 case llvm::Triple::ppc64:
532 llvm::outs() << "ppc64" << "\n";
533 break;
534 }
535 return false;
536 }
537
Daniel Dunbarcb881672009-03-13 00:51:18 +0000538 return true;
539}
540
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000541static unsigned PrintActions1(const Compilation &C,
Daniel Dunbarba102132009-03-13 12:19:02 +0000542 Action *A,
543 std::map<Action*, unsigned> &Ids) {
544 if (Ids.count(A))
545 return Ids[A];
546
547 std::string str;
548 llvm::raw_string_ostream os(str);
549
550 os << Action::getClassName(A->getKind()) << ", ";
551 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000552 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbarba102132009-03-13 12:19:02 +0000553 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000554 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
555 C.getDefaultToolChain().getArchName()) << '"'
556 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbarba102132009-03-13 12:19:02 +0000557 } else {
558 os << "{";
559 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000560 os << PrintActions1(C, *it, Ids);
Daniel Dunbarba102132009-03-13 12:19:02 +0000561 ++it;
562 if (it != ie)
563 os << ", ";
564 }
565 os << "}";
566 }
567
568 unsigned Id = Ids.size();
569 Ids[A] = Id;
Daniel Dunbarb269c322009-03-13 17:20:20 +0000570 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbarba102132009-03-13 12:19:02 +0000571 << types::getTypeName(A->getType()) << "\n";
572
573 return Id;
574}
575
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000576void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbarba102132009-03-13 12:19:02 +0000577 std::map<Action*, unsigned> Ids;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000578 for (ActionList::const_iterator it = C.getActions().begin(),
579 ie = C.getActions().end(); it != ie; ++it)
580 PrintActions1(C, *it, Ids);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000581}
582
Daniel Dunbar21549232009-03-18 02:55:38 +0000583void Driver::BuildUniversalActions(const ArgList &Args,
584 ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000585 llvm::PrettyStackTraceString CrashInfo("Building actions for universal build");
Daniel Dunbar13689542009-03-13 20:33:35 +0000586 // Collect the list of architectures. Duplicates are allowed, but
587 // should only be handled once (in the order seen).
588 llvm::StringSet<> ArchNames;
589 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000590 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
591 it != ie; ++it) {
592 Arg *A = *it;
593
594 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbar13689542009-03-13 20:33:35 +0000595 const char *Name = A->getValue(Args);
596
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000597 // FIXME: We need to handle canonicalization of the specified
598 // arch?
599
Daniel Dunbar75877192009-03-19 07:55:12 +0000600 A->claim();
Daniel Dunbar13689542009-03-13 20:33:35 +0000601 if (ArchNames.insert(Name))
602 Archs.push_back(Name);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000603 }
604 }
605
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000606 // When there is no explicit arch for this platform, make sure we
607 // still bind the architecture (to the default) so that -Xarch_ is
608 // handled correctly.
609 if (!Archs.size())
610 Archs.push_back(0);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000611
612 // FIXME: We killed off some others but these aren't yet detected in
613 // a functional manner. If we added information to jobs about which
614 // "auxiliary" files they wrote then we could detect the conflict
615 // these cause downstream.
616 if (Archs.size() > 1) {
617 // No recovery needed, the point of this is just to prevent
618 // overwriting the same files.
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000619 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
620 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000621 << A->getAsString(Args);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000622 }
623
624 ActionList SingleActions;
625 BuildActions(Args, SingleActions);
626
627 // Add in arch binding and lipo (if necessary) for every top level
628 // action.
629 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
630 Action *Act = SingleActions[i];
631
632 // Make sure we can lipo this kind of output. If not (and it is an
633 // actual output) then we disallow, since we can't create an
634 // output file with the right name without overwriting it. We
635 // could remove this oddity by just changing the output names to
636 // include the arch, which would also fix
637 // -save-temps. Compatibility wins for now.
638
Daniel Dunbar3dbd6c52009-03-13 17:46:02 +0000639 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000640 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
641 << types::getTypeName(Act->getType());
642
643 ActionList Inputs;
Daniel Dunbar75877192009-03-19 07:55:12 +0000644 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbar13689542009-03-13 20:33:35 +0000645 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000646
647 // Lipo if necessary, We do it this way because we need to set the
648 // arch flag so that -Xarch_ gets overwritten.
649 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
650 Actions.append(Inputs.begin(), Inputs.end());
651 else
652 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
653 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000654}
655
Daniel Dunbar21549232009-03-18 02:55:38 +0000656void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000657 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000658 // Start by constructing the list of inputs and their types.
659
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000660 // Track the current user specified (-x) input. We also explicitly
661 // track the argument used to set the type; we only want to claim
662 // the type when we actually use it, so we warn about unused -x
663 // arguments.
664 types::ID InputType = types::TY_Nothing;
665 Arg *InputTypeArg = 0;
666
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000667 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
668 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
669 it != ie; ++it) {
670 Arg *A = *it;
671
672 if (isa<InputOption>(A->getOption())) {
673 const char *Value = A->getValue(Args);
674 types::ID Ty = types::TY_INVALID;
675
676 // Infer the input type if necessary.
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000677 if (InputType == types::TY_Nothing) {
678 // If there was an explicit arg for this, claim it.
679 if (InputTypeArg)
680 InputTypeArg->claim();
681
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000682 // stdin must be handled specially.
683 if (memcmp(Value, "-", 2) == 0) {
684 // If running with -E, treat as a C input (this changes the
685 // builtin macros, for example). This may be overridden by
686 // -ObjC below.
687 //
688 // Otherwise emit an error but still use a valid type to
689 // avoid spurious errors (e.g., no inputs).
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000690 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000691 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000692 Ty = types::TY_C;
693 } else {
694 // Otherwise lookup by extension, and fallback to ObjectType
Daniel Dunbare33bea42009-03-20 23:39:23 +0000695 // if not found. We use a host hook here because Darwin at
696 // least has its own idea of what .s is.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000697 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbare33bea42009-03-20 23:39:23 +0000698 Ty = Host->lookupTypeForExtension(Ext + 1);
699
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000700 if (Ty == types::TY_INVALID)
701 Ty = types::TY_Object;
702 }
703
Daniel Dunbar683ca382009-05-18 21:47:54 +0000704 // -ObjC and -ObjC++ override the default language, but only for "source
705 // files". We just treat everything that isn't a linker input as a
706 // source file.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000707 //
Daniel Dunbar683ca382009-05-18 21:47:54 +0000708 // FIXME: Clean this up if we move the phase sequence into the type.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000709 if (Ty != types::TY_Object) {
710 if (Args.hasArg(options::OPT_ObjC))
711 Ty = types::TY_ObjC;
712 else if (Args.hasArg(options::OPT_ObjCXX))
713 Ty = types::TY_ObjCXX;
714 }
715 } else {
716 assert(InputTypeArg && "InputType set w/o InputTypeArg");
717 InputTypeArg->claim();
718 Ty = InputType;
719 }
720
721 // Check that the file exists. It isn't clear this is worth
722 // doing, since the tool presumably does this anyway, and this
723 // just adds an extra stat to the equation, but this is gcc
724 // compatible.
725 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000726 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000727 else
728 Inputs.push_back(std::make_pair(Ty, A));
729
730 } else if (A->getOption().isLinkerInput()) {
731 // Just treat as object type, we could make a special type for
732 // this if necessary.
733 Inputs.push_back(std::make_pair(types::TY_Object, A));
734
735 } else if (A->getOption().getId() == options::OPT_x) {
736 InputTypeArg = A;
737 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
738
739 // Follow gcc behavior and treat as linker input for invalid -x
740 // options. Its not clear why we shouldn't just revert to
741 // unknown; but this isn't very important, we might as well be
742 // bug comatible.
743 if (!InputType) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000744 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000745 InputType = types::TY_Object;
746 }
747 }
748 }
749
Daniel Dunbar8b1604e2009-03-13 00:17:48 +0000750 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000751 Diag(clang::diag::err_drv_no_input_files);
752 return;
753 }
754
755 // Determine which compilation mode we are in. We look for options
756 // which affect the phase, starting with the earliest phases, and
757 // record which option we used to determine the final phase.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000758 Arg *FinalPhaseArg = 0;
759 phases::ID FinalPhase;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000760
761 // -{E,M,MM} only run the preprocessor.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000762 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
763 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
764 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
765 FinalPhase = phases::Preprocess;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000766
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000767 // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler.
768 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
Daniel Dunbar63be57a2009-05-06 02:12:32 +0000769 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze,
770 options::OPT__analyze_auto)) ||
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000771 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
772 FinalPhase = phases::Compile;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000773
774 // -c only runs up to the assembler.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000775 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
776 FinalPhase = phases::Assemble;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000777
778 // Otherwise do everything.
779 } else
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000780 FinalPhase = phases::Link;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000781
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000782 // Reject -Z* at the top level, these options should never have been
783 // exposed by gcc.
Daniel Dunbard7b88c22009-03-26 16:12:09 +0000784 if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000785 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000786
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000787 // Construct the actions to perform.
788 ActionList LinkerInputs;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000789 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000790 types::ID InputType = Inputs[i].first;
791 const Arg *InputArg = Inputs[i].second;
792
793 unsigned NumSteps = types::getNumCompilationPhases(InputType);
794 assert(NumSteps && "Invalid number of steps!");
795
796 // If the first step comes after the final phase we are doing as
797 // part of this compilation, warn the user about it.
798 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
799 if (InitialPhase > FinalPhase) {
Daniel Dunbar05494a72009-03-19 07:57:08 +0000800 // Claim here to avoid the more general unused warning.
801 InputArg->claim();
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000802 Diag(clang::diag::warn_drv_input_file_unused)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000803 << InputArg->getAsString(Args)
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000804 << getPhaseName(InitialPhase)
805 << FinalPhaseArg->getOption().getName();
806 continue;
807 }
808
809 // Build the pipeline for this file.
810 Action *Current = new InputAction(*InputArg, InputType);
811 for (unsigned i = 0; i != NumSteps; ++i) {
812 phases::ID Phase = types::getCompilationPhase(InputType, i);
813
814 // We are done if this step is past what the user requested.
815 if (Phase > FinalPhase)
816 break;
817
818 // Queue linker inputs.
819 if (Phase == phases::Link) {
820 assert(i + 1 == NumSteps && "linking must be final compilation step.");
821 LinkerInputs.push_back(Current);
822 Current = 0;
823 break;
824 }
825
Daniel Dunbar337a6272009-03-24 20:17:30 +0000826 // Some types skip the assembler phase (e.g., llvm-bc), but we
827 // can't encode this in the steps because the intermediate type
828 // depends on arguments. Just special case here.
829 if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
830 continue;
831
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000832 // Otherwise construct the appropriate action.
833 Current = ConstructPhaseAction(Args, Phase, Current);
834 if (Current->getType() == types::TY_Nothing)
835 break;
836 }
837
838 // If we ended with something, add to the output list.
839 if (Current)
840 Actions.push_back(Current);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000841 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000842
843 // Add a link action if necessary.
844 if (!LinkerInputs.empty())
845 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
846}
847
848Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
849 Action *Input) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000850 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000851 // Build the appropriate action.
852 switch (Phase) {
853 case phases::Link: assert(0 && "link action invalid here.");
854 case phases::Preprocess: {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +0000855 types::ID OutputTy;
856 // -{M, MM} alter the output type.
857 if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) {
858 OutputTy = types::TY_Dependencies;
859 } else {
860 OutputTy = types::getPreprocessedType(Input->getType());
861 assert(OutputTy != types::TY_INVALID &&
862 "Cannot preprocess this input type!");
863 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000864 return new PreprocessJobAction(Input, OutputTy);
865 }
866 case phases::Precompile:
867 return new PrecompileJobAction(Input, types::TY_PCH);
868 case phases::Compile: {
869 if (Args.hasArg(options::OPT_fsyntax_only)) {
870 return new CompileJobAction(Input, types::TY_Nothing);
Daniel Dunbar63be57a2009-05-06 02:12:32 +0000871 } else if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000872 return new AnalyzeJobAction(Input, types::TY_Plist);
Daniel Dunbar337a6272009-03-24 20:17:30 +0000873 } else if (Args.hasArg(options::OPT_emit_llvm) ||
874 Args.hasArg(options::OPT_flto) ||
875 Args.hasArg(options::OPT_O4)) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000876 types::ID Output =
877 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
878 return new CompileJobAction(Input, Output);
879 } else {
880 return new CompileJobAction(Input, types::TY_PP_Asm);
881 }
882 }
883 case phases::Assemble:
884 return new AssembleJobAction(Input, types::TY_Object);
885 }
886
887 assert(0 && "invalid phase in ConstructPhaseAction");
888 return 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000889}
890
Daniel Dunbar21549232009-03-18 02:55:38 +0000891void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000892 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000893 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
894 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000895
896 // FIXME: Pipes are forcibly disabled until we support executing
897 // them.
898 if (!CCCPrintBindings)
899 UsePipes = false;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000900
901 // -save-temps inhibits pipes.
902 if (SaveTemps && UsePipes) {
903 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
904 UsePipes = true;
905 }
906
907 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
908
909 // It is an error to provide a -o option if we are making multiple
910 // output files.
911 if (FinalOutput) {
912 unsigned NumOutputs = 0;
Daniel Dunbar21549232009-03-18 02:55:38 +0000913 for (ActionList::const_iterator it = C.getActions().begin(),
914 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000915 if ((*it)->getType() != types::TY_Nothing)
916 ++NumOutputs;
917
918 if (NumOutputs > 1) {
919 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
920 FinalOutput = 0;
921 }
922 }
923
Daniel Dunbar21549232009-03-18 02:55:38 +0000924 for (ActionList::const_iterator it = C.getActions().begin(),
925 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000926 Action *A = *it;
927
928 // If we are linking an image for multiple archs then the linker
929 // wants -arch_multiple and -final_output <final image
930 // name>. Unfortunately, this doesn't fit in cleanly because we
931 // have to pass this information down.
932 //
933 // FIXME: This is a hack; find a cleaner way to integrate this
934 // into the process.
935 const char *LinkingOutput = 0;
Daniel Dunbard7b88c22009-03-26 16:12:09 +0000936 if (isa<LipoJobAction>(A)) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000937 if (FinalOutput)
938 LinkingOutput = FinalOutput->getValue(C.getArgs());
939 else
940 LinkingOutput = DefaultImageName.c_str();
941 }
942
943 InputInfo II;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000944 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000945 /*CanAcceptPipe*/ true,
946 /*AtTopLevel*/ true,
947 /*LinkingOutput*/ LinkingOutput,
948 II);
949 }
Daniel Dunbar586dc232009-03-16 06:42:30 +0000950
Daniel Dunbarbf4a6762009-04-03 22:09:23 +0000951 // If the user passed -Qunused-arguments or there were errors, don't
952 // warn about any unused arguments.
Daniel Dunbar1e23f5f2009-04-07 19:04:18 +0000953 if (Diags.getNumErrors() ||
954 C.getArgs().hasArg(options::OPT_Qunused_arguments))
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000955 return;
956
Daniel Dunbara2094e72009-03-29 22:24:54 +0000957 // Claim -### here.
958 (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
959
Daniel Dunbar586dc232009-03-16 06:42:30 +0000960 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
961 it != ie; ++it) {
962 Arg *A = *it;
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000963
Daniel Dunbar586dc232009-03-16 06:42:30 +0000964 // FIXME: It would be nice to be able to send the argument to the
965 // Diagnostic, so that extra values, position, and so on could be
966 // printed.
Daniel Dunbar4f53b292009-04-04 00:52:26 +0000967 if (!A->isClaimed()) {
Daniel Dunbar1e23f5f2009-04-07 19:04:18 +0000968 if (A->getOption().hasNoArgumentUnused())
969 continue;
970
Daniel Dunbar4f53b292009-04-04 00:52:26 +0000971 // Suppress the warning automatically if this is just a flag,
972 // and it is an instance of an argument we already claimed.
973 const Option &Opt = A->getOption();
974 if (isa<FlagOption>(Opt)) {
975 bool DuplicateClaimed = false;
976
977 // FIXME: Use iterator.
978 for (ArgList::const_iterator it = C.getArgs().begin(),
979 ie = C.getArgs().end(); it != ie; ++it) {
980 if ((*it)->isClaimed() && (*it)->getOption().matches(Opt.getId())) {
981 DuplicateClaimed = true;
982 break;
983 }
984 }
985
986 if (DuplicateClaimed)
987 continue;
988 }
989
Daniel Dunbar586dc232009-03-16 06:42:30 +0000990 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000991 << A->getAsString(C.getArgs());
Daniel Dunbar4f53b292009-04-04 00:52:26 +0000992 }
Daniel Dunbar586dc232009-03-16 06:42:30 +0000993 }
Daniel Dunbar57b704d2009-03-13 22:12:33 +0000994}
995
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000996void Driver::BuildJobsForAction(Compilation &C,
997 const Action *A,
998 const ToolChain *TC,
999 bool CanAcceptPipe,
1000 bool AtTopLevel,
1001 const char *LinkingOutput,
1002 InputInfo &Result) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +00001003 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action");
Daniel Dunbar60ccc762009-03-18 23:18:19 +00001004
1005 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
1006 // FIXME: Pipes are forcibly disabled until we support executing
1007 // them.
1008 if (!CCCPrintBindings)
1009 UsePipes = false;
1010
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001011 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar115a7922009-03-19 07:29:38 +00001012 // FIXME: It would be nice to not claim this here; maybe the old
1013 // scheme of just using Args was better?
1014 const Arg &Input = IA->getInputArg();
1015 Input.claim();
1016 if (isa<PositionalArg>(Input)) {
1017 const char *Name = Input.getValue(C.getArgs());
1018 Result = InputInfo(Name, A->getType(), Name);
1019 } else
1020 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001021 return;
1022 }
1023
1024 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
1025 const char *ArchName = BAA->getArchName();
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001026 std::string Arch;
1027 if (!ArchName) {
1028 Arch = C.getDefaultToolChain().getArchName();
1029 ArchName = Arch.c_str();
1030 }
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001031 BuildJobsForAction(C,
1032 *BAA->begin(),
1033 Host->getToolChain(C.getArgs(), ArchName),
1034 CanAcceptPipe,
1035 AtTopLevel,
1036 LinkingOutput,
1037 Result);
1038 return;
1039 }
1040
1041 const JobAction *JA = cast<JobAction>(A);
1042 const Tool &T = TC->SelectTool(C, *JA);
1043
1044 // See if we should use an integrated preprocessor. We do so when we
1045 // have exactly one input, since this is the only use case we care
1046 // about (irrelevant since we don't support combine yet).
1047 bool UseIntegratedCPP = false;
1048 const ActionList *Inputs = &A->getInputs();
1049 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
1050 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
1051 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
1052 !C.getArgs().hasArg(options::OPT_save_temps) &&
1053 T.hasIntegratedCPP()) {
1054 UseIntegratedCPP = true;
1055 Inputs = &(*Inputs)[0]->getInputs();
1056 }
1057 }
1058
1059 // Only use pipes when there is exactly one input.
1060 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001061 InputInfoList InputInfos;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001062 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
1063 it != ie; ++it) {
1064 InputInfo II;
1065 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
1066 /*AtTopLevel*/false,
1067 LinkingOutput,
1068 II);
1069 InputInfos.push_back(II);
1070 }
1071
1072 // Determine if we should output to a pipe.
1073 bool OutputToPipe = false;
1074 if (CanAcceptPipe && T.canPipeOutput()) {
1075 // Some actions default to writing to a pipe if they are the top
1076 // level phase and there was no user override.
1077 //
1078 // FIXME: Is there a better way to handle this?
1079 if (AtTopLevel) {
1080 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
1081 OutputToPipe = true;
Daniel Dunbar60ccc762009-03-18 23:18:19 +00001082 } else if (UsePipes)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001083 OutputToPipe = true;
1084 }
1085
1086 // Figure out where to put the job (pipes).
1087 Job *Dest = &C.getJobs();
1088 if (InputInfos[0].isPipe()) {
Daniel Dunbar441d0602009-03-17 17:53:55 +00001089 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001090 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
1091 Dest = &InputInfos[0].getPipe();
1092 }
1093
1094 // Always use the first input as the base input.
1095 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar441d0602009-03-17 17:53:55 +00001096
1097 // Determine the place to write output to (nothing, pipe, or
1098 // filename) and where to put the new job.
Daniel Dunbar441d0602009-03-17 17:53:55 +00001099 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001100 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001101 } else if (OutputToPipe) {
1102 // Append to current piped job or create a new one as appropriate.
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001103 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
1104 if (!PJ) {
1105 PJ = new PipedJob();
Daniel Dunbarb7b61b22009-03-20 00:11:04 +00001106 // FIXME: Temporary hack so that -ccc-print-bindings work until
1107 // we have pipe support. Please remove later.
1108 if (!CCCPrintBindings)
1109 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar871adcf2009-03-18 07:06:02 +00001110 Dest = PJ;
Daniel Dunbar441d0602009-03-17 17:53:55 +00001111 }
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001112 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001113 } else {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001114 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
1115 A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001116 }
1117
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001118 if (CCCPrintBindings) {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00001119 llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"'
1120 << " - \"" << T.getName() << "\", inputs: [";
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001121 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
1122 llvm::errs() << InputInfos[i].getAsString();
1123 if (i + 1 != e)
1124 llvm::errs() << ", ";
1125 }
1126 llvm::errs() << "], output: " << Result.getAsString() << "\n";
1127 } else {
Daniel Dunbarf3cad362009-03-25 04:13:45 +00001128 T.ConstructJob(C, *JA, *Dest, Result, InputInfos,
1129 C.getArgsForToolChain(TC), LinkingOutput);
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001130 }
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001131}
1132
Daniel Dunbar441d0602009-03-17 17:53:55 +00001133const char *Driver::GetNamedOutputPath(Compilation &C,
1134 const JobAction &JA,
1135 const char *BaseInput,
1136 bool AtTopLevel) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +00001137 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar441d0602009-03-17 17:53:55 +00001138 // Output to a user requested destination?
1139 if (AtTopLevel) {
1140 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
1141 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
1142 }
1143
1144 // Output to a temporary file?
1145 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbar214399e2009-03-18 19:34:39 +00001146 std::string TmpName =
1147 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
1148 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar441d0602009-03-17 17:53:55 +00001149 }
1150
1151 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbar5796bf42009-03-18 02:00:31 +00001152 std::string BaseName(BasePath.getLast());
Daniel Dunbar441d0602009-03-17 17:53:55 +00001153
1154 // Determine what the derived output name should be.
1155 const char *NamedOutput;
1156 if (JA.getType() == types::TY_Image) {
1157 NamedOutput = DefaultImageName.c_str();
1158 } else {
1159 const char *Suffix = types::getTypeTempSuffix(JA.getType());
1160 assert(Suffix && "All types used for output should have a suffix.");
1161
1162 std::string::size_type End = std::string::npos;
1163 if (!types::appendSuffixForType(JA.getType()))
1164 End = BaseName.rfind('.');
1165 std::string Suffixed(BaseName.substr(0, End));
1166 Suffixed += '.';
1167 Suffixed += Suffix;
1168 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
1169 }
1170
1171 // As an annoying special case, PCH generation doesn't strip the
1172 // pathname.
1173 if (JA.getType() == types::TY_PCH) {
1174 BasePath.eraseComponent();
Daniel Dunbar56c55942009-03-18 09:58:30 +00001175 if (BasePath.isEmpty())
1176 BasePath = NamedOutput;
1177 else
1178 BasePath.appendComponent(NamedOutput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001179 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
1180 } else {
1181 return C.addResultFile(NamedOutput);
1182 }
1183}
1184
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +00001185llvm::sys::Path Driver::GetFilePath(const char *Name,
Daniel Dunbar21549232009-03-18 02:55:38 +00001186 const ToolChain &TC) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001187 const ToolChain::path_list &List = TC.getFilePaths();
1188 for (ToolChain::path_list::const_iterator
1189 it = List.begin(), ie = List.end(); it != ie; ++it) {
1190 llvm::sys::Path P(*it);
1191 P.appendComponent(Name);
1192 if (P.exists())
1193 return P;
1194 }
1195
Daniel Dunbarcb881672009-03-13 00:51:18 +00001196 return llvm::sys::Path(Name);
1197}
1198
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +00001199llvm::sys::Path Driver::GetProgramPath(const char *Name,
Mike Stump950bedd2009-03-27 00:40:20 +00001200 const ToolChain &TC,
1201 bool WantFile) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001202 const ToolChain::path_list &List = TC.getProgramPaths();
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);
Mike Stump950bedd2009-03-27 00:40:20 +00001207 if (WantFile ? P.exists() : P.canExecute())
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001208 return P;
1209 }
1210
Daniel Dunbarc50b00d2009-03-23 16:15:50 +00001211 // If all else failed, search the path.
1212 llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
Daniel Dunbar632f50e2009-03-18 21:34:08 +00001213 if (!P.empty())
1214 return P;
1215
Daniel Dunbarcb881672009-03-13 00:51:18 +00001216 return llvm::sys::Path(Name);
1217}
1218
Daniel Dunbar214399e2009-03-18 19:34:39 +00001219std::string Driver::GetTemporaryPath(const char *Suffix) const {
1220 // FIXME: This is lame; sys::Path should provide this function (in
1221 // particular, it should know how to find the temporary files dir).
1222 std::string Error;
Daniel Dunbarb03417f2009-04-20 20:28:21 +00001223 const char *TmpDir = ::getenv("TMPDIR");
1224 if (!TmpDir)
1225 TmpDir = ::getenv("TEMP");
1226 if (!TmpDir)
Daniel Dunbar3ca7ee92009-04-21 00:25:10 +00001227 TmpDir = ::getenv("TMP");
1228 if (!TmpDir)
Daniel Dunbarb03417f2009-04-20 20:28:21 +00001229 TmpDir = "/tmp";
1230 llvm::sys::Path P(TmpDir);
Daniel Dunbarf60c63a2009-04-20 17:32:49 +00001231 P.appendComponent("cc");
Daniel Dunbar214399e2009-03-18 19:34:39 +00001232 if (P.makeUnique(false, &Error)) {
1233 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
1234 return "";
1235 }
1236
Daniel Dunbar84603bc2009-03-18 23:08:52 +00001237 // FIXME: Grumble, makeUnique sometimes leaves the file around!?
1238 // PR3837.
1239 P.eraseFromDisk(false, 0);
1240
Daniel Dunbar214399e2009-03-18 19:34:39 +00001241 P.appendSuffix(Suffix);
1242 return P.toString();
1243}
1244
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001245const HostInfo *Driver::GetHostInfo(const char *TripleStr) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +00001246 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001247 llvm::Triple Triple(TripleStr);
Daniel Dunbardd98e2c2009-03-10 23:41:59 +00001248
Daniel Dunbar1fd6c4b2009-03-17 19:00:50 +00001249 // Normalize Arch a bit.
1250 //
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001251 // FIXME: We shouldn't need to do this once everything goes through the triple
1252 // interface.
1253 if (Triple.getArchName() == "i686")
1254 Triple.setArchName("i386");
1255 else if (Triple.getArchName() == "amd64")
1256 Triple.setArchName("x86_64");
1257 else if (Triple.getArchName() == "ppc" ||
1258 Triple.getArchName() == "Power Macintosh")
1259 Triple.setArchName("powerpc");
1260 else if (Triple.getArchName() == "ppc64")
1261 Triple.setArchName("powerpc64");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001262
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001263 switch (Triple.getOS()) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001264 case llvm::Triple::AuroraUX:
1265 return createAuroraUXHostInfo(*this, Triple);
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001266 case llvm::Triple::Darwin:
1267 return createDarwinHostInfo(*this, Triple);
1268 case llvm::Triple::DragonFly:
1269 return createDragonFlyHostInfo(*this, Triple);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001270 case llvm::Triple::OpenBSD:
1271 return createOpenBSDHostInfo(*this, Triple);
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001272 case llvm::Triple::FreeBSD:
1273 return createFreeBSDHostInfo(*this, Triple);
Eli Friedman6b3454a2009-05-26 07:52:18 +00001274 case llvm::Triple::Linux:
1275 return createLinuxHostInfo(*this, Triple);
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001276 default:
1277 return createUnknownHostInfo(*this, Triple);
1278 }
Daniel Dunbardd98e2c2009-03-10 23:41:59 +00001279}
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001280
1281bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
Daniel Dunbarbf54a062009-04-01 20:33:11 +00001282 const std::string &ArchNameStr) const {
1283 // FIXME: Remove this hack.
1284 const char *ArchName = ArchNameStr.c_str();
1285 if (ArchNameStr == "powerpc")
1286 ArchName = "ppc";
1287 else if (ArchNameStr == "powerpc64")
1288 ArchName = "ppc64";
1289
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001290 // Check if user requested no clang, or clang doesn't understand
1291 // this type (we only handle single inputs for now).
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001292 if (!CCCUseClang || JA.size() != 1 ||
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001293 !types::isAcceptedByClang((*JA.begin())->getType()))
1294 return false;
1295
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001296 // Otherwise make sure this is an action clang understands.
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001297 if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbar6256d362009-03-24 19:14:56 +00001298 if (!CCCUseClangCPP) {
1299 Diag(clang::diag::warn_drv_not_using_clang_cpp);
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001300 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001301 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001302 } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
1303 return false;
1304
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001305 // Use clang for C++?
Daniel Dunbar6256d362009-03-24 19:14:56 +00001306 if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
1307 Diag(clang::diag::warn_drv_not_using_clang_cxx);
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001308 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001309 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001310
Daniel Dunbarfec26bd2009-04-16 23:10:13 +00001311 // Always use clang for precompiling, regardless of archs. PTH is
1312 // platform independent, and this allows the use of the static
1313 // analyzer on platforms we don't have full IRgen support for.
1314 if (isa<PrecompileJobAction>(JA))
1315 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}