blob: f516986f133e35f2053f2d0745c89f223bbfe91a [file] [log] [blame]
Chris Lattner7915a1e2003-10-14 21:34:11 +00001//===-- ToolRunner.cpp ----------------------------------------------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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"
Reid Spencer551ccae2004-09-01 22:55:40 +000016#include "llvm/Config/config.h" // for HAVE_LINK_R
Chris Lattner45495c52005-02-13 23:13:47 +000017#include "llvm/System/Program.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000018#include "llvm/Support/Debug.h"
19#include "llvm/Support/FileUtilities.h"
Chris Lattner7915a1e2003-10-14 21:34:11 +000020#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
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000024ToolExecutionError::~ToolExecutionError() throw() { }
25
Chris Lattner45495c52005-02-13 23:13:47 +000026/// RunProgramWithTimeout - This function provides an alternate interface to the
27/// sys::Program::ExecuteAndWait interface.
28/// @see sys:Program::ExecuteAndWait
29static int RunProgramWithTimeout(const sys::Path &ProgramPath,
30 const char **Args,
31 const sys::Path &StdInFile,
32 const sys::Path &StdOutFile,
33 const sys::Path &StdErrFile,
34 unsigned NumSeconds = 0) {
35 const sys::Path* redirects[3];
36 redirects[0] = &StdInFile;
37 redirects[1] = &StdOutFile;
38 redirects[2] = &StdErrFile;
Misha Brukmanf976c852005-04-21 22:55:34 +000039
40 return
Chris Lattner45495c52005-02-13 23:13:47 +000041 sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects, NumSeconds);
42}
43
44
45
Reid Spencerb31baa82004-12-19 18:00:21 +000046static void ProcessFailure(sys::Path ProgPath, const char** Args) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +000047 std::ostringstream OS;
Chris Lattnera3de1172004-02-18 20:58:00 +000048 OS << "\nError running tool:\n ";
Chris Lattner89bf9ea2004-02-18 20:38:00 +000049 for (const char **Arg = Args; *Arg; ++Arg)
50 OS << " " << *Arg;
51 OS << "\n";
52
53 // Rerun the compiler, capturing any error messages to print them.
Reid Spencercda985e2004-12-15 01:51:56 +000054 sys::Path ErrorFilename("error_messages");
55 ErrorFilename.makeUnique();
Reid Spencerb31baa82004-12-19 18:00:21 +000056 RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename,
57 ErrorFilename);
Chris Lattner89bf9ea2004-02-18 20:38:00 +000058
59 // Print out the error messages generated by GCC if possible...
60 std::ifstream ErrorFile(ErrorFilename.c_str());
61 if (ErrorFile) {
62 std::copy(std::istreambuf_iterator<char>(ErrorFile),
63 std::istreambuf_iterator<char>(),
64 std::ostreambuf_iterator<char>(OS));
65 ErrorFile.close();
66 }
67
Reid Spencera229c5c2005-07-08 03:08:58 +000068 ErrorFilename.eraseFromDisk();
Chris Lattner89bf9ea2004-02-18 20:38:00 +000069 throw ToolExecutionError(OS.str());
70}
71
Misha Brukman9558c6a2003-09-29 22:39:25 +000072//===---------------------------------------------------------------------===//
73// LLI Implementation of AbstractIntepreter interface
74//
Chris Lattner2cdd21c2003-12-14 21:35:53 +000075namespace {
76 class LLI : public AbstractInterpreter {
77 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +000078 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +000079 public:
Brian Gaeked11577b2004-05-04 21:09:01 +000080 LLI(const std::string &Path, const std::vector<std::string> *Args)
81 : LLIPath(Path) {
82 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +000083 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +000084 }
Misha Brukmanf976c852005-04-21 22:55:34 +000085
Chris Lattner2cdd21c2003-12-14 21:35:53 +000086 virtual int ExecuteProgram(const std::string &Bytecode,
87 const std::vector<std::string> &Args,
88 const std::string &InputFile,
89 const std::string &OutputFile,
Misha Brukmanf976c852005-04-21 22:55:34 +000090 const std::vector<std::string> &SharedLibs =
Chris Lattnere96b2ed2004-07-24 07:49:11 +000091 std::vector<std::string>(),
92 unsigned Timeout = 0);
Chris Lattner2cdd21c2003-12-14 21:35:53 +000093 };
94}
Misha Brukman9558c6a2003-09-29 22:39:25 +000095
96int LLI::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +000097 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +000098 const std::string &InputFile,
99 const std::string &OutputFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000100 const std::vector<std::string> &SharedLibs,
101 unsigned Timeout) {
Chris Lattner8c56be52004-02-18 20:21:57 +0000102 if (!SharedLibs.empty())
103 throw ToolExecutionError("LLI currently does not support "
104 "loading shared libraries.");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000105
106 std::vector<const char*> LLIArgs;
107 LLIArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000108 LLIArgs.push_back("-force-interpreter=true");
Brian Gaeked11577b2004-05-04 21:09:01 +0000109
110 // Add any extra LLI args.
111 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
112 LLIArgs.push_back(ToolArgs[i].c_str());
113
Misha Brukman9558c6a2003-09-29 22:39:25 +0000114 LLIArgs.push_back(Bytecode.c_str());
115 // Add optional parameters to the running program from Argv
116 for (unsigned i=0, e = Args.size(); i != e; ++i)
117 LLIArgs.push_back(Args[i].c_str());
118 LLIArgs.push_back(0);
119
120 std::cout << "<lli>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000121 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000122 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000123 std::cerr << " " << LLIArgs[i];
124 std::cerr << "\n";
125 );
Reid Spencerb31baa82004-12-19 18:00:21 +0000126 return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000127 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Reid Spencerb31baa82004-12-19 18:00:21 +0000128 Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000129}
130
131// LLI create method - Try to find the LLI executable
Chris Lattner7915a1e2003-10-14 21:34:11 +0000132AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000133 std::string &Message,
134 const std::vector<std::string> *ToolArgs) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000135 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000136 if (!LLIPath.empty()) {
137 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000138 return new LLI(LLIPath, ToolArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000139 }
140
141 Message = "Cannot find `lli' in executable directory or PATH!\n";
142 return 0;
143}
144
145//===----------------------------------------------------------------------===//
146// LLC Implementation of AbstractIntepreter interface
147//
Reid Spencer9ac14182004-12-16 23:01:34 +0000148void LLC::OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000149 sys::Path uniqueFile(Bytecode+".llc.s");
150 uniqueFile.makeUnique();
Reid Spencer9ac14182004-12-16 23:01:34 +0000151 OutputAsmFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000152 std::vector<const char *> LLCArgs;
153 LLCArgs.push_back (LLCPath.c_str());
154
155 // Add any extra LLC args.
156 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
157 LLCArgs.push_back(ToolArgs[i].c_str());
158
159 LLCArgs.push_back ("-o");
160 LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file
161 LLCArgs.push_back ("-f"); // Overwrite as necessary...
162 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
163 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000164
165 std::cout << "<llc>" << std::flush;
Brian Gaeked11577b2004-05-04 21:09:01 +0000166 DEBUG(std::cerr << "\nAbout to run:\t";
167 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
168 std::cerr << " " << LLCArgs[i];
169 std::cerr << "\n";
170 );
Misha Brukmanf976c852005-04-21 22:55:34 +0000171 if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0],
Reid Spencerb31baa82004-12-19 18:00:21 +0000172 sys::Path(), sys::Path(), sys::Path()))
173 ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000174}
175
Chris Lattner9cbbee32004-02-18 23:24:41 +0000176void LLC::compileProgram(const std::string &Bytecode) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000177 sys::Path OutputAsmFile;
Chris Lattner9cbbee32004-02-18 23:24:41 +0000178 OutputAsm(Bytecode, OutputAsmFile);
Reid Spencera229c5c2005-07-08 03:08:58 +0000179 OutputAsmFile.eraseFromDisk();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000180}
181
Misha Brukman9558c6a2003-09-29 22:39:25 +0000182int LLC::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 Lattnere96b2ed2004-07-24 07:49:11 +0000186 const std::vector<std::string> &SharedLibs,
187 unsigned Timeout) {
Chris Lattnereeed9832003-10-14 21:52:52 +0000188
Reid Spencer9ac14182004-12-16 23:01:34 +0000189 sys::Path OutputAsmFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000190 OutputAsm(Bytecode, OutputAsmFile);
191 FileRemover OutFileRemover(OutputAsmFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000192
193 // Assuming LLC worked, compile the result with GCC and run it.
Reid Spencer9ac14182004-12-16 23:01:34 +0000194 return gcc->ExecuteProgram(OutputAsmFile.toString(), Args, GCC::AsmFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000195 InputFile, OutputFile, SharedLibs, Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000196}
197
Chris Lattner7915a1e2003-10-14 21:34:11 +0000198/// createLLC - Try to find the LLC executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000199///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000200LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000201 std::string &Message,
202 const std::vector<std::string> *Args) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000203 std::string LLCPath = FindExecutable("llc", ProgramPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000204 if (LLCPath.empty()) {
205 Message = "Cannot find `llc' in executable directory or PATH!\n";
206 return 0;
207 }
208
209 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000210 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000211 if (!gcc) {
212 std::cerr << Message << "\n";
213 exit(1);
214 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000215 return new LLC(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000216}
217
218//===---------------------------------------------------------------------===//
219// JIT Implementation of AbstractIntepreter interface
220//
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000221namespace {
222 class JIT : public AbstractInterpreter {
223 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000224 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000225 public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000226 JIT(const std::string &Path, const std::vector<std::string> *Args)
227 : LLIPath(Path) {
228 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000229 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000230 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000231
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000232 virtual int ExecuteProgram(const std::string &Bytecode,
233 const std::vector<std::string> &Args,
234 const std::string &InputFile,
235 const std::string &OutputFile,
Misha Brukmanf976c852005-04-21 22:55:34 +0000236 const std::vector<std::string> &SharedLibs =
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000237 std::vector<std::string>(), unsigned Timeout =0);
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000238 };
239}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000240
241int JIT::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000242 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000243 const std::string &InputFile,
244 const std::string &OutputFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000245 const std::vector<std::string> &SharedLibs,
246 unsigned Timeout) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000247 // Construct a vector of parameters, incorporating those from the command-line
248 std::vector<const char*> JITArgs;
249 JITArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000250 JITArgs.push_back("-force-interpreter=false");
Chris Lattnereeed9832003-10-14 21:52:52 +0000251
Brian Gaeked11577b2004-05-04 21:09:01 +0000252 // Add any extra LLI args.
253 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
254 JITArgs.push_back(ToolArgs[i].c_str());
255
Chris Lattnereeed9832003-10-14 21:52:52 +0000256 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000257 JITArgs.push_back("-load");
Chris Lattnereeed9832003-10-14 21:52:52 +0000258 JITArgs.push_back(SharedLibs[i].c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000259 }
260 JITArgs.push_back(Bytecode.c_str());
261 // Add optional parameters to the running program from Argv
262 for (unsigned i=0, e = Args.size(); i != e; ++i)
263 JITArgs.push_back(Args[i].c_str());
264 JITArgs.push_back(0);
265
266 std::cout << "<jit>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000267 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000268 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000269 std::cerr << " " << JITArgs[i];
270 std::cerr << "\n";
271 );
272 DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
Reid Spencerb31baa82004-12-19 18:00:21 +0000273 return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000274 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Reid Spencerb31baa82004-12-19 18:00:21 +0000275 Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000276}
277
Chris Lattner7915a1e2003-10-14 21:34:11 +0000278/// createJIT - Try to find the LLI executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000279///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000280AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000281 std::string &Message, const std::vector<std::string> *Args) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000282 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000283 if (!LLIPath.empty()) {
284 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000285 return new JIT(LLIPath, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000286 }
287
288 Message = "Cannot find `lli' in executable directory or PATH!\n";
289 return 0;
290}
291
Reid Spencer9ac14182004-12-16 23:01:34 +0000292void CBE::OutputC(const std::string &Bytecode, sys::Path& OutputCFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000293 sys::Path uniqueFile(Bytecode+".cbe.c");
294 uniqueFile.makeUnique();
Reid Spencer9ac14182004-12-16 23:01:34 +0000295 OutputCFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000296 std::vector<const char *> LLCArgs;
297 LLCArgs.push_back (LLCPath.c_str());
298
299 // Add any extra LLC args.
300 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
301 LLCArgs.push_back(ToolArgs[i].c_str());
302
303 LLCArgs.push_back ("-o");
304 LLCArgs.push_back (OutputCFile.c_str()); // Output to the C file
305 LLCArgs.push_back ("-march=c"); // Output C language
306 LLCArgs.push_back ("-f"); // Overwrite as necessary...
307 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
308 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000309
310 std::cout << "<cbe>" << std::flush;
Brian Gaeked11577b2004-05-04 21:09:01 +0000311 DEBUG(std::cerr << "\nAbout to run:\t";
312 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
313 std::cerr << " " << LLCArgs[i];
314 std::cerr << "\n";
315 );
Misha Brukmanf976c852005-04-21 22:55:34 +0000316 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(),
Reid Spencerb31baa82004-12-19 18:00:21 +0000317 sys::Path()))
Brian Gaeked11577b2004-05-04 21:09:01 +0000318 ProcessFailure(LLCPath, &LLCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000319}
320
Chris Lattner9cbbee32004-02-18 23:24:41 +0000321void CBE::compileProgram(const std::string &Bytecode) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000322 sys::Path OutputCFile;
Chris Lattner9cbbee32004-02-18 23:24:41 +0000323 OutputC(Bytecode, OutputCFile);
Reid Spencera229c5c2005-07-08 03:08:58 +0000324 OutputCFile.eraseFromDisk();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000325}
326
Misha Brukman9558c6a2003-09-29 22:39:25 +0000327int CBE::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000328 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000329 const std::string &InputFile,
330 const std::string &OutputFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000331 const std::vector<std::string> &SharedLibs,
332 unsigned Timeout) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000333 sys::Path OutputCFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000334 OutputC(Bytecode, OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000335
Chris Lattner8c56be52004-02-18 20:21:57 +0000336 FileRemover CFileRemove(OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000337
Misha Brukmanf976c852005-04-21 22:55:34 +0000338 return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000339 InputFile, OutputFile, SharedLibs, Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000340}
341
Chris Lattner9915cd92004-02-17 06:40:06 +0000342/// createCBE - Try to find the 'llc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000343///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000344CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000345 std::string &Message,
346 const std::vector<std::string> *Args) {
Reid Spencerb31baa82004-12-19 18:00:21 +0000347 sys::Path LLCPath = FindExecutable("llc", ProgramPath);
348 if (LLCPath.isEmpty()) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000349 Message =
Chris Lattner9915cd92004-02-17 06:40:06 +0000350 "Cannot find `llc' in executable directory or PATH!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000351 return 0;
352 }
353
Reid Spencerb31baa82004-12-19 18:00:21 +0000354 Message = "Found llc: " + LLCPath.toString() + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000355 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000356 if (!gcc) {
357 std::cerr << Message << "\n";
358 exit(1);
359 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000360 return new CBE(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000361}
362
363//===---------------------------------------------------------------------===//
364// GCC abstraction
365//
Misha Brukman9558c6a2003-09-29 22:39:25 +0000366int GCC::ExecuteProgram(const std::string &ProgramFile,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000367 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000368 FileType fileType,
369 const std::string &InputFile,
370 const std::string &OutputFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000371 const std::vector<std::string> &SharedLibs,
372 unsigned Timeout) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000373 std::vector<const char*> GCCArgs;
374
375 GCCArgs.push_back(GCCPath.c_str());
Chris Lattnereeed9832003-10-14 21:52:52 +0000376
377 // Specify the shared libraries to link in...
378 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i)
379 GCCArgs.push_back(SharedLibs[i].c_str());
Misha Brukmanf976c852005-04-21 22:55:34 +0000380
Chris Lattnereeed9832003-10-14 21:52:52 +0000381 // Specify -x explicitly in case the extension is wonky
Misha Brukman9558c6a2003-09-29 22:39:25 +0000382 GCCArgs.push_back("-x");
383 if (fileType == CFile) {
384 GCCArgs.push_back("c");
385 GCCArgs.push_back("-fno-strict-aliasing");
386 } else {
387 GCCArgs.push_back("assembler");
388 }
389 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
390 GCCArgs.push_back("-o");
Reid Spencercda985e2004-12-15 01:51:56 +0000391 sys::Path OutputBinary (ProgramFile+".gcc.exe");
392 OutputBinary.makeUnique();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000393 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
394 GCCArgs.push_back("-lm"); // Hard-code the math library...
395 GCCArgs.push_back("-O2"); // Optimize the program a bit...
Brian Gaekec8db76c2003-11-18 06:31:17 +0000396#if defined (HAVE_LINK_R)
Chris Lattner1f0f1622003-10-18 21:54:47 +0000397 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
Brian Gaekec8db76c2003-11-18 06:31:17 +0000398#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000399 GCCArgs.push_back(0); // NULL terminator
400
401 std::cout << "<gcc>" << std::flush;
Reid Spencerb31baa82004-12-19 18:00:21 +0000402 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
403 sys::Path())) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000404 ProcessFailure(GCCPath, &GCCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000405 exit(1);
406 }
407
408 std::vector<const char*> ProgramArgs;
Chris Lattner45495c52005-02-13 23:13:47 +0000409
Misha Brukman9558c6a2003-09-29 22:39:25 +0000410 ProgramArgs.push_back(OutputBinary.c_str());
411 // Add optional parameters to the running program from Argv
412 for (unsigned i=0, e = Args.size(); i != e; ++i)
413 ProgramArgs.push_back(Args[i].c_str());
414 ProgramArgs.push_back(0); // NULL terminator
415
416 // Now that we have a binary, run it!
417 std::cout << "<program>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000418 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000419 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000420 std::cerr << " " << ProgramArgs[i];
421 std::cerr << "\n";
422 );
Chris Lattner8c56be52004-02-18 20:21:57 +0000423
Reid Spencer9ac14182004-12-16 23:01:34 +0000424 FileRemover OutputBinaryRemover(OutputBinary);
Reid Spencerb31baa82004-12-19 18:00:21 +0000425 return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000426 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Reid Spencerb31baa82004-12-19 18:00:21 +0000427 Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000428}
429
Chris Lattner1798e4a2003-10-14 21:07:25 +0000430int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000431 std::string &OutputFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000432 sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
433 uniqueFilename.makeUnique();
434 OutputFile = uniqueFilename.toString();
435
Misha Brukman9558c6a2003-09-29 22:39:25 +0000436 // Compile the C/asm file into a shared object
437 const char* GCCArgs[] = {
438 GCCPath.c_str(),
439 "-x", (fileType == AsmFile) ? "assembler" : "c",
440 "-fno-strict-aliasing",
441 InputFile.c_str(), // Specify the input filename...
442#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
443 "-G", // Compile a shared library, `-G' for Sparc
Nate Begeman72b286b2005-07-08 00:23:26 +0000444#elif defined(__APPLE__)
Nate Begeman79255882004-10-17 23:03:32 +0000445 "-single_module", // link all source files into a single module
Misha Brukmanb3998ec2004-07-16 19:45:45 +0000446 "-dynamiclib", // `-dynamiclib' for MacOS X/PowerPC
Chris Lattnere312e152004-07-19 06:03:51 +0000447 "-undefined", // in data segment, rather than generating
Chris Lattner5fbf29c2004-07-19 06:00:17 +0000448 "dynamic_lookup", // blocks. dynamic_lookup requires that you set
449 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env.
Misha Brukmanb3998ec2004-07-16 19:45:45 +0000450#else
Misha Brukman9558c6a2003-09-29 22:39:25 +0000451 "-shared", // `-shared' for Linux/X86, maybe others
452#endif
Chris Lattner1c81f132005-03-09 03:31:02 +0000453
Andrew Lenharth572668a2005-03-10 20:15:09 +0000454#if defined(__ia64__) || defined(__alpha__)
Chris Lattner1c81f132005-03-09 03:31:02 +0000455 "-fPIC", // IA64 requires shared objs to contain PIC
456#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000457 "-o", OutputFile.c_str(), // Output to the right filename...
458 "-O2", // Optimize the program a bit...
459 0
460 };
Misha Brukmanf976c852005-04-21 22:55:34 +0000461
Misha Brukman9558c6a2003-09-29 22:39:25 +0000462 std::cout << "<gcc>" << std::flush;
Misha Brukmanf976c852005-04-21 22:55:34 +0000463 if (RunProgramWithTimeout(GCCPath, GCCArgs, sys::Path(), sys::Path(),
Reid Spencerb31baa82004-12-19 18:00:21 +0000464 sys::Path())) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000465 ProcessFailure(GCCPath, GCCArgs);
Chris Lattner1798e4a2003-10-14 21:07:25 +0000466 return 1;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000467 }
468 return 0;
469}
470
Chris Lattner7915a1e2003-10-14 21:34:11 +0000471/// create - Try to find the `gcc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000472///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000473GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
Reid Spencerb31baa82004-12-19 18:00:21 +0000474 sys::Path GCCPath = FindExecutable("gcc", ProgramPath);
475 if (GCCPath.isEmpty()) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000476 Message = "Cannot find `gcc' in executable directory or PATH!\n";
477 return 0;
478 }
479
Reid Spencerb31baa82004-12-19 18:00:21 +0000480 Message = "Found gcc: " + GCCPath.toString() + "\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000481 return new GCC(GCCPath);
482}