blob: 6ad3d4392c0a6295f09c5709670b5d3d3b029ce8 [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 Lattner89bf9ea2004-02-18 20:38:00 +000021#include <sstream>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattner89bf9ea2004-02-18 20:38:00 +000024static void ProcessFailure(std::string ProgPath, const char** Args) {
25 std::ostringstream OS;
Chris Lattnera3de1172004-02-18 20:58:00 +000026 OS << "\nError running tool:\n ";
Chris Lattner89bf9ea2004-02-18 20:38:00 +000027 for (const char **Arg = Args; *Arg; ++Arg)
28 OS << " " << *Arg;
29 OS << "\n";
30
31 // Rerun the compiler, capturing any error messages to print them.
32 std::string ErrorFilename = getUniqueFilename("error_messages");
33 RunProgramWithTimeout(ProgPath, Args, "/dev/null", ErrorFilename.c_str(),
34 ErrorFilename.c_str());
35
36 // Print out the error messages generated by GCC if possible...
37 std::ifstream ErrorFile(ErrorFilename.c_str());
38 if (ErrorFile) {
39 std::copy(std::istreambuf_iterator<char>(ErrorFile),
40 std::istreambuf_iterator<char>(),
41 std::ostreambuf_iterator<char>(OS));
42 ErrorFile.close();
43 }
44
45 removeFile(ErrorFilename);
46 throw ToolExecutionError(OS.str());
47}
48
Misha Brukman9558c6a2003-09-29 22:39:25 +000049//===---------------------------------------------------------------------===//
50// LLI Implementation of AbstractIntepreter interface
51//
Chris Lattner2cdd21c2003-12-14 21:35:53 +000052namespace {
53 class LLI : public AbstractInterpreter {
54 std::string LLIPath; // The path to the LLI executable
55 public:
56 LLI(const std::string &Path) : LLIPath(Path) { }
57
58
59 virtual int ExecuteProgram(const std::string &Bytecode,
60 const std::vector<std::string> &Args,
61 const std::string &InputFile,
62 const std::string &OutputFile,
63 const std::vector<std::string> &SharedLibs =
Chris Lattnereeed9832003-10-14 21:52:52 +000064 std::vector<std::string>());
Chris Lattner2cdd21c2003-12-14 21:35:53 +000065 };
66}
Misha Brukman9558c6a2003-09-29 22:39:25 +000067
68int LLI::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +000069 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +000070 const std::string &InputFile,
71 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +000072 const std::vector<std::string> &SharedLibs) {
Chris Lattner8c56be52004-02-18 20:21:57 +000073 if (!SharedLibs.empty())
74 throw ToolExecutionError("LLI currently does not support "
75 "loading shared libraries.");
Misha Brukman9558c6a2003-09-29 22:39:25 +000076
77 std::vector<const char*> LLIArgs;
78 LLIArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +000079 LLIArgs.push_back("-quiet");
80 LLIArgs.push_back("-force-interpreter=true");
81 LLIArgs.push_back(Bytecode.c_str());
82 // Add optional parameters to the running program from Argv
83 for (unsigned i=0, e = Args.size(); i != e; ++i)
84 LLIArgs.push_back(Args[i].c_str());
85 LLIArgs.push_back(0);
86
87 std::cout << "<lli>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +000088 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +000089 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +000090 std::cerr << " " << LLIArgs[i];
91 std::cerr << "\n";
92 );
93 return RunProgramWithTimeout(LLIPath, &LLIArgs[0],
94 InputFile, OutputFile, OutputFile);
95}
96
97// LLI create method - Try to find the LLI executable
Chris Lattner7915a1e2003-10-14 21:34:11 +000098AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
99 std::string &Message) {
100 std::string LLIPath = FindExecutable("lli", ProgPath);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000101 if (!LLIPath.empty()) {
102 Message = "Found lli: " + LLIPath + "\n";
103 return new LLI(LLIPath);
104 }
105
106 Message = "Cannot find `lli' in executable directory or PATH!\n";
107 return 0;
108}
109
110//===----------------------------------------------------------------------===//
111// LLC Implementation of AbstractIntepreter interface
112//
Chris Lattner8c56be52004-02-18 20:21:57 +0000113void LLC::OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000114 OutputAsmFile = getUniqueFilename(Bytecode+".llc.s");
115 const char *LLCArgs[] = {
116 LLCPath.c_str(),
117 "-o", OutputAsmFile.c_str(), // Output to the Asm file
118 "-f", // Overwrite as necessary...
119 Bytecode.c_str(), // This is the input bytecode
120 0
121 };
122
123 std::cout << "<llc>" << std::flush;
124 if (RunProgramWithTimeout(LLCPath, LLCArgs, "/dev/null", "/dev/null",
Chris Lattner8c56be52004-02-18 20:21:57 +0000125 "/dev/null"))
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000126 ProcessFailure(LLCPath, LLCArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000127}
128
Chris Lattner9cbbee32004-02-18 23:24:41 +0000129void LLC::compileProgram(const std::string &Bytecode) {
130 std::string OutputAsmFile;
131 OutputAsm(Bytecode, OutputAsmFile);
132 removeFile(OutputAsmFile);
133}
134
Misha Brukman9558c6a2003-09-29 22:39:25 +0000135int LLC::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000136 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000137 const std::string &InputFile,
138 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000139 const std::vector<std::string> &SharedLibs) {
140
Misha Brukman9558c6a2003-09-29 22:39:25 +0000141 std::string OutputAsmFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000142 OutputAsm(Bytecode, OutputAsmFile);
143 FileRemover OutFileRemover(OutputAsmFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000144
145 // Assuming LLC worked, compile the result with GCC and run it.
Chris Lattner8c56be52004-02-18 20:21:57 +0000146 return gcc->ExecuteProgram(OutputAsmFile, Args, GCC::AsmFile,
147 InputFile, OutputFile, SharedLibs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000148}
149
Chris Lattner7915a1e2003-10-14 21:34:11 +0000150/// createLLC - Try to find the LLC executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000151///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000152LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
153 std::string &Message) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000154 std::string LLCPath = FindExecutable("llc", ProgramPath);
155 if (LLCPath.empty()) {
156 Message = "Cannot find `llc' in executable directory or PATH!\n";
157 return 0;
158 }
159
160 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000161 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000162 if (!gcc) {
163 std::cerr << Message << "\n";
164 exit(1);
165 }
166 return new LLC(LLCPath, gcc);
167}
168
169//===---------------------------------------------------------------------===//
170// JIT Implementation of AbstractIntepreter interface
171//
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000172namespace {
173 class JIT : public AbstractInterpreter {
174 std::string LLIPath; // The path to the LLI executable
175 public:
176 JIT(const std::string &Path) : LLIPath(Path) { }
177
178
179 virtual int ExecuteProgram(const std::string &Bytecode,
180 const std::vector<std::string> &Args,
181 const std::string &InputFile,
182 const std::string &OutputFile,
183 const std::vector<std::string> &SharedLibs =
Chris Lattnereeed9832003-10-14 21:52:52 +0000184 std::vector<std::string>());
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000185 };
186}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000187
188int JIT::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000189 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000190 const std::string &InputFile,
191 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000192 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000193 // Construct a vector of parameters, incorporating those from the command-line
194 std::vector<const char*> JITArgs;
195 JITArgs.push_back(LLIPath.c_str());
196 JITArgs.push_back("-quiet");
197 JITArgs.push_back("-force-interpreter=false");
Chris Lattnereeed9832003-10-14 21:52:52 +0000198
199 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000200 JITArgs.push_back("-load");
Chris Lattnereeed9832003-10-14 21:52:52 +0000201 JITArgs.push_back(SharedLibs[i].c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000202 }
203 JITArgs.push_back(Bytecode.c_str());
204 // Add optional parameters to the running program from Argv
205 for (unsigned i=0, e = Args.size(); i != e; ++i)
206 JITArgs.push_back(Args[i].c_str());
207 JITArgs.push_back(0);
208
209 std::cout << "<jit>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000210 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000211 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000212 std::cerr << " " << JITArgs[i];
213 std::cerr << "\n";
214 );
215 DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
216 return RunProgramWithTimeout(LLIPath, &JITArgs[0],
217 InputFile, OutputFile, OutputFile);
218}
219
Chris Lattner7915a1e2003-10-14 21:34:11 +0000220/// createJIT - Try to find the LLI executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000221///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000222AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
223 std::string &Message) {
224 std::string LLIPath = FindExecutable("lli", ProgPath);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000225 if (!LLIPath.empty()) {
226 Message = "Found lli: " + LLIPath + "\n";
227 return new JIT(LLIPath);
228 }
229
230 Message = "Cannot find `lli' in executable directory or PATH!\n";
231 return 0;
232}
233
Chris Lattner8c56be52004-02-18 20:21:57 +0000234void CBE::OutputC(const std::string &Bytecode,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000235 std::string &OutputCFile) {
236 OutputCFile = getUniqueFilename(Bytecode+".cbe.c");
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000237 const char *LLCArgs[] = {
Chris Lattner9915cd92004-02-17 06:40:06 +0000238 LLCPath.c_str(),
Misha Brukman9558c6a2003-09-29 22:39:25 +0000239 "-o", OutputCFile.c_str(), // Output to the C file
Chris Lattner9915cd92004-02-17 06:40:06 +0000240 "-march=c", // Output to C
Misha Brukman9558c6a2003-09-29 22:39:25 +0000241 "-f", // Overwrite as necessary...
242 Bytecode.c_str(), // This is the input bytecode
243 0
244 };
245
246 std::cout << "<cbe>" << std::flush;
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000247 if (RunProgramWithTimeout(LLCPath, LLCArgs, "/dev/null", "/dev/null",
Chris Lattner8c56be52004-02-18 20:21:57 +0000248 "/dev/null"))
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000249 ProcessFailure(LLCPath, LLCArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000250}
251
Chris Lattner9cbbee32004-02-18 23:24:41 +0000252void CBE::compileProgram(const std::string &Bytecode) {
253 std::string OutputCFile;
254 OutputC(Bytecode, OutputCFile);
255 removeFile(OutputCFile);
256}
257
Misha Brukman9558c6a2003-09-29 22:39:25 +0000258int CBE::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000259 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000260 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::string OutputCFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000264 OutputC(Bytecode, OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000265
Chris Lattner8c56be52004-02-18 20:21:57 +0000266 FileRemover CFileRemove(OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000267
Chris Lattner8c56be52004-02-18 20:21:57 +0000268 return gcc->ExecuteProgram(OutputCFile, Args, GCC::CFile,
269 InputFile, OutputFile, SharedLibs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000270}
271
Chris Lattner9915cd92004-02-17 06:40:06 +0000272/// createCBE - Try to find the 'llc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000273///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000274CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
275 std::string &Message) {
Chris Lattner9915cd92004-02-17 06:40:06 +0000276 std::string LLCPath = FindExecutable("llc", ProgramPath);
277 if (LLCPath.empty()) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000278 Message =
Chris Lattner9915cd92004-02-17 06:40:06 +0000279 "Cannot find `llc' in executable directory or PATH!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000280 return 0;
281 }
282
Chris Lattner9915cd92004-02-17 06:40:06 +0000283 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000284 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000285 if (!gcc) {
286 std::cerr << Message << "\n";
287 exit(1);
288 }
Chris Lattner9915cd92004-02-17 06:40:06 +0000289 return new CBE(LLCPath, gcc);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000290}
291
292//===---------------------------------------------------------------------===//
293// GCC abstraction
294//
Misha Brukman9558c6a2003-09-29 22:39:25 +0000295int GCC::ExecuteProgram(const std::string &ProgramFile,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000296 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000297 FileType fileType,
298 const std::string &InputFile,
299 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000300 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000301 std::vector<const char*> GCCArgs;
302
303 GCCArgs.push_back(GCCPath.c_str());
Chris Lattnereeed9832003-10-14 21:52:52 +0000304
305 // Specify the shared libraries to link in...
306 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i)
307 GCCArgs.push_back(SharedLibs[i].c_str());
308
309 // Specify -x explicitly in case the extension is wonky
Misha Brukman9558c6a2003-09-29 22:39:25 +0000310 GCCArgs.push_back("-x");
311 if (fileType == CFile) {
312 GCCArgs.push_back("c");
313 GCCArgs.push_back("-fno-strict-aliasing");
314 } else {
315 GCCArgs.push_back("assembler");
316 }
317 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
318 GCCArgs.push_back("-o");
Chris Lattnereeed9832003-10-14 21:52:52 +0000319 std::string OutputBinary = getUniqueFilename(ProgramFile+".gcc.exe");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000320 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
321 GCCArgs.push_back("-lm"); // Hard-code the math library...
322 GCCArgs.push_back("-O2"); // Optimize the program a bit...
Brian Gaekec8db76c2003-11-18 06:31:17 +0000323#if defined (HAVE_LINK_R)
Chris Lattner1f0f1622003-10-18 21:54:47 +0000324 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
Brian Gaekec8db76c2003-11-18 06:31:17 +0000325#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000326 GCCArgs.push_back(0); // NULL terminator
327
328 std::cout << "<gcc>" << std::flush;
329 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "/dev/null", "/dev/null",
330 "/dev/null")) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000331 ProcessFailure(GCCPath, &GCCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000332 exit(1);
333 }
334
335 std::vector<const char*> ProgramArgs;
336 ProgramArgs.push_back(OutputBinary.c_str());
337 // Add optional parameters to the running program from Argv
338 for (unsigned i=0, e = Args.size(); i != e; ++i)
339 ProgramArgs.push_back(Args[i].c_str());
340 ProgramArgs.push_back(0); // NULL terminator
341
342 // Now that we have a binary, run it!
343 std::cout << "<program>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000344 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000345 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000346 std::cerr << " " << ProgramArgs[i];
347 std::cerr << "\n";
348 );
Chris Lattner8c56be52004-02-18 20:21:57 +0000349
350 FileRemover OutputBinaryRemover(OutputBinary);
351 return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
352 InputFile, OutputFile, OutputFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000353}
354
Chris Lattner1798e4a2003-10-14 21:07:25 +0000355int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000356 std::string &OutputFile) {
John Criswell7f7d16b2004-01-26 20:59:41 +0000357 OutputFile = getUniqueFilename(InputFile+SHLIBEXT);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000358 // Compile the C/asm file into a shared object
359 const char* GCCArgs[] = {
360 GCCPath.c_str(),
361 "-x", (fileType == AsmFile) ? "assembler" : "c",
362 "-fno-strict-aliasing",
363 InputFile.c_str(), // Specify the input filename...
364#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
365 "-G", // Compile a shared library, `-G' for Sparc
366#else
367 "-shared", // `-shared' for Linux/X86, maybe others
368#endif
369 "-o", OutputFile.c_str(), // Output to the right filename...
370 "-O2", // Optimize the program a bit...
371 0
372 };
373
374 std::cout << "<gcc>" << std::flush;
Chris Lattner1798e4a2003-10-14 21:07:25 +0000375 if (RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null",
376 "/dev/null")) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000377 ProcessFailure(GCCPath, GCCArgs);
Chris Lattner1798e4a2003-10-14 21:07:25 +0000378 return 1;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000379 }
380 return 0;
381}
382
Chris Lattner7915a1e2003-10-14 21:34:11 +0000383/// create - Try to find the `gcc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000384///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000385GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000386 std::string GCCPath = FindExecutable("gcc", ProgramPath);
387 if (GCCPath.empty()) {
388 Message = "Cannot find `gcc' in executable directory or PATH!\n";
389 return 0;
390 }
391
392 Message = "Found gcc: " + GCCPath + "\n";
393 return new GCC(GCCPath);
394}