blob: db084262e9c2c11cb315c3df985be75cbcbfb9ae [file] [log] [blame]
Daniel Dunbar3ede8d02009-03-02 19:59:07 +00001//===--- Driver.cpp - Clang GCC Compatible Driver -----------------------*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000010#include "clang/Driver/Driver.h"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000011
Daniel Dunbar53ec5522009-03-12 07:58:46 +000012#include "clang/Driver/Action.h"
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
15#include "clang/Driver/Compilation.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000017#include "clang/Driver/HostInfo.h"
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000018#include "clang/Driver/Job.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000019#include "clang/Driver/Option.h"
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000020#include "clang/Driver/Options.h"
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000021#include "clang/Driver/Tool.h"
22#include "clang/Driver/ToolChain.h"
Daniel Dunbar53ec5522009-03-12 07:58:46 +000023#include "clang/Driver/Types.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000024
Daniel Dunbar13689542009-03-13 20:33:35 +000025#include "llvm/ADT/StringSet.h"
Daniel Dunbar8f25c792009-03-18 01:38:48 +000026#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbar53ec5522009-03-12 07:58:46 +000028#include "llvm/System/Path.h"
Daniel Dunbar632f50e2009-03-18 21:34:08 +000029#include "llvm/System/Program.h"
Daniel Dunbarba102132009-03-13 12:19:02 +000030
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000031#include "InputInfo.h"
32
Daniel Dunbarba102132009-03-13 12:19:02 +000033#include <map>
34
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000035using namespace clang::driver;
36
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000037Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000038 const char *_DefaultHostTriple,
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000039 const char *_DefaultImageName,
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000040 Diagnostic &_Diags)
41 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000042 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000043 DefaultImageName(_DefaultImageName),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000044 Host(0),
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +000045 CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false),
Daniel Dunbar8b1604e2009-03-13 00:17:48 +000046 CCCNoClang(false), CCCNoClangCXX(false), CCCNoClangCPP(false),
47 SuppressMissingInputWarning(false)
Daniel Dunbar365c02f2009-03-10 20:52:46 +000048{
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000049}
50
51Driver::~Driver() {
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000052 delete Opts;
Daniel Dunbar7e4534d2009-03-18 01:09:40 +000053 delete Host;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000054}
55
Daniel Dunbar06482622009-03-05 06:38:47 +000056ArgList *Driver::ParseArgStrings(const char **ArgBegin, const char **ArgEnd) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +000057 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbar06482622009-03-05 06:38:47 +000058 ArgList *Args = new ArgList(ArgBegin, ArgEnd);
59
Daniel Dunbarad2a9af2009-03-13 11:38:42 +000060 // FIXME: Handle '@' args (or at least error on them).
61
Daniel Dunbar06482622009-03-05 06:38:47 +000062 unsigned Index = 0, End = ArgEnd - ArgBegin;
63 while (Index < End) {
Daniel Dunbar41393402009-03-13 01:01:44 +000064 // gcc's handling of empty arguments doesn't make
65 // sense, but this is not a common use case. :)
66 //
67 // We just ignore them here (note that other things may
68 // still take them as arguments).
69 if (Args->getArgString(Index)[0] == '\0') {
70 ++Index;
71 continue;
72 }
73
Daniel Dunbar06482622009-03-05 06:38:47 +000074 unsigned Prev = Index;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000075 Arg *A = getOpts().ParseOneArg(*Args, Index);
76 assert(Index > Prev && "Parser failed to consume argument.");
Daniel Dunbar53ec5522009-03-12 07:58:46 +000077
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000078 // Check for missing argument error.
79 if (!A) {
80 assert(Index >= End && "Unexpected parser error.");
81 Diag(clang::diag::err_drv_missing_argument)
82 << Args->getArgString(Prev)
83 << (Index - Prev - 1);
84 break;
Daniel Dunbar53ec5522009-03-12 07:58:46 +000085 }
Daniel Dunbar06482622009-03-05 06:38:47 +000086
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000087 if (A->getOption().isUnsupported()) {
88 Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
89 continue;
90 }
91 Args->append(A);
Daniel Dunbar06482622009-03-05 06:38:47 +000092 }
93
94 return Args;
95}
96
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000097Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +000098 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
99
Daniel Dunbarcb881672009-03-13 00:51:18 +0000100 // FIXME: Handle environment options which effect driver behavior,
101 // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH,
102 // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS.
103
104 // FIXME: What are we going to do with -V and -b?
105
106 // FIXME: Handle CCC_ADD_ARGS.
107
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000108 // FIXME: This stuff needs to go into the Compilation, not the
109 // driver.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000110 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar06482622009-03-05 06:38:47 +0000111
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000112 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000113 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000114
115 // Read -ccc args.
116 //
117 // FIXME: We need to figure out where this behavior should
118 // live. Most of it should be outside in the client; the parts that
119 // aren't should have proper options, either by introducing new ones
120 // or by overloading gcc ones like -V or -b.
121 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
122 const char *Opt = *Start + 5;
123
124 if (!strcmp(Opt, "print-options")) {
125 CCCPrintOptions = true;
126 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000127 CCCPrintActions = true;
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000128 } else if (!strcmp(Opt, "print-bindings")) {
129 CCCPrintBindings = true;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000130 } else if (!strcmp(Opt, "cxx")) {
131 CCCIsCXX = true;
132 } else if (!strcmp(Opt, "echo")) {
133 CCCEcho = true;
134
135 } else if (!strcmp(Opt, "no-clang")) {
136 CCCNoClang = true;
137 } else if (!strcmp(Opt, "no-clang-cxx")) {
138 CCCNoClangCXX = true;
139 } else if (!strcmp(Opt, "no-clang-cpp")) {
140 CCCNoClangCPP = true;
141 } else if (!strcmp(Opt, "clang-archs")) {
142 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
143 const char *Cur = *++Start;
144
145 for (;;) {
146 const char *Next = strchr(Cur, ',');
147
148 if (Next) {
149 CCCClangArchs.insert(std::string(Cur, Next));
150 Cur = Next + 1;
151 } else {
152 CCCClangArchs.insert(std::string(Cur));
153 break;
154 }
155 }
156
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000157 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000158 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000159 HostTriple = *++Start;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000160
161 } else {
162 // FIXME: Error handling.
163 llvm::errs() << "invalid option: " << *Start << "\n";
164 exit(1);
165 }
166 }
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000167
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000168 ArgList *Args = ParseArgStrings(Start, End);
169
Daniel Dunbare5049522009-03-17 20:45:45 +0000170 Host = GetHostInfo(HostTriple);
Daniel Dunbarcb881672009-03-13 00:51:18 +0000171
Daniel Dunbar586dc232009-03-16 06:42:30 +0000172 // The compilation takes ownership of Args.
Daniel Dunbare530ad42009-03-18 22:16:03 +0000173 Compilation *C = new Compilation(*this, *Host->getToolChain(*Args), Args);
Daniel Dunbar21549232009-03-18 02:55:38 +0000174
175 // FIXME: This behavior shouldn't be here.
176 if (CCCPrintOptions) {
177 PrintOptions(C->getArgs());
178 return C;
179 }
180
181 if (!HandleImmediateArgs(*C))
182 return C;
183
184 // Construct the list of abstract actions to perform for this
185 // compilation. We avoid passing a Compilation here simply to
186 // enforce the abstraction that pipelining is not host or toolchain
187 // dependent (other than the driver driver test).
188 if (Host->useDriverDriver())
189 BuildUniversalActions(C->getArgs(), C->getActions());
190 else
191 BuildActions(C->getArgs(), C->getActions());
192
193 if (CCCPrintActions) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000194 PrintActions(*C);
Daniel Dunbar21549232009-03-18 02:55:38 +0000195 return C;
196 }
197
198 BuildJobs(*C);
Daniel Dunbar8d2554a2009-03-15 01:38:15 +0000199
200 return C;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000201}
202
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000203void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar06482622009-03-05 06:38:47 +0000204 unsigned i = 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000205 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar06482622009-03-05 06:38:47 +0000206 it != ie; ++it, ++i) {
207 Arg *A = *it;
208 llvm::errs() << "Option " << i << " - "
209 << "Name: \"" << A->getOption().getName() << "\", "
210 << "Values: {";
211 for (unsigned j = 0; j < A->getNumValues(); ++j) {
212 if (j)
213 llvm::errs() << ", ";
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000214 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar06482622009-03-05 06:38:47 +0000215 }
216 llvm::errs() << "}\n";
Daniel Dunbar06482622009-03-05 06:38:47 +0000217 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000218}
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000219
Daniel Dunbarcb881672009-03-13 00:51:18 +0000220void Driver::PrintVersion() const {
Mike Stump5d023c32009-03-18 14:00:02 +0000221 static char buf[] = "$URL$";
222 char *zap = strstr(buf, "/lib/Driver");
223 if (zap)
224 *zap = 0;
225 zap = strstr(buf, "/clang/tools/clang");
226 if (zap)
227 *zap = 0;
Mike Stumpe70295b2009-03-18 15:19:35 +0000228 const char *vers = buf+6;
Mike Stump8944c382009-03-18 18:45:55 +0000229 // FIXME: Add cmake support and remove #ifdef
230#ifdef SVN_REVISION
231 const char *revision = SVN_REVISION;
232#else
233 const char *revision = "";
234#endif
Daniel Dunbarcb881672009-03-13 00:51:18 +0000235 // FIXME: The following handlers should use a callback mechanism, we
236 // don't know what the client would like to do.
Mike Stumpd227fe72009-03-18 21:19:11 +0000237 llvm::errs() << "clang version 1.0 (" << vers << " " << revision << ")" << "\n";
Mike Stump5d023c32009-03-18 14:00:02 +0000238 // FIXME: Add cmake support and remove #ifdef
239#ifdef TARGET_TRIPLE
240 llvm::errs() << "Target: " << TARGET_TRIPLE << "\n";
241#endif
Daniel Dunbarcb881672009-03-13 00:51:18 +0000242}
243
Daniel Dunbar21549232009-03-18 02:55:38 +0000244bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000245 // The order these options are handled in in gcc is all over the
246 // place, but we don't expect inconsistencies w.r.t. that to matter
247 // in practice.
Daniel Dunbar21549232009-03-18 02:55:38 +0000248 if (C.getArgs().hasArg(options::OPT_v) ||
249 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000250 PrintVersion();
251 SuppressMissingInputWarning = true;
252 }
253
Daniel Dunbar21549232009-03-18 02:55:38 +0000254 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbarca3459e2009-03-20 04:37:21 +0000255 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
256 llvm::outs() << "programs: =";
257 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
258 ie = TC.getProgramPaths().end(); it != ie; ++it) {
259 if (it != TC.getProgramPaths().begin())
260 llvm::outs() << ':';
261 llvm::outs() << *it;
262 }
263 llvm::outs() << "\n";
264 llvm::outs() << "libraries: =";
265 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
266 ie = TC.getFilePaths().end(); it != ie; ++it) {
267 if (it != TC.getFilePaths().begin())
268 llvm::outs() << ':';
269 llvm::outs() << *it;
270 }
271 llvm::outs() << "\n";
272 }
273
Daniel Dunbarcb881672009-03-13 00:51:18 +0000274 // FIXME: The following handlers should use a callback mechanism, we
275 // don't know what the client would like to do.
Daniel Dunbar21549232009-03-18 02:55:38 +0000276 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
277 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).toString()
278 << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000279 return false;
280 }
281
Daniel Dunbar21549232009-03-18 02:55:38 +0000282 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
283 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).toString()
284 << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000285 return false;
286 }
287
Daniel Dunbar21549232009-03-18 02:55:38 +0000288 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
289 llvm::outs() << GetProgramPath("libgcc.a", TC).toString() << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000290 return false;
291 }
292
293 return true;
294}
295
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000296static unsigned PrintActions1(const Compilation &C,
Daniel Dunbarba102132009-03-13 12:19:02 +0000297 Action *A,
298 std::map<Action*, unsigned> &Ids) {
299 if (Ids.count(A))
300 return Ids[A];
301
302 std::string str;
303 llvm::raw_string_ostream os(str);
304
305 os << Action::getClassName(A->getKind()) << ", ";
306 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000307 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbarba102132009-03-13 12:19:02 +0000308 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000309 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
310 C.getDefaultToolChain().getArchName()) << '"'
311 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbarba102132009-03-13 12:19:02 +0000312 } else {
313 os << "{";
314 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000315 os << PrintActions1(C, *it, Ids);
Daniel Dunbarba102132009-03-13 12:19:02 +0000316 ++it;
317 if (it != ie)
318 os << ", ";
319 }
320 os << "}";
321 }
322
323 unsigned Id = Ids.size();
324 Ids[A] = Id;
Daniel Dunbarb269c322009-03-13 17:20:20 +0000325 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbarba102132009-03-13 12:19:02 +0000326 << types::getTypeName(A->getType()) << "\n";
327
328 return Id;
329}
330
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000331void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbarba102132009-03-13 12:19:02 +0000332 std::map<Action*, unsigned> Ids;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000333 for (ActionList::const_iterator it = C.getActions().begin(),
334 ie = C.getActions().end(); it != ie; ++it)
335 PrintActions1(C, *it, Ids);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000336}
337
Daniel Dunbar21549232009-03-18 02:55:38 +0000338void Driver::BuildUniversalActions(const ArgList &Args,
339 ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000340 llvm::PrettyStackTraceString CrashInfo("Building actions for universal build");
Daniel Dunbar13689542009-03-13 20:33:35 +0000341 // Collect the list of architectures. Duplicates are allowed, but
342 // should only be handled once (in the order seen).
343 llvm::StringSet<> ArchNames;
344 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000345 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
346 it != ie; ++it) {
347 Arg *A = *it;
348
349 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbar13689542009-03-13 20:33:35 +0000350 const char *Name = A->getValue(Args);
351
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000352 // FIXME: We need to handle canonicalization of the specified
353 // arch?
354
Daniel Dunbar75877192009-03-19 07:55:12 +0000355 A->claim();
Daniel Dunbar13689542009-03-13 20:33:35 +0000356 if (ArchNames.insert(Name))
357 Archs.push_back(Name);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000358 }
359 }
360
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000361 // When there is no explicit arch for this platform, make sure we
362 // still bind the architecture (to the default) so that -Xarch_ is
363 // handled correctly.
364 if (!Archs.size())
365 Archs.push_back(0);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000366
367 // FIXME: We killed off some others but these aren't yet detected in
368 // a functional manner. If we added information to jobs about which
369 // "auxiliary" files they wrote then we could detect the conflict
370 // these cause downstream.
371 if (Archs.size() > 1) {
372 // No recovery needed, the point of this is just to prevent
373 // overwriting the same files.
374 if (const Arg *A = Args.getLastArg(options::OPT_M_Group))
375 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000376 << A->getAsString(Args);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000377 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
378 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000379 << A->getAsString(Args);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000380 }
381
382 ActionList SingleActions;
383 BuildActions(Args, SingleActions);
384
385 // Add in arch binding and lipo (if necessary) for every top level
386 // action.
387 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
388 Action *Act = SingleActions[i];
389
390 // Make sure we can lipo this kind of output. If not (and it is an
391 // actual output) then we disallow, since we can't create an
392 // output file with the right name without overwriting it. We
393 // could remove this oddity by just changing the output names to
394 // include the arch, which would also fix
395 // -save-temps. Compatibility wins for now.
396
Daniel Dunbar3dbd6c52009-03-13 17:46:02 +0000397 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000398 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
399 << types::getTypeName(Act->getType());
400
401 ActionList Inputs;
Daniel Dunbar75877192009-03-19 07:55:12 +0000402 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbar13689542009-03-13 20:33:35 +0000403 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000404
405 // Lipo if necessary, We do it this way because we need to set the
406 // arch flag so that -Xarch_ gets overwritten.
407 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
408 Actions.append(Inputs.begin(), Inputs.end());
409 else
410 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
411 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000412}
413
Daniel Dunbar21549232009-03-18 02:55:38 +0000414void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000415 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000416 // Start by constructing the list of inputs and their types.
417
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000418 // Track the current user specified (-x) input. We also explicitly
419 // track the argument used to set the type; we only want to claim
420 // the type when we actually use it, so we warn about unused -x
421 // arguments.
422 types::ID InputType = types::TY_Nothing;
423 Arg *InputTypeArg = 0;
424
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000425 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
426 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
427 it != ie; ++it) {
428 Arg *A = *it;
429
430 if (isa<InputOption>(A->getOption())) {
431 const char *Value = A->getValue(Args);
432 types::ID Ty = types::TY_INVALID;
433
434 // Infer the input type if necessary.
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000435 if (InputType == types::TY_Nothing) {
436 // If there was an explicit arg for this, claim it.
437 if (InputTypeArg)
438 InputTypeArg->claim();
439
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000440 // stdin must be handled specially.
441 if (memcmp(Value, "-", 2) == 0) {
442 // If running with -E, treat as a C input (this changes the
443 // builtin macros, for example). This may be overridden by
444 // -ObjC below.
445 //
446 // Otherwise emit an error but still use a valid type to
447 // avoid spurious errors (e.g., no inputs).
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000448 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000449 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000450 Ty = types::TY_C;
451 } else {
452 // Otherwise lookup by extension, and fallback to ObjectType
Daniel Dunbare33bea42009-03-20 23:39:23 +0000453 // if not found. We use a host hook here because Darwin at
454 // least has its own idea of what .s is.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000455 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbare33bea42009-03-20 23:39:23 +0000456 Ty = Host->lookupTypeForExtension(Ext + 1);
457
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000458 if (Ty == types::TY_INVALID)
459 Ty = types::TY_Object;
460 }
461
462 // -ObjC and -ObjC++ override the default language, but only
463 // -for "source files". We just treat everything that isn't a
464 // -linker input as a source file.
465 //
466 // FIXME: Clean this up if we move the phase sequence into the
467 // type.
468 if (Ty != types::TY_Object) {
469 if (Args.hasArg(options::OPT_ObjC))
470 Ty = types::TY_ObjC;
471 else if (Args.hasArg(options::OPT_ObjCXX))
472 Ty = types::TY_ObjCXX;
473 }
474 } else {
475 assert(InputTypeArg && "InputType set w/o InputTypeArg");
476 InputTypeArg->claim();
477 Ty = InputType;
478 }
479
480 // Check that the file exists. It isn't clear this is worth
481 // doing, since the tool presumably does this anyway, and this
482 // just adds an extra stat to the equation, but this is gcc
483 // compatible.
484 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000485 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000486 else
487 Inputs.push_back(std::make_pair(Ty, A));
488
489 } else if (A->getOption().isLinkerInput()) {
490 // Just treat as object type, we could make a special type for
491 // this if necessary.
492 Inputs.push_back(std::make_pair(types::TY_Object, A));
493
494 } else if (A->getOption().getId() == options::OPT_x) {
495 InputTypeArg = A;
496 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
497
498 // Follow gcc behavior and treat as linker input for invalid -x
499 // options. Its not clear why we shouldn't just revert to
500 // unknown; but this isn't very important, we might as well be
501 // bug comatible.
502 if (!InputType) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000503 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000504 InputType = types::TY_Object;
505 }
506 }
507 }
508
Daniel Dunbar8b1604e2009-03-13 00:17:48 +0000509 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000510 Diag(clang::diag::err_drv_no_input_files);
511 return;
512 }
513
514 // Determine which compilation mode we are in. We look for options
515 // which affect the phase, starting with the earliest phases, and
516 // record which option we used to determine the final phase.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000517 Arg *FinalPhaseArg = 0;
518 phases::ID FinalPhase;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000519
520 // -{E,M,MM} only run the preprocessor.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000521 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
522 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
523 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
524 FinalPhase = phases::Preprocess;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000525
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000526 // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler.
527 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
528 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze)) ||
529 (FinalPhaseArg = Args.getLastArg(options::OPT_emit_llvm)) ||
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000530 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
531 FinalPhase = phases::Compile;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000532
533 // -c only runs up to the assembler.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000534 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
535 FinalPhase = phases::Assemble;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000536
537 // Otherwise do everything.
538 } else
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000539 FinalPhase = phases::Link;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000540
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000541 // Reject -Z* at the top level, these options should never have been
542 // exposed by gcc.
543 if (Arg *A = Args.getLastArg(options::OPT_Z))
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000544 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000545
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000546 // Construct the actions to perform.
547 ActionList LinkerInputs;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000548 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000549 types::ID InputType = Inputs[i].first;
550 const Arg *InputArg = Inputs[i].second;
551
552 unsigned NumSteps = types::getNumCompilationPhases(InputType);
553 assert(NumSteps && "Invalid number of steps!");
554
555 // If the first step comes after the final phase we are doing as
556 // part of this compilation, warn the user about it.
557 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
558 if (InitialPhase > FinalPhase) {
Daniel Dunbar05494a72009-03-19 07:57:08 +0000559 // Claim here to avoid the more general unused warning.
560 InputArg->claim();
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000561 Diag(clang::diag::warn_drv_input_file_unused)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000562 << InputArg->getAsString(Args)
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000563 << getPhaseName(InitialPhase)
564 << FinalPhaseArg->getOption().getName();
565 continue;
566 }
567
568 // Build the pipeline for this file.
569 Action *Current = new InputAction(*InputArg, InputType);
570 for (unsigned i = 0; i != NumSteps; ++i) {
571 phases::ID Phase = types::getCompilationPhase(InputType, i);
572
573 // We are done if this step is past what the user requested.
574 if (Phase > FinalPhase)
575 break;
576
577 // Queue linker inputs.
578 if (Phase == phases::Link) {
579 assert(i + 1 == NumSteps && "linking must be final compilation step.");
580 LinkerInputs.push_back(Current);
581 Current = 0;
582 break;
583 }
584
585 // Otherwise construct the appropriate action.
586 Current = ConstructPhaseAction(Args, Phase, Current);
587 if (Current->getType() == types::TY_Nothing)
588 break;
589 }
590
591 // If we ended with something, add to the output list.
592 if (Current)
593 Actions.push_back(Current);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000594 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000595
596 // Add a link action if necessary.
597 if (!LinkerInputs.empty())
598 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
599}
600
601Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
602 Action *Input) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000603 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000604 // Build the appropriate action.
605 switch (Phase) {
606 case phases::Link: assert(0 && "link action invalid here.");
607 case phases::Preprocess: {
608 types::ID OutputTy = types::getPreprocessedType(Input->getType());
609 assert(OutputTy != types::TY_INVALID &&
610 "Cannot preprocess this input type!");
611 return new PreprocessJobAction(Input, OutputTy);
612 }
613 case phases::Precompile:
614 return new PrecompileJobAction(Input, types::TY_PCH);
615 case phases::Compile: {
616 if (Args.hasArg(options::OPT_fsyntax_only)) {
617 return new CompileJobAction(Input, types::TY_Nothing);
618 } else if (Args.hasArg(options::OPT__analyze)) {
619 return new AnalyzeJobAction(Input, types::TY_Plist);
620 } else if (Args.hasArg(options::OPT_emit_llvm)) {
621 types::ID Output =
622 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
623 return new CompileJobAction(Input, Output);
624 } else {
625 return new CompileJobAction(Input, types::TY_PP_Asm);
626 }
627 }
628 case phases::Assemble:
629 return new AssembleJobAction(Input, types::TY_Object);
630 }
631
632 assert(0 && "invalid phase in ConstructPhaseAction");
633 return 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000634}
635
Daniel Dunbar21549232009-03-18 02:55:38 +0000636void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000637 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000638 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
639 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000640
641 // FIXME: Pipes are forcibly disabled until we support executing
642 // them.
643 if (!CCCPrintBindings)
644 UsePipes = false;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000645
646 // -save-temps inhibits pipes.
647 if (SaveTemps && UsePipes) {
648 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
649 UsePipes = true;
650 }
651
652 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
653
654 // It is an error to provide a -o option if we are making multiple
655 // output files.
656 if (FinalOutput) {
657 unsigned NumOutputs = 0;
Daniel Dunbar21549232009-03-18 02:55:38 +0000658 for (ActionList::const_iterator it = C.getActions().begin(),
659 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000660 if ((*it)->getType() != types::TY_Nothing)
661 ++NumOutputs;
662
663 if (NumOutputs > 1) {
664 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
665 FinalOutput = 0;
666 }
667 }
668
Daniel Dunbar21549232009-03-18 02:55:38 +0000669 for (ActionList::const_iterator it = C.getActions().begin(),
670 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000671 Action *A = *it;
672
673 // If we are linking an image for multiple archs then the linker
674 // wants -arch_multiple and -final_output <final image
675 // name>. Unfortunately, this doesn't fit in cleanly because we
676 // have to pass this information down.
677 //
678 // FIXME: This is a hack; find a cleaner way to integrate this
679 // into the process.
680 const char *LinkingOutput = 0;
681 if (isa<LinkJobAction>(A)) {
682 if (FinalOutput)
683 LinkingOutput = FinalOutput->getValue(C.getArgs());
684 else
685 LinkingOutput = DefaultImageName.c_str();
686 }
687
688 InputInfo II;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000689 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000690 /*CanAcceptPipe*/ true,
691 /*AtTopLevel*/ true,
692 /*LinkingOutput*/ LinkingOutput,
693 II);
694 }
Daniel Dunbar586dc232009-03-16 06:42:30 +0000695
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000696 // If there were errors, don't warn about any unused arguments.
697 if (Diags.getNumErrors())
698 return;
699
Daniel Dunbar586dc232009-03-16 06:42:30 +0000700 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
701 it != ie; ++it) {
702 Arg *A = *it;
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000703
Daniel Dunbar586dc232009-03-16 06:42:30 +0000704 // FIXME: It would be nice to be able to send the argument to the
705 // Diagnostic, so that extra values, position, and so on could be
706 // printed.
707 if (!A->isClaimed())
708 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000709 << A->getAsString(C.getArgs());
Daniel Dunbar586dc232009-03-16 06:42:30 +0000710 }
Daniel Dunbar57b704d2009-03-13 22:12:33 +0000711}
712
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000713void Driver::BuildJobsForAction(Compilation &C,
714 const Action *A,
715 const ToolChain *TC,
716 bool CanAcceptPipe,
717 bool AtTopLevel,
718 const char *LinkingOutput,
719 InputInfo &Result) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000720 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action");
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000721
722 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
723 // FIXME: Pipes are forcibly disabled until we support executing
724 // them.
725 if (!CCCPrintBindings)
726 UsePipes = false;
727
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000728 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar115a7922009-03-19 07:29:38 +0000729 // FIXME: It would be nice to not claim this here; maybe the old
730 // scheme of just using Args was better?
731 const Arg &Input = IA->getInputArg();
732 Input.claim();
733 if (isa<PositionalArg>(Input)) {
734 const char *Name = Input.getValue(C.getArgs());
735 Result = InputInfo(Name, A->getType(), Name);
736 } else
737 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000738 return;
739 }
740
741 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
742 const char *ArchName = BAA->getArchName();
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000743 if (!ArchName)
744 ArchName = C.getDefaultToolChain().getArchName().c_str();
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000745 BuildJobsForAction(C,
746 *BAA->begin(),
747 Host->getToolChain(C.getArgs(), ArchName),
748 CanAcceptPipe,
749 AtTopLevel,
750 LinkingOutput,
751 Result);
752 return;
753 }
754
755 const JobAction *JA = cast<JobAction>(A);
756 const Tool &T = TC->SelectTool(C, *JA);
757
758 // See if we should use an integrated preprocessor. We do so when we
759 // have exactly one input, since this is the only use case we care
760 // about (irrelevant since we don't support combine yet).
761 bool UseIntegratedCPP = false;
762 const ActionList *Inputs = &A->getInputs();
763 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
764 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
765 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
766 !C.getArgs().hasArg(options::OPT_save_temps) &&
767 T.hasIntegratedCPP()) {
768 UseIntegratedCPP = true;
769 Inputs = &(*Inputs)[0]->getInputs();
770 }
771 }
772
773 // Only use pipes when there is exactly one input.
774 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000775 InputInfoList InputInfos;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000776 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
777 it != ie; ++it) {
778 InputInfo II;
779 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
780 /*AtTopLevel*/false,
781 LinkingOutput,
782 II);
783 InputInfos.push_back(II);
784 }
785
786 // Determine if we should output to a pipe.
787 bool OutputToPipe = false;
788 if (CanAcceptPipe && T.canPipeOutput()) {
789 // Some actions default to writing to a pipe if they are the top
790 // level phase and there was no user override.
791 //
792 // FIXME: Is there a better way to handle this?
793 if (AtTopLevel) {
794 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
795 OutputToPipe = true;
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000796 } else if (UsePipes)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000797 OutputToPipe = true;
798 }
799
800 // Figure out where to put the job (pipes).
801 Job *Dest = &C.getJobs();
802 if (InputInfos[0].isPipe()) {
Daniel Dunbar441d0602009-03-17 17:53:55 +0000803 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000804 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
805 Dest = &InputInfos[0].getPipe();
806 }
807
808 // Always use the first input as the base input.
809 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar441d0602009-03-17 17:53:55 +0000810
811 // Determine the place to write output to (nothing, pipe, or
812 // filename) and where to put the new job.
Daniel Dunbar441d0602009-03-17 17:53:55 +0000813 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000814 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000815 } else if (OutputToPipe) {
816 // Append to current piped job or create a new one as appropriate.
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000817 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
818 if (!PJ) {
819 PJ = new PipedJob();
Daniel Dunbarb7b61b22009-03-20 00:11:04 +0000820 // FIXME: Temporary hack so that -ccc-print-bindings work until
821 // we have pipe support. Please remove later.
822 if (!CCCPrintBindings)
823 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar871adcf2009-03-18 07:06:02 +0000824 Dest = PJ;
Daniel Dunbar441d0602009-03-17 17:53:55 +0000825 }
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000826 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000827 } else {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000828 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
829 A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000830 }
831
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000832 if (CCCPrintBindings) {
833 llvm::errs() << "bind - \"" << T.getName() << "\", inputs: [";
834 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
835 llvm::errs() << InputInfos[i].getAsString();
836 if (i + 1 != e)
837 llvm::errs() << ", ";
838 }
839 llvm::errs() << "], output: " << Result.getAsString() << "\n";
840 } else {
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000841 const ArgList &TCArgs = C.getArgsForToolChain(TC);
Daniel Dunbar871adcf2009-03-18 07:06:02 +0000842 T.ConstructJob(C, *JA, *Dest, Result, InputInfos, TCArgs, LinkingOutput);
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000843 }
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000844}
845
Daniel Dunbar441d0602009-03-17 17:53:55 +0000846const char *Driver::GetNamedOutputPath(Compilation &C,
847 const JobAction &JA,
848 const char *BaseInput,
849 bool AtTopLevel) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000850 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar441d0602009-03-17 17:53:55 +0000851 // Output to a user requested destination?
852 if (AtTopLevel) {
853 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
854 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
855 }
856
857 // Output to a temporary file?
858 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbar214399e2009-03-18 19:34:39 +0000859 std::string TmpName =
860 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
861 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar441d0602009-03-17 17:53:55 +0000862 }
863
864 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbar5796bf42009-03-18 02:00:31 +0000865 std::string BaseName(BasePath.getLast());
Daniel Dunbar441d0602009-03-17 17:53:55 +0000866
867 // Determine what the derived output name should be.
868 const char *NamedOutput;
869 if (JA.getType() == types::TY_Image) {
870 NamedOutput = DefaultImageName.c_str();
871 } else {
872 const char *Suffix = types::getTypeTempSuffix(JA.getType());
873 assert(Suffix && "All types used for output should have a suffix.");
874
875 std::string::size_type End = std::string::npos;
876 if (!types::appendSuffixForType(JA.getType()))
877 End = BaseName.rfind('.');
878 std::string Suffixed(BaseName.substr(0, End));
879 Suffixed += '.';
880 Suffixed += Suffix;
881 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
882 }
883
884 // As an annoying special case, PCH generation doesn't strip the
885 // pathname.
886 if (JA.getType() == types::TY_PCH) {
887 BasePath.eraseComponent();
Daniel Dunbar56c55942009-03-18 09:58:30 +0000888 if (BasePath.isEmpty())
889 BasePath = NamedOutput;
890 else
891 BasePath.appendComponent(NamedOutput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000892 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
893 } else {
894 return C.addResultFile(NamedOutput);
895 }
896}
897
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000898llvm::sys::Path Driver::GetFilePath(const char *Name,
Daniel Dunbar21549232009-03-18 02:55:38 +0000899 const ToolChain &TC) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +0000900 const ToolChain::path_list &List = TC.getFilePaths();
901 for (ToolChain::path_list::const_iterator
902 it = List.begin(), ie = List.end(); it != ie; ++it) {
903 llvm::sys::Path P(*it);
904 P.appendComponent(Name);
905 if (P.exists())
906 return P;
907 }
908
Daniel Dunbarcb881672009-03-13 00:51:18 +0000909 return llvm::sys::Path(Name);
910}
911
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000912llvm::sys::Path Driver::GetProgramPath(const char *Name,
Daniel Dunbar21549232009-03-18 02:55:38 +0000913 const ToolChain &TC) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +0000914 const ToolChain::path_list &List = TC.getProgramPaths();
915 for (ToolChain::path_list::const_iterator
916 it = List.begin(), ie = List.end(); it != ie; ++it) {
917 llvm::sys::Path P(*it);
918 P.appendComponent(Name);
919 if (P.exists())
920 return P;
921 }
922
923 // As a last resort, always search in our directory before pulling
924 // from the path.
925 llvm::sys::Path P(Dir);
926 P.appendComponent(Name);
927 if (P.exists())
928 return P;
929
Daniel Dunbar632f50e2009-03-18 21:34:08 +0000930 // Search path to increase accuracy of logging output.
931 P = llvm::sys::Program::FindProgramByName(Name);
932 if (!P.empty())
933 return P;
934
Daniel Dunbarcb881672009-03-13 00:51:18 +0000935 return llvm::sys::Path(Name);
936}
937
Daniel Dunbar214399e2009-03-18 19:34:39 +0000938std::string Driver::GetTemporaryPath(const char *Suffix) const {
939 // FIXME: This is lame; sys::Path should provide this function (in
940 // particular, it should know how to find the temporary files dir).
941 std::string Error;
942 llvm::sys::Path P("/tmp/cc");
943 if (P.makeUnique(false, &Error)) {
944 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
945 return "";
946 }
947
Daniel Dunbar84603bc2009-03-18 23:08:52 +0000948 // FIXME: Grumble, makeUnique sometimes leaves the file around!?
949 // PR3837.
950 P.eraseFromDisk(false, 0);
951
Daniel Dunbar214399e2009-03-18 19:34:39 +0000952 P.appendSuffix(Suffix);
953 return P.toString();
954}
955
Daniel Dunbare5049522009-03-17 20:45:45 +0000956const HostInfo *Driver::GetHostInfo(const char *Triple) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000957 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000958 // Dice into arch, platform, and OS. This matches
959 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
960 // and missing fields are left empty.
961 std::string Arch, Platform, OS;
962
963 if (const char *ArchEnd = strchr(Triple, '-')) {
964 Arch = std::string(Triple, ArchEnd);
965
966 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
967 Platform = std::string(ArchEnd+1, PlatformEnd);
968 OS = PlatformEnd+1;
969 } else
970 Platform = ArchEnd+1;
971 } else
972 Arch = Triple;
973
Daniel Dunbar1fd6c4b2009-03-17 19:00:50 +0000974 // Normalize Arch a bit.
975 //
976 // FIXME: This is very incomplete.
977 if (Arch == "i686")
978 Arch = "i386";
979 else if (Arch == "amd64")
980 Arch = "x86_64";
Daniel Dunbarc811b6c2009-03-18 04:41:46 +0000981 else if (Arch == "powerpc" || Arch == "Power Macintosh")
982 Arch = "ppc";
Daniel Dunbar1fd6c4b2009-03-17 19:00:50 +0000983
Daniel Dunbara88162c2009-03-13 12:23:29 +0000984 if (memcmp(&OS[0], "darwin", 6) == 0)
Daniel Dunbare5049522009-03-17 20:45:45 +0000985 return createDarwinHostInfo(*this, Arch.c_str(), Platform.c_str(),
986 OS.c_str());
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000987
Daniel Dunbare5049522009-03-17 20:45:45 +0000988 return createUnknownHostInfo(*this, Arch.c_str(), Platform.c_str(),
989 OS.c_str());
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000990}