blob: ef07079af309f84ac5ce7df676681c30e06fb1cc [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//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Chris Lattnerf1b20d82006-06-06 22:30:59 +000015#include "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"
Evan Cheng34e400e2007-05-03 18:36:15 +000018#include "llvm/Support/CommandLine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/Support/Debug.h"
20#include "llvm/Support/FileUtilities.h"
Chris Lattner7915a1e2003-10-14 21:34:11 +000021#include <fstream>
Chris Lattner89bf9ea2004-02-18 20:38:00 +000022#include <sstream>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Evan Cheng34e400e2007-05-03 18:36:15 +000025namespace {
26 cl::opt<std::string>
Evan Cheng70f684f2008-09-09 06:11:26 +000027 RemoteClient("remote-client",
28 cl::desc("Remote execution client (rsh/ssh)"));
Evan Cheng34e400e2007-05-03 18:36:15 +000029
30 cl::opt<std::string>
Evan Cheng70f684f2008-09-09 06:11:26 +000031 RemoteHost("remote-host",
32 cl::desc("Remote execution (rsh/ssh) host"));
33
34 cl::opt<std::string>
David Goodwin80becf12009-07-10 21:39:28 +000035 RemotePort("remote-port",
36 cl::desc("Remote execution (rsh/ssh) port"));
37
38 cl::opt<std::string>
Evan Cheng70f684f2008-09-09 06:11:26 +000039 RemoteUser("remote-user",
40 cl::desc("Remote execution (rsh/ssh) user id"));
41
42 cl::opt<std::string>
43 RemoteExtra("remote-extra-options",
44 cl::desc("Remote execution (rsh/ssh) extra options"));
Evan Cheng34e400e2007-05-03 18:36:15 +000045}
46
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000047ToolExecutionError::~ToolExecutionError() throw() { }
48
Viktor Kutuzovfc2271f2009-07-18 18:39:24 +000049/// RunProgramWithTimeout - This function provides an alternate interface
50/// to the sys::Program::ExecuteAndWait interface.
Chris Lattner45495c52005-02-13 23:13:47 +000051/// @see sys:Program::ExecuteAndWait
52static int RunProgramWithTimeout(const sys::Path &ProgramPath,
53 const char **Args,
54 const sys::Path &StdInFile,
55 const sys::Path &StdOutFile,
56 const sys::Path &StdErrFile,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000057 unsigned NumSeconds = 0,
58 unsigned MemoryLimit = 0) {
Chris Lattner45495c52005-02-13 23:13:47 +000059 const sys::Path* redirects[3];
60 redirects[0] = &StdInFile;
61 redirects[1] = &StdOutFile;
62 redirects[2] = &StdErrFile;
Viktor Kutuzovfc2271f2009-07-18 18:39:24 +000063
64#if 0 // For debug purposes
65 {
Dan Gohman65f57c22009-07-15 16:35:29 +000066 errs() << "RUN:";
Chris Lattnerd4223502006-09-15 23:01:10 +000067 for (unsigned i = 0; Args[i]; ++i)
Dan Gohman65f57c22009-07-15 16:35:29 +000068 errs() << " " << Args[i];
69 errs() << "\n";
Chris Lattnerd4223502006-09-15 23:01:10 +000070 }
Viktor Kutuzovfc2271f2009-07-18 18:39:24 +000071#endif
Misha Brukmanf976c852005-04-21 22:55:34 +000072
73 return
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000074 sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects,
75 NumSeconds, MemoryLimit);
Chris Lattner45495c52005-02-13 23:13:47 +000076}
77
Viktor Kutuzovfc2271f2009-07-18 18:39:24 +000078/// RunProgramRemotelyWithTimeout - This function runs the given program
79/// remotely using the given remote client and the sys::Program::ExecuteAndWait.
80/// Returns the remote program exit code or reports a remote client error if it
81/// fails. Remote client is required to return 255 if it failed or program exit
82/// code otherwise.
83/// @see sys:Program::ExecuteAndWait
84static int RunProgramRemotelyWithTimeout(const sys::Path &RemoteClientPath,
85 const char **Args,
86 const sys::Path &StdInFile,
87 const sys::Path &StdOutFile,
88 const sys::Path &StdErrFile,
89 unsigned NumSeconds = 0,
90 unsigned MemoryLimit = 0) {
91 const sys::Path* redirects[3];
92 redirects[0] = &StdInFile;
93 redirects[1] = &StdOutFile;
94 redirects[2] = &StdErrFile;
Chris Lattner45495c52005-02-13 23:13:47 +000095
Viktor Kutuzovfc2271f2009-07-18 18:39:24 +000096#if 0 // For debug purposes
97 {
98 errs() << "RUN:";
99 for (unsigned i = 0; Args[i]; ++i)
100 errs() << " " << Args[i];
101 errs() << "\n";
102 }
103#endif
104
105 // Run the program remotely with the remote client
106 int ReturnCode = sys::Program::ExecuteAndWait(RemoteClientPath, Args,
107 0, redirects, NumSeconds, MemoryLimit);
108
109 // Has the remote client fail?
110 if (255 == ReturnCode) {
111 std::ostringstream OS;
112 OS << "\nError running remote client:\n ";
113 for (const char **Arg = Args; *Arg; ++Arg)
114 OS << " " << *Arg;
115 OS << "\n";
116
117 // The error message is in the output file, let's print it out from there.
118 std::ifstream ErrorFile(StdOutFile.c_str());
119 if (ErrorFile) {
120 std::copy(std::istreambuf_iterator<char>(ErrorFile),
121 std::istreambuf_iterator<char>(),
122 std::ostreambuf_iterator<char>(OS));
123 ErrorFile.close();
124 }
125
126 throw ToolExecutionError(OS.str());
127 }
128
129 return ReturnCode;
130}
Chris Lattner45495c52005-02-13 23:13:47 +0000131
Reid Spencerb31baa82004-12-19 18:00:21 +0000132static void ProcessFailure(sys::Path ProgPath, const char** Args) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000133 std::ostringstream OS;
Chris Lattnera3de1172004-02-18 20:58:00 +0000134 OS << "\nError running tool:\n ";
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000135 for (const char **Arg = Args; *Arg; ++Arg)
136 OS << " " << *Arg;
137 OS << "\n";
138
139 // Rerun the compiler, capturing any error messages to print them.
Dan Gohmana971b292009-02-12 20:53:27 +0000140 sys::Path ErrorFilename("bugpoint.program_error_messages");
Reid Spencer51c5a282006-08-23 20:34:57 +0000141 std::string ErrMsg;
142 if (ErrorFilename.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000143 errs() << "Error making unique filename: " << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000144 exit(1);
145 }
Reid Spencerb31baa82004-12-19 18:00:21 +0000146 RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename,
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000147 ErrorFilename); // FIXME: check return code ?
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000148
149 // Print out the error messages generated by GCC if possible...
150 std::ifstream ErrorFile(ErrorFilename.c_str());
151 if (ErrorFile) {
152 std::copy(std::istreambuf_iterator<char>(ErrorFile),
153 std::istreambuf_iterator<char>(),
154 std::ostreambuf_iterator<char>(OS));
155 ErrorFile.close();
156 }
157
Reid Spencera229c5c2005-07-08 03:08:58 +0000158 ErrorFilename.eraseFromDisk();
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000159 throw ToolExecutionError(OS.str());
160}
161
Misha Brukman9558c6a2003-09-29 22:39:25 +0000162//===---------------------------------------------------------------------===//
163// LLI Implementation of AbstractIntepreter interface
164//
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000165namespace {
166 class LLI : public AbstractInterpreter {
167 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000168 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000169 public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000170 LLI(const std::string &Path, const std::vector<std::string> *Args)
171 : LLIPath(Path) {
172 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000173 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000174 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000175
Gabor Greif8ff70c22007-07-04 21:55:50 +0000176 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000177 const std::vector<std::string> &Args,
178 const std::string &InputFile,
179 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000180 const std::vector<std::string> &GCCArgs,
Misha Brukmanf976c852005-04-21 22:55:34 +0000181 const std::vector<std::string> &SharedLibs =
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000182 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000183 unsigned Timeout = 0,
184 unsigned MemoryLimit = 0);
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000185 };
186}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000187
Gabor Greif8ff70c22007-07-04 21:55:50 +0000188int LLI::ExecuteProgram(const std::string &Bitcode,
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,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000192 const std::vector<std::string> &GCCArgs,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000193 const std::vector<std::string> &SharedLibs,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000194 unsigned Timeout,
195 unsigned MemoryLimit) {
Chris Lattner8c56be52004-02-18 20:21:57 +0000196 if (!SharedLibs.empty())
197 throw ToolExecutionError("LLI currently does not support "
198 "loading shared libraries.");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000199
200 std::vector<const char*> LLIArgs;
201 LLIArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000202 LLIArgs.push_back("-force-interpreter=true");
Brian Gaeked11577b2004-05-04 21:09:01 +0000203
204 // Add any extra LLI args.
205 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
206 LLIArgs.push_back(ToolArgs[i].c_str());
207
Gabor Greif8ff70c22007-07-04 21:55:50 +0000208 LLIArgs.push_back(Bitcode.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000209 // Add optional parameters to the running program from Argv
210 for (unsigned i=0, e = Args.size(); i != e; ++i)
211 LLIArgs.push_back(Args[i].c_str());
212 LLIArgs.push_back(0);
213
Dan Gohmana1bdced2009-07-15 17:29:42 +0000214 outs() << "<lli>"; outs().flush();
Dan Gohman65f57c22009-07-15 16:35:29 +0000215 DEBUG(errs() << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000216 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
Dan Gohman65f57c22009-07-15 16:35:29 +0000217 errs() << " " << LLIArgs[i];
218 errs() << "\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000219 );
Reid Spencerb31baa82004-12-19 18:00:21 +0000220 return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000221 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000222 Timeout, MemoryLimit);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000223}
224
225// LLI create method - Try to find the LLI executable
Chris Lattner7915a1e2003-10-14 21:34:11 +0000226AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000227 std::string &Message,
228 const std::vector<std::string> *ToolArgs) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000229 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000230 if (!LLIPath.empty()) {
231 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000232 return new LLI(LLIPath, ToolArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000233 }
234
235 Message = "Cannot find `lli' in executable directory or PATH!\n";
236 return 0;
237}
238
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000239//===---------------------------------------------------------------------===//
240// Custom execution command implementation of AbstractIntepreter interface
241//
242// Allows using a custom command for executing the bitcode, thus allows,
243// for example, to invoke a cross compiler for code generation followed by
244// a simulator that executes the generated binary.
245namespace {
246 class CustomExecutor : public AbstractInterpreter {
247 std::string ExecutionCommand;
248 std::vector<std::string> ExecutorArgs;
249 public:
250 CustomExecutor(
251 const std::string &ExecutionCmd, std::vector<std::string> ExecArgs) :
252 ExecutionCommand(ExecutionCmd), ExecutorArgs(ExecArgs) {}
253
254 virtual int ExecuteProgram(const std::string &Bitcode,
255 const std::vector<std::string> &Args,
256 const std::string &InputFile,
257 const std::string &OutputFile,
258 const std::vector<std::string> &GCCArgs,
259 const std::vector<std::string> &SharedLibs =
260 std::vector<std::string>(),
261 unsigned Timeout = 0,
262 unsigned MemoryLimit = 0);
263 };
264}
265
266int CustomExecutor::ExecuteProgram(const std::string &Bitcode,
267 const std::vector<std::string> &Args,
268 const std::string &InputFile,
269 const std::string &OutputFile,
270 const std::vector<std::string> &GCCArgs,
271 const std::vector<std::string> &SharedLibs,
272 unsigned Timeout,
273 unsigned MemoryLimit) {
274
275 std::vector<const char*> ProgramArgs;
276 ProgramArgs.push_back(ExecutionCommand.c_str());
277
278 for (std::size_t i = 0; i < ExecutorArgs.size(); ++i)
279 ProgramArgs.push_back(ExecutorArgs.at(i).c_str());
280 ProgramArgs.push_back(Bitcode.c_str());
281 ProgramArgs.push_back(0);
282
283 // Add optional parameters to the running program from Argv
284 for (unsigned i=0, e = Args.size(); i != e; ++i)
285 ProgramArgs.push_back(Args[i].c_str());
286
287 return RunProgramWithTimeout(
288 sys::Path(ExecutionCommand),
289 &ProgramArgs[0], sys::Path(InputFile), sys::Path(OutputFile),
290 sys::Path(OutputFile), Timeout, MemoryLimit);
291}
292
293// Custom execution environment create method, takes the execution command
294// as arguments
295AbstractInterpreter *AbstractInterpreter::createCustom(
296 const std::string &ProgramPath,
297 std::string &Message,
298 const std::string &ExecCommandLine) {
299
300 std::string Command = "";
301 std::vector<std::string> Args;
302 std::string delimiters = " ";
303
304 // Tokenize the ExecCommandLine to the command and the args to allow
305 // defining a full command line as the command instead of just the
306 // executed program. We cannot just pass the whole string after the command
307 // as a single argument because then program sees only a single
308 // command line argument (with spaces in it: "foo bar" instead
309 // of "foo" and "bar").
310
311 // code borrowed from:
312 // http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
313 std::string::size_type lastPos =
314 ExecCommandLine.find_first_not_of(delimiters, 0);
315 std::string::size_type pos =
316 ExecCommandLine.find_first_of(delimiters, lastPos);
317
318 while (std::string::npos != pos || std::string::npos != lastPos) {
319 std::string token = ExecCommandLine.substr(lastPos, pos - lastPos);
320 if (Command == "")
321 Command = token;
322 else
323 Args.push_back(token);
324 // Skip delimiters. Note the "not_of"
325 lastPos = ExecCommandLine.find_first_not_of(delimiters, pos);
326 // Find next "non-delimiter"
327 pos = ExecCommandLine.find_first_of(delimiters, lastPos);
328 }
329
330 std::string CmdPath = FindExecutable(Command, ProgramPath).toString();
331 if (CmdPath.empty()) {
332 Message =
333 std::string("Cannot find '") + Command +
334 "' in executable directory or PATH!\n";
335 return 0;
336 }
337
338 Message = "Found command in: " + CmdPath + "\n";
339
340 return new CustomExecutor(CmdPath, Args);
341}
342
Misha Brukman9558c6a2003-09-29 22:39:25 +0000343//===----------------------------------------------------------------------===//
344// LLC Implementation of AbstractIntepreter interface
345//
Gabor Greif8ff70c22007-07-04 21:55:50 +0000346GCC::FileType LLC::OutputCode(const std::string &Bitcode,
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000347 sys::Path &OutputAsmFile) {
Gabor Greif8ff70c22007-07-04 21:55:50 +0000348 sys::Path uniqueFile(Bitcode+".llc.s");
Reid Spencer51c5a282006-08-23 20:34:57 +0000349 std::string ErrMsg;
350 if (uniqueFile.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000351 errs() << "Error making unique filename: " << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000352 exit(1);
353 }
Reid Spencer9ac14182004-12-16 23:01:34 +0000354 OutputAsmFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000355 std::vector<const char *> LLCArgs;
356 LLCArgs.push_back (LLCPath.c_str());
357
358 // Add any extra LLC args.
359 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
360 LLCArgs.push_back(ToolArgs[i].c_str());
361
362 LLCArgs.push_back ("-o");
363 LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file
364 LLCArgs.push_back ("-f"); // Overwrite as necessary...
Gabor Greif8ff70c22007-07-04 21:55:50 +0000365 LLCArgs.push_back (Bitcode.c_str()); // This is the input bitcode
Brian Gaeked11577b2004-05-04 21:09:01 +0000366 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000367
Dan Gohmana1bdced2009-07-15 17:29:42 +0000368 outs() << "<llc>"; outs().flush();
Dan Gohman65f57c22009-07-15 16:35:29 +0000369 DEBUG(errs() << "\nAbout to run:\t";
Brian Gaeked11577b2004-05-04 21:09:01 +0000370 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
Dan Gohman65f57c22009-07-15 16:35:29 +0000371 errs() << " " << LLCArgs[i];
372 errs() << "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000373 );
Misha Brukmanf976c852005-04-21 22:55:34 +0000374 if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0],
Reid Spencerb31baa82004-12-19 18:00:21 +0000375 sys::Path(), sys::Path(), sys::Path()))
376 ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]);
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000377
378 return GCC::AsmFile;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000379}
380
Gabor Greif8ff70c22007-07-04 21:55:50 +0000381void LLC::compileProgram(const std::string &Bitcode) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000382 sys::Path OutputAsmFile;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000383 OutputCode(Bitcode, OutputAsmFile);
Reid Spencera229c5c2005-07-08 03:08:58 +0000384 OutputAsmFile.eraseFromDisk();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000385}
386
Gabor Greif8ff70c22007-07-04 21:55:50 +0000387int LLC::ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000388 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000389 const std::string &InputFile,
390 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000391 const std::vector<std::string> &ArgsForGCC,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000392 const std::vector<std::string> &SharedLibs,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000393 unsigned Timeout,
394 unsigned MemoryLimit) {
Chris Lattnereeed9832003-10-14 21:52:52 +0000395
Reid Spencer9ac14182004-12-16 23:01:34 +0000396 sys::Path OutputAsmFile;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000397 OutputCode(Bitcode, OutputAsmFile);
Chris Lattner8c56be52004-02-18 20:21:57 +0000398 FileRemover OutFileRemover(OutputAsmFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000399
Reid Spencer51ab5c82006-06-06 00:00:42 +0000400 std::vector<std::string> GCCArgs(ArgsForGCC);
Bill Wendling38efa382009-03-02 23:13:18 +0000401 GCCArgs.insert(GCCArgs.end(), SharedLibs.begin(), SharedLibs.end());
402 GCCArgs.insert(GCCArgs.end(), gccArgs.begin(), gccArgs.end());
Reid Spencer51ab5c82006-06-06 00:00:42 +0000403
Misha Brukman9558c6a2003-09-29 22:39:25 +0000404 // Assuming LLC worked, compile the result with GCC and run it.
Reid Spencer9ac14182004-12-16 23:01:34 +0000405 return gcc->ExecuteProgram(OutputAsmFile.toString(), Args, GCC::AsmFile,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000406 InputFile, OutputFile, GCCArgs,
407 Timeout, MemoryLimit);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000408}
409
Chris Lattner7915a1e2003-10-14 21:34:11 +0000410/// createLLC - Try to find the LLC executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000411///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000412LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000413 std::string &Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000414 const std::vector<std::string> *Args,
415 const std::vector<std::string> *GCCArgs) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000416 std::string LLCPath = FindExecutable("llc", ProgramPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000417 if (LLCPath.empty()) {
418 Message = "Cannot find `llc' in executable directory or PATH!\n";
419 return 0;
420 }
421
422 Message = "Found llc: " + LLCPath + "\n";
Bill Wendling38efa382009-03-02 23:13:18 +0000423 GCC *gcc = GCC::create(ProgramPath, Message, GCCArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000424 if (!gcc) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000425 errs() << Message << "\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000426 exit(1);
427 }
Bill Wendling38efa382009-03-02 23:13:18 +0000428 return new LLC(LLCPath, gcc, Args, GCCArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000429}
430
431//===---------------------------------------------------------------------===//
432// JIT Implementation of AbstractIntepreter interface
433//
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000434namespace {
435 class JIT : public AbstractInterpreter {
436 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000437 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000438 public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000439 JIT(const std::string &Path, const std::vector<std::string> *Args)
440 : LLIPath(Path) {
441 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000442 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000443 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000444
Gabor Greif8ff70c22007-07-04 21:55:50 +0000445 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000446 const std::vector<std::string> &Args,
447 const std::string &InputFile,
448 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000449 const std::vector<std::string> &GCCArgs =
450 std::vector<std::string>(),
Misha Brukmanf976c852005-04-21 22:55:34 +0000451 const std::vector<std::string> &SharedLibs =
Reid Spencer51ab5c82006-06-06 00:00:42 +0000452 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000453 unsigned Timeout =0,
454 unsigned MemoryLimit =0);
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000455 };
456}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000457
Gabor Greif8ff70c22007-07-04 21:55:50 +0000458int JIT::ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000459 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000460 const std::string &InputFile,
461 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000462 const std::vector<std::string> &GCCArgs,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000463 const std::vector<std::string> &SharedLibs,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000464 unsigned Timeout,
465 unsigned MemoryLimit) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000466 // Construct a vector of parameters, incorporating those from the command-line
467 std::vector<const char*> JITArgs;
468 JITArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000469 JITArgs.push_back("-force-interpreter=false");
Chris Lattnereeed9832003-10-14 21:52:52 +0000470
Brian Gaeked11577b2004-05-04 21:09:01 +0000471 // Add any extra LLI args.
472 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
473 JITArgs.push_back(ToolArgs[i].c_str());
474
Chris Lattnereeed9832003-10-14 21:52:52 +0000475 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000476 JITArgs.push_back("-load");
Chris Lattnereeed9832003-10-14 21:52:52 +0000477 JITArgs.push_back(SharedLibs[i].c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000478 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000479 JITArgs.push_back(Bitcode.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000480 // Add optional parameters to the running program from Argv
481 for (unsigned i=0, e = Args.size(); i != e; ++i)
482 JITArgs.push_back(Args[i].c_str());
483 JITArgs.push_back(0);
484
Dan Gohmana1bdced2009-07-15 17:29:42 +0000485 outs() << "<jit>"; outs().flush();
Dan Gohman65f57c22009-07-15 16:35:29 +0000486 DEBUG(errs() << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000487 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
Dan Gohman65f57c22009-07-15 16:35:29 +0000488 errs() << " " << JITArgs[i];
489 errs() << "\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000490 );
Dan Gohman65f57c22009-07-15 16:35:29 +0000491 DEBUG(errs() << "\nSending output to " << OutputFile << "\n");
Reid Spencerb31baa82004-12-19 18:00:21 +0000492 return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000493 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000494 Timeout, MemoryLimit);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000495}
496
Chris Lattner7915a1e2003-10-14 21:34:11 +0000497/// createJIT - Try to find the LLI executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000498///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000499AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000500 std::string &Message, const std::vector<std::string> *Args) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000501 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000502 if (!LLIPath.empty()) {
503 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000504 return new JIT(LLIPath, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000505 }
506
507 Message = "Cannot find `lli' in executable directory or PATH!\n";
508 return 0;
509}
510
Gabor Greif8ff70c22007-07-04 21:55:50 +0000511GCC::FileType CBE::OutputCode(const std::string &Bitcode,
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000512 sys::Path &OutputCFile) {
Gabor Greif8ff70c22007-07-04 21:55:50 +0000513 sys::Path uniqueFile(Bitcode+".cbe.c");
Reid Spencer51c5a282006-08-23 20:34:57 +0000514 std::string ErrMsg;
515 if (uniqueFile.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000516 errs() << "Error making unique filename: " << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000517 exit(1);
518 }
Reid Spencer9ac14182004-12-16 23:01:34 +0000519 OutputCFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000520 std::vector<const char *> LLCArgs;
521 LLCArgs.push_back (LLCPath.c_str());
522
523 // Add any extra LLC args.
524 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
525 LLCArgs.push_back(ToolArgs[i].c_str());
526
527 LLCArgs.push_back ("-o");
528 LLCArgs.push_back (OutputCFile.c_str()); // Output to the C file
529 LLCArgs.push_back ("-march=c"); // Output C language
530 LLCArgs.push_back ("-f"); // Overwrite as necessary...
Gabor Greif8ff70c22007-07-04 21:55:50 +0000531 LLCArgs.push_back (Bitcode.c_str()); // This is the input bitcode
Brian Gaeked11577b2004-05-04 21:09:01 +0000532 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000533
Dan Gohmana1bdced2009-07-15 17:29:42 +0000534 outs() << "<cbe>"; outs().flush();
Dan Gohman65f57c22009-07-15 16:35:29 +0000535 DEBUG(errs() << "\nAbout to run:\t";
Brian Gaeked11577b2004-05-04 21:09:01 +0000536 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
Dan Gohman65f57c22009-07-15 16:35:29 +0000537 errs() << " " << LLCArgs[i];
538 errs() << "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000539 );
Misha Brukmanf976c852005-04-21 22:55:34 +0000540 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(),
Reid Spencerb31baa82004-12-19 18:00:21 +0000541 sys::Path()))
Brian Gaeked11577b2004-05-04 21:09:01 +0000542 ProcessFailure(LLCPath, &LLCArgs[0]);
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000543 return GCC::CFile;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000544}
545
Gabor Greif8ff70c22007-07-04 21:55:50 +0000546void CBE::compileProgram(const std::string &Bitcode) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000547 sys::Path OutputCFile;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000548 OutputCode(Bitcode, OutputCFile);
Reid Spencera229c5c2005-07-08 03:08:58 +0000549 OutputCFile.eraseFromDisk();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000550}
551
Gabor Greif8ff70c22007-07-04 21:55:50 +0000552int CBE::ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000553 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000554 const std::string &InputFile,
555 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000556 const std::vector<std::string> &ArgsForGCC,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000557 const std::vector<std::string> &SharedLibs,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000558 unsigned Timeout,
559 unsigned MemoryLimit) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000560 sys::Path OutputCFile;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000561 OutputCode(Bitcode, OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000562
Chris Lattner8c56be52004-02-18 20:21:57 +0000563 FileRemover CFileRemove(OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000564
Reid Spencer51ab5c82006-06-06 00:00:42 +0000565 std::vector<std::string> GCCArgs(ArgsForGCC);
Bill Wendling38efa382009-03-02 23:13:18 +0000566 GCCArgs.insert(GCCArgs.end(), SharedLibs.begin(), SharedLibs.end());
567
Misha Brukmanf976c852005-04-21 22:55:34 +0000568 return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000569 InputFile, OutputFile, GCCArgs,
570 Timeout, MemoryLimit);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000571}
572
Chris Lattner9915cd92004-02-17 06:40:06 +0000573/// createCBE - Try to find the 'llc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000574///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000575CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000576 std::string &Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000577 const std::vector<std::string> *Args,
578 const std::vector<std::string> *GCCArgs) {
Reid Spencerb31baa82004-12-19 18:00:21 +0000579 sys::Path LLCPath = FindExecutable("llc", ProgramPath);
580 if (LLCPath.isEmpty()) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000581 Message =
Chris Lattner9915cd92004-02-17 06:40:06 +0000582 "Cannot find `llc' in executable directory or PATH!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000583 return 0;
584 }
585
Reid Spencerb31baa82004-12-19 18:00:21 +0000586 Message = "Found llc: " + LLCPath.toString() + "\n";
Bill Wendling38efa382009-03-02 23:13:18 +0000587 GCC *gcc = GCC::create(ProgramPath, Message, GCCArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000588 if (!gcc) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000589 errs() << Message << "\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000590 exit(1);
591 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000592 return new CBE(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000593}
594
595//===---------------------------------------------------------------------===//
596// GCC abstraction
597//
David Goodwin80becf12009-07-10 21:39:28 +0000598
Daniel Dunbar84f9abe2009-07-11 21:24:52 +0000599#ifdef __APPLE__
David Goodwin80becf12009-07-10 21:39:28 +0000600static bool
601IsARMArchitecture(std::vector<std::string> Args)
602{
603 for (std::vector<std::string>::const_iterator
604 I = Args.begin(), E = Args.end(); I != E; ++I) {
605 if (!strcasecmp(I->c_str(), "-arch")) {
606 ++I;
607 if ((I != E) && !strncasecmp(I->c_str(), "arm", strlen("arm"))) {
608 return true;
609 }
610 }
611 }
612
613 return false;
614}
Daniel Dunbar84f9abe2009-07-11 21:24:52 +0000615#endif
David Goodwin80becf12009-07-10 21:39:28 +0000616
Misha Brukman9558c6a2003-09-29 22:39:25 +0000617int GCC::ExecuteProgram(const std::string &ProgramFile,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000618 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000619 FileType fileType,
620 const std::string &InputFile,
621 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000622 const std::vector<std::string> &ArgsForGCC,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000623 unsigned Timeout,
624 unsigned MemoryLimit) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000625 std::vector<const char*> GCCArgs;
626
627 GCCArgs.push_back(GCCPath.c_str());
Chris Lattnereeed9832003-10-14 21:52:52 +0000628
Bill Wendling38efa382009-03-02 23:13:18 +0000629 for (std::vector<std::string>::const_iterator
630 I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I)
631 GCCArgs.push_back(I->c_str());
632
Chris Lattnereeed9832003-10-14 21:52:52 +0000633 // Specify -x explicitly in case the extension is wonky
Misha Brukman9558c6a2003-09-29 22:39:25 +0000634 GCCArgs.push_back("-x");
635 if (fileType == CFile) {
636 GCCArgs.push_back("c");
637 GCCArgs.push_back("-fno-strict-aliasing");
638 } else {
639 GCCArgs.push_back("assembler");
Chris Lattnerd00b2882005-08-29 13:14:24 +0000640#ifdef __APPLE__
David Goodwin80becf12009-07-10 21:39:28 +0000641 // For ARM architectures we don't want this flag. bugpoint isn't
642 // explicitly told what architecture it is working on, so we get
643 // it from gcc flags
644 if (!IsARMArchitecture(ArgsForGCC))
645 GCCArgs.push_back("-force_cpusubtype_ALL");
Chris Lattnerd00b2882005-08-29 13:14:24 +0000646#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000647 }
648 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
Chris Lattner629e4872006-06-09 21:31:53 +0000649 GCCArgs.push_back("-x");
650 GCCArgs.push_back("none");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000651 GCCArgs.push_back("-o");
Reid Spencercda985e2004-12-15 01:51:56 +0000652 sys::Path OutputBinary (ProgramFile+".gcc.exe");
Reid Spencer51c5a282006-08-23 20:34:57 +0000653 std::string ErrMsg;
654 if (OutputBinary.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000655 errs() << "Error making unique filename: " << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000656 exit(1);
657 }
Misha Brukman9558c6a2003-09-29 22:39:25 +0000658 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
Reid Spencer51ab5c82006-06-06 00:00:42 +0000659
660 // Add any arguments intended for GCC. We locate them here because this is
661 // most likely -L and -l options that need to come before other libraries but
662 // after the source. Other options won't be sensitive to placement on the
663 // command line, so this should be safe.
664 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
665 GCCArgs.push_back(ArgsForGCC[i].c_str());
666
Misha Brukman9558c6a2003-09-29 22:39:25 +0000667 GCCArgs.push_back("-lm"); // Hard-code the math library...
668 GCCArgs.push_back("-O2"); // Optimize the program a bit...
Brian Gaekec8db76c2003-11-18 06:31:17 +0000669#if defined (HAVE_LINK_R)
Chris Lattner1f0f1622003-10-18 21:54:47 +0000670 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
Brian Gaekec8db76c2003-11-18 06:31:17 +0000671#endif
Chris Lattnerfdcc71e2006-02-04 05:02:27 +0000672#ifdef __sparc__
673 GCCArgs.push_back("-mcpu=v9");
674#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000675 GCCArgs.push_back(0); // NULL terminator
676
Dan Gohmana1bdced2009-07-15 17:29:42 +0000677 outs() << "<gcc>"; outs().flush();
Dan Gohman65f57c22009-07-15 16:35:29 +0000678 DEBUG(errs() << "\nAbout to run:\t";
Evan Cheng7f7fdcc2007-01-03 07:44:30 +0000679 for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i)
Dan Gohman65f57c22009-07-15 16:35:29 +0000680 errs() << " " << GCCArgs[i];
681 errs() << "\n";
Evan Cheng7f7fdcc2007-01-03 07:44:30 +0000682 );
Reid Spencerb31baa82004-12-19 18:00:21 +0000683 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
684 sys::Path())) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000685 ProcessFailure(GCCPath, &GCCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000686 exit(1);
687 }
688
689 std::vector<const char*> ProgramArgs;
Chris Lattner45495c52005-02-13 23:13:47 +0000690
Evan Cheng70f684f2008-09-09 06:11:26 +0000691 if (RemoteClientPath.isEmpty())
Evan Cheng34e400e2007-05-03 18:36:15 +0000692 ProgramArgs.push_back(OutputBinary.c_str());
693 else {
Evan Cheng70f684f2008-09-09 06:11:26 +0000694 ProgramArgs.push_back(RemoteClientPath.c_str());
695 ProgramArgs.push_back(RemoteHost.c_str());
Evan Cheng34e400e2007-05-03 18:36:15 +0000696 ProgramArgs.push_back("-l");
Evan Cheng70f684f2008-09-09 06:11:26 +0000697 ProgramArgs.push_back(RemoteUser.c_str());
David Goodwin80becf12009-07-10 21:39:28 +0000698 if (!RemotePort.empty()) {
699 ProgramArgs.push_back("-p");
700 ProgramArgs.push_back(RemotePort.c_str());
701 }
Evan Cheng70f684f2008-09-09 06:11:26 +0000702 if (!RemoteExtra.empty()) {
703 ProgramArgs.push_back(RemoteExtra.c_str());
704 }
Evan Cheng34e400e2007-05-03 18:36:15 +0000705
David Goodwin91cf3612009-07-20 17:15:03 +0000706 // Full path to the binary. We need to cd to the exec directory because
707 // there is a dylib there that the exec expects to find in the CWD
Evan Cheng34e400e2007-05-03 18:36:15 +0000708 char* env_pwd = getenv("PWD");
David Goodwin91cf3612009-07-20 17:15:03 +0000709 std::string Exec = "cd ";
Evan Cheng34e400e2007-05-03 18:36:15 +0000710 Exec += env_pwd;
David Goodwin91cf3612009-07-20 17:15:03 +0000711 Exec += "; ./";
Evan Cheng34e400e2007-05-03 18:36:15 +0000712 Exec += OutputBinary.c_str();
713 ProgramArgs.push_back(Exec.c_str());
714 }
715
Misha Brukman9558c6a2003-09-29 22:39:25 +0000716 // Add optional parameters to the running program from Argv
717 for (unsigned i=0, e = Args.size(); i != e; ++i)
718 ProgramArgs.push_back(Args[i].c_str());
719 ProgramArgs.push_back(0); // NULL terminator
720
721 // Now that we have a binary, run it!
Dan Gohmana1bdced2009-07-15 17:29:42 +0000722 outs() << "<program>"; outs().flush();
Dan Gohman65f57c22009-07-15 16:35:29 +0000723 DEBUG(errs() << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000724 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
Dan Gohman65f57c22009-07-15 16:35:29 +0000725 errs() << " " << ProgramArgs[i];
726 errs() << "\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000727 );
Chris Lattner8c56be52004-02-18 20:21:57 +0000728
Reid Spencer9ac14182004-12-16 23:01:34 +0000729 FileRemover OutputBinaryRemover(OutputBinary);
Evan Cheng34e400e2007-05-03 18:36:15 +0000730
Viktor Kutuzov699220b2009-07-14 19:10:55 +0000731 if (RemoteClientPath.isEmpty()) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000732 DEBUG(errs() << "<run locally>";);
Evan Cheng34e400e2007-05-03 18:36:15 +0000733 return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
734 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
735 Timeout, MemoryLimit);
Viktor Kutuzov699220b2009-07-14 19:10:55 +0000736 } else {
Dan Gohmana1bdced2009-07-15 17:29:42 +0000737 outs() << "<run remotely>"; outs().flush();
Viktor Kutuzovfc2271f2009-07-18 18:39:24 +0000738 return RunProgramRemotelyWithTimeout(sys::Path(RemoteClientPath),
Viktor Kutuzov699220b2009-07-14 19:10:55 +0000739 &ProgramArgs[0], sys::Path(InputFile), sys::Path(OutputFile),
740 sys::Path(OutputFile), Timeout, MemoryLimit);
Viktor Kutuzov699220b2009-07-14 19:10:55 +0000741 }
Misha Brukman9558c6a2003-09-29 22:39:25 +0000742}
743
Chris Lattner1798e4a2003-10-14 21:07:25 +0000744int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
Chris Lattner130e2a32006-06-27 20:35:36 +0000745 std::string &OutputFile,
746 const std::vector<std::string> &ArgsForGCC) {
Reid Spencercda985e2004-12-15 01:51:56 +0000747 sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
Reid Spencer51c5a282006-08-23 20:34:57 +0000748 std::string ErrMsg;
749 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000750 errs() << "Error making unique filename: " << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000751 exit(1);
752 }
Reid Spencercda985e2004-12-15 01:51:56 +0000753 OutputFile = uniqueFilename.toString();
754
Chris Lattner130e2a32006-06-27 20:35:36 +0000755 std::vector<const char*> GCCArgs;
756
757 GCCArgs.push_back(GCCPath.c_str());
Evan Chengfd829952009-03-12 00:53:34 +0000758
759 for (std::vector<std::string>::const_iterator
760 I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I)
761 GCCArgs.push_back(I->c_str());
762
Misha Brukman9558c6a2003-09-29 22:39:25 +0000763 // Compile the C/asm file into a shared object
Chris Lattner130e2a32006-06-27 20:35:36 +0000764 GCCArgs.push_back("-x");
765 GCCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
766 GCCArgs.push_back("-fno-strict-aliasing");
767 GCCArgs.push_back(InputFile.c_str()); // Specify the input filename.
Lauro Ramos Venancio497391a2007-06-06 23:10:56 +0000768 GCCArgs.push_back("-x");
769 GCCArgs.push_back("none");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000770#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Chris Lattner130e2a32006-06-27 20:35:36 +0000771 GCCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc
Nate Begeman72b286b2005-07-08 00:23:26 +0000772#elif defined(__APPLE__)
Chris Lattner130e2a32006-06-27 20:35:36 +0000773 // link all source files into a single module in data segment, rather than
774 // generating blocks. dynamic_lookup requires that you set
775 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for
776 // bugpoint to just pass that in the environment of GCC.
777 GCCArgs.push_back("-single_module");
778 GCCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC
779 GCCArgs.push_back("-undefined");
780 GCCArgs.push_back("dynamic_lookup");
Misha Brukmanb3998ec2004-07-16 19:45:45 +0000781#else
Chris Lattner130e2a32006-06-27 20:35:36 +0000782 GCCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others
Misha Brukman9558c6a2003-09-29 22:39:25 +0000783#endif
Chris Lattner1c81f132005-03-09 03:31:02 +0000784
Torok Edwinaf4fc282008-04-06 12:42:29 +0000785#if defined(__ia64__) || defined(__alpha__) || defined(__amd64__)
Chris Lattner130e2a32006-06-27 20:35:36 +0000786 GCCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC
Chris Lattner1c81f132005-03-09 03:31:02 +0000787#endif
Chris Lattnerfdcc71e2006-02-04 05:02:27 +0000788#ifdef __sparc__
Chris Lattner130e2a32006-06-27 20:35:36 +0000789 GCCArgs.push_back("-mcpu=v9");
Chris Lattnerfdcc71e2006-02-04 05:02:27 +0000790#endif
Chris Lattner130e2a32006-06-27 20:35:36 +0000791 GCCArgs.push_back("-o");
792 GCCArgs.push_back(OutputFile.c_str()); // Output to the right filename.
793 GCCArgs.push_back("-O2"); // Optimize the program a bit.
794
795
796
797 // Add any arguments intended for GCC. We locate them here because this is
798 // most likely -L and -l options that need to come before other libraries but
799 // after the source. Other options won't be sensitive to placement on the
800 // command line, so this should be safe.
801 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
802 GCCArgs.push_back(ArgsForGCC[i].c_str());
803 GCCArgs.push_back(0); // NULL terminator
804
805
Misha Brukmanf976c852005-04-21 22:55:34 +0000806
Dan Gohmana1bdced2009-07-15 17:29:42 +0000807 outs() << "<gcc>"; outs().flush();
Dan Gohman65f57c22009-07-15 16:35:29 +0000808 DEBUG(errs() << "\nAbout to run:\t";
Evan Cheng7f7fdcc2007-01-03 07:44:30 +0000809 for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i)
Dan Gohman65f57c22009-07-15 16:35:29 +0000810 errs() << " " << GCCArgs[i];
811 errs() << "\n";
Evan Cheng7f7fdcc2007-01-03 07:44:30 +0000812 );
Chris Lattner130e2a32006-06-27 20:35:36 +0000813 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
Reid Spencerb31baa82004-12-19 18:00:21 +0000814 sys::Path())) {
Chris Lattner130e2a32006-06-27 20:35:36 +0000815 ProcessFailure(GCCPath, &GCCArgs[0]);
Chris Lattner1798e4a2003-10-14 21:07:25 +0000816 return 1;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000817 }
818 return 0;
819}
820
Chris Lattner7915a1e2003-10-14 21:34:11 +0000821/// create - Try to find the `gcc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000822///
Bill Wendling38efa382009-03-02 23:13:18 +0000823GCC *GCC::create(const std::string &ProgramPath, std::string &Message,
824 const std::vector<std::string> *Args) {
Reid Spencerb31baa82004-12-19 18:00:21 +0000825 sys::Path GCCPath = FindExecutable("gcc", ProgramPath);
826 if (GCCPath.isEmpty()) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000827 Message = "Cannot find `gcc' in executable directory or PATH!\n";
828 return 0;
829 }
830
Evan Cheng70f684f2008-09-09 06:11:26 +0000831 sys::Path RemoteClientPath;
832 if (!RemoteClient.empty())
833 RemoteClientPath = FindExecutable(RemoteClient.c_str(), ProgramPath);
Evan Cheng34e400e2007-05-03 18:36:15 +0000834
Reid Spencerb31baa82004-12-19 18:00:21 +0000835 Message = "Found gcc: " + GCCPath.toString() + "\n";
Bill Wendling38efa382009-03-02 23:13:18 +0000836 return new GCC(GCCPath, RemoteClientPath, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000837}