blob: bf9244f28286407fcfd9498a50f623c2ae91cbf6 [file] [log] [blame]
Daniel Dunbar544ecd12009-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 Dunbar544ecd12009-03-02 19:59:07 +000010#include "clang/Driver/Driver.h"
Daniel Dunbar544ecd12009-03-02 19:59:07 +000011
Daniel Dunbar1688f1a2009-03-12 07:58:46 +000012#include "clang/Driver/Action.h"
Daniel Dunbarb2cd66b2009-03-04 20:49:20 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
15#include "clang/Driver/Compilation.h"
Daniel Dunbarc0b3e952009-03-12 08:55:43 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar4dff6a42009-03-10 23:41:59 +000017#include "clang/Driver/HostInfo.h"
Daniel Dunbare75d8342009-03-16 06:56:51 +000018#include "clang/Driver/Job.h"
Daniel Dunbaraa767372009-11-19 00:15:11 +000019#include "clang/Driver/OptTable.h"
Daniel Dunbard02cb1d2009-03-05 06:38:47 +000020#include "clang/Driver/Option.h"
Daniel Dunbarb2cd66b2009-03-04 20:49:20 +000021#include "clang/Driver/Options.h"
Daniel Dunbare75d8342009-03-16 06:56:51 +000022#include "clang/Driver/Tool.h"
23#include "clang/Driver/ToolChain.h"
Daniel Dunbar1688f1a2009-03-12 07:58:46 +000024#include "clang/Driver/Types.h"
Daniel Dunbard02cb1d2009-03-05 06:38:47 +000025
Douglas Gregor7b71e632009-04-27 22:23:34 +000026#include "clang/Basic/Version.h"
27
Daniel Dunbare5dc4822009-03-13 20:33:35 +000028#include "llvm/ADT/StringSet.h"
Daniel Dunbar2608c542009-03-18 01:38:48 +000029#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbard02cb1d2009-03-05 06:38:47 +000030#include "llvm/Support/raw_ostream.h"
Daniel Dunbar1688f1a2009-03-12 07:58:46 +000031#include "llvm/System/Path.h"
Daniel Dunbar6f668772009-03-18 21:34:08 +000032#include "llvm/System/Program.h"
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +000033
Daniel Dunbare75d8342009-03-16 06:56:51 +000034#include "InputInfo.h"
35
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +000036#include <map>
37
Daniel Dunbarb2cd66b2009-03-04 20:49:20 +000038using namespace clang::driver;
Chris Lattnerada1c912009-03-26 05:56:24 +000039using namespace clang;
Daniel Dunbarb2cd66b2009-03-04 20:49:20 +000040
Daniel Dunbarb5bcd6b2009-08-23 18:42:54 +000041// Used to set values for "production" clang, for releases.
Daniel Dunbarccc60da2009-08-23 19:41:53 +000042// #define USE_PRODUCTION_CLANG
Daniel Dunbarb5bcd6b2009-08-23 18:42:54 +000043
Daniel Dunbar4dff6a42009-03-10 23:41:59 +000044Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbarc0b3e952009-03-12 08:55:43 +000045 const char *_DefaultHostTriple,
Daniel Dunbare75d8342009-03-16 06:56:51 +000046 const char *_DefaultImageName,
Daniel Dunbar5564ba72009-09-22 22:31:13 +000047 bool IsProduction, Diagnostic &_Diags)
Daniel Dunbar26228a02009-11-18 20:19:36 +000048 : Opts(createDriverOptTable()), Diags(_Diags),
Daniel Dunbar4dff6a42009-03-10 23:41:59 +000049 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
Daniel Dunbare75d8342009-03-16 06:56:51 +000050 DefaultImageName(_DefaultImageName),
Daniel Dunbar4dff6a42009-03-10 23:41:59 +000051 Host(0),
Daniel Dunbarb39cc522009-03-17 22:47:06 +000052 CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false),
Daniel Dunbarb5bcd6b2009-08-23 18:42:54 +000053 CCCGenericGCCName("gcc"), CCCUseClang(true),
Daniel Dunbar5564ba72009-09-22 22:31:13 +000054 CCCUseClangCXX(true), CCCUseClangCPP(true), CCCUsePCH(true),
Mike Stump11289f42009-09-09 15:08:12 +000055 SuppressMissingInputWarning(false) {
Daniel Dunbar5564ba72009-09-22 22:31:13 +000056 if (IsProduction) {
57 // In a "production" build, only use clang on architectures we expect to
58 // work, and don't use clang C++.
59 //
60 // During development its more convenient to always have the driver use
61 // clang, but we don't want users to be confused when things don't work, or
62 // to file bugs for things we don't support.
63 CCCClangArchs.insert(llvm::Triple::x86);
64 CCCClangArchs.insert(llvm::Triple::x86_64);
65 CCCClangArchs.insert(llvm::Triple::arm);
66
67 CCCUseClangCXX = false;
68 }
Daniel Dunbar544ecd12009-03-02 19:59:07 +000069}
70
71Driver::~Driver() {
Daniel Dunbarb2cd66b2009-03-04 20:49:20 +000072 delete Opts;
Daniel Dunbar01601722009-03-18 01:09:40 +000073 delete Host;
Daniel Dunbar544ecd12009-03-02 19:59:07 +000074}
75
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +000076InputArgList *Driver::ParseArgStrings(const char **ArgBegin,
Daniel Dunbardac54a82009-03-25 04:13:45 +000077 const char **ArgEnd) {
Daniel Dunbar2608c542009-03-18 01:38:48 +000078 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbardac54a82009-03-25 04:13:45 +000079 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +000080
Daniel Dunbar65229332009-03-13 11:38:42 +000081 // FIXME: Handle '@' args (or at least error on them).
82
Daniel Dunbard02cb1d2009-03-05 06:38:47 +000083 unsigned Index = 0, End = ArgEnd - ArgBegin;
84 while (Index < End) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +000085 // gcc's handling of empty arguments doesn't make sense, but this is not a
86 // common use case. :)
Daniel Dunbar0f35a022009-03-13 01:01:44 +000087 //
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +000088 // We just ignore them here (note that other things may still take them as
89 // arguments).
Daniel Dunbar0f35a022009-03-13 01:01:44 +000090 if (Args->getArgString(Index)[0] == '\0') {
91 ++Index;
92 continue;
93 }
94
Daniel Dunbard02cb1d2009-03-05 06:38:47 +000095 unsigned Prev = Index;
Daniel Dunbard8500f32009-03-22 23:26:43 +000096 Arg *A = getOpts().ParseOneArg(*Args, Index);
97 assert(Index > Prev && "Parser failed to consume argument.");
Daniel Dunbar1688f1a2009-03-12 07:58:46 +000098
Daniel Dunbard8500f32009-03-22 23:26:43 +000099 // Check for missing argument error.
100 if (!A) {
101 assert(Index >= End && "Unexpected parser error.");
102 Diag(clang::diag::err_drv_missing_argument)
103 << Args->getArgString(Prev)
104 << (Index - Prev - 1);
105 break;
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000106 }
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000107
Daniel Dunbard8500f32009-03-22 23:26:43 +0000108 if (A->getOption().isUnsupported()) {
109 Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
110 continue;
111 }
112 Args->append(A);
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000113 }
114
115 return Args;
116}
117
Daniel Dunbar544ecd12009-03-02 19:59:07 +0000118Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar2608c542009-03-18 01:38:48 +0000119 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
120
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000121 // FIXME: Handle environment options which effect driver behavior, somewhere
122 // (client?). GCC_EXEC_PREFIX, COMPILER_PATH, LIBRARY_PATH, LPATH,
123 // CC_PRINT_OPTIONS.
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000124
125 // FIXME: What are we going to do with -V and -b?
126
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000127 // FIXME: This stuff needs to go into the Compilation, not the driver.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000128 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000129
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000130 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbar4dff6a42009-03-10 23:41:59 +0000131 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000132
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000133 // Read -ccc args.
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000134 //
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000135 // FIXME: We need to figure out where this behavior should live. Most of it
136 // should be outside in the client; the parts that aren't should have proper
137 // options, either by introducing new ones or by overloading gcc ones like -V
138 // or -b.
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000139 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
140 const char *Opt = *Start + 5;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000141
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000142 if (!strcmp(Opt, "print-options")) {
143 CCCPrintOptions = true;
144 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000145 CCCPrintActions = true;
Daniel Dunbarb39cc522009-03-17 22:47:06 +0000146 } else if (!strcmp(Opt, "print-bindings")) {
147 CCCPrintBindings = true;
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000148 } else if (!strcmp(Opt, "cxx")) {
149 CCCIsCXX = true;
150 } else if (!strcmp(Opt, "echo")) {
151 CCCEcho = true;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000152
Daniel Dunbar7803c952009-04-01 23:34:41 +0000153 } else if (!strcmp(Opt, "gcc-name")) {
154 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
155 CCCGenericGCCName = *++Start;
156
Daniel Dunbar88f356e2009-03-24 19:02:31 +0000157 } else if (!strcmp(Opt, "clang-cxx")) {
158 CCCUseClangCXX = true;
Daniel Dunbar07b74922009-07-23 17:48:59 +0000159 } else if (!strcmp(Opt, "no-clang-cxx")) {
160 CCCUseClangCXX = false;
Douglas Gregor111af7d2009-04-18 00:34:01 +0000161 } else if (!strcmp(Opt, "pch-is-pch")) {
162 CCCUsePCH = true;
163 } else if (!strcmp(Opt, "pch-is-pth")) {
164 CCCUsePCH = false;
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000165 } else if (!strcmp(Opt, "no-clang")) {
Daniel Dunbar88f356e2009-03-24 19:02:31 +0000166 CCCUseClang = false;
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000167 } else if (!strcmp(Opt, "no-clang-cpp")) {
Daniel Dunbar88f356e2009-03-24 19:02:31 +0000168 CCCUseClangCPP = false;
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000169 } else if (!strcmp(Opt, "clang-archs")) {
170 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbar953b8d12009-09-08 23:36:55 +0000171 llvm::StringRef Cur = *++Start;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000172
Daniel Dunbar88f356e2009-03-24 19:02:31 +0000173 CCCClangArchs.clear();
Daniel Dunbar953b8d12009-09-08 23:36:55 +0000174 while (!Cur.empty()) {
175 std::pair<llvm::StringRef, llvm::StringRef> Split = Cur.split(',');
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000176
Daniel Dunbar953b8d12009-09-08 23:36:55 +0000177 if (!Split.first.empty()) {
178 llvm::Triple::ArchType Arch =
179 llvm::Triple(Split.first, "", "").getArch();
180
181 if (Arch == llvm::Triple::UnknownArch) {
182 // FIXME: Error handling.
183 llvm::errs() << "invalid arch name: " << Split.first << "\n";
184 exit(1);
185 }
186
187 CCCClangArchs.insert(Arch);
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000188 }
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000189
Daniel Dunbar953b8d12009-09-08 23:36:55 +0000190 Cur = Split.second;
191 }
Daniel Dunbar4dff6a42009-03-10 23:41:59 +0000192 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000193 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbar4dff6a42009-03-10 23:41:59 +0000194 HostTriple = *++Start;
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000195
Daniel Dunbar5ed07fe2009-09-04 18:35:03 +0000196 } else if (!strcmp(Opt, "install-dir")) {
197 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
198 Dir = *++Start;
199
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000200 } else {
201 // FIXME: Error handling.
202 llvm::errs() << "invalid option: " << *Start << "\n";
203 exit(1);
204 }
205 }
Daniel Dunbar4dff6a42009-03-10 23:41:59 +0000206
Daniel Dunbardac54a82009-03-25 04:13:45 +0000207 InputArgList *Args = ParseArgStrings(Start, End);
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000208
Daniel Dunbar52e0c702009-03-17 20:45:45 +0000209 Host = GetHostInfo(HostTriple);
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000210
Daniel Dunbar3ce436d2009-03-16 06:42:30 +0000211 // The compilation takes ownership of Args.
Daniel Dunbar1ef3f2a2009-09-08 23:37:19 +0000212 Compilation *C = new Compilation(*this, *Host->CreateToolChain(*Args), Args);
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000213
214 // FIXME: This behavior shouldn't be here.
215 if (CCCPrintOptions) {
216 PrintOptions(C->getArgs());
217 return C;
218 }
219
220 if (!HandleImmediateArgs(*C))
221 return C;
222
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000223 // Construct the list of abstract actions to perform for this compilation. We
224 // avoid passing a Compilation here simply to enforce the abstraction that
225 // pipelining is not host or toolchain dependent (other than the driver driver
226 // test).
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000227 if (Host->useDriverDriver())
228 BuildUniversalActions(C->getArgs(), C->getActions());
229 else
230 BuildActions(C->getArgs(), C->getActions());
231
232 if (CCCPrintActions) {
Daniel Dunbareb843be2009-03-18 03:13:20 +0000233 PrintActions(*C);
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000234 return C;
235 }
236
237 BuildJobs(*C);
Daniel Dunbaradc91e62009-03-15 01:38:15 +0000238
239 return C;
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000240}
241
Daniel Dunbar38bfda62009-07-01 20:03:04 +0000242int Driver::ExecuteCompilation(const Compilation &C) const {
243 // Just print if -### was present.
244 if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
245 C.PrintJob(llvm::errs(), C.getJobs(), "\n", true);
246 return 0;
247 }
248
249 // If there were errors building the compilation, quit now.
250 if (getDiags().getNumErrors())
251 return 1;
252
253 const Command *FailingCommand = 0;
254 int Res = C.ExecuteJob(C.getJobs(), FailingCommand);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000255
Daniel Dunbar38bfda62009-07-01 20:03:04 +0000256 // Remove temp files.
257 C.CleanupFileList(C.getTempFiles());
258
259 // If the compilation failed, remove result files as well.
260 if (Res != 0 && !C.getArgs().hasArg(options::OPT_save_temps))
261 C.CleanupFileList(C.getResultFiles(), true);
262
263 // Print extra information about abnormal failures, if possible.
264 if (Res) {
265 // This is ad-hoc, but we don't want to be excessively noisy. If the result
266 // status was 1, assume the command failed normally. In particular, if it
267 // was the compiler then assume it gave a reasonable error code. Failures in
268 // other tools are less common, and they generally have worse diagnostics,
269 // so always print the diagnostic there.
270 const Action &Source = FailingCommand->getSource();
271 bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) ||
272 isa<PrecompileJobAction>(Source) ||
273 isa<AnalyzeJobAction>(Source) ||
274 isa<CompileJobAction>(Source));
275
276 if (!IsFriendlyTool || Res != 1) {
277 // FIXME: See FIXME above regarding result code interpretation.
278 if (Res < 0)
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000279 Diag(clang::diag::err_drv_command_signalled)
Daniel Dunbar38bfda62009-07-01 20:03:04 +0000280 << Source.getClassName() << -Res;
281 else
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000282 Diag(clang::diag::err_drv_command_failed)
Daniel Dunbar38bfda62009-07-01 20:03:04 +0000283 << Source.getClassName() << Res;
284 }
285 }
286
287 return Res;
288}
289
Daniel Dunbar446f6842009-03-12 18:24:49 +0000290void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000291 unsigned i = 0;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000292 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000293 it != ie; ++it, ++i) {
294 Arg *A = *it;
295 llvm::errs() << "Option " << i << " - "
296 << "Name: \"" << A->getOption().getName() << "\", "
297 << "Values: {";
298 for (unsigned j = 0; j < A->getNumValues(); ++j) {
299 if (j)
300 llvm::errs() << ", ";
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000301 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000302 }
303 llvm::errs() << "}\n";
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000304 }
Daniel Dunbar544ecd12009-03-02 19:59:07 +0000305}
Daniel Dunbar4dff6a42009-03-10 23:41:59 +0000306
Daniel Dunbar7c925282009-03-31 21:38:17 +0000307static std::string getOptionHelpName(const OptTable &Opts, options::ID Id) {
308 std::string Name = Opts.getOptionName(Id);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000309
Daniel Dunbar7c925282009-03-31 21:38:17 +0000310 // Add metavar, if used.
311 switch (Opts.getOptionKind(Id)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000312 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
Daniel Dunbar7c925282009-03-31 21:38:17 +0000313 assert(0 && "Invalid option with help text.");
314
315 case Option::MultiArgClass: case Option::JoinedAndSeparateClass:
316 assert(0 && "Cannot print metavar for this kind of option.");
317
318 case Option::FlagClass:
319 break;
320
321 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
322 Name += ' ';
323 // FALLTHROUGH
324 case Option::JoinedClass: case Option::CommaJoinedClass:
325 Name += Opts.getOptionMetaVar(Id);
326 break;
327 }
328
329 return Name;
330}
331
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000332void Driver::PrintHelp(bool ShowHidden) const {
Daniel Dunbar7c925282009-03-31 21:38:17 +0000333 llvm::raw_ostream &OS = llvm::outs();
334
335 OS << "OVERVIEW: clang \"gcc-compatible\" driver\n";
336 OS << '\n';
337 OS << "USAGE: " << Name << " [options] <input files>\n";
338 OS << '\n';
339 OS << "OPTIONS:\n";
340
341 // Render help text into (option, help) pairs.
342 std::vector< std::pair<std::string, const char*> > OptionHelp;
343
Daniel Dunbarda13faf2009-11-19 04:25:22 +0000344 for (unsigned i = 0, e = getOpts().getNumOptions(); i != e; ++i) {
345 options::ID Id = (options::ID) (i + 1);
Daniel Dunbar7c925282009-03-31 21:38:17 +0000346 if (const char *Text = getOpts().getOptionHelpText(Id))
347 OptionHelp.push_back(std::make_pair(getOptionHelpName(getOpts(), Id),
348 Text));
349 }
350
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000351 if (ShowHidden) {
352 OptionHelp.push_back(std::make_pair("\nDRIVER OPTIONS:",""));
353 OptionHelp.push_back(std::make_pair("-ccc-cxx",
354 "Act as a C++ driver"));
355 OptionHelp.push_back(std::make_pair("-ccc-gcc-name",
356 "Name for native GCC compiler"));
357 OptionHelp.push_back(std::make_pair("-ccc-clang-cxx",
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +0000358 "Enable the clang compiler for C++"));
359 OptionHelp.push_back(std::make_pair("-ccc-no-clang-cxx",
360 "Disable the clang compiler for C++"));
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000361 OptionHelp.push_back(std::make_pair("-ccc-no-clang",
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +0000362 "Disable the clang compiler"));
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000363 OptionHelp.push_back(std::make_pair("-ccc-no-clang-cpp",
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +0000364 "Disable the clang preprocessor"));
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000365 OptionHelp.push_back(std::make_pair("-ccc-clang-archs",
366 "Comma separate list of architectures "
367 "to use the clang compiler for"));
Douglas Gregor111af7d2009-04-18 00:34:01 +0000368 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pch",
369 "Use lazy PCH for precompiled headers"));
370 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pth",
371 "Use pretokenized headers for precompiled headers"));
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000372
373 OptionHelp.push_back(std::make_pair("\nDEBUG/DEVELOPMENT OPTIONS:",""));
374 OptionHelp.push_back(std::make_pair("-ccc-host-triple",
Daniel Dunbar5ed07fe2009-09-04 18:35:03 +0000375 "Simulate running on the given target"));
376 OptionHelp.push_back(std::make_pair("-ccc-install-dir",
377 "Simulate installation in the given directory"));
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000378 OptionHelp.push_back(std::make_pair("-ccc-print-options",
379 "Dump parsed command line arguments"));
380 OptionHelp.push_back(std::make_pair("-ccc-print-phases",
381 "Dump list of actions to perform"));
382 OptionHelp.push_back(std::make_pair("-ccc-print-bindings",
383 "Show bindings of tools to actions"));
384 OptionHelp.push_back(std::make_pair("CCC_ADD_ARGS",
385 "(ENVIRONMENT VARIABLE) Comma separated list of "
386 "arguments to prepend to the command line"));
387 }
388
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000389 // Find the maximum option length.
Daniel Dunbar7c925282009-03-31 21:38:17 +0000390 unsigned OptionFieldWidth = 0;
391 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000392 // Skip titles.
393 if (!OptionHelp[i].second)
394 continue;
395
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000396 // Limit the amount of padding we are willing to give up for alignment.
Daniel Dunbar7c925282009-03-31 21:38:17 +0000397 unsigned Length = OptionHelp[i].first.size();
398 if (Length <= 23)
399 OptionFieldWidth = std::max(OptionFieldWidth, Length);
400 }
401
402 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
403 const std::string &Option = OptionHelp[i].first;
404 OS << " " << Option;
405 for (int j = Option.length(), e = OptionFieldWidth; j < e; ++j)
406 OS << ' ';
407 OS << ' ' << OptionHelp[i].second << '\n';
408 }
409
410 OS.flush();
411}
412
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000413void Driver::PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000414 // FIXME: The following handlers should use a callback mechanism, we don't
415 // know what the client would like to do.
Mike Stump727170d2009-10-09 17:31:54 +0000416#ifdef CLANG_VENDOR
417 OS << CLANG_VENDOR;
418#endif
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000419 OS << "clang version " CLANG_VERSION_STRING " ("
Douglas Gregor1b7035d2009-10-05 20:33:49 +0000420 << getClangSubversionPath();
421 if (unsigned Revision = getClangSubversionRevision())
422 OS << " " << Revision;
423 OS << ")" << '\n';
Daniel Dunbarb10248802009-03-26 16:09:13 +0000424
425 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000426 OS << "Target: " << TC.getTripleString() << '\n';
Daniel Dunbar10978e42009-06-16 23:32:58 +0000427
428 // Print the threading model.
429 //
430 // FIXME: Implement correctly.
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000431 OS << "Thread model: " << "posix" << '\n';
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000432}
433
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000434bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000435 // The order these options are handled in in gcc is all over the place, but we
436 // don't expect inconsistencies w.r.t. that to matter in practice.
Daniel Dunbar7c925282009-03-31 21:38:17 +0000437
Daniel Dunbara9bbcfa2009-04-04 05:17:38 +0000438 if (C.getArgs().hasArg(options::OPT_dumpversion)) {
Douglas Gregor7b71e632009-04-27 22:23:34 +0000439 llvm::outs() << CLANG_VERSION_STRING "\n";
Daniel Dunbara9bbcfa2009-04-04 05:17:38 +0000440 return false;
441 }
442
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000443 if (C.getArgs().hasArg(options::OPT__help) ||
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000444 C.getArgs().hasArg(options::OPT__help_hidden)) {
445 PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden));
Daniel Dunbar7c925282009-03-31 21:38:17 +0000446 return false;
447 }
448
Daniel Dunbarb0006ae2009-04-02 15:05:41 +0000449 if (C.getArgs().hasArg(options::OPT__version)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000450 // Follow gcc behavior and use stdout for --version and stderr for -v.
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000451 PrintVersion(C, llvm::outs());
Daniel Dunbarb0006ae2009-04-02 15:05:41 +0000452 return false;
453 }
454
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000455 if (C.getArgs().hasArg(options::OPT_v) ||
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000456 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000457 PrintVersion(C, llvm::errs());
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000458 SuppressMissingInputWarning = true;
459 }
460
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000461 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbard972e222009-03-20 04:37:21 +0000462 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
463 llvm::outs() << "programs: =";
464 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
465 ie = TC.getProgramPaths().end(); it != ie; ++it) {
466 if (it != TC.getProgramPaths().begin())
467 llvm::outs() << ':';
468 llvm::outs() << *it;
469 }
470 llvm::outs() << "\n";
471 llvm::outs() << "libraries: =";
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000472 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
Daniel Dunbard972e222009-03-20 04:37:21 +0000473 ie = TC.getFilePaths().end(); it != ie; ++it) {
474 if (it != TC.getFilePaths().begin())
475 llvm::outs() << ':';
476 llvm::outs() << *it;
477 }
478 llvm::outs() << "\n";
Daniel Dunbar7c925282009-03-31 21:38:17 +0000479 return false;
Daniel Dunbard972e222009-03-20 04:37:21 +0000480 }
481
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000482 // FIXME: The following handlers should use a callback mechanism, we don't
483 // know what the client would like to do.
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000484 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
Daniel Dunbar1ce81532009-09-09 22:33:00 +0000485 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC) << "\n";
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000486 return false;
487 }
488
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000489 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
Daniel Dunbar1ce81532009-09-09 22:33:00 +0000490 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC) << "\n";
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000491 return false;
492 }
493
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000494 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbar1ce81532009-09-09 22:33:00 +0000495 llvm::outs() << GetFilePath("libgcc.a", TC) << "\n";
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000496 return false;
497 }
498
Daniel Dunbar1b3ec3a2009-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;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000506
Daniel Dunbar1b3ec3a2009-06-16 23:25:22 +0000507 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;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000528
Daniel Dunbar1b3ec3a2009-06-16 23:25:22 +0000529 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 Dunbar5e0f6af2009-03-13 00:51:18 +0000540 return true;
541}
542
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000543static unsigned PrintActions1(const Compilation &C, Action *A,
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000544 std::map<Action*, unsigned> &Ids) {
545 if (Ids.count(A))
546 return Ids[A];
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000547
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000548 std::string str;
549 llvm::raw_string_ostream os(str);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000550
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000551 os << Action::getClassName(A->getKind()) << ", ";
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000552 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbareb843be2009-03-18 03:13:20 +0000553 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000554 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000555 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
Daniel Dunbareb843be2009-03-18 03:13:20 +0000556 C.getDefaultToolChain().getArchName()) << '"'
557 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000558 } else {
559 os << "{";
560 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbareb843be2009-03-18 03:13:20 +0000561 os << PrintActions1(C, *it, Ids);
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000562 ++it;
563 if (it != ie)
564 os << ", ";
565 }
566 os << "}";
567 }
568
569 unsigned Id = Ids.size();
570 Ids[A] = Id;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000571 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000572 << types::getTypeName(A->getType()) << "\n";
573
574 return Id;
575}
576
Daniel Dunbareb843be2009-03-18 03:13:20 +0000577void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000578 std::map<Action*, unsigned> Ids;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000579 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbareb843be2009-03-18 03:13:20 +0000580 ie = C.getActions().end(); it != ie; ++it)
581 PrintActions1(C, *it, Ids);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000582}
583
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000584void Driver::BuildUniversalActions(const ArgList &Args,
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000585 ActionList &Actions) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000586 llvm::PrettyStackTraceString CrashInfo("Building universal build actions");
587 // Collect the list of architectures. Duplicates are allowed, but should only
588 // be handled once (in the order seen).
Daniel Dunbare5dc4822009-03-13 20:33:35 +0000589 llvm::StringSet<> ArchNames;
590 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000591 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbarf479c122009-03-12 18:40:18 +0000592 it != ie; ++it) {
593 Arg *A = *it;
594
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000595 if (A->getOption().matches(options::OPT_arch)) {
Daniel Dunbar9c3f7c42009-09-08 23:37:30 +0000596 // Validate the option here; we don't save the type here because its
597 // particular spelling may participate in other driver choices.
598 llvm::Triple::ArchType Arch =
599 llvm::Triple::getArchTypeForDarwinArchName(A->getValue(Args));
600 if (Arch == llvm::Triple::UnknownArch) {
601 Diag(clang::diag::err_drv_invalid_arch_name)
602 << A->getAsString(Args);
603 continue;
604 }
605
Daniel Dunbar2da02722009-03-19 07:55:12 +0000606 A->claim();
Daniel Dunbar7b574042009-09-08 23:37:02 +0000607 if (ArchNames.insert(A->getValue(Args)))
608 Archs.push_back(A->getValue(Args));
Daniel Dunbarf479c122009-03-12 18:40:18 +0000609 }
610 }
611
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000612 // When there is no explicit arch for this platform, make sure we still bind
613 // the architecture (to the default) so that -Xarch_ is handled correctly.
Daniel Dunbareb843be2009-03-18 03:13:20 +0000614 if (!Archs.size())
615 Archs.push_back(0);
Daniel Dunbarf479c122009-03-12 18:40:18 +0000616
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000617 // FIXME: We killed off some others but these aren't yet detected in a
618 // functional manner. If we added information to jobs about which "auxiliary"
619 // files they wrote then we could detect the conflict these cause downstream.
Daniel Dunbarf479c122009-03-12 18:40:18 +0000620 if (Archs.size() > 1) {
621 // No recovery needed, the point of this is just to prevent
622 // overwriting the same files.
Daniel Dunbarf479c122009-03-12 18:40:18 +0000623 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000624 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar0e759942009-03-20 06:14:23 +0000625 << A->getAsString(Args);
Daniel Dunbarf479c122009-03-12 18:40:18 +0000626 }
627
628 ActionList SingleActions;
629 BuildActions(Args, SingleActions);
630
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000631 // Add in arch binding and lipo (if necessary) for every top level action.
Daniel Dunbarf479c122009-03-12 18:40:18 +0000632 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
633 Action *Act = SingleActions[i];
634
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000635 // Make sure we can lipo this kind of output. If not (and it is an actual
636 // output) then we disallow, since we can't create an output file with the
637 // right name without overwriting it. We could remove this oddity by just
638 // changing the output names to include the arch, which would also fix
Daniel Dunbarf479c122009-03-12 18:40:18 +0000639 // -save-temps. Compatibility wins for now.
640
Daniel Dunbare2ca3bd2009-03-13 17:46:02 +0000641 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbarf479c122009-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 Dunbar2da02722009-03-19 07:55:12 +0000646 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbare5dc4822009-03-13 20:33:35 +0000647 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbarf479c122009-03-12 18:40:18 +0000648
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000649 // Lipo if necessary, we do it this way because we need to set the arch flag
650 // so that -Xarch_ gets overwritten.
Daniel Dunbarf479c122009-03-12 18:40:18 +0000651 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 Dunbar1688f1a2009-03-12 07:58:46 +0000656}
657
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000658void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +0000659 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000660 // Start by constructing the list of inputs and their types.
661
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000662 // Track the current user specified (-x) input. We also explicitly track the
663 // argument used to set the type; we only want to claim the type when we
664 // actually use it, so we warn about unused -x arguments.
Daniel Dunbarc5a5ac52009-03-13 17:57:10 +0000665 types::ID InputType = types::TY_Nothing;
666 Arg *InputTypeArg = 0;
667
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000668 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000669 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000670 it != ie; ++it) {
671 Arg *A = *it;
672
673 if (isa<InputOption>(A->getOption())) {
674 const char *Value = A->getValue(Args);
675 types::ID Ty = types::TY_INVALID;
676
677 // Infer the input type if necessary.
Daniel Dunbarc5a5ac52009-03-13 17:57:10 +0000678 if (InputType == types::TY_Nothing) {
679 // If there was an explicit arg for this, claim it.
680 if (InputTypeArg)
681 InputTypeArg->claim();
682
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000683 // stdin must be handled specially.
684 if (memcmp(Value, "-", 2) == 0) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000685 // If running with -E, treat as a C input (this changes the builtin
686 // macros, for example). This may be overridden by -ObjC below.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000687 //
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000688 // Otherwise emit an error but still use a valid type to avoid
689 // spurious errors (e.g., no inputs).
Daniel Dunbarfffd1812009-11-19 04:00:53 +0000690 if (!Args.hasArgNoClaim(options::OPT_E))
Daniel Dunbar5cd1e412009-03-12 09:13:48 +0000691 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000692 Ty = types::TY_C;
693 } else {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000694 // Otherwise lookup by extension, and fallback to ObjectType if not
695 // found. We use a host hook here because Darwin at least has its own
696 // idea of what .s is.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000697 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbarea9f0322009-03-20 23:39:23 +0000698 Ty = Host->lookupTypeForExtension(Ext + 1);
699
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000700 if (Ty == types::TY_INVALID)
701 Ty = types::TY_Object;
702 }
703
Daniel Dunbar82b22102009-05-18 21:47:54 +0000704 // -ObjC and -ObjC++ override the default language, but only for "source
705 // files". We just treat everything that isn't a linker input as a
706 // source file.
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000707 //
Daniel Dunbar82b22102009-05-18 21:47:54 +0000708 // FIXME: Clean this up if we move the phase sequence into the type.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000709 if (Ty != types::TY_Object) {
710 if (Args.hasArg(options::OPT_ObjC))
711 Ty = types::TY_ObjC;
712 else if (Args.hasArg(options::OPT_ObjCXX))
713 Ty = types::TY_ObjCXX;
714 }
715 } else {
716 assert(InputTypeArg && "InputType set w/o InputTypeArg");
717 InputTypeArg->claim();
718 Ty = InputType;
719 }
720
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000721 // Check that the file exists. It isn't clear this is worth doing, since
722 // the tool presumably does this anyway, and this just adds an extra stat
723 // to the equation, but this is gcc compatible.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000724 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbar5cd1e412009-03-12 09:13:48 +0000725 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000726 else
727 Inputs.push_back(std::make_pair(Ty, A));
728
729 } else if (A->getOption().isLinkerInput()) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000730 // Just treat as object type, we could make a special type for this if
731 // necessary.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000732 Inputs.push_back(std::make_pair(types::TY_Object, A));
733
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000734 } else if (A->getOption().matches(options::OPT_x)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000735 InputTypeArg = A;
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000736 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
737
738 // Follow gcc behavior and treat as linker input for invalid -x
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000739 // options. Its not clear why we shouldn't just revert to unknown; but
740 // this isn't very important, we might as well be bug comatible.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000741 if (!InputType) {
Daniel Dunbar5cd1e412009-03-12 09:13:48 +0000742 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000743 InputType = types::TY_Object;
744 }
745 }
746 }
747
Daniel Dunbar34c41872009-03-13 00:17:48 +0000748 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000749 Diag(clang::diag::err_drv_no_input_files);
750 return;
751 }
752
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000753 // Determine which compilation mode we are in. We look for options which
754 // affect the phase, starting with the earliest phases, and record which
755 // option we used to determine the final phase.
Daniel Dunbar65229332009-03-13 11:38:42 +0000756 Arg *FinalPhaseArg = 0;
757 phases::ID FinalPhase;
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000758
759 // -{E,M,MM} only run the preprocessor.
Daniel Dunbar65229332009-03-13 11:38:42 +0000760 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
761 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
762 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
763 FinalPhase = phases::Preprocess;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000764
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +0000765 // -{fsyntax-only,-analyze,emit-ast,S} only run up to the compiler.
Daniel Dunbarae0e55e2009-03-15 00:48:16 +0000766 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
Daniel Dunbar5e051f92009-05-06 02:12:32 +0000767 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze,
768 options::OPT__analyze_auto)) ||
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +0000769 (FinalPhaseArg = Args.getLastArg(options::OPT_emit_ast)) ||
Daniel Dunbar65229332009-03-13 11:38:42 +0000770 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
771 FinalPhase = phases::Compile;
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000772
773 // -c only runs up to the assembler.
Daniel Dunbar65229332009-03-13 11:38:42 +0000774 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
775 FinalPhase = phases::Assemble;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000776
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000777 // Otherwise do everything.
778 } else
Daniel Dunbar65229332009-03-13 11:38:42 +0000779 FinalPhase = phases::Link;
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000780
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000781 // Reject -Z* at the top level, these options should never have been exposed
782 // by gcc.
Daniel Dunbardd765242009-03-26 16:12:09 +0000783 if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
Daniel Dunbar0e759942009-03-20 06:14:23 +0000784 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000785
Daniel Dunbar65229332009-03-13 11:38:42 +0000786 // Construct the actions to perform.
787 ActionList LinkerInputs;
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000788 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbar65229332009-03-13 11:38:42 +0000789 types::ID InputType = Inputs[i].first;
790 const Arg *InputArg = Inputs[i].second;
791
792 unsigned NumSteps = types::getNumCompilationPhases(InputType);
793 assert(NumSteps && "Invalid number of steps!");
794
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000795 // If the first step comes after the final phase we are doing as part of
796 // this compilation, warn the user about it.
Daniel Dunbar65229332009-03-13 11:38:42 +0000797 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
798 if (InitialPhase > FinalPhase) {
Daniel Dunbaradc5c7c2009-03-19 07:57:08 +0000799 // Claim here to avoid the more general unused warning.
800 InputArg->claim();
Daniel Dunbar07806ca2009-09-17 04:13:26 +0000801
802 // Special case '-E' warning on a previously preprocessed file to make
803 // more sense.
804 if (InitialPhase == phases::Compile && FinalPhase == phases::Preprocess &&
805 getPreprocessedType(InputType) == types::TY_INVALID)
806 Diag(clang::diag::warn_drv_preprocessed_input_file_unused)
807 << InputArg->getAsString(Args)
808 << FinalPhaseArg->getOption().getName();
809 else
810 Diag(clang::diag::warn_drv_input_file_unused)
811 << InputArg->getAsString(Args)
812 << getPhaseName(InitialPhase)
813 << FinalPhaseArg->getOption().getName();
Daniel Dunbar65229332009-03-13 11:38:42 +0000814 continue;
815 }
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000816
Daniel Dunbar65229332009-03-13 11:38:42 +0000817 // Build the pipeline for this file.
818 Action *Current = new InputAction(*InputArg, InputType);
819 for (unsigned i = 0; i != NumSteps; ++i) {
820 phases::ID Phase = types::getCompilationPhase(InputType, i);
821
822 // We are done if this step is past what the user requested.
823 if (Phase > FinalPhase)
824 break;
825
826 // Queue linker inputs.
827 if (Phase == phases::Link) {
828 assert(i + 1 == NumSteps && "linking must be final compilation step.");
829 LinkerInputs.push_back(Current);
830 Current = 0;
831 break;
832 }
833
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000834 // Some types skip the assembler phase (e.g., llvm-bc), but we can't
835 // encode this in the steps because the intermediate type depends on
836 // arguments. Just special case here.
Daniel Dunbar13864952009-03-24 20:17:30 +0000837 if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
838 continue;
839
Daniel Dunbar65229332009-03-13 11:38:42 +0000840 // Otherwise construct the appropriate action.
841 Current = ConstructPhaseAction(Args, Phase, Current);
842 if (Current->getType() == types::TY_Nothing)
843 break;
844 }
845
846 // If we ended with something, add to the output list.
847 if (Current)
848 Actions.push_back(Current);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000849 }
Daniel Dunbar65229332009-03-13 11:38:42 +0000850
851 // Add a link action if necessary.
852 if (!LinkerInputs.empty())
853 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
854}
855
856Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
857 Action *Input) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +0000858 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbar65229332009-03-13 11:38:42 +0000859 // Build the appropriate action.
860 switch (Phase) {
861 case phases::Link: assert(0 && "link action invalid here.");
862 case phases::Preprocess: {
Daniel Dunbard67a3222009-03-30 06:36:42 +0000863 types::ID OutputTy;
864 // -{M, MM} alter the output type.
865 if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) {
866 OutputTy = types::TY_Dependencies;
867 } else {
868 OutputTy = types::getPreprocessedType(Input->getType());
869 assert(OutputTy != types::TY_INVALID &&
870 "Cannot preprocess this input type!");
871 }
Daniel Dunbar65229332009-03-13 11:38:42 +0000872 return new PreprocessJobAction(Input, OutputTy);
873 }
874 case phases::Precompile:
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000875 return new PrecompileJobAction(Input, types::TY_PCH);
Daniel Dunbar65229332009-03-13 11:38:42 +0000876 case phases::Compile: {
877 if (Args.hasArg(options::OPT_fsyntax_only)) {
878 return new CompileJobAction(Input, types::TY_Nothing);
Daniel Dunbar5e051f92009-05-06 02:12:32 +0000879 } else if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) {
Daniel Dunbar65229332009-03-13 11:38:42 +0000880 return new AnalyzeJobAction(Input, types::TY_Plist);
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +0000881 } else if (Args.hasArg(options::OPT_emit_ast)) {
882 return new CompileJobAction(Input, types::TY_AST);
Daniel Dunbar13864952009-03-24 20:17:30 +0000883 } else if (Args.hasArg(options::OPT_emit_llvm) ||
884 Args.hasArg(options::OPT_flto) ||
885 Args.hasArg(options::OPT_O4)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000886 types::ID Output =
Daniel Dunbar65229332009-03-13 11:38:42 +0000887 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
888 return new CompileJobAction(Input, Output);
889 } else {
890 return new CompileJobAction(Input, types::TY_PP_Asm);
891 }
892 }
893 case phases::Assemble:
894 return new AssembleJobAction(Input, types::TY_Object);
895 }
896
897 assert(0 && "invalid phase in ConstructPhaseAction");
898 return 0;
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000899}
900
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000901void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +0000902 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbare75d8342009-03-16 06:56:51 +0000903 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
904 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +0000905
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000906 // FIXME: Pipes are forcibly disabled until we support executing them.
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +0000907 if (!CCCPrintBindings)
908 UsePipes = false;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000909
Daniel Dunbare75d8342009-03-16 06:56:51 +0000910 // -save-temps inhibits pipes.
911 if (SaveTemps && UsePipes) {
912 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
913 UsePipes = true;
914 }
915
916 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
917
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000918 // It is an error to provide a -o option if we are making multiple output
919 // files.
Daniel Dunbare75d8342009-03-16 06:56:51 +0000920 if (FinalOutput) {
921 unsigned NumOutputs = 0;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000922 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000923 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbare75d8342009-03-16 06:56:51 +0000924 if ((*it)->getType() != types::TY_Nothing)
925 ++NumOutputs;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000926
Daniel Dunbare75d8342009-03-16 06:56:51 +0000927 if (NumOutputs > 1) {
928 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
929 FinalOutput = 0;
930 }
931 }
932
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000933 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000934 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbare75d8342009-03-16 06:56:51 +0000935 Action *A = *it;
936
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000937 // If we are linking an image for multiple archs then the linker wants
938 // -arch_multiple and -final_output <final image name>. Unfortunately, this
939 // doesn't fit in cleanly because we have to pass this information down.
Daniel Dunbare75d8342009-03-16 06:56:51 +0000940 //
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000941 // FIXME: This is a hack; find a cleaner way to integrate this into the
942 // process.
Daniel Dunbare75d8342009-03-16 06:56:51 +0000943 const char *LinkingOutput = 0;
Daniel Dunbardd765242009-03-26 16:12:09 +0000944 if (isa<LipoJobAction>(A)) {
Daniel Dunbare75d8342009-03-16 06:56:51 +0000945 if (FinalOutput)
946 LinkingOutput = FinalOutput->getValue(C.getArgs());
947 else
948 LinkingOutput = DefaultImageName.c_str();
949 }
950
951 InputInfo II;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000952 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +0000953 /*BoundArch*/0,
Daniel Dunbare75d8342009-03-16 06:56:51 +0000954 /*CanAcceptPipe*/ true,
955 /*AtTopLevel*/ true,
956 /*LinkingOutput*/ LinkingOutput,
957 II);
958 }
Daniel Dunbar3ce436d2009-03-16 06:42:30 +0000959
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000960 // If the user passed -Qunused-arguments or there were errors, don't warn
961 // about any unused arguments.
962 if (Diags.getNumErrors() ||
Daniel Dunbara3cfbe32009-04-07 19:04:18 +0000963 C.getArgs().hasArg(options::OPT_Qunused_arguments))
Daniel Dunbard175d972009-03-18 18:03:46 +0000964 return;
965
Daniel Dunbar58399ae2009-03-29 22:24:54 +0000966 // Claim -### here.
967 (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000968
Daniel Dunbar3ce436d2009-03-16 06:42:30 +0000969 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
970 it != ie; ++it) {
971 Arg *A = *it;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000972
Daniel Dunbar3ce436d2009-03-16 06:42:30 +0000973 // FIXME: It would be nice to be able to send the argument to the
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000974 // Diagnostic, so that extra values, position, and so on could be printed.
Daniel Dunbar90dd6f42009-04-04 00:52:26 +0000975 if (!A->isClaimed()) {
Daniel Dunbara3cfbe32009-04-07 19:04:18 +0000976 if (A->getOption().hasNoArgumentUnused())
977 continue;
978
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000979 // Suppress the warning automatically if this is just a flag, and it is an
980 // instance of an argument we already claimed.
Daniel Dunbar90dd6f42009-04-04 00:52:26 +0000981 const Option &Opt = A->getOption();
982 if (isa<FlagOption>(Opt)) {
983 bool DuplicateClaimed = false;
984
985 // FIXME: Use iterator.
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000986 for (ArgList::const_iterator it = C.getArgs().begin(),
Daniel Dunbar90dd6f42009-04-04 00:52:26 +0000987 ie = C.getArgs().end(); it != ie; ++it) {
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000988 if ((*it)->isClaimed() && (*it)->getOption().matches(&Opt)) {
Daniel Dunbar90dd6f42009-04-04 00:52:26 +0000989 DuplicateClaimed = true;
990 break;
991 }
992 }
993
994 if (DuplicateClaimed)
995 continue;
996 }
997
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000998 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar0e759942009-03-20 06:14:23 +0000999 << A->getAsString(C.getArgs());
Daniel Dunbar90dd6f42009-04-04 00:52:26 +00001000 }
Daniel Dunbar3ce436d2009-03-16 06:42:30 +00001001 }
Daniel Dunbar95e6b192009-03-13 22:12:33 +00001002}
1003
Daniel Dunbare75d8342009-03-16 06:56:51 +00001004void Driver::BuildJobsForAction(Compilation &C,
1005 const Action *A,
1006 const ToolChain *TC,
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +00001007 const char *BoundArch,
Daniel Dunbare75d8342009-03-16 06:56:51 +00001008 bool CanAcceptPipe,
1009 bool AtTopLevel,
1010 const char *LinkingOutput,
1011 InputInfo &Result) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001012 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +00001013
1014 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001015 // FIXME: Pipes are forcibly disabled until we support executing them.
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +00001016 if (!CCCPrintBindings)
1017 UsePipes = false;
1018
Daniel Dunbare75d8342009-03-16 06:56:51 +00001019 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001020 // FIXME: It would be nice to not claim this here; maybe the old scheme of
1021 // just using Args was better?
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00001022 const Arg &Input = IA->getInputArg();
1023 Input.claim();
1024 if (isa<PositionalArg>(Input)) {
1025 const char *Name = Input.getValue(C.getArgs());
1026 Result = InputInfo(Name, A->getType(), Name);
1027 } else
1028 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbare75d8342009-03-16 06:56:51 +00001029 return;
1030 }
1031
1032 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar1ef3f2a2009-09-08 23:37:19 +00001033 const ToolChain *TC = &C.getDefaultToolChain();
1034
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001035 std::string Arch;
Daniel Dunbar1ef3f2a2009-09-08 23:37:19 +00001036 if (BAA->getArchName())
1037 TC = Host->CreateToolChain(C.getArgs(), BAA->getArchName());
1038
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +00001039 BuildJobsForAction(C, *BAA->begin(), TC, BAA->getArchName(),
1040 CanAcceptPipe, AtTopLevel, LinkingOutput, Result);
Daniel Dunbare75d8342009-03-16 06:56:51 +00001041 return;
1042 }
1043
1044 const JobAction *JA = cast<JobAction>(A);
1045 const Tool &T = TC->SelectTool(C, *JA);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001046
1047 // See if we should use an integrated preprocessor. We do so when we have
1048 // exactly one input, since this is the only use case we care about
1049 // (irrelevant since we don't support combine yet).
Daniel Dunbare75d8342009-03-16 06:56:51 +00001050 bool UseIntegratedCPP = false;
1051 const ActionList *Inputs = &A->getInputs();
1052 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
1053 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
1054 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
1055 !C.getArgs().hasArg(options::OPT_save_temps) &&
1056 T.hasIntegratedCPP()) {
1057 UseIntegratedCPP = true;
1058 Inputs = &(*Inputs)[0]->getInputs();
1059 }
1060 }
1061
1062 // Only use pipes when there is exactly one input.
1063 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001064 InputInfoList InputInfos;
Daniel Dunbare75d8342009-03-16 06:56:51 +00001065 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
1066 it != ie; ++it) {
1067 InputInfo II;
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +00001068 BuildJobsForAction(C, *it, TC, BoundArch, TryToUsePipeInput,
1069 /*AtTopLevel*/false, LinkingOutput, II);
Daniel Dunbare75d8342009-03-16 06:56:51 +00001070 InputInfos.push_back(II);
1071 }
1072
1073 // Determine if we should output to a pipe.
1074 bool OutputToPipe = false;
1075 if (CanAcceptPipe && T.canPipeOutput()) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001076 // Some actions default to writing to a pipe if they are the top level phase
1077 // and there was no user override.
Daniel Dunbare75d8342009-03-16 06:56:51 +00001078 //
1079 // FIXME: Is there a better way to handle this?
1080 if (AtTopLevel) {
1081 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
1082 OutputToPipe = true;
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +00001083 } else if (UsePipes)
Daniel Dunbare75d8342009-03-16 06:56:51 +00001084 OutputToPipe = true;
1085 }
1086
1087 // Figure out where to put the job (pipes).
1088 Job *Dest = &C.getJobs();
1089 if (InputInfos[0].isPipe()) {
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001090 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbare75d8342009-03-16 06:56:51 +00001091 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
1092 Dest = &InputInfos[0].getPipe();
1093 }
1094
1095 // Always use the first input as the base input.
1096 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001097
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001098 // Determine the place to write output to (nothing, pipe, or filename) and
1099 // where to put the new job.
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001100 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbarb39cc522009-03-17 22:47:06 +00001101 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001102 } else if (OutputToPipe) {
1103 // Append to current piped job or create a new one as appropriate.
Daniel Dunbarb39cc522009-03-17 22:47:06 +00001104 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
1105 if (!PJ) {
1106 PJ = new PipedJob();
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001107 // FIXME: Temporary hack so that -ccc-print-bindings work until we have
1108 // pipe support. Please remove later.
Daniel Dunbar1fc898c2009-03-20 00:11:04 +00001109 if (!CCCPrintBindings)
1110 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +00001111 Dest = PJ;
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001112 }
Daniel Dunbarb39cc522009-03-17 22:47:06 +00001113 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001114 } else {
Daniel Dunbarb39cc522009-03-17 22:47:06 +00001115 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
1116 A->getType(), BaseInput);
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001117 }
1118
Daniel Dunbarb39cc522009-03-17 22:47:06 +00001119 if (CCCPrintBindings) {
Daniel Dunbard67a3222009-03-30 06:36:42 +00001120 llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"'
1121 << " - \"" << T.getName() << "\", inputs: [";
Daniel Dunbarb39cc522009-03-17 22:47:06 +00001122 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
1123 llvm::errs() << InputInfos[i].getAsString();
1124 if (i + 1 != e)
1125 llvm::errs() << ", ";
1126 }
1127 llvm::errs() << "], output: " << Result.getAsString() << "\n";
1128 } else {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001129 T.ConstructJob(C, *JA, *Dest, Result, InputInfos,
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +00001130 C.getArgsForToolChain(TC, BoundArch), LinkingOutput);
Daniel Dunbarb39cc522009-03-17 22:47:06 +00001131 }
Daniel Dunbare75d8342009-03-16 06:56:51 +00001132}
1133
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001134const char *Driver::GetNamedOutputPath(Compilation &C,
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001135 const JobAction &JA,
1136 const char *BaseInput,
1137 bool AtTopLevel) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +00001138 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001139 // Output to a user requested destination?
1140 if (AtTopLevel) {
1141 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
1142 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
1143 }
1144
1145 // Output to a temporary file?
1146 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001147 std::string TmpName =
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001148 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
1149 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001150 }
1151
1152 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbard00978b2009-03-18 02:00:31 +00001153 std::string BaseName(BasePath.getLast());
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001154
1155 // Determine what the derived output name should be.
1156 const char *NamedOutput;
1157 if (JA.getType() == types::TY_Image) {
1158 NamedOutput = DefaultImageName.c_str();
1159 } else {
1160 const char *Suffix = types::getTypeTempSuffix(JA.getType());
1161 assert(Suffix && "All types used for output should have a suffix.");
1162
1163 std::string::size_type End = std::string::npos;
1164 if (!types::appendSuffixForType(JA.getType()))
1165 End = BaseName.rfind('.');
1166 std::string Suffixed(BaseName.substr(0, End));
1167 Suffixed += '.';
1168 Suffixed += Suffix;
1169 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
1170 }
1171
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001172 // As an annoying special case, PCH generation doesn't strip the pathname.
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001173 if (JA.getType() == types::TY_PCH) {
1174 BasePath.eraseComponent();
Daniel Dunbare6c83192009-03-18 09:58:30 +00001175 if (BasePath.isEmpty())
1176 BasePath = NamedOutput;
1177 else
1178 BasePath.appendComponent(NamedOutput);
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001179 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
1180 } else {
1181 return C.addResultFile(NamedOutput);
1182 }
1183}
1184
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001185std::string Driver::GetFilePath(const char *Name, const ToolChain &TC) const {
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001186 const ToolChain::path_list &List = TC.getFilePaths();
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001187 for (ToolChain::path_list::const_iterator
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001188 it = List.begin(), ie = List.end(); it != ie; ++it) {
1189 llvm::sys::Path P(*it);
1190 P.appendComponent(Name);
1191 if (P.exists())
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001192 return P.str();
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001193 }
1194
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001195 return Name;
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +00001196}
1197
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001198std::string Driver::GetProgramPath(const char *Name, const ToolChain &TC,
1199 bool WantFile) const {
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001200 const ToolChain::path_list &List = TC.getProgramPaths();
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001201 for (ToolChain::path_list::const_iterator
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001202 it = List.begin(), ie = List.end(); it != ie; ++it) {
1203 llvm::sys::Path P(*it);
1204 P.appendComponent(Name);
Mike Stumpdb657372009-03-27 00:40:20 +00001205 if (WantFile ? P.exists() : P.canExecute())
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001206 return P.str();
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001207 }
1208
Daniel Dunbar76ce7412009-03-23 16:15:50 +00001209 // If all else failed, search the path.
1210 llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
Daniel Dunbar6f668772009-03-18 21:34:08 +00001211 if (!P.empty())
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001212 return P.str();
Daniel Dunbar6f668772009-03-18 21:34:08 +00001213
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001214 return Name;
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +00001215}
1216
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001217std::string Driver::GetTemporaryPath(const char *Suffix) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001218 // FIXME: This is lame; sys::Path should provide this function (in particular,
1219 // it should know how to find the temporary files dir).
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001220 std::string Error;
Daniel Dunbarc35694d2009-04-20 20:28:21 +00001221 const char *TmpDir = ::getenv("TMPDIR");
1222 if (!TmpDir)
1223 TmpDir = ::getenv("TEMP");
1224 if (!TmpDir)
Daniel Dunbar712e4de2009-04-21 00:25:10 +00001225 TmpDir = ::getenv("TMP");
1226 if (!TmpDir)
Daniel Dunbarc35694d2009-04-20 20:28:21 +00001227 TmpDir = "/tmp";
1228 llvm::sys::Path P(TmpDir);
Daniel Dunbarab8ce7c2009-04-20 17:32:49 +00001229 P.appendComponent("cc");
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001230 if (P.makeUnique(false, &Error)) {
1231 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
1232 return "";
1233 }
1234
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001235 // FIXME: Grumble, makeUnique sometimes leaves the file around!? PR3837.
Daniel Dunbar18d69de2009-03-18 23:08:52 +00001236 P.eraseFromDisk(false, 0);
1237
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001238 P.appendSuffix(Suffix);
Chris Lattner3441b4f2009-08-23 22:45:33 +00001239 return P.str();
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001240}
1241
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001242const HostInfo *Driver::GetHostInfo(const char *TripleStr) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +00001243 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001244 llvm::Triple Triple(TripleStr);
Daniel Dunbar4dff6a42009-03-10 23:41:59 +00001245
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001246 switch (Triple.getOS()) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001247 case llvm::Triple::AuroraUX:
1248 return createAuroraUXHostInfo(*this, Triple);
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001249 case llvm::Triple::Darwin:
1250 return createDarwinHostInfo(*this, Triple);
1251 case llvm::Triple::DragonFly:
1252 return createDragonFlyHostInfo(*this, Triple);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001253 case llvm::Triple::OpenBSD:
1254 return createOpenBSDHostInfo(*this, Triple);
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001255 case llvm::Triple::FreeBSD:
1256 return createFreeBSDHostInfo(*this, Triple);
Eli Friedman5cd659f2009-05-26 07:52:18 +00001257 case llvm::Triple::Linux:
1258 return createLinuxHostInfo(*this, Triple);
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001259 default:
1260 return createUnknownHostInfo(*this, Triple);
1261 }
Daniel Dunbar4dff6a42009-03-10 23:41:59 +00001262}
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001263
1264bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
Daniel Dunbar953b8d12009-09-08 23:36:55 +00001265 const llvm::Triple &Triple) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001266 // Check if user requested no clang, or clang doesn't understand this type (we
1267 // only handle single inputs for now).
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00001268 if (!CCCUseClang || JA.size() != 1 ||
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001269 !types::isAcceptedByClang((*JA.begin())->getType()))
1270 return false;
1271
Daniel Dunbar88f356e2009-03-24 19:02:31 +00001272 // Otherwise make sure this is an action clang understands.
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001273 if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001274 if (!CCCUseClangCPP) {
1275 Diag(clang::diag::warn_drv_not_using_clang_cpp);
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001276 return false;
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001277 }
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001278 } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
1279 return false;
1280
Daniel Dunbar88f356e2009-03-24 19:02:31 +00001281 // Use clang for C++?
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001282 if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
1283 Diag(clang::diag::warn_drv_not_using_clang_cxx);
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001284 return false;
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001285 }
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001286
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001287 // Always use clang for precompiling and AST generation, regardless of archs.
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00001288 if (isa<PrecompileJobAction>(JA) || JA.getType() == types::TY_AST)
Daniel Dunbarf2df7c22009-04-16 23:10:13 +00001289 return true;
1290
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001291 // Finally, don't use clang if this isn't one of the user specified archs to
1292 // build.
Daniel Dunbar953b8d12009-09-08 23:36:55 +00001293 if (!CCCClangArchs.empty() && !CCCClangArchs.count(Triple.getArch())) {
1294 Diag(clang::diag::warn_drv_not_using_clang_arch) << Triple.getArchName();
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001295 return false;
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001296 }
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001297
1298 return true;
1299}
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001300
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001301/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the
1302/// grouped values as integers. Numbers which are not provided are set to 0.
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001303///
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001304/// \return True if the entire string was parsed (9.2), or all groups were
1305/// parsed (10.3.5extrastuff).
1306bool Driver::GetReleaseVersion(const char *Str, unsigned &Major,
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001307 unsigned &Minor, unsigned &Micro,
1308 bool &HadExtra) {
1309 HadExtra = false;
1310
1311 Major = Minor = Micro = 0;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001312 if (*Str == '\0')
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001313 return true;
1314
1315 char *End;
1316 Major = (unsigned) strtol(Str, &End, 10);
1317 if (*Str != '\0' && *End == '\0')
1318 return true;
1319 if (*End != '.')
1320 return false;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001321
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001322 Str = End+1;
1323 Minor = (unsigned) strtol(Str, &End, 10);
1324 if (*Str != '\0' && *End == '\0')
1325 return true;
1326 if (*End != '.')
1327 return false;
1328
1329 Str = End+1;
1330 Micro = (unsigned) strtol(Str, &End, 10);
1331 if (*Str != '\0' && *End == '\0')
1332 return true;
1333 if (Str == End)
1334 return false;
1335 HadExtra = true;
1336 return true;
1337}