blob: 84e58e8389a30345ed41cefd4b5711f3a6e2f305 [file] [log] [blame]
Daniel Dunbar63c4da92009-03-02 19:59:07 +00001//===--- Driver.cpp - Clang GCC Compatible Driver -----------------------*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Dunbar63c4da92009-03-02 19:59:07 +000010#include "clang/Driver/Driver.h"
Daniel Dunbar63c4da92009-03-02 19:59:07 +000011
Daniel Dunbardb62cc32009-03-12 07:58:46 +000012#include "clang/Driver/Action.h"
Daniel Dunbard6f0e372009-03-04 20:49:20 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
15#include "clang/Driver/Compilation.h"
Daniel Dunbar93468492009-03-12 08:55:43 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbard25acaa2009-03-10 23:41:59 +000017#include "clang/Driver/HostInfo.h"
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000018#include "clang/Driver/Job.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000019#include "clang/Driver/Option.h"
Daniel Dunbard6f0e372009-03-04 20:49:20 +000020#include "clang/Driver/Options.h"
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000021#include "clang/Driver/Tool.h"
22#include "clang/Driver/ToolChain.h"
Daniel Dunbardb62cc32009-03-12 07:58:46 +000023#include "clang/Driver/Types.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000024
Douglas Gregorb7064742009-04-27 22:23:34 +000025#include "clang/Basic/Version.h"
26
Daniel Dunbarb1873cd2009-03-13 20:33:35 +000027#include "llvm/ADT/StringSet.h"
Daniel Dunbar16e04ff2009-03-18 01:38:48 +000028#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000029#include "llvm/Support/raw_ostream.h"
Daniel Dunbardb62cc32009-03-12 07:58:46 +000030#include "llvm/System/Path.h"
Daniel Dunbarcb84b9a2009-03-18 21:34:08 +000031#include "llvm/System/Program.h"
Daniel Dunbar494646b2009-03-13 12:19:02 +000032
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000033#include "InputInfo.h"
34
Daniel Dunbar494646b2009-03-13 12:19:02 +000035#include <map>
36
Daniel Dunbard6f0e372009-03-04 20:49:20 +000037using namespace clang::driver;
Chris Lattnerc272d262009-03-26 05:56:24 +000038using namespace clang;
Daniel Dunbard6f0e372009-03-04 20:49:20 +000039
Daniel Dunbar89301fb2009-08-23 18:42:54 +000040// Used to set values for "production" clang, for releases.
Daniel Dunbard83eaa02009-08-23 19:41:53 +000041// #define USE_PRODUCTION_CLANG
Daniel Dunbar89301fb2009-08-23 18:42:54 +000042
Daniel Dunbard25acaa2009-03-10 23:41:59 +000043Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar93468492009-03-12 08:55:43 +000044 const char *_DefaultHostTriple,
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000045 const char *_DefaultImageName,
Daniel Dunbar93468492009-03-12 08:55:43 +000046 Diagnostic &_Diags)
47 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbard25acaa2009-03-10 23:41:59 +000048 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000049 DefaultImageName(_DefaultImageName),
Daniel Dunbard25acaa2009-03-10 23:41:59 +000050 Host(0),
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +000051 CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false),
Daniel Dunbar89301fb2009-08-23 18:42:54 +000052 CCCGenericGCCName("gcc"), CCCUseClang(true),
53#ifdef USE_PRODUCTION_CLANG
54 CCCUseClangCXX(false),
55#else
56 CCCUseClangCXX(true),
57#endif
Douglas Gregorf93696d2009-04-28 22:44:02 +000058 CCCUseClangCPP(true), CCCUsePCH(true),
Daniel Dunbar5a5ec5c2009-03-13 00:17:48 +000059 SuppressMissingInputWarning(false)
Daniel Dunbarb282ced2009-03-10 20:52:46 +000060{
Daniel Dunbar89301fb2009-08-23 18:42:54 +000061#ifdef USE_PRODUCTION_CLANG
62 // Only use clang on i386 and x86_64 by default, in a "production" build.
63 CCCClangArchs.insert("i386");
64 CCCClangArchs.insert("x86_64");
65#endif
Daniel Dunbar63c4da92009-03-02 19:59:07 +000066}
67
68Driver::~Driver() {
Daniel Dunbard6f0e372009-03-04 20:49:20 +000069 delete Opts;
Daniel Dunbar2f8b37e2009-03-18 01:09:40 +000070 delete Host;
Daniel Dunbar63c4da92009-03-02 19:59:07 +000071}
72
Daniel Dunbara16e4fe2009-03-25 04:13:45 +000073InputArgList *Driver::ParseArgStrings(const char **ArgBegin,
74 const char **ArgEnd) {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +000075 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbara16e4fe2009-03-25 04:13:45 +000076 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000077
Daniel Dunbar85cb3592009-03-13 11:38:42 +000078 // FIXME: Handle '@' args (or at least error on them).
79
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000080 unsigned Index = 0, End = ArgEnd - ArgBegin;
81 while (Index < End) {
Daniel Dunbarb043ebd2009-03-13 01:01:44 +000082 // gcc's handling of empty arguments doesn't make
83 // sense, but this is not a common use case. :)
84 //
85 // We just ignore them here (note that other things may
86 // still take them as arguments).
87 if (Args->getArgString(Index)[0] == '\0') {
88 ++Index;
89 continue;
90 }
91
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000092 unsigned Prev = Index;
Daniel Dunbarfb88ce02009-03-22 23:26:43 +000093 Arg *A = getOpts().ParseOneArg(*Args, Index);
94 assert(Index > Prev && "Parser failed to consume argument.");
Daniel Dunbardb62cc32009-03-12 07:58:46 +000095
Daniel Dunbarfb88ce02009-03-22 23:26:43 +000096 // Check for missing argument error.
97 if (!A) {
98 assert(Index >= End && "Unexpected parser error.");
99 Diag(clang::diag::err_drv_missing_argument)
100 << Args->getArgString(Prev)
101 << (Index - Prev - 1);
102 break;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000103 }
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000104
Daniel Dunbarfb88ce02009-03-22 23:26:43 +0000105 if (A->getOption().isUnsupported()) {
106 Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
107 continue;
108 }
109 Args->append(A);
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000110 }
111
112 return Args;
113}
114
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000115Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000116 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
117
Daniel Dunbarcc006892009-03-13 00:51:18 +0000118 // FIXME: Handle environment options which effect driver behavior,
119 // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH,
120 // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS.
121
122 // FIXME: What are we going to do with -V and -b?
123
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000124 // FIXME: This stuff needs to go into the Compilation, not the
125 // driver.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000126 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000127
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000128 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000129 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000130
131 // Read -ccc args.
132 //
133 // FIXME: We need to figure out where this behavior should
134 // live. Most of it should be outside in the client; the parts that
135 // aren't should have proper options, either by introducing new ones
136 // or by overloading gcc ones like -V or -b.
137 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
138 const char *Opt = *Start + 5;
139
140 if (!strcmp(Opt, "print-options")) {
141 CCCPrintOptions = true;
142 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000143 CCCPrintActions = true;
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000144 } else if (!strcmp(Opt, "print-bindings")) {
145 CCCPrintBindings = true;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000146 } else if (!strcmp(Opt, "cxx")) {
147 CCCIsCXX = true;
148 } else if (!strcmp(Opt, "echo")) {
149 CCCEcho = true;
150
Daniel Dunbar126da5e2009-04-01 23:34:41 +0000151 } else if (!strcmp(Opt, "gcc-name")) {
152 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
153 CCCGenericGCCName = *++Start;
154
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000155 } else if (!strcmp(Opt, "clang-cxx")) {
156 CCCUseClangCXX = true;
Daniel Dunbar422ce672009-07-23 17:48:59 +0000157 } else if (!strcmp(Opt, "no-clang-cxx")) {
158 CCCUseClangCXX = false;
Douglas Gregor95734502009-04-18 00:34:01 +0000159 } else if (!strcmp(Opt, "pch-is-pch")) {
160 CCCUsePCH = true;
161 } else if (!strcmp(Opt, "pch-is-pth")) {
162 CCCUsePCH = false;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000163 } else if (!strcmp(Opt, "no-clang")) {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000164 CCCUseClang = false;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000165 } else if (!strcmp(Opt, "no-clang-cpp")) {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000166 CCCUseClangCPP = false;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000167 } else if (!strcmp(Opt, "clang-archs")) {
168 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
169 const char *Cur = *++Start;
170
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000171 CCCClangArchs.clear();
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000172 for (;;) {
173 const char *Next = strchr(Cur, ',');
174
175 if (Next) {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000176 if (Cur != Next)
177 CCCClangArchs.insert(std::string(Cur, Next));
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000178 Cur = Next + 1;
179 } else {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000180 if (*Cur != '\0')
181 CCCClangArchs.insert(std::string(Cur));
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000182 break;
183 }
184 }
185
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000186 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000187 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000188 HostTriple = *++Start;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000189
190 } else {
191 // FIXME: Error handling.
192 llvm::errs() << "invalid option: " << *Start << "\n";
193 exit(1);
194 }
195 }
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000196
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000197 InputArgList *Args = ParseArgStrings(Start, End);
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000198
Daniel Dunbar08966ca2009-03-17 20:45:45 +0000199 Host = GetHostInfo(HostTriple);
Daniel Dunbarcc006892009-03-13 00:51:18 +0000200
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000201 // The compilation takes ownership of Args.
Daniel Dunbar31a76e32009-03-18 22:16:03 +0000202 Compilation *C = new Compilation(*this, *Host->getToolChain(*Args), Args);
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000203
204 // FIXME: This behavior shouldn't be here.
205 if (CCCPrintOptions) {
206 PrintOptions(C->getArgs());
207 return C;
208 }
209
210 if (!HandleImmediateArgs(*C))
211 return C;
212
213 // Construct the list of abstract actions to perform for this
214 // compilation. We avoid passing a Compilation here simply to
215 // enforce the abstraction that pipelining is not host or toolchain
216 // dependent (other than the driver driver test).
217 if (Host->useDriverDriver())
218 BuildUniversalActions(C->getArgs(), C->getActions());
219 else
220 BuildActions(C->getArgs(), C->getActions());
221
222 if (CCCPrintActions) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000223 PrintActions(*C);
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000224 return C;
225 }
226
227 BuildJobs(*C);
Daniel Dunbarc413f822009-03-15 01:38:15 +0000228
229 return C;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000230}
231
Daniel Dunbar9fbdc882009-07-01 20:03:04 +0000232int Driver::ExecuteCompilation(const Compilation &C) const {
233 // Just print if -### was present.
234 if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
235 C.PrintJob(llvm::errs(), C.getJobs(), "\n", true);
236 return 0;
237 }
238
239 // If there were errors building the compilation, quit now.
240 if (getDiags().getNumErrors())
241 return 1;
242
243 const Command *FailingCommand = 0;
244 int Res = C.ExecuteJob(C.getJobs(), FailingCommand);
245
246 // Remove temp files.
247 C.CleanupFileList(C.getTempFiles());
248
249 // If the compilation failed, remove result files as well.
250 if (Res != 0 && !C.getArgs().hasArg(options::OPT_save_temps))
251 C.CleanupFileList(C.getResultFiles(), true);
252
253 // Print extra information about abnormal failures, if possible.
254 if (Res) {
255 // This is ad-hoc, but we don't want to be excessively noisy. If the result
256 // status was 1, assume the command failed normally. In particular, if it
257 // was the compiler then assume it gave a reasonable error code. Failures in
258 // other tools are less common, and they generally have worse diagnostics,
259 // so always print the diagnostic there.
260 const Action &Source = FailingCommand->getSource();
261 bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) ||
262 isa<PrecompileJobAction>(Source) ||
263 isa<AnalyzeJobAction>(Source) ||
264 isa<CompileJobAction>(Source));
265
266 if (!IsFriendlyTool || Res != 1) {
267 // FIXME: See FIXME above regarding result code interpretation.
268 if (Res < 0)
269 Diag(clang::diag::err_drv_command_signalled)
270 << Source.getClassName() << -Res;
271 else
272 Diag(clang::diag::err_drv_command_failed)
273 << Source.getClassName() << Res;
274 }
275 }
276
277 return Res;
278}
279
Daniel Dunbara790d372009-03-12 18:24:49 +0000280void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000281 unsigned i = 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000282 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000283 it != ie; ++it, ++i) {
284 Arg *A = *it;
285 llvm::errs() << "Option " << i << " - "
286 << "Name: \"" << A->getOption().getName() << "\", "
287 << "Values: {";
288 for (unsigned j = 0; j < A->getNumValues(); ++j) {
289 if (j)
290 llvm::errs() << ", ";
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000291 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000292 }
293 llvm::errs() << "}\n";
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000294 }
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000295}
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000296
Daniel Dunbare627b682009-03-31 21:38:17 +0000297static std::string getOptionHelpName(const OptTable &Opts, options::ID Id) {
298 std::string Name = Opts.getOptionName(Id);
299
300 // Add metavar, if used.
301 switch (Opts.getOptionKind(Id)) {
302 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
303 assert(0 && "Invalid option with help text.");
304
305 case Option::MultiArgClass: case Option::JoinedAndSeparateClass:
306 assert(0 && "Cannot print metavar for this kind of option.");
307
308 case Option::FlagClass:
309 break;
310
311 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
312 Name += ' ';
313 // FALLTHROUGH
314 case Option::JoinedClass: case Option::CommaJoinedClass:
315 Name += Opts.getOptionMetaVar(Id);
316 break;
317 }
318
319 return Name;
320}
321
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000322void Driver::PrintHelp(bool ShowHidden) const {
Daniel Dunbare627b682009-03-31 21:38:17 +0000323 llvm::raw_ostream &OS = llvm::outs();
324
325 OS << "OVERVIEW: clang \"gcc-compatible\" driver\n";
326 OS << '\n';
327 OS << "USAGE: " << Name << " [options] <input files>\n";
328 OS << '\n';
329 OS << "OPTIONS:\n";
330
331 // Render help text into (option, help) pairs.
332 std::vector< std::pair<std::string, const char*> > OptionHelp;
333
334 for (unsigned i = options::OPT_INPUT, e = options::LastOption; i != e; ++i) {
335 options::ID Id = (options::ID) i;
336 if (const char *Text = getOpts().getOptionHelpText(Id))
337 OptionHelp.push_back(std::make_pair(getOptionHelpName(getOpts(), Id),
338 Text));
339 }
340
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000341 if (ShowHidden) {
342 OptionHelp.push_back(std::make_pair("\nDRIVER OPTIONS:",""));
343 OptionHelp.push_back(std::make_pair("-ccc-cxx",
344 "Act as a C++ driver"));
345 OptionHelp.push_back(std::make_pair("-ccc-gcc-name",
346 "Name for native GCC compiler"));
347 OptionHelp.push_back(std::make_pair("-ccc-clang-cxx",
Daniel Dunbarec438002009-09-01 16:57:46 +0000348 "Enable the clang compiler for C++"));
349 OptionHelp.push_back(std::make_pair("-ccc-no-clang-cxx",
350 "Disable the clang compiler for C++"));
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000351 OptionHelp.push_back(std::make_pair("-ccc-no-clang",
Daniel Dunbarec438002009-09-01 16:57:46 +0000352 "Disable the clang compiler"));
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000353 OptionHelp.push_back(std::make_pair("-ccc-no-clang-cpp",
Daniel Dunbarec438002009-09-01 16:57:46 +0000354 "Disable the clang preprocessor"));
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000355 OptionHelp.push_back(std::make_pair("-ccc-clang-archs",
356 "Comma separate list of architectures "
357 "to use the clang compiler for"));
Douglas Gregor95734502009-04-18 00:34:01 +0000358 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pch",
359 "Use lazy PCH for precompiled headers"));
360 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pth",
361 "Use pretokenized headers for precompiled headers"));
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000362
363 OptionHelp.push_back(std::make_pair("\nDEBUG/DEVELOPMENT OPTIONS:",""));
364 OptionHelp.push_back(std::make_pair("-ccc-host-triple",
365 "Simulate running on the given target"));
366 OptionHelp.push_back(std::make_pair("-ccc-print-options",
367 "Dump parsed command line arguments"));
368 OptionHelp.push_back(std::make_pair("-ccc-print-phases",
369 "Dump list of actions to perform"));
370 OptionHelp.push_back(std::make_pair("-ccc-print-bindings",
371 "Show bindings of tools to actions"));
372 OptionHelp.push_back(std::make_pair("CCC_ADD_ARGS",
373 "(ENVIRONMENT VARIABLE) Comma separated list of "
374 "arguments to prepend to the command line"));
375 }
376
Daniel Dunbare627b682009-03-31 21:38:17 +0000377 // Find the maximum option length.
378 unsigned OptionFieldWidth = 0;
379 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000380 // Skip titles.
381 if (!OptionHelp[i].second)
382 continue;
383
Daniel Dunbare627b682009-03-31 21:38:17 +0000384 // Limit the amount of padding we are willing to give up for
385 // alignment.
386 unsigned Length = OptionHelp[i].first.size();
387 if (Length <= 23)
388 OptionFieldWidth = std::max(OptionFieldWidth, Length);
389 }
390
391 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
392 const std::string &Option = OptionHelp[i].first;
393 OS << " " << Option;
394 for (int j = Option.length(), e = OptionFieldWidth; j < e; ++j)
395 OS << ' ';
396 OS << ' ' << OptionHelp[i].second << '\n';
397 }
398
399 OS.flush();
400}
401
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000402void Driver::PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const {
Mike Stump74d94472009-03-18 14:00:02 +0000403 static char buf[] = "$URL$";
404 char *zap = strstr(buf, "/lib/Driver");
405 if (zap)
406 *zap = 0;
407 zap = strstr(buf, "/clang/tools/clang");
408 if (zap)
409 *zap = 0;
Mike Stumpbc927292009-03-18 15:19:35 +0000410 const char *vers = buf+6;
Mike Stump3fc58f02009-03-18 18:45:55 +0000411 // FIXME: Add cmake support and remove #ifdef
412#ifdef SVN_REVISION
413 const char *revision = SVN_REVISION;
414#else
415 const char *revision = "";
416#endif
Daniel Dunbarcc006892009-03-13 00:51:18 +0000417 // FIXME: The following handlers should use a callback mechanism, we
418 // don't know what the client would like to do.
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000419 OS << "clang version " CLANG_VERSION_STRING " ("
Daniel Dunbar41c34b32009-06-16 23:32:58 +0000420 << vers << " " << revision << ")" << '\n';
Daniel Dunbar0ded7de2009-03-26 16:09:13 +0000421
422 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000423 OS << "Target: " << TC.getTripleString() << '\n';
Daniel Dunbar41c34b32009-06-16 23:32:58 +0000424
425 // Print the threading model.
426 //
427 // FIXME: Implement correctly.
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000428 OS << "Thread model: " << "posix" << '\n';
Daniel Dunbarcc006892009-03-13 00:51:18 +0000429}
430
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000431bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbarcc006892009-03-13 00:51:18 +0000432 // The order these options are handled in in gcc is all over the
433 // place, but we don't expect inconsistencies w.r.t. that to matter
434 // in practice.
Daniel Dunbare627b682009-03-31 21:38:17 +0000435
Daniel Dunbar9a553c42009-04-04 05:17:38 +0000436 if (C.getArgs().hasArg(options::OPT_dumpversion)) {
Douglas Gregorb7064742009-04-27 22:23:34 +0000437 llvm::outs() << CLANG_VERSION_STRING "\n";
Daniel Dunbar9a553c42009-04-04 05:17:38 +0000438 return false;
439 }
440
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000441 if (C.getArgs().hasArg(options::OPT__help) ||
442 C.getArgs().hasArg(options::OPT__help_hidden)) {
443 PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden));
Daniel Dunbare627b682009-03-31 21:38:17 +0000444 return false;
445 }
446
Daniel Dunbar3b7c84c2009-04-02 15:05:41 +0000447 if (C.getArgs().hasArg(options::OPT__version)) {
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000448 // Follow gcc behavior and use stdout for --version and stderr for -v
449 PrintVersion(C, llvm::outs());
Daniel Dunbar3b7c84c2009-04-02 15:05:41 +0000450 return false;
451 }
452
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000453 if (C.getArgs().hasArg(options::OPT_v) ||
454 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000455 PrintVersion(C, llvm::errs());
Daniel Dunbarcc006892009-03-13 00:51:18 +0000456 SuppressMissingInputWarning = true;
457 }
458
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000459 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbar91b9e202009-03-20 04:37:21 +0000460 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
461 llvm::outs() << "programs: =";
462 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
463 ie = TC.getProgramPaths().end(); it != ie; ++it) {
464 if (it != TC.getProgramPaths().begin())
465 llvm::outs() << ':';
466 llvm::outs() << *it;
467 }
468 llvm::outs() << "\n";
469 llvm::outs() << "libraries: =";
470 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
471 ie = TC.getFilePaths().end(); it != ie; ++it) {
472 if (it != TC.getFilePaths().begin())
473 llvm::outs() << ':';
474 llvm::outs() << *it;
475 }
476 llvm::outs() << "\n";
Daniel Dunbare627b682009-03-31 21:38:17 +0000477 return false;
Daniel Dunbar91b9e202009-03-20 04:37:21 +0000478 }
479
Daniel Dunbarcc006892009-03-13 00:51:18 +0000480 // FIXME: The following handlers should use a callback mechanism, we
481 // don't know what the client would like to do.
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000482 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
Chris Lattner31bc3042009-08-23 22:45:33 +0000483 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).str()
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000484 << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000485 return false;
486 }
487
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000488 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
Chris Lattner31bc3042009-08-23 22:45:33 +0000489 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).str()
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000490 << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000491 return false;
492 }
493
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000494 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
Chris Lattner31bc3042009-08-23 22:45:33 +0000495 llvm::outs() << GetFilePath("libgcc.a", TC).str() << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000496 return false;
497 }
498
Daniel Dunbar329aa992009-06-16 23:25:22 +0000499 if (C.getArgs().hasArg(options::OPT_print_multi_lib)) {
500 // FIXME: We need tool chain support for this.
501 llvm::outs() << ".;\n";
502
503 switch (C.getDefaultToolChain().getTriple().getArch()) {
504 default:
505 break;
506
507 case llvm::Triple::x86_64:
508 llvm::outs() << "x86_64;@m64" << "\n";
509 break;
510
511 case llvm::Triple::ppc64:
512 llvm::outs() << "ppc64;@m64" << "\n";
513 break;
514 }
515 return false;
516 }
517
518 // FIXME: What is the difference between print-multi-directory and
519 // print-multi-os-directory?
520 if (C.getArgs().hasArg(options::OPT_print_multi_directory) ||
521 C.getArgs().hasArg(options::OPT_print_multi_os_directory)) {
522 switch (C.getDefaultToolChain().getTriple().getArch()) {
523 default:
524 case llvm::Triple::x86:
525 case llvm::Triple::ppc:
526 llvm::outs() << "." << "\n";
527 break;
528
529 case llvm::Triple::x86_64:
530 llvm::outs() << "x86_64" << "\n";
531 break;
532
533 case llvm::Triple::ppc64:
534 llvm::outs() << "ppc64" << "\n";
535 break;
536 }
537 return false;
538 }
539
Daniel Dunbarcc006892009-03-13 00:51:18 +0000540 return true;
541}
542
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000543static unsigned PrintActions1(const Compilation &C,
Daniel Dunbar494646b2009-03-13 12:19:02 +0000544 Action *A,
545 std::map<Action*, unsigned> &Ids) {
546 if (Ids.count(A))
547 return Ids[A];
548
549 std::string str;
550 llvm::raw_string_ostream os(str);
551
552 os << Action::getClassName(A->getKind()) << ", ";
553 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000554 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbar494646b2009-03-13 12:19:02 +0000555 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000556 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
557 C.getDefaultToolChain().getArchName()) << '"'
558 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbar494646b2009-03-13 12:19:02 +0000559 } else {
560 os << "{";
561 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000562 os << PrintActions1(C, *it, Ids);
Daniel Dunbar494646b2009-03-13 12:19:02 +0000563 ++it;
564 if (it != ie)
565 os << ", ";
566 }
567 os << "}";
568 }
569
570 unsigned Id = Ids.size();
571 Ids[A] = Id;
Daniel Dunbar9dc28b82009-03-13 17:20:20 +0000572 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbar494646b2009-03-13 12:19:02 +0000573 << types::getTypeName(A->getType()) << "\n";
574
575 return Id;
576}
577
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000578void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbar494646b2009-03-13 12:19:02 +0000579 std::map<Action*, unsigned> Ids;
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000580 for (ActionList::const_iterator it = C.getActions().begin(),
581 ie = C.getActions().end(); it != ie; ++it)
582 PrintActions1(C, *it, Ids);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000583}
584
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000585void Driver::BuildUniversalActions(const ArgList &Args,
586 ActionList &Actions) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000587 llvm::PrettyStackTraceString CrashInfo("Building actions for universal build");
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000588 // Collect the list of architectures. Duplicates are allowed, but
589 // should only be handled once (in the order seen).
590 llvm::StringSet<> ArchNames;
591 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000592 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
593 it != ie; ++it) {
594 Arg *A = *it;
595
596 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000597 const char *Name = A->getValue(Args);
598
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000599 // FIXME: We need to handle canonicalization of the specified
600 // arch?
601
Daniel Dunbara345a442009-03-19 07:55:12 +0000602 A->claim();
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000603 if (ArchNames.insert(Name))
604 Archs.push_back(Name);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000605 }
606 }
607
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000608 // When there is no explicit arch for this platform, make sure we
609 // still bind the architecture (to the default) so that -Xarch_ is
610 // handled correctly.
611 if (!Archs.size())
612 Archs.push_back(0);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000613
614 // FIXME: We killed off some others but these aren't yet detected in
615 // a functional manner. If we added information to jobs about which
616 // "auxiliary" files they wrote then we could detect the conflict
617 // these cause downstream.
618 if (Archs.size() > 1) {
619 // No recovery needed, the point of this is just to prevent
620 // overwriting the same files.
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000621 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
622 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar73225932009-03-20 06:14:23 +0000623 << A->getAsString(Args);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000624 }
625
626 ActionList SingleActions;
627 BuildActions(Args, SingleActions);
628
629 // Add in arch binding and lipo (if necessary) for every top level
630 // action.
631 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
632 Action *Act = SingleActions[i];
633
634 // Make sure we can lipo this kind of output. If not (and it is an
635 // actual output) then we disallow, since we can't create an
636 // output file with the right name without overwriting it. We
637 // could remove this oddity by just changing the output names to
638 // include the arch, which would also fix
639 // -save-temps. Compatibility wins for now.
640
Daniel Dunbardd863aa2009-03-13 17:46:02 +0000641 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000642 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
643 << types::getTypeName(Act->getType());
644
645 ActionList Inputs;
Daniel Dunbara345a442009-03-19 07:55:12 +0000646 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000647 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000648
649 // Lipo if necessary, We do it this way because we need to set the
650 // arch flag so that -Xarch_ gets overwritten.
651 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
652 Actions.append(Inputs.begin(), Inputs.end());
653 else
654 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
655 }
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000656}
657
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000658void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000659 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000660 // Start by constructing the list of inputs and their types.
661
Daniel Dunbar5cb75d62009-03-13 17:57:10 +0000662 // Track the current user specified (-x) input. We also explicitly
663 // track the argument used to set the type; we only want to claim
664 // the type when we actually use it, so we warn about unused -x
665 // arguments.
666 types::ID InputType = types::TY_Nothing;
667 Arg *InputTypeArg = 0;
668
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000669 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
670 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
671 it != ie; ++it) {
672 Arg *A = *it;
673
674 if (isa<InputOption>(A->getOption())) {
675 const char *Value = A->getValue(Args);
676 types::ID Ty = types::TY_INVALID;
677
678 // Infer the input type if necessary.
Daniel Dunbar5cb75d62009-03-13 17:57:10 +0000679 if (InputType == types::TY_Nothing) {
680 // If there was an explicit arg for this, claim it.
681 if (InputTypeArg)
682 InputTypeArg->claim();
683
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000684 // stdin must be handled specially.
685 if (memcmp(Value, "-", 2) == 0) {
686 // If running with -E, treat as a C input (this changes the
687 // builtin macros, for example). This may be overridden by
688 // -ObjC below.
689 //
690 // Otherwise emit an error but still use a valid type to
691 // avoid spurious errors (e.g., no inputs).
Daniel Dunbare9c70fa2009-03-15 00:48:16 +0000692 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbard724e332009-03-12 09:13:48 +0000693 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000694 Ty = types::TY_C;
695 } else {
696 // Otherwise lookup by extension, and fallback to ObjectType
Daniel Dunbar7b6dfbd2009-03-20 23:39:23 +0000697 // if not found. We use a host hook here because Darwin at
698 // least has its own idea of what .s is.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000699 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbar7b6dfbd2009-03-20 23:39:23 +0000700 Ty = Host->lookupTypeForExtension(Ext + 1);
701
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000702 if (Ty == types::TY_INVALID)
703 Ty = types::TY_Object;
704 }
705
Daniel Dunbar84266db2009-05-18 21:47:54 +0000706 // -ObjC and -ObjC++ override the default language, but only for "source
707 // files". We just treat everything that isn't a linker input as a
708 // source file.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000709 //
Daniel Dunbar84266db2009-05-18 21:47:54 +0000710 // FIXME: Clean this up if we move the phase sequence into the type.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000711 if (Ty != types::TY_Object) {
712 if (Args.hasArg(options::OPT_ObjC))
713 Ty = types::TY_ObjC;
714 else if (Args.hasArg(options::OPT_ObjCXX))
715 Ty = types::TY_ObjCXX;
716 }
717 } else {
718 assert(InputTypeArg && "InputType set w/o InputTypeArg");
719 InputTypeArg->claim();
720 Ty = InputType;
721 }
722
723 // Check that the file exists. It isn't clear this is worth
724 // doing, since the tool presumably does this anyway, and this
725 // just adds an extra stat to the equation, but this is gcc
726 // compatible.
727 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbard724e332009-03-12 09:13:48 +0000728 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000729 else
730 Inputs.push_back(std::make_pair(Ty, A));
731
732 } else if (A->getOption().isLinkerInput()) {
733 // Just treat as object type, we could make a special type for
734 // this if necessary.
735 Inputs.push_back(std::make_pair(types::TY_Object, A));
736
737 } else if (A->getOption().getId() == options::OPT_x) {
738 InputTypeArg = A;
739 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
740
741 // Follow gcc behavior and treat as linker input for invalid -x
742 // options. Its not clear why we shouldn't just revert to
743 // unknown; but this isn't very important, we might as well be
744 // bug comatible.
745 if (!InputType) {
Daniel Dunbard724e332009-03-12 09:13:48 +0000746 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000747 InputType = types::TY_Object;
748 }
749 }
750 }
751
Daniel Dunbar5a5ec5c2009-03-13 00:17:48 +0000752 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000753 Diag(clang::diag::err_drv_no_input_files);
754 return;
755 }
756
757 // Determine which compilation mode we are in. We look for options
758 // which affect the phase, starting with the earliest phases, and
759 // record which option we used to determine the final phase.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000760 Arg *FinalPhaseArg = 0;
761 phases::ID FinalPhase;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000762
763 // -{E,M,MM} only run the preprocessor.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000764 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
765 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
766 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
767 FinalPhase = phases::Preprocess;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000768
Daniel Dunbarec438002009-09-01 16:57:46 +0000769 // -{fsyntax-only,-analyze,emit-ast,S} only run up to the compiler.
Daniel Dunbare9c70fa2009-03-15 00:48:16 +0000770 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
Daniel Dunbar6e3de3d2009-05-06 02:12:32 +0000771 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze,
772 options::OPT__analyze_auto)) ||
Daniel Dunbarec438002009-09-01 16:57:46 +0000773 (FinalPhaseArg = Args.getLastArg(options::OPT_emit_ast)) ||
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000774 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
775 FinalPhase = phases::Compile;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000776
777 // -c only runs up to the assembler.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000778 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
779 FinalPhase = phases::Assemble;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000780
781 // Otherwise do everything.
782 } else
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000783 FinalPhase = phases::Link;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000784
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000785 // Reject -Z* at the top level, these options should never have been
786 // exposed by gcc.
Daniel Dunbar55e34e32009-03-26 16:12:09 +0000787 if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
Daniel Dunbar73225932009-03-20 06:14:23 +0000788 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000789
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000790 // Construct the actions to perform.
791 ActionList LinkerInputs;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000792 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000793 types::ID InputType = Inputs[i].first;
794 const Arg *InputArg = Inputs[i].second;
795
796 unsigned NumSteps = types::getNumCompilationPhases(InputType);
797 assert(NumSteps && "Invalid number of steps!");
798
799 // If the first step comes after the final phase we are doing as
800 // part of this compilation, warn the user about it.
801 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
802 if (InitialPhase > FinalPhase) {
Daniel Dunbar923e7032009-03-19 07:57:08 +0000803 // Claim here to avoid the more general unused warning.
804 InputArg->claim();
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000805 Diag(clang::diag::warn_drv_input_file_unused)
Daniel Dunbar73225932009-03-20 06:14:23 +0000806 << InputArg->getAsString(Args)
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000807 << getPhaseName(InitialPhase)
808 << FinalPhaseArg->getOption().getName();
809 continue;
810 }
811
812 // Build the pipeline for this file.
813 Action *Current = new InputAction(*InputArg, InputType);
814 for (unsigned i = 0; i != NumSteps; ++i) {
815 phases::ID Phase = types::getCompilationPhase(InputType, i);
816
817 // We are done if this step is past what the user requested.
818 if (Phase > FinalPhase)
819 break;
820
821 // Queue linker inputs.
822 if (Phase == phases::Link) {
823 assert(i + 1 == NumSteps && "linking must be final compilation step.");
824 LinkerInputs.push_back(Current);
825 Current = 0;
826 break;
827 }
828
Daniel Dunbardec14452009-03-24 20:17:30 +0000829 // Some types skip the assembler phase (e.g., llvm-bc), but we
830 // can't encode this in the steps because the intermediate type
831 // depends on arguments. Just special case here.
832 if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
833 continue;
834
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000835 // Otherwise construct the appropriate action.
836 Current = ConstructPhaseAction(Args, Phase, Current);
837 if (Current->getType() == types::TY_Nothing)
838 break;
839 }
840
841 // If we ended with something, add to the output list.
842 if (Current)
843 Actions.push_back(Current);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000844 }
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000845
846 // Add a link action if necessary.
847 if (!LinkerInputs.empty())
848 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
849}
850
851Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
852 Action *Input) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000853 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000854 // Build the appropriate action.
855 switch (Phase) {
856 case phases::Link: assert(0 && "link action invalid here.");
857 case phases::Preprocess: {
Daniel Dunbar049b7242009-03-30 06:36:42 +0000858 types::ID OutputTy;
859 // -{M, MM} alter the output type.
860 if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) {
861 OutputTy = types::TY_Dependencies;
862 } else {
863 OutputTy = types::getPreprocessedType(Input->getType());
864 assert(OutputTy != types::TY_INVALID &&
865 "Cannot preprocess this input type!");
866 }
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000867 return new PreprocessJobAction(Input, OutputTy);
868 }
869 case phases::Precompile:
870 return new PrecompileJobAction(Input, types::TY_PCH);
871 case phases::Compile: {
872 if (Args.hasArg(options::OPT_fsyntax_only)) {
873 return new CompileJobAction(Input, types::TY_Nothing);
Daniel Dunbar6e3de3d2009-05-06 02:12:32 +0000874 } else if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) {
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000875 return new AnalyzeJobAction(Input, types::TY_Plist);
Daniel Dunbarec438002009-09-01 16:57:46 +0000876 } else if (Args.hasArg(options::OPT_emit_ast)) {
877 return new CompileJobAction(Input, types::TY_AST);
Daniel Dunbardec14452009-03-24 20:17:30 +0000878 } else if (Args.hasArg(options::OPT_emit_llvm) ||
879 Args.hasArg(options::OPT_flto) ||
880 Args.hasArg(options::OPT_O4)) {
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000881 types::ID Output =
882 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
883 return new CompileJobAction(Input, Output);
884 } else {
885 return new CompileJobAction(Input, types::TY_PP_Asm);
886 }
887 }
888 case phases::Assemble:
889 return new AssembleJobAction(Input, types::TY_Object);
890 }
891
892 assert(0 && "invalid phase in ConstructPhaseAction");
893 return 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000894}
895
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000896void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000897 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000898 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
899 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbarbac6b642009-03-18 23:18:19 +0000900
901 // FIXME: Pipes are forcibly disabled until we support executing
902 // them.
903 if (!CCCPrintBindings)
904 UsePipes = false;
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000905
906 // -save-temps inhibits pipes.
907 if (SaveTemps && UsePipes) {
908 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
909 UsePipes = true;
910 }
911
912 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
913
914 // It is an error to provide a -o option if we are making multiple
915 // output files.
916 if (FinalOutput) {
917 unsigned NumOutputs = 0;
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000918 for (ActionList::const_iterator it = C.getActions().begin(),
919 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000920 if ((*it)->getType() != types::TY_Nothing)
921 ++NumOutputs;
922
923 if (NumOutputs > 1) {
924 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
925 FinalOutput = 0;
926 }
927 }
928
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000929 for (ActionList::const_iterator it = C.getActions().begin(),
930 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000931 Action *A = *it;
932
933 // If we are linking an image for multiple archs then the linker
934 // wants -arch_multiple and -final_output <final image
935 // name>. Unfortunately, this doesn't fit in cleanly because we
936 // have to pass this information down.
937 //
938 // FIXME: This is a hack; find a cleaner way to integrate this
939 // into the process.
940 const char *LinkingOutput = 0;
Daniel Dunbar55e34e32009-03-26 16:12:09 +0000941 if (isa<LipoJobAction>(A)) {
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000942 if (FinalOutput)
943 LinkingOutput = FinalOutput->getValue(C.getArgs());
944 else
945 LinkingOutput = DefaultImageName.c_str();
946 }
947
948 InputInfo II;
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000949 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000950 /*CanAcceptPipe*/ true,
951 /*AtTopLevel*/ true,
952 /*LinkingOutput*/ LinkingOutput,
953 II);
954 }
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000955
Daniel Dunbar025de202009-04-03 22:09:23 +0000956 // If the user passed -Qunused-arguments or there were errors, don't
957 // warn about any unused arguments.
Daniel Dunbar1cfd8912009-04-07 19:04:18 +0000958 if (Diags.getNumErrors() ||
959 C.getArgs().hasArg(options::OPT_Qunused_arguments))
Daniel Dunbar46c70822009-03-18 18:03:46 +0000960 return;
961
Daniel Dunbar1e6f9ff2009-03-29 22:24:54 +0000962 // Claim -### here.
963 (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
964
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000965 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
966 it != ie; ++it) {
967 Arg *A = *it;
Daniel Dunbar46c70822009-03-18 18:03:46 +0000968
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000969 // FIXME: It would be nice to be able to send the argument to the
970 // Diagnostic, so that extra values, position, and so on could be
971 // printed.
Daniel Dunbar6c6e3f72009-04-04 00:52:26 +0000972 if (!A->isClaimed()) {
Daniel Dunbar1cfd8912009-04-07 19:04:18 +0000973 if (A->getOption().hasNoArgumentUnused())
974 continue;
975
Daniel Dunbar6c6e3f72009-04-04 00:52:26 +0000976 // Suppress the warning automatically if this is just a flag,
977 // and it is an instance of an argument we already claimed.
978 const Option &Opt = A->getOption();
979 if (isa<FlagOption>(Opt)) {
980 bool DuplicateClaimed = false;
981
982 // FIXME: Use iterator.
983 for (ArgList::const_iterator it = C.getArgs().begin(),
984 ie = C.getArgs().end(); it != ie; ++it) {
985 if ((*it)->isClaimed() && (*it)->getOption().matches(Opt.getId())) {
986 DuplicateClaimed = true;
987 break;
988 }
989 }
990
991 if (DuplicateClaimed)
992 continue;
993 }
994
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000995 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar73225932009-03-20 06:14:23 +0000996 << A->getAsString(C.getArgs());
Daniel Dunbar6c6e3f72009-04-04 00:52:26 +0000997 }
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000998 }
Daniel Dunbar47d762e2009-03-13 22:12:33 +0000999}
1000
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001001void Driver::BuildJobsForAction(Compilation &C,
1002 const Action *A,
1003 const ToolChain *TC,
1004 bool CanAcceptPipe,
1005 bool AtTopLevel,
1006 const char *LinkingOutput,
1007 InputInfo &Result) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +00001008 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action");
Daniel Dunbarbac6b642009-03-18 23:18:19 +00001009
1010 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
1011 // FIXME: Pipes are forcibly disabled until we support executing
1012 // them.
1013 if (!CCCPrintBindings)
1014 UsePipes = false;
1015
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001016 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbarc3be67b2009-03-19 07:29:38 +00001017 // FIXME: It would be nice to not claim this here; maybe the old
1018 // scheme of just using Args was better?
1019 const Arg &Input = IA->getInputArg();
1020 Input.claim();
1021 if (isa<PositionalArg>(Input)) {
1022 const char *Name = Input.getValue(C.getArgs());
1023 Result = InputInfo(Name, A->getType(), Name);
1024 } else
1025 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001026 return;
1027 }
1028
1029 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
1030 const char *ArchName = BAA->getArchName();
Daniel Dunbar08303652009-05-22 02:53:45 +00001031 std::string Arch;
1032 if (!ArchName) {
1033 Arch = C.getDefaultToolChain().getArchName();
1034 ArchName = Arch.c_str();
1035 }
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001036 BuildJobsForAction(C,
1037 *BAA->begin(),
1038 Host->getToolChain(C.getArgs(), ArchName),
1039 CanAcceptPipe,
1040 AtTopLevel,
1041 LinkingOutput,
1042 Result);
1043 return;
1044 }
1045
1046 const JobAction *JA = cast<JobAction>(A);
1047 const Tool &T = TC->SelectTool(C, *JA);
1048
1049 // See if we should use an integrated preprocessor. We do so when we
1050 // have exactly one input, since this is the only use case we care
1051 // about (irrelevant since we don't support combine yet).
1052 bool UseIntegratedCPP = false;
1053 const ActionList *Inputs = &A->getInputs();
1054 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
1055 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
1056 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
1057 !C.getArgs().hasArg(options::OPT_save_temps) &&
1058 T.hasIntegratedCPP()) {
1059 UseIntegratedCPP = true;
1060 Inputs = &(*Inputs)[0]->getInputs();
1061 }
1062 }
1063
1064 // Only use pipes when there is exactly one input.
1065 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar00fd3792009-03-18 06:00:36 +00001066 InputInfoList InputInfos;
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001067 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
1068 it != ie; ++it) {
1069 InputInfo II;
1070 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
1071 /*AtTopLevel*/false,
1072 LinkingOutput,
1073 II);
1074 InputInfos.push_back(II);
1075 }
1076
1077 // Determine if we should output to a pipe.
1078 bool OutputToPipe = false;
1079 if (CanAcceptPipe && T.canPipeOutput()) {
1080 // Some actions default to writing to a pipe if they are the top
1081 // level phase and there was no user override.
1082 //
1083 // FIXME: Is there a better way to handle this?
1084 if (AtTopLevel) {
1085 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
1086 OutputToPipe = true;
Daniel Dunbarbac6b642009-03-18 23:18:19 +00001087 } else if (UsePipes)
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001088 OutputToPipe = true;
1089 }
1090
1091 // Figure out where to put the job (pipes).
1092 Job *Dest = &C.getJobs();
1093 if (InputInfos[0].isPipe()) {
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001094 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001095 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
1096 Dest = &InputInfos[0].getPipe();
1097 }
1098
1099 // Always use the first input as the base input.
1100 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001101
1102 // Determine the place to write output to (nothing, pipe, or
1103 // filename) and where to put the new job.
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001104 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001105 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001106 } else if (OutputToPipe) {
1107 // Append to current piped job or create a new one as appropriate.
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001108 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
1109 if (!PJ) {
1110 PJ = new PipedJob();
Daniel Dunbar8eeb5a32009-03-20 00:11:04 +00001111 // FIXME: Temporary hack so that -ccc-print-bindings work until
1112 // we have pipe support. Please remove later.
1113 if (!CCCPrintBindings)
1114 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar933ac452009-03-18 07:06:02 +00001115 Dest = PJ;
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001116 }
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001117 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001118 } else {
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001119 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
1120 A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001121 }
1122
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001123 if (CCCPrintBindings) {
Daniel Dunbar049b7242009-03-30 06:36:42 +00001124 llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"'
1125 << " - \"" << T.getName() << "\", inputs: [";
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001126 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
1127 llvm::errs() << InputInfos[i].getAsString();
1128 if (i + 1 != e)
1129 llvm::errs() << ", ";
1130 }
1131 llvm::errs() << "], output: " << Result.getAsString() << "\n";
1132 } else {
Daniel Dunbara16e4fe2009-03-25 04:13:45 +00001133 T.ConstructJob(C, *JA, *Dest, Result, InputInfos,
1134 C.getArgsForToolChain(TC), LinkingOutput);
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001135 }
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001136}
1137
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001138const char *Driver::GetNamedOutputPath(Compilation &C,
1139 const JobAction &JA,
1140 const char *BaseInput,
1141 bool AtTopLevel) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +00001142 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001143 // Output to a user requested destination?
1144 if (AtTopLevel) {
1145 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
1146 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
1147 }
1148
1149 // Output to a temporary file?
1150 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001151 std::string TmpName =
1152 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
1153 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001154 }
1155
1156 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbarfb5e3332009-03-18 02:00:31 +00001157 std::string BaseName(BasePath.getLast());
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001158
1159 // Determine what the derived output name should be.
1160 const char *NamedOutput;
1161 if (JA.getType() == types::TY_Image) {
1162 NamedOutput = DefaultImageName.c_str();
1163 } else {
1164 const char *Suffix = types::getTypeTempSuffix(JA.getType());
1165 assert(Suffix && "All types used for output should have a suffix.");
1166
1167 std::string::size_type End = std::string::npos;
1168 if (!types::appendSuffixForType(JA.getType()))
1169 End = BaseName.rfind('.');
1170 std::string Suffixed(BaseName.substr(0, End));
1171 Suffixed += '.';
1172 Suffixed += Suffix;
1173 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
1174 }
1175
1176 // As an annoying special case, PCH generation doesn't strip the
1177 // pathname.
1178 if (JA.getType() == types::TY_PCH) {
1179 BasePath.eraseComponent();
Daniel Dunbar58a51262009-03-18 09:58:30 +00001180 if (BasePath.isEmpty())
1181 BasePath = NamedOutput;
1182 else
1183 BasePath.appendComponent(NamedOutput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001184 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
1185 } else {
1186 return C.addResultFile(NamedOutput);
1187 }
1188}
1189
Daniel Dunbare1cef7d2009-03-16 05:25:36 +00001190llvm::sys::Path Driver::GetFilePath(const char *Name,
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +00001191 const ToolChain &TC) const {
Daniel Dunbarcc7600c2009-03-18 20:26:19 +00001192 const ToolChain::path_list &List = TC.getFilePaths();
1193 for (ToolChain::path_list::const_iterator
1194 it = List.begin(), ie = List.end(); it != ie; ++it) {
1195 llvm::sys::Path P(*it);
1196 P.appendComponent(Name);
1197 if (P.exists())
1198 return P;
1199 }
1200
Daniel Dunbarcc006892009-03-13 00:51:18 +00001201 return llvm::sys::Path(Name);
1202}
1203
Daniel Dunbare1cef7d2009-03-16 05:25:36 +00001204llvm::sys::Path Driver::GetProgramPath(const char *Name,
Mike Stump32b1bbe2009-03-27 00:40:20 +00001205 const ToolChain &TC,
1206 bool WantFile) const {
Daniel Dunbarcc7600c2009-03-18 20:26:19 +00001207 const ToolChain::path_list &List = TC.getProgramPaths();
1208 for (ToolChain::path_list::const_iterator
1209 it = List.begin(), ie = List.end(); it != ie; ++it) {
1210 llvm::sys::Path P(*it);
1211 P.appendComponent(Name);
Mike Stump32b1bbe2009-03-27 00:40:20 +00001212 if (WantFile ? P.exists() : P.canExecute())
Daniel Dunbarcc7600c2009-03-18 20:26:19 +00001213 return P;
1214 }
1215
Daniel Dunbar49a35de2009-03-23 16:15:50 +00001216 // If all else failed, search the path.
1217 llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
Daniel Dunbarcb84b9a2009-03-18 21:34:08 +00001218 if (!P.empty())
1219 return P;
1220
Daniel Dunbarcc006892009-03-13 00:51:18 +00001221 return llvm::sys::Path(Name);
1222}
1223
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001224std::string Driver::GetTemporaryPath(const char *Suffix) const {
1225 // FIXME: This is lame; sys::Path should provide this function (in
1226 // particular, it should know how to find the temporary files dir).
1227 std::string Error;
Daniel Dunbar02e396a2009-04-20 20:28:21 +00001228 const char *TmpDir = ::getenv("TMPDIR");
1229 if (!TmpDir)
1230 TmpDir = ::getenv("TEMP");
1231 if (!TmpDir)
Daniel Dunbara77aa002009-04-21 00:25:10 +00001232 TmpDir = ::getenv("TMP");
1233 if (!TmpDir)
Daniel Dunbar02e396a2009-04-20 20:28:21 +00001234 TmpDir = "/tmp";
1235 llvm::sys::Path P(TmpDir);
Daniel Dunbar121f2512009-04-20 17:32:49 +00001236 P.appendComponent("cc");
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001237 if (P.makeUnique(false, &Error)) {
1238 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
1239 return "";
1240 }
1241
Daniel Dunbar9083abe2009-03-18 23:08:52 +00001242 // FIXME: Grumble, makeUnique sometimes leaves the file around!?
1243 // PR3837.
1244 P.eraseFromDisk(false, 0);
1245
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001246 P.appendSuffix(Suffix);
Chris Lattner31bc3042009-08-23 22:45:33 +00001247 return P.str();
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001248}
1249
Daniel Dunbar08303652009-05-22 02:53:45 +00001250const HostInfo *Driver::GetHostInfo(const char *TripleStr) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +00001251 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbar08303652009-05-22 02:53:45 +00001252 llvm::Triple Triple(TripleStr);
Daniel Dunbard25acaa2009-03-10 23:41:59 +00001253
Daniel Dunbar08303652009-05-22 02:53:45 +00001254 switch (Triple.getOS()) {
Edward O'Callaghanb68ac562009-08-22 01:06:46 +00001255 case llvm::Triple::AuroraUX:
1256 return createAuroraUXHostInfo(*this, Triple);
Daniel Dunbar08303652009-05-22 02:53:45 +00001257 case llvm::Triple::Darwin:
1258 return createDarwinHostInfo(*this, Triple);
1259 case llvm::Triple::DragonFly:
1260 return createDragonFlyHostInfo(*this, Triple);
Daniel Dunbar64c77a12009-06-29 20:52:51 +00001261 case llvm::Triple::OpenBSD:
1262 return createOpenBSDHostInfo(*this, Triple);
Daniel Dunbar08303652009-05-22 02:53:45 +00001263 case llvm::Triple::FreeBSD:
1264 return createFreeBSDHostInfo(*this, Triple);
Eli Friedman8cac4352009-05-26 07:52:18 +00001265 case llvm::Triple::Linux:
1266 return createLinuxHostInfo(*this, Triple);
Daniel Dunbar08303652009-05-22 02:53:45 +00001267 default:
1268 return createUnknownHostInfo(*this, Triple);
1269 }
Daniel Dunbard25acaa2009-03-10 23:41:59 +00001270}
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001271
1272bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
Daniel Dunbarb7daa2d2009-04-01 20:33:11 +00001273 const std::string &ArchNameStr) const {
1274 // FIXME: Remove this hack.
1275 const char *ArchName = ArchNameStr.c_str();
1276 if (ArchNameStr == "powerpc")
1277 ArchName = "ppc";
1278 else if (ArchNameStr == "powerpc64")
1279 ArchName = "ppc64";
1280
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001281 // Check if user requested no clang, or clang doesn't understand
1282 // this type (we only handle single inputs for now).
Daniel Dunbarec438002009-09-01 16:57:46 +00001283 if (!CCCUseClang || JA.size() != 1 ||
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001284 !types::isAcceptedByClang((*JA.begin())->getType()))
1285 return false;
1286
Daniel Dunbar0ea85f72009-03-24 19:02:31 +00001287 // Otherwise make sure this is an action clang understands.
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001288 if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001289 if (!CCCUseClangCPP) {
1290 Diag(clang::diag::warn_drv_not_using_clang_cpp);
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001291 return false;
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001292 }
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001293 } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
1294 return false;
1295
Daniel Dunbar0ea85f72009-03-24 19:02:31 +00001296 // Use clang for C++?
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001297 if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
1298 Diag(clang::diag::warn_drv_not_using_clang_cxx);
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001299 return false;
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001300 }
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001301
Daniel Dunbarec438002009-09-01 16:57:46 +00001302 // Always use clang for precompiling and AST generation, regardless of
1303 // archs.
1304 if (isa<PrecompileJobAction>(JA) || JA.getType() == types::TY_AST)
Daniel Dunbar5a34ca82009-04-16 23:10:13 +00001305 return true;
1306
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001307 // Finally, don't use clang if this isn't one of the user specified
1308 // archs to build.
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001309 if (!CCCClangArchs.empty() && !CCCClangArchs.count(ArchName)) {
1310 Diag(clang::diag::warn_drv_not_using_clang_arch) << ArchName;
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001311 return false;
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001312 }
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001313
1314 return true;
1315}
Daniel Dunbar9cdd4d42009-03-26 15:58:36 +00001316
1317/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and
1318/// return the grouped values as integers. Numbers which are not
1319/// provided are set to 0.
1320///
1321/// \return True if the entire string was parsed (9.2), or all groups
1322/// were parsed (10.3.5extrastuff).
1323bool Driver::GetReleaseVersion(const char *Str, unsigned &Major,
1324 unsigned &Minor, unsigned &Micro,
1325 bool &HadExtra) {
1326 HadExtra = false;
1327
1328 Major = Minor = Micro = 0;
1329 if (*Str == '\0')
1330 return true;
1331
1332 char *End;
1333 Major = (unsigned) strtol(Str, &End, 10);
1334 if (*Str != '\0' && *End == '\0')
1335 return true;
1336 if (*End != '.')
1337 return false;
1338
1339 Str = End+1;
1340 Minor = (unsigned) strtol(Str, &End, 10);
1341 if (*Str != '\0' && *End == '\0')
1342 return true;
1343 if (*End != '.')
1344 return false;
1345
1346 Str = End+1;
1347 Micro = (unsigned) strtol(Str, &End, 10);
1348 if (*Str != '\0' && *End == '\0')
1349 return true;
1350 if (Str == End)
1351 return false;
1352 HadExtra = true;
1353 return true;
1354}