1 //===-- ToolRunner.cpp ----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the interfaces described in the ToolRunner.h file.
12 //===----------------------------------------------------------------------===//
14 #include "ToolRunner.h"
15 #include "llvm/Config/config.h" // for HAVE_LINK_R
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/FileUtilities.h"
20 #include "llvm/Support/Program.h"
21 #include "llvm/Support/raw_ostream.h"
27 #define DEBUG_TYPE "toolrunner"
31 SaveTemps("save-temps", cl::init(false), cl::desc("Save temporary files"));
36 RemoteClient("remote-client",
37 cl::desc("Remote execution client (rsh/ssh)"));
40 RemoteHost("remote-host",
41 cl::desc("Remote execution (rsh/ssh) host"));
44 RemotePort("remote-port",
45 cl::desc("Remote execution (rsh/ssh) port"));
48 RemoteUser("remote-user",
49 cl::desc("Remote execution (rsh/ssh) user id"));
52 RemoteExtra("remote-extra-options",
53 cl::desc("Remote execution (rsh/ssh) extra options"));
56 /// RunProgramWithTimeout - This function provides an alternate interface
57 /// to the sys::Program::ExecuteAndWait interface.
58 /// @see sys::Program::ExecuteAndWait
59 static int RunProgramWithTimeout(StringRef ProgramPath,
64 unsigned NumSeconds = 0,
65 unsigned MemoryLimit = 0,
66 std::string *ErrMsg = nullptr) {
67 const StringRef *Redirects[3] = { &StdInFile, &StdOutFile, &StdErrFile };
68 return sys::ExecuteAndWait(ProgramPath, Args, nullptr, Redirects,
69 NumSeconds, MemoryLimit, ErrMsg);
72 /// RunProgramRemotelyWithTimeout - This function runs the given program
73 /// remotely using the given remote client and the sys::Program::ExecuteAndWait.
74 /// Returns the remote program exit code or reports a remote client error if it
75 /// fails. Remote client is required to return 255 if it failed or program exit
77 /// @see sys::Program::ExecuteAndWait
78 static int RunProgramRemotelyWithTimeout(StringRef RemoteClientPath,
83 unsigned NumSeconds = 0,
84 unsigned MemoryLimit = 0) {
85 const StringRef *Redirects[3] = { &StdInFile, &StdOutFile, &StdErrFile };
87 // Run the program remotely with the remote client
88 int ReturnCode = sys::ExecuteAndWait(RemoteClientPath, Args, nullptr,
89 Redirects, NumSeconds, MemoryLimit);
91 // Has the remote client fail?
92 if (255 == ReturnCode) {
93 std::ostringstream OS;
94 OS << "\nError running remote client:\n ";
95 for (const char **Arg = Args; *Arg; ++Arg)
99 // The error message is in the output file, let's print it out from there.
100 std::string StdOutFileName = StdOutFile.str();
101 std::ifstream ErrorFile(StdOutFileName.c_str());
103 std::copy(std::istreambuf_iterator<char>(ErrorFile),
104 std::istreambuf_iterator<char>(),
105 std::ostreambuf_iterator<char>(OS));
115 static std::string ProcessFailure(StringRef ProgPath, const char** Args,
116 unsigned Timeout = 0,
117 unsigned MemoryLimit = 0) {
118 std::ostringstream OS;
119 OS << "\nError running tool:\n ";
120 for (const char **Arg = Args; *Arg; ++Arg)
124 // Rerun the compiler, capturing any error messages to print them.
125 SmallString<128> ErrorFilename;
126 std::error_code EC = sys::fs::createTemporaryFile(
127 "bugpoint.program_error_messages", "", ErrorFilename);
129 errs() << "Error making unique filename: " << EC.message() << "\n";
133 RunProgramWithTimeout(ProgPath, Args, "", ErrorFilename.str(),
134 ErrorFilename.str(), Timeout, MemoryLimit);
135 // FIXME: check return code ?
137 // Print out the error messages generated by CC if possible...
138 std::ifstream ErrorFile(ErrorFilename.c_str());
140 std::copy(std::istreambuf_iterator<char>(ErrorFile),
141 std::istreambuf_iterator<char>(),
142 std::ostreambuf_iterator<char>(OS));
146 sys::fs::remove(ErrorFilename.c_str());
150 //===---------------------------------------------------------------------===//
151 // LLI Implementation of AbstractIntepreter interface
154 class LLI : public AbstractInterpreter {
155 std::string LLIPath; // The path to the LLI executable
156 std::vector<std::string> ToolArgs; // Args to pass to LLI
158 LLI(const std::string &Path, const std::vector<std::string> *Args)
161 if (Args) { ToolArgs = *Args; }
164 int ExecuteProgram(const std::string &Bitcode,
165 const std::vector<std::string> &Args,
166 const std::string &InputFile,
167 const std::string &OutputFile,
169 const std::vector<std::string> &CCArgs,
170 const std::vector<std::string> &SharedLibs =
171 std::vector<std::string>(),
172 unsigned Timeout = 0,
173 unsigned MemoryLimit = 0) override;
177 int LLI::ExecuteProgram(const std::string &Bitcode,
178 const std::vector<std::string> &Args,
179 const std::string &InputFile,
180 const std::string &OutputFile,
182 const std::vector<std::string> &CCArgs,
183 const std::vector<std::string> &SharedLibs,
185 unsigned MemoryLimit) {
186 std::vector<const char*> LLIArgs;
187 LLIArgs.push_back(LLIPath.c_str());
188 LLIArgs.push_back("-force-interpreter=true");
190 for (std::vector<std::string>::const_iterator i = SharedLibs.begin(),
191 e = SharedLibs.end(); i != e; ++i) {
192 LLIArgs.push_back("-load");
193 LLIArgs.push_back((*i).c_str());
196 // Add any extra LLI args.
197 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
198 LLIArgs.push_back(ToolArgs[i].c_str());
200 LLIArgs.push_back(Bitcode.c_str());
201 // Add optional parameters to the running program from Argv
202 for (unsigned i=0, e = Args.size(); i != e; ++i)
203 LLIArgs.push_back(Args[i].c_str());
204 LLIArgs.push_back(nullptr);
206 outs() << "<lli>"; outs().flush();
207 DEBUG(errs() << "\nAbout to run:\t";
208 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
209 errs() << " " << LLIArgs[i];
212 return RunProgramWithTimeout(LLIPath, &LLIArgs[0],
213 InputFile, OutputFile, OutputFile,
214 Timeout, MemoryLimit, Error);
217 void AbstractInterpreter::anchor() { }
219 #if defined(LLVM_ON_UNIX)
220 const char EXESuffix[] = "";
221 #elif defined (LLVM_ON_WIN32)
222 const char EXESuffix[] = "exe";
225 /// Prepend the path to the program being executed
226 /// to \p ExeName, given the value of argv[0] and the address of main()
227 /// itself. This allows us to find another LLVM tool if it is built in the same
228 /// directory. An empty string is returned on error; note that this function
229 /// just mainpulates the path and doesn't check for executability.
230 /// @brief Find a named executable.
231 static std::string PrependMainExecutablePath(const std::string &ExeName,
234 // Check the directory that the calling program is in. We can do
235 // this if ProgramPath contains at least one / character, indicating that it
236 // is a relative path to the executable itself.
237 std::string Main = sys::fs::getMainExecutable(Argv0, MainAddr);
238 StringRef Result = sys::path::parent_path(Main);
240 if (!Result.empty()) {
241 SmallString<128> Storage = Result;
242 sys::path::append(Storage, ExeName);
243 sys::path::replace_extension(Storage, EXESuffix);
244 return Storage.str();
250 // LLI create method - Try to find the LLI executable
251 AbstractInterpreter *AbstractInterpreter::createLLI(const char *Argv0,
252 std::string &Message,
253 const std::vector<std::string> *ToolArgs) {
254 std::string LLIPath =
255 PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t) & createLLI);
256 if (!LLIPath.empty()) {
257 Message = "Found lli: " + LLIPath + "\n";
258 return new LLI(LLIPath, ToolArgs);
261 Message = "Cannot find `lli' in executable directory!\n";
265 //===---------------------------------------------------------------------===//
266 // Custom compiler command implementation of AbstractIntepreter interface
268 // Allows using a custom command for compiling the bitcode, thus allows, for
269 // example, to compile a bitcode fragment without linking or executing, then
270 // using a custom wrapper script to check for compiler errors.
272 class CustomCompiler : public AbstractInterpreter {
273 std::string CompilerCommand;
274 std::vector<std::string> CompilerArgs;
276 CustomCompiler(const std::string &CompilerCmd,
277 std::vector<std::string> CompArgs)
278 : CompilerCommand(CompilerCmd), CompilerArgs(std::move(CompArgs)) {}
280 void compileProgram(const std::string &Bitcode,
282 unsigned Timeout = 0,
283 unsigned MemoryLimit = 0) override;
285 int ExecuteProgram(const std::string &Bitcode,
286 const std::vector<std::string> &Args,
287 const std::string &InputFile,
288 const std::string &OutputFile,
290 const std::vector<std::string> &CCArgs =
291 std::vector<std::string>(),
292 const std::vector<std::string> &SharedLibs =
293 std::vector<std::string>(),
294 unsigned Timeout = 0,
295 unsigned MemoryLimit = 0) override {
296 *Error = "Execution not supported with -compile-custom";
302 void CustomCompiler::compileProgram(const std::string &Bitcode,
305 unsigned MemoryLimit) {
307 std::vector<const char*> ProgramArgs;
308 ProgramArgs.push_back(CompilerCommand.c_str());
310 for (std::size_t i = 0; i < CompilerArgs.size(); ++i)
311 ProgramArgs.push_back(CompilerArgs.at(i).c_str());
312 ProgramArgs.push_back(Bitcode.c_str());
313 ProgramArgs.push_back(nullptr);
315 // Add optional parameters to the running program from Argv
316 for (unsigned i = 0, e = CompilerArgs.size(); i != e; ++i)
317 ProgramArgs.push_back(CompilerArgs[i].c_str());
319 if (RunProgramWithTimeout(CompilerCommand, &ProgramArgs[0],
321 Timeout, MemoryLimit, Error))
322 *Error = ProcessFailure(CompilerCommand, &ProgramArgs[0],
323 Timeout, MemoryLimit);
326 //===---------------------------------------------------------------------===//
327 // Custom execution command implementation of AbstractIntepreter interface
329 // Allows using a custom command for executing the bitcode, thus allows,
330 // for example, to invoke a cross compiler for code generation followed by
331 // a simulator that executes the generated binary.
333 class CustomExecutor : public AbstractInterpreter {
334 std::string ExecutionCommand;
335 std::vector<std::string> ExecutorArgs;
337 CustomExecutor(const std::string &ExecutionCmd,
338 std::vector<std::string> ExecArgs)
339 : ExecutionCommand(ExecutionCmd), ExecutorArgs(std::move(ExecArgs)) {}
341 int ExecuteProgram(const std::string &Bitcode,
342 const std::vector<std::string> &Args,
343 const std::string &InputFile,
344 const std::string &OutputFile,
346 const std::vector<std::string> &CCArgs,
347 const std::vector<std::string> &SharedLibs =
348 std::vector<std::string>(),
349 unsigned Timeout = 0,
350 unsigned MemoryLimit = 0) override;
354 int CustomExecutor::ExecuteProgram(const std::string &Bitcode,
355 const std::vector<std::string> &Args,
356 const std::string &InputFile,
357 const std::string &OutputFile,
359 const std::vector<std::string> &CCArgs,
360 const std::vector<std::string> &SharedLibs,
362 unsigned MemoryLimit) {
364 std::vector<const char*> ProgramArgs;
365 ProgramArgs.push_back(ExecutionCommand.c_str());
367 for (std::size_t i = 0; i < ExecutorArgs.size(); ++i)
368 ProgramArgs.push_back(ExecutorArgs.at(i).c_str());
369 ProgramArgs.push_back(Bitcode.c_str());
370 ProgramArgs.push_back(nullptr);
372 // Add optional parameters to the running program from Argv
373 for (unsigned i = 0, e = Args.size(); i != e; ++i)
374 ProgramArgs.push_back(Args[i].c_str());
376 return RunProgramWithTimeout(
378 &ProgramArgs[0], InputFile, OutputFile,
379 OutputFile, Timeout, MemoryLimit, Error);
382 // Tokenize the CommandLine to the command and the args to allow
383 // defining a full command line as the command instead of just the
384 // executed program. We cannot just pass the whole string after the command
385 // as a single argument because then program sees only a single
386 // command line argument (with spaces in it: "foo bar" instead
387 // of "foo" and "bar").
389 // code borrowed from:
390 // http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
391 static void lexCommand(std::string &Message, const std::string &CommandLine,
392 std::string &CmdPath, std::vector<std::string> &Args) {
394 std::string Command = "";
395 std::string delimiters = " ";
397 std::string::size_type lastPos = CommandLine.find_first_not_of(delimiters, 0);
398 std::string::size_type pos = CommandLine.find_first_of(delimiters, lastPos);
400 while (std::string::npos != pos || std::string::npos != lastPos) {
401 std::string token = CommandLine.substr(lastPos, pos - lastPos);
405 Args.push_back(token);
406 // Skip delimiters. Note the "not_of"
407 lastPos = CommandLine.find_first_not_of(delimiters, pos);
408 // Find next "non-delimiter"
409 pos = CommandLine.find_first_of(delimiters, lastPos);
412 auto Path = sys::findProgramByName(Command);
415 std::string("Cannot find '") + Command +
416 "' in PATH: " + Path.getError().message() + "\n";
421 Message = "Found command in: " + CmdPath + "\n";
424 // Custom execution environment create method, takes the execution command
426 AbstractInterpreter *AbstractInterpreter::createCustomCompiler(
427 std::string &Message,
428 const std::string &CompileCommandLine) {
431 std::vector<std::string> Args;
432 lexCommand(Message, CompileCommandLine, CmdPath, Args);
436 return new CustomCompiler(CmdPath, Args);
439 // Custom execution environment create method, takes the execution command
441 AbstractInterpreter *AbstractInterpreter::createCustomExecutor(
442 std::string &Message,
443 const std::string &ExecCommandLine) {
447 std::vector<std::string> Args;
448 lexCommand(Message, ExecCommandLine, CmdPath, Args);
452 return new CustomExecutor(CmdPath, Args);
455 //===----------------------------------------------------------------------===//
456 // LLC Implementation of AbstractIntepreter interface
458 CC::FileType LLC::OutputCode(const std::string &Bitcode,
459 std::string &OutputAsmFile, std::string &Error,
460 unsigned Timeout, unsigned MemoryLimit) {
461 const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s");
463 SmallString<128> UniqueFile;
465 sys::fs::createUniqueFile(Bitcode + "-%%%%%%%" + Suffix, UniqueFile);
467 errs() << "Error making unique filename: " << EC.message() << "\n";
470 OutputAsmFile = UniqueFile.str();
471 std::vector<const char *> LLCArgs;
472 LLCArgs.push_back(LLCPath.c_str());
474 // Add any extra LLC args.
475 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
476 LLCArgs.push_back(ToolArgs[i].c_str());
478 LLCArgs.push_back("-o");
479 LLCArgs.push_back(OutputAsmFile.c_str()); // Output to the Asm file
480 LLCArgs.push_back(Bitcode.c_str()); // This is the input bitcode
482 if (UseIntegratedAssembler)
483 LLCArgs.push_back("-filetype=obj");
485 LLCArgs.push_back (nullptr);
487 outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>");
489 DEBUG(errs() << "\nAbout to run:\t";
490 for (unsigned i = 0, e = LLCArgs.size()-1; i != e; ++i)
491 errs() << " " << LLCArgs[i];
494 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0],
496 Timeout, MemoryLimit))
497 Error = ProcessFailure(LLCPath, &LLCArgs[0],
498 Timeout, MemoryLimit);
499 return UseIntegratedAssembler ? CC::ObjectFile : CC::AsmFile;
502 void LLC::compileProgram(const std::string &Bitcode, std::string *Error,
503 unsigned Timeout, unsigned MemoryLimit) {
504 std::string OutputAsmFile;
505 OutputCode(Bitcode, OutputAsmFile, *Error, Timeout, MemoryLimit);
506 sys::fs::remove(OutputAsmFile);
509 int LLC::ExecuteProgram(const std::string &Bitcode,
510 const std::vector<std::string> &Args,
511 const std::string &InputFile,
512 const std::string &OutputFile,
514 const std::vector<std::string> &ArgsForCC,
515 const std::vector<std::string> &SharedLibs,
517 unsigned MemoryLimit) {
519 std::string OutputAsmFile;
520 CC::FileType FileKind = OutputCode(Bitcode, OutputAsmFile, *Error, Timeout,
522 FileRemover OutFileRemover(OutputAsmFile, !SaveTemps);
524 std::vector<std::string> CCArgs(ArgsForCC);
525 CCArgs.insert(CCArgs.end(), SharedLibs.begin(), SharedLibs.end());
527 // Assuming LLC worked, compile the result with CC and run it.
528 return cc->ExecuteProgram(OutputAsmFile, Args, FileKind,
529 InputFile, OutputFile, Error, CCArgs,
530 Timeout, MemoryLimit);
533 /// createLLC - Try to find the LLC executable
535 LLC *AbstractInterpreter::createLLC(const char *Argv0,
536 std::string &Message,
537 const std::string &CCBinary,
538 const std::vector<std::string> *Args,
539 const std::vector<std::string> *CCArgs,
540 bool UseIntegratedAssembler) {
541 std::string LLCPath =
542 PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t) & createLLC);
543 if (LLCPath.empty()) {
544 Message = "Cannot find `llc' in executable directory!\n";
548 CC *cc = CC::create(Message, CCBinary, CCArgs);
550 errs() << Message << "\n";
553 Message = "Found llc: " + LLCPath + "\n";
554 return new LLC(LLCPath, cc, Args, UseIntegratedAssembler);
557 //===---------------------------------------------------------------------===//
558 // JIT Implementation of AbstractIntepreter interface
561 class JIT : public AbstractInterpreter {
562 std::string LLIPath; // The path to the LLI executable
563 std::vector<std::string> ToolArgs; // Args to pass to LLI
565 JIT(const std::string &Path, const std::vector<std::string> *Args)
568 if (Args) { ToolArgs = *Args; }
571 int ExecuteProgram(const std::string &Bitcode,
572 const std::vector<std::string> &Args,
573 const std::string &InputFile,
574 const std::string &OutputFile,
576 const std::vector<std::string> &CCArgs =
577 std::vector<std::string>(),
578 const std::vector<std::string> &SharedLibs =
579 std::vector<std::string>(),
580 unsigned Timeout = 0,
581 unsigned MemoryLimit = 0) override;
585 int JIT::ExecuteProgram(const std::string &Bitcode,
586 const std::vector<std::string> &Args,
587 const std::string &InputFile,
588 const std::string &OutputFile,
590 const std::vector<std::string> &CCArgs,
591 const std::vector<std::string> &SharedLibs,
593 unsigned MemoryLimit) {
594 // Construct a vector of parameters, incorporating those from the command-line
595 std::vector<const char*> JITArgs;
596 JITArgs.push_back(LLIPath.c_str());
597 JITArgs.push_back("-force-interpreter=false");
599 // Add any extra LLI args.
600 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
601 JITArgs.push_back(ToolArgs[i].c_str());
603 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
604 JITArgs.push_back("-load");
605 JITArgs.push_back(SharedLibs[i].c_str());
607 JITArgs.push_back(Bitcode.c_str());
608 // Add optional parameters to the running program from Argv
609 for (unsigned i=0, e = Args.size(); i != e; ++i)
610 JITArgs.push_back(Args[i].c_str());
611 JITArgs.push_back(nullptr);
613 outs() << "<jit>"; outs().flush();
614 DEBUG(errs() << "\nAbout to run:\t";
615 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
616 errs() << " " << JITArgs[i];
619 DEBUG(errs() << "\nSending output to " << OutputFile << "\n");
620 return RunProgramWithTimeout(LLIPath, &JITArgs[0],
621 InputFile, OutputFile, OutputFile,
622 Timeout, MemoryLimit, Error);
625 /// createJIT - Try to find the LLI executable
627 AbstractInterpreter *AbstractInterpreter::createJIT(const char *Argv0,
628 std::string &Message, const std::vector<std::string> *Args) {
629 std::string LLIPath =
630 PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t) & createJIT);
631 if (!LLIPath.empty()) {
632 Message = "Found lli: " + LLIPath + "\n";
633 return new JIT(LLIPath, Args);
636 Message = "Cannot find `lli' in executable directory!\n";
640 //===---------------------------------------------------------------------===//
644 static bool IsARMArchitecture(std::vector<const char*> Args) {
645 for (std::vector<const char*>::const_iterator
646 I = Args.begin(), E = Args.end(); I != E; ++I) {
647 if (StringRef(*I).equals_lower("-arch")) {
649 if (I != E && StringRef(*I).startswith_lower("arm"))
657 int CC::ExecuteProgram(const std::string &ProgramFile,
658 const std::vector<std::string> &Args,
660 const std::string &InputFile,
661 const std::string &OutputFile,
663 const std::vector<std::string> &ArgsForCC,
665 unsigned MemoryLimit) {
666 std::vector<const char*> CCArgs;
668 CCArgs.push_back(CCPath.c_str());
670 if (TargetTriple.getArch() == Triple::x86)
671 CCArgs.push_back("-m32");
673 for (std::vector<std::string>::const_iterator
674 I = ccArgs.begin(), E = ccArgs.end(); I != E; ++I)
675 CCArgs.push_back(I->c_str());
677 // Specify -x explicitly in case the extension is wonky
678 if (fileType != ObjectFile) {
679 CCArgs.push_back("-x");
680 if (fileType == CFile) {
681 CCArgs.push_back("c");
682 CCArgs.push_back("-fno-strict-aliasing");
684 CCArgs.push_back("assembler");
686 // For ARM architectures we don't want this flag. bugpoint isn't
687 // explicitly told what architecture it is working on, so we get
689 if (TargetTriple.isOSDarwin() && !IsARMArchitecture(CCArgs))
690 CCArgs.push_back("-force_cpusubtype_ALL");
694 CCArgs.push_back(ProgramFile.c_str()); // Specify the input filename.
696 CCArgs.push_back("-x");
697 CCArgs.push_back("none");
698 CCArgs.push_back("-o");
700 SmallString<128> OutputBinary;
702 sys::fs::createUniqueFile(ProgramFile + "-%%%%%%%.cc.exe", OutputBinary);
704 errs() << "Error making unique filename: " << EC.message() << "\n";
707 CCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
709 // Add any arguments intended for CC. We locate them here because this is
710 // most likely -L and -l options that need to come before other libraries but
711 // after the source. Other options won't be sensitive to placement on the
712 // command line, so this should be safe.
713 for (unsigned i = 0, e = ArgsForCC.size(); i != e; ++i)
714 CCArgs.push_back(ArgsForCC[i].c_str());
716 CCArgs.push_back("-lm"); // Hard-code the math library...
717 CCArgs.push_back("-O2"); // Optimize the program a bit...
718 #if defined (HAVE_LINK_R)
719 CCArgs.push_back("-Wl,-R."); // Search this dir for .so files
721 if (TargetTriple.getArch() == Triple::sparc)
722 CCArgs.push_back("-mcpu=v9");
723 CCArgs.push_back(nullptr); // NULL terminator
725 outs() << "<CC>"; outs().flush();
726 DEBUG(errs() << "\nAbout to run:\t";
727 for (unsigned i = 0, e = CCArgs.size()-1; i != e; ++i)
728 errs() << " " << CCArgs[i];
731 if (RunProgramWithTimeout(CCPath, &CCArgs[0], "", "", "")) {
732 *Error = ProcessFailure(CCPath, &CCArgs[0]);
736 std::vector<const char*> ProgramArgs;
738 // Declared here so that the destructor only runs after
739 // ProgramArgs is used.
742 if (RemoteClientPath.empty())
743 ProgramArgs.push_back(OutputBinary.c_str());
745 ProgramArgs.push_back(RemoteClientPath.c_str());
746 ProgramArgs.push_back(RemoteHost.c_str());
747 if (!RemoteUser.empty()) {
748 ProgramArgs.push_back("-l");
749 ProgramArgs.push_back(RemoteUser.c_str());
751 if (!RemotePort.empty()) {
752 ProgramArgs.push_back("-p");
753 ProgramArgs.push_back(RemotePort.c_str());
755 if (!RemoteExtra.empty()) {
756 ProgramArgs.push_back(RemoteExtra.c_str());
759 // Full path to the binary. We need to cd to the exec directory because
760 // there is a dylib there that the exec expects to find in the CWD
761 char* env_pwd = getenv("PWD");
765 Exec += OutputBinary.c_str();
766 ProgramArgs.push_back(Exec.c_str());
769 // Add optional parameters to the running program from Argv
770 for (unsigned i = 0, e = Args.size(); i != e; ++i)
771 ProgramArgs.push_back(Args[i].c_str());
772 ProgramArgs.push_back(nullptr); // NULL terminator
774 // Now that we have a binary, run it!
775 outs() << "<program>"; outs().flush();
776 DEBUG(errs() << "\nAbout to run:\t";
777 for (unsigned i = 0, e = ProgramArgs.size()-1; i != e; ++i)
778 errs() << " " << ProgramArgs[i];
782 FileRemover OutputBinaryRemover(OutputBinary.str(), !SaveTemps);
784 if (RemoteClientPath.empty()) {
785 DEBUG(errs() << "<run locally>");
786 int ExitCode = RunProgramWithTimeout(OutputBinary.str(), &ProgramArgs[0],
787 InputFile, OutputFile, OutputFile,
788 Timeout, MemoryLimit, Error);
789 // Treat a signal (usually SIGSEGV) or timeout as part of the program output
790 // so that crash-causing miscompilation is handled seamlessly.
792 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
793 outFile << *Error << '\n';
799 outs() << "<run remotely>"; outs().flush();
800 return RunProgramRemotelyWithTimeout(RemoteClientPath,
801 &ProgramArgs[0], InputFile, OutputFile,
802 OutputFile, Timeout, MemoryLimit);
806 int CC::MakeSharedObject(const std::string &InputFile, FileType fileType,
807 std::string &OutputFile,
808 const std::vector<std::string> &ArgsForCC,
809 std::string &Error) {
810 SmallString<128> UniqueFilename;
811 std::error_code EC = sys::fs::createUniqueFile(
812 InputFile + "-%%%%%%%" + LTDL_SHLIB_EXT, UniqueFilename);
814 errs() << "Error making unique filename: " << EC.message() << "\n";
817 OutputFile = UniqueFilename.str();
819 std::vector<const char*> CCArgs;
821 CCArgs.push_back(CCPath.c_str());
823 if (TargetTriple.getArch() == Triple::x86)
824 CCArgs.push_back("-m32");
826 for (std::vector<std::string>::const_iterator
827 I = ccArgs.begin(), E = ccArgs.end(); I != E; ++I)
828 CCArgs.push_back(I->c_str());
830 // Compile the C/asm file into a shared object
831 if (fileType != ObjectFile) {
832 CCArgs.push_back("-x");
833 CCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
835 CCArgs.push_back("-fno-strict-aliasing");
836 CCArgs.push_back(InputFile.c_str()); // Specify the input filename.
837 CCArgs.push_back("-x");
838 CCArgs.push_back("none");
839 if (TargetTriple.getArch() == Triple::sparc)
840 CCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc
841 else if (TargetTriple.isOSDarwin()) {
842 // link all source files into a single module in data segment, rather than
843 // generating blocks. dynamic_lookup requires that you set
844 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for
845 // bugpoint to just pass that in the environment of CC.
846 CCArgs.push_back("-single_module");
847 CCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC
848 CCArgs.push_back("-undefined");
849 CCArgs.push_back("dynamic_lookup");
851 CCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others
853 if (TargetTriple.getArch() == Triple::x86_64)
854 CCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC
856 if (TargetTriple.getArch() == Triple::sparc)
857 CCArgs.push_back("-mcpu=v9");
859 CCArgs.push_back("-o");
860 CCArgs.push_back(OutputFile.c_str()); // Output to the right filename.
861 CCArgs.push_back("-O2"); // Optimize the program a bit.
865 // Add any arguments intended for CC. We locate them here because this is
866 // most likely -L and -l options that need to come before other libraries but
867 // after the source. Other options won't be sensitive to placement on the
868 // command line, so this should be safe.
869 for (unsigned i = 0, e = ArgsForCC.size(); i != e; ++i)
870 CCArgs.push_back(ArgsForCC[i].c_str());
871 CCArgs.push_back(nullptr); // NULL terminator
875 outs() << "<CC>"; outs().flush();
876 DEBUG(errs() << "\nAbout to run:\t";
877 for (unsigned i = 0, e = CCArgs.size()-1; i != e; ++i)
878 errs() << " " << CCArgs[i];
881 if (RunProgramWithTimeout(CCPath, &CCArgs[0], "", "", "")) {
882 Error = ProcessFailure(CCPath, &CCArgs[0]);
888 /// create - Try to find the CC executable
890 CC *CC::create(std::string &Message,
891 const std::string &CCBinary,
892 const std::vector<std::string> *Args) {
893 auto CCPath = sys::findProgramByName(CCBinary);
895 Message = "Cannot find `" + CCBinary + "' in PATH: " +
896 CCPath.getError().message() + "\n";
900 std::string RemoteClientPath;
901 if (!RemoteClient.empty()) {
902 auto Path = sys::findProgramByName(RemoteClient);
904 Message = "Cannot find `" + RemoteClient + "' in PATH: " +
905 Path.getError().message() + "\n";
908 RemoteClientPath = *Path;
911 Message = "Found CC: " + *CCPath + "\n";
912 return new CC(*CCPath, RemoteClientPath, Args);