blob: 1640803cd4ed41681f19b6f7e2b5e87535463212 [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 Spencer5c56dc12004-08-13 20:22:43 +000018#include "ConfigData.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//===------------------------------------------------------------------------===
29static cl::opt<CompilerDriver::Phases> FinalPhase(
30 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//===------------------------------------------------------------------------===
48static cl::opt<CompilerDriver::OptimizationLevels> OptLevel(
49 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 Spencerbf437722004-08-15 08:19:46 +000072static cl::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 Spencerbf437722004-08-15 08:19:46 +000076static cl::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 Spencerbf437722004-08-15 08:19:46 +000080static cl::list<std::string> AssemblerToolOpts("Tasm", cl::ZeroOrMore,
81 cl::desc("Pass specific options to the assembler"),
82 cl::value_desc("option"));
83
84static cl::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 Spencerbf437722004-08-15 08:19:46 +000088static cl::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
96static cl::list<std::string> LibPaths("L", cl::Prefix,
97 cl::desc("Specify a library search path"), cl::value_desc("directory"));
98
99static cl::list<std::string> Libraries("l", cl::Prefix,
100 cl::desc("Specify libraries to link to"), cl::value_desc("library prefix"));
101
102
103//===------------------------------------------------------------------------===
104//=== OUTPUT OPTIONS
105//===------------------------------------------------------------------------===
106
Reid Spencer5c56dc12004-08-13 20:22:43 +0000107static cl::opt<std::string> OutputFilename("o",
Reid Spencer034a5442004-08-10 16:26:01 +0000108 cl::desc("Override output filename"), cl::value_desc("filename"));
109
Reid Spencer5c56dc12004-08-13 20:22:43 +0000110static cl::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 Spencer5c56dc12004-08-13 20:22:43 +0000113static cl::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 Spencer5c56dc12004-08-13 20:22:43 +0000120static cl::opt<bool> DryRun("dry-run", cl::Optional, cl::init(false),
121 cl::desc("Do everything but perform the compilation actions"));
Reid Spencer034a5442004-08-10 16:26:01 +0000122
Reid Spencer5c56dc12004-08-13 20:22:43 +0000123static cl::alias DryRunAlias("y", cl::Optional,
124 cl::desc("Alias for -dry-run"), cl::aliasopt(DryRun));
Reid Spencer034a5442004-08-10 16:26:01 +0000125
Reid Spencer5c56dc12004-08-13 20:22:43 +0000126static cl::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 Spencer5c56dc12004-08-13 20:22:43 +0000129static cl::alias VerboseAlias("v", cl::Optional,
Reid Spencer034a5442004-08-10 16:26:01 +0000130 cl::desc("Alias for -verbose"), cl::aliasopt(Verbose));
131
Reid Spencer5c56dc12004-08-13 20:22:43 +0000132static cl::opt<bool> Debug("debug", cl::Optional, cl::init(false),
133 cl::Hidden, cl::desc("Print out debugging information"));
134
135static cl::alias DebugAlias("d", cl::Optional,
136 cl::desc("Alias for -debug"), cl::aliasopt(Debug));
137
138static cl::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
141//===------------------------------------------------------------------------===
142//=== ADVANCED OPTIONS
143//===------------------------------------------------------------------------===
144
Reid Spencer5c56dc12004-08-13 20:22:43 +0000145static cl::opt<std::string> ConfigDir("config-dir", cl::Optional,
146 cl::desc("Specify a configuration directory to override defaults"),
147 cl::value_desc("directory"));
Reid Spencer034a5442004-08-10 16:26:01 +0000148
Reid Spencerbf437722004-08-15 08:19:46 +0000149static cl::opt<bool> EmitRawCode("emit-raw-code", cl::Hidden, cl::Optional,
Reid Spencer034a5442004-08-10 16:26:01 +0000150 cl::desc("Emit raw, unoptimized code"));
151
Reid Spencerbf437722004-08-15 08:19:46 +0000152static cl::opt<bool> PipeCommands("pipe", cl::Optional,
153 cl::desc("Invoke sub-commands by linking input/output with pipes"));
154
Reid Spencer034a5442004-08-10 16:26:01 +0000155//===------------------------------------------------------------------------===
156//=== POSITIONAL OPTIONS
157//===------------------------------------------------------------------------===
158
159static cl::list<std::string> Files(cl::Positional, cl::OneOrMore,
Reid Spencer5c56dc12004-08-13 20:22:43 +0000160 cl::desc("[Sources/objects/libraries]"));
161
162static cl::list<std::string> Languages("x", cl::ZeroOrMore,
163 cl::desc("Specify the source language for subsequent files"),
164 cl::value_desc("language"));
165
166//===------------------------------------------------------------------------===
167//=== GetFileType - determine type of a file
168//===------------------------------------------------------------------------===
169const std::string GetFileType(const std::string& fname, unsigned pos ) {
170 static std::vector<std::string>::iterator langIt = Languages.begin();
171 static std::string CurrLang = "";
172
173 // If a -x LANG option has been specified ..
174 if ( langIt != Languages.end() )
175 // If the -x LANG option came before the current file on command line
176 if ( Languages.getPosition( langIt - Languages.begin() ) < pos ) {
177 // use that language
178 CurrLang = *langIt++;
179 return CurrLang;
180 }
181
182 // If there's a current language in effect
183 if (!CurrLang.empty())
184 return CurrLang; // use that language
185
186 // otherwise just determine lang from the filename's suffix
187 return fname.substr( fname.rfind('.',fname.size()) + 1 );
188}
189
190} // end anonymous namespace
Reid Spencer034a5442004-08-10 16:26:01 +0000191
192
193/// @brief The main program for llvmc
194int main(int argc, char **argv) {
195 // Make sure we print stack trace if we get bad signals
196 PrintStackTraceOnErrorSignal();
197
198 // Parse the command line options
199 cl::ParseCommandLineOptions(argc, argv,
200 " LLVM Compilation Driver (llvmc)\n\n"
201 " This program provides easy invocation of the LLVM tool set\n"
202 " and source language compiler tools.\n"
203 );
204
Reid Spencer5c56dc12004-08-13 20:22:43 +0000205 // Deal with unimplemented options.
206 if (Native)
207 std::cerr << argv[0] << ": Not implemented yet: -native";
208 if (EmitRawCode)
209 std::cerr << argv[0] << ": Not implemented yet: -emit-raw-code";
Reid Spencerbf437722004-08-15 08:19:46 +0000210 if (PipeCommands)
211 std::cerr << argv[0] << ": Not implemented yet: -pipe";
Reid Spencer034a5442004-08-10 16:26:01 +0000212
Reid Spencer5c56dc12004-08-13 20:22:43 +0000213 // Default the output file, only if we're going to try to link
214 if (OutputFilename.empty() && OptLevel == CompilerDriver::LINKING)
215 OutputFilename = "a.out";
216
217 // Construct the ConfigDataProvider object
218 LLVMC_ConfigDataProvider Provider;
Reid Spencer68fb37a2004-08-14 09:37:15 +0000219 Provider.setConfigDir(ConfigDir);
Reid Spencer5c56dc12004-08-13 20:22:43 +0000220
221 // Construct the CompilerDriver object
222 CompilerDriver CD(Provider);
223
224 // Configure the driver based on options
225 CD.setVerbose(Verbose);
226 CD.setDebug(Debug);
227 CD.setDryRun(DryRun);
228 CD.setFinalPhase(FinalPhase);
229 CD.setOptimization(OptLevel);
230 CD.setOutputMachine(OutputMachine);
231 CD.setEmitNativeCode(Native);
232 CD.setEmitRawCode(EmitRawCode);
Reid Spencerbf437722004-08-15 08:19:46 +0000233 CD.setLibraryPaths(LibPaths);
234 CD.setPreprocessorOptions(PreprocessorToolOpts);
235 CD.setTranslatorOptions(TranslatorToolOpts);
236 CD.setOptimizerOptions(OptimizerToolOpts);
237 CD.setAssemblerOptions(AssemblerToolOpts);
238 CD.setLinkerOptions(LinkerToolOpts);
Reid Spencer5c56dc12004-08-13 20:22:43 +0000239
240 // Prepare the list of files to be compiled by the CompilerDriver.
241 CompilerDriver::InputList InpList;
242 std::vector<std::string>::iterator fileIt = Files.begin();
243 std::vector<std::string>::iterator libIt = Libraries.begin();
244 unsigned libPos = 0, filePos = 0;
245 while ( 1 ) {
246 if ( libIt != Libraries.end() )
247 libPos = Libraries.getPosition( libIt - Libraries.begin() );
248 else
249 libPos = 0;
250 if ( fileIt != Files.end() )
251 filePos = Files.getPosition( fileIt - Files.begin() );
252 else
253 filePos = 0;
254
255 if ( filePos != 0 && (libPos == 0 || filePos < libPos) ) {
256 // Add a source file
257 InpList.push_back( std::make_pair(*fileIt, GetFileType(*fileIt,filePos)));
258 ++fileIt;
259 }
260 else if ( libPos != 0 && (filePos == 0 || libPos < filePos) ) {
261 // Add a library
262 InpList.push_back( std::make_pair(*libIt++,""));
263 }
264 else
265 break; // we're done with the list
266 }
Reid Spencer034a5442004-08-10 16:26:01 +0000267
268 // Tell the driver to do its thing
Reid Spencer5c56dc12004-08-13 20:22:43 +0000269 int result = CD.execute(InpList,OutputFilename);
Reid Spencer034a5442004-08-10 16:26:01 +0000270 if (result != 0) {
271 std::cerr << argv[0] << ": Error executing actions. Terminated.\n";
272 return result;
273 }
274
275 // All is good, return success
276 return 0;
277}