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 #define DEBUG_TYPE "toolrunner"
15 #include "ToolRunner.h"
16 #include "llvm/Config/config.h" // for HAVE_LINK_R
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/FileUtilities.h"
20 #include "llvm/Support/PathV1.h"
21 #include "llvm/Support/Program.h"
22 #include "llvm/Support/raw_ostream.h"
29 SaveTemps("save-temps", cl::init(false), cl::desc("Save temporary files"));
34 RemoteClient("remote-client",
35 cl::desc("Remote execution client (rsh/ssh)"));
38 RemoteHost("remote-host",
39 cl::desc("Remote execution (rsh/ssh) host"));
42 RemotePort("remote-port",
43 cl::desc("Remote execution (rsh/ssh) port"));
46 RemoteUser("remote-user",
47 cl::desc("Remote execution (rsh/ssh) user id"));
50 RemoteExtra("remote-extra-options",
51 cl::desc("Remote execution (rsh/ssh) extra options"));
54 /// RunProgramWithTimeout - This function provides an alternate interface
55 /// to the sys::Program::ExecuteAndWait interface.
56 /// @see sys::Program::ExecuteAndWait
57 static int RunProgramWithTimeout(StringRef ProgramPath,
62 unsigned NumSeconds = 0,
63 unsigned MemoryLimit = 0,
64 std::string *ErrMsg = 0) {
65 const StringRef *Redirects[3] = { &StdInFile, &StdOutFile, &StdErrFile };
67 #if 0 // For debug purposes
70 for (unsigned i = 0; Args[i]; ++i)
71 errs() << " " << Args[i];
76 return sys::ExecuteAndWait(ProgramPath, Args, 0, Redirects,
77 NumSeconds, MemoryLimit, ErrMsg);
80 /// RunProgramRemotelyWithTimeout - This function runs the given program
81 /// remotely using the given remote client and the sys::Program::ExecuteAndWait.
82 /// Returns the remote program exit code or reports a remote client error if it
83 /// fails. Remote client is required to return 255 if it failed or program exit
85 /// @see sys::Program::ExecuteAndWait
86 static int RunProgramRemotelyWithTimeout(StringRef RemoteClientPath,
91 unsigned NumSeconds = 0,
92 unsigned MemoryLimit = 0) {
93 const StringRef *Redirects[3] = { &StdInFile, &StdOutFile, &StdErrFile };
95 #if 0 // For debug purposes
98 for (unsigned i = 0; Args[i]; ++i)
99 errs() << " " << Args[i];
104 // Run the program remotely with the remote client
105 int ReturnCode = sys::ExecuteAndWait(RemoteClientPath, Args, 0,
106 Redirects, NumSeconds, MemoryLimit);
108 // Has the remote client fail?
109 if (255 == ReturnCode) {
110 std::ostringstream OS;
111 OS << "\nError running remote client:\n ";
112 for (const char **Arg = Args; *Arg; ++Arg)
116 // The error message is in the output file, let's print it out from there.
117 std::string StdOutFileName = StdOutFile.str();
118 std::ifstream ErrorFile(StdOutFileName.c_str());
120 std::copy(std::istreambuf_iterator<char>(ErrorFile),
121 std::istreambuf_iterator<char>(),
122 std::ostreambuf_iterator<char>(OS));
132 static std::string ProcessFailure(StringRef ProgPath, const char** Args,
133 unsigned Timeout = 0,
134 unsigned MemoryLimit = 0) {
135 std::ostringstream OS;
136 OS << "\nError running tool:\n ";
137 for (const char **Arg = Args; *Arg; ++Arg)
141 // Rerun the compiler, capturing any error messages to print them.
142 sys::Path ErrorFilename("bugpoint.program_error_messages");
144 if (ErrorFilename.makeUnique(true, &ErrMsg)) {
145 errs() << "Error making unique filename: " << ErrMsg << "\n";
148 RunProgramWithTimeout(ProgPath, Args, "", ErrorFilename.str(),
149 ErrorFilename.str(), Timeout, MemoryLimit);
150 // FIXME: check return code ?
152 // Print out the error messages generated by GCC if possible...
153 std::ifstream ErrorFile(ErrorFilename.c_str());
155 std::copy(std::istreambuf_iterator<char>(ErrorFile),
156 std::istreambuf_iterator<char>(),
157 std::ostreambuf_iterator<char>(OS));
161 ErrorFilename.eraseFromDisk();
165 //===---------------------------------------------------------------------===//
166 // LLI Implementation of AbstractIntepreter interface
169 class LLI : public AbstractInterpreter {
170 std::string LLIPath; // The path to the LLI executable
171 std::vector<std::string> ToolArgs; // Args to pass to LLI
173 LLI(const std::string &Path, const std::vector<std::string> *Args)
176 if (Args) { ToolArgs = *Args; }
179 virtual int ExecuteProgram(const std::string &Bitcode,
180 const std::vector<std::string> &Args,
181 const std::string &InputFile,
182 const std::string &OutputFile,
184 const std::vector<std::string> &GCCArgs,
185 const std::vector<std::string> &SharedLibs =
186 std::vector<std::string>(),
187 unsigned Timeout = 0,
188 unsigned MemoryLimit = 0);
192 int LLI::ExecuteProgram(const std::string &Bitcode,
193 const std::vector<std::string> &Args,
194 const std::string &InputFile,
195 const std::string &OutputFile,
197 const std::vector<std::string> &GCCArgs,
198 const std::vector<std::string> &SharedLibs,
200 unsigned MemoryLimit) {
201 std::vector<const char*> LLIArgs;
202 LLIArgs.push_back(LLIPath.c_str());
203 LLIArgs.push_back("-force-interpreter=true");
205 for (std::vector<std::string>::const_iterator i = SharedLibs.begin(),
206 e = SharedLibs.end(); i != e; ++i) {
207 LLIArgs.push_back("-load");
208 LLIArgs.push_back((*i).c_str());
211 // Add any extra LLI args.
212 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
213 LLIArgs.push_back(ToolArgs[i].c_str());
215 LLIArgs.push_back(Bitcode.c_str());
216 // Add optional parameters to the running program from Argv
217 for (unsigned i=0, e = Args.size(); i != e; ++i)
218 LLIArgs.push_back(Args[i].c_str());
219 LLIArgs.push_back(0);
221 outs() << "<lli>"; outs().flush();
222 DEBUG(errs() << "\nAbout to run:\t";
223 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
224 errs() << " " << LLIArgs[i];
227 return RunProgramWithTimeout(LLIPath, &LLIArgs[0],
228 InputFile, OutputFile, OutputFile,
229 Timeout, MemoryLimit, Error);
232 void AbstractInterpreter::anchor() { }
234 /// Prepend the path to the program being executed
235 /// to \p ExeName, given the value of argv[0] and the address of main()
236 /// itself. This allows us to find another LLVM tool if it is built in the same
237 /// directory. An empty string is returned on error; note that this function
238 /// just mainpulates the path and doesn't check for executability.
239 /// @brief Find a named executable.
240 static std::string PrependMainExecutablePath(const std::string &ExeName,
243 // Check the directory that the calling program is in. We can do
244 // this if ProgramPath contains at least one / character, indicating that it
245 // is a relative path to the executable itself.
246 sys::Path Result = sys::Path::GetMainExecutable(Argv0, MainAddr);
247 Result.eraseComponent();
249 if (!Result.isEmpty()) {
250 Result.appendComponent(ExeName);
251 Result.appendSuffix(sys::Path::GetEXESuffix());
257 // LLI create method - Try to find the LLI executable
258 AbstractInterpreter *AbstractInterpreter::createLLI(const char *Argv0,
259 std::string &Message,
260 const std::vector<std::string> *ToolArgs) {
261 std::string LLIPath =
262 PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t) & createLLI);
263 if (!LLIPath.empty()) {
264 Message = "Found lli: " + LLIPath + "\n";
265 return new LLI(LLIPath, ToolArgs);
268 Message = "Cannot find `lli' in executable directory!\n";
272 //===---------------------------------------------------------------------===//
273 // Custom compiler command implementation of AbstractIntepreter interface
275 // Allows using a custom command for compiling the bitcode, thus allows, for
276 // example, to compile a bitcode fragment without linking or executing, then
277 // using a custom wrapper script to check for compiler errors.
279 class CustomCompiler : public AbstractInterpreter {
280 std::string CompilerCommand;
281 std::vector<std::string> CompilerArgs;
284 const std::string &CompilerCmd, std::vector<std::string> CompArgs) :
285 CompilerCommand(CompilerCmd), CompilerArgs(CompArgs) {}
287 virtual void compileProgram(const std::string &Bitcode,
289 unsigned Timeout = 0,
290 unsigned MemoryLimit = 0);
292 virtual int ExecuteProgram(const std::string &Bitcode,
293 const std::vector<std::string> &Args,
294 const std::string &InputFile,
295 const std::string &OutputFile,
297 const std::vector<std::string> &GCCArgs =
298 std::vector<std::string>(),
299 const std::vector<std::string> &SharedLibs =
300 std::vector<std::string>(),
301 unsigned Timeout = 0,
302 unsigned MemoryLimit = 0) {
303 *Error = "Execution not supported with -compile-custom";
309 void CustomCompiler::compileProgram(const std::string &Bitcode,
312 unsigned MemoryLimit) {
314 std::vector<const char*> ProgramArgs;
315 ProgramArgs.push_back(CompilerCommand.c_str());
317 for (std::size_t i = 0; i < CompilerArgs.size(); ++i)
318 ProgramArgs.push_back(CompilerArgs.at(i).c_str());
319 ProgramArgs.push_back(Bitcode.c_str());
320 ProgramArgs.push_back(0);
322 // Add optional parameters to the running program from Argv
323 for (unsigned i = 0, e = CompilerArgs.size(); i != e; ++i)
324 ProgramArgs.push_back(CompilerArgs[i].c_str());
326 if (RunProgramWithTimeout(CompilerCommand, &ProgramArgs[0],
328 Timeout, MemoryLimit, Error))
329 *Error = ProcessFailure(CompilerCommand, &ProgramArgs[0],
330 Timeout, MemoryLimit);
333 //===---------------------------------------------------------------------===//
334 // Custom execution command implementation of AbstractIntepreter interface
336 // Allows using a custom command for executing the bitcode, thus allows,
337 // for example, to invoke a cross compiler for code generation followed by
338 // a simulator that executes the generated binary.
340 class CustomExecutor : public AbstractInterpreter {
341 std::string ExecutionCommand;
342 std::vector<std::string> ExecutorArgs;
345 const std::string &ExecutionCmd, std::vector<std::string> ExecArgs) :
346 ExecutionCommand(ExecutionCmd), ExecutorArgs(ExecArgs) {}
348 virtual int ExecuteProgram(const std::string &Bitcode,
349 const std::vector<std::string> &Args,
350 const std::string &InputFile,
351 const std::string &OutputFile,
353 const std::vector<std::string> &GCCArgs,
354 const std::vector<std::string> &SharedLibs =
355 std::vector<std::string>(),
356 unsigned Timeout = 0,
357 unsigned MemoryLimit = 0);
361 int CustomExecutor::ExecuteProgram(const std::string &Bitcode,
362 const std::vector<std::string> &Args,
363 const std::string &InputFile,
364 const std::string &OutputFile,
366 const std::vector<std::string> &GCCArgs,
367 const std::vector<std::string> &SharedLibs,
369 unsigned MemoryLimit) {
371 std::vector<const char*> ProgramArgs;
372 ProgramArgs.push_back(ExecutionCommand.c_str());
374 for (std::size_t i = 0; i < ExecutorArgs.size(); ++i)
375 ProgramArgs.push_back(ExecutorArgs.at(i).c_str());
376 ProgramArgs.push_back(Bitcode.c_str());
377 ProgramArgs.push_back(0);
379 // Add optional parameters to the running program from Argv
380 for (unsigned i = 0, e = Args.size(); i != e; ++i)
381 ProgramArgs.push_back(Args[i].c_str());
383 return RunProgramWithTimeout(
385 &ProgramArgs[0], InputFile, OutputFile,
386 OutputFile, Timeout, MemoryLimit, Error);
389 // Tokenize the CommandLine to the command and the args to allow
390 // defining a full command line as the command instead of just the
391 // executed program. We cannot just pass the whole string after the command
392 // as a single argument because then program sees only a single
393 // command line argument (with spaces in it: "foo bar" instead
394 // of "foo" and "bar").
396 // code borrowed from:
397 // http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
398 static void lexCommand(std::string &Message, const std::string &CommandLine,
399 std::string &CmdPath, std::vector<std::string> Args) {
401 std::string Command = "";
402 std::string delimiters = " ";
404 std::string::size_type lastPos = CommandLine.find_first_not_of(delimiters, 0);
405 std::string::size_type pos = CommandLine.find_first_of(delimiters, lastPos);
407 while (std::string::npos != pos || std::string::npos != lastPos) {
408 std::string token = CommandLine.substr(lastPos, pos - lastPos);
412 Args.push_back(token);
413 // Skip delimiters. Note the "not_of"
414 lastPos = CommandLine.find_first_not_of(delimiters, pos);
415 // Find next "non-delimiter"
416 pos = CommandLine.find_first_of(delimiters, lastPos);
419 CmdPath = sys::FindProgramByName(Command);
420 if (CmdPath.empty()) {
422 std::string("Cannot find '") + Command +
427 Message = "Found command in: " + CmdPath + "\n";
430 // Custom execution environment create method, takes the execution command
432 AbstractInterpreter *AbstractInterpreter::createCustomCompiler(
433 std::string &Message,
434 const std::string &CompileCommandLine) {
437 std::vector<std::string> Args;
438 lexCommand(Message, CompileCommandLine, CmdPath, Args);
442 return new CustomCompiler(CmdPath, Args);
445 // Custom execution environment create method, takes the execution command
447 AbstractInterpreter *AbstractInterpreter::createCustomExecutor(
448 std::string &Message,
449 const std::string &ExecCommandLine) {
453 std::vector<std::string> Args;
454 lexCommand(Message, ExecCommandLine, CmdPath, Args);
458 return new CustomExecutor(CmdPath, Args);
461 //===----------------------------------------------------------------------===//
462 // LLC Implementation of AbstractIntepreter interface
464 GCC::FileType LLC::OutputCode(const std::string &Bitcode,
465 std::string &OutputAsmFile, std::string &Error,
466 unsigned Timeout, unsigned MemoryLimit) {
467 const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s");
468 sys::Path uniqueFile(Bitcode + Suffix);
470 if (uniqueFile.makeUnique(true, &ErrMsg)) {
471 errs() << "Error making unique filename: " << ErrMsg << "\n";
474 OutputAsmFile = uniqueFile.str();
475 std::vector<const char *> LLCArgs;
476 LLCArgs.push_back(LLCPath.c_str());
478 // Add any extra LLC args.
479 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
480 LLCArgs.push_back(ToolArgs[i].c_str());
482 LLCArgs.push_back("-o");
483 LLCArgs.push_back(OutputAsmFile.c_str()); // Output to the Asm file
484 LLCArgs.push_back(Bitcode.c_str()); // This is the input bitcode
486 if (UseIntegratedAssembler)
487 LLCArgs.push_back("-filetype=obj");
489 LLCArgs.push_back (0);
491 outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>");
493 DEBUG(errs() << "\nAbout to run:\t";
494 for (unsigned i = 0, e = LLCArgs.size()-1; i != e; ++i)
495 errs() << " " << LLCArgs[i];
498 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0],
500 Timeout, MemoryLimit))
501 Error = ProcessFailure(LLCPath, &LLCArgs[0],
502 Timeout, MemoryLimit);
503 return UseIntegratedAssembler ? GCC::ObjectFile : GCC::AsmFile;
506 void LLC::compileProgram(const std::string &Bitcode, std::string *Error,
507 unsigned Timeout, unsigned MemoryLimit) {
508 std::string OutputAsmFile;
509 OutputCode(Bitcode, OutputAsmFile, *Error, Timeout, MemoryLimit);
510 sys::fs::remove(OutputAsmFile);
513 int LLC::ExecuteProgram(const std::string &Bitcode,
514 const std::vector<std::string> &Args,
515 const std::string &InputFile,
516 const std::string &OutputFile,
518 const std::vector<std::string> &ArgsForGCC,
519 const std::vector<std::string> &SharedLibs,
521 unsigned MemoryLimit) {
523 std::string OutputAsmFile;
524 GCC::FileType FileKind = OutputCode(Bitcode, OutputAsmFile, *Error, Timeout,
526 FileRemover OutFileRemover(OutputAsmFile, !SaveTemps);
528 std::vector<std::string> GCCArgs(ArgsForGCC);
529 GCCArgs.insert(GCCArgs.end(), SharedLibs.begin(), SharedLibs.end());
531 // Assuming LLC worked, compile the result with GCC and run it.
532 return gcc->ExecuteProgram(OutputAsmFile, Args, FileKind,
533 InputFile, OutputFile, Error, GCCArgs,
534 Timeout, MemoryLimit);
537 /// createLLC - Try to find the LLC executable
539 LLC *AbstractInterpreter::createLLC(const char *Argv0,
540 std::string &Message,
541 const std::string &GCCBinary,
542 const std::vector<std::string> *Args,
543 const std::vector<std::string> *GCCArgs,
544 bool UseIntegratedAssembler) {
545 std::string LLCPath =
546 PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t) & createLLC);
547 if (LLCPath.empty()) {
548 Message = "Cannot find `llc' in executable directory!\n";
552 GCC *gcc = GCC::create(Message, GCCBinary, GCCArgs);
554 errs() << Message << "\n";
557 Message = "Found llc: " + LLCPath + "\n";
558 return new LLC(LLCPath, gcc, Args, UseIntegratedAssembler);
561 //===---------------------------------------------------------------------===//
562 // JIT Implementation of AbstractIntepreter interface
565 class JIT : public AbstractInterpreter {
566 std::string LLIPath; // The path to the LLI executable
567 std::vector<std::string> ToolArgs; // Args to pass to LLI
569 JIT(const std::string &Path, const std::vector<std::string> *Args)
572 if (Args) { ToolArgs = *Args; }
575 virtual int ExecuteProgram(const std::string &Bitcode,
576 const std::vector<std::string> &Args,
577 const std::string &InputFile,
578 const std::string &OutputFile,
580 const std::vector<std::string> &GCCArgs =
581 std::vector<std::string>(),
582 const std::vector<std::string> &SharedLibs =
583 std::vector<std::string>(),
584 unsigned Timeout = 0,
585 unsigned MemoryLimit = 0);
589 int JIT::ExecuteProgram(const std::string &Bitcode,
590 const std::vector<std::string> &Args,
591 const std::string &InputFile,
592 const std::string &OutputFile,
594 const std::vector<std::string> &GCCArgs,
595 const std::vector<std::string> &SharedLibs,
597 unsigned MemoryLimit) {
598 // Construct a vector of parameters, incorporating those from the command-line
599 std::vector<const char*> JITArgs;
600 JITArgs.push_back(LLIPath.c_str());
601 JITArgs.push_back("-force-interpreter=false");
603 // Add any extra LLI args.
604 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
605 JITArgs.push_back(ToolArgs[i].c_str());
607 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
608 JITArgs.push_back("-load");
609 JITArgs.push_back(SharedLibs[i].c_str());
611 JITArgs.push_back(Bitcode.c_str());
612 // Add optional parameters to the running program from Argv
613 for (unsigned i=0, e = Args.size(); i != e; ++i)
614 JITArgs.push_back(Args[i].c_str());
615 JITArgs.push_back(0);
617 outs() << "<jit>"; outs().flush();
618 DEBUG(errs() << "\nAbout to run:\t";
619 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
620 errs() << " " << JITArgs[i];
623 DEBUG(errs() << "\nSending output to " << OutputFile << "\n");
624 return RunProgramWithTimeout(LLIPath, &JITArgs[0],
625 InputFile, OutputFile, OutputFile,
626 Timeout, MemoryLimit, Error);
629 /// createJIT - Try to find the LLI executable
631 AbstractInterpreter *AbstractInterpreter::createJIT(const char *Argv0,
632 std::string &Message, const std::vector<std::string> *Args) {
633 std::string LLIPath =
634 PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t) & createJIT);
635 if (!LLIPath.empty()) {
636 Message = "Found lli: " + LLIPath + "\n";
637 return new JIT(LLIPath, Args);
640 Message = "Cannot find `lli' in executable directory!\n";
644 //===---------------------------------------------------------------------===//
648 static bool IsARMArchitecture(std::vector<const char*> Args) {
649 for (std::vector<const char*>::const_iterator
650 I = Args.begin(), E = Args.end(); I != E; ++I) {
651 if (StringRef(*I).equals_lower("-arch")) {
653 if (I != E && StringRef(*I).substr(0, strlen("arm")).equals_lower("arm"))
661 int GCC::ExecuteProgram(const std::string &ProgramFile,
662 const std::vector<std::string> &Args,
664 const std::string &InputFile,
665 const std::string &OutputFile,
667 const std::vector<std::string> &ArgsForGCC,
669 unsigned MemoryLimit) {
670 std::vector<const char*> GCCArgs;
672 GCCArgs.push_back(GCCPath.c_str());
674 if (TargetTriple.getArch() == Triple::x86)
675 GCCArgs.push_back("-m32");
677 for (std::vector<std::string>::const_iterator
678 I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I)
679 GCCArgs.push_back(I->c_str());
681 // Specify -x explicitly in case the extension is wonky
682 if (fileType != ObjectFile) {
683 GCCArgs.push_back("-x");
684 if (fileType == CFile) {
685 GCCArgs.push_back("c");
686 GCCArgs.push_back("-fno-strict-aliasing");
688 GCCArgs.push_back("assembler");
690 // For ARM architectures we don't want this flag. bugpoint isn't
691 // explicitly told what architecture it is working on, so we get
693 if (TargetTriple.isOSDarwin() && !IsARMArchitecture(GCCArgs))
694 GCCArgs.push_back("-force_cpusubtype_ALL");
698 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename.
700 GCCArgs.push_back("-x");
701 GCCArgs.push_back("none");
702 GCCArgs.push_back("-o");
703 sys::Path OutputBinary (ProgramFile+".gcc.exe");
705 if (OutputBinary.makeUnique(true, &ErrMsg)) {
706 errs() << "Error making unique filename: " << ErrMsg << "\n";
709 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
711 // Add any arguments intended for GCC. We locate them here because this is
712 // most likely -L and -l options that need to come before other libraries but
713 // after the source. Other options won't be sensitive to placement on the
714 // command line, so this should be safe.
715 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
716 GCCArgs.push_back(ArgsForGCC[i].c_str());
718 GCCArgs.push_back("-lm"); // Hard-code the math library...
719 GCCArgs.push_back("-O2"); // Optimize the program a bit...
720 #if defined (HAVE_LINK_R)
721 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
723 if (TargetTriple.getArch() == Triple::sparc)
724 GCCArgs.push_back("-mcpu=v9");
725 GCCArgs.push_back(0); // NULL terminator
727 outs() << "<gcc>"; outs().flush();
728 DEBUG(errs() << "\nAbout to run:\t";
729 for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i)
730 errs() << " " << GCCArgs[i];
733 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "", "", "")) {
734 *Error = ProcessFailure(GCCPath, &GCCArgs[0]);
738 std::vector<const char*> ProgramArgs;
740 // Declared here so that the destructor only runs after
741 // ProgramArgs is used.
744 if (RemoteClientPath.empty())
745 ProgramArgs.push_back(OutputBinary.c_str());
747 ProgramArgs.push_back(RemoteClientPath.c_str());
748 ProgramArgs.push_back(RemoteHost.c_str());
749 if (!RemoteUser.empty()) {
750 ProgramArgs.push_back("-l");
751 ProgramArgs.push_back(RemoteUser.c_str());
753 if (!RemotePort.empty()) {
754 ProgramArgs.push_back("-p");
755 ProgramArgs.push_back(RemotePort.c_str());
757 if (!RemoteExtra.empty()) {
758 ProgramArgs.push_back(RemoteExtra.c_str());
761 // Full path to the binary. We need to cd to the exec directory because
762 // there is a dylib there that the exec expects to find in the CWD
763 char* env_pwd = getenv("PWD");
767 Exec += OutputBinary.c_str();
768 ProgramArgs.push_back(Exec.c_str());
771 // Add optional parameters to the running program from Argv
772 for (unsigned i = 0, e = Args.size(); i != e; ++i)
773 ProgramArgs.push_back(Args[i].c_str());
774 ProgramArgs.push_back(0); // NULL terminator
776 // Now that we have a binary, run it!
777 outs() << "<program>"; outs().flush();
778 DEBUG(errs() << "\nAbout to run:\t";
779 for (unsigned i = 0, e = ProgramArgs.size()-1; i != e; ++i)
780 errs() << " " << ProgramArgs[i];
784 FileRemover OutputBinaryRemover(OutputBinary.str(), !SaveTemps);
786 if (RemoteClientPath.empty()) {
787 DEBUG(errs() << "<run locally>");
788 int ExitCode = RunProgramWithTimeout(OutputBinary.str(), &ProgramArgs[0],
789 InputFile, OutputFile, OutputFile,
790 Timeout, MemoryLimit, Error);
791 // Treat a signal (usually SIGSEGV) or timeout as part of the program output
792 // so that crash-causing miscompilation is handled seamlessly.
794 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
795 outFile << *Error << '\n';
801 outs() << "<run remotely>"; outs().flush();
802 return RunProgramRemotelyWithTimeout(RemoteClientPath,
803 &ProgramArgs[0], InputFile, OutputFile,
804 OutputFile, Timeout, MemoryLimit);
808 int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
809 std::string &OutputFile,
810 const std::vector<std::string> &ArgsForGCC,
811 std::string &Error) {
812 sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
814 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
815 errs() << "Error making unique filename: " << ErrMsg << "\n";
818 OutputFile = uniqueFilename.str();
820 std::vector<const char*> GCCArgs;
822 GCCArgs.push_back(GCCPath.c_str());
824 if (TargetTriple.getArch() == Triple::x86)
825 GCCArgs.push_back("-m32");
827 for (std::vector<std::string>::const_iterator
828 I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I)
829 GCCArgs.push_back(I->c_str());
831 // Compile the C/asm file into a shared object
832 if (fileType != ObjectFile) {
833 GCCArgs.push_back("-x");
834 GCCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
836 GCCArgs.push_back("-fno-strict-aliasing");
837 GCCArgs.push_back(InputFile.c_str()); // Specify the input filename.
838 GCCArgs.push_back("-x");
839 GCCArgs.push_back("none");
840 if (TargetTriple.getArch() == Triple::sparc)
841 GCCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc
842 else if (TargetTriple.isOSDarwin()) {
843 // link all source files into a single module in data segment, rather than
844 // generating blocks. dynamic_lookup requires that you set
845 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for
846 // bugpoint to just pass that in the environment of GCC.
847 GCCArgs.push_back("-single_module");
848 GCCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC
849 GCCArgs.push_back("-undefined");
850 GCCArgs.push_back("dynamic_lookup");
852 GCCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others
854 if (TargetTriple.getArch() == Triple::x86_64)
855 GCCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC
857 if (TargetTriple.getArch() == Triple::sparc)
858 GCCArgs.push_back("-mcpu=v9");
860 GCCArgs.push_back("-o");
861 GCCArgs.push_back(OutputFile.c_str()); // Output to the right filename.
862 GCCArgs.push_back("-O2"); // Optimize the program a bit.
866 // Add any arguments intended for GCC. We locate them here because this is
867 // most likely -L and -l options that need to come before other libraries but
868 // after the source. Other options won't be sensitive to placement on the
869 // command line, so this should be safe.
870 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
871 GCCArgs.push_back(ArgsForGCC[i].c_str());
872 GCCArgs.push_back(0); // NULL terminator
876 outs() << "<gcc>"; outs().flush();
877 DEBUG(errs() << "\nAbout to run:\t";
878 for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i)
879 errs() << " " << GCCArgs[i];
882 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "", "", "")) {
883 Error = ProcessFailure(GCCPath, &GCCArgs[0]);
889 /// create - Try to find the `gcc' executable
891 GCC *GCC::create(std::string &Message,
892 const std::string &GCCBinary,
893 const std::vector<std::string> *Args) {
894 std::string GCCPath = sys::FindProgramByName(GCCBinary);
895 if (GCCPath.empty()) {
896 Message = "Cannot find `"+ GCCBinary +"' in PATH!\n";
900 std::string RemoteClientPath;
901 if (!RemoteClient.empty())
902 RemoteClientPath = sys::FindProgramByName(RemoteClient);
904 Message = "Found gcc: " + GCCPath + "\n";
905 return new GCC(GCCPath, RemoteClientPath, Args);