1 //===--- llvmc.cpp - The LLVM Compiler Driver -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
15 //===------------------------------------------------------------------------===
17 #include "CompilerDriver.h"
18 #include "Configuration.h"
19 #include "llvm/Pass.h"
20 #include "llvm/System/Signals.h"
21 #include "Support/CommandLine.h"
27 //===------------------------------------------------------------------------===
29 //===------------------------------------------------------------------------===
30 cl::opt<CompilerDriver::Phases> FinalPhase(
31 cl::desc("Choose final phase of compilation:"),
32 cl::init(CompilerDriver::LINKING),
34 clEnumValN(CompilerDriver::PREPROCESSING,"E",
35 "Stop compilation after pre-processing phase"),
36 clEnumValN(CompilerDriver::TRANSLATION, "t",
37 "Stop compilation after translation phase"),
38 clEnumValN(CompilerDriver::OPTIMIZATION,"c",
39 "Stop compilation after optimization phase"),
40 clEnumValN(CompilerDriver::ASSEMBLY,"S",
41 "Stop compilation after assembly phase"),
46 //===------------------------------------------------------------------------===
47 //=== OPTIMIZATION OPTIONS
48 //===------------------------------------------------------------------------===
49 cl::opt<CompilerDriver::OptimizationLevels> OptLevel(
50 cl::desc("Choose level of optimization to apply:"),
51 cl::init(CompilerDriver::OPT_FAST_COMPILE),
53 clEnumValN(CompilerDriver::OPT_FAST_COMPILE,"O0",
54 "An alias for the -O1 option."),
55 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"),
69 //===------------------------------------------------------------------------===
71 //===------------------------------------------------------------------------===
73 cl::list<std::string> PreprocessorToolOpts("Tpre", cl::ZeroOrMore,
74 cl::desc("Pass specific options to the pre-processor"),
75 cl::value_desc("option"));
77 cl::list<std::string> TranslatorToolOpts("Ttrn", cl::ZeroOrMore,
78 cl::desc("Pass specific options to the assembler"),
79 cl::value_desc("option"));
81 cl::list<std::string> AssemblerToolOpts("Tasm", cl::ZeroOrMore,
82 cl::desc("Pass specific options to the assembler"),
83 cl::value_desc("option"));
85 cl::list<std::string> OptimizerToolOpts("Topt", cl::ZeroOrMore,
86 cl::desc("Pass specific options to the optimizer"),
87 cl::value_desc("option"));
89 cl::list<std::string> LinkerToolOpts("Tlnk", cl::ZeroOrMore,
90 cl::desc("Pass specific options to the linker"),
91 cl::value_desc("option"));
93 //===------------------------------------------------------------------------===
95 //===------------------------------------------------------------------------===
97 cl::list<std::string> LibPaths("L", cl::Prefix,
98 cl::desc("Specify a library search path"), cl::value_desc("directory"));
100 cl::list<std::string> Libraries("l", cl::Prefix,
101 cl::desc("Specify libraries to link to"), cl::value_desc("library prefix"));
104 //===------------------------------------------------------------------------===
106 //===------------------------------------------------------------------------===
108 cl::opt<std::string> OutputFilename("o",
109 cl::desc("Override output filename"), cl::value_desc("filename"));
111 cl::opt<bool> ForceOutput("f", cl::Optional, cl::init(false),
112 cl::desc("Force output files to be overridden"));
114 cl::opt<std::string> OutputMachine("m", cl::Prefix,
115 cl::desc("Specify a target machine"), cl::value_desc("machine"));
117 cl::opt<bool> Native("native", cl::init(false),
118 cl::desc("Generative native object and executables instead of bytecode"));
120 //===------------------------------------------------------------------------===
121 //=== INFORMATION OPTIONS
122 //===------------------------------------------------------------------------===
124 cl::opt<bool> DryRun("dry-run", cl::Optional, cl::init(false),
125 cl::desc("Do everything but perform the compilation actions"));
127 cl::alias DryRunAlias("y", cl::Optional,
128 cl::desc("Alias for -dry-run"), cl::aliasopt(DryRun));
130 cl::opt<bool> Verbose("verbose", cl::Optional, cl::init(false),
131 cl::desc("Print out each action taken"));
133 cl::alias VerboseAlias("v", cl::Optional,
134 cl::desc("Alias for -verbose"), cl::aliasopt(Verbose));
136 cl::opt<bool> Debug("debug", cl::Optional, cl::init(false),
137 cl::Hidden, cl::desc("Print out debugging information"));
139 cl::alias DebugAlias("d", cl::Optional,
140 cl::desc("Alias for -debug"), cl::aliasopt(Debug));
142 cl::opt<bool> TimeActions("time-actions", cl::Optional, cl::init(false),
143 cl::desc("Print execution time for each action taken"));
145 cl::opt<bool> ShowStats("stats", cl::Optional, cl::init(false),
146 cl::desc("Print statistics accumulated during optimization"));
148 //===------------------------------------------------------------------------===
149 //=== ADVANCED OPTIONS
150 //===------------------------------------------------------------------------===
152 static cl::opt<std::string> ConfigDir("config-dir", cl::Optional,
153 cl::desc("Specify a configuration directory to override defaults"),
154 cl::value_desc("directory"));
156 static cl::opt<bool> EmitRawCode("emit-raw-code", cl::Hidden, cl::Optional,
157 cl::desc("Emit raw, unoptimized code"));
159 static cl::opt<bool> PipeCommands("pipe", cl::Optional,
160 cl::desc("Invoke sub-commands by linking input/output with pipes"));
162 static cl::opt<bool> KeepTemps("keep-temps", cl::Optional,
163 cl::desc("Don't delete the temporary files created during compilation"));
165 //===------------------------------------------------------------------------===
166 //=== POSITIONAL OPTIONS
167 //===------------------------------------------------------------------------===
169 static cl::list<std::string> Files(cl::Positional, cl::OneOrMore,
170 cl::desc("[Sources/objects/libraries]"));
172 static cl::list<std::string> Languages("x", cl::ZeroOrMore,
173 cl::desc("Specify the source language for subsequent files"),
174 cl::value_desc("language"));
176 //===------------------------------------------------------------------------===
177 //=== GetFileType - determine type of a file
178 //===------------------------------------------------------------------------===
179 const std::string GetFileType(const std::string& fname, unsigned pos ) {
180 static std::vector<std::string>::iterator langIt = Languages.begin();
181 static std::string CurrLang = "";
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 ) {
188 CurrLang = *langIt++;
192 // If there's a current language in effect
193 if (!CurrLang.empty())
194 return CurrLang; // use that language
196 // otherwise just determine lang from the filename's suffix
197 return fname.substr( fname.rfind('.',fname.size()) + 1 );
200 } // end anonymous namespace
203 /// @brief The main program for llvmc
204 int main(int argc, char **argv) {
205 // Make sure we print stack trace if we get bad signals
206 sys::PrintStackTraceOnErrorSignal();
210 // Parse the command line options
211 cl::ParseCommandLineOptions(argc, argv,
212 " LLVM Compiler Driver (llvmc)\n\n"
213 " This program provides easy invocation of the LLVM tool set\n"
214 " and other compiler tools.\n"
217 // Deal with unimplemented options.
219 throw "Not implemented yet: -pipe";
221 if (OutputFilename.empty())
222 if (OptLevel == CompilerDriver::LINKING)
223 OutputFilename = "a.out";
225 throw "An output file must be specified. Please use the -o option";
227 // Construct the ConfigDataProvider object
228 LLVMC_ConfigDataProvider Provider;
229 Provider.setConfigDir(sys::Path(ConfigDir));
231 // Construct the CompilerDriver object
232 CompilerDriver* CD = CompilerDriver::Get(Provider);
234 // 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);
240 // Set the driver flags based on command line options
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);
254 // Specify requred parameters
255 CD->setFinalPhase(FinalPhase);
256 CD->setOptimization(OptLevel);
257 CD->setOutputMachine(OutputMachine);
258 CD->setLibraryPaths(LibPaths);
260 // Provide additional tool arguments
261 if (!PreprocessorToolOpts.empty())
262 CD->setPhaseArgs(CompilerDriver::PREPROCESSING, PreprocessorToolOpts);
263 if (!TranslatorToolOpts.empty())
264 CD->setPhaseArgs(CompilerDriver::TRANSLATION, TranslatorToolOpts);
265 if (!OptimizerToolOpts.empty())
266 CD->setPhaseArgs(CompilerDriver::OPTIMIZATION, OptimizerToolOpts);
267 if (!AssemblerToolOpts.empty())
268 CD->setPhaseArgs(CompilerDriver::ASSEMBLY,AssemblerToolOpts);
269 if (!LinkerToolOpts.empty())
270 CD->setPhaseArgs(CompilerDriver::LINKING, LinkerToolOpts);
272 // 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;
278 if ( libIt != Libraries.end() )
279 libPos = Libraries.getPosition( libIt - Libraries.begin() );
282 if ( fileIt != Files.end() )
283 filePos = Files.getPosition( fileIt - Files.begin() );
287 if ( filePos != 0 && (libPos == 0 || filePos < libPos) ) {
289 InpList.push_back( std::make_pair(*fileIt, GetFileType(*fileIt,filePos)));
292 else if ( libPos != 0 && (filePos == 0 || libPos < filePos) ) {
294 InpList.push_back( std::make_pair(*libIt++,""));
297 break; // we're done with the list
300 // Tell the driver to do its thing
301 int result = CD->execute(InpList,sys::Path(OutputFilename));
303 throw "Error executing actions. Terminated.\n";
307 // All is good, return success
309 } catch (std::string& msg) {
310 std::cerr << argv[0] << ": " << msg << "\n";
312 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";