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"
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"
30 cl::opt<bool> SaveTemps("save-temps", cl::init(false),
31 cl::desc("Save temporary files"));
36 RemoteClient("remote-client",
37 cl::desc("Remote execution client (rsh/ssh)"));
39 cl::opt<std::string> RemoteHost("remote-host",
40 cl::desc("Remote execution (rsh/ssh) host"));
42 cl::opt<std::string> RemotePort("remote-port",
43 cl::desc("Remote execution (rsh/ssh) port"));
45 cl::opt<std::string> RemoteUser("remote-user",
46 cl::desc("Remote execution (rsh/ssh) user id"));
49 RemoteExtra("remote-extra-options",
50 cl::desc("Remote execution (rsh/ssh) extra options"));
53 /// RunProgramWithTimeout - This function provides an alternate interface
54 /// to the sys::Program::ExecuteAndWait interface.
55 /// @see sys::Program::ExecuteAndWait
56 static int RunProgramWithTimeout(StringRef ProgramPath, const char **Args,
57 StringRef StdInFile, StringRef StdOutFile,
58 StringRef StdErrFile, unsigned NumSeconds = 0,
59 unsigned MemoryLimit = 0,
60 std::string *ErrMsg = nullptr) {
61 const StringRef *Redirects[3] = {&StdInFile, &StdOutFile, &StdErrFile};
62 return sys::ExecuteAndWait(ProgramPath, Args, nullptr, Redirects, NumSeconds,
66 /// RunProgramRemotelyWithTimeout - This function runs the given program
67 /// remotely using the given remote client and the sys::Program::ExecuteAndWait.
68 /// Returns the remote program exit code or reports a remote client error if it
69 /// fails. Remote client is required to return 255 if it failed or program exit
71 /// @see sys::Program::ExecuteAndWait
72 static int RunProgramRemotelyWithTimeout(StringRef RemoteClientPath,
73 const char **Args, StringRef StdInFile,
76 unsigned NumSeconds = 0,
77 unsigned MemoryLimit = 0) {
78 const StringRef *Redirects[3] = {&StdInFile, &StdOutFile, &StdErrFile};
80 // Run the program remotely with the remote client
81 int ReturnCode = sys::ExecuteAndWait(RemoteClientPath, Args, nullptr,
82 Redirects, NumSeconds, MemoryLimit);
84 // Has the remote client fail?
85 if (255 == ReturnCode) {
86 std::ostringstream OS;
87 OS << "\nError running remote client:\n ";
88 for (const char **Arg = Args; *Arg; ++Arg)
92 // The error message is in the output file, let's print it out from there.
93 std::string StdOutFileName = StdOutFile.str();
94 std::ifstream ErrorFile(StdOutFileName.c_str());
96 std::copy(std::istreambuf_iterator<char>(ErrorFile),
97 std::istreambuf_iterator<char>(),
98 std::ostreambuf_iterator<char>(OS));
108 static Error ProcessFailure(StringRef ProgPath, const char **Args,
109 unsigned Timeout = 0, unsigned MemoryLimit = 0) {
110 std::ostringstream OS;
111 OS << "\nError running tool:\n ";
112 for (const char **Arg = Args; *Arg; ++Arg)
116 // Rerun the compiler, capturing any error messages to print them.
117 SmallString<128> ErrorFilename;
118 std::error_code EC = sys::fs::createTemporaryFile(
119 "bugpoint.program_error_messages", "", ErrorFilename);
121 errs() << "Error making unique filename: " << EC.message() << "\n";
125 RunProgramWithTimeout(ProgPath, Args, "", ErrorFilename.str(),
126 ErrorFilename.str(), Timeout, MemoryLimit);
127 // FIXME: check return code ?
129 // Print out the error messages generated by CC if possible...
130 std::ifstream ErrorFile(ErrorFilename.c_str());
132 std::copy(std::istreambuf_iterator<char>(ErrorFile),
133 std::istreambuf_iterator<char>(),
134 std::ostreambuf_iterator<char>(OS));
138 sys::fs::remove(ErrorFilename.c_str());
139 return make_error<StringError>(OS.str(), inconvertibleErrorCode());
142 //===---------------------------------------------------------------------===//
143 // LLI Implementation of AbstractIntepreter interface
146 class LLI : public AbstractInterpreter {
147 std::string LLIPath; // The path to the LLI executable
148 std::vector<std::string> ToolArgs; // Args to pass to LLI
150 LLI(const std::string &Path, const std::vector<std::string> *Args)
158 Expected<int> ExecuteProgram(
159 const std::string &Bitcode, const std::vector<std::string> &Args,
160 const std::string &InputFile, const std::string &OutputFile,
161 const std::vector<std::string> &CCArgs,
162 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
163 unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
167 Expected<int> LLI::ExecuteProgram(const std::string &Bitcode,
168 const std::vector<std::string> &Args,
169 const std::string &InputFile,
170 const std::string &OutputFile,
171 const std::vector<std::string> &CCArgs,
172 const std::vector<std::string> &SharedLibs,
173 unsigned Timeout, unsigned MemoryLimit) {
174 std::vector<const char *> LLIArgs;
175 LLIArgs.push_back(LLIPath.c_str());
176 LLIArgs.push_back("-force-interpreter=true");
178 for (std::vector<std::string>::const_iterator i = SharedLibs.begin(),
179 e = SharedLibs.end();
181 LLIArgs.push_back("-load");
182 LLIArgs.push_back((*i).c_str());
185 // Add any extra LLI args.
186 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
187 LLIArgs.push_back(ToolArgs[i].c_str());
189 LLIArgs.push_back(Bitcode.c_str());
190 // Add optional parameters to the running program from Argv
191 for (unsigned i = 0, e = Args.size(); i != e; ++i)
192 LLIArgs.push_back(Args[i].c_str());
193 LLIArgs.push_back(nullptr);
197 DEBUG(errs() << "\nAbout to run:\t";
198 for (unsigned i = 0, e = LLIArgs.size() - 1; i != e; ++i) errs()
199 << " " << LLIArgs[i];
201 return RunProgramWithTimeout(LLIPath, &LLIArgs[0], InputFile, OutputFile,
202 OutputFile, Timeout, MemoryLimit);
205 void AbstractInterpreter::anchor() {}
207 #if defined(LLVM_ON_UNIX)
208 const char EXESuffix[] = "";
209 #elif defined(LLVM_ON_WIN32)
210 const char EXESuffix[] = "exe";
213 /// Prepend the path to the program being executed
214 /// to \p ExeName, given the value of argv[0] and the address of main()
215 /// itself. This allows us to find another LLVM tool if it is built in the same
216 /// directory. An empty string is returned on error; note that this function
217 /// just mainpulates the path and doesn't check for executability.
218 /// @brief Find a named executable.
219 static std::string PrependMainExecutablePath(const std::string &ExeName,
222 // Check the directory that the calling program is in. We can do
223 // this if ProgramPath contains at least one / character, indicating that it
224 // is a relative path to the executable itself.
225 std::string Main = sys::fs::getMainExecutable(Argv0, MainAddr);
226 StringRef Result = sys::path::parent_path(Main);
228 if (!Result.empty()) {
229 SmallString<128> Storage = Result;
230 sys::path::append(Storage, ExeName);
231 sys::path::replace_extension(Storage, EXESuffix);
232 return Storage.str();
238 // LLI create method - Try to find the LLI executable
239 AbstractInterpreter *
240 AbstractInterpreter::createLLI(const char *Argv0, std::string &Message,
241 const std::vector<std::string> *ToolArgs) {
242 std::string LLIPath =
243 PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createLLI);
244 if (!LLIPath.empty()) {
245 Message = "Found lli: " + LLIPath + "\n";
246 return new LLI(LLIPath, ToolArgs);
249 Message = "Cannot find `lli' in executable directory!\n";
253 //===---------------------------------------------------------------------===//
254 // Custom compiler command implementation of AbstractIntepreter interface
256 // Allows using a custom command for compiling the bitcode, thus allows, for
257 // example, to compile a bitcode fragment without linking or executing, then
258 // using a custom wrapper script to check for compiler errors.
260 class CustomCompiler : public AbstractInterpreter {
261 std::string CompilerCommand;
262 std::vector<std::string> CompilerArgs;
265 CustomCompiler(const std::string &CompilerCmd,
266 std::vector<std::string> CompArgs)
267 : CompilerCommand(CompilerCmd), CompilerArgs(std::move(CompArgs)) {}
269 Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0,
270 unsigned MemoryLimit = 0) override;
272 Expected<int> ExecuteProgram(
273 const std::string &Bitcode, const std::vector<std::string> &Args,
274 const std::string &InputFile, const std::string &OutputFile,
275 const std::vector<std::string> &CCArgs = std::vector<std::string>(),
276 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
277 unsigned Timeout = 0, unsigned MemoryLimit = 0) override {
278 return make_error<StringError>(
279 "Execution not supported with -compile-custom",
280 inconvertibleErrorCode());
285 Error CustomCompiler::compileProgram(const std::string &Bitcode,
286 unsigned Timeout, unsigned MemoryLimit) {
288 std::vector<const char *> ProgramArgs;
289 ProgramArgs.push_back(CompilerCommand.c_str());
291 for (std::size_t i = 0; i < CompilerArgs.size(); ++i)
292 ProgramArgs.push_back(CompilerArgs.at(i).c_str());
293 ProgramArgs.push_back(Bitcode.c_str());
294 ProgramArgs.push_back(nullptr);
296 // Add optional parameters to the running program from Argv
297 for (unsigned i = 0, e = CompilerArgs.size(); i != e; ++i)
298 ProgramArgs.push_back(CompilerArgs[i].c_str());
300 if (RunProgramWithTimeout(CompilerCommand, &ProgramArgs[0], "", "", "",
301 Timeout, MemoryLimit))
302 return ProcessFailure(CompilerCommand, &ProgramArgs[0], Timeout,
304 return Error::success();
307 //===---------------------------------------------------------------------===//
308 // Custom execution command implementation of AbstractIntepreter interface
310 // Allows using a custom command for executing the bitcode, thus allows,
311 // for example, to invoke a cross compiler for code generation followed by
312 // a simulator that executes the generated binary.
314 class CustomExecutor : public AbstractInterpreter {
315 std::string ExecutionCommand;
316 std::vector<std::string> ExecutorArgs;
319 CustomExecutor(const std::string &ExecutionCmd,
320 std::vector<std::string> ExecArgs)
321 : ExecutionCommand(ExecutionCmd), ExecutorArgs(std::move(ExecArgs)) {}
323 Expected<int> ExecuteProgram(
324 const std::string &Bitcode, const std::vector<std::string> &Args,
325 const std::string &InputFile, const std::string &OutputFile,
326 const std::vector<std::string> &CCArgs,
327 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
328 unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
332 Expected<int> CustomExecutor::ExecuteProgram(
333 const std::string &Bitcode, const std::vector<std::string> &Args,
334 const std::string &InputFile, const std::string &OutputFile,
335 const std::vector<std::string> &CCArgs,
336 const std::vector<std::string> &SharedLibs, unsigned Timeout,
337 unsigned MemoryLimit) {
339 std::vector<const char *> ProgramArgs;
340 ProgramArgs.push_back(ExecutionCommand.c_str());
342 for (std::size_t i = 0; i < ExecutorArgs.size(); ++i)
343 ProgramArgs.push_back(ExecutorArgs.at(i).c_str());
344 ProgramArgs.push_back(Bitcode.c_str());
345 ProgramArgs.push_back(nullptr);
347 // Add optional parameters to the running program from Argv
348 for (unsigned i = 0, e = Args.size(); i != e; ++i)
349 ProgramArgs.push_back(Args[i].c_str());
351 return RunProgramWithTimeout(ExecutionCommand, &ProgramArgs[0], InputFile,
352 OutputFile, OutputFile, Timeout, MemoryLimit);
355 // Tokenize the CommandLine to the command and the args to allow
356 // defining a full command line as the command instead of just the
357 // executed program. We cannot just pass the whole string after the command
358 // as a single argument because then program sees only a single
359 // command line argument (with spaces in it: "foo bar" instead
360 // of "foo" and "bar").
362 // code borrowed from:
363 // http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
364 static void lexCommand(std::string &Message, const std::string &CommandLine,
365 std::string &CmdPath, std::vector<std::string> &Args) {
367 std::string Command = "";
368 std::string delimiters = " ";
370 std::string::size_type lastPos = CommandLine.find_first_not_of(delimiters, 0);
371 std::string::size_type pos = CommandLine.find_first_of(delimiters, lastPos);
373 while (std::string::npos != pos || std::string::npos != lastPos) {
374 std::string token = CommandLine.substr(lastPos, pos - lastPos);
378 Args.push_back(token);
379 // Skip delimiters. Note the "not_of"
380 lastPos = CommandLine.find_first_not_of(delimiters, pos);
381 // Find next "non-delimiter"
382 pos = CommandLine.find_first_of(delimiters, lastPos);
385 auto Path = sys::findProgramByName(Command);
387 Message = std::string("Cannot find '") + Command + "' in PATH: " +
388 Path.getError().message() + "\n";
393 Message = "Found command in: " + CmdPath + "\n";
396 // Custom execution environment create method, takes the execution command
398 AbstractInterpreter *AbstractInterpreter::createCustomCompiler(
399 std::string &Message, const std::string &CompileCommandLine) {
402 std::vector<std::string> Args;
403 lexCommand(Message, CompileCommandLine, CmdPath, Args);
407 return new CustomCompiler(CmdPath, Args);
410 // Custom execution environment create method, takes the execution command
412 AbstractInterpreter *
413 AbstractInterpreter::createCustomExecutor(std::string &Message,
414 const std::string &ExecCommandLine) {
417 std::vector<std::string> Args;
418 lexCommand(Message, ExecCommandLine, CmdPath, Args);
422 return new CustomExecutor(CmdPath, Args);
425 //===----------------------------------------------------------------------===//
426 // LLC Implementation of AbstractIntepreter interface
428 Expected<CC::FileType> LLC::OutputCode(const std::string &Bitcode,
429 std::string &OutputAsmFile,
430 unsigned Timeout, unsigned MemoryLimit) {
431 const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s");
433 SmallString<128> UniqueFile;
435 sys::fs::createUniqueFile(Bitcode + "-%%%%%%%" + Suffix, UniqueFile);
437 errs() << "Error making unique filename: " << EC.message() << "\n";
440 OutputAsmFile = UniqueFile.str();
441 std::vector<const char *> LLCArgs;
442 LLCArgs.push_back(LLCPath.c_str());
444 // Add any extra LLC args.
445 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
446 LLCArgs.push_back(ToolArgs[i].c_str());
448 LLCArgs.push_back("-o");
449 LLCArgs.push_back(OutputAsmFile.c_str()); // Output to the Asm file
450 LLCArgs.push_back(Bitcode.c_str()); // This is the input bitcode
452 if (UseIntegratedAssembler)
453 LLCArgs.push_back("-filetype=obj");
455 LLCArgs.push_back(nullptr);
457 outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>");
459 DEBUG(errs() << "\nAbout to run:\t";
460 for (unsigned i = 0, e = LLCArgs.size() - 1; i != e; ++i) errs()
461 << " " << LLCArgs[i];
463 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], "", "", "", Timeout,
465 return ProcessFailure(LLCPath, &LLCArgs[0], Timeout, MemoryLimit);
466 return UseIntegratedAssembler ? CC::ObjectFile : CC::AsmFile;
469 Error LLC::compileProgram(const std::string &Bitcode, unsigned Timeout,
470 unsigned MemoryLimit) {
471 std::string OutputAsmFile;
472 Expected<CC::FileType> Result =
473 OutputCode(Bitcode, OutputAsmFile, Timeout, MemoryLimit);
474 sys::fs::remove(OutputAsmFile);
475 if (Error E = Result.takeError())
477 return Error::success();
480 Expected<int> LLC::ExecuteProgram(const std::string &Bitcode,
481 const std::vector<std::string> &Args,
482 const std::string &InputFile,
483 const std::string &OutputFile,
484 const std::vector<std::string> &ArgsForCC,
485 const std::vector<std::string> &SharedLibs,
486 unsigned Timeout, unsigned MemoryLimit) {
488 std::string OutputAsmFile;
489 Expected<CC::FileType> FileKind =
490 OutputCode(Bitcode, OutputAsmFile, Timeout, MemoryLimit);
491 FileRemover OutFileRemover(OutputAsmFile, !SaveTemps);
492 if (Error E = FileKind.takeError())
495 std::vector<std::string> CCArgs(ArgsForCC);
496 CCArgs.insert(CCArgs.end(), SharedLibs.begin(), SharedLibs.end());
498 // Assuming LLC worked, compile the result with CC and run it.
499 return cc->ExecuteProgram(OutputAsmFile, Args, *FileKind, InputFile,
500 OutputFile, CCArgs, Timeout, MemoryLimit);
503 /// createLLC - Try to find the LLC executable
505 LLC *AbstractInterpreter::createLLC(const char *Argv0, std::string &Message,
506 const std::string &CCBinary,
507 const std::vector<std::string> *Args,
508 const std::vector<std::string> *CCArgs,
509 bool UseIntegratedAssembler) {
510 std::string LLCPath =
511 PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t)&createLLC);
512 if (LLCPath.empty()) {
513 Message = "Cannot find `llc' in executable directory!\n";
517 CC *cc = CC::create(Message, CCBinary, CCArgs);
519 errs() << Message << "\n";
522 Message = "Found llc: " + LLCPath + "\n";
523 return new LLC(LLCPath, cc, Args, UseIntegratedAssembler);
526 //===---------------------------------------------------------------------===//
527 // JIT Implementation of AbstractIntepreter interface
530 class JIT : public AbstractInterpreter {
531 std::string LLIPath; // The path to the LLI executable
532 std::vector<std::string> ToolArgs; // Args to pass to LLI
534 JIT(const std::string &Path, const std::vector<std::string> *Args)
542 Expected<int> ExecuteProgram(
543 const std::string &Bitcode, const std::vector<std::string> &Args,
544 const std::string &InputFile, const std::string &OutputFile,
545 const std::vector<std::string> &CCArgs = std::vector<std::string>(),
546 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
547 unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
551 Expected<int> JIT::ExecuteProgram(const std::string &Bitcode,
552 const std::vector<std::string> &Args,
553 const std::string &InputFile,
554 const std::string &OutputFile,
555 const std::vector<std::string> &CCArgs,
556 const std::vector<std::string> &SharedLibs,
557 unsigned Timeout, unsigned MemoryLimit) {
558 // Construct a vector of parameters, incorporating those from the command-line
559 std::vector<const char *> JITArgs;
560 JITArgs.push_back(LLIPath.c_str());
561 JITArgs.push_back("-force-interpreter=false");
563 // Add any extra LLI args.
564 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
565 JITArgs.push_back(ToolArgs[i].c_str());
567 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
568 JITArgs.push_back("-load");
569 JITArgs.push_back(SharedLibs[i].c_str());
571 JITArgs.push_back(Bitcode.c_str());
572 // Add optional parameters to the running program from Argv
573 for (unsigned i = 0, e = Args.size(); i != e; ++i)
574 JITArgs.push_back(Args[i].c_str());
575 JITArgs.push_back(nullptr);
579 DEBUG(errs() << "\nAbout to run:\t";
580 for (unsigned i = 0, e = JITArgs.size() - 1; i != e; ++i) errs()
581 << " " << JITArgs[i];
583 DEBUG(errs() << "\nSending output to " << OutputFile << "\n");
584 return RunProgramWithTimeout(LLIPath, &JITArgs[0], InputFile, OutputFile,
585 OutputFile, Timeout, MemoryLimit);
588 /// createJIT - Try to find the LLI executable
590 AbstractInterpreter *
591 AbstractInterpreter::createJIT(const char *Argv0, std::string &Message,
592 const std::vector<std::string> *Args) {
593 std::string LLIPath =
594 PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createJIT);
595 if (!LLIPath.empty()) {
596 Message = "Found lli: " + LLIPath + "\n";
597 return new JIT(LLIPath, Args);
600 Message = "Cannot find `lli' in executable directory!\n";
604 //===---------------------------------------------------------------------===//
608 static bool IsARMArchitecture(std::vector<const char *> Args) {
609 for (std::vector<const char *>::const_iterator I = Args.begin(),
612 if (StringRef(*I).equals_lower("-arch")) {
614 if (I != E && StringRef(*I).startswith_lower("arm"))
622 Expected<int> CC::ExecuteProgram(const std::string &ProgramFile,
623 const std::vector<std::string> &Args,
625 const std::string &InputFile,
626 const std::string &OutputFile,
627 const std::vector<std::string> &ArgsForCC,
628 unsigned Timeout, unsigned MemoryLimit) {
629 std::vector<const char *> CCArgs;
631 CCArgs.push_back(CCPath.c_str());
633 if (TargetTriple.getArch() == Triple::x86)
634 CCArgs.push_back("-m32");
636 for (std::vector<std::string>::const_iterator I = ccArgs.begin(),
639 CCArgs.push_back(I->c_str());
641 // Specify -x explicitly in case the extension is wonky
642 if (fileType != ObjectFile) {
643 CCArgs.push_back("-x");
644 if (fileType == CFile) {
645 CCArgs.push_back("c");
646 CCArgs.push_back("-fno-strict-aliasing");
648 CCArgs.push_back("assembler");
650 // For ARM architectures we don't want this flag. bugpoint isn't
651 // explicitly told what architecture it is working on, so we get
653 if (TargetTriple.isOSDarwin() && !IsARMArchitecture(CCArgs))
654 CCArgs.push_back("-force_cpusubtype_ALL");
658 CCArgs.push_back(ProgramFile.c_str()); // Specify the input filename.
660 CCArgs.push_back("-x");
661 CCArgs.push_back("none");
662 CCArgs.push_back("-o");
664 SmallString<128> OutputBinary;
666 sys::fs::createUniqueFile(ProgramFile + "-%%%%%%%.cc.exe", OutputBinary);
668 errs() << "Error making unique filename: " << EC.message() << "\n";
671 CCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
673 // Add any arguments intended for CC. We locate them here because this is
674 // most likely -L and -l options that need to come before other libraries but
675 // after the source. Other options won't be sensitive to placement on the
676 // command line, so this should be safe.
677 for (unsigned i = 0, e = ArgsForCC.size(); i != e; ++i)
678 CCArgs.push_back(ArgsForCC[i].c_str());
680 CCArgs.push_back("-lm"); // Hard-code the math library...
681 CCArgs.push_back("-O2"); // Optimize the program a bit...
682 if (TargetTriple.getArch() == Triple::sparc)
683 CCArgs.push_back("-mcpu=v9");
684 CCArgs.push_back(nullptr); // NULL terminator
688 DEBUG(errs() << "\nAbout to run:\t";
689 for (unsigned i = 0, e = CCArgs.size() - 1; i != e; ++i) errs()
692 if (RunProgramWithTimeout(CCPath, &CCArgs[0], "", "", ""))
693 return ProcessFailure(CCPath, &CCArgs[0]);
695 std::vector<const char *> ProgramArgs;
697 // Declared here so that the destructor only runs after
698 // ProgramArgs is used.
701 if (RemoteClientPath.empty())
702 ProgramArgs.push_back(OutputBinary.c_str());
704 ProgramArgs.push_back(RemoteClientPath.c_str());
705 ProgramArgs.push_back(RemoteHost.c_str());
706 if (!RemoteUser.empty()) {
707 ProgramArgs.push_back("-l");
708 ProgramArgs.push_back(RemoteUser.c_str());
710 if (!RemotePort.empty()) {
711 ProgramArgs.push_back("-p");
712 ProgramArgs.push_back(RemotePort.c_str());
714 if (!RemoteExtra.empty()) {
715 ProgramArgs.push_back(RemoteExtra.c_str());
718 // Full path to the binary. We need to cd to the exec directory because
719 // there is a dylib there that the exec expects to find in the CWD
720 char *env_pwd = getenv("PWD");
724 Exec += OutputBinary.c_str();
725 ProgramArgs.push_back(Exec.c_str());
728 // Add optional parameters to the running program from Argv
729 for (unsigned i = 0, e = Args.size(); i != e; ++i)
730 ProgramArgs.push_back(Args[i].c_str());
731 ProgramArgs.push_back(nullptr); // NULL terminator
733 // Now that we have a binary, run it!
734 outs() << "<program>";
736 DEBUG(errs() << "\nAbout to run:\t";
737 for (unsigned i = 0, e = ProgramArgs.size() - 1; i != e; ++i) errs()
738 << " " << ProgramArgs[i];
741 FileRemover OutputBinaryRemover(OutputBinary.str(), !SaveTemps);
743 if (RemoteClientPath.empty()) {
744 DEBUG(errs() << "<run locally>");
746 int ExitCode = RunProgramWithTimeout(OutputBinary.str(), &ProgramArgs[0],
747 InputFile, OutputFile, OutputFile,
748 Timeout, MemoryLimit, &Error);
749 // Treat a signal (usually SIGSEGV) or timeout as part of the program output
750 // so that crash-causing miscompilation is handled seamlessly.
752 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
753 outFile << Error << '\n';
758 outs() << "<run remotely>";
760 return RunProgramRemotelyWithTimeout(RemoteClientPath, &ProgramArgs[0],
761 InputFile, OutputFile, OutputFile,
762 Timeout, MemoryLimit);
766 Error CC::MakeSharedObject(const std::string &InputFile, FileType fileType,
767 std::string &OutputFile,
768 const std::vector<std::string> &ArgsForCC) {
769 SmallString<128> UniqueFilename;
770 std::error_code EC = sys::fs::createUniqueFile(
771 InputFile + "-%%%%%%%" + LTDL_SHLIB_EXT, UniqueFilename);
773 errs() << "Error making unique filename: " << EC.message() << "\n";
776 OutputFile = UniqueFilename.str();
778 std::vector<const char *> CCArgs;
780 CCArgs.push_back(CCPath.c_str());
782 if (TargetTriple.getArch() == Triple::x86)
783 CCArgs.push_back("-m32");
785 for (std::vector<std::string>::const_iterator I = ccArgs.begin(),
788 CCArgs.push_back(I->c_str());
790 // Compile the C/asm file into a shared object
791 if (fileType != ObjectFile) {
792 CCArgs.push_back("-x");
793 CCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
795 CCArgs.push_back("-fno-strict-aliasing");
796 CCArgs.push_back(InputFile.c_str()); // Specify the input filename.
797 CCArgs.push_back("-x");
798 CCArgs.push_back("none");
799 if (TargetTriple.getArch() == Triple::sparc)
800 CCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc
801 else if (TargetTriple.isOSDarwin()) {
802 // link all source files into a single module in data segment, rather than
803 // generating blocks. dynamic_lookup requires that you set
804 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for
805 // bugpoint to just pass that in the environment of CC.
806 CCArgs.push_back("-single_module");
807 CCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC
808 CCArgs.push_back("-undefined");
809 CCArgs.push_back("dynamic_lookup");
811 CCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others
813 if (TargetTriple.getArch() == Triple::x86_64)
814 CCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC
816 if (TargetTriple.getArch() == Triple::sparc)
817 CCArgs.push_back("-mcpu=v9");
819 CCArgs.push_back("-o");
820 CCArgs.push_back(OutputFile.c_str()); // Output to the right filename.
821 CCArgs.push_back("-O2"); // Optimize the program a bit.
823 // Add any arguments intended for CC. We locate them here because this is
824 // most likely -L and -l options that need to come before other libraries but
825 // after the source. Other options won't be sensitive to placement on the
826 // command line, so this should be safe.
827 for (unsigned i = 0, e = ArgsForCC.size(); i != e; ++i)
828 CCArgs.push_back(ArgsForCC[i].c_str());
829 CCArgs.push_back(nullptr); // NULL terminator
833 DEBUG(errs() << "\nAbout to run:\t";
834 for (unsigned i = 0, e = CCArgs.size() - 1; i != e; ++i) errs()
837 if (RunProgramWithTimeout(CCPath, &CCArgs[0], "", "", ""))
838 return ProcessFailure(CCPath, &CCArgs[0]);
839 return Error::success();;
842 /// create - Try to find the CC executable
844 CC *CC::create(std::string &Message, const std::string &CCBinary,
845 const std::vector<std::string> *Args) {
846 auto CCPath = sys::findProgramByName(CCBinary);
848 Message = "Cannot find `" + CCBinary + "' in PATH: " +
849 CCPath.getError().message() + "\n";
853 std::string RemoteClientPath;
854 if (!RemoteClient.empty()) {
855 auto Path = sys::findProgramByName(RemoteClient);
857 Message = "Cannot find `" + RemoteClient + "' in PATH: " +
858 Path.getError().message() + "\n";
861 RemoteClientPath = *Path;
864 Message = "Found CC: " + *CCPath + "\n";
865 return new CC(*CCPath, RemoteClientPath, Args);