blob: 83b358be552e50085deb6b4d2e0095d025e1684d [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"
Chris Lattner45495c52005-02-13 23:13:47 +000016#include "llvm/System/Program.h"
Evan Cheng34e400e2007-05-03 18:36:15 +000017#include "llvm/Support/CommandLine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000018#include "llvm/Support/Debug.h"
19#include "llvm/Support/FileUtilities.h"
Chris Lattner74382b72009-08-23 22:45:37 +000020#include "llvm/Support/raw_ostream.h"
21#include "llvm/Config/config.h" // for HAVE_LINK_R
Chris Lattner7915a1e2003-10-14 21:34:11 +000022#include <fstream>
Chris Lattner89bf9ea2004-02-18 20:38:00 +000023#include <sstream>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Anton Korobeynikov86c006a2009-08-05 09:32:10 +000026namespace llvm {
27 cl::opt<bool>
28 SaveTemps("save-temps", cl::init(false), cl::desc("Save temporary files"));
29}
30
Evan Cheng34e400e2007-05-03 18:36:15 +000031namespace {
32 cl::opt<std::string>
Evan Cheng70f684f2008-09-09 06:11:26 +000033 RemoteClient("remote-client",
34 cl::desc("Remote execution client (rsh/ssh)"));
Evan Cheng34e400e2007-05-03 18:36:15 +000035
36 cl::opt<std::string>
Evan Cheng70f684f2008-09-09 06:11:26 +000037 RemoteHost("remote-host",
38 cl::desc("Remote execution (rsh/ssh) host"));
39
40 cl::opt<std::string>
David Goodwin80becf12009-07-10 21:39:28 +000041 RemotePort("remote-port",
42 cl::desc("Remote execution (rsh/ssh) port"));
43
44 cl::opt<std::string>
Evan Cheng70f684f2008-09-09 06:11:26 +000045 RemoteUser("remote-user",
46 cl::desc("Remote execution (rsh/ssh) user id"));
47
48 cl::opt<std::string>
49 RemoteExtra("remote-extra-options",
50 cl::desc("Remote execution (rsh/ssh) extra options"));
Evan Cheng34e400e2007-05-03 18:36:15 +000051}
52
Viktor Kutuzovfc2271f2009-07-18 18:39:24 +000053/// RunProgramWithTimeout - This function provides an alternate interface
54/// to the sys::Program::ExecuteAndWait interface.
Nick Lewycky22ff7482010-04-12 05:08:25 +000055/// @see sys::Program::ExecuteAndWait
Chris Lattner45495c52005-02-13 23:13:47 +000056static int RunProgramWithTimeout(const sys::Path &ProgramPath,
57 const char **Args,
58 const sys::Path &StdInFile,
59 const sys::Path &StdOutFile,
60 const sys::Path &StdErrFile,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000061 unsigned NumSeconds = 0,
62 unsigned MemoryLimit = 0) {
Chris Lattner45495c52005-02-13 23:13:47 +000063 const sys::Path* redirects[3];
64 redirects[0] = &StdInFile;
65 redirects[1] = &StdOutFile;
66 redirects[2] = &StdErrFile;
Viktor Kutuzovfc2271f2009-07-18 18:39:24 +000067
68#if 0 // For debug purposes
69 {
Dan Gohman65f57c22009-07-15 16:35:29 +000070 errs() << "RUN:";
Chris Lattnerd4223502006-09-15 23:01:10 +000071 for (unsigned i = 0; Args[i]; ++i)
Dan Gohman65f57c22009-07-15 16:35:29 +000072 errs() << " " << Args[i];
73 errs() << "\n";
Chris Lattnerd4223502006-09-15 23:01:10 +000074 }
Viktor Kutuzovfc2271f2009-07-18 18:39:24 +000075#endif
Misha Brukmanf976c852005-04-21 22:55:34 +000076
77 return
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000078 sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects,
79 NumSeconds, MemoryLimit);
Chris Lattner45495c52005-02-13 23:13:47 +000080}
81
Viktor Kutuzovfc2271f2009-07-18 18:39:24 +000082/// RunProgramRemotelyWithTimeout - This function runs the given program
83/// remotely using the given remote client and the sys::Program::ExecuteAndWait.
84/// Returns the remote program exit code or reports a remote client error if it
85/// fails. Remote client is required to return 255 if it failed or program exit
86/// code otherwise.
Nick Lewycky22ff7482010-04-12 05:08:25 +000087/// @see sys::Program::ExecuteAndWait
Viktor Kutuzovfc2271f2009-07-18 18:39:24 +000088static int RunProgramRemotelyWithTimeout(const sys::Path &RemoteClientPath,
89 const char **Args,
90 const sys::Path &StdInFile,
91 const sys::Path &StdOutFile,
92 const sys::Path &StdErrFile,
93 unsigned NumSeconds = 0,
94 unsigned MemoryLimit = 0) {
95 const sys::Path* redirects[3];
96 redirects[0] = &StdInFile;
97 redirects[1] = &StdOutFile;
98 redirects[2] = &StdErrFile;
Chris Lattner45495c52005-02-13 23:13:47 +000099
Viktor Kutuzovfc2271f2009-07-18 18:39:24 +0000100#if 0 // For debug purposes
101 {
102 errs() << "RUN:";
103 for (unsigned i = 0; Args[i]; ++i)
104 errs() << " " << Args[i];
105 errs() << "\n";
106 }
107#endif
108
109 // Run the program remotely with the remote client
110 int ReturnCode = sys::Program::ExecuteAndWait(RemoteClientPath, Args,
111 0, redirects, NumSeconds, MemoryLimit);
112
113 // Has the remote client fail?
114 if (255 == ReturnCode) {
115 std::ostringstream OS;
116 OS << "\nError running remote client:\n ";
117 for (const char **Arg = Args; *Arg; ++Arg)
118 OS << " " << *Arg;
119 OS << "\n";
120
121 // The error message is in the output file, let's print it out from there.
122 std::ifstream ErrorFile(StdOutFile.c_str());
123 if (ErrorFile) {
124 std::copy(std::istreambuf_iterator<char>(ErrorFile),
125 std::istreambuf_iterator<char>(),
126 std::ostreambuf_iterator<char>(OS));
127 ErrorFile.close();
128 }
129
Nick Lewycky22ff7482010-04-12 05:08:25 +0000130 errs() << OS;
Viktor Kutuzovfc2271f2009-07-18 18:39:24 +0000131 }
132
133 return ReturnCode;
134}
Chris Lattner45495c52005-02-13 23:13:47 +0000135
Duncan Sands41396302010-05-24 07:49:55 +0000136static std::string ProcessFailure(sys::Path ProgPath, const char** Args,
137 unsigned Timeout = 0,
138 unsigned MemoryLimit = 0) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000139 std::ostringstream OS;
Chris Lattnera3de1172004-02-18 20:58:00 +0000140 OS << "\nError running tool:\n ";
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000141 for (const char **Arg = Args; *Arg; ++Arg)
142 OS << " " << *Arg;
143 OS << "\n";
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000144
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000145 // Rerun the compiler, capturing any error messages to print them.
Dan Gohmana971b292009-02-12 20:53:27 +0000146 sys::Path ErrorFilename("bugpoint.program_error_messages");
Reid Spencer51c5a282006-08-23 20:34:57 +0000147 std::string ErrMsg;
148 if (ErrorFilename.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000149 errs() << "Error making unique filename: " << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000150 exit(1);
151 }
Reid Spencerb31baa82004-12-19 18:00:21 +0000152 RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename,
Duncan Sands41396302010-05-24 07:49:55 +0000153 ErrorFilename, Timeout, MemoryLimit);
154 // FIXME: check return code ?
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000155
156 // Print out the error messages generated by GCC if possible...
157 std::ifstream ErrorFile(ErrorFilename.c_str());
158 if (ErrorFile) {
159 std::copy(std::istreambuf_iterator<char>(ErrorFile),
160 std::istreambuf_iterator<char>(),
161 std::ostreambuf_iterator<char>(OS));
162 ErrorFile.close();
163 }
164
Reid Spencera229c5c2005-07-08 03:08:58 +0000165 ErrorFilename.eraseFromDisk();
Nick Lewycky22ff7482010-04-12 05:08:25 +0000166 return OS.str();
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000167}
168
Misha Brukman9558c6a2003-09-29 22:39:25 +0000169//===---------------------------------------------------------------------===//
170// LLI Implementation of AbstractIntepreter interface
171//
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000172namespace {
173 class LLI : public AbstractInterpreter {
174 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000175 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000176 public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000177 LLI(const std::string &Path, const std::vector<std::string> *Args)
178 : LLIPath(Path) {
179 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000180 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000181 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000182
Gabor Greif8ff70c22007-07-04 21:55:50 +0000183 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000184 const std::vector<std::string> &Args,
185 const std::string &InputFile,
186 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000187 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000188 const std::vector<std::string> &GCCArgs,
Misha Brukmanf976c852005-04-21 22:55:34 +0000189 const std::vector<std::string> &SharedLibs =
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000190 std::vector<std::string>(),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000191 unsigned Timeout = 0,
192 unsigned MemoryLimit = 0);
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000193 };
194}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000195
Gabor Greif8ff70c22007-07-04 21:55:50 +0000196int LLI::ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000197 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000198 const std::string &InputFile,
199 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000200 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000201 const std::vector<std::string> &GCCArgs,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000202 const std::vector<std::string> &SharedLibs,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000203 unsigned Timeout,
204 unsigned MemoryLimit) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000205 std::vector<const char*> LLIArgs;
206 LLIArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000207 LLIArgs.push_back("-force-interpreter=true");
Brian Gaeked11577b2004-05-04 21:09:01 +0000208
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000209 for (std::vector<std::string>::const_iterator i = SharedLibs.begin(),
210 e = SharedLibs.end(); i != e; ++i) {
Duncan Sands3ea11cf2009-11-17 10:20:22 +0000211 LLIArgs.push_back("-load");
212 LLIArgs.push_back((*i).c_str());
213 }
214
Brian Gaeked11577b2004-05-04 21:09:01 +0000215 // Add any extra LLI args.
216 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
217 LLIArgs.push_back(ToolArgs[i].c_str());
218
Gabor Greif8ff70c22007-07-04 21:55:50 +0000219 LLIArgs.push_back(Bitcode.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000220 // Add optional parameters to the running program from Argv
221 for (unsigned i=0, e = Args.size(); i != e; ++i)
222 LLIArgs.push_back(Args[i].c_str());
223 LLIArgs.push_back(0);
224
Dan Gohmana1bdced2009-07-15 17:29:42 +0000225 outs() << "<lli>"; outs().flush();
Dan Gohman65f57c22009-07-15 16:35:29 +0000226 DEBUG(errs() << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000227 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
Dan Gohman65f57c22009-07-15 16:35:29 +0000228 errs() << " " << LLIArgs[i];
229 errs() << "\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000230 );
Reid Spencerb31baa82004-12-19 18:00:21 +0000231 return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000232 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000233 Timeout, MemoryLimit);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000234}
235
236// LLI create method - Try to find the LLI executable
Dan Gohman197f7282009-08-05 20:21:17 +0000237AbstractInterpreter *AbstractInterpreter::createLLI(const char *Argv0,
Brian Gaeked11577b2004-05-04 21:09:01 +0000238 std::string &Message,
239 const std::vector<std::string> *ToolArgs) {
Dan Gohman197f7282009-08-05 20:21:17 +0000240 std::string LLIPath =
Chris Lattner74382b72009-08-23 22:45:37 +0000241 FindExecutable("lli", Argv0, (void *)(intptr_t)&createLLI).str();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000242 if (!LLIPath.empty()) {
243 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000244 return new LLI(LLIPath, ToolArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000245 }
246
Dan Gohman62a5ff52010-10-29 16:15:23 +0000247 Message = "Cannot find `lli' in executable directory!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000248 return 0;
249}
250
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000251//===---------------------------------------------------------------------===//
252// Custom execution command implementation of AbstractIntepreter interface
253//
254// Allows using a custom command for executing the bitcode, thus allows,
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000255// for example, to invoke a cross compiler for code generation followed by
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000256// a simulator that executes the generated binary.
257namespace {
258 class CustomExecutor : public AbstractInterpreter {
259 std::string ExecutionCommand;
260 std::vector<std::string> ExecutorArgs;
261 public:
262 CustomExecutor(
263 const std::string &ExecutionCmd, std::vector<std::string> ExecArgs) :
264 ExecutionCommand(ExecutionCmd), ExecutorArgs(ExecArgs) {}
265
266 virtual int ExecuteProgram(const std::string &Bitcode,
267 const std::vector<std::string> &Args,
268 const std::string &InputFile,
269 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000270 std::string *Error,
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000271 const std::vector<std::string> &GCCArgs,
272 const std::vector<std::string> &SharedLibs =
Nick Lewycky22ff7482010-04-12 05:08:25 +0000273 std::vector<std::string>(),
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000274 unsigned Timeout = 0,
275 unsigned MemoryLimit = 0);
276 };
277}
278
279int CustomExecutor::ExecuteProgram(const std::string &Bitcode,
280 const std::vector<std::string> &Args,
281 const std::string &InputFile,
282 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000283 std::string *Error,
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000284 const std::vector<std::string> &GCCArgs,
285 const std::vector<std::string> &SharedLibs,
286 unsigned Timeout,
287 unsigned MemoryLimit) {
288
289 std::vector<const char*> ProgramArgs;
290 ProgramArgs.push_back(ExecutionCommand.c_str());
291
292 for (std::size_t i = 0; i < ExecutorArgs.size(); ++i)
293 ProgramArgs.push_back(ExecutorArgs.at(i).c_str());
294 ProgramArgs.push_back(Bitcode.c_str());
295 ProgramArgs.push_back(0);
296
297 // Add optional parameters to the running program from Argv
Nick Lewycky22ff7482010-04-12 05:08:25 +0000298 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000299 ProgramArgs.push_back(Args[i].c_str());
300
301 return RunProgramWithTimeout(
302 sys::Path(ExecutionCommand),
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000303 &ProgramArgs[0], sys::Path(InputFile), sys::Path(OutputFile),
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000304 sys::Path(OutputFile), Timeout, MemoryLimit);
305}
306
307// Custom execution environment create method, takes the execution command
308// as arguments
309AbstractInterpreter *AbstractInterpreter::createCustom(
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000310 std::string &Message,
311 const std::string &ExecCommandLine) {
312
313 std::string Command = "";
314 std::vector<std::string> Args;
315 std::string delimiters = " ";
316
317 // Tokenize the ExecCommandLine to the command and the args to allow
318 // defining a full command line as the command instead of just the
319 // executed program. We cannot just pass the whole string after the command
320 // as a single argument because then program sees only a single
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000321 // command line argument (with spaces in it: "foo bar" instead
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000322 // of "foo" and "bar").
323
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000324 // code borrowed from:
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000325 // http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000326 std::string::size_type lastPos =
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000327 ExecCommandLine.find_first_not_of(delimiters, 0);
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000328 std::string::size_type pos =
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000329 ExecCommandLine.find_first_of(delimiters, lastPos);
330
331 while (std::string::npos != pos || std::string::npos != lastPos) {
332 std::string token = ExecCommandLine.substr(lastPos, pos - lastPos);
333 if (Command == "")
334 Command = token;
335 else
336 Args.push_back(token);
337 // Skip delimiters. Note the "not_of"
338 lastPos = ExecCommandLine.find_first_not_of(delimiters, pos);
339 // Find next "non-delimiter"
340 pos = ExecCommandLine.find_first_of(delimiters, lastPos);
341 }
342
Chris Lattner74382b72009-08-23 22:45:37 +0000343 std::string CmdPath = sys::Program::FindProgramByName(Command).str();
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000344 if (CmdPath.empty()) {
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000345 Message =
346 std::string("Cannot find '") + Command +
Dan Gohman62a5ff52010-10-29 16:15:23 +0000347 "' in PATH!\n";
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000348 return 0;
349 }
350
351 Message = "Found command in: " + CmdPath + "\n";
352
353 return new CustomExecutor(CmdPath, Args);
354}
355
Misha Brukman9558c6a2003-09-29 22:39:25 +0000356//===----------------------------------------------------------------------===//
357// LLC Implementation of AbstractIntepreter interface
358//
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000359GCC::FileType LLC::OutputCode(const std::string &Bitcode,
Duncan Sands41396302010-05-24 07:49:55 +0000360 sys::Path &OutputAsmFile, std::string &Error,
361 unsigned Timeout, unsigned MemoryLimit) {
Chris Lattner50010422010-03-16 06:41:47 +0000362 const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s");
363 sys::Path uniqueFile(Bitcode + Suffix);
Reid Spencer51c5a282006-08-23 20:34:57 +0000364 std::string ErrMsg;
365 if (uniqueFile.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000366 errs() << "Error making unique filename: " << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000367 exit(1);
368 }
Reid Spencer9ac14182004-12-16 23:01:34 +0000369 OutputAsmFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000370 std::vector<const char *> LLCArgs;
Chris Lattner50010422010-03-16 06:41:47 +0000371 LLCArgs.push_back(LLCPath.c_str());
Brian Gaeked11577b2004-05-04 21:09:01 +0000372
373 // Add any extra LLC args.
374 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
375 LLCArgs.push_back(ToolArgs[i].c_str());
376
Chris Lattner50010422010-03-16 06:41:47 +0000377 LLCArgs.push_back("-o");
378 LLCArgs.push_back(OutputAsmFile.c_str()); // Output to the Asm file
379 LLCArgs.push_back(Bitcode.c_str()); // This is the input bitcode
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000380
Chris Lattner50010422010-03-16 06:41:47 +0000381 if (UseIntegratedAssembler)
382 LLCArgs.push_back("-filetype=obj");
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000383
Brian Gaeked11577b2004-05-04 21:09:01 +0000384 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000385
Chris Lattner50010422010-03-16 06:41:47 +0000386 outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>");
387 outs().flush();
Dan Gohman65f57c22009-07-15 16:35:29 +0000388 DEBUG(errs() << "\nAbout to run:\t";
Nick Lewycky22ff7482010-04-12 05:08:25 +0000389 for (unsigned i = 0, e = LLCArgs.size()-1; i != e; ++i)
Dan Gohman65f57c22009-07-15 16:35:29 +0000390 errs() << " " << LLCArgs[i];
391 errs() << "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000392 );
Misha Brukmanf976c852005-04-21 22:55:34 +0000393 if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0],
Duncan Sands41396302010-05-24 07:49:55 +0000394 sys::Path(), sys::Path(), sys::Path(),
395 Timeout, MemoryLimit))
396 Error = ProcessFailure(sys::Path(LLCPath), &LLCArgs[0],
397 Timeout, MemoryLimit);
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000398 return UseIntegratedAssembler ? GCC::ObjectFile : GCC::AsmFile;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000399}
400
Duncan Sands41396302010-05-24 07:49:55 +0000401void LLC::compileProgram(const std::string &Bitcode, std::string *Error,
402 unsigned Timeout, unsigned MemoryLimit) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000403 sys::Path OutputAsmFile;
Duncan Sands41396302010-05-24 07:49:55 +0000404 OutputCode(Bitcode, OutputAsmFile, *Error, Timeout, MemoryLimit);
Reid Spencera229c5c2005-07-08 03:08:58 +0000405 OutputAsmFile.eraseFromDisk();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000406}
407
Gabor Greif8ff70c22007-07-04 21:55:50 +0000408int LLC::ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000409 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000410 const std::string &InputFile,
411 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000412 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000413 const std::vector<std::string> &ArgsForGCC,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000414 const std::vector<std::string> &SharedLibs,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000415 unsigned Timeout,
416 unsigned MemoryLimit) {
Chris Lattnereeed9832003-10-14 21:52:52 +0000417
Reid Spencer9ac14182004-12-16 23:01:34 +0000418 sys::Path OutputAsmFile;
Duncan Sands41396302010-05-24 07:49:55 +0000419 GCC::FileType FileKind = OutputCode(Bitcode, OutputAsmFile, *Error, Timeout,
420 MemoryLimit);
Anton Korobeynikov86c006a2009-08-05 09:32:10 +0000421 FileRemover OutFileRemover(OutputAsmFile, !SaveTemps);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000422
Reid Spencer51ab5c82006-06-06 00:00:42 +0000423 std::vector<std::string> GCCArgs(ArgsForGCC);
Bill Wendling38efa382009-03-02 23:13:18 +0000424 GCCArgs.insert(GCCArgs.end(), SharedLibs.begin(), SharedLibs.end());
Reid Spencer51ab5c82006-06-06 00:00:42 +0000425
Misha Brukman9558c6a2003-09-29 22:39:25 +0000426 // Assuming LLC worked, compile the result with GCC and run it.
Chris Lattner50010422010-03-16 06:41:47 +0000427 return gcc->ExecuteProgram(OutputAsmFile.str(), Args, FileKind,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000428 InputFile, OutputFile, Error, GCCArgs,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000429 Timeout, MemoryLimit);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000430}
431
Chris Lattner7915a1e2003-10-14 21:34:11 +0000432/// createLLC - Try to find the LLC executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000433///
Dan Gohman197f7282009-08-05 20:21:17 +0000434LLC *AbstractInterpreter::createLLC(const char *Argv0,
Brian Gaeked11577b2004-05-04 21:09:01 +0000435 std::string &Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000436 const std::string &GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +0000437 const std::vector<std::string> *Args,
Chris Lattner50010422010-03-16 06:41:47 +0000438 const std::vector<std::string> *GCCArgs,
439 bool UseIntegratedAssembler) {
Dan Gohman197f7282009-08-05 20:21:17 +0000440 std::string LLCPath =
Chris Lattner74382b72009-08-23 22:45:37 +0000441 FindExecutable("llc", Argv0, (void *)(intptr_t)&createLLC).str();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000442 if (LLCPath.empty()) {
Dan Gohman62a5ff52010-10-29 16:15:23 +0000443 Message = "Cannot find `llc' in executable directory!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000444 return 0;
445 }
446
447 Message = "Found llc: " + LLCPath + "\n";
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000448 GCC *gcc = GCC::create(Message, GCCBinary, GCCArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000449 if (!gcc) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000450 errs() << Message << "\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000451 exit(1);
452 }
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000453 return new LLC(LLCPath, gcc, Args, UseIntegratedAssembler);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000454}
455
456//===---------------------------------------------------------------------===//
457// JIT Implementation of AbstractIntepreter interface
458//
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000459namespace {
460 class JIT : public AbstractInterpreter {
461 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000462 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000463 public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000464 JIT(const std::string &Path, const std::vector<std::string> *Args)
465 : LLIPath(Path) {
466 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000467 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000468 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000469
Gabor Greif8ff70c22007-07-04 21:55:50 +0000470 virtual int ExecuteProgram(const std::string &Bitcode,
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000471 const std::vector<std::string> &Args,
472 const std::string &InputFile,
473 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000474 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000475 const std::vector<std::string> &GCCArgs =
476 std::vector<std::string>(),
Misha Brukmanf976c852005-04-21 22:55:34 +0000477 const std::vector<std::string> &SharedLibs =
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000478 std::vector<std::string>(),
Nick Lewycky22ff7482010-04-12 05:08:25 +0000479 unsigned Timeout = 0,
480 unsigned MemoryLimit = 0);
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000481 };
482}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000483
Gabor Greif8ff70c22007-07-04 21:55:50 +0000484int JIT::ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000485 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000486 const std::string &InputFile,
487 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000488 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000489 const std::vector<std::string> &GCCArgs,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000490 const std::vector<std::string> &SharedLibs,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000491 unsigned Timeout,
492 unsigned MemoryLimit) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000493 // Construct a vector of parameters, incorporating those from the command-line
494 std::vector<const char*> JITArgs;
495 JITArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000496 JITArgs.push_back("-force-interpreter=false");
Chris Lattnereeed9832003-10-14 21:52:52 +0000497
Brian Gaeked11577b2004-05-04 21:09:01 +0000498 // Add any extra LLI args.
499 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
500 JITArgs.push_back(ToolArgs[i].c_str());
501
Chris Lattnereeed9832003-10-14 21:52:52 +0000502 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000503 JITArgs.push_back("-load");
Chris Lattnereeed9832003-10-14 21:52:52 +0000504 JITArgs.push_back(SharedLibs[i].c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000505 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000506 JITArgs.push_back(Bitcode.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000507 // Add optional parameters to the running program from Argv
508 for (unsigned i=0, e = Args.size(); i != e; ++i)
509 JITArgs.push_back(Args[i].c_str());
510 JITArgs.push_back(0);
511
Dan Gohmana1bdced2009-07-15 17:29:42 +0000512 outs() << "<jit>"; outs().flush();
Dan Gohman65f57c22009-07-15 16:35:29 +0000513 DEBUG(errs() << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000514 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
Dan Gohman65f57c22009-07-15 16:35:29 +0000515 errs() << " " << JITArgs[i];
516 errs() << "\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000517 );
Dan Gohman65f57c22009-07-15 16:35:29 +0000518 DEBUG(errs() << "\nSending output to " << OutputFile << "\n");
Reid Spencerb31baa82004-12-19 18:00:21 +0000519 return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0],
Misha Brukmanf976c852005-04-21 22:55:34 +0000520 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000521 Timeout, MemoryLimit);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000522}
523
Chris Lattner7915a1e2003-10-14 21:34:11 +0000524/// createJIT - Try to find the LLI executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000525///
Dan Gohman197f7282009-08-05 20:21:17 +0000526AbstractInterpreter *AbstractInterpreter::createJIT(const char *Argv0,
Brian Gaeked11577b2004-05-04 21:09:01 +0000527 std::string &Message, const std::vector<std::string> *Args) {
Dan Gohman197f7282009-08-05 20:21:17 +0000528 std::string LLIPath =
Chris Lattner74382b72009-08-23 22:45:37 +0000529 FindExecutable("lli", Argv0, (void *)(intptr_t)&createJIT).str();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000530 if (!LLIPath.empty()) {
531 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000532 return new JIT(LLIPath, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000533 }
534
Dan Gohman62a5ff52010-10-29 16:15:23 +0000535 Message = "Cannot find `lli' in executable directory!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000536 return 0;
537}
538
Gabor Greif8ff70c22007-07-04 21:55:50 +0000539GCC::FileType CBE::OutputCode(const std::string &Bitcode,
Duncan Sands41396302010-05-24 07:49:55 +0000540 sys::Path &OutputCFile, std::string &Error,
541 unsigned Timeout, unsigned MemoryLimit) {
Gabor Greif8ff70c22007-07-04 21:55:50 +0000542 sys::Path uniqueFile(Bitcode+".cbe.c");
Reid Spencer51c5a282006-08-23 20:34:57 +0000543 std::string ErrMsg;
544 if (uniqueFile.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000545 errs() << "Error making unique filename: " << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000546 exit(1);
547 }
Reid Spencer9ac14182004-12-16 23:01:34 +0000548 OutputCFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000549 std::vector<const char *> LLCArgs;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000550 LLCArgs.push_back(LLCPath.c_str());
Brian Gaeked11577b2004-05-04 21:09:01 +0000551
552 // Add any extra LLC args.
553 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
554 LLCArgs.push_back(ToolArgs[i].c_str());
555
Nick Lewycky22ff7482010-04-12 05:08:25 +0000556 LLCArgs.push_back("-o");
557 LLCArgs.push_back(OutputCFile.c_str()); // Output to the C file
558 LLCArgs.push_back("-march=c"); // Output C language
Nick Lewycky22ff7482010-04-12 05:08:25 +0000559 LLCArgs.push_back(Bitcode.c_str()); // This is the input bitcode
560 LLCArgs.push_back(0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000561
Dan Gohmana1bdced2009-07-15 17:29:42 +0000562 outs() << "<cbe>"; outs().flush();
Dan Gohman65f57c22009-07-15 16:35:29 +0000563 DEBUG(errs() << "\nAbout to run:\t";
Nick Lewycky22ff7482010-04-12 05:08:25 +0000564 for (unsigned i = 0, e = LLCArgs.size()-1; i != e; ++i)
Dan Gohman65f57c22009-07-15 16:35:29 +0000565 errs() << " " << LLCArgs[i];
566 errs() << "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000567 );
Misha Brukmanf976c852005-04-21 22:55:34 +0000568 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(),
Duncan Sands41396302010-05-24 07:49:55 +0000569 sys::Path(), Timeout, MemoryLimit))
570 Error = ProcessFailure(LLCPath, &LLCArgs[0], Timeout, MemoryLimit);
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000571 return GCC::CFile;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000572}
573
Duncan Sands41396302010-05-24 07:49:55 +0000574void CBE::compileProgram(const std::string &Bitcode, std::string *Error,
575 unsigned Timeout, unsigned MemoryLimit) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000576 sys::Path OutputCFile;
Duncan Sands41396302010-05-24 07:49:55 +0000577 OutputCode(Bitcode, OutputCFile, *Error, Timeout, MemoryLimit);
Reid Spencera229c5c2005-07-08 03:08:58 +0000578 OutputCFile.eraseFromDisk();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000579}
580
Gabor Greif8ff70c22007-07-04 21:55:50 +0000581int CBE::ExecuteProgram(const std::string &Bitcode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000582 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000583 const std::string &InputFile,
584 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000585 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000586 const std::vector<std::string> &ArgsForGCC,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000587 const std::vector<std::string> &SharedLibs,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000588 unsigned Timeout,
589 unsigned MemoryLimit) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000590 sys::Path OutputCFile;
Duncan Sands41396302010-05-24 07:49:55 +0000591 OutputCode(Bitcode, OutputCFile, *Error, Timeout, MemoryLimit);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000592
Anton Korobeynikov86c006a2009-08-05 09:32:10 +0000593 FileRemover CFileRemove(OutputCFile, !SaveTemps);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000594
Reid Spencer51ab5c82006-06-06 00:00:42 +0000595 std::vector<std::string> GCCArgs(ArgsForGCC);
Bill Wendling38efa382009-03-02 23:13:18 +0000596 GCCArgs.insert(GCCArgs.end(), SharedLibs.begin(), SharedLibs.end());
597
Chris Lattner74382b72009-08-23 22:45:37 +0000598 return gcc->ExecuteProgram(OutputCFile.str(), Args, GCC::CFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000599 InputFile, OutputFile, Error, GCCArgs,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000600 Timeout, MemoryLimit);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000601}
602
Chris Lattner9915cd92004-02-17 06:40:06 +0000603/// createCBE - Try to find the 'llc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000604///
Dan Gohman197f7282009-08-05 20:21:17 +0000605CBE *AbstractInterpreter::createCBE(const char *Argv0,
Brian Gaeked11577b2004-05-04 21:09:01 +0000606 std::string &Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000607 const std::string &GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +0000608 const std::vector<std::string> *Args,
609 const std::vector<std::string> *GCCArgs) {
Dan Gohman197f7282009-08-05 20:21:17 +0000610 sys::Path LLCPath =
Dan Gohman8608cf22009-08-05 21:03:39 +0000611 FindExecutable("llc", Argv0, (void *)(intptr_t)&createCBE);
Reid Spencerb31baa82004-12-19 18:00:21 +0000612 if (LLCPath.isEmpty()) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000613 Message =
Dan Gohman62a5ff52010-10-29 16:15:23 +0000614 "Cannot find `llc' in executable directory!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000615 return 0;
616 }
617
Chris Lattner74382b72009-08-23 22:45:37 +0000618 Message = "Found llc: " + LLCPath.str() + "\n";
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000619 GCC *gcc = GCC::create(Message, GCCBinary, GCCArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000620 if (!gcc) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000621 errs() << Message << "\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000622 exit(1);
623 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000624 return new CBE(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000625}
626
627//===---------------------------------------------------------------------===//
628// GCC abstraction
629//
David Goodwin80becf12009-07-10 21:39:28 +0000630
Jakob Stoklund Olesen887b7032010-07-29 00:52:16 +0000631static bool IsARMArchitecture(std::vector<const char*> Args) {
632 for (std::vector<const char*>::const_iterator
David Goodwin80becf12009-07-10 21:39:28 +0000633 I = Args.begin(), E = Args.end(); I != E; ++I) {
Jakob Stoklund Olesen18e05b42010-05-13 17:58:15 +0000634 if (StringRef(*I).equals_lower("-arch")) {
David Goodwin80becf12009-07-10 21:39:28 +0000635 ++I;
Jakob Stoklund Olesen18e05b42010-05-13 17:58:15 +0000636 if (I != E && StringRef(*I).substr(0, strlen("arm")).equals_lower("arm"))
David Goodwin80becf12009-07-10 21:39:28 +0000637 return true;
David Goodwin80becf12009-07-10 21:39:28 +0000638 }
639 }
640
641 return false;
642}
643
Misha Brukman9558c6a2003-09-29 22:39:25 +0000644int GCC::ExecuteProgram(const std::string &ProgramFile,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000645 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000646 FileType fileType,
647 const std::string &InputFile,
648 const std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000649 std::string *Error,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000650 const std::vector<std::string> &ArgsForGCC,
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000651 unsigned Timeout,
652 unsigned MemoryLimit) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000653 std::vector<const char*> GCCArgs;
654
655 GCCArgs.push_back(GCCPath.c_str());
Chris Lattnereeed9832003-10-14 21:52:52 +0000656
Chris Lattner50010422010-03-16 06:41:47 +0000657 if (TargetTriple.getArch() == Triple::x86)
658 GCCArgs.push_back("-m32");
659
Bill Wendling38efa382009-03-02 23:13:18 +0000660 for (std::vector<std::string>::const_iterator
661 I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I)
662 GCCArgs.push_back(I->c_str());
663
Chris Lattnereeed9832003-10-14 21:52:52 +0000664 // Specify -x explicitly in case the extension is wonky
Chris Lattner50010422010-03-16 06:41:47 +0000665 if (fileType != ObjectFile) {
666 GCCArgs.push_back("-x");
667 if (fileType == CFile) {
668 GCCArgs.push_back("c");
669 GCCArgs.push_back("-fno-strict-aliasing");
670 } else {
671 GCCArgs.push_back("assembler");
Daniel Dunbarca740962009-08-18 03:35:57 +0000672
Chris Lattner50010422010-03-16 06:41:47 +0000673 // For ARM architectures we don't want this flag. bugpoint isn't
674 // explicitly told what architecture it is working on, so we get
675 // it from gcc flags
676 if ((TargetTriple.getOS() == Triple::Darwin) &&
Jakob Stoklund Olesen887b7032010-07-29 00:52:16 +0000677 !IsARMArchitecture(GCCArgs))
Chris Lattner50010422010-03-16 06:41:47 +0000678 GCCArgs.push_back("-force_cpusubtype_ALL");
679 }
Misha Brukman9558c6a2003-09-29 22:39:25 +0000680 }
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000681
Chris Lattner50010422010-03-16 06:41:47 +0000682 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename.
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000683
Chris Lattner629e4872006-06-09 21:31:53 +0000684 GCCArgs.push_back("-x");
685 GCCArgs.push_back("none");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000686 GCCArgs.push_back("-o");
Reid Spencercda985e2004-12-15 01:51:56 +0000687 sys::Path OutputBinary (ProgramFile+".gcc.exe");
Reid Spencer51c5a282006-08-23 20:34:57 +0000688 std::string ErrMsg;
689 if (OutputBinary.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000690 errs() << "Error making unique filename: " << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000691 exit(1);
692 }
Misha Brukman9558c6a2003-09-29 22:39:25 +0000693 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
Reid Spencer51ab5c82006-06-06 00:00:42 +0000694
695 // Add any arguments intended for GCC. We locate them here because this is
696 // most likely -L and -l options that need to come before other libraries but
697 // after the source. Other options won't be sensitive to placement on the
698 // command line, so this should be safe.
699 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
700 GCCArgs.push_back(ArgsForGCC[i].c_str());
701
Misha Brukman9558c6a2003-09-29 22:39:25 +0000702 GCCArgs.push_back("-lm"); // Hard-code the math library...
703 GCCArgs.push_back("-O2"); // Optimize the program a bit...
Brian Gaekec8db76c2003-11-18 06:31:17 +0000704#if defined (HAVE_LINK_R)
Chris Lattner1f0f1622003-10-18 21:54:47 +0000705 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
Brian Gaekec8db76c2003-11-18 06:31:17 +0000706#endif
Daniel Dunbarca740962009-08-18 03:35:57 +0000707 if (TargetTriple.getArch() == Triple::sparc)
708 GCCArgs.push_back("-mcpu=v9");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000709 GCCArgs.push_back(0); // NULL terminator
710
Dan Gohmana1bdced2009-07-15 17:29:42 +0000711 outs() << "<gcc>"; outs().flush();
Dan Gohman65f57c22009-07-15 16:35:29 +0000712 DEBUG(errs() << "\nAbout to run:\t";
Nick Lewycky22ff7482010-04-12 05:08:25 +0000713 for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i)
Dan Gohman65f57c22009-07-15 16:35:29 +0000714 errs() << " " << GCCArgs[i];
715 errs() << "\n";
Evan Cheng7f7fdcc2007-01-03 07:44:30 +0000716 );
Reid Spencerb31baa82004-12-19 18:00:21 +0000717 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
718 sys::Path())) {
Nick Lewycky22ff7482010-04-12 05:08:25 +0000719 *Error = ProcessFailure(GCCPath, &GCCArgs[0]);
720 return -1;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000721 }
722
Rafael Espindolab30cdd82010-07-24 23:05:45 +0000723 std::vector<const char*> ProgramArgs;
Rafael Espindola97595eb2010-07-24 23:02:11 +0000724
725 // Declared here so that the destructor only runs after
726 // ProgramArgs is used.
727 std::string Exec;
Chris Lattner45495c52005-02-13 23:13:47 +0000728
Evan Cheng70f684f2008-09-09 06:11:26 +0000729 if (RemoteClientPath.isEmpty())
Evan Cheng34e400e2007-05-03 18:36:15 +0000730 ProgramArgs.push_back(OutputBinary.c_str());
731 else {
Evan Cheng70f684f2008-09-09 06:11:26 +0000732 ProgramArgs.push_back(RemoteClientPath.c_str());
733 ProgramArgs.push_back(RemoteHost.c_str());
Anton Korobeynikov26964b62009-08-05 09:32:53 +0000734 if (!RemoteUser.empty()) {
735 ProgramArgs.push_back("-l");
736 ProgramArgs.push_back(RemoteUser.c_str());
737 }
David Goodwin80becf12009-07-10 21:39:28 +0000738 if (!RemotePort.empty()) {
739 ProgramArgs.push_back("-p");
740 ProgramArgs.push_back(RemotePort.c_str());
741 }
Evan Cheng70f684f2008-09-09 06:11:26 +0000742 if (!RemoteExtra.empty()) {
743 ProgramArgs.push_back(RemoteExtra.c_str());
744 }
Evan Cheng34e400e2007-05-03 18:36:15 +0000745
David Goodwin91cf3612009-07-20 17:15:03 +0000746 // Full path to the binary. We need to cd to the exec directory because
747 // there is a dylib there that the exec expects to find in the CWD
Evan Cheng34e400e2007-05-03 18:36:15 +0000748 char* env_pwd = getenv("PWD");
Rafael Espindola97595eb2010-07-24 23:02:11 +0000749 Exec = "cd ";
Evan Cheng34e400e2007-05-03 18:36:15 +0000750 Exec += env_pwd;
David Goodwin91cf3612009-07-20 17:15:03 +0000751 Exec += "; ./";
Evan Cheng34e400e2007-05-03 18:36:15 +0000752 Exec += OutputBinary.c_str();
753 ProgramArgs.push_back(Exec.c_str());
754 }
755
Misha Brukman9558c6a2003-09-29 22:39:25 +0000756 // Add optional parameters to the running program from Argv
Nick Lewycky22ff7482010-04-12 05:08:25 +0000757 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000758 ProgramArgs.push_back(Args[i].c_str());
759 ProgramArgs.push_back(0); // NULL terminator
760
761 // Now that we have a binary, run it!
Dan Gohmana1bdced2009-07-15 17:29:42 +0000762 outs() << "<program>"; outs().flush();
Dan Gohman65f57c22009-07-15 16:35:29 +0000763 DEBUG(errs() << "\nAbout to run:\t";
Nick Lewycky22ff7482010-04-12 05:08:25 +0000764 for (unsigned i = 0, e = ProgramArgs.size()-1; i != e; ++i)
Dan Gohman65f57c22009-07-15 16:35:29 +0000765 errs() << " " << ProgramArgs[i];
766 errs() << "\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000767 );
Chris Lattner8c56be52004-02-18 20:21:57 +0000768
Anton Korobeynikov86c006a2009-08-05 09:32:10 +0000769 FileRemover OutputBinaryRemover(OutputBinary, !SaveTemps);
Evan Cheng34e400e2007-05-03 18:36:15 +0000770
Viktor Kutuzov699220b2009-07-14 19:10:55 +0000771 if (RemoteClientPath.isEmpty()) {
Nick Lewycky22ff7482010-04-12 05:08:25 +0000772 DEBUG(errs() << "<run locally>");
Evan Cheng34e400e2007-05-03 18:36:15 +0000773 return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
774 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
775 Timeout, MemoryLimit);
Viktor Kutuzov699220b2009-07-14 19:10:55 +0000776 } else {
Dan Gohmana1bdced2009-07-15 17:29:42 +0000777 outs() << "<run remotely>"; outs().flush();
Viktor Kutuzovfc2271f2009-07-18 18:39:24 +0000778 return RunProgramRemotelyWithTimeout(sys::Path(RemoteClientPath),
Viktor Kutuzov699220b2009-07-14 19:10:55 +0000779 &ProgramArgs[0], sys::Path(InputFile), sys::Path(OutputFile),
780 sys::Path(OutputFile), Timeout, MemoryLimit);
Viktor Kutuzov699220b2009-07-14 19:10:55 +0000781 }
Misha Brukman9558c6a2003-09-29 22:39:25 +0000782}
783
Chris Lattner1798e4a2003-10-14 21:07:25 +0000784int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
Chris Lattner130e2a32006-06-27 20:35:36 +0000785 std::string &OutputFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000786 const std::vector<std::string> &ArgsForGCC,
787 std::string &Error) {
Reid Spencercda985e2004-12-15 01:51:56 +0000788 sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
Reid Spencer51c5a282006-08-23 20:34:57 +0000789 std::string ErrMsg;
790 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000791 errs() << "Error making unique filename: " << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000792 exit(1);
793 }
Chris Lattner74382b72009-08-23 22:45:37 +0000794 OutputFile = uniqueFilename.str();
Reid Spencercda985e2004-12-15 01:51:56 +0000795
Chris Lattner130e2a32006-06-27 20:35:36 +0000796 std::vector<const char*> GCCArgs;
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000797
Chris Lattner130e2a32006-06-27 20:35:36 +0000798 GCCArgs.push_back(GCCPath.c_str());
Evan Chengfd829952009-03-12 00:53:34 +0000799
Chris Lattner50010422010-03-16 06:41:47 +0000800 if (TargetTriple.getArch() == Triple::x86)
801 GCCArgs.push_back("-m32");
802
Evan Chengfd829952009-03-12 00:53:34 +0000803 for (std::vector<std::string>::const_iterator
804 I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I)
805 GCCArgs.push_back(I->c_str());
806
Misha Brukman9558c6a2003-09-29 22:39:25 +0000807 // Compile the C/asm file into a shared object
Chris Lattner50010422010-03-16 06:41:47 +0000808 if (fileType != ObjectFile) {
809 GCCArgs.push_back("-x");
810 GCCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
811 }
Chris Lattner130e2a32006-06-27 20:35:36 +0000812 GCCArgs.push_back("-fno-strict-aliasing");
813 GCCArgs.push_back(InputFile.c_str()); // Specify the input filename.
Lauro Ramos Venancio497391a2007-06-06 23:10:56 +0000814 GCCArgs.push_back("-x");
815 GCCArgs.push_back("none");
Daniel Dunbarca740962009-08-18 03:35:57 +0000816 if (TargetTriple.getArch() == Triple::sparc)
817 GCCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc
818 else if (TargetTriple.getOS() == Triple::Darwin) {
819 // link all source files into a single module in data segment, rather than
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000820 // generating blocks. dynamic_lookup requires that you set
Daniel Dunbarca740962009-08-18 03:35:57 +0000821 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for
822 // bugpoint to just pass that in the environment of GCC.
823 GCCArgs.push_back("-single_module");
824 GCCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC
825 GCCArgs.push_back("-undefined");
826 GCCArgs.push_back("dynamic_lookup");
827 } else
828 GCCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others
Chris Lattner1c81f132005-03-09 03:31:02 +0000829
Daniel Dunbarca740962009-08-18 03:35:57 +0000830 if ((TargetTriple.getArch() == Triple::alpha) ||
831 (TargetTriple.getArch() == Triple::x86_64))
832 GCCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC
833
834 if (TargetTriple.getArch() == Triple::sparc)
835 GCCArgs.push_back("-mcpu=v9");
836
Chris Lattner130e2a32006-06-27 20:35:36 +0000837 GCCArgs.push_back("-o");
838 GCCArgs.push_back(OutputFile.c_str()); // Output to the right filename.
839 GCCArgs.push_back("-O2"); // Optimize the program a bit.
840
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000841
842
Chris Lattner130e2a32006-06-27 20:35:36 +0000843 // Add any arguments intended for GCC. We locate them here because this is
844 // most likely -L and -l options that need to come before other libraries but
845 // after the source. Other options won't be sensitive to placement on the
846 // command line, so this should be safe.
847 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
848 GCCArgs.push_back(ArgsForGCC[i].c_str());
849 GCCArgs.push_back(0); // NULL terminator
850
Mikhail Glushenkov544fba12010-11-03 16:14:07 +0000851
Misha Brukmanf976c852005-04-21 22:55:34 +0000852
Dan Gohmana1bdced2009-07-15 17:29:42 +0000853 outs() << "<gcc>"; outs().flush();
Dan Gohman65f57c22009-07-15 16:35:29 +0000854 DEBUG(errs() << "\nAbout to run:\t";
Nick Lewycky22ff7482010-04-12 05:08:25 +0000855 for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i)
Dan Gohman65f57c22009-07-15 16:35:29 +0000856 errs() << " " << GCCArgs[i];
857 errs() << "\n";
Evan Cheng7f7fdcc2007-01-03 07:44:30 +0000858 );
Chris Lattner130e2a32006-06-27 20:35:36 +0000859 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
Reid Spencerb31baa82004-12-19 18:00:21 +0000860 sys::Path())) {
Nick Lewycky22ff7482010-04-12 05:08:25 +0000861 Error = ProcessFailure(GCCPath, &GCCArgs[0]);
Chris Lattner1798e4a2003-10-14 21:07:25 +0000862 return 1;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000863 }
864 return 0;
865}
866
Chris Lattner7915a1e2003-10-14 21:34:11 +0000867/// create - Try to find the `gcc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000868///
Dan Gohman197f7282009-08-05 20:21:17 +0000869GCC *GCC::create(std::string &Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000870 const std::string &GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +0000871 const std::vector<std::string> *Args) {
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000872 sys::Path GCCPath = sys::Program::FindProgramByName(GCCBinary);
Reid Spencerb31baa82004-12-19 18:00:21 +0000873 if (GCCPath.isEmpty()) {
Dan Gohman62a5ff52010-10-29 16:15:23 +0000874 Message = "Cannot find `"+ GCCBinary +"' in PATH!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000875 return 0;
876 }
877
Evan Cheng70f684f2008-09-09 06:11:26 +0000878 sys::Path RemoteClientPath;
879 if (!RemoteClient.empty())
Dan Gohman197f7282009-08-05 20:21:17 +0000880 RemoteClientPath = sys::Program::FindProgramByName(RemoteClient);
Evan Cheng34e400e2007-05-03 18:36:15 +0000881
Chris Lattner74382b72009-08-23 22:45:37 +0000882 Message = "Found gcc: " + GCCPath.str() + "\n";
Bill Wendling38efa382009-03-02 23:13:18 +0000883 return new GCC(GCCPath, RemoteClientPath, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000884}