blob: a441b2caeb1794915bf5e37628654dba4ade706f [file] [log] [blame]
Chris Lattner7915a1e2003-10-14 21:34:11 +00001//===-- ToolRunner.cpp ----------------------------------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner7915a1e2003-10-14 21:34:11 +00009//
10// This file implements the interfaces described in the ToolRunner.h file.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner0b1fe842003-10-19 02:27:40 +000014#define DEBUG_TYPE "toolrunner"
Misha Brukman68734502003-10-06 18:37:24 +000015#include "llvm/Support/ToolRunner.h"
Brian Gaekec8db76c2003-11-18 06:31:17 +000016#include "Config/config.h" // for HAVE_LINK_R
Misha Brukman9558c6a2003-09-29 22:39:25 +000017#include "Support/Debug.h"
18#include "Support/FileUtilities.h"
Chris Lattner7915a1e2003-10-14 21:34:11 +000019#include <iostream>
20#include <fstream>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Misha Brukman9558c6a2003-09-29 22:39:25 +000023//===---------------------------------------------------------------------===//
24// LLI Implementation of AbstractIntepreter interface
25//
Chris Lattner2cdd21c2003-12-14 21:35:53 +000026namespace {
27 class LLI : public AbstractInterpreter {
28 std::string LLIPath; // The path to the LLI executable
29 public:
30 LLI(const std::string &Path) : LLIPath(Path) { }
31
32
33 virtual int ExecuteProgram(const std::string &Bytecode,
34 const std::vector<std::string> &Args,
35 const std::string &InputFile,
36 const std::string &OutputFile,
37 const std::vector<std::string> &SharedLibs =
Chris Lattnereeed9832003-10-14 21:52:52 +000038 std::vector<std::string>());
Chris Lattner2cdd21c2003-12-14 21:35:53 +000039 };
40}
Misha Brukman9558c6a2003-09-29 22:39:25 +000041
42int LLI::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +000043 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +000044 const std::string &InputFile,
45 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +000046 const std::vector<std::string> &SharedLibs) {
Chris Lattner8c56be52004-02-18 20:21:57 +000047 if (!SharedLibs.empty())
48 throw ToolExecutionError("LLI currently does not support "
49 "loading shared libraries.");
Misha Brukman9558c6a2003-09-29 22:39:25 +000050
51 std::vector<const char*> LLIArgs;
52 LLIArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +000053 LLIArgs.push_back("-quiet");
54 LLIArgs.push_back("-force-interpreter=true");
55 LLIArgs.push_back(Bytecode.c_str());
56 // Add optional parameters to the running program from Argv
57 for (unsigned i=0, e = Args.size(); i != e; ++i)
58 LLIArgs.push_back(Args[i].c_str());
59 LLIArgs.push_back(0);
60
61 std::cout << "<lli>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +000062 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +000063 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +000064 std::cerr << " " << LLIArgs[i];
65 std::cerr << "\n";
66 );
67 return RunProgramWithTimeout(LLIPath, &LLIArgs[0],
68 InputFile, OutputFile, OutputFile);
69}
70
71// LLI create method - Try to find the LLI executable
Chris Lattner7915a1e2003-10-14 21:34:11 +000072AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
73 std::string &Message) {
74 std::string LLIPath = FindExecutable("lli", ProgPath);
Misha Brukman9558c6a2003-09-29 22:39:25 +000075 if (!LLIPath.empty()) {
76 Message = "Found lli: " + LLIPath + "\n";
77 return new LLI(LLIPath);
78 }
79
80 Message = "Cannot find `lli' in executable directory or PATH!\n";
81 return 0;
82}
83
84//===----------------------------------------------------------------------===//
85// LLC Implementation of AbstractIntepreter interface
86//
Chris Lattner8c56be52004-02-18 20:21:57 +000087void LLC::OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile) {
Misha Brukman9558c6a2003-09-29 22:39:25 +000088 OutputAsmFile = getUniqueFilename(Bytecode+".llc.s");
89 const char *LLCArgs[] = {
90 LLCPath.c_str(),
91 "-o", OutputAsmFile.c_str(), // Output to the Asm file
92 "-f", // Overwrite as necessary...
93 Bytecode.c_str(), // This is the input bytecode
94 0
95 };
96
97 std::cout << "<llc>" << std::flush;
98 if (RunProgramWithTimeout(LLCPath, LLCArgs, "/dev/null", "/dev/null",
Chris Lattner8c56be52004-02-18 20:21:57 +000099 "/dev/null"))
100 throw ToolExecutionError("LLC failed to compile the program.");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000101}
102
103int LLC::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000104 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000105 const std::string &InputFile,
106 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000107 const std::vector<std::string> &SharedLibs) {
108
Misha Brukman9558c6a2003-09-29 22:39:25 +0000109 std::string OutputAsmFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000110 OutputAsm(Bytecode, OutputAsmFile);
111 FileRemover OutFileRemover(OutputAsmFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000112
113 // Assuming LLC worked, compile the result with GCC and run it.
Chris Lattner8c56be52004-02-18 20:21:57 +0000114 return gcc->ExecuteProgram(OutputAsmFile, Args, GCC::AsmFile,
115 InputFile, OutputFile, SharedLibs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000116}
117
Chris Lattner7915a1e2003-10-14 21:34:11 +0000118/// createLLC - Try to find the LLC executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000119///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000120LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
121 std::string &Message) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000122 std::string LLCPath = FindExecutable("llc", ProgramPath);
123 if (LLCPath.empty()) {
124 Message = "Cannot find `llc' in executable directory or PATH!\n";
125 return 0;
126 }
127
128 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000129 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000130 if (!gcc) {
131 std::cerr << Message << "\n";
132 exit(1);
133 }
134 return new LLC(LLCPath, gcc);
135}
136
137//===---------------------------------------------------------------------===//
138// JIT Implementation of AbstractIntepreter interface
139//
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000140namespace {
141 class JIT : public AbstractInterpreter {
142 std::string LLIPath; // The path to the LLI executable
143 public:
144 JIT(const std::string &Path) : LLIPath(Path) { }
145
146
147 virtual int ExecuteProgram(const std::string &Bytecode,
148 const std::vector<std::string> &Args,
149 const std::string &InputFile,
150 const std::string &OutputFile,
151 const std::vector<std::string> &SharedLibs =
Chris Lattnereeed9832003-10-14 21:52:52 +0000152 std::vector<std::string>());
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000153 };
154}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000155
156int JIT::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000157 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000158 const std::string &InputFile,
159 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000160 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000161 // Construct a vector of parameters, incorporating those from the command-line
162 std::vector<const char*> JITArgs;
163 JITArgs.push_back(LLIPath.c_str());
164 JITArgs.push_back("-quiet");
165 JITArgs.push_back("-force-interpreter=false");
Chris Lattnereeed9832003-10-14 21:52:52 +0000166
167 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000168 JITArgs.push_back("-load");
Chris Lattnereeed9832003-10-14 21:52:52 +0000169 JITArgs.push_back(SharedLibs[i].c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000170 }
171 JITArgs.push_back(Bytecode.c_str());
172 // Add optional parameters to the running program from Argv
173 for (unsigned i=0, e = Args.size(); i != e; ++i)
174 JITArgs.push_back(Args[i].c_str());
175 JITArgs.push_back(0);
176
177 std::cout << "<jit>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000178 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000179 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000180 std::cerr << " " << JITArgs[i];
181 std::cerr << "\n";
182 );
183 DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
184 return RunProgramWithTimeout(LLIPath, &JITArgs[0],
185 InputFile, OutputFile, OutputFile);
186}
187
Chris Lattner7915a1e2003-10-14 21:34:11 +0000188/// createJIT - Try to find the LLI executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000189///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000190AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
191 std::string &Message) {
192 std::string LLIPath = FindExecutable("lli", ProgPath);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000193 if (!LLIPath.empty()) {
194 Message = "Found lli: " + LLIPath + "\n";
195 return new JIT(LLIPath);
196 }
197
198 Message = "Cannot find `lli' in executable directory or PATH!\n";
199 return 0;
200}
201
Chris Lattner8c56be52004-02-18 20:21:57 +0000202void CBE::OutputC(const std::string &Bytecode,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000203 std::string &OutputCFile) {
204 OutputCFile = getUniqueFilename(Bytecode+".cbe.c");
205 const char *DisArgs[] = {
Chris Lattner9915cd92004-02-17 06:40:06 +0000206 LLCPath.c_str(),
Misha Brukman9558c6a2003-09-29 22:39:25 +0000207 "-o", OutputCFile.c_str(), // Output to the C file
Chris Lattner9915cd92004-02-17 06:40:06 +0000208 "-march=c", // Output to C
Misha Brukman9558c6a2003-09-29 22:39:25 +0000209 "-f", // Overwrite as necessary...
210 Bytecode.c_str(), // This is the input bytecode
211 0
212 };
213
214 std::cout << "<cbe>" << std::flush;
Chris Lattner9915cd92004-02-17 06:40:06 +0000215 if (RunProgramWithTimeout(LLCPath, DisArgs, "/dev/null", "/dev/null",
Chris Lattner8c56be52004-02-18 20:21:57 +0000216 "/dev/null"))
217 throw ToolExecutionError("llc -march=c failed!");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000218}
219
220int CBE::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000221 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000222 const std::string &InputFile,
223 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000224 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000225 std::string OutputCFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000226 OutputC(Bytecode, OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000227
Chris Lattner8c56be52004-02-18 20:21:57 +0000228 FileRemover CFileRemove(OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000229
Chris Lattner8c56be52004-02-18 20:21:57 +0000230 return gcc->ExecuteProgram(OutputCFile, Args, GCC::CFile,
231 InputFile, OutputFile, SharedLibs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000232}
233
Chris Lattner9915cd92004-02-17 06:40:06 +0000234/// createCBE - Try to find the 'llc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000235///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000236CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
237 std::string &Message) {
Chris Lattner9915cd92004-02-17 06:40:06 +0000238 std::string LLCPath = FindExecutable("llc", ProgramPath);
239 if (LLCPath.empty()) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000240 Message =
Chris Lattner9915cd92004-02-17 06:40:06 +0000241 "Cannot find `llc' in executable directory or PATH!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000242 return 0;
243 }
244
Chris Lattner9915cd92004-02-17 06:40:06 +0000245 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000246 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000247 if (!gcc) {
248 std::cerr << Message << "\n";
249 exit(1);
250 }
Chris Lattner9915cd92004-02-17 06:40:06 +0000251 return new CBE(LLCPath, gcc);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000252}
253
254//===---------------------------------------------------------------------===//
255// GCC abstraction
256//
Misha Brukman9558c6a2003-09-29 22:39:25 +0000257int GCC::ExecuteProgram(const std::string &ProgramFile,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000258 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000259 FileType fileType,
260 const std::string &InputFile,
261 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000262 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000263 std::vector<const char*> GCCArgs;
264
265 GCCArgs.push_back(GCCPath.c_str());
Chris Lattnereeed9832003-10-14 21:52:52 +0000266
267 // Specify the shared libraries to link in...
268 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i)
269 GCCArgs.push_back(SharedLibs[i].c_str());
270
271 // Specify -x explicitly in case the extension is wonky
Misha Brukman9558c6a2003-09-29 22:39:25 +0000272 GCCArgs.push_back("-x");
273 if (fileType == CFile) {
274 GCCArgs.push_back("c");
275 GCCArgs.push_back("-fno-strict-aliasing");
276 } else {
277 GCCArgs.push_back("assembler");
278 }
279 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
280 GCCArgs.push_back("-o");
Chris Lattnereeed9832003-10-14 21:52:52 +0000281 std::string OutputBinary = getUniqueFilename(ProgramFile+".gcc.exe");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000282 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
283 GCCArgs.push_back("-lm"); // Hard-code the math library...
284 GCCArgs.push_back("-O2"); // Optimize the program a bit...
Brian Gaekec8db76c2003-11-18 06:31:17 +0000285#if defined (HAVE_LINK_R)
Chris Lattner1f0f1622003-10-18 21:54:47 +0000286 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
Brian Gaekec8db76c2003-11-18 06:31:17 +0000287#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000288 GCCArgs.push_back(0); // NULL terminator
289
290 std::cout << "<gcc>" << std::flush;
291 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "/dev/null", "/dev/null",
292 "/dev/null")) {
293 ProcessFailure(&GCCArgs[0]);
294 exit(1);
295 }
296
297 std::vector<const char*> ProgramArgs;
298 ProgramArgs.push_back(OutputBinary.c_str());
299 // Add optional parameters to the running program from Argv
300 for (unsigned i=0, e = Args.size(); i != e; ++i)
301 ProgramArgs.push_back(Args[i].c_str());
302 ProgramArgs.push_back(0); // NULL terminator
303
304 // Now that we have a binary, run it!
305 std::cout << "<program>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000306 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000307 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000308 std::cerr << " " << ProgramArgs[i];
309 std::cerr << "\n";
310 );
Chris Lattner8c56be52004-02-18 20:21:57 +0000311
312 FileRemover OutputBinaryRemover(OutputBinary);
313 return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
314 InputFile, OutputFile, OutputFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000315}
316
Chris Lattner1798e4a2003-10-14 21:07:25 +0000317int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000318 std::string &OutputFile) {
John Criswell7f7d16b2004-01-26 20:59:41 +0000319 OutputFile = getUniqueFilename(InputFile+SHLIBEXT);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000320 // Compile the C/asm file into a shared object
321 const char* GCCArgs[] = {
322 GCCPath.c_str(),
323 "-x", (fileType == AsmFile) ? "assembler" : "c",
324 "-fno-strict-aliasing",
325 InputFile.c_str(), // Specify the input filename...
326#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
327 "-G", // Compile a shared library, `-G' for Sparc
328#else
329 "-shared", // `-shared' for Linux/X86, maybe others
330#endif
331 "-o", OutputFile.c_str(), // Output to the right filename...
332 "-O2", // Optimize the program a bit...
333 0
334 };
335
336 std::cout << "<gcc>" << std::flush;
Chris Lattner1798e4a2003-10-14 21:07:25 +0000337 if (RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null",
338 "/dev/null")) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000339 ProcessFailure(GCCArgs);
Chris Lattner1798e4a2003-10-14 21:07:25 +0000340 return 1;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000341 }
342 return 0;
343}
344
345void GCC::ProcessFailure(const char** GCCArgs) {
Chris Lattner8c56be52004-02-18 20:21:57 +0000346 std::cerr << "\n*** Error: program invocation!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000347 for (const char **Arg = GCCArgs; *Arg; ++Arg)
348 std::cerr << " " << *Arg;
349 std::cerr << "\n";
350
351 // Rerun the compiler, capturing any error messages to print them.
352 std::string ErrorFilename = getUniqueFilename("gcc.errors");
353 RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", ErrorFilename.c_str(),
354 ErrorFilename.c_str());
355
356 // Print out the error messages generated by GCC if possible...
357 std::ifstream ErrorFile(ErrorFilename.c_str());
358 if (ErrorFile) {
359 std::copy(std::istreambuf_iterator<char>(ErrorFile),
360 std::istreambuf_iterator<char>(),
361 std::ostreambuf_iterator<char>(std::cerr));
362 ErrorFile.close();
363 std::cerr << "\n";
364 }
365
366 removeFile(ErrorFilename);
367}
368
Chris Lattner7915a1e2003-10-14 21:34:11 +0000369/// create - Try to find the `gcc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000370///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000371GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000372 std::string GCCPath = FindExecutable("gcc", ProgramPath);
373 if (GCCPath.empty()) {
374 Message = "Cannot find `gcc' in executable directory or PATH!\n";
375 return 0;
376 }
377
378 Message = "Found gcc: " + GCCPath + "\n";
379 return new GCC(GCCPath);
380}