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/Program.h"
21 #include "llvm/Support/raw_ostream.h"
28 SaveTemps("save-temps", cl::init(false), cl::desc("Save temporary files"));
33 RemoteClient("remote-client",
34 cl::desc("Remote execution client (rsh/ssh)"));
37 RemoteHost("remote-host",
38 cl::desc("Remote execution (rsh/ssh) host"));
41 RemotePort("remote-port",
42 cl::desc("Remote execution (rsh/ssh) port"));
45 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,
61 unsigned NumSeconds = 0,
62 unsigned MemoryLimit = 0,
63 std::string *ErrMsg = 0) {
64 const sys::Path P[3] = { sys::Path(StdInFile), sys::Path(StdOutFile),
65 sys::Path(StdErrFile) };
66 const sys::Path* redirects[3];
67 for (int I = 0; I < 3; ++I)
70 #if 0 // For debug purposes
73 for (unsigned i = 0; Args[i]; ++i)
74 errs() << " " << Args[i];
79 return sys::ExecuteAndWait(sys::Path(ProgramPath), Args, 0, redirects,
80 NumSeconds, MemoryLimit, ErrMsg);
83 /// RunProgramRemotelyWithTimeout - This function runs the given program
84 /// remotely using the given remote client and the sys::Program::ExecuteAndWait.
85 /// Returns the remote program exit code or reports a remote client error if it
86 /// fails. Remote client is required to return 255 if it failed or program exit
88 /// @see sys::Program::ExecuteAndWait
89 static int RunProgramRemotelyWithTimeout(StringRef RemoteClientPath,
94 unsigned NumSeconds = 0,
95 unsigned MemoryLimit = 0) {
96 const sys::Path P[3] = { sys::Path(StdInFile), sys::Path(StdOutFile),
97 sys::Path(StdErrFile) };
98 const sys::Path* redirects[3];
99 for (int I = 0; I < 3; ++I)
100 redirects[I] = &P[I];
102 #if 0 // For debug purposes
105 for (unsigned i = 0; Args[i]; ++i)
106 errs() << " " << Args[i];
111 // Run the program remotely with the remote client
112 int ReturnCode = sys::ExecuteAndWait(sys::Path(RemoteClientPath), Args, 0,
113 redirects, NumSeconds, MemoryLimit);
115 // Has the remote client fail?
116 if (255 == ReturnCode) {
117 std::ostringstream OS;
118 OS << "\nError running remote client:\n ";
119 for (const char **Arg = Args; *Arg; ++Arg)
123 // The error message is in the output file, let's print it out from there.
124 std::string StdOutFileName = StdOutFile.str();
125 std::ifstream ErrorFile(StdOutFileName.c_str());
127 std::copy(std::istreambuf_iterator<char>(ErrorFile),
128 std::istreambuf_iterator<char>(),
129 std::ostreambuf_iterator<char>(OS));
139 static std::string ProcessFailure(StringRef ProgPath, const char** Args,
140 unsigned Timeout = 0,
141 unsigned MemoryLimit = 0) {
142 std::ostringstream OS;
143 OS << "\nError running tool:\n ";
144 for (const char **Arg = Args; *Arg; ++Arg)
148 // Rerun the compiler, capturing any error messages to print them.
149 sys::Path ErrorFilename("bugpoint.program_error_messages");
151 if (ErrorFilename.makeUnique(true, &ErrMsg)) {
152 errs() << "Error making unique filename: " << ErrMsg << "\n";
155 RunProgramWithTimeout(ProgPath, Args, "", ErrorFilename.str(),
156 ErrorFilename.str(), Timeout, MemoryLimit);
157 // FIXME: check return code ?
159 // Print out the error messages generated by GCC if possible...
160 std::ifstream ErrorFile(ErrorFilename.c_str());
162 std::copy(std::istreambuf_iterator<char>(ErrorFile),
163 std::istreambuf_iterator<char>(),
164 std::ostreambuf_iterator<char>(OS));
168 ErrorFilename.eraseFromDisk();
172 //===---------------------------------------------------------------------===//
173 // LLI Implementation of AbstractIntepreter interface
176 class LLI : public AbstractInterpreter {
177 std::string LLIPath; // The path to the LLI executable
178 std::vector<std::string> ToolArgs; // Args to pass to LLI
180 LLI(const std::string &Path, const std::vector<std::string> *Args)
183 if (Args) { ToolArgs = *Args; }
186 virtual int ExecuteProgram(const std::string &Bitcode,
187 const std::vector<std::string> &Args,
188 const std::string &InputFile,
189 const std::string &OutputFile,
191 const std::vector<std::string> &GCCArgs,
192 const std::vector<std::string> &SharedLibs =
193 std::vector<std::string>(),
194 unsigned Timeout = 0,
195 unsigned MemoryLimit = 0);
199 int LLI::ExecuteProgram(const std::string &Bitcode,
200 const std::vector<std::string> &Args,
201 const std::string &InputFile,
202 const std::string &OutputFile,
204 const std::vector<std::string> &GCCArgs,
205 const std::vector<std::string> &SharedLibs,
207 unsigned MemoryLimit) {
208 std::vector<const char*> LLIArgs;
209 LLIArgs.push_back(LLIPath.c_str());
210 LLIArgs.push_back("-force-interpreter=true");
212 for (std::vector<std::string>::const_iterator i = SharedLibs.begin(),
213 e = SharedLibs.end(); i != e; ++i) {
214 LLIArgs.push_back("-load");
215 LLIArgs.push_back((*i).c_str());
218 // Add any extra LLI args.
219 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
220 LLIArgs.push_back(ToolArgs[i].c_str());
222 LLIArgs.push_back(Bitcode.c_str());
223 // Add optional parameters to the running program from Argv
224 for (unsigned i=0, e = Args.size(); i != e; ++i)
225 LLIArgs.push_back(Args[i].c_str());
226 LLIArgs.push_back(0);
228 outs() << "<lli>"; outs().flush();
229 DEBUG(errs() << "\nAbout to run:\t";
230 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
231 errs() << " " << LLIArgs[i];
234 return RunProgramWithTimeout(LLIPath, &LLIArgs[0],
235 InputFile, OutputFile, OutputFile,
236 Timeout, MemoryLimit, Error);
239 void AbstractInterpreter::anchor() { }
241 // LLI create method - Try to find the LLI executable
242 AbstractInterpreter *AbstractInterpreter::createLLI(const char *Argv0,
243 std::string &Message,
244 const std::vector<std::string> *ToolArgs) {
245 std::string LLIPath =
246 PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createLLI).str();
247 if (!LLIPath.empty()) {
248 Message = "Found lli: " + LLIPath + "\n";
249 return new LLI(LLIPath, ToolArgs);
252 Message = "Cannot find `lli' in executable directory!\n";
256 //===---------------------------------------------------------------------===//
257 // Custom compiler command implementation of AbstractIntepreter interface
259 // Allows using a custom command for compiling the bitcode, thus allows, for
260 // example, to compile a bitcode fragment without linking or executing, then
261 // using a custom wrapper script to check for compiler errors.
263 class CustomCompiler : public AbstractInterpreter {
264 std::string CompilerCommand;
265 std::vector<std::string> CompilerArgs;
268 const std::string &CompilerCmd, std::vector<std::string> CompArgs) :
269 CompilerCommand(CompilerCmd), CompilerArgs(CompArgs) {}
271 virtual void compileProgram(const std::string &Bitcode,
273 unsigned Timeout = 0,
274 unsigned MemoryLimit = 0);
276 virtual int ExecuteProgram(const std::string &Bitcode,
277 const std::vector<std::string> &Args,
278 const std::string &InputFile,
279 const std::string &OutputFile,
281 const std::vector<std::string> &GCCArgs =
282 std::vector<std::string>(),
283 const std::vector<std::string> &SharedLibs =
284 std::vector<std::string>(),
285 unsigned Timeout = 0,
286 unsigned MemoryLimit = 0) {
287 *Error = "Execution not supported with -compile-custom";
293 void CustomCompiler::compileProgram(const std::string &Bitcode,
296 unsigned MemoryLimit) {
298 std::vector<const char*> ProgramArgs;
299 ProgramArgs.push_back(CompilerCommand.c_str());
301 for (std::size_t i = 0; i < CompilerArgs.size(); ++i)
302 ProgramArgs.push_back(CompilerArgs.at(i).c_str());
303 ProgramArgs.push_back(Bitcode.c_str());
304 ProgramArgs.push_back(0);
306 // Add optional parameters to the running program from Argv
307 for (unsigned i = 0, e = CompilerArgs.size(); i != e; ++i)
308 ProgramArgs.push_back(CompilerArgs[i].c_str());
310 if (RunProgramWithTimeout(CompilerCommand, &ProgramArgs[0],
312 Timeout, MemoryLimit, Error))
313 *Error = ProcessFailure(CompilerCommand, &ProgramArgs[0],
314 Timeout, MemoryLimit);
317 //===---------------------------------------------------------------------===//
318 // Custom execution command implementation of AbstractIntepreter interface
320 // Allows using a custom command for executing the bitcode, thus allows,
321 // for example, to invoke a cross compiler for code generation followed by
322 // a simulator that executes the generated binary.
324 class CustomExecutor : public AbstractInterpreter {
325 std::string ExecutionCommand;
326 std::vector<std::string> ExecutorArgs;
329 const std::string &ExecutionCmd, std::vector<std::string> ExecArgs) :
330 ExecutionCommand(ExecutionCmd), ExecutorArgs(ExecArgs) {}
332 virtual int ExecuteProgram(const std::string &Bitcode,
333 const std::vector<std::string> &Args,
334 const std::string &InputFile,
335 const std::string &OutputFile,
337 const std::vector<std::string> &GCCArgs,
338 const std::vector<std::string> &SharedLibs =
339 std::vector<std::string>(),
340 unsigned Timeout = 0,
341 unsigned MemoryLimit = 0);
345 int CustomExecutor::ExecuteProgram(const std::string &Bitcode,
346 const std::vector<std::string> &Args,
347 const std::string &InputFile,
348 const std::string &OutputFile,
350 const std::vector<std::string> &GCCArgs,
351 const std::vector<std::string> &SharedLibs,
353 unsigned MemoryLimit) {
355 std::vector<const char*> ProgramArgs;
356 ProgramArgs.push_back(ExecutionCommand.c_str());
358 for (std::size_t i = 0; i < ExecutorArgs.size(); ++i)
359 ProgramArgs.push_back(ExecutorArgs.at(i).c_str());
360 ProgramArgs.push_back(Bitcode.c_str());
361 ProgramArgs.push_back(0);
363 // Add optional parameters to the running program from Argv
364 for (unsigned i = 0, e = Args.size(); i != e; ++i)
365 ProgramArgs.push_back(Args[i].c_str());
367 return RunProgramWithTimeout(
369 &ProgramArgs[0], InputFile, OutputFile,
370 OutputFile, Timeout, MemoryLimit, Error);
373 // Tokenize the CommandLine to the command and the args to allow
374 // defining a full command line as the command instead of just the
375 // executed program. We cannot just pass the whole string after the command
376 // as a single argument because then program sees only a single
377 // command line argument (with spaces in it: "foo bar" instead
378 // of "foo" and "bar").
380 // code borrowed from:
381 // http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
382 static void lexCommand(std::string &Message, const std::string &CommandLine,
383 std::string &CmdPath, std::vector<std::string> Args) {
385 std::string Command = "";
386 std::string delimiters = " ";
388 std::string::size_type lastPos = CommandLine.find_first_not_of(delimiters, 0);
389 std::string::size_type pos = CommandLine.find_first_of(delimiters, lastPos);
391 while (std::string::npos != pos || std::string::npos != lastPos) {
392 std::string token = CommandLine.substr(lastPos, pos - lastPos);
396 Args.push_back(token);
397 // Skip delimiters. Note the "not_of"
398 lastPos = CommandLine.find_first_not_of(delimiters, pos);
399 // Find next "non-delimiter"
400 pos = CommandLine.find_first_of(delimiters, lastPos);
403 CmdPath = sys::FindProgramByName(Command);
404 if (CmdPath.empty()) {
406 std::string("Cannot find '") + Command +
411 Message = "Found command in: " + CmdPath + "\n";
414 // Custom execution environment create method, takes the execution command
416 AbstractInterpreter *AbstractInterpreter::createCustomCompiler(
417 std::string &Message,
418 const std::string &CompileCommandLine) {
421 std::vector<std::string> Args;
422 lexCommand(Message, CompileCommandLine, CmdPath, Args);
426 return new CustomCompiler(CmdPath, Args);
429 // Custom execution environment create method, takes the execution command
431 AbstractInterpreter *AbstractInterpreter::createCustomExecutor(
432 std::string &Message,
433 const std::string &ExecCommandLine) {
437 std::vector<std::string> Args;
438 lexCommand(Message, ExecCommandLine, CmdPath, Args);
442 return new CustomExecutor(CmdPath, Args);
445 //===----------------------------------------------------------------------===//
446 // LLC Implementation of AbstractIntepreter interface
448 GCC::FileType LLC::OutputCode(const std::string &Bitcode,
449 sys::Path &OutputAsmFile, std::string &Error,
450 unsigned Timeout, unsigned MemoryLimit) {
451 const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s");
452 sys::Path uniqueFile(Bitcode + Suffix);
454 if (uniqueFile.makeUnique(true, &ErrMsg)) {
455 errs() << "Error making unique filename: " << ErrMsg << "\n";
458 OutputAsmFile = uniqueFile;
459 std::vector<const char *> LLCArgs;
460 LLCArgs.push_back(LLCPath.c_str());
462 // Add any extra LLC args.
463 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
464 LLCArgs.push_back(ToolArgs[i].c_str());
466 LLCArgs.push_back("-o");
467 LLCArgs.push_back(OutputAsmFile.c_str()); // Output to the Asm file
468 LLCArgs.push_back(Bitcode.c_str()); // This is the input bitcode
470 if (UseIntegratedAssembler)
471 LLCArgs.push_back("-filetype=obj");
473 LLCArgs.push_back (0);
475 outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>");
477 DEBUG(errs() << "\nAbout to run:\t";
478 for (unsigned i = 0, e = LLCArgs.size()-1; i != e; ++i)
479 errs() << " " << LLCArgs[i];
482 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0],
484 Timeout, MemoryLimit))
485 Error = ProcessFailure(LLCPath, &LLCArgs[0],
486 Timeout, MemoryLimit);
487 return UseIntegratedAssembler ? GCC::ObjectFile : GCC::AsmFile;
490 void LLC::compileProgram(const std::string &Bitcode, std::string *Error,
491 unsigned Timeout, unsigned MemoryLimit) {
492 sys::Path OutputAsmFile;
493 OutputCode(Bitcode, OutputAsmFile, *Error, Timeout, MemoryLimit);
494 OutputAsmFile.eraseFromDisk();
497 int LLC::ExecuteProgram(const std::string &Bitcode,
498 const std::vector<std::string> &Args,
499 const std::string &InputFile,
500 const std::string &OutputFile,
502 const std::vector<std::string> &ArgsForGCC,
503 const std::vector<std::string> &SharedLibs,
505 unsigned MemoryLimit) {
507 sys::Path OutputAsmFile;
508 GCC::FileType FileKind = OutputCode(Bitcode, OutputAsmFile, *Error, Timeout,
510 FileRemover OutFileRemover(OutputAsmFile.str(), !SaveTemps);
512 std::vector<std::string> GCCArgs(ArgsForGCC);
513 GCCArgs.insert(GCCArgs.end(), SharedLibs.begin(), SharedLibs.end());
515 // Assuming LLC worked, compile the result with GCC and run it.
516 return gcc->ExecuteProgram(OutputAsmFile.str(), Args, FileKind,
517 InputFile, OutputFile, Error, GCCArgs,
518 Timeout, MemoryLimit);
521 /// createLLC - Try to find the LLC executable
523 LLC *AbstractInterpreter::createLLC(const char *Argv0,
524 std::string &Message,
525 const std::string &GCCBinary,
526 const std::vector<std::string> *Args,
527 const std::vector<std::string> *GCCArgs,
528 bool UseIntegratedAssembler) {
529 std::string LLCPath =
530 PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t)&createLLC).str();
531 if (LLCPath.empty()) {
532 Message = "Cannot find `llc' in executable directory!\n";
536 GCC *gcc = GCC::create(Message, GCCBinary, GCCArgs);
538 errs() << Message << "\n";
541 Message = "Found llc: " + LLCPath + "\n";
542 return new LLC(LLCPath, gcc, Args, UseIntegratedAssembler);
545 //===---------------------------------------------------------------------===//
546 // JIT Implementation of AbstractIntepreter interface
549 class JIT : public AbstractInterpreter {
550 std::string LLIPath; // The path to the LLI executable
551 std::vector<std::string> ToolArgs; // Args to pass to LLI
553 JIT(const std::string &Path, const std::vector<std::string> *Args)
556 if (Args) { ToolArgs = *Args; }
559 virtual int ExecuteProgram(const std::string &Bitcode,
560 const std::vector<std::string> &Args,
561 const std::string &InputFile,
562 const std::string &OutputFile,
564 const std::vector<std::string> &GCCArgs =
565 std::vector<std::string>(),
566 const std::vector<std::string> &SharedLibs =
567 std::vector<std::string>(),
568 unsigned Timeout = 0,
569 unsigned MemoryLimit = 0);
573 int JIT::ExecuteProgram(const std::string &Bitcode,
574 const std::vector<std::string> &Args,
575 const std::string &InputFile,
576 const std::string &OutputFile,
578 const std::vector<std::string> &GCCArgs,
579 const std::vector<std::string> &SharedLibs,
581 unsigned MemoryLimit) {
582 // Construct a vector of parameters, incorporating those from the command-line
583 std::vector<const char*> JITArgs;
584 JITArgs.push_back(LLIPath.c_str());
585 JITArgs.push_back("-force-interpreter=false");
587 // Add any extra LLI args.
588 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
589 JITArgs.push_back(ToolArgs[i].c_str());
591 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
592 JITArgs.push_back("-load");
593 JITArgs.push_back(SharedLibs[i].c_str());
595 JITArgs.push_back(Bitcode.c_str());
596 // Add optional parameters to the running program from Argv
597 for (unsigned i=0, e = Args.size(); i != e; ++i)
598 JITArgs.push_back(Args[i].c_str());
599 JITArgs.push_back(0);
601 outs() << "<jit>"; outs().flush();
602 DEBUG(errs() << "\nAbout to run:\t";
603 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
604 errs() << " " << JITArgs[i];
607 DEBUG(errs() << "\nSending output to " << OutputFile << "\n");
608 return RunProgramWithTimeout(LLIPath, &JITArgs[0],
609 InputFile, OutputFile, OutputFile,
610 Timeout, MemoryLimit, Error);
613 /// createJIT - Try to find the LLI executable
615 AbstractInterpreter *AbstractInterpreter::createJIT(const char *Argv0,
616 std::string &Message, const std::vector<std::string> *Args) {
617 std::string LLIPath =
618 PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createJIT).str();
619 if (!LLIPath.empty()) {
620 Message = "Found lli: " + LLIPath + "\n";
621 return new JIT(LLIPath, Args);
624 Message = "Cannot find `lli' in executable directory!\n";
628 //===---------------------------------------------------------------------===//
632 static bool IsARMArchitecture(std::vector<const char*> Args) {
633 for (std::vector<const char*>::const_iterator
634 I = Args.begin(), E = Args.end(); I != E; ++I) {
635 if (StringRef(*I).equals_lower("-arch")) {
637 if (I != E && StringRef(*I).substr(0, strlen("arm")).equals_lower("arm"))
645 int GCC::ExecuteProgram(const std::string &ProgramFile,
646 const std::vector<std::string> &Args,
648 const std::string &InputFile,
649 const std::string &OutputFile,
651 const std::vector<std::string> &ArgsForGCC,
653 unsigned MemoryLimit) {
654 std::vector<const char*> GCCArgs;
656 GCCArgs.push_back(GCCPath.c_str());
658 if (TargetTriple.getArch() == Triple::x86)
659 GCCArgs.push_back("-m32");
661 for (std::vector<std::string>::const_iterator
662 I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I)
663 GCCArgs.push_back(I->c_str());
665 // Specify -x explicitly in case the extension is wonky
666 if (fileType != ObjectFile) {
667 GCCArgs.push_back("-x");
668 if (fileType == CFile) {
669 GCCArgs.push_back("c");
670 GCCArgs.push_back("-fno-strict-aliasing");
672 GCCArgs.push_back("assembler");
674 // For ARM architectures we don't want this flag. bugpoint isn't
675 // explicitly told what architecture it is working on, so we get
677 if (TargetTriple.isOSDarwin() && !IsARMArchitecture(GCCArgs))
678 GCCArgs.push_back("-force_cpusubtype_ALL");
682 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename.
684 GCCArgs.push_back("-x");
685 GCCArgs.push_back("none");
686 GCCArgs.push_back("-o");
687 sys::Path OutputBinary (ProgramFile+".gcc.exe");
689 if (OutputBinary.makeUnique(true, &ErrMsg)) {
690 errs() << "Error making unique filename: " << ErrMsg << "\n";
693 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
695 // Add any arguments intended for GCC. We locate them here because this is
696 // most likely -L and -l options that need to come before other libraries but
697 // after the source. Other options won't be sensitive to placement on the
698 // command line, so this should be safe.
699 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
700 GCCArgs.push_back(ArgsForGCC[i].c_str());
702 GCCArgs.push_back("-lm"); // Hard-code the math library...
703 GCCArgs.push_back("-O2"); // Optimize the program a bit...
704 #if defined (HAVE_LINK_R)
705 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
707 if (TargetTriple.getArch() == Triple::sparc)
708 GCCArgs.push_back("-mcpu=v9");
709 GCCArgs.push_back(0); // NULL terminator
711 outs() << "<gcc>"; outs().flush();
712 DEBUG(errs() << "\nAbout to run:\t";
713 for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i)
714 errs() << " " << GCCArgs[i];
717 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "", "", "")) {
718 *Error = ProcessFailure(GCCPath, &GCCArgs[0]);
722 std::vector<const char*> ProgramArgs;
724 // Declared here so that the destructor only runs after
725 // ProgramArgs is used.
728 if (RemoteClientPath.empty())
729 ProgramArgs.push_back(OutputBinary.c_str());
731 ProgramArgs.push_back(RemoteClientPath.c_str());
732 ProgramArgs.push_back(RemoteHost.c_str());
733 if (!RemoteUser.empty()) {
734 ProgramArgs.push_back("-l");
735 ProgramArgs.push_back(RemoteUser.c_str());
737 if (!RemotePort.empty()) {
738 ProgramArgs.push_back("-p");
739 ProgramArgs.push_back(RemotePort.c_str());
741 if (!RemoteExtra.empty()) {
742 ProgramArgs.push_back(RemoteExtra.c_str());
745 // Full path to the binary. We need to cd to the exec directory because
746 // there is a dylib there that the exec expects to find in the CWD
747 char* env_pwd = getenv("PWD");
751 Exec += OutputBinary.c_str();
752 ProgramArgs.push_back(Exec.c_str());
755 // Add optional parameters to the running program from Argv
756 for (unsigned i = 0, e = Args.size(); i != e; ++i)
757 ProgramArgs.push_back(Args[i].c_str());
758 ProgramArgs.push_back(0); // NULL terminator
760 // Now that we have a binary, run it!
761 outs() << "<program>"; outs().flush();
762 DEBUG(errs() << "\nAbout to run:\t";
763 for (unsigned i = 0, e = ProgramArgs.size()-1; i != e; ++i)
764 errs() << " " << ProgramArgs[i];
768 FileRemover OutputBinaryRemover(OutputBinary.str(), !SaveTemps);
770 if (RemoteClientPath.empty()) {
771 DEBUG(errs() << "<run locally>");
772 int ExitCode = RunProgramWithTimeout(OutputBinary.str(), &ProgramArgs[0],
773 InputFile, OutputFile, OutputFile,
774 Timeout, MemoryLimit, Error);
775 // Treat a signal (usually SIGSEGV) or timeout as part of the program output
776 // so that crash-causing miscompilation is handled seamlessly.
778 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
779 outFile << *Error << '\n';
785 outs() << "<run remotely>"; outs().flush();
786 return RunProgramRemotelyWithTimeout(RemoteClientPath,
787 &ProgramArgs[0], InputFile, OutputFile,
788 OutputFile, Timeout, MemoryLimit);
792 int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
793 std::string &OutputFile,
794 const std::vector<std::string> &ArgsForGCC,
795 std::string &Error) {
796 sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
798 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
799 errs() << "Error making unique filename: " << ErrMsg << "\n";
802 OutputFile = uniqueFilename.str();
804 std::vector<const char*> GCCArgs;
806 GCCArgs.push_back(GCCPath.c_str());
808 if (TargetTriple.getArch() == Triple::x86)
809 GCCArgs.push_back("-m32");
811 for (std::vector<std::string>::const_iterator
812 I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I)
813 GCCArgs.push_back(I->c_str());
815 // Compile the C/asm file into a shared object
816 if (fileType != ObjectFile) {
817 GCCArgs.push_back("-x");
818 GCCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
820 GCCArgs.push_back("-fno-strict-aliasing");
821 GCCArgs.push_back(InputFile.c_str()); // Specify the input filename.
822 GCCArgs.push_back("-x");
823 GCCArgs.push_back("none");
824 if (TargetTriple.getArch() == Triple::sparc)
825 GCCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc
826 else if (TargetTriple.isOSDarwin()) {
827 // link all source files into a single module in data segment, rather than
828 // generating blocks. dynamic_lookup requires that you set
829 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for
830 // bugpoint to just pass that in the environment of GCC.
831 GCCArgs.push_back("-single_module");
832 GCCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC
833 GCCArgs.push_back("-undefined");
834 GCCArgs.push_back("dynamic_lookup");
836 GCCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others
838 if (TargetTriple.getArch() == Triple::x86_64)
839 GCCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC
841 if (TargetTriple.getArch() == Triple::sparc)
842 GCCArgs.push_back("-mcpu=v9");
844 GCCArgs.push_back("-o");
845 GCCArgs.push_back(OutputFile.c_str()); // Output to the right filename.
846 GCCArgs.push_back("-O2"); // Optimize the program a bit.
850 // Add any arguments intended for GCC. We locate them here because this is
851 // most likely -L and -l options that need to come before other libraries but
852 // after the source. Other options won't be sensitive to placement on the
853 // command line, so this should be safe.
854 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
855 GCCArgs.push_back(ArgsForGCC[i].c_str());
856 GCCArgs.push_back(0); // NULL terminator
860 outs() << "<gcc>"; outs().flush();
861 DEBUG(errs() << "\nAbout to run:\t";
862 for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i)
863 errs() << " " << GCCArgs[i];
866 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "", "", "")) {
867 Error = ProcessFailure(GCCPath, &GCCArgs[0]);
873 /// create - Try to find the `gcc' executable
875 GCC *GCC::create(std::string &Message,
876 const std::string &GCCBinary,
877 const std::vector<std::string> *Args) {
878 std::string GCCPath = sys::FindProgramByName(GCCBinary);
879 if (GCCPath.empty()) {
880 Message = "Cannot find `"+ GCCBinary +"' in PATH!\n";
884 std::string RemoteClientPath;
885 if (!RemoteClient.empty())
886 RemoteClientPath = sys::FindProgramByName(RemoteClient);
888 Message = "Found gcc: " + GCCPath + "\n";
889 return new GCC(GCCPath, RemoteClientPath, Args);