blob: c87c6523f1520308ea0ae6eec1115c0a8fdaccff [file] [log] [blame]
Reid Spencer5c56dc12004-08-13 20:22:43 +00001//===--- llvmc.cpp - The LLVM Compiler Driver -------------------*- C++ -*-===//
Reid Spencer034a5442004-08-10 16:26:01 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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.
14//
15//===------------------------------------------------------------------------===
16
Reid Spencerabf1ce32004-08-10 16:29:18 +000017#include "CompilerDriver.h"
Reid Spencerf51a87c2004-08-19 21:52:49 +000018#include "Configuration.h"
Reid Spencer034a5442004-08-10 16:26:01 +000019#include "llvm/System/Signals.h"
20#include "Support/CommandLine.h"
21#include <iostream>
22
23using namespace llvm;
24
Reid Spencer5c56dc12004-08-13 20:22:43 +000025namespace {
Reid Spencer034a5442004-08-10 16:26:01 +000026//===------------------------------------------------------------------------===
27//=== PHASE OPTIONS
28//===------------------------------------------------------------------------===
Reid Spencerbae68252004-08-19 04:49:47 +000029cl::opt<CompilerDriver::Phases> FinalPhase(
Reid Spencer034a5442004-08-10 16:26:01 +000030 cl::desc("Choose final phase of compilation:"),
Reid Spencer5c56dc12004-08-13 20:22:43 +000031 cl::init(CompilerDriver::LINKING),
Reid Spencer034a5442004-08-10 16:26:01 +000032 cl::values(
33 clEnumValN(CompilerDriver::PREPROCESSING,"E",
Reid Spencer5c56dc12004-08-13 20:22:43 +000034 "Stop compilation after pre-processing phase"),
35 clEnumValN(CompilerDriver::TRANSLATION, "t",
36 "Stop compilation after translation phase"),
Reid Spencer034a5442004-08-10 16:26:01 +000037 clEnumValN(CompilerDriver::OPTIMIZATION,"c",
Reid Spencer5c56dc12004-08-13 20:22:43 +000038 "Stop compilation after optimization phase"),
Reid Spencer034a5442004-08-10 16:26:01 +000039 clEnumValN(CompilerDriver::ASSEMBLY,"S",
Reid Spencer5c56dc12004-08-13 20:22:43 +000040 "Stop compilation after assembly phase"),
Reid Spencer034a5442004-08-10 16:26:01 +000041 clEnumValEnd
42 )
43);
44
45//===------------------------------------------------------------------------===
46//=== OPTIMIZATION OPTIONS
47//===------------------------------------------------------------------------===
Reid Spencerbae68252004-08-19 04:49:47 +000048cl::opt<CompilerDriver::OptimizationLevels> OptLevel(
Reid Spencer034a5442004-08-10 16:26:01 +000049 cl::desc("Choose level of optimization to apply:"),
Reid Spencer5c56dc12004-08-13 20:22:43 +000050 cl::init(CompilerDriver::OPT_FAST_COMPILE),
Reid Spencer034a5442004-08-10 16:26:01 +000051 cl::values(
52 clEnumValN(CompilerDriver::OPT_FAST_COMPILE,"O0",
Reid Spencer5c56dc12004-08-13 20:22:43 +000053 "An alias for the -O1 option."),
Reid Spencer034a5442004-08-10 16:26:01 +000054 clEnumValN(CompilerDriver::OPT_FAST_COMPILE,"O1",
55 "Optimize for compilation speed, not execution speed."),
56 clEnumValN(CompilerDriver::OPT_SIMPLE,"O2",
57 "Perform simple translation time optimizations"),
58 clEnumValN(CompilerDriver::OPT_AGGRESSIVE,"O3",
59 "Perform aggressive translation time optimizations"),
60 clEnumValN(CompilerDriver::OPT_LINK_TIME,"O4",
61 "Perform link time optimizations"),
62 clEnumValN(CompilerDriver::OPT_AGGRESSIVE_LINK_TIME,"O5",
63 "Perform aggressive link time optimizations"),
64 clEnumValEnd
65 )
66);
67
68//===------------------------------------------------------------------------===
69//=== TOOL OPTIONS
70//===------------------------------------------------------------------------===
71
Reid Spencerbae68252004-08-19 04:49:47 +000072cl::list<std::string> PreprocessorToolOpts("Tpre", cl::ZeroOrMore,
Reid Spencer5c56dc12004-08-13 20:22:43 +000073 cl::desc("Pass specific options to the pre-processor"),
74 cl::value_desc("option"));
Reid Spencer034a5442004-08-10 16:26:01 +000075
Reid Spencerbae68252004-08-19 04:49:47 +000076cl::list<std::string> TranslatorToolOpts("Ttrn", cl::ZeroOrMore,
Reid Spencer5c56dc12004-08-13 20:22:43 +000077 cl::desc("Pass specific options to the assembler"),
78 cl::value_desc("option"));
Reid Spencer034a5442004-08-10 16:26:01 +000079
Reid Spencerbae68252004-08-19 04:49:47 +000080cl::list<std::string> AssemblerToolOpts("Tasm", cl::ZeroOrMore,
Reid Spencerbf437722004-08-15 08:19:46 +000081 cl::desc("Pass specific options to the assembler"),
82 cl::value_desc("option"));
83
Reid Spencerbae68252004-08-19 04:49:47 +000084cl::list<std::string> OptimizerToolOpts("Topt", cl::ZeroOrMore,
Reid Spencer5c56dc12004-08-13 20:22:43 +000085 cl::desc("Pass specific options to the optimizer"),
86 cl::value_desc("option"));
Reid Spencer034a5442004-08-10 16:26:01 +000087
Reid Spencerbae68252004-08-19 04:49:47 +000088cl::list<std::string> LinkerToolOpts("Tlnk", cl::ZeroOrMore,
Reid Spencer5c56dc12004-08-13 20:22:43 +000089 cl::desc("Pass specific options to the linker"),
90 cl::value_desc("option"));
Reid Spencer034a5442004-08-10 16:26:01 +000091
92//===------------------------------------------------------------------------===
93//=== INPUT OPTIONS
94//===------------------------------------------------------------------------===
95
Reid Spencerbae68252004-08-19 04:49:47 +000096cl::list<std::string> LibPaths("L", cl::Prefix,
Reid Spencer034a5442004-08-10 16:26:01 +000097 cl::desc("Specify a library search path"), cl::value_desc("directory"));
98
Reid Spencerbae68252004-08-19 04:49:47 +000099cl::list<std::string> Libraries("l", cl::Prefix,
Reid Spencer034a5442004-08-10 16:26:01 +0000100 cl::desc("Specify libraries to link to"), cl::value_desc("library prefix"));
101
102
103//===------------------------------------------------------------------------===
104//=== OUTPUT OPTIONS
105//===------------------------------------------------------------------------===
106
Reid Spencerbae68252004-08-19 04:49:47 +0000107cl::opt<std::string> OutputFilename("o",
Reid Spencer034a5442004-08-10 16:26:01 +0000108 cl::desc("Override output filename"), cl::value_desc("filename"));
109
Reid Spencerbae68252004-08-19 04:49:47 +0000110cl::opt<std::string> OutputMachine("m", cl::Prefix,
Reid Spencer034a5442004-08-10 16:26:01 +0000111 cl::desc("Specify a target machine"), cl::value_desc("machine"));
112
Reid Spencerbae68252004-08-19 04:49:47 +0000113cl::opt<bool> Native("native", cl::init(false),
Reid Spencer034a5442004-08-10 16:26:01 +0000114 cl::desc("Generative native object and executables instead of bytecode"));
115
116//===------------------------------------------------------------------------===
117//=== INFORMATION OPTIONS
118//===------------------------------------------------------------------------===
119
Reid Spencerbae68252004-08-19 04:49:47 +0000120cl::opt<bool> DryRun("dry-run", cl::Optional, cl::init(false),
Reid Spencer5c56dc12004-08-13 20:22:43 +0000121 cl::desc("Do everything but perform the compilation actions"));
Reid Spencer034a5442004-08-10 16:26:01 +0000122
Reid Spencerbae68252004-08-19 04:49:47 +0000123cl::alias DryRunAlias("y", cl::Optional,
Reid Spencer5c56dc12004-08-13 20:22:43 +0000124 cl::desc("Alias for -dry-run"), cl::aliasopt(DryRun));
Reid Spencer034a5442004-08-10 16:26:01 +0000125
Reid Spencerbae68252004-08-19 04:49:47 +0000126cl::opt<bool> Verbose("verbose", cl::Optional, cl::init(false),
Reid Spencer034a5442004-08-10 16:26:01 +0000127 cl::desc("Print out each action taken"));
128
Reid Spencerbae68252004-08-19 04:49:47 +0000129cl::alias VerboseAlias("v", cl::Optional,
Reid Spencer034a5442004-08-10 16:26:01 +0000130 cl::desc("Alias for -verbose"), cl::aliasopt(Verbose));
131
Reid Spencerbae68252004-08-19 04:49:47 +0000132cl::opt<bool> Debug("debug", cl::Optional, cl::init(false),
Reid Spencer5c56dc12004-08-13 20:22:43 +0000133 cl::Hidden, cl::desc("Print out debugging information"));
134
Reid Spencerbae68252004-08-19 04:49:47 +0000135cl::alias DebugAlias("d", cl::Optional,
Reid Spencer5c56dc12004-08-13 20:22:43 +0000136 cl::desc("Alias for -debug"), cl::aliasopt(Debug));
137
Reid Spencerbae68252004-08-19 04:49:47 +0000138cl::opt<bool> TimeActions("time-actions", cl::Optional, cl::init(false),
Reid Spencer034a5442004-08-10 16:26:01 +0000139 cl::desc("Print execution time for each action taken"));
140
Reid Spencerbae68252004-08-19 04:49:47 +0000141cl::opt<bool> TimePasses("time-passes", cl::Optional, cl::init(false),
142 cl::desc("Print execution time for each optimization pass"));
143
144cl::opt<bool> ShowStats("stats", cl::Optional, cl::init(false),
145 cl::desc("Print statistics accumulated during optimization"));
146
Reid Spencer034a5442004-08-10 16:26:01 +0000147//===------------------------------------------------------------------------===
148//=== ADVANCED OPTIONS
149//===------------------------------------------------------------------------===
150
Reid Spencer5c56dc12004-08-13 20:22:43 +0000151static cl::opt<std::string> ConfigDir("config-dir", cl::Optional,
152 cl::desc("Specify a configuration directory to override defaults"),
153 cl::value_desc("directory"));
Reid Spencer034a5442004-08-10 16:26:01 +0000154
Reid Spencerbf437722004-08-15 08:19:46 +0000155static cl::opt<bool> EmitRawCode("emit-raw-code", cl::Hidden, cl::Optional,
Reid Spencer034a5442004-08-10 16:26:01 +0000156 cl::desc("Emit raw, unoptimized code"));
157
Reid Spencerbf437722004-08-15 08:19:46 +0000158static cl::opt<bool> PipeCommands("pipe", cl::Optional,
159 cl::desc("Invoke sub-commands by linking input/output with pipes"));
160
Reid Spencerbae68252004-08-19 04:49:47 +0000161static cl::opt<bool> KeepTemporaries("keep-temps", cl::Optional,
162 cl::desc("Don't delete the temporary files created during compilation"));
163
Reid Spencer034a5442004-08-10 16:26:01 +0000164//===------------------------------------------------------------------------===
165//=== POSITIONAL OPTIONS
166//===------------------------------------------------------------------------===
167
168static cl::list<std::string> Files(cl::Positional, cl::OneOrMore,
Reid Spencer5c56dc12004-08-13 20:22:43 +0000169 cl::desc("[Sources/objects/libraries]"));
170
171static cl::list<std::string> Languages("x", cl::ZeroOrMore,
172 cl::desc("Specify the source language for subsequent files"),
173 cl::value_desc("language"));
174
175//===------------------------------------------------------------------------===
176//=== GetFileType - determine type of a file
177//===------------------------------------------------------------------------===
178const std::string GetFileType(const std::string& fname, unsigned pos ) {
179 static std::vector<std::string>::iterator langIt = Languages.begin();
180 static std::string CurrLang = "";
181
182 // If a -x LANG option has been specified ..
183 if ( langIt != Languages.end() )
184 // If the -x LANG option came before the current file on command line
185 if ( Languages.getPosition( langIt - Languages.begin() ) < pos ) {
186 // use that language
187 CurrLang = *langIt++;
188 return CurrLang;
189 }
190
191 // If there's a current language in effect
192 if (!CurrLang.empty())
193 return CurrLang; // use that language
194
195 // otherwise just determine lang from the filename's suffix
196 return fname.substr( fname.rfind('.',fname.size()) + 1 );
197}
198
199} // end anonymous namespace
Reid Spencer034a5442004-08-10 16:26:01 +0000200
201
202/// @brief The main program for llvmc
203int main(int argc, char **argv) {
Reid Spencerbae68252004-08-19 04:49:47 +0000204 try {
205 // Make sure we print stack trace if we get bad signals
206 PrintStackTraceOnErrorSignal();
Reid Spencer034a5442004-08-10 16:26:01 +0000207
Reid Spencerbae68252004-08-19 04:49:47 +0000208 // Parse the command line options
209 cl::ParseCommandLineOptions(argc, argv,
210 " LLVM Compilation Driver (llvmc)\n\n"
211 " This program provides easy invocation of the LLVM tool set\n"
212 " and source language compiler tools.\n"
213 );
Reid Spencer034a5442004-08-10 16:26:01 +0000214
Reid Spencerbae68252004-08-19 04:49:47 +0000215 // Deal with unimplemented options.
216 if (PipeCommands)
Reid Spencer2cf17a42004-08-24 14:05:30 +0000217 throw "Not implemented yet: -pipe";
Reid Spencer034a5442004-08-10 16:26:01 +0000218
Reid Spencer2cf17a42004-08-24 14:05:30 +0000219 if (OutputFilename.empty())
220 if (OptLevel == CompilerDriver::LINKING)
221 OutputFilename = "a.out";
222 else
223 throw "An output file must be specified. Please use the -o option";
224
Reid Spencer5c56dc12004-08-13 20:22:43 +0000225
Reid Spencerbae68252004-08-19 04:49:47 +0000226 // Construct the ConfigDataProvider object
227 LLVMC_ConfigDataProvider Provider;
228 Provider.setConfigDir(ConfigDir);
Reid Spencer5c56dc12004-08-13 20:22:43 +0000229
Reid Spencerbae68252004-08-19 04:49:47 +0000230 // Construct the CompilerDriver object
231 CompilerDriver CD(Provider);
Reid Spencer5c56dc12004-08-13 20:22:43 +0000232
Reid Spencerbae68252004-08-19 04:49:47 +0000233 // Configure the driver based on options
234 CD.setVerbose(Verbose);
235 CD.setDebug(Debug);
236 CD.setDryRun(DryRun);
237 CD.setFinalPhase(FinalPhase);
238 CD.setOptimization(OptLevel);
239 CD.setOutputMachine(OutputMachine);
240 CD.setEmitNativeCode(Native);
241 CD.setEmitRawCode(EmitRawCode);
242 CD.setTimeActions(TimeActions);
243 CD.setTimePasses(TimePasses);
244 CD.setShowStats(ShowStats);
245 CD.setKeepTemporaries(KeepTemporaries);
246 CD.setLibraryPaths(LibPaths);
247 if (!PreprocessorToolOpts.empty())
248 CD.setPhaseArgs(CompilerDriver::PREPROCESSING, PreprocessorToolOpts);
249 if (!TranslatorToolOpts.empty())
250 CD.setPhaseArgs(CompilerDriver::TRANSLATION, TranslatorToolOpts);
251 if (!OptimizerToolOpts.empty())
252 CD.setPhaseArgs(CompilerDriver::OPTIMIZATION, OptimizerToolOpts);
253 if (!AssemblerToolOpts.empty())
254 CD.setPhaseArgs(CompilerDriver::ASSEMBLY,AssemblerToolOpts);
255 if (!LinkerToolOpts.empty())
256 CD.setPhaseArgs(CompilerDriver::LINKING, LinkerToolOpts);
Reid Spencer5c56dc12004-08-13 20:22:43 +0000257
Reid Spencerbae68252004-08-19 04:49:47 +0000258 // Prepare the list of files to be compiled by the CompilerDriver.
259 CompilerDriver::InputList InpList;
260 std::vector<std::string>::iterator fileIt = Files.begin();
261 std::vector<std::string>::iterator libIt = Libraries.begin();
262 unsigned libPos = 0, filePos = 0;
263 while ( 1 ) {
264 if ( libIt != Libraries.end() )
265 libPos = Libraries.getPosition( libIt - Libraries.begin() );
266 else
267 libPos = 0;
268 if ( fileIt != Files.end() )
269 filePos = Files.getPosition( fileIt - Files.begin() );
270 else
271 filePos = 0;
Reid Spencer5c56dc12004-08-13 20:22:43 +0000272
Reid Spencerbae68252004-08-19 04:49:47 +0000273 if ( filePos != 0 && (libPos == 0 || filePos < libPos) ) {
274 // Add a source file
275 InpList.push_back( std::make_pair(*fileIt, GetFileType(*fileIt,filePos)));
276 ++fileIt;
277 }
278 else if ( libPos != 0 && (filePos == 0 || libPos < filePos) ) {
279 // Add a library
280 InpList.push_back( std::make_pair(*libIt++,""));
281 }
282 else
283 break; // we're done with the list
Reid Spencer5c56dc12004-08-13 20:22:43 +0000284 }
Reid Spencerbae68252004-08-19 04:49:47 +0000285
286 // Tell the driver to do its thing
287 int result = CD.execute(InpList,OutputFilename);
288 if (result != 0) {
Reid Spencer2cf17a42004-08-24 14:05:30 +0000289 throw "Error executing actions. Terminated.\n";
Reid Spencerbae68252004-08-19 04:49:47 +0000290 return result;
Reid Spencer5c56dc12004-08-13 20:22:43 +0000291 }
Reid Spencer034a5442004-08-10 16:26:01 +0000292
Reid Spencerbae68252004-08-19 04:49:47 +0000293 // All is good, return success
294 return 0;
295 } catch (std::string& msg) {
Reid Spencer2cf17a42004-08-24 14:05:30 +0000296 std::cerr << argv[0] << ": " << msg << "\n";
Reid Spencerbae68252004-08-19 04:49:47 +0000297 } catch (...) {
Reid Spencer2cf17a42004-08-24 14:05:30 +0000298 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Reid Spencer034a5442004-08-10 16:26:01 +0000299 }
Reid Spencer034a5442004-08-10 16:26:01 +0000300}