blob: ab948552d92361ed168196dd26726432834a3a99 [file] [log] [blame]
Daniel Dunbar3ede8d02009-03-02 19:59:07 +00001//===--- Driver.cpp - Clang GCC Compatible Driver -----------------------*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000010#include "clang/Driver/Driver.h"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000011
Daniel Dunbar53ec5522009-03-12 07:58:46 +000012#include "clang/Driver/Action.h"
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
15#include "clang/Driver/Compilation.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000017#include "clang/Driver/HostInfo.h"
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000018#include "clang/Driver/Job.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000019#include "clang/Driver/Option.h"
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000020#include "clang/Driver/Options.h"
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000021#include "clang/Driver/Tool.h"
22#include "clang/Driver/ToolChain.h"
Daniel Dunbar53ec5522009-03-12 07:58:46 +000023#include "clang/Driver/Types.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000024
Douglas Gregorab41e632009-04-27 22:23:34 +000025#include "clang/Basic/Version.h"
26
Daniel Dunbar13689542009-03-13 20:33:35 +000027#include "llvm/ADT/StringSet.h"
Daniel Dunbar8f25c792009-03-18 01:38:48 +000028#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000029#include "llvm/Support/raw_ostream.h"
Daniel Dunbar53ec5522009-03-12 07:58:46 +000030#include "llvm/System/Path.h"
Daniel Dunbar632f50e2009-03-18 21:34:08 +000031#include "llvm/System/Program.h"
Daniel Dunbarba102132009-03-13 12:19:02 +000032
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000033#include "InputInfo.h"
34
Daniel Dunbarba102132009-03-13 12:19:02 +000035#include <map>
36
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000037using namespace clang::driver;
Chris Lattner92b36992009-03-26 05:56:24 +000038using namespace clang;
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000039
Daniel Dunbar4a124082009-08-23 18:42:54 +000040// Used to set values for "production" clang, for releases.
Daniel Dunbar8bde5052009-08-23 19:41:53 +000041// #define USE_PRODUCTION_CLANG
Daniel Dunbar4a124082009-08-23 18:42:54 +000042
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000043Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000044 const char *_DefaultHostTriple,
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000045 const char *_DefaultImageName,
Daniel Dunbarf44c5852009-09-22 22:31:13 +000046 bool IsProduction, Diagnostic &_Diags)
Daniel Dunbara8231832009-09-08 23:36:43 +000047 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000048 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000049 DefaultImageName(_DefaultImageName),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000050 Host(0),
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +000051 CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false),
Daniel Dunbar4a124082009-08-23 18:42:54 +000052 CCCGenericGCCName("gcc"), CCCUseClang(true),
Daniel Dunbarf44c5852009-09-22 22:31:13 +000053 CCCUseClangCXX(true), CCCUseClangCPP(true), CCCUsePCH(true),
Mike Stump1eb44332009-09-09 15:08:12 +000054 SuppressMissingInputWarning(false) {
Daniel Dunbarf44c5852009-09-22 22:31:13 +000055 if (IsProduction) {
56 // In a "production" build, only use clang on architectures we expect to
57 // work, and don't use clang C++.
58 //
59 // During development its more convenient to always have the driver use
60 // clang, but we don't want users to be confused when things don't work, or
61 // to file bugs for things we don't support.
62 CCCClangArchs.insert(llvm::Triple::x86);
63 CCCClangArchs.insert(llvm::Triple::x86_64);
64 CCCClangArchs.insert(llvm::Triple::arm);
65
66 CCCUseClangCXX = false;
67 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000068}
69
70Driver::~Driver() {
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000071 delete Opts;
Daniel Dunbar7e4534d2009-03-18 01:09:40 +000072 delete Host;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000073}
74
Daniel Dunbara8231832009-09-08 23:36:43 +000075InputArgList *Driver::ParseArgStrings(const char **ArgBegin,
Daniel Dunbarf3cad362009-03-25 04:13:45 +000076 const char **ArgEnd) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +000077 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbarf3cad362009-03-25 04:13:45 +000078 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
Daniel Dunbara8231832009-09-08 23:36:43 +000079
Daniel Dunbarad2a9af2009-03-13 11:38:42 +000080 // FIXME: Handle '@' args (or at least error on them).
81
Daniel Dunbar06482622009-03-05 06:38:47 +000082 unsigned Index = 0, End = ArgEnd - ArgBegin;
83 while (Index < End) {
Daniel Dunbara8231832009-09-08 23:36:43 +000084 // gcc's handling of empty arguments doesn't make sense, but this is not a
85 // common use case. :)
Daniel Dunbar41393402009-03-13 01:01:44 +000086 //
Daniel Dunbara8231832009-09-08 23:36:43 +000087 // We just ignore them here (note that other things may still take them as
88 // arguments).
Daniel Dunbar41393402009-03-13 01:01:44 +000089 if (Args->getArgString(Index)[0] == '\0') {
90 ++Index;
91 continue;
92 }
93
Daniel Dunbar06482622009-03-05 06:38:47 +000094 unsigned Prev = Index;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000095 Arg *A = getOpts().ParseOneArg(*Args, Index);
96 assert(Index > Prev && "Parser failed to consume argument.");
Daniel Dunbar53ec5522009-03-12 07:58:46 +000097
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000098 // Check for missing argument error.
99 if (!A) {
100 assert(Index >= End && "Unexpected parser error.");
101 Diag(clang::diag::err_drv_missing_argument)
102 << Args->getArgString(Prev)
103 << (Index - Prev - 1);
104 break;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000105 }
Daniel Dunbar06482622009-03-05 06:38:47 +0000106
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000107 if (A->getOption().isUnsupported()) {
108 Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
109 continue;
110 }
111 Args->append(A);
Daniel Dunbar06482622009-03-05 06:38:47 +0000112 }
113
114 return Args;
115}
116
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000117Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000118 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
119
Daniel Dunbara8231832009-09-08 23:36:43 +0000120 // FIXME: Handle environment options which effect driver behavior, somewhere
121 // (client?). GCC_EXEC_PREFIX, COMPILER_PATH, LIBRARY_PATH, LPATH,
122 // CC_PRINT_OPTIONS.
Daniel Dunbarcb881672009-03-13 00:51:18 +0000123
124 // FIXME: What are we going to do with -V and -b?
125
Daniel Dunbara8231832009-09-08 23:36:43 +0000126 // FIXME: This stuff needs to go into the Compilation, not the driver.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000127 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar06482622009-03-05 06:38:47 +0000128
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000129 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000130 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000131
Daniel Dunbara8231832009-09-08 23:36:43 +0000132 // Read -ccc args.
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000133 //
Daniel Dunbara8231832009-09-08 23:36:43 +0000134 // FIXME: We need to figure out where this behavior should live. Most of it
135 // should be outside in the client; the parts that aren't should have proper
136 // options, either by introducing new ones or by overloading gcc ones like -V
137 // or -b.
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000138 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
139 const char *Opt = *Start + 5;
Daniel Dunbara8231832009-09-08 23:36:43 +0000140
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000141 if (!strcmp(Opt, "print-options")) {
142 CCCPrintOptions = true;
143 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000144 CCCPrintActions = true;
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000145 } else if (!strcmp(Opt, "print-bindings")) {
146 CCCPrintBindings = true;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000147 } else if (!strcmp(Opt, "cxx")) {
148 CCCIsCXX = true;
149 } else if (!strcmp(Opt, "echo")) {
150 CCCEcho = true;
Daniel Dunbara8231832009-09-08 23:36:43 +0000151
Daniel Dunbar78d8a082009-04-01 23:34:41 +0000152 } else if (!strcmp(Opt, "gcc-name")) {
153 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
154 CCCGenericGCCName = *++Start;
155
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000156 } else if (!strcmp(Opt, "clang-cxx")) {
157 CCCUseClangCXX = true;
Daniel Dunbardfaf4b32009-07-23 17:48:59 +0000158 } else if (!strcmp(Opt, "no-clang-cxx")) {
159 CCCUseClangCXX = false;
Douglas Gregordf91ef32009-04-18 00:34:01 +0000160 } else if (!strcmp(Opt, "pch-is-pch")) {
161 CCCUsePCH = true;
162 } else if (!strcmp(Opt, "pch-is-pth")) {
163 CCCUsePCH = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000164 } else if (!strcmp(Opt, "no-clang")) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000165 CCCUseClang = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000166 } else if (!strcmp(Opt, "no-clang-cpp")) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000167 CCCUseClangCPP = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000168 } else if (!strcmp(Opt, "clang-archs")) {
169 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbara6046be2009-09-08 23:36:55 +0000170 llvm::StringRef Cur = *++Start;
Daniel Dunbara8231832009-09-08 23:36:43 +0000171
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000172 CCCClangArchs.clear();
Daniel Dunbara6046be2009-09-08 23:36:55 +0000173 while (!Cur.empty()) {
174 std::pair<llvm::StringRef, llvm::StringRef> Split = Cur.split(',');
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000175
Daniel Dunbara6046be2009-09-08 23:36:55 +0000176 if (!Split.first.empty()) {
177 llvm::Triple::ArchType Arch =
178 llvm::Triple(Split.first, "", "").getArch();
179
180 if (Arch == llvm::Triple::UnknownArch) {
181 // FIXME: Error handling.
182 llvm::errs() << "invalid arch name: " << Split.first << "\n";
183 exit(1);
184 }
185
186 CCCClangArchs.insert(Arch);
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000187 }
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000188
Daniel Dunbara6046be2009-09-08 23:36:55 +0000189 Cur = Split.second;
190 }
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000191 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000192 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000193 HostTriple = *++Start;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000194
Daniel Dunbarda641412009-09-04 18:35:03 +0000195 } else if (!strcmp(Opt, "install-dir")) {
196 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
197 Dir = *++Start;
198
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000199 } else {
200 // FIXME: Error handling.
201 llvm::errs() << "invalid option: " << *Start << "\n";
202 exit(1);
203 }
204 }
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000205
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000206 InputArgList *Args = ParseArgStrings(Start, End);
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000207
Daniel Dunbare5049522009-03-17 20:45:45 +0000208 Host = GetHostInfo(HostTriple);
Daniel Dunbarcb881672009-03-13 00:51:18 +0000209
Daniel Dunbar586dc232009-03-16 06:42:30 +0000210 // The compilation takes ownership of Args.
Daniel Dunbard7502d02009-09-08 23:37:19 +0000211 Compilation *C = new Compilation(*this, *Host->CreateToolChain(*Args), Args);
Daniel Dunbar21549232009-03-18 02:55:38 +0000212
213 // FIXME: This behavior shouldn't be here.
214 if (CCCPrintOptions) {
215 PrintOptions(C->getArgs());
216 return C;
217 }
218
219 if (!HandleImmediateArgs(*C))
220 return C;
221
Daniel Dunbara8231832009-09-08 23:36:43 +0000222 // Construct the list of abstract actions to perform for this compilation. We
223 // avoid passing a Compilation here simply to enforce the abstraction that
224 // pipelining is not host or toolchain dependent (other than the driver driver
225 // test).
Daniel Dunbar21549232009-03-18 02:55:38 +0000226 if (Host->useDriverDriver())
227 BuildUniversalActions(C->getArgs(), C->getActions());
228 else
229 BuildActions(C->getArgs(), C->getActions());
230
231 if (CCCPrintActions) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000232 PrintActions(*C);
Daniel Dunbar21549232009-03-18 02:55:38 +0000233 return C;
234 }
235
236 BuildJobs(*C);
Daniel Dunbar8d2554a2009-03-15 01:38:15 +0000237
238 return C;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000239}
240
Daniel Dunbarc88a88f2009-07-01 20:03:04 +0000241int Driver::ExecuteCompilation(const Compilation &C) const {
242 // Just print if -### was present.
243 if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
244 C.PrintJob(llvm::errs(), C.getJobs(), "\n", true);
245 return 0;
246 }
247
248 // If there were errors building the compilation, quit now.
249 if (getDiags().getNumErrors())
250 return 1;
251
252 const Command *FailingCommand = 0;
253 int Res = C.ExecuteJob(C.getJobs(), FailingCommand);
Daniel Dunbara8231832009-09-08 23:36:43 +0000254
Daniel Dunbarc88a88f2009-07-01 20:03:04 +0000255 // Remove temp files.
256 C.CleanupFileList(C.getTempFiles());
257
258 // If the compilation failed, remove result files as well.
259 if (Res != 0 && !C.getArgs().hasArg(options::OPT_save_temps))
260 C.CleanupFileList(C.getResultFiles(), true);
261
262 // Print extra information about abnormal failures, if possible.
263 if (Res) {
264 // This is ad-hoc, but we don't want to be excessively noisy. If the result
265 // status was 1, assume the command failed normally. In particular, if it
266 // was the compiler then assume it gave a reasonable error code. Failures in
267 // other tools are less common, and they generally have worse diagnostics,
268 // so always print the diagnostic there.
269 const Action &Source = FailingCommand->getSource();
270 bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) ||
271 isa<PrecompileJobAction>(Source) ||
272 isa<AnalyzeJobAction>(Source) ||
273 isa<CompileJobAction>(Source));
274
275 if (!IsFriendlyTool || Res != 1) {
276 // FIXME: See FIXME above regarding result code interpretation.
277 if (Res < 0)
Daniel Dunbara8231832009-09-08 23:36:43 +0000278 Diag(clang::diag::err_drv_command_signalled)
Daniel Dunbarc88a88f2009-07-01 20:03:04 +0000279 << Source.getClassName() << -Res;
280 else
Daniel Dunbara8231832009-09-08 23:36:43 +0000281 Diag(clang::diag::err_drv_command_failed)
Daniel Dunbarc88a88f2009-07-01 20:03:04 +0000282 << Source.getClassName() << Res;
283 }
284 }
285
286 return Res;
287}
288
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000289void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar06482622009-03-05 06:38:47 +0000290 unsigned i = 0;
Daniel Dunbara8231832009-09-08 23:36:43 +0000291 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar06482622009-03-05 06:38:47 +0000292 it != ie; ++it, ++i) {
293 Arg *A = *it;
294 llvm::errs() << "Option " << i << " - "
295 << "Name: \"" << A->getOption().getName() << "\", "
296 << "Values: {";
297 for (unsigned j = 0; j < A->getNumValues(); ++j) {
298 if (j)
299 llvm::errs() << ", ";
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000300 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar06482622009-03-05 06:38:47 +0000301 }
302 llvm::errs() << "}\n";
Daniel Dunbar06482622009-03-05 06:38:47 +0000303 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000304}
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000305
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000306static std::string getOptionHelpName(const OptTable &Opts, options::ID Id) {
307 std::string Name = Opts.getOptionName(Id);
Daniel Dunbara8231832009-09-08 23:36:43 +0000308
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000309 // Add metavar, if used.
310 switch (Opts.getOptionKind(Id)) {
Daniel Dunbara8231832009-09-08 23:36:43 +0000311 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000312 assert(0 && "Invalid option with help text.");
313
314 case Option::MultiArgClass: case Option::JoinedAndSeparateClass:
315 assert(0 && "Cannot print metavar for this kind of option.");
316
317 case Option::FlagClass:
318 break;
319
320 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
321 Name += ' ';
322 // FALLTHROUGH
323 case Option::JoinedClass: case Option::CommaJoinedClass:
324 Name += Opts.getOptionMetaVar(Id);
325 break;
326 }
327
328 return Name;
329}
330
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000331void Driver::PrintHelp(bool ShowHidden) const {
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000332 llvm::raw_ostream &OS = llvm::outs();
333
334 OS << "OVERVIEW: clang \"gcc-compatible\" driver\n";
335 OS << '\n';
336 OS << "USAGE: " << Name << " [options] <input files>\n";
337 OS << '\n';
338 OS << "OPTIONS:\n";
339
340 // Render help text into (option, help) pairs.
341 std::vector< std::pair<std::string, const char*> > OptionHelp;
342
343 for (unsigned i = options::OPT_INPUT, e = options::LastOption; i != e; ++i) {
344 options::ID Id = (options::ID) i;
345 if (const char *Text = getOpts().getOptionHelpText(Id))
346 OptionHelp.push_back(std::make_pair(getOptionHelpName(getOpts(), Id),
347 Text));
348 }
349
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000350 if (ShowHidden) {
351 OptionHelp.push_back(std::make_pair("\nDRIVER OPTIONS:",""));
352 OptionHelp.push_back(std::make_pair("-ccc-cxx",
353 "Act as a C++ driver"));
354 OptionHelp.push_back(std::make_pair("-ccc-gcc-name",
355 "Name for native GCC compiler"));
356 OptionHelp.push_back(std::make_pair("-ccc-clang-cxx",
Daniel Dunbar5915fbf2009-09-01 16:57:46 +0000357 "Enable the clang compiler for C++"));
358 OptionHelp.push_back(std::make_pair("-ccc-no-clang-cxx",
359 "Disable the clang compiler for C++"));
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000360 OptionHelp.push_back(std::make_pair("-ccc-no-clang",
Daniel Dunbar5915fbf2009-09-01 16:57:46 +0000361 "Disable the clang compiler"));
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000362 OptionHelp.push_back(std::make_pair("-ccc-no-clang-cpp",
Daniel Dunbar5915fbf2009-09-01 16:57:46 +0000363 "Disable the clang preprocessor"));
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000364 OptionHelp.push_back(std::make_pair("-ccc-clang-archs",
365 "Comma separate list of architectures "
366 "to use the clang compiler for"));
Douglas Gregordf91ef32009-04-18 00:34:01 +0000367 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pch",
368 "Use lazy PCH for precompiled headers"));
369 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pth",
370 "Use pretokenized headers for precompiled headers"));
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000371
372 OptionHelp.push_back(std::make_pair("\nDEBUG/DEVELOPMENT OPTIONS:",""));
373 OptionHelp.push_back(std::make_pair("-ccc-host-triple",
Daniel Dunbarda641412009-09-04 18:35:03 +0000374 "Simulate running on the given target"));
375 OptionHelp.push_back(std::make_pair("-ccc-install-dir",
376 "Simulate installation in the given directory"));
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000377 OptionHelp.push_back(std::make_pair("-ccc-print-options",
378 "Dump parsed command line arguments"));
379 OptionHelp.push_back(std::make_pair("-ccc-print-phases",
380 "Dump list of actions to perform"));
381 OptionHelp.push_back(std::make_pair("-ccc-print-bindings",
382 "Show bindings of tools to actions"));
383 OptionHelp.push_back(std::make_pair("CCC_ADD_ARGS",
384 "(ENVIRONMENT VARIABLE) Comma separated list of "
385 "arguments to prepend to the command line"));
386 }
387
Daniel Dunbara8231832009-09-08 23:36:43 +0000388 // Find the maximum option length.
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000389 unsigned OptionFieldWidth = 0;
390 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000391 // Skip titles.
392 if (!OptionHelp[i].second)
393 continue;
394
Daniel Dunbara8231832009-09-08 23:36:43 +0000395 // Limit the amount of padding we are willing to give up for alignment.
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000396 unsigned Length = OptionHelp[i].first.size();
397 if (Length <= 23)
398 OptionFieldWidth = std::max(OptionFieldWidth, Length);
399 }
400
401 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
402 const std::string &Option = OptionHelp[i].first;
403 OS << " " << Option;
404 for (int j = Option.length(), e = OptionFieldWidth; j < e; ++j)
405 OS << ' ';
406 OS << ' ' << OptionHelp[i].second << '\n';
407 }
408
409 OS.flush();
410}
411
Daniel Dunbar79300722009-07-21 20:06:58 +0000412void Driver::PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const {
Daniel Dunbara8231832009-09-08 23:36:43 +0000413 // FIXME: The following handlers should use a callback mechanism, we don't
414 // know what the client would like to do.
415 OS << "clang version " CLANG_VERSION_STRING " ("
Douglas Gregorb8d11912009-10-05 20:33:49 +0000416 << getClangSubversionPath();
417 if (unsigned Revision = getClangSubversionRevision())
418 OS << " " << Revision;
419 OS << ")" << '\n';
Daniel Dunbar70c8db12009-03-26 16:09:13 +0000420
421 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbar79300722009-07-21 20:06:58 +0000422 OS << "Target: " << TC.getTripleString() << '\n';
Daniel Dunbar3ee96ba2009-06-16 23:32:58 +0000423
424 // Print the threading model.
425 //
426 // FIXME: Implement correctly.
Daniel Dunbar79300722009-07-21 20:06:58 +0000427 OS << "Thread model: " << "posix" << '\n';
Daniel Dunbarcb881672009-03-13 00:51:18 +0000428}
429
Daniel Dunbar21549232009-03-18 02:55:38 +0000430bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbara8231832009-09-08 23:36:43 +0000431 // The order these options are handled in in gcc is all over the place, but we
432 // don't expect inconsistencies w.r.t. that to matter in practice.
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000433
Daniel Dunbare06dc212009-04-04 05:17:38 +0000434 if (C.getArgs().hasArg(options::OPT_dumpversion)) {
Douglas Gregorab41e632009-04-27 22:23:34 +0000435 llvm::outs() << CLANG_VERSION_STRING "\n";
Daniel Dunbare06dc212009-04-04 05:17:38 +0000436 return false;
437 }
438
Daniel Dunbara8231832009-09-08 23:36:43 +0000439 if (C.getArgs().hasArg(options::OPT__help) ||
Daniel Dunbarc35d71f2009-04-15 16:34:29 +0000440 C.getArgs().hasArg(options::OPT__help_hidden)) {
441 PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden));
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000442 return false;
443 }
444
Daniel Dunbar6cc73de2009-04-02 15:05:41 +0000445 if (C.getArgs().hasArg(options::OPT__version)) {
Daniel Dunbara8231832009-09-08 23:36:43 +0000446 // Follow gcc behavior and use stdout for --version and stderr for -v.
Daniel Dunbar79300722009-07-21 20:06:58 +0000447 PrintVersion(C, llvm::outs());
Daniel Dunbar6cc73de2009-04-02 15:05:41 +0000448 return false;
449 }
450
Daniel Dunbara8231832009-09-08 23:36:43 +0000451 if (C.getArgs().hasArg(options::OPT_v) ||
Daniel Dunbar21549232009-03-18 02:55:38 +0000452 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbar79300722009-07-21 20:06:58 +0000453 PrintVersion(C, llvm::errs());
Daniel Dunbarcb881672009-03-13 00:51:18 +0000454 SuppressMissingInputWarning = true;
455 }
456
Daniel Dunbar21549232009-03-18 02:55:38 +0000457 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbarca3459e2009-03-20 04:37:21 +0000458 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
459 llvm::outs() << "programs: =";
460 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
461 ie = TC.getProgramPaths().end(); it != ie; ++it) {
462 if (it != TC.getProgramPaths().begin())
463 llvm::outs() << ':';
464 llvm::outs() << *it;
465 }
466 llvm::outs() << "\n";
467 llvm::outs() << "libraries: =";
Daniel Dunbara8231832009-09-08 23:36:43 +0000468 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
Daniel Dunbarca3459e2009-03-20 04:37:21 +0000469 ie = TC.getFilePaths().end(); it != ie; ++it) {
470 if (it != TC.getFilePaths().begin())
471 llvm::outs() << ':';
472 llvm::outs() << *it;
473 }
474 llvm::outs() << "\n";
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000475 return false;
Daniel Dunbarca3459e2009-03-20 04:37:21 +0000476 }
477
Daniel Dunbara8231832009-09-08 23:36:43 +0000478 // FIXME: The following handlers should use a callback mechanism, we don't
479 // know what the client would like to do.
Daniel Dunbar21549232009-03-18 02:55:38 +0000480 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
Daniel Dunbar5ed34f42009-09-09 22:33:00 +0000481 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC) << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000482 return false;
483 }
484
Daniel Dunbar21549232009-03-18 02:55:38 +0000485 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
Daniel Dunbar5ed34f42009-09-09 22:33:00 +0000486 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC) << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000487 return false;
488 }
489
Daniel Dunbar21549232009-03-18 02:55:38 +0000490 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbar5ed34f42009-09-09 22:33:00 +0000491 llvm::outs() << GetFilePath("libgcc.a", TC) << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000492 return false;
493 }
494
Daniel Dunbar12cfe032009-06-16 23:25:22 +0000495 if (C.getArgs().hasArg(options::OPT_print_multi_lib)) {
496 // FIXME: We need tool chain support for this.
497 llvm::outs() << ".;\n";
498
499 switch (C.getDefaultToolChain().getTriple().getArch()) {
500 default:
501 break;
Daniel Dunbara8231832009-09-08 23:36:43 +0000502
Daniel Dunbar12cfe032009-06-16 23:25:22 +0000503 case llvm::Triple::x86_64:
504 llvm::outs() << "x86_64;@m64" << "\n";
505 break;
506
507 case llvm::Triple::ppc64:
508 llvm::outs() << "ppc64;@m64" << "\n";
509 break;
510 }
511 return false;
512 }
513
514 // FIXME: What is the difference between print-multi-directory and
515 // print-multi-os-directory?
516 if (C.getArgs().hasArg(options::OPT_print_multi_directory) ||
517 C.getArgs().hasArg(options::OPT_print_multi_os_directory)) {
518 switch (C.getDefaultToolChain().getTriple().getArch()) {
519 default:
520 case llvm::Triple::x86:
521 case llvm::Triple::ppc:
522 llvm::outs() << "." << "\n";
523 break;
Daniel Dunbara8231832009-09-08 23:36:43 +0000524
Daniel Dunbar12cfe032009-06-16 23:25:22 +0000525 case llvm::Triple::x86_64:
526 llvm::outs() << "x86_64" << "\n";
527 break;
528
529 case llvm::Triple::ppc64:
530 llvm::outs() << "ppc64" << "\n";
531 break;
532 }
533 return false;
534 }
535
Daniel Dunbarcb881672009-03-13 00:51:18 +0000536 return true;
537}
538
Daniel Dunbara8231832009-09-08 23:36:43 +0000539static unsigned PrintActions1(const Compilation &C, Action *A,
Daniel Dunbarba102132009-03-13 12:19:02 +0000540 std::map<Action*, unsigned> &Ids) {
541 if (Ids.count(A))
542 return Ids[A];
Daniel Dunbara8231832009-09-08 23:36:43 +0000543
Daniel Dunbarba102132009-03-13 12:19:02 +0000544 std::string str;
545 llvm::raw_string_ostream os(str);
Daniel Dunbara8231832009-09-08 23:36:43 +0000546
Daniel Dunbarba102132009-03-13 12:19:02 +0000547 os << Action::getClassName(A->getKind()) << ", ";
Daniel Dunbara8231832009-09-08 23:36:43 +0000548 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000549 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbarba102132009-03-13 12:19:02 +0000550 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbara8231832009-09-08 23:36:43 +0000551 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000552 C.getDefaultToolChain().getArchName()) << '"'
553 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbarba102132009-03-13 12:19:02 +0000554 } else {
555 os << "{";
556 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000557 os << PrintActions1(C, *it, Ids);
Daniel Dunbarba102132009-03-13 12:19:02 +0000558 ++it;
559 if (it != ie)
560 os << ", ";
561 }
562 os << "}";
563 }
564
565 unsigned Id = Ids.size();
566 Ids[A] = Id;
Daniel Dunbara8231832009-09-08 23:36:43 +0000567 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbarba102132009-03-13 12:19:02 +0000568 << types::getTypeName(A->getType()) << "\n";
569
570 return Id;
571}
572
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000573void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbarba102132009-03-13 12:19:02 +0000574 std::map<Action*, unsigned> Ids;
Daniel Dunbara8231832009-09-08 23:36:43 +0000575 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000576 ie = C.getActions().end(); it != ie; ++it)
577 PrintActions1(C, *it, Ids);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000578}
579
Daniel Dunbara8231832009-09-08 23:36:43 +0000580void Driver::BuildUniversalActions(const ArgList &Args,
Daniel Dunbar21549232009-03-18 02:55:38 +0000581 ActionList &Actions) const {
Daniel Dunbara8231832009-09-08 23:36:43 +0000582 llvm::PrettyStackTraceString CrashInfo("Building universal build actions");
583 // Collect the list of architectures. Duplicates are allowed, but should only
584 // be handled once (in the order seen).
Daniel Dunbar13689542009-03-13 20:33:35 +0000585 llvm::StringSet<> ArchNames;
586 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbara8231832009-09-08 23:36:43 +0000587 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000588 it != ie; ++it) {
589 Arg *A = *it;
590
591 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbar36df2902009-09-08 23:37:30 +0000592 // Validate the option here; we don't save the type here because its
593 // particular spelling may participate in other driver choices.
594 llvm::Triple::ArchType Arch =
595 llvm::Triple::getArchTypeForDarwinArchName(A->getValue(Args));
596 if (Arch == llvm::Triple::UnknownArch) {
597 Diag(clang::diag::err_drv_invalid_arch_name)
598 << A->getAsString(Args);
599 continue;
600 }
601
Daniel Dunbar75877192009-03-19 07:55:12 +0000602 A->claim();
Daniel Dunbar3f30ddf2009-09-08 23:37:02 +0000603 if (ArchNames.insert(A->getValue(Args)))
604 Archs.push_back(A->getValue(Args));
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000605 }
606 }
607
Daniel Dunbara8231832009-09-08 23:36:43 +0000608 // When there is no explicit arch for this platform, make sure we still bind
609 // the architecture (to the default) so that -Xarch_ is handled correctly.
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000610 if (!Archs.size())
611 Archs.push_back(0);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000612
Daniel Dunbara8231832009-09-08 23:36:43 +0000613 // FIXME: We killed off some others but these aren't yet detected in a
614 // functional manner. If we added information to jobs about which "auxiliary"
615 // files they wrote then we could detect the conflict these cause downstream.
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000616 if (Archs.size() > 1) {
617 // No recovery needed, the point of this is just to prevent
618 // overwriting the same files.
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000619 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
Daniel Dunbara8231832009-09-08 23:36:43 +0000620 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000621 << A->getAsString(Args);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000622 }
623
624 ActionList SingleActions;
625 BuildActions(Args, SingleActions);
626
Daniel Dunbara8231832009-09-08 23:36:43 +0000627 // Add in arch binding and lipo (if necessary) for every top level action.
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000628 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
629 Action *Act = SingleActions[i];
630
Daniel Dunbara8231832009-09-08 23:36:43 +0000631 // Make sure we can lipo this kind of output. If not (and it is an actual
632 // output) then we disallow, since we can't create an output file with the
633 // right name without overwriting it. We could remove this oddity by just
634 // changing the output names to include the arch, which would also fix
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000635 // -save-temps. Compatibility wins for now.
636
Daniel Dunbar3dbd6c52009-03-13 17:46:02 +0000637 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000638 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
639 << types::getTypeName(Act->getType());
640
641 ActionList Inputs;
Daniel Dunbar75877192009-03-19 07:55:12 +0000642 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbar13689542009-03-13 20:33:35 +0000643 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000644
Daniel Dunbara8231832009-09-08 23:36:43 +0000645 // Lipo if necessary, we do it this way because we need to set the arch flag
646 // so that -Xarch_ gets overwritten.
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000647 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
648 Actions.append(Inputs.begin(), Inputs.end());
649 else
650 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
651 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000652}
653
Daniel Dunbar21549232009-03-18 02:55:38 +0000654void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000655 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000656 // Start by constructing the list of inputs and their types.
657
Daniel Dunbara8231832009-09-08 23:36:43 +0000658 // Track the current user specified (-x) input. We also explicitly track the
659 // argument used to set the type; we only want to claim the type when we
660 // actually use it, so we warn about unused -x arguments.
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000661 types::ID InputType = types::TY_Nothing;
662 Arg *InputTypeArg = 0;
663
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000664 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
Daniel Dunbara8231832009-09-08 23:36:43 +0000665 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000666 it != ie; ++it) {
667 Arg *A = *it;
668
669 if (isa<InputOption>(A->getOption())) {
670 const char *Value = A->getValue(Args);
671 types::ID Ty = types::TY_INVALID;
672
673 // Infer the input type if necessary.
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000674 if (InputType == types::TY_Nothing) {
675 // If there was an explicit arg for this, claim it.
676 if (InputTypeArg)
677 InputTypeArg->claim();
678
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000679 // stdin must be handled specially.
680 if (memcmp(Value, "-", 2) == 0) {
Daniel Dunbara8231832009-09-08 23:36:43 +0000681 // If running with -E, treat as a C input (this changes the builtin
682 // macros, for example). This may be overridden by -ObjC below.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000683 //
Daniel Dunbara8231832009-09-08 23:36:43 +0000684 // Otherwise emit an error but still use a valid type to avoid
685 // spurious errors (e.g., no inputs).
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000686 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000687 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000688 Ty = types::TY_C;
689 } else {
Daniel Dunbara8231832009-09-08 23:36:43 +0000690 // Otherwise lookup by extension, and fallback to ObjectType if not
691 // found. We use a host hook here because Darwin at least has its own
692 // idea of what .s is.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000693 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbare33bea42009-03-20 23:39:23 +0000694 Ty = Host->lookupTypeForExtension(Ext + 1);
695
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000696 if (Ty == types::TY_INVALID)
697 Ty = types::TY_Object;
698 }
699
Daniel Dunbar683ca382009-05-18 21:47:54 +0000700 // -ObjC and -ObjC++ override the default language, but only for "source
701 // files". We just treat everything that isn't a linker input as a
702 // source file.
Daniel Dunbara8231832009-09-08 23:36:43 +0000703 //
Daniel Dunbar683ca382009-05-18 21:47:54 +0000704 // FIXME: Clean this up if we move the phase sequence into the type.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000705 if (Ty != types::TY_Object) {
706 if (Args.hasArg(options::OPT_ObjC))
707 Ty = types::TY_ObjC;
708 else if (Args.hasArg(options::OPT_ObjCXX))
709 Ty = types::TY_ObjCXX;
710 }
711 } else {
712 assert(InputTypeArg && "InputType set w/o InputTypeArg");
713 InputTypeArg->claim();
714 Ty = InputType;
715 }
716
Daniel Dunbara8231832009-09-08 23:36:43 +0000717 // Check that the file exists. It isn't clear this is worth doing, since
718 // the tool presumably does this anyway, and this just adds an extra stat
719 // to the equation, but this is gcc compatible.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000720 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000721 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000722 else
723 Inputs.push_back(std::make_pair(Ty, A));
724
725 } else if (A->getOption().isLinkerInput()) {
Daniel Dunbara8231832009-09-08 23:36:43 +0000726 // Just treat as object type, we could make a special type for this if
727 // necessary.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000728 Inputs.push_back(std::make_pair(types::TY_Object, A));
729
730 } else if (A->getOption().getId() == options::OPT_x) {
Daniel Dunbara8231832009-09-08 23:36:43 +0000731 InputTypeArg = A;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000732 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
733
734 // Follow gcc behavior and treat as linker input for invalid -x
Daniel Dunbara8231832009-09-08 23:36:43 +0000735 // options. Its not clear why we shouldn't just revert to unknown; but
736 // this isn't very important, we might as well be bug comatible.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000737 if (!InputType) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000738 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000739 InputType = types::TY_Object;
740 }
741 }
742 }
743
Daniel Dunbar8b1604e2009-03-13 00:17:48 +0000744 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000745 Diag(clang::diag::err_drv_no_input_files);
746 return;
747 }
748
Daniel Dunbara8231832009-09-08 23:36:43 +0000749 // Determine which compilation mode we are in. We look for options which
750 // affect the phase, starting with the earliest phases, and record which
751 // option we used to determine the final phase.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000752 Arg *FinalPhaseArg = 0;
753 phases::ID FinalPhase;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000754
755 // -{E,M,MM} only run the preprocessor.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000756 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
757 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
758 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
759 FinalPhase = phases::Preprocess;
Daniel Dunbara8231832009-09-08 23:36:43 +0000760
Daniel Dunbar5915fbf2009-09-01 16:57:46 +0000761 // -{fsyntax-only,-analyze,emit-ast,S} only run up to the compiler.
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000762 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
Daniel Dunbar63be57a2009-05-06 02:12:32 +0000763 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze,
764 options::OPT__analyze_auto)) ||
Daniel Dunbar5915fbf2009-09-01 16:57:46 +0000765 (FinalPhaseArg = Args.getLastArg(options::OPT_emit_ast)) ||
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000766 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
767 FinalPhase = phases::Compile;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000768
769 // -c only runs up to the assembler.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000770 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
771 FinalPhase = phases::Assemble;
Daniel Dunbara8231832009-09-08 23:36:43 +0000772
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000773 // Otherwise do everything.
774 } else
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000775 FinalPhase = phases::Link;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000776
Daniel Dunbara8231832009-09-08 23:36:43 +0000777 // Reject -Z* at the top level, these options should never have been exposed
778 // by gcc.
Daniel Dunbard7b88c22009-03-26 16:12:09 +0000779 if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000780 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000781
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000782 // Construct the actions to perform.
783 ActionList LinkerInputs;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000784 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000785 types::ID InputType = Inputs[i].first;
786 const Arg *InputArg = Inputs[i].second;
787
788 unsigned NumSteps = types::getNumCompilationPhases(InputType);
789 assert(NumSteps && "Invalid number of steps!");
790
Daniel Dunbara8231832009-09-08 23:36:43 +0000791 // If the first step comes after the final phase we are doing as part of
792 // this compilation, warn the user about it.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000793 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
794 if (InitialPhase > FinalPhase) {
Daniel Dunbar05494a72009-03-19 07:57:08 +0000795 // Claim here to avoid the more general unused warning.
796 InputArg->claim();
Daniel Dunbar634b2452009-09-17 04:13:26 +0000797
798 // Special case '-E' warning on a previously preprocessed file to make
799 // more sense.
800 if (InitialPhase == phases::Compile && FinalPhase == phases::Preprocess &&
801 getPreprocessedType(InputType) == types::TY_INVALID)
802 Diag(clang::diag::warn_drv_preprocessed_input_file_unused)
803 << InputArg->getAsString(Args)
804 << FinalPhaseArg->getOption().getName();
805 else
806 Diag(clang::diag::warn_drv_input_file_unused)
807 << InputArg->getAsString(Args)
808 << getPhaseName(InitialPhase)
809 << FinalPhaseArg->getOption().getName();
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000810 continue;
811 }
Daniel Dunbara8231832009-09-08 23:36:43 +0000812
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000813 // Build the pipeline for this file.
814 Action *Current = new InputAction(*InputArg, InputType);
815 for (unsigned i = 0; i != NumSteps; ++i) {
816 phases::ID Phase = types::getCompilationPhase(InputType, i);
817
818 // We are done if this step is past what the user requested.
819 if (Phase > FinalPhase)
820 break;
821
822 // Queue linker inputs.
823 if (Phase == phases::Link) {
824 assert(i + 1 == NumSteps && "linking must be final compilation step.");
825 LinkerInputs.push_back(Current);
826 Current = 0;
827 break;
828 }
829
Daniel Dunbara8231832009-09-08 23:36:43 +0000830 // Some types skip the assembler phase (e.g., llvm-bc), but we can't
831 // encode this in the steps because the intermediate type depends on
832 // arguments. Just special case here.
Daniel Dunbar337a6272009-03-24 20:17:30 +0000833 if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
834 continue;
835
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000836 // Otherwise construct the appropriate action.
837 Current = ConstructPhaseAction(Args, Phase, Current);
838 if (Current->getType() == types::TY_Nothing)
839 break;
840 }
841
842 // If we ended with something, add to the output list.
843 if (Current)
844 Actions.push_back(Current);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000845 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000846
847 // Add a link action if necessary.
848 if (!LinkerInputs.empty())
849 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
850}
851
852Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
853 Action *Input) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000854 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000855 // Build the appropriate action.
856 switch (Phase) {
857 case phases::Link: assert(0 && "link action invalid here.");
858 case phases::Preprocess: {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +0000859 types::ID OutputTy;
860 // -{M, MM} alter the output type.
861 if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) {
862 OutputTy = types::TY_Dependencies;
863 } else {
864 OutputTy = types::getPreprocessedType(Input->getType());
865 assert(OutputTy != types::TY_INVALID &&
866 "Cannot preprocess this input type!");
867 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000868 return new PreprocessJobAction(Input, OutputTy);
869 }
870 case phases::Precompile:
Daniel Dunbara8231832009-09-08 23:36:43 +0000871 return new PrecompileJobAction(Input, types::TY_PCH);
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000872 case phases::Compile: {
873 if (Args.hasArg(options::OPT_fsyntax_only)) {
874 return new CompileJobAction(Input, types::TY_Nothing);
Daniel Dunbar63be57a2009-05-06 02:12:32 +0000875 } else if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000876 return new AnalyzeJobAction(Input, types::TY_Plist);
Daniel Dunbar5915fbf2009-09-01 16:57:46 +0000877 } else if (Args.hasArg(options::OPT_emit_ast)) {
878 return new CompileJobAction(Input, types::TY_AST);
Daniel Dunbar337a6272009-03-24 20:17:30 +0000879 } else if (Args.hasArg(options::OPT_emit_llvm) ||
880 Args.hasArg(options::OPT_flto) ||
881 Args.hasArg(options::OPT_O4)) {
Daniel Dunbara8231832009-09-08 23:36:43 +0000882 types::ID Output =
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000883 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
884 return new CompileJobAction(Input, Output);
885 } else {
886 return new CompileJobAction(Input, types::TY_PP_Asm);
887 }
888 }
889 case phases::Assemble:
890 return new AssembleJobAction(Input, types::TY_Object);
891 }
892
893 assert(0 && "invalid phase in ConstructPhaseAction");
894 return 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000895}
896
Daniel Dunbar21549232009-03-18 02:55:38 +0000897void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000898 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000899 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
900 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000901
Daniel Dunbara8231832009-09-08 23:36:43 +0000902 // FIXME: Pipes are forcibly disabled until we support executing them.
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000903 if (!CCCPrintBindings)
904 UsePipes = false;
Daniel Dunbara8231832009-09-08 23:36:43 +0000905
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000906 // -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
Daniel Dunbara8231832009-09-08 23:36:43 +0000914 // It is an error to provide a -o option if we are making multiple output
915 // files.
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000916 if (FinalOutput) {
917 unsigned NumOutputs = 0;
Daniel Dunbara8231832009-09-08 23:36:43 +0000918 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbar21549232009-03-18 02:55:38 +0000919 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000920 if ((*it)->getType() != types::TY_Nothing)
921 ++NumOutputs;
Daniel Dunbara8231832009-09-08 23:36:43 +0000922
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000923 if (NumOutputs > 1) {
924 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
925 FinalOutput = 0;
926 }
927 }
928
Daniel Dunbara8231832009-09-08 23:36:43 +0000929 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbar21549232009-03-18 02:55:38 +0000930 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000931 Action *A = *it;
932
Daniel Dunbara8231832009-09-08 23:36:43 +0000933 // If we are linking an image for multiple archs then the linker wants
934 // -arch_multiple and -final_output <final image name>. Unfortunately, this
935 // doesn't fit in cleanly because we have to pass this information down.
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000936 //
Daniel Dunbara8231832009-09-08 23:36:43 +0000937 // FIXME: This is a hack; find a cleaner way to integrate this into the
938 // process.
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000939 const char *LinkingOutput = 0;
Daniel Dunbard7b88c22009-03-26 16:12:09 +0000940 if (isa<LipoJobAction>(A)) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000941 if (FinalOutput)
942 LinkingOutput = FinalOutput->getValue(C.getArgs());
943 else
944 LinkingOutput = DefaultImageName.c_str();
945 }
946
947 InputInfo II;
Daniel Dunbara8231832009-09-08 23:36:43 +0000948 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbar49540182009-09-09 18:36:01 +0000949 /*BoundArch*/0,
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000950 /*CanAcceptPipe*/ true,
951 /*AtTopLevel*/ true,
952 /*LinkingOutput*/ LinkingOutput,
953 II);
954 }
Daniel Dunbar586dc232009-03-16 06:42:30 +0000955
Daniel Dunbara8231832009-09-08 23:36:43 +0000956 // If the user passed -Qunused-arguments or there were errors, don't warn
957 // about any unused arguments.
958 if (Diags.getNumErrors() ||
Daniel Dunbar1e23f5f2009-04-07 19:04:18 +0000959 C.getArgs().hasArg(options::OPT_Qunused_arguments))
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000960 return;
961
Daniel Dunbara2094e72009-03-29 22:24:54 +0000962 // Claim -### here.
963 (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
Daniel Dunbara8231832009-09-08 23:36:43 +0000964
Daniel Dunbar586dc232009-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 Dunbara8231832009-09-08 23:36:43 +0000968
Daniel Dunbar586dc232009-03-16 06:42:30 +0000969 // FIXME: It would be nice to be able to send the argument to the
Daniel Dunbara8231832009-09-08 23:36:43 +0000970 // Diagnostic, so that extra values, position, and so on could be printed.
Daniel Dunbar4f53b292009-04-04 00:52:26 +0000971 if (!A->isClaimed()) {
Daniel Dunbar1e23f5f2009-04-07 19:04:18 +0000972 if (A->getOption().hasNoArgumentUnused())
973 continue;
974
Daniel Dunbara8231832009-09-08 23:36:43 +0000975 // Suppress the warning automatically if this is just a flag, and it is an
976 // instance of an argument we already claimed.
Daniel Dunbar4f53b292009-04-04 00:52:26 +0000977 const Option &Opt = A->getOption();
978 if (isa<FlagOption>(Opt)) {
979 bool DuplicateClaimed = false;
980
981 // FIXME: Use iterator.
Daniel Dunbara8231832009-09-08 23:36:43 +0000982 for (ArgList::const_iterator it = C.getArgs().begin(),
Daniel Dunbar4f53b292009-04-04 00:52:26 +0000983 ie = C.getArgs().end(); it != ie; ++it) {
984 if ((*it)->isClaimed() && (*it)->getOption().matches(Opt.getId())) {
985 DuplicateClaimed = true;
986 break;
987 }
988 }
989
990 if (DuplicateClaimed)
991 continue;
992 }
993
Daniel Dunbara8231832009-09-08 23:36:43 +0000994 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000995 << A->getAsString(C.getArgs());
Daniel Dunbar4f53b292009-04-04 00:52:26 +0000996 }
Daniel Dunbar586dc232009-03-16 06:42:30 +0000997 }
Daniel Dunbar57b704d2009-03-13 22:12:33 +0000998}
999
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001000void Driver::BuildJobsForAction(Compilation &C,
1001 const Action *A,
1002 const ToolChain *TC,
Daniel Dunbar49540182009-09-09 18:36:01 +00001003 const char *BoundArch,
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001004 bool CanAcceptPipe,
1005 bool AtTopLevel,
1006 const char *LinkingOutput,
1007 InputInfo &Result) const {
Daniel Dunbara8231832009-09-08 23:36:43 +00001008 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbar60ccc762009-03-18 23:18:19 +00001009
1010 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbara8231832009-09-08 23:36:43 +00001011 // FIXME: Pipes are forcibly disabled until we support executing them.
Daniel Dunbar60ccc762009-03-18 23:18:19 +00001012 if (!CCCPrintBindings)
1013 UsePipes = false;
1014
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001015 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbara8231832009-09-08 23:36:43 +00001016 // FIXME: It would be nice to not claim this here; maybe the old scheme of
1017 // just using Args was better?
Daniel Dunbar115a7922009-03-19 07:29:38 +00001018 const Arg &Input = IA->getInputArg();
1019 Input.claim();
1020 if (isa<PositionalArg>(Input)) {
1021 const char *Name = Input.getValue(C.getArgs());
1022 Result = InputInfo(Name, A->getType(), Name);
1023 } else
1024 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001025 return;
1026 }
1027
1028 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbard7502d02009-09-08 23:37:19 +00001029 const ToolChain *TC = &C.getDefaultToolChain();
1030
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001031 std::string Arch;
Daniel Dunbard7502d02009-09-08 23:37:19 +00001032 if (BAA->getArchName())
1033 TC = Host->CreateToolChain(C.getArgs(), BAA->getArchName());
1034
Daniel Dunbar49540182009-09-09 18:36:01 +00001035 BuildJobsForAction(C, *BAA->begin(), TC, BAA->getArchName(),
1036 CanAcceptPipe, AtTopLevel, LinkingOutput, Result);
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001037 return;
1038 }
1039
1040 const JobAction *JA = cast<JobAction>(A);
1041 const Tool &T = TC->SelectTool(C, *JA);
Daniel Dunbara8231832009-09-08 23:36:43 +00001042
1043 // See if we should use an integrated preprocessor. We do so when we have
1044 // exactly one input, since this is the only use case we care about
1045 // (irrelevant since we don't support combine yet).
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001046 bool UseIntegratedCPP = false;
1047 const ActionList *Inputs = &A->getInputs();
1048 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
1049 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
1050 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
1051 !C.getArgs().hasArg(options::OPT_save_temps) &&
1052 T.hasIntegratedCPP()) {
1053 UseIntegratedCPP = true;
1054 Inputs = &(*Inputs)[0]->getInputs();
1055 }
1056 }
1057
1058 // Only use pipes when there is exactly one input.
1059 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001060 InputInfoList InputInfos;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001061 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
1062 it != ie; ++it) {
1063 InputInfo II;
Daniel Dunbar49540182009-09-09 18:36:01 +00001064 BuildJobsForAction(C, *it, TC, BoundArch, TryToUsePipeInput,
1065 /*AtTopLevel*/false, LinkingOutput, II);
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001066 InputInfos.push_back(II);
1067 }
1068
1069 // Determine if we should output to a pipe.
1070 bool OutputToPipe = false;
1071 if (CanAcceptPipe && T.canPipeOutput()) {
Daniel Dunbara8231832009-09-08 23:36:43 +00001072 // Some actions default to writing to a pipe if they are the top level phase
1073 // and there was no user override.
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001074 //
1075 // FIXME: Is there a better way to handle this?
1076 if (AtTopLevel) {
1077 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
1078 OutputToPipe = true;
Daniel Dunbar60ccc762009-03-18 23:18:19 +00001079 } else if (UsePipes)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001080 OutputToPipe = true;
1081 }
1082
1083 // Figure out where to put the job (pipes).
1084 Job *Dest = &C.getJobs();
1085 if (InputInfos[0].isPipe()) {
Daniel Dunbar441d0602009-03-17 17:53:55 +00001086 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001087 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
1088 Dest = &InputInfos[0].getPipe();
1089 }
1090
1091 // Always use the first input as the base input.
1092 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar441d0602009-03-17 17:53:55 +00001093
Daniel Dunbara8231832009-09-08 23:36:43 +00001094 // Determine the place to write output to (nothing, pipe, or filename) and
1095 // where to put the new job.
Daniel Dunbar441d0602009-03-17 17:53:55 +00001096 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001097 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001098 } else if (OutputToPipe) {
1099 // Append to current piped job or create a new one as appropriate.
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001100 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
1101 if (!PJ) {
1102 PJ = new PipedJob();
Daniel Dunbara8231832009-09-08 23:36:43 +00001103 // FIXME: Temporary hack so that -ccc-print-bindings work until we have
1104 // pipe support. Please remove later.
Daniel Dunbarb7b61b22009-03-20 00:11:04 +00001105 if (!CCCPrintBindings)
1106 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar871adcf2009-03-18 07:06:02 +00001107 Dest = PJ;
Daniel Dunbar441d0602009-03-17 17:53:55 +00001108 }
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001109 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001110 } else {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001111 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
1112 A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001113 }
1114
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001115 if (CCCPrintBindings) {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00001116 llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"'
1117 << " - \"" << T.getName() << "\", inputs: [";
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001118 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
1119 llvm::errs() << InputInfos[i].getAsString();
1120 if (i + 1 != e)
1121 llvm::errs() << ", ";
1122 }
1123 llvm::errs() << "], output: " << Result.getAsString() << "\n";
1124 } else {
Daniel Dunbara8231832009-09-08 23:36:43 +00001125 T.ConstructJob(C, *JA, *Dest, Result, InputInfos,
Daniel Dunbar49540182009-09-09 18:36:01 +00001126 C.getArgsForToolChain(TC, BoundArch), LinkingOutput);
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +00001127 }
Daniel Dunbarf353c8c2009-03-16 06:56:51 +00001128}
1129
Daniel Dunbara8231832009-09-08 23:36:43 +00001130const char *Driver::GetNamedOutputPath(Compilation &C,
Daniel Dunbar441d0602009-03-17 17:53:55 +00001131 const JobAction &JA,
1132 const char *BaseInput,
1133 bool AtTopLevel) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +00001134 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar441d0602009-03-17 17:53:55 +00001135 // Output to a user requested destination?
1136 if (AtTopLevel) {
1137 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
1138 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
1139 }
1140
1141 // Output to a temporary file?
1142 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbara8231832009-09-08 23:36:43 +00001143 std::string TmpName =
Daniel Dunbar214399e2009-03-18 19:34:39 +00001144 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
1145 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar441d0602009-03-17 17:53:55 +00001146 }
1147
1148 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbar5796bf42009-03-18 02:00:31 +00001149 std::string BaseName(BasePath.getLast());
Daniel Dunbar441d0602009-03-17 17:53:55 +00001150
1151 // Determine what the derived output name should be.
1152 const char *NamedOutput;
1153 if (JA.getType() == types::TY_Image) {
1154 NamedOutput = DefaultImageName.c_str();
1155 } else {
1156 const char *Suffix = types::getTypeTempSuffix(JA.getType());
1157 assert(Suffix && "All types used for output should have a suffix.");
1158
1159 std::string::size_type End = std::string::npos;
1160 if (!types::appendSuffixForType(JA.getType()))
1161 End = BaseName.rfind('.');
1162 std::string Suffixed(BaseName.substr(0, End));
1163 Suffixed += '.';
1164 Suffixed += Suffix;
1165 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
1166 }
1167
Daniel Dunbara8231832009-09-08 23:36:43 +00001168 // As an annoying special case, PCH generation doesn't strip the pathname.
Daniel Dunbar441d0602009-03-17 17:53:55 +00001169 if (JA.getType() == types::TY_PCH) {
1170 BasePath.eraseComponent();
Daniel Dunbar56c55942009-03-18 09:58:30 +00001171 if (BasePath.isEmpty())
1172 BasePath = NamedOutput;
1173 else
1174 BasePath.appendComponent(NamedOutput);
Daniel Dunbar441d0602009-03-17 17:53:55 +00001175 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
1176 } else {
1177 return C.addResultFile(NamedOutput);
1178 }
1179}
1180
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00001181std::string Driver::GetFilePath(const char *Name, const ToolChain &TC) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001182 const ToolChain::path_list &List = TC.getFilePaths();
Daniel Dunbara8231832009-09-08 23:36:43 +00001183 for (ToolChain::path_list::const_iterator
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001184 it = List.begin(), ie = List.end(); it != ie; ++it) {
1185 llvm::sys::Path P(*it);
1186 P.appendComponent(Name);
1187 if (P.exists())
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00001188 return P.str();
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001189 }
1190
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00001191 return Name;
Daniel Dunbarcb881672009-03-13 00:51:18 +00001192}
1193
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00001194std::string Driver::GetProgramPath(const char *Name, const ToolChain &TC,
1195 bool WantFile) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001196 const ToolChain::path_list &List = TC.getProgramPaths();
Daniel Dunbara8231832009-09-08 23:36:43 +00001197 for (ToolChain::path_list::const_iterator
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001198 it = List.begin(), ie = List.end(); it != ie; ++it) {
1199 llvm::sys::Path P(*it);
1200 P.appendComponent(Name);
Mike Stump950bedd2009-03-27 00:40:20 +00001201 if (WantFile ? P.exists() : P.canExecute())
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00001202 return P.str();
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001203 }
1204
Daniel Dunbarc50b00d2009-03-23 16:15:50 +00001205 // If all else failed, search the path.
1206 llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
Daniel Dunbar632f50e2009-03-18 21:34:08 +00001207 if (!P.empty())
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00001208 return P.str();
Daniel Dunbar632f50e2009-03-18 21:34:08 +00001209
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00001210 return Name;
Daniel Dunbarcb881672009-03-13 00:51:18 +00001211}
1212
Daniel Dunbar214399e2009-03-18 19:34:39 +00001213std::string Driver::GetTemporaryPath(const char *Suffix) const {
Daniel Dunbara8231832009-09-08 23:36:43 +00001214 // FIXME: This is lame; sys::Path should provide this function (in particular,
1215 // it should know how to find the temporary files dir).
Daniel Dunbar214399e2009-03-18 19:34:39 +00001216 std::string Error;
Daniel Dunbarb03417f2009-04-20 20:28:21 +00001217 const char *TmpDir = ::getenv("TMPDIR");
1218 if (!TmpDir)
1219 TmpDir = ::getenv("TEMP");
1220 if (!TmpDir)
Daniel Dunbar3ca7ee92009-04-21 00:25:10 +00001221 TmpDir = ::getenv("TMP");
1222 if (!TmpDir)
Daniel Dunbarb03417f2009-04-20 20:28:21 +00001223 TmpDir = "/tmp";
1224 llvm::sys::Path P(TmpDir);
Daniel Dunbarf60c63a2009-04-20 17:32:49 +00001225 P.appendComponent("cc");
Daniel Dunbar214399e2009-03-18 19:34:39 +00001226 if (P.makeUnique(false, &Error)) {
1227 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
1228 return "";
1229 }
1230
Daniel Dunbara8231832009-09-08 23:36:43 +00001231 // FIXME: Grumble, makeUnique sometimes leaves the file around!? PR3837.
Daniel Dunbar84603bc2009-03-18 23:08:52 +00001232 P.eraseFromDisk(false, 0);
1233
Daniel Dunbar214399e2009-03-18 19:34:39 +00001234 P.appendSuffix(Suffix);
Chris Lattnerd57a7ef2009-08-23 22:45:33 +00001235 return P.str();
Daniel Dunbar214399e2009-03-18 19:34:39 +00001236}
1237
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001238const HostInfo *Driver::GetHostInfo(const char *TripleStr) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +00001239 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001240 llvm::Triple Triple(TripleStr);
Daniel Dunbardd98e2c2009-03-10 23:41:59 +00001241
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001242 switch (Triple.getOS()) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001243 case llvm::Triple::AuroraUX:
1244 return createAuroraUXHostInfo(*this, Triple);
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001245 case llvm::Triple::Darwin:
1246 return createDarwinHostInfo(*this, Triple);
1247 case llvm::Triple::DragonFly:
1248 return createDragonFlyHostInfo(*this, Triple);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001249 case llvm::Triple::OpenBSD:
1250 return createOpenBSDHostInfo(*this, Triple);
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001251 case llvm::Triple::FreeBSD:
1252 return createFreeBSDHostInfo(*this, Triple);
Eli Friedman6b3454a2009-05-26 07:52:18 +00001253 case llvm::Triple::Linux:
1254 return createLinuxHostInfo(*this, Triple);
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001255 default:
1256 return createUnknownHostInfo(*this, Triple);
1257 }
Daniel Dunbardd98e2c2009-03-10 23:41:59 +00001258}
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001259
1260bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
Daniel Dunbara6046be2009-09-08 23:36:55 +00001261 const llvm::Triple &Triple) const {
Daniel Dunbara8231832009-09-08 23:36:43 +00001262 // Check if user requested no clang, or clang doesn't understand this type (we
1263 // only handle single inputs for now).
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00001264 if (!CCCUseClang || JA.size() != 1 ||
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001265 !types::isAcceptedByClang((*JA.begin())->getType()))
1266 return false;
1267
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001268 // Otherwise make sure this is an action clang understands.
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001269 if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbar6256d362009-03-24 19:14:56 +00001270 if (!CCCUseClangCPP) {
1271 Diag(clang::diag::warn_drv_not_using_clang_cpp);
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001272 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001273 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001274 } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
1275 return false;
1276
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001277 // Use clang for C++?
Daniel Dunbar6256d362009-03-24 19:14:56 +00001278 if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
1279 Diag(clang::diag::warn_drv_not_using_clang_cxx);
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001280 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001281 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001282
Daniel Dunbara8231832009-09-08 23:36:43 +00001283 // Always use clang for precompiling and AST generation, regardless of archs.
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00001284 if (isa<PrecompileJobAction>(JA) || JA.getType() == types::TY_AST)
Daniel Dunbarfec26bd2009-04-16 23:10:13 +00001285 return true;
1286
Daniel Dunbara8231832009-09-08 23:36:43 +00001287 // Finally, don't use clang if this isn't one of the user specified archs to
1288 // build.
Daniel Dunbara6046be2009-09-08 23:36:55 +00001289 if (!CCCClangArchs.empty() && !CCCClangArchs.count(Triple.getArch())) {
1290 Diag(clang::diag::warn_drv_not_using_clang_arch) << Triple.getArchName();
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001291 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001292 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001293
1294 return true;
1295}
Daniel Dunbard73fe9b2009-03-26 15:58:36 +00001296
Daniel Dunbara8231832009-09-08 23:36:43 +00001297/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the
1298/// grouped values as integers. Numbers which are not provided are set to 0.
Daniel Dunbard73fe9b2009-03-26 15:58:36 +00001299///
Daniel Dunbara8231832009-09-08 23:36:43 +00001300/// \return True if the entire string was parsed (9.2), or all groups were
1301/// parsed (10.3.5extrastuff).
1302bool Driver::GetReleaseVersion(const char *Str, unsigned &Major,
Daniel Dunbard73fe9b2009-03-26 15:58:36 +00001303 unsigned &Minor, unsigned &Micro,
1304 bool &HadExtra) {
1305 HadExtra = false;
1306
1307 Major = Minor = Micro = 0;
Daniel Dunbara8231832009-09-08 23:36:43 +00001308 if (*Str == '\0')
Daniel Dunbard73fe9b2009-03-26 15:58:36 +00001309 return true;
1310
1311 char *End;
1312 Major = (unsigned) strtol(Str, &End, 10);
1313 if (*Str != '\0' && *End == '\0')
1314 return true;
1315 if (*End != '.')
1316 return false;
Daniel Dunbara8231832009-09-08 23:36:43 +00001317
Daniel Dunbard73fe9b2009-03-26 15:58:36 +00001318 Str = End+1;
1319 Minor = (unsigned) strtol(Str, &End, 10);
1320 if (*Str != '\0' && *End == '\0')
1321 return true;
1322 if (*End != '.')
1323 return false;
1324
1325 Str = End+1;
1326 Micro = (unsigned) strtol(Str, &End, 10);
1327 if (*Str != '\0' && *End == '\0')
1328 return true;
1329 if (Str == End)
1330 return false;
1331 HadExtra = true;
1332 return true;
1333}