blob: 4092eda610383ac7d678f57a37e1009c85e3b3b6 [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
Chris Lattner7cf7c2b2007-02-07 23:48:32 +000065static bool GetBytecodeDependentLibraries(const std::string &fname,
66 Module::LibraryListType& deplibs,
67 BCDecompressor_t *BCDC,
68 std::string* ErrMsg) {
69 ModuleProvider* MP = getBytecodeModuleProvider(fname, BCDC, ErrMsg);
70 if (!MP) {
71 deplibs.clear();
72 return true;
73 }
74 Module* M = MP->releaseModule(ErrMsg);
75 deplibs = M->getLibraries();
76 delete M;
77 delete MP;
78 return false;
79}
80
81
Reid Spenceraf77d742004-10-28 04:05:06 +000082class CompilerDriverImpl : public CompilerDriver {
83/// @name Constructors
84/// @{
85public:
86 CompilerDriverImpl(ConfigDataProvider& confDatProv )
87 : cdp(&confDatProv)
88 , finalPhase(LINKING)
Misha Brukman3da94ae2005-04-22 00:00:37 +000089 , optLevel(OPT_FAST_COMPILE)
Reid Spenceraf77d742004-10-28 04:05:06 +000090 , Flags(0)
91 , machine()
92 , LibraryPaths()
93 , TempDir()
94 , AdditionalArgs()
95 {
Reid Spenceraf77d742004-10-28 04:05:06 +000096 AdditionalArgs.reserve(NUM_PHASES);
97 StringVector emptyVec;
98 for (unsigned i = 0; i < NUM_PHASES; ++i)
99 AdditionalArgs.push_back(emptyVec);
100 }
101
102 virtual ~CompilerDriverImpl() {
103 cleanup();
104 cdp = 0;
105 LibraryPaths.clear();
106 IncludePaths.clear();
107 Defines.clear();
108 TempDir.clear();
109 AdditionalArgs.clear();
110 fOptions.clear();
111 MOptions.clear();
112 WOptions.clear();
113 }
114
115/// @}
116/// @name Methods
117/// @{
118public:
Misha Brukman0b861482005-05-03 20:30:34 +0000119 virtual void setFinalPhase(Phases phase) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000120 finalPhase = phase;
Reid Spenceraf77d742004-10-28 04:05:06 +0000121 }
122
Misha Brukman0b861482005-05-03 20:30:34 +0000123 virtual void setOptimization(OptimizationLevels level) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000124 optLevel = level;
Reid Spenceraf77d742004-10-28 04:05:06 +0000125 }
126
Misha Brukman0b861482005-05-03 20:30:34 +0000127 virtual void setDriverFlags(unsigned flags) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000128 Flags = flags & DRIVER_FLAGS_MASK;
Reid Spenceraf77d742004-10-28 04:05:06 +0000129 }
130
Misha Brukman0b861482005-05-03 20:30:34 +0000131 virtual void setOutputMachine(const std::string& machineName) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000132 machine = machineName;
133 }
134
135 virtual void setPhaseArgs(Phases phase, const StringVector& opts) {
136 assert(phase <= LINKING && phase >= PREPROCESSING);
137 AdditionalArgs[phase] = opts;
138 }
139
140 virtual void setIncludePaths(const StringVector& paths) {
141 StringVector::const_iterator I = paths.begin();
142 StringVector::const_iterator E = paths.end();
143 while (I != E) {
144 sys::Path tmp;
Reid Spencerdd04df02005-07-07 23:21:43 +0000145 tmp.set(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000146 IncludePaths.push_back(tmp);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000147 ++I;
148 }
Reid Spencer68fb37a2004-08-14 09:37:15 +0000149 }
150
Reid Spenceraf77d742004-10-28 04:05:06 +0000151 virtual void setSymbolDefines(const StringVector& defs) {
152 Defines = defs;
153 }
154
155 virtual void setLibraryPaths(const StringVector& paths) {
156 StringVector::const_iterator I = paths.begin();
157 StringVector::const_iterator E = paths.end();
158 while (I != E) {
159 sys::Path tmp;
Reid Spencerdd04df02005-07-07 23:21:43 +0000160 tmp.set(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000161 LibraryPaths.push_back(tmp);
Reid Spencerbf437722004-08-15 08:19:46 +0000162 ++I;
163 }
Reid Spencerbf437722004-08-15 08:19:46 +0000164 }
165
Misha Brukman0b861482005-05-03 20:30:34 +0000166 virtual void addLibraryPath(const sys::Path& libPath) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000167 LibraryPaths.push_back(libPath);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000168 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000169
Misha Brukman0b861482005-05-03 20:30:34 +0000170 virtual void addToolPath(const sys::Path& toolPath) {
Reid Spencer07adb282004-11-05 22:15:36 +0000171 ToolPaths.push_back(toolPath);
172 }
173
Reid Spenceraf77d742004-10-28 04:05:06 +0000174 virtual void setfPassThrough(const StringVector& fOpts) {
175 fOptions = fOpts;
176 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000177
Reid Spenceraf77d742004-10-28 04:05:06 +0000178 /// @brief Set the list of -M options to be passed through
179 virtual void setMPassThrough(const StringVector& MOpts) {
180 MOptions = MOpts;
181 }
Reid Spencerbae68252004-08-19 04:49:47 +0000182
Reid Spenceraf77d742004-10-28 04:05:06 +0000183 /// @brief Set the list of -W options to be passed through
184 virtual void setWPassThrough(const StringVector& WOpts) {
185 WOptions = WOpts;
186 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000187
Reid Spenceraf77d742004-10-28 04:05:06 +0000188/// @}
189/// @name Functions
190/// @{
191private:
192 bool isSet(DriverFlags flag) {
193 return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
194 }
Reid Spencerbae68252004-08-19 04:49:47 +0000195
Reid Spenceraf77d742004-10-28 04:05:06 +0000196 void cleanup() {
197 if (!isSet(KEEP_TEMPS_FLAG)) {
Reid Spencerc74b4612007-04-07 18:53:16 +0000198 const sys::FileStatus *Status =
199 sys::PathWithStatus(TempDir).getFileStatus();
Reid Spencer8475ec02007-03-29 19:05:44 +0000200 if (Status && Status->isDir)
Reid Spencera229c5c2005-07-08 03:08:58 +0000201 TempDir.eraseFromDisk(/*remove_contents=*/true);
Reid Spenceraf77d742004-10-28 04:05:06 +0000202 } else {
Reid Spencer12786d52004-12-13 08:53:36 +0000203 std::cout << "Temporary files are in " << TempDir << "\n";
Reid Spenceraf77d742004-10-28 04:05:06 +0000204 }
205 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000206
Misha Brukman3da94ae2005-04-22 00:00:37 +0000207 sys::Path MakeTempFile(const std::string& basename,
Reid Spencer48744762006-08-22 19:01:30 +0000208 const std::string& suffix,
209 std::string* ErrMsg) {
210 if (TempDir.isEmpty()) {
211 TempDir = sys::Path::GetTemporaryDirectory(ErrMsg);
212 if (TempDir.isEmpty())
213 return sys::Path();
214 sys::RemoveDirectoryOnSignal(TempDir);
215 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000216 sys::Path result(TempDir);
Reid Spencer48744762006-08-22 19:01:30 +0000217 if (!result.appendComponent(basename)) {
218 if (ErrMsg)
219 *ErrMsg = basename + ": can't use this file name";
220 return sys::Path();
221 }
222 if (!result.appendSuffix(suffix)) {
223 if (ErrMsg)
224 *ErrMsg = suffix + ": can't use this file suffix";
225 return sys::Path();
226 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000227 return result;
228 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000229
Misha Brukman3da94ae2005-04-22 00:00:37 +0000230 Action* GetAction(ConfigData* cd,
231 const sys::Path& input,
Reid Spenceraf77d742004-10-28 04:05:06 +0000232 const sys::Path& output,
233 Phases phase)
234 {
235 Action* pat = 0; ///< The pattern/template for the action
236 Action* action = new Action; ///< The actual action to execute
Reid Spencer52c2dc12004-08-29 19:26:56 +0000237
Reid Spenceraf77d742004-10-28 04:05:06 +0000238 // Get the action pattern
239 switch (phase) {
240 case PREPROCESSING: pat = &cd->PreProcessor; break;
241 case TRANSLATION: pat = &cd->Translator; break;
242 case OPTIMIZATION: pat = &cd->Optimizer; break;
243 case ASSEMBLY: pat = &cd->Assembler; break;
244 case LINKING: pat = &cd->Linker; break;
245 default:
246 assert(!"Invalid driver phase!");
247 break;
248 }
249 assert(pat != 0 && "Invalid command pattern");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000250
Reid Spenceraf77d742004-10-28 04:05:06 +0000251 // Copy over some pattern things that don't need to change
Reid Spenceraf77d742004-10-28 04:05:06 +0000252 action->flags = pat->flags;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000253
Reid Spencer3b4c5d72006-08-16 20:31:44 +0000254 // See if program starts with wildcard...
255 std::string programName=pat->program.toString();
256 if (programName[0] == '%' && programName.length() >2) {
257 switch(programName[1]){
258 case 'b':
259 if (programName.substr(0,8) == "%bindir%") {
260 std::string tmp(LLVM_BINDIR);
261 tmp.append(programName.substr(8));
262 pat->program.set(tmp);
263 }
264 break;
265 case 'l':
266 if (programName.substr(0,12) == "%llvmgccdir%"){
267 std::string tmp(LLVMGCCDIR);
268 tmp.append(programName.substr(12));
269 pat->program.set(tmp);
270 }else if (programName.substr(0,13) == "%llvmgccarch%"){
271 std::string tmp(LLVMGCCARCH);
272 tmp.append(programName.substr(13));
273 pat->program.set(tmp);
274 }else if (programName.substr(0,9) == "%llvmgcc%"){
275 std::string tmp(LLVMGCC);
276 tmp.append(programName.substr(9));
277 pat->program.set(tmp);
278 }else if (programName.substr(0,9) == "%llvmgxx%"){
279 std::string tmp(LLVMGXX);
280 tmp.append(programName.substr(9));
281 pat->program.set(tmp);
282 }else if (programName.substr(0,9) == "%llvmcc1%"){
283 std::string tmp(LLVMCC1);
284 tmp.append(programName.substr(9));
285 pat->program.set(tmp);
286 }else if (programName.substr(0,13) == "%llvmcc1plus%"){
287 std::string tmp(LLVMCC1PLUS);
288 tmp.append(programName.substr(13));
289 pat->program.set(tmp);
290 }else if (programName.substr(0,8) == "%libdir%") {
291 std::string tmp(LLVM_LIBDIR);
292 tmp.append(programName.substr(8));
293 pat->program.set(tmp);
294 }
295 break;
296 }
297 }
298 action->program = pat->program;
299
Reid Spenceraf77d742004-10-28 04:05:06 +0000300 // Do the substitutions from the pattern to the actual
301 StringVector::iterator PI = pat->args.begin();
302 StringVector::iterator PE = pat->args.end();
303 while (PI != PE) {
304 if ((*PI)[0] == '%' && PI->length() >2) {
305 bool found = true;
306 switch ((*PI)[1]) {
307 case 'a':
308 if (*PI == "%args%") {
309 if (AdditionalArgs.size() > unsigned(phase))
310 if (!AdditionalArgs[phase].empty()) {
311 // Get specific options for each kind of action type
312 StringVector& addargs = AdditionalArgs[phase];
313 // Add specific options for each kind of action type
Misha Brukman3da94ae2005-04-22 00:00:37 +0000314 action->args.insert(action->args.end(), addargs.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000315 addargs.end());
316 }
317 } else
318 found = false;
319 break;
Reid Spencercc97cfc2005-05-19 00:52:28 +0000320 case 'b':
321 if (*PI == "%bindir%") {
322 std::string tmp(*PI);
323 tmp.replace(0,8,LLVM_BINDIR);
324 action->args.push_back(tmp);
325 } else
326 found = false;
327 break;
Reid Spenceraf77d742004-10-28 04:05:06 +0000328 case 'd':
329 if (*PI == "%defs%") {
330 StringVector::iterator I = Defines.begin();
331 StringVector::iterator E = Defines.end();
332 while (I != E) {
333 action->args.push_back( std::string("-D") + *I);
334 ++I;
335 }
336 } else
337 found = false;
338 break;
339 case 'f':
340 if (*PI == "%fOpts%") {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000341 if (!fOptions.empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000342 action->args.insert(action->args.end(), fOptions.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000343 fOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000344 } else
345 found = false;
346 break;
347 case 'i':
348 if (*PI == "%in%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000349 action->args.push_back(input.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000350 } else if (*PI == "%incls%") {
351 PathVector::iterator I = IncludePaths.begin();
352 PathVector::iterator E = IncludePaths.end();
353 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000354 action->args.push_back( std::string("-I") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000355 ++I;
356 }
357 } else
358 found = false;
359 break;
360 case 'l':
Reid Spencercc97cfc2005-05-19 00:52:28 +0000361 if ((*PI)[1] == 'l') {
362 std::string tmp(*PI);
363 if (*PI == "%llvmgccdir%")
364 tmp.replace(0,12,LLVMGCCDIR);
365 else if (*PI == "%llvmgccarch%")
366 tmp.replace(0,13,LLVMGCCARCH);
367 else if (*PI == "%llvmgcc%")
368 tmp.replace(0,9,LLVMGCC);
369 else if (*PI == "%llvmgxx%")
370 tmp.replace(0,9,LLVMGXX);
371 else if (*PI == "%llvmcc1%")
372 tmp.replace(0,9,LLVMCC1);
Jeff Cohen00b168892005-07-27 06:12:32 +0000373 else if (*PI == "%llvmcc1plus%")
Reid Spencercc97cfc2005-05-19 00:52:28 +0000374 tmp.replace(0,9,LLVMCC1);
375 else
376 found = false;
377 if (found)
378 action->args.push_back(tmp);
379 } else if (*PI == "%libs%") {
Reid Spenceraf77d742004-10-28 04:05:06 +0000380 PathVector::iterator I = LibraryPaths.begin();
381 PathVector::iterator E = LibraryPaths.end();
382 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000383 action->args.push_back( std::string("-L") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000384 ++I;
385 }
Reid Spencercc97cfc2005-05-19 00:52:28 +0000386 } else if (*PI == "%libdir%") {
387 std::string tmp(*PI);
388 tmp.replace(0,8,LLVM_LIBDIR);
389 action->args.push_back(tmp);
Reid Spenceraf77d742004-10-28 04:05:06 +0000390 } else
391 found = false;
392 break;
393 case 'o':
394 if (*PI == "%out%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000395 action->args.push_back(output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000396 } else if (*PI == "%opt%") {
397 if (!isSet(EMIT_RAW_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000398 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
Reid Spenceraf77d742004-10-28 04:05:06 +0000399 !cd->opts[optLevel].empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000400 action->args.insert(action->args.end(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000401 cd->opts[optLevel].begin(),
402 cd->opts[optLevel].end());
403 else
Misha Brukman3da94ae2005-04-22 00:00:37 +0000404 throw std::string("Optimization options for level ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000405 utostr(unsigned(optLevel)) + " were not specified";
406 }
407 } else
408 found = false;
409 break;
410 case 's':
411 if (*PI == "%stats%") {
412 if (isSet(SHOW_STATS_FLAG))
413 action->args.push_back("-stats");
414 } else
415 found = false;
416 break;
417 case 't':
418 if (*PI == "%target%") {
419 action->args.push_back(std::string("-march=") + machine);
420 } else if (*PI == "%time%") {
421 if (isSet(TIME_PASSES_FLAG))
422 action->args.push_back("-time-passes");
423 } else
424 found = false;
425 break;
426 case 'v':
427 if (*PI == "%verbose%") {
428 if (isSet(VERBOSE_FLAG))
429 action->args.push_back("-v");
430 } else
431 found = false;
432 break;
433 case 'M':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000434 if (*PI == "%Mopts%") {
435 if (!MOptions.empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000436 action->args.insert(action->args.end(), MOptions.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000437 MOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000438 } else
439 found = false;
440 break;
441 case 'W':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000442 if (*PI == "%Wopts%") {
443 for (StringVector::iterator I = WOptions.begin(),
444 E = WOptions.end(); I != E ; ++I ) {
Misha Brukman0b861482005-05-03 20:30:34 +0000445 action->args.push_back(std::string("-W") + *I);
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000446 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000447 } else
448 found = false;
449 break;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000450 default:
Reid Spenceraf77d742004-10-28 04:05:06 +0000451 found = false;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000452 break;
453 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000454 if (!found) {
455 // Did it even look like a substitution?
Misha Brukman3da94ae2005-04-22 00:00:37 +0000456 if (PI->length()>1 && (*PI)[0] == '%' &&
Reid Spenceraf77d742004-10-28 04:05:06 +0000457 (*PI)[PI->length()-1] == '%') {
458 throw std::string("Invalid substitution token: '") + *PI +
Reid Spencer1fce0912004-12-11 00:14:15 +0000459 "' for command '" + pat->program.toString() + "'";
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000460 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000461 // It's not a legal substitution, just pass it through
Reid Spencer52c2dc12004-08-29 19:26:56 +0000462 action->args.push_back(*PI);
463 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000464 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000465 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000466 // Its not a substitution, just put it in the action
467 action->args.push_back(*PI);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000468 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000469 PI++;
470 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000471
Reid Spenceraf77d742004-10-28 04:05:06 +0000472 // Finally, we're done
473 return action;
474 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000475
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000476 int DoAction(Action*action, std::string& ErrMsg) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000477 assert(action != 0 && "Invalid Action!");
478 if (isSet(VERBOSE_FLAG))
479 WriteAction(action);
480 if (!isSet(DRY_RUN_FLAG)) {
481 sys::Path progpath = sys::Program::FindProgramByName(
Reid Spencer1fce0912004-12-11 00:14:15 +0000482 action->program.toString());
Reid Spencer07adb282004-11-05 22:15:36 +0000483 if (progpath.isEmpty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000484 throw std::string("Can't find program '" +
485 action->program.toString()+"'");
Reid Spencerc7f08322005-07-07 18:21:42 +0000486 else if (progpath.canExecute())
Reid Spenceraf77d742004-10-28 04:05:06 +0000487 action->program = progpath;
488 else
Reid Spencer1fce0912004-12-11 00:14:15 +0000489 throw std::string("Program '"+action->program.toString()+
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000490 "' is not executable.");
Reid Spenceraf77d742004-10-28 04:05:06 +0000491
492 // Invoke the program
Misha Brukman3da94ae2005-04-22 00:00:37 +0000493 const char** Args = (const char**)
Reid Spencerc30088f2005-04-11 05:48:04 +0000494 alloca(sizeof(const char*)*(action->args.size()+2));
495 Args[0] = action->program.toString().c_str();
Reid Spencer3b4c5d72006-08-16 20:31:44 +0000496 for (unsigned i = 1; i <= action->args.size(); ++i)
497 Args[i] = action->args[i-1].c_str();
498 Args[action->args.size()+1] = 0; // null terminate list.
Reid Spenceraf77d742004-10-28 04:05:06 +0000499 if (isSet(TIME_ACTIONS_FLAG)) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000500 Timer timer(action->program.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000501 timer.startTimer();
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000502 int resultCode =
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000503 sys::Program::ExecuteAndWait(action->program, Args,0,0,0,0, &ErrMsg);
Reid Spenceraf77d742004-10-28 04:05:06 +0000504 timer.stopTimer();
505 timer.print(timer,std::cerr);
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000506 return resultCode;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000507 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000508 else
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000509 return
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000510 sys::Program::ExecuteAndWait(action->program, Args, 0,0,0,0, &ErrMsg);
Reid Spenceraf77d742004-10-28 04:05:06 +0000511 }
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000512 return 0;
Reid Spenceraf77d742004-10-28 04:05:06 +0000513 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000514
Reid Spenceraf77d742004-10-28 04:05:06 +0000515 /// This method tries various variants of a linkage item's file
516 /// name to see if it can find an appropriate file to link with
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000517 /// in the directories of the LibraryPaths.
Reid Spenceraf77d742004-10-28 04:05:06 +0000518 llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
Reid Spenceraf77d742004-10-28 04:05:06 +0000519 bool native = false) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000520 sys::Path fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000521 fullpath.set(link_item);
Reid Spencerc7f08322005-07-07 18:21:42 +0000522 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000523 return fullpath;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000524 for (PathVector::iterator PI = LibraryPaths.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000525 PE = LibraryPaths.end(); PI != PE; ++PI) {
Reid Spencerdd04df02005-07-07 23:21:43 +0000526 fullpath.set(PI->toString());
527 fullpath.appendComponent(link_item);
Reid Spencerc7f08322005-07-07 18:21:42 +0000528 if (fullpath.canRead())
Reid Spenceraf77d742004-10-28 04:05:06 +0000529 return fullpath;
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000530 if (native) {
531 fullpath.appendSuffix("a");
532 } else {
533 fullpath.appendSuffix("bc");
Reid Spencerc7f08322005-07-07 18:21:42 +0000534 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000535 return fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000536 fullpath.eraseSuffix();
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000537 fullpath.appendSuffix("o");
Reid Spencerc7f08322005-07-07 18:21:42 +0000538 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000539 return fullpath;
540 fullpath = *PI;
Reid Spencerdd04df02005-07-07 23:21:43 +0000541 fullpath.appendComponent(std::string("lib") + link_item);
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000542 fullpath.appendSuffix("a");
Reid Spencerc7f08322005-07-07 18:21:42 +0000543 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000544 return fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000545 fullpath.eraseSuffix();
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000546 fullpath.appendSuffix("so");
Reid Spencerc7f08322005-07-07 18:21:42 +0000547 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000548 return fullpath;
549 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000550 }
551
552 // Didn't find one.
553 fullpath.clear();
554 return fullpath;
555 }
556
557 /// This method processes a linkage item. The item could be a
558 /// Bytecode file needing translation to native code and that is
559 /// dependent on other bytecode libraries, or a native code
560 /// library that should just be linked into the program.
561 bool ProcessLinkageItem(const llvm::sys::Path& link_item,
562 SetVector<sys::Path>& set,
563 std::string& err) {
564 // First, see if the unadorned file name is not readable. If so,
565 // we must track down the file in the lib search path.
566 sys::Path fullpath;
Reid Spencerc7f08322005-07-07 18:21:42 +0000567 if (!link_item.canRead()) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000568 // look for the library using the -L arguments specified
Reid Spenceraf77d742004-10-28 04:05:06 +0000569 // on the command line.
Reid Spencer1fce0912004-12-11 00:14:15 +0000570 fullpath = GetPathForLinkageItem(link_item.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000571
Reid Spenceraf77d742004-10-28 04:05:06 +0000572 // If we didn't find the file in any of the library search paths
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000573 // we have to bail. No where else to look.
Reid Spencer07adb282004-11-05 22:15:36 +0000574 if (fullpath.isEmpty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000575 err =
Reid Spencer1fce0912004-12-11 00:14:15 +0000576 std::string("Can't find linkage item '") + link_item.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000577 return false;
578 }
579 } else {
580 fullpath = link_item;
581 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000582
Reid Spenceraf77d742004-10-28 04:05:06 +0000583 // If we got here fullpath is the path to the file, and its readable.
584 set.insert(fullpath);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000585
Reid Spenceraf77d742004-10-28 04:05:06 +0000586 // If its an LLVM bytecode file ...
Reid Spencer07adb282004-11-05 22:15:36 +0000587 if (fullpath.isBytecodeFile()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000588 // Process the dependent libraries recursively
589 Module::LibraryListType modlibs;
Chris Lattnerf2e292c2007-02-07 21:41:02 +0000590 if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs,
591 Compressor::decompressToNewBuffer,
592 &err)) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000593 // Traverse the dependent libraries list
594 Module::lib_iterator LI = modlibs.begin();
595 Module::lib_iterator LE = modlibs.end();
596 while ( LI != LE ) {
597 if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
598 if (err.empty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000599 err = std::string("Library '") + *LI +
Reid Spenceraf77d742004-10-28 04:05:06 +0000600 "' is not valid for linking but is required by file '" +
Reid Spencer1fce0912004-12-11 00:14:15 +0000601 fullpath.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000602 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000603 err += " which is required by file '" + fullpath.toString() + "'";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000604 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000605 return false;
606 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000607 ++LI;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000608 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000609 } else if (err.empty()) {
610 err = std::string(
Misha Brukman3da94ae2005-04-22 00:00:37 +0000611 "The dependent libraries could not be extracted from '") +
Reid Spencer1fce0912004-12-11 00:14:15 +0000612 fullpath.toString();
Reid Spenceraf77d742004-10-28 04:05:06 +0000613 return false;
Reid Spencer0b5a5042006-08-25 17:43:11 +0000614 } else
615 return false;
Reid Spenceraf77d742004-10-28 04:05:06 +0000616 }
617 return true;
618 }
619
620/// @}
621/// @name Methods
622/// @{
623public:
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000624 virtual int execute(const InputList& InpList, const sys::Path& Output, std::string& ErrMsg ) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000625 try {
626 // Echo the configuration of options if we're running verbose
627 if (isSet(DEBUG_FLAG)) {
628 std::cerr << "Compiler Driver Options:\n";
629 std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
630 std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
631 std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
632 std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
633 std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
634 std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
635 std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
636 std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
637 std::cerr << "OutputMachine = " << machine << "\n";
638 InputList::const_iterator I = InpList.begin();
639 while ( I != InpList.end() ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000640 std::cerr << "Input: " << I->first << "(" << I->second
Reid Spenceraf77d742004-10-28 04:05:06 +0000641 << ")\n";
642 ++I;
643 }
Reid Spencer12786d52004-12-13 08:53:36 +0000644 std::cerr << "Output: " << Output << "\n";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000645 }
646
Reid Spenceraf77d742004-10-28 04:05:06 +0000647 // If there's no input, we're done.
648 if (InpList.empty())
649 throw std::string("Nothing to compile.");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000650
Reid Spenceraf77d742004-10-28 04:05:06 +0000651 // If they are asking for linking and didn't provide an output
652 // file then its an error (no way for us to "make up" a meaningful
653 // file name based on the various linker input files).
Reid Spencer07adb282004-11-05 22:15:36 +0000654 if (finalPhase == LINKING && Output.isEmpty())
Reid Spenceraf77d742004-10-28 04:05:06 +0000655 throw std::string(
656 "An output file name must be specified for linker output");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000657
Reid Spenceraf77d742004-10-28 04:05:06 +0000658 // If they are not asking for linking, provided an output file and
659 // there is more than one input file, its an error
Reid Spencer07adb282004-11-05 22:15:36 +0000660 if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1)
Reid Spenceraf77d742004-10-28 04:05:06 +0000661 throw std::string("An output file name cannot be specified ") +
662 "with more than one input file name when not linking";
663
664 // This vector holds all the resulting actions of the following loop.
665 std::vector<Action*> actions;
666
667 /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
668 // for each input item
669 SetVector<sys::Path> LinkageItems;
Reid Spencerf6358c72004-12-19 18:00:56 +0000670 StringVector LibFiles;
Reid Spenceraf77d742004-10-28 04:05:06 +0000671 InputList::const_iterator I = InpList.begin();
Reid Spencer679a7232004-11-20 20:39:33 +0000672 for (InputList::const_iterator I = InpList.begin(), E = InpList.end();
673 I != E; ++I ) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000674 // Get the suffix of the file name
675 const std::string& ftype = I->second;
676
Misha Brukman3da94ae2005-04-22 00:00:37 +0000677 // If its a library, bytecode file, or object file, save
678 // it for linking below and short circuit the
Reid Spenceraf77d742004-10-28 04:05:06 +0000679 // pre-processing/translation/assembly phases
680 if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000681 // We shouldn't get any of these types of files unless we're
Reid Spenceraf77d742004-10-28 04:05:06 +0000682 // later going to link. Enforce this limit now.
683 if (finalPhase != LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000684 throw std::string(
Reid Spenceraf77d742004-10-28 04:05:06 +0000685 "Pre-compiled objects found but linking not requested");
686 }
687 if (ftype.empty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000688 LibFiles.push_back(I->first.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000689 else
690 LinkageItems.insert(I->first);
Reid Spencer679a7232004-11-20 20:39:33 +0000691 continue; // short circuit remainder of loop
Reid Spenceraf77d742004-10-28 04:05:06 +0000692 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000693
Reid Spenceraf77d742004-10-28 04:05:06 +0000694 // At this point, we know its something we need to translate
695 // and/or optimize. See if we can get the configuration data
696 // for this kind of file.
697 ConfigData* cd = cdp->ProvideConfigData(I->second);
698 if (cd == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000699 throw std::string("Files of type '") + I->second +
700 "' are not recognized.";
Reid Spenceraf77d742004-10-28 04:05:06 +0000701 if (isSet(DEBUG_FLAG))
702 DumpConfigData(cd,I->second);
Reid Spencer54fafe42004-09-14 01:58:45 +0000703
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000704 // Add the config data's library paths to the end of the list
705 for (StringVector::iterator LPI = cd->libpaths.begin(),
706 LPE = cd->libpaths.end(); LPI != LPE; ++LPI){
707 LibraryPaths.push_back(sys::Path(*LPI));
708 }
709
Reid Spenceraf77d742004-10-28 04:05:06 +0000710 // Initialize the input and output files
711 sys::Path InFile(I->first);
Reid Spencer07adb282004-11-05 22:15:36 +0000712 sys::Path OutFile(I->first.getBasename());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000713
Reid Spenceraf77d742004-10-28 04:05:06 +0000714 // PRE-PROCESSING PHASE
715 Action& action = cd->PreProcessor;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000716
Reid Spenceraf77d742004-10-28 04:05:06 +0000717 // Get the preprocessing action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000718 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000719 if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
720 if (finalPhase == PREPROCESSING) {
Reid Spencer679a7232004-11-20 20:39:33 +0000721 if (Output.isEmpty()) {
722 OutFile.appendSuffix("E");
723 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
724 } else {
725 actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
726 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000727 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000728 sys::Path TempFile(
729 MakeTempFile(I->first.getBasename(),"E",&ErrMsg));
730 if (TempFile.isEmpty())
731 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000732 actions.push_back(GetAction(cd,InFile,TempFile,
733 PREPROCESSING));
734 InFile = TempFile;
735 }
736 }
737 } else if (finalPhase == PREPROCESSING) {
738 throw cd->langName + " does not support pre-processing";
739 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000740 throw std::string("Don't know how to pre-process ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000741 cd->langName + " files";
742 }
743
Misha Brukman3da94ae2005-04-22 00:00:37 +0000744 // Short-circuit remaining actions if all they want is
Reid Spenceraf77d742004-10-28 04:05:06 +0000745 // pre-processing
Reid Spencer679a7232004-11-20 20:39:33 +0000746 if (finalPhase == PREPROCESSING) { continue; };
Reid Spenceraf77d742004-10-28 04:05:06 +0000747
748 /// TRANSLATION PHASE
749 action = cd->Translator;
750
751 // Get the translation action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000752 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000753 if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
754 if (finalPhase == TRANSLATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000755 if (Output.isEmpty()) {
756 OutFile.appendSuffix("o");
757 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
758 } else {
759 actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
760 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000761 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000762 sys::Path TempFile(
763 MakeTempFile(I->first.getBasename(),"trans", &ErrMsg));
764 if (TempFile.isEmpty())
765 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000766 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
767 InFile = TempFile;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000768 }
769
Reid Spenceraf77d742004-10-28 04:05:06 +0000770 // ll -> bc Helper
771 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
772 /// The output of the translator is an LLVM Assembly program
773 /// We need to translate it to bytecode
774 Action* action = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000775 action->program.set("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000776 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000777 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000778 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000779 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000780 actions.push_back(action);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000781 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000782 }
783 } else if (finalPhase == TRANSLATION) {
784 throw cd->langName + " does not support translation";
785 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000786 throw std::string("Don't know how to translate ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000787 cd->langName + " files";
788 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000789
Reid Spenceraf77d742004-10-28 04:05:06 +0000790 // Short-circuit remaining actions if all they want is translation
Reid Spencer679a7232004-11-20 20:39:33 +0000791 if (finalPhase == TRANSLATION) { continue; }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000792
Reid Spenceraf77d742004-10-28 04:05:06 +0000793 /// OPTIMIZATION PHASE
794 action = cd->Optimizer;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000795
Reid Spenceraf77d742004-10-28 04:05:06 +0000796 // Get the optimization action, if needed, or error if appropriate
797 if (!isSet(EMIT_RAW_FLAG)) {
Reid Spencer07adb282004-11-05 22:15:36 +0000798 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000799 if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
800 if (finalPhase == OPTIMIZATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000801 if (Output.isEmpty()) {
802 OutFile.appendSuffix("o");
803 actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
804 } else {
805 actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
806 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000807 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000808 sys::Path TempFile(
809 MakeTempFile(I->first.getBasename(),"opt", &ErrMsg));
810 if (TempFile.isEmpty())
811 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000812 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
813 InFile = TempFile;
814 }
815 // ll -> bc Helper
816 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
817 /// The output of the optimizer is an LLVM Assembly program
818 /// We need to translate it to bytecode with llvm-as
Reid Spencer52c2dc12004-08-29 19:26:56 +0000819 Action* action = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000820 action->program.set("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000821 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000822 action->args.push_back("-f");
823 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000824 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000825 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000826 actions.push_back(action);
827 }
828 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000829 } else if (finalPhase == OPTIMIZATION) {
830 throw cd->langName + " does not support optimization";
831 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000832 throw std::string("Don't know how to optimize ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000833 cd->langName + " files";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000834 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000835 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000836
837 // Short-circuit remaining actions if all they want is optimization
Reid Spencer679a7232004-11-20 20:39:33 +0000838 if (finalPhase == OPTIMIZATION) { continue; }
Reid Spenceraf77d742004-10-28 04:05:06 +0000839
840 /// ASSEMBLY PHASE
841 action = cd->Assembler;
842
843 if (finalPhase == ASSEMBLY) {
Reid Spencer679a7232004-11-20 20:39:33 +0000844
845 // Build either a native compilation action or a disassembly action
846 Action* action = new Action();
Reid Spenceraf77d742004-10-28 04:05:06 +0000847 if (isSet(EMIT_NATIVE_FLAG)) {
848 // Use llc to get the native assembly file
Reid Spencerdd04df02005-07-07 23:21:43 +0000849 action->program.set("llc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000850 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000851 action->args.push_back("-f");
852 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000853 if (Output.isEmpty()) {
854 OutFile.appendSuffix("o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000855 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000856 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000857 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000858 }
859 actions.push_back(action);
Reid Spenceraf77d742004-10-28 04:05:06 +0000860 } else {
861 // Just convert back to llvm assembly with llvm-dis
Reid Spencerdd04df02005-07-07 23:21:43 +0000862 action->program.set("llvm-dis");
Reid Spencer1fce0912004-12-11 00:14:15 +0000863 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000864 action->args.push_back("-f");
865 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000866 if (Output.isEmpty()) {
867 OutFile.appendSuffix("ll");
Reid Spencer1fce0912004-12-11 00:14:15 +0000868 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000869 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000870 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000871 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000872 }
873
Reid Spencer679a7232004-11-20 20:39:33 +0000874 // Put the action on the list
875 actions.push_back(action);
876
Misha Brukman3da94ae2005-04-22 00:00:37 +0000877 // Short circuit the rest of the loop, we don't want to link
Reid Spenceraf77d742004-10-28 04:05:06 +0000878 continue;
879 }
880
881 // Register the result of the actions as a link candidate
882 LinkageItems.insert(InFile);
883
Reid Spenceraf77d742004-10-28 04:05:06 +0000884 } // end while loop over each input file
885
886 /// RUN THE COMPILATION ACTIONS
887 std::vector<Action*>::iterator AI = actions.begin();
888 std::vector<Action*>::iterator AE = actions.end();
889 while (AI != AE) {
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000890 int ActionResult = DoAction(*AI, ErrMsg);
891 if (ActionResult != 0)
892 return ActionResult;
Reid Spenceraf77d742004-10-28 04:05:06 +0000893 AI++;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000894 }
895
Reid Spenceraf77d742004-10-28 04:05:06 +0000896 /// LINKING PHASE
897 if (finalPhase == LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000898
Reid Spenceraf77d742004-10-28 04:05:06 +0000899 // Insert the platform-specific system libraries to the path list
Reid Spencer11db4b82004-12-13 03:01:26 +0000900 std::vector<sys::Path> SysLibs;
901 sys::Path::GetSystemLibraryPaths(SysLibs);
902 LibraryPaths.insert(LibraryPaths.end(), SysLibs.begin(), SysLibs.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000903
904 // Set up the linking action with llvm-ld
905 Action* link = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000906 link->program.set("llvm-ld");
Reid Spenceraf77d742004-10-28 04:05:06 +0000907
908 // Add in the optimization level requested
909 switch (optLevel) {
910 case OPT_FAST_COMPILE:
911 link->args.push_back("-O1");
912 break;
913 case OPT_SIMPLE:
914 link->args.push_back("-O2");
915 break;
916 case OPT_AGGRESSIVE:
917 link->args.push_back("-O3");
918 break;
919 case OPT_LINK_TIME:
920 link->args.push_back("-O4");
921 break;
922 case OPT_AGGRESSIVE_LINK_TIME:
923 link->args.push_back("-O5");
924 break;
925 case OPT_NONE:
926 break;
927 }
928
929 // Add in all the linkage items we generated. This includes the
930 // output from the translation/optimization phases as well as any
931 // -l arguments specified.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000932 for (PathVector::const_iterator I=LinkageItems.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000933 E=LinkageItems.end(); I != E; ++I )
Reid Spencer1fce0912004-12-11 00:14:15 +0000934 link->args.push_back(I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000935
936 // Add in all the libraries we found.
Reid Spencerf6358c72004-12-19 18:00:56 +0000937 for (StringVector::const_iterator I=LibFiles.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000938 E=LibFiles.end(); I != E; ++I )
939 link->args.push_back(std::string("-l")+*I);
940
941 // Add in all the library paths to the command line
942 for (PathVector::const_iterator I=LibraryPaths.begin(),
943 E=LibraryPaths.end(); I != E; ++I)
Reid Spencer1fce0912004-12-11 00:14:15 +0000944 link->args.push_back( std::string("-L") + I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000945
946 // Add in the additional linker arguments requested
947 for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(),
948 E=AdditionalArgs[LINKING].end(); I != E; ++I)
949 link->args.push_back( *I );
950
951 // Add in other optional flags
952 if (isSet(EMIT_NATIVE_FLAG))
953 link->args.push_back("-native");
954 if (isSet(VERBOSE_FLAG))
955 link->args.push_back("-v");
956 if (isSet(TIME_PASSES_FLAG))
957 link->args.push_back("-time-passes");
958 if (isSet(SHOW_STATS_FLAG))
959 link->args.push_back("-stats");
960 if (isSet(STRIP_OUTPUT_FLAG))
961 link->args.push_back("-s");
962 if (isSet(DEBUG_FLAG)) {
963 link->args.push_back("-debug");
964 link->args.push_back("-debug-pass=Details");
965 }
966
967 // Add in mandatory flags
968 link->args.push_back("-o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000969 link->args.push_back(Output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000970
971 // Execute the link
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000972 int ActionResult = DoAction(link, ErrMsg);
973 if (ActionResult != 0)
974 return ActionResult;
Reid Spenceraf77d742004-10-28 04:05:06 +0000975 }
976 } catch (std::string& msg) {
977 cleanup();
978 throw;
979 } catch (...) {
980 cleanup();
981 throw std::string("Unspecified error");
982 }
983 cleanup();
984 return 0;
985 }
986
987/// @}
988/// @name Data
989/// @{
990private:
991 ConfigDataProvider* cdp; ///< Where we get configuration data from
992 Phases finalPhase; ///< The final phase of compilation
993 OptimizationLevels optLevel; ///< The optimization level to apply
994 unsigned Flags; ///< The driver flags
995 std::string machine; ///< Target machine name
996 PathVector LibraryPaths; ///< -L options
997 PathVector IncludePaths; ///< -I options
Reid Spencer07adb282004-11-05 22:15:36 +0000998 PathVector ToolPaths; ///< -B options
Reid Spenceraf77d742004-10-28 04:05:06 +0000999 StringVector Defines; ///< -D options
1000 sys::Path TempDir; ///< Name of the temporary directory.
1001 StringTable AdditionalArgs; ///< The -Txyz options
1002 StringVector fOptions; ///< -f options
1003 StringVector MOptions; ///< -M options
1004 StringVector WOptions; ///< -W options
1005
1006/// @}
1007};
Reid Spencer5c56dc12004-08-13 20:22:43 +00001008}
1009
1010CompilerDriver::~CompilerDriver() {
Reid Spencer52c2dc12004-08-29 19:26:56 +00001011}
1012
Reid Spencercc97cfc2005-05-19 00:52:28 +00001013CompilerDriver::ConfigDataProvider::~ConfigDataProvider() {}
1014
Reid Spencer52c2dc12004-08-29 19:26:56 +00001015CompilerDriver*
1016CompilerDriver::Get(ConfigDataProvider& CDP) {
1017 return new CompilerDriverImpl(CDP);
Reid Spencerbae68252004-08-19 04:49:47 +00001018}
1019
1020CompilerDriver::ConfigData::ConfigData()
1021 : langName()
1022 , PreProcessor()
1023 , Translator()
1024 , Optimizer()
1025 , Assembler()
1026 , Linker()
1027{
1028 StringVector emptyVec;
1029 for (unsigned i = 0; i < NUM_PHASES; ++i)
1030 opts.push_back(emptyVec);
Reid Spencer5c56dc12004-08-13 20:22:43 +00001031}