blob: 2ccd649051289b65075f11fa1af97a6bbc9fe171 [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 };
Craig Toppere6cb63e2014-04-25 04:24:47 +000067 return sys::ExecuteAndWait(ProgramPath, Args, nullptr, Redirects,
Rafael Espindolab851d422013-06-13 15:47:11 +000068 NumSeconds, MemoryLimit, ErrMsg);
Chris Lattnerffac2862006-06-06 22:30:59 +000069}
70
Viktor Kutuzov85870252009-07-18 18:39:24 +000071/// RunProgramRemotelyWithTimeout - This function runs the given program
72/// remotely using the given remote client and the sys::Program::ExecuteAndWait.
73/// Returns the remote program exit code or reports a remote client error if it
74/// fails. Remote client is required to return 255 if it failed or program exit
75/// code otherwise.
Nick Lewycky6ba630b2010-04-12 05:08:25 +000076/// @see sys::Program::ExecuteAndWait
Rafael Espindolab851d422013-06-13 15:47:11 +000077static int RunProgramRemotelyWithTimeout(StringRef RemoteClientPath,
Viktor Kutuzov85870252009-07-18 18:39:24 +000078 const char **Args,
Rafael Espindolab851d422013-06-13 15:47:11 +000079 StringRef StdInFile,
80 StringRef StdOutFile,
81 StringRef StdErrFile,
Viktor Kutuzov85870252009-07-18 18:39:24 +000082 unsigned NumSeconds = 0,
83 unsigned MemoryLimit = 0) {
Rafael Espindola7c1023a2013-06-13 20:25:38 +000084 const StringRef *Redirects[3] = { &StdInFile, &StdOutFile, &StdErrFile };
Chris Lattnerffac2862006-06-06 22:30:59 +000085
Viktor Kutuzov85870252009-07-18 18:39:24 +000086 // Run the program remotely with the remote client
Craig Toppere6cb63e2014-04-25 04:24:47 +000087 int ReturnCode = sys::ExecuteAndWait(RemoteClientPath, Args, nullptr,
Rafael Espindola7c1023a2013-06-13 20:25:38 +000088 Redirects, NumSeconds, MemoryLimit);
Viktor Kutuzov85870252009-07-18 18:39:24 +000089
90 // Has the remote client fail?
91 if (255 == ReturnCode) {
92 std::ostringstream OS;
93 OS << "\nError running remote client:\n ";
94 for (const char **Arg = Args; *Arg; ++Arg)
95 OS << " " << *Arg;
96 OS << "\n";
97
98 // The error message is in the output file, let's print it out from there.
Rafael Espindolac0bbe362013-06-13 15:52:54 +000099 std::string StdOutFileName = StdOutFile.str();
100 std::ifstream ErrorFile(StdOutFileName.c_str());
Viktor Kutuzov85870252009-07-18 18:39:24 +0000101 if (ErrorFile) {
102 std::copy(std::istreambuf_iterator<char>(ErrorFile),
103 std::istreambuf_iterator<char>(),
104 std::ostreambuf_iterator<char>(OS));
105 ErrorFile.close();
106 }
107
John McCall2a784002012-05-02 05:39:10 +0000108 errs() << OS.str();
Viktor Kutuzov85870252009-07-18 18:39:24 +0000109 }
110
111 return ReturnCode;
112}
Chris Lattnerffac2862006-06-06 22:30:59 +0000113
Rafael Espindolab851d422013-06-13 15:47:11 +0000114static std::string ProcessFailure(StringRef ProgPath, const char** Args,
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000115 unsigned Timeout = 0,
116 unsigned MemoryLimit = 0) {
Chris Lattnerffac2862006-06-06 22:30:59 +0000117 std::ostringstream OS;
118 OS << "\nError running tool:\n ";
119 for (const char **Arg = Args; *Arg; ++Arg)
120 OS << " " << *Arg;
121 OS << "\n";
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000122
Chris Lattnerffac2862006-06-06 22:30:59 +0000123 // Rerun the compiler, capturing any error messages to print them.
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000124 SmallString<128> ErrorFilename;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000125 std::error_code EC = sys::fs::createTemporaryFile(
Lang Hames29593562014-10-07 21:47:23 +0000126 "bugpoint.program_error_messages", "", ErrorFilename);
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000127 if (EC) {
128 errs() << "Error making unique filename: " << EC.message() << "\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000129 exit(1);
130 }
NAKAMURA Takumif0e30f62014-07-13 13:28:18 +0000131
Rafael Espindolab851d422013-06-13 15:47:11 +0000132 RunProgramWithTimeout(ProgPath, Args, "", ErrorFilename.str(),
133 ErrorFilename.str(), Timeout, MemoryLimit);
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000134 // FIXME: check return code ?
Chris Lattnerffac2862006-06-06 22:30:59 +0000135
Davide Italianoab256212015-10-14 20:29:54 +0000136 // Print out the error messages generated by CC if possible...
Chris Lattnerffac2862006-06-06 22:30:59 +0000137 std::ifstream ErrorFile(ErrorFilename.c_str());
138 if (ErrorFile) {
139 std::copy(std::istreambuf_iterator<char>(ErrorFile),
140 std::istreambuf_iterator<char>(),
141 std::ostreambuf_iterator<char>(OS));
142 ErrorFile.close();
143 }
144
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000145 sys::fs::remove(ErrorFilename.c_str());
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000146 return OS.str();
Chris Lattnerffac2862006-06-06 22:30:59 +0000147}
148
149//===---------------------------------------------------------------------===//
150// LLI Implementation of AbstractIntepreter interface
151//
152namespace {
153 class LLI : public AbstractInterpreter {
154 std::string LLIPath; // The path to the LLI executable
155 std::vector<std::string> ToolArgs; // Args to pass to LLI
156 public:
157 LLI(const std::string &Path, const std::vector<std::string> *Args)
158 : LLIPath(Path) {
159 ToolArgs.clear ();
160 if (Args) { ToolArgs = *Args; }
161 }
162
Craig Toppere56917c2014-03-08 08:27:28 +0000163 int ExecuteProgram(const std::string &Bitcode,
164 const std::vector<std::string> &Args,
165 const std::string &InputFile,
166 const std::string &OutputFile,
167 std::string *Error,
Davide Italianoab256212015-10-14 20:29:54 +0000168 const std::vector<std::string> &CCArgs,
Craig Toppere56917c2014-03-08 08:27:28 +0000169 const std::vector<std::string> &SharedLibs =
170 std::vector<std::string>(),
171 unsigned Timeout = 0,
172 unsigned MemoryLimit = 0) override;
Chris Lattnerffac2862006-06-06 22:30:59 +0000173 };
174}
175
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000176int LLI::ExecuteProgram(const std::string &Bitcode,
Chris Lattnerffac2862006-06-06 22:30:59 +0000177 const std::vector<std::string> &Args,
178 const std::string &InputFile,
179 const std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000180 std::string *Error,
Davide Italianoab256212015-10-14 20:29:54 +0000181 const std::vector<std::string> &CCArgs,
Chris Lattnerffac2862006-06-06 22:30:59 +0000182 const std::vector<std::string> &SharedLibs,
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000183 unsigned Timeout,
184 unsigned MemoryLimit) {
Chris Lattnerffac2862006-06-06 22:30:59 +0000185 std::vector<const char*> LLIArgs;
186 LLIArgs.push_back(LLIPath.c_str());
187 LLIArgs.push_back("-force-interpreter=true");
188
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000189 for (std::vector<std::string>::const_iterator i = SharedLibs.begin(),
190 e = SharedLibs.end(); i != e; ++i) {
Duncan Sands3246fe02009-11-17 10:20:22 +0000191 LLIArgs.push_back("-load");
192 LLIArgs.push_back((*i).c_str());
193 }
194
Chris Lattnerffac2862006-06-06 22:30:59 +0000195 // Add any extra LLI args.
196 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
197 LLIArgs.push_back(ToolArgs[i].c_str());
198
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000199 LLIArgs.push_back(Bitcode.c_str());
Chris Lattnerffac2862006-06-06 22:30:59 +0000200 // Add optional parameters to the running program from Argv
201 for (unsigned i=0, e = Args.size(); i != e; ++i)
202 LLIArgs.push_back(Args[i].c_str());
Craig Toppere6cb63e2014-04-25 04:24:47 +0000203 LLIArgs.push_back(nullptr);
Chris Lattnerffac2862006-06-06 22:30:59 +0000204
Dan Gohman607818a2009-07-15 17:29:42 +0000205 outs() << "<lli>"; outs().flush();
Dan Gohmand8db3762009-07-15 16:35:29 +0000206 DEBUG(errs() << "\nAbout to run:\t";
Chris Lattnerffac2862006-06-06 22:30:59 +0000207 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
Dan Gohmand8db3762009-07-15 16:35:29 +0000208 errs() << " " << LLIArgs[i];
209 errs() << "\n";
Chris Lattnerffac2862006-06-06 22:30:59 +0000210 );
Rafael Espindolab851d422013-06-13 15:47:11 +0000211 return RunProgramWithTimeout(LLIPath, &LLIArgs[0],
212 InputFile, OutputFile, OutputFile,
Dan Gohman7b0c25f2010-11-09 01:13:31 +0000213 Timeout, MemoryLimit, Error);
Chris Lattnerffac2862006-06-06 22:30:59 +0000214}
215
David Blaikiea379b1812011-12-20 02:50:00 +0000216void AbstractInterpreter::anchor() { }
217
Rafael Espindola242fcb82013-06-25 14:42:30 +0000218#if defined(LLVM_ON_UNIX)
219const char EXESuffix[] = "";
220#elif defined (LLVM_ON_WIN32)
221const char EXESuffix[] = "exe";
222#endif
223
Rafael Espindolab7863042013-06-14 15:12:13 +0000224/// Prepend the path to the program being executed
225/// to \p ExeName, given the value of argv[0] and the address of main()
226/// itself. This allows us to find another LLVM tool if it is built in the same
227/// directory. An empty string is returned on error; note that this function
228/// just mainpulates the path and doesn't check for executability.
229/// @brief Find a named executable.
Rafael Espindolac7247652013-06-18 16:47:55 +0000230static std::string PrependMainExecutablePath(const std::string &ExeName,
231 const char *Argv0,
232 void *MainAddr) {
Rafael Espindolab7863042013-06-14 15:12:13 +0000233 // Check the directory that the calling program is in. We can do
234 // this if ProgramPath contains at least one / character, indicating that it
235 // is a relative path to the executable itself.
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000236 std::string Main = sys::fs::getMainExecutable(Argv0, MainAddr);
237 StringRef Result = sys::path::parent_path(Main);
Rafael Espindolab7863042013-06-14 15:12:13 +0000238
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000239 if (!Result.empty()) {
240 SmallString<128> Storage = Result;
241 sys::path::append(Storage, ExeName);
Rafael Espindola242fcb82013-06-25 14:42:30 +0000242 sys::path::replace_extension(Storage, EXESuffix);
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000243 return Storage.str();
Rafael Espindolab7863042013-06-14 15:12:13 +0000244 }
245
Rafael Espindolac7247652013-06-18 16:47:55 +0000246 return Result.str();
Rafael Espindolab7863042013-06-14 15:12:13 +0000247}
248
Chris Lattnerffac2862006-06-06 22:30:59 +0000249// LLI create method - Try to find the LLI executable
Dan Gohman46ffffa2009-08-05 20:21:17 +0000250AbstractInterpreter *AbstractInterpreter::createLLI(const char *Argv0,
Chris Lattnerffac2862006-06-06 22:30:59 +0000251 std::string &Message,
252 const std::vector<std::string> *ToolArgs) {
Dan Gohman46ffffa2009-08-05 20:21:17 +0000253 std::string LLIPath =
Rafael Espindolac7247652013-06-18 16:47:55 +0000254 PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t) & createLLI);
Chris Lattnerffac2862006-06-06 22:30:59 +0000255 if (!LLIPath.empty()) {
256 Message = "Found lli: " + LLIPath + "\n";
257 return new LLI(LLIPath, ToolArgs);
258 }
259
Dan Gohman9e082462010-10-29 16:15:23 +0000260 Message = "Cannot find `lli' in executable directory!\n";
Craig Toppere6cb63e2014-04-25 04:24:47 +0000261 return nullptr;
Chris Lattnerffac2862006-06-06 22:30:59 +0000262}
263
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000264//===---------------------------------------------------------------------===//
Andrew Trick8665d592011-02-08 18:20:48 +0000265// Custom compiler command implementation of AbstractIntepreter interface
266//
267// Allows using a custom command for compiling the bitcode, thus allows, for
268// example, to compile a bitcode fragment without linking or executing, then
269// using a custom wrapper script to check for compiler errors.
270namespace {
271 class CustomCompiler : public AbstractInterpreter {
272 std::string CompilerCommand;
273 std::vector<std::string> CompilerArgs;
274 public:
275 CustomCompiler(
276 const std::string &CompilerCmd, std::vector<std::string> CompArgs) :
277 CompilerCommand(CompilerCmd), CompilerArgs(CompArgs) {}
278
Craig Toppere56917c2014-03-08 08:27:28 +0000279 void compileProgram(const std::string &Bitcode,
280 std::string *Error,
281 unsigned Timeout = 0,
282 unsigned MemoryLimit = 0) override;
Andrew Trick8665d592011-02-08 18:20:48 +0000283
Craig Toppere56917c2014-03-08 08:27:28 +0000284 int ExecuteProgram(const std::string &Bitcode,
285 const std::vector<std::string> &Args,
286 const std::string &InputFile,
287 const std::string &OutputFile,
288 std::string *Error,
Davide Italianoab256212015-10-14 20:29:54 +0000289 const std::vector<std::string> &CCArgs =
Craig Toppere56917c2014-03-08 08:27:28 +0000290 std::vector<std::string>(),
291 const std::vector<std::string> &SharedLibs =
292 std::vector<std::string>(),
293 unsigned Timeout = 0,
294 unsigned MemoryLimit = 0) override {
Andrew Trick8665d592011-02-08 18:20:48 +0000295 *Error = "Execution not supported with -compile-custom";
296 return -1;
297 }
298 };
299}
300
301void CustomCompiler::compileProgram(const std::string &Bitcode,
302 std::string *Error,
303 unsigned Timeout,
304 unsigned MemoryLimit) {
305
306 std::vector<const char*> ProgramArgs;
307 ProgramArgs.push_back(CompilerCommand.c_str());
308
309 for (std::size_t i = 0; i < CompilerArgs.size(); ++i)
310 ProgramArgs.push_back(CompilerArgs.at(i).c_str());
311 ProgramArgs.push_back(Bitcode.c_str());
Craig Toppere6cb63e2014-04-25 04:24:47 +0000312 ProgramArgs.push_back(nullptr);
Andrew Trick8665d592011-02-08 18:20:48 +0000313
314 // Add optional parameters to the running program from Argv
315 for (unsigned i = 0, e = CompilerArgs.size(); i != e; ++i)
316 ProgramArgs.push_back(CompilerArgs[i].c_str());
317
Rafael Espindolab851d422013-06-13 15:47:11 +0000318 if (RunProgramWithTimeout(CompilerCommand, &ProgramArgs[0],
319 "", "", "",
Andrew Trick8665d592011-02-08 18:20:48 +0000320 Timeout, MemoryLimit, Error))
Rafael Espindolab851d422013-06-13 15:47:11 +0000321 *Error = ProcessFailure(CompilerCommand, &ProgramArgs[0],
Andrew Trick8665d592011-02-08 18:20:48 +0000322 Timeout, MemoryLimit);
323}
324
325//===---------------------------------------------------------------------===//
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000326// Custom execution command implementation of AbstractIntepreter interface
327//
328// Allows using a custom command for executing the bitcode, thus allows,
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000329// for example, to invoke a cross compiler for code generation followed by
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000330// a simulator that executes the generated binary.
331namespace {
332 class CustomExecutor : public AbstractInterpreter {
333 std::string ExecutionCommand;
334 std::vector<std::string> ExecutorArgs;
335 public:
336 CustomExecutor(
337 const std::string &ExecutionCmd, std::vector<std::string> ExecArgs) :
338 ExecutionCommand(ExecutionCmd), ExecutorArgs(ExecArgs) {}
339
Craig Toppere56917c2014-03-08 08:27:28 +0000340 int ExecuteProgram(const std::string &Bitcode,
341 const std::vector<std::string> &Args,
342 const std::string &InputFile,
343 const std::string &OutputFile,
344 std::string *Error,
Davide Italianoab256212015-10-14 20:29:54 +0000345 const std::vector<std::string> &CCArgs,
Craig Toppere56917c2014-03-08 08:27:28 +0000346 const std::vector<std::string> &SharedLibs =
347 std::vector<std::string>(),
348 unsigned Timeout = 0,
349 unsigned MemoryLimit = 0) override;
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000350 };
351}
352
353int CustomExecutor::ExecuteProgram(const std::string &Bitcode,
354 const std::vector<std::string> &Args,
355 const std::string &InputFile,
356 const std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000357 std::string *Error,
Davide Italianoab256212015-10-14 20:29:54 +0000358 const std::vector<std::string> &CCArgs,
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000359 const std::vector<std::string> &SharedLibs,
360 unsigned Timeout,
361 unsigned MemoryLimit) {
362
363 std::vector<const char*> ProgramArgs;
364 ProgramArgs.push_back(ExecutionCommand.c_str());
365
366 for (std::size_t i = 0; i < ExecutorArgs.size(); ++i)
367 ProgramArgs.push_back(ExecutorArgs.at(i).c_str());
368 ProgramArgs.push_back(Bitcode.c_str());
Craig Toppere6cb63e2014-04-25 04:24:47 +0000369 ProgramArgs.push_back(nullptr);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000370
371 // Add optional parameters to the running program from Argv
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000372 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000373 ProgramArgs.push_back(Args[i].c_str());
374
375 return RunProgramWithTimeout(
Rafael Espindolab851d422013-06-13 15:47:11 +0000376 ExecutionCommand,
377 &ProgramArgs[0], InputFile, OutputFile,
378 OutputFile, Timeout, MemoryLimit, Error);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000379}
380
Andrew Trick8665d592011-02-08 18:20:48 +0000381// Tokenize the CommandLine to the command and the args to allow
382// defining a full command line as the command instead of just the
383// executed program. We cannot just pass the whole string after the command
384// as a single argument because then program sees only a single
385// command line argument (with spaces in it: "foo bar" instead
386// of "foo" and "bar").
387//
388// code borrowed from:
389// http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
390static void lexCommand(std::string &Message, const std::string &CommandLine,
Adam Nemet5f9eb0a2014-03-08 07:48:19 +0000391 std::string &CmdPath, std::vector<std::string> &Args) {
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000392
393 std::string Command = "";
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000394 std::string delimiters = " ";
395
Andrew Trick8665d592011-02-08 18:20:48 +0000396 std::string::size_type lastPos = CommandLine.find_first_not_of(delimiters, 0);
397 std::string::size_type pos = CommandLine.find_first_of(delimiters, lastPos);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000398
399 while (std::string::npos != pos || std::string::npos != lastPos) {
Andrew Trick8665d592011-02-08 18:20:48 +0000400 std::string token = CommandLine.substr(lastPos, pos - lastPos);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000401 if (Command == "")
402 Command = token;
403 else
404 Args.push_back(token);
405 // Skip delimiters. Note the "not_of"
Andrew Trick8665d592011-02-08 18:20:48 +0000406 lastPos = CommandLine.find_first_not_of(delimiters, pos);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000407 // Find next "non-delimiter"
Andrew Trick8665d592011-02-08 18:20:48 +0000408 pos = CommandLine.find_first_of(delimiters, lastPos);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000409 }
410
Michael J. Spencerf9074b52014-11-04 01:29:59 +0000411 auto Path = sys::findProgramByName(Command);
412 if (!Path) {
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000413 Message =
414 std::string("Cannot find '") + Command +
Michael J. Spencerf9074b52014-11-04 01:29:59 +0000415 "' in PATH: " + Path.getError().message() + "\n";
Andrew Trick8665d592011-02-08 18:20:48 +0000416 return;
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000417 }
Michael J. Spencerf9074b52014-11-04 01:29:59 +0000418 CmdPath = *Path;
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000419
420 Message = "Found command in: " + CmdPath + "\n";
Andrew Trick8665d592011-02-08 18:20:48 +0000421}
422
423// Custom execution environment create method, takes the execution command
424// as arguments
425AbstractInterpreter *AbstractInterpreter::createCustomCompiler(
426 std::string &Message,
427 const std::string &CompileCommandLine) {
428
429 std::string CmdPath;
430 std::vector<std::string> Args;
431 lexCommand(Message, CompileCommandLine, CmdPath, Args);
432 if (CmdPath.empty())
Craig Toppere6cb63e2014-04-25 04:24:47 +0000433 return nullptr;
Andrew Trick8665d592011-02-08 18:20:48 +0000434
435 return new CustomCompiler(CmdPath, Args);
436}
437
438// Custom execution environment create method, takes the execution command
439// as arguments
440AbstractInterpreter *AbstractInterpreter::createCustomExecutor(
441 std::string &Message,
442 const std::string &ExecCommandLine) {
443
444
445 std::string CmdPath;
446 std::vector<std::string> Args;
447 lexCommand(Message, ExecCommandLine, CmdPath, Args);
448 if (CmdPath.empty())
Craig Toppere6cb63e2014-04-25 04:24:47 +0000449 return nullptr;
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000450
451 return new CustomExecutor(CmdPath, Args);
452}
453
Chris Lattnerffac2862006-06-06 22:30:59 +0000454//===----------------------------------------------------------------------===//
455// LLC Implementation of AbstractIntepreter interface
456//
Davide Italianoab256212015-10-14 20:29:54 +0000457CC::FileType LLC::OutputCode(const std::string &Bitcode,
Rafael Espindola34889ca2013-06-17 19:21:38 +0000458 std::string &OutputAsmFile, std::string &Error,
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000459 unsigned Timeout, unsigned MemoryLimit) {
Chris Lattnerfd381322010-03-16 06:41:47 +0000460 const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s");
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000461
462 SmallString<128> UniqueFile;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000463 std::error_code EC =
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000464 sys::fs::createUniqueFile(Bitcode + "-%%%%%%%" + Suffix, UniqueFile);
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000465 if (EC) {
466 errs() << "Error making unique filename: " << EC.message() << "\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000467 exit(1);
468 }
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000469 OutputAsmFile = UniqueFile.str();
Chris Lattnerffac2862006-06-06 22:30:59 +0000470 std::vector<const char *> LLCArgs;
Chris Lattnerfd381322010-03-16 06:41:47 +0000471 LLCArgs.push_back(LLCPath.c_str());
Chris Lattnerffac2862006-06-06 22:30:59 +0000472
473 // Add any extra LLC args.
474 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
475 LLCArgs.push_back(ToolArgs[i].c_str());
476
Chris Lattnerfd381322010-03-16 06:41:47 +0000477 LLCArgs.push_back("-o");
478 LLCArgs.push_back(OutputAsmFile.c_str()); // Output to the Asm file
479 LLCArgs.push_back(Bitcode.c_str()); // This is the input bitcode
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000480
Chris Lattnerfd381322010-03-16 06:41:47 +0000481 if (UseIntegratedAssembler)
482 LLCArgs.push_back("-filetype=obj");
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000483
Craig Toppere6cb63e2014-04-25 04:24:47 +0000484 LLCArgs.push_back (nullptr);
Chris Lattnerffac2862006-06-06 22:30:59 +0000485
Chris Lattnerfd381322010-03-16 06:41:47 +0000486 outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>");
487 outs().flush();
Dan Gohmand8db3762009-07-15 16:35:29 +0000488 DEBUG(errs() << "\nAbout to run:\t";
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000489 for (unsigned i = 0, e = LLCArgs.size()-1; i != e; ++i)
Dan Gohmand8db3762009-07-15 16:35:29 +0000490 errs() << " " << LLCArgs[i];
491 errs() << "\n";
Chris Lattnerffac2862006-06-06 22:30:59 +0000492 );
Rafael Espindolab851d422013-06-13 15:47:11 +0000493 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0],
494 "", "", "",
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000495 Timeout, MemoryLimit))
Rafael Espindolab851d422013-06-13 15:47:11 +0000496 Error = ProcessFailure(LLCPath, &LLCArgs[0],
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000497 Timeout, MemoryLimit);
Davide Italianoab256212015-10-14 20:29:54 +0000498 return UseIntegratedAssembler ? CC::ObjectFile : CC::AsmFile;
Chris Lattnerffac2862006-06-06 22:30:59 +0000499}
500
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000501void LLC::compileProgram(const std::string &Bitcode, std::string *Error,
502 unsigned Timeout, unsigned MemoryLimit) {
Rafael Espindola34889ca2013-06-17 19:21:38 +0000503 std::string OutputAsmFile;
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000504 OutputCode(Bitcode, OutputAsmFile, *Error, Timeout, MemoryLimit);
Rafael Espindola34889ca2013-06-17 19:21:38 +0000505 sys::fs::remove(OutputAsmFile);
Chris Lattnerffac2862006-06-06 22:30:59 +0000506}
507
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000508int LLC::ExecuteProgram(const std::string &Bitcode,
Chris Lattnerffac2862006-06-06 22:30:59 +0000509 const std::vector<std::string> &Args,
510 const std::string &InputFile,
511 const std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000512 std::string *Error,
Davide Italianoab256212015-10-14 20:29:54 +0000513 const std::vector<std::string> &ArgsForCC,
Chris Lattnerffac2862006-06-06 22:30:59 +0000514 const std::vector<std::string> &SharedLibs,
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000515 unsigned Timeout,
516 unsigned MemoryLimit) {
Chris Lattnerffac2862006-06-06 22:30:59 +0000517
Rafael Espindola34889ca2013-06-17 19:21:38 +0000518 std::string OutputAsmFile;
Davide Italianoab256212015-10-14 20:29:54 +0000519 CC::FileType FileKind = OutputCode(Bitcode, OutputAsmFile, *Error, Timeout,
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000520 MemoryLimit);
Rafael Espindola34889ca2013-06-17 19:21:38 +0000521 FileRemover OutFileRemover(OutputAsmFile, !SaveTemps);
Chris Lattnerffac2862006-06-06 22:30:59 +0000522
Davide Italianoab256212015-10-14 20:29:54 +0000523 std::vector<std::string> CCArgs(ArgsForCC);
524 CCArgs.insert(CCArgs.end(), SharedLibs.begin(), SharedLibs.end());
Chris Lattnerffac2862006-06-06 22:30:59 +0000525
Davide Italianoab256212015-10-14 20:29:54 +0000526 // Assuming LLC worked, compile the result with CC and run it.
527 return cc->ExecuteProgram(OutputAsmFile, Args, FileKind,
528 InputFile, OutputFile, Error, CCArgs,
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000529 Timeout, MemoryLimit);
Chris Lattnerffac2862006-06-06 22:30:59 +0000530}
531
532/// createLLC - Try to find the LLC executable
533///
Dan Gohman46ffffa2009-08-05 20:21:17 +0000534LLC *AbstractInterpreter::createLLC(const char *Argv0,
Chris Lattnerffac2862006-06-06 22:30:59 +0000535 std::string &Message,
Davide Italianoab256212015-10-14 20:29:54 +0000536 const std::string &CCBinary,
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000537 const std::vector<std::string> *Args,
Davide Italianoab256212015-10-14 20:29:54 +0000538 const std::vector<std::string> *CCArgs,
Chris Lattnerfd381322010-03-16 06:41:47 +0000539 bool UseIntegratedAssembler) {
Dan Gohman46ffffa2009-08-05 20:21:17 +0000540 std::string LLCPath =
Rafael Espindolac7247652013-06-18 16:47:55 +0000541 PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t) & createLLC);
Chris Lattnerffac2862006-06-06 22:30:59 +0000542 if (LLCPath.empty()) {
Dan Gohman9e082462010-10-29 16:15:23 +0000543 Message = "Cannot find `llc' in executable directory!\n";
Craig Toppere6cb63e2014-04-25 04:24:47 +0000544 return nullptr;
Chris Lattnerffac2862006-06-06 22:30:59 +0000545 }
546
Davide Italianoab256212015-10-14 20:29:54 +0000547 CC *cc = CC::create(Message, CCBinary, CCArgs);
548 if (!cc) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000549 errs() << Message << "\n";
Chris Lattnerffac2862006-06-06 22:30:59 +0000550 exit(1);
551 }
Saleem Abdulrasoolab4d7bc2013-01-24 16:49:12 +0000552 Message = "Found llc: " + LLCPath + "\n";
Davide Italianoab256212015-10-14 20:29:54 +0000553 return new LLC(LLCPath, cc, Args, UseIntegratedAssembler);
Chris Lattnerffac2862006-06-06 22:30:59 +0000554}
555
556//===---------------------------------------------------------------------===//
557// JIT Implementation of AbstractIntepreter interface
558//
559namespace {
560 class JIT : public AbstractInterpreter {
561 std::string LLIPath; // The path to the LLI executable
562 std::vector<std::string> ToolArgs; // Args to pass to LLI
563 public:
564 JIT(const std::string &Path, const std::vector<std::string> *Args)
565 : LLIPath(Path) {
566 ToolArgs.clear ();
567 if (Args) { ToolArgs = *Args; }
568 }
569
Craig Toppere56917c2014-03-08 08:27:28 +0000570 int ExecuteProgram(const std::string &Bitcode,
571 const std::vector<std::string> &Args,
572 const std::string &InputFile,
573 const std::string &OutputFile,
574 std::string *Error,
Davide Italianoab256212015-10-14 20:29:54 +0000575 const std::vector<std::string> &CCArgs =
Craig Toppere56917c2014-03-08 08:27:28 +0000576 std::vector<std::string>(),
577 const std::vector<std::string> &SharedLibs =
578 std::vector<std::string>(),
579 unsigned Timeout = 0,
580 unsigned MemoryLimit = 0) override;
Chris Lattnerffac2862006-06-06 22:30:59 +0000581 };
582}
583
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000584int JIT::ExecuteProgram(const std::string &Bitcode,
Chris Lattnerffac2862006-06-06 22:30:59 +0000585 const std::vector<std::string> &Args,
586 const std::string &InputFile,
587 const std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000588 std::string *Error,
Davide Italianoab256212015-10-14 20:29:54 +0000589 const std::vector<std::string> &CCArgs,
Chris Lattnerffac2862006-06-06 22:30:59 +0000590 const std::vector<std::string> &SharedLibs,
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000591 unsigned Timeout,
592 unsigned MemoryLimit) {
Chris Lattnerffac2862006-06-06 22:30:59 +0000593 // Construct a vector of parameters, incorporating those from the command-line
594 std::vector<const char*> JITArgs;
595 JITArgs.push_back(LLIPath.c_str());
596 JITArgs.push_back("-force-interpreter=false");
597
598 // Add any extra LLI args.
599 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
600 JITArgs.push_back(ToolArgs[i].c_str());
601
602 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
603 JITArgs.push_back("-load");
604 JITArgs.push_back(SharedLibs[i].c_str());
605 }
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000606 JITArgs.push_back(Bitcode.c_str());
Chris Lattnerffac2862006-06-06 22:30:59 +0000607 // Add optional parameters to the running program from Argv
608 for (unsigned i=0, e = Args.size(); i != e; ++i)
609 JITArgs.push_back(Args[i].c_str());
Craig Toppere6cb63e2014-04-25 04:24:47 +0000610 JITArgs.push_back(nullptr);
Chris Lattnerffac2862006-06-06 22:30:59 +0000611
Dan Gohman607818a2009-07-15 17:29:42 +0000612 outs() << "<jit>"; outs().flush();
Dan Gohmand8db3762009-07-15 16:35:29 +0000613 DEBUG(errs() << "\nAbout to run:\t";
Chris Lattnerffac2862006-06-06 22:30:59 +0000614 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
Dan Gohmand8db3762009-07-15 16:35:29 +0000615 errs() << " " << JITArgs[i];
616 errs() << "\n";
Chris Lattnerffac2862006-06-06 22:30:59 +0000617 );
Dan Gohmand8db3762009-07-15 16:35:29 +0000618 DEBUG(errs() << "\nSending output to " << OutputFile << "\n");
Rafael Espindolab851d422013-06-13 15:47:11 +0000619 return RunProgramWithTimeout(LLIPath, &JITArgs[0],
620 InputFile, OutputFile, OutputFile,
Dan Gohman7b0c25f2010-11-09 01:13:31 +0000621 Timeout, MemoryLimit, Error);
Chris Lattnerffac2862006-06-06 22:30:59 +0000622}
623
624/// createJIT - Try to find the LLI executable
625///
Dan Gohman46ffffa2009-08-05 20:21:17 +0000626AbstractInterpreter *AbstractInterpreter::createJIT(const char *Argv0,
Chris Lattnerffac2862006-06-06 22:30:59 +0000627 std::string &Message, const std::vector<std::string> *Args) {
Dan Gohman46ffffa2009-08-05 20:21:17 +0000628 std::string LLIPath =
Rafael Espindolac7247652013-06-18 16:47:55 +0000629 PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t) & createJIT);
Chris Lattnerffac2862006-06-06 22:30:59 +0000630 if (!LLIPath.empty()) {
631 Message = "Found lli: " + LLIPath + "\n";
632 return new JIT(LLIPath, Args);
633 }
634
Dan Gohman9e082462010-10-29 16:15:23 +0000635 Message = "Cannot find `lli' in executable directory!\n";
Craig Toppere6cb63e2014-04-25 04:24:47 +0000636 return nullptr;
Chris Lattnerffac2862006-06-06 22:30:59 +0000637}
638
Chris Lattnerffac2862006-06-06 22:30:59 +0000639//===---------------------------------------------------------------------===//
Davide Italianoab256212015-10-14 20:29:54 +0000640// CC abstraction
Chris Lattnerffac2862006-06-06 22:30:59 +0000641//
David Goodwin73f4e3f2009-07-10 21:39:28 +0000642
Jakob Stoklund Olesen12222492010-07-29 00:52:16 +0000643static bool IsARMArchitecture(std::vector<const char*> Args) {
644 for (std::vector<const char*>::const_iterator
David Goodwin73f4e3f2009-07-10 21:39:28 +0000645 I = Args.begin(), E = Args.end(); I != E; ++I) {
Jakob Stoklund Olesen3e0ddc02010-05-13 17:58:15 +0000646 if (StringRef(*I).equals_lower("-arch")) {
David Goodwin73f4e3f2009-07-10 21:39:28 +0000647 ++I;
Jakub Staszakcfcfee02013-11-04 19:22:50 +0000648 if (I != E && StringRef(*I).startswith_lower("arm"))
David Goodwin73f4e3f2009-07-10 21:39:28 +0000649 return true;
David Goodwin73f4e3f2009-07-10 21:39:28 +0000650 }
651 }
652
653 return false;
654}
655
Davide Italianoab256212015-10-14 20:29:54 +0000656int CC::ExecuteProgram(const std::string &ProgramFile,
Chris Lattnerffac2862006-06-06 22:30:59 +0000657 const std::vector<std::string> &Args,
658 FileType fileType,
659 const std::string &InputFile,
660 const std::string &OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000661 std::string *Error,
Davide Italianoab256212015-10-14 20:29:54 +0000662 const std::vector<std::string> &ArgsForCC,
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000663 unsigned Timeout,
664 unsigned MemoryLimit) {
Davide Italianoab256212015-10-14 20:29:54 +0000665 std::vector<const char*> CCArgs;
Chris Lattnerffac2862006-06-06 22:30:59 +0000666
Davide Italianoab256212015-10-14 20:29:54 +0000667 CCArgs.push_back(CCPath.c_str());
Chris Lattnerffac2862006-06-06 22:30:59 +0000668
Chris Lattnerfd381322010-03-16 06:41:47 +0000669 if (TargetTriple.getArch() == Triple::x86)
Davide Italianoab256212015-10-14 20:29:54 +0000670 CCArgs.push_back("-m32");
Chris Lattnerfd381322010-03-16 06:41:47 +0000671
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000672 for (std::vector<std::string>::const_iterator
Davide Italianoab256212015-10-14 20:29:54 +0000673 I = ccArgs.begin(), E = ccArgs.end(); I != E; ++I)
674 CCArgs.push_back(I->c_str());
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000675
Chris Lattnerffac2862006-06-06 22:30:59 +0000676 // Specify -x explicitly in case the extension is wonky
Chris Lattnerfd381322010-03-16 06:41:47 +0000677 if (fileType != ObjectFile) {
Davide Italianoab256212015-10-14 20:29:54 +0000678 CCArgs.push_back("-x");
Chris Lattnerfd381322010-03-16 06:41:47 +0000679 if (fileType == CFile) {
Davide Italianoab256212015-10-14 20:29:54 +0000680 CCArgs.push_back("c");
681 CCArgs.push_back("-fno-strict-aliasing");
Chris Lattnerfd381322010-03-16 06:41:47 +0000682 } else {
Davide Italianoab256212015-10-14 20:29:54 +0000683 CCArgs.push_back("assembler");
Daniel Dunbar8575a602009-08-18 03:35:57 +0000684
Chris Lattnerfd381322010-03-16 06:41:47 +0000685 // For ARM architectures we don't want this flag. bugpoint isn't
686 // explicitly told what architecture it is working on, so we get
Davide Italianoab256212015-10-14 20:29:54 +0000687 // it from cc flags
688 if (TargetTriple.isOSDarwin() && !IsARMArchitecture(CCArgs))
689 CCArgs.push_back("-force_cpusubtype_ALL");
Chris Lattnerfd381322010-03-16 06:41:47 +0000690 }
Chris Lattnerffac2862006-06-06 22:30:59 +0000691 }
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000692
Davide Italianoab256212015-10-14 20:29:54 +0000693 CCArgs.push_back(ProgramFile.c_str()); // Specify the input filename.
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000694
Davide Italianoab256212015-10-14 20:29:54 +0000695 CCArgs.push_back("-x");
696 CCArgs.push_back("none");
697 CCArgs.push_back("-o");
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000698
699 SmallString<128> OutputBinary;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000700 std::error_code EC =
Davide Italianoab256212015-10-14 20:29:54 +0000701 sys::fs::createUniqueFile(ProgramFile + "-%%%%%%%.cc.exe", OutputBinary);
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000702 if (EC) {
703 errs() << "Error making unique filename: " << EC.message() << "\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000704 exit(1);
705 }
Davide Italianoab256212015-10-14 20:29:54 +0000706 CCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
Chris Lattnerffac2862006-06-06 22:30:59 +0000707
Davide Italianoab256212015-10-14 20:29:54 +0000708 // Add any arguments intended for CC. We locate them here because this is
Chris Lattnerffac2862006-06-06 22:30:59 +0000709 // most likely -L and -l options that need to come before other libraries but
710 // after the source. Other options won't be sensitive to placement on the
711 // command line, so this should be safe.
Davide Italianoab256212015-10-14 20:29:54 +0000712 for (unsigned i = 0, e = ArgsForCC.size(); i != e; ++i)
713 CCArgs.push_back(ArgsForCC[i].c_str());
Chris Lattnerffac2862006-06-06 22:30:59 +0000714
Davide Italianoab256212015-10-14 20:29:54 +0000715 CCArgs.push_back("-lm"); // Hard-code the math library...
716 CCArgs.push_back("-O2"); // Optimize the program a bit...
Chris Lattnerffac2862006-06-06 22:30:59 +0000717#if defined (HAVE_LINK_R)
Davide Italianoab256212015-10-14 20:29:54 +0000718 CCArgs.push_back("-Wl,-R."); // Search this dir for .so files
Chris Lattnerffac2862006-06-06 22:30:59 +0000719#endif
Daniel Dunbar8575a602009-08-18 03:35:57 +0000720 if (TargetTriple.getArch() == Triple::sparc)
Davide Italianoab256212015-10-14 20:29:54 +0000721 CCArgs.push_back("-mcpu=v9");
722 CCArgs.push_back(nullptr); // NULL terminator
Chris Lattnerffac2862006-06-06 22:30:59 +0000723
Davide Italianoab256212015-10-14 20:29:54 +0000724 outs() << "<CC>"; outs().flush();
Dan Gohmand8db3762009-07-15 16:35:29 +0000725 DEBUG(errs() << "\nAbout to run:\t";
Davide Italianoab256212015-10-14 20:29:54 +0000726 for (unsigned i = 0, e = CCArgs.size()-1; i != e; ++i)
727 errs() << " " << CCArgs[i];
Dan Gohmand8db3762009-07-15 16:35:29 +0000728 errs() << "\n";
Evan Chenga50e3912007-01-03 07:44:30 +0000729 );
Davide Italianoab256212015-10-14 20:29:54 +0000730 if (RunProgramWithTimeout(CCPath, &CCArgs[0], "", "", "")) {
731 *Error = ProcessFailure(CCPath, &CCArgs[0]);
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000732 return -1;
Chris Lattnerffac2862006-06-06 22:30:59 +0000733 }
734
Rafael Espindola74cc8b22010-07-24 23:05:45 +0000735 std::vector<const char*> ProgramArgs;
Rafael Espindolaf8203a32010-07-24 23:02:11 +0000736
737 // Declared here so that the destructor only runs after
738 // ProgramArgs is used.
739 std::string Exec;
Chris Lattnerffac2862006-06-06 22:30:59 +0000740
Rafael Espindolae7a629f2013-06-13 16:22:26 +0000741 if (RemoteClientPath.empty())
Evan Cheng779b52e2007-05-03 18:36:15 +0000742 ProgramArgs.push_back(OutputBinary.c_str());
743 else {
Evan Cheng7aedcf12008-09-09 06:11:26 +0000744 ProgramArgs.push_back(RemoteClientPath.c_str());
745 ProgramArgs.push_back(RemoteHost.c_str());
Anton Korobeynikov0c3f8d52009-08-05 09:32:53 +0000746 if (!RemoteUser.empty()) {
747 ProgramArgs.push_back("-l");
748 ProgramArgs.push_back(RemoteUser.c_str());
749 }
David Goodwin73f4e3f2009-07-10 21:39:28 +0000750 if (!RemotePort.empty()) {
751 ProgramArgs.push_back("-p");
752 ProgramArgs.push_back(RemotePort.c_str());
753 }
Evan Cheng7aedcf12008-09-09 06:11:26 +0000754 if (!RemoteExtra.empty()) {
755 ProgramArgs.push_back(RemoteExtra.c_str());
756 }
Evan Cheng779b52e2007-05-03 18:36:15 +0000757
David Goodwin47ca4612009-07-20 17:15:03 +0000758 // Full path to the binary. We need to cd to the exec directory because
759 // there is a dylib there that the exec expects to find in the CWD
Evan Cheng779b52e2007-05-03 18:36:15 +0000760 char* env_pwd = getenv("PWD");
Rafael Espindolaf8203a32010-07-24 23:02:11 +0000761 Exec = "cd ";
Evan Cheng779b52e2007-05-03 18:36:15 +0000762 Exec += env_pwd;
David Goodwin47ca4612009-07-20 17:15:03 +0000763 Exec += "; ./";
Evan Cheng779b52e2007-05-03 18:36:15 +0000764 Exec += OutputBinary.c_str();
765 ProgramArgs.push_back(Exec.c_str());
766 }
767
Chris Lattnerffac2862006-06-06 22:30:59 +0000768 // Add optional parameters to the running program from Argv
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000769 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Chris Lattnerffac2862006-06-06 22:30:59 +0000770 ProgramArgs.push_back(Args[i].c_str());
Craig Toppere6cb63e2014-04-25 04:24:47 +0000771 ProgramArgs.push_back(nullptr); // NULL terminator
Chris Lattnerffac2862006-06-06 22:30:59 +0000772
773 // Now that we have a binary, run it!
Dan Gohman607818a2009-07-15 17:29:42 +0000774 outs() << "<program>"; outs().flush();
Dan Gohmand8db3762009-07-15 16:35:29 +0000775 DEBUG(errs() << "\nAbout to run:\t";
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000776 for (unsigned i = 0, e = ProgramArgs.size()-1; i != e; ++i)
Dan Gohmand8db3762009-07-15 16:35:29 +0000777 errs() << " " << ProgramArgs[i];
778 errs() << "\n";
Chris Lattnerffac2862006-06-06 22:30:59 +0000779 );
780
Michael J. Spencerc60223e2011-03-31 13:04:19 +0000781 FileRemover OutputBinaryRemover(OutputBinary.str(), !SaveTemps);
Evan Cheng779b52e2007-05-03 18:36:15 +0000782
Rafael Espindolae7a629f2013-06-13 16:22:26 +0000783 if (RemoteClientPath.empty()) {
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000784 DEBUG(errs() << "<run locally>");
Rafael Espindolab851d422013-06-13 15:47:11 +0000785 int ExitCode = RunProgramWithTimeout(OutputBinary.str(), &ProgramArgs[0],
786 InputFile, OutputFile, OutputFile,
787 Timeout, MemoryLimit, Error);
Andrew Trickd5d07642011-05-21 00:56:46 +0000788 // Treat a signal (usually SIGSEGV) or timeout as part of the program output
789 // so that crash-causing miscompilation is handled seamlessly.
790 if (ExitCode < -1) {
Andrew Trick55aeb552011-05-11 16:31:24 +0000791 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
792 outFile << *Error << '\n';
793 outFile.close();
794 Error->clear();
795 }
796 return ExitCode;
Viktor Kutuzov1518de12009-07-14 19:10:55 +0000797 } else {
Dan Gohman607818a2009-07-15 17:29:42 +0000798 outs() << "<run remotely>"; outs().flush();
Rafael Espindolae7a629f2013-06-13 16:22:26 +0000799 return RunProgramRemotelyWithTimeout(RemoteClientPath,
Rafael Espindolab851d422013-06-13 15:47:11 +0000800 &ProgramArgs[0], InputFile, OutputFile,
801 OutputFile, Timeout, MemoryLimit);
Viktor Kutuzov1518de12009-07-14 19:10:55 +0000802 }
Chris Lattnerffac2862006-06-06 22:30:59 +0000803}
804
Davide Italianoab256212015-10-14 20:29:54 +0000805int CC::MakeSharedObject(const std::string &InputFile, FileType fileType,
Chris Lattnercc21fa72006-06-27 20:35:36 +0000806 std::string &OutputFile,
Davide Italianoab256212015-10-14 20:29:54 +0000807 const std::vector<std::string> &ArgsForCC,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000808 std::string &Error) {
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000809 SmallString<128> UniqueFilename;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000810 std::error_code EC = sys::fs::createUniqueFile(
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000811 InputFile + "-%%%%%%%" + LTDL_SHLIB_EXT, UniqueFilename);
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000812 if (EC) {
813 errs() << "Error making unique filename: " << EC.message() << "\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000814 exit(1);
815 }
Rafael Espindola8ffbac92013-06-18 17:20:08 +0000816 OutputFile = UniqueFilename.str();
Chris Lattnerffac2862006-06-06 22:30:59 +0000817
Davide Italianoab256212015-10-14 20:29:54 +0000818 std::vector<const char*> CCArgs;
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000819
Davide Italianoab256212015-10-14 20:29:54 +0000820 CCArgs.push_back(CCPath.c_str());
Evan Cheng690b6352009-03-12 00:53:34 +0000821
Chris Lattnerfd381322010-03-16 06:41:47 +0000822 if (TargetTriple.getArch() == Triple::x86)
Davide Italianoab256212015-10-14 20:29:54 +0000823 CCArgs.push_back("-m32");
Chris Lattnerfd381322010-03-16 06:41:47 +0000824
Evan Cheng690b6352009-03-12 00:53:34 +0000825 for (std::vector<std::string>::const_iterator
Davide Italianoab256212015-10-14 20:29:54 +0000826 I = ccArgs.begin(), E = ccArgs.end(); I != E; ++I)
827 CCArgs.push_back(I->c_str());
Evan Cheng690b6352009-03-12 00:53:34 +0000828
Chris Lattnerffac2862006-06-06 22:30:59 +0000829 // Compile the C/asm file into a shared object
Chris Lattnerfd381322010-03-16 06:41:47 +0000830 if (fileType != ObjectFile) {
Davide Italianoab256212015-10-14 20:29:54 +0000831 CCArgs.push_back("-x");
832 CCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
Chris Lattnerfd381322010-03-16 06:41:47 +0000833 }
Davide Italianoab256212015-10-14 20:29:54 +0000834 CCArgs.push_back("-fno-strict-aliasing");
835 CCArgs.push_back(InputFile.c_str()); // Specify the input filename.
836 CCArgs.push_back("-x");
837 CCArgs.push_back("none");
Daniel Dunbar8575a602009-08-18 03:35:57 +0000838 if (TargetTriple.getArch() == Triple::sparc)
Davide Italianoab256212015-10-14 20:29:54 +0000839 CCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000840 else if (TargetTriple.isOSDarwin()) {
Daniel Dunbar8575a602009-08-18 03:35:57 +0000841 // link all source files into a single module in data segment, rather than
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000842 // generating blocks. dynamic_lookup requires that you set
Daniel Dunbar8575a602009-08-18 03:35:57 +0000843 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for
Davide Italianoab256212015-10-14 20:29:54 +0000844 // bugpoint to just pass that in the environment of CC.
845 CCArgs.push_back("-single_module");
846 CCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC
847 CCArgs.push_back("-undefined");
848 CCArgs.push_back("dynamic_lookup");
Daniel Dunbar8575a602009-08-18 03:35:57 +0000849 } else
Davide Italianoab256212015-10-14 20:29:54 +0000850 CCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others
Chris Lattnerffac2862006-06-06 22:30:59 +0000851
Dan Gohman4c9fca92011-10-27 22:56:32 +0000852 if (TargetTriple.getArch() == Triple::x86_64)
Davide Italianoab256212015-10-14 20:29:54 +0000853 CCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC
Daniel Dunbar8575a602009-08-18 03:35:57 +0000854
855 if (TargetTriple.getArch() == Triple::sparc)
Davide Italianoab256212015-10-14 20:29:54 +0000856 CCArgs.push_back("-mcpu=v9");
Daniel Dunbar8575a602009-08-18 03:35:57 +0000857
Davide Italianoab256212015-10-14 20:29:54 +0000858 CCArgs.push_back("-o");
859 CCArgs.push_back(OutputFile.c_str()); // Output to the right filename.
860 CCArgs.push_back("-O2"); // Optimize the program a bit.
Chris Lattnercc21fa72006-06-27 20:35:36 +0000861
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000862
863
Davide Italianoab256212015-10-14 20:29:54 +0000864 // Add any arguments intended for CC. We locate them here because this is
Chris Lattnercc21fa72006-06-27 20:35:36 +0000865 // most likely -L and -l options that need to come before other libraries but
866 // after the source. Other options won't be sensitive to placement on the
867 // command line, so this should be safe.
Davide Italianoab256212015-10-14 20:29:54 +0000868 for (unsigned i = 0, e = ArgsForCC.size(); i != e; ++i)
869 CCArgs.push_back(ArgsForCC[i].c_str());
870 CCArgs.push_back(nullptr); // NULL terminator
Chris Lattnercc21fa72006-06-27 20:35:36 +0000871
Mikhail Glushenkov189c6c62010-11-03 16:14:07 +0000872
Chris Lattnerffac2862006-06-06 22:30:59 +0000873
Davide Italianoab256212015-10-14 20:29:54 +0000874 outs() << "<CC>"; outs().flush();
Dan Gohmand8db3762009-07-15 16:35:29 +0000875 DEBUG(errs() << "\nAbout to run:\t";
Davide Italianoab256212015-10-14 20:29:54 +0000876 for (unsigned i = 0, e = CCArgs.size()-1; i != e; ++i)
877 errs() << " " << CCArgs[i];
Dan Gohmand8db3762009-07-15 16:35:29 +0000878 errs() << "\n";
Evan Chenga50e3912007-01-03 07:44:30 +0000879 );
Davide Italianoab256212015-10-14 20:29:54 +0000880 if (RunProgramWithTimeout(CCPath, &CCArgs[0], "", "", "")) {
881 Error = ProcessFailure(CCPath, &CCArgs[0]);
Chris Lattnerffac2862006-06-06 22:30:59 +0000882 return 1;
883 }
884 return 0;
885}
886
Davide Italianoab256212015-10-14 20:29:54 +0000887/// create - Try to find the CC executable
Chris Lattnerffac2862006-06-06 22:30:59 +0000888///
Davide Italianoab256212015-10-14 20:29:54 +0000889CC *CC::create(std::string &Message,
890 const std::string &CCBinary,
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000891 const std::vector<std::string> *Args) {
Davide Italianoab256212015-10-14 20:29:54 +0000892 auto CCPath = sys::findProgramByName(CCBinary);
893 if (!CCPath) {
894 Message = "Cannot find `" + CCBinary + "' in PATH: " +
895 CCPath.getError().message() + "\n";
Craig Toppere6cb63e2014-04-25 04:24:47 +0000896 return nullptr;
Chris Lattnerffac2862006-06-06 22:30:59 +0000897 }
898
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +0000899 std::string RemoteClientPath;
Michael J. Spencerf9074b52014-11-04 01:29:59 +0000900 if (!RemoteClient.empty()) {
901 auto Path = sys::findProgramByName(RemoteClient);
902 if (!Path) {
903 Message = "Cannot find `" + RemoteClient + "' in PATH: " +
904 Path.getError().message() + "\n";
905 return nullptr;
906 }
907 RemoteClientPath = *Path;
908 }
Evan Cheng779b52e2007-05-03 18:36:15 +0000909
Davide Italianoab256212015-10-14 20:29:54 +0000910 Message = "Found CC: " + *CCPath + "\n";
911 return new CC(*CCPath, RemoteClientPath, Args);
Chris Lattnerffac2862006-06-06 22:30:59 +0000912}