blob: a28b2856f1e9cf0683e95ab91ace29c52b6f936a [file] [log] [blame]
Chris Lattnerffac2862006-06-06 22:30:59 +00001//===-- ToolRunner.cpp ----------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerffac2862006-06-06 22:30:59 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the interfaces described in the ToolRunner.h file.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerffac2862006-06-06 22:30:59 +000014#include "ToolRunner.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000015#include "llvm/Config/config.h" // for HAVE_LINK_R
Evan Cheng779b52e2007-05-03 18:36:15 +000016#include "llvm/Support/CommandLine.h"
Chris Lattnerffac2862006-06-06 22:30:59 +000017#include "llvm/Support/Debug.h"
Rafael Espindola8ffbac92013-06-18 17:20:08 +000018#include "llvm/Support/FileSystem.h"
Chris Lattnerffac2862006-06-06 22:30:59 +000019#include "llvm/Support/FileUtilities.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000020#include "llvm/Support/Program.h"
Chris Lattnerc521f542009-08-23 22:45:37 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattnerffac2862006-06-06 22:30:59 +000022#include <fstream>
23#include <sstream>
Chris Lattnerffac2862006-06-06 22:30:59 +000024using namespace llvm;
25
Chandler Carruthf98597a2014-04-22 03:10:36 +000026#define DEBUG_TYPE "toolrunner"
27
Anton Korobeynikov7cbff912009-08-05 09:32:10 +000028namespace llvm {
29 cl::opt<bool>
30 SaveTemps("save-temps", cl::init(false), cl::desc("Save temporary files"));
31}
32
Evan Cheng779b52e2007-05-03 18:36:15 +000033namespace {
34 cl::opt<std::string>
Evan Cheng7aedcf12008-09-09 06:11:26 +000035 RemoteClient("remote-client",
36 cl::desc("Remote execution client (rsh/ssh)"));
Evan Cheng779b52e2007-05-03 18:36:15 +000037
38 cl::opt<std::string>
Evan Cheng7aedcf12008-09-09 06:11:26 +000039 RemoteHost("remote-host",
40 cl::desc("Remote execution (rsh/ssh) host"));
41
42 cl::opt<std::string>
David Goodwin73f4e3f2009-07-10 21:39:28 +000043 RemotePort("remote-port",
44 cl::desc("Remote execution (rsh/ssh) port"));
45
46 cl::opt<std::string>
Evan Cheng7aedcf12008-09-09 06:11:26 +000047 RemoteUser("remote-user",
48 cl::desc("Remote execution (rsh/ssh) user id"));
49
50 cl::opt<std::string>
51 RemoteExtra("remote-extra-options",
52 cl::desc("Remote execution (rsh/ssh) extra options"));
Evan Cheng779b52e2007-05-03 18:36:15 +000053}
54
Viktor Kutuzov85870252009-07-18 18:39:24 +000055/// RunProgramWithTimeout - This function provides an alternate interface
56/// to the sys::Program::ExecuteAndWait interface.
Nick Lewycky6ba630b2010-04-12 05:08:25 +000057/// @see sys::Program::ExecuteAndWait
Rafael Espindolab851d422013-06-13 15:47:11 +000058static int RunProgramWithTimeout(StringRef ProgramPath,
Chris Lattnerffac2862006-06-06 22:30:59 +000059 const char **Args,
Rafael Espindolab851d422013-06-13 15:47:11 +000060 StringRef StdInFile,
61 StringRef StdOutFile,
62 StringRef StdErrFile,
Anton Korobeynikovd01defe2007-02-16 19:11:07 +000063 unsigned NumSeconds = 0,
Dan Gohman7b0c25f2010-11-09 01:13:31 +000064 unsigned MemoryLimit = 0,
Craig Toppere6cb63e2014-04-25 04:24:47 +000065 std::string *ErrMsg = nullptr) {
Rafael Espindola7c1023a2013-06-13 20:25:38 +000066 const StringRef *Redirects[3] = { &StdInFile, &StdOutFile, &StdErrFile };
Viktor Kutuzov85870252009-07-18 18:39:24 +000067
68#if 0 // For debug purposes
69 {
Dan Gohmand8db3762009-07-15 16:35:29 +000070 errs() << "RUN:";
Chris Lattnerff8a6b82006-09-15 23:01:10 +000071 for (unsigned i = 0; Args[i]; ++i)
Dan Gohmand8db3762009-07-15 16:35:29 +000072 errs() << " " << Args[i];
73 errs() << "\n";
Chris Lattnerff8a6b82006-09-15 23:01:10 +000074 }
Viktor Kutuzov85870252009-07-18 18:39:24 +000075#endif
Chris Lattnerffac2862006-06-06 22:30:59 +000076
Craig Toppere6cb63e2014-04-25 04:24:47 +000077 return sys::ExecuteAndWait(ProgramPath, Args, nullptr, Redirects,
Rafael Espindolab851d422013-06-13 15:47:11 +000078 NumSeconds, MemoryLimit, ErrMsg);
Chris Lattnerffac2862006-06-06 22:30:59 +000079}
80
Viktor Kutuzov85870252009-07-18 18:39:24 +000081/// RunProgramRemotelyWithTimeout - This function runs the given program
82/// remotely using the given remote client and the sys::Program::ExecuteAndWait.
83/// Returns the remote program exit code or reports a remote client error if it
84/// fails. Remote client is required to return 255 if it failed or program exit
85/// code otherwise.
Nick Lewycky6ba630b2010-04-12 05:08:25 +000086/// @see sys::Program::ExecuteAndWait
Rafael Espindolab851d422013-06-13 15:47:11 +000087static int RunProgramRemotelyWithTimeout(StringRef RemoteClientPath,
Viktor Kutuzov85870252009-07-18 18:39:24 +000088 const char **Args,
Rafael Espindolab851d422013-06-13 15:47:11 +000089 StringRef StdInFile,
90 StringRef StdOutFile,
91 StringRef StdErrFile,
Viktor Kutuzov85870252009-07-18 18:39:24 +000092 unsigned NumSeconds = 0,
93 unsigned MemoryLimit = 0) {
Rafael Espindola7c1023a2013-06-13 20:25:38 +000094 const StringRef *Redirects[3] = { &StdInFile, &StdOutFile, &StdErrFile };
Chris Lattnerffac2862006-06-06 22:30:59 +000095
Viktor Kutuzov85870252009-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
Craig Toppere6cb63e2014-04-25 04:24:47 +0000106 int ReturnCode = sys::ExecuteAndWait(RemoteClientPath, Args, nullptr,
Rafael Espindola7c1023a2013-06-13 20:25:38 +0000107 Redirects, NumSeconds, MemoryLimit);
Viktor Kutuzov85870252009-07-18 18:39:24 +0000108
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.
Rafael Espindolac0bbe362013-06-13 15:52:54 +0000118 std::string StdOutFileName = StdOutFile.str();
119 std::ifstream ErrorFile(StdOutFileName.c_str());
Viktor Kutuzov85870252009-07-18 18:39:24 +0000120 if (ErrorFile) {
121 std::copy(std::istreambuf_iterator<char>(ErrorFile),
122 std::istreambuf_iterator<char>(),
123 std::ostreambuf_iterator<char>(OS));
124 ErrorFile.close();
125 }
126
John McCall2a784002012-05-02 05:39:10 +0000127 errs() << OS.str();
Viktor Kutuzov85870252009-07-18 18:39:24 +0000128 }
129
130 return ReturnCode;
131}
Chris Lattnerffac2862006-06-06 22:30:59 +0000132
Rafael Espindolab851d422013-06-13 15:47:11 +0000133static std::string ProcessFailure(StringRef ProgPath, const char** Args,
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000134 unsigned Timeout = 0,
135 unsigned MemoryLimit = 0) {
Chris Lattnerffac2862006-06-06 22:30:59 +0000136 std::ostringstream OS;
137 OS << "\nError running tool:\n ";
138 for (const char **Arg = Args; *Arg; ++Arg)
139 OS << " " << *Arg;
140 OS << "\n";
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000141
Chris Lattnerffac2862006-06-06 22:30:59 +0000142 // Rerun the compiler, capturing any error messages to print them.
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000143 SmallString<128> ErrorFilename;
144 int ErrorFD;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000145 std::error_code EC = sys::fs::createTemporaryFile(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000146 "bugpoint.program_error_messages", "", ErrorFD, ErrorFilename);
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000147 if (EC) {
148 errs() << "Error making unique filename: " << EC.message() << "\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000149 exit(1);
150 }
NAKAMURA Takumif0e30f62014-07-13 13:28:18 +0000151
152#ifdef _WIN32
153 // Close ErrorFD immediately, or it couldn't be reopened on Win32.
154 // FIXME: We may have an option in openFileForWrite(), not to use ResultFD
155 // but to close it.
156 delete new raw_fd_ostream(ErrorFD, true);
157#endif
158
Rafael Espindolab851d422013-06-13 15:47:11 +0000159 RunProgramWithTimeout(ProgPath, Args, "", ErrorFilename.str(),
160 ErrorFilename.str(), Timeout, MemoryLimit);
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000161 // FIXME: check return code ?
Chris Lattnerffac2862006-06-06 22:30:59 +0000162
163 // Print out the error messages generated by GCC if possible...
164 std::ifstream ErrorFile(ErrorFilename.c_str());
165 if (ErrorFile) {
166 std::copy(std::istreambuf_iterator<char>(ErrorFile),
167 std::istreambuf_iterator<char>(),
168 std::ostreambuf_iterator<char>(OS));
169 ErrorFile.close();
170 }
171
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000172 sys::fs::remove(ErrorFilename.c_str());
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000173 return OS.str();
Chris Lattnerffac2862006-06-06 22:30:59 +0000174}
175
176//===---------------------------------------------------------------------===//
177// LLI Implementation of AbstractIntepreter interface
178//
179namespace {
180 class LLI : public AbstractInterpreter {
181 std::string LLIPath; // The path to the LLI executable
182 std::vector<std::string> ToolArgs; // Args to pass to LLI
183 public:
184 LLI(const std::string &Path, const std::vector<std::string> *Args)
185 : LLIPath(Path) {
186 ToolArgs.clear ();
187 if (Args) { ToolArgs = *Args; }
188 }
189
Craig Toppere56917c2014-03-08 08:27:28 +0000190 int ExecuteProgram(const std::string &Bitcode,
191 const std::vector<std::string> &Args,
192 const std::string &InputFile,
193 const std::string &OutputFile,
194 std::string *Error,
195 const std::vector<std::string> &GCCArgs,
196 const std::vector<std::string> &SharedLibs =
197 std::vector<std::string>(),
198 unsigned Timeout = 0,
199 unsigned MemoryLimit = 0) override;
Chris Lattnerffac2862006-06-06 22:30:59 +0000200 };
201}
202
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000203int LLI::ExecuteProgram(const std::string &Bitcode,
Chris Lattnerffac2862006-06-06 22:30:59 +0000204 const std::vector<std::string> &Args,
205 const std::string &InputFile,
206 const std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000207 std::string *Error,
Chris Lattnerffac2862006-06-06 22:30:59 +0000208 const std::vector<std::string> &GCCArgs,
209 const std::vector<std::string> &SharedLibs,
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000210 unsigned Timeout,
211 unsigned MemoryLimit) {
Chris Lattnerffac2862006-06-06 22:30:59 +0000212 std::vector<const char*> LLIArgs;
213 LLIArgs.push_back(LLIPath.c_str());
214 LLIArgs.push_back("-force-interpreter=true");
215
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000216 for (std::vector<std::string>::const_iterator i = SharedLibs.begin(),
217 e = SharedLibs.end(); i != e; ++i) {
Duncan Sands3246fe02009-11-17 10:20:22 +0000218 LLIArgs.push_back("-load");
219 LLIArgs.push_back((*i).c_str());
220 }
221
Chris Lattnerffac2862006-06-06 22:30:59 +0000222 // Add any extra LLI args.
223 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
224 LLIArgs.push_back(ToolArgs[i].c_str());
225
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000226 LLIArgs.push_back(Bitcode.c_str());
Chris Lattnerffac2862006-06-06 22:30:59 +0000227 // Add optional parameters to the running program from Argv
228 for (unsigned i=0, e = Args.size(); i != e; ++i)
229 LLIArgs.push_back(Args[i].c_str());
Craig Toppere6cb63e2014-04-25 04:24:47 +0000230 LLIArgs.push_back(nullptr);
Chris Lattnerffac2862006-06-06 22:30:59 +0000231
Dan Gohman607818a2009-07-15 17:29:42 +0000232 outs() << "<lli>"; outs().flush();
Dan Gohmand8db3762009-07-15 16:35:29 +0000233 DEBUG(errs() << "\nAbout to run:\t";
Chris Lattnerffac2862006-06-06 22:30:59 +0000234 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
Dan Gohmand8db3762009-07-15 16:35:29 +0000235 errs() << " " << LLIArgs[i];
236 errs() << "\n";
Chris Lattnerffac2862006-06-06 22:30:59 +0000237 );
Rafael Espindolab851d422013-06-13 15:47:11 +0000238 return RunProgramWithTimeout(LLIPath, &LLIArgs[0],
239 InputFile, OutputFile, OutputFile,
Dan Gohman7b0c25f2010-11-09 01:13:31 +0000240 Timeout, MemoryLimit, Error);
Chris Lattnerffac2862006-06-06 22:30:59 +0000241}
242
David Blaikiea379b1812011-12-20 02:50:00 +0000243void AbstractInterpreter::anchor() { }
244
Rafael Espindola242fcb82013-06-25 14:42:30 +0000245#if defined(LLVM_ON_UNIX)
246const char EXESuffix[] = "";
247#elif defined (LLVM_ON_WIN32)
248const char EXESuffix[] = "exe";
249#endif
250
Rafael Espindolab7863042013-06-14 15:12:13 +0000251/// Prepend the path to the program being executed
252/// to \p ExeName, given the value of argv[0] and the address of main()
253/// itself. This allows us to find another LLVM tool if it is built in the same
254/// directory. An empty string is returned on error; note that this function
255/// just mainpulates the path and doesn't check for executability.
256/// @brief Find a named executable.
Rafael Espindolac7247652013-06-18 16:47:55 +0000257static std::string PrependMainExecutablePath(const std::string &ExeName,
258 const char *Argv0,
259 void *MainAddr) {
Rafael Espindolab7863042013-06-14 15:12:13 +0000260 // Check the directory that the calling program is in. We can do
261 // this if ProgramPath contains at least one / character, indicating that it
262 // is a relative path to the executable itself.
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000263 std::string Main = sys::fs::getMainExecutable(Argv0, MainAddr);
264 StringRef Result = sys::path::parent_path(Main);
Rafael Espindolab7863042013-06-14 15:12:13 +0000265
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000266 if (!Result.empty()) {
267 SmallString<128> Storage = Result;
268 sys::path::append(Storage, ExeName);
Rafael Espindola242fcb82013-06-25 14:42:30 +0000269 sys::path::replace_extension(Storage, EXESuffix);
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000270 return Storage.str();
Rafael Espindolab7863042013-06-14 15:12:13 +0000271 }
272
Rafael Espindolac7247652013-06-18 16:47:55 +0000273 return Result.str();
Rafael Espindolab7863042013-06-14 15:12:13 +0000274}
275
Chris Lattnerffac2862006-06-06 22:30:59 +0000276// LLI create method - Try to find the LLI executable
Dan Gohman46ffffa2009-08-05 20:21:17 +0000277AbstractInterpreter *AbstractInterpreter::createLLI(const char *Argv0,
Chris Lattnerffac2862006-06-06 22:30:59 +0000278 std::string &Message,
279 const std::vector<std::string> *ToolArgs) {
Dan Gohman46ffffa2009-08-05 20:21:17 +0000280 std::string LLIPath =
Rafael Espindolac7247652013-06-18 16:47:55 +0000281 PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t) & createLLI);
Chris Lattnerffac2862006-06-06 22:30:59 +0000282 if (!LLIPath.empty()) {
283 Message = "Found lli: " + LLIPath + "\n";
284 return new LLI(LLIPath, ToolArgs);
285 }
286
Dan Gohman9e082462010-10-29 16:15:23 +0000287 Message = "Cannot find `lli' in executable directory!\n";
Craig Toppere6cb63e2014-04-25 04:24:47 +0000288 return nullptr;
Chris Lattnerffac2862006-06-06 22:30:59 +0000289}
290
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000291//===---------------------------------------------------------------------===//
Andrew Trick8665d592011-02-08 18:20:48 +0000292// Custom compiler command implementation of AbstractIntepreter interface
293//
294// Allows using a custom command for compiling the bitcode, thus allows, for
295// example, to compile a bitcode fragment without linking or executing, then
296// using a custom wrapper script to check for compiler errors.
297namespace {
298 class CustomCompiler : public AbstractInterpreter {
299 std::string CompilerCommand;
300 std::vector<std::string> CompilerArgs;
301 public:
302 CustomCompiler(
303 const std::string &CompilerCmd, std::vector<std::string> CompArgs) :
304 CompilerCommand(CompilerCmd), CompilerArgs(CompArgs) {}
305
Craig Toppere56917c2014-03-08 08:27:28 +0000306 void compileProgram(const std::string &Bitcode,
307 std::string *Error,
308 unsigned Timeout = 0,
309 unsigned MemoryLimit = 0) override;
Andrew Trick8665d592011-02-08 18:20:48 +0000310
Craig Toppere56917c2014-03-08 08:27:28 +0000311 int ExecuteProgram(const std::string &Bitcode,
312 const std::vector<std::string> &Args,
313 const std::string &InputFile,
314 const std::string &OutputFile,
315 std::string *Error,
316 const std::vector<std::string> &GCCArgs =
317 std::vector<std::string>(),
318 const std::vector<std::string> &SharedLibs =
319 std::vector<std::string>(),
320 unsigned Timeout = 0,
321 unsigned MemoryLimit = 0) override {
Andrew Trick8665d592011-02-08 18:20:48 +0000322 *Error = "Execution not supported with -compile-custom";
323 return -1;
324 }
325 };
326}
327
328void CustomCompiler::compileProgram(const std::string &Bitcode,
329 std::string *Error,
330 unsigned Timeout,
331 unsigned MemoryLimit) {
332
333 std::vector<const char*> ProgramArgs;
334 ProgramArgs.push_back(CompilerCommand.c_str());
335
336 for (std::size_t i = 0; i < CompilerArgs.size(); ++i)
337 ProgramArgs.push_back(CompilerArgs.at(i).c_str());
338 ProgramArgs.push_back(Bitcode.c_str());
Craig Toppere6cb63e2014-04-25 04:24:47 +0000339 ProgramArgs.push_back(nullptr);
Andrew Trick8665d592011-02-08 18:20:48 +0000340
341 // Add optional parameters to the running program from Argv
342 for (unsigned i = 0, e = CompilerArgs.size(); i != e; ++i)
343 ProgramArgs.push_back(CompilerArgs[i].c_str());
344
Rafael Espindolab851d422013-06-13 15:47:11 +0000345 if (RunProgramWithTimeout(CompilerCommand, &ProgramArgs[0],
346 "", "", "",
Andrew Trick8665d592011-02-08 18:20:48 +0000347 Timeout, MemoryLimit, Error))
Rafael Espindolab851d422013-06-13 15:47:11 +0000348 *Error = ProcessFailure(CompilerCommand, &ProgramArgs[0],
Andrew Trick8665d592011-02-08 18:20:48 +0000349 Timeout, MemoryLimit);
350}
351
352//===---------------------------------------------------------------------===//
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000353// Custom execution command implementation of AbstractIntepreter interface
354//
355// Allows using a custom command for executing the bitcode, thus allows,
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000356// for example, to invoke a cross compiler for code generation followed by
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000357// a simulator that executes the generated binary.
358namespace {
359 class CustomExecutor : public AbstractInterpreter {
360 std::string ExecutionCommand;
361 std::vector<std::string> ExecutorArgs;
362 public:
363 CustomExecutor(
364 const std::string &ExecutionCmd, std::vector<std::string> ExecArgs) :
365 ExecutionCommand(ExecutionCmd), ExecutorArgs(ExecArgs) {}
366
Craig Toppere56917c2014-03-08 08:27:28 +0000367 int ExecuteProgram(const std::string &Bitcode,
368 const std::vector<std::string> &Args,
369 const std::string &InputFile,
370 const std::string &OutputFile,
371 std::string *Error,
372 const std::vector<std::string> &GCCArgs,
373 const std::vector<std::string> &SharedLibs =
374 std::vector<std::string>(),
375 unsigned Timeout = 0,
376 unsigned MemoryLimit = 0) override;
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000377 };
378}
379
380int CustomExecutor::ExecuteProgram(const std::string &Bitcode,
381 const std::vector<std::string> &Args,
382 const std::string &InputFile,
383 const std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000384 std::string *Error,
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000385 const std::vector<std::string> &GCCArgs,
386 const std::vector<std::string> &SharedLibs,
387 unsigned Timeout,
388 unsigned MemoryLimit) {
389
390 std::vector<const char*> ProgramArgs;
391 ProgramArgs.push_back(ExecutionCommand.c_str());
392
393 for (std::size_t i = 0; i < ExecutorArgs.size(); ++i)
394 ProgramArgs.push_back(ExecutorArgs.at(i).c_str());
395 ProgramArgs.push_back(Bitcode.c_str());
Craig Toppere6cb63e2014-04-25 04:24:47 +0000396 ProgramArgs.push_back(nullptr);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000397
398 // Add optional parameters to the running program from Argv
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000399 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000400 ProgramArgs.push_back(Args[i].c_str());
401
402 return RunProgramWithTimeout(
Rafael Espindolab851d422013-06-13 15:47:11 +0000403 ExecutionCommand,
404 &ProgramArgs[0], InputFile, OutputFile,
405 OutputFile, Timeout, MemoryLimit, Error);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000406}
407
Andrew Trick8665d592011-02-08 18:20:48 +0000408// Tokenize the CommandLine to the command and the args to allow
409// defining a full command line as the command instead of just the
410// executed program. We cannot just pass the whole string after the command
411// as a single argument because then program sees only a single
412// command line argument (with spaces in it: "foo bar" instead
413// of "foo" and "bar").
414//
415// code borrowed from:
416// http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
417static void lexCommand(std::string &Message, const std::string &CommandLine,
Adam Nemet5f9eb0a2014-03-08 07:48:19 +0000418 std::string &CmdPath, std::vector<std::string> &Args) {
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000419
420 std::string Command = "";
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000421 std::string delimiters = " ";
422
Andrew Trick8665d592011-02-08 18:20:48 +0000423 std::string::size_type lastPos = CommandLine.find_first_not_of(delimiters, 0);
424 std::string::size_type pos = CommandLine.find_first_of(delimiters, lastPos);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000425
426 while (std::string::npos != pos || std::string::npos != lastPos) {
Andrew Trick8665d592011-02-08 18:20:48 +0000427 std::string token = CommandLine.substr(lastPos, pos - lastPos);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000428 if (Command == "")
429 Command = token;
430 else
431 Args.push_back(token);
432 // Skip delimiters. Note the "not_of"
Andrew Trick8665d592011-02-08 18:20:48 +0000433 lastPos = CommandLine.find_first_not_of(delimiters, pos);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000434 // Find next "non-delimiter"
Andrew Trick8665d592011-02-08 18:20:48 +0000435 pos = CommandLine.find_first_of(delimiters, lastPos);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000436 }
437
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +0000438 CmdPath = sys::FindProgramByName(Command);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000439 if (CmdPath.empty()) {
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000440 Message =
441 std::string("Cannot find '") + Command +
Dan Gohman9e082462010-10-29 16:15:23 +0000442 "' in PATH!\n";
Andrew Trick8665d592011-02-08 18:20:48 +0000443 return;
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000444 }
445
446 Message = "Found command in: " + CmdPath + "\n";
Andrew Trick8665d592011-02-08 18:20:48 +0000447}
448
449// Custom execution environment create method, takes the execution command
450// as arguments
451AbstractInterpreter *AbstractInterpreter::createCustomCompiler(
452 std::string &Message,
453 const std::string &CompileCommandLine) {
454
455 std::string CmdPath;
456 std::vector<std::string> Args;
457 lexCommand(Message, CompileCommandLine, CmdPath, Args);
458 if (CmdPath.empty())
Craig Toppere6cb63e2014-04-25 04:24:47 +0000459 return nullptr;
Andrew Trick8665d592011-02-08 18:20:48 +0000460
461 return new CustomCompiler(CmdPath, Args);
462}
463
464// Custom execution environment create method, takes the execution command
465// as arguments
466AbstractInterpreter *AbstractInterpreter::createCustomExecutor(
467 std::string &Message,
468 const std::string &ExecCommandLine) {
469
470
471 std::string CmdPath;
472 std::vector<std::string> Args;
473 lexCommand(Message, ExecCommandLine, CmdPath, Args);
474 if (CmdPath.empty())
Craig Toppere6cb63e2014-04-25 04:24:47 +0000475 return nullptr;
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000476
477 return new CustomExecutor(CmdPath, Args);
478}
479
Chris Lattnerffac2862006-06-06 22:30:59 +0000480//===----------------------------------------------------------------------===//
481// LLC Implementation of AbstractIntepreter interface
482//
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000483GCC::FileType LLC::OutputCode(const std::string &Bitcode,
Rafael Espindola34889ca2013-06-17 19:21:38 +0000484 std::string &OutputAsmFile, std::string &Error,
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000485 unsigned Timeout, unsigned MemoryLimit) {
Chris Lattnerfd381322010-03-16 06:41:47 +0000486 const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s");
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000487
488 SmallString<128> UniqueFile;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000489 std::error_code EC =
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000490 sys::fs::createUniqueFile(Bitcode + "-%%%%%%%" + Suffix, UniqueFile);
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000491 if (EC) {
492 errs() << "Error making unique filename: " << EC.message() << "\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000493 exit(1);
494 }
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000495 OutputAsmFile = UniqueFile.str();
Chris Lattnerffac2862006-06-06 22:30:59 +0000496 std::vector<const char *> LLCArgs;
Chris Lattnerfd381322010-03-16 06:41:47 +0000497 LLCArgs.push_back(LLCPath.c_str());
Chris Lattnerffac2862006-06-06 22:30:59 +0000498
499 // Add any extra LLC args.
500 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
501 LLCArgs.push_back(ToolArgs[i].c_str());
502
Chris Lattnerfd381322010-03-16 06:41:47 +0000503 LLCArgs.push_back("-o");
504 LLCArgs.push_back(OutputAsmFile.c_str()); // Output to the Asm file
505 LLCArgs.push_back(Bitcode.c_str()); // This is the input bitcode
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000506
Chris Lattnerfd381322010-03-16 06:41:47 +0000507 if (UseIntegratedAssembler)
508 LLCArgs.push_back("-filetype=obj");
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000509
Craig Toppere6cb63e2014-04-25 04:24:47 +0000510 LLCArgs.push_back (nullptr);
Chris Lattnerffac2862006-06-06 22:30:59 +0000511
Chris Lattnerfd381322010-03-16 06:41:47 +0000512 outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>");
513 outs().flush();
Dan Gohmand8db3762009-07-15 16:35:29 +0000514 DEBUG(errs() << "\nAbout to run:\t";
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000515 for (unsigned i = 0, e = LLCArgs.size()-1; i != e; ++i)
Dan Gohmand8db3762009-07-15 16:35:29 +0000516 errs() << " " << LLCArgs[i];
517 errs() << "\n";
Chris Lattnerffac2862006-06-06 22:30:59 +0000518 );
Rafael Espindolab851d422013-06-13 15:47:11 +0000519 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0],
520 "", "", "",
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000521 Timeout, MemoryLimit))
Rafael Espindolab851d422013-06-13 15:47:11 +0000522 Error = ProcessFailure(LLCPath, &LLCArgs[0],
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000523 Timeout, MemoryLimit);
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000524 return UseIntegratedAssembler ? GCC::ObjectFile : GCC::AsmFile;
Chris Lattnerffac2862006-06-06 22:30:59 +0000525}
526
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000527void LLC::compileProgram(const std::string &Bitcode, std::string *Error,
528 unsigned Timeout, unsigned MemoryLimit) {
Rafael Espindola34889ca2013-06-17 19:21:38 +0000529 std::string OutputAsmFile;
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000530 OutputCode(Bitcode, OutputAsmFile, *Error, Timeout, MemoryLimit);
Rafael Espindola34889ca2013-06-17 19:21:38 +0000531 sys::fs::remove(OutputAsmFile);
Chris Lattnerffac2862006-06-06 22:30:59 +0000532}
533
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000534int LLC::ExecuteProgram(const std::string &Bitcode,
Chris Lattnerffac2862006-06-06 22:30:59 +0000535 const std::vector<std::string> &Args,
536 const std::string &InputFile,
537 const std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000538 std::string *Error,
Chris Lattnerffac2862006-06-06 22:30:59 +0000539 const std::vector<std::string> &ArgsForGCC,
540 const std::vector<std::string> &SharedLibs,
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000541 unsigned Timeout,
542 unsigned MemoryLimit) {
Chris Lattnerffac2862006-06-06 22:30:59 +0000543
Rafael Espindola34889ca2013-06-17 19:21:38 +0000544 std::string OutputAsmFile;
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000545 GCC::FileType FileKind = OutputCode(Bitcode, OutputAsmFile, *Error, Timeout,
546 MemoryLimit);
Rafael Espindola34889ca2013-06-17 19:21:38 +0000547 FileRemover OutFileRemover(OutputAsmFile, !SaveTemps);
Chris Lattnerffac2862006-06-06 22:30:59 +0000548
549 std::vector<std::string> GCCArgs(ArgsForGCC);
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000550 GCCArgs.insert(GCCArgs.end(), SharedLibs.begin(), SharedLibs.end());
Chris Lattnerffac2862006-06-06 22:30:59 +0000551
552 // Assuming LLC worked, compile the result with GCC and run it.
Rafael Espindola34889ca2013-06-17 19:21:38 +0000553 return gcc->ExecuteProgram(OutputAsmFile, Args, FileKind,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000554 InputFile, OutputFile, Error, GCCArgs,
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000555 Timeout, MemoryLimit);
Chris Lattnerffac2862006-06-06 22:30:59 +0000556}
557
558/// createLLC - Try to find the LLC executable
559///
Dan Gohman46ffffa2009-08-05 20:21:17 +0000560LLC *AbstractInterpreter::createLLC(const char *Argv0,
Chris Lattnerffac2862006-06-06 22:30:59 +0000561 std::string &Message,
Kalle Raiskila6be58292010-05-10 07:38:37 +0000562 const std::string &GCCBinary,
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000563 const std::vector<std::string> *Args,
Chris Lattnerfd381322010-03-16 06:41:47 +0000564 const std::vector<std::string> *GCCArgs,
565 bool UseIntegratedAssembler) {
Dan Gohman46ffffa2009-08-05 20:21:17 +0000566 std::string LLCPath =
Rafael Espindolac7247652013-06-18 16:47:55 +0000567 PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t) & createLLC);
Chris Lattnerffac2862006-06-06 22:30:59 +0000568 if (LLCPath.empty()) {
Dan Gohman9e082462010-10-29 16:15:23 +0000569 Message = "Cannot find `llc' in executable directory!\n";
Craig Toppere6cb63e2014-04-25 04:24:47 +0000570 return nullptr;
Chris Lattnerffac2862006-06-06 22:30:59 +0000571 }
572
Kalle Raiskila6be58292010-05-10 07:38:37 +0000573 GCC *gcc = GCC::create(Message, GCCBinary, GCCArgs);
Chris Lattnerffac2862006-06-06 22:30:59 +0000574 if (!gcc) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000575 errs() << Message << "\n";
Chris Lattnerffac2862006-06-06 22:30:59 +0000576 exit(1);
577 }
Saleem Abdulrasoolab4d7bc2013-01-24 16:49:12 +0000578 Message = "Found llc: " + LLCPath + "\n";
Kalle Raiskila6be58292010-05-10 07:38:37 +0000579 return new LLC(LLCPath, gcc, Args, UseIntegratedAssembler);
Chris Lattnerffac2862006-06-06 22:30:59 +0000580}
581
582//===---------------------------------------------------------------------===//
583// JIT Implementation of AbstractIntepreter interface
584//
585namespace {
586 class JIT : public AbstractInterpreter {
587 std::string LLIPath; // The path to the LLI executable
588 std::vector<std::string> ToolArgs; // Args to pass to LLI
589 public:
590 JIT(const std::string &Path, const std::vector<std::string> *Args)
591 : LLIPath(Path) {
592 ToolArgs.clear ();
593 if (Args) { ToolArgs = *Args; }
594 }
595
Craig Toppere56917c2014-03-08 08:27:28 +0000596 int ExecuteProgram(const std::string &Bitcode,
597 const std::vector<std::string> &Args,
598 const std::string &InputFile,
599 const std::string &OutputFile,
600 std::string *Error,
601 const std::vector<std::string> &GCCArgs =
602 std::vector<std::string>(),
603 const std::vector<std::string> &SharedLibs =
604 std::vector<std::string>(),
605 unsigned Timeout = 0,
606 unsigned MemoryLimit = 0) override;
Chris Lattnerffac2862006-06-06 22:30:59 +0000607 };
608}
609
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000610int JIT::ExecuteProgram(const std::string &Bitcode,
Chris Lattnerffac2862006-06-06 22:30:59 +0000611 const std::vector<std::string> &Args,
612 const std::string &InputFile,
613 const std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000614 std::string *Error,
Chris Lattnerffac2862006-06-06 22:30:59 +0000615 const std::vector<std::string> &GCCArgs,
616 const std::vector<std::string> &SharedLibs,
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000617 unsigned Timeout,
618 unsigned MemoryLimit) {
Chris Lattnerffac2862006-06-06 22:30:59 +0000619 // Construct a vector of parameters, incorporating those from the command-line
620 std::vector<const char*> JITArgs;
621 JITArgs.push_back(LLIPath.c_str());
622 JITArgs.push_back("-force-interpreter=false");
623
624 // Add any extra LLI args.
625 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
626 JITArgs.push_back(ToolArgs[i].c_str());
627
628 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
629 JITArgs.push_back("-load");
630 JITArgs.push_back(SharedLibs[i].c_str());
631 }
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000632 JITArgs.push_back(Bitcode.c_str());
Chris Lattnerffac2862006-06-06 22:30:59 +0000633 // Add optional parameters to the running program from Argv
634 for (unsigned i=0, e = Args.size(); i != e; ++i)
635 JITArgs.push_back(Args[i].c_str());
Craig Toppere6cb63e2014-04-25 04:24:47 +0000636 JITArgs.push_back(nullptr);
Chris Lattnerffac2862006-06-06 22:30:59 +0000637
Dan Gohman607818a2009-07-15 17:29:42 +0000638 outs() << "<jit>"; outs().flush();
Dan Gohmand8db3762009-07-15 16:35:29 +0000639 DEBUG(errs() << "\nAbout to run:\t";
Chris Lattnerffac2862006-06-06 22:30:59 +0000640 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
Dan Gohmand8db3762009-07-15 16:35:29 +0000641 errs() << " " << JITArgs[i];
642 errs() << "\n";
Chris Lattnerffac2862006-06-06 22:30:59 +0000643 );
Dan Gohmand8db3762009-07-15 16:35:29 +0000644 DEBUG(errs() << "\nSending output to " << OutputFile << "\n");
Rafael Espindolab851d422013-06-13 15:47:11 +0000645 return RunProgramWithTimeout(LLIPath, &JITArgs[0],
646 InputFile, OutputFile, OutputFile,
Dan Gohman7b0c25f2010-11-09 01:13:31 +0000647 Timeout, MemoryLimit, Error);
Chris Lattnerffac2862006-06-06 22:30:59 +0000648}
649
650/// createJIT - Try to find the LLI executable
651///
Dan Gohman46ffffa2009-08-05 20:21:17 +0000652AbstractInterpreter *AbstractInterpreter::createJIT(const char *Argv0,
Chris Lattnerffac2862006-06-06 22:30:59 +0000653 std::string &Message, const std::vector<std::string> *Args) {
Dan Gohman46ffffa2009-08-05 20:21:17 +0000654 std::string LLIPath =
Rafael Espindolac7247652013-06-18 16:47:55 +0000655 PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t) & createJIT);
Chris Lattnerffac2862006-06-06 22:30:59 +0000656 if (!LLIPath.empty()) {
657 Message = "Found lli: " + LLIPath + "\n";
658 return new JIT(LLIPath, Args);
659 }
660
Dan Gohman9e082462010-10-29 16:15:23 +0000661 Message = "Cannot find `lli' in executable directory!\n";
Craig Toppere6cb63e2014-04-25 04:24:47 +0000662 return nullptr;
Chris Lattnerffac2862006-06-06 22:30:59 +0000663}
664
Chris Lattnerffac2862006-06-06 22:30:59 +0000665//===---------------------------------------------------------------------===//
666// GCC abstraction
667//
David Goodwin73f4e3f2009-07-10 21:39:28 +0000668
Jakob Stoklund Olesen12222492010-07-29 00:52:16 +0000669static bool IsARMArchitecture(std::vector<const char*> Args) {
670 for (std::vector<const char*>::const_iterator
David Goodwin73f4e3f2009-07-10 21:39:28 +0000671 I = Args.begin(), E = Args.end(); I != E; ++I) {
Jakob Stoklund Olesen3e0ddc02010-05-13 17:58:15 +0000672 if (StringRef(*I).equals_lower("-arch")) {
David Goodwin73f4e3f2009-07-10 21:39:28 +0000673 ++I;
Jakub Staszakcfcfee02013-11-04 19:22:50 +0000674 if (I != E && StringRef(*I).startswith_lower("arm"))
David Goodwin73f4e3f2009-07-10 21:39:28 +0000675 return true;
David Goodwin73f4e3f2009-07-10 21:39:28 +0000676 }
677 }
678
679 return false;
680}
681
Chris Lattnerffac2862006-06-06 22:30:59 +0000682int GCC::ExecuteProgram(const std::string &ProgramFile,
683 const std::vector<std::string> &Args,
684 FileType fileType,
685 const std::string &InputFile,
686 const std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000687 std::string *Error,
Chris Lattnerffac2862006-06-06 22:30:59 +0000688 const std::vector<std::string> &ArgsForGCC,
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000689 unsigned Timeout,
690 unsigned MemoryLimit) {
Chris Lattnerffac2862006-06-06 22:30:59 +0000691 std::vector<const char*> GCCArgs;
692
693 GCCArgs.push_back(GCCPath.c_str());
694
Chris Lattnerfd381322010-03-16 06:41:47 +0000695 if (TargetTriple.getArch() == Triple::x86)
696 GCCArgs.push_back("-m32");
697
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000698 for (std::vector<std::string>::const_iterator
699 I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I)
700 GCCArgs.push_back(I->c_str());
701
Chris Lattnerffac2862006-06-06 22:30:59 +0000702 // Specify -x explicitly in case the extension is wonky
Chris Lattnerfd381322010-03-16 06:41:47 +0000703 if (fileType != ObjectFile) {
704 GCCArgs.push_back("-x");
705 if (fileType == CFile) {
706 GCCArgs.push_back("c");
707 GCCArgs.push_back("-fno-strict-aliasing");
708 } else {
709 GCCArgs.push_back("assembler");
Daniel Dunbar8575a602009-08-18 03:35:57 +0000710
Chris Lattnerfd381322010-03-16 06:41:47 +0000711 // For ARM architectures we don't want this flag. bugpoint isn't
712 // explicitly told what architecture it is working on, so we get
713 // it from gcc flags
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000714 if (TargetTriple.isOSDarwin() && !IsARMArchitecture(GCCArgs))
Chris Lattnerfd381322010-03-16 06:41:47 +0000715 GCCArgs.push_back("-force_cpusubtype_ALL");
716 }
Chris Lattnerffac2862006-06-06 22:30:59 +0000717 }
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000718
Chris Lattnerfd381322010-03-16 06:41:47 +0000719 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename.
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000720
Chris Lattner572e78c2006-06-09 21:31:53 +0000721 GCCArgs.push_back("-x");
722 GCCArgs.push_back("none");
Chris Lattnerffac2862006-06-06 22:30:59 +0000723 GCCArgs.push_back("-o");
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000724
725 SmallString<128> OutputBinary;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000726 std::error_code EC =
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000727 sys::fs::createUniqueFile(ProgramFile + "-%%%%%%%.gcc.exe", OutputBinary);
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000728 if (EC) {
729 errs() << "Error making unique filename: " << EC.message() << "\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000730 exit(1);
731 }
Chris Lattnerffac2862006-06-06 22:30:59 +0000732 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
733
734 // Add any arguments intended for GCC. We locate them here because this is
735 // most likely -L and -l options that need to come before other libraries but
736 // after the source. Other options won't be sensitive to placement on the
737 // command line, so this should be safe.
738 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
739 GCCArgs.push_back(ArgsForGCC[i].c_str());
740
741 GCCArgs.push_back("-lm"); // Hard-code the math library...
742 GCCArgs.push_back("-O2"); // Optimize the program a bit...
743#if defined (HAVE_LINK_R)
744 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
745#endif
Daniel Dunbar8575a602009-08-18 03:35:57 +0000746 if (TargetTriple.getArch() == Triple::sparc)
747 GCCArgs.push_back("-mcpu=v9");
Craig Toppere6cb63e2014-04-25 04:24:47 +0000748 GCCArgs.push_back(nullptr); // NULL terminator
Chris Lattnerffac2862006-06-06 22:30:59 +0000749
Dan Gohman607818a2009-07-15 17:29:42 +0000750 outs() << "<gcc>"; outs().flush();
Dan Gohmand8db3762009-07-15 16:35:29 +0000751 DEBUG(errs() << "\nAbout to run:\t";
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000752 for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i)
Dan Gohmand8db3762009-07-15 16:35:29 +0000753 errs() << " " << GCCArgs[i];
754 errs() << "\n";
Evan Chenga50e3912007-01-03 07:44:30 +0000755 );
Rafael Espindolae7a629f2013-06-13 16:22:26 +0000756 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "", "", "")) {
757 *Error = ProcessFailure(GCCPath, &GCCArgs[0]);
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000758 return -1;
Chris Lattnerffac2862006-06-06 22:30:59 +0000759 }
760
Rafael Espindola74cc8b22010-07-24 23:05:45 +0000761 std::vector<const char*> ProgramArgs;
Rafael Espindolaf8203a32010-07-24 23:02:11 +0000762
763 // Declared here so that the destructor only runs after
764 // ProgramArgs is used.
765 std::string Exec;
Chris Lattnerffac2862006-06-06 22:30:59 +0000766
Rafael Espindolae7a629f2013-06-13 16:22:26 +0000767 if (RemoteClientPath.empty())
Evan Cheng779b52e2007-05-03 18:36:15 +0000768 ProgramArgs.push_back(OutputBinary.c_str());
769 else {
Evan Cheng7aedcf12008-09-09 06:11:26 +0000770 ProgramArgs.push_back(RemoteClientPath.c_str());
771 ProgramArgs.push_back(RemoteHost.c_str());
Anton Korobeynikov0c3f8d52009-08-05 09:32:53 +0000772 if (!RemoteUser.empty()) {
773 ProgramArgs.push_back("-l");
774 ProgramArgs.push_back(RemoteUser.c_str());
775 }
David Goodwin73f4e3f2009-07-10 21:39:28 +0000776 if (!RemotePort.empty()) {
777 ProgramArgs.push_back("-p");
778 ProgramArgs.push_back(RemotePort.c_str());
779 }
Evan Cheng7aedcf12008-09-09 06:11:26 +0000780 if (!RemoteExtra.empty()) {
781 ProgramArgs.push_back(RemoteExtra.c_str());
782 }
Evan Cheng779b52e2007-05-03 18:36:15 +0000783
David Goodwin47ca4612009-07-20 17:15:03 +0000784 // Full path to the binary. We need to cd to the exec directory because
785 // there is a dylib there that the exec expects to find in the CWD
Evan Cheng779b52e2007-05-03 18:36:15 +0000786 char* env_pwd = getenv("PWD");
Rafael Espindolaf8203a32010-07-24 23:02:11 +0000787 Exec = "cd ";
Evan Cheng779b52e2007-05-03 18:36:15 +0000788 Exec += env_pwd;
David Goodwin47ca4612009-07-20 17:15:03 +0000789 Exec += "; ./";
Evan Cheng779b52e2007-05-03 18:36:15 +0000790 Exec += OutputBinary.c_str();
791 ProgramArgs.push_back(Exec.c_str());
792 }
793
Chris Lattnerffac2862006-06-06 22:30:59 +0000794 // Add optional parameters to the running program from Argv
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000795 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Chris Lattnerffac2862006-06-06 22:30:59 +0000796 ProgramArgs.push_back(Args[i].c_str());
Craig Toppere6cb63e2014-04-25 04:24:47 +0000797 ProgramArgs.push_back(nullptr); // NULL terminator
Chris Lattnerffac2862006-06-06 22:30:59 +0000798
799 // Now that we have a binary, run it!
Dan Gohman607818a2009-07-15 17:29:42 +0000800 outs() << "<program>"; outs().flush();
Dan Gohmand8db3762009-07-15 16:35:29 +0000801 DEBUG(errs() << "\nAbout to run:\t";
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000802 for (unsigned i = 0, e = ProgramArgs.size()-1; i != e; ++i)
Dan Gohmand8db3762009-07-15 16:35:29 +0000803 errs() << " " << ProgramArgs[i];
804 errs() << "\n";
Chris Lattnerffac2862006-06-06 22:30:59 +0000805 );
806
Michael J. Spencerc60223e2011-03-31 13:04:19 +0000807 FileRemover OutputBinaryRemover(OutputBinary.str(), !SaveTemps);
Evan Cheng779b52e2007-05-03 18:36:15 +0000808
Rafael Espindolae7a629f2013-06-13 16:22:26 +0000809 if (RemoteClientPath.empty()) {
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000810 DEBUG(errs() << "<run locally>");
Rafael Espindolab851d422013-06-13 15:47:11 +0000811 int ExitCode = RunProgramWithTimeout(OutputBinary.str(), &ProgramArgs[0],
812 InputFile, OutputFile, OutputFile,
813 Timeout, MemoryLimit, Error);
Andrew Trickd5d07642011-05-21 00:56:46 +0000814 // Treat a signal (usually SIGSEGV) or timeout as part of the program output
815 // so that crash-causing miscompilation is handled seamlessly.
816 if (ExitCode < -1) {
Andrew Trick55aeb552011-05-11 16:31:24 +0000817 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
818 outFile << *Error << '\n';
819 outFile.close();
820 Error->clear();
821 }
822 return ExitCode;
Viktor Kutuzov1518de12009-07-14 19:10:55 +0000823 } else {
Dan Gohman607818a2009-07-15 17:29:42 +0000824 outs() << "<run remotely>"; outs().flush();
Rafael Espindolae7a629f2013-06-13 16:22:26 +0000825 return RunProgramRemotelyWithTimeout(RemoteClientPath,
Rafael Espindolab851d422013-06-13 15:47:11 +0000826 &ProgramArgs[0], InputFile, OutputFile,
827 OutputFile, Timeout, MemoryLimit);
Viktor Kutuzov1518de12009-07-14 19:10:55 +0000828 }
Chris Lattnerffac2862006-06-06 22:30:59 +0000829}
830
831int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
Chris Lattnercc21fa72006-06-27 20:35:36 +0000832 std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000833 const std::vector<std::string> &ArgsForGCC,
834 std::string &Error) {
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000835 SmallString<128> UniqueFilename;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000836 std::error_code EC = sys::fs::createUniqueFile(
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000837 InputFile + "-%%%%%%%" + LTDL_SHLIB_EXT, UniqueFilename);
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000838 if (EC) {
839 errs() << "Error making unique filename: " << EC.message() << "\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000840 exit(1);
841 }
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000842 OutputFile = UniqueFilename.str();
Chris Lattnerffac2862006-06-06 22:30:59 +0000843
Chris Lattnercc21fa72006-06-27 20:35:36 +0000844 std::vector<const char*> GCCArgs;
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000845
Chris Lattnercc21fa72006-06-27 20:35:36 +0000846 GCCArgs.push_back(GCCPath.c_str());
Evan Cheng690b6352009-03-12 00:53:34 +0000847
Chris Lattnerfd381322010-03-16 06:41:47 +0000848 if (TargetTriple.getArch() == Triple::x86)
849 GCCArgs.push_back("-m32");
850
Evan Cheng690b6352009-03-12 00:53:34 +0000851 for (std::vector<std::string>::const_iterator
852 I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I)
853 GCCArgs.push_back(I->c_str());
854
Chris Lattnerffac2862006-06-06 22:30:59 +0000855 // Compile the C/asm file into a shared object
Chris Lattnerfd381322010-03-16 06:41:47 +0000856 if (fileType != ObjectFile) {
857 GCCArgs.push_back("-x");
858 GCCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
859 }
Chris Lattnercc21fa72006-06-27 20:35:36 +0000860 GCCArgs.push_back("-fno-strict-aliasing");
861 GCCArgs.push_back(InputFile.c_str()); // Specify the input filename.
Lauro Ramos Venanciof04823a2007-06-06 23:10:56 +0000862 GCCArgs.push_back("-x");
863 GCCArgs.push_back("none");
Daniel Dunbar8575a602009-08-18 03:35:57 +0000864 if (TargetTriple.getArch() == Triple::sparc)
865 GCCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000866 else if (TargetTriple.isOSDarwin()) {
Daniel Dunbar8575a602009-08-18 03:35:57 +0000867 // link all source files into a single module in data segment, rather than
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000868 // generating blocks. dynamic_lookup requires that you set
Daniel Dunbar8575a602009-08-18 03:35:57 +0000869 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for
870 // bugpoint to just pass that in the environment of GCC.
871 GCCArgs.push_back("-single_module");
872 GCCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC
873 GCCArgs.push_back("-undefined");
874 GCCArgs.push_back("dynamic_lookup");
875 } else
876 GCCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others
Chris Lattnerffac2862006-06-06 22:30:59 +0000877
Dan Gohman4c9fca92011-10-27 22:56:32 +0000878 if (TargetTriple.getArch() == Triple::x86_64)
Daniel Dunbar8575a602009-08-18 03:35:57 +0000879 GCCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC
880
881 if (TargetTriple.getArch() == Triple::sparc)
882 GCCArgs.push_back("-mcpu=v9");
883
Chris Lattnercc21fa72006-06-27 20:35:36 +0000884 GCCArgs.push_back("-o");
885 GCCArgs.push_back(OutputFile.c_str()); // Output to the right filename.
886 GCCArgs.push_back("-O2"); // Optimize the program a bit.
887
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000888
889
Chris Lattnercc21fa72006-06-27 20:35:36 +0000890 // Add any arguments intended for GCC. We locate them here because this is
891 // most likely -L and -l options that need to come before other libraries but
892 // after the source. Other options won't be sensitive to placement on the
893 // command line, so this should be safe.
894 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
895 GCCArgs.push_back(ArgsForGCC[i].c_str());
Craig Toppere6cb63e2014-04-25 04:24:47 +0000896 GCCArgs.push_back(nullptr); // NULL terminator
Chris Lattnercc21fa72006-06-27 20:35:36 +0000897
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000898
Chris Lattnerffac2862006-06-06 22:30:59 +0000899
Dan Gohman607818a2009-07-15 17:29:42 +0000900 outs() << "<gcc>"; outs().flush();
Dan Gohmand8db3762009-07-15 16:35:29 +0000901 DEBUG(errs() << "\nAbout to run:\t";
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000902 for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i)
Dan Gohmand8db3762009-07-15 16:35:29 +0000903 errs() << " " << GCCArgs[i];
904 errs() << "\n";
Evan Chenga50e3912007-01-03 07:44:30 +0000905 );
Rafael Espindolae7a629f2013-06-13 16:22:26 +0000906 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "", "", "")) {
907 Error = ProcessFailure(GCCPath, &GCCArgs[0]);
Chris Lattnerffac2862006-06-06 22:30:59 +0000908 return 1;
909 }
910 return 0;
911}
912
913/// create - Try to find the `gcc' executable
914///
Dan Gohman46ffffa2009-08-05 20:21:17 +0000915GCC *GCC::create(std::string &Message,
Kalle Raiskila6be58292010-05-10 07:38:37 +0000916 const std::string &GCCBinary,
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000917 const std::vector<std::string> *Args) {
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +0000918 std::string GCCPath = sys::FindProgramByName(GCCBinary);
919 if (GCCPath.empty()) {
Dan Gohman9e082462010-10-29 16:15:23 +0000920 Message = "Cannot find `"+ GCCBinary +"' in PATH!\n";
Craig Toppere6cb63e2014-04-25 04:24:47 +0000921 return nullptr;
Chris Lattnerffac2862006-06-06 22:30:59 +0000922 }
923
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +0000924 std::string RemoteClientPath;
Evan Cheng7aedcf12008-09-09 06:11:26 +0000925 if (!RemoteClient.empty())
Rafael Espindolacb2eca02013-06-12 20:58:35 +0000926 RemoteClientPath = sys::FindProgramByName(RemoteClient);
Evan Cheng779b52e2007-05-03 18:36:15 +0000927
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +0000928 Message = "Found gcc: " + GCCPath + "\n";
929 return new GCC(GCCPath, RemoteClientPath, Args);
Chris Lattnerffac2862006-06-06 22:30:59 +0000930}