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,
57 ArrayRef<StringRef> Args, StringRef StdInFile,
58 StringRef StdOutFile, StringRef StdErrFile,
59 unsigned NumSeconds = 0,
60 unsigned MemoryLimit = 0,
61 std::string *ErrMsg = nullptr) {
62 Optional<StringRef> Redirects[3] = {StdInFile, StdOutFile, StdErrFile};
63 return sys::ExecuteAndWait(ProgramPath, Args, None, Redirects, NumSeconds,
67 /// RunProgramRemotelyWithTimeout - This function runs the given program
68 /// remotely using the given remote client and the sys::Program::ExecuteAndWait.
69 /// Returns the remote program exit code or reports a remote client error if it
70 /// fails. Remote client is required to return 255 if it failed or program exit
72 /// @see sys::Program::ExecuteAndWait
73 static int RunProgramRemotelyWithTimeout(
74 StringRef RemoteClientPath, ArrayRef<StringRef> Args, StringRef StdInFile,
75 StringRef StdOutFile, StringRef StdErrFile, unsigned NumSeconds = 0,
76 unsigned MemoryLimit = 0) {
77 Optional<StringRef> Redirects[3] = {StdInFile, StdOutFile, StdErrFile};
79 // Run the program remotely with the remote client
80 int ReturnCode = sys::ExecuteAndWait(RemoteClientPath, Args, None, Redirects,
81 NumSeconds, MemoryLimit);
83 // Has the remote client fail?
84 if (255 == ReturnCode) {
85 std::ostringstream OS;
86 OS << "\nError running remote client:\n ";
87 for (StringRef Arg : Args)
88 OS << " " << Arg.str();
91 // The error message is in the output file, let's print it out from there.
92 std::string StdOutFileName = StdOutFile.str();
93 std::ifstream ErrorFile(StdOutFileName.c_str());
95 std::copy(std::istreambuf_iterator<char>(ErrorFile),
96 std::istreambuf_iterator<char>(),
97 std::ostreambuf_iterator<char>(OS));
107 static Error ProcessFailure(StringRef ProgPath, ArrayRef<StringRef> Args,
108 unsigned Timeout = 0, unsigned MemoryLimit = 0) {
109 std::ostringstream OS;
110 OS << "\nError running tool:\n ";
111 for (StringRef Arg : Args)
112 OS << " " << Arg.str();
115 // Rerun the compiler, capturing any error messages to print them.
116 SmallString<128> ErrorFilename;
117 std::error_code EC = sys::fs::createTemporaryFile(
118 "bugpoint.program_error_messages", "", ErrorFilename);
120 errs() << "Error making unique filename: " << EC.message() << "\n";
124 RunProgramWithTimeout(ProgPath, Args, "", ErrorFilename.str(),
125 ErrorFilename.str(), Timeout, MemoryLimit);
126 // FIXME: check return code ?
128 // Print out the error messages generated by CC if possible...
129 std::ifstream ErrorFile(ErrorFilename.c_str());
131 std::copy(std::istreambuf_iterator<char>(ErrorFile),
132 std::istreambuf_iterator<char>(),
133 std::ostreambuf_iterator<char>(OS));
137 sys::fs::remove(ErrorFilename.c_str());
138 return make_error<StringError>(OS.str(), inconvertibleErrorCode());
141 //===---------------------------------------------------------------------===//
142 // LLI Implementation of AbstractIntepreter interface
145 class LLI : public AbstractInterpreter {
146 std::string LLIPath; // The path to the LLI executable
147 std::vector<std::string> ToolArgs; // Args to pass to LLI
149 LLI(const std::string &Path, const std::vector<std::string> *Args)
157 Expected<int> ExecuteProgram(
158 const std::string &Bitcode, const std::vector<std::string> &Args,
159 const std::string &InputFile, const std::string &OutputFile,
160 const std::vector<std::string> &CCArgs,
161 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
162 unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
166 Expected<int> LLI::ExecuteProgram(const std::string &Bitcode,
167 const std::vector<std::string> &Args,
168 const std::string &InputFile,
169 const std::string &OutputFile,
170 const std::vector<std::string> &CCArgs,
171 const std::vector<std::string> &SharedLibs,
172 unsigned Timeout, unsigned MemoryLimit) {
173 std::vector<StringRef> LLIArgs;
174 LLIArgs.push_back(LLIPath.c_str());
175 LLIArgs.push_back("-force-interpreter=true");
177 for (std::vector<std::string>::const_iterator i = SharedLibs.begin(),
178 e = SharedLibs.end();
180 LLIArgs.push_back("-load");
181 LLIArgs.push_back(*i);
184 // Add any extra LLI args.
185 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
186 LLIArgs.push_back(ToolArgs[i]);
188 LLIArgs.push_back(Bitcode);
189 // Add optional parameters to the running program from Argv
190 for (unsigned i = 0, e = Args.size(); i != e; ++i)
191 LLIArgs.push_back(Args[i]);
195 LLVM_DEBUG(errs() << "\nAbout to run:\t";
196 for (unsigned i = 0, e = LLIArgs.size() - 1; i != e; ++i) errs()
197 << " " << LLIArgs[i];
199 return RunProgramWithTimeout(LLIPath, LLIArgs, InputFile, OutputFile,
200 OutputFile, Timeout, MemoryLimit);
203 void AbstractInterpreter::anchor() {}
205 ErrorOr<std::string> llvm::FindProgramByName(const std::string &ExeName,
208 // Check the directory that the calling program is in. We can do
209 // this if ProgramPath contains at least one / character, indicating that it
210 // is a relative path to the executable itself.
211 std::string Main = sys::fs::getMainExecutable(Argv0, MainAddr);
212 StringRef Result = sys::path::parent_path(Main);
213 if (ErrorOr<std::string> Path = sys::findProgramByName(ExeName, Result))
216 // Check the user PATH.
217 return sys::findProgramByName(ExeName);
220 // LLI create method - Try to find the LLI executable
221 AbstractInterpreter *
222 AbstractInterpreter::createLLI(const char *Argv0, std::string &Message,
223 const std::vector<std::string> *ToolArgs) {
224 if (ErrorOr<std::string> LLIPath =
225 FindProgramByName("lli", Argv0, (void *)(intptr_t)&createLLI)) {
226 Message = "Found lli: " + *LLIPath + "\n";
227 return new LLI(*LLIPath, ToolArgs);
229 Message = LLIPath.getError().message() + "\n";
234 //===---------------------------------------------------------------------===//
235 // Custom compiler command implementation of AbstractIntepreter interface
237 // Allows using a custom command for compiling the bitcode, thus allows, for
238 // example, to compile a bitcode fragment without linking or executing, then
239 // using a custom wrapper script to check for compiler errors.
241 class CustomCompiler : public AbstractInterpreter {
242 std::string CompilerCommand;
243 std::vector<std::string> CompilerArgs;
246 CustomCompiler(const std::string &CompilerCmd,
247 std::vector<std::string> CompArgs)
248 : CompilerCommand(CompilerCmd), CompilerArgs(std::move(CompArgs)) {}
250 Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0,
251 unsigned MemoryLimit = 0) override;
253 Expected<int> ExecuteProgram(
254 const std::string &Bitcode, const std::vector<std::string> &Args,
255 const std::string &InputFile, const std::string &OutputFile,
256 const std::vector<std::string> &CCArgs = std::vector<std::string>(),
257 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
258 unsigned Timeout = 0, unsigned MemoryLimit = 0) override {
259 return make_error<StringError>(
260 "Execution not supported with -compile-custom",
261 inconvertibleErrorCode());
266 Error CustomCompiler::compileProgram(const std::string &Bitcode,
267 unsigned Timeout, unsigned MemoryLimit) {
269 std::vector<StringRef> ProgramArgs;
270 ProgramArgs.push_back(CompilerCommand.c_str());
272 for (std::size_t i = 0; i < CompilerArgs.size(); ++i)
273 ProgramArgs.push_back(CompilerArgs.at(i).c_str());
274 ProgramArgs.push_back(Bitcode);
276 // Add optional parameters to the running program from Argv
277 for (unsigned i = 0, e = CompilerArgs.size(); i != e; ++i)
278 ProgramArgs.push_back(CompilerArgs[i].c_str());
280 if (RunProgramWithTimeout(CompilerCommand, ProgramArgs, "", "", "", Timeout,
282 return ProcessFailure(CompilerCommand, ProgramArgs, Timeout, MemoryLimit);
283 return Error::success();
286 //===---------------------------------------------------------------------===//
287 // Custom execution command implementation of AbstractIntepreter interface
289 // Allows using a custom command for executing the bitcode, thus allows,
290 // for example, to invoke a cross compiler for code generation followed by
291 // a simulator that executes the generated binary.
293 class CustomExecutor : public AbstractInterpreter {
294 std::string ExecutionCommand;
295 std::vector<std::string> ExecutorArgs;
298 CustomExecutor(const std::string &ExecutionCmd,
299 std::vector<std::string> ExecArgs)
300 : ExecutionCommand(ExecutionCmd), ExecutorArgs(std::move(ExecArgs)) {}
302 Expected<int> ExecuteProgram(
303 const std::string &Bitcode, const std::vector<std::string> &Args,
304 const std::string &InputFile, const std::string &OutputFile,
305 const std::vector<std::string> &CCArgs,
306 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
307 unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
311 Expected<int> CustomExecutor::ExecuteProgram(
312 const std::string &Bitcode, const std::vector<std::string> &Args,
313 const std::string &InputFile, const std::string &OutputFile,
314 const std::vector<std::string> &CCArgs,
315 const std::vector<std::string> &SharedLibs, unsigned Timeout,
316 unsigned MemoryLimit) {
318 std::vector<StringRef> ProgramArgs;
319 ProgramArgs.push_back(ExecutionCommand);
321 for (std::size_t i = 0; i < ExecutorArgs.size(); ++i)
322 ProgramArgs.push_back(ExecutorArgs[i]);
323 ProgramArgs.push_back(Bitcode);
325 // Add optional parameters to the running program from Argv
326 for (unsigned i = 0, e = Args.size(); i != e; ++i)
327 ProgramArgs.push_back(Args[i]);
329 return RunProgramWithTimeout(ExecutionCommand, ProgramArgs, InputFile,
330 OutputFile, OutputFile, Timeout, MemoryLimit);
333 // Tokenize the CommandLine to the command and the args to allow
334 // defining a full command line as the command instead of just the
335 // executed program. We cannot just pass the whole string after the command
336 // as a single argument because then the program sees only a single
337 // command line argument (with spaces in it: "foo bar" instead
338 // of "foo" and "bar").
340 // Spaces are used as a delimiter; however repeated, leading, and trailing
341 // whitespace are ignored. Simple escaping is allowed via the '\'
342 // character, as seen below:
344 // Two consecutive '\' evaluate to a single '\'.
345 // A space after a '\' evaluates to a space that is not interpreted as a
347 // Any other instances of the '\' character are removed.
352 // 'exa\mple' -> 'example'
354 static void lexCommand(const char *Argv0, std::string &Message,
355 const std::string &CommandLine, std::string &CmdPath,
356 std::vector<std::string> &Args) {
360 bool FoundPath = false;
362 // first argument is the PATH.
363 // Skip repeated whitespace, leading whitespace and trailing whitespace.
364 for (std::size_t Pos = 0u; Pos <= CommandLine.size(); ++Pos) {
365 if ('\\' == CommandLine[Pos]) {
366 if (Pos + 1 < CommandLine.size())
367 Token.push_back(CommandLine[++Pos]);
371 if (' ' == CommandLine[Pos] || CommandLine.size() == Pos) {
382 Args.push_back(Token);
386 Token.push_back(CommandLine[Pos]);
389 auto Path = FindProgramByName(Command, Argv0, (void *)(intptr_t)&lexCommand);
391 Message = std::string("Cannot find '") + Command +
392 "' in PATH: " + Path.getError().message() + "\n";
397 Message = "Found command in: " + CmdPath + "\n";
400 // Custom execution environment create method, takes the execution command
402 AbstractInterpreter *AbstractInterpreter::createCustomCompiler(
403 const char *Argv0, std::string &Message,
404 const std::string &CompileCommandLine) {
407 std::vector<std::string> Args;
408 lexCommand(Argv0, Message, CompileCommandLine, CmdPath, Args);
412 return new CustomCompiler(CmdPath, Args);
415 // Custom execution environment create method, takes the execution command
417 AbstractInterpreter *
418 AbstractInterpreter::createCustomExecutor(const char *Argv0,
419 std::string &Message,
420 const std::string &ExecCommandLine) {
423 std::vector<std::string> Args;
424 lexCommand(Argv0, Message, ExecCommandLine, CmdPath, Args);
428 return new CustomExecutor(CmdPath, Args);
431 //===----------------------------------------------------------------------===//
432 // LLC Implementation of AbstractIntepreter interface
434 Expected<CC::FileType> LLC::OutputCode(const std::string &Bitcode,
435 std::string &OutputAsmFile,
436 unsigned Timeout, unsigned MemoryLimit) {
437 const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s");
439 SmallString<128> UniqueFile;
441 sys::fs::createUniqueFile(Bitcode + "-%%%%%%%" + Suffix, UniqueFile);
443 errs() << "Error making unique filename: " << EC.message() << "\n";
446 OutputAsmFile = UniqueFile.str();
447 std::vector<StringRef> LLCArgs;
448 LLCArgs.push_back(LLCPath);
450 // Add any extra LLC args.
451 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
452 LLCArgs.push_back(ToolArgs[i]);
454 LLCArgs.push_back("-o");
455 LLCArgs.push_back(OutputAsmFile); // Output to the Asm file
456 LLCArgs.push_back(Bitcode); // This is the input bitcode
458 if (UseIntegratedAssembler)
459 LLCArgs.push_back("-filetype=obj");
461 outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>");
463 LLVM_DEBUG(errs() << "\nAbout to run:\t";
464 for (unsigned i = 0, e = LLCArgs.size() - 1; i != e; ++i) errs()
465 << " " << LLCArgs[i];
467 if (RunProgramWithTimeout(LLCPath, LLCArgs, "", "", "", Timeout, MemoryLimit))
468 return ProcessFailure(LLCPath, LLCArgs, Timeout, MemoryLimit);
469 return UseIntegratedAssembler ? CC::ObjectFile : CC::AsmFile;
472 Error LLC::compileProgram(const std::string &Bitcode, unsigned Timeout,
473 unsigned MemoryLimit) {
474 std::string OutputAsmFile;
475 Expected<CC::FileType> Result =
476 OutputCode(Bitcode, OutputAsmFile, Timeout, MemoryLimit);
477 sys::fs::remove(OutputAsmFile);
478 if (Error E = Result.takeError())
480 return Error::success();
483 Expected<int> LLC::ExecuteProgram(const std::string &Bitcode,
484 const std::vector<std::string> &Args,
485 const std::string &InputFile,
486 const std::string &OutputFile,
487 const std::vector<std::string> &ArgsForCC,
488 const std::vector<std::string> &SharedLibs,
489 unsigned Timeout, unsigned MemoryLimit) {
491 std::string OutputAsmFile;
492 Expected<CC::FileType> FileKind =
493 OutputCode(Bitcode, OutputAsmFile, Timeout, MemoryLimit);
494 FileRemover OutFileRemover(OutputAsmFile, !SaveTemps);
495 if (Error E = FileKind.takeError())
498 std::vector<std::string> CCArgs(ArgsForCC);
499 CCArgs.insert(CCArgs.end(), SharedLibs.begin(), SharedLibs.end());
501 // Assuming LLC worked, compile the result with CC and run it.
502 return cc->ExecuteProgram(OutputAsmFile, Args, *FileKind, InputFile,
503 OutputFile, CCArgs, Timeout, MemoryLimit);
506 /// createLLC - Try to find the LLC executable
508 LLC *AbstractInterpreter::createLLC(const char *Argv0, std::string &Message,
509 const std::string &CCBinary,
510 const std::vector<std::string> *Args,
511 const std::vector<std::string> *CCArgs,
512 bool UseIntegratedAssembler) {
513 ErrorOr<std::string> LLCPath =
514 FindProgramByName("llc", Argv0, (void *)(intptr_t)&createLLC);
516 Message = LLCPath.getError().message() + "\n";
520 CC *cc = CC::create(Argv0, Message, CCBinary, CCArgs);
522 errs() << Message << "\n";
525 Message = "Found llc: " + *LLCPath + "\n";
526 return new LLC(*LLCPath, cc, Args, UseIntegratedAssembler);
529 //===---------------------------------------------------------------------===//
530 // JIT Implementation of AbstractIntepreter interface
533 class JIT : public AbstractInterpreter {
534 std::string LLIPath; // The path to the LLI executable
535 std::vector<std::string> ToolArgs; // Args to pass to LLI
537 JIT(const std::string &Path, const std::vector<std::string> *Args)
545 Expected<int> ExecuteProgram(
546 const std::string &Bitcode, const std::vector<std::string> &Args,
547 const std::string &InputFile, const std::string &OutputFile,
548 const std::vector<std::string> &CCArgs = std::vector<std::string>(),
549 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
550 unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
554 Expected<int> JIT::ExecuteProgram(const std::string &Bitcode,
555 const std::vector<std::string> &Args,
556 const std::string &InputFile,
557 const std::string &OutputFile,
558 const std::vector<std::string> &CCArgs,
559 const std::vector<std::string> &SharedLibs,
560 unsigned Timeout, unsigned MemoryLimit) {
561 // Construct a vector of parameters, incorporating those from the command-line
562 std::vector<StringRef> JITArgs;
563 JITArgs.push_back(LLIPath.c_str());
564 JITArgs.push_back("-force-interpreter=false");
566 // Add any extra LLI args.
567 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
568 JITArgs.push_back(ToolArgs[i]);
570 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
571 JITArgs.push_back("-load");
572 JITArgs.push_back(SharedLibs[i]);
574 JITArgs.push_back(Bitcode.c_str());
575 // Add optional parameters to the running program from Argv
576 for (unsigned i = 0, e = Args.size(); i != e; ++i)
577 JITArgs.push_back(Args[i]);
581 LLVM_DEBUG(errs() << "\nAbout to run:\t";
582 for (unsigned i = 0, e = JITArgs.size() - 1; i != e; ++i) errs()
583 << " " << JITArgs[i];
585 LLVM_DEBUG(errs() << "\nSending output to " << OutputFile << "\n");
586 return RunProgramWithTimeout(LLIPath, JITArgs, InputFile, OutputFile,
587 OutputFile, Timeout, MemoryLimit);
590 /// createJIT - Try to find the LLI executable
592 AbstractInterpreter *
593 AbstractInterpreter::createJIT(const char *Argv0, std::string &Message,
594 const std::vector<std::string> *Args) {
595 if (ErrorOr<std::string> LLIPath =
596 FindProgramByName("lli", Argv0, (void *)(intptr_t)&createJIT)) {
597 Message = "Found lli: " + *LLIPath + "\n";
598 return new JIT(*LLIPath, Args);
600 Message = LLIPath.getError().message() + "\n";
605 //===---------------------------------------------------------------------===//
609 static bool IsARMArchitecture(std::vector<StringRef> Args) {
610 for (size_t I = 0; I < Args.size(); ++I) {
611 if (!Args[I].equals_lower("-arch"))
614 if (I == Args.size())
616 if (Args[I].startswith_lower("arm"))
623 Expected<int> CC::ExecuteProgram(const std::string &ProgramFile,
624 const std::vector<std::string> &Args,
626 const std::string &InputFile,
627 const std::string &OutputFile,
628 const std::vector<std::string> &ArgsForCC,
629 unsigned Timeout, unsigned MemoryLimit) {
630 std::vector<StringRef> CCArgs;
632 CCArgs.push_back(CCPath);
634 if (TargetTriple.getArch() == Triple::x86)
635 CCArgs.push_back("-m32");
637 for (std::vector<std::string>::const_iterator I = ccArgs.begin(),
640 CCArgs.push_back(*I);
642 // Specify -x explicitly in case the extension is wonky
643 if (fileType != ObjectFile) {
644 CCArgs.push_back("-x");
645 if (fileType == CFile) {
646 CCArgs.push_back("c");
647 CCArgs.push_back("-fno-strict-aliasing");
649 CCArgs.push_back("assembler");
651 // For ARM architectures we don't want this flag. bugpoint isn't
652 // explicitly told what architecture it is working on, so we get
654 if (TargetTriple.isOSDarwin() && !IsARMArchitecture(CCArgs))
655 CCArgs.push_back("-force_cpusubtype_ALL");
659 CCArgs.push_back(ProgramFile); // Specify the input filename.
661 CCArgs.push_back("-x");
662 CCArgs.push_back("none");
663 CCArgs.push_back("-o");
665 SmallString<128> OutputBinary;
667 sys::fs::createUniqueFile(ProgramFile + "-%%%%%%%.cc.exe", OutputBinary);
669 errs() << "Error making unique filename: " << EC.message() << "\n";
672 CCArgs.push_back(OutputBinary); // Output to the right file...
674 // Add any arguments intended for CC. We locate them here because this is
675 // most likely -L and -l options that need to come before other libraries but
676 // after the source. Other options won't be sensitive to placement on the
677 // command line, so this should be safe.
678 for (unsigned i = 0, e = ArgsForCC.size(); i != e; ++i)
679 CCArgs.push_back(ArgsForCC[i]);
681 CCArgs.push_back("-lm"); // Hard-code the math library...
682 CCArgs.push_back("-O2"); // Optimize the program a bit...
683 if (TargetTriple.getArch() == Triple::sparc)
684 CCArgs.push_back("-mcpu=v9");
688 LLVM_DEBUG(errs() << "\nAbout to run:\t";
689 for (unsigned i = 0, e = CCArgs.size() - 1; i != e; ++i) errs()
692 if (RunProgramWithTimeout(CCPath, CCArgs, "", "", ""))
693 return ProcessFailure(CCPath, CCArgs);
695 std::vector<StringRef> ProgramArgs;
697 // Declared here so that the destructor only runs after
698 // ProgramArgs is used.
701 if (RemoteClientPath.empty())
702 ProgramArgs.push_back(OutputBinary);
704 ProgramArgs.push_back(RemoteClientPath);
705 ProgramArgs.push_back(RemoteHost);
706 if (!RemoteUser.empty()) {
707 ProgramArgs.push_back("-l");
708 ProgramArgs.push_back(RemoteUser);
710 if (!RemotePort.empty()) {
711 ProgramArgs.push_back("-p");
712 ProgramArgs.push_back(RemotePort);
714 if (!RemoteExtra.empty()) {
715 ProgramArgs.push_back(RemoteExtra);
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);
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]);
732 // Now that we have a binary, run it!
733 outs() << "<program>";
736 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 LLVM_DEBUG(errs() << "<run locally>");
746 int ExitCode = RunProgramWithTimeout(OutputBinary.str(), ProgramArgs,
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,
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<StringRef> CCArgs;
780 CCArgs.push_back(CCPath);
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);
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); // 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); // 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]);
832 LLVM_DEBUG(errs() << "\nAbout to run:\t";
833 for (unsigned i = 0, e = CCArgs.size() - 1; i != e; ++i) errs()
836 if (RunProgramWithTimeout(CCPath, CCArgs, "", "", ""))
837 return ProcessFailure(CCPath, CCArgs);
838 return Error::success();
841 /// create - Try to find the CC executable
843 CC *CC::create(const char *Argv0, std::string &Message,
844 const std::string &CCBinary,
845 const std::vector<std::string> *Args) {
846 auto CCPath = FindProgramByName(CCBinary, Argv0, (void *)(intptr_t)&create);
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);