blob: 93fbe54ebe3979276a431ff323b56cd1657be1c1 [file] [log] [blame]
Daniel Dunbar63c4da92009-03-02 19:59:07 +00001//===--- Driver.cpp - Clang GCC Compatible Driver -----------------------*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Dunbar63c4da92009-03-02 19:59:07 +000010#include "clang/Driver/Driver.h"
Daniel Dunbar63c4da92009-03-02 19:59:07 +000011
Daniel Dunbardb62cc32009-03-12 07:58:46 +000012#include "clang/Driver/Action.h"
Daniel Dunbard6f0e372009-03-04 20:49:20 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
15#include "clang/Driver/Compilation.h"
Daniel Dunbar93468492009-03-12 08:55:43 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbard25acaa2009-03-10 23:41:59 +000017#include "clang/Driver/HostInfo.h"
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000018#include "clang/Driver/Job.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000019#include "clang/Driver/Option.h"
Daniel Dunbard6f0e372009-03-04 20:49:20 +000020#include "clang/Driver/Options.h"
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000021#include "clang/Driver/Tool.h"
22#include "clang/Driver/ToolChain.h"
Daniel Dunbardb62cc32009-03-12 07:58:46 +000023#include "clang/Driver/Types.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000024
Douglas Gregorb7064742009-04-27 22:23:34 +000025#include "clang/Basic/Version.h"
26
Daniel Dunbarb1873cd2009-03-13 20:33:35 +000027#include "llvm/ADT/StringSet.h"
Daniel Dunbar16e04ff2009-03-18 01:38:48 +000028#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000029#include "llvm/Support/raw_ostream.h"
Daniel Dunbardb62cc32009-03-12 07:58:46 +000030#include "llvm/System/Path.h"
Daniel Dunbarcb84b9a2009-03-18 21:34:08 +000031#include "llvm/System/Program.h"
Daniel Dunbar494646b2009-03-13 12:19:02 +000032
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000033#include "InputInfo.h"
34
Daniel Dunbar494646b2009-03-13 12:19:02 +000035#include <map>
36
Daniel Dunbard6f0e372009-03-04 20:49:20 +000037using namespace clang::driver;
Chris Lattnerc272d262009-03-26 05:56:24 +000038using namespace clang;
Daniel Dunbard6f0e372009-03-04 20:49:20 +000039
Daniel Dunbar89301fb2009-08-23 18:42:54 +000040// Used to set values for "production" clang, for releases.
Daniel Dunbard83eaa02009-08-23 19:41:53 +000041// #define USE_PRODUCTION_CLANG
Daniel Dunbar89301fb2009-08-23 18:42:54 +000042
Daniel Dunbard25acaa2009-03-10 23:41:59 +000043Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar93468492009-03-12 08:55:43 +000044 const char *_DefaultHostTriple,
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000045 const char *_DefaultImageName,
Daniel Dunbar84f695a2009-09-08 23:36:43 +000046 Diagnostic &_Diags)
47 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbard25acaa2009-03-10 23:41:59 +000048 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000049 DefaultImageName(_DefaultImageName),
Daniel Dunbard25acaa2009-03-10 23:41:59 +000050 Host(0),
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +000051 CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false),
Daniel Dunbar89301fb2009-08-23 18:42:54 +000052 CCCGenericGCCName("gcc"), CCCUseClang(true),
53#ifdef USE_PRODUCTION_CLANG
Daniel Dunbar84f695a2009-09-08 23:36:43 +000054 CCCUseClangCXX(false),
Daniel Dunbar89301fb2009-08-23 18:42:54 +000055#else
Daniel Dunbar84f695a2009-09-08 23:36:43 +000056 CCCUseClangCXX(true),
Daniel Dunbar89301fb2009-08-23 18:42:54 +000057#endif
Douglas Gregorf93696d2009-04-28 22:44:02 +000058 CCCUseClangCPP(true), CCCUsePCH(true),
Daniel Dunbar5a5ec5c2009-03-13 00:17:48 +000059 SuppressMissingInputWarning(false)
Daniel Dunbarb282ced2009-03-10 20:52:46 +000060{
Daniel Dunbar89301fb2009-08-23 18:42:54 +000061#ifdef USE_PRODUCTION_CLANG
Daniel Dunbar84f695a2009-09-08 23:36:43 +000062 // In a "production" build, only use clang on architectures we expect to work.
Daniel Dunbar89301fb2009-08-23 18:42:54 +000063 CCCClangArchs.insert("i386");
64 CCCClangArchs.insert("x86_64");
Mike Stumpdf96c8f2009-09-03 01:30:36 +000065 CCCClangArchs.insert("arm");
66 CCCClangArchs.insert("armv5");
67 CCCClangArchs.insert("armv6");
68 CCCClangArchs.insert("armv7");
Daniel Dunbar89301fb2009-08-23 18:42:54 +000069#endif
Daniel Dunbar63c4da92009-03-02 19:59:07 +000070}
71
72Driver::~Driver() {
Daniel Dunbard6f0e372009-03-04 20:49:20 +000073 delete Opts;
Daniel Dunbar2f8b37e2009-03-18 01:09:40 +000074 delete Host;
Daniel Dunbar63c4da92009-03-02 19:59:07 +000075}
76
Daniel Dunbar84f695a2009-09-08 23:36:43 +000077InputArgList *Driver::ParseArgStrings(const char **ArgBegin,
Daniel Dunbara16e4fe2009-03-25 04:13:45 +000078 const char **ArgEnd) {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +000079 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbara16e4fe2009-03-25 04:13:45 +000080 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
Daniel Dunbar84f695a2009-09-08 23:36:43 +000081
Daniel Dunbar85cb3592009-03-13 11:38:42 +000082 // FIXME: Handle '@' args (or at least error on them).
83
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000084 unsigned Index = 0, End = ArgEnd - ArgBegin;
85 while (Index < End) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +000086 // gcc's handling of empty arguments doesn't make sense, but this is not a
87 // common use case. :)
Daniel Dunbarb043ebd2009-03-13 01:01:44 +000088 //
Daniel Dunbar84f695a2009-09-08 23:36:43 +000089 // We just ignore them here (note that other things may still take them as
90 // arguments).
Daniel Dunbarb043ebd2009-03-13 01:01:44 +000091 if (Args->getArgString(Index)[0] == '\0') {
92 ++Index;
93 continue;
94 }
95
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000096 unsigned Prev = Index;
Daniel Dunbarfb88ce02009-03-22 23:26:43 +000097 Arg *A = getOpts().ParseOneArg(*Args, Index);
98 assert(Index > Prev && "Parser failed to consume argument.");
Daniel Dunbardb62cc32009-03-12 07:58:46 +000099
Daniel Dunbarfb88ce02009-03-22 23:26:43 +0000100 // Check for missing argument error.
101 if (!A) {
102 assert(Index >= End && "Unexpected parser error.");
103 Diag(clang::diag::err_drv_missing_argument)
104 << Args->getArgString(Prev)
105 << (Index - Prev - 1);
106 break;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000107 }
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000108
Daniel Dunbarfb88ce02009-03-22 23:26:43 +0000109 if (A->getOption().isUnsupported()) {
110 Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
111 continue;
112 }
113 Args->append(A);
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000114 }
115
116 return Args;
117}
118
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000119Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000120 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
121
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000122 // FIXME: Handle environment options which effect driver behavior, somewhere
123 // (client?). GCC_EXEC_PREFIX, COMPILER_PATH, LIBRARY_PATH, LPATH,
124 // CC_PRINT_OPTIONS.
Daniel Dunbarcc006892009-03-13 00:51:18 +0000125
126 // FIXME: What are we going to do with -V and -b?
127
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000128 // FIXME: This stuff needs to go into the Compilation, not the driver.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000129 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000130
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000131 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000132 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000133
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000134 // Read -ccc args.
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000135 //
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000136 // FIXME: We need to figure out where this behavior should live. Most of it
137 // should be outside in the client; the parts that aren't should have proper
138 // options, either by introducing new ones or by overloading gcc ones like -V
139 // or -b.
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000140 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
141 const char *Opt = *Start + 5;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000142
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000143 if (!strcmp(Opt, "print-options")) {
144 CCCPrintOptions = true;
145 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000146 CCCPrintActions = true;
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000147 } else if (!strcmp(Opt, "print-bindings")) {
148 CCCPrintBindings = true;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000149 } else if (!strcmp(Opt, "cxx")) {
150 CCCIsCXX = true;
151 } else if (!strcmp(Opt, "echo")) {
152 CCCEcho = true;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000153
Daniel Dunbar126da5e2009-04-01 23:34:41 +0000154 } else if (!strcmp(Opt, "gcc-name")) {
155 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
156 CCCGenericGCCName = *++Start;
157
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000158 } else if (!strcmp(Opt, "clang-cxx")) {
159 CCCUseClangCXX = true;
Daniel Dunbar422ce672009-07-23 17:48:59 +0000160 } else if (!strcmp(Opt, "no-clang-cxx")) {
161 CCCUseClangCXX = false;
Douglas Gregor95734502009-04-18 00:34:01 +0000162 } else if (!strcmp(Opt, "pch-is-pch")) {
163 CCCUsePCH = true;
164 } else if (!strcmp(Opt, "pch-is-pth")) {
165 CCCUsePCH = false;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000166 } else if (!strcmp(Opt, "no-clang")) {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000167 CCCUseClang = false;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000168 } else if (!strcmp(Opt, "no-clang-cpp")) {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000169 CCCUseClangCPP = false;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000170 } else if (!strcmp(Opt, "clang-archs")) {
171 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
172 const char *Cur = *++Start;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000173
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000174 CCCClangArchs.clear();
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000175 for (;;) {
176 const char *Next = strchr(Cur, ',');
177
178 if (Next) {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000179 if (Cur != Next)
180 CCCClangArchs.insert(std::string(Cur, Next));
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000181 Cur = Next + 1;
182 } else {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000183 if (*Cur != '\0')
184 CCCClangArchs.insert(std::string(Cur));
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000185 break;
186 }
187 }
188
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000189 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000190 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000191 HostTriple = *++Start;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000192
Daniel Dunbarc0cc8712009-09-04 18:35:03 +0000193 } else if (!strcmp(Opt, "install-dir")) {
194 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
195 Dir = *++Start;
196
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000197 } else {
198 // FIXME: Error handling.
199 llvm::errs() << "invalid option: " << *Start << "\n";
200 exit(1);
201 }
202 }
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000203
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000204 InputArgList *Args = ParseArgStrings(Start, End);
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000205
Daniel Dunbar08966ca2009-03-17 20:45:45 +0000206 Host = GetHostInfo(HostTriple);
Daniel Dunbarcc006892009-03-13 00:51:18 +0000207
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000208 // The compilation takes ownership of Args.
Daniel Dunbar31a76e32009-03-18 22:16:03 +0000209 Compilation *C = new Compilation(*this, *Host->getToolChain(*Args), Args);
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000210
211 // FIXME: This behavior shouldn't be here.
212 if (CCCPrintOptions) {
213 PrintOptions(C->getArgs());
214 return C;
215 }
216
217 if (!HandleImmediateArgs(*C))
218 return C;
219
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000220 // Construct the list of abstract actions to perform for this compilation. We
221 // avoid passing a Compilation here simply to enforce the abstraction that
222 // pipelining is not host or toolchain dependent (other than the driver driver
223 // test).
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000224 if (Host->useDriverDriver())
225 BuildUniversalActions(C->getArgs(), C->getActions());
226 else
227 BuildActions(C->getArgs(), C->getActions());
228
229 if (CCCPrintActions) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000230 PrintActions(*C);
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000231 return C;
232 }
233
234 BuildJobs(*C);
Daniel Dunbarc413f822009-03-15 01:38:15 +0000235
236 return C;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000237}
238
Daniel Dunbar9fbdc882009-07-01 20:03:04 +0000239int Driver::ExecuteCompilation(const Compilation &C) const {
240 // Just print if -### was present.
241 if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
242 C.PrintJob(llvm::errs(), C.getJobs(), "\n", true);
243 return 0;
244 }
245
246 // If there were errors building the compilation, quit now.
247 if (getDiags().getNumErrors())
248 return 1;
249
250 const Command *FailingCommand = 0;
251 int Res = C.ExecuteJob(C.getJobs(), FailingCommand);
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000252
Daniel Dunbar9fbdc882009-07-01 20:03:04 +0000253 // Remove temp files.
254 C.CleanupFileList(C.getTempFiles());
255
256 // If the compilation failed, remove result files as well.
257 if (Res != 0 && !C.getArgs().hasArg(options::OPT_save_temps))
258 C.CleanupFileList(C.getResultFiles(), true);
259
260 // Print extra information about abnormal failures, if possible.
261 if (Res) {
262 // This is ad-hoc, but we don't want to be excessively noisy. If the result
263 // status was 1, assume the command failed normally. In particular, if it
264 // was the compiler then assume it gave a reasonable error code. Failures in
265 // other tools are less common, and they generally have worse diagnostics,
266 // so always print the diagnostic there.
267 const Action &Source = FailingCommand->getSource();
268 bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) ||
269 isa<PrecompileJobAction>(Source) ||
270 isa<AnalyzeJobAction>(Source) ||
271 isa<CompileJobAction>(Source));
272
273 if (!IsFriendlyTool || Res != 1) {
274 // FIXME: See FIXME above regarding result code interpretation.
275 if (Res < 0)
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000276 Diag(clang::diag::err_drv_command_signalled)
Daniel Dunbar9fbdc882009-07-01 20:03:04 +0000277 << Source.getClassName() << -Res;
278 else
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000279 Diag(clang::diag::err_drv_command_failed)
Daniel Dunbar9fbdc882009-07-01 20:03:04 +0000280 << Source.getClassName() << Res;
281 }
282 }
283
284 return Res;
285}
286
Daniel Dunbara790d372009-03-12 18:24:49 +0000287void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000288 unsigned i = 0;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000289 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000290 it != ie; ++it, ++i) {
291 Arg *A = *it;
292 llvm::errs() << "Option " << i << " - "
293 << "Name: \"" << A->getOption().getName() << "\", "
294 << "Values: {";
295 for (unsigned j = 0; j < A->getNumValues(); ++j) {
296 if (j)
297 llvm::errs() << ", ";
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000298 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000299 }
300 llvm::errs() << "}\n";
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000301 }
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000302}
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000303
Daniel Dunbare627b682009-03-31 21:38:17 +0000304static std::string getOptionHelpName(const OptTable &Opts, options::ID Id) {
305 std::string Name = Opts.getOptionName(Id);
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000306
Daniel Dunbare627b682009-03-31 21:38:17 +0000307 // Add metavar, if used.
308 switch (Opts.getOptionKind(Id)) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000309 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
Daniel Dunbare627b682009-03-31 21:38:17 +0000310 assert(0 && "Invalid option with help text.");
311
312 case Option::MultiArgClass: case Option::JoinedAndSeparateClass:
313 assert(0 && "Cannot print metavar for this kind of option.");
314
315 case Option::FlagClass:
316 break;
317
318 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
319 Name += ' ';
320 // FALLTHROUGH
321 case Option::JoinedClass: case Option::CommaJoinedClass:
322 Name += Opts.getOptionMetaVar(Id);
323 break;
324 }
325
326 return Name;
327}
328
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000329void Driver::PrintHelp(bool ShowHidden) const {
Daniel Dunbare627b682009-03-31 21:38:17 +0000330 llvm::raw_ostream &OS = llvm::outs();
331
332 OS << "OVERVIEW: clang \"gcc-compatible\" driver\n";
333 OS << '\n';
334 OS << "USAGE: " << Name << " [options] <input files>\n";
335 OS << '\n';
336 OS << "OPTIONS:\n";
337
338 // Render help text into (option, help) pairs.
339 std::vector< std::pair<std::string, const char*> > OptionHelp;
340
341 for (unsigned i = options::OPT_INPUT, e = options::LastOption; i != e; ++i) {
342 options::ID Id = (options::ID) i;
343 if (const char *Text = getOpts().getOptionHelpText(Id))
344 OptionHelp.push_back(std::make_pair(getOptionHelpName(getOpts(), Id),
345 Text));
346 }
347
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000348 if (ShowHidden) {
349 OptionHelp.push_back(std::make_pair("\nDRIVER OPTIONS:",""));
350 OptionHelp.push_back(std::make_pair("-ccc-cxx",
351 "Act as a C++ driver"));
352 OptionHelp.push_back(std::make_pair("-ccc-gcc-name",
353 "Name for native GCC compiler"));
354 OptionHelp.push_back(std::make_pair("-ccc-clang-cxx",
Daniel Dunbarec438002009-09-01 16:57:46 +0000355 "Enable the clang compiler for C++"));
356 OptionHelp.push_back(std::make_pair("-ccc-no-clang-cxx",
357 "Disable the clang compiler for C++"));
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000358 OptionHelp.push_back(std::make_pair("-ccc-no-clang",
Daniel Dunbarec438002009-09-01 16:57:46 +0000359 "Disable the clang compiler"));
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000360 OptionHelp.push_back(std::make_pair("-ccc-no-clang-cpp",
Daniel Dunbarec438002009-09-01 16:57:46 +0000361 "Disable the clang preprocessor"));
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000362 OptionHelp.push_back(std::make_pair("-ccc-clang-archs",
363 "Comma separate list of architectures "
364 "to use the clang compiler for"));
Douglas Gregor95734502009-04-18 00:34:01 +0000365 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pch",
366 "Use lazy PCH for precompiled headers"));
367 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pth",
368 "Use pretokenized headers for precompiled headers"));
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000369
370 OptionHelp.push_back(std::make_pair("\nDEBUG/DEVELOPMENT OPTIONS:",""));
371 OptionHelp.push_back(std::make_pair("-ccc-host-triple",
Daniel Dunbarc0cc8712009-09-04 18:35:03 +0000372 "Simulate running on the given target"));
373 OptionHelp.push_back(std::make_pair("-ccc-install-dir",
374 "Simulate installation in the given directory"));
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000375 OptionHelp.push_back(std::make_pair("-ccc-print-options",
376 "Dump parsed command line arguments"));
377 OptionHelp.push_back(std::make_pair("-ccc-print-phases",
378 "Dump list of actions to perform"));
379 OptionHelp.push_back(std::make_pair("-ccc-print-bindings",
380 "Show bindings of tools to actions"));
381 OptionHelp.push_back(std::make_pair("CCC_ADD_ARGS",
382 "(ENVIRONMENT VARIABLE) Comma separated list of "
383 "arguments to prepend to the command line"));
384 }
385
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000386 // Find the maximum option length.
Daniel Dunbare627b682009-03-31 21:38:17 +0000387 unsigned OptionFieldWidth = 0;
388 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000389 // Skip titles.
390 if (!OptionHelp[i].second)
391 continue;
392
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000393 // Limit the amount of padding we are willing to give up for alignment.
Daniel Dunbare627b682009-03-31 21:38:17 +0000394 unsigned Length = OptionHelp[i].first.size();
395 if (Length <= 23)
396 OptionFieldWidth = std::max(OptionFieldWidth, Length);
397 }
398
399 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
400 const std::string &Option = OptionHelp[i].first;
401 OS << " " << Option;
402 for (int j = Option.length(), e = OptionFieldWidth; j < e; ++j)
403 OS << ' ';
404 OS << ' ' << OptionHelp[i].second << '\n';
405 }
406
407 OS.flush();
408}
409
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000410void Driver::PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const {
Mike Stump74d94472009-03-18 14:00:02 +0000411 static char buf[] = "$URL$";
412 char *zap = strstr(buf, "/lib/Driver");
413 if (zap)
414 *zap = 0;
415 zap = strstr(buf, "/clang/tools/clang");
416 if (zap)
417 *zap = 0;
Mike Stumpbc927292009-03-18 15:19:35 +0000418 const char *vers = buf+6;
Mike Stump3fc58f02009-03-18 18:45:55 +0000419 // FIXME: Add cmake support and remove #ifdef
420#ifdef SVN_REVISION
421 const char *revision = SVN_REVISION;
422#else
423 const char *revision = "";
424#endif
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000425 // FIXME: The following handlers should use a callback mechanism, we don't
426 // know what the client would like to do.
427 OS << "clang version " CLANG_VERSION_STRING " ("
428 << vers << " " << revision << ")" << '\n';
Daniel Dunbar0ded7de2009-03-26 16:09:13 +0000429
430 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000431 OS << "Target: " << TC.getTripleString() << '\n';
Daniel Dunbar41c34b32009-06-16 23:32:58 +0000432
433 // Print the threading model.
434 //
435 // FIXME: Implement correctly.
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000436 OS << "Thread model: " << "posix" << '\n';
Daniel Dunbarcc006892009-03-13 00:51:18 +0000437}
438
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000439bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000440 // The order these options are handled in in gcc is all over the place, but we
441 // don't expect inconsistencies w.r.t. that to matter in practice.
Daniel Dunbare627b682009-03-31 21:38:17 +0000442
Daniel Dunbar9a553c42009-04-04 05:17:38 +0000443 if (C.getArgs().hasArg(options::OPT_dumpversion)) {
Douglas Gregorb7064742009-04-27 22:23:34 +0000444 llvm::outs() << CLANG_VERSION_STRING "\n";
Daniel Dunbar9a553c42009-04-04 05:17:38 +0000445 return false;
446 }
447
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000448 if (C.getArgs().hasArg(options::OPT__help) ||
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000449 C.getArgs().hasArg(options::OPT__help_hidden)) {
450 PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden));
Daniel Dunbare627b682009-03-31 21:38:17 +0000451 return false;
452 }
453
Daniel Dunbar3b7c84c2009-04-02 15:05:41 +0000454 if (C.getArgs().hasArg(options::OPT__version)) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000455 // Follow gcc behavior and use stdout for --version and stderr for -v.
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000456 PrintVersion(C, llvm::outs());
Daniel Dunbar3b7c84c2009-04-02 15:05:41 +0000457 return false;
458 }
459
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000460 if (C.getArgs().hasArg(options::OPT_v) ||
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000461 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000462 PrintVersion(C, llvm::errs());
Daniel Dunbarcc006892009-03-13 00:51:18 +0000463 SuppressMissingInputWarning = true;
464 }
465
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000466 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbar91b9e202009-03-20 04:37:21 +0000467 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
468 llvm::outs() << "programs: =";
469 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
470 ie = TC.getProgramPaths().end(); it != ie; ++it) {
471 if (it != TC.getProgramPaths().begin())
472 llvm::outs() << ':';
473 llvm::outs() << *it;
474 }
475 llvm::outs() << "\n";
476 llvm::outs() << "libraries: =";
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000477 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
Daniel Dunbar91b9e202009-03-20 04:37:21 +0000478 ie = TC.getFilePaths().end(); it != ie; ++it) {
479 if (it != TC.getFilePaths().begin())
480 llvm::outs() << ':';
481 llvm::outs() << *it;
482 }
483 llvm::outs() << "\n";
Daniel Dunbare627b682009-03-31 21:38:17 +0000484 return false;
Daniel Dunbar91b9e202009-03-20 04:37:21 +0000485 }
486
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000487 // FIXME: The following handlers should use a callback mechanism, we don't
488 // know what the client would like to do.
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000489 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000490 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).str()
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000491 << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000492 return false;
493 }
494
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000495 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000496 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).str()
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000497 << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000498 return false;
499 }
500
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000501 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
Chris Lattner31bc3042009-08-23 22:45:33 +0000502 llvm::outs() << GetFilePath("libgcc.a", TC).str() << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000503 return false;
504 }
505
Daniel Dunbar329aa992009-06-16 23:25:22 +0000506 if (C.getArgs().hasArg(options::OPT_print_multi_lib)) {
507 // FIXME: We need tool chain support for this.
508 llvm::outs() << ".;\n";
509
510 switch (C.getDefaultToolChain().getTriple().getArch()) {
511 default:
512 break;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000513
Daniel Dunbar329aa992009-06-16 23:25:22 +0000514 case llvm::Triple::x86_64:
515 llvm::outs() << "x86_64;@m64" << "\n";
516 break;
517
518 case llvm::Triple::ppc64:
519 llvm::outs() << "ppc64;@m64" << "\n";
520 break;
521 }
522 return false;
523 }
524
525 // FIXME: What is the difference between print-multi-directory and
526 // print-multi-os-directory?
527 if (C.getArgs().hasArg(options::OPT_print_multi_directory) ||
528 C.getArgs().hasArg(options::OPT_print_multi_os_directory)) {
529 switch (C.getDefaultToolChain().getTriple().getArch()) {
530 default:
531 case llvm::Triple::x86:
532 case llvm::Triple::ppc:
533 llvm::outs() << "." << "\n";
534 break;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000535
Daniel Dunbar329aa992009-06-16 23:25:22 +0000536 case llvm::Triple::x86_64:
537 llvm::outs() << "x86_64" << "\n";
538 break;
539
540 case llvm::Triple::ppc64:
541 llvm::outs() << "ppc64" << "\n";
542 break;
543 }
544 return false;
545 }
546
Daniel Dunbarcc006892009-03-13 00:51:18 +0000547 return true;
548}
549
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000550static unsigned PrintActions1(const Compilation &C, Action *A,
Daniel Dunbar494646b2009-03-13 12:19:02 +0000551 std::map<Action*, unsigned> &Ids) {
552 if (Ids.count(A))
553 return Ids[A];
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000554
Daniel Dunbar494646b2009-03-13 12:19:02 +0000555 std::string str;
556 llvm::raw_string_ostream os(str);
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000557
Daniel Dunbar494646b2009-03-13 12:19:02 +0000558 os << Action::getClassName(A->getKind()) << ", ";
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000559 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000560 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbar494646b2009-03-13 12:19:02 +0000561 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000562 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000563 C.getDefaultToolChain().getArchName()) << '"'
564 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbar494646b2009-03-13 12:19:02 +0000565 } else {
566 os << "{";
567 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000568 os << PrintActions1(C, *it, Ids);
Daniel Dunbar494646b2009-03-13 12:19:02 +0000569 ++it;
570 if (it != ie)
571 os << ", ";
572 }
573 os << "}";
574 }
575
576 unsigned Id = Ids.size();
577 Ids[A] = Id;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000578 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbar494646b2009-03-13 12:19:02 +0000579 << types::getTypeName(A->getType()) << "\n";
580
581 return Id;
582}
583
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000584void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbar494646b2009-03-13 12:19:02 +0000585 std::map<Action*, unsigned> Ids;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000586 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000587 ie = C.getActions().end(); it != ie; ++it)
588 PrintActions1(C, *it, Ids);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000589}
590
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000591void Driver::BuildUniversalActions(const ArgList &Args,
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000592 ActionList &Actions) const {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000593 llvm::PrettyStackTraceString CrashInfo("Building universal build actions");
594 // Collect the list of architectures. Duplicates are allowed, but should only
595 // be handled once (in the order seen).
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000596 llvm::StringSet<> ArchNames;
597 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000598 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000599 it != ie; ++it) {
600 Arg *A = *it;
601
602 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000603 const char *Name = A->getValue(Args);
604
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000605 // FIXME: We need to handle canonicalization of the specified
606 // arch?
607
Daniel Dunbara345a442009-03-19 07:55:12 +0000608 A->claim();
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000609 if (ArchNames.insert(Name))
610 Archs.push_back(Name);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000611 }
612 }
613
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000614 // When there is no explicit arch for this platform, make sure we still bind
615 // the architecture (to the default) so that -Xarch_ is handled correctly.
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000616 if (!Archs.size())
617 Archs.push_back(0);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000618
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000619 // FIXME: We killed off some others but these aren't yet detected in a
620 // functional manner. If we added information to jobs about which "auxiliary"
621 // files they wrote then we could detect the conflict these cause downstream.
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000622 if (Archs.size() > 1) {
623 // No recovery needed, the point of this is just to prevent
624 // overwriting the same files.
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000625 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000626 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar73225932009-03-20 06:14:23 +0000627 << A->getAsString(Args);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000628 }
629
630 ActionList SingleActions;
631 BuildActions(Args, SingleActions);
632
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000633 // Add in arch binding and lipo (if necessary) for every top level action.
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000634 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
635 Action *Act = SingleActions[i];
636
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000637 // Make sure we can lipo this kind of output. If not (and it is an actual
638 // output) then we disallow, since we can't create an output file with the
639 // right name without overwriting it. We could remove this oddity by just
640 // changing the output names to include the arch, which would also fix
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000641 // -save-temps. Compatibility wins for now.
642
Daniel Dunbardd863aa2009-03-13 17:46:02 +0000643 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000644 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
645 << types::getTypeName(Act->getType());
646
647 ActionList Inputs;
Daniel Dunbara345a442009-03-19 07:55:12 +0000648 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000649 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000650
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000651 // Lipo if necessary, we do it this way because we need to set the arch flag
652 // so that -Xarch_ gets overwritten.
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000653 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
654 Actions.append(Inputs.begin(), Inputs.end());
655 else
656 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
657 }
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000658}
659
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000660void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000661 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000662 // Start by constructing the list of inputs and their types.
663
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000664 // Track the current user specified (-x) input. We also explicitly track the
665 // argument used to set the type; we only want to claim the type when we
666 // actually use it, so we warn about unused -x arguments.
Daniel Dunbar5cb75d62009-03-13 17:57:10 +0000667 types::ID InputType = types::TY_Nothing;
668 Arg *InputTypeArg = 0;
669
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000670 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000671 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000672 it != ie; ++it) {
673 Arg *A = *it;
674
675 if (isa<InputOption>(A->getOption())) {
676 const char *Value = A->getValue(Args);
677 types::ID Ty = types::TY_INVALID;
678
679 // Infer the input type if necessary.
Daniel Dunbar5cb75d62009-03-13 17:57:10 +0000680 if (InputType == types::TY_Nothing) {
681 // If there was an explicit arg for this, claim it.
682 if (InputTypeArg)
683 InputTypeArg->claim();
684
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000685 // stdin must be handled specially.
686 if (memcmp(Value, "-", 2) == 0) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000687 // If running with -E, treat as a C input (this changes the builtin
688 // macros, for example). This may be overridden by -ObjC below.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000689 //
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000690 // Otherwise emit an error but still use a valid type to avoid
691 // spurious errors (e.g., no inputs).
Daniel Dunbare9c70fa2009-03-15 00:48:16 +0000692 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbard724e332009-03-12 09:13:48 +0000693 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000694 Ty = types::TY_C;
695 } else {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000696 // Otherwise lookup by extension, and fallback to ObjectType if not
697 // found. We use a host hook here because Darwin at least has its own
698 // idea of what .s is.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000699 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbar7b6dfbd2009-03-20 23:39:23 +0000700 Ty = Host->lookupTypeForExtension(Ext + 1);
701
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000702 if (Ty == types::TY_INVALID)
703 Ty = types::TY_Object;
704 }
705
Daniel Dunbar84266db2009-05-18 21:47:54 +0000706 // -ObjC and -ObjC++ override the default language, but only for "source
707 // files". We just treat everything that isn't a linker input as a
708 // source file.
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000709 //
Daniel Dunbar84266db2009-05-18 21:47:54 +0000710 // FIXME: Clean this up if we move the phase sequence into the type.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000711 if (Ty != types::TY_Object) {
712 if (Args.hasArg(options::OPT_ObjC))
713 Ty = types::TY_ObjC;
714 else if (Args.hasArg(options::OPT_ObjCXX))
715 Ty = types::TY_ObjCXX;
716 }
717 } else {
718 assert(InputTypeArg && "InputType set w/o InputTypeArg");
719 InputTypeArg->claim();
720 Ty = InputType;
721 }
722
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000723 // Check that the file exists. It isn't clear this is worth doing, since
724 // the tool presumably does this anyway, and this just adds an extra stat
725 // to the equation, but this is gcc compatible.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000726 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbard724e332009-03-12 09:13:48 +0000727 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000728 else
729 Inputs.push_back(std::make_pair(Ty, A));
730
731 } else if (A->getOption().isLinkerInput()) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000732 // Just treat as object type, we could make a special type for this if
733 // necessary.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000734 Inputs.push_back(std::make_pair(types::TY_Object, A));
735
736 } else if (A->getOption().getId() == options::OPT_x) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000737 InputTypeArg = A;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000738 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
739
740 // Follow gcc behavior and treat as linker input for invalid -x
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000741 // options. Its not clear why we shouldn't just revert to unknown; but
742 // this isn't very important, we might as well be bug comatible.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000743 if (!InputType) {
Daniel Dunbard724e332009-03-12 09:13:48 +0000744 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000745 InputType = types::TY_Object;
746 }
747 }
748 }
749
Daniel Dunbar5a5ec5c2009-03-13 00:17:48 +0000750 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000751 Diag(clang::diag::err_drv_no_input_files);
752 return;
753 }
754
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000755 // Determine which compilation mode we are in. We look for options which
756 // affect the phase, starting with the earliest phases, and record which
757 // option we used to determine the final phase.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000758 Arg *FinalPhaseArg = 0;
759 phases::ID FinalPhase;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000760
761 // -{E,M,MM} only run the preprocessor.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000762 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
763 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
764 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
765 FinalPhase = phases::Preprocess;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000766
Daniel Dunbarec438002009-09-01 16:57:46 +0000767 // -{fsyntax-only,-analyze,emit-ast,S} only run up to the compiler.
Daniel Dunbare9c70fa2009-03-15 00:48:16 +0000768 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
Daniel Dunbar6e3de3d2009-05-06 02:12:32 +0000769 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze,
770 options::OPT__analyze_auto)) ||
Daniel Dunbarec438002009-09-01 16:57:46 +0000771 (FinalPhaseArg = Args.getLastArg(options::OPT_emit_ast)) ||
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000772 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
773 FinalPhase = phases::Compile;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000774
775 // -c only runs up to the assembler.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000776 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
777 FinalPhase = phases::Assemble;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000778
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000779 // Otherwise do everything.
780 } else
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000781 FinalPhase = phases::Link;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000782
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000783 // Reject -Z* at the top level, these options should never have been exposed
784 // by gcc.
Daniel Dunbar55e34e32009-03-26 16:12:09 +0000785 if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
Daniel Dunbar73225932009-03-20 06:14:23 +0000786 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000787
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000788 // Construct the actions to perform.
789 ActionList LinkerInputs;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000790 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000791 types::ID InputType = Inputs[i].first;
792 const Arg *InputArg = Inputs[i].second;
793
794 unsigned NumSteps = types::getNumCompilationPhases(InputType);
795 assert(NumSteps && "Invalid number of steps!");
796
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000797 // If the first step comes after the final phase we are doing as part of
798 // this compilation, warn the user about it.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000799 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
800 if (InitialPhase > FinalPhase) {
Daniel Dunbar923e7032009-03-19 07:57:08 +0000801 // Claim here to avoid the more general unused warning.
802 InputArg->claim();
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000803 Diag(clang::diag::warn_drv_input_file_unused)
Daniel Dunbar73225932009-03-20 06:14:23 +0000804 << InputArg->getAsString(Args)
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000805 << getPhaseName(InitialPhase)
806 << FinalPhaseArg->getOption().getName();
807 continue;
808 }
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000809
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000810 // Build the pipeline for this file.
811 Action *Current = new InputAction(*InputArg, InputType);
812 for (unsigned i = 0; i != NumSteps; ++i) {
813 phases::ID Phase = types::getCompilationPhase(InputType, i);
814
815 // We are done if this step is past what the user requested.
816 if (Phase > FinalPhase)
817 break;
818
819 // Queue linker inputs.
820 if (Phase == phases::Link) {
821 assert(i + 1 == NumSteps && "linking must be final compilation step.");
822 LinkerInputs.push_back(Current);
823 Current = 0;
824 break;
825 }
826
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000827 // Some types skip the assembler phase (e.g., llvm-bc), but we can't
828 // encode this in the steps because the intermediate type depends on
829 // arguments. Just special case here.
Daniel Dunbardec14452009-03-24 20:17:30 +0000830 if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
831 continue;
832
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000833 // Otherwise construct the appropriate action.
834 Current = ConstructPhaseAction(Args, Phase, Current);
835 if (Current->getType() == types::TY_Nothing)
836 break;
837 }
838
839 // If we ended with something, add to the output list.
840 if (Current)
841 Actions.push_back(Current);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000842 }
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000843
844 // Add a link action if necessary.
845 if (!LinkerInputs.empty())
846 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
847}
848
849Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
850 Action *Input) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000851 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000852 // Build the appropriate action.
853 switch (Phase) {
854 case phases::Link: assert(0 && "link action invalid here.");
855 case phases::Preprocess: {
Daniel Dunbar049b7242009-03-30 06:36:42 +0000856 types::ID OutputTy;
857 // -{M, MM} alter the output type.
858 if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) {
859 OutputTy = types::TY_Dependencies;
860 } else {
861 OutputTy = types::getPreprocessedType(Input->getType());
862 assert(OutputTy != types::TY_INVALID &&
863 "Cannot preprocess this input type!");
864 }
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000865 return new PreprocessJobAction(Input, OutputTy);
866 }
867 case phases::Precompile:
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000868 return new PrecompileJobAction(Input, types::TY_PCH);
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000869 case phases::Compile: {
870 if (Args.hasArg(options::OPT_fsyntax_only)) {
871 return new CompileJobAction(Input, types::TY_Nothing);
Daniel Dunbar6e3de3d2009-05-06 02:12:32 +0000872 } else if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) {
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000873 return new AnalyzeJobAction(Input, types::TY_Plist);
Daniel Dunbarec438002009-09-01 16:57:46 +0000874 } else if (Args.hasArg(options::OPT_emit_ast)) {
875 return new CompileJobAction(Input, types::TY_AST);
Daniel Dunbardec14452009-03-24 20:17:30 +0000876 } else if (Args.hasArg(options::OPT_emit_llvm) ||
877 Args.hasArg(options::OPT_flto) ||
878 Args.hasArg(options::OPT_O4)) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000879 types::ID Output =
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000880 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
881 return new CompileJobAction(Input, Output);
882 } else {
883 return new CompileJobAction(Input, types::TY_PP_Asm);
884 }
885 }
886 case phases::Assemble:
887 return new AssembleJobAction(Input, types::TY_Object);
888 }
889
890 assert(0 && "invalid phase in ConstructPhaseAction");
891 return 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000892}
893
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000894void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000895 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000896 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
897 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbarbac6b642009-03-18 23:18:19 +0000898
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000899 // FIXME: Pipes are forcibly disabled until we support executing them.
Daniel Dunbarbac6b642009-03-18 23:18:19 +0000900 if (!CCCPrintBindings)
901 UsePipes = false;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000902
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000903 // -save-temps inhibits pipes.
904 if (SaveTemps && UsePipes) {
905 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
906 UsePipes = true;
907 }
908
909 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
910
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000911 // It is an error to provide a -o option if we are making multiple output
912 // files.
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000913 if (FinalOutput) {
914 unsigned NumOutputs = 0;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000915 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000916 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000917 if ((*it)->getType() != types::TY_Nothing)
918 ++NumOutputs;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000919
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000920 if (NumOutputs > 1) {
921 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
922 FinalOutput = 0;
923 }
924 }
925
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000926 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000927 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000928 Action *A = *it;
929
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000930 // If we are linking an image for multiple archs then the linker wants
931 // -arch_multiple and -final_output <final image name>. Unfortunately, this
932 // doesn't fit in cleanly because we have to pass this information down.
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000933 //
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000934 // FIXME: This is a hack; find a cleaner way to integrate this into the
935 // process.
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000936 const char *LinkingOutput = 0;
Daniel Dunbar55e34e32009-03-26 16:12:09 +0000937 if (isa<LipoJobAction>(A)) {
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000938 if (FinalOutput)
939 LinkingOutput = FinalOutput->getValue(C.getArgs());
940 else
941 LinkingOutput = DefaultImageName.c_str();
942 }
943
944 InputInfo II;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000945 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000946 /*CanAcceptPipe*/ true,
947 /*AtTopLevel*/ true,
948 /*LinkingOutput*/ LinkingOutput,
949 II);
950 }
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000951
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000952 // If the user passed -Qunused-arguments or there were errors, don't warn
953 // about any unused arguments.
954 if (Diags.getNumErrors() ||
Daniel Dunbar1cfd8912009-04-07 19:04:18 +0000955 C.getArgs().hasArg(options::OPT_Qunused_arguments))
Daniel Dunbar46c70822009-03-18 18:03:46 +0000956 return;
957
Daniel Dunbar1e6f9ff2009-03-29 22:24:54 +0000958 // Claim -### here.
959 (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000960
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000961 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
962 it != ie; ++it) {
963 Arg *A = *it;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000964
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000965 // FIXME: It would be nice to be able to send the argument to the
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000966 // Diagnostic, so that extra values, position, and so on could be printed.
Daniel Dunbar6c6e3f72009-04-04 00:52:26 +0000967 if (!A->isClaimed()) {
Daniel Dunbar1cfd8912009-04-07 19:04:18 +0000968 if (A->getOption().hasNoArgumentUnused())
969 continue;
970
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000971 // Suppress the warning automatically if this is just a flag, and it is an
972 // instance of an argument we already claimed.
Daniel Dunbar6c6e3f72009-04-04 00:52:26 +0000973 const Option &Opt = A->getOption();
974 if (isa<FlagOption>(Opt)) {
975 bool DuplicateClaimed = false;
976
977 // FIXME: Use iterator.
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000978 for (ArgList::const_iterator it = C.getArgs().begin(),
Daniel Dunbar6c6e3f72009-04-04 00:52:26 +0000979 ie = C.getArgs().end(); it != ie; ++it) {
980 if ((*it)->isClaimed() && (*it)->getOption().matches(Opt.getId())) {
981 DuplicateClaimed = true;
982 break;
983 }
984 }
985
986 if (DuplicateClaimed)
987 continue;
988 }
989
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000990 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar73225932009-03-20 06:14:23 +0000991 << A->getAsString(C.getArgs());
Daniel Dunbar6c6e3f72009-04-04 00:52:26 +0000992 }
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000993 }
Daniel Dunbar47d762e2009-03-13 22:12:33 +0000994}
995
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000996void Driver::BuildJobsForAction(Compilation &C,
997 const Action *A,
998 const ToolChain *TC,
999 bool CanAcceptPipe,
1000 bool AtTopLevel,
1001 const char *LinkingOutput,
1002 InputInfo &Result) const {
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001003 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbarbac6b642009-03-18 23:18:19 +00001004
1005 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001006 // FIXME: Pipes are forcibly disabled until we support executing them.
Daniel Dunbarbac6b642009-03-18 23:18:19 +00001007 if (!CCCPrintBindings)
1008 UsePipes = false;
1009
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001010 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001011 // FIXME: It would be nice to not claim this here; maybe the old scheme of
1012 // just using Args was better?
Daniel Dunbarc3be67b2009-03-19 07:29:38 +00001013 const Arg &Input = IA->getInputArg();
1014 Input.claim();
1015 if (isa<PositionalArg>(Input)) {
1016 const char *Name = Input.getValue(C.getArgs());
1017 Result = InputInfo(Name, A->getType(), Name);
1018 } else
1019 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001020 return;
1021 }
1022
1023 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
1024 const char *ArchName = BAA->getArchName();
Daniel Dunbar08303652009-05-22 02:53:45 +00001025 std::string Arch;
1026 if (!ArchName) {
1027 Arch = C.getDefaultToolChain().getArchName();
1028 ArchName = Arch.c_str();
1029 }
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001030 BuildJobsForAction(C,
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001031 *BAA->begin(),
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001032 Host->getToolChain(C.getArgs(), ArchName),
1033 CanAcceptPipe,
1034 AtTopLevel,
1035 LinkingOutput,
1036 Result);
1037 return;
1038 }
1039
1040 const JobAction *JA = cast<JobAction>(A);
1041 const Tool &T = TC->SelectTool(C, *JA);
Daniel Dunbar84f695a2009-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 Dunbar7ce6add2009-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 Dunbar00fd3792009-03-18 06:00:36 +00001060 InputInfoList InputInfos;
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001061 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
1062 it != ie; ++it) {
1063 InputInfo II;
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001064 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001065 /*AtTopLevel*/false,
1066 LinkingOutput,
1067 II);
1068 InputInfos.push_back(II);
1069 }
1070
1071 // Determine if we should output to a pipe.
1072 bool OutputToPipe = false;
1073 if (CanAcceptPipe && T.canPipeOutput()) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001074 // Some actions default to writing to a pipe if they are the top level phase
1075 // and there was no user override.
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001076 //
1077 // FIXME: Is there a better way to handle this?
1078 if (AtTopLevel) {
1079 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
1080 OutputToPipe = true;
Daniel Dunbarbac6b642009-03-18 23:18:19 +00001081 } else if (UsePipes)
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001082 OutputToPipe = true;
1083 }
1084
1085 // Figure out where to put the job (pipes).
1086 Job *Dest = &C.getJobs();
1087 if (InputInfos[0].isPipe()) {
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001088 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001089 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
1090 Dest = &InputInfos[0].getPipe();
1091 }
1092
1093 // Always use the first input as the base input.
1094 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001095
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001096 // Determine the place to write output to (nothing, pipe, or filename) and
1097 // where to put the new job.
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001098 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001099 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001100 } else if (OutputToPipe) {
1101 // Append to current piped job or create a new one as appropriate.
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001102 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
1103 if (!PJ) {
1104 PJ = new PipedJob();
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001105 // FIXME: Temporary hack so that -ccc-print-bindings work until we have
1106 // pipe support. Please remove later.
Daniel Dunbar8eeb5a32009-03-20 00:11:04 +00001107 if (!CCCPrintBindings)
1108 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar933ac452009-03-18 07:06:02 +00001109 Dest = PJ;
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001110 }
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001111 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001112 } else {
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001113 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
1114 A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001115 }
1116
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001117 if (CCCPrintBindings) {
Daniel Dunbar049b7242009-03-30 06:36:42 +00001118 llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"'
1119 << " - \"" << T.getName() << "\", inputs: [";
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001120 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
1121 llvm::errs() << InputInfos[i].getAsString();
1122 if (i + 1 != e)
1123 llvm::errs() << ", ";
1124 }
1125 llvm::errs() << "], output: " << Result.getAsString() << "\n";
1126 } else {
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001127 T.ConstructJob(C, *JA, *Dest, Result, InputInfos,
Daniel Dunbara16e4fe2009-03-25 04:13:45 +00001128 C.getArgsForToolChain(TC), LinkingOutput);
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001129 }
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001130}
1131
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001132const char *Driver::GetNamedOutputPath(Compilation &C,
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001133 const JobAction &JA,
1134 const char *BaseInput,
1135 bool AtTopLevel) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +00001136 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001137 // Output to a user requested destination?
1138 if (AtTopLevel) {
1139 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
1140 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
1141 }
1142
1143 // Output to a temporary file?
1144 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001145 std::string TmpName =
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001146 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
1147 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001148 }
1149
1150 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbarfb5e3332009-03-18 02:00:31 +00001151 std::string BaseName(BasePath.getLast());
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001152
1153 // Determine what the derived output name should be.
1154 const char *NamedOutput;
1155 if (JA.getType() == types::TY_Image) {
1156 NamedOutput = DefaultImageName.c_str();
1157 } else {
1158 const char *Suffix = types::getTypeTempSuffix(JA.getType());
1159 assert(Suffix && "All types used for output should have a suffix.");
1160
1161 std::string::size_type End = std::string::npos;
1162 if (!types::appendSuffixForType(JA.getType()))
1163 End = BaseName.rfind('.');
1164 std::string Suffixed(BaseName.substr(0, End));
1165 Suffixed += '.';
1166 Suffixed += Suffix;
1167 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
1168 }
1169
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001170 // As an annoying special case, PCH generation doesn't strip the pathname.
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001171 if (JA.getType() == types::TY_PCH) {
1172 BasePath.eraseComponent();
Daniel Dunbar58a51262009-03-18 09:58:30 +00001173 if (BasePath.isEmpty())
1174 BasePath = NamedOutput;
1175 else
1176 BasePath.appendComponent(NamedOutput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001177 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
1178 } else {
1179 return C.addResultFile(NamedOutput);
1180 }
1181}
1182
Daniel Dunbare1cef7d2009-03-16 05:25:36 +00001183llvm::sys::Path Driver::GetFilePath(const char *Name,
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +00001184 const ToolChain &TC) const {
Daniel Dunbarcc7600c2009-03-18 20:26:19 +00001185 const ToolChain::path_list &List = TC.getFilePaths();
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001186 for (ToolChain::path_list::const_iterator
Daniel Dunbarcc7600c2009-03-18 20:26:19 +00001187 it = List.begin(), ie = List.end(); it != ie; ++it) {
1188 llvm::sys::Path P(*it);
1189 P.appendComponent(Name);
1190 if (P.exists())
1191 return P;
1192 }
1193
Daniel Dunbarcc006892009-03-13 00:51:18 +00001194 return llvm::sys::Path(Name);
1195}
1196
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001197llvm::sys::Path Driver::GetProgramPath(const char *Name,
Mike Stump32b1bbe2009-03-27 00:40:20 +00001198 const ToolChain &TC,
1199 bool WantFile) const {
Daniel Dunbarcc7600c2009-03-18 20:26:19 +00001200 const ToolChain::path_list &List = TC.getProgramPaths();
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001201 for (ToolChain::path_list::const_iterator
Daniel Dunbarcc7600c2009-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 Stump32b1bbe2009-03-27 00:40:20 +00001205 if (WantFile ? P.exists() : P.canExecute())
Daniel Dunbarcc7600c2009-03-18 20:26:19 +00001206 return P;
1207 }
1208
Daniel Dunbar49a35de2009-03-23 16:15:50 +00001209 // If all else failed, search the path.
1210 llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
Daniel Dunbarcb84b9a2009-03-18 21:34:08 +00001211 if (!P.empty())
1212 return P;
1213
Daniel Dunbarcc006892009-03-13 00:51:18 +00001214 return llvm::sys::Path(Name);
1215}
1216
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001217std::string Driver::GetTemporaryPath(const char *Suffix) const {
Daniel Dunbar84f695a2009-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 Dunbarb6ddc952009-03-18 19:34:39 +00001220 std::string Error;
Daniel Dunbar02e396a2009-04-20 20:28:21 +00001221 const char *TmpDir = ::getenv("TMPDIR");
1222 if (!TmpDir)
1223 TmpDir = ::getenv("TEMP");
1224 if (!TmpDir)
Daniel Dunbara77aa002009-04-21 00:25:10 +00001225 TmpDir = ::getenv("TMP");
1226 if (!TmpDir)
Daniel Dunbar02e396a2009-04-20 20:28:21 +00001227 TmpDir = "/tmp";
1228 llvm::sys::Path P(TmpDir);
Daniel Dunbar121f2512009-04-20 17:32:49 +00001229 P.appendComponent("cc");
Daniel Dunbarb6ddc952009-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 Dunbar84f695a2009-09-08 23:36:43 +00001235 // FIXME: Grumble, makeUnique sometimes leaves the file around!? PR3837.
Daniel Dunbar9083abe2009-03-18 23:08:52 +00001236 P.eraseFromDisk(false, 0);
1237
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001238 P.appendSuffix(Suffix);
Chris Lattner31bc3042009-08-23 22:45:33 +00001239 return P.str();
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001240}
1241
Daniel Dunbar08303652009-05-22 02:53:45 +00001242const HostInfo *Driver::GetHostInfo(const char *TripleStr) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +00001243 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbar08303652009-05-22 02:53:45 +00001244 llvm::Triple Triple(TripleStr);
Daniel Dunbard25acaa2009-03-10 23:41:59 +00001245
Daniel Dunbar08303652009-05-22 02:53:45 +00001246 switch (Triple.getOS()) {
Edward O'Callaghanb68ac562009-08-22 01:06:46 +00001247 case llvm::Triple::AuroraUX:
1248 return createAuroraUXHostInfo(*this, Triple);
Daniel Dunbar08303652009-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 Dunbar64c77a12009-06-29 20:52:51 +00001253 case llvm::Triple::OpenBSD:
1254 return createOpenBSDHostInfo(*this, Triple);
Daniel Dunbar08303652009-05-22 02:53:45 +00001255 case llvm::Triple::FreeBSD:
1256 return createFreeBSDHostInfo(*this, Triple);
Eli Friedman8cac4352009-05-26 07:52:18 +00001257 case llvm::Triple::Linux:
1258 return createLinuxHostInfo(*this, Triple);
Daniel Dunbar08303652009-05-22 02:53:45 +00001259 default:
1260 return createUnknownHostInfo(*this, Triple);
1261 }
Daniel Dunbard25acaa2009-03-10 23:41:59 +00001262}
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001263
1264bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
Daniel Dunbarb7daa2d2009-04-01 20:33:11 +00001265 const std::string &ArchNameStr) const {
1266 // FIXME: Remove this hack.
1267 const char *ArchName = ArchNameStr.c_str();
1268 if (ArchNameStr == "powerpc")
1269 ArchName = "ppc";
1270 else if (ArchNameStr == "powerpc64")
1271 ArchName = "ppc64";
1272
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001273 // Check if user requested no clang, or clang doesn't understand this type (we
1274 // only handle single inputs for now).
Daniel Dunbarec438002009-09-01 16:57:46 +00001275 if (!CCCUseClang || JA.size() != 1 ||
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001276 !types::isAcceptedByClang((*JA.begin())->getType()))
1277 return false;
1278
Daniel Dunbar0ea85f72009-03-24 19:02:31 +00001279 // Otherwise make sure this is an action clang understands.
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001280 if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001281 if (!CCCUseClangCPP) {
1282 Diag(clang::diag::warn_drv_not_using_clang_cpp);
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001283 return false;
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001284 }
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001285 } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
1286 return false;
1287
Daniel Dunbar0ea85f72009-03-24 19:02:31 +00001288 // Use clang for C++?
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001289 if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
1290 Diag(clang::diag::warn_drv_not_using_clang_cxx);
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001291 return false;
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001292 }
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001293
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001294 // Always use clang for precompiling and AST generation, regardless of archs.
Daniel Dunbarec438002009-09-01 16:57:46 +00001295 if (isa<PrecompileJobAction>(JA) || JA.getType() == types::TY_AST)
Daniel Dunbar5a34ca82009-04-16 23:10:13 +00001296 return true;
1297
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001298 // Finally, don't use clang if this isn't one of the user specified archs to
1299 // build.
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001300 if (!CCCClangArchs.empty() && !CCCClangArchs.count(ArchName)) {
1301 Diag(clang::diag::warn_drv_not_using_clang_arch) << ArchName;
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001302 return false;
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001303 }
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001304
1305 return true;
1306}
Daniel Dunbar9cdd4d42009-03-26 15:58:36 +00001307
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001308/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the
1309/// grouped values as integers. Numbers which are not provided are set to 0.
Daniel Dunbar9cdd4d42009-03-26 15:58:36 +00001310///
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001311/// \return True if the entire string was parsed (9.2), or all groups were
1312/// parsed (10.3.5extrastuff).
1313bool Driver::GetReleaseVersion(const char *Str, unsigned &Major,
Daniel Dunbar9cdd4d42009-03-26 15:58:36 +00001314 unsigned &Minor, unsigned &Micro,
1315 bool &HadExtra) {
1316 HadExtra = false;
1317
1318 Major = Minor = Micro = 0;
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001319 if (*Str == '\0')
Daniel Dunbar9cdd4d42009-03-26 15:58:36 +00001320 return true;
1321
1322 char *End;
1323 Major = (unsigned) strtol(Str, &End, 10);
1324 if (*Str != '\0' && *End == '\0')
1325 return true;
1326 if (*End != '.')
1327 return false;
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001328
Daniel Dunbar9cdd4d42009-03-26 15:58:36 +00001329 Str = End+1;
1330 Minor = (unsigned) strtol(Str, &End, 10);
1331 if (*Str != '\0' && *End == '\0')
1332 return true;
1333 if (*End != '.')
1334 return false;
1335
1336 Str = End+1;
1337 Micro = (unsigned) strtol(Str, &End, 10);
1338 if (*Str != '\0' && *End == '\0')
1339 return true;
1340 if (Str == End)
1341 return false;
1342 HadExtra = true;
1343 return true;
1344}