blob: 2e90accf343a1a635e088f2e44274c557731fd45 [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 Dunbar78d8a082009-04-01 23:34:41 +000049 CCCGenericGCCName("gcc"), CCCUseClang(true), CCCUseClangCXX(false),
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 Dunbar0f99d2e2009-03-24 19:02:31 +000053 // Only use clang on i386 and x86_64 by default.
54 CCCClangArchs.insert("i386");
55 CCCClangArchs.insert("x86_64");
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000056}
57
58Driver::~Driver() {
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000059 delete Opts;
Daniel Dunbar7e4534d2009-03-18 01:09:40 +000060 delete Host;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000061}
62
Daniel Dunbarf3cad362009-03-25 04:13:45 +000063InputArgList *Driver::ParseArgStrings(const char **ArgBegin,
64 const char **ArgEnd) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +000065 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbarf3cad362009-03-25 04:13:45 +000066 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
Daniel Dunbar06482622009-03-05 06:38:47 +000067
Daniel Dunbarad2a9af2009-03-13 11:38:42 +000068 // FIXME: Handle '@' args (or at least error on them).
69
Daniel Dunbar06482622009-03-05 06:38:47 +000070 unsigned Index = 0, End = ArgEnd - ArgBegin;
71 while (Index < End) {
Daniel Dunbar41393402009-03-13 01:01:44 +000072 // gcc's handling of empty arguments doesn't make
73 // sense, but this is not a common use case. :)
74 //
75 // We just ignore them here (note that other things may
76 // still take them as arguments).
77 if (Args->getArgString(Index)[0] == '\0') {
78 ++Index;
79 continue;
80 }
81
Daniel Dunbar06482622009-03-05 06:38:47 +000082 unsigned Prev = Index;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000083 Arg *A = getOpts().ParseOneArg(*Args, Index);
84 assert(Index > Prev && "Parser failed to consume argument.");
Daniel Dunbar53ec5522009-03-12 07:58:46 +000085
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000086 // Check for missing argument error.
87 if (!A) {
88 assert(Index >= End && "Unexpected parser error.");
89 Diag(clang::diag::err_drv_missing_argument)
90 << Args->getArgString(Prev)
91 << (Index - Prev - 1);
92 break;
Daniel Dunbar53ec5522009-03-12 07:58:46 +000093 }
Daniel Dunbar06482622009-03-05 06:38:47 +000094
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000095 if (A->getOption().isUnsupported()) {
96 Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
97 continue;
98 }
99 Args->append(A);
Daniel Dunbar06482622009-03-05 06:38:47 +0000100 }
101
102 return Args;
103}
104
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000105Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000106 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
107
Daniel Dunbarcb881672009-03-13 00:51:18 +0000108 // FIXME: Handle environment options which effect driver behavior,
109 // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH,
110 // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS.
111
112 // FIXME: What are we going to do with -V and -b?
113
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000114 // FIXME: This stuff needs to go into the Compilation, not the
115 // driver.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000116 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar06482622009-03-05 06:38:47 +0000117
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000118 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000119 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000120
121 // Read -ccc args.
122 //
123 // FIXME: We need to figure out where this behavior should
124 // live. Most of it should be outside in the client; the parts that
125 // aren't should have proper options, either by introducing new ones
126 // or by overloading gcc ones like -V or -b.
127 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
128 const char *Opt = *Start + 5;
129
130 if (!strcmp(Opt, "print-options")) {
131 CCCPrintOptions = true;
132 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000133 CCCPrintActions = true;
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000134 } else if (!strcmp(Opt, "print-bindings")) {
135 CCCPrintBindings = true;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000136 } else if (!strcmp(Opt, "cxx")) {
137 CCCIsCXX = true;
138 } else if (!strcmp(Opt, "echo")) {
139 CCCEcho = true;
140
Daniel Dunbar78d8a082009-04-01 23:34:41 +0000141 } else if (!strcmp(Opt, "gcc-name")) {
142 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
143 CCCGenericGCCName = *++Start;
144
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000145 } else if (!strcmp(Opt, "clang-cxx")) {
146 CCCUseClangCXX = true;
Douglas Gregordf91ef32009-04-18 00:34:01 +0000147 } else if (!strcmp(Opt, "pch-is-pch")) {
148 CCCUsePCH = true;
149 } else if (!strcmp(Opt, "pch-is-pth")) {
150 CCCUsePCH = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000151 } else if (!strcmp(Opt, "no-clang")) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000152 CCCUseClang = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000153 } else if (!strcmp(Opt, "no-clang-cpp")) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000154 CCCUseClangCPP = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000155 } else if (!strcmp(Opt, "clang-archs")) {
156 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
157 const char *Cur = *++Start;
158
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000159 CCCClangArchs.clear();
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000160 for (;;) {
161 const char *Next = strchr(Cur, ',');
162
163 if (Next) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000164 if (Cur != Next)
165 CCCClangArchs.insert(std::string(Cur, Next));
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000166 Cur = Next + 1;
167 } else {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000168 if (*Cur != '\0')
169 CCCClangArchs.insert(std::string(Cur));
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000170 break;
171 }
172 }
173
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000174 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000175 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000176 HostTriple = *++Start;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000177
178 } else {
179 // FIXME: Error handling.
180 llvm::errs() << "invalid option: " << *Start << "\n";
181 exit(1);
182 }
183 }
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000184
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000185 InputArgList *Args = ParseArgStrings(Start, End);
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000186
Daniel Dunbare5049522009-03-17 20:45:45 +0000187 Host = GetHostInfo(HostTriple);
Daniel Dunbarcb881672009-03-13 00:51:18 +0000188
Daniel Dunbar586dc232009-03-16 06:42:30 +0000189 // The compilation takes ownership of Args.
Daniel Dunbare530ad42009-03-18 22:16:03 +0000190 Compilation *C = new Compilation(*this, *Host->getToolChain(*Args), Args);
Daniel Dunbar21549232009-03-18 02:55:38 +0000191
192 // FIXME: This behavior shouldn't be here.
193 if (CCCPrintOptions) {
194 PrintOptions(C->getArgs());
195 return C;
196 }
197
198 if (!HandleImmediateArgs(*C))
199 return C;
200
201 // Construct the list of abstract actions to perform for this
202 // compilation. We avoid passing a Compilation here simply to
203 // enforce the abstraction that pipelining is not host or toolchain
204 // dependent (other than the driver driver test).
205 if (Host->useDriverDriver())
206 BuildUniversalActions(C->getArgs(), C->getActions());
207 else
208 BuildActions(C->getArgs(), C->getActions());
209
210 if (CCCPrintActions) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000211 PrintActions(*C);
Daniel Dunbar21549232009-03-18 02:55:38 +0000212 return C;
213 }
214
215 BuildJobs(*C);
Daniel Dunbar8d2554a2009-03-15 01:38:15 +0000216
217 return C;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000218}
219
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000220void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar06482622009-03-05 06:38:47 +0000221 unsigned i = 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000222 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar06482622009-03-05 06:38:47 +0000223 it != ie; ++it, ++i) {
224 Arg *A = *it;
225 llvm::errs() << "Option " << i << " - "
226 << "Name: \"" << A->getOption().getName() << "\", "
227 << "Values: {";
228 for (unsigned j = 0; j < A->getNumValues(); ++j) {
229 if (j)
230 llvm::errs() << ", ";
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000231 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar06482622009-03-05 06:38:47 +0000232 }
233 llvm::errs() << "}\n";
Daniel Dunbar06482622009-03-05 06:38:47 +0000234 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000235}
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000236
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000237static std::string getOptionHelpName(const OptTable &Opts, options::ID Id) {
238 std::string Name = Opts.getOptionName(Id);
239
240 // Add metavar, if used.
241 switch (Opts.getOptionKind(Id)) {
242 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
243 assert(0 && "Invalid option with help text.");
244
245 case Option::MultiArgClass: case Option::JoinedAndSeparateClass:
246 assert(0 && "Cannot print metavar for this kind of option.");
247
248 case Option::FlagClass:
249 break;
250
251 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
252 Name += ' ';
253 // FALLTHROUGH
254 case Option::JoinedClass: case Option::CommaJoinedClass:
255 Name += Opts.getOptionMetaVar(Id);
256 break;
257 }
258
259 return Name;
260}
261
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000262void Driver::PrintHelp(bool ShowHidden) const {
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000263 llvm::raw_ostream &OS = llvm::outs();
264
265 OS << "OVERVIEW: clang \"gcc-compatible\" driver\n";
266 OS << '\n';
267 OS << "USAGE: " << Name << " [options] <input files>\n";
268 OS << '\n';
269 OS << "OPTIONS:\n";
270
271 // Render help text into (option, help) pairs.
272 std::vector< std::pair<std::string, const char*> > OptionHelp;
273
274 for (unsigned i = options::OPT_INPUT, e = options::LastOption; i != e; ++i) {
275 options::ID Id = (options::ID) i;
276 if (const char *Text = getOpts().getOptionHelpText(Id))
277 OptionHelp.push_back(std::make_pair(getOptionHelpName(getOpts(), Id),
278 Text));
279 }
280
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000281 if (ShowHidden) {
282 OptionHelp.push_back(std::make_pair("\nDRIVER OPTIONS:",""));
283 OptionHelp.push_back(std::make_pair("-ccc-cxx",
284 "Act as a C++ driver"));
285 OptionHelp.push_back(std::make_pair("-ccc-gcc-name",
286 "Name for native GCC compiler"));
287 OptionHelp.push_back(std::make_pair("-ccc-clang-cxx",
288 "Use the clang compiler for C++"));
289 OptionHelp.push_back(std::make_pair("-ccc-no-clang",
290 "Never use the clang compiler"));
291 OptionHelp.push_back(std::make_pair("-ccc-no-clang-cpp",
292 "Never use the clang preprocessor"));
293 OptionHelp.push_back(std::make_pair("-ccc-clang-archs",
294 "Comma separate list of architectures "
295 "to use the clang compiler for"));
Douglas Gregordf91ef32009-04-18 00:34:01 +0000296 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pch",
297 "Use lazy PCH for precompiled headers"));
298 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pth",
299 "Use pretokenized headers for precompiled headers"));
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000300
301 OptionHelp.push_back(std::make_pair("\nDEBUG/DEVELOPMENT OPTIONS:",""));
302 OptionHelp.push_back(std::make_pair("-ccc-host-triple",
303 "Simulate running on the given target"));
304 OptionHelp.push_back(std::make_pair("-ccc-print-options",
305 "Dump parsed command line arguments"));
306 OptionHelp.push_back(std::make_pair("-ccc-print-phases",
307 "Dump list of actions to perform"));
308 OptionHelp.push_back(std::make_pair("-ccc-print-bindings",
309 "Show bindings of tools to actions"));
310 OptionHelp.push_back(std::make_pair("CCC_ADD_ARGS",
311 "(ENVIRONMENT VARIABLE) Comma separated list of "
312 "arguments to prepend to the command line"));
313 }
314
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000315 // Find the maximum option length.
316 unsigned OptionFieldWidth = 0;
317 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000318 // Skip titles.
319 if (!OptionHelp[i].second)
320 continue;
321
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000322 // Limit the amount of padding we are willing to give up for
323 // alignment.
324 unsigned Length = OptionHelp[i].first.size();
325 if (Length <= 23)
326 OptionFieldWidth = std::max(OptionFieldWidth, Length);
327 }
328
329 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
330 const std::string &Option = OptionHelp[i].first;
331 OS << " " << Option;
332 for (int j = Option.length(), e = OptionFieldWidth; j < e; ++j)
333 OS << ' ';
334 OS << ' ' << OptionHelp[i].second << '\n';
335 }
336
337 OS.flush();
338}
339
Daniel Dunbar70c8db12009-03-26 16:09:13 +0000340void Driver::PrintVersion(const Compilation &C) const {
Mike Stump5d023c32009-03-18 14:00:02 +0000341 static char buf[] = "$URL$";
342 char *zap = strstr(buf, "/lib/Driver");
343 if (zap)
344 *zap = 0;
345 zap = strstr(buf, "/clang/tools/clang");
346 if (zap)
347 *zap = 0;
Mike Stumpe70295b2009-03-18 15:19:35 +0000348 const char *vers = buf+6;
Mike Stump8944c382009-03-18 18:45:55 +0000349 // FIXME: Add cmake support and remove #ifdef
350#ifdef SVN_REVISION
351 const char *revision = SVN_REVISION;
352#else
353 const char *revision = "";
354#endif
Daniel Dunbarcb881672009-03-13 00:51:18 +0000355 // FIXME: The following handlers should use a callback mechanism, we
356 // don't know what the client would like to do.
Daniel Dunbare06dc212009-04-04 05:17:38 +0000357
Douglas Gregorab41e632009-04-27 22:23:34 +0000358 llvm::errs() << "clang version " CLANG_VERSION_STRING " ("
Daniel Dunbar3ee96ba2009-06-16 23:32:58 +0000359 << vers << " " << revision << ")" << '\n';
Daniel Dunbar70c8db12009-03-26 16:09:13 +0000360
361 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +0000362 llvm::errs() << "Target: " << TC.getTripleString() << '\n';
Daniel Dunbar3ee96ba2009-06-16 23:32:58 +0000363
364 // Print the threading model.
365 //
366 // FIXME: Implement correctly.
367 llvm::errs() << "Thread model: " << "posix" << '\n';
Daniel Dunbarcb881672009-03-13 00:51:18 +0000368}
369
Daniel Dunbar21549232009-03-18 02:55:38 +0000370bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000371 // The order these options are handled in in gcc is all over the
372 // place, but we don't expect inconsistencies w.r.t. that to matter
373 // in practice.
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000374
Daniel Dunbare06dc212009-04-04 05:17:38 +0000375 if (C.getArgs().hasArg(options::OPT_dumpversion)) {
Douglas Gregorab41e632009-04-27 22:23:34 +0000376 llvm::outs() << CLANG_VERSION_STRING "\n";
Daniel Dunbare06dc212009-04-04 05:17:38 +0000377 return false;
378 }
379
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000380 if (C.getArgs().hasArg(options::OPT__help) ||
381 C.getArgs().hasArg(options::OPT__help_hidden)) {
382 PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden));
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000383 return false;
384 }
385
Daniel Dunbar6cc73de2009-04-02 15:05:41 +0000386 if (C.getArgs().hasArg(options::OPT__version)) {
387 PrintVersion(C);
388 return false;
389 }
390
Daniel Dunbar21549232009-03-18 02:55:38 +0000391 if (C.getArgs().hasArg(options::OPT_v) ||
392 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbar70c8db12009-03-26 16:09:13 +0000393 PrintVersion(C);
Daniel Dunbarcb881672009-03-13 00:51:18 +0000394 SuppressMissingInputWarning = true;
395 }
396
Daniel Dunbar21549232009-03-18 02:55:38 +0000397 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbarca3459e2009-03-20 04:37:21 +0000398 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
399 llvm::outs() << "programs: =";
400 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
401 ie = TC.getProgramPaths().end(); it != ie; ++it) {
402 if (it != TC.getProgramPaths().begin())
403 llvm::outs() << ':';
404 llvm::outs() << *it;
405 }
406 llvm::outs() << "\n";
407 llvm::outs() << "libraries: =";
408 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
409 ie = TC.getFilePaths().end(); it != ie; ++it) {
410 if (it != TC.getFilePaths().begin())
411 llvm::outs() << ':';
412 llvm::outs() << *it;
413 }
414 llvm::outs() << "\n";
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000415 return false;
Daniel Dunbarca3459e2009-03-20 04:37:21 +0000416 }
417
Daniel Dunbarcb881672009-03-13 00:51:18 +0000418 // FIXME: The following handlers should use a callback mechanism, we
419 // don't know what the client would like to do.
Daniel Dunbar21549232009-03-18 02:55:38 +0000420 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
421 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).toString()
422 << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000423 return false;
424 }
425
Daniel Dunbar21549232009-03-18 02:55:38 +0000426 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
427 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).toString()
428 << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000429 return false;
430 }
431
Daniel Dunbar21549232009-03-18 02:55:38 +0000432 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbar08c65e02009-03-27 14:26:33 +0000433 llvm::outs() << GetFilePath("libgcc.a", TC).toString() << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000434 return false;
435 }
436
Daniel Dunbar12cfe032009-06-16 23:25:22 +0000437 if (C.getArgs().hasArg(options::OPT_print_multi_lib)) {
438 // FIXME: We need tool chain support for this.
439 llvm::outs() << ".;\n";
440
441 switch (C.getDefaultToolChain().getTriple().getArch()) {
442 default:
443 break;
444
445 case llvm::Triple::x86_64:
446 llvm::outs() << "x86_64;@m64" << "\n";
447 break;
448
449 case llvm::Triple::ppc64:
450 llvm::outs() << "ppc64;@m64" << "\n";
451 break;
452 }
453 return false;
454 }
455
456 // FIXME: What is the difference between print-multi-directory and
457 // print-multi-os-directory?
458 if (C.getArgs().hasArg(options::OPT_print_multi_directory) ||
459 C.getArgs().hasArg(options::OPT_print_multi_os_directory)) {
460 switch (C.getDefaultToolChain().getTriple().getArch()) {
461 default:
462 case llvm::Triple::x86:
463 case llvm::Triple::ppc:
464 llvm::outs() << "." << "\n";
465 break;
466
467 case llvm::Triple::x86_64:
468 llvm::outs() << "x86_64" << "\n";
469 break;
470
471 case llvm::Triple::ppc64:
472 llvm::outs() << "ppc64" << "\n";
473 break;
474 }
475 return false;
476 }
477
Daniel Dunbarcb881672009-03-13 00:51:18 +0000478 return true;
479}
480
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000481static unsigned PrintActions1(const Compilation &C,
Daniel Dunbarba102132009-03-13 12:19:02 +0000482 Action *A,
483 std::map<Action*, unsigned> &Ids) {
484 if (Ids.count(A))
485 return Ids[A];
486
487 std::string str;
488 llvm::raw_string_ostream os(str);
489
490 os << Action::getClassName(A->getKind()) << ", ";
491 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000492 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbarba102132009-03-13 12:19:02 +0000493 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000494 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
495 C.getDefaultToolChain().getArchName()) << '"'
496 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbarba102132009-03-13 12:19:02 +0000497 } else {
498 os << "{";
499 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000500 os << PrintActions1(C, *it, Ids);
Daniel Dunbarba102132009-03-13 12:19:02 +0000501 ++it;
502 if (it != ie)
503 os << ", ";
504 }
505 os << "}";
506 }
507
508 unsigned Id = Ids.size();
509 Ids[A] = Id;
Daniel Dunbarb269c322009-03-13 17:20:20 +0000510 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbarba102132009-03-13 12:19:02 +0000511 << types::getTypeName(A->getType()) << "\n";
512
513 return Id;
514}
515
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000516void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbarba102132009-03-13 12:19:02 +0000517 std::map<Action*, unsigned> Ids;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000518 for (ActionList::const_iterator it = C.getActions().begin(),
519 ie = C.getActions().end(); it != ie; ++it)
520 PrintActions1(C, *it, Ids);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000521}
522
Daniel Dunbar21549232009-03-18 02:55:38 +0000523void Driver::BuildUniversalActions(const ArgList &Args,
524 ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000525 llvm::PrettyStackTraceString CrashInfo("Building actions for universal build");
Daniel Dunbar13689542009-03-13 20:33:35 +0000526 // Collect the list of architectures. Duplicates are allowed, but
527 // should only be handled once (in the order seen).
528 llvm::StringSet<> ArchNames;
529 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000530 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
531 it != ie; ++it) {
532 Arg *A = *it;
533
534 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbar13689542009-03-13 20:33:35 +0000535 const char *Name = A->getValue(Args);
536
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000537 // FIXME: We need to handle canonicalization of the specified
538 // arch?
539
Daniel Dunbar75877192009-03-19 07:55:12 +0000540 A->claim();
Daniel Dunbar13689542009-03-13 20:33:35 +0000541 if (ArchNames.insert(Name))
542 Archs.push_back(Name);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000543 }
544 }
545
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000546 // When there is no explicit arch for this platform, make sure we
547 // still bind the architecture (to the default) so that -Xarch_ is
548 // handled correctly.
549 if (!Archs.size())
550 Archs.push_back(0);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000551
552 // FIXME: We killed off some others but these aren't yet detected in
553 // a functional manner. If we added information to jobs about which
554 // "auxiliary" files they wrote then we could detect the conflict
555 // these cause downstream.
556 if (Archs.size() > 1) {
557 // No recovery needed, the point of this is just to prevent
558 // overwriting the same files.
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000559 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
560 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000561 << A->getAsString(Args);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000562 }
563
564 ActionList SingleActions;
565 BuildActions(Args, SingleActions);
566
567 // Add in arch binding and lipo (if necessary) for every top level
568 // action.
569 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
570 Action *Act = SingleActions[i];
571
572 // Make sure we can lipo this kind of output. If not (and it is an
573 // actual output) then we disallow, since we can't create an
574 // output file with the right name without overwriting it. We
575 // could remove this oddity by just changing the output names to
576 // include the arch, which would also fix
577 // -save-temps. Compatibility wins for now.
578
Daniel Dunbar3dbd6c52009-03-13 17:46:02 +0000579 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000580 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
581 << types::getTypeName(Act->getType());
582
583 ActionList Inputs;
Daniel Dunbar75877192009-03-19 07:55:12 +0000584 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbar13689542009-03-13 20:33:35 +0000585 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000586
587 // Lipo if necessary, We do it this way because we need to set the
588 // arch flag so that -Xarch_ gets overwritten.
589 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
590 Actions.append(Inputs.begin(), Inputs.end());
591 else
592 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
593 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000594}
595
Daniel Dunbar21549232009-03-18 02:55:38 +0000596void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000597 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000598 // Start by constructing the list of inputs and their types.
599
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000600 // Track the current user specified (-x) input. We also explicitly
601 // track the argument used to set the type; we only want to claim
602 // the type when we actually use it, so we warn about unused -x
603 // arguments.
604 types::ID InputType = types::TY_Nothing;
605 Arg *InputTypeArg = 0;
606
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000607 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
608 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
609 it != ie; ++it) {
610 Arg *A = *it;
611
612 if (isa<InputOption>(A->getOption())) {
613 const char *Value = A->getValue(Args);
614 types::ID Ty = types::TY_INVALID;
615
616 // Infer the input type if necessary.
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000617 if (InputType == types::TY_Nothing) {
618 // If there was an explicit arg for this, claim it.
619 if (InputTypeArg)
620 InputTypeArg->claim();
621
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000622 // stdin must be handled specially.
623 if (memcmp(Value, "-", 2) == 0) {
624 // If running with -E, treat as a C input (this changes the
625 // builtin macros, for example). This may be overridden by
626 // -ObjC below.
627 //
628 // Otherwise emit an error but still use a valid type to
629 // avoid spurious errors (e.g., no inputs).
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000630 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000631 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000632 Ty = types::TY_C;
633 } else {
634 // Otherwise lookup by extension, and fallback to ObjectType
Daniel Dunbare33bea42009-03-20 23:39:23 +0000635 // if not found. We use a host hook here because Darwin at
636 // least has its own idea of what .s is.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000637 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbare33bea42009-03-20 23:39:23 +0000638 Ty = Host->lookupTypeForExtension(Ext + 1);
639
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000640 if (Ty == types::TY_INVALID)
641 Ty = types::TY_Object;
642 }
643
Daniel Dunbar683ca382009-05-18 21:47:54 +0000644 // -ObjC and -ObjC++ override the default language, but only for "source
645 // files". We just treat everything that isn't a linker input as a
646 // source file.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000647 //
Daniel Dunbar683ca382009-05-18 21:47:54 +0000648 // FIXME: Clean this up if we move the phase sequence into the type.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000649 if (Ty != types::TY_Object) {
650 if (Args.hasArg(options::OPT_ObjC))
651 Ty = types::TY_ObjC;
652 else if (Args.hasArg(options::OPT_ObjCXX))
653 Ty = types::TY_ObjCXX;
654 }
655 } else {
656 assert(InputTypeArg && "InputType set w/o InputTypeArg");
657 InputTypeArg->claim();
658 Ty = InputType;
659 }
660
661 // Check that the file exists. It isn't clear this is worth
662 // doing, since the tool presumably does this anyway, and this
663 // just adds an extra stat to the equation, but this is gcc
664 // compatible.
665 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000666 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000667 else
668 Inputs.push_back(std::make_pair(Ty, A));
669
670 } else if (A->getOption().isLinkerInput()) {
671 // Just treat as object type, we could make a special type for
672 // this if necessary.
673 Inputs.push_back(std::make_pair(types::TY_Object, A));
674
675 } else if (A->getOption().getId() == options::OPT_x) {
676 InputTypeArg = A;
677 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
678
679 // Follow gcc behavior and treat as linker input for invalid -x
680 // options. Its not clear why we shouldn't just revert to
681 // unknown; but this isn't very important, we might as well be
682 // bug comatible.
683 if (!InputType) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000684 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000685 InputType = types::TY_Object;
686 }
687 }
688 }
689
Daniel Dunbar8b1604e2009-03-13 00:17:48 +0000690 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000691 Diag(clang::diag::err_drv_no_input_files);
692 return;
693 }
694
695 // Determine which compilation mode we are in. We look for options
696 // which affect the phase, starting with the earliest phases, and
697 // record which option we used to determine the final phase.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000698 Arg *FinalPhaseArg = 0;
699 phases::ID FinalPhase;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000700
701 // -{E,M,MM} only run the preprocessor.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000702 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
703 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
704 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
705 FinalPhase = phases::Preprocess;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000706
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000707 // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler.
708 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
Daniel Dunbar63be57a2009-05-06 02:12:32 +0000709 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze,
710 options::OPT__analyze_auto)) ||
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000711 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
712 FinalPhase = phases::Compile;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000713
714 // -c only runs up to the assembler.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000715 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
716 FinalPhase = phases::Assemble;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000717
718 // Otherwise do everything.
719 } else
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000720 FinalPhase = phases::Link;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000721
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000722 // Reject -Z* at the top level, these options should never have been
723 // exposed by gcc.
Daniel Dunbard7b88c22009-03-26 16:12:09 +0000724 if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000725 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000726
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000727 // Construct the actions to perform.
728 ActionList LinkerInputs;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000729 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000730 types::ID InputType = Inputs[i].first;
731 const Arg *InputArg = Inputs[i].second;
732
733 unsigned NumSteps = types::getNumCompilationPhases(InputType);
734 assert(NumSteps && "Invalid number of steps!");
735
736 // If the first step comes after the final phase we are doing as
737 // part of this compilation, warn the user about it.
738 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
739 if (InitialPhase > FinalPhase) {
Daniel Dunbar05494a72009-03-19 07:57:08 +0000740 // Claim here to avoid the more general unused warning.
741 InputArg->claim();
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000742 Diag(clang::diag::warn_drv_input_file_unused)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000743 << InputArg->getAsString(Args)
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000744 << getPhaseName(InitialPhase)
745 << FinalPhaseArg->getOption().getName();
746 continue;
747 }
748
749 // Build the pipeline for this file.
750 Action *Current = new InputAction(*InputArg, InputType);
751 for (unsigned i = 0; i != NumSteps; ++i) {
752 phases::ID Phase = types::getCompilationPhase(InputType, i);
753
754 // We are done if this step is past what the user requested.
755 if (Phase > FinalPhase)
756 break;
757
758 // Queue linker inputs.
759 if (Phase == phases::Link) {
760 assert(i + 1 == NumSteps && "linking must be final compilation step.");
761 LinkerInputs.push_back(Current);
762 Current = 0;
763 break;
764 }
765
Daniel Dunbar337a6272009-03-24 20:17:30 +0000766 // Some types skip the assembler phase (e.g., llvm-bc), but we
767 // can't encode this in the steps because the intermediate type
768 // depends on arguments. Just special case here.
769 if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
770 continue;
771
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000772 // Otherwise construct the appropriate action.
773 Current = ConstructPhaseAction(Args, Phase, Current);
774 if (Current->getType() == types::TY_Nothing)
775 break;
776 }
777
778 // If we ended with something, add to the output list.
779 if (Current)
780 Actions.push_back(Current);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000781 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000782
783 // Add a link action if necessary.
784 if (!LinkerInputs.empty())
785 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
786}
787
788Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
789 Action *Input) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000790 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000791 // Build the appropriate action.
792 switch (Phase) {
793 case phases::Link: assert(0 && "link action invalid here.");
794 case phases::Preprocess: {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +0000795 types::ID OutputTy;
796 // -{M, MM} alter the output type.
797 if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) {
798 OutputTy = types::TY_Dependencies;
799 } else {
800 OutputTy = types::getPreprocessedType(Input->getType());
801 assert(OutputTy != types::TY_INVALID &&
802 "Cannot preprocess this input type!");
803 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000804 return new PreprocessJobAction(Input, OutputTy);
805 }
806 case phases::Precompile:
807 return new PrecompileJobAction(Input, types::TY_PCH);
808 case phases::Compile: {
809 if (Args.hasArg(options::OPT_fsyntax_only)) {
810 return new CompileJobAction(Input, types::TY_Nothing);
Daniel Dunbar63be57a2009-05-06 02:12:32 +0000811 } else if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000812 return new AnalyzeJobAction(Input, types::TY_Plist);
Daniel Dunbar337a6272009-03-24 20:17:30 +0000813 } else if (Args.hasArg(options::OPT_emit_llvm) ||
814 Args.hasArg(options::OPT_flto) ||
815 Args.hasArg(options::OPT_O4)) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000816 types::ID Output =
817 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
818 return new CompileJobAction(Input, Output);
819 } else {
820 return new CompileJobAction(Input, types::TY_PP_Asm);
821 }
822 }
823 case phases::Assemble:
824 return new AssembleJobAction(Input, types::TY_Object);
825 }
826
827 assert(0 && "invalid phase in ConstructPhaseAction");
828 return 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000829}
830
Daniel Dunbar21549232009-03-18 02:55:38 +0000831void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000832 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000833 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
834 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000835
836 // FIXME: Pipes are forcibly disabled until we support executing
837 // them.
838 if (!CCCPrintBindings)
839 UsePipes = false;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000840
841 // -save-temps inhibits pipes.
842 if (SaveTemps && UsePipes) {
843 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
844 UsePipes = true;
845 }
846
847 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
848
849 // It is an error to provide a -o option if we are making multiple
850 // output files.
851 if (FinalOutput) {
852 unsigned NumOutputs = 0;
Daniel Dunbar21549232009-03-18 02:55:38 +0000853 for (ActionList::const_iterator it = C.getActions().begin(),
854 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000855 if ((*it)->getType() != types::TY_Nothing)
856 ++NumOutputs;
857
858 if (NumOutputs > 1) {
859 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
860 FinalOutput = 0;
861 }
862 }
863
Daniel Dunbar21549232009-03-18 02:55:38 +0000864 for (ActionList::const_iterator it = C.getActions().begin(),
865 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000866 Action *A = *it;
867
868 // If we are linking an image for multiple archs then the linker
869 // wants -arch_multiple and -final_output <final image
870 // name>. Unfortunately, this doesn't fit in cleanly because we
871 // have to pass this information down.
872 //
873 // FIXME: This is a hack; find a cleaner way to integrate this
874 // into the process.
875 const char *LinkingOutput = 0;
Daniel Dunbard7b88c22009-03-26 16:12:09 +0000876 if (isa<LipoJobAction>(A)) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000877 if (FinalOutput)
878 LinkingOutput = FinalOutput->getValue(C.getArgs());
879 else
880 LinkingOutput = DefaultImageName.c_str();
881 }
882
883 InputInfo II;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000884 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000885 /*CanAcceptPipe*/ true,
886 /*AtTopLevel*/ true,
887 /*LinkingOutput*/ LinkingOutput,
888 II);
889 }
Daniel Dunbar586dc232009-03-16 06:42:30 +0000890
Daniel Dunbarbf4a6762009-04-03 22:09:23 +0000891 // If the user passed -Qunused-arguments or there were errors, don't
892 // warn about any unused arguments.
Daniel Dunbar1e23f5f2009-04-07 19:04:18 +0000893 if (Diags.getNumErrors() ||
894 C.getArgs().hasArg(options::OPT_Qunused_arguments))
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000895 return;
896
Daniel Dunbara2094e72009-03-29 22:24:54 +0000897 // Claim -### here.
898 (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
899
Daniel Dunbar586dc232009-03-16 06:42:30 +0000900 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
901 it != ie; ++it) {
902 Arg *A = *it;
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000903
Daniel Dunbar586dc232009-03-16 06:42:30 +0000904 // FIXME: It would be nice to be able to send the argument to the
905 // Diagnostic, so that extra values, position, and so on could be
906 // printed.
Daniel Dunbar4f53b292009-04-04 00:52:26 +0000907 if (!A->isClaimed()) {
Daniel Dunbar1e23f5f2009-04-07 19:04:18 +0000908 if (A->getOption().hasNoArgumentUnused())
909 continue;
910
Daniel Dunbar4f53b292009-04-04 00:52:26 +0000911 // Suppress the warning automatically if this is just a flag,
912 // and it is an instance of an argument we already claimed.
913 const Option &Opt = A->getOption();
914 if (isa<FlagOption>(Opt)) {
915 bool DuplicateClaimed = false;
916
917 // FIXME: Use iterator.
918 for (ArgList::const_iterator it = C.getArgs().begin(),
919 ie = C.getArgs().end(); it != ie; ++it) {
920 if ((*it)->isClaimed() && (*it)->getOption().matches(Opt.getId())) {
921 DuplicateClaimed = true;
922 break;
923 }
924 }
925
926 if (DuplicateClaimed)
927 continue;
928 }
929
Daniel Dunbar586dc232009-03-16 06:42:30 +0000930 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000931 << A->getAsString(C.getArgs());
Daniel Dunbar4f53b292009-04-04 00:52:26 +0000932 }
Daniel Dunbar586dc232009-03-16 06:42:30 +0000933 }
Daniel Dunbar57b704d2009-03-13 22:12:33 +0000934}
935
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000936void Driver::BuildJobsForAction(Compilation &C,
937 const Action *A,
938 const ToolChain *TC,
939 bool CanAcceptPipe,
940 bool AtTopLevel,
941 const char *LinkingOutput,
942 InputInfo &Result) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000943 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action");
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000944
945 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
946 // FIXME: Pipes are forcibly disabled until we support executing
947 // them.
948 if (!CCCPrintBindings)
949 UsePipes = false;
950
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000951 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar115a7922009-03-19 07:29:38 +0000952 // FIXME: It would be nice to not claim this here; maybe the old
953 // scheme of just using Args was better?
954 const Arg &Input = IA->getInputArg();
955 Input.claim();
956 if (isa<PositionalArg>(Input)) {
957 const char *Name = Input.getValue(C.getArgs());
958 Result = InputInfo(Name, A->getType(), Name);
959 } else
960 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000961 return;
962 }
963
964 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
965 const char *ArchName = BAA->getArchName();
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000966 std::string Arch;
967 if (!ArchName) {
968 Arch = C.getDefaultToolChain().getArchName();
969 ArchName = Arch.c_str();
970 }
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000971 BuildJobsForAction(C,
972 *BAA->begin(),
973 Host->getToolChain(C.getArgs(), ArchName),
974 CanAcceptPipe,
975 AtTopLevel,
976 LinkingOutput,
977 Result);
978 return;
979 }
980
981 const JobAction *JA = cast<JobAction>(A);
982 const Tool &T = TC->SelectTool(C, *JA);
983
984 // See if we should use an integrated preprocessor. We do so when we
985 // have exactly one input, since this is the only use case we care
986 // about (irrelevant since we don't support combine yet).
987 bool UseIntegratedCPP = false;
988 const ActionList *Inputs = &A->getInputs();
989 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
990 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
991 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
992 !C.getArgs().hasArg(options::OPT_save_temps) &&
993 T.hasIntegratedCPP()) {
994 UseIntegratedCPP = true;
995 Inputs = &(*Inputs)[0]->getInputs();
996 }
997 }
998
999 // Only use pipes when there is exactly one input.
1000 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001001 InputInfoList InputInfos;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001002 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
1003 it != ie; ++it) {
1004 InputInfo II;
1005 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
1006 /*AtTopLevel*/false,
1007 LinkingOutput,
1008 II);
1009 InputInfos.push_back(II);
1010 }
1011
1012 // Determine if we should output to a pipe.
1013 bool OutputToPipe = false;
1014 if (CanAcceptPipe && T.canPipeOutput()) {
1015 // Some actions default to writing to a pipe if they are the top
1016 // level phase and there was no user override.
1017 //
1018 // FIXME: Is there a better way to handle this?
1019 if (AtTopLevel) {
1020 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
1021 OutputToPipe = true;
Daniel Dunbar60ccc762009-03-18 23:18:19 +00001022 } else if (UsePipes)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001023 OutputToPipe = true;
1024 }
1025
1026 // Figure out where to put the job (pipes).
1027 Job *Dest = &C.getJobs();
1028 if (InputInfos[0].isPipe()) {
Daniel Dunbar441d0602009-03-17 17:53:55 +00001029 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001030 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
1031 Dest = &InputInfos[0].getPipe();
1032 }
1033
1034 // Always use the first input as the base input.
1035 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar441d0602009-03-17 17:53:55 +00001036
1037 // Determine the place to write output to (nothing, pipe, or
1038 // filename) and where to put the new job.
Daniel Dunbar441d0602009-03-17 17:53:55 +00001039 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001040 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001041 } else if (OutputToPipe) {
1042 // Append to current piped job or create a new one as appropriate.
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001043 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
1044 if (!PJ) {
1045 PJ = new PipedJob();
Daniel Dunbarb7b61b22009-03-20 00:11:04 +00001046 // FIXME: Temporary hack so that -ccc-print-bindings work until
1047 // we have pipe support. Please remove later.
1048 if (!CCCPrintBindings)
1049 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar871adcf2009-03-18 07:06:02 +00001050 Dest = PJ;
Daniel Dunbar441d0602009-03-17 17:53:55 +00001051 }
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001052 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001053 } else {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001054 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
1055 A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001056 }
1057
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001058 if (CCCPrintBindings) {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00001059 llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"'
1060 << " - \"" << T.getName() << "\", inputs: [";
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001061 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
1062 llvm::errs() << InputInfos[i].getAsString();
1063 if (i + 1 != e)
1064 llvm::errs() << ", ";
1065 }
1066 llvm::errs() << "], output: " << Result.getAsString() << "\n";
1067 } else {
Daniel Dunbarf3cad362009-03-25 04:13:45 +00001068 T.ConstructJob(C, *JA, *Dest, Result, InputInfos,
1069 C.getArgsForToolChain(TC), LinkingOutput);
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001070 }
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001071}
1072
Daniel Dunbar441d0602009-03-17 17:53:55 +00001073const char *Driver::GetNamedOutputPath(Compilation &C,
1074 const JobAction &JA,
1075 const char *BaseInput,
1076 bool AtTopLevel) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +00001077 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar441d0602009-03-17 17:53:55 +00001078 // Output to a user requested destination?
1079 if (AtTopLevel) {
1080 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
1081 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
1082 }
1083
1084 // Output to a temporary file?
1085 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbar214399e2009-03-18 19:34:39 +00001086 std::string TmpName =
1087 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
1088 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar441d0602009-03-17 17:53:55 +00001089 }
1090
1091 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbar5796bf42009-03-18 02:00:31 +00001092 std::string BaseName(BasePath.getLast());
Daniel Dunbar441d0602009-03-17 17:53:55 +00001093
1094 // Determine what the derived output name should be.
1095 const char *NamedOutput;
1096 if (JA.getType() == types::TY_Image) {
1097 NamedOutput = DefaultImageName.c_str();
1098 } else {
1099 const char *Suffix = types::getTypeTempSuffix(JA.getType());
1100 assert(Suffix && "All types used for output should have a suffix.");
1101
1102 std::string::size_type End = std::string::npos;
1103 if (!types::appendSuffixForType(JA.getType()))
1104 End = BaseName.rfind('.');
1105 std::string Suffixed(BaseName.substr(0, End));
1106 Suffixed += '.';
1107 Suffixed += Suffix;
1108 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
1109 }
1110
1111 // As an annoying special case, PCH generation doesn't strip the
1112 // pathname.
1113 if (JA.getType() == types::TY_PCH) {
1114 BasePath.eraseComponent();
Daniel Dunbar56c55942009-03-18 09:58:30 +00001115 if (BasePath.isEmpty())
1116 BasePath = NamedOutput;
1117 else
1118 BasePath.appendComponent(NamedOutput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001119 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
1120 } else {
1121 return C.addResultFile(NamedOutput);
1122 }
1123}
1124
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +00001125llvm::sys::Path Driver::GetFilePath(const char *Name,
Daniel Dunbar21549232009-03-18 02:55:38 +00001126 const ToolChain &TC) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001127 const ToolChain::path_list &List = TC.getFilePaths();
1128 for (ToolChain::path_list::const_iterator
1129 it = List.begin(), ie = List.end(); it != ie; ++it) {
1130 llvm::sys::Path P(*it);
1131 P.appendComponent(Name);
1132 if (P.exists())
1133 return P;
1134 }
1135
Daniel Dunbarcb881672009-03-13 00:51:18 +00001136 return llvm::sys::Path(Name);
1137}
1138
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +00001139llvm::sys::Path Driver::GetProgramPath(const char *Name,
Mike Stump950bedd2009-03-27 00:40:20 +00001140 const ToolChain &TC,
1141 bool WantFile) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001142 const ToolChain::path_list &List = TC.getProgramPaths();
1143 for (ToolChain::path_list::const_iterator
1144 it = List.begin(), ie = List.end(); it != ie; ++it) {
1145 llvm::sys::Path P(*it);
1146 P.appendComponent(Name);
Mike Stump950bedd2009-03-27 00:40:20 +00001147 if (WantFile ? P.exists() : P.canExecute())
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001148 return P;
1149 }
1150
Daniel Dunbarc50b00d2009-03-23 16:15:50 +00001151 // If all else failed, search the path.
1152 llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
Daniel Dunbar632f50e2009-03-18 21:34:08 +00001153 if (!P.empty())
1154 return P;
1155
Daniel Dunbarcb881672009-03-13 00:51:18 +00001156 return llvm::sys::Path(Name);
1157}
1158
Daniel Dunbar214399e2009-03-18 19:34:39 +00001159std::string Driver::GetTemporaryPath(const char *Suffix) const {
1160 // FIXME: This is lame; sys::Path should provide this function (in
1161 // particular, it should know how to find the temporary files dir).
1162 std::string Error;
Daniel Dunbarb03417f2009-04-20 20:28:21 +00001163 const char *TmpDir = ::getenv("TMPDIR");
1164 if (!TmpDir)
1165 TmpDir = ::getenv("TEMP");
1166 if (!TmpDir)
Daniel Dunbar3ca7ee92009-04-21 00:25:10 +00001167 TmpDir = ::getenv("TMP");
1168 if (!TmpDir)
Daniel Dunbarb03417f2009-04-20 20:28:21 +00001169 TmpDir = "/tmp";
1170 llvm::sys::Path P(TmpDir);
Daniel Dunbarf60c63a2009-04-20 17:32:49 +00001171 P.appendComponent("cc");
Daniel Dunbar214399e2009-03-18 19:34:39 +00001172 if (P.makeUnique(false, &Error)) {
1173 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
1174 return "";
1175 }
1176
Daniel Dunbar84603bc2009-03-18 23:08:52 +00001177 // FIXME: Grumble, makeUnique sometimes leaves the file around!?
1178 // PR3837.
1179 P.eraseFromDisk(false, 0);
1180
Daniel Dunbar214399e2009-03-18 19:34:39 +00001181 P.appendSuffix(Suffix);
1182 return P.toString();
1183}
1184
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001185const HostInfo *Driver::GetHostInfo(const char *TripleStr) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +00001186 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001187 llvm::Triple Triple(TripleStr);
Daniel Dunbardd98e2c2009-03-10 23:41:59 +00001188
Daniel Dunbar1fd6c4b2009-03-17 19:00:50 +00001189 // Normalize Arch a bit.
1190 //
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001191 // FIXME: We shouldn't need to do this once everything goes through the triple
1192 // interface.
1193 if (Triple.getArchName() == "i686")
1194 Triple.setArchName("i386");
1195 else if (Triple.getArchName() == "amd64")
1196 Triple.setArchName("x86_64");
1197 else if (Triple.getArchName() == "ppc" ||
1198 Triple.getArchName() == "Power Macintosh")
1199 Triple.setArchName("powerpc");
1200 else if (Triple.getArchName() == "ppc64")
1201 Triple.setArchName("powerpc64");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001202
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001203 switch (Triple.getOS()) {
1204 case llvm::Triple::Darwin:
1205 return createDarwinHostInfo(*this, Triple);
1206 case llvm::Triple::DragonFly:
1207 return createDragonFlyHostInfo(*this, Triple);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001208 case llvm::Triple::OpenBSD:
1209 return createOpenBSDHostInfo(*this, Triple);
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001210 case llvm::Triple::FreeBSD:
1211 return createFreeBSDHostInfo(*this, Triple);
Eli Friedman6b3454a2009-05-26 07:52:18 +00001212 case llvm::Triple::Linux:
1213 return createLinuxHostInfo(*this, Triple);
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001214 default:
1215 return createUnknownHostInfo(*this, Triple);
1216 }
Daniel Dunbardd98e2c2009-03-10 23:41:59 +00001217}
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001218
1219bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
Daniel Dunbarbf54a062009-04-01 20:33:11 +00001220 const std::string &ArchNameStr) const {
1221 // FIXME: Remove this hack.
1222 const char *ArchName = ArchNameStr.c_str();
1223 if (ArchNameStr == "powerpc")
1224 ArchName = "ppc";
1225 else if (ArchNameStr == "powerpc64")
1226 ArchName = "ppc64";
1227
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001228 // Check if user requested no clang, or clang doesn't understand
1229 // this type (we only handle single inputs for now).
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001230 if (!CCCUseClang || JA.size() != 1 ||
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001231 !types::isAcceptedByClang((*JA.begin())->getType()))
1232 return false;
1233
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001234 // Otherwise make sure this is an action clang understands.
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001235 if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbar6256d362009-03-24 19:14:56 +00001236 if (!CCCUseClangCPP) {
1237 Diag(clang::diag::warn_drv_not_using_clang_cpp);
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001238 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001239 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001240 } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
1241 return false;
1242
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001243 // Use clang for C++?
Daniel Dunbar6256d362009-03-24 19:14:56 +00001244 if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
1245 Diag(clang::diag::warn_drv_not_using_clang_cxx);
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001246 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001247 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001248
Daniel Dunbarfec26bd2009-04-16 23:10:13 +00001249 // Always use clang for precompiling, regardless of archs. PTH is
1250 // platform independent, and this allows the use of the static
1251 // analyzer on platforms we don't have full IRgen support for.
1252 if (isa<PrecompileJobAction>(JA))
1253 return true;
1254
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001255 // Finally, don't use clang if this isn't one of the user specified
1256 // archs to build.
Daniel Dunbar6256d362009-03-24 19:14:56 +00001257 if (!CCCClangArchs.empty() && !CCCClangArchs.count(ArchName)) {
1258 Diag(clang::diag::warn_drv_not_using_clang_arch) << ArchName;
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001259 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001260 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001261
1262 return true;
1263}
Daniel Dunbard73fe9b2009-03-26 15:58:36 +00001264
1265/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and
1266/// return the grouped values as integers. Numbers which are not
1267/// provided are set to 0.
1268///
1269/// \return True if the entire string was parsed (9.2), or all groups
1270/// were parsed (10.3.5extrastuff).
1271bool Driver::GetReleaseVersion(const char *Str, unsigned &Major,
1272 unsigned &Minor, unsigned &Micro,
1273 bool &HadExtra) {
1274 HadExtra = false;
1275
1276 Major = Minor = Micro = 0;
1277 if (*Str == '\0')
1278 return true;
1279
1280 char *End;
1281 Major = (unsigned) strtol(Str, &End, 10);
1282 if (*Str != '\0' && *End == '\0')
1283 return true;
1284 if (*End != '.')
1285 return false;
1286
1287 Str = End+1;
1288 Minor = (unsigned) strtol(Str, &End, 10);
1289 if (*Str != '\0' && *End == '\0')
1290 return true;
1291 if (*End != '.')
1292 return false;
1293
1294 Str = End+1;
1295 Micro = (unsigned) strtol(Str, &End, 10);
1296 if (*Str != '\0' && *End == '\0')
1297 return true;
1298 if (Str == End)
1299 return false;
1300 HadExtra = true;
1301 return true;
1302}