blob: 7875aff18d745fe4fc04862352354be25c1d747f [file] [log] [blame]
Reid Spencer5c56dc12004-08-13 20:22:43 +00001//===--- llvmc.cpp - The LLVM Compiler Driver -------------------*- C++ -*-===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
Reid Spencer034a5442004-08-10 16:26:01 +00003// The LLVM Compiler Infrastructure
4//
Reid Spencer5c56dc12004-08-13 20:22:43 +00005// This file was developed by Reid Spencer and is distributed under the
Reid Spencer034a5442004-08-10 16:26:01 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
Reid Spencer034a5442004-08-10 16:26:01 +00008//===----------------------------------------------------------------------===//
9//
10// This tool provides a single point of access to the LLVM compilation tools.
11// It has many options. To discover the options supported please refer to the
12// tools' manual page (docs/CommandGuide/html/llvmc.html) or run the tool with
13// the --help option.
Misha Brukman3da94ae2005-04-22 00:00:37 +000014//
Reid Spencer034a5442004-08-10 16:26:01 +000015//===------------------------------------------------------------------------===
16
Reid Spencerabf1ce32004-08-10 16:29:18 +000017#include "CompilerDriver.h"
Reid Spencerf51a87c2004-08-19 21:52:49 +000018#include "Configuration.h"
Reid Spencer44956322004-08-24 17:54:26 +000019#include "llvm/Pass.h"
Reid Spencer034a5442004-08-10 16:26:01 +000020#include "llvm/System/Signals.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Support/CommandLine.h"
Reid Spencer034a5442004-08-10 16:26:01 +000022#include <iostream>
23
24using namespace llvm;
25
Reid Spencer5c56dc12004-08-13 20:22:43 +000026namespace {
Reid Spencer034a5442004-08-10 16:26:01 +000027//===------------------------------------------------------------------------===
28//=== PHASE OPTIONS
29//===------------------------------------------------------------------------===
Reid Spencer65bc4e02004-11-23 23:47:58 +000030cl::opt<CompilerDriver::Phases> FinalPhase(cl::Optional,
Misha Brukman3da94ae2005-04-22 00:00:37 +000031 cl::desc("Choose final phase of compilation:"),
Reid Spencer5c56dc12004-08-13 20:22:43 +000032 cl::init(CompilerDriver::LINKING),
Reid Spencer034a5442004-08-10 16:26:01 +000033 cl::values(
34 clEnumValN(CompilerDriver::PREPROCESSING,"E",
Reid Spencer5c56dc12004-08-13 20:22:43 +000035 "Stop compilation after pre-processing phase"),
36 clEnumValN(CompilerDriver::TRANSLATION, "t",
37 "Stop compilation after translation phase"),
Reid Spencer034a5442004-08-10 16:26:01 +000038 clEnumValN(CompilerDriver::OPTIMIZATION,"c",
Reid Spencer5c56dc12004-08-13 20:22:43 +000039 "Stop compilation after optimization phase"),
Reid Spencer034a5442004-08-10 16:26:01 +000040 clEnumValN(CompilerDriver::ASSEMBLY,"S",
Reid Spencer5c56dc12004-08-13 20:22:43 +000041 "Stop compilation after assembly phase"),
Reid Spencer034a5442004-08-10 16:26:01 +000042 clEnumValEnd
43 )
44);
45
46//===------------------------------------------------------------------------===
47//=== OPTIMIZATION OPTIONS
48//===------------------------------------------------------------------------===
Reid Spencer65bc4e02004-11-23 23:47:58 +000049cl::opt<CompilerDriver::OptimizationLevels> OptLevel(cl::ZeroOrMore,
Reid Spencer034a5442004-08-10 16:26:01 +000050 cl::desc("Choose level of optimization to apply:"),
Reid Spencer5c56dc12004-08-13 20:22:43 +000051 cl::init(CompilerDriver::OPT_FAST_COMPILE),
Reid Spencer034a5442004-08-10 16:26:01 +000052 cl::values(
53 clEnumValN(CompilerDriver::OPT_FAST_COMPILE,"O0",
Reid Spencer5c56dc12004-08-13 20:22:43 +000054 "An alias for the -O1 option."),
Reid Spencer034a5442004-08-10 16:26:01 +000055 clEnumValN(CompilerDriver::OPT_FAST_COMPILE,"O1",
56 "Optimize for compilation speed, not execution speed."),
57 clEnumValN(CompilerDriver::OPT_SIMPLE,"O2",
58 "Perform simple translation time optimizations"),
59 clEnumValN(CompilerDriver::OPT_AGGRESSIVE,"O3",
60 "Perform aggressive translation time optimizations"),
61 clEnumValN(CompilerDriver::OPT_LINK_TIME,"O4",
62 "Perform link time optimizations"),
63 clEnumValN(CompilerDriver::OPT_AGGRESSIVE_LINK_TIME,"O5",
64 "Perform aggressive link time optimizations"),
65 clEnumValEnd
66 )
67);
68
69//===------------------------------------------------------------------------===
70//=== TOOL OPTIONS
71//===------------------------------------------------------------------------===
72
Reid Spencerbae68252004-08-19 04:49:47 +000073cl::list<std::string> PreprocessorToolOpts("Tpre", cl::ZeroOrMore,
Misha Brukman3da94ae2005-04-22 00:00:37 +000074 cl::desc("Pass specific options to the pre-processor"),
Reid Spencer5c56dc12004-08-13 20:22:43 +000075 cl::value_desc("option"));
Reid Spencer034a5442004-08-10 16:26:01 +000076
Reid Spencer2967b612004-10-28 03:56:16 +000077cl::alias PreprocessorToolOptsAlias("Wp,", cl::ZeroOrMore,
78 cl::desc("Alias for -Tpre"), cl::aliasopt(PreprocessorToolOpts));
79
Reid Spencerbae68252004-08-19 04:49:47 +000080cl::list<std::string> TranslatorToolOpts("Ttrn", cl::ZeroOrMore,
Reid Spencer5c56dc12004-08-13 20:22:43 +000081 cl::desc("Pass specific options to the assembler"),
82 cl::value_desc("option"));
Reid Spencer034a5442004-08-10 16:26:01 +000083
Reid Spencerbae68252004-08-19 04:49:47 +000084cl::list<std::string> AssemblerToolOpts("Tasm", cl::ZeroOrMore,
Reid Spencerbf437722004-08-15 08:19:46 +000085 cl::desc("Pass specific options to the assembler"),
86 cl::value_desc("option"));
87
Reid Spencer2967b612004-10-28 03:56:16 +000088cl::alias AssemblerToolOptsAlias("Wa,", cl::ZeroOrMore,
89 cl::desc("Alias for -Tasm"), cl::aliasopt(AssemblerToolOpts));
90
Reid Spencerbae68252004-08-19 04:49:47 +000091cl::list<std::string> OptimizerToolOpts("Topt", cl::ZeroOrMore,
Reid Spencer5c56dc12004-08-13 20:22:43 +000092 cl::desc("Pass specific options to the optimizer"),
93 cl::value_desc("option"));
Reid Spencer034a5442004-08-10 16:26:01 +000094
Reid Spencerbae68252004-08-19 04:49:47 +000095cl::list<std::string> LinkerToolOpts("Tlnk", cl::ZeroOrMore,
Reid Spencer5c56dc12004-08-13 20:22:43 +000096 cl::desc("Pass specific options to the linker"),
97 cl::value_desc("option"));
Reid Spencer034a5442004-08-10 16:26:01 +000098
Reid Spencer2967b612004-10-28 03:56:16 +000099cl::alias LinkerToolOptsAlias("Wl,", cl::ZeroOrMore,
100 cl::desc("Alias for -Tlnk"), cl::aliasopt(LinkerToolOpts));
101
Reid Spencer54fafe42004-09-14 01:58:45 +0000102cl::list<std::string> fOpts("f", cl::ZeroOrMore, cl::Prefix,
103 cl::desc("Pass through -f options to compiler tools"),
Reid Spencer5443af22005-02-18 20:00:05 +0000104 cl::value_desc("option"));
Reid Spencer54fafe42004-09-14 01:58:45 +0000105
106cl::list<std::string> MOpts("M", cl::ZeroOrMore, cl::Prefix,
107 cl::desc("Pass through -M options to compiler tools"),
Reid Spencer5443af22005-02-18 20:00:05 +0000108 cl::value_desc("option"));
Reid Spencer54fafe42004-09-14 01:58:45 +0000109
110cl::list<std::string> WOpts("W", cl::ZeroOrMore, cl::Prefix,
111 cl::desc("Pass through -W options to compiler tools"),
Reid Spencer5443af22005-02-18 20:00:05 +0000112 cl::value_desc("option"));
Reid Spencer54fafe42004-09-14 01:58:45 +0000113
Misha Brukman3da94ae2005-04-22 00:00:37 +0000114cl::list<std::string> BOpt("B", cl::ZeroOrMore, cl::Prefix,
Reid Spencer5443af22005-02-18 20:00:05 +0000115 cl::desc("Specify path to find llvmc sub-tools"),
116 cl::value_desc("dir"));
Reid Spencer07adb282004-11-05 22:15:36 +0000117
Reid Spencer034a5442004-08-10 16:26:01 +0000118//===------------------------------------------------------------------------===
119//=== INPUT OPTIONS
120//===------------------------------------------------------------------------===
121
Reid Spencerbae68252004-08-19 04:49:47 +0000122cl::list<std::string> LibPaths("L", cl::Prefix,
Reid Spencer5443af22005-02-18 20:00:05 +0000123 cl::desc("Specify a library search path"), cl::value_desc("dir"));
Reid Spencer08602e52004-11-20 20:45:33 +0000124
Reid Spencerbae68252004-08-19 04:49:47 +0000125cl::list<std::string> Libraries("l", cl::Prefix,
Reid Spencer5443af22005-02-18 20:00:05 +0000126 cl::desc("Specify base name of libraries to link to"), cl::value_desc("lib"));
Reid Spencer034a5442004-08-10 16:26:01 +0000127
Reid Spencer7c14fd12004-08-30 06:27:32 +0000128cl::list<std::string> Includes("I", cl::Prefix,
Misha Brukman3da94ae2005-04-22 00:00:37 +0000129 cl::desc("Specify location to search for included source"),
Reid Spencer5443af22005-02-18 20:00:05 +0000130 cl::value_desc("dir"));
Reid Spencer7c14fd12004-08-30 06:27:32 +0000131
132cl::list<std::string> Defines("D", cl::Prefix,
Reid Spencer5443af22005-02-18 20:00:05 +0000133 cl::desc("Specify a pre-processor symbol to define"),
134 cl::value_desc("symbol"));
Reid Spencer7c14fd12004-08-30 06:27:32 +0000135
Reid Spencer034a5442004-08-10 16:26:01 +0000136//===------------------------------------------------------------------------===
137//=== OUTPUT OPTIONS
138//===------------------------------------------------------------------------===
139
Misha Brukman3da94ae2005-04-22 00:00:37 +0000140cl::opt<std::string> OutputFilename("o",
Reid Spencer5443af22005-02-18 20:00:05 +0000141 cl::desc("Override output filename"), cl::value_desc("file"));
Reid Spencer034a5442004-08-10 16:26:01 +0000142
Reid Spencerbae68252004-08-19 04:49:47 +0000143cl::opt<std::string> OutputMachine("m", cl::Prefix,
Reid Spencer034a5442004-08-10 16:26:01 +0000144 cl::desc("Specify a target machine"), cl::value_desc("machine"));
Reid Spencer2967b612004-10-28 03:56:16 +0000145
Reid Spencerbae68252004-08-19 04:49:47 +0000146cl::opt<bool> Native("native", cl::init(false),
Reid Spencer5443af22005-02-18 20:00:05 +0000147 cl::desc("Generative native code instead of bytecode"));
Reid Spencer034a5442004-08-10 16:26:01 +0000148
Reid Spencer7c14fd12004-08-30 06:27:32 +0000149cl::opt<bool> DebugOutput("g", cl::init(false),
150 cl::desc("Generate objects that include debug symbols"));
151
Reid Spencer54fafe42004-09-14 01:58:45 +0000152cl::opt<bool> StripOutput("strip", cl::init(false),
153 cl::desc("Strip all symbols from linked output file"));
154
Reid Spencer5443af22005-02-18 20:00:05 +0000155cl::opt<std::string> PrintFileName("print-fname", cl::Optional,
156 cl::value_desc("file"),
Reid Spencer65bc4e02004-11-23 23:47:58 +0000157 cl::desc("Print the full path for the option's value"));
158
Reid Spencer034a5442004-08-10 16:26:01 +0000159//===------------------------------------------------------------------------===
160//=== INFORMATION OPTIONS
161//===------------------------------------------------------------------------===
162
Reid Spencerbae68252004-08-19 04:49:47 +0000163cl::opt<bool> DryRun("dry-run", cl::Optional, cl::init(false),
Reid Spencer5c56dc12004-08-13 20:22:43 +0000164 cl::desc("Do everything but perform the compilation actions"));
Reid Spencer034a5442004-08-10 16:26:01 +0000165
Reid Spencerbae68252004-08-19 04:49:47 +0000166cl::alias DryRunAlias("y", cl::Optional,
Reid Spencer5c56dc12004-08-13 20:22:43 +0000167 cl::desc("Alias for -dry-run"), cl::aliasopt(DryRun));
Reid Spencer034a5442004-08-10 16:26:01 +0000168
Reid Spencerbae68252004-08-19 04:49:47 +0000169cl::opt<bool> Verbose("verbose", cl::Optional, cl::init(false),
Reid Spencer034a5442004-08-10 16:26:01 +0000170 cl::desc("Print out each action taken"));
171
Misha Brukman3da94ae2005-04-22 00:00:37 +0000172cl::alias VerboseAlias("v", cl::Optional,
Reid Spencer034a5442004-08-10 16:26:01 +0000173 cl::desc("Alias for -verbose"), cl::aliasopt(Verbose));
174
Misha Brukman3da94ae2005-04-22 00:00:37 +0000175cl::opt<bool> Debug("debug", cl::Optional, cl::init(false),
Reid Spencer5c56dc12004-08-13 20:22:43 +0000176 cl::Hidden, cl::desc("Print out debugging information"));
177
Reid Spencerbae68252004-08-19 04:49:47 +0000178cl::alias DebugAlias("d", cl::Optional,
Reid Spencer5c56dc12004-08-13 20:22:43 +0000179 cl::desc("Alias for -debug"), cl::aliasopt(Debug));
180
Reid Spencerbae68252004-08-19 04:49:47 +0000181cl::opt<bool> TimeActions("time-actions", cl::Optional, cl::init(false),
Reid Spencer034a5442004-08-10 16:26:01 +0000182 cl::desc("Print execution time for each action taken"));
183
Reid Spencerbae68252004-08-19 04:49:47 +0000184cl::opt<bool> ShowStats("stats", cl::Optional, cl::init(false),
185 cl::desc("Print statistics accumulated during optimization"));
186
Reid Spencer034a5442004-08-10 16:26:01 +0000187//===------------------------------------------------------------------------===
188//=== ADVANCED OPTIONS
189//===------------------------------------------------------------------------===
190
Reid Spencer5c56dc12004-08-13 20:22:43 +0000191static cl::opt<std::string> ConfigDir("config-dir", cl::Optional,
Reid Spencer5443af22005-02-18 20:00:05 +0000192 cl::desc("Specify configuration directory to override defaults"),
193 cl::value_desc("dir"));
Reid Spencer034a5442004-08-10 16:26:01 +0000194
Reid Spencerbf437722004-08-15 08:19:46 +0000195static cl::opt<bool> EmitRawCode("emit-raw-code", cl::Hidden, cl::Optional,
Reid Spencer034a5442004-08-10 16:26:01 +0000196 cl::desc("Emit raw, unoptimized code"));
197
Reid Spencerbf437722004-08-15 08:19:46 +0000198static cl::opt<bool> PipeCommands("pipe", cl::Optional,
199 cl::desc("Invoke sub-commands by linking input/output with pipes"));
200
Reid Spencer52c2dc12004-08-29 19:26:56 +0000201static cl::opt<bool> KeepTemps("keep-temps", cl::Optional,
Reid Spencer5443af22005-02-18 20:00:05 +0000202 cl::desc("Don't delete temporary files created by llvmc"));
Reid Spencerbae68252004-08-19 04:49:47 +0000203
Reid Spencer034a5442004-08-10 16:26:01 +0000204//===------------------------------------------------------------------------===
205//=== POSITIONAL OPTIONS
206//===------------------------------------------------------------------------===
207
Reid Spencer65bc4e02004-11-23 23:47:58 +0000208static cl::list<std::string> Files(cl::Positional, cl::ZeroOrMore,
Reid Spencer5c56dc12004-08-13 20:22:43 +0000209 cl::desc("[Sources/objects/libraries]"));
210
211static cl::list<std::string> Languages("x", cl::ZeroOrMore,
212 cl::desc("Specify the source language for subsequent files"),
213 cl::value_desc("language"));
214
215//===------------------------------------------------------------------------===
216//=== GetFileType - determine type of a file
217//===------------------------------------------------------------------------===
218const std::string GetFileType(const std::string& fname, unsigned pos ) {
219 static std::vector<std::string>::iterator langIt = Languages.begin();
220 static std::string CurrLang = "";
221
222 // If a -x LANG option has been specified ..
223 if ( langIt != Languages.end() )
224 // If the -x LANG option came before the current file on command line
225 if ( Languages.getPosition( langIt - Languages.begin() ) < pos ) {
226 // use that language
227 CurrLang = *langIt++;
228 return CurrLang;
229 }
230
231 // If there's a current language in effect
232 if (!CurrLang.empty())
233 return CurrLang; // use that language
234
235 // otherwise just determine lang from the filename's suffix
236 return fname.substr( fname.rfind('.',fname.size()) + 1 );
237}
238
239} // end anonymous namespace
Reid Spencer034a5442004-08-10 16:26:01 +0000240
Reid Spencer65bc4e02004-11-23 23:47:58 +0000241void handleTerminatingOptions(CompilerDriver* CD) {
242 if (!PrintFileName.empty()) {
243 sys::Path path = CD->GetPathForLinkageItem(PrintFileName,false);
Reid Spencer1fce0912004-12-11 00:14:15 +0000244 std::string p = path.toString();
Reid Spencer65bc4e02004-11-23 23:47:58 +0000245 if (p.empty())
246 std::cout << "Can't locate '" << PrintFileName << "'.\n";
247 else
248 std::cout << p << "\n";
249 exit(0);
250 }
251}
Reid Spencer034a5442004-08-10 16:26:01 +0000252
253/// @brief The main program for llvmc
254int main(int argc, char **argv) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000255 // Make sure we print stack trace if we get bad signals
256 sys::PrintStackTraceOnErrorSignal();
257
Reid Spencerbae68252004-08-19 04:49:47 +0000258 try {
Reid Spencer034a5442004-08-10 16:26:01 +0000259
Reid Spencerbae68252004-08-19 04:49:47 +0000260 // Parse the command line options
Misha Brukman3da94ae2005-04-22 00:00:37 +0000261 cl::ParseCommandLineOptions(argc, argv,
Reid Spencer52c2dc12004-08-29 19:26:56 +0000262 " LLVM Compiler Driver (llvmc)\n\n"
Reid Spencerbae68252004-08-19 04:49:47 +0000263 " This program provides easy invocation of the LLVM tool set\n"
Reid Spencer52c2dc12004-08-29 19:26:56 +0000264 " and other compiler tools.\n"
Reid Spencerbae68252004-08-19 04:49:47 +0000265 );
Reid Spencer034a5442004-08-10 16:26:01 +0000266
Reid Spencerbae68252004-08-19 04:49:47 +0000267 // Deal with unimplemented options.
268 if (PipeCommands)
Reid Spencer63860092004-08-30 00:06:52 +0000269 throw std::string("Not implemented yet: -pipe");
Reid Spencer034a5442004-08-10 16:26:01 +0000270
Reid Spencer2cf17a42004-08-24 14:05:30 +0000271 if (OutputFilename.empty())
272 if (OptLevel == CompilerDriver::LINKING)
273 OutputFilename = "a.out";
Reid Spencer2cf17a42004-08-24 14:05:30 +0000274
Reid Spencerbae68252004-08-19 04:49:47 +0000275 // Construct the ConfigDataProvider object
276 LLVMC_ConfigDataProvider Provider;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000277 Provider.setConfigDir(sys::Path(ConfigDir));
Reid Spencer5c56dc12004-08-13 20:22:43 +0000278
Reid Spencerbae68252004-08-19 04:49:47 +0000279 // Construct the CompilerDriver object
Reid Spencer52c2dc12004-08-29 19:26:56 +0000280 CompilerDriver* CD = CompilerDriver::Get(Provider);
Reid Spencer5c56dc12004-08-13 20:22:43 +0000281
Reid Spenceraff3c772004-08-24 22:53:13 +0000282 // If the LLVM_LIB_SEARCH_PATH environment variable is
283 // set, append it to the list of places to search for libraries
284 std::string srchPath = getenv("LLVM_LIB_SEARCH_PATH");
285 if (!srchPath.empty())
286 LibPaths.push_back(srchPath);
287
Reid Spencer52c2dc12004-08-29 19:26:56 +0000288 // Set the driver flags based on command line options
289 unsigned flags = 0;
290 if (Verbose) flags |= CompilerDriver::VERBOSE_FLAG;
291 if (Debug) flags |= CompilerDriver::DEBUG_FLAG;
292 if (DryRun) flags |= CompilerDriver::DRY_RUN_FLAG;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000293 if (Native) flags |= CompilerDriver::EMIT_NATIVE_FLAG;
294 if (EmitRawCode) flags |= CompilerDriver::EMIT_RAW_FLAG;
295 if (KeepTemps) flags |= CompilerDriver::KEEP_TEMPS_FLAG;
296 if (ShowStats) flags |= CompilerDriver::SHOW_STATS_FLAG;
297 if (TimeActions) flags |= CompilerDriver::TIME_ACTIONS_FLAG;
298 if (TimePassesIsEnabled) flags |= CompilerDriver::TIME_PASSES_FLAG;
Reid Spencer54fafe42004-09-14 01:58:45 +0000299 if (StripOutput) flags |= CompilerDriver::STRIP_OUTPUT_FLAG;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000300 CD->setDriverFlags(flags);
301
302 // Specify requred parameters
303 CD->setFinalPhase(FinalPhase);
304 CD->setOptimization(OptLevel);
305 CD->setOutputMachine(OutputMachine);
Reid Spencer7c14fd12004-08-30 06:27:32 +0000306 CD->setIncludePaths(Includes);
307 CD->setSymbolDefines(Defines);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000308 CD->setLibraryPaths(LibPaths);
Reid Spencer54fafe42004-09-14 01:58:45 +0000309 CD->setfPassThrough(fOpts);
310 CD->setMPassThrough(MOpts);
311 CD->setWPassThrough(WOpts);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000312
313 // Provide additional tool arguments
Reid Spencerbae68252004-08-19 04:49:47 +0000314 if (!PreprocessorToolOpts.empty())
Reid Spencer52c2dc12004-08-29 19:26:56 +0000315 CD->setPhaseArgs(CompilerDriver::PREPROCESSING, PreprocessorToolOpts);
Reid Spencerbae68252004-08-19 04:49:47 +0000316 if (!TranslatorToolOpts.empty())
Reid Spencer52c2dc12004-08-29 19:26:56 +0000317 CD->setPhaseArgs(CompilerDriver::TRANSLATION, TranslatorToolOpts);
Reid Spencerbae68252004-08-19 04:49:47 +0000318 if (!OptimizerToolOpts.empty())
Reid Spencer52c2dc12004-08-29 19:26:56 +0000319 CD->setPhaseArgs(CompilerDriver::OPTIMIZATION, OptimizerToolOpts);
Reid Spencerbae68252004-08-19 04:49:47 +0000320 if (!AssemblerToolOpts.empty())
Reid Spencer52c2dc12004-08-29 19:26:56 +0000321 CD->setPhaseArgs(CompilerDriver::ASSEMBLY,AssemblerToolOpts);
Reid Spencerbae68252004-08-19 04:49:47 +0000322 if (!LinkerToolOpts.empty())
Reid Spencer52c2dc12004-08-29 19:26:56 +0000323 CD->setPhaseArgs(CompilerDriver::LINKING, LinkerToolOpts);
Reid Spencer5c56dc12004-08-13 20:22:43 +0000324
Reid Spencer65bc4e02004-11-23 23:47:58 +0000325 // Check for options that cause us to terminate before any significant work
326 // is done.
327 handleTerminatingOptions(CD);
328
Reid Spencerbae68252004-08-19 04:49:47 +0000329 // Prepare the list of files to be compiled by the CompilerDriver.
330 CompilerDriver::InputList InpList;
331 std::vector<std::string>::iterator fileIt = Files.begin();
332 std::vector<std::string>::iterator libIt = Libraries.begin();
333 unsigned libPos = 0, filePos = 0;
334 while ( 1 ) {
335 if ( libIt != Libraries.end() )
336 libPos = Libraries.getPosition( libIt - Libraries.begin() );
337 else
338 libPos = 0;
339 if ( fileIt != Files.end() )
340 filePos = Files.getPosition( fileIt - Files.begin() );
341 else
342 filePos = 0;
Reid Spencer5c56dc12004-08-13 20:22:43 +0000343
Reid Spencerbae68252004-08-19 04:49:47 +0000344 if ( filePos != 0 && (libPos == 0 || filePos < libPos) ) {
345 // Add a source file
346 InpList.push_back( std::make_pair(*fileIt, GetFileType(*fileIt,filePos)));
347 ++fileIt;
348 }
349 else if ( libPos != 0 && (filePos == 0 || libPos < filePos) ) {
350 // Add a library
351 InpList.push_back( std::make_pair(*libIt++,""));
352 }
353 else
354 break; // we're done with the list
Reid Spencer5c56dc12004-08-13 20:22:43 +0000355 }
Reid Spencerbae68252004-08-19 04:49:47 +0000356
357 // Tell the driver to do its thing
Reid Spencer52c2dc12004-08-29 19:26:56 +0000358 int result = CD->execute(InpList,sys::Path(OutputFilename));
Reid Spencerbae68252004-08-19 04:49:47 +0000359 if (result != 0) {
Reid Spencer63860092004-08-30 00:06:52 +0000360 throw std::string("Error executing actions. Terminated.");
Reid Spencerbae68252004-08-19 04:49:47 +0000361 return result;
Reid Spencer5c56dc12004-08-13 20:22:43 +0000362 }
Reid Spencer034a5442004-08-10 16:26:01 +0000363
Reid Spencerbae68252004-08-19 04:49:47 +0000364 // All is good, return success
365 return 0;
Reid Spencera3b4e092004-09-03 22:59:32 +0000366 } catch (const std::string& msg) {
Reid Spencer2cf17a42004-08-24 14:05:30 +0000367 std::cerr << argv[0] << ": " << msg << "\n";
Reid Spencerbae68252004-08-19 04:49:47 +0000368 } catch (...) {
Reid Spencer2cf17a42004-08-24 14:05:30 +0000369 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Reid Spencer034a5442004-08-10 16:26:01 +0000370 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000371 return 1;
Reid Spencer034a5442004-08-10 16:26:01 +0000372}