blob: 8adeb02b901dcc03330432a2cb81734657888176 [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 Dunbardd98e2c2009-03-10 23:41:59 +000040Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000041 const char *_DefaultHostTriple,
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000042 const char *_DefaultImageName,
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000043 Diagnostic &_Diags)
44 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000045 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000046 DefaultImageName(_DefaultImageName),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000047 Host(0),
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +000048 CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false),
Daniel Dunbardfaf4b32009-07-23 17:48:59 +000049 CCCGenericGCCName("gcc"), CCCUseClang(true), CCCUseClangCXX(true),
Douglas Gregor214e8722009-04-28 22:44:02 +000050 CCCUseClangCPP(true), CCCUsePCH(true),
Daniel Dunbar8b1604e2009-03-13 00:17:48 +000051 SuppressMissingInputWarning(false)
Daniel Dunbar365c02f2009-03-10 20:52:46 +000052{
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000053}
54
55Driver::~Driver() {
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000056 delete Opts;
Daniel Dunbar7e4534d2009-03-18 01:09:40 +000057 delete Host;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000058}
59
Daniel Dunbarf3cad362009-03-25 04:13:45 +000060InputArgList *Driver::ParseArgStrings(const char **ArgBegin,
61 const char **ArgEnd) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +000062 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbarf3cad362009-03-25 04:13:45 +000063 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
Daniel Dunbar06482622009-03-05 06:38:47 +000064
Daniel Dunbarad2a9af2009-03-13 11:38:42 +000065 // FIXME: Handle '@' args (or at least error on them).
66
Daniel Dunbar06482622009-03-05 06:38:47 +000067 unsigned Index = 0, End = ArgEnd - ArgBegin;
68 while (Index < End) {
Daniel Dunbar41393402009-03-13 01:01:44 +000069 // gcc's handling of empty arguments doesn't make
70 // sense, but this is not a common use case. :)
71 //
72 // We just ignore them here (note that other things may
73 // still take them as arguments).
74 if (Args->getArgString(Index)[0] == '\0') {
75 ++Index;
76 continue;
77 }
78
Daniel Dunbar06482622009-03-05 06:38:47 +000079 unsigned Prev = Index;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000080 Arg *A = getOpts().ParseOneArg(*Args, Index);
81 assert(Index > Prev && "Parser failed to consume argument.");
Daniel Dunbar53ec5522009-03-12 07:58:46 +000082
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000083 // Check for missing argument error.
84 if (!A) {
85 assert(Index >= End && "Unexpected parser error.");
86 Diag(clang::diag::err_drv_missing_argument)
87 << Args->getArgString(Prev)
88 << (Index - Prev - 1);
89 break;
Daniel Dunbar53ec5522009-03-12 07:58:46 +000090 }
Daniel Dunbar06482622009-03-05 06:38:47 +000091
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000092 if (A->getOption().isUnsupported()) {
93 Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
94 continue;
95 }
96 Args->append(A);
Daniel Dunbar06482622009-03-05 06:38:47 +000097 }
98
99 return Args;
100}
101
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000102Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000103 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
104
Daniel Dunbarcb881672009-03-13 00:51:18 +0000105 // FIXME: Handle environment options which effect driver behavior,
106 // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH,
107 // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS.
108
109 // FIXME: What are we going to do with -V and -b?
110
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000111 // FIXME: This stuff needs to go into the Compilation, not the
112 // driver.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000113 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar06482622009-03-05 06:38:47 +0000114
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000115 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000116 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000117
118 // Read -ccc args.
119 //
120 // FIXME: We need to figure out where this behavior should
121 // live. Most of it should be outside in the client; the parts that
122 // aren't should have proper options, either by introducing new ones
123 // or by overloading gcc ones like -V or -b.
124 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
125 const char *Opt = *Start + 5;
126
127 if (!strcmp(Opt, "print-options")) {
128 CCCPrintOptions = true;
129 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000130 CCCPrintActions = true;
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000131 } else if (!strcmp(Opt, "print-bindings")) {
132 CCCPrintBindings = true;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000133 } else if (!strcmp(Opt, "cxx")) {
134 CCCIsCXX = true;
135 } else if (!strcmp(Opt, "echo")) {
136 CCCEcho = true;
137
Daniel Dunbar78d8a082009-04-01 23:34:41 +0000138 } else if (!strcmp(Opt, "gcc-name")) {
139 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
140 CCCGenericGCCName = *++Start;
141
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000142 } else if (!strcmp(Opt, "clang-cxx")) {
143 CCCUseClangCXX = true;
Daniel Dunbardfaf4b32009-07-23 17:48:59 +0000144 } else if (!strcmp(Opt, "no-clang-cxx")) {
145 CCCUseClangCXX = false;
Douglas Gregordf91ef32009-04-18 00:34:01 +0000146 } else if (!strcmp(Opt, "pch-is-pch")) {
147 CCCUsePCH = true;
148 } else if (!strcmp(Opt, "pch-is-pth")) {
149 CCCUsePCH = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000150 } else if (!strcmp(Opt, "no-clang")) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000151 CCCUseClang = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000152 } else if (!strcmp(Opt, "no-clang-cpp")) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000153 CCCUseClangCPP = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000154 } else if (!strcmp(Opt, "clang-archs")) {
155 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
156 const char *Cur = *++Start;
157
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000158 CCCClangArchs.clear();
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000159 for (;;) {
160 const char *Next = strchr(Cur, ',');
161
162 if (Next) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000163 if (Cur != Next)
164 CCCClangArchs.insert(std::string(Cur, Next));
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000165 Cur = Next + 1;
166 } else {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000167 if (*Cur != '\0')
168 CCCClangArchs.insert(std::string(Cur));
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000169 break;
170 }
171 }
172
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000173 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000174 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000175 HostTriple = *++Start;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000176
177 } else {
178 // FIXME: Error handling.
179 llvm::errs() << "invalid option: " << *Start << "\n";
180 exit(1);
181 }
182 }
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000183
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000184 InputArgList *Args = ParseArgStrings(Start, End);
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000185
Daniel Dunbare5049522009-03-17 20:45:45 +0000186 Host = GetHostInfo(HostTriple);
Daniel Dunbarcb881672009-03-13 00:51:18 +0000187
Daniel Dunbar586dc232009-03-16 06:42:30 +0000188 // The compilation takes ownership of Args.
Daniel Dunbare530ad42009-03-18 22:16:03 +0000189 Compilation *C = new Compilation(*this, *Host->getToolChain(*Args), Args);
Daniel Dunbar21549232009-03-18 02:55:38 +0000190
191 // FIXME: This behavior shouldn't be here.
192 if (CCCPrintOptions) {
193 PrintOptions(C->getArgs());
194 return C;
195 }
196
197 if (!HandleImmediateArgs(*C))
198 return C;
199
200 // Construct the list of abstract actions to perform for this
201 // compilation. We avoid passing a Compilation here simply to
202 // enforce the abstraction that pipelining is not host or toolchain
203 // dependent (other than the driver driver test).
204 if (Host->useDriverDriver())
205 BuildUniversalActions(C->getArgs(), C->getActions());
206 else
207 BuildActions(C->getArgs(), C->getActions());
208
209 if (CCCPrintActions) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000210 PrintActions(*C);
Daniel Dunbar21549232009-03-18 02:55:38 +0000211 return C;
212 }
213
214 BuildJobs(*C);
Daniel Dunbar8d2554a2009-03-15 01:38:15 +0000215
216 return C;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000217}
218
Daniel Dunbarc88a88f2009-07-01 20:03:04 +0000219int Driver::ExecuteCompilation(const Compilation &C) const {
220 // Just print if -### was present.
221 if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
222 C.PrintJob(llvm::errs(), C.getJobs(), "\n", true);
223 return 0;
224 }
225
226 // If there were errors building the compilation, quit now.
227 if (getDiags().getNumErrors())
228 return 1;
229
230 const Command *FailingCommand = 0;
231 int Res = C.ExecuteJob(C.getJobs(), FailingCommand);
232
233 // Remove temp files.
234 C.CleanupFileList(C.getTempFiles());
235
236 // If the compilation failed, remove result files as well.
237 if (Res != 0 && !C.getArgs().hasArg(options::OPT_save_temps))
238 C.CleanupFileList(C.getResultFiles(), true);
239
240 // Print extra information about abnormal failures, if possible.
241 if (Res) {
242 // This is ad-hoc, but we don't want to be excessively noisy. If the result
243 // status was 1, assume the command failed normally. In particular, if it
244 // was the compiler then assume it gave a reasonable error code. Failures in
245 // other tools are less common, and they generally have worse diagnostics,
246 // so always print the diagnostic there.
247 const Action &Source = FailingCommand->getSource();
248 bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) ||
249 isa<PrecompileJobAction>(Source) ||
250 isa<AnalyzeJobAction>(Source) ||
251 isa<CompileJobAction>(Source));
252
253 if (!IsFriendlyTool || Res != 1) {
254 // FIXME: See FIXME above regarding result code interpretation.
255 if (Res < 0)
256 Diag(clang::diag::err_drv_command_signalled)
257 << Source.getClassName() << -Res;
258 else
259 Diag(clang::diag::err_drv_command_failed)
260 << Source.getClassName() << Res;
261 }
262 }
263
264 return Res;
265}
266
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000267void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar06482622009-03-05 06:38:47 +0000268 unsigned i = 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000269 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar06482622009-03-05 06:38:47 +0000270 it != ie; ++it, ++i) {
271 Arg *A = *it;
272 llvm::errs() << "Option " << i << " - "
273 << "Name: \"" << A->getOption().getName() << "\", "
274 << "Values: {";
275 for (unsigned j = 0; j < A->getNumValues(); ++j) {
276 if (j)
277 llvm::errs() << ", ";
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000278 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar06482622009-03-05 06:38:47 +0000279 }
280 llvm::errs() << "}\n";
Daniel Dunbar06482622009-03-05 06:38:47 +0000281 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000282}
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000283
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000284static std::string getOptionHelpName(const OptTable &Opts, options::ID Id) {
285 std::string Name = Opts.getOptionName(Id);
286
287 // Add metavar, if used.
288 switch (Opts.getOptionKind(Id)) {
289 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
290 assert(0 && "Invalid option with help text.");
291
292 case Option::MultiArgClass: case Option::JoinedAndSeparateClass:
293 assert(0 && "Cannot print metavar for this kind of option.");
294
295 case Option::FlagClass:
296 break;
297
298 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
299 Name += ' ';
300 // FALLTHROUGH
301 case Option::JoinedClass: case Option::CommaJoinedClass:
302 Name += Opts.getOptionMetaVar(Id);
303 break;
304 }
305
306 return Name;
307}
308
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000309void Driver::PrintHelp(bool ShowHidden) const {
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000310 llvm::raw_ostream &OS = llvm::outs();
311
312 OS << "OVERVIEW: clang \"gcc-compatible\" driver\n";
313 OS << '\n';
314 OS << "USAGE: " << Name << " [options] <input files>\n";
315 OS << '\n';
316 OS << "OPTIONS:\n";
317
318 // Render help text into (option, help) pairs.
319 std::vector< std::pair<std::string, const char*> > OptionHelp;
320
321 for (unsigned i = options::OPT_INPUT, e = options::LastOption; i != e; ++i) {
322 options::ID Id = (options::ID) i;
323 if (const char *Text = getOpts().getOptionHelpText(Id))
324 OptionHelp.push_back(std::make_pair(getOptionHelpName(getOpts(), Id),
325 Text));
326 }
327
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000328 if (ShowHidden) {
329 OptionHelp.push_back(std::make_pair("\nDRIVER OPTIONS:",""));
330 OptionHelp.push_back(std::make_pair("-ccc-cxx",
331 "Act as a C++ driver"));
332 OptionHelp.push_back(std::make_pair("-ccc-gcc-name",
333 "Name for native GCC compiler"));
334 OptionHelp.push_back(std::make_pair("-ccc-clang-cxx",
335 "Use the clang compiler for C++"));
336 OptionHelp.push_back(std::make_pair("-ccc-no-clang",
337 "Never use the clang compiler"));
338 OptionHelp.push_back(std::make_pair("-ccc-no-clang-cpp",
339 "Never use the clang preprocessor"));
340 OptionHelp.push_back(std::make_pair("-ccc-clang-archs",
341 "Comma separate list of architectures "
342 "to use the clang compiler for"));
Douglas Gregordf91ef32009-04-18 00:34:01 +0000343 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pch",
344 "Use lazy PCH for precompiled headers"));
345 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pth",
346 "Use pretokenized headers for precompiled headers"));
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000347
348 OptionHelp.push_back(std::make_pair("\nDEBUG/DEVELOPMENT OPTIONS:",""));
349 OptionHelp.push_back(std::make_pair("-ccc-host-triple",
350 "Simulate running on the given target"));
351 OptionHelp.push_back(std::make_pair("-ccc-print-options",
352 "Dump parsed command line arguments"));
353 OptionHelp.push_back(std::make_pair("-ccc-print-phases",
354 "Dump list of actions to perform"));
355 OptionHelp.push_back(std::make_pair("-ccc-print-bindings",
356 "Show bindings of tools to actions"));
357 OptionHelp.push_back(std::make_pair("CCC_ADD_ARGS",
358 "(ENVIRONMENT VARIABLE) Comma separated list of "
359 "arguments to prepend to the command line"));
360 }
361
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000362 // Find the maximum option length.
363 unsigned OptionFieldWidth = 0;
364 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000365 // Skip titles.
366 if (!OptionHelp[i].second)
367 continue;
368
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000369 // Limit the amount of padding we are willing to give up for
370 // alignment.
371 unsigned Length = OptionHelp[i].first.size();
372 if (Length <= 23)
373 OptionFieldWidth = std::max(OptionFieldWidth, Length);
374 }
375
376 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
377 const std::string &Option = OptionHelp[i].first;
378 OS << " " << Option;
379 for (int j = Option.length(), e = OptionFieldWidth; j < e; ++j)
380 OS << ' ';
381 OS << ' ' << OptionHelp[i].second << '\n';
382 }
383
384 OS.flush();
385}
386
Daniel Dunbar79300722009-07-21 20:06:58 +0000387void Driver::PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const {
Mike Stump5d023c32009-03-18 14:00:02 +0000388 static char buf[] = "$URL$";
389 char *zap = strstr(buf, "/lib/Driver");
390 if (zap)
391 *zap = 0;
392 zap = strstr(buf, "/clang/tools/clang");
393 if (zap)
394 *zap = 0;
Mike Stumpe70295b2009-03-18 15:19:35 +0000395 const char *vers = buf+6;
Mike Stump8944c382009-03-18 18:45:55 +0000396 // FIXME: Add cmake support and remove #ifdef
397#ifdef SVN_REVISION
398 const char *revision = SVN_REVISION;
399#else
400 const char *revision = "";
401#endif
Daniel Dunbarcb881672009-03-13 00:51:18 +0000402 // FIXME: The following handlers should use a callback mechanism, we
403 // don't know what the client would like to do.
Daniel Dunbar79300722009-07-21 20:06:58 +0000404 OS << "clang version " CLANG_VERSION_STRING " ("
Daniel Dunbar3ee96ba2009-06-16 23:32:58 +0000405 << vers << " " << revision << ")" << '\n';
Daniel Dunbar70c8db12009-03-26 16:09:13 +0000406
407 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbar79300722009-07-21 20:06:58 +0000408 OS << "Target: " << TC.getTripleString() << '\n';
Daniel Dunbar3ee96ba2009-06-16 23:32:58 +0000409
410 // Print the threading model.
411 //
412 // FIXME: Implement correctly.
Daniel Dunbar79300722009-07-21 20:06:58 +0000413 OS << "Thread model: " << "posix" << '\n';
Daniel Dunbarcb881672009-03-13 00:51:18 +0000414}
415
Daniel Dunbar21549232009-03-18 02:55:38 +0000416bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000417 // The order these options are handled in in gcc is all over the
418 // place, but we don't expect inconsistencies w.r.t. that to matter
419 // in practice.
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000420
Daniel Dunbare06dc212009-04-04 05:17:38 +0000421 if (C.getArgs().hasArg(options::OPT_dumpversion)) {
Douglas Gregorab41e632009-04-27 22:23:34 +0000422 llvm::outs() << CLANG_VERSION_STRING "\n";
Daniel Dunbare06dc212009-04-04 05:17:38 +0000423 return false;
424 }
425
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000426 if (C.getArgs().hasArg(options::OPT__help) ||
427 C.getArgs().hasArg(options::OPT__help_hidden)) {
428 PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden));
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000429 return false;
430 }
431
Daniel Dunbar6cc73de2009-04-02 15:05:41 +0000432 if (C.getArgs().hasArg(options::OPT__version)) {
Daniel Dunbar79300722009-07-21 20:06:58 +0000433 // Follow gcc behavior and use stdout for --version and stderr for -v
434 PrintVersion(C, llvm::outs());
Daniel Dunbar6cc73de2009-04-02 15:05:41 +0000435 return false;
436 }
437
Daniel Dunbar21549232009-03-18 02:55:38 +0000438 if (C.getArgs().hasArg(options::OPT_v) ||
439 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbar79300722009-07-21 20:06:58 +0000440 PrintVersion(C, llvm::errs());
Daniel Dunbarcb881672009-03-13 00:51:18 +0000441 SuppressMissingInputWarning = true;
442 }
443
Daniel Dunbar21549232009-03-18 02:55:38 +0000444 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbarca3459e2009-03-20 04:37:21 +0000445 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
446 llvm::outs() << "programs: =";
447 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
448 ie = TC.getProgramPaths().end(); it != ie; ++it) {
449 if (it != TC.getProgramPaths().begin())
450 llvm::outs() << ':';
451 llvm::outs() << *it;
452 }
453 llvm::outs() << "\n";
454 llvm::outs() << "libraries: =";
455 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
456 ie = TC.getFilePaths().end(); it != ie; ++it) {
457 if (it != TC.getFilePaths().begin())
458 llvm::outs() << ':';
459 llvm::outs() << *it;
460 }
461 llvm::outs() << "\n";
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000462 return false;
Daniel Dunbarca3459e2009-03-20 04:37:21 +0000463 }
464
Daniel Dunbarcb881672009-03-13 00:51:18 +0000465 // FIXME: The following handlers should use a callback mechanism, we
466 // don't know what the client would like to do.
Daniel Dunbar21549232009-03-18 02:55:38 +0000467 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
468 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).toString()
469 << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000470 return false;
471 }
472
Daniel Dunbar21549232009-03-18 02:55:38 +0000473 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
474 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).toString()
475 << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000476 return false;
477 }
478
Daniel Dunbar21549232009-03-18 02:55:38 +0000479 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbar08c65e02009-03-27 14:26:33 +0000480 llvm::outs() << GetFilePath("libgcc.a", TC).toString() << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000481 return false;
482 }
483
Daniel Dunbar12cfe032009-06-16 23:25:22 +0000484 if (C.getArgs().hasArg(options::OPT_print_multi_lib)) {
485 // FIXME: We need tool chain support for this.
486 llvm::outs() << ".;\n";
487
488 switch (C.getDefaultToolChain().getTriple().getArch()) {
489 default:
490 break;
491
492 case llvm::Triple::x86_64:
493 llvm::outs() << "x86_64;@m64" << "\n";
494 break;
495
496 case llvm::Triple::ppc64:
497 llvm::outs() << "ppc64;@m64" << "\n";
498 break;
499 }
500 return false;
501 }
502
503 // FIXME: What is the difference between print-multi-directory and
504 // print-multi-os-directory?
505 if (C.getArgs().hasArg(options::OPT_print_multi_directory) ||
506 C.getArgs().hasArg(options::OPT_print_multi_os_directory)) {
507 switch (C.getDefaultToolChain().getTriple().getArch()) {
508 default:
509 case llvm::Triple::x86:
510 case llvm::Triple::ppc:
511 llvm::outs() << "." << "\n";
512 break;
513
514 case llvm::Triple::x86_64:
515 llvm::outs() << "x86_64" << "\n";
516 break;
517
518 case llvm::Triple::ppc64:
519 llvm::outs() << "ppc64" << "\n";
520 break;
521 }
522 return false;
523 }
524
Daniel Dunbarcb881672009-03-13 00:51:18 +0000525 return true;
526}
527
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000528static unsigned PrintActions1(const Compilation &C,
Daniel Dunbarba102132009-03-13 12:19:02 +0000529 Action *A,
530 std::map<Action*, unsigned> &Ids) {
531 if (Ids.count(A))
532 return Ids[A];
533
534 std::string str;
535 llvm::raw_string_ostream os(str);
536
537 os << Action::getClassName(A->getKind()) << ", ";
538 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000539 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbarba102132009-03-13 12:19:02 +0000540 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000541 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
542 C.getDefaultToolChain().getArchName()) << '"'
543 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbarba102132009-03-13 12:19:02 +0000544 } else {
545 os << "{";
546 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000547 os << PrintActions1(C, *it, Ids);
Daniel Dunbarba102132009-03-13 12:19:02 +0000548 ++it;
549 if (it != ie)
550 os << ", ";
551 }
552 os << "}";
553 }
554
555 unsigned Id = Ids.size();
556 Ids[A] = Id;
Daniel Dunbarb269c322009-03-13 17:20:20 +0000557 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbarba102132009-03-13 12:19:02 +0000558 << types::getTypeName(A->getType()) << "\n";
559
560 return Id;
561}
562
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000563void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbarba102132009-03-13 12:19:02 +0000564 std::map<Action*, unsigned> Ids;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000565 for (ActionList::const_iterator it = C.getActions().begin(),
566 ie = C.getActions().end(); it != ie; ++it)
567 PrintActions1(C, *it, Ids);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000568}
569
Daniel Dunbar21549232009-03-18 02:55:38 +0000570void Driver::BuildUniversalActions(const ArgList &Args,
571 ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000572 llvm::PrettyStackTraceString CrashInfo("Building actions for universal build");
Daniel Dunbar13689542009-03-13 20:33:35 +0000573 // Collect the list of architectures. Duplicates are allowed, but
574 // should only be handled once (in the order seen).
575 llvm::StringSet<> ArchNames;
576 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000577 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
578 it != ie; ++it) {
579 Arg *A = *it;
580
581 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbar13689542009-03-13 20:33:35 +0000582 const char *Name = A->getValue(Args);
583
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000584 // FIXME: We need to handle canonicalization of the specified
585 // arch?
586
Daniel Dunbar75877192009-03-19 07:55:12 +0000587 A->claim();
Daniel Dunbar13689542009-03-13 20:33:35 +0000588 if (ArchNames.insert(Name))
589 Archs.push_back(Name);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000590 }
591 }
592
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000593 // When there is no explicit arch for this platform, make sure we
594 // still bind the architecture (to the default) so that -Xarch_ is
595 // handled correctly.
596 if (!Archs.size())
597 Archs.push_back(0);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000598
599 // FIXME: We killed off some others but these aren't yet detected in
600 // a functional manner. If we added information to jobs about which
601 // "auxiliary" files they wrote then we could detect the conflict
602 // these cause downstream.
603 if (Archs.size() > 1) {
604 // No recovery needed, the point of this is just to prevent
605 // overwriting the same files.
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000606 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
607 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000608 << A->getAsString(Args);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000609 }
610
611 ActionList SingleActions;
612 BuildActions(Args, SingleActions);
613
614 // Add in arch binding and lipo (if necessary) for every top level
615 // action.
616 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
617 Action *Act = SingleActions[i];
618
619 // Make sure we can lipo this kind of output. If not (and it is an
620 // actual output) then we disallow, since we can't create an
621 // output file with the right name without overwriting it. We
622 // could remove this oddity by just changing the output names to
623 // include the arch, which would also fix
624 // -save-temps. Compatibility wins for now.
625
Daniel Dunbar3dbd6c52009-03-13 17:46:02 +0000626 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000627 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
628 << types::getTypeName(Act->getType());
629
630 ActionList Inputs;
Daniel Dunbar75877192009-03-19 07:55:12 +0000631 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbar13689542009-03-13 20:33:35 +0000632 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000633
634 // Lipo if necessary, We do it this way because we need to set the
635 // arch flag so that -Xarch_ gets overwritten.
636 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
637 Actions.append(Inputs.begin(), Inputs.end());
638 else
639 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
640 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000641}
642
Daniel Dunbar21549232009-03-18 02:55:38 +0000643void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000644 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000645 // Start by constructing the list of inputs and their types.
646
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000647 // Track the current user specified (-x) input. We also explicitly
648 // track the argument used to set the type; we only want to claim
649 // the type when we actually use it, so we warn about unused -x
650 // arguments.
651 types::ID InputType = types::TY_Nothing;
652 Arg *InputTypeArg = 0;
653
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000654 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
655 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
656 it != ie; ++it) {
657 Arg *A = *it;
658
659 if (isa<InputOption>(A->getOption())) {
660 const char *Value = A->getValue(Args);
661 types::ID Ty = types::TY_INVALID;
662
663 // Infer the input type if necessary.
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000664 if (InputType == types::TY_Nothing) {
665 // If there was an explicit arg for this, claim it.
666 if (InputTypeArg)
667 InputTypeArg->claim();
668
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000669 // stdin must be handled specially.
670 if (memcmp(Value, "-", 2) == 0) {
671 // If running with -E, treat as a C input (this changes the
672 // builtin macros, for example). This may be overridden by
673 // -ObjC below.
674 //
675 // Otherwise emit an error but still use a valid type to
676 // avoid spurious errors (e.g., no inputs).
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000677 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000678 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000679 Ty = types::TY_C;
680 } else {
681 // Otherwise lookup by extension, and fallback to ObjectType
Daniel Dunbare33bea42009-03-20 23:39:23 +0000682 // if not found. We use a host hook here because Darwin at
683 // least has its own idea of what .s is.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000684 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbare33bea42009-03-20 23:39:23 +0000685 Ty = Host->lookupTypeForExtension(Ext + 1);
686
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000687 if (Ty == types::TY_INVALID)
688 Ty = types::TY_Object;
689 }
690
Daniel Dunbar683ca382009-05-18 21:47:54 +0000691 // -ObjC and -ObjC++ override the default language, but only for "source
692 // files". We just treat everything that isn't a linker input as a
693 // source file.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000694 //
Daniel Dunbar683ca382009-05-18 21:47:54 +0000695 // FIXME: Clean this up if we move the phase sequence into the type.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000696 if (Ty != types::TY_Object) {
697 if (Args.hasArg(options::OPT_ObjC))
698 Ty = types::TY_ObjC;
699 else if (Args.hasArg(options::OPT_ObjCXX))
700 Ty = types::TY_ObjCXX;
701 }
702 } else {
703 assert(InputTypeArg && "InputType set w/o InputTypeArg");
704 InputTypeArg->claim();
705 Ty = InputType;
706 }
707
708 // Check that the file exists. It isn't clear this is worth
709 // doing, since the tool presumably does this anyway, and this
710 // just adds an extra stat to the equation, but this is gcc
711 // compatible.
712 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000713 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000714 else
715 Inputs.push_back(std::make_pair(Ty, A));
716
717 } else if (A->getOption().isLinkerInput()) {
718 // Just treat as object type, we could make a special type for
719 // this if necessary.
720 Inputs.push_back(std::make_pair(types::TY_Object, A));
721
722 } else if (A->getOption().getId() == options::OPT_x) {
723 InputTypeArg = A;
724 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
725
726 // Follow gcc behavior and treat as linker input for invalid -x
727 // options. Its not clear why we shouldn't just revert to
728 // unknown; but this isn't very important, we might as well be
729 // bug comatible.
730 if (!InputType) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000731 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000732 InputType = types::TY_Object;
733 }
734 }
735 }
736
Daniel Dunbar8b1604e2009-03-13 00:17:48 +0000737 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000738 Diag(clang::diag::err_drv_no_input_files);
739 return;
740 }
741
742 // Determine which compilation mode we are in. We look for options
743 // which affect the phase, starting with the earliest phases, and
744 // record which option we used to determine the final phase.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000745 Arg *FinalPhaseArg = 0;
746 phases::ID FinalPhase;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000747
748 // -{E,M,MM} only run the preprocessor.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000749 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
750 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
751 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
752 FinalPhase = phases::Preprocess;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000753
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000754 // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler.
755 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
Daniel Dunbar63be57a2009-05-06 02:12:32 +0000756 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze,
757 options::OPT__analyze_auto)) ||
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000758 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
759 FinalPhase = phases::Compile;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000760
761 // -c only runs up to the assembler.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000762 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
763 FinalPhase = phases::Assemble;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000764
765 // Otherwise do everything.
766 } else
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000767 FinalPhase = phases::Link;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000768
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000769 // Reject -Z* at the top level, these options should never have been
770 // exposed by gcc.
Daniel Dunbard7b88c22009-03-26 16:12:09 +0000771 if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000772 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000773
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000774 // Construct the actions to perform.
775 ActionList LinkerInputs;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000776 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000777 types::ID InputType = Inputs[i].first;
778 const Arg *InputArg = Inputs[i].second;
779
780 unsigned NumSteps = types::getNumCompilationPhases(InputType);
781 assert(NumSteps && "Invalid number of steps!");
782
783 // If the first step comes after the final phase we are doing as
784 // part of this compilation, warn the user about it.
785 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
786 if (InitialPhase > FinalPhase) {
Daniel Dunbar05494a72009-03-19 07:57:08 +0000787 // Claim here to avoid the more general unused warning.
788 InputArg->claim();
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000789 Diag(clang::diag::warn_drv_input_file_unused)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000790 << InputArg->getAsString(Args)
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000791 << getPhaseName(InitialPhase)
792 << FinalPhaseArg->getOption().getName();
793 continue;
794 }
795
796 // Build the pipeline for this file.
797 Action *Current = new InputAction(*InputArg, InputType);
798 for (unsigned i = 0; i != NumSteps; ++i) {
799 phases::ID Phase = types::getCompilationPhase(InputType, i);
800
801 // We are done if this step is past what the user requested.
802 if (Phase > FinalPhase)
803 break;
804
805 // Queue linker inputs.
806 if (Phase == phases::Link) {
807 assert(i + 1 == NumSteps && "linking must be final compilation step.");
808 LinkerInputs.push_back(Current);
809 Current = 0;
810 break;
811 }
812
Daniel Dunbar337a6272009-03-24 20:17:30 +0000813 // Some types skip the assembler phase (e.g., llvm-bc), but we
814 // can't encode this in the steps because the intermediate type
815 // depends on arguments. Just special case here.
816 if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
817 continue;
818
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000819 // Otherwise construct the appropriate action.
820 Current = ConstructPhaseAction(Args, Phase, Current);
821 if (Current->getType() == types::TY_Nothing)
822 break;
823 }
824
825 // If we ended with something, add to the output list.
826 if (Current)
827 Actions.push_back(Current);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000828 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000829
830 // Add a link action if necessary.
831 if (!LinkerInputs.empty())
832 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
833}
834
835Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
836 Action *Input) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000837 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000838 // Build the appropriate action.
839 switch (Phase) {
840 case phases::Link: assert(0 && "link action invalid here.");
841 case phases::Preprocess: {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +0000842 types::ID OutputTy;
843 // -{M, MM} alter the output type.
844 if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) {
845 OutputTy = types::TY_Dependencies;
846 } else {
847 OutputTy = types::getPreprocessedType(Input->getType());
848 assert(OutputTy != types::TY_INVALID &&
849 "Cannot preprocess this input type!");
850 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000851 return new PreprocessJobAction(Input, OutputTy);
852 }
853 case phases::Precompile:
854 return new PrecompileJobAction(Input, types::TY_PCH);
855 case phases::Compile: {
856 if (Args.hasArg(options::OPT_fsyntax_only)) {
857 return new CompileJobAction(Input, types::TY_Nothing);
Daniel Dunbar63be57a2009-05-06 02:12:32 +0000858 } else if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000859 return new AnalyzeJobAction(Input, types::TY_Plist);
Daniel Dunbar337a6272009-03-24 20:17:30 +0000860 } else if (Args.hasArg(options::OPT_emit_llvm) ||
861 Args.hasArg(options::OPT_flto) ||
862 Args.hasArg(options::OPT_O4)) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000863 types::ID Output =
864 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
865 return new CompileJobAction(Input, Output);
866 } else {
867 return new CompileJobAction(Input, types::TY_PP_Asm);
868 }
869 }
870 case phases::Assemble:
871 return new AssembleJobAction(Input, types::TY_Object);
872 }
873
874 assert(0 && "invalid phase in ConstructPhaseAction");
875 return 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000876}
877
Daniel Dunbar21549232009-03-18 02:55:38 +0000878void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000879 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000880 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
881 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000882
883 // FIXME: Pipes are forcibly disabled until we support executing
884 // them.
885 if (!CCCPrintBindings)
886 UsePipes = false;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000887
888 // -save-temps inhibits pipes.
889 if (SaveTemps && UsePipes) {
890 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
891 UsePipes = true;
892 }
893
894 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
895
896 // It is an error to provide a -o option if we are making multiple
897 // output files.
898 if (FinalOutput) {
899 unsigned NumOutputs = 0;
Daniel Dunbar21549232009-03-18 02:55:38 +0000900 for (ActionList::const_iterator it = C.getActions().begin(),
901 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000902 if ((*it)->getType() != types::TY_Nothing)
903 ++NumOutputs;
904
905 if (NumOutputs > 1) {
906 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
907 FinalOutput = 0;
908 }
909 }
910
Daniel Dunbar21549232009-03-18 02:55:38 +0000911 for (ActionList::const_iterator it = C.getActions().begin(),
912 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000913 Action *A = *it;
914
915 // If we are linking an image for multiple archs then the linker
916 // wants -arch_multiple and -final_output <final image
917 // name>. Unfortunately, this doesn't fit in cleanly because we
918 // have to pass this information down.
919 //
920 // FIXME: This is a hack; find a cleaner way to integrate this
921 // into the process.
922 const char *LinkingOutput = 0;
Daniel Dunbard7b88c22009-03-26 16:12:09 +0000923 if (isa<LipoJobAction>(A)) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000924 if (FinalOutput)
925 LinkingOutput = FinalOutput->getValue(C.getArgs());
926 else
927 LinkingOutput = DefaultImageName.c_str();
928 }
929
930 InputInfo II;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000931 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000932 /*CanAcceptPipe*/ true,
933 /*AtTopLevel*/ true,
934 /*LinkingOutput*/ LinkingOutput,
935 II);
936 }
Daniel Dunbar586dc232009-03-16 06:42:30 +0000937
Daniel Dunbarbf4a6762009-04-03 22:09:23 +0000938 // If the user passed -Qunused-arguments or there were errors, don't
939 // warn about any unused arguments.
Daniel Dunbar1e23f5f2009-04-07 19:04:18 +0000940 if (Diags.getNumErrors() ||
941 C.getArgs().hasArg(options::OPT_Qunused_arguments))
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000942 return;
943
Daniel Dunbara2094e72009-03-29 22:24:54 +0000944 // Claim -### here.
945 (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
946
Daniel Dunbar586dc232009-03-16 06:42:30 +0000947 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
948 it != ie; ++it) {
949 Arg *A = *it;
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000950
Daniel Dunbar586dc232009-03-16 06:42:30 +0000951 // FIXME: It would be nice to be able to send the argument to the
952 // Diagnostic, so that extra values, position, and so on could be
953 // printed.
Daniel Dunbar4f53b292009-04-04 00:52:26 +0000954 if (!A->isClaimed()) {
Daniel Dunbar1e23f5f2009-04-07 19:04:18 +0000955 if (A->getOption().hasNoArgumentUnused())
956 continue;
957
Daniel Dunbar4f53b292009-04-04 00:52:26 +0000958 // Suppress the warning automatically if this is just a flag,
959 // and it is an instance of an argument we already claimed.
960 const Option &Opt = A->getOption();
961 if (isa<FlagOption>(Opt)) {
962 bool DuplicateClaimed = false;
963
964 // FIXME: Use iterator.
965 for (ArgList::const_iterator it = C.getArgs().begin(),
966 ie = C.getArgs().end(); it != ie; ++it) {
967 if ((*it)->isClaimed() && (*it)->getOption().matches(Opt.getId())) {
968 DuplicateClaimed = true;
969 break;
970 }
971 }
972
973 if (DuplicateClaimed)
974 continue;
975 }
976
Daniel Dunbar586dc232009-03-16 06:42:30 +0000977 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000978 << A->getAsString(C.getArgs());
Daniel Dunbar4f53b292009-04-04 00:52:26 +0000979 }
Daniel Dunbar586dc232009-03-16 06:42:30 +0000980 }
Daniel Dunbar57b704d2009-03-13 22:12:33 +0000981}
982
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000983void Driver::BuildJobsForAction(Compilation &C,
984 const Action *A,
985 const ToolChain *TC,
986 bool CanAcceptPipe,
987 bool AtTopLevel,
988 const char *LinkingOutput,
989 InputInfo &Result) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000990 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action");
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000991
992 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
993 // FIXME: Pipes are forcibly disabled until we support executing
994 // them.
995 if (!CCCPrintBindings)
996 UsePipes = false;
997
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000998 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar115a7922009-03-19 07:29:38 +0000999 // FIXME: It would be nice to not claim this here; maybe the old
1000 // scheme of just using Args was better?
1001 const Arg &Input = IA->getInputArg();
1002 Input.claim();
1003 if (isa<PositionalArg>(Input)) {
1004 const char *Name = Input.getValue(C.getArgs());
1005 Result = InputInfo(Name, A->getType(), Name);
1006 } else
1007 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001008 return;
1009 }
1010
1011 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
1012 const char *ArchName = BAA->getArchName();
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001013 std::string Arch;
1014 if (!ArchName) {
1015 Arch = C.getDefaultToolChain().getArchName();
1016 ArchName = Arch.c_str();
1017 }
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001018 BuildJobsForAction(C,
1019 *BAA->begin(),
1020 Host->getToolChain(C.getArgs(), ArchName),
1021 CanAcceptPipe,
1022 AtTopLevel,
1023 LinkingOutput,
1024 Result);
1025 return;
1026 }
1027
1028 const JobAction *JA = cast<JobAction>(A);
1029 const Tool &T = TC->SelectTool(C, *JA);
1030
1031 // See if we should use an integrated preprocessor. We do so when we
1032 // have exactly one input, since this is the only use case we care
1033 // about (irrelevant since we don't support combine yet).
1034 bool UseIntegratedCPP = false;
1035 const ActionList *Inputs = &A->getInputs();
1036 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
1037 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
1038 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
1039 !C.getArgs().hasArg(options::OPT_save_temps) &&
1040 T.hasIntegratedCPP()) {
1041 UseIntegratedCPP = true;
1042 Inputs = &(*Inputs)[0]->getInputs();
1043 }
1044 }
1045
1046 // Only use pipes when there is exactly one input.
1047 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001048 InputInfoList InputInfos;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001049 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
1050 it != ie; ++it) {
1051 InputInfo II;
1052 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
1053 /*AtTopLevel*/false,
1054 LinkingOutput,
1055 II);
1056 InputInfos.push_back(II);
1057 }
1058
1059 // Determine if we should output to a pipe.
1060 bool OutputToPipe = false;
1061 if (CanAcceptPipe && T.canPipeOutput()) {
1062 // Some actions default to writing to a pipe if they are the top
1063 // level phase and there was no user override.
1064 //
1065 // FIXME: Is there a better way to handle this?
1066 if (AtTopLevel) {
1067 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
1068 OutputToPipe = true;
Daniel Dunbar60ccc762009-03-18 23:18:19 +00001069 } else if (UsePipes)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001070 OutputToPipe = true;
1071 }
1072
1073 // Figure out where to put the job (pipes).
1074 Job *Dest = &C.getJobs();
1075 if (InputInfos[0].isPipe()) {
Daniel Dunbar441d0602009-03-17 17:53:55 +00001076 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001077 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
1078 Dest = &InputInfos[0].getPipe();
1079 }
1080
1081 // Always use the first input as the base input.
1082 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar441d0602009-03-17 17:53:55 +00001083
1084 // Determine the place to write output to (nothing, pipe, or
1085 // filename) and where to put the new job.
Daniel Dunbar441d0602009-03-17 17:53:55 +00001086 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001087 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001088 } else if (OutputToPipe) {
1089 // Append to current piped job or create a new one as appropriate.
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001090 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
1091 if (!PJ) {
1092 PJ = new PipedJob();
Daniel Dunbarb7b61b22009-03-20 00:11:04 +00001093 // FIXME: Temporary hack so that -ccc-print-bindings work until
1094 // we have pipe support. Please remove later.
1095 if (!CCCPrintBindings)
1096 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar871adcf2009-03-18 07:06:02 +00001097 Dest = PJ;
Daniel Dunbar441d0602009-03-17 17:53:55 +00001098 }
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001099 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001100 } else {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001101 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
1102 A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001103 }
1104
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001105 if (CCCPrintBindings) {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00001106 llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"'
1107 << " - \"" << T.getName() << "\", inputs: [";
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001108 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
1109 llvm::errs() << InputInfos[i].getAsString();
1110 if (i + 1 != e)
1111 llvm::errs() << ", ";
1112 }
1113 llvm::errs() << "], output: " << Result.getAsString() << "\n";
1114 } else {
Daniel Dunbarf3cad362009-03-25 04:13:45 +00001115 T.ConstructJob(C, *JA, *Dest, Result, InputInfos,
1116 C.getArgsForToolChain(TC), LinkingOutput);
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001117 }
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001118}
1119
Daniel Dunbar441d0602009-03-17 17:53:55 +00001120const char *Driver::GetNamedOutputPath(Compilation &C,
1121 const JobAction &JA,
1122 const char *BaseInput,
1123 bool AtTopLevel) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +00001124 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar441d0602009-03-17 17:53:55 +00001125 // Output to a user requested destination?
1126 if (AtTopLevel) {
1127 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
1128 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
1129 }
1130
1131 // Output to a temporary file?
1132 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbar214399e2009-03-18 19:34:39 +00001133 std::string TmpName =
1134 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
1135 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar441d0602009-03-17 17:53:55 +00001136 }
1137
1138 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbar5796bf42009-03-18 02:00:31 +00001139 std::string BaseName(BasePath.getLast());
Daniel Dunbar441d0602009-03-17 17:53:55 +00001140
1141 // Determine what the derived output name should be.
1142 const char *NamedOutput;
1143 if (JA.getType() == types::TY_Image) {
1144 NamedOutput = DefaultImageName.c_str();
1145 } else {
1146 const char *Suffix = types::getTypeTempSuffix(JA.getType());
1147 assert(Suffix && "All types used for output should have a suffix.");
1148
1149 std::string::size_type End = std::string::npos;
1150 if (!types::appendSuffixForType(JA.getType()))
1151 End = BaseName.rfind('.');
1152 std::string Suffixed(BaseName.substr(0, End));
1153 Suffixed += '.';
1154 Suffixed += Suffix;
1155 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
1156 }
1157
1158 // As an annoying special case, PCH generation doesn't strip the
1159 // pathname.
1160 if (JA.getType() == types::TY_PCH) {
1161 BasePath.eraseComponent();
Daniel Dunbar56c55942009-03-18 09:58:30 +00001162 if (BasePath.isEmpty())
1163 BasePath = NamedOutput;
1164 else
1165 BasePath.appendComponent(NamedOutput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001166 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
1167 } else {
1168 return C.addResultFile(NamedOutput);
1169 }
1170}
1171
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +00001172llvm::sys::Path Driver::GetFilePath(const char *Name,
Daniel Dunbar21549232009-03-18 02:55:38 +00001173 const ToolChain &TC) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001174 const ToolChain::path_list &List = TC.getFilePaths();
1175 for (ToolChain::path_list::const_iterator
1176 it = List.begin(), ie = List.end(); it != ie; ++it) {
1177 llvm::sys::Path P(*it);
1178 P.appendComponent(Name);
1179 if (P.exists())
1180 return P;
1181 }
1182
Daniel Dunbarcb881672009-03-13 00:51:18 +00001183 return llvm::sys::Path(Name);
1184}
1185
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +00001186llvm::sys::Path Driver::GetProgramPath(const char *Name,
Mike Stump950bedd2009-03-27 00:40:20 +00001187 const ToolChain &TC,
1188 bool WantFile) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001189 const ToolChain::path_list &List = TC.getProgramPaths();
1190 for (ToolChain::path_list::const_iterator
1191 it = List.begin(), ie = List.end(); it != ie; ++it) {
1192 llvm::sys::Path P(*it);
1193 P.appendComponent(Name);
Mike Stump950bedd2009-03-27 00:40:20 +00001194 if (WantFile ? P.exists() : P.canExecute())
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001195 return P;
1196 }
1197
Daniel Dunbarc50b00d2009-03-23 16:15:50 +00001198 // If all else failed, search the path.
1199 llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
Daniel Dunbar632f50e2009-03-18 21:34:08 +00001200 if (!P.empty())
1201 return P;
1202
Daniel Dunbarcb881672009-03-13 00:51:18 +00001203 return llvm::sys::Path(Name);
1204}
1205
Daniel Dunbar214399e2009-03-18 19:34:39 +00001206std::string Driver::GetTemporaryPath(const char *Suffix) const {
1207 // FIXME: This is lame; sys::Path should provide this function (in
1208 // particular, it should know how to find the temporary files dir).
1209 std::string Error;
Daniel Dunbarb03417f2009-04-20 20:28:21 +00001210 const char *TmpDir = ::getenv("TMPDIR");
1211 if (!TmpDir)
1212 TmpDir = ::getenv("TEMP");
1213 if (!TmpDir)
Daniel Dunbar3ca7ee92009-04-21 00:25:10 +00001214 TmpDir = ::getenv("TMP");
1215 if (!TmpDir)
Daniel Dunbarb03417f2009-04-20 20:28:21 +00001216 TmpDir = "/tmp";
1217 llvm::sys::Path P(TmpDir);
Daniel Dunbarf60c63a2009-04-20 17:32:49 +00001218 P.appendComponent("cc");
Daniel Dunbar214399e2009-03-18 19:34:39 +00001219 if (P.makeUnique(false, &Error)) {
1220 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
1221 return "";
1222 }
1223
Daniel Dunbar84603bc2009-03-18 23:08:52 +00001224 // FIXME: Grumble, makeUnique sometimes leaves the file around!?
1225 // PR3837.
1226 P.eraseFromDisk(false, 0);
1227
Daniel Dunbar214399e2009-03-18 19:34:39 +00001228 P.appendSuffix(Suffix);
1229 return P.toString();
1230}
1231
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001232const HostInfo *Driver::GetHostInfo(const char *TripleStr) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +00001233 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001234 llvm::Triple Triple(TripleStr);
Daniel Dunbardd98e2c2009-03-10 23:41:59 +00001235
Daniel Dunbar1fd6c4b2009-03-17 19:00:50 +00001236 // Normalize Arch a bit.
1237 //
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001238 // FIXME: We shouldn't need to do this once everything goes through the triple
1239 // interface.
1240 if (Triple.getArchName() == "i686")
1241 Triple.setArchName("i386");
1242 else if (Triple.getArchName() == "amd64")
1243 Triple.setArchName("x86_64");
1244 else if (Triple.getArchName() == "ppc" ||
1245 Triple.getArchName() == "Power Macintosh")
1246 Triple.setArchName("powerpc");
1247 else if (Triple.getArchName() == "ppc64")
1248 Triple.setArchName("powerpc64");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001249
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001250 switch (Triple.getOS()) {
1251 case llvm::Triple::Darwin:
1252 return createDarwinHostInfo(*this, Triple);
1253 case llvm::Triple::DragonFly:
1254 return createDragonFlyHostInfo(*this, Triple);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001255 case llvm::Triple::OpenBSD:
1256 return createOpenBSDHostInfo(*this, Triple);
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001257 case llvm::Triple::FreeBSD:
1258 return createFreeBSDHostInfo(*this, Triple);
Eli Friedman6b3454a2009-05-26 07:52:18 +00001259 case llvm::Triple::Linux:
1260 return createLinuxHostInfo(*this, Triple);
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001261 default:
1262 return createUnknownHostInfo(*this, Triple);
1263 }
Daniel Dunbardd98e2c2009-03-10 23:41:59 +00001264}
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001265
1266bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
Daniel Dunbarbf54a062009-04-01 20:33:11 +00001267 const std::string &ArchNameStr) const {
1268 // FIXME: Remove this hack.
1269 const char *ArchName = ArchNameStr.c_str();
1270 if (ArchNameStr == "powerpc")
1271 ArchName = "ppc";
1272 else if (ArchNameStr == "powerpc64")
1273 ArchName = "ppc64";
1274
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001275 // Check if user requested no clang, or clang doesn't understand
1276 // this type (we only handle single inputs for now).
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001277 if (!CCCUseClang || JA.size() != 1 ||
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001278 !types::isAcceptedByClang((*JA.begin())->getType()))
1279 return false;
1280
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001281 // Otherwise make sure this is an action clang understands.
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001282 if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbar6256d362009-03-24 19:14:56 +00001283 if (!CCCUseClangCPP) {
1284 Diag(clang::diag::warn_drv_not_using_clang_cpp);
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001285 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001286 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001287 } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
1288 return false;
1289
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001290 // Use clang for C++?
Daniel Dunbar6256d362009-03-24 19:14:56 +00001291 if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
1292 Diag(clang::diag::warn_drv_not_using_clang_cxx);
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001293 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001294 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001295
Daniel Dunbarfec26bd2009-04-16 23:10:13 +00001296 // Always use clang for precompiling, regardless of archs. PTH is
1297 // platform independent, and this allows the use of the static
1298 // analyzer on platforms we don't have full IRgen support for.
1299 if (isa<PrecompileJobAction>(JA))
1300 return true;
1301
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001302 // Finally, don't use clang if this isn't one of the user specified
1303 // archs to build.
Daniel Dunbar6256d362009-03-24 19:14:56 +00001304 if (!CCCClangArchs.empty() && !CCCClangArchs.count(ArchName)) {
1305 Diag(clang::diag::warn_drv_not_using_clang_arch) << ArchName;
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001306 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001307 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001308
1309 return true;
1310}
Daniel Dunbard73fe9b2009-03-26 15:58:36 +00001311
1312/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and
1313/// return the grouped values as integers. Numbers which are not
1314/// provided are set to 0.
1315///
1316/// \return True if the entire string was parsed (9.2), or all groups
1317/// were parsed (10.3.5extrastuff).
1318bool Driver::GetReleaseVersion(const char *Str, unsigned &Major,
1319 unsigned &Minor, unsigned &Micro,
1320 bool &HadExtra) {
1321 HadExtra = false;
1322
1323 Major = Minor = Micro = 0;
1324 if (*Str == '\0')
1325 return true;
1326
1327 char *End;
1328 Major = (unsigned) strtol(Str, &End, 10);
1329 if (*Str != '\0' && *End == '\0')
1330 return true;
1331 if (*End != '.')
1332 return false;
1333
1334 Str = End+1;
1335 Minor = (unsigned) strtol(Str, &End, 10);
1336 if (*Str != '\0' && *End == '\0')
1337 return true;
1338 if (*End != '.')
1339 return false;
1340
1341 Str = End+1;
1342 Micro = (unsigned) strtol(Str, &End, 10);
1343 if (*Str != '\0' && *End == '\0')
1344 return true;
1345 if (Str == End)
1346 return false;
1347 HadExtra = true;
1348 return true;
1349}