blob: 59099a33e6f0e6a24808aced9a579041b006bc90 [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 Dunbara28b9d42009-09-08 23:36:55 +000063 CCCClangArchs.insert(llvm::Triple::x86);
64 CCCClangArchs.insert(llvm::Triple::x86_64);
65 CCCClangArchs.insert(llvm::Triple::arm);
Daniel Dunbar89301fb2009-08-23 18:42:54 +000066#endif
Daniel Dunbar63c4da92009-03-02 19:59:07 +000067}
68
69Driver::~Driver() {
Daniel Dunbard6f0e372009-03-04 20:49:20 +000070 delete Opts;
Daniel Dunbar2f8b37e2009-03-18 01:09:40 +000071 delete Host;
Daniel Dunbar63c4da92009-03-02 19:59:07 +000072}
73
Daniel Dunbar84f695a2009-09-08 23:36:43 +000074InputArgList *Driver::ParseArgStrings(const char **ArgBegin,
Daniel Dunbara16e4fe2009-03-25 04:13:45 +000075 const char **ArgEnd) {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +000076 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbara16e4fe2009-03-25 04:13:45 +000077 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
Daniel Dunbar84f695a2009-09-08 23:36:43 +000078
Daniel Dunbar85cb3592009-03-13 11:38:42 +000079 // FIXME: Handle '@' args (or at least error on them).
80
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000081 unsigned Index = 0, End = ArgEnd - ArgBegin;
82 while (Index < End) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +000083 // gcc's handling of empty arguments doesn't make sense, but this is not a
84 // common use case. :)
Daniel Dunbarb043ebd2009-03-13 01:01:44 +000085 //
Daniel Dunbar84f695a2009-09-08 23:36:43 +000086 // We just ignore them here (note that other things may still take them as
87 // arguments).
Daniel Dunbarb043ebd2009-03-13 01:01:44 +000088 if (Args->getArgString(Index)[0] == '\0') {
89 ++Index;
90 continue;
91 }
92
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000093 unsigned Prev = Index;
Daniel Dunbarfb88ce02009-03-22 23:26:43 +000094 Arg *A = getOpts().ParseOneArg(*Args, Index);
95 assert(Index > Prev && "Parser failed to consume argument.");
Daniel Dunbardb62cc32009-03-12 07:58:46 +000096
Daniel Dunbarfb88ce02009-03-22 23:26:43 +000097 // Check for missing argument error.
98 if (!A) {
99 assert(Index >= End && "Unexpected parser error.");
100 Diag(clang::diag::err_drv_missing_argument)
101 << Args->getArgString(Prev)
102 << (Index - Prev - 1);
103 break;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000104 }
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000105
Daniel Dunbarfb88ce02009-03-22 23:26:43 +0000106 if (A->getOption().isUnsupported()) {
107 Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
108 continue;
109 }
110 Args->append(A);
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000111 }
112
113 return Args;
114}
115
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000116Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000117 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
118
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000119 // FIXME: Handle environment options which effect driver behavior, somewhere
120 // (client?). GCC_EXEC_PREFIX, COMPILER_PATH, LIBRARY_PATH, LPATH,
121 // CC_PRINT_OPTIONS.
Daniel Dunbarcc006892009-03-13 00:51:18 +0000122
123 // FIXME: What are we going to do with -V and -b?
124
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000125 // FIXME: This stuff needs to go into the Compilation, not the driver.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000126 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000127
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000128 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000129 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000130
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000131 // Read -ccc args.
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000132 //
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000133 // FIXME: We need to figure out where this behavior should live. Most of it
134 // should be outside in the client; the parts that aren't should have proper
135 // options, either by introducing new ones or by overloading gcc ones like -V
136 // or -b.
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000137 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
138 const char *Opt = *Start + 5;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000139
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000140 if (!strcmp(Opt, "print-options")) {
141 CCCPrintOptions = true;
142 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000143 CCCPrintActions = true;
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000144 } else if (!strcmp(Opt, "print-bindings")) {
145 CCCPrintBindings = true;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000146 } else if (!strcmp(Opt, "cxx")) {
147 CCCIsCXX = true;
148 } else if (!strcmp(Opt, "echo")) {
149 CCCEcho = true;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000150
Daniel Dunbar126da5e2009-04-01 23:34:41 +0000151 } else if (!strcmp(Opt, "gcc-name")) {
152 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
153 CCCGenericGCCName = *++Start;
154
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000155 } else if (!strcmp(Opt, "clang-cxx")) {
156 CCCUseClangCXX = true;
Daniel Dunbar422ce672009-07-23 17:48:59 +0000157 } else if (!strcmp(Opt, "no-clang-cxx")) {
158 CCCUseClangCXX = false;
Douglas Gregor95734502009-04-18 00:34:01 +0000159 } else if (!strcmp(Opt, "pch-is-pch")) {
160 CCCUsePCH = true;
161 } else if (!strcmp(Opt, "pch-is-pth")) {
162 CCCUsePCH = false;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000163 } else if (!strcmp(Opt, "no-clang")) {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000164 CCCUseClang = false;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000165 } else if (!strcmp(Opt, "no-clang-cpp")) {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000166 CCCUseClangCPP = false;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000167 } else if (!strcmp(Opt, "clang-archs")) {
168 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbara28b9d42009-09-08 23:36:55 +0000169 llvm::StringRef Cur = *++Start;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000170
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000171 CCCClangArchs.clear();
Daniel Dunbara28b9d42009-09-08 23:36:55 +0000172 while (!Cur.empty()) {
173 std::pair<llvm::StringRef, llvm::StringRef> Split = Cur.split(',');
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000174
Daniel Dunbara28b9d42009-09-08 23:36:55 +0000175 if (!Split.first.empty()) {
176 llvm::Triple::ArchType Arch =
177 llvm::Triple(Split.first, "", "").getArch();
178
179 if (Arch == llvm::Triple::UnknownArch) {
180 // FIXME: Error handling.
181 llvm::errs() << "invalid arch name: " << Split.first << "\n";
182 exit(1);
183 }
184
185 CCCClangArchs.insert(Arch);
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000186 }
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000187
Daniel Dunbara28b9d42009-09-08 23:36:55 +0000188 Cur = Split.second;
189 }
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000190 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000191 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000192 HostTriple = *++Start;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000193
Daniel Dunbarc0cc8712009-09-04 18:35:03 +0000194 } else if (!strcmp(Opt, "install-dir")) {
195 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
196 Dir = *++Start;
197
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000198 } else {
199 // FIXME: Error handling.
200 llvm::errs() << "invalid option: " << *Start << "\n";
201 exit(1);
202 }
203 }
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000204
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000205 InputArgList *Args = ParseArgStrings(Start, End);
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000206
Daniel Dunbar08966ca2009-03-17 20:45:45 +0000207 Host = GetHostInfo(HostTriple);
Daniel Dunbarcc006892009-03-13 00:51:18 +0000208
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000209 // The compilation takes ownership of Args.
Daniel Dunbar31a70e92009-09-08 23:37:19 +0000210 Compilation *C = new Compilation(*this, *Host->CreateToolChain(*Args), Args);
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000211
212 // FIXME: This behavior shouldn't be here.
213 if (CCCPrintOptions) {
214 PrintOptions(C->getArgs());
215 return C;
216 }
217
218 if (!HandleImmediateArgs(*C))
219 return C;
220
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000221 // Construct the list of abstract actions to perform for this compilation. We
222 // avoid passing a Compilation here simply to enforce the abstraction that
223 // pipelining is not host or toolchain dependent (other than the driver driver
224 // test).
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000225 if (Host->useDriverDriver())
226 BuildUniversalActions(C->getArgs(), C->getActions());
227 else
228 BuildActions(C->getArgs(), C->getActions());
229
230 if (CCCPrintActions) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000231 PrintActions(*C);
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000232 return C;
233 }
234
235 BuildJobs(*C);
Daniel Dunbarc413f822009-03-15 01:38:15 +0000236
237 return C;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000238}
239
Daniel Dunbar9fbdc882009-07-01 20:03:04 +0000240int Driver::ExecuteCompilation(const Compilation &C) const {
241 // Just print if -### was present.
242 if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
243 C.PrintJob(llvm::errs(), C.getJobs(), "\n", true);
244 return 0;
245 }
246
247 // If there were errors building the compilation, quit now.
248 if (getDiags().getNumErrors())
249 return 1;
250
251 const Command *FailingCommand = 0;
252 int Res = C.ExecuteJob(C.getJobs(), FailingCommand);
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000253
Daniel Dunbar9fbdc882009-07-01 20:03:04 +0000254 // Remove temp files.
255 C.CleanupFileList(C.getTempFiles());
256
257 // If the compilation failed, remove result files as well.
258 if (Res != 0 && !C.getArgs().hasArg(options::OPT_save_temps))
259 C.CleanupFileList(C.getResultFiles(), true);
260
261 // Print extra information about abnormal failures, if possible.
262 if (Res) {
263 // This is ad-hoc, but we don't want to be excessively noisy. If the result
264 // status was 1, assume the command failed normally. In particular, if it
265 // was the compiler then assume it gave a reasonable error code. Failures in
266 // other tools are less common, and they generally have worse diagnostics,
267 // so always print the diagnostic there.
268 const Action &Source = FailingCommand->getSource();
269 bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) ||
270 isa<PrecompileJobAction>(Source) ||
271 isa<AnalyzeJobAction>(Source) ||
272 isa<CompileJobAction>(Source));
273
274 if (!IsFriendlyTool || Res != 1) {
275 // FIXME: See FIXME above regarding result code interpretation.
276 if (Res < 0)
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000277 Diag(clang::diag::err_drv_command_signalled)
Daniel Dunbar9fbdc882009-07-01 20:03:04 +0000278 << Source.getClassName() << -Res;
279 else
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000280 Diag(clang::diag::err_drv_command_failed)
Daniel Dunbar9fbdc882009-07-01 20:03:04 +0000281 << Source.getClassName() << Res;
282 }
283 }
284
285 return Res;
286}
287
Daniel Dunbara790d372009-03-12 18:24:49 +0000288void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000289 unsigned i = 0;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000290 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000291 it != ie; ++it, ++i) {
292 Arg *A = *it;
293 llvm::errs() << "Option " << i << " - "
294 << "Name: \"" << A->getOption().getName() << "\", "
295 << "Values: {";
296 for (unsigned j = 0; j < A->getNumValues(); ++j) {
297 if (j)
298 llvm::errs() << ", ";
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000299 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000300 }
301 llvm::errs() << "}\n";
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000302 }
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000303}
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000304
Daniel Dunbare627b682009-03-31 21:38:17 +0000305static std::string getOptionHelpName(const OptTable &Opts, options::ID Id) {
306 std::string Name = Opts.getOptionName(Id);
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000307
Daniel Dunbare627b682009-03-31 21:38:17 +0000308 // Add metavar, if used.
309 switch (Opts.getOptionKind(Id)) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000310 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
Daniel Dunbare627b682009-03-31 21:38:17 +0000311 assert(0 && "Invalid option with help text.");
312
313 case Option::MultiArgClass: case Option::JoinedAndSeparateClass:
314 assert(0 && "Cannot print metavar for this kind of option.");
315
316 case Option::FlagClass:
317 break;
318
319 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
320 Name += ' ';
321 // FALLTHROUGH
322 case Option::JoinedClass: case Option::CommaJoinedClass:
323 Name += Opts.getOptionMetaVar(Id);
324 break;
325 }
326
327 return Name;
328}
329
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000330void Driver::PrintHelp(bool ShowHidden) const {
Daniel Dunbare627b682009-03-31 21:38:17 +0000331 llvm::raw_ostream &OS = llvm::outs();
332
333 OS << "OVERVIEW: clang \"gcc-compatible\" driver\n";
334 OS << '\n';
335 OS << "USAGE: " << Name << " [options] <input files>\n";
336 OS << '\n';
337 OS << "OPTIONS:\n";
338
339 // Render help text into (option, help) pairs.
340 std::vector< std::pair<std::string, const char*> > OptionHelp;
341
342 for (unsigned i = options::OPT_INPUT, e = options::LastOption; i != e; ++i) {
343 options::ID Id = (options::ID) i;
344 if (const char *Text = getOpts().getOptionHelpText(Id))
345 OptionHelp.push_back(std::make_pair(getOptionHelpName(getOpts(), Id),
346 Text));
347 }
348
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000349 if (ShowHidden) {
350 OptionHelp.push_back(std::make_pair("\nDRIVER OPTIONS:",""));
351 OptionHelp.push_back(std::make_pair("-ccc-cxx",
352 "Act as a C++ driver"));
353 OptionHelp.push_back(std::make_pair("-ccc-gcc-name",
354 "Name for native GCC compiler"));
355 OptionHelp.push_back(std::make_pair("-ccc-clang-cxx",
Daniel Dunbarec438002009-09-01 16:57:46 +0000356 "Enable the clang compiler for C++"));
357 OptionHelp.push_back(std::make_pair("-ccc-no-clang-cxx",
358 "Disable the clang compiler for C++"));
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000359 OptionHelp.push_back(std::make_pair("-ccc-no-clang",
Daniel Dunbarec438002009-09-01 16:57:46 +0000360 "Disable the clang compiler"));
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000361 OptionHelp.push_back(std::make_pair("-ccc-no-clang-cpp",
Daniel Dunbarec438002009-09-01 16:57:46 +0000362 "Disable the clang preprocessor"));
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000363 OptionHelp.push_back(std::make_pair("-ccc-clang-archs",
364 "Comma separate list of architectures "
365 "to use the clang compiler for"));
Douglas Gregor95734502009-04-18 00:34:01 +0000366 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pch",
367 "Use lazy PCH for precompiled headers"));
368 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pth",
369 "Use pretokenized headers for precompiled headers"));
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000370
371 OptionHelp.push_back(std::make_pair("\nDEBUG/DEVELOPMENT OPTIONS:",""));
372 OptionHelp.push_back(std::make_pair("-ccc-host-triple",
Daniel Dunbarc0cc8712009-09-04 18:35:03 +0000373 "Simulate running on the given target"));
374 OptionHelp.push_back(std::make_pair("-ccc-install-dir",
375 "Simulate installation in the given directory"));
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000376 OptionHelp.push_back(std::make_pair("-ccc-print-options",
377 "Dump parsed command line arguments"));
378 OptionHelp.push_back(std::make_pair("-ccc-print-phases",
379 "Dump list of actions to perform"));
380 OptionHelp.push_back(std::make_pair("-ccc-print-bindings",
381 "Show bindings of tools to actions"));
382 OptionHelp.push_back(std::make_pair("CCC_ADD_ARGS",
383 "(ENVIRONMENT VARIABLE) Comma separated list of "
384 "arguments to prepend to the command line"));
385 }
386
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000387 // Find the maximum option length.
Daniel Dunbare627b682009-03-31 21:38:17 +0000388 unsigned OptionFieldWidth = 0;
389 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000390 // Skip titles.
391 if (!OptionHelp[i].second)
392 continue;
393
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000394 // Limit the amount of padding we are willing to give up for alignment.
Daniel Dunbare627b682009-03-31 21:38:17 +0000395 unsigned Length = OptionHelp[i].first.size();
396 if (Length <= 23)
397 OptionFieldWidth = std::max(OptionFieldWidth, Length);
398 }
399
400 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
401 const std::string &Option = OptionHelp[i].first;
402 OS << " " << Option;
403 for (int j = Option.length(), e = OptionFieldWidth; j < e; ++j)
404 OS << ' ';
405 OS << ' ' << OptionHelp[i].second << '\n';
406 }
407
408 OS.flush();
409}
410
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000411void Driver::PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const {
Mike Stump74d94472009-03-18 14:00:02 +0000412 static char buf[] = "$URL$";
413 char *zap = strstr(buf, "/lib/Driver");
414 if (zap)
415 *zap = 0;
416 zap = strstr(buf, "/clang/tools/clang");
417 if (zap)
418 *zap = 0;
Mike Stumpbc927292009-03-18 15:19:35 +0000419 const char *vers = buf+6;
Mike Stump3fc58f02009-03-18 18:45:55 +0000420 // FIXME: Add cmake support and remove #ifdef
421#ifdef SVN_REVISION
422 const char *revision = SVN_REVISION;
423#else
424 const char *revision = "";
425#endif
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000426 // FIXME: The following handlers should use a callback mechanism, we don't
427 // know what the client would like to do.
428 OS << "clang version " CLANG_VERSION_STRING " ("
429 << vers << " " << revision << ")" << '\n';
Daniel Dunbar0ded7de2009-03-26 16:09:13 +0000430
431 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000432 OS << "Target: " << TC.getTripleString() << '\n';
Daniel Dunbar41c34b32009-06-16 23:32:58 +0000433
434 // Print the threading model.
435 //
436 // FIXME: Implement correctly.
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000437 OS << "Thread model: " << "posix" << '\n';
Daniel Dunbarcc006892009-03-13 00:51:18 +0000438}
439
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000440bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000441 // The order these options are handled in in gcc is all over the place, but we
442 // don't expect inconsistencies w.r.t. that to matter in practice.
Daniel Dunbare627b682009-03-31 21:38:17 +0000443
Daniel Dunbar9a553c42009-04-04 05:17:38 +0000444 if (C.getArgs().hasArg(options::OPT_dumpversion)) {
Douglas Gregorb7064742009-04-27 22:23:34 +0000445 llvm::outs() << CLANG_VERSION_STRING "\n";
Daniel Dunbar9a553c42009-04-04 05:17:38 +0000446 return false;
447 }
448
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000449 if (C.getArgs().hasArg(options::OPT__help) ||
Daniel Dunbara5f09bc2009-04-15 16:34:29 +0000450 C.getArgs().hasArg(options::OPT__help_hidden)) {
451 PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden));
Daniel Dunbare627b682009-03-31 21:38:17 +0000452 return false;
453 }
454
Daniel Dunbar3b7c84c2009-04-02 15:05:41 +0000455 if (C.getArgs().hasArg(options::OPT__version)) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000456 // Follow gcc behavior and use stdout for --version and stderr for -v.
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000457 PrintVersion(C, llvm::outs());
Daniel Dunbar3b7c84c2009-04-02 15:05:41 +0000458 return false;
459 }
460
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000461 if (C.getArgs().hasArg(options::OPT_v) ||
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000462 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbare00f1cd2009-07-21 20:06:58 +0000463 PrintVersion(C, llvm::errs());
Daniel Dunbarcc006892009-03-13 00:51:18 +0000464 SuppressMissingInputWarning = true;
465 }
466
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000467 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbar91b9e202009-03-20 04:37:21 +0000468 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
469 llvm::outs() << "programs: =";
470 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
471 ie = TC.getProgramPaths().end(); it != ie; ++it) {
472 if (it != TC.getProgramPaths().begin())
473 llvm::outs() << ':';
474 llvm::outs() << *it;
475 }
476 llvm::outs() << "\n";
477 llvm::outs() << "libraries: =";
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000478 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
Daniel Dunbar91b9e202009-03-20 04:37:21 +0000479 ie = TC.getFilePaths().end(); it != ie; ++it) {
480 if (it != TC.getFilePaths().begin())
481 llvm::outs() << ':';
482 llvm::outs() << *it;
483 }
484 llvm::outs() << "\n";
Daniel Dunbare627b682009-03-31 21:38:17 +0000485 return false;
Daniel Dunbar91b9e202009-03-20 04:37:21 +0000486 }
487
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000488 // FIXME: The following handlers should use a callback mechanism, we don't
489 // know what the client would like to do.
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000490 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000491 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).str()
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000492 << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000493 return false;
494 }
495
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000496 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000497 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).str()
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000498 << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000499 return false;
500 }
501
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000502 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
Chris Lattner31bc3042009-08-23 22:45:33 +0000503 llvm::outs() << GetFilePath("libgcc.a", TC).str() << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000504 return false;
505 }
506
Daniel Dunbar329aa992009-06-16 23:25:22 +0000507 if (C.getArgs().hasArg(options::OPT_print_multi_lib)) {
508 // FIXME: We need tool chain support for this.
509 llvm::outs() << ".;\n";
510
511 switch (C.getDefaultToolChain().getTriple().getArch()) {
512 default:
513 break;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000514
Daniel Dunbar329aa992009-06-16 23:25:22 +0000515 case llvm::Triple::x86_64:
516 llvm::outs() << "x86_64;@m64" << "\n";
517 break;
518
519 case llvm::Triple::ppc64:
520 llvm::outs() << "ppc64;@m64" << "\n";
521 break;
522 }
523 return false;
524 }
525
526 // FIXME: What is the difference between print-multi-directory and
527 // print-multi-os-directory?
528 if (C.getArgs().hasArg(options::OPT_print_multi_directory) ||
529 C.getArgs().hasArg(options::OPT_print_multi_os_directory)) {
530 switch (C.getDefaultToolChain().getTriple().getArch()) {
531 default:
532 case llvm::Triple::x86:
533 case llvm::Triple::ppc:
534 llvm::outs() << "." << "\n";
535 break;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000536
Daniel Dunbar329aa992009-06-16 23:25:22 +0000537 case llvm::Triple::x86_64:
538 llvm::outs() << "x86_64" << "\n";
539 break;
540
541 case llvm::Triple::ppc64:
542 llvm::outs() << "ppc64" << "\n";
543 break;
544 }
545 return false;
546 }
547
Daniel Dunbarcc006892009-03-13 00:51:18 +0000548 return true;
549}
550
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000551static unsigned PrintActions1(const Compilation &C, Action *A,
Daniel Dunbar494646b2009-03-13 12:19:02 +0000552 std::map<Action*, unsigned> &Ids) {
553 if (Ids.count(A))
554 return Ids[A];
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000555
Daniel Dunbar494646b2009-03-13 12:19:02 +0000556 std::string str;
557 llvm::raw_string_ostream os(str);
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000558
Daniel Dunbar494646b2009-03-13 12:19:02 +0000559 os << Action::getClassName(A->getKind()) << ", ";
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000560 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000561 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbar494646b2009-03-13 12:19:02 +0000562 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000563 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000564 C.getDefaultToolChain().getArchName()) << '"'
565 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbar494646b2009-03-13 12:19:02 +0000566 } else {
567 os << "{";
568 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000569 os << PrintActions1(C, *it, Ids);
Daniel Dunbar494646b2009-03-13 12:19:02 +0000570 ++it;
571 if (it != ie)
572 os << ", ";
573 }
574 os << "}";
575 }
576
577 unsigned Id = Ids.size();
578 Ids[A] = Id;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000579 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbar494646b2009-03-13 12:19:02 +0000580 << types::getTypeName(A->getType()) << "\n";
581
582 return Id;
583}
584
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000585void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbar494646b2009-03-13 12:19:02 +0000586 std::map<Action*, unsigned> Ids;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000587 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000588 ie = C.getActions().end(); it != ie; ++it)
589 PrintActions1(C, *it, Ids);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000590}
591
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000592void Driver::BuildUniversalActions(const ArgList &Args,
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000593 ActionList &Actions) const {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000594 llvm::PrettyStackTraceString CrashInfo("Building universal build actions");
595 // Collect the list of architectures. Duplicates are allowed, but should only
596 // be handled once (in the order seen).
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000597 llvm::StringSet<> ArchNames;
598 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000599 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000600 it != ie; ++it) {
601 Arg *A = *it;
602
603 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbarf985dab2009-09-08 23:37:30 +0000604 // Validate the option here; we don't save the type here because its
605 // particular spelling may participate in other driver choices.
606 llvm::Triple::ArchType Arch =
607 llvm::Triple::getArchTypeForDarwinArchName(A->getValue(Args));
608 if (Arch == llvm::Triple::UnknownArch) {
609 Diag(clang::diag::err_drv_invalid_arch_name)
610 << A->getAsString(Args);
611 continue;
612 }
613
Daniel Dunbara345a442009-03-19 07:55:12 +0000614 A->claim();
Daniel Dunbarf881bae2009-09-08 23:37:02 +0000615 if (ArchNames.insert(A->getValue(Args)))
616 Archs.push_back(A->getValue(Args));
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000617 }
618 }
619
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000620 // When there is no explicit arch for this platform, make sure we still bind
621 // the architecture (to the default) so that -Xarch_ is handled correctly.
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000622 if (!Archs.size())
623 Archs.push_back(0);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000624
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000625 // FIXME: We killed off some others but these aren't yet detected in a
626 // functional manner. If we added information to jobs about which "auxiliary"
627 // files they wrote then we could detect the conflict these cause downstream.
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000628 if (Archs.size() > 1) {
629 // No recovery needed, the point of this is just to prevent
630 // overwriting the same files.
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000631 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000632 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar73225932009-03-20 06:14:23 +0000633 << A->getAsString(Args);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000634 }
635
636 ActionList SingleActions;
637 BuildActions(Args, SingleActions);
638
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000639 // Add in arch binding and lipo (if necessary) for every top level action.
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000640 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
641 Action *Act = SingleActions[i];
642
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000643 // Make sure we can lipo this kind of output. If not (and it is an actual
644 // output) then we disallow, since we can't create an output file with the
645 // right name without overwriting it. We could remove this oddity by just
646 // changing the output names to include the arch, which would also fix
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000647 // -save-temps. Compatibility wins for now.
648
Daniel Dunbardd863aa2009-03-13 17:46:02 +0000649 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000650 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
651 << types::getTypeName(Act->getType());
652
653 ActionList Inputs;
Daniel Dunbara345a442009-03-19 07:55:12 +0000654 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000655 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000656
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000657 // Lipo if necessary, we do it this way because we need to set the arch flag
658 // so that -Xarch_ gets overwritten.
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000659 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
660 Actions.append(Inputs.begin(), Inputs.end());
661 else
662 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
663 }
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000664}
665
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000666void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000667 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000668 // Start by constructing the list of inputs and their types.
669
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000670 // Track the current user specified (-x) input. We also explicitly track the
671 // argument used to set the type; we only want to claim the type when we
672 // actually use it, so we warn about unused -x arguments.
Daniel Dunbar5cb75d62009-03-13 17:57:10 +0000673 types::ID InputType = types::TY_Nothing;
674 Arg *InputTypeArg = 0;
675
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000676 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000677 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000678 it != ie; ++it) {
679 Arg *A = *it;
680
681 if (isa<InputOption>(A->getOption())) {
682 const char *Value = A->getValue(Args);
683 types::ID Ty = types::TY_INVALID;
684
685 // Infer the input type if necessary.
Daniel Dunbar5cb75d62009-03-13 17:57:10 +0000686 if (InputType == types::TY_Nothing) {
687 // If there was an explicit arg for this, claim it.
688 if (InputTypeArg)
689 InputTypeArg->claim();
690
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000691 // stdin must be handled specially.
692 if (memcmp(Value, "-", 2) == 0) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000693 // If running with -E, treat as a C input (this changes the builtin
694 // macros, for example). This may be overridden by -ObjC below.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000695 //
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000696 // Otherwise emit an error but still use a valid type to avoid
697 // spurious errors (e.g., no inputs).
Daniel Dunbare9c70fa2009-03-15 00:48:16 +0000698 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbard724e332009-03-12 09:13:48 +0000699 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000700 Ty = types::TY_C;
701 } else {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000702 // Otherwise lookup by extension, and fallback to ObjectType if not
703 // found. We use a host hook here because Darwin at least has its own
704 // idea of what .s is.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000705 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbar7b6dfbd2009-03-20 23:39:23 +0000706 Ty = Host->lookupTypeForExtension(Ext + 1);
707
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000708 if (Ty == types::TY_INVALID)
709 Ty = types::TY_Object;
710 }
711
Daniel Dunbar84266db2009-05-18 21:47:54 +0000712 // -ObjC and -ObjC++ override the default language, but only for "source
713 // files". We just treat everything that isn't a linker input as a
714 // source file.
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000715 //
Daniel Dunbar84266db2009-05-18 21:47:54 +0000716 // FIXME: Clean this up if we move the phase sequence into the type.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000717 if (Ty != types::TY_Object) {
718 if (Args.hasArg(options::OPT_ObjC))
719 Ty = types::TY_ObjC;
720 else if (Args.hasArg(options::OPT_ObjCXX))
721 Ty = types::TY_ObjCXX;
722 }
723 } else {
724 assert(InputTypeArg && "InputType set w/o InputTypeArg");
725 InputTypeArg->claim();
726 Ty = InputType;
727 }
728
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000729 // Check that the file exists. It isn't clear this is worth doing, since
730 // the tool presumably does this anyway, and this just adds an extra stat
731 // to the equation, but this is gcc compatible.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000732 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbard724e332009-03-12 09:13:48 +0000733 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000734 else
735 Inputs.push_back(std::make_pair(Ty, A));
736
737 } else if (A->getOption().isLinkerInput()) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000738 // Just treat as object type, we could make a special type for this if
739 // necessary.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000740 Inputs.push_back(std::make_pair(types::TY_Object, A));
741
742 } else if (A->getOption().getId() == options::OPT_x) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000743 InputTypeArg = A;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000744 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
745
746 // Follow gcc behavior and treat as linker input for invalid -x
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000747 // options. Its not clear why we shouldn't just revert to unknown; but
748 // this isn't very important, we might as well be bug comatible.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000749 if (!InputType) {
Daniel Dunbard724e332009-03-12 09:13:48 +0000750 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000751 InputType = types::TY_Object;
752 }
753 }
754 }
755
Daniel Dunbar5a5ec5c2009-03-13 00:17:48 +0000756 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000757 Diag(clang::diag::err_drv_no_input_files);
758 return;
759 }
760
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000761 // Determine which compilation mode we are in. We look for options which
762 // affect the phase, starting with the earliest phases, and record which
763 // option we used to determine the final phase.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000764 Arg *FinalPhaseArg = 0;
765 phases::ID FinalPhase;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000766
767 // -{E,M,MM} only run the preprocessor.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000768 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
769 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
770 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
771 FinalPhase = phases::Preprocess;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000772
Daniel Dunbarec438002009-09-01 16:57:46 +0000773 // -{fsyntax-only,-analyze,emit-ast,S} only run up to the compiler.
Daniel Dunbare9c70fa2009-03-15 00:48:16 +0000774 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
Daniel Dunbar6e3de3d2009-05-06 02:12:32 +0000775 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze,
776 options::OPT__analyze_auto)) ||
Daniel Dunbarec438002009-09-01 16:57:46 +0000777 (FinalPhaseArg = Args.getLastArg(options::OPT_emit_ast)) ||
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000778 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
779 FinalPhase = phases::Compile;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000780
781 // -c only runs up to the assembler.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000782 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
783 FinalPhase = phases::Assemble;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000784
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000785 // Otherwise do everything.
786 } else
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000787 FinalPhase = phases::Link;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000788
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000789 // Reject -Z* at the top level, these options should never have been exposed
790 // by gcc.
Daniel Dunbar55e34e32009-03-26 16:12:09 +0000791 if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
Daniel Dunbar73225932009-03-20 06:14:23 +0000792 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000793
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000794 // Construct the actions to perform.
795 ActionList LinkerInputs;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000796 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000797 types::ID InputType = Inputs[i].first;
798 const Arg *InputArg = Inputs[i].second;
799
800 unsigned NumSteps = types::getNumCompilationPhases(InputType);
801 assert(NumSteps && "Invalid number of steps!");
802
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000803 // If the first step comes after the final phase we are doing as part of
804 // this compilation, warn the user about it.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000805 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
806 if (InitialPhase > FinalPhase) {
Daniel Dunbar923e7032009-03-19 07:57:08 +0000807 // Claim here to avoid the more general unused warning.
808 InputArg->claim();
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000809 Diag(clang::diag::warn_drv_input_file_unused)
Daniel Dunbar73225932009-03-20 06:14:23 +0000810 << InputArg->getAsString(Args)
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000811 << getPhaseName(InitialPhase)
812 << FinalPhaseArg->getOption().getName();
813 continue;
814 }
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000815
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000816 // Build the pipeline for this file.
817 Action *Current = new InputAction(*InputArg, InputType);
818 for (unsigned i = 0; i != NumSteps; ++i) {
819 phases::ID Phase = types::getCompilationPhase(InputType, i);
820
821 // We are done if this step is past what the user requested.
822 if (Phase > FinalPhase)
823 break;
824
825 // Queue linker inputs.
826 if (Phase == phases::Link) {
827 assert(i + 1 == NumSteps && "linking must be final compilation step.");
828 LinkerInputs.push_back(Current);
829 Current = 0;
830 break;
831 }
832
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000833 // Some types skip the assembler phase (e.g., llvm-bc), but we can't
834 // encode this in the steps because the intermediate type depends on
835 // arguments. Just special case here.
Daniel Dunbardec14452009-03-24 20:17:30 +0000836 if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
837 continue;
838
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000839 // Otherwise construct the appropriate action.
840 Current = ConstructPhaseAction(Args, Phase, Current);
841 if (Current->getType() == types::TY_Nothing)
842 break;
843 }
844
845 // If we ended with something, add to the output list.
846 if (Current)
847 Actions.push_back(Current);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000848 }
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000849
850 // Add a link action if necessary.
851 if (!LinkerInputs.empty())
852 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
853}
854
855Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
856 Action *Input) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000857 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000858 // Build the appropriate action.
859 switch (Phase) {
860 case phases::Link: assert(0 && "link action invalid here.");
861 case phases::Preprocess: {
Daniel Dunbar049b7242009-03-30 06:36:42 +0000862 types::ID OutputTy;
863 // -{M, MM} alter the output type.
864 if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) {
865 OutputTy = types::TY_Dependencies;
866 } else {
867 OutputTy = types::getPreprocessedType(Input->getType());
868 assert(OutputTy != types::TY_INVALID &&
869 "Cannot preprocess this input type!");
870 }
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000871 return new PreprocessJobAction(Input, OutputTy);
872 }
873 case phases::Precompile:
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000874 return new PrecompileJobAction(Input, types::TY_PCH);
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000875 case phases::Compile: {
876 if (Args.hasArg(options::OPT_fsyntax_only)) {
877 return new CompileJobAction(Input, types::TY_Nothing);
Daniel Dunbar6e3de3d2009-05-06 02:12:32 +0000878 } else if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) {
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000879 return new AnalyzeJobAction(Input, types::TY_Plist);
Daniel Dunbarec438002009-09-01 16:57:46 +0000880 } else if (Args.hasArg(options::OPT_emit_ast)) {
881 return new CompileJobAction(Input, types::TY_AST);
Daniel Dunbardec14452009-03-24 20:17:30 +0000882 } else if (Args.hasArg(options::OPT_emit_llvm) ||
883 Args.hasArg(options::OPT_flto) ||
884 Args.hasArg(options::OPT_O4)) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000885 types::ID Output =
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000886 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
887 return new CompileJobAction(Input, Output);
888 } else {
889 return new CompileJobAction(Input, types::TY_PP_Asm);
890 }
891 }
892 case phases::Assemble:
893 return new AssembleJobAction(Input, types::TY_Object);
894 }
895
896 assert(0 && "invalid phase in ConstructPhaseAction");
897 return 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000898}
899
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000900void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000901 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000902 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
903 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbarbac6b642009-03-18 23:18:19 +0000904
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000905 // FIXME: Pipes are forcibly disabled until we support executing them.
Daniel Dunbarbac6b642009-03-18 23:18:19 +0000906 if (!CCCPrintBindings)
907 UsePipes = false;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000908
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000909 // -save-temps inhibits pipes.
910 if (SaveTemps && UsePipes) {
911 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
912 UsePipes = true;
913 }
914
915 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
916
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000917 // It is an error to provide a -o option if we are making multiple output
918 // files.
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000919 if (FinalOutput) {
920 unsigned NumOutputs = 0;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000921 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000922 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000923 if ((*it)->getType() != types::TY_Nothing)
924 ++NumOutputs;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000925
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000926 if (NumOutputs > 1) {
927 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
928 FinalOutput = 0;
929 }
930 }
931
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000932 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000933 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000934 Action *A = *it;
935
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000936 // If we are linking an image for multiple archs then the linker wants
937 // -arch_multiple and -final_output <final image name>. Unfortunately, this
938 // doesn't fit in cleanly because we have to pass this information down.
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000939 //
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000940 // FIXME: This is a hack; find a cleaner way to integrate this into the
941 // process.
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000942 const char *LinkingOutput = 0;
Daniel Dunbar55e34e32009-03-26 16:12:09 +0000943 if (isa<LipoJobAction>(A)) {
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000944 if (FinalOutput)
945 LinkingOutput = FinalOutput->getValue(C.getArgs());
946 else
947 LinkingOutput = DefaultImageName.c_str();
948 }
949
950 InputInfo II;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000951 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000952 /*CanAcceptPipe*/ true,
953 /*AtTopLevel*/ true,
954 /*LinkingOutput*/ LinkingOutput,
955 II);
956 }
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000957
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000958 // If the user passed -Qunused-arguments or there were errors, don't warn
959 // about any unused arguments.
960 if (Diags.getNumErrors() ||
Daniel Dunbar1cfd8912009-04-07 19:04:18 +0000961 C.getArgs().hasArg(options::OPT_Qunused_arguments))
Daniel Dunbar46c70822009-03-18 18:03:46 +0000962 return;
963
Daniel Dunbar1e6f9ff2009-03-29 22:24:54 +0000964 // Claim -### here.
965 (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000966
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000967 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
968 it != ie; ++it) {
969 Arg *A = *it;
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000970
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000971 // FIXME: It would be nice to be able to send the argument to the
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000972 // Diagnostic, so that extra values, position, and so on could be printed.
Daniel Dunbar6c6e3f72009-04-04 00:52:26 +0000973 if (!A->isClaimed()) {
Daniel Dunbar1cfd8912009-04-07 19:04:18 +0000974 if (A->getOption().hasNoArgumentUnused())
975 continue;
976
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000977 // Suppress the warning automatically if this is just a flag, and it is an
978 // instance of an argument we already claimed.
Daniel Dunbar6c6e3f72009-04-04 00:52:26 +0000979 const Option &Opt = A->getOption();
980 if (isa<FlagOption>(Opt)) {
981 bool DuplicateClaimed = false;
982
983 // FIXME: Use iterator.
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000984 for (ArgList::const_iterator it = C.getArgs().begin(),
Daniel Dunbar6c6e3f72009-04-04 00:52:26 +0000985 ie = C.getArgs().end(); it != ie; ++it) {
986 if ((*it)->isClaimed() && (*it)->getOption().matches(Opt.getId())) {
987 DuplicateClaimed = true;
988 break;
989 }
990 }
991
992 if (DuplicateClaimed)
993 continue;
994 }
995
Daniel Dunbar84f695a2009-09-08 23:36:43 +0000996 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar73225932009-03-20 06:14:23 +0000997 << A->getAsString(C.getArgs());
Daniel Dunbar6c6e3f72009-04-04 00:52:26 +0000998 }
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000999 }
Daniel Dunbar47d762e2009-03-13 22:12:33 +00001000}
1001
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001002void Driver::BuildJobsForAction(Compilation &C,
1003 const Action *A,
1004 const ToolChain *TC,
1005 bool CanAcceptPipe,
1006 bool AtTopLevel,
1007 const char *LinkingOutput,
1008 InputInfo &Result) const {
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001009 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbarbac6b642009-03-18 23:18:19 +00001010
1011 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001012 // FIXME: Pipes are forcibly disabled until we support executing them.
Daniel Dunbarbac6b642009-03-18 23:18:19 +00001013 if (!CCCPrintBindings)
1014 UsePipes = false;
1015
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001016 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001017 // FIXME: It would be nice to not claim this here; maybe the old scheme of
1018 // just using Args was better?
Daniel Dunbarc3be67b2009-03-19 07:29:38 +00001019 const Arg &Input = IA->getInputArg();
1020 Input.claim();
1021 if (isa<PositionalArg>(Input)) {
1022 const char *Name = Input.getValue(C.getArgs());
1023 Result = InputInfo(Name, A->getType(), Name);
1024 } else
1025 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001026 return;
1027 }
1028
1029 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar31a70e92009-09-08 23:37:19 +00001030 const ToolChain *TC = &C.getDefaultToolChain();
1031
Daniel Dunbar08303652009-05-22 02:53:45 +00001032 std::string Arch;
Daniel Dunbar31a70e92009-09-08 23:37:19 +00001033 if (BAA->getArchName())
1034 TC = Host->CreateToolChain(C.getArgs(), BAA->getArchName());
1035
1036 BuildJobsForAction(C, *BAA->begin(), TC, CanAcceptPipe, AtTopLevel,
1037 LinkingOutput, Result);
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001038 return;
1039 }
1040
1041 const JobAction *JA = cast<JobAction>(A);
1042 const Tool &T = TC->SelectTool(C, *JA);
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001043
1044 // See if we should use an integrated preprocessor. We do so when we have
1045 // exactly one input, since this is the only use case we care about
1046 // (irrelevant since we don't support combine yet).
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001047 bool UseIntegratedCPP = false;
1048 const ActionList *Inputs = &A->getInputs();
1049 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
1050 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
1051 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
1052 !C.getArgs().hasArg(options::OPT_save_temps) &&
1053 T.hasIntegratedCPP()) {
1054 UseIntegratedCPP = true;
1055 Inputs = &(*Inputs)[0]->getInputs();
1056 }
1057 }
1058
1059 // Only use pipes when there is exactly one input.
1060 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar00fd3792009-03-18 06:00:36 +00001061 InputInfoList InputInfos;
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001062 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
1063 it != ie; ++it) {
1064 InputInfo II;
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001065 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001066 /*AtTopLevel*/false,
1067 LinkingOutput,
1068 II);
1069 InputInfos.push_back(II);
1070 }
1071
1072 // Determine if we should output to a pipe.
1073 bool OutputToPipe = false;
1074 if (CanAcceptPipe && T.canPipeOutput()) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001075 // Some actions default to writing to a pipe if they are the top level phase
1076 // and there was no user override.
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001077 //
1078 // FIXME: Is there a better way to handle this?
1079 if (AtTopLevel) {
1080 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
1081 OutputToPipe = true;
Daniel Dunbarbac6b642009-03-18 23:18:19 +00001082 } else if (UsePipes)
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001083 OutputToPipe = true;
1084 }
1085
1086 // Figure out where to put the job (pipes).
1087 Job *Dest = &C.getJobs();
1088 if (InputInfos[0].isPipe()) {
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001089 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001090 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
1091 Dest = &InputInfos[0].getPipe();
1092 }
1093
1094 // Always use the first input as the base input.
1095 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001096
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001097 // Determine the place to write output to (nothing, pipe, or filename) and
1098 // where to put the new job.
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001099 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001100 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001101 } else if (OutputToPipe) {
1102 // Append to current piped job or create a new one as appropriate.
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001103 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
1104 if (!PJ) {
1105 PJ = new PipedJob();
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001106 // FIXME: Temporary hack so that -ccc-print-bindings work until we have
1107 // pipe support. Please remove later.
Daniel Dunbar8eeb5a32009-03-20 00:11:04 +00001108 if (!CCCPrintBindings)
1109 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar933ac452009-03-18 07:06:02 +00001110 Dest = PJ;
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001111 }
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001112 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001113 } else {
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001114 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
1115 A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001116 }
1117
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001118 if (CCCPrintBindings) {
Daniel Dunbar049b7242009-03-30 06:36:42 +00001119 llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"'
1120 << " - \"" << T.getName() << "\", inputs: [";
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001121 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
1122 llvm::errs() << InputInfos[i].getAsString();
1123 if (i + 1 != e)
1124 llvm::errs() << ", ";
1125 }
1126 llvm::errs() << "], output: " << Result.getAsString() << "\n";
1127 } else {
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001128 T.ConstructJob(C, *JA, *Dest, Result, InputInfos,
Daniel Dunbara16e4fe2009-03-25 04:13:45 +00001129 C.getArgsForToolChain(TC), LinkingOutput);
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +00001130 }
Daniel Dunbar7ce6add2009-03-16 06:56:51 +00001131}
1132
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001133const char *Driver::GetNamedOutputPath(Compilation &C,
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001134 const JobAction &JA,
1135 const char *BaseInput,
1136 bool AtTopLevel) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +00001137 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001138 // Output to a user requested destination?
1139 if (AtTopLevel) {
1140 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
1141 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
1142 }
1143
1144 // Output to a temporary file?
1145 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001146 std::string TmpName =
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001147 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
1148 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001149 }
1150
1151 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbarfb5e3332009-03-18 02:00:31 +00001152 std::string BaseName(BasePath.getLast());
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001153
1154 // Determine what the derived output name should be.
1155 const char *NamedOutput;
1156 if (JA.getType() == types::TY_Image) {
1157 NamedOutput = DefaultImageName.c_str();
1158 } else {
1159 const char *Suffix = types::getTypeTempSuffix(JA.getType());
1160 assert(Suffix && "All types used for output should have a suffix.");
1161
1162 std::string::size_type End = std::string::npos;
1163 if (!types::appendSuffixForType(JA.getType()))
1164 End = BaseName.rfind('.');
1165 std::string Suffixed(BaseName.substr(0, End));
1166 Suffixed += '.';
1167 Suffixed += Suffix;
1168 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
1169 }
1170
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001171 // As an annoying special case, PCH generation doesn't strip the pathname.
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001172 if (JA.getType() == types::TY_PCH) {
1173 BasePath.eraseComponent();
Daniel Dunbar58a51262009-03-18 09:58:30 +00001174 if (BasePath.isEmpty())
1175 BasePath = NamedOutput;
1176 else
1177 BasePath.appendComponent(NamedOutput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001178 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
1179 } else {
1180 return C.addResultFile(NamedOutput);
1181 }
1182}
1183
Daniel Dunbare1cef7d2009-03-16 05:25:36 +00001184llvm::sys::Path Driver::GetFilePath(const char *Name,
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +00001185 const ToolChain &TC) const {
Daniel Dunbarcc7600c2009-03-18 20:26:19 +00001186 const ToolChain::path_list &List = TC.getFilePaths();
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001187 for (ToolChain::path_list::const_iterator
Daniel Dunbarcc7600c2009-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())
1192 return P;
1193 }
1194
Daniel Dunbarcc006892009-03-13 00:51:18 +00001195 return llvm::sys::Path(Name);
1196}
1197
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001198llvm::sys::Path Driver::GetProgramPath(const char *Name,
Mike Stump32b1bbe2009-03-27 00:40:20 +00001199 const ToolChain &TC,
1200 bool WantFile) const {
Daniel Dunbarcc7600c2009-03-18 20:26:19 +00001201 const ToolChain::path_list &List = TC.getProgramPaths();
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001202 for (ToolChain::path_list::const_iterator
Daniel Dunbarcc7600c2009-03-18 20:26:19 +00001203 it = List.begin(), ie = List.end(); it != ie; ++it) {
1204 llvm::sys::Path P(*it);
1205 P.appendComponent(Name);
Mike Stump32b1bbe2009-03-27 00:40:20 +00001206 if (WantFile ? P.exists() : P.canExecute())
Daniel Dunbarcc7600c2009-03-18 20:26:19 +00001207 return P;
1208 }
1209
Daniel Dunbar49a35de2009-03-23 16:15:50 +00001210 // If all else failed, search the path.
1211 llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
Daniel Dunbarcb84b9a2009-03-18 21:34:08 +00001212 if (!P.empty())
1213 return P;
1214
Daniel Dunbarcc006892009-03-13 00:51:18 +00001215 return llvm::sys::Path(Name);
1216}
1217
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001218std::string Driver::GetTemporaryPath(const char *Suffix) const {
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001219 // FIXME: This is lame; sys::Path should provide this function (in particular,
1220 // it should know how to find the temporary files dir).
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001221 std::string Error;
Daniel Dunbar02e396a2009-04-20 20:28:21 +00001222 const char *TmpDir = ::getenv("TMPDIR");
1223 if (!TmpDir)
1224 TmpDir = ::getenv("TEMP");
1225 if (!TmpDir)
Daniel Dunbara77aa002009-04-21 00:25:10 +00001226 TmpDir = ::getenv("TMP");
1227 if (!TmpDir)
Daniel Dunbar02e396a2009-04-20 20:28:21 +00001228 TmpDir = "/tmp";
1229 llvm::sys::Path P(TmpDir);
Daniel Dunbar121f2512009-04-20 17:32:49 +00001230 P.appendComponent("cc");
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001231 if (P.makeUnique(false, &Error)) {
1232 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
1233 return "";
1234 }
1235
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001236 // FIXME: Grumble, makeUnique sometimes leaves the file around!? PR3837.
Daniel Dunbar9083abe2009-03-18 23:08:52 +00001237 P.eraseFromDisk(false, 0);
1238
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001239 P.appendSuffix(Suffix);
Chris Lattner31bc3042009-08-23 22:45:33 +00001240 return P.str();
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001241}
1242
Daniel Dunbar08303652009-05-22 02:53:45 +00001243const HostInfo *Driver::GetHostInfo(const char *TripleStr) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +00001244 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbar08303652009-05-22 02:53:45 +00001245 llvm::Triple Triple(TripleStr);
Daniel Dunbard25acaa2009-03-10 23:41:59 +00001246
Daniel Dunbar08303652009-05-22 02:53:45 +00001247 switch (Triple.getOS()) {
Edward O'Callaghanb68ac562009-08-22 01:06:46 +00001248 case llvm::Triple::AuroraUX:
1249 return createAuroraUXHostInfo(*this, Triple);
Daniel Dunbar08303652009-05-22 02:53:45 +00001250 case llvm::Triple::Darwin:
1251 return createDarwinHostInfo(*this, Triple);
1252 case llvm::Triple::DragonFly:
1253 return createDragonFlyHostInfo(*this, Triple);
Daniel Dunbar64c77a12009-06-29 20:52:51 +00001254 case llvm::Triple::OpenBSD:
1255 return createOpenBSDHostInfo(*this, Triple);
Daniel Dunbar08303652009-05-22 02:53:45 +00001256 case llvm::Triple::FreeBSD:
1257 return createFreeBSDHostInfo(*this, Triple);
Eli Friedman8cac4352009-05-26 07:52:18 +00001258 case llvm::Triple::Linux:
1259 return createLinuxHostInfo(*this, Triple);
Daniel Dunbar08303652009-05-22 02:53:45 +00001260 default:
1261 return createUnknownHostInfo(*this, Triple);
1262 }
Daniel Dunbard25acaa2009-03-10 23:41:59 +00001263}
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001264
1265bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
Daniel Dunbara28b9d42009-09-08 23:36:55 +00001266 const llvm::Triple &Triple) const {
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001267 // Check if user requested no clang, or clang doesn't understand this type (we
1268 // only handle single inputs for now).
Daniel Dunbarec438002009-09-01 16:57:46 +00001269 if (!CCCUseClang || JA.size() != 1 ||
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001270 !types::isAcceptedByClang((*JA.begin())->getType()))
1271 return false;
1272
Daniel Dunbar0ea85f72009-03-24 19:02:31 +00001273 // Otherwise make sure this is an action clang understands.
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001274 if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001275 if (!CCCUseClangCPP) {
1276 Diag(clang::diag::warn_drv_not_using_clang_cpp);
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001277 return false;
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001278 }
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001279 } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
1280 return false;
1281
Daniel Dunbar0ea85f72009-03-24 19:02:31 +00001282 // Use clang for C++?
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001283 if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
1284 Diag(clang::diag::warn_drv_not_using_clang_cxx);
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001285 return false;
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001286 }
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001287
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001288 // Always use clang for precompiling and AST generation, regardless of archs.
Daniel Dunbarec438002009-09-01 16:57:46 +00001289 if (isa<PrecompileJobAction>(JA) || JA.getType() == types::TY_AST)
Daniel Dunbar5a34ca82009-04-16 23:10:13 +00001290 return true;
1291
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001292 // Finally, don't use clang if this isn't one of the user specified archs to
1293 // build.
Daniel Dunbara28b9d42009-09-08 23:36:55 +00001294 if (!CCCClangArchs.empty() && !CCCClangArchs.count(Triple.getArch())) {
1295 Diag(clang::diag::warn_drv_not_using_clang_arch) << Triple.getArchName();
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001296 return false;
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001297 }
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001298
1299 return true;
1300}
Daniel Dunbar9cdd4d42009-03-26 15:58:36 +00001301
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001302/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the
1303/// grouped values as integers. Numbers which are not provided are set to 0.
Daniel Dunbar9cdd4d42009-03-26 15:58:36 +00001304///
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001305/// \return True if the entire string was parsed (9.2), or all groups were
1306/// parsed (10.3.5extrastuff).
1307bool Driver::GetReleaseVersion(const char *Str, unsigned &Major,
Daniel Dunbar9cdd4d42009-03-26 15:58:36 +00001308 unsigned &Minor, unsigned &Micro,
1309 bool &HadExtra) {
1310 HadExtra = false;
1311
1312 Major = Minor = Micro = 0;
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001313 if (*Str == '\0')
Daniel Dunbar9cdd4d42009-03-26 15:58:36 +00001314 return true;
1315
1316 char *End;
1317 Major = (unsigned) strtol(Str, &End, 10);
1318 if (*Str != '\0' && *End == '\0')
1319 return true;
1320 if (*End != '.')
1321 return false;
Daniel Dunbar84f695a2009-09-08 23:36:43 +00001322
Daniel Dunbar9cdd4d42009-03-26 15:58:36 +00001323 Str = End+1;
1324 Minor = (unsigned) strtol(Str, &End, 10);
1325 if (*Str != '\0' && *End == '\0')
1326 return true;
1327 if (*End != '.')
1328 return false;
1329
1330 Str = End+1;
1331 Micro = (unsigned) strtol(Str, &End, 10);
1332 if (*Str != '\0' && *End == '\0')
1333 return true;
1334 if (Str == End)
1335 return false;
1336 HadExtra = true;
1337 return true;
1338}