blob: 6a332738063596ceb30842c713b6c2139397e71a [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;
26 OS << "\n*** Error running tool:\n";
27 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
129int LLC::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000130 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000131 const std::string &InputFile,
132 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000133 const std::vector<std::string> &SharedLibs) {
134
Misha Brukman9558c6a2003-09-29 22:39:25 +0000135 std::string OutputAsmFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000136 OutputAsm(Bytecode, OutputAsmFile);
137 FileRemover OutFileRemover(OutputAsmFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000138
139 // Assuming LLC worked, compile the result with GCC and run it.
Chris Lattner8c56be52004-02-18 20:21:57 +0000140 return gcc->ExecuteProgram(OutputAsmFile, Args, GCC::AsmFile,
141 InputFile, OutputFile, SharedLibs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000142}
143
Chris Lattner7915a1e2003-10-14 21:34:11 +0000144/// createLLC - Try to find the LLC executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000145///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000146LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
147 std::string &Message) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000148 std::string LLCPath = FindExecutable("llc", ProgramPath);
149 if (LLCPath.empty()) {
150 Message = "Cannot find `llc' in executable directory or PATH!\n";
151 return 0;
152 }
153
154 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000155 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000156 if (!gcc) {
157 std::cerr << Message << "\n";
158 exit(1);
159 }
160 return new LLC(LLCPath, gcc);
161}
162
163//===---------------------------------------------------------------------===//
164// JIT Implementation of AbstractIntepreter interface
165//
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000166namespace {
167 class JIT : public AbstractInterpreter {
168 std::string LLIPath; // The path to the LLI executable
169 public:
170 JIT(const std::string &Path) : LLIPath(Path) { }
171
172
173 virtual int ExecuteProgram(const std::string &Bytecode,
174 const std::vector<std::string> &Args,
175 const std::string &InputFile,
176 const std::string &OutputFile,
177 const std::vector<std::string> &SharedLibs =
Chris Lattnereeed9832003-10-14 21:52:52 +0000178 std::vector<std::string>());
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000179 };
180}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000181
182int JIT::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000183 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000184 const std::string &InputFile,
185 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000186 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000187 // Construct a vector of parameters, incorporating those from the command-line
188 std::vector<const char*> JITArgs;
189 JITArgs.push_back(LLIPath.c_str());
190 JITArgs.push_back("-quiet");
191 JITArgs.push_back("-force-interpreter=false");
Chris Lattnereeed9832003-10-14 21:52:52 +0000192
193 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000194 JITArgs.push_back("-load");
Chris Lattnereeed9832003-10-14 21:52:52 +0000195 JITArgs.push_back(SharedLibs[i].c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000196 }
197 JITArgs.push_back(Bytecode.c_str());
198 // Add optional parameters to the running program from Argv
199 for (unsigned i=0, e = Args.size(); i != e; ++i)
200 JITArgs.push_back(Args[i].c_str());
201 JITArgs.push_back(0);
202
203 std::cout << "<jit>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000204 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000205 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000206 std::cerr << " " << JITArgs[i];
207 std::cerr << "\n";
208 );
209 DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
210 return RunProgramWithTimeout(LLIPath, &JITArgs[0],
211 InputFile, OutputFile, OutputFile);
212}
213
Chris Lattner7915a1e2003-10-14 21:34:11 +0000214/// createJIT - Try to find the LLI executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000215///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000216AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
217 std::string &Message) {
218 std::string LLIPath = FindExecutable("lli", ProgPath);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000219 if (!LLIPath.empty()) {
220 Message = "Found lli: " + LLIPath + "\n";
221 return new JIT(LLIPath);
222 }
223
224 Message = "Cannot find `lli' in executable directory or PATH!\n";
225 return 0;
226}
227
Chris Lattner8c56be52004-02-18 20:21:57 +0000228void CBE::OutputC(const std::string &Bytecode,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000229 std::string &OutputCFile) {
230 OutputCFile = getUniqueFilename(Bytecode+".cbe.c");
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000231 const char *LLCArgs[] = {
Chris Lattner9915cd92004-02-17 06:40:06 +0000232 LLCPath.c_str(),
Misha Brukman9558c6a2003-09-29 22:39:25 +0000233 "-o", OutputCFile.c_str(), // Output to the C file
Chris Lattner9915cd92004-02-17 06:40:06 +0000234 "-march=c", // Output to C
Misha Brukman9558c6a2003-09-29 22:39:25 +0000235 "-f", // Overwrite as necessary...
236 Bytecode.c_str(), // This is the input bytecode
237 0
238 };
239
240 std::cout << "<cbe>" << std::flush;
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000241 if (RunProgramWithTimeout(LLCPath, LLCArgs, "/dev/null", "/dev/null",
Chris Lattner8c56be52004-02-18 20:21:57 +0000242 "/dev/null"))
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000243 ProcessFailure(LLCPath, LLCArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000244}
245
246int CBE::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000247 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000248 const std::string &InputFile,
249 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000250 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000251 std::string OutputCFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000252 OutputC(Bytecode, OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000253
Chris Lattner8c56be52004-02-18 20:21:57 +0000254 FileRemover CFileRemove(OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000255
Chris Lattner8c56be52004-02-18 20:21:57 +0000256 return gcc->ExecuteProgram(OutputCFile, Args, GCC::CFile,
257 InputFile, OutputFile, SharedLibs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000258}
259
Chris Lattner9915cd92004-02-17 06:40:06 +0000260/// createCBE - Try to find the 'llc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000261///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000262CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
263 std::string &Message) {
Chris Lattner9915cd92004-02-17 06:40:06 +0000264 std::string LLCPath = FindExecutable("llc", ProgramPath);
265 if (LLCPath.empty()) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000266 Message =
Chris Lattner9915cd92004-02-17 06:40:06 +0000267 "Cannot find `llc' in executable directory or PATH!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000268 return 0;
269 }
270
Chris Lattner9915cd92004-02-17 06:40:06 +0000271 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000272 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000273 if (!gcc) {
274 std::cerr << Message << "\n";
275 exit(1);
276 }
Chris Lattner9915cd92004-02-17 06:40:06 +0000277 return new CBE(LLCPath, gcc);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000278}
279
280//===---------------------------------------------------------------------===//
281// GCC abstraction
282//
Misha Brukman9558c6a2003-09-29 22:39:25 +0000283int GCC::ExecuteProgram(const std::string &ProgramFile,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000284 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000285 FileType fileType,
286 const std::string &InputFile,
287 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000288 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000289 std::vector<const char*> GCCArgs;
290
291 GCCArgs.push_back(GCCPath.c_str());
Chris Lattnereeed9832003-10-14 21:52:52 +0000292
293 // Specify the shared libraries to link in...
294 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i)
295 GCCArgs.push_back(SharedLibs[i].c_str());
296
297 // Specify -x explicitly in case the extension is wonky
Misha Brukman9558c6a2003-09-29 22:39:25 +0000298 GCCArgs.push_back("-x");
299 if (fileType == CFile) {
300 GCCArgs.push_back("c");
301 GCCArgs.push_back("-fno-strict-aliasing");
302 } else {
303 GCCArgs.push_back("assembler");
304 }
305 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
306 GCCArgs.push_back("-o");
Chris Lattnereeed9832003-10-14 21:52:52 +0000307 std::string OutputBinary = getUniqueFilename(ProgramFile+".gcc.exe");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000308 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
309 GCCArgs.push_back("-lm"); // Hard-code the math library...
310 GCCArgs.push_back("-O2"); // Optimize the program a bit...
Brian Gaekec8db76c2003-11-18 06:31:17 +0000311#if defined (HAVE_LINK_R)
Chris Lattner1f0f1622003-10-18 21:54:47 +0000312 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
Brian Gaekec8db76c2003-11-18 06:31:17 +0000313#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000314 GCCArgs.push_back(0); // NULL terminator
315
316 std::cout << "<gcc>" << std::flush;
317 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "/dev/null", "/dev/null",
318 "/dev/null")) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000319 ProcessFailure(GCCPath, &GCCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000320 exit(1);
321 }
322
323 std::vector<const char*> ProgramArgs;
324 ProgramArgs.push_back(OutputBinary.c_str());
325 // Add optional parameters to the running program from Argv
326 for (unsigned i=0, e = Args.size(); i != e; ++i)
327 ProgramArgs.push_back(Args[i].c_str());
328 ProgramArgs.push_back(0); // NULL terminator
329
330 // Now that we have a binary, run it!
331 std::cout << "<program>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000332 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000333 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000334 std::cerr << " " << ProgramArgs[i];
335 std::cerr << "\n";
336 );
Chris Lattner8c56be52004-02-18 20:21:57 +0000337
338 FileRemover OutputBinaryRemover(OutputBinary);
339 return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
340 InputFile, OutputFile, OutputFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000341}
342
Chris Lattner1798e4a2003-10-14 21:07:25 +0000343int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000344 std::string &OutputFile) {
John Criswell7f7d16b2004-01-26 20:59:41 +0000345 OutputFile = getUniqueFilename(InputFile+SHLIBEXT);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000346 // Compile the C/asm file into a shared object
347 const char* GCCArgs[] = {
348 GCCPath.c_str(),
349 "-x", (fileType == AsmFile) ? "assembler" : "c",
350 "-fno-strict-aliasing",
351 InputFile.c_str(), // Specify the input filename...
352#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
353 "-G", // Compile a shared library, `-G' for Sparc
354#else
355 "-shared", // `-shared' for Linux/X86, maybe others
356#endif
357 "-o", OutputFile.c_str(), // Output to the right filename...
358 "-O2", // Optimize the program a bit...
359 0
360 };
361
362 std::cout << "<gcc>" << std::flush;
Chris Lattner1798e4a2003-10-14 21:07:25 +0000363 if (RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null",
364 "/dev/null")) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000365 ProcessFailure(GCCPath, GCCArgs);
Chris Lattner1798e4a2003-10-14 21:07:25 +0000366 return 1;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000367 }
368 return 0;
369}
370
Chris Lattner7915a1e2003-10-14 21:34:11 +0000371/// create - Try to find the `gcc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000372///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000373GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000374 std::string GCCPath = FindExecutable("gcc", ProgramPath);
375 if (GCCPath.empty()) {
376 Message = "Cannot find `gcc' in executable directory or PATH!\n";
377 return 0;
378 }
379
380 Message = "Found gcc: " + GCCPath + "\n";
381 return new GCC(GCCPath);
382}