xref: /minix3/external/bsd/llvm/dist/llvm/tools/bugpoint/ToolRunner.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===-- ToolRunner.cpp ----------------------------------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements the interfaces described in the ToolRunner.h file.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "ToolRunner.h"
15f4a2713aSLionel Sambuc #include "llvm/Config/config.h"   // for HAVE_LINK_R
16f4a2713aSLionel Sambuc #include "llvm/Support/CommandLine.h"
17f4a2713aSLionel Sambuc #include "llvm/Support/Debug.h"
18f4a2713aSLionel Sambuc #include "llvm/Support/FileSystem.h"
19f4a2713aSLionel Sambuc #include "llvm/Support/FileUtilities.h"
20f4a2713aSLionel Sambuc #include "llvm/Support/Program.h"
21f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
22f4a2713aSLionel Sambuc #include <fstream>
23f4a2713aSLionel Sambuc #include <sstream>
24f4a2713aSLionel Sambuc using namespace llvm;
25f4a2713aSLionel Sambuc 
26*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "toolrunner"
27*0a6a1f1dSLionel Sambuc 
28f4a2713aSLionel Sambuc namespace llvm {
29f4a2713aSLionel Sambuc   cl::opt<bool>
30f4a2713aSLionel Sambuc   SaveTemps("save-temps", cl::init(false), cl::desc("Save temporary files"));
31f4a2713aSLionel Sambuc }
32f4a2713aSLionel Sambuc 
33f4a2713aSLionel Sambuc namespace {
34f4a2713aSLionel Sambuc   cl::opt<std::string>
35f4a2713aSLionel Sambuc   RemoteClient("remote-client",
36f4a2713aSLionel Sambuc                cl::desc("Remote execution client (rsh/ssh)"));
37f4a2713aSLionel Sambuc 
38f4a2713aSLionel Sambuc   cl::opt<std::string>
39f4a2713aSLionel Sambuc   RemoteHost("remote-host",
40f4a2713aSLionel Sambuc              cl::desc("Remote execution (rsh/ssh) host"));
41f4a2713aSLionel Sambuc 
42f4a2713aSLionel Sambuc   cl::opt<std::string>
43f4a2713aSLionel Sambuc   RemotePort("remote-port",
44f4a2713aSLionel Sambuc              cl::desc("Remote execution (rsh/ssh) port"));
45f4a2713aSLionel Sambuc 
46f4a2713aSLionel Sambuc   cl::opt<std::string>
47f4a2713aSLionel Sambuc   RemoteUser("remote-user",
48f4a2713aSLionel Sambuc              cl::desc("Remote execution (rsh/ssh) user id"));
49f4a2713aSLionel Sambuc 
50f4a2713aSLionel Sambuc   cl::opt<std::string>
51f4a2713aSLionel Sambuc   RemoteExtra("remote-extra-options",
52f4a2713aSLionel Sambuc           cl::desc("Remote execution (rsh/ssh) extra options"));
53f4a2713aSLionel Sambuc }
54f4a2713aSLionel Sambuc 
55f4a2713aSLionel Sambuc /// RunProgramWithTimeout - This function provides an alternate interface
56f4a2713aSLionel Sambuc /// to the sys::Program::ExecuteAndWait interface.
57f4a2713aSLionel Sambuc /// @see sys::Program::ExecuteAndWait
RunProgramWithTimeout(StringRef ProgramPath,const char ** Args,StringRef StdInFile,StringRef StdOutFile,StringRef StdErrFile,unsigned NumSeconds=0,unsigned MemoryLimit=0,std::string * ErrMsg=nullptr)58f4a2713aSLionel Sambuc static int RunProgramWithTimeout(StringRef ProgramPath,
59f4a2713aSLionel Sambuc                                  const char **Args,
60f4a2713aSLionel Sambuc                                  StringRef StdInFile,
61f4a2713aSLionel Sambuc                                  StringRef StdOutFile,
62f4a2713aSLionel Sambuc                                  StringRef StdErrFile,
63f4a2713aSLionel Sambuc                                  unsigned NumSeconds = 0,
64f4a2713aSLionel Sambuc                                  unsigned MemoryLimit = 0,
65*0a6a1f1dSLionel Sambuc                                  std::string *ErrMsg = nullptr) {
66f4a2713aSLionel Sambuc   const StringRef *Redirects[3] = { &StdInFile, &StdOutFile, &StdErrFile };
67f4a2713aSLionel Sambuc 
68f4a2713aSLionel Sambuc #if 0 // For debug purposes
69f4a2713aSLionel Sambuc   {
70f4a2713aSLionel Sambuc     errs() << "RUN:";
71f4a2713aSLionel Sambuc     for (unsigned i = 0; Args[i]; ++i)
72f4a2713aSLionel Sambuc       errs() << " " << Args[i];
73f4a2713aSLionel Sambuc     errs() << "\n";
74f4a2713aSLionel Sambuc   }
75f4a2713aSLionel Sambuc #endif
76f4a2713aSLionel Sambuc 
77*0a6a1f1dSLionel Sambuc   return sys::ExecuteAndWait(ProgramPath, Args, nullptr, Redirects,
78f4a2713aSLionel Sambuc                              NumSeconds, MemoryLimit, ErrMsg);
79f4a2713aSLionel Sambuc }
80f4a2713aSLionel Sambuc 
81f4a2713aSLionel Sambuc /// RunProgramRemotelyWithTimeout - This function runs the given program
82f4a2713aSLionel Sambuc /// remotely using the given remote client and the sys::Program::ExecuteAndWait.
83f4a2713aSLionel Sambuc /// Returns the remote program exit code or reports a remote client error if it
84f4a2713aSLionel Sambuc /// fails. Remote client is required to return 255 if it failed or program exit
85f4a2713aSLionel Sambuc /// code otherwise.
86f4a2713aSLionel Sambuc /// @see sys::Program::ExecuteAndWait
RunProgramRemotelyWithTimeout(StringRef RemoteClientPath,const char ** Args,StringRef StdInFile,StringRef StdOutFile,StringRef StdErrFile,unsigned NumSeconds=0,unsigned MemoryLimit=0)87f4a2713aSLionel Sambuc static int RunProgramRemotelyWithTimeout(StringRef RemoteClientPath,
88f4a2713aSLionel Sambuc                                          const char **Args,
89f4a2713aSLionel Sambuc                                          StringRef StdInFile,
90f4a2713aSLionel Sambuc                                          StringRef StdOutFile,
91f4a2713aSLionel Sambuc                                          StringRef StdErrFile,
92f4a2713aSLionel Sambuc                                          unsigned NumSeconds = 0,
93f4a2713aSLionel Sambuc                                          unsigned MemoryLimit = 0) {
94f4a2713aSLionel Sambuc   const StringRef *Redirects[3] = { &StdInFile, &StdOutFile, &StdErrFile };
95f4a2713aSLionel Sambuc 
96f4a2713aSLionel Sambuc #if 0 // For debug purposes
97f4a2713aSLionel Sambuc   {
98f4a2713aSLionel Sambuc     errs() << "RUN:";
99f4a2713aSLionel Sambuc     for (unsigned i = 0; Args[i]; ++i)
100f4a2713aSLionel Sambuc       errs() << " " << Args[i];
101f4a2713aSLionel Sambuc     errs() << "\n";
102f4a2713aSLionel Sambuc   }
103f4a2713aSLionel Sambuc #endif
104f4a2713aSLionel Sambuc 
105f4a2713aSLionel Sambuc   // Run the program remotely with the remote client
106*0a6a1f1dSLionel Sambuc   int ReturnCode = sys::ExecuteAndWait(RemoteClientPath, Args, nullptr,
107f4a2713aSLionel Sambuc                                        Redirects, NumSeconds, MemoryLimit);
108f4a2713aSLionel Sambuc 
109f4a2713aSLionel Sambuc   // Has the remote client fail?
110f4a2713aSLionel Sambuc   if (255 == ReturnCode) {
111f4a2713aSLionel Sambuc     std::ostringstream OS;
112f4a2713aSLionel Sambuc     OS << "\nError running remote client:\n ";
113f4a2713aSLionel Sambuc     for (const char **Arg = Args; *Arg; ++Arg)
114f4a2713aSLionel Sambuc       OS << " " << *Arg;
115f4a2713aSLionel Sambuc     OS << "\n";
116f4a2713aSLionel Sambuc 
117f4a2713aSLionel Sambuc     // The error message is in the output file, let's print it out from there.
118f4a2713aSLionel Sambuc     std::string StdOutFileName = StdOutFile.str();
119f4a2713aSLionel Sambuc     std::ifstream ErrorFile(StdOutFileName.c_str());
120f4a2713aSLionel Sambuc     if (ErrorFile) {
121f4a2713aSLionel Sambuc       std::copy(std::istreambuf_iterator<char>(ErrorFile),
122f4a2713aSLionel Sambuc                 std::istreambuf_iterator<char>(),
123f4a2713aSLionel Sambuc                 std::ostreambuf_iterator<char>(OS));
124f4a2713aSLionel Sambuc       ErrorFile.close();
125f4a2713aSLionel Sambuc     }
126f4a2713aSLionel Sambuc 
127f4a2713aSLionel Sambuc     errs() << OS.str();
128f4a2713aSLionel Sambuc   }
129f4a2713aSLionel Sambuc 
130f4a2713aSLionel Sambuc   return ReturnCode;
131f4a2713aSLionel Sambuc }
132f4a2713aSLionel Sambuc 
ProcessFailure(StringRef ProgPath,const char ** Args,unsigned Timeout=0,unsigned MemoryLimit=0)133f4a2713aSLionel Sambuc static std::string ProcessFailure(StringRef ProgPath, const char** Args,
134f4a2713aSLionel Sambuc                                   unsigned Timeout = 0,
135f4a2713aSLionel Sambuc                                   unsigned MemoryLimit = 0) {
136f4a2713aSLionel Sambuc   std::ostringstream OS;
137f4a2713aSLionel Sambuc   OS << "\nError running tool:\n ";
138f4a2713aSLionel Sambuc   for (const char **Arg = Args; *Arg; ++Arg)
139f4a2713aSLionel Sambuc     OS << " " << *Arg;
140f4a2713aSLionel Sambuc   OS << "\n";
141f4a2713aSLionel Sambuc 
142f4a2713aSLionel Sambuc   // Rerun the compiler, capturing any error messages to print them.
143f4a2713aSLionel Sambuc   SmallString<128> ErrorFilename;
144*0a6a1f1dSLionel Sambuc   std::error_code EC = sys::fs::createTemporaryFile(
145*0a6a1f1dSLionel Sambuc       "bugpoint.program_error_messages", "", ErrorFilename);
146f4a2713aSLionel Sambuc   if (EC) {
147f4a2713aSLionel Sambuc     errs() << "Error making unique filename: " << EC.message() << "\n";
148f4a2713aSLionel Sambuc     exit(1);
149f4a2713aSLionel Sambuc   }
150*0a6a1f1dSLionel Sambuc 
151f4a2713aSLionel Sambuc   RunProgramWithTimeout(ProgPath, Args, "", ErrorFilename.str(),
152f4a2713aSLionel Sambuc                         ErrorFilename.str(), Timeout, MemoryLimit);
153f4a2713aSLionel Sambuc   // FIXME: check return code ?
154f4a2713aSLionel Sambuc 
155f4a2713aSLionel Sambuc   // Print out the error messages generated by GCC if possible...
156f4a2713aSLionel Sambuc   std::ifstream ErrorFile(ErrorFilename.c_str());
157f4a2713aSLionel Sambuc   if (ErrorFile) {
158f4a2713aSLionel Sambuc     std::copy(std::istreambuf_iterator<char>(ErrorFile),
159f4a2713aSLionel Sambuc               std::istreambuf_iterator<char>(),
160f4a2713aSLionel Sambuc               std::ostreambuf_iterator<char>(OS));
161f4a2713aSLionel Sambuc     ErrorFile.close();
162f4a2713aSLionel Sambuc   }
163f4a2713aSLionel Sambuc 
164f4a2713aSLionel Sambuc   sys::fs::remove(ErrorFilename.c_str());
165f4a2713aSLionel Sambuc   return OS.str();
166f4a2713aSLionel Sambuc }
167f4a2713aSLionel Sambuc 
168f4a2713aSLionel Sambuc //===---------------------------------------------------------------------===//
169f4a2713aSLionel Sambuc // LLI Implementation of AbstractIntepreter interface
170f4a2713aSLionel Sambuc //
171f4a2713aSLionel Sambuc namespace {
172f4a2713aSLionel Sambuc   class LLI : public AbstractInterpreter {
173f4a2713aSLionel Sambuc     std::string LLIPath;          // The path to the LLI executable
174f4a2713aSLionel Sambuc     std::vector<std::string> ToolArgs; // Args to pass to LLI
175f4a2713aSLionel Sambuc   public:
LLI(const std::string & Path,const std::vector<std::string> * Args)176f4a2713aSLionel Sambuc     LLI(const std::string &Path, const std::vector<std::string> *Args)
177f4a2713aSLionel Sambuc       : LLIPath(Path) {
178f4a2713aSLionel Sambuc       ToolArgs.clear ();
179f4a2713aSLionel Sambuc       if (Args) { ToolArgs = *Args; }
180f4a2713aSLionel Sambuc     }
181f4a2713aSLionel Sambuc 
182*0a6a1f1dSLionel Sambuc     int ExecuteProgram(const std::string &Bitcode,
183f4a2713aSLionel Sambuc                        const std::vector<std::string> &Args,
184f4a2713aSLionel Sambuc                        const std::string &InputFile,
185f4a2713aSLionel Sambuc                        const std::string &OutputFile,
186f4a2713aSLionel Sambuc                        std::string *Error,
187f4a2713aSLionel Sambuc                        const std::vector<std::string> &GCCArgs,
188f4a2713aSLionel Sambuc                        const std::vector<std::string> &SharedLibs =
189f4a2713aSLionel Sambuc                        std::vector<std::string>(),
190f4a2713aSLionel Sambuc                        unsigned Timeout = 0,
191*0a6a1f1dSLionel Sambuc                        unsigned MemoryLimit = 0) override;
192f4a2713aSLionel Sambuc   };
193f4a2713aSLionel Sambuc }
194f4a2713aSLionel Sambuc 
ExecuteProgram(const std::string & Bitcode,const std::vector<std::string> & Args,const std::string & InputFile,const std::string & OutputFile,std::string * Error,const std::vector<std::string> & GCCArgs,const std::vector<std::string> & SharedLibs,unsigned Timeout,unsigned MemoryLimit)195f4a2713aSLionel Sambuc int LLI::ExecuteProgram(const std::string &Bitcode,
196f4a2713aSLionel Sambuc                         const std::vector<std::string> &Args,
197f4a2713aSLionel Sambuc                         const std::string &InputFile,
198f4a2713aSLionel Sambuc                         const std::string &OutputFile,
199f4a2713aSLionel Sambuc                         std::string *Error,
200f4a2713aSLionel Sambuc                         const std::vector<std::string> &GCCArgs,
201f4a2713aSLionel Sambuc                         const std::vector<std::string> &SharedLibs,
202f4a2713aSLionel Sambuc                         unsigned Timeout,
203f4a2713aSLionel Sambuc                         unsigned MemoryLimit) {
204f4a2713aSLionel Sambuc   std::vector<const char*> LLIArgs;
205f4a2713aSLionel Sambuc   LLIArgs.push_back(LLIPath.c_str());
206f4a2713aSLionel Sambuc   LLIArgs.push_back("-force-interpreter=true");
207f4a2713aSLionel Sambuc 
208f4a2713aSLionel Sambuc   for (std::vector<std::string>::const_iterator i = SharedLibs.begin(),
209f4a2713aSLionel Sambuc          e = SharedLibs.end(); i != e; ++i) {
210f4a2713aSLionel Sambuc     LLIArgs.push_back("-load");
211f4a2713aSLionel Sambuc     LLIArgs.push_back((*i).c_str());
212f4a2713aSLionel Sambuc   }
213f4a2713aSLionel Sambuc 
214f4a2713aSLionel Sambuc   // Add any extra LLI args.
215f4a2713aSLionel Sambuc   for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
216f4a2713aSLionel Sambuc     LLIArgs.push_back(ToolArgs[i].c_str());
217f4a2713aSLionel Sambuc 
218f4a2713aSLionel Sambuc   LLIArgs.push_back(Bitcode.c_str());
219f4a2713aSLionel Sambuc   // Add optional parameters to the running program from Argv
220f4a2713aSLionel Sambuc   for (unsigned i=0, e = Args.size(); i != e; ++i)
221f4a2713aSLionel Sambuc     LLIArgs.push_back(Args[i].c_str());
222*0a6a1f1dSLionel Sambuc   LLIArgs.push_back(nullptr);
223f4a2713aSLionel Sambuc 
224f4a2713aSLionel Sambuc   outs() << "<lli>"; outs().flush();
225f4a2713aSLionel Sambuc   DEBUG(errs() << "\nAbout to run:\t";
226f4a2713aSLionel Sambuc         for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
227f4a2713aSLionel Sambuc           errs() << " " << LLIArgs[i];
228f4a2713aSLionel Sambuc         errs() << "\n";
229f4a2713aSLionel Sambuc         );
230f4a2713aSLionel Sambuc   return RunProgramWithTimeout(LLIPath, &LLIArgs[0],
231f4a2713aSLionel Sambuc       InputFile, OutputFile, OutputFile,
232f4a2713aSLionel Sambuc       Timeout, MemoryLimit, Error);
233f4a2713aSLionel Sambuc }
234f4a2713aSLionel Sambuc 
anchor()235f4a2713aSLionel Sambuc void AbstractInterpreter::anchor() { }
236f4a2713aSLionel Sambuc 
237f4a2713aSLionel Sambuc #if defined(LLVM_ON_UNIX)
238f4a2713aSLionel Sambuc const char EXESuffix[] = "";
239f4a2713aSLionel Sambuc #elif defined (LLVM_ON_WIN32)
240f4a2713aSLionel Sambuc const char EXESuffix[] = "exe";
241f4a2713aSLionel Sambuc #endif
242f4a2713aSLionel Sambuc 
243f4a2713aSLionel Sambuc /// Prepend the path to the program being executed
244f4a2713aSLionel Sambuc /// to \p ExeName, given the value of argv[0] and the address of main()
245f4a2713aSLionel Sambuc /// itself. This allows us to find another LLVM tool if it is built in the same
246f4a2713aSLionel Sambuc /// directory. An empty string is returned on error; note that this function
247f4a2713aSLionel Sambuc /// just mainpulates the path and doesn't check for executability.
248f4a2713aSLionel Sambuc /// @brief Find a named executable.
PrependMainExecutablePath(const std::string & ExeName,const char * Argv0,void * MainAddr)249f4a2713aSLionel Sambuc static std::string PrependMainExecutablePath(const std::string &ExeName,
250f4a2713aSLionel Sambuc                                              const char *Argv0,
251f4a2713aSLionel Sambuc                                              void *MainAddr) {
252f4a2713aSLionel Sambuc   // Check the directory that the calling program is in.  We can do
253f4a2713aSLionel Sambuc   // this if ProgramPath contains at least one / character, indicating that it
254f4a2713aSLionel Sambuc   // is a relative path to the executable itself.
255f4a2713aSLionel Sambuc   std::string Main = sys::fs::getMainExecutable(Argv0, MainAddr);
256f4a2713aSLionel Sambuc   StringRef Result = sys::path::parent_path(Main);
257f4a2713aSLionel Sambuc 
258f4a2713aSLionel Sambuc   if (!Result.empty()) {
259f4a2713aSLionel Sambuc     SmallString<128> Storage = Result;
260f4a2713aSLionel Sambuc     sys::path::append(Storage, ExeName);
261f4a2713aSLionel Sambuc     sys::path::replace_extension(Storage, EXESuffix);
262f4a2713aSLionel Sambuc     return Storage.str();
263f4a2713aSLionel Sambuc   }
264f4a2713aSLionel Sambuc 
265f4a2713aSLionel Sambuc   return Result.str();
266f4a2713aSLionel Sambuc }
267f4a2713aSLionel Sambuc 
268f4a2713aSLionel Sambuc // LLI create method - Try to find the LLI executable
createLLI(const char * Argv0,std::string & Message,const std::vector<std::string> * ToolArgs)269f4a2713aSLionel Sambuc AbstractInterpreter *AbstractInterpreter::createLLI(const char *Argv0,
270f4a2713aSLionel Sambuc                                                     std::string &Message,
271f4a2713aSLionel Sambuc                                      const std::vector<std::string> *ToolArgs) {
272f4a2713aSLionel Sambuc   std::string LLIPath =
273f4a2713aSLionel Sambuc       PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t) & createLLI);
274f4a2713aSLionel Sambuc   if (!LLIPath.empty()) {
275f4a2713aSLionel Sambuc     Message = "Found lli: " + LLIPath + "\n";
276f4a2713aSLionel Sambuc     return new LLI(LLIPath, ToolArgs);
277f4a2713aSLionel Sambuc   }
278f4a2713aSLionel Sambuc 
279f4a2713aSLionel Sambuc   Message = "Cannot find `lli' in executable directory!\n";
280*0a6a1f1dSLionel Sambuc   return nullptr;
281f4a2713aSLionel Sambuc }
282f4a2713aSLionel Sambuc 
283f4a2713aSLionel Sambuc //===---------------------------------------------------------------------===//
284f4a2713aSLionel Sambuc // Custom compiler command implementation of AbstractIntepreter interface
285f4a2713aSLionel Sambuc //
286f4a2713aSLionel Sambuc // Allows using a custom command for compiling the bitcode, thus allows, for
287f4a2713aSLionel Sambuc // example, to compile a bitcode fragment without linking or executing, then
288f4a2713aSLionel Sambuc // using a custom wrapper script to check for compiler errors.
289f4a2713aSLionel Sambuc namespace {
290f4a2713aSLionel Sambuc   class CustomCompiler : public AbstractInterpreter {
291f4a2713aSLionel Sambuc     std::string CompilerCommand;
292f4a2713aSLionel Sambuc     std::vector<std::string> CompilerArgs;
293f4a2713aSLionel Sambuc   public:
CustomCompiler(const std::string & CompilerCmd,std::vector<std::string> CompArgs)294f4a2713aSLionel Sambuc     CustomCompiler(
295f4a2713aSLionel Sambuc       const std::string &CompilerCmd, std::vector<std::string> CompArgs) :
296f4a2713aSLionel Sambuc       CompilerCommand(CompilerCmd), CompilerArgs(CompArgs) {}
297f4a2713aSLionel Sambuc 
298*0a6a1f1dSLionel Sambuc     void compileProgram(const std::string &Bitcode,
299f4a2713aSLionel Sambuc                         std::string *Error,
300f4a2713aSLionel Sambuc                         unsigned Timeout = 0,
301*0a6a1f1dSLionel Sambuc                         unsigned MemoryLimit = 0) override;
302f4a2713aSLionel Sambuc 
ExecuteProgram(const std::string & Bitcode,const std::vector<std::string> & Args,const std::string & InputFile,const std::string & OutputFile,std::string * Error,const std::vector<std::string> & GCCArgs=std::vector<std::string> (),const std::vector<std::string> & SharedLibs=std::vector<std::string> (),unsigned Timeout=0,unsigned MemoryLimit=0)303*0a6a1f1dSLionel Sambuc     int ExecuteProgram(const std::string &Bitcode,
304f4a2713aSLionel Sambuc                        const std::vector<std::string> &Args,
305f4a2713aSLionel Sambuc                        const std::string &InputFile,
306f4a2713aSLionel Sambuc                        const std::string &OutputFile,
307f4a2713aSLionel Sambuc                        std::string *Error,
308f4a2713aSLionel Sambuc                        const std::vector<std::string> &GCCArgs =
309f4a2713aSLionel Sambuc                        std::vector<std::string>(),
310f4a2713aSLionel Sambuc                        const std::vector<std::string> &SharedLibs =
311f4a2713aSLionel Sambuc                        std::vector<std::string>(),
312f4a2713aSLionel Sambuc                        unsigned Timeout = 0,
313*0a6a1f1dSLionel Sambuc                        unsigned MemoryLimit = 0) override {
314f4a2713aSLionel Sambuc       *Error = "Execution not supported with -compile-custom";
315f4a2713aSLionel Sambuc       return -1;
316f4a2713aSLionel Sambuc     }
317f4a2713aSLionel Sambuc   };
318f4a2713aSLionel Sambuc }
319f4a2713aSLionel Sambuc 
compileProgram(const std::string & Bitcode,std::string * Error,unsigned Timeout,unsigned MemoryLimit)320f4a2713aSLionel Sambuc void CustomCompiler::compileProgram(const std::string &Bitcode,
321f4a2713aSLionel Sambuc                                     std::string *Error,
322f4a2713aSLionel Sambuc                                     unsigned Timeout,
323f4a2713aSLionel Sambuc                                     unsigned MemoryLimit) {
324f4a2713aSLionel Sambuc 
325f4a2713aSLionel Sambuc   std::vector<const char*> ProgramArgs;
326f4a2713aSLionel Sambuc   ProgramArgs.push_back(CompilerCommand.c_str());
327f4a2713aSLionel Sambuc 
328f4a2713aSLionel Sambuc   for (std::size_t i = 0; i < CompilerArgs.size(); ++i)
329f4a2713aSLionel Sambuc     ProgramArgs.push_back(CompilerArgs.at(i).c_str());
330f4a2713aSLionel Sambuc   ProgramArgs.push_back(Bitcode.c_str());
331*0a6a1f1dSLionel Sambuc   ProgramArgs.push_back(nullptr);
332f4a2713aSLionel Sambuc 
333f4a2713aSLionel Sambuc   // Add optional parameters to the running program from Argv
334f4a2713aSLionel Sambuc   for (unsigned i = 0, e = CompilerArgs.size(); i != e; ++i)
335f4a2713aSLionel Sambuc     ProgramArgs.push_back(CompilerArgs[i].c_str());
336f4a2713aSLionel Sambuc 
337f4a2713aSLionel Sambuc   if (RunProgramWithTimeout(CompilerCommand, &ProgramArgs[0],
338f4a2713aSLionel Sambuc                              "", "", "",
339f4a2713aSLionel Sambuc                              Timeout, MemoryLimit, Error))
340f4a2713aSLionel Sambuc     *Error = ProcessFailure(CompilerCommand, &ProgramArgs[0],
341f4a2713aSLionel Sambuc                            Timeout, MemoryLimit);
342f4a2713aSLionel Sambuc }
343f4a2713aSLionel Sambuc 
344f4a2713aSLionel Sambuc //===---------------------------------------------------------------------===//
345f4a2713aSLionel Sambuc // Custom execution command implementation of AbstractIntepreter interface
346f4a2713aSLionel Sambuc //
347f4a2713aSLionel Sambuc // Allows using a custom command for executing the bitcode, thus allows,
348f4a2713aSLionel Sambuc // for example, to invoke a cross compiler for code generation followed by
349f4a2713aSLionel Sambuc // a simulator that executes the generated binary.
350f4a2713aSLionel Sambuc namespace {
351f4a2713aSLionel Sambuc   class CustomExecutor : public AbstractInterpreter {
352f4a2713aSLionel Sambuc     std::string ExecutionCommand;
353f4a2713aSLionel Sambuc     std::vector<std::string> ExecutorArgs;
354f4a2713aSLionel Sambuc   public:
CustomExecutor(const std::string & ExecutionCmd,std::vector<std::string> ExecArgs)355f4a2713aSLionel Sambuc     CustomExecutor(
356f4a2713aSLionel Sambuc       const std::string &ExecutionCmd, std::vector<std::string> ExecArgs) :
357f4a2713aSLionel Sambuc       ExecutionCommand(ExecutionCmd), ExecutorArgs(ExecArgs) {}
358f4a2713aSLionel Sambuc 
359*0a6a1f1dSLionel Sambuc     int ExecuteProgram(const std::string &Bitcode,
360f4a2713aSLionel Sambuc                        const std::vector<std::string> &Args,
361f4a2713aSLionel Sambuc                        const std::string &InputFile,
362f4a2713aSLionel Sambuc                        const std::string &OutputFile,
363f4a2713aSLionel Sambuc                        std::string *Error,
364f4a2713aSLionel Sambuc                        const std::vector<std::string> &GCCArgs,
365f4a2713aSLionel Sambuc                        const std::vector<std::string> &SharedLibs =
366f4a2713aSLionel Sambuc                          std::vector<std::string>(),
367f4a2713aSLionel Sambuc                        unsigned Timeout = 0,
368*0a6a1f1dSLionel Sambuc                        unsigned MemoryLimit = 0) override;
369f4a2713aSLionel Sambuc   };
370f4a2713aSLionel Sambuc }
371f4a2713aSLionel Sambuc 
ExecuteProgram(const std::string & Bitcode,const std::vector<std::string> & Args,const std::string & InputFile,const std::string & OutputFile,std::string * Error,const std::vector<std::string> & GCCArgs,const std::vector<std::string> & SharedLibs,unsigned Timeout,unsigned MemoryLimit)372f4a2713aSLionel Sambuc int CustomExecutor::ExecuteProgram(const std::string &Bitcode,
373f4a2713aSLionel Sambuc                         const std::vector<std::string> &Args,
374f4a2713aSLionel Sambuc                         const std::string &InputFile,
375f4a2713aSLionel Sambuc                         const std::string &OutputFile,
376f4a2713aSLionel Sambuc                         std::string *Error,
377f4a2713aSLionel Sambuc                         const std::vector<std::string> &GCCArgs,
378f4a2713aSLionel Sambuc                         const std::vector<std::string> &SharedLibs,
379f4a2713aSLionel Sambuc                         unsigned Timeout,
380f4a2713aSLionel Sambuc                         unsigned MemoryLimit) {
381f4a2713aSLionel Sambuc 
382f4a2713aSLionel Sambuc   std::vector<const char*> ProgramArgs;
383f4a2713aSLionel Sambuc   ProgramArgs.push_back(ExecutionCommand.c_str());
384f4a2713aSLionel Sambuc 
385f4a2713aSLionel Sambuc   for (std::size_t i = 0; i < ExecutorArgs.size(); ++i)
386f4a2713aSLionel Sambuc     ProgramArgs.push_back(ExecutorArgs.at(i).c_str());
387f4a2713aSLionel Sambuc   ProgramArgs.push_back(Bitcode.c_str());
388*0a6a1f1dSLionel Sambuc   ProgramArgs.push_back(nullptr);
389f4a2713aSLionel Sambuc 
390f4a2713aSLionel Sambuc   // Add optional parameters to the running program from Argv
391f4a2713aSLionel Sambuc   for (unsigned i = 0, e = Args.size(); i != e; ++i)
392f4a2713aSLionel Sambuc     ProgramArgs.push_back(Args[i].c_str());
393f4a2713aSLionel Sambuc 
394f4a2713aSLionel Sambuc   return RunProgramWithTimeout(
395f4a2713aSLionel Sambuc     ExecutionCommand,
396f4a2713aSLionel Sambuc     &ProgramArgs[0], InputFile, OutputFile,
397f4a2713aSLionel Sambuc     OutputFile, Timeout, MemoryLimit, Error);
398f4a2713aSLionel Sambuc }
399f4a2713aSLionel Sambuc 
400f4a2713aSLionel Sambuc // Tokenize the CommandLine to the command and the args to allow
401f4a2713aSLionel Sambuc // defining a full command line as the command instead of just the
402f4a2713aSLionel Sambuc // executed program. We cannot just pass the whole string after the command
403f4a2713aSLionel Sambuc // as a single argument because then program sees only a single
404f4a2713aSLionel Sambuc // command line argument (with spaces in it: "foo bar" instead
405f4a2713aSLionel Sambuc // of "foo" and "bar").
406f4a2713aSLionel Sambuc //
407f4a2713aSLionel Sambuc // code borrowed from:
408f4a2713aSLionel Sambuc // http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
lexCommand(std::string & Message,const std::string & CommandLine,std::string & CmdPath,std::vector<std::string> & Args)409f4a2713aSLionel Sambuc static void lexCommand(std::string &Message, const std::string &CommandLine,
410*0a6a1f1dSLionel Sambuc                        std::string &CmdPath, std::vector<std::string> &Args) {
411f4a2713aSLionel Sambuc 
412f4a2713aSLionel Sambuc   std::string Command = "";
413f4a2713aSLionel Sambuc   std::string delimiters = " ";
414f4a2713aSLionel Sambuc 
415f4a2713aSLionel Sambuc   std::string::size_type lastPos = CommandLine.find_first_not_of(delimiters, 0);
416f4a2713aSLionel Sambuc   std::string::size_type pos = CommandLine.find_first_of(delimiters, lastPos);
417f4a2713aSLionel Sambuc 
418f4a2713aSLionel Sambuc   while (std::string::npos != pos || std::string::npos != lastPos) {
419f4a2713aSLionel Sambuc     std::string token = CommandLine.substr(lastPos, pos - lastPos);
420f4a2713aSLionel Sambuc     if (Command == "")
421f4a2713aSLionel Sambuc        Command = token;
422f4a2713aSLionel Sambuc     else
423f4a2713aSLionel Sambuc        Args.push_back(token);
424f4a2713aSLionel Sambuc     // Skip delimiters.  Note the "not_of"
425f4a2713aSLionel Sambuc     lastPos = CommandLine.find_first_not_of(delimiters, pos);
426f4a2713aSLionel Sambuc     // Find next "non-delimiter"
427f4a2713aSLionel Sambuc     pos = CommandLine.find_first_of(delimiters, lastPos);
428f4a2713aSLionel Sambuc   }
429f4a2713aSLionel Sambuc 
430*0a6a1f1dSLionel Sambuc   auto Path = sys::findProgramByName(Command);
431*0a6a1f1dSLionel Sambuc   if (!Path) {
432f4a2713aSLionel Sambuc     Message =
433f4a2713aSLionel Sambuc       std::string("Cannot find '") + Command +
434*0a6a1f1dSLionel Sambuc       "' in PATH: " + Path.getError().message() + "\n";
435f4a2713aSLionel Sambuc     return;
436f4a2713aSLionel Sambuc   }
437*0a6a1f1dSLionel Sambuc   CmdPath = *Path;
438f4a2713aSLionel Sambuc 
439f4a2713aSLionel Sambuc   Message = "Found command in: " + CmdPath + "\n";
440f4a2713aSLionel Sambuc }
441f4a2713aSLionel Sambuc 
442f4a2713aSLionel Sambuc // Custom execution environment create method, takes the execution command
443f4a2713aSLionel Sambuc // as arguments
createCustomCompiler(std::string & Message,const std::string & CompileCommandLine)444f4a2713aSLionel Sambuc AbstractInterpreter *AbstractInterpreter::createCustomCompiler(
445f4a2713aSLionel Sambuc                     std::string &Message,
446f4a2713aSLionel Sambuc                     const std::string &CompileCommandLine) {
447f4a2713aSLionel Sambuc 
448f4a2713aSLionel Sambuc   std::string CmdPath;
449f4a2713aSLionel Sambuc   std::vector<std::string> Args;
450f4a2713aSLionel Sambuc   lexCommand(Message, CompileCommandLine, CmdPath, Args);
451f4a2713aSLionel Sambuc   if (CmdPath.empty())
452*0a6a1f1dSLionel Sambuc     return nullptr;
453f4a2713aSLionel Sambuc 
454f4a2713aSLionel Sambuc   return new CustomCompiler(CmdPath, Args);
455f4a2713aSLionel Sambuc }
456f4a2713aSLionel Sambuc 
457f4a2713aSLionel Sambuc // Custom execution environment create method, takes the execution command
458f4a2713aSLionel Sambuc // as arguments
createCustomExecutor(std::string & Message,const std::string & ExecCommandLine)459f4a2713aSLionel Sambuc AbstractInterpreter *AbstractInterpreter::createCustomExecutor(
460f4a2713aSLionel Sambuc                     std::string &Message,
461f4a2713aSLionel Sambuc                     const std::string &ExecCommandLine) {
462f4a2713aSLionel Sambuc 
463f4a2713aSLionel Sambuc 
464f4a2713aSLionel Sambuc   std::string CmdPath;
465f4a2713aSLionel Sambuc   std::vector<std::string> Args;
466f4a2713aSLionel Sambuc   lexCommand(Message, ExecCommandLine, CmdPath, Args);
467f4a2713aSLionel Sambuc   if (CmdPath.empty())
468*0a6a1f1dSLionel Sambuc     return nullptr;
469f4a2713aSLionel Sambuc 
470f4a2713aSLionel Sambuc   return new CustomExecutor(CmdPath, Args);
471f4a2713aSLionel Sambuc }
472f4a2713aSLionel Sambuc 
473f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
474f4a2713aSLionel Sambuc // LLC Implementation of AbstractIntepreter interface
475f4a2713aSLionel Sambuc //
OutputCode(const std::string & Bitcode,std::string & OutputAsmFile,std::string & Error,unsigned Timeout,unsigned MemoryLimit)476f4a2713aSLionel Sambuc GCC::FileType LLC::OutputCode(const std::string &Bitcode,
477f4a2713aSLionel Sambuc                               std::string &OutputAsmFile, std::string &Error,
478f4a2713aSLionel Sambuc                               unsigned Timeout, unsigned MemoryLimit) {
479f4a2713aSLionel Sambuc   const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s");
480f4a2713aSLionel Sambuc 
481f4a2713aSLionel Sambuc   SmallString<128> UniqueFile;
482*0a6a1f1dSLionel Sambuc   std::error_code EC =
483f4a2713aSLionel Sambuc       sys::fs::createUniqueFile(Bitcode + "-%%%%%%%" + Suffix, UniqueFile);
484f4a2713aSLionel Sambuc   if (EC) {
485f4a2713aSLionel Sambuc     errs() << "Error making unique filename: " << EC.message() << "\n";
486f4a2713aSLionel Sambuc     exit(1);
487f4a2713aSLionel Sambuc   }
488f4a2713aSLionel Sambuc   OutputAsmFile = UniqueFile.str();
489f4a2713aSLionel Sambuc   std::vector<const char *> LLCArgs;
490f4a2713aSLionel Sambuc   LLCArgs.push_back(LLCPath.c_str());
491f4a2713aSLionel Sambuc 
492f4a2713aSLionel Sambuc   // Add any extra LLC args.
493f4a2713aSLionel Sambuc   for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
494f4a2713aSLionel Sambuc     LLCArgs.push_back(ToolArgs[i].c_str());
495f4a2713aSLionel Sambuc 
496f4a2713aSLionel Sambuc   LLCArgs.push_back("-o");
497f4a2713aSLionel Sambuc   LLCArgs.push_back(OutputAsmFile.c_str()); // Output to the Asm file
498f4a2713aSLionel Sambuc   LLCArgs.push_back(Bitcode.c_str());      // This is the input bitcode
499f4a2713aSLionel Sambuc 
500f4a2713aSLionel Sambuc   if (UseIntegratedAssembler)
501f4a2713aSLionel Sambuc     LLCArgs.push_back("-filetype=obj");
502f4a2713aSLionel Sambuc 
503*0a6a1f1dSLionel Sambuc   LLCArgs.push_back (nullptr);
504f4a2713aSLionel Sambuc 
505f4a2713aSLionel Sambuc   outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>");
506f4a2713aSLionel Sambuc   outs().flush();
507f4a2713aSLionel Sambuc   DEBUG(errs() << "\nAbout to run:\t";
508f4a2713aSLionel Sambuc         for (unsigned i = 0, e = LLCArgs.size()-1; i != e; ++i)
509f4a2713aSLionel Sambuc           errs() << " " << LLCArgs[i];
510f4a2713aSLionel Sambuc         errs() << "\n";
511f4a2713aSLionel Sambuc         );
512f4a2713aSLionel Sambuc   if (RunProgramWithTimeout(LLCPath, &LLCArgs[0],
513f4a2713aSLionel Sambuc                             "", "", "",
514f4a2713aSLionel Sambuc                             Timeout, MemoryLimit))
515f4a2713aSLionel Sambuc     Error = ProcessFailure(LLCPath, &LLCArgs[0],
516f4a2713aSLionel Sambuc                            Timeout, MemoryLimit);
517f4a2713aSLionel Sambuc   return UseIntegratedAssembler ? GCC::ObjectFile : GCC::AsmFile;
518f4a2713aSLionel Sambuc }
519f4a2713aSLionel Sambuc 
compileProgram(const std::string & Bitcode,std::string * Error,unsigned Timeout,unsigned MemoryLimit)520f4a2713aSLionel Sambuc void LLC::compileProgram(const std::string &Bitcode, std::string *Error,
521f4a2713aSLionel Sambuc                          unsigned Timeout, unsigned MemoryLimit) {
522f4a2713aSLionel Sambuc   std::string OutputAsmFile;
523f4a2713aSLionel Sambuc   OutputCode(Bitcode, OutputAsmFile, *Error, Timeout, MemoryLimit);
524f4a2713aSLionel Sambuc   sys::fs::remove(OutputAsmFile);
525f4a2713aSLionel Sambuc }
526f4a2713aSLionel Sambuc 
ExecuteProgram(const std::string & Bitcode,const std::vector<std::string> & Args,const std::string & InputFile,const std::string & OutputFile,std::string * Error,const std::vector<std::string> & ArgsForGCC,const std::vector<std::string> & SharedLibs,unsigned Timeout,unsigned MemoryLimit)527f4a2713aSLionel Sambuc int LLC::ExecuteProgram(const std::string &Bitcode,
528f4a2713aSLionel Sambuc                         const std::vector<std::string> &Args,
529f4a2713aSLionel Sambuc                         const std::string &InputFile,
530f4a2713aSLionel Sambuc                         const std::string &OutputFile,
531f4a2713aSLionel Sambuc                         std::string *Error,
532f4a2713aSLionel Sambuc                         const std::vector<std::string> &ArgsForGCC,
533f4a2713aSLionel Sambuc                         const std::vector<std::string> &SharedLibs,
534f4a2713aSLionel Sambuc                         unsigned Timeout,
535f4a2713aSLionel Sambuc                         unsigned MemoryLimit) {
536f4a2713aSLionel Sambuc 
537f4a2713aSLionel Sambuc   std::string OutputAsmFile;
538f4a2713aSLionel Sambuc   GCC::FileType FileKind = OutputCode(Bitcode, OutputAsmFile, *Error, Timeout,
539f4a2713aSLionel Sambuc                                       MemoryLimit);
540f4a2713aSLionel Sambuc   FileRemover OutFileRemover(OutputAsmFile, !SaveTemps);
541f4a2713aSLionel Sambuc 
542f4a2713aSLionel Sambuc   std::vector<std::string> GCCArgs(ArgsForGCC);
543f4a2713aSLionel Sambuc   GCCArgs.insert(GCCArgs.end(), SharedLibs.begin(), SharedLibs.end());
544f4a2713aSLionel Sambuc 
545f4a2713aSLionel Sambuc   // Assuming LLC worked, compile the result with GCC and run it.
546f4a2713aSLionel Sambuc   return gcc->ExecuteProgram(OutputAsmFile, Args, FileKind,
547f4a2713aSLionel Sambuc                              InputFile, OutputFile, Error, GCCArgs,
548f4a2713aSLionel Sambuc                              Timeout, MemoryLimit);
549f4a2713aSLionel Sambuc }
550f4a2713aSLionel Sambuc 
551f4a2713aSLionel Sambuc /// createLLC - Try to find the LLC executable
552f4a2713aSLionel Sambuc ///
createLLC(const char * Argv0,std::string & Message,const std::string & GCCBinary,const std::vector<std::string> * Args,const std::vector<std::string> * GCCArgs,bool UseIntegratedAssembler)553f4a2713aSLionel Sambuc LLC *AbstractInterpreter::createLLC(const char *Argv0,
554f4a2713aSLionel Sambuc                                     std::string &Message,
555f4a2713aSLionel Sambuc                                     const std::string &GCCBinary,
556f4a2713aSLionel Sambuc                                     const std::vector<std::string> *Args,
557f4a2713aSLionel Sambuc                                     const std::vector<std::string> *GCCArgs,
558f4a2713aSLionel Sambuc                                     bool UseIntegratedAssembler) {
559f4a2713aSLionel Sambuc   std::string LLCPath =
560f4a2713aSLionel Sambuc       PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t) & createLLC);
561f4a2713aSLionel Sambuc   if (LLCPath.empty()) {
562f4a2713aSLionel Sambuc     Message = "Cannot find `llc' in executable directory!\n";
563*0a6a1f1dSLionel Sambuc     return nullptr;
564f4a2713aSLionel Sambuc   }
565f4a2713aSLionel Sambuc 
566f4a2713aSLionel Sambuc   GCC *gcc = GCC::create(Message, GCCBinary, GCCArgs);
567f4a2713aSLionel Sambuc   if (!gcc) {
568f4a2713aSLionel Sambuc     errs() << Message << "\n";
569f4a2713aSLionel Sambuc     exit(1);
570f4a2713aSLionel Sambuc   }
571f4a2713aSLionel Sambuc   Message = "Found llc: " + LLCPath + "\n";
572f4a2713aSLionel Sambuc   return new LLC(LLCPath, gcc, Args, UseIntegratedAssembler);
573f4a2713aSLionel Sambuc }
574f4a2713aSLionel Sambuc 
575f4a2713aSLionel Sambuc //===---------------------------------------------------------------------===//
576f4a2713aSLionel Sambuc // JIT Implementation of AbstractIntepreter interface
577f4a2713aSLionel Sambuc //
578f4a2713aSLionel Sambuc namespace {
579f4a2713aSLionel Sambuc   class JIT : public AbstractInterpreter {
580f4a2713aSLionel Sambuc     std::string LLIPath;          // The path to the LLI executable
581f4a2713aSLionel Sambuc     std::vector<std::string> ToolArgs; // Args to pass to LLI
582f4a2713aSLionel Sambuc   public:
JIT(const std::string & Path,const std::vector<std::string> * Args)583f4a2713aSLionel Sambuc     JIT(const std::string &Path, const std::vector<std::string> *Args)
584f4a2713aSLionel Sambuc       : LLIPath(Path) {
585f4a2713aSLionel Sambuc       ToolArgs.clear ();
586f4a2713aSLionel Sambuc       if (Args) { ToolArgs = *Args; }
587f4a2713aSLionel Sambuc     }
588f4a2713aSLionel Sambuc 
589*0a6a1f1dSLionel Sambuc     int ExecuteProgram(const std::string &Bitcode,
590f4a2713aSLionel Sambuc                        const std::vector<std::string> &Args,
591f4a2713aSLionel Sambuc                        const std::string &InputFile,
592f4a2713aSLionel Sambuc                        const std::string &OutputFile,
593f4a2713aSLionel Sambuc                        std::string *Error,
594f4a2713aSLionel Sambuc                        const std::vector<std::string> &GCCArgs =
595f4a2713aSLionel Sambuc                          std::vector<std::string>(),
596f4a2713aSLionel Sambuc                        const std::vector<std::string> &SharedLibs =
597f4a2713aSLionel Sambuc                          std::vector<std::string>(),
598f4a2713aSLionel Sambuc                        unsigned Timeout = 0,
599*0a6a1f1dSLionel Sambuc                        unsigned MemoryLimit = 0) override;
600f4a2713aSLionel Sambuc   };
601f4a2713aSLionel Sambuc }
602f4a2713aSLionel Sambuc 
ExecuteProgram(const std::string & Bitcode,const std::vector<std::string> & Args,const std::string & InputFile,const std::string & OutputFile,std::string * Error,const std::vector<std::string> & GCCArgs,const std::vector<std::string> & SharedLibs,unsigned Timeout,unsigned MemoryLimit)603f4a2713aSLionel Sambuc int JIT::ExecuteProgram(const std::string &Bitcode,
604f4a2713aSLionel Sambuc                         const std::vector<std::string> &Args,
605f4a2713aSLionel Sambuc                         const std::string &InputFile,
606f4a2713aSLionel Sambuc                         const std::string &OutputFile,
607f4a2713aSLionel Sambuc                         std::string *Error,
608f4a2713aSLionel Sambuc                         const std::vector<std::string> &GCCArgs,
609f4a2713aSLionel Sambuc                         const std::vector<std::string> &SharedLibs,
610f4a2713aSLionel Sambuc                         unsigned Timeout,
611f4a2713aSLionel Sambuc                         unsigned MemoryLimit) {
612f4a2713aSLionel Sambuc   // Construct a vector of parameters, incorporating those from the command-line
613f4a2713aSLionel Sambuc   std::vector<const char*> JITArgs;
614f4a2713aSLionel Sambuc   JITArgs.push_back(LLIPath.c_str());
615f4a2713aSLionel Sambuc   JITArgs.push_back("-force-interpreter=false");
616f4a2713aSLionel Sambuc 
617f4a2713aSLionel Sambuc   // Add any extra LLI args.
618f4a2713aSLionel Sambuc   for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
619f4a2713aSLionel Sambuc     JITArgs.push_back(ToolArgs[i].c_str());
620f4a2713aSLionel Sambuc 
621f4a2713aSLionel Sambuc   for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
622f4a2713aSLionel Sambuc     JITArgs.push_back("-load");
623f4a2713aSLionel Sambuc     JITArgs.push_back(SharedLibs[i].c_str());
624f4a2713aSLionel Sambuc   }
625f4a2713aSLionel Sambuc   JITArgs.push_back(Bitcode.c_str());
626f4a2713aSLionel Sambuc   // Add optional parameters to the running program from Argv
627f4a2713aSLionel Sambuc   for (unsigned i=0, e = Args.size(); i != e; ++i)
628f4a2713aSLionel Sambuc     JITArgs.push_back(Args[i].c_str());
629*0a6a1f1dSLionel Sambuc   JITArgs.push_back(nullptr);
630f4a2713aSLionel Sambuc 
631f4a2713aSLionel Sambuc   outs() << "<jit>"; outs().flush();
632f4a2713aSLionel Sambuc   DEBUG(errs() << "\nAbout to run:\t";
633f4a2713aSLionel Sambuc         for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
634f4a2713aSLionel Sambuc           errs() << " " << JITArgs[i];
635f4a2713aSLionel Sambuc         errs() << "\n";
636f4a2713aSLionel Sambuc         );
637f4a2713aSLionel Sambuc   DEBUG(errs() << "\nSending output to " << OutputFile << "\n");
638f4a2713aSLionel Sambuc   return RunProgramWithTimeout(LLIPath, &JITArgs[0],
639f4a2713aSLionel Sambuc       InputFile, OutputFile, OutputFile,
640f4a2713aSLionel Sambuc       Timeout, MemoryLimit, Error);
641f4a2713aSLionel Sambuc }
642f4a2713aSLionel Sambuc 
643f4a2713aSLionel Sambuc /// createJIT - Try to find the LLI executable
644f4a2713aSLionel Sambuc ///
createJIT(const char * Argv0,std::string & Message,const std::vector<std::string> * Args)645f4a2713aSLionel Sambuc AbstractInterpreter *AbstractInterpreter::createJIT(const char *Argv0,
646f4a2713aSLionel Sambuc                    std::string &Message, const std::vector<std::string> *Args) {
647f4a2713aSLionel Sambuc   std::string LLIPath =
648f4a2713aSLionel Sambuc       PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t) & createJIT);
649f4a2713aSLionel Sambuc   if (!LLIPath.empty()) {
650f4a2713aSLionel Sambuc     Message = "Found lli: " + LLIPath + "\n";
651f4a2713aSLionel Sambuc     return new JIT(LLIPath, Args);
652f4a2713aSLionel Sambuc   }
653f4a2713aSLionel Sambuc 
654f4a2713aSLionel Sambuc   Message = "Cannot find `lli' in executable directory!\n";
655*0a6a1f1dSLionel Sambuc   return nullptr;
656f4a2713aSLionel Sambuc }
657f4a2713aSLionel Sambuc 
658f4a2713aSLionel Sambuc //===---------------------------------------------------------------------===//
659f4a2713aSLionel Sambuc // GCC abstraction
660f4a2713aSLionel Sambuc //
661f4a2713aSLionel Sambuc 
IsARMArchitecture(std::vector<const char * > Args)662f4a2713aSLionel Sambuc static bool IsARMArchitecture(std::vector<const char*> Args) {
663f4a2713aSLionel Sambuc   for (std::vector<const char*>::const_iterator
664f4a2713aSLionel Sambuc          I = Args.begin(), E = Args.end(); I != E; ++I) {
665f4a2713aSLionel Sambuc     if (StringRef(*I).equals_lower("-arch")) {
666f4a2713aSLionel Sambuc       ++I;
667f4a2713aSLionel Sambuc       if (I != E && StringRef(*I).startswith_lower("arm"))
668f4a2713aSLionel Sambuc         return true;
669f4a2713aSLionel Sambuc     }
670f4a2713aSLionel Sambuc   }
671f4a2713aSLionel Sambuc 
672f4a2713aSLionel Sambuc   return false;
673f4a2713aSLionel Sambuc }
674f4a2713aSLionel Sambuc 
ExecuteProgram(const std::string & ProgramFile,const std::vector<std::string> & Args,FileType fileType,const std::string & InputFile,const std::string & OutputFile,std::string * Error,const std::vector<std::string> & ArgsForGCC,unsigned Timeout,unsigned MemoryLimit)675f4a2713aSLionel Sambuc int GCC::ExecuteProgram(const std::string &ProgramFile,
676f4a2713aSLionel Sambuc                         const std::vector<std::string> &Args,
677f4a2713aSLionel Sambuc                         FileType fileType,
678f4a2713aSLionel Sambuc                         const std::string &InputFile,
679f4a2713aSLionel Sambuc                         const std::string &OutputFile,
680f4a2713aSLionel Sambuc                         std::string *Error,
681f4a2713aSLionel Sambuc                         const std::vector<std::string> &ArgsForGCC,
682f4a2713aSLionel Sambuc                         unsigned Timeout,
683f4a2713aSLionel Sambuc                         unsigned MemoryLimit) {
684f4a2713aSLionel Sambuc   std::vector<const char*> GCCArgs;
685f4a2713aSLionel Sambuc 
686f4a2713aSLionel Sambuc   GCCArgs.push_back(GCCPath.c_str());
687f4a2713aSLionel Sambuc 
688f4a2713aSLionel Sambuc   if (TargetTriple.getArch() == Triple::x86)
689f4a2713aSLionel Sambuc     GCCArgs.push_back("-m32");
690f4a2713aSLionel Sambuc 
691f4a2713aSLionel Sambuc   for (std::vector<std::string>::const_iterator
692f4a2713aSLionel Sambuc          I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I)
693f4a2713aSLionel Sambuc     GCCArgs.push_back(I->c_str());
694f4a2713aSLionel Sambuc 
695f4a2713aSLionel Sambuc   // Specify -x explicitly in case the extension is wonky
696f4a2713aSLionel Sambuc   if (fileType != ObjectFile) {
697f4a2713aSLionel Sambuc     GCCArgs.push_back("-x");
698f4a2713aSLionel Sambuc     if (fileType == CFile) {
699f4a2713aSLionel Sambuc       GCCArgs.push_back("c");
700f4a2713aSLionel Sambuc       GCCArgs.push_back("-fno-strict-aliasing");
701f4a2713aSLionel Sambuc     } else {
702f4a2713aSLionel Sambuc       GCCArgs.push_back("assembler");
703f4a2713aSLionel Sambuc 
704f4a2713aSLionel Sambuc       // For ARM architectures we don't want this flag. bugpoint isn't
705f4a2713aSLionel Sambuc       // explicitly told what architecture it is working on, so we get
706f4a2713aSLionel Sambuc       // it from gcc flags
707f4a2713aSLionel Sambuc       if (TargetTriple.isOSDarwin() && !IsARMArchitecture(GCCArgs))
708f4a2713aSLionel Sambuc         GCCArgs.push_back("-force_cpusubtype_ALL");
709f4a2713aSLionel Sambuc     }
710f4a2713aSLionel Sambuc   }
711f4a2713aSLionel Sambuc 
712f4a2713aSLionel Sambuc   GCCArgs.push_back(ProgramFile.c_str());  // Specify the input filename.
713f4a2713aSLionel Sambuc 
714f4a2713aSLionel Sambuc   GCCArgs.push_back("-x");
715f4a2713aSLionel Sambuc   GCCArgs.push_back("none");
716f4a2713aSLionel Sambuc   GCCArgs.push_back("-o");
717f4a2713aSLionel Sambuc 
718f4a2713aSLionel Sambuc   SmallString<128> OutputBinary;
719*0a6a1f1dSLionel Sambuc   std::error_code EC =
720f4a2713aSLionel Sambuc       sys::fs::createUniqueFile(ProgramFile + "-%%%%%%%.gcc.exe", OutputBinary);
721f4a2713aSLionel Sambuc   if (EC) {
722f4a2713aSLionel Sambuc     errs() << "Error making unique filename: " << EC.message() << "\n";
723f4a2713aSLionel Sambuc     exit(1);
724f4a2713aSLionel Sambuc   }
725f4a2713aSLionel Sambuc   GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
726f4a2713aSLionel Sambuc 
727f4a2713aSLionel Sambuc   // Add any arguments intended for GCC. We locate them here because this is
728f4a2713aSLionel Sambuc   // most likely -L and -l options that need to come before other libraries but
729f4a2713aSLionel Sambuc   // after the source. Other options won't be sensitive to placement on the
730f4a2713aSLionel Sambuc   // command line, so this should be safe.
731f4a2713aSLionel Sambuc   for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
732f4a2713aSLionel Sambuc     GCCArgs.push_back(ArgsForGCC[i].c_str());
733f4a2713aSLionel Sambuc 
734f4a2713aSLionel Sambuc   GCCArgs.push_back("-lm");                // Hard-code the math library...
735f4a2713aSLionel Sambuc   GCCArgs.push_back("-O2");                // Optimize the program a bit...
736f4a2713aSLionel Sambuc #if defined (HAVE_LINK_R)
737f4a2713aSLionel Sambuc   GCCArgs.push_back("-Wl,-R.");            // Search this dir for .so files
738f4a2713aSLionel Sambuc #endif
739f4a2713aSLionel Sambuc   if (TargetTriple.getArch() == Triple::sparc)
740f4a2713aSLionel Sambuc     GCCArgs.push_back("-mcpu=v9");
741*0a6a1f1dSLionel Sambuc   GCCArgs.push_back(nullptr);                    // NULL terminator
742f4a2713aSLionel Sambuc 
743f4a2713aSLionel Sambuc   outs() << "<gcc>"; outs().flush();
744f4a2713aSLionel Sambuc   DEBUG(errs() << "\nAbout to run:\t";
745f4a2713aSLionel Sambuc         for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i)
746f4a2713aSLionel Sambuc           errs() << " " << GCCArgs[i];
747f4a2713aSLionel Sambuc         errs() << "\n";
748f4a2713aSLionel Sambuc         );
749f4a2713aSLionel Sambuc   if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "", "", "")) {
750f4a2713aSLionel Sambuc     *Error = ProcessFailure(GCCPath, &GCCArgs[0]);
751f4a2713aSLionel Sambuc     return -1;
752f4a2713aSLionel Sambuc   }
753f4a2713aSLionel Sambuc 
754f4a2713aSLionel Sambuc   std::vector<const char*> ProgramArgs;
755f4a2713aSLionel Sambuc 
756f4a2713aSLionel Sambuc   // Declared here so that the destructor only runs after
757f4a2713aSLionel Sambuc   // ProgramArgs is used.
758f4a2713aSLionel Sambuc   std::string Exec;
759f4a2713aSLionel Sambuc 
760f4a2713aSLionel Sambuc   if (RemoteClientPath.empty())
761f4a2713aSLionel Sambuc     ProgramArgs.push_back(OutputBinary.c_str());
762f4a2713aSLionel Sambuc   else {
763f4a2713aSLionel Sambuc     ProgramArgs.push_back(RemoteClientPath.c_str());
764f4a2713aSLionel Sambuc     ProgramArgs.push_back(RemoteHost.c_str());
765f4a2713aSLionel Sambuc     if (!RemoteUser.empty()) {
766f4a2713aSLionel Sambuc       ProgramArgs.push_back("-l");
767f4a2713aSLionel Sambuc       ProgramArgs.push_back(RemoteUser.c_str());
768f4a2713aSLionel Sambuc     }
769f4a2713aSLionel Sambuc     if (!RemotePort.empty()) {
770f4a2713aSLionel Sambuc       ProgramArgs.push_back("-p");
771f4a2713aSLionel Sambuc       ProgramArgs.push_back(RemotePort.c_str());
772f4a2713aSLionel Sambuc     }
773f4a2713aSLionel Sambuc     if (!RemoteExtra.empty()) {
774f4a2713aSLionel Sambuc       ProgramArgs.push_back(RemoteExtra.c_str());
775f4a2713aSLionel Sambuc     }
776f4a2713aSLionel Sambuc 
777f4a2713aSLionel Sambuc     // Full path to the binary. We need to cd to the exec directory because
778f4a2713aSLionel Sambuc     // there is a dylib there that the exec expects to find in the CWD
779f4a2713aSLionel Sambuc     char* env_pwd = getenv("PWD");
780f4a2713aSLionel Sambuc     Exec = "cd ";
781f4a2713aSLionel Sambuc     Exec += env_pwd;
782f4a2713aSLionel Sambuc     Exec += "; ./";
783f4a2713aSLionel Sambuc     Exec += OutputBinary.c_str();
784f4a2713aSLionel Sambuc     ProgramArgs.push_back(Exec.c_str());
785f4a2713aSLionel Sambuc   }
786f4a2713aSLionel Sambuc 
787f4a2713aSLionel Sambuc   // Add optional parameters to the running program from Argv
788f4a2713aSLionel Sambuc   for (unsigned i = 0, e = Args.size(); i != e; ++i)
789f4a2713aSLionel Sambuc     ProgramArgs.push_back(Args[i].c_str());
790*0a6a1f1dSLionel Sambuc   ProgramArgs.push_back(nullptr);                // NULL terminator
791f4a2713aSLionel Sambuc 
792f4a2713aSLionel Sambuc   // Now that we have a binary, run it!
793f4a2713aSLionel Sambuc   outs() << "<program>"; outs().flush();
794f4a2713aSLionel Sambuc   DEBUG(errs() << "\nAbout to run:\t";
795f4a2713aSLionel Sambuc         for (unsigned i = 0, e = ProgramArgs.size()-1; i != e; ++i)
796f4a2713aSLionel Sambuc           errs() << " " << ProgramArgs[i];
797f4a2713aSLionel Sambuc         errs() << "\n";
798f4a2713aSLionel Sambuc         );
799f4a2713aSLionel Sambuc 
800f4a2713aSLionel Sambuc   FileRemover OutputBinaryRemover(OutputBinary.str(), !SaveTemps);
801f4a2713aSLionel Sambuc 
802f4a2713aSLionel Sambuc   if (RemoteClientPath.empty()) {
803f4a2713aSLionel Sambuc     DEBUG(errs() << "<run locally>");
804f4a2713aSLionel Sambuc     int ExitCode = RunProgramWithTimeout(OutputBinary.str(), &ProgramArgs[0],
805f4a2713aSLionel Sambuc                                          InputFile, OutputFile, OutputFile,
806f4a2713aSLionel Sambuc                                          Timeout, MemoryLimit, Error);
807f4a2713aSLionel Sambuc     // Treat a signal (usually SIGSEGV) or timeout as part of the program output
808f4a2713aSLionel Sambuc     // so that crash-causing miscompilation is handled seamlessly.
809f4a2713aSLionel Sambuc     if (ExitCode < -1) {
810f4a2713aSLionel Sambuc       std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
811f4a2713aSLionel Sambuc       outFile << *Error << '\n';
812f4a2713aSLionel Sambuc       outFile.close();
813f4a2713aSLionel Sambuc       Error->clear();
814f4a2713aSLionel Sambuc     }
815f4a2713aSLionel Sambuc     return ExitCode;
816f4a2713aSLionel Sambuc   } else {
817f4a2713aSLionel Sambuc     outs() << "<run remotely>"; outs().flush();
818f4a2713aSLionel Sambuc     return RunProgramRemotelyWithTimeout(RemoteClientPath,
819f4a2713aSLionel Sambuc         &ProgramArgs[0], InputFile, OutputFile,
820f4a2713aSLionel Sambuc         OutputFile, Timeout, MemoryLimit);
821f4a2713aSLionel Sambuc   }
822f4a2713aSLionel Sambuc }
823f4a2713aSLionel Sambuc 
MakeSharedObject(const std::string & InputFile,FileType fileType,std::string & OutputFile,const std::vector<std::string> & ArgsForGCC,std::string & Error)824f4a2713aSLionel Sambuc int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
825f4a2713aSLionel Sambuc                           std::string &OutputFile,
826f4a2713aSLionel Sambuc                           const std::vector<std::string> &ArgsForGCC,
827f4a2713aSLionel Sambuc                           std::string &Error) {
828f4a2713aSLionel Sambuc   SmallString<128> UniqueFilename;
829*0a6a1f1dSLionel Sambuc   std::error_code EC = sys::fs::createUniqueFile(
830f4a2713aSLionel Sambuc       InputFile + "-%%%%%%%" + LTDL_SHLIB_EXT, UniqueFilename);
831f4a2713aSLionel Sambuc   if (EC) {
832f4a2713aSLionel Sambuc     errs() << "Error making unique filename: " << EC.message() << "\n";
833f4a2713aSLionel Sambuc     exit(1);
834f4a2713aSLionel Sambuc   }
835f4a2713aSLionel Sambuc   OutputFile = UniqueFilename.str();
836f4a2713aSLionel Sambuc 
837f4a2713aSLionel Sambuc   std::vector<const char*> GCCArgs;
838f4a2713aSLionel Sambuc 
839f4a2713aSLionel Sambuc   GCCArgs.push_back(GCCPath.c_str());
840f4a2713aSLionel Sambuc 
841f4a2713aSLionel Sambuc   if (TargetTriple.getArch() == Triple::x86)
842f4a2713aSLionel Sambuc     GCCArgs.push_back("-m32");
843f4a2713aSLionel Sambuc 
844f4a2713aSLionel Sambuc   for (std::vector<std::string>::const_iterator
845f4a2713aSLionel Sambuc          I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I)
846f4a2713aSLionel Sambuc     GCCArgs.push_back(I->c_str());
847f4a2713aSLionel Sambuc 
848f4a2713aSLionel Sambuc   // Compile the C/asm file into a shared object
849f4a2713aSLionel Sambuc   if (fileType != ObjectFile) {
850f4a2713aSLionel Sambuc     GCCArgs.push_back("-x");
851f4a2713aSLionel Sambuc     GCCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
852f4a2713aSLionel Sambuc   }
853f4a2713aSLionel Sambuc   GCCArgs.push_back("-fno-strict-aliasing");
854f4a2713aSLionel Sambuc   GCCArgs.push_back(InputFile.c_str());   // Specify the input filename.
855f4a2713aSLionel Sambuc   GCCArgs.push_back("-x");
856f4a2713aSLionel Sambuc   GCCArgs.push_back("none");
857f4a2713aSLionel Sambuc   if (TargetTriple.getArch() == Triple::sparc)
858f4a2713aSLionel Sambuc     GCCArgs.push_back("-G");       // Compile a shared library, `-G' for Sparc
859f4a2713aSLionel Sambuc   else if (TargetTriple.isOSDarwin()) {
860f4a2713aSLionel Sambuc     // link all source files into a single module in data segment, rather than
861f4a2713aSLionel Sambuc     // generating blocks. dynamic_lookup requires that you set
862f4a2713aSLionel Sambuc     // MACOSX_DEPLOYMENT_TARGET=10.3 in your env.  FIXME: it would be better for
863f4a2713aSLionel Sambuc     // bugpoint to just pass that in the environment of GCC.
864f4a2713aSLionel Sambuc     GCCArgs.push_back("-single_module");
865f4a2713aSLionel Sambuc     GCCArgs.push_back("-dynamiclib");   // `-dynamiclib' for MacOS X/PowerPC
866f4a2713aSLionel Sambuc     GCCArgs.push_back("-undefined");
867f4a2713aSLionel Sambuc     GCCArgs.push_back("dynamic_lookup");
868f4a2713aSLionel Sambuc   } else
869f4a2713aSLionel Sambuc     GCCArgs.push_back("-shared");  // `-shared' for Linux/X86, maybe others
870f4a2713aSLionel Sambuc 
871f4a2713aSLionel Sambuc   if (TargetTriple.getArch() == Triple::x86_64)
872f4a2713aSLionel Sambuc     GCCArgs.push_back("-fPIC");   // Requires shared objs to contain PIC
873f4a2713aSLionel Sambuc 
874f4a2713aSLionel Sambuc   if (TargetTriple.getArch() == Triple::sparc)
875f4a2713aSLionel Sambuc     GCCArgs.push_back("-mcpu=v9");
876f4a2713aSLionel Sambuc 
877f4a2713aSLionel Sambuc   GCCArgs.push_back("-o");
878f4a2713aSLionel Sambuc   GCCArgs.push_back(OutputFile.c_str()); // Output to the right filename.
879f4a2713aSLionel Sambuc   GCCArgs.push_back("-O2");              // Optimize the program a bit.
880f4a2713aSLionel Sambuc 
881f4a2713aSLionel Sambuc 
882f4a2713aSLionel Sambuc 
883f4a2713aSLionel Sambuc   // Add any arguments intended for GCC. We locate them here because this is
884f4a2713aSLionel Sambuc   // most likely -L and -l options that need to come before other libraries but
885f4a2713aSLionel Sambuc   // after the source. Other options won't be sensitive to placement on the
886f4a2713aSLionel Sambuc   // command line, so this should be safe.
887f4a2713aSLionel Sambuc   for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
888f4a2713aSLionel Sambuc     GCCArgs.push_back(ArgsForGCC[i].c_str());
889*0a6a1f1dSLionel Sambuc   GCCArgs.push_back(nullptr);                    // NULL terminator
890f4a2713aSLionel Sambuc 
891f4a2713aSLionel Sambuc 
892f4a2713aSLionel Sambuc 
893f4a2713aSLionel Sambuc   outs() << "<gcc>"; outs().flush();
894f4a2713aSLionel Sambuc   DEBUG(errs() << "\nAbout to run:\t";
895f4a2713aSLionel Sambuc         for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i)
896f4a2713aSLionel Sambuc           errs() << " " << GCCArgs[i];
897f4a2713aSLionel Sambuc         errs() << "\n";
898f4a2713aSLionel Sambuc         );
899f4a2713aSLionel Sambuc   if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "", "", "")) {
900f4a2713aSLionel Sambuc     Error = ProcessFailure(GCCPath, &GCCArgs[0]);
901f4a2713aSLionel Sambuc     return 1;
902f4a2713aSLionel Sambuc   }
903f4a2713aSLionel Sambuc   return 0;
904f4a2713aSLionel Sambuc }
905f4a2713aSLionel Sambuc 
906f4a2713aSLionel Sambuc /// create - Try to find the `gcc' executable
907f4a2713aSLionel Sambuc ///
create(std::string & Message,const std::string & GCCBinary,const std::vector<std::string> * Args)908f4a2713aSLionel Sambuc GCC *GCC::create(std::string &Message,
909f4a2713aSLionel Sambuc                  const std::string &GCCBinary,
910f4a2713aSLionel Sambuc                  const std::vector<std::string> *Args) {
911*0a6a1f1dSLionel Sambuc   auto GCCPath = sys::findProgramByName(GCCBinary);
912*0a6a1f1dSLionel Sambuc   if (!GCCPath) {
913*0a6a1f1dSLionel Sambuc     Message = "Cannot find `" + GCCBinary + "' in PATH: " +
914*0a6a1f1dSLionel Sambuc               GCCPath.getError().message() + "\n";
915*0a6a1f1dSLionel Sambuc     return nullptr;
916f4a2713aSLionel Sambuc   }
917f4a2713aSLionel Sambuc 
918f4a2713aSLionel Sambuc   std::string RemoteClientPath;
919*0a6a1f1dSLionel Sambuc   if (!RemoteClient.empty()) {
920*0a6a1f1dSLionel Sambuc     auto Path = sys::findProgramByName(RemoteClient);
921*0a6a1f1dSLionel Sambuc     if (!Path) {
922*0a6a1f1dSLionel Sambuc       Message = "Cannot find `" + RemoteClient + "' in PATH: " +
923*0a6a1f1dSLionel Sambuc                 Path.getError().message() + "\n";
924*0a6a1f1dSLionel Sambuc       return nullptr;
925*0a6a1f1dSLionel Sambuc     }
926*0a6a1f1dSLionel Sambuc     RemoteClientPath = *Path;
927*0a6a1f1dSLionel Sambuc   }
928f4a2713aSLionel Sambuc 
929*0a6a1f1dSLionel Sambuc   Message = "Found gcc: " + *GCCPath + "\n";
930*0a6a1f1dSLionel Sambuc   return new GCC(*GCCPath, RemoteClientPath, Args);
931f4a2713aSLionel Sambuc }
932