blob: 7c21594e6fe6b626c33c0499a2bca98d7a838c75 [file] [log] [blame]
Daniel Dunbar544ecd12009-03-02 19:59:07 +00001//===--- Driver.cpp - Clang GCC Compatible Driver -----------------------*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Dunbar544ecd12009-03-02 19:59:07 +000010#include "clang/Driver/Driver.h"
Daniel Dunbar544ecd12009-03-02 19:59:07 +000011
Daniel Dunbar1688f1a2009-03-12 07:58:46 +000012#include "clang/Driver/Action.h"
Daniel Dunbarb2cd66b2009-03-04 20:49:20 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
15#include "clang/Driver/Compilation.h"
Daniel Dunbarc0b3e952009-03-12 08:55:43 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar4dff6a42009-03-10 23:41:59 +000017#include "clang/Driver/HostInfo.h"
Daniel Dunbare75d8342009-03-16 06:56:51 +000018#include "clang/Driver/Job.h"
Daniel Dunbaraa767372009-11-19 00:15:11 +000019#include "clang/Driver/OptTable.h"
Daniel Dunbard02cb1d2009-03-05 06:38:47 +000020#include "clang/Driver/Option.h"
Daniel Dunbarb2cd66b2009-03-04 20:49:20 +000021#include "clang/Driver/Options.h"
Daniel Dunbare75d8342009-03-16 06:56:51 +000022#include "clang/Driver/Tool.h"
23#include "clang/Driver/ToolChain.h"
Daniel Dunbar1688f1a2009-03-12 07:58:46 +000024#include "clang/Driver/Types.h"
Daniel Dunbard02cb1d2009-03-05 06:38:47 +000025
Douglas Gregor7b71e632009-04-27 22:23:34 +000026#include "clang/Basic/Version.h"
27
Daniel Dunbare5dc4822009-03-13 20:33:35 +000028#include "llvm/ADT/StringSet.h"
Daniel Dunbar2608c542009-03-18 01:38:48 +000029#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbard02cb1d2009-03-05 06:38:47 +000030#include "llvm/Support/raw_ostream.h"
Daniel Dunbar1688f1a2009-03-12 07:58:46 +000031#include "llvm/System/Path.h"
Daniel Dunbar6f668772009-03-18 21:34:08 +000032#include "llvm/System/Program.h"
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +000033
Daniel Dunbare75d8342009-03-16 06:56:51 +000034#include "InputInfo.h"
35
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +000036#include <map>
37
Daniel Dunbarb2cd66b2009-03-04 20:49:20 +000038using namespace clang::driver;
Chris Lattnerada1c912009-03-26 05:56:24 +000039using namespace clang;
Daniel Dunbarb2cd66b2009-03-04 20:49:20 +000040
Daniel Dunbarb5bcd6b2009-08-23 18:42:54 +000041// Used to set values for "production" clang, for releases.
Daniel Dunbarccc60da2009-08-23 19:41:53 +000042// #define USE_PRODUCTION_CLANG
Daniel Dunbarb5bcd6b2009-08-23 18:42:54 +000043
Daniel Dunbar4dff6a42009-03-10 23:41:59 +000044Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbarc0b3e952009-03-12 08:55:43 +000045 const char *_DefaultHostTriple,
Daniel Dunbare75d8342009-03-16 06:56:51 +000046 const char *_DefaultImageName,
Daniel Dunbar5564ba72009-09-22 22:31:13 +000047 bool IsProduction, Diagnostic &_Diags)
Daniel Dunbar26228a02009-11-18 20:19:36 +000048 : Opts(createDriverOptTable()), Diags(_Diags),
Daniel Dunbar4dff6a42009-03-10 23:41:59 +000049 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
Daniel Dunbare75d8342009-03-16 06:56:51 +000050 DefaultImageName(_DefaultImageName),
Daniel Dunbar4dff6a42009-03-10 23:41:59 +000051 Host(0),
Daniel Dunbarb39cc522009-03-17 22:47:06 +000052 CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false),
Daniel Dunbarb5bcd6b2009-08-23 18:42:54 +000053 CCCGenericGCCName("gcc"), CCCUseClang(true),
Daniel Dunbar5564ba72009-09-22 22:31:13 +000054 CCCUseClangCXX(true), CCCUseClangCPP(true), CCCUsePCH(true),
Mike Stump11289f42009-09-09 15:08:12 +000055 SuppressMissingInputWarning(false) {
Daniel Dunbar5564ba72009-09-22 22:31:13 +000056 if (IsProduction) {
57 // In a "production" build, only use clang on architectures we expect to
58 // work, and don't use clang C++.
59 //
60 // During development its more convenient to always have the driver use
61 // clang, but we don't want users to be confused when things don't work, or
62 // to file bugs for things we don't support.
63 CCCClangArchs.insert(llvm::Triple::x86);
64 CCCClangArchs.insert(llvm::Triple::x86_64);
65 CCCClangArchs.insert(llvm::Triple::arm);
66
67 CCCUseClangCXX = false;
68 }
Daniel Dunbar544ecd12009-03-02 19:59:07 +000069}
70
71Driver::~Driver() {
Daniel Dunbarb2cd66b2009-03-04 20:49:20 +000072 delete Opts;
Daniel Dunbar01601722009-03-18 01:09:40 +000073 delete Host;
Daniel Dunbar544ecd12009-03-02 19:59:07 +000074}
75
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +000076InputArgList *Driver::ParseArgStrings(const char **ArgBegin,
Daniel Dunbardac54a82009-03-25 04:13:45 +000077 const char **ArgEnd) {
Daniel Dunbar2608c542009-03-18 01:38:48 +000078 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbar52ed5fe2009-11-19 06:35:06 +000079 unsigned MissingArgIndex, MissingArgCount;
80 InputArgList *Args = getOpts().ParseArgs(ArgBegin, ArgEnd,
81 MissingArgIndex, MissingArgCount);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +000082
Daniel Dunbar52ed5fe2009-11-19 06:35:06 +000083 // Check for missing argument error.
84 if (MissingArgCount)
85 Diag(clang::diag::err_drv_missing_argument)
86 << Args->getArgString(MissingArgIndex) << MissingArgCount;
Daniel Dunbar65229332009-03-13 11:38:42 +000087
Daniel Dunbar52ed5fe2009-11-19 06:35:06 +000088 // Check for unsupported options.
89 for (ArgList::const_iterator it = Args->begin(), ie = Args->end();
90 it != ie; ++it) {
91 Arg *A = *it;
Daniel Dunbard8500f32009-03-22 23:26:43 +000092 if (A->getOption().isUnsupported()) {
93 Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
94 continue;
95 }
Daniel Dunbard02cb1d2009-03-05 06:38:47 +000096 }
97
98 return Args;
99}
100
Daniel Dunbar544ecd12009-03-02 19:59:07 +0000101Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar2608c542009-03-18 01:38:48 +0000102 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
103
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000104 // FIXME: Handle environment options which effect driver behavior, somewhere
105 // (client?). GCC_EXEC_PREFIX, COMPILER_PATH, LIBRARY_PATH, LPATH,
106 // CC_PRINT_OPTIONS.
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000107
108 // FIXME: What are we going to do with -V and -b?
109
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000110 // FIXME: This stuff needs to go into the Compilation, not the driver.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000111 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000112
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000113 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbar4dff6a42009-03-10 23:41:59 +0000114 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000115
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000116 // Read -ccc args.
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000117 //
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000118 // FIXME: We need to figure out where this behavior should live. Most of it
119 // should be outside in the client; the parts that aren't should have proper
120 // options, either by introducing new ones or by overloading gcc ones like -V
121 // or -b.
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000122 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
123 const char *Opt = *Start + 5;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000124
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000125 if (!strcmp(Opt, "print-options")) {
126 CCCPrintOptions = true;
127 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000128 CCCPrintActions = true;
Daniel Dunbarb39cc522009-03-17 22:47:06 +0000129 } else if (!strcmp(Opt, "print-bindings")) {
130 CCCPrintBindings = true;
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000131 } else if (!strcmp(Opt, "cxx")) {
132 CCCIsCXX = true;
133 } else if (!strcmp(Opt, "echo")) {
134 CCCEcho = true;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000135
Daniel Dunbar7803c952009-04-01 23:34:41 +0000136 } else if (!strcmp(Opt, "gcc-name")) {
137 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
138 CCCGenericGCCName = *++Start;
139
Daniel Dunbar88f356e2009-03-24 19:02:31 +0000140 } else if (!strcmp(Opt, "clang-cxx")) {
141 CCCUseClangCXX = true;
Daniel Dunbar07b74922009-07-23 17:48:59 +0000142 } else if (!strcmp(Opt, "no-clang-cxx")) {
143 CCCUseClangCXX = false;
Douglas Gregor111af7d2009-04-18 00:34:01 +0000144 } else if (!strcmp(Opt, "pch-is-pch")) {
145 CCCUsePCH = true;
146 } else if (!strcmp(Opt, "pch-is-pth")) {
147 CCCUsePCH = false;
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000148 } else if (!strcmp(Opt, "no-clang")) {
Daniel Dunbar88f356e2009-03-24 19:02:31 +0000149 CCCUseClang = false;
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000150 } else if (!strcmp(Opt, "no-clang-cpp")) {
Daniel Dunbar88f356e2009-03-24 19:02:31 +0000151 CCCUseClangCPP = false;
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000152 } else if (!strcmp(Opt, "clang-archs")) {
153 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbar953b8d12009-09-08 23:36:55 +0000154 llvm::StringRef Cur = *++Start;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000155
Daniel Dunbar88f356e2009-03-24 19:02:31 +0000156 CCCClangArchs.clear();
Daniel Dunbar953b8d12009-09-08 23:36:55 +0000157 while (!Cur.empty()) {
158 std::pair<llvm::StringRef, llvm::StringRef> Split = Cur.split(',');
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000159
Daniel Dunbar953b8d12009-09-08 23:36:55 +0000160 if (!Split.first.empty()) {
161 llvm::Triple::ArchType Arch =
162 llvm::Triple(Split.first, "", "").getArch();
163
164 if (Arch == llvm::Triple::UnknownArch) {
165 // FIXME: Error handling.
166 llvm::errs() << "invalid arch name: " << Split.first << "\n";
167 exit(1);
168 }
169
170 CCCClangArchs.insert(Arch);
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000171 }
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000172
Daniel Dunbar953b8d12009-09-08 23:36:55 +0000173 Cur = Split.second;
174 }
Daniel Dunbar4dff6a42009-03-10 23:41:59 +0000175 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000176 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbar4dff6a42009-03-10 23:41:59 +0000177 HostTriple = *++Start;
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000178
Daniel Dunbar5ed07fe2009-09-04 18:35:03 +0000179 } else if (!strcmp(Opt, "install-dir")) {
180 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
181 Dir = *++Start;
182
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000183 } else {
184 // FIXME: Error handling.
185 llvm::errs() << "invalid option: " << *Start << "\n";
186 exit(1);
187 }
188 }
Daniel Dunbar4dff6a42009-03-10 23:41:59 +0000189
Daniel Dunbardac54a82009-03-25 04:13:45 +0000190 InputArgList *Args = ParseArgStrings(Start, End);
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000191
Daniel Dunbar52e0c702009-03-17 20:45:45 +0000192 Host = GetHostInfo(HostTriple);
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000193
Daniel Dunbar3ce436d2009-03-16 06:42:30 +0000194 // The compilation takes ownership of Args.
Daniel Dunbar1ef3f2a2009-09-08 23:37:19 +0000195 Compilation *C = new Compilation(*this, *Host->CreateToolChain(*Args), Args);
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000196
197 // FIXME: This behavior shouldn't be here.
198 if (CCCPrintOptions) {
199 PrintOptions(C->getArgs());
200 return C;
201 }
202
203 if (!HandleImmediateArgs(*C))
204 return C;
205
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000206 // Construct the list of abstract actions to perform for this compilation. We
207 // avoid passing a Compilation here simply to enforce the abstraction that
208 // pipelining is not host or toolchain dependent (other than the driver driver
209 // test).
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000210 if (Host->useDriverDriver())
211 BuildUniversalActions(C->getArgs(), C->getActions());
212 else
213 BuildActions(C->getArgs(), C->getActions());
214
215 if (CCCPrintActions) {
Daniel Dunbareb843be2009-03-18 03:13:20 +0000216 PrintActions(*C);
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000217 return C;
218 }
219
220 BuildJobs(*C);
Daniel Dunbaradc91e62009-03-15 01:38:15 +0000221
222 return C;
Daniel Dunbaree66cf22009-03-10 20:52:46 +0000223}
224
Daniel Dunbar38bfda62009-07-01 20:03:04 +0000225int Driver::ExecuteCompilation(const Compilation &C) const {
226 // Just print if -### was present.
227 if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
228 C.PrintJob(llvm::errs(), C.getJobs(), "\n", true);
229 return 0;
230 }
231
232 // If there were errors building the compilation, quit now.
233 if (getDiags().getNumErrors())
234 return 1;
235
236 const Command *FailingCommand = 0;
237 int Res = C.ExecuteJob(C.getJobs(), FailingCommand);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000238
Daniel Dunbar38bfda62009-07-01 20:03:04 +0000239 // Remove temp files.
240 C.CleanupFileList(C.getTempFiles());
241
242 // If the compilation failed, remove result files as well.
243 if (Res != 0 && !C.getArgs().hasArg(options::OPT_save_temps))
244 C.CleanupFileList(C.getResultFiles(), true);
245
246 // Print extra information about abnormal failures, if possible.
247 if (Res) {
248 // This is ad-hoc, but we don't want to be excessively noisy. If the result
249 // status was 1, assume the command failed normally. In particular, if it
250 // was the compiler then assume it gave a reasonable error code. Failures in
251 // other tools are less common, and they generally have worse diagnostics,
252 // so always print the diagnostic there.
253 const Action &Source = FailingCommand->getSource();
254 bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) ||
255 isa<PrecompileJobAction>(Source) ||
256 isa<AnalyzeJobAction>(Source) ||
257 isa<CompileJobAction>(Source));
258
259 if (!IsFriendlyTool || Res != 1) {
260 // FIXME: See FIXME above regarding result code interpretation.
261 if (Res < 0)
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000262 Diag(clang::diag::err_drv_command_signalled)
Daniel Dunbar38bfda62009-07-01 20:03:04 +0000263 << Source.getClassName() << -Res;
264 else
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000265 Diag(clang::diag::err_drv_command_failed)
Daniel Dunbar38bfda62009-07-01 20:03:04 +0000266 << Source.getClassName() << Res;
267 }
268 }
269
270 return Res;
271}
272
Daniel Dunbar446f6842009-03-12 18:24:49 +0000273void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000274 unsigned i = 0;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000275 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000276 it != ie; ++it, ++i) {
277 Arg *A = *it;
278 llvm::errs() << "Option " << i << " - "
279 << "Name: \"" << A->getOption().getName() << "\", "
280 << "Values: {";
281 for (unsigned j = 0; j < A->getNumValues(); ++j) {
282 if (j)
283 llvm::errs() << ", ";
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000284 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000285 }
286 llvm::errs() << "}\n";
Daniel Dunbard02cb1d2009-03-05 06:38:47 +0000287 }
Daniel Dunbar544ecd12009-03-02 19:59:07 +0000288}
Daniel Dunbar4dff6a42009-03-10 23:41:59 +0000289
Daniel Dunbar7c925282009-03-31 21:38:17 +0000290static std::string getOptionHelpName(const OptTable &Opts, options::ID Id) {
291 std::string Name = Opts.getOptionName(Id);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000292
Daniel Dunbar7c925282009-03-31 21:38:17 +0000293 // Add metavar, if used.
294 switch (Opts.getOptionKind(Id)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000295 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
Daniel Dunbar7c925282009-03-31 21:38:17 +0000296 assert(0 && "Invalid option with help text.");
297
298 case Option::MultiArgClass: case Option::JoinedAndSeparateClass:
299 assert(0 && "Cannot print metavar for this kind of option.");
300
301 case Option::FlagClass:
302 break;
303
304 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
305 Name += ' ';
306 // FALLTHROUGH
307 case Option::JoinedClass: case Option::CommaJoinedClass:
308 Name += Opts.getOptionMetaVar(Id);
309 break;
310 }
311
312 return Name;
313}
314
Daniel Dunbar65b99522009-12-03 07:01:38 +0000315// FIXME: Move -ccc options to real options in the .td file (or eliminate), and
316// then move to using OptTable::PrintHelp.
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000317void Driver::PrintHelp(bool ShowHidden) const {
Daniel Dunbar7c925282009-03-31 21:38:17 +0000318 llvm::raw_ostream &OS = llvm::outs();
319
320 OS << "OVERVIEW: clang \"gcc-compatible\" driver\n";
321 OS << '\n';
322 OS << "USAGE: " << Name << " [options] <input files>\n";
323 OS << '\n';
324 OS << "OPTIONS:\n";
325
326 // Render help text into (option, help) pairs.
327 std::vector< std::pair<std::string, const char*> > OptionHelp;
328
Daniel Dunbarda13faf2009-11-19 04:25:22 +0000329 for (unsigned i = 0, e = getOpts().getNumOptions(); i != e; ++i) {
330 options::ID Id = (options::ID) (i + 1);
Daniel Dunbar7c925282009-03-31 21:38:17 +0000331 if (const char *Text = getOpts().getOptionHelpText(Id))
332 OptionHelp.push_back(std::make_pair(getOptionHelpName(getOpts(), Id),
333 Text));
334 }
335
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000336 if (ShowHidden) {
337 OptionHelp.push_back(std::make_pair("\nDRIVER OPTIONS:",""));
338 OptionHelp.push_back(std::make_pair("-ccc-cxx",
339 "Act as a C++ driver"));
340 OptionHelp.push_back(std::make_pair("-ccc-gcc-name",
341 "Name for native GCC compiler"));
342 OptionHelp.push_back(std::make_pair("-ccc-clang-cxx",
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +0000343 "Enable the clang compiler for C++"));
344 OptionHelp.push_back(std::make_pair("-ccc-no-clang-cxx",
345 "Disable the clang compiler for C++"));
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000346 OptionHelp.push_back(std::make_pair("-ccc-no-clang",
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +0000347 "Disable the clang compiler"));
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000348 OptionHelp.push_back(std::make_pair("-ccc-no-clang-cpp",
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +0000349 "Disable the clang preprocessor"));
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000350 OptionHelp.push_back(std::make_pair("-ccc-clang-archs",
351 "Comma separate list of architectures "
352 "to use the clang compiler for"));
Douglas Gregor111af7d2009-04-18 00:34:01 +0000353 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pch",
354 "Use lazy PCH for precompiled headers"));
355 OptionHelp.push_back(std::make_pair("-ccc-pch-is-pth",
356 "Use pretokenized headers for precompiled headers"));
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000357
358 OptionHelp.push_back(std::make_pair("\nDEBUG/DEVELOPMENT OPTIONS:",""));
359 OptionHelp.push_back(std::make_pair("-ccc-host-triple",
Daniel Dunbar5ed07fe2009-09-04 18:35:03 +0000360 "Simulate running on the given target"));
361 OptionHelp.push_back(std::make_pair("-ccc-install-dir",
362 "Simulate installation in the given directory"));
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000363 OptionHelp.push_back(std::make_pair("-ccc-print-options",
364 "Dump parsed command line arguments"));
365 OptionHelp.push_back(std::make_pair("-ccc-print-phases",
366 "Dump list of actions to perform"));
367 OptionHelp.push_back(std::make_pair("-ccc-print-bindings",
368 "Show bindings of tools to actions"));
369 OptionHelp.push_back(std::make_pair("CCC_ADD_ARGS",
370 "(ENVIRONMENT VARIABLE) Comma separated list of "
371 "arguments to prepend to the command line"));
372 }
373
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000374 // Find the maximum option length.
Daniel Dunbar7c925282009-03-31 21:38:17 +0000375 unsigned OptionFieldWidth = 0;
376 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000377 // Skip titles.
378 if (!OptionHelp[i].second)
379 continue;
380
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000381 // Limit the amount of padding we are willing to give up for alignment.
Daniel Dunbar7c925282009-03-31 21:38:17 +0000382 unsigned Length = OptionHelp[i].first.size();
383 if (Length <= 23)
384 OptionFieldWidth = std::max(OptionFieldWidth, Length);
385 }
386
387 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
388 const std::string &Option = OptionHelp[i].first;
389 OS << " " << Option;
390 for (int j = Option.length(), e = OptionFieldWidth; j < e; ++j)
391 OS << ' ';
392 OS << ' ' << OptionHelp[i].second << '\n';
393 }
394
395 OS.flush();
396}
397
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000398void Driver::PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000399 // FIXME: The following handlers should use a callback mechanism, we don't
400 // know what the client would like to do.
Mike Stump727170d2009-10-09 17:31:54 +0000401#ifdef CLANG_VENDOR
402 OS << CLANG_VENDOR;
403#endif
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000404 OS << "clang version " CLANG_VERSION_STRING " ("
Douglas Gregor1b7035d2009-10-05 20:33:49 +0000405 << getClangSubversionPath();
406 if (unsigned Revision = getClangSubversionRevision())
407 OS << " " << Revision;
408 OS << ")" << '\n';
Daniel Dunbarb10248802009-03-26 16:09:13 +0000409
410 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000411 OS << "Target: " << TC.getTripleString() << '\n';
Daniel Dunbar10978e42009-06-16 23:32:58 +0000412
413 // Print the threading model.
414 //
415 // FIXME: Implement correctly.
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000416 OS << "Thread model: " << "posix" << '\n';
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000417}
418
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000419bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000420 // The order these options are handled in in gcc is all over the place, but we
421 // don't expect inconsistencies w.r.t. that to matter in practice.
Daniel Dunbar7c925282009-03-31 21:38:17 +0000422
Daniel Dunbara9bbcfa2009-04-04 05:17:38 +0000423 if (C.getArgs().hasArg(options::OPT_dumpversion)) {
Douglas Gregor7b71e632009-04-27 22:23:34 +0000424 llvm::outs() << CLANG_VERSION_STRING "\n";
Daniel Dunbara9bbcfa2009-04-04 05:17:38 +0000425 return false;
426 }
427
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000428 if (C.getArgs().hasArg(options::OPT__help) ||
Daniel Dunbara7b5e212009-04-15 16:34:29 +0000429 C.getArgs().hasArg(options::OPT__help_hidden)) {
430 PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden));
Daniel Dunbar7c925282009-03-31 21:38:17 +0000431 return false;
432 }
433
Daniel Dunbarb0006ae2009-04-02 15:05:41 +0000434 if (C.getArgs().hasArg(options::OPT__version)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000435 // Follow gcc behavior and use stdout for --version and stderr for -v.
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000436 PrintVersion(C, llvm::outs());
Daniel Dunbarb0006ae2009-04-02 15:05:41 +0000437 return false;
438 }
439
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000440 if (C.getArgs().hasArg(options::OPT_v) ||
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000441 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbar08e41d62009-07-21 20:06:58 +0000442 PrintVersion(C, llvm::errs());
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000443 SuppressMissingInputWarning = true;
444 }
445
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000446 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbard972e222009-03-20 04:37:21 +0000447 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
448 llvm::outs() << "programs: =";
449 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
450 ie = TC.getProgramPaths().end(); it != ie; ++it) {
451 if (it != TC.getProgramPaths().begin())
452 llvm::outs() << ':';
453 llvm::outs() << *it;
454 }
455 llvm::outs() << "\n";
456 llvm::outs() << "libraries: =";
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000457 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
Daniel Dunbard972e222009-03-20 04:37:21 +0000458 ie = TC.getFilePaths().end(); it != ie; ++it) {
459 if (it != TC.getFilePaths().begin())
460 llvm::outs() << ':';
461 llvm::outs() << *it;
462 }
463 llvm::outs() << "\n";
Daniel Dunbar7c925282009-03-31 21:38:17 +0000464 return false;
Daniel Dunbard972e222009-03-20 04:37:21 +0000465 }
466
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000467 // FIXME: The following handlers should use a callback mechanism, we don't
468 // know what the client would like to do.
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000469 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
Daniel Dunbar1ce81532009-09-09 22:33:00 +0000470 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC) << "\n";
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000471 return false;
472 }
473
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000474 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
Daniel Dunbar1ce81532009-09-09 22:33:00 +0000475 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC) << "\n";
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000476 return false;
477 }
478
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000479 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbar1ce81532009-09-09 22:33:00 +0000480 llvm::outs() << GetFilePath("libgcc.a", TC) << "\n";
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000481 return false;
482 }
483
Daniel Dunbar1b3ec3a2009-06-16 23:25:22 +0000484 if (C.getArgs().hasArg(options::OPT_print_multi_lib)) {
485 // FIXME: We need tool chain support for this.
486 llvm::outs() << ".;\n";
487
488 switch (C.getDefaultToolChain().getTriple().getArch()) {
489 default:
490 break;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000491
Daniel Dunbar1b3ec3a2009-06-16 23:25:22 +0000492 case llvm::Triple::x86_64:
493 llvm::outs() << "x86_64;@m64" << "\n";
494 break;
495
496 case llvm::Triple::ppc64:
497 llvm::outs() << "ppc64;@m64" << "\n";
498 break;
499 }
500 return false;
501 }
502
503 // FIXME: What is the difference between print-multi-directory and
504 // print-multi-os-directory?
505 if (C.getArgs().hasArg(options::OPT_print_multi_directory) ||
506 C.getArgs().hasArg(options::OPT_print_multi_os_directory)) {
507 switch (C.getDefaultToolChain().getTriple().getArch()) {
508 default:
509 case llvm::Triple::x86:
510 case llvm::Triple::ppc:
511 llvm::outs() << "." << "\n";
512 break;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000513
Daniel Dunbar1b3ec3a2009-06-16 23:25:22 +0000514 case llvm::Triple::x86_64:
515 llvm::outs() << "x86_64" << "\n";
516 break;
517
518 case llvm::Triple::ppc64:
519 llvm::outs() << "ppc64" << "\n";
520 break;
521 }
522 return false;
523 }
524
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +0000525 return true;
526}
527
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000528static unsigned PrintActions1(const Compilation &C, Action *A,
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000529 std::map<Action*, unsigned> &Ids) {
530 if (Ids.count(A))
531 return Ids[A];
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000532
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000533 std::string str;
534 llvm::raw_string_ostream os(str);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000535
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000536 os << Action::getClassName(A->getKind()) << ", ";
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000537 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbareb843be2009-03-18 03:13:20 +0000538 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000539 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000540 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
Daniel Dunbareb843be2009-03-18 03:13:20 +0000541 C.getDefaultToolChain().getArchName()) << '"'
542 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000543 } else {
544 os << "{";
545 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbareb843be2009-03-18 03:13:20 +0000546 os << PrintActions1(C, *it, Ids);
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000547 ++it;
548 if (it != ie)
549 os << ", ";
550 }
551 os << "}";
552 }
553
554 unsigned Id = Ids.size();
555 Ids[A] = Id;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000556 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000557 << types::getTypeName(A->getType()) << "\n";
558
559 return Id;
560}
561
Daniel Dunbareb843be2009-03-18 03:13:20 +0000562void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbaraaf1ea62009-03-13 12:19:02 +0000563 std::map<Action*, unsigned> Ids;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000564 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbareb843be2009-03-18 03:13:20 +0000565 ie = C.getActions().end(); it != ie; ++it)
566 PrintActions1(C, *it, Ids);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000567}
568
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000569void Driver::BuildUniversalActions(const ArgList &Args,
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000570 ActionList &Actions) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000571 llvm::PrettyStackTraceString CrashInfo("Building universal build actions");
572 // Collect the list of architectures. Duplicates are allowed, but should only
573 // be handled once (in the order seen).
Daniel Dunbare5dc4822009-03-13 20:33:35 +0000574 llvm::StringSet<> ArchNames;
575 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000576 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbarf479c122009-03-12 18:40:18 +0000577 it != ie; ++it) {
578 Arg *A = *it;
579
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000580 if (A->getOption().matches(options::OPT_arch)) {
Daniel Dunbar9c3f7c42009-09-08 23:37:30 +0000581 // Validate the option here; we don't save the type here because its
582 // particular spelling may participate in other driver choices.
583 llvm::Triple::ArchType Arch =
584 llvm::Triple::getArchTypeForDarwinArchName(A->getValue(Args));
585 if (Arch == llvm::Triple::UnknownArch) {
586 Diag(clang::diag::err_drv_invalid_arch_name)
587 << A->getAsString(Args);
588 continue;
589 }
590
Daniel Dunbar2da02722009-03-19 07:55:12 +0000591 A->claim();
Daniel Dunbar7b574042009-09-08 23:37:02 +0000592 if (ArchNames.insert(A->getValue(Args)))
593 Archs.push_back(A->getValue(Args));
Daniel Dunbarf479c122009-03-12 18:40:18 +0000594 }
595 }
596
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000597 // When there is no explicit arch for this platform, make sure we still bind
598 // the architecture (to the default) so that -Xarch_ is handled correctly.
Daniel Dunbareb843be2009-03-18 03:13:20 +0000599 if (!Archs.size())
600 Archs.push_back(0);
Daniel Dunbarf479c122009-03-12 18:40:18 +0000601
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000602 // FIXME: We killed off some others but these aren't yet detected in a
603 // functional manner. If we added information to jobs about which "auxiliary"
604 // files they wrote then we could detect the conflict these cause downstream.
Daniel Dunbarf479c122009-03-12 18:40:18 +0000605 if (Archs.size() > 1) {
606 // No recovery needed, the point of this is just to prevent
607 // overwriting the same files.
Daniel Dunbarf479c122009-03-12 18:40:18 +0000608 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000609 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar0e759942009-03-20 06:14:23 +0000610 << A->getAsString(Args);
Daniel Dunbarf479c122009-03-12 18:40:18 +0000611 }
612
613 ActionList SingleActions;
614 BuildActions(Args, SingleActions);
615
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000616 // Add in arch binding and lipo (if necessary) for every top level action.
Daniel Dunbarf479c122009-03-12 18:40:18 +0000617 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
618 Action *Act = SingleActions[i];
619
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000620 // Make sure we can lipo this kind of output. If not (and it is an actual
621 // output) then we disallow, since we can't create an output file with the
622 // right name without overwriting it. We could remove this oddity by just
623 // changing the output names to include the arch, which would also fix
Daniel Dunbarf479c122009-03-12 18:40:18 +0000624 // -save-temps. Compatibility wins for now.
625
Daniel Dunbare2ca3bd2009-03-13 17:46:02 +0000626 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbarf479c122009-03-12 18:40:18 +0000627 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
628 << types::getTypeName(Act->getType());
629
630 ActionList Inputs;
Daniel Dunbar2da02722009-03-19 07:55:12 +0000631 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbare5dc4822009-03-13 20:33:35 +0000632 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbarf479c122009-03-12 18:40:18 +0000633
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000634 // Lipo if necessary, we do it this way because we need to set the arch flag
635 // so that -Xarch_ gets overwritten.
Daniel Dunbarf479c122009-03-12 18:40:18 +0000636 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
637 Actions.append(Inputs.begin(), Inputs.end());
638 else
639 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
640 }
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000641}
642
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000643void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +0000644 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000645 // Start by constructing the list of inputs and their types.
646
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000647 // Track the current user specified (-x) input. We also explicitly track the
648 // argument used to set the type; we only want to claim the type when we
649 // actually use it, so we warn about unused -x arguments.
Daniel Dunbarc5a5ac52009-03-13 17:57:10 +0000650 types::ID InputType = types::TY_Nothing;
651 Arg *InputTypeArg = 0;
652
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000653 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000654 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000655 it != ie; ++it) {
656 Arg *A = *it;
657
658 if (isa<InputOption>(A->getOption())) {
659 const char *Value = A->getValue(Args);
660 types::ID Ty = types::TY_INVALID;
661
662 // Infer the input type if necessary.
Daniel Dunbarc5a5ac52009-03-13 17:57:10 +0000663 if (InputType == types::TY_Nothing) {
664 // If there was an explicit arg for this, claim it.
665 if (InputTypeArg)
666 InputTypeArg->claim();
667
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000668 // stdin must be handled specially.
669 if (memcmp(Value, "-", 2) == 0) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000670 // If running with -E, treat as a C input (this changes the builtin
671 // macros, for example). This may be overridden by -ObjC below.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000672 //
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000673 // Otherwise emit an error but still use a valid type to avoid
674 // spurious errors (e.g., no inputs).
Daniel Dunbarfffd1812009-11-19 04:00:53 +0000675 if (!Args.hasArgNoClaim(options::OPT_E))
Daniel Dunbar5cd1e412009-03-12 09:13:48 +0000676 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000677 Ty = types::TY_C;
678 } else {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000679 // Otherwise lookup by extension, and fallback to ObjectType if not
680 // found. We use a host hook here because Darwin at least has its own
681 // idea of what .s is.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000682 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbarea9f0322009-03-20 23:39:23 +0000683 Ty = Host->lookupTypeForExtension(Ext + 1);
684
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000685 if (Ty == types::TY_INVALID)
686 Ty = types::TY_Object;
687 }
688
Daniel Dunbar82b22102009-05-18 21:47:54 +0000689 // -ObjC and -ObjC++ override the default language, but only for "source
690 // files". We just treat everything that isn't a linker input as a
691 // source file.
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000692 //
Daniel Dunbar82b22102009-05-18 21:47:54 +0000693 // FIXME: Clean this up if we move the phase sequence into the type.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000694 if (Ty != types::TY_Object) {
695 if (Args.hasArg(options::OPT_ObjC))
696 Ty = types::TY_ObjC;
697 else if (Args.hasArg(options::OPT_ObjCXX))
698 Ty = types::TY_ObjCXX;
699 }
700 } else {
701 assert(InputTypeArg && "InputType set w/o InputTypeArg");
702 InputTypeArg->claim();
703 Ty = InputType;
704 }
705
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000706 // Check that the file exists. It isn't clear this is worth doing, since
707 // the tool presumably does this anyway, and this just adds an extra stat
708 // to the equation, but this is gcc compatible.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000709 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbar5cd1e412009-03-12 09:13:48 +0000710 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000711 else
712 Inputs.push_back(std::make_pair(Ty, A));
713
714 } else if (A->getOption().isLinkerInput()) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000715 // Just treat as object type, we could make a special type for this if
716 // necessary.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000717 Inputs.push_back(std::make_pair(types::TY_Object, A));
718
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000719 } else if (A->getOption().matches(options::OPT_x)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000720 InputTypeArg = A;
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000721 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
722
723 // Follow gcc behavior and treat as linker input for invalid -x
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000724 // options. Its not clear why we shouldn't just revert to unknown; but
725 // this isn't very important, we might as well be bug comatible.
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000726 if (!InputType) {
Daniel Dunbar5cd1e412009-03-12 09:13:48 +0000727 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000728 InputType = types::TY_Object;
729 }
730 }
731 }
732
Daniel Dunbar34c41872009-03-13 00:17:48 +0000733 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000734 Diag(clang::diag::err_drv_no_input_files);
735 return;
736 }
737
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000738 // Determine which compilation mode we are in. We look for options which
739 // affect the phase, starting with the earliest phases, and record which
740 // option we used to determine the final phase.
Daniel Dunbar65229332009-03-13 11:38:42 +0000741 Arg *FinalPhaseArg = 0;
742 phases::ID FinalPhase;
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000743
744 // -{E,M,MM} only run the preprocessor.
Daniel Dunbar65229332009-03-13 11:38:42 +0000745 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
746 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
747 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
748 FinalPhase = phases::Preprocess;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000749
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +0000750 // -{fsyntax-only,-analyze,emit-ast,S} only run up to the compiler.
Daniel Dunbarae0e55e2009-03-15 00:48:16 +0000751 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
Daniel Dunbar5e051f92009-05-06 02:12:32 +0000752 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze,
753 options::OPT__analyze_auto)) ||
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +0000754 (FinalPhaseArg = Args.getLastArg(options::OPT_emit_ast)) ||
Daniel Dunbar65229332009-03-13 11:38:42 +0000755 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
756 FinalPhase = phases::Compile;
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000757
758 // -c only runs up to the assembler.
Daniel Dunbar65229332009-03-13 11:38:42 +0000759 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
760 FinalPhase = phases::Assemble;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000761
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000762 // Otherwise do everything.
763 } else
Daniel Dunbar65229332009-03-13 11:38:42 +0000764 FinalPhase = phases::Link;
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000765
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000766 // Reject -Z* at the top level, these options should never have been exposed
767 // by gcc.
Daniel Dunbardd765242009-03-26 16:12:09 +0000768 if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
Daniel Dunbar0e759942009-03-20 06:14:23 +0000769 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbarbfeec742009-03-12 23:55:14 +0000770
Daniel Dunbar65229332009-03-13 11:38:42 +0000771 // Construct the actions to perform.
772 ActionList LinkerInputs;
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000773 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbar65229332009-03-13 11:38:42 +0000774 types::ID InputType = Inputs[i].first;
775 const Arg *InputArg = Inputs[i].second;
776
777 unsigned NumSteps = types::getNumCompilationPhases(InputType);
778 assert(NumSteps && "Invalid number of steps!");
779
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000780 // If the first step comes after the final phase we are doing as part of
781 // this compilation, warn the user about it.
Daniel Dunbar65229332009-03-13 11:38:42 +0000782 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
783 if (InitialPhase > FinalPhase) {
Daniel Dunbaradc5c7c2009-03-19 07:57:08 +0000784 // Claim here to avoid the more general unused warning.
785 InputArg->claim();
Daniel Dunbar07806ca2009-09-17 04:13:26 +0000786
787 // Special case '-E' warning on a previously preprocessed file to make
788 // more sense.
789 if (InitialPhase == phases::Compile && FinalPhase == phases::Preprocess &&
790 getPreprocessedType(InputType) == types::TY_INVALID)
791 Diag(clang::diag::warn_drv_preprocessed_input_file_unused)
792 << InputArg->getAsString(Args)
793 << FinalPhaseArg->getOption().getName();
794 else
795 Diag(clang::diag::warn_drv_input_file_unused)
796 << InputArg->getAsString(Args)
797 << getPhaseName(InitialPhase)
798 << FinalPhaseArg->getOption().getName();
Daniel Dunbar65229332009-03-13 11:38:42 +0000799 continue;
800 }
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000801
Daniel Dunbar65229332009-03-13 11:38:42 +0000802 // Build the pipeline for this file.
803 Action *Current = new InputAction(*InputArg, InputType);
804 for (unsigned i = 0; i != NumSteps; ++i) {
805 phases::ID Phase = types::getCompilationPhase(InputType, i);
806
807 // We are done if this step is past what the user requested.
808 if (Phase > FinalPhase)
809 break;
810
811 // Queue linker inputs.
812 if (Phase == phases::Link) {
813 assert(i + 1 == NumSteps && "linking must be final compilation step.");
814 LinkerInputs.push_back(Current);
815 Current = 0;
816 break;
817 }
818
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000819 // Some types skip the assembler phase (e.g., llvm-bc), but we can't
820 // encode this in the steps because the intermediate type depends on
821 // arguments. Just special case here.
Daniel Dunbar13864952009-03-24 20:17:30 +0000822 if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
823 continue;
824
Daniel Dunbar65229332009-03-13 11:38:42 +0000825 // Otherwise construct the appropriate action.
826 Current = ConstructPhaseAction(Args, Phase, Current);
827 if (Current->getType() == types::TY_Nothing)
828 break;
829 }
830
831 // If we ended with something, add to the output list.
832 if (Current)
833 Actions.push_back(Current);
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000834 }
Daniel Dunbar65229332009-03-13 11:38:42 +0000835
836 // Add a link action if necessary.
837 if (!LinkerInputs.empty())
838 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
839}
840
841Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
842 Action *Input) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +0000843 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbar65229332009-03-13 11:38:42 +0000844 // Build the appropriate action.
845 switch (Phase) {
846 case phases::Link: assert(0 && "link action invalid here.");
847 case phases::Preprocess: {
Daniel Dunbard67a3222009-03-30 06:36:42 +0000848 types::ID OutputTy;
849 // -{M, MM} alter the output type.
850 if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) {
851 OutputTy = types::TY_Dependencies;
852 } else {
853 OutputTy = types::getPreprocessedType(Input->getType());
854 assert(OutputTy != types::TY_INVALID &&
855 "Cannot preprocess this input type!");
856 }
Daniel Dunbar65229332009-03-13 11:38:42 +0000857 return new PreprocessJobAction(Input, OutputTy);
858 }
859 case phases::Precompile:
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000860 return new PrecompileJobAction(Input, types::TY_PCH);
Daniel Dunbar65229332009-03-13 11:38:42 +0000861 case phases::Compile: {
862 if (Args.hasArg(options::OPT_fsyntax_only)) {
863 return new CompileJobAction(Input, types::TY_Nothing);
Daniel Dunbar5e051f92009-05-06 02:12:32 +0000864 } else if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) {
Daniel Dunbar65229332009-03-13 11:38:42 +0000865 return new AnalyzeJobAction(Input, types::TY_Plist);
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +0000866 } else if (Args.hasArg(options::OPT_emit_ast)) {
867 return new CompileJobAction(Input, types::TY_AST);
Daniel Dunbar13864952009-03-24 20:17:30 +0000868 } else if (Args.hasArg(options::OPT_emit_llvm) ||
869 Args.hasArg(options::OPT_flto) ||
870 Args.hasArg(options::OPT_O4)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000871 types::ID Output =
Daniel Dunbar65229332009-03-13 11:38:42 +0000872 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
873 return new CompileJobAction(Input, Output);
874 } else {
875 return new CompileJobAction(Input, types::TY_PP_Asm);
876 }
877 }
878 case phases::Assemble:
879 return new AssembleJobAction(Input, types::TY_Object);
880 }
881
882 assert(0 && "invalid phase in ConstructPhaseAction");
883 return 0;
Daniel Dunbar1688f1a2009-03-12 07:58:46 +0000884}
885
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000886void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +0000887 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbare75d8342009-03-16 06:56:51 +0000888 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
889 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +0000890
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000891 // FIXME: Pipes are forcibly disabled until we support executing them.
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +0000892 if (!CCCPrintBindings)
893 UsePipes = false;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000894
Daniel Dunbare75d8342009-03-16 06:56:51 +0000895 // -save-temps inhibits pipes.
896 if (SaveTemps && UsePipes) {
897 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
898 UsePipes = true;
899 }
900
901 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
902
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000903 // It is an error to provide a -o option if we are making multiple output
904 // files.
Daniel Dunbare75d8342009-03-16 06:56:51 +0000905 if (FinalOutput) {
906 unsigned NumOutputs = 0;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000907 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000908 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbare75d8342009-03-16 06:56:51 +0000909 if ((*it)->getType() != types::TY_Nothing)
910 ++NumOutputs;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000911
Daniel Dunbare75d8342009-03-16 06:56:51 +0000912 if (NumOutputs > 1) {
913 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
914 FinalOutput = 0;
915 }
916 }
917
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000918 for (ActionList::const_iterator it = C.getActions().begin(),
Daniel Dunbarf0eddb82009-03-18 02:55:38 +0000919 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbare75d8342009-03-16 06:56:51 +0000920 Action *A = *it;
921
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000922 // If we are linking an image for multiple archs then the linker wants
923 // -arch_multiple and -final_output <final image name>. Unfortunately, this
924 // doesn't fit in cleanly because we have to pass this information down.
Daniel Dunbare75d8342009-03-16 06:56:51 +0000925 //
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000926 // FIXME: This is a hack; find a cleaner way to integrate this into the
927 // process.
Daniel Dunbare75d8342009-03-16 06:56:51 +0000928 const char *LinkingOutput = 0;
Daniel Dunbardd765242009-03-26 16:12:09 +0000929 if (isa<LipoJobAction>(A)) {
Daniel Dunbare75d8342009-03-16 06:56:51 +0000930 if (FinalOutput)
931 LinkingOutput = FinalOutput->getValue(C.getArgs());
932 else
933 LinkingOutput = DefaultImageName.c_str();
934 }
935
936 InputInfo II;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000937 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +0000938 /*BoundArch*/0,
Daniel Dunbare75d8342009-03-16 06:56:51 +0000939 /*CanAcceptPipe*/ true,
940 /*AtTopLevel*/ true,
941 /*LinkingOutput*/ LinkingOutput,
942 II);
943 }
Daniel Dunbar3ce436d2009-03-16 06:42:30 +0000944
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000945 // If the user passed -Qunused-arguments or there were errors, don't warn
946 // about any unused arguments.
947 if (Diags.getNumErrors() ||
Daniel Dunbara3cfbe32009-04-07 19:04:18 +0000948 C.getArgs().hasArg(options::OPT_Qunused_arguments))
Daniel Dunbard175d972009-03-18 18:03:46 +0000949 return;
950
Daniel Dunbar58399ae2009-03-29 22:24:54 +0000951 // Claim -### here.
952 (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000953
Daniel Dunbar3ce436d2009-03-16 06:42:30 +0000954 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
955 it != ie; ++it) {
956 Arg *A = *it;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000957
Daniel Dunbar3ce436d2009-03-16 06:42:30 +0000958 // FIXME: It would be nice to be able to send the argument to the
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000959 // Diagnostic, so that extra values, position, and so on could be printed.
Daniel Dunbar90dd6f42009-04-04 00:52:26 +0000960 if (!A->isClaimed()) {
Daniel Dunbara3cfbe32009-04-07 19:04:18 +0000961 if (A->getOption().hasNoArgumentUnused())
962 continue;
963
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000964 // Suppress the warning automatically if this is just a flag, and it is an
965 // instance of an argument we already claimed.
Daniel Dunbar90dd6f42009-04-04 00:52:26 +0000966 const Option &Opt = A->getOption();
967 if (isa<FlagOption>(Opt)) {
968 bool DuplicateClaimed = false;
969
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000970 for (arg_iterator it = C.getArgs().filtered_begin(&Opt),
971 ie = C.getArgs().filtered_end(); it != ie; ++it) {
972 if ((*it)->isClaimed()) {
Daniel Dunbar90dd6f42009-04-04 00:52:26 +0000973 DuplicateClaimed = true;
974 break;
975 }
976 }
977
978 if (DuplicateClaimed)
979 continue;
980 }
981
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000982 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar0e759942009-03-20 06:14:23 +0000983 << A->getAsString(C.getArgs());
Daniel Dunbar90dd6f42009-04-04 00:52:26 +0000984 }
Daniel Dunbar3ce436d2009-03-16 06:42:30 +0000985 }
Daniel Dunbar95e6b192009-03-13 22:12:33 +0000986}
987
Daniel Dunbare75d8342009-03-16 06:56:51 +0000988void Driver::BuildJobsForAction(Compilation &C,
989 const Action *A,
990 const ToolChain *TC,
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +0000991 const char *BoundArch,
Daniel Dunbare75d8342009-03-16 06:56:51 +0000992 bool CanAcceptPipe,
993 bool AtTopLevel,
994 const char *LinkingOutput,
995 InputInfo &Result) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000996 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +0000997
998 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +0000999 // FIXME: Pipes are forcibly disabled until we support executing them.
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +00001000 if (!CCCPrintBindings)
1001 UsePipes = false;
1002
Daniel Dunbare75d8342009-03-16 06:56:51 +00001003 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001004 // FIXME: It would be nice to not claim this here; maybe the old scheme of
1005 // just using Args was better?
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00001006 const Arg &Input = IA->getInputArg();
1007 Input.claim();
1008 if (isa<PositionalArg>(Input)) {
1009 const char *Name = Input.getValue(C.getArgs());
1010 Result = InputInfo(Name, A->getType(), Name);
1011 } else
1012 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbare75d8342009-03-16 06:56:51 +00001013 return;
1014 }
1015
1016 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar1ef3f2a2009-09-08 23:37:19 +00001017 const ToolChain *TC = &C.getDefaultToolChain();
1018
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001019 std::string Arch;
Daniel Dunbar1ef3f2a2009-09-08 23:37:19 +00001020 if (BAA->getArchName())
1021 TC = Host->CreateToolChain(C.getArgs(), BAA->getArchName());
1022
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +00001023 BuildJobsForAction(C, *BAA->begin(), TC, BAA->getArchName(),
1024 CanAcceptPipe, AtTopLevel, LinkingOutput, Result);
Daniel Dunbare75d8342009-03-16 06:56:51 +00001025 return;
1026 }
1027
1028 const JobAction *JA = cast<JobAction>(A);
1029 const Tool &T = TC->SelectTool(C, *JA);
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001030
1031 // See if we should use an integrated preprocessor. We do so when we have
1032 // exactly one input, since this is the only use case we care about
1033 // (irrelevant since we don't support combine yet).
Daniel Dunbare75d8342009-03-16 06:56:51 +00001034 bool UseIntegratedCPP = false;
1035 const ActionList *Inputs = &A->getInputs();
1036 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
1037 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
1038 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
1039 !C.getArgs().hasArg(options::OPT_save_temps) &&
1040 T.hasIntegratedCPP()) {
1041 UseIntegratedCPP = true;
1042 Inputs = &(*Inputs)[0]->getInputs();
1043 }
1044 }
1045
1046 // Only use pipes when there is exactly one input.
1047 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001048 InputInfoList InputInfos;
Daniel Dunbare75d8342009-03-16 06:56:51 +00001049 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
1050 it != ie; ++it) {
1051 InputInfo II;
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +00001052 BuildJobsForAction(C, *it, TC, BoundArch, TryToUsePipeInput,
1053 /*AtTopLevel*/false, LinkingOutput, II);
Daniel Dunbare75d8342009-03-16 06:56:51 +00001054 InputInfos.push_back(II);
1055 }
1056
1057 // Determine if we should output to a pipe.
1058 bool OutputToPipe = false;
1059 if (CanAcceptPipe && T.canPipeOutput()) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001060 // Some actions default to writing to a pipe if they are the top level phase
1061 // and there was no user override.
Daniel Dunbare75d8342009-03-16 06:56:51 +00001062 //
1063 // FIXME: Is there a better way to handle this?
1064 if (AtTopLevel) {
1065 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
1066 OutputToPipe = true;
Daniel Dunbarc4acf9d2009-03-18 23:18:19 +00001067 } else if (UsePipes)
Daniel Dunbare75d8342009-03-16 06:56:51 +00001068 OutputToPipe = true;
1069 }
1070
1071 // Figure out where to put the job (pipes).
1072 Job *Dest = &C.getJobs();
1073 if (InputInfos[0].isPipe()) {
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001074 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbare75d8342009-03-16 06:56:51 +00001075 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
1076 Dest = &InputInfos[0].getPipe();
1077 }
1078
1079 // Always use the first input as the base input.
1080 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001081
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001082 // Determine the place to write output to (nothing, pipe, or filename) and
1083 // where to put the new job.
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001084 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbarb39cc522009-03-17 22:47:06 +00001085 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001086 } else if (OutputToPipe) {
1087 // Append to current piped job or create a new one as appropriate.
Daniel Dunbarb39cc522009-03-17 22:47:06 +00001088 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
1089 if (!PJ) {
1090 PJ = new PipedJob();
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001091 // FIXME: Temporary hack so that -ccc-print-bindings work until we have
1092 // pipe support. Please remove later.
Daniel Dunbar1fc898c2009-03-20 00:11:04 +00001093 if (!CCCPrintBindings)
1094 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +00001095 Dest = PJ;
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001096 }
Daniel Dunbarb39cc522009-03-17 22:47:06 +00001097 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001098 } else {
Daniel Dunbarb39cc522009-03-17 22:47:06 +00001099 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
1100 A->getType(), BaseInput);
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001101 }
1102
Daniel Dunbarb39cc522009-03-17 22:47:06 +00001103 if (CCCPrintBindings) {
Daniel Dunbard67a3222009-03-30 06:36:42 +00001104 llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"'
1105 << " - \"" << T.getName() << "\", inputs: [";
Daniel Dunbarb39cc522009-03-17 22:47:06 +00001106 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
1107 llvm::errs() << InputInfos[i].getAsString();
1108 if (i + 1 != e)
1109 llvm::errs() << ", ";
1110 }
1111 llvm::errs() << "], output: " << Result.getAsString() << "\n";
1112 } else {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001113 T.ConstructJob(C, *JA, *Dest, Result, InputInfos,
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +00001114 C.getArgsForToolChain(TC, BoundArch), LinkingOutput);
Daniel Dunbarb39cc522009-03-17 22:47:06 +00001115 }
Daniel Dunbare75d8342009-03-16 06:56:51 +00001116}
1117
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001118const char *Driver::GetNamedOutputPath(Compilation &C,
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001119 const JobAction &JA,
1120 const char *BaseInput,
1121 bool AtTopLevel) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +00001122 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001123 // Output to a user requested destination?
1124 if (AtTopLevel) {
1125 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
1126 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
1127 }
1128
1129 // Output to a temporary file?
1130 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001131 std::string TmpName =
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001132 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
1133 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001134 }
1135
1136 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbard00978b2009-03-18 02:00:31 +00001137 std::string BaseName(BasePath.getLast());
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001138
1139 // Determine what the derived output name should be.
1140 const char *NamedOutput;
1141 if (JA.getType() == types::TY_Image) {
1142 NamedOutput = DefaultImageName.c_str();
1143 } else {
1144 const char *Suffix = types::getTypeTempSuffix(JA.getType());
1145 assert(Suffix && "All types used for output should have a suffix.");
1146
1147 std::string::size_type End = std::string::npos;
1148 if (!types::appendSuffixForType(JA.getType()))
1149 End = BaseName.rfind('.');
1150 std::string Suffixed(BaseName.substr(0, End));
1151 Suffixed += '.';
1152 Suffixed += Suffix;
1153 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
1154 }
1155
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001156 // As an annoying special case, PCH generation doesn't strip the pathname.
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001157 if (JA.getType() == types::TY_PCH) {
1158 BasePath.eraseComponent();
Daniel Dunbare6c83192009-03-18 09:58:30 +00001159 if (BasePath.isEmpty())
1160 BasePath = NamedOutput;
1161 else
1162 BasePath.appendComponent(NamedOutput);
Daniel Dunbar7a178a42009-03-17 17:53:55 +00001163 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
1164 } else {
1165 return C.addResultFile(NamedOutput);
1166 }
1167}
1168
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001169std::string Driver::GetFilePath(const char *Name, const ToolChain &TC) const {
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001170 const ToolChain::path_list &List = TC.getFilePaths();
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001171 for (ToolChain::path_list::const_iterator
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001172 it = List.begin(), ie = List.end(); it != ie; ++it) {
1173 llvm::sys::Path P(*it);
1174 P.appendComponent(Name);
1175 if (P.exists())
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001176 return P.str();
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001177 }
1178
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001179 return Name;
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +00001180}
1181
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001182std::string Driver::GetProgramPath(const char *Name, const ToolChain &TC,
1183 bool WantFile) const {
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001184 const ToolChain::path_list &List = TC.getProgramPaths();
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001185 for (ToolChain::path_list::const_iterator
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001186 it = List.begin(), ie = List.end(); it != ie; ++it) {
1187 llvm::sys::Path P(*it);
1188 P.appendComponent(Name);
Mike Stumpdb657372009-03-27 00:40:20 +00001189 if (WantFile ? P.exists() : P.canExecute())
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001190 return P.str();
Daniel Dunbar68b01a02009-03-18 20:26:19 +00001191 }
1192
Daniel Dunbar76ce7412009-03-23 16:15:50 +00001193 // If all else failed, search the path.
1194 llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
Daniel Dunbar6f668772009-03-18 21:34:08 +00001195 if (!P.empty())
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001196 return P.str();
Daniel Dunbar6f668772009-03-18 21:34:08 +00001197
Daniel Dunbar1ce81532009-09-09 22:33:00 +00001198 return Name;
Daniel Dunbar5e0f6af2009-03-13 00:51:18 +00001199}
1200
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001201std::string Driver::GetTemporaryPath(const char *Suffix) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001202 // FIXME: This is lame; sys::Path should provide this function (in particular,
1203 // it should know how to find the temporary files dir).
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001204 std::string Error;
Daniel Dunbarc35694d2009-04-20 20:28:21 +00001205 const char *TmpDir = ::getenv("TMPDIR");
1206 if (!TmpDir)
1207 TmpDir = ::getenv("TEMP");
1208 if (!TmpDir)
Daniel Dunbar712e4de2009-04-21 00:25:10 +00001209 TmpDir = ::getenv("TMP");
1210 if (!TmpDir)
Daniel Dunbarc35694d2009-04-20 20:28:21 +00001211 TmpDir = "/tmp";
1212 llvm::sys::Path P(TmpDir);
Daniel Dunbarab8ce7c2009-04-20 17:32:49 +00001213 P.appendComponent("cc");
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001214 if (P.makeUnique(false, &Error)) {
1215 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
1216 return "";
1217 }
1218
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001219 // FIXME: Grumble, makeUnique sometimes leaves the file around!? PR3837.
Daniel Dunbar18d69de2009-03-18 23:08:52 +00001220 P.eraseFromDisk(false, 0);
1221
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001222 P.appendSuffix(Suffix);
Chris Lattner3441b4f2009-08-23 22:45:33 +00001223 return P.str();
Daniel Dunbare627c1c2009-03-18 19:34:39 +00001224}
1225
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001226const HostInfo *Driver::GetHostInfo(const char *TripleStr) const {
Daniel Dunbar2608c542009-03-18 01:38:48 +00001227 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001228 llvm::Triple Triple(TripleStr);
Daniel Dunbar4dff6a42009-03-10 23:41:59 +00001229
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001230 switch (Triple.getOS()) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001231 case llvm::Triple::AuroraUX:
1232 return createAuroraUXHostInfo(*this, Triple);
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001233 case llvm::Triple::Darwin:
1234 return createDarwinHostInfo(*this, Triple);
1235 case llvm::Triple::DragonFly:
1236 return createDragonFlyHostInfo(*this, Triple);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001237 case llvm::Triple::OpenBSD:
1238 return createOpenBSDHostInfo(*this, Triple);
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001239 case llvm::Triple::FreeBSD:
1240 return createFreeBSDHostInfo(*this, Triple);
Eli Friedman5cd659f2009-05-26 07:52:18 +00001241 case llvm::Triple::Linux:
1242 return createLinuxHostInfo(*this, Triple);
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001243 default:
1244 return createUnknownHostInfo(*this, Triple);
1245 }
Daniel Dunbar4dff6a42009-03-10 23:41:59 +00001246}
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001247
1248bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
Daniel Dunbar953b8d12009-09-08 23:36:55 +00001249 const llvm::Triple &Triple) const {
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001250 // Check if user requested no clang, or clang doesn't understand this type (we
1251 // only handle single inputs for now).
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00001252 if (!CCCUseClang || JA.size() != 1 ||
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001253 !types::isAcceptedByClang((*JA.begin())->getType()))
1254 return false;
1255
Daniel Dunbar88f356e2009-03-24 19:02:31 +00001256 // Otherwise make sure this is an action clang understands.
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001257 if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001258 if (!CCCUseClangCPP) {
1259 Diag(clang::diag::warn_drv_not_using_clang_cpp);
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001260 return false;
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001261 }
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001262 } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
1263 return false;
1264
Daniel Dunbar88f356e2009-03-24 19:02:31 +00001265 // Use clang for C++?
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001266 if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
1267 Diag(clang::diag::warn_drv_not_using_clang_cxx);
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001268 return false;
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001269 }
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001270
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001271 // Always use clang for precompiling and AST generation, regardless of archs.
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00001272 if (isa<PrecompileJobAction>(JA) || JA.getType() == types::TY_AST)
Daniel Dunbarf2df7c22009-04-16 23:10:13 +00001273 return true;
1274
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001275 // Finally, don't use clang if this isn't one of the user specified archs to
1276 // build.
Daniel Dunbar953b8d12009-09-08 23:36:55 +00001277 if (!CCCClangArchs.empty() && !CCCClangArchs.count(Triple.getArch())) {
1278 Diag(clang::diag::warn_drv_not_using_clang_arch) << Triple.getArchName();
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001279 return false;
Daniel Dunbar6b5244d2009-03-24 19:14:56 +00001280 }
Daniel Dunbar8fa879d2009-03-24 18:57:02 +00001281
1282 return true;
1283}
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001284
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001285/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the
1286/// grouped values as integers. Numbers which are not provided are set to 0.
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001287///
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001288/// \return True if the entire string was parsed (9.2), or all groups were
1289/// parsed (10.3.5extrastuff).
1290bool Driver::GetReleaseVersion(const char *Str, unsigned &Major,
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001291 unsigned &Minor, unsigned &Micro,
1292 bool &HadExtra) {
1293 HadExtra = false;
1294
1295 Major = Minor = Micro = 0;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001296 if (*Str == '\0')
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001297 return true;
1298
1299 char *End;
1300 Major = (unsigned) strtol(Str, &End, 10);
1301 if (*Str != '\0' && *End == '\0')
1302 return true;
1303 if (*End != '.')
1304 return false;
Daniel Dunbarf26a7ab2009-09-08 23:36:43 +00001305
Daniel Dunbarc7fd57a2009-03-26 15:58:36 +00001306 Str = End+1;
1307 Minor = (unsigned) strtol(Str, &End, 10);
1308 if (*Str != '\0' && *End == '\0')
1309 return true;
1310 if (*End != '.')
1311 return false;
1312
1313 Str = End+1;
1314 Micro = (unsigned) strtol(Str, &End, 10);
1315 if (*Str != '\0' && *End == '\0')
1316 return true;
1317 if (Str == End)
1318 return false;
1319 HadExtra = true;
1320 return true;
1321}