blob: ad0d2187aaea18d2e0172ac4b8d90698fe06b6b6 [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 Spencer44956322004-08-24 17:54:26 +000019#include "llvm/Pass.h"
Reid Spencer034a5442004-08-10 16:26:01 +000020#include "llvm/System/Signals.h"
21#include "Support/CommandLine.h"
22#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 Spencerbae68252004-08-19 04:49:47 +000030cl::opt<CompilerDriver::Phases> FinalPhase(
Reid Spencer034a5442004-08-10 16:26:01 +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 Spencerbae68252004-08-19 04:49:47 +000049cl::opt<CompilerDriver::OptimizationLevels> OptLevel(
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,
Reid Spencer5c56dc12004-08-13 20:22:43 +000074 cl::desc("Pass specific options to the pre-processor"),
75 cl::value_desc("option"));
Reid Spencer034a5442004-08-10 16:26:01 +000076
Reid Spencerbae68252004-08-19 04:49:47 +000077cl::list<std::string> TranslatorToolOpts("Ttrn", cl::ZeroOrMore,
Reid Spencer5c56dc12004-08-13 20:22:43 +000078 cl::desc("Pass specific options to the assembler"),
79 cl::value_desc("option"));
Reid Spencer034a5442004-08-10 16:26:01 +000080
Reid Spencerbae68252004-08-19 04:49:47 +000081cl::list<std::string> AssemblerToolOpts("Tasm", cl::ZeroOrMore,
Reid Spencerbf437722004-08-15 08:19:46 +000082 cl::desc("Pass specific options to the assembler"),
83 cl::value_desc("option"));
84
Reid Spencerbae68252004-08-19 04:49:47 +000085cl::list<std::string> OptimizerToolOpts("Topt", cl::ZeroOrMore,
Reid Spencer5c56dc12004-08-13 20:22:43 +000086 cl::desc("Pass specific options to the optimizer"),
87 cl::value_desc("option"));
Reid Spencer034a5442004-08-10 16:26:01 +000088
Reid Spencerbae68252004-08-19 04:49:47 +000089cl::list<std::string> LinkerToolOpts("Tlnk", cl::ZeroOrMore,
Reid Spencer5c56dc12004-08-13 20:22:43 +000090 cl::desc("Pass specific options to the linker"),
91 cl::value_desc("option"));
Reid Spencer034a5442004-08-10 16:26:01 +000092
93//===------------------------------------------------------------------------===
94//=== INPUT OPTIONS
95//===------------------------------------------------------------------------===
96
Reid Spencerbae68252004-08-19 04:49:47 +000097cl::list<std::string> LibPaths("L", cl::Prefix,
Reid Spencer034a5442004-08-10 16:26:01 +000098 cl::desc("Specify a library search path"), cl::value_desc("directory"));
99
Reid Spencerbae68252004-08-19 04:49:47 +0000100cl::list<std::string> Libraries("l", cl::Prefix,
Reid Spencer034a5442004-08-10 16:26:01 +0000101 cl::desc("Specify libraries to link to"), cl::value_desc("library prefix"));
102
103
104//===------------------------------------------------------------------------===
105//=== OUTPUT OPTIONS
106//===------------------------------------------------------------------------===
107
Reid Spencerbae68252004-08-19 04:49:47 +0000108cl::opt<std::string> OutputFilename("o",
Reid Spencer034a5442004-08-10 16:26:01 +0000109 cl::desc("Override output filename"), cl::value_desc("filename"));
110
Reid Spencer52c2dc12004-08-29 19:26:56 +0000111cl::opt<bool> ForceOutput("f", cl::Optional, cl::init(false),
112 cl::desc("Force output files to be overridden"));
113
Reid Spencerbae68252004-08-19 04:49:47 +0000114cl::opt<std::string> OutputMachine("m", cl::Prefix,
Reid Spencer034a5442004-08-10 16:26:01 +0000115 cl::desc("Specify a target machine"), cl::value_desc("machine"));
116
Reid Spencerbae68252004-08-19 04:49:47 +0000117cl::opt<bool> Native("native", cl::init(false),
Reid Spencer034a5442004-08-10 16:26:01 +0000118 cl::desc("Generative native object and executables instead of bytecode"));
119
120//===------------------------------------------------------------------------===
121//=== INFORMATION OPTIONS
122//===------------------------------------------------------------------------===
123
Reid Spencerbae68252004-08-19 04:49:47 +0000124cl::opt<bool> DryRun("dry-run", cl::Optional, cl::init(false),
Reid Spencer5c56dc12004-08-13 20:22:43 +0000125 cl::desc("Do everything but perform the compilation actions"));
Reid Spencer034a5442004-08-10 16:26:01 +0000126
Reid Spencerbae68252004-08-19 04:49:47 +0000127cl::alias DryRunAlias("y", cl::Optional,
Reid Spencer5c56dc12004-08-13 20:22:43 +0000128 cl::desc("Alias for -dry-run"), cl::aliasopt(DryRun));
Reid Spencer034a5442004-08-10 16:26:01 +0000129
Reid Spencerbae68252004-08-19 04:49:47 +0000130cl::opt<bool> Verbose("verbose", cl::Optional, cl::init(false),
Reid Spencer034a5442004-08-10 16:26:01 +0000131 cl::desc("Print out each action taken"));
132
Reid Spencerbae68252004-08-19 04:49:47 +0000133cl::alias VerboseAlias("v", cl::Optional,
Reid Spencer034a5442004-08-10 16:26:01 +0000134 cl::desc("Alias for -verbose"), cl::aliasopt(Verbose));
135
Reid Spencerbae68252004-08-19 04:49:47 +0000136cl::opt<bool> Debug("debug", cl::Optional, cl::init(false),
Reid Spencer5c56dc12004-08-13 20:22:43 +0000137 cl::Hidden, cl::desc("Print out debugging information"));
138
Reid Spencerbae68252004-08-19 04:49:47 +0000139cl::alias DebugAlias("d", cl::Optional,
Reid Spencer5c56dc12004-08-13 20:22:43 +0000140 cl::desc("Alias for -debug"), cl::aliasopt(Debug));
141
Reid Spencerbae68252004-08-19 04:49:47 +0000142cl::opt<bool> TimeActions("time-actions", cl::Optional, cl::init(false),
Reid Spencer034a5442004-08-10 16:26:01 +0000143 cl::desc("Print execution time for each action taken"));
144
Reid Spencerbae68252004-08-19 04:49:47 +0000145cl::opt<bool> ShowStats("stats", cl::Optional, cl::init(false),
146 cl::desc("Print statistics accumulated during optimization"));
147
Reid Spencer034a5442004-08-10 16:26:01 +0000148//===------------------------------------------------------------------------===
149//=== ADVANCED OPTIONS
150//===------------------------------------------------------------------------===
151
Reid Spencer5c56dc12004-08-13 20:22:43 +0000152static cl::opt<std::string> ConfigDir("config-dir", cl::Optional,
153 cl::desc("Specify a configuration directory to override defaults"),
154 cl::value_desc("directory"));
Reid Spencer034a5442004-08-10 16:26:01 +0000155
Reid Spencerbf437722004-08-15 08:19:46 +0000156static cl::opt<bool> EmitRawCode("emit-raw-code", cl::Hidden, cl::Optional,
Reid Spencer034a5442004-08-10 16:26:01 +0000157 cl::desc("Emit raw, unoptimized code"));
158
Reid Spencerbf437722004-08-15 08:19:46 +0000159static cl::opt<bool> PipeCommands("pipe", cl::Optional,
160 cl::desc("Invoke sub-commands by linking input/output with pipes"));
161
Reid Spencer52c2dc12004-08-29 19:26:56 +0000162static cl::opt<bool> KeepTemps("keep-temps", cl::Optional,
Reid Spencerbae68252004-08-19 04:49:47 +0000163 cl::desc("Don't delete the temporary files created during compilation"));
164
Reid Spencer034a5442004-08-10 16:26:01 +0000165//===------------------------------------------------------------------------===
166//=== POSITIONAL OPTIONS
167//===------------------------------------------------------------------------===
168
169static cl::list<std::string> Files(cl::Positional, cl::OneOrMore,
Reid Spencer5c56dc12004-08-13 20:22:43 +0000170 cl::desc("[Sources/objects/libraries]"));
171
172static cl::list<std::string> Languages("x", cl::ZeroOrMore,
173 cl::desc("Specify the source language for subsequent files"),
174 cl::value_desc("language"));
175
176//===------------------------------------------------------------------------===
177//=== GetFileType - determine type of a file
178//===------------------------------------------------------------------------===
179const std::string GetFileType(const std::string& fname, unsigned pos ) {
180 static std::vector<std::string>::iterator langIt = Languages.begin();
181 static std::string CurrLang = "";
182
183 // If a -x LANG option has been specified ..
184 if ( langIt != Languages.end() )
185 // If the -x LANG option came before the current file on command line
186 if ( Languages.getPosition( langIt - Languages.begin() ) < pos ) {
187 // use that language
188 CurrLang = *langIt++;
189 return CurrLang;
190 }
191
192 // If there's a current language in effect
193 if (!CurrLang.empty())
194 return CurrLang; // use that language
195
196 // otherwise just determine lang from the filename's suffix
197 return fname.substr( fname.rfind('.',fname.size()) + 1 );
198}
199
200} // end anonymous namespace
Reid Spencer034a5442004-08-10 16:26:01 +0000201
202
203/// @brief The main program for llvmc
204int main(int argc, char **argv) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000205 // Make sure we print stack trace if we get bad signals
206 sys::PrintStackTraceOnErrorSignal();
207
Reid Spencerbae68252004-08-19 04:49:47 +0000208 try {
Reid Spencer034a5442004-08-10 16:26:01 +0000209
Reid Spencerbae68252004-08-19 04:49:47 +0000210 // Parse the command line options
211 cl::ParseCommandLineOptions(argc, argv,
Reid Spencer52c2dc12004-08-29 19:26:56 +0000212 " LLVM Compiler Driver (llvmc)\n\n"
Reid Spencerbae68252004-08-19 04:49:47 +0000213 " This program provides easy invocation of the LLVM tool set\n"
Reid Spencer52c2dc12004-08-29 19:26:56 +0000214 " and other compiler tools.\n"
Reid Spencerbae68252004-08-19 04:49:47 +0000215 );
Reid Spencer034a5442004-08-10 16:26:01 +0000216
Reid Spencerbae68252004-08-19 04:49:47 +0000217 // Deal with unimplemented options.
218 if (PipeCommands)
Reid Spencer2cf17a42004-08-24 14:05:30 +0000219 throw "Not implemented yet: -pipe";
Reid Spencer034a5442004-08-10 16:26:01 +0000220
Reid Spencer2cf17a42004-08-24 14:05:30 +0000221 if (OutputFilename.empty())
222 if (OptLevel == CompilerDriver::LINKING)
223 OutputFilename = "a.out";
224 else
225 throw "An output file must be specified. Please use the -o option";
226
Reid Spencerbae68252004-08-19 04:49:47 +0000227 // Construct the ConfigDataProvider object
228 LLVMC_ConfigDataProvider Provider;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000229 Provider.setConfigDir(sys::Path(ConfigDir));
Reid Spencer5c56dc12004-08-13 20:22:43 +0000230
Reid Spencerbae68252004-08-19 04:49:47 +0000231 // Construct the CompilerDriver object
Reid Spencer52c2dc12004-08-29 19:26:56 +0000232 CompilerDriver* CD = CompilerDriver::Get(Provider);
Reid Spencer5c56dc12004-08-13 20:22:43 +0000233
Reid Spenceraff3c772004-08-24 22:53:13 +0000234 // If the LLVM_LIB_SEARCH_PATH environment variable is
235 // set, append it to the list of places to search for libraries
236 std::string srchPath = getenv("LLVM_LIB_SEARCH_PATH");
237 if (!srchPath.empty())
238 LibPaths.push_back(srchPath);
239
Reid Spencer52c2dc12004-08-29 19:26:56 +0000240 // Set the driver flags based on command line options
241 unsigned flags = 0;
242 if (Verbose) flags |= CompilerDriver::VERBOSE_FLAG;
243 if (Debug) flags |= CompilerDriver::DEBUG_FLAG;
244 if (DryRun) flags |= CompilerDriver::DRY_RUN_FLAG;
245 if (ForceOutput) flags |= CompilerDriver::FORCE_FLAG;
246 if (Native) flags |= CompilerDriver::EMIT_NATIVE_FLAG;
247 if (EmitRawCode) flags |= CompilerDriver::EMIT_RAW_FLAG;
248 if (KeepTemps) flags |= CompilerDriver::KEEP_TEMPS_FLAG;
249 if (ShowStats) flags |= CompilerDriver::SHOW_STATS_FLAG;
250 if (TimeActions) flags |= CompilerDriver::TIME_ACTIONS_FLAG;
251 if (TimePassesIsEnabled) flags |= CompilerDriver::TIME_PASSES_FLAG;
252 CD->setDriverFlags(flags);
253
254 // Specify requred parameters
255 CD->setFinalPhase(FinalPhase);
256 CD->setOptimization(OptLevel);
257 CD->setOutputMachine(OutputMachine);
258 CD->setLibraryPaths(LibPaths);
259
260 // Provide additional tool arguments
Reid Spencerbae68252004-08-19 04:49:47 +0000261 if (!PreprocessorToolOpts.empty())
Reid Spencer52c2dc12004-08-29 19:26:56 +0000262 CD->setPhaseArgs(CompilerDriver::PREPROCESSING, PreprocessorToolOpts);
Reid Spencerbae68252004-08-19 04:49:47 +0000263 if (!TranslatorToolOpts.empty())
Reid Spencer52c2dc12004-08-29 19:26:56 +0000264 CD->setPhaseArgs(CompilerDriver::TRANSLATION, TranslatorToolOpts);
Reid Spencerbae68252004-08-19 04:49:47 +0000265 if (!OptimizerToolOpts.empty())
Reid Spencer52c2dc12004-08-29 19:26:56 +0000266 CD->setPhaseArgs(CompilerDriver::OPTIMIZATION, OptimizerToolOpts);
Reid Spencerbae68252004-08-19 04:49:47 +0000267 if (!AssemblerToolOpts.empty())
Reid Spencer52c2dc12004-08-29 19:26:56 +0000268 CD->setPhaseArgs(CompilerDriver::ASSEMBLY,AssemblerToolOpts);
Reid Spencerbae68252004-08-19 04:49:47 +0000269 if (!LinkerToolOpts.empty())
Reid Spencer52c2dc12004-08-29 19:26:56 +0000270 CD->setPhaseArgs(CompilerDriver::LINKING, LinkerToolOpts);
Reid Spencer5c56dc12004-08-13 20:22:43 +0000271
Reid Spencerbae68252004-08-19 04:49:47 +0000272 // Prepare the list of files to be compiled by the CompilerDriver.
273 CompilerDriver::InputList InpList;
274 std::vector<std::string>::iterator fileIt = Files.begin();
275 std::vector<std::string>::iterator libIt = Libraries.begin();
276 unsigned libPos = 0, filePos = 0;
277 while ( 1 ) {
278 if ( libIt != Libraries.end() )
279 libPos = Libraries.getPosition( libIt - Libraries.begin() );
280 else
281 libPos = 0;
282 if ( fileIt != Files.end() )
283 filePos = Files.getPosition( fileIt - Files.begin() );
284 else
285 filePos = 0;
Reid Spencer5c56dc12004-08-13 20:22:43 +0000286
Reid Spencerbae68252004-08-19 04:49:47 +0000287 if ( filePos != 0 && (libPos == 0 || filePos < libPos) ) {
288 // Add a source file
289 InpList.push_back( std::make_pair(*fileIt, GetFileType(*fileIt,filePos)));
290 ++fileIt;
291 }
292 else if ( libPos != 0 && (filePos == 0 || libPos < filePos) ) {
293 // Add a library
294 InpList.push_back( std::make_pair(*libIt++,""));
295 }
296 else
297 break; // we're done with the list
Reid Spencer5c56dc12004-08-13 20:22:43 +0000298 }
Reid Spencerbae68252004-08-19 04:49:47 +0000299
300 // Tell the driver to do its thing
Reid Spencer52c2dc12004-08-29 19:26:56 +0000301 int result = CD->execute(InpList,sys::Path(OutputFilename));
Reid Spencerbae68252004-08-19 04:49:47 +0000302 if (result != 0) {
Reid Spencer2cf17a42004-08-24 14:05:30 +0000303 throw "Error executing actions. Terminated.\n";
Reid Spencerbae68252004-08-19 04:49:47 +0000304 return result;
Reid Spencer5c56dc12004-08-13 20:22:43 +0000305 }
Reid Spencer034a5442004-08-10 16:26:01 +0000306
Reid Spencerbae68252004-08-19 04:49:47 +0000307 // All is good, return success
308 return 0;
309 } catch (std::string& msg) {
Reid Spencer2cf17a42004-08-24 14:05:30 +0000310 std::cerr << argv[0] << ": " << msg << "\n";
Reid Spencerbae68252004-08-19 04:49:47 +0000311 } catch (...) {
Reid Spencer2cf17a42004-08-24 14:05:30 +0000312 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Reid Spencer034a5442004-08-10 16:26:01 +0000313 }
Reid Spencer034a5442004-08-10 16:26:01 +0000314}