blob: 0f2e13e20f16394c73e8c17c86b0f50a6a084c73 [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>
Misha Brukman9558c6a2003-09-29 22:39:25 +000021
Brian Gaeked0fde302003-11-11 22:41:34 +000022namespace llvm {
23
Misha Brukman9558c6a2003-09-29 22:39:25 +000024//===---------------------------------------------------------------------===//
25// LLI Implementation of AbstractIntepreter interface
26//
27class LLI : public AbstractInterpreter {
28 std::string LLIPath; // The path to the LLI executable
29public:
30 LLI(const std::string &Path) : LLIPath(Path) { }
31
32
33 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +000034 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +000035 const std::string &InputFile,
36 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +000037 const std::vector<std::string> &SharedLibs =
38 std::vector<std::string>());
Misha Brukman9558c6a2003-09-29 22:39:25 +000039};
40
41int LLI::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +000042 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +000043 const std::string &InputFile,
44 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +000045 const std::vector<std::string> &SharedLibs) {
46 if (!SharedLibs.empty()) {
Misha Brukman9558c6a2003-09-29 22:39:25 +000047 std::cerr << "LLI currently does not support loading shared libraries.\n"
48 << "Exiting.\n";
49 exit(1);
50 }
51
52 std::vector<const char*> LLIArgs;
53 LLIArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +000054 LLIArgs.push_back("-quiet");
55 LLIArgs.push_back("-force-interpreter=true");
56 LLIArgs.push_back(Bytecode.c_str());
57 // Add optional parameters to the running program from Argv
58 for (unsigned i=0, e = Args.size(); i != e; ++i)
59 LLIArgs.push_back(Args[i].c_str());
60 LLIArgs.push_back(0);
61
62 std::cout << "<lli>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +000063 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +000064 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +000065 std::cerr << " " << LLIArgs[i];
66 std::cerr << "\n";
67 );
68 return RunProgramWithTimeout(LLIPath, &LLIArgs[0],
69 InputFile, OutputFile, OutputFile);
70}
71
72// LLI create method - Try to find the LLI executable
Chris Lattner7915a1e2003-10-14 21:34:11 +000073AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
74 std::string &Message) {
75 std::string LLIPath = FindExecutable("lli", ProgPath);
Misha Brukman9558c6a2003-09-29 22:39:25 +000076 if (!LLIPath.empty()) {
77 Message = "Found lli: " + LLIPath + "\n";
78 return new LLI(LLIPath);
79 }
80
81 Message = "Cannot find `lli' in executable directory or PATH!\n";
82 return 0;
83}
84
85//===----------------------------------------------------------------------===//
86// LLC Implementation of AbstractIntepreter interface
87//
Chris Lattner7915a1e2003-10-14 21:34:11 +000088int LLC::OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile) {
Misha Brukman9558c6a2003-09-29 22:39:25 +000089 OutputAsmFile = getUniqueFilename(Bytecode+".llc.s");
90 const char *LLCArgs[] = {
91 LLCPath.c_str(),
92 "-o", OutputAsmFile.c_str(), // Output to the Asm file
93 "-f", // Overwrite as necessary...
94 Bytecode.c_str(), // This is the input bytecode
95 0
96 };
97
98 std::cout << "<llc>" << std::flush;
99 if (RunProgramWithTimeout(LLCPath, LLCArgs, "/dev/null", "/dev/null",
100 "/dev/null")) {
101 // If LLC failed on the bytecode, print error...
102 std::cerr << "Error: `llc' failed!\n";
103 removeFile(OutputAsmFile);
104 return 1;
105 }
106
107 return 0;
108}
109
110int LLC::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000111 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000112 const std::string &InputFile,
113 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000114 const std::vector<std::string> &SharedLibs) {
115
Misha Brukman9558c6a2003-09-29 22:39:25 +0000116 std::string OutputAsmFile;
117 if (OutputAsm(Bytecode, OutputAsmFile)) {
118 std::cerr << "Could not generate asm code with `llc', exiting.\n";
119 exit(1);
120 }
121
122 // Assuming LLC worked, compile the result with GCC and run it.
Chris Lattner7915a1e2003-10-14 21:34:11 +0000123 int Result = gcc->ExecuteProgram(OutputAsmFile, Args, GCC::AsmFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000124 InputFile, OutputFile, SharedLibs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000125 removeFile(OutputAsmFile);
126 return Result;
127}
128
Chris Lattner7915a1e2003-10-14 21:34:11 +0000129/// createLLC - Try to find the LLC executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000130///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000131LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
132 std::string &Message) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000133 std::string LLCPath = FindExecutable("llc", ProgramPath);
134 if (LLCPath.empty()) {
135 Message = "Cannot find `llc' in executable directory or PATH!\n";
136 return 0;
137 }
138
139 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000140 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000141 if (!gcc) {
142 std::cerr << Message << "\n";
143 exit(1);
144 }
145 return new LLC(LLCPath, gcc);
146}
147
148//===---------------------------------------------------------------------===//
149// JIT Implementation of AbstractIntepreter interface
150//
151class JIT : public AbstractInterpreter {
152 std::string LLIPath; // The path to the LLI executable
153public:
154 JIT(const std::string &Path) : LLIPath(Path) { }
155
156
157 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000158 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000159 const std::string &InputFile,
160 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000161 const std::vector<std::string> &SharedLibs =
162 std::vector<std::string>());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000163};
164
165int JIT::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000166 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000167 const std::string &InputFile,
168 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000169 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000170 // Construct a vector of parameters, incorporating those from the command-line
171 std::vector<const char*> JITArgs;
172 JITArgs.push_back(LLIPath.c_str());
173 JITArgs.push_back("-quiet");
174 JITArgs.push_back("-force-interpreter=false");
Chris Lattnereeed9832003-10-14 21:52:52 +0000175
176 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000177 JITArgs.push_back("-load");
Chris Lattnereeed9832003-10-14 21:52:52 +0000178 JITArgs.push_back(SharedLibs[i].c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000179 }
180 JITArgs.push_back(Bytecode.c_str());
181 // Add optional parameters to the running program from Argv
182 for (unsigned i=0, e = Args.size(); i != e; ++i)
183 JITArgs.push_back(Args[i].c_str());
184 JITArgs.push_back(0);
185
186 std::cout << "<jit>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000187 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000188 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000189 std::cerr << " " << JITArgs[i];
190 std::cerr << "\n";
191 );
192 DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
193 return RunProgramWithTimeout(LLIPath, &JITArgs[0],
194 InputFile, OutputFile, OutputFile);
195}
196
Chris Lattner7915a1e2003-10-14 21:34:11 +0000197/// createJIT - Try to find the LLI executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000198///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000199AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
200 std::string &Message) {
201 std::string LLIPath = FindExecutable("lli", ProgPath);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000202 if (!LLIPath.empty()) {
203 Message = "Found lli: " + LLIPath + "\n";
204 return new JIT(LLIPath);
205 }
206
207 Message = "Cannot find `lli' in executable directory or PATH!\n";
208 return 0;
209}
210
211int CBE::OutputC(const std::string &Bytecode,
212 std::string &OutputCFile) {
213 OutputCFile = getUniqueFilename(Bytecode+".cbe.c");
214 const char *DisArgs[] = {
215 DISPath.c_str(),
216 "-o", OutputCFile.c_str(), // Output to the C file
217 "-c", // Output to C
218 "-f", // Overwrite as necessary...
219 Bytecode.c_str(), // This is the input bytecode
220 0
221 };
222
223 std::cout << "<cbe>" << std::flush;
224 if (RunProgramWithTimeout(DISPath, DisArgs, "/dev/null", "/dev/null",
225 "/dev/null")) {
226 // If dis failed on the bytecode, print error...
227 std::cerr << "Error: `llvm-dis -c' failed!\n";
228 return 1;
229 }
230
231 return 0;
232}
233
234int CBE::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000235 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000236 const std::string &InputFile,
237 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000238 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000239 std::string OutputCFile;
240 if (OutputC(Bytecode, OutputCFile)) {
241 std::cerr << "Could not generate C code with `llvm-dis', exiting.\n";
242 exit(1);
243 }
244
Chris Lattner7915a1e2003-10-14 21:34:11 +0000245 int Result = gcc->ExecuteProgram(OutputCFile, Args, GCC::CFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000246 InputFile, OutputFile, SharedLibs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000247 removeFile(OutputCFile);
248
249 return Result;
250}
251
Chris Lattner7915a1e2003-10-14 21:34:11 +0000252/// createCBE - Try to find the 'llvm-dis' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000253///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000254CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
255 std::string &Message) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000256 std::string DISPath = FindExecutable("llvm-dis", ProgramPath);
257 if (DISPath.empty()) {
258 Message =
259 "Cannot find `llvm-dis' in executable directory or PATH!\n";
260 return 0;
261 }
262
263 Message = "Found llvm-dis: " + DISPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000264 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000265 if (!gcc) {
266 std::cerr << Message << "\n";
267 exit(1);
268 }
269 return new CBE(DISPath, gcc);
270}
271
272//===---------------------------------------------------------------------===//
273// GCC abstraction
274//
Misha Brukman9558c6a2003-09-29 22:39:25 +0000275int GCC::ExecuteProgram(const std::string &ProgramFile,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000276 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000277 FileType fileType,
278 const std::string &InputFile,
279 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000280 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000281 std::vector<const char*> GCCArgs;
282
283 GCCArgs.push_back(GCCPath.c_str());
Chris Lattnereeed9832003-10-14 21:52:52 +0000284
285 // Specify the shared libraries to link in...
286 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i)
287 GCCArgs.push_back(SharedLibs[i].c_str());
288
289 // Specify -x explicitly in case the extension is wonky
Misha Brukman9558c6a2003-09-29 22:39:25 +0000290 GCCArgs.push_back("-x");
291 if (fileType == CFile) {
292 GCCArgs.push_back("c");
293 GCCArgs.push_back("-fno-strict-aliasing");
294 } else {
295 GCCArgs.push_back("assembler");
296 }
297 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
298 GCCArgs.push_back("-o");
Chris Lattnereeed9832003-10-14 21:52:52 +0000299 std::string OutputBinary = getUniqueFilename(ProgramFile+".gcc.exe");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000300 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
301 GCCArgs.push_back("-lm"); // Hard-code the math library...
302 GCCArgs.push_back("-O2"); // Optimize the program a bit...
Brian Gaekec8db76c2003-11-18 06:31:17 +0000303#if defined (HAVE_LINK_R)
Chris Lattner1f0f1622003-10-18 21:54:47 +0000304 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
Brian Gaekec8db76c2003-11-18 06:31:17 +0000305#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000306 GCCArgs.push_back(0); // NULL terminator
307
308 std::cout << "<gcc>" << std::flush;
309 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "/dev/null", "/dev/null",
310 "/dev/null")) {
311 ProcessFailure(&GCCArgs[0]);
312 exit(1);
313 }
314
315 std::vector<const char*> ProgramArgs;
316 ProgramArgs.push_back(OutputBinary.c_str());
317 // Add optional parameters to the running program from Argv
318 for (unsigned i=0, e = Args.size(); i != e; ++i)
319 ProgramArgs.push_back(Args[i].c_str());
320 ProgramArgs.push_back(0); // NULL terminator
321
322 // Now that we have a binary, run it!
323 std::cout << "<program>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000324 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000325 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000326 std::cerr << " " << ProgramArgs[i];
327 std::cerr << "\n";
328 );
329 int ProgramResult = RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
330 InputFile, OutputFile, OutputFile);
331 removeFile(OutputBinary);
332 return ProgramResult;
333}
334
Chris Lattner1798e4a2003-10-14 21:07:25 +0000335int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000336 std::string &OutputFile) {
337 OutputFile = getUniqueFilename(InputFile+".so");
338 // Compile the C/asm file into a shared object
339 const char* GCCArgs[] = {
340 GCCPath.c_str(),
341 "-x", (fileType == AsmFile) ? "assembler" : "c",
342 "-fno-strict-aliasing",
343 InputFile.c_str(), // Specify the input filename...
344#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
345 "-G", // Compile a shared library, `-G' for Sparc
346#else
347 "-shared", // `-shared' for Linux/X86, maybe others
348#endif
349 "-o", OutputFile.c_str(), // Output to the right filename...
350 "-O2", // Optimize the program a bit...
351 0
352 };
353
354 std::cout << "<gcc>" << std::flush;
Chris Lattner1798e4a2003-10-14 21:07:25 +0000355 if (RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null",
356 "/dev/null")) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000357 ProcessFailure(GCCArgs);
Chris Lattner1798e4a2003-10-14 21:07:25 +0000358 return 1;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000359 }
360 return 0;
361}
362
363void GCC::ProcessFailure(const char** GCCArgs) {
364 std::cerr << "\n*** Error: invocation of the C compiler failed!\n";
365 for (const char **Arg = GCCArgs; *Arg; ++Arg)
366 std::cerr << " " << *Arg;
367 std::cerr << "\n";
368
369 // Rerun the compiler, capturing any error messages to print them.
370 std::string ErrorFilename = getUniqueFilename("gcc.errors");
371 RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", ErrorFilename.c_str(),
372 ErrorFilename.c_str());
373
374 // Print out the error messages generated by GCC if possible...
375 std::ifstream ErrorFile(ErrorFilename.c_str());
376 if (ErrorFile) {
377 std::copy(std::istreambuf_iterator<char>(ErrorFile),
378 std::istreambuf_iterator<char>(),
379 std::ostreambuf_iterator<char>(std::cerr));
380 ErrorFile.close();
381 std::cerr << "\n";
382 }
383
384 removeFile(ErrorFilename);
385}
386
Chris Lattner7915a1e2003-10-14 21:34:11 +0000387/// create - Try to find the `gcc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000388///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000389GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000390 std::string GCCPath = FindExecutable("gcc", ProgramPath);
391 if (GCCPath.empty()) {
392 Message = "Cannot find `gcc' in executable directory or PATH!\n";
393 return 0;
394 }
395
396 Message = "Found gcc: " + GCCPath + "\n";
397 return new GCC(GCCPath);
398}
Brian Gaeked0fde302003-11-11 22:41:34 +0000399
400} // End llvm namespace