blob: 82dc547ba393a641216d3b625b1e7f9085d01324 [file] [log] [blame]
Reid Spencera3f18552004-08-13 20:25:54 +00001//===- CompilerDriver.cpp - The LLVM Compiler Driver ------------*- C++ -*-===//
Reid Spencer5c56dc12004-08-13 20:22:43 +00002//
Misha Brukman3da94ae2005-04-22 00:00:37 +00003//
Reid Spencer5c56dc12004-08-13 20:22:43 +00004// The LLVM Compiler Infrastructure
5//
Misha Brukman3da94ae2005-04-22 00:00:37 +00006// This file was developed by Reid Spencer and is distributed under the
Reid Spencer5c56dc12004-08-13 20:22:43 +00007// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00008//
Reid Spencer5c56dc12004-08-13 20:22:43 +00009//===----------------------------------------------------------------------===//
10//
Reid Spencera3f18552004-08-13 20:25:54 +000011// This file implements the bulk of the LLVM Compiler Driver (llvmc).
Reid Spencer5c56dc12004-08-13 20:22:43 +000012//
Chris Lattner74f48d12006-05-29 18:52:05 +000013//===----------------------------------------------------------------------===//
Reid Spencer5c56dc12004-08-13 20:22:43 +000014
15#include "CompilerDriver.h"
Reid Spencerbf437722004-08-15 08:19:46 +000016#include "ConfigLexer.h"
Reid Spencera01439a2004-08-24 13:55:17 +000017#include "llvm/Module.h"
Reid Spencer52c2dc12004-08-29 19:26:56 +000018#include "llvm/Bytecode/Reader.h"
Reid Spencer54fafe42004-09-14 01:58:45 +000019#include "llvm/Support/Timer.h"
Reid Spencer93426ba2004-08-29 20:02:28 +000020#include "llvm/System/Signals.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/SetVector.h"
22#include "llvm/ADT/StringExtras.h"
Brian Gaekefabf41f2004-12-20 04:02:01 +000023#include "llvm/Config/alloca.h"
Misha Brukmanbaec07c2005-04-20 04:51:29 +000024#include <iostream>
Reid Spencer5c56dc12004-08-13 20:22:43 +000025using namespace llvm;
26
27namespace {
Reid Spencer5c56dc12004-08-13 20:22:43 +000028
Reid Spenceraf77d742004-10-28 04:05:06 +000029void WriteAction(CompilerDriver::Action* action ) {
30 std::cerr << action->program.c_str();
Reid Spencerf6358c72004-12-19 18:00:56 +000031 std::vector<std::string>::const_iterator I = action->args.begin();
Reid Spenceraf77d742004-10-28 04:05:06 +000032 while (I != action->args.end()) {
Misha Brukman0b861482005-05-03 20:30:34 +000033 std::cerr << ' ' << *I;
Reid Spenceraf77d742004-10-28 04:05:06 +000034 ++I;
35 }
Misha Brukman0b861482005-05-03 20:30:34 +000036 std::cerr << '\n';
Reid Spenceraf77d742004-10-28 04:05:06 +000037}
38
39void DumpAction(CompilerDriver::Action* action) {
40 std::cerr << "command = " << action->program.c_str();
Reid Spencerf6358c72004-12-19 18:00:56 +000041 std::vector<std::string>::const_iterator I = action->args.begin();
Reid Spenceraf77d742004-10-28 04:05:06 +000042 while (I != action->args.end()) {
Misha Brukman0b861482005-05-03 20:30:34 +000043 std::cerr << ' ' << *I;
Reid Spenceraf77d742004-10-28 04:05:06 +000044 ++I;
45 }
Misha Brukman0b861482005-05-03 20:30:34 +000046 std::cerr << '\n';
47 std::cerr << "flags = " << action->flags << '\n';
Reid Spenceraf77d742004-10-28 04:05:06 +000048}
49
50void DumpConfigData(CompilerDriver::ConfigData* cd, const std::string& type ){
Misha Brukman3da94ae2005-04-22 00:00:37 +000051 std::cerr << "Configuration Data For '" << cd->langName << "' (" << type
Reid Spenceraf77d742004-10-28 04:05:06 +000052 << ")\n";
53 std::cerr << "PreProcessor: ";
54 DumpAction(&cd->PreProcessor);
55 std::cerr << "Translator: ";
56 DumpAction(&cd->Translator);
57 std::cerr << "Optimizer: ";
58 DumpAction(&cd->Optimizer);
59 std::cerr << "Assembler: ";
60 DumpAction(&cd->Assembler);
61 std::cerr << "Linker: ";
62 DumpAction(&cd->Linker);
63}
64
65/// This specifies the passes to run for OPT_FAST_COMPILE (-O1)
66/// which should reduce the volume of code and make compilation
Misha Brukman3da94ae2005-04-22 00:00:37 +000067/// faster. This is also safe on any llvm module.
Reid Spenceraf77d742004-10-28 04:05:06 +000068static const char* DefaultFastCompileOptimizations[] = {
69 "-simplifycfg", "-mem2reg", "-instcombine"
70};
71
72class CompilerDriverImpl : public CompilerDriver {
73/// @name Constructors
74/// @{
75public:
76 CompilerDriverImpl(ConfigDataProvider& confDatProv )
77 : cdp(&confDatProv)
78 , finalPhase(LINKING)
Misha Brukman3da94ae2005-04-22 00:00:37 +000079 , optLevel(OPT_FAST_COMPILE)
Reid Spenceraf77d742004-10-28 04:05:06 +000080 , Flags(0)
81 , machine()
82 , LibraryPaths()
83 , TempDir()
84 , AdditionalArgs()
85 {
86 TempDir = sys::Path::GetTemporaryDirectory();
87 sys::RemoveDirectoryOnSignal(TempDir);
88 AdditionalArgs.reserve(NUM_PHASES);
89 StringVector emptyVec;
90 for (unsigned i = 0; i < NUM_PHASES; ++i)
91 AdditionalArgs.push_back(emptyVec);
92 }
93
94 virtual ~CompilerDriverImpl() {
95 cleanup();
96 cdp = 0;
97 LibraryPaths.clear();
98 IncludePaths.clear();
99 Defines.clear();
100 TempDir.clear();
101 AdditionalArgs.clear();
102 fOptions.clear();
103 MOptions.clear();
104 WOptions.clear();
105 }
106
107/// @}
108/// @name Methods
109/// @{
110public:
Misha Brukman0b861482005-05-03 20:30:34 +0000111 virtual void setFinalPhase(Phases phase) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000112 finalPhase = phase;
Reid Spenceraf77d742004-10-28 04:05:06 +0000113 }
114
Misha Brukman0b861482005-05-03 20:30:34 +0000115 virtual void setOptimization(OptimizationLevels level) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000116 optLevel = level;
Reid Spenceraf77d742004-10-28 04:05:06 +0000117 }
118
Misha Brukman0b861482005-05-03 20:30:34 +0000119 virtual void setDriverFlags(unsigned flags) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000120 Flags = flags & DRIVER_FLAGS_MASK;
Reid Spenceraf77d742004-10-28 04:05:06 +0000121 }
122
Misha Brukman0b861482005-05-03 20:30:34 +0000123 virtual void setOutputMachine(const std::string& machineName) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000124 machine = machineName;
125 }
126
127 virtual void setPhaseArgs(Phases phase, const StringVector& opts) {
128 assert(phase <= LINKING && phase >= PREPROCESSING);
129 AdditionalArgs[phase] = opts;
130 }
131
132 virtual void setIncludePaths(const StringVector& paths) {
133 StringVector::const_iterator I = paths.begin();
134 StringVector::const_iterator E = paths.end();
135 while (I != E) {
136 sys::Path tmp;
Reid Spencerdd04df02005-07-07 23:21:43 +0000137 tmp.set(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000138 IncludePaths.push_back(tmp);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000139 ++I;
140 }
Reid Spencer68fb37a2004-08-14 09:37:15 +0000141 }
142
Reid Spenceraf77d742004-10-28 04:05:06 +0000143 virtual void setSymbolDefines(const StringVector& defs) {
144 Defines = defs;
145 }
146
147 virtual void setLibraryPaths(const StringVector& paths) {
148 StringVector::const_iterator I = paths.begin();
149 StringVector::const_iterator E = paths.end();
150 while (I != E) {
151 sys::Path tmp;
Reid Spencerdd04df02005-07-07 23:21:43 +0000152 tmp.set(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000153 LibraryPaths.push_back(tmp);
Reid Spencerbf437722004-08-15 08:19:46 +0000154 ++I;
155 }
Reid Spencerbf437722004-08-15 08:19:46 +0000156 }
157
Misha Brukman0b861482005-05-03 20:30:34 +0000158 virtual void addLibraryPath(const sys::Path& libPath) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000159 LibraryPaths.push_back(libPath);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000160 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000161
Misha Brukman0b861482005-05-03 20:30:34 +0000162 virtual void addToolPath(const sys::Path& toolPath) {
Reid Spencer07adb282004-11-05 22:15:36 +0000163 ToolPaths.push_back(toolPath);
164 }
165
Reid Spenceraf77d742004-10-28 04:05:06 +0000166 virtual void setfPassThrough(const StringVector& fOpts) {
167 fOptions = fOpts;
168 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000169
Reid Spenceraf77d742004-10-28 04:05:06 +0000170 /// @brief Set the list of -M options to be passed through
171 virtual void setMPassThrough(const StringVector& MOpts) {
172 MOptions = MOpts;
173 }
Reid Spencerbae68252004-08-19 04:49:47 +0000174
Reid Spenceraf77d742004-10-28 04:05:06 +0000175 /// @brief Set the list of -W options to be passed through
176 virtual void setWPassThrough(const StringVector& WOpts) {
177 WOptions = WOpts;
178 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000179
Reid Spenceraf77d742004-10-28 04:05:06 +0000180/// @}
181/// @name Functions
182/// @{
183private:
184 bool isSet(DriverFlags flag) {
185 return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
186 }
Reid Spencerbae68252004-08-19 04:49:47 +0000187
Reid Spenceraf77d742004-10-28 04:05:06 +0000188 void cleanup() {
189 if (!isSet(KEEP_TEMPS_FLAG)) {
Chris Lattner33b0e9c2006-08-01 18:12:29 +0000190 sys::FileStatus Status;
191 if (!TempDir.getFileStatus(Status) && Status.isDir)
Reid Spencera229c5c2005-07-08 03:08:58 +0000192 TempDir.eraseFromDisk(/*remove_contents=*/true);
Reid Spenceraf77d742004-10-28 04:05:06 +0000193 } else {
Reid Spencer12786d52004-12-13 08:53:36 +0000194 std::cout << "Temporary files are in " << TempDir << "\n";
Reid Spenceraf77d742004-10-28 04:05:06 +0000195 }
196 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000197
Misha Brukman3da94ae2005-04-22 00:00:37 +0000198 sys::Path MakeTempFile(const std::string& basename,
Misha Brukman0b861482005-05-03 20:30:34 +0000199 const std::string& suffix) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000200 sys::Path result(TempDir);
Reid Spencerdd04df02005-07-07 23:21:43 +0000201 if (!result.appendComponent(basename))
Reid Spenceraf77d742004-10-28 04:05:06 +0000202 throw basename + ": can't use this file name";
Reid Spencer07adb282004-11-05 22:15:36 +0000203 if (!result.appendSuffix(suffix))
Reid Spenceraf77d742004-10-28 04:05:06 +0000204 throw suffix + ": can't use this file suffix";
205 return result;
206 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000207
Misha Brukman3da94ae2005-04-22 00:00:37 +0000208 Action* GetAction(ConfigData* cd,
209 const sys::Path& input,
Reid Spenceraf77d742004-10-28 04:05:06 +0000210 const sys::Path& output,
211 Phases phase)
212 {
213 Action* pat = 0; ///< The pattern/template for the action
214 Action* action = new Action; ///< The actual action to execute
Reid Spencer52c2dc12004-08-29 19:26:56 +0000215
Reid Spenceraf77d742004-10-28 04:05:06 +0000216 // Get the action pattern
217 switch (phase) {
218 case PREPROCESSING: pat = &cd->PreProcessor; break;
219 case TRANSLATION: pat = &cd->Translator; break;
220 case OPTIMIZATION: pat = &cd->Optimizer; break;
221 case ASSEMBLY: pat = &cd->Assembler; break;
222 case LINKING: pat = &cd->Linker; break;
223 default:
224 assert(!"Invalid driver phase!");
225 break;
226 }
227 assert(pat != 0 && "Invalid command pattern");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000228
Reid Spenceraf77d742004-10-28 04:05:06 +0000229 // Copy over some pattern things that don't need to change
230 action->program = pat->program;
231 action->flags = pat->flags;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000232
Reid Spenceraf77d742004-10-28 04:05:06 +0000233 // Do the substitutions from the pattern to the actual
234 StringVector::iterator PI = pat->args.begin();
235 StringVector::iterator PE = pat->args.end();
236 while (PI != PE) {
237 if ((*PI)[0] == '%' && PI->length() >2) {
238 bool found = true;
239 switch ((*PI)[1]) {
240 case 'a':
241 if (*PI == "%args%") {
242 if (AdditionalArgs.size() > unsigned(phase))
243 if (!AdditionalArgs[phase].empty()) {
244 // Get specific options for each kind of action type
245 StringVector& addargs = AdditionalArgs[phase];
246 // Add specific options for each kind of action type
Misha Brukman3da94ae2005-04-22 00:00:37 +0000247 action->args.insert(action->args.end(), addargs.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000248 addargs.end());
249 }
250 } else
251 found = false;
252 break;
Reid Spencercc97cfc2005-05-19 00:52:28 +0000253 case 'b':
254 if (*PI == "%bindir%") {
255 std::string tmp(*PI);
256 tmp.replace(0,8,LLVM_BINDIR);
257 action->args.push_back(tmp);
258 } else
259 found = false;
260 break;
Reid Spenceraf77d742004-10-28 04:05:06 +0000261 case 'd':
262 if (*PI == "%defs%") {
263 StringVector::iterator I = Defines.begin();
264 StringVector::iterator E = Defines.end();
265 while (I != E) {
266 action->args.push_back( std::string("-D") + *I);
267 ++I;
268 }
269 } else
270 found = false;
271 break;
272 case 'f':
273 if (*PI == "%fOpts%") {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000274 if (!fOptions.empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000275 action->args.insert(action->args.end(), fOptions.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000276 fOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000277 } else
278 found = false;
279 break;
280 case 'i':
281 if (*PI == "%in%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000282 action->args.push_back(input.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000283 } else if (*PI == "%incls%") {
284 PathVector::iterator I = IncludePaths.begin();
285 PathVector::iterator E = IncludePaths.end();
286 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000287 action->args.push_back( std::string("-I") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000288 ++I;
289 }
290 } else
291 found = false;
292 break;
293 case 'l':
Reid Spencercc97cfc2005-05-19 00:52:28 +0000294 if ((*PI)[1] == 'l') {
295 std::string tmp(*PI);
296 if (*PI == "%llvmgccdir%")
297 tmp.replace(0,12,LLVMGCCDIR);
298 else if (*PI == "%llvmgccarch%")
299 tmp.replace(0,13,LLVMGCCARCH);
300 else if (*PI == "%llvmgcc%")
301 tmp.replace(0,9,LLVMGCC);
302 else if (*PI == "%llvmgxx%")
303 tmp.replace(0,9,LLVMGXX);
304 else if (*PI == "%llvmcc1%")
305 tmp.replace(0,9,LLVMCC1);
Jeff Cohen00b168892005-07-27 06:12:32 +0000306 else if (*PI == "%llvmcc1plus%")
Reid Spencercc97cfc2005-05-19 00:52:28 +0000307 tmp.replace(0,9,LLVMCC1);
308 else
309 found = false;
310 if (found)
311 action->args.push_back(tmp);
312 } else if (*PI == "%libs%") {
Reid Spenceraf77d742004-10-28 04:05:06 +0000313 PathVector::iterator I = LibraryPaths.begin();
314 PathVector::iterator E = LibraryPaths.end();
315 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000316 action->args.push_back( std::string("-L") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000317 ++I;
318 }
Reid Spencercc97cfc2005-05-19 00:52:28 +0000319 } else if (*PI == "%libdir%") {
320 std::string tmp(*PI);
321 tmp.replace(0,8,LLVM_LIBDIR);
322 action->args.push_back(tmp);
Reid Spenceraf77d742004-10-28 04:05:06 +0000323 } else
324 found = false;
325 break;
326 case 'o':
327 if (*PI == "%out%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000328 action->args.push_back(output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000329 } else if (*PI == "%opt%") {
330 if (!isSet(EMIT_RAW_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000331 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
Reid Spenceraf77d742004-10-28 04:05:06 +0000332 !cd->opts[optLevel].empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000333 action->args.insert(action->args.end(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000334 cd->opts[optLevel].begin(),
335 cd->opts[optLevel].end());
336 else
Misha Brukman3da94ae2005-04-22 00:00:37 +0000337 throw std::string("Optimization options for level ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000338 utostr(unsigned(optLevel)) + " were not specified";
339 }
340 } else
341 found = false;
342 break;
343 case 's':
344 if (*PI == "%stats%") {
345 if (isSet(SHOW_STATS_FLAG))
346 action->args.push_back("-stats");
347 } else
348 found = false;
349 break;
350 case 't':
351 if (*PI == "%target%") {
352 action->args.push_back(std::string("-march=") + machine);
353 } else if (*PI == "%time%") {
354 if (isSet(TIME_PASSES_FLAG))
355 action->args.push_back("-time-passes");
356 } else
357 found = false;
358 break;
359 case 'v':
360 if (*PI == "%verbose%") {
361 if (isSet(VERBOSE_FLAG))
362 action->args.push_back("-v");
363 } else
364 found = false;
365 break;
366 case 'M':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000367 if (*PI == "%Mopts%") {
368 if (!MOptions.empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000369 action->args.insert(action->args.end(), MOptions.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000370 MOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000371 } else
372 found = false;
373 break;
374 case 'W':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000375 if (*PI == "%Wopts%") {
376 for (StringVector::iterator I = WOptions.begin(),
377 E = WOptions.end(); I != E ; ++I ) {
Misha Brukman0b861482005-05-03 20:30:34 +0000378 action->args.push_back(std::string("-W") + *I);
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000379 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000380 } else
381 found = false;
382 break;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000383 default:
Reid Spenceraf77d742004-10-28 04:05:06 +0000384 found = false;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000385 break;
386 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000387 if (!found) {
388 // Did it even look like a substitution?
Misha Brukman3da94ae2005-04-22 00:00:37 +0000389 if (PI->length()>1 && (*PI)[0] == '%' &&
Reid Spenceraf77d742004-10-28 04:05:06 +0000390 (*PI)[PI->length()-1] == '%') {
391 throw std::string("Invalid substitution token: '") + *PI +
Reid Spencer1fce0912004-12-11 00:14:15 +0000392 "' for command '" + pat->program.toString() + "'";
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000393 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000394 // It's not a legal substitution, just pass it through
Reid Spencer52c2dc12004-08-29 19:26:56 +0000395 action->args.push_back(*PI);
396 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000397 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000398 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000399 // Its not a substitution, just put it in the action
400 action->args.push_back(*PI);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000401 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000402 PI++;
403 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000404
Reid Spenceraf77d742004-10-28 04:05:06 +0000405 // Finally, we're done
406 return action;
407 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000408
Reid Spenceraf77d742004-10-28 04:05:06 +0000409 bool DoAction(Action*action) {
410 assert(action != 0 && "Invalid Action!");
411 if (isSet(VERBOSE_FLAG))
412 WriteAction(action);
413 if (!isSet(DRY_RUN_FLAG)) {
414 sys::Path progpath = sys::Program::FindProgramByName(
Reid Spencer1fce0912004-12-11 00:14:15 +0000415 action->program.toString());
Reid Spencer07adb282004-11-05 22:15:36 +0000416 if (progpath.isEmpty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000417 throw std::string("Can't find program '" +
418 action->program.toString()+"'");
Reid Spencerc7f08322005-07-07 18:21:42 +0000419 else if (progpath.canExecute())
Reid Spenceraf77d742004-10-28 04:05:06 +0000420 action->program = progpath;
421 else
Reid Spencer1fce0912004-12-11 00:14:15 +0000422 throw std::string("Program '"+action->program.toString()+
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000423 "' is not executable.");
Reid Spenceraf77d742004-10-28 04:05:06 +0000424
425 // Invoke the program
Misha Brukman3da94ae2005-04-22 00:00:37 +0000426 const char** Args = (const char**)
Reid Spencerc30088f2005-04-11 05:48:04 +0000427 alloca(sizeof(const char*)*(action->args.size()+2));
428 Args[0] = action->program.toString().c_str();
429 for (unsigned i = 1; i != action->args.size(); ++i)
Reid Spencerf6358c72004-12-19 18:00:56 +0000430 Args[i] = action->args[i].c_str();
Chris Lattner7456e3c2005-02-13 23:10:45 +0000431 Args[action->args.size()] = 0; // null terminate list.
Reid Spenceraf77d742004-10-28 04:05:06 +0000432 if (isSet(TIME_ACTIONS_FLAG)) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000433 Timer timer(action->program.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000434 timer.startTimer();
Chris Lattner7456e3c2005-02-13 23:10:45 +0000435 int resultCode = sys::Program::ExecuteAndWait(action->program, Args);
Reid Spenceraf77d742004-10-28 04:05:06 +0000436 timer.stopTimer();
437 timer.print(timer,std::cerr);
438 return resultCode == 0;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000439 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000440 else
Chris Lattner7456e3c2005-02-13 23:10:45 +0000441 return 0 == sys::Program::ExecuteAndWait(action->program, Args);
Reid Spenceraf77d742004-10-28 04:05:06 +0000442 }
443 return true;
444 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000445
Reid Spenceraf77d742004-10-28 04:05:06 +0000446 /// This method tries various variants of a linkage item's file
447 /// name to see if it can find an appropriate file to link with
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000448 /// in the directories of the LibraryPaths.
Reid Spenceraf77d742004-10-28 04:05:06 +0000449 llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
Reid Spenceraf77d742004-10-28 04:05:06 +0000450 bool native = false) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000451 sys::Path fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000452 fullpath.set(link_item);
Reid Spencerc7f08322005-07-07 18:21:42 +0000453 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000454 return fullpath;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000455 for (PathVector::iterator PI = LibraryPaths.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000456 PE = LibraryPaths.end(); PI != PE; ++PI) {
Reid Spencerdd04df02005-07-07 23:21:43 +0000457 fullpath.set(PI->toString());
458 fullpath.appendComponent(link_item);
Reid Spencerc7f08322005-07-07 18:21:42 +0000459 if (fullpath.canRead())
Reid Spenceraf77d742004-10-28 04:05:06 +0000460 return fullpath;
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000461 if (native) {
462 fullpath.appendSuffix("a");
463 } else {
464 fullpath.appendSuffix("bc");
Reid Spencerc7f08322005-07-07 18:21:42 +0000465 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000466 return fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000467 fullpath.eraseSuffix();
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000468 fullpath.appendSuffix("o");
Reid Spencerc7f08322005-07-07 18:21:42 +0000469 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000470 return fullpath;
471 fullpath = *PI;
Reid Spencerdd04df02005-07-07 23:21:43 +0000472 fullpath.appendComponent(std::string("lib") + link_item);
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000473 fullpath.appendSuffix("a");
Reid Spencerc7f08322005-07-07 18:21:42 +0000474 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000475 return fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000476 fullpath.eraseSuffix();
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000477 fullpath.appendSuffix("so");
Reid Spencerc7f08322005-07-07 18:21:42 +0000478 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000479 return fullpath;
480 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000481 }
482
483 // Didn't find one.
484 fullpath.clear();
485 return fullpath;
486 }
487
488 /// This method processes a linkage item. The item could be a
489 /// Bytecode file needing translation to native code and that is
490 /// dependent on other bytecode libraries, or a native code
491 /// library that should just be linked into the program.
492 bool ProcessLinkageItem(const llvm::sys::Path& link_item,
493 SetVector<sys::Path>& set,
494 std::string& err) {
495 // First, see if the unadorned file name is not readable. If so,
496 // we must track down the file in the lib search path.
497 sys::Path fullpath;
Reid Spencerc7f08322005-07-07 18:21:42 +0000498 if (!link_item.canRead()) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000499 // look for the library using the -L arguments specified
Reid Spenceraf77d742004-10-28 04:05:06 +0000500 // on the command line.
Reid Spencer1fce0912004-12-11 00:14:15 +0000501 fullpath = GetPathForLinkageItem(link_item.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000502
Reid Spenceraf77d742004-10-28 04:05:06 +0000503 // If we didn't find the file in any of the library search paths
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000504 // we have to bail. No where else to look.
Reid Spencer07adb282004-11-05 22:15:36 +0000505 if (fullpath.isEmpty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000506 err =
Reid Spencer1fce0912004-12-11 00:14:15 +0000507 std::string("Can't find linkage item '") + link_item.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000508 return false;
509 }
510 } else {
511 fullpath = link_item;
512 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000513
Reid Spenceraf77d742004-10-28 04:05:06 +0000514 // If we got here fullpath is the path to the file, and its readable.
515 set.insert(fullpath);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000516
Reid Spenceraf77d742004-10-28 04:05:06 +0000517 // If its an LLVM bytecode file ...
Reid Spencer07adb282004-11-05 22:15:36 +0000518 if (fullpath.isBytecodeFile()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000519 // Process the dependent libraries recursively
520 Module::LibraryListType modlibs;
Reid Spencer1fce0912004-12-11 00:14:15 +0000521 if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs)) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000522 // Traverse the dependent libraries list
523 Module::lib_iterator LI = modlibs.begin();
524 Module::lib_iterator LE = modlibs.end();
525 while ( LI != LE ) {
526 if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
527 if (err.empty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000528 err = std::string("Library '") + *LI +
Reid Spenceraf77d742004-10-28 04:05:06 +0000529 "' is not valid for linking but is required by file '" +
Reid Spencer1fce0912004-12-11 00:14:15 +0000530 fullpath.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000531 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000532 err += " which is required by file '" + fullpath.toString() + "'";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000533 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000534 return false;
535 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000536 ++LI;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000537 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000538 } else if (err.empty()) {
539 err = std::string(
Misha Brukman3da94ae2005-04-22 00:00:37 +0000540 "The dependent libraries could not be extracted from '") +
Reid Spencer1fce0912004-12-11 00:14:15 +0000541 fullpath.toString();
Reid Spenceraf77d742004-10-28 04:05:06 +0000542 return false;
543 }
544 }
545 return true;
546 }
547
548/// @}
549/// @name Methods
550/// @{
551public:
552 virtual int execute(const InputList& InpList, const sys::Path& Output ) {
553 try {
554 // Echo the configuration of options if we're running verbose
555 if (isSet(DEBUG_FLAG)) {
556 std::cerr << "Compiler Driver Options:\n";
557 std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
558 std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
559 std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
560 std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
561 std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
562 std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
563 std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
564 std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
565 std::cerr << "OutputMachine = " << machine << "\n";
566 InputList::const_iterator I = InpList.begin();
567 while ( I != InpList.end() ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000568 std::cerr << "Input: " << I->first << "(" << I->second
Reid Spenceraf77d742004-10-28 04:05:06 +0000569 << ")\n";
570 ++I;
571 }
Reid Spencer12786d52004-12-13 08:53:36 +0000572 std::cerr << "Output: " << Output << "\n";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000573 }
574
Reid Spenceraf77d742004-10-28 04:05:06 +0000575 // If there's no input, we're done.
576 if (InpList.empty())
577 throw std::string("Nothing to compile.");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000578
Reid Spenceraf77d742004-10-28 04:05:06 +0000579 // If they are asking for linking and didn't provide an output
580 // file then its an error (no way for us to "make up" a meaningful
581 // file name based on the various linker input files).
Reid Spencer07adb282004-11-05 22:15:36 +0000582 if (finalPhase == LINKING && Output.isEmpty())
Reid Spenceraf77d742004-10-28 04:05:06 +0000583 throw std::string(
584 "An output file name must be specified for linker output");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000585
Reid Spenceraf77d742004-10-28 04:05:06 +0000586 // If they are not asking for linking, provided an output file and
587 // there is more than one input file, its an error
Reid Spencer07adb282004-11-05 22:15:36 +0000588 if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1)
Reid Spenceraf77d742004-10-28 04:05:06 +0000589 throw std::string("An output file name cannot be specified ") +
590 "with more than one input file name when not linking";
591
592 // This vector holds all the resulting actions of the following loop.
593 std::vector<Action*> actions;
594
595 /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
596 // for each input item
597 SetVector<sys::Path> LinkageItems;
Reid Spencerf6358c72004-12-19 18:00:56 +0000598 StringVector LibFiles;
Reid Spenceraf77d742004-10-28 04:05:06 +0000599 InputList::const_iterator I = InpList.begin();
Reid Spencer679a7232004-11-20 20:39:33 +0000600 for (InputList::const_iterator I = InpList.begin(), E = InpList.end();
601 I != E; ++I ) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000602 // Get the suffix of the file name
603 const std::string& ftype = I->second;
604
Misha Brukman3da94ae2005-04-22 00:00:37 +0000605 // If its a library, bytecode file, or object file, save
606 // it for linking below and short circuit the
Reid Spenceraf77d742004-10-28 04:05:06 +0000607 // pre-processing/translation/assembly phases
608 if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000609 // We shouldn't get any of these types of files unless we're
Reid Spenceraf77d742004-10-28 04:05:06 +0000610 // later going to link. Enforce this limit now.
611 if (finalPhase != LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000612 throw std::string(
Reid Spenceraf77d742004-10-28 04:05:06 +0000613 "Pre-compiled objects found but linking not requested");
614 }
615 if (ftype.empty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000616 LibFiles.push_back(I->first.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000617 else
618 LinkageItems.insert(I->first);
Reid Spencer679a7232004-11-20 20:39:33 +0000619 continue; // short circuit remainder of loop
Reid Spenceraf77d742004-10-28 04:05:06 +0000620 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000621
Reid Spenceraf77d742004-10-28 04:05:06 +0000622 // At this point, we know its something we need to translate
623 // and/or optimize. See if we can get the configuration data
624 // for this kind of file.
625 ConfigData* cd = cdp->ProvideConfigData(I->second);
626 if (cd == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000627 throw std::string("Files of type '") + I->second +
628 "' are not recognized.";
Reid Spenceraf77d742004-10-28 04:05:06 +0000629 if (isSet(DEBUG_FLAG))
630 DumpConfigData(cd,I->second);
Reid Spencer54fafe42004-09-14 01:58:45 +0000631
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000632 // Add the config data's library paths to the end of the list
633 for (StringVector::iterator LPI = cd->libpaths.begin(),
634 LPE = cd->libpaths.end(); LPI != LPE; ++LPI){
635 LibraryPaths.push_back(sys::Path(*LPI));
636 }
637
Reid Spenceraf77d742004-10-28 04:05:06 +0000638 // Initialize the input and output files
639 sys::Path InFile(I->first);
Reid Spencer07adb282004-11-05 22:15:36 +0000640 sys::Path OutFile(I->first.getBasename());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000641
Reid Spenceraf77d742004-10-28 04:05:06 +0000642 // PRE-PROCESSING PHASE
643 Action& action = cd->PreProcessor;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000644
Reid Spenceraf77d742004-10-28 04:05:06 +0000645 // Get the preprocessing action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000646 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000647 if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
648 if (finalPhase == PREPROCESSING) {
Reid Spencer679a7232004-11-20 20:39:33 +0000649 if (Output.isEmpty()) {
650 OutFile.appendSuffix("E");
651 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
652 } else {
653 actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
654 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000655 } else {
Reid Spencer07adb282004-11-05 22:15:36 +0000656 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"E"));
Reid Spenceraf77d742004-10-28 04:05:06 +0000657 actions.push_back(GetAction(cd,InFile,TempFile,
658 PREPROCESSING));
659 InFile = TempFile;
660 }
661 }
662 } else if (finalPhase == PREPROCESSING) {
663 throw cd->langName + " does not support pre-processing";
664 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000665 throw std::string("Don't know how to pre-process ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000666 cd->langName + " files";
667 }
668
Misha Brukman3da94ae2005-04-22 00:00:37 +0000669 // Short-circuit remaining actions if all they want is
Reid Spenceraf77d742004-10-28 04:05:06 +0000670 // pre-processing
Reid Spencer679a7232004-11-20 20:39:33 +0000671 if (finalPhase == PREPROCESSING) { continue; };
Reid Spenceraf77d742004-10-28 04:05:06 +0000672
673 /// TRANSLATION PHASE
674 action = cd->Translator;
675
676 // Get the translation action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000677 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000678 if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
679 if (finalPhase == TRANSLATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000680 if (Output.isEmpty()) {
681 OutFile.appendSuffix("o");
682 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
683 } else {
684 actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
685 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000686 } else {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000687 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"trans"));
Reid Spenceraf77d742004-10-28 04:05:06 +0000688 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
689 InFile = TempFile;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000690 }
691
Reid Spenceraf77d742004-10-28 04:05:06 +0000692 // ll -> bc Helper
693 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
694 /// The output of the translator is an LLVM Assembly program
695 /// We need to translate it to bytecode
696 Action* action = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000697 action->program.set("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000698 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000699 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000700 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000701 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000702 actions.push_back(action);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000703 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000704 }
705 } else if (finalPhase == TRANSLATION) {
706 throw cd->langName + " does not support translation";
707 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000708 throw std::string("Don't know how to translate ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000709 cd->langName + " files";
710 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000711
Reid Spenceraf77d742004-10-28 04:05:06 +0000712 // Short-circuit remaining actions if all they want is translation
Reid Spencer679a7232004-11-20 20:39:33 +0000713 if (finalPhase == TRANSLATION) { continue; }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000714
Reid Spenceraf77d742004-10-28 04:05:06 +0000715 /// OPTIMIZATION PHASE
716 action = cd->Optimizer;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000717
Reid Spenceraf77d742004-10-28 04:05:06 +0000718 // Get the optimization action, if needed, or error if appropriate
719 if (!isSet(EMIT_RAW_FLAG)) {
Reid Spencer07adb282004-11-05 22:15:36 +0000720 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000721 if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
722 if (finalPhase == OPTIMIZATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000723 if (Output.isEmpty()) {
724 OutFile.appendSuffix("o");
725 actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
726 } else {
727 actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
728 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000729 } else {
Reid Spencer07adb282004-11-05 22:15:36 +0000730 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"opt"));
Reid Spenceraf77d742004-10-28 04:05:06 +0000731 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
732 InFile = TempFile;
733 }
734 // ll -> bc Helper
735 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
736 /// The output of the optimizer is an LLVM Assembly program
737 /// We need to translate it to bytecode with llvm-as
Reid Spencer52c2dc12004-08-29 19:26:56 +0000738 Action* action = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000739 action->program.set("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000740 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000741 action->args.push_back("-f");
742 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000743 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000744 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000745 actions.push_back(action);
746 }
747 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000748 } else if (finalPhase == OPTIMIZATION) {
749 throw cd->langName + " does not support optimization";
750 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000751 throw std::string("Don't know how to optimize ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000752 cd->langName + " files";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000753 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000754 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000755
756 // Short-circuit remaining actions if all they want is optimization
Reid Spencer679a7232004-11-20 20:39:33 +0000757 if (finalPhase == OPTIMIZATION) { continue; }
Reid Spenceraf77d742004-10-28 04:05:06 +0000758
759 /// ASSEMBLY PHASE
760 action = cd->Assembler;
761
762 if (finalPhase == ASSEMBLY) {
Reid Spencer679a7232004-11-20 20:39:33 +0000763
764 // Build either a native compilation action or a disassembly action
765 Action* action = new Action();
Reid Spenceraf77d742004-10-28 04:05:06 +0000766 if (isSet(EMIT_NATIVE_FLAG)) {
767 // Use llc to get the native assembly file
Reid Spencerdd04df02005-07-07 23:21:43 +0000768 action->program.set("llc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000769 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000770 action->args.push_back("-f");
771 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000772 if (Output.isEmpty()) {
773 OutFile.appendSuffix("o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000774 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000775 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000776 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000777 }
778 actions.push_back(action);
Reid Spenceraf77d742004-10-28 04:05:06 +0000779 } else {
780 // Just convert back to llvm assembly with llvm-dis
Reid Spencerdd04df02005-07-07 23:21:43 +0000781 action->program.set("llvm-dis");
Reid Spencer1fce0912004-12-11 00:14:15 +0000782 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000783 action->args.push_back("-f");
784 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000785 if (Output.isEmpty()) {
786 OutFile.appendSuffix("ll");
Reid Spencer1fce0912004-12-11 00:14:15 +0000787 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000788 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000789 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000790 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000791 }
792
Reid Spencer679a7232004-11-20 20:39:33 +0000793 // Put the action on the list
794 actions.push_back(action);
795
Misha Brukman3da94ae2005-04-22 00:00:37 +0000796 // Short circuit the rest of the loop, we don't want to link
Reid Spenceraf77d742004-10-28 04:05:06 +0000797 continue;
798 }
799
800 // Register the result of the actions as a link candidate
801 LinkageItems.insert(InFile);
802
Reid Spenceraf77d742004-10-28 04:05:06 +0000803 } // end while loop over each input file
804
805 /// RUN THE COMPILATION ACTIONS
806 std::vector<Action*>::iterator AI = actions.begin();
807 std::vector<Action*>::iterator AE = actions.end();
808 while (AI != AE) {
809 if (!DoAction(*AI))
810 throw std::string("Action failed");
811 AI++;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000812 }
813
Reid Spenceraf77d742004-10-28 04:05:06 +0000814 /// LINKING PHASE
815 if (finalPhase == LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000816
Reid Spenceraf77d742004-10-28 04:05:06 +0000817 // Insert the platform-specific system libraries to the path list
Reid Spencer11db4b82004-12-13 03:01:26 +0000818 std::vector<sys::Path> SysLibs;
819 sys::Path::GetSystemLibraryPaths(SysLibs);
820 LibraryPaths.insert(LibraryPaths.end(), SysLibs.begin(), SysLibs.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000821
822 // Set up the linking action with llvm-ld
823 Action* link = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000824 link->program.set("llvm-ld");
Reid Spenceraf77d742004-10-28 04:05:06 +0000825
826 // Add in the optimization level requested
827 switch (optLevel) {
828 case OPT_FAST_COMPILE:
829 link->args.push_back("-O1");
830 break;
831 case OPT_SIMPLE:
832 link->args.push_back("-O2");
833 break;
834 case OPT_AGGRESSIVE:
835 link->args.push_back("-O3");
836 break;
837 case OPT_LINK_TIME:
838 link->args.push_back("-O4");
839 break;
840 case OPT_AGGRESSIVE_LINK_TIME:
841 link->args.push_back("-O5");
842 break;
843 case OPT_NONE:
844 break;
845 }
846
847 // Add in all the linkage items we generated. This includes the
848 // output from the translation/optimization phases as well as any
849 // -l arguments specified.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000850 for (PathVector::const_iterator I=LinkageItems.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000851 E=LinkageItems.end(); I != E; ++I )
Reid Spencer1fce0912004-12-11 00:14:15 +0000852 link->args.push_back(I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000853
854 // Add in all the libraries we found.
Reid Spencerf6358c72004-12-19 18:00:56 +0000855 for (StringVector::const_iterator I=LibFiles.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000856 E=LibFiles.end(); I != E; ++I )
857 link->args.push_back(std::string("-l")+*I);
858
859 // Add in all the library paths to the command line
860 for (PathVector::const_iterator I=LibraryPaths.begin(),
861 E=LibraryPaths.end(); I != E; ++I)
Reid Spencer1fce0912004-12-11 00:14:15 +0000862 link->args.push_back( std::string("-L") + I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000863
864 // Add in the additional linker arguments requested
865 for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(),
866 E=AdditionalArgs[LINKING].end(); I != E; ++I)
867 link->args.push_back( *I );
868
869 // Add in other optional flags
870 if (isSet(EMIT_NATIVE_FLAG))
871 link->args.push_back("-native");
872 if (isSet(VERBOSE_FLAG))
873 link->args.push_back("-v");
874 if (isSet(TIME_PASSES_FLAG))
875 link->args.push_back("-time-passes");
876 if (isSet(SHOW_STATS_FLAG))
877 link->args.push_back("-stats");
878 if (isSet(STRIP_OUTPUT_FLAG))
879 link->args.push_back("-s");
880 if (isSet(DEBUG_FLAG)) {
881 link->args.push_back("-debug");
882 link->args.push_back("-debug-pass=Details");
883 }
884
885 // Add in mandatory flags
886 link->args.push_back("-o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000887 link->args.push_back(Output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000888
889 // Execute the link
890 if (!DoAction(link))
891 throw std::string("Action failed");
892 }
893 } catch (std::string& msg) {
894 cleanup();
895 throw;
896 } catch (...) {
897 cleanup();
898 throw std::string("Unspecified error");
899 }
900 cleanup();
901 return 0;
902 }
903
904/// @}
905/// @name Data
906/// @{
907private:
908 ConfigDataProvider* cdp; ///< Where we get configuration data from
909 Phases finalPhase; ///< The final phase of compilation
910 OptimizationLevels optLevel; ///< The optimization level to apply
911 unsigned Flags; ///< The driver flags
912 std::string machine; ///< Target machine name
913 PathVector LibraryPaths; ///< -L options
914 PathVector IncludePaths; ///< -I options
Reid Spencer07adb282004-11-05 22:15:36 +0000915 PathVector ToolPaths; ///< -B options
Reid Spenceraf77d742004-10-28 04:05:06 +0000916 StringVector Defines; ///< -D options
917 sys::Path TempDir; ///< Name of the temporary directory.
918 StringTable AdditionalArgs; ///< The -Txyz options
919 StringVector fOptions; ///< -f options
920 StringVector MOptions; ///< -M options
921 StringVector WOptions; ///< -W options
922
923/// @}
924};
Reid Spencer5c56dc12004-08-13 20:22:43 +0000925}
926
927CompilerDriver::~CompilerDriver() {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000928}
929
Reid Spencercc97cfc2005-05-19 00:52:28 +0000930CompilerDriver::ConfigDataProvider::~ConfigDataProvider() {}
931
Reid Spencer52c2dc12004-08-29 19:26:56 +0000932CompilerDriver*
933CompilerDriver::Get(ConfigDataProvider& CDP) {
934 return new CompilerDriverImpl(CDP);
Reid Spencerbae68252004-08-19 04:49:47 +0000935}
936
937CompilerDriver::ConfigData::ConfigData()
938 : langName()
939 , PreProcessor()
940 , Translator()
941 , Optimizer()
942 , Assembler()
943 , Linker()
944{
945 StringVector emptyVec;
946 for (unsigned i = 0; i < NUM_PHASES; ++i)
947 opts.push_back(emptyVec);
Reid Spencer5c56dc12004-08-13 20:22:43 +0000948}