blob: 055a4f4a32337906e505e509eb77a544c336ebae [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
Reid Spenceraf77d742004-10-28 04:05:06 +000065class CompilerDriverImpl : public CompilerDriver {
66/// @name Constructors
67/// @{
68public:
69 CompilerDriverImpl(ConfigDataProvider& confDatProv )
70 : cdp(&confDatProv)
71 , finalPhase(LINKING)
Misha Brukman3da94ae2005-04-22 00:00:37 +000072 , optLevel(OPT_FAST_COMPILE)
Reid Spenceraf77d742004-10-28 04:05:06 +000073 , Flags(0)
74 , machine()
75 , LibraryPaths()
76 , TempDir()
77 , AdditionalArgs()
78 {
Reid Spenceraf77d742004-10-28 04:05:06 +000079 AdditionalArgs.reserve(NUM_PHASES);
80 StringVector emptyVec;
81 for (unsigned i = 0; i < NUM_PHASES; ++i)
82 AdditionalArgs.push_back(emptyVec);
83 }
84
85 virtual ~CompilerDriverImpl() {
86 cleanup();
87 cdp = 0;
88 LibraryPaths.clear();
89 IncludePaths.clear();
90 Defines.clear();
91 TempDir.clear();
92 AdditionalArgs.clear();
93 fOptions.clear();
94 MOptions.clear();
95 WOptions.clear();
96 }
97
98/// @}
99/// @name Methods
100/// @{
101public:
Misha Brukman0b861482005-05-03 20:30:34 +0000102 virtual void setFinalPhase(Phases phase) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000103 finalPhase = phase;
Reid Spenceraf77d742004-10-28 04:05:06 +0000104 }
105
Misha Brukman0b861482005-05-03 20:30:34 +0000106 virtual void setOptimization(OptimizationLevels level) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000107 optLevel = level;
Reid Spenceraf77d742004-10-28 04:05:06 +0000108 }
109
Misha Brukman0b861482005-05-03 20:30:34 +0000110 virtual void setDriverFlags(unsigned flags) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000111 Flags = flags & DRIVER_FLAGS_MASK;
Reid Spenceraf77d742004-10-28 04:05:06 +0000112 }
113
Misha Brukman0b861482005-05-03 20:30:34 +0000114 virtual void setOutputMachine(const std::string& machineName) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000115 machine = machineName;
116 }
117
118 virtual void setPhaseArgs(Phases phase, const StringVector& opts) {
119 assert(phase <= LINKING && phase >= PREPROCESSING);
120 AdditionalArgs[phase] = opts;
121 }
122
123 virtual void setIncludePaths(const StringVector& paths) {
124 StringVector::const_iterator I = paths.begin();
125 StringVector::const_iterator E = paths.end();
126 while (I != E) {
127 sys::Path tmp;
Reid Spencerdd04df02005-07-07 23:21:43 +0000128 tmp.set(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000129 IncludePaths.push_back(tmp);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000130 ++I;
131 }
Reid Spencer68fb37a2004-08-14 09:37:15 +0000132 }
133
Reid Spenceraf77d742004-10-28 04:05:06 +0000134 virtual void setSymbolDefines(const StringVector& defs) {
135 Defines = defs;
136 }
137
138 virtual void setLibraryPaths(const StringVector& paths) {
139 StringVector::const_iterator I = paths.begin();
140 StringVector::const_iterator E = paths.end();
141 while (I != E) {
142 sys::Path tmp;
Reid Spencerdd04df02005-07-07 23:21:43 +0000143 tmp.set(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000144 LibraryPaths.push_back(tmp);
Reid Spencerbf437722004-08-15 08:19:46 +0000145 ++I;
146 }
Reid Spencerbf437722004-08-15 08:19:46 +0000147 }
148
Misha Brukman0b861482005-05-03 20:30:34 +0000149 virtual void addLibraryPath(const sys::Path& libPath) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000150 LibraryPaths.push_back(libPath);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000151 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000152
Misha Brukman0b861482005-05-03 20:30:34 +0000153 virtual void addToolPath(const sys::Path& toolPath) {
Reid Spencer07adb282004-11-05 22:15:36 +0000154 ToolPaths.push_back(toolPath);
155 }
156
Reid Spenceraf77d742004-10-28 04:05:06 +0000157 virtual void setfPassThrough(const StringVector& fOpts) {
158 fOptions = fOpts;
159 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000160
Reid Spenceraf77d742004-10-28 04:05:06 +0000161 /// @brief Set the list of -M options to be passed through
162 virtual void setMPassThrough(const StringVector& MOpts) {
163 MOptions = MOpts;
164 }
Reid Spencerbae68252004-08-19 04:49:47 +0000165
Reid Spenceraf77d742004-10-28 04:05:06 +0000166 /// @brief Set the list of -W options to be passed through
167 virtual void setWPassThrough(const StringVector& WOpts) {
168 WOptions = WOpts;
169 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000170
Reid Spenceraf77d742004-10-28 04:05:06 +0000171/// @}
172/// @name Functions
173/// @{
174private:
175 bool isSet(DriverFlags flag) {
176 return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
177 }
Reid Spencerbae68252004-08-19 04:49:47 +0000178
Reid Spenceraf77d742004-10-28 04:05:06 +0000179 void cleanup() {
180 if (!isSet(KEEP_TEMPS_FLAG)) {
Chris Lattner33b0e9c2006-08-01 18:12:29 +0000181 sys::FileStatus Status;
182 if (!TempDir.getFileStatus(Status) && Status.isDir)
Reid Spencera229c5c2005-07-08 03:08:58 +0000183 TempDir.eraseFromDisk(/*remove_contents=*/true);
Reid Spenceraf77d742004-10-28 04:05:06 +0000184 } else {
Reid Spencer12786d52004-12-13 08:53:36 +0000185 std::cout << "Temporary files are in " << TempDir << "\n";
Reid Spenceraf77d742004-10-28 04:05:06 +0000186 }
187 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000188
Misha Brukman3da94ae2005-04-22 00:00:37 +0000189 sys::Path MakeTempFile(const std::string& basename,
Reid Spencer48744762006-08-22 19:01:30 +0000190 const std::string& suffix,
191 std::string* ErrMsg) {
192 if (TempDir.isEmpty()) {
193 TempDir = sys::Path::GetTemporaryDirectory(ErrMsg);
194 if (TempDir.isEmpty())
195 return sys::Path();
196 sys::RemoveDirectoryOnSignal(TempDir);
197 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000198 sys::Path result(TempDir);
Reid Spencer48744762006-08-22 19:01:30 +0000199 if (!result.appendComponent(basename)) {
200 if (ErrMsg)
201 *ErrMsg = basename + ": can't use this file name";
202 return sys::Path();
203 }
204 if (!result.appendSuffix(suffix)) {
205 if (ErrMsg)
206 *ErrMsg = suffix + ": can't use this file suffix";
207 return sys::Path();
208 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000209 return result;
210 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000211
Misha Brukman3da94ae2005-04-22 00:00:37 +0000212 Action* GetAction(ConfigData* cd,
213 const sys::Path& input,
Reid Spenceraf77d742004-10-28 04:05:06 +0000214 const sys::Path& output,
215 Phases phase)
216 {
217 Action* pat = 0; ///< The pattern/template for the action
218 Action* action = new Action; ///< The actual action to execute
Reid Spencer52c2dc12004-08-29 19:26:56 +0000219
Reid Spenceraf77d742004-10-28 04:05:06 +0000220 // Get the action pattern
221 switch (phase) {
222 case PREPROCESSING: pat = &cd->PreProcessor; break;
223 case TRANSLATION: pat = &cd->Translator; break;
224 case OPTIMIZATION: pat = &cd->Optimizer; break;
225 case ASSEMBLY: pat = &cd->Assembler; break;
226 case LINKING: pat = &cd->Linker; break;
227 default:
228 assert(!"Invalid driver phase!");
229 break;
230 }
231 assert(pat != 0 && "Invalid command pattern");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000232
Reid Spenceraf77d742004-10-28 04:05:06 +0000233 // Copy over some pattern things that don't need to change
Reid Spenceraf77d742004-10-28 04:05:06 +0000234 action->flags = pat->flags;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000235
Reid Spencer3b4c5d72006-08-16 20:31:44 +0000236 // See if program starts with wildcard...
237 std::string programName=pat->program.toString();
238 if (programName[0] == '%' && programName.length() >2) {
239 switch(programName[1]){
240 case 'b':
241 if (programName.substr(0,8) == "%bindir%") {
242 std::string tmp(LLVM_BINDIR);
243 tmp.append(programName.substr(8));
244 pat->program.set(tmp);
245 }
246 break;
247 case 'l':
248 if (programName.substr(0,12) == "%llvmgccdir%"){
249 std::string tmp(LLVMGCCDIR);
250 tmp.append(programName.substr(12));
251 pat->program.set(tmp);
252 }else if (programName.substr(0,13) == "%llvmgccarch%"){
253 std::string tmp(LLVMGCCARCH);
254 tmp.append(programName.substr(13));
255 pat->program.set(tmp);
256 }else if (programName.substr(0,9) == "%llvmgcc%"){
257 std::string tmp(LLVMGCC);
258 tmp.append(programName.substr(9));
259 pat->program.set(tmp);
260 }else if (programName.substr(0,9) == "%llvmgxx%"){
261 std::string tmp(LLVMGXX);
262 tmp.append(programName.substr(9));
263 pat->program.set(tmp);
264 }else if (programName.substr(0,9) == "%llvmcc1%"){
265 std::string tmp(LLVMCC1);
266 tmp.append(programName.substr(9));
267 pat->program.set(tmp);
268 }else if (programName.substr(0,13) == "%llvmcc1plus%"){
269 std::string tmp(LLVMCC1PLUS);
270 tmp.append(programName.substr(13));
271 pat->program.set(tmp);
272 }else if (programName.substr(0,8) == "%libdir%") {
273 std::string tmp(LLVM_LIBDIR);
274 tmp.append(programName.substr(8));
275 pat->program.set(tmp);
276 }
277 break;
278 }
279 }
280 action->program = pat->program;
281
Reid Spenceraf77d742004-10-28 04:05:06 +0000282 // Do the substitutions from the pattern to the actual
283 StringVector::iterator PI = pat->args.begin();
284 StringVector::iterator PE = pat->args.end();
285 while (PI != PE) {
286 if ((*PI)[0] == '%' && PI->length() >2) {
287 bool found = true;
288 switch ((*PI)[1]) {
289 case 'a':
290 if (*PI == "%args%") {
291 if (AdditionalArgs.size() > unsigned(phase))
292 if (!AdditionalArgs[phase].empty()) {
293 // Get specific options for each kind of action type
294 StringVector& addargs = AdditionalArgs[phase];
295 // Add specific options for each kind of action type
Misha Brukman3da94ae2005-04-22 00:00:37 +0000296 action->args.insert(action->args.end(), addargs.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000297 addargs.end());
298 }
299 } else
300 found = false;
301 break;
Reid Spencercc97cfc2005-05-19 00:52:28 +0000302 case 'b':
303 if (*PI == "%bindir%") {
304 std::string tmp(*PI);
305 tmp.replace(0,8,LLVM_BINDIR);
306 action->args.push_back(tmp);
307 } else
308 found = false;
309 break;
Reid Spenceraf77d742004-10-28 04:05:06 +0000310 case 'd':
311 if (*PI == "%defs%") {
312 StringVector::iterator I = Defines.begin();
313 StringVector::iterator E = Defines.end();
314 while (I != E) {
315 action->args.push_back( std::string("-D") + *I);
316 ++I;
317 }
318 } else
319 found = false;
320 break;
321 case 'f':
322 if (*PI == "%fOpts%") {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000323 if (!fOptions.empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000324 action->args.insert(action->args.end(), fOptions.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000325 fOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000326 } else
327 found = false;
328 break;
329 case 'i':
330 if (*PI == "%in%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000331 action->args.push_back(input.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000332 } else if (*PI == "%incls%") {
333 PathVector::iterator I = IncludePaths.begin();
334 PathVector::iterator E = IncludePaths.end();
335 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000336 action->args.push_back( std::string("-I") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000337 ++I;
338 }
339 } else
340 found = false;
341 break;
342 case 'l':
Reid Spencercc97cfc2005-05-19 00:52:28 +0000343 if ((*PI)[1] == 'l') {
344 std::string tmp(*PI);
345 if (*PI == "%llvmgccdir%")
346 tmp.replace(0,12,LLVMGCCDIR);
347 else if (*PI == "%llvmgccarch%")
348 tmp.replace(0,13,LLVMGCCARCH);
349 else if (*PI == "%llvmgcc%")
350 tmp.replace(0,9,LLVMGCC);
351 else if (*PI == "%llvmgxx%")
352 tmp.replace(0,9,LLVMGXX);
353 else if (*PI == "%llvmcc1%")
354 tmp.replace(0,9,LLVMCC1);
Jeff Cohen00b168892005-07-27 06:12:32 +0000355 else if (*PI == "%llvmcc1plus%")
Reid Spencercc97cfc2005-05-19 00:52:28 +0000356 tmp.replace(0,9,LLVMCC1);
357 else
358 found = false;
359 if (found)
360 action->args.push_back(tmp);
361 } else if (*PI == "%libs%") {
Reid Spenceraf77d742004-10-28 04:05:06 +0000362 PathVector::iterator I = LibraryPaths.begin();
363 PathVector::iterator E = LibraryPaths.end();
364 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000365 action->args.push_back( std::string("-L") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000366 ++I;
367 }
Reid Spencercc97cfc2005-05-19 00:52:28 +0000368 } else if (*PI == "%libdir%") {
369 std::string tmp(*PI);
370 tmp.replace(0,8,LLVM_LIBDIR);
371 action->args.push_back(tmp);
Reid Spenceraf77d742004-10-28 04:05:06 +0000372 } else
373 found = false;
374 break;
375 case 'o':
376 if (*PI == "%out%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000377 action->args.push_back(output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000378 } else if (*PI == "%opt%") {
379 if (!isSet(EMIT_RAW_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000380 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
Reid Spenceraf77d742004-10-28 04:05:06 +0000381 !cd->opts[optLevel].empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000382 action->args.insert(action->args.end(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000383 cd->opts[optLevel].begin(),
384 cd->opts[optLevel].end());
385 else
Misha Brukman3da94ae2005-04-22 00:00:37 +0000386 throw std::string("Optimization options for level ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000387 utostr(unsigned(optLevel)) + " were not specified";
388 }
389 } else
390 found = false;
391 break;
392 case 's':
393 if (*PI == "%stats%") {
394 if (isSet(SHOW_STATS_FLAG))
395 action->args.push_back("-stats");
396 } else
397 found = false;
398 break;
399 case 't':
400 if (*PI == "%target%") {
401 action->args.push_back(std::string("-march=") + machine);
402 } else if (*PI == "%time%") {
403 if (isSet(TIME_PASSES_FLAG))
404 action->args.push_back("-time-passes");
405 } else
406 found = false;
407 break;
408 case 'v':
409 if (*PI == "%verbose%") {
410 if (isSet(VERBOSE_FLAG))
411 action->args.push_back("-v");
412 } else
413 found = false;
414 break;
415 case 'M':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000416 if (*PI == "%Mopts%") {
417 if (!MOptions.empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000418 action->args.insert(action->args.end(), MOptions.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000419 MOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000420 } else
421 found = false;
422 break;
423 case 'W':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000424 if (*PI == "%Wopts%") {
425 for (StringVector::iterator I = WOptions.begin(),
426 E = WOptions.end(); I != E ; ++I ) {
Misha Brukman0b861482005-05-03 20:30:34 +0000427 action->args.push_back(std::string("-W") + *I);
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000428 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000429 } else
430 found = false;
431 break;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000432 default:
Reid Spenceraf77d742004-10-28 04:05:06 +0000433 found = false;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000434 break;
435 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000436 if (!found) {
437 // Did it even look like a substitution?
Misha Brukman3da94ae2005-04-22 00:00:37 +0000438 if (PI->length()>1 && (*PI)[0] == '%' &&
Reid Spenceraf77d742004-10-28 04:05:06 +0000439 (*PI)[PI->length()-1] == '%') {
440 throw std::string("Invalid substitution token: '") + *PI +
Reid Spencer1fce0912004-12-11 00:14:15 +0000441 "' for command '" + pat->program.toString() + "'";
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000442 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000443 // It's not a legal substitution, just pass it through
Reid Spencer52c2dc12004-08-29 19:26:56 +0000444 action->args.push_back(*PI);
445 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000446 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000447 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000448 // Its not a substitution, just put it in the action
449 action->args.push_back(*PI);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000450 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000451 PI++;
452 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000453
Reid Spenceraf77d742004-10-28 04:05:06 +0000454 // Finally, we're done
455 return action;
456 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000457
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000458 int DoAction(Action*action, std::string& ErrMsg) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000459 assert(action != 0 && "Invalid Action!");
460 if (isSet(VERBOSE_FLAG))
461 WriteAction(action);
462 if (!isSet(DRY_RUN_FLAG)) {
463 sys::Path progpath = sys::Program::FindProgramByName(
Reid Spencer1fce0912004-12-11 00:14:15 +0000464 action->program.toString());
Reid Spencer07adb282004-11-05 22:15:36 +0000465 if (progpath.isEmpty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000466 throw std::string("Can't find program '" +
467 action->program.toString()+"'");
Reid Spencerc7f08322005-07-07 18:21:42 +0000468 else if (progpath.canExecute())
Reid Spenceraf77d742004-10-28 04:05:06 +0000469 action->program = progpath;
470 else
Reid Spencer1fce0912004-12-11 00:14:15 +0000471 throw std::string("Program '"+action->program.toString()+
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000472 "' is not executable.");
Reid Spenceraf77d742004-10-28 04:05:06 +0000473
474 // Invoke the program
Misha Brukman3da94ae2005-04-22 00:00:37 +0000475 const char** Args = (const char**)
Reid Spencerc30088f2005-04-11 05:48:04 +0000476 alloca(sizeof(const char*)*(action->args.size()+2));
477 Args[0] = action->program.toString().c_str();
Reid Spencer3b4c5d72006-08-16 20:31:44 +0000478 for (unsigned i = 1; i <= action->args.size(); ++i)
479 Args[i] = action->args[i-1].c_str();
480 Args[action->args.size()+1] = 0; // null terminate list.
Reid Spenceraf77d742004-10-28 04:05:06 +0000481 if (isSet(TIME_ACTIONS_FLAG)) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000482 Timer timer(action->program.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000483 timer.startTimer();
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000484 int resultCode =
485 sys::Program::ExecuteAndWait(action->program, Args,0,0,0,&ErrMsg);
Reid Spenceraf77d742004-10-28 04:05:06 +0000486 timer.stopTimer();
487 timer.print(timer,std::cerr);
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000488 return resultCode;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000489 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000490 else
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000491 return
492 sys::Program::ExecuteAndWait(action->program, Args, 0,0,0, &ErrMsg);
Reid Spenceraf77d742004-10-28 04:05:06 +0000493 }
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000494 return 0;
Reid Spenceraf77d742004-10-28 04:05:06 +0000495 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000496
Reid Spenceraf77d742004-10-28 04:05:06 +0000497 /// This method tries various variants of a linkage item's file
498 /// name to see if it can find an appropriate file to link with
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000499 /// in the directories of the LibraryPaths.
Reid Spenceraf77d742004-10-28 04:05:06 +0000500 llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
Reid Spenceraf77d742004-10-28 04:05:06 +0000501 bool native = false) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000502 sys::Path fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000503 fullpath.set(link_item);
Reid Spencerc7f08322005-07-07 18:21:42 +0000504 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000505 return fullpath;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000506 for (PathVector::iterator PI = LibraryPaths.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000507 PE = LibraryPaths.end(); PI != PE; ++PI) {
Reid Spencerdd04df02005-07-07 23:21:43 +0000508 fullpath.set(PI->toString());
509 fullpath.appendComponent(link_item);
Reid Spencerc7f08322005-07-07 18:21:42 +0000510 if (fullpath.canRead())
Reid Spenceraf77d742004-10-28 04:05:06 +0000511 return fullpath;
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000512 if (native) {
513 fullpath.appendSuffix("a");
514 } else {
515 fullpath.appendSuffix("bc");
Reid Spencerc7f08322005-07-07 18:21:42 +0000516 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000517 return fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000518 fullpath.eraseSuffix();
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000519 fullpath.appendSuffix("o");
Reid Spencerc7f08322005-07-07 18:21:42 +0000520 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000521 return fullpath;
522 fullpath = *PI;
Reid Spencerdd04df02005-07-07 23:21:43 +0000523 fullpath.appendComponent(std::string("lib") + link_item);
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000524 fullpath.appendSuffix("a");
Reid Spencerc7f08322005-07-07 18:21:42 +0000525 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000526 return fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000527 fullpath.eraseSuffix();
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000528 fullpath.appendSuffix("so");
Reid Spencerc7f08322005-07-07 18:21:42 +0000529 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000530 return fullpath;
531 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000532 }
533
534 // Didn't find one.
535 fullpath.clear();
536 return fullpath;
537 }
538
539 /// This method processes a linkage item. The item could be a
540 /// Bytecode file needing translation to native code and that is
541 /// dependent on other bytecode libraries, or a native code
542 /// library that should just be linked into the program.
543 bool ProcessLinkageItem(const llvm::sys::Path& link_item,
544 SetVector<sys::Path>& set,
545 std::string& err) {
546 // First, see if the unadorned file name is not readable. If so,
547 // we must track down the file in the lib search path.
548 sys::Path fullpath;
Reid Spencerc7f08322005-07-07 18:21:42 +0000549 if (!link_item.canRead()) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000550 // look for the library using the -L arguments specified
Reid Spenceraf77d742004-10-28 04:05:06 +0000551 // on the command line.
Reid Spencer1fce0912004-12-11 00:14:15 +0000552 fullpath = GetPathForLinkageItem(link_item.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000553
Reid Spenceraf77d742004-10-28 04:05:06 +0000554 // If we didn't find the file in any of the library search paths
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000555 // we have to bail. No where else to look.
Reid Spencer07adb282004-11-05 22:15:36 +0000556 if (fullpath.isEmpty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000557 err =
Reid Spencer1fce0912004-12-11 00:14:15 +0000558 std::string("Can't find linkage item '") + link_item.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000559 return false;
560 }
561 } else {
562 fullpath = link_item;
563 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000564
Reid Spenceraf77d742004-10-28 04:05:06 +0000565 // If we got here fullpath is the path to the file, and its readable.
566 set.insert(fullpath);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000567
Reid Spenceraf77d742004-10-28 04:05:06 +0000568 // If its an LLVM bytecode file ...
Reid Spencer07adb282004-11-05 22:15:36 +0000569 if (fullpath.isBytecodeFile()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000570 // Process the dependent libraries recursively
571 Module::LibraryListType modlibs;
Chris Lattnerf2e292c2007-02-07 21:41:02 +0000572 if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs,
573 Compressor::decompressToNewBuffer,
574 &err)) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000575 // Traverse the dependent libraries list
576 Module::lib_iterator LI = modlibs.begin();
577 Module::lib_iterator LE = modlibs.end();
578 while ( LI != LE ) {
579 if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
580 if (err.empty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000581 err = std::string("Library '") + *LI +
Reid Spenceraf77d742004-10-28 04:05:06 +0000582 "' is not valid for linking but is required by file '" +
Reid Spencer1fce0912004-12-11 00:14:15 +0000583 fullpath.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000584 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000585 err += " which is required by file '" + fullpath.toString() + "'";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000586 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000587 return false;
588 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000589 ++LI;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000590 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000591 } else if (err.empty()) {
592 err = std::string(
Misha Brukman3da94ae2005-04-22 00:00:37 +0000593 "The dependent libraries could not be extracted from '") +
Reid Spencer1fce0912004-12-11 00:14:15 +0000594 fullpath.toString();
Reid Spenceraf77d742004-10-28 04:05:06 +0000595 return false;
Reid Spencer0b5a5042006-08-25 17:43:11 +0000596 } else
597 return false;
Reid Spenceraf77d742004-10-28 04:05:06 +0000598 }
599 return true;
600 }
601
602/// @}
603/// @name Methods
604/// @{
605public:
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000606 virtual int execute(const InputList& InpList, const sys::Path& Output, std::string& ErrMsg ) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000607 try {
608 // Echo the configuration of options if we're running verbose
609 if (isSet(DEBUG_FLAG)) {
610 std::cerr << "Compiler Driver Options:\n";
611 std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
612 std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
613 std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
614 std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
615 std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
616 std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
617 std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
618 std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
619 std::cerr << "OutputMachine = " << machine << "\n";
620 InputList::const_iterator I = InpList.begin();
621 while ( I != InpList.end() ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000622 std::cerr << "Input: " << I->first << "(" << I->second
Reid Spenceraf77d742004-10-28 04:05:06 +0000623 << ")\n";
624 ++I;
625 }
Reid Spencer12786d52004-12-13 08:53:36 +0000626 std::cerr << "Output: " << Output << "\n";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000627 }
628
Reid Spenceraf77d742004-10-28 04:05:06 +0000629 // If there's no input, we're done.
630 if (InpList.empty())
631 throw std::string("Nothing to compile.");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000632
Reid Spenceraf77d742004-10-28 04:05:06 +0000633 // If they are asking for linking and didn't provide an output
634 // file then its an error (no way for us to "make up" a meaningful
635 // file name based on the various linker input files).
Reid Spencer07adb282004-11-05 22:15:36 +0000636 if (finalPhase == LINKING && Output.isEmpty())
Reid Spenceraf77d742004-10-28 04:05:06 +0000637 throw std::string(
638 "An output file name must be specified for linker output");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000639
Reid Spenceraf77d742004-10-28 04:05:06 +0000640 // If they are not asking for linking, provided an output file and
641 // there is more than one input file, its an error
Reid Spencer07adb282004-11-05 22:15:36 +0000642 if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1)
Reid Spenceraf77d742004-10-28 04:05:06 +0000643 throw std::string("An output file name cannot be specified ") +
644 "with more than one input file name when not linking";
645
646 // This vector holds all the resulting actions of the following loop.
647 std::vector<Action*> actions;
648
649 /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
650 // for each input item
651 SetVector<sys::Path> LinkageItems;
Reid Spencerf6358c72004-12-19 18:00:56 +0000652 StringVector LibFiles;
Reid Spenceraf77d742004-10-28 04:05:06 +0000653 InputList::const_iterator I = InpList.begin();
Reid Spencer679a7232004-11-20 20:39:33 +0000654 for (InputList::const_iterator I = InpList.begin(), E = InpList.end();
655 I != E; ++I ) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000656 // Get the suffix of the file name
657 const std::string& ftype = I->second;
658
Misha Brukman3da94ae2005-04-22 00:00:37 +0000659 // If its a library, bytecode file, or object file, save
660 // it for linking below and short circuit the
Reid Spenceraf77d742004-10-28 04:05:06 +0000661 // pre-processing/translation/assembly phases
662 if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000663 // We shouldn't get any of these types of files unless we're
Reid Spenceraf77d742004-10-28 04:05:06 +0000664 // later going to link. Enforce this limit now.
665 if (finalPhase != LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000666 throw std::string(
Reid Spenceraf77d742004-10-28 04:05:06 +0000667 "Pre-compiled objects found but linking not requested");
668 }
669 if (ftype.empty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000670 LibFiles.push_back(I->first.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000671 else
672 LinkageItems.insert(I->first);
Reid Spencer679a7232004-11-20 20:39:33 +0000673 continue; // short circuit remainder of loop
Reid Spenceraf77d742004-10-28 04:05:06 +0000674 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000675
Reid Spenceraf77d742004-10-28 04:05:06 +0000676 // At this point, we know its something we need to translate
677 // and/or optimize. See if we can get the configuration data
678 // for this kind of file.
679 ConfigData* cd = cdp->ProvideConfigData(I->second);
680 if (cd == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000681 throw std::string("Files of type '") + I->second +
682 "' are not recognized.";
Reid Spenceraf77d742004-10-28 04:05:06 +0000683 if (isSet(DEBUG_FLAG))
684 DumpConfigData(cd,I->second);
Reid Spencer54fafe42004-09-14 01:58:45 +0000685
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000686 // Add the config data's library paths to the end of the list
687 for (StringVector::iterator LPI = cd->libpaths.begin(),
688 LPE = cd->libpaths.end(); LPI != LPE; ++LPI){
689 LibraryPaths.push_back(sys::Path(*LPI));
690 }
691
Reid Spenceraf77d742004-10-28 04:05:06 +0000692 // Initialize the input and output files
693 sys::Path InFile(I->first);
Reid Spencer07adb282004-11-05 22:15:36 +0000694 sys::Path OutFile(I->first.getBasename());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000695
Reid Spenceraf77d742004-10-28 04:05:06 +0000696 // PRE-PROCESSING PHASE
697 Action& action = cd->PreProcessor;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000698
Reid Spenceraf77d742004-10-28 04:05:06 +0000699 // Get the preprocessing action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000700 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000701 if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
702 if (finalPhase == PREPROCESSING) {
Reid Spencer679a7232004-11-20 20:39:33 +0000703 if (Output.isEmpty()) {
704 OutFile.appendSuffix("E");
705 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
706 } else {
707 actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
708 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000709 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000710 sys::Path TempFile(
711 MakeTempFile(I->first.getBasename(),"E",&ErrMsg));
712 if (TempFile.isEmpty())
713 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000714 actions.push_back(GetAction(cd,InFile,TempFile,
715 PREPROCESSING));
716 InFile = TempFile;
717 }
718 }
719 } else if (finalPhase == PREPROCESSING) {
720 throw cd->langName + " does not support pre-processing";
721 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000722 throw std::string("Don't know how to pre-process ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000723 cd->langName + " files";
724 }
725
Misha Brukman3da94ae2005-04-22 00:00:37 +0000726 // Short-circuit remaining actions if all they want is
Reid Spenceraf77d742004-10-28 04:05:06 +0000727 // pre-processing
Reid Spencer679a7232004-11-20 20:39:33 +0000728 if (finalPhase == PREPROCESSING) { continue; };
Reid Spenceraf77d742004-10-28 04:05:06 +0000729
730 /// TRANSLATION PHASE
731 action = cd->Translator;
732
733 // Get the translation action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000734 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000735 if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
736 if (finalPhase == TRANSLATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000737 if (Output.isEmpty()) {
738 OutFile.appendSuffix("o");
739 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
740 } else {
741 actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
742 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000743 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000744 sys::Path TempFile(
745 MakeTempFile(I->first.getBasename(),"trans", &ErrMsg));
746 if (TempFile.isEmpty())
747 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000748 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
749 InFile = TempFile;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000750 }
751
Reid Spenceraf77d742004-10-28 04:05:06 +0000752 // ll -> bc Helper
753 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
754 /// The output of the translator is an LLVM Assembly program
755 /// We need to translate it to bytecode
756 Action* action = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000757 action->program.set("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000758 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000759 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000760 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000761 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000762 actions.push_back(action);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000763 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000764 }
765 } else if (finalPhase == TRANSLATION) {
766 throw cd->langName + " does not support translation";
767 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000768 throw std::string("Don't know how to translate ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000769 cd->langName + " files";
770 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000771
Reid Spenceraf77d742004-10-28 04:05:06 +0000772 // Short-circuit remaining actions if all they want is translation
Reid Spencer679a7232004-11-20 20:39:33 +0000773 if (finalPhase == TRANSLATION) { continue; }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000774
Reid Spenceraf77d742004-10-28 04:05:06 +0000775 /// OPTIMIZATION PHASE
776 action = cd->Optimizer;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000777
Reid Spenceraf77d742004-10-28 04:05:06 +0000778 // Get the optimization action, if needed, or error if appropriate
779 if (!isSet(EMIT_RAW_FLAG)) {
Reid Spencer07adb282004-11-05 22:15:36 +0000780 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000781 if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
782 if (finalPhase == OPTIMIZATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000783 if (Output.isEmpty()) {
784 OutFile.appendSuffix("o");
785 actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
786 } else {
787 actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
788 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000789 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000790 sys::Path TempFile(
791 MakeTempFile(I->first.getBasename(),"opt", &ErrMsg));
792 if (TempFile.isEmpty())
793 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000794 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
795 InFile = TempFile;
796 }
797 // ll -> bc Helper
798 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
799 /// The output of the optimizer is an LLVM Assembly program
800 /// We need to translate it to bytecode with llvm-as
Reid Spencer52c2dc12004-08-29 19:26:56 +0000801 Action* action = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000802 action->program.set("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000803 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000804 action->args.push_back("-f");
805 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000806 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000807 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000808 actions.push_back(action);
809 }
810 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000811 } else if (finalPhase == OPTIMIZATION) {
812 throw cd->langName + " does not support optimization";
813 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000814 throw std::string("Don't know how to optimize ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000815 cd->langName + " files";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000816 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000817 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000818
819 // Short-circuit remaining actions if all they want is optimization
Reid Spencer679a7232004-11-20 20:39:33 +0000820 if (finalPhase == OPTIMIZATION) { continue; }
Reid Spenceraf77d742004-10-28 04:05:06 +0000821
822 /// ASSEMBLY PHASE
823 action = cd->Assembler;
824
825 if (finalPhase == ASSEMBLY) {
Reid Spencer679a7232004-11-20 20:39:33 +0000826
827 // Build either a native compilation action or a disassembly action
828 Action* action = new Action();
Reid Spenceraf77d742004-10-28 04:05:06 +0000829 if (isSet(EMIT_NATIVE_FLAG)) {
830 // Use llc to get the native assembly file
Reid Spencerdd04df02005-07-07 23:21:43 +0000831 action->program.set("llc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000832 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000833 action->args.push_back("-f");
834 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000835 if (Output.isEmpty()) {
836 OutFile.appendSuffix("o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000837 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000838 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000839 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000840 }
841 actions.push_back(action);
Reid Spenceraf77d742004-10-28 04:05:06 +0000842 } else {
843 // Just convert back to llvm assembly with llvm-dis
Reid Spencerdd04df02005-07-07 23:21:43 +0000844 action->program.set("llvm-dis");
Reid Spencer1fce0912004-12-11 00:14:15 +0000845 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000846 action->args.push_back("-f");
847 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000848 if (Output.isEmpty()) {
849 OutFile.appendSuffix("ll");
Reid Spencer1fce0912004-12-11 00:14:15 +0000850 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000851 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000852 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000853 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000854 }
855
Reid Spencer679a7232004-11-20 20:39:33 +0000856 // Put the action on the list
857 actions.push_back(action);
858
Misha Brukman3da94ae2005-04-22 00:00:37 +0000859 // Short circuit the rest of the loop, we don't want to link
Reid Spenceraf77d742004-10-28 04:05:06 +0000860 continue;
861 }
862
863 // Register the result of the actions as a link candidate
864 LinkageItems.insert(InFile);
865
Reid Spenceraf77d742004-10-28 04:05:06 +0000866 } // end while loop over each input file
867
868 /// RUN THE COMPILATION ACTIONS
869 std::vector<Action*>::iterator AI = actions.begin();
870 std::vector<Action*>::iterator AE = actions.end();
871 while (AI != AE) {
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000872 int ActionResult = DoAction(*AI, ErrMsg);
873 if (ActionResult != 0)
874 return ActionResult;
Reid Spenceraf77d742004-10-28 04:05:06 +0000875 AI++;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000876 }
877
Reid Spenceraf77d742004-10-28 04:05:06 +0000878 /// LINKING PHASE
879 if (finalPhase == LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000880
Reid Spenceraf77d742004-10-28 04:05:06 +0000881 // Insert the platform-specific system libraries to the path list
Reid Spencer11db4b82004-12-13 03:01:26 +0000882 std::vector<sys::Path> SysLibs;
883 sys::Path::GetSystemLibraryPaths(SysLibs);
884 LibraryPaths.insert(LibraryPaths.end(), SysLibs.begin(), SysLibs.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000885
886 // Set up the linking action with llvm-ld
887 Action* link = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000888 link->program.set("llvm-ld");
Reid Spenceraf77d742004-10-28 04:05:06 +0000889
890 // Add in the optimization level requested
891 switch (optLevel) {
892 case OPT_FAST_COMPILE:
893 link->args.push_back("-O1");
894 break;
895 case OPT_SIMPLE:
896 link->args.push_back("-O2");
897 break;
898 case OPT_AGGRESSIVE:
899 link->args.push_back("-O3");
900 break;
901 case OPT_LINK_TIME:
902 link->args.push_back("-O4");
903 break;
904 case OPT_AGGRESSIVE_LINK_TIME:
905 link->args.push_back("-O5");
906 break;
907 case OPT_NONE:
908 break;
909 }
910
911 // Add in all the linkage items we generated. This includes the
912 // output from the translation/optimization phases as well as any
913 // -l arguments specified.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000914 for (PathVector::const_iterator I=LinkageItems.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000915 E=LinkageItems.end(); I != E; ++I )
Reid Spencer1fce0912004-12-11 00:14:15 +0000916 link->args.push_back(I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000917
918 // Add in all the libraries we found.
Reid Spencerf6358c72004-12-19 18:00:56 +0000919 for (StringVector::const_iterator I=LibFiles.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000920 E=LibFiles.end(); I != E; ++I )
921 link->args.push_back(std::string("-l")+*I);
922
923 // Add in all the library paths to the command line
924 for (PathVector::const_iterator I=LibraryPaths.begin(),
925 E=LibraryPaths.end(); I != E; ++I)
Reid Spencer1fce0912004-12-11 00:14:15 +0000926 link->args.push_back( std::string("-L") + I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000927
928 // Add in the additional linker arguments requested
929 for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(),
930 E=AdditionalArgs[LINKING].end(); I != E; ++I)
931 link->args.push_back( *I );
932
933 // Add in other optional flags
934 if (isSet(EMIT_NATIVE_FLAG))
935 link->args.push_back("-native");
936 if (isSet(VERBOSE_FLAG))
937 link->args.push_back("-v");
938 if (isSet(TIME_PASSES_FLAG))
939 link->args.push_back("-time-passes");
940 if (isSet(SHOW_STATS_FLAG))
941 link->args.push_back("-stats");
942 if (isSet(STRIP_OUTPUT_FLAG))
943 link->args.push_back("-s");
944 if (isSet(DEBUG_FLAG)) {
945 link->args.push_back("-debug");
946 link->args.push_back("-debug-pass=Details");
947 }
948
949 // Add in mandatory flags
950 link->args.push_back("-o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000951 link->args.push_back(Output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000952
953 // Execute the link
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000954 int ActionResult = DoAction(link, ErrMsg);
955 if (ActionResult != 0)
956 return ActionResult;
Reid Spenceraf77d742004-10-28 04:05:06 +0000957 }
958 } catch (std::string& msg) {
959 cleanup();
960 throw;
961 } catch (...) {
962 cleanup();
963 throw std::string("Unspecified error");
964 }
965 cleanup();
966 return 0;
967 }
968
969/// @}
970/// @name Data
971/// @{
972private:
973 ConfigDataProvider* cdp; ///< Where we get configuration data from
974 Phases finalPhase; ///< The final phase of compilation
975 OptimizationLevels optLevel; ///< The optimization level to apply
976 unsigned Flags; ///< The driver flags
977 std::string machine; ///< Target machine name
978 PathVector LibraryPaths; ///< -L options
979 PathVector IncludePaths; ///< -I options
Reid Spencer07adb282004-11-05 22:15:36 +0000980 PathVector ToolPaths; ///< -B options
Reid Spenceraf77d742004-10-28 04:05:06 +0000981 StringVector Defines; ///< -D options
982 sys::Path TempDir; ///< Name of the temporary directory.
983 StringTable AdditionalArgs; ///< The -Txyz options
984 StringVector fOptions; ///< -f options
985 StringVector MOptions; ///< -M options
986 StringVector WOptions; ///< -W options
987
988/// @}
989};
Reid Spencer5c56dc12004-08-13 20:22:43 +0000990}
991
992CompilerDriver::~CompilerDriver() {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000993}
994
Reid Spencercc97cfc2005-05-19 00:52:28 +0000995CompilerDriver::ConfigDataProvider::~ConfigDataProvider() {}
996
Reid Spencer52c2dc12004-08-29 19:26:56 +0000997CompilerDriver*
998CompilerDriver::Get(ConfigDataProvider& CDP) {
999 return new CompilerDriverImpl(CDP);
Reid Spencerbae68252004-08-19 04:49:47 +00001000}
1001
1002CompilerDriver::ConfigData::ConfigData()
1003 : langName()
1004 , PreProcessor()
1005 , Translator()
1006 , Optimizer()
1007 , Assembler()
1008 , Linker()
1009{
1010 StringVector emptyVec;
1011 for (unsigned i = 0; i < NUM_PHASES; ++i)
1012 opts.push_back(emptyVec);
Reid Spencer5c56dc12004-08-13 20:22:43 +00001013}