blob: 61d0d5ac8b94d772795f8f9e852843bb39f57a99 [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//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file was developed by Reid Spencer and is distributed under the
7// University of Illinois Open Source License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
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//
13//===------------------------------------------------------------------------===
14
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"
Reid Spencer5c56dc12004-08-13 20:22:43 +000023#include <iostream>
24
25using 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();
31 std::vector<std::string>::iterator I = action->args.begin();
32 while (I != action->args.end()) {
33 std::cerr << " " + *I;
34 ++I;
35 }
36 std::cerr << "\n";
37}
38
39void DumpAction(CompilerDriver::Action* action) {
40 std::cerr << "command = " << action->program.c_str();
41 std::vector<std::string>::iterator I = action->args.begin();
42 while (I != action->args.end()) {
43 std::cerr << " " + *I;
44 ++I;
45 }
46 std::cerr << "\n";
47 std::cerr << "flags = " << action->flags << "\n";
48}
49
50void DumpConfigData(CompilerDriver::ConfigData* cd, const std::string& type ){
51 std::cerr << "Configuration Data For '" << cd->langName << "' (" << type
52 << ")\n";
53 std::cerr << "PreProcessor: ";
54 DumpAction(&cd->PreProcessor);
55 std::cerr << "Translator: ";
56 DumpAction(&cd->Translator);
57 std::cerr << "Optimizer: ";
58 DumpAction(&cd->Optimizer);
59 std::cerr << "Assembler: ";
60 DumpAction(&cd->Assembler);
61 std::cerr << "Linker: ";
62 DumpAction(&cd->Linker);
63}
64
65/// This specifies the passes to run for OPT_FAST_COMPILE (-O1)
66/// which should reduce the volume of code and make compilation
67/// faster. This is also safe on any llvm module.
68static const char* DefaultFastCompileOptimizations[] = {
69 "-simplifycfg", "-mem2reg", "-instcombine"
70};
71
72class CompilerDriverImpl : public CompilerDriver {
73/// @name Constructors
74/// @{
75public:
76 CompilerDriverImpl(ConfigDataProvider& confDatProv )
77 : cdp(&confDatProv)
78 , finalPhase(LINKING)
79 , optLevel(OPT_FAST_COMPILE)
80 , Flags(0)
81 , machine()
82 , LibraryPaths()
83 , TempDir()
84 , AdditionalArgs()
85 {
86 TempDir = sys::Path::GetTemporaryDirectory();
87 sys::RemoveDirectoryOnSignal(TempDir);
88 AdditionalArgs.reserve(NUM_PHASES);
89 StringVector emptyVec;
90 for (unsigned i = 0; i < NUM_PHASES; ++i)
91 AdditionalArgs.push_back(emptyVec);
92 }
93
94 virtual ~CompilerDriverImpl() {
95 cleanup();
96 cdp = 0;
97 LibraryPaths.clear();
98 IncludePaths.clear();
99 Defines.clear();
100 TempDir.clear();
101 AdditionalArgs.clear();
102 fOptions.clear();
103 MOptions.clear();
104 WOptions.clear();
105 }
106
107/// @}
108/// @name Methods
109/// @{
110public:
111 virtual void setFinalPhase( Phases phase ) {
112 finalPhase = phase;
113 }
114
115 virtual void setOptimization( OptimizationLevels level ) {
116 optLevel = level;
117 }
118
119 virtual void setDriverFlags( unsigned flags ) {
120 Flags = flags & DRIVER_FLAGS_MASK;
121 }
122
123 virtual void setOutputMachine( const std::string& machineName ) {
124 machine = machineName;
125 }
126
127 virtual void setPhaseArgs(Phases phase, const StringVector& opts) {
128 assert(phase <= LINKING && phase >= PREPROCESSING);
129 AdditionalArgs[phase] = opts;
130 }
131
132 virtual void setIncludePaths(const StringVector& paths) {
133 StringVector::const_iterator I = paths.begin();
134 StringVector::const_iterator E = paths.end();
135 while (I != E) {
136 sys::Path tmp;
Reid Spencer07adb282004-11-05 22:15:36 +0000137 tmp.setDirectory(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000138 IncludePaths.push_back(tmp);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000139 ++I;
140 }
Reid Spencer68fb37a2004-08-14 09:37:15 +0000141 }
142
Reid Spenceraf77d742004-10-28 04:05:06 +0000143 virtual void setSymbolDefines(const StringVector& defs) {
144 Defines = defs;
145 }
146
147 virtual void setLibraryPaths(const StringVector& paths) {
148 StringVector::const_iterator I = paths.begin();
149 StringVector::const_iterator E = paths.end();
150 while (I != E) {
151 sys::Path tmp;
Reid Spencer07adb282004-11-05 22:15:36 +0000152 tmp.setDirectory(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000153 LibraryPaths.push_back(tmp);
Reid Spencerbf437722004-08-15 08:19:46 +0000154 ++I;
155 }
Reid Spencerbf437722004-08-15 08:19:46 +0000156 }
157
Reid Spenceraf77d742004-10-28 04:05:06 +0000158 virtual void addLibraryPath( const sys::Path& libPath ) {
159 LibraryPaths.push_back(libPath);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000160 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000161
Reid Spencer07adb282004-11-05 22:15:36 +0000162 virtual void addToolPath( const sys::Path& toolPath ) {
163 ToolPaths.push_back(toolPath);
164 }
165
Reid Spenceraf77d742004-10-28 04:05:06 +0000166 virtual void setfPassThrough(const StringVector& fOpts) {
167 fOptions = fOpts;
168 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000169
Reid Spenceraf77d742004-10-28 04:05:06 +0000170 /// @brief Set the list of -M options to be passed through
171 virtual void setMPassThrough(const StringVector& MOpts) {
172 MOptions = MOpts;
173 }
Reid Spencerbae68252004-08-19 04:49:47 +0000174
Reid Spenceraf77d742004-10-28 04:05:06 +0000175 /// @brief Set the list of -W options to be passed through
176 virtual void setWPassThrough(const StringVector& WOpts) {
177 WOptions = WOpts;
178 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000179
Reid Spenceraf77d742004-10-28 04:05:06 +0000180/// @}
181/// @name Functions
182/// @{
183private:
184 bool isSet(DriverFlags flag) {
185 return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
186 }
Reid Spencerbae68252004-08-19 04:49:47 +0000187
Reid Spenceraf77d742004-10-28 04:05:06 +0000188 void cleanup() {
189 if (!isSet(KEEP_TEMPS_FLAG)) {
Reid Spencer07adb282004-11-05 22:15:36 +0000190 if (TempDir.isDirectory() && TempDir.writable())
191 TempDir.destroyDirectory(/*remove_contents=*/true);
Reid Spenceraf77d742004-10-28 04:05:06 +0000192 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000193 std::cout << "Temporary files are in " << TempDir.toString() << "\n";
Reid Spenceraf77d742004-10-28 04:05:06 +0000194 }
195 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000196
Reid Spenceraf77d742004-10-28 04:05:06 +0000197 sys::Path MakeTempFile(const std::string& basename,
198 const std::string& suffix ) {
199 sys::Path result(TempDir);
Reid Spencer07adb282004-11-05 22:15:36 +0000200 if (!result.appendFile(basename))
Reid Spenceraf77d742004-10-28 04:05:06 +0000201 throw basename + ": can't use this file name";
Reid Spencer07adb282004-11-05 22:15:36 +0000202 if (!result.appendSuffix(suffix))
Reid Spenceraf77d742004-10-28 04:05:06 +0000203 throw suffix + ": can't use this file suffix";
204 return result;
205 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000206
Reid Spenceraf77d742004-10-28 04:05:06 +0000207 Action* GetAction(ConfigData* cd,
208 const sys::Path& input,
209 const sys::Path& output,
210 Phases phase)
211 {
212 Action* pat = 0; ///< The pattern/template for the action
213 Action* action = new Action; ///< The actual action to execute
Reid Spencer52c2dc12004-08-29 19:26:56 +0000214
Reid Spenceraf77d742004-10-28 04:05:06 +0000215 // Get the action pattern
216 switch (phase) {
217 case PREPROCESSING: pat = &cd->PreProcessor; break;
218 case TRANSLATION: pat = &cd->Translator; break;
219 case OPTIMIZATION: pat = &cd->Optimizer; break;
220 case ASSEMBLY: pat = &cd->Assembler; break;
221 case LINKING: pat = &cd->Linker; break;
222 default:
223 assert(!"Invalid driver phase!");
224 break;
225 }
226 assert(pat != 0 && "Invalid command pattern");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000227
Reid Spenceraf77d742004-10-28 04:05:06 +0000228 // Copy over some pattern things that don't need to change
229 action->program = pat->program;
230 action->flags = pat->flags;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000231
Reid Spenceraf77d742004-10-28 04:05:06 +0000232 // Do the substitutions from the pattern to the actual
233 StringVector::iterator PI = pat->args.begin();
234 StringVector::iterator PE = pat->args.end();
235 while (PI != PE) {
236 if ((*PI)[0] == '%' && PI->length() >2) {
237 bool found = true;
238 switch ((*PI)[1]) {
239 case 'a':
240 if (*PI == "%args%") {
241 if (AdditionalArgs.size() > unsigned(phase))
242 if (!AdditionalArgs[phase].empty()) {
243 // Get specific options for each kind of action type
244 StringVector& addargs = AdditionalArgs[phase];
245 // Add specific options for each kind of action type
246 action->args.insert(action->args.end(), addargs.begin(),
247 addargs.end());
248 }
249 } else
250 found = false;
251 break;
252 case 'd':
253 if (*PI == "%defs%") {
254 StringVector::iterator I = Defines.begin();
255 StringVector::iterator E = Defines.end();
256 while (I != E) {
257 action->args.push_back( std::string("-D") + *I);
258 ++I;
259 }
260 } else
261 found = false;
262 break;
263 case 'f':
264 if (*PI == "%fOpts%") {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000265 if (!fOptions.empty())
266 action->args.insert(action->args.end(), fOptions.begin(),
267 fOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000268 } else
269 found = false;
270 break;
271 case 'i':
272 if (*PI == "%in%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000273 action->args.push_back(input.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000274 } else if (*PI == "%incls%") {
275 PathVector::iterator I = IncludePaths.begin();
276 PathVector::iterator E = IncludePaths.end();
277 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000278 action->args.push_back( std::string("-I") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000279 ++I;
280 }
281 } else
282 found = false;
283 break;
284 case 'l':
285 if (*PI == "%libs%") {
286 PathVector::iterator I = LibraryPaths.begin();
287 PathVector::iterator E = LibraryPaths.end();
288 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000289 action->args.push_back( std::string("-L") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000290 ++I;
291 }
292 } else
293 found = false;
294 break;
295 case 'o':
296 if (*PI == "%out%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000297 action->args.push_back(output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000298 } else if (*PI == "%opt%") {
299 if (!isSet(EMIT_RAW_FLAG)) {
300 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
301 !cd->opts[optLevel].empty())
302 action->args.insert(action->args.end(),
303 cd->opts[optLevel].begin(),
304 cd->opts[optLevel].end());
305 else
306 throw std::string("Optimization options for level ") +
307 utostr(unsigned(optLevel)) + " were not specified";
308 }
309 } else
310 found = false;
311 break;
312 case 's':
313 if (*PI == "%stats%") {
314 if (isSet(SHOW_STATS_FLAG))
315 action->args.push_back("-stats");
316 } else
317 found = false;
318 break;
319 case 't':
320 if (*PI == "%target%") {
321 action->args.push_back(std::string("-march=") + machine);
322 } else if (*PI == "%time%") {
323 if (isSet(TIME_PASSES_FLAG))
324 action->args.push_back("-time-passes");
325 } else
326 found = false;
327 break;
328 case 'v':
329 if (*PI == "%verbose%") {
330 if (isSet(VERBOSE_FLAG))
331 action->args.push_back("-v");
332 } else
333 found = false;
334 break;
335 case 'M':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000336 if (*PI == "%Mopts%") {
337 if (!MOptions.empty())
338 action->args.insert(action->args.end(), MOptions.begin(),
339 MOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000340 } else
341 found = false;
342 break;
343 case 'W':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000344 if (*PI == "%Wopts%") {
345 for (StringVector::iterator I = WOptions.begin(),
346 E = WOptions.end(); I != E ; ++I ) {
347 action->args.push_back( std::string("-W") + *I );
348 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000349 } else
350 found = false;
351 break;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000352 default:
Reid Spenceraf77d742004-10-28 04:05:06 +0000353 found = false;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000354 break;
355 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000356 if (!found) {
357 // Did it even look like a substitution?
358 if (PI->length()>1 && (*PI)[0] == '%' &&
359 (*PI)[PI->length()-1] == '%') {
360 throw std::string("Invalid substitution token: '") + *PI +
Reid Spencer1fce0912004-12-11 00:14:15 +0000361 "' for command '" + pat->program.toString() + "'";
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000362 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000363 // It's not a legal substitution, just pass it through
Reid Spencer52c2dc12004-08-29 19:26:56 +0000364 action->args.push_back(*PI);
365 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000366 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000367 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000368 // Its not a substitution, just put it in the action
369 action->args.push_back(*PI);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000370 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000371 PI++;
372 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000373
Reid Spenceraf77d742004-10-28 04:05:06 +0000374 // Finally, we're done
375 return action;
376 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000377
Reid Spenceraf77d742004-10-28 04:05:06 +0000378 bool DoAction(Action*action) {
379 assert(action != 0 && "Invalid Action!");
380 if (isSet(VERBOSE_FLAG))
381 WriteAction(action);
382 if (!isSet(DRY_RUN_FLAG)) {
383 sys::Path progpath = sys::Program::FindProgramByName(
Reid Spencer1fce0912004-12-11 00:14:15 +0000384 action->program.toString());
Reid Spencer07adb282004-11-05 22:15:36 +0000385 if (progpath.isEmpty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000386 throw std::string("Can't find program '" +
387 action->program.toString()+"'");
Reid Spenceraf77d742004-10-28 04:05:06 +0000388 else if (progpath.executable())
389 action->program = progpath;
390 else
Reid Spencer1fce0912004-12-11 00:14:15 +0000391 throw std::string("Program '"+action->program.toString()+
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000392 "' is not executable.");
Reid Spenceraf77d742004-10-28 04:05:06 +0000393
394 // Invoke the program
395 if (isSet(TIME_ACTIONS_FLAG)) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000396 Timer timer(action->program.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000397 timer.startTimer();
398 int resultCode =
399 sys::Program::ExecuteAndWait(action->program,action->args);
400 timer.stopTimer();
401 timer.print(timer,std::cerr);
402 return resultCode == 0;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000403 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000404 else
405 return 0 ==
406 sys::Program::ExecuteAndWait(action->program, action->args);
407 }
408 return true;
409 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000410
Reid Spenceraf77d742004-10-28 04:05:06 +0000411 /// This method tries various variants of a linkage item's file
412 /// name to see if it can find an appropriate file to link with
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000413 /// in the directories of the LibraryPaths.
Reid Spenceraf77d742004-10-28 04:05:06 +0000414 llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
Reid Spenceraf77d742004-10-28 04:05:06 +0000415 bool native = false) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000416 sys::Path fullpath;
417 fullpath.setFile(link_item);
418 if (fullpath.readable())
419 return fullpath;
420 for (PathVector::iterator PI = LibraryPaths.begin(),
421 PE = LibraryPaths.end(); PI != PE; ++PI) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000422 fullpath.setDirectory(PI->toString());
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000423 fullpath.appendFile(link_item);
Reid Spenceraf77d742004-10-28 04:05:06 +0000424 if (fullpath.readable())
425 return fullpath;
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000426 if (native) {
427 fullpath.appendSuffix("a");
428 } else {
429 fullpath.appendSuffix("bc");
430 if (fullpath.readable())
431 return fullpath;
432 fullpath.elideSuffix();
433 fullpath.appendSuffix("o");
434 if (fullpath.readable())
435 return fullpath;
436 fullpath = *PI;
437 fullpath.appendFile(std::string("lib") + link_item);
438 fullpath.appendSuffix("a");
439 if (fullpath.readable())
440 return fullpath;
441 fullpath.elideSuffix();
442 fullpath.appendSuffix("so");
443 if (fullpath.readable())
444 return fullpath;
445 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000446 }
447
448 // Didn't find one.
449 fullpath.clear();
450 return fullpath;
451 }
452
453 /// This method processes a linkage item. The item could be a
454 /// Bytecode file needing translation to native code and that is
455 /// dependent on other bytecode libraries, or a native code
456 /// library that should just be linked into the program.
457 bool ProcessLinkageItem(const llvm::sys::Path& link_item,
458 SetVector<sys::Path>& set,
459 std::string& err) {
460 // First, see if the unadorned file name is not readable. If so,
461 // we must track down the file in the lib search path.
462 sys::Path fullpath;
463 if (!link_item.readable()) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000464 // look for the library using the -L arguments specified
Reid Spenceraf77d742004-10-28 04:05:06 +0000465 // on the command line.
Reid Spencer1fce0912004-12-11 00:14:15 +0000466 fullpath = GetPathForLinkageItem(link_item.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000467
Reid Spenceraf77d742004-10-28 04:05:06 +0000468 // If we didn't find the file in any of the library search paths
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000469 // we have to bail. No where else to look.
Reid Spencer07adb282004-11-05 22:15:36 +0000470 if (fullpath.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000471 err =
Reid Spencer1fce0912004-12-11 00:14:15 +0000472 std::string("Can't find linkage item '") + link_item.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000473 return false;
474 }
475 } else {
476 fullpath = link_item;
477 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000478
Reid Spenceraf77d742004-10-28 04:05:06 +0000479 // If we got here fullpath is the path to the file, and its readable.
480 set.insert(fullpath);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000481
Reid Spenceraf77d742004-10-28 04:05:06 +0000482 // If its an LLVM bytecode file ...
Reid Spencer07adb282004-11-05 22:15:36 +0000483 if (fullpath.isBytecodeFile()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000484 // Process the dependent libraries recursively
485 Module::LibraryListType modlibs;
Reid Spencer1fce0912004-12-11 00:14:15 +0000486 if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs)) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000487 // Traverse the dependent libraries list
488 Module::lib_iterator LI = modlibs.begin();
489 Module::lib_iterator LE = modlibs.end();
490 while ( LI != LE ) {
491 if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
492 if (err.empty()) {
493 err = std::string("Library '") + *LI +
494 "' is not valid for linking but is required by file '" +
Reid Spencer1fce0912004-12-11 00:14:15 +0000495 fullpath.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000496 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000497 err += " which is required by file '" + fullpath.toString() + "'";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000498 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000499 return false;
500 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000501 ++LI;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000502 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000503 } else if (err.empty()) {
504 err = std::string(
505 "The dependent libraries could not be extracted from '") +
Reid Spencer1fce0912004-12-11 00:14:15 +0000506 fullpath.toString();
Reid Spenceraf77d742004-10-28 04:05:06 +0000507 return false;
508 }
509 }
510 return true;
511 }
512
513/// @}
514/// @name Methods
515/// @{
516public:
517 virtual int execute(const InputList& InpList, const sys::Path& Output ) {
518 try {
519 // Echo the configuration of options if we're running verbose
520 if (isSet(DEBUG_FLAG)) {
521 std::cerr << "Compiler Driver Options:\n";
522 std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
523 std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
524 std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
525 std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
526 std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
527 std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
528 std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
529 std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
530 std::cerr << "OutputMachine = " << machine << "\n";
531 InputList::const_iterator I = InpList.begin();
532 while ( I != InpList.end() ) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000533 std::cerr << "Input: " << I->first.toString() << "(" << I->second
Reid Spenceraf77d742004-10-28 04:05:06 +0000534 << ")\n";
535 ++I;
536 }
Reid Spencer1fce0912004-12-11 00:14:15 +0000537 std::cerr << "Output: " << Output.toString() << "\n";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000538 }
539
Reid Spenceraf77d742004-10-28 04:05:06 +0000540 // If there's no input, we're done.
541 if (InpList.empty())
542 throw std::string("Nothing to compile.");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000543
Reid Spenceraf77d742004-10-28 04:05:06 +0000544 // If they are asking for linking and didn't provide an output
545 // file then its an error (no way for us to "make up" a meaningful
546 // file name based on the various linker input files).
Reid Spencer07adb282004-11-05 22:15:36 +0000547 if (finalPhase == LINKING && Output.isEmpty())
Reid Spenceraf77d742004-10-28 04:05:06 +0000548 throw std::string(
549 "An output file name must be specified for linker output");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000550
Reid Spenceraf77d742004-10-28 04:05:06 +0000551 // If they are not asking for linking, provided an output file and
552 // there is more than one input file, its an error
Reid Spencer07adb282004-11-05 22:15:36 +0000553 if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1)
Reid Spenceraf77d742004-10-28 04:05:06 +0000554 throw std::string("An output file name cannot be specified ") +
555 "with more than one input file name when not linking";
556
557 // This vector holds all the resulting actions of the following loop.
558 std::vector<Action*> actions;
559
560 /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
561 // for each input item
562 SetVector<sys::Path> LinkageItems;
563 std::vector<std::string> LibFiles;
564 InputList::const_iterator I = InpList.begin();
Reid Spencer679a7232004-11-20 20:39:33 +0000565 for (InputList::const_iterator I = InpList.begin(), E = InpList.end();
566 I != E; ++I ) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000567 // Get the suffix of the file name
568 const std::string& ftype = I->second;
569
570 // If its a library, bytecode file, or object file, save
571 // it for linking below and short circuit the
572 // pre-processing/translation/assembly phases
573 if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") {
574 // We shouldn't get any of these types of files unless we're
575 // later going to link. Enforce this limit now.
576 if (finalPhase != LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000577 throw std::string(
Reid Spenceraf77d742004-10-28 04:05:06 +0000578 "Pre-compiled objects found but linking not requested");
579 }
580 if (ftype.empty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000581 LibFiles.push_back(I->first.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000582 else
583 LinkageItems.insert(I->first);
Reid Spencer679a7232004-11-20 20:39:33 +0000584 continue; // short circuit remainder of loop
Reid Spenceraf77d742004-10-28 04:05:06 +0000585 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000586
Reid Spenceraf77d742004-10-28 04:05:06 +0000587 // At this point, we know its something we need to translate
588 // and/or optimize. See if we can get the configuration data
589 // for this kind of file.
590 ConfigData* cd = cdp->ProvideConfigData(I->second);
591 if (cd == 0)
592 throw std::string("Files of type '") + I->second +
593 "' are not recognized.";
594 if (isSet(DEBUG_FLAG))
595 DumpConfigData(cd,I->second);
Reid Spencer54fafe42004-09-14 01:58:45 +0000596
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000597 // Add the config data's library paths to the end of the list
598 for (StringVector::iterator LPI = cd->libpaths.begin(),
599 LPE = cd->libpaths.end(); LPI != LPE; ++LPI){
600 LibraryPaths.push_back(sys::Path(*LPI));
601 }
602
Reid Spenceraf77d742004-10-28 04:05:06 +0000603 // Initialize the input and output files
604 sys::Path InFile(I->first);
Reid Spencer07adb282004-11-05 22:15:36 +0000605 sys::Path OutFile(I->first.getBasename());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000606
Reid Spenceraf77d742004-10-28 04:05:06 +0000607 // PRE-PROCESSING PHASE
608 Action& action = cd->PreProcessor;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000609
Reid Spenceraf77d742004-10-28 04:05:06 +0000610 // Get the preprocessing action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000611 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000612 if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
613 if (finalPhase == PREPROCESSING) {
Reid Spencer679a7232004-11-20 20:39:33 +0000614 if (Output.isEmpty()) {
615 OutFile.appendSuffix("E");
616 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
617 } else {
618 actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
619 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000620 } else {
Reid Spencer07adb282004-11-05 22:15:36 +0000621 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"E"));
Reid Spenceraf77d742004-10-28 04:05:06 +0000622 actions.push_back(GetAction(cd,InFile,TempFile,
623 PREPROCESSING));
624 InFile = TempFile;
625 }
626 }
627 } else if (finalPhase == PREPROCESSING) {
628 throw cd->langName + " does not support pre-processing";
629 } else if (action.isSet(REQUIRED_FLAG)) {
630 throw std::string("Don't know how to pre-process ") +
631 cd->langName + " files";
632 }
633
634 // Short-circuit remaining actions if all they want is
635 // pre-processing
Reid Spencer679a7232004-11-20 20:39:33 +0000636 if (finalPhase == PREPROCESSING) { continue; };
Reid Spenceraf77d742004-10-28 04:05:06 +0000637
638 /// TRANSLATION PHASE
639 action = cd->Translator;
640
641 // Get the translation action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000642 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000643 if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
644 if (finalPhase == TRANSLATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000645 if (Output.isEmpty()) {
646 OutFile.appendSuffix("o");
647 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
648 } else {
649 actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
650 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000651 } else {
Reid Spencer07adb282004-11-05 22:15:36 +0000652 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"trans"));
Reid Spenceraf77d742004-10-28 04:05:06 +0000653 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
654 InFile = TempFile;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000655 }
656
Reid Spenceraf77d742004-10-28 04:05:06 +0000657 // ll -> bc Helper
658 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
659 /// The output of the translator is an LLVM Assembly program
660 /// We need to translate it to bytecode
661 Action* action = new Action();
Reid Spencer07adb282004-11-05 22:15:36 +0000662 action->program.setFile("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000663 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000664 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000665 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000666 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000667 actions.push_back(action);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000668 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000669 }
670 } else if (finalPhase == TRANSLATION) {
671 throw cd->langName + " does not support translation";
672 } else if (action.isSet(REQUIRED_FLAG)) {
673 throw std::string("Don't know how to translate ") +
674 cd->langName + " files";
675 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000676
Reid Spenceraf77d742004-10-28 04:05:06 +0000677 // Short-circuit remaining actions if all they want is translation
Reid Spencer679a7232004-11-20 20:39:33 +0000678 if (finalPhase == TRANSLATION) { continue; }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000679
Reid Spenceraf77d742004-10-28 04:05:06 +0000680 /// OPTIMIZATION PHASE
681 action = cd->Optimizer;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000682
Reid Spenceraf77d742004-10-28 04:05:06 +0000683 // Get the optimization action, if needed, or error if appropriate
684 if (!isSet(EMIT_RAW_FLAG)) {
Reid Spencer07adb282004-11-05 22:15:36 +0000685 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000686 if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
687 if (finalPhase == OPTIMIZATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000688 if (Output.isEmpty()) {
689 OutFile.appendSuffix("o");
690 actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
691 } else {
692 actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
693 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000694 } else {
Reid Spencer07adb282004-11-05 22:15:36 +0000695 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"opt"));
Reid Spenceraf77d742004-10-28 04:05:06 +0000696 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
697 InFile = TempFile;
698 }
699 // ll -> bc Helper
700 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
701 /// The output of the optimizer is an LLVM Assembly program
702 /// We need to translate it to bytecode with llvm-as
Reid Spencer52c2dc12004-08-29 19:26:56 +0000703 Action* action = new Action();
Reid Spencer07adb282004-11-05 22:15:36 +0000704 action->program.setFile("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000705 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000706 action->args.push_back("-f");
707 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000708 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000709 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000710 actions.push_back(action);
711 }
712 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000713 } else if (finalPhase == OPTIMIZATION) {
714 throw cd->langName + " does not support optimization";
715 } else if (action.isSet(REQUIRED_FLAG)) {
716 throw std::string("Don't know how to optimize ") +
717 cd->langName + " files";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000718 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000719 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000720
721 // Short-circuit remaining actions if all they want is optimization
Reid Spencer679a7232004-11-20 20:39:33 +0000722 if (finalPhase == OPTIMIZATION) { continue; }
Reid Spenceraf77d742004-10-28 04:05:06 +0000723
724 /// ASSEMBLY PHASE
725 action = cd->Assembler;
726
727 if (finalPhase == ASSEMBLY) {
Reid Spencer679a7232004-11-20 20:39:33 +0000728
729 // Build either a native compilation action or a disassembly action
730 Action* action = new Action();
Reid Spenceraf77d742004-10-28 04:05:06 +0000731 if (isSet(EMIT_NATIVE_FLAG)) {
732 // Use llc to get the native assembly file
Reid Spencer07adb282004-11-05 22:15:36 +0000733 action->program.setFile("llc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000734 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000735 action->args.push_back("-f");
736 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000737 if (Output.isEmpty()) {
738 OutFile.appendSuffix("o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000739 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000740 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000741 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000742 }
743 actions.push_back(action);
Reid Spenceraf77d742004-10-28 04:05:06 +0000744 } else {
745 // Just convert back to llvm assembly with llvm-dis
Reid Spencer07adb282004-11-05 22:15:36 +0000746 action->program.setFile("llvm-dis");
Reid Spencer1fce0912004-12-11 00:14:15 +0000747 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000748 action->args.push_back("-f");
749 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000750 if (Output.isEmpty()) {
751 OutFile.appendSuffix("ll");
Reid Spencer1fce0912004-12-11 00:14:15 +0000752 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000753 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000754 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000755 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000756 }
757
Reid Spencer679a7232004-11-20 20:39:33 +0000758 // Put the action on the list
759 actions.push_back(action);
760
Reid Spenceraf77d742004-10-28 04:05:06 +0000761 // Short circuit the rest of the loop, we don't want to link
Reid Spenceraf77d742004-10-28 04:05:06 +0000762 continue;
763 }
764
765 // Register the result of the actions as a link candidate
766 LinkageItems.insert(InFile);
767
Reid Spenceraf77d742004-10-28 04:05:06 +0000768 } // end while loop over each input file
769
770 /// RUN THE COMPILATION ACTIONS
771 std::vector<Action*>::iterator AI = actions.begin();
772 std::vector<Action*>::iterator AE = actions.end();
773 while (AI != AE) {
774 if (!DoAction(*AI))
775 throw std::string("Action failed");
776 AI++;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000777 }
778
Reid Spenceraf77d742004-10-28 04:05:06 +0000779 /// LINKING PHASE
780 if (finalPhase == LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000781
Reid Spenceraf77d742004-10-28 04:05:06 +0000782 // Insert the platform-specific system libraries to the path list
783 LibraryPaths.push_back(sys::Path::GetSystemLibraryPath1());
784 LibraryPaths.push_back(sys::Path::GetSystemLibraryPath2());
785
786 // Set up the linking action with llvm-ld
787 Action* link = new Action();
Reid Spencer07adb282004-11-05 22:15:36 +0000788 link->program.setFile("llvm-ld");
Reid Spenceraf77d742004-10-28 04:05:06 +0000789
790 // Add in the optimization level requested
791 switch (optLevel) {
792 case OPT_FAST_COMPILE:
793 link->args.push_back("-O1");
794 break;
795 case OPT_SIMPLE:
796 link->args.push_back("-O2");
797 break;
798 case OPT_AGGRESSIVE:
799 link->args.push_back("-O3");
800 break;
801 case OPT_LINK_TIME:
802 link->args.push_back("-O4");
803 break;
804 case OPT_AGGRESSIVE_LINK_TIME:
805 link->args.push_back("-O5");
806 break;
807 case OPT_NONE:
808 break;
809 }
810
811 // Add in all the linkage items we generated. This includes the
812 // output from the translation/optimization phases as well as any
813 // -l arguments specified.
814 for (PathVector::const_iterator I=LinkageItems.begin(),
815 E=LinkageItems.end(); I != E; ++I )
Reid Spencer1fce0912004-12-11 00:14:15 +0000816 link->args.push_back(I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000817
818 // Add in all the libraries we found.
819 for (std::vector<std::string>::const_iterator I=LibFiles.begin(),
820 E=LibFiles.end(); I != E; ++I )
821 link->args.push_back(std::string("-l")+*I);
822
823 // Add in all the library paths to the command line
824 for (PathVector::const_iterator I=LibraryPaths.begin(),
825 E=LibraryPaths.end(); I != E; ++I)
Reid Spencer1fce0912004-12-11 00:14:15 +0000826 link->args.push_back( std::string("-L") + I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000827
828 // Add in the additional linker arguments requested
829 for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(),
830 E=AdditionalArgs[LINKING].end(); I != E; ++I)
831 link->args.push_back( *I );
832
833 // Add in other optional flags
834 if (isSet(EMIT_NATIVE_FLAG))
835 link->args.push_back("-native");
836 if (isSet(VERBOSE_FLAG))
837 link->args.push_back("-v");
838 if (isSet(TIME_PASSES_FLAG))
839 link->args.push_back("-time-passes");
840 if (isSet(SHOW_STATS_FLAG))
841 link->args.push_back("-stats");
842 if (isSet(STRIP_OUTPUT_FLAG))
843 link->args.push_back("-s");
844 if (isSet(DEBUG_FLAG)) {
845 link->args.push_back("-debug");
846 link->args.push_back("-debug-pass=Details");
847 }
848
849 // Add in mandatory flags
850 link->args.push_back("-o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000851 link->args.push_back(Output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000852
853 // Execute the link
854 if (!DoAction(link))
855 throw std::string("Action failed");
856 }
857 } catch (std::string& msg) {
858 cleanup();
859 throw;
860 } catch (...) {
861 cleanup();
862 throw std::string("Unspecified error");
863 }
864 cleanup();
865 return 0;
866 }
867
868/// @}
869/// @name Data
870/// @{
871private:
872 ConfigDataProvider* cdp; ///< Where we get configuration data from
873 Phases finalPhase; ///< The final phase of compilation
874 OptimizationLevels optLevel; ///< The optimization level to apply
875 unsigned Flags; ///< The driver flags
876 std::string machine; ///< Target machine name
877 PathVector LibraryPaths; ///< -L options
878 PathVector IncludePaths; ///< -I options
Reid Spencer07adb282004-11-05 22:15:36 +0000879 PathVector ToolPaths; ///< -B options
Reid Spenceraf77d742004-10-28 04:05:06 +0000880 StringVector Defines; ///< -D options
881 sys::Path TempDir; ///< Name of the temporary directory.
882 StringTable AdditionalArgs; ///< The -Txyz options
883 StringVector fOptions; ///< -f options
884 StringVector MOptions; ///< -M options
885 StringVector WOptions; ///< -W options
886
887/// @}
888};
Reid Spencer5c56dc12004-08-13 20:22:43 +0000889}
890
891CompilerDriver::~CompilerDriver() {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000892}
893
894CompilerDriver*
895CompilerDriver::Get(ConfigDataProvider& CDP) {
896 return new CompilerDriverImpl(CDP);
Reid Spencerbae68252004-08-19 04:49:47 +0000897}
898
899CompilerDriver::ConfigData::ConfigData()
900 : langName()
901 , PreProcessor()
902 , Translator()
903 , Optimizer()
904 , Assembler()
905 , Linker()
906{
907 StringVector emptyVec;
908 for (unsigned i = 0; i < NUM_PHASES; ++i)
909 opts.push_back(emptyVec);
Reid Spencer5c56dc12004-08-13 20:22:43 +0000910}
911
Reid Spencer5c56dc12004-08-13 20:22:43 +0000912// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab