1 //===-- ToolRunner.cpp ----------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the interfaces described in the ToolRunner.h file. 11 // 12 //===----------------------------------------------------------------------===// 13 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" 22 #include <fstream> 23 #include <sstream> 24 using namespace llvm; 25 26 namespace llvm { 27 cl::opt<bool> 28 SaveTemps("save-temps", cl::init(false), cl::desc("Save temporary files")); 29 } 30 31 namespace { 32 cl::opt<std::string> 33 RemoteClient("remote-client", 34 cl::desc("Remote execution client (rsh/ssh)")); 35 36 cl::opt<std::string> 37 RemoteHost("remote-host", 38 cl::desc("Remote execution (rsh/ssh) host")); 39 40 cl::opt<std::string> 41 RemotePort("remote-port", 42 cl::desc("Remote execution (rsh/ssh) port")); 43 44 cl::opt<std::string> 45 RemoteUser("remote-user", 46 cl::desc("Remote execution (rsh/ssh) user id")); 47 48 cl::opt<std::string> 49 RemoteExtra("remote-extra-options", 50 cl::desc("Remote execution (rsh/ssh) extra options")); 51 } 52 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 const char **Args, 58 StringRef StdInFile, 59 StringRef StdOutFile, 60 StringRef StdErrFile, 61 unsigned NumSeconds = 0, 62 unsigned MemoryLimit = 0, 63 std::string *ErrMsg = 0) { 64 const StringRef *Redirects[3] = { &StdInFile, &StdOutFile, &StdErrFile }; 65 66 #if 0 // For debug purposes 67 { 68 errs() << "RUN:"; 69 for (unsigned i = 0; Args[i]; ++i) 70 errs() << " " << Args[i]; 71 errs() << "\n"; 72 } 73 #endif 74 75 return sys::ExecuteAndWait(ProgramPath, Args, 0, Redirects, 76 NumSeconds, MemoryLimit, ErrMsg); 77 } 78 79 /// RunProgramRemotelyWithTimeout - This function runs the given program 80 /// remotely using the given remote client and the sys::Program::ExecuteAndWait. 81 /// Returns the remote program exit code or reports a remote client error if it 82 /// fails. Remote client is required to return 255 if it failed or program exit 83 /// code otherwise. 84 /// @see sys::Program::ExecuteAndWait 85 static int RunProgramRemotelyWithTimeout(StringRef RemoteClientPath, 86 const char **Args, 87 StringRef StdInFile, 88 StringRef StdOutFile, 89 StringRef StdErrFile, 90 unsigned NumSeconds = 0, 91 unsigned MemoryLimit = 0) { 92 const StringRef *Redirects[3] = { &StdInFile, &StdOutFile, &StdErrFile }; 93 94 #if 0 // For debug purposes 95 { 96 errs() << "RUN:"; 97 for (unsigned i = 0; Args[i]; ++i) 98 errs() << " " << Args[i]; 99 errs() << "\n"; 100 } 101 #endif 102 103 // Run the program remotely with the remote client 104 int ReturnCode = sys::ExecuteAndWait(RemoteClientPath, Args, 0, 105 Redirects, NumSeconds, MemoryLimit); 106 107 // Has the remote client fail? 108 if (255 == ReturnCode) { 109 std::ostringstream OS; 110 OS << "\nError running remote client:\n "; 111 for (const char **Arg = Args; *Arg; ++Arg) 112 OS << " " << *Arg; 113 OS << "\n"; 114 115 // The error message is in the output file, let's print it out from there. 116 std::string StdOutFileName = StdOutFile.str(); 117 std::ifstream ErrorFile(StdOutFileName.c_str()); 118 if (ErrorFile) { 119 std::copy(std::istreambuf_iterator<char>(ErrorFile), 120 std::istreambuf_iterator<char>(), 121 std::ostreambuf_iterator<char>(OS)); 122 ErrorFile.close(); 123 } 124 125 errs() << OS.str(); 126 } 127 128 return ReturnCode; 129 } 130 131 static std::string ProcessFailure(StringRef ProgPath, const char** Args, 132 unsigned Timeout = 0, 133 unsigned MemoryLimit = 0) { 134 std::ostringstream OS; 135 OS << "\nError running tool:\n "; 136 for (const char **Arg = Args; *Arg; ++Arg) 137 OS << " " << *Arg; 138 OS << "\n"; 139 140 // Rerun the compiler, capturing any error messages to print them. 141 sys::Path ErrorFilename("bugpoint.program_error_messages"); 142 std::string ErrMsg; 143 if (ErrorFilename.makeUnique(true, &ErrMsg)) { 144 errs() << "Error making unique filename: " << ErrMsg << "\n"; 145 exit(1); 146 } 147 RunProgramWithTimeout(ProgPath, Args, "", ErrorFilename.str(), 148 ErrorFilename.str(), Timeout, MemoryLimit); 149 // FIXME: check return code ? 150 151 // Print out the error messages generated by GCC if possible... 152 std::ifstream ErrorFile(ErrorFilename.c_str()); 153 if (ErrorFile) { 154 std::copy(std::istreambuf_iterator<char>(ErrorFile), 155 std::istreambuf_iterator<char>(), 156 std::ostreambuf_iterator<char>(OS)); 157 ErrorFile.close(); 158 } 159 160 ErrorFilename.eraseFromDisk(); 161 return OS.str(); 162 } 163 164 //===---------------------------------------------------------------------===// 165 // LLI Implementation of AbstractIntepreter interface 166 // 167 namespace { 168 class LLI : public AbstractInterpreter { 169 std::string LLIPath; // The path to the LLI executable 170 std::vector<std::string> ToolArgs; // Args to pass to LLI 171 public: 172 LLI(const std::string &Path, const std::vector<std::string> *Args) 173 : LLIPath(Path) { 174 ToolArgs.clear (); 175 if (Args) { ToolArgs = *Args; } 176 } 177 178 virtual int ExecuteProgram(const std::string &Bitcode, 179 const std::vector<std::string> &Args, 180 const std::string &InputFile, 181 const std::string &OutputFile, 182 std::string *Error, 183 const std::vector<std::string> &GCCArgs, 184 const std::vector<std::string> &SharedLibs = 185 std::vector<std::string>(), 186 unsigned Timeout = 0, 187 unsigned MemoryLimit = 0); 188 }; 189 } 190 191 int LLI::ExecuteProgram(const std::string &Bitcode, 192 const std::vector<std::string> &Args, 193 const std::string &InputFile, 194 const std::string &OutputFile, 195 std::string *Error, 196 const std::vector<std::string> &GCCArgs, 197 const std::vector<std::string> &SharedLibs, 198 unsigned Timeout, 199 unsigned MemoryLimit) { 200 std::vector<const char*> LLIArgs; 201 LLIArgs.push_back(LLIPath.c_str()); 202 LLIArgs.push_back("-force-interpreter=true"); 203 204 for (std::vector<std::string>::const_iterator i = SharedLibs.begin(), 205 e = SharedLibs.end(); i != e; ++i) { 206 LLIArgs.push_back("-load"); 207 LLIArgs.push_back((*i).c_str()); 208 } 209 210 // Add any extra LLI args. 211 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) 212 LLIArgs.push_back(ToolArgs[i].c_str()); 213 214 LLIArgs.push_back(Bitcode.c_str()); 215 // Add optional parameters to the running program from Argv 216 for (unsigned i=0, e = Args.size(); i != e; ++i) 217 LLIArgs.push_back(Args[i].c_str()); 218 LLIArgs.push_back(0); 219 220 outs() << "<lli>"; outs().flush(); 221 DEBUG(errs() << "\nAbout to run:\t"; 222 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i) 223 errs() << " " << LLIArgs[i]; 224 errs() << "\n"; 225 ); 226 return RunProgramWithTimeout(LLIPath, &LLIArgs[0], 227 InputFile, OutputFile, OutputFile, 228 Timeout, MemoryLimit, Error); 229 } 230 231 void AbstractInterpreter::anchor() { } 232 233 /// Prepend the path to the program being executed 234 /// to \p ExeName, given the value of argv[0] and the address of main() 235 /// itself. This allows us to find another LLVM tool if it is built in the same 236 /// directory. An empty string is returned on error; note that this function 237 /// just mainpulates the path and doesn't check for executability. 238 /// @brief Find a named executable. 239 static sys::Path PrependMainExecutablePath(const std::string &ExeName, 240 const char *Argv0, void *MainAddr) { 241 // Check the directory that the calling program is in. We can do 242 // this if ProgramPath contains at least one / character, indicating that it 243 // is a relative path to the executable itself. 244 sys::Path Result = sys::Path::GetMainExecutable(Argv0, MainAddr); 245 Result.eraseComponent(); 246 247 if (!Result.isEmpty()) { 248 Result.appendComponent(ExeName); 249 Result.appendSuffix(sys::Path::GetEXESuffix()); 250 } 251 252 return Result; 253 } 254 255 // LLI create method - Try to find the LLI executable 256 AbstractInterpreter *AbstractInterpreter::createLLI(const char *Argv0, 257 std::string &Message, 258 const std::vector<std::string> *ToolArgs) { 259 std::string LLIPath = 260 PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createLLI).str(); 261 if (!LLIPath.empty()) { 262 Message = "Found lli: " + LLIPath + "\n"; 263 return new LLI(LLIPath, ToolArgs); 264 } 265 266 Message = "Cannot find `lli' in executable directory!\n"; 267 return 0; 268 } 269 270 //===---------------------------------------------------------------------===// 271 // Custom compiler command implementation of AbstractIntepreter interface 272 // 273 // Allows using a custom command for compiling the bitcode, thus allows, for 274 // example, to compile a bitcode fragment without linking or executing, then 275 // using a custom wrapper script to check for compiler errors. 276 namespace { 277 class CustomCompiler : public AbstractInterpreter { 278 std::string CompilerCommand; 279 std::vector<std::string> CompilerArgs; 280 public: 281 CustomCompiler( 282 const std::string &CompilerCmd, std::vector<std::string> CompArgs) : 283 CompilerCommand(CompilerCmd), CompilerArgs(CompArgs) {} 284 285 virtual void compileProgram(const std::string &Bitcode, 286 std::string *Error, 287 unsigned Timeout = 0, 288 unsigned MemoryLimit = 0); 289 290 virtual int ExecuteProgram(const std::string &Bitcode, 291 const std::vector<std::string> &Args, 292 const std::string &InputFile, 293 const std::string &OutputFile, 294 std::string *Error, 295 const std::vector<std::string> &GCCArgs = 296 std::vector<std::string>(), 297 const std::vector<std::string> &SharedLibs = 298 std::vector<std::string>(), 299 unsigned Timeout = 0, 300 unsigned MemoryLimit = 0) { 301 *Error = "Execution not supported with -compile-custom"; 302 return -1; 303 } 304 }; 305 } 306 307 void CustomCompiler::compileProgram(const std::string &Bitcode, 308 std::string *Error, 309 unsigned Timeout, 310 unsigned MemoryLimit) { 311 312 std::vector<const char*> ProgramArgs; 313 ProgramArgs.push_back(CompilerCommand.c_str()); 314 315 for (std::size_t i = 0; i < CompilerArgs.size(); ++i) 316 ProgramArgs.push_back(CompilerArgs.at(i).c_str()); 317 ProgramArgs.push_back(Bitcode.c_str()); 318 ProgramArgs.push_back(0); 319 320 // Add optional parameters to the running program from Argv 321 for (unsigned i = 0, e = CompilerArgs.size(); i != e; ++i) 322 ProgramArgs.push_back(CompilerArgs[i].c_str()); 323 324 if (RunProgramWithTimeout(CompilerCommand, &ProgramArgs[0], 325 "", "", "", 326 Timeout, MemoryLimit, Error)) 327 *Error = ProcessFailure(CompilerCommand, &ProgramArgs[0], 328 Timeout, MemoryLimit); 329 } 330 331 //===---------------------------------------------------------------------===// 332 // Custom execution command implementation of AbstractIntepreter interface 333 // 334 // Allows using a custom command for executing the bitcode, thus allows, 335 // for example, to invoke a cross compiler for code generation followed by 336 // a simulator that executes the generated binary. 337 namespace { 338 class CustomExecutor : public AbstractInterpreter { 339 std::string ExecutionCommand; 340 std::vector<std::string> ExecutorArgs; 341 public: 342 CustomExecutor( 343 const std::string &ExecutionCmd, std::vector<std::string> ExecArgs) : 344 ExecutionCommand(ExecutionCmd), ExecutorArgs(ExecArgs) {} 345 346 virtual int ExecuteProgram(const std::string &Bitcode, 347 const std::vector<std::string> &Args, 348 const std::string &InputFile, 349 const std::string &OutputFile, 350 std::string *Error, 351 const std::vector<std::string> &GCCArgs, 352 const std::vector<std::string> &SharedLibs = 353 std::vector<std::string>(), 354 unsigned Timeout = 0, 355 unsigned MemoryLimit = 0); 356 }; 357 } 358 359 int CustomExecutor::ExecuteProgram(const std::string &Bitcode, 360 const std::vector<std::string> &Args, 361 const std::string &InputFile, 362 const std::string &OutputFile, 363 std::string *Error, 364 const std::vector<std::string> &GCCArgs, 365 const std::vector<std::string> &SharedLibs, 366 unsigned Timeout, 367 unsigned MemoryLimit) { 368 369 std::vector<const char*> ProgramArgs; 370 ProgramArgs.push_back(ExecutionCommand.c_str()); 371 372 for (std::size_t i = 0; i < ExecutorArgs.size(); ++i) 373 ProgramArgs.push_back(ExecutorArgs.at(i).c_str()); 374 ProgramArgs.push_back(Bitcode.c_str()); 375 ProgramArgs.push_back(0); 376 377 // Add optional parameters to the running program from Argv 378 for (unsigned i = 0, e = Args.size(); i != e; ++i) 379 ProgramArgs.push_back(Args[i].c_str()); 380 381 return RunProgramWithTimeout( 382 ExecutionCommand, 383 &ProgramArgs[0], InputFile, OutputFile, 384 OutputFile, Timeout, MemoryLimit, Error); 385 } 386 387 // Tokenize the CommandLine to the command and the args to allow 388 // defining a full command line as the command instead of just the 389 // executed program. We cannot just pass the whole string after the command 390 // as a single argument because then program sees only a single 391 // command line argument (with spaces in it: "foo bar" instead 392 // of "foo" and "bar"). 393 // 394 // code borrowed from: 395 // http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html 396 static void lexCommand(std::string &Message, const std::string &CommandLine, 397 std::string &CmdPath, std::vector<std::string> Args) { 398 399 std::string Command = ""; 400 std::string delimiters = " "; 401 402 std::string::size_type lastPos = CommandLine.find_first_not_of(delimiters, 0); 403 std::string::size_type pos = CommandLine.find_first_of(delimiters, lastPos); 404 405 while (std::string::npos != pos || std::string::npos != lastPos) { 406 std::string token = CommandLine.substr(lastPos, pos - lastPos); 407 if (Command == "") 408 Command = token; 409 else 410 Args.push_back(token); 411 // Skip delimiters. Note the "not_of" 412 lastPos = CommandLine.find_first_not_of(delimiters, pos); 413 // Find next "non-delimiter" 414 pos = CommandLine.find_first_of(delimiters, lastPos); 415 } 416 417 CmdPath = sys::FindProgramByName(Command); 418 if (CmdPath.empty()) { 419 Message = 420 std::string("Cannot find '") + Command + 421 "' in PATH!\n"; 422 return; 423 } 424 425 Message = "Found command in: " + CmdPath + "\n"; 426 } 427 428 // Custom execution environment create method, takes the execution command 429 // as arguments 430 AbstractInterpreter *AbstractInterpreter::createCustomCompiler( 431 std::string &Message, 432 const std::string &CompileCommandLine) { 433 434 std::string CmdPath; 435 std::vector<std::string> Args; 436 lexCommand(Message, CompileCommandLine, CmdPath, Args); 437 if (CmdPath.empty()) 438 return 0; 439 440 return new CustomCompiler(CmdPath, Args); 441 } 442 443 // Custom execution environment create method, takes the execution command 444 // as arguments 445 AbstractInterpreter *AbstractInterpreter::createCustomExecutor( 446 std::string &Message, 447 const std::string &ExecCommandLine) { 448 449 450 std::string CmdPath; 451 std::vector<std::string> Args; 452 lexCommand(Message, ExecCommandLine, CmdPath, Args); 453 if (CmdPath.empty()) 454 return 0; 455 456 return new CustomExecutor(CmdPath, Args); 457 } 458 459 //===----------------------------------------------------------------------===// 460 // LLC Implementation of AbstractIntepreter interface 461 // 462 GCC::FileType LLC::OutputCode(const std::string &Bitcode, 463 sys::Path &OutputAsmFile, std::string &Error, 464 unsigned Timeout, unsigned MemoryLimit) { 465 const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s"); 466 sys::Path uniqueFile(Bitcode + Suffix); 467 std::string ErrMsg; 468 if (uniqueFile.makeUnique(true, &ErrMsg)) { 469 errs() << "Error making unique filename: " << ErrMsg << "\n"; 470 exit(1); 471 } 472 OutputAsmFile = uniqueFile; 473 std::vector<const char *> LLCArgs; 474 LLCArgs.push_back(LLCPath.c_str()); 475 476 // Add any extra LLC args. 477 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) 478 LLCArgs.push_back(ToolArgs[i].c_str()); 479 480 LLCArgs.push_back("-o"); 481 LLCArgs.push_back(OutputAsmFile.c_str()); // Output to the Asm file 482 LLCArgs.push_back(Bitcode.c_str()); // This is the input bitcode 483 484 if (UseIntegratedAssembler) 485 LLCArgs.push_back("-filetype=obj"); 486 487 LLCArgs.push_back (0); 488 489 outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>"); 490 outs().flush(); 491 DEBUG(errs() << "\nAbout to run:\t"; 492 for (unsigned i = 0, e = LLCArgs.size()-1; i != e; ++i) 493 errs() << " " << LLCArgs[i]; 494 errs() << "\n"; 495 ); 496 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], 497 "", "", "", 498 Timeout, MemoryLimit)) 499 Error = ProcessFailure(LLCPath, &LLCArgs[0], 500 Timeout, MemoryLimit); 501 return UseIntegratedAssembler ? GCC::ObjectFile : GCC::AsmFile; 502 } 503 504 void LLC::compileProgram(const std::string &Bitcode, std::string *Error, 505 unsigned Timeout, unsigned MemoryLimit) { 506 sys::Path OutputAsmFile; 507 OutputCode(Bitcode, OutputAsmFile, *Error, Timeout, MemoryLimit); 508 OutputAsmFile.eraseFromDisk(); 509 } 510 511 int LLC::ExecuteProgram(const std::string &Bitcode, 512 const std::vector<std::string> &Args, 513 const std::string &InputFile, 514 const std::string &OutputFile, 515 std::string *Error, 516 const std::vector<std::string> &ArgsForGCC, 517 const std::vector<std::string> &SharedLibs, 518 unsigned Timeout, 519 unsigned MemoryLimit) { 520 521 sys::Path OutputAsmFile; 522 GCC::FileType FileKind = OutputCode(Bitcode, OutputAsmFile, *Error, Timeout, 523 MemoryLimit); 524 FileRemover OutFileRemover(OutputAsmFile.str(), !SaveTemps); 525 526 std::vector<std::string> GCCArgs(ArgsForGCC); 527 GCCArgs.insert(GCCArgs.end(), SharedLibs.begin(), SharedLibs.end()); 528 529 // Assuming LLC worked, compile the result with GCC and run it. 530 return gcc->ExecuteProgram(OutputAsmFile.str(), Args, FileKind, 531 InputFile, OutputFile, Error, GCCArgs, 532 Timeout, MemoryLimit); 533 } 534 535 /// createLLC - Try to find the LLC executable 536 /// 537 LLC *AbstractInterpreter::createLLC(const char *Argv0, 538 std::string &Message, 539 const std::string &GCCBinary, 540 const std::vector<std::string> *Args, 541 const std::vector<std::string> *GCCArgs, 542 bool UseIntegratedAssembler) { 543 std::string LLCPath = 544 PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t)&createLLC).str(); 545 if (LLCPath.empty()) { 546 Message = "Cannot find `llc' in executable directory!\n"; 547 return 0; 548 } 549 550 GCC *gcc = GCC::create(Message, GCCBinary, GCCArgs); 551 if (!gcc) { 552 errs() << Message << "\n"; 553 exit(1); 554 } 555 Message = "Found llc: " + LLCPath + "\n"; 556 return new LLC(LLCPath, gcc, Args, UseIntegratedAssembler); 557 } 558 559 //===---------------------------------------------------------------------===// 560 // JIT Implementation of AbstractIntepreter interface 561 // 562 namespace { 563 class JIT : public AbstractInterpreter { 564 std::string LLIPath; // The path to the LLI executable 565 std::vector<std::string> ToolArgs; // Args to pass to LLI 566 public: 567 JIT(const std::string &Path, const std::vector<std::string> *Args) 568 : LLIPath(Path) { 569 ToolArgs.clear (); 570 if (Args) { ToolArgs = *Args; } 571 } 572 573 virtual int ExecuteProgram(const std::string &Bitcode, 574 const std::vector<std::string> &Args, 575 const std::string &InputFile, 576 const std::string &OutputFile, 577 std::string *Error, 578 const std::vector<std::string> &GCCArgs = 579 std::vector<std::string>(), 580 const std::vector<std::string> &SharedLibs = 581 std::vector<std::string>(), 582 unsigned Timeout = 0, 583 unsigned MemoryLimit = 0); 584 }; 585 } 586 587 int JIT::ExecuteProgram(const std::string &Bitcode, 588 const std::vector<std::string> &Args, 589 const std::string &InputFile, 590 const std::string &OutputFile, 591 std::string *Error, 592 const std::vector<std::string> &GCCArgs, 593 const std::vector<std::string> &SharedLibs, 594 unsigned Timeout, 595 unsigned MemoryLimit) { 596 // Construct a vector of parameters, incorporating those from the command-line 597 std::vector<const char*> JITArgs; 598 JITArgs.push_back(LLIPath.c_str()); 599 JITArgs.push_back("-force-interpreter=false"); 600 601 // Add any extra LLI args. 602 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) 603 JITArgs.push_back(ToolArgs[i].c_str()); 604 605 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) { 606 JITArgs.push_back("-load"); 607 JITArgs.push_back(SharedLibs[i].c_str()); 608 } 609 JITArgs.push_back(Bitcode.c_str()); 610 // Add optional parameters to the running program from Argv 611 for (unsigned i=0, e = Args.size(); i != e; ++i) 612 JITArgs.push_back(Args[i].c_str()); 613 JITArgs.push_back(0); 614 615 outs() << "<jit>"; outs().flush(); 616 DEBUG(errs() << "\nAbout to run:\t"; 617 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i) 618 errs() << " " << JITArgs[i]; 619 errs() << "\n"; 620 ); 621 DEBUG(errs() << "\nSending output to " << OutputFile << "\n"); 622 return RunProgramWithTimeout(LLIPath, &JITArgs[0], 623 InputFile, OutputFile, OutputFile, 624 Timeout, MemoryLimit, Error); 625 } 626 627 /// createJIT - Try to find the LLI executable 628 /// 629 AbstractInterpreter *AbstractInterpreter::createJIT(const char *Argv0, 630 std::string &Message, const std::vector<std::string> *Args) { 631 std::string LLIPath = 632 PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createJIT).str(); 633 if (!LLIPath.empty()) { 634 Message = "Found lli: " + LLIPath + "\n"; 635 return new JIT(LLIPath, Args); 636 } 637 638 Message = "Cannot find `lli' in executable directory!\n"; 639 return 0; 640 } 641 642 //===---------------------------------------------------------------------===// 643 // GCC abstraction 644 // 645 646 static bool IsARMArchitecture(std::vector<const char*> Args) { 647 for (std::vector<const char*>::const_iterator 648 I = Args.begin(), E = Args.end(); I != E; ++I) { 649 if (StringRef(*I).equals_lower("-arch")) { 650 ++I; 651 if (I != E && StringRef(*I).substr(0, strlen("arm")).equals_lower("arm")) 652 return true; 653 } 654 } 655 656 return false; 657 } 658 659 int GCC::ExecuteProgram(const std::string &ProgramFile, 660 const std::vector<std::string> &Args, 661 FileType fileType, 662 const std::string &InputFile, 663 const std::string &OutputFile, 664 std::string *Error, 665 const std::vector<std::string> &ArgsForGCC, 666 unsigned Timeout, 667 unsigned MemoryLimit) { 668 std::vector<const char*> GCCArgs; 669 670 GCCArgs.push_back(GCCPath.c_str()); 671 672 if (TargetTriple.getArch() == Triple::x86) 673 GCCArgs.push_back("-m32"); 674 675 for (std::vector<std::string>::const_iterator 676 I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I) 677 GCCArgs.push_back(I->c_str()); 678 679 // Specify -x explicitly in case the extension is wonky 680 if (fileType != ObjectFile) { 681 GCCArgs.push_back("-x"); 682 if (fileType == CFile) { 683 GCCArgs.push_back("c"); 684 GCCArgs.push_back("-fno-strict-aliasing"); 685 } else { 686 GCCArgs.push_back("assembler"); 687 688 // For ARM architectures we don't want this flag. bugpoint isn't 689 // explicitly told what architecture it is working on, so we get 690 // it from gcc flags 691 if (TargetTriple.isOSDarwin() && !IsARMArchitecture(GCCArgs)) 692 GCCArgs.push_back("-force_cpusubtype_ALL"); 693 } 694 } 695 696 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename. 697 698 GCCArgs.push_back("-x"); 699 GCCArgs.push_back("none"); 700 GCCArgs.push_back("-o"); 701 sys::Path OutputBinary (ProgramFile+".gcc.exe"); 702 std::string ErrMsg; 703 if (OutputBinary.makeUnique(true, &ErrMsg)) { 704 errs() << "Error making unique filename: " << ErrMsg << "\n"; 705 exit(1); 706 } 707 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file... 708 709 // Add any arguments intended for GCC. We locate them here because this is 710 // most likely -L and -l options that need to come before other libraries but 711 // after the source. Other options won't be sensitive to placement on the 712 // command line, so this should be safe. 713 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i) 714 GCCArgs.push_back(ArgsForGCC[i].c_str()); 715 716 GCCArgs.push_back("-lm"); // Hard-code the math library... 717 GCCArgs.push_back("-O2"); // Optimize the program a bit... 718 #if defined (HAVE_LINK_R) 719 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files 720 #endif 721 if (TargetTriple.getArch() == Triple::sparc) 722 GCCArgs.push_back("-mcpu=v9"); 723 GCCArgs.push_back(0); // NULL terminator 724 725 outs() << "<gcc>"; outs().flush(); 726 DEBUG(errs() << "\nAbout to run:\t"; 727 for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i) 728 errs() << " " << GCCArgs[i]; 729 errs() << "\n"; 730 ); 731 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "", "", "")) { 732 *Error = ProcessFailure(GCCPath, &GCCArgs[0]); 733 return -1; 734 } 735 736 std::vector<const char*> ProgramArgs; 737 738 // Declared here so that the destructor only runs after 739 // ProgramArgs is used. 740 std::string Exec; 741 742 if (RemoteClientPath.empty()) 743 ProgramArgs.push_back(OutputBinary.c_str()); 744 else { 745 ProgramArgs.push_back(RemoteClientPath.c_str()); 746 ProgramArgs.push_back(RemoteHost.c_str()); 747 if (!RemoteUser.empty()) { 748 ProgramArgs.push_back("-l"); 749 ProgramArgs.push_back(RemoteUser.c_str()); 750 } 751 if (!RemotePort.empty()) { 752 ProgramArgs.push_back("-p"); 753 ProgramArgs.push_back(RemotePort.c_str()); 754 } 755 if (!RemoteExtra.empty()) { 756 ProgramArgs.push_back(RemoteExtra.c_str()); 757 } 758 759 // Full path to the binary. We need to cd to the exec directory because 760 // there is a dylib there that the exec expects to find in the CWD 761 char* env_pwd = getenv("PWD"); 762 Exec = "cd "; 763 Exec += env_pwd; 764 Exec += "; ./"; 765 Exec += OutputBinary.c_str(); 766 ProgramArgs.push_back(Exec.c_str()); 767 } 768 769 // Add optional parameters to the running program from Argv 770 for (unsigned i = 0, e = Args.size(); i != e; ++i) 771 ProgramArgs.push_back(Args[i].c_str()); 772 ProgramArgs.push_back(0); // NULL terminator 773 774 // Now that we have a binary, run it! 775 outs() << "<program>"; outs().flush(); 776 DEBUG(errs() << "\nAbout to run:\t"; 777 for (unsigned i = 0, e = ProgramArgs.size()-1; i != e; ++i) 778 errs() << " " << ProgramArgs[i]; 779 errs() << "\n"; 780 ); 781 782 FileRemover OutputBinaryRemover(OutputBinary.str(), !SaveTemps); 783 784 if (RemoteClientPath.empty()) { 785 DEBUG(errs() << "<run locally>"); 786 int ExitCode = RunProgramWithTimeout(OutputBinary.str(), &ProgramArgs[0], 787 InputFile, OutputFile, OutputFile, 788 Timeout, MemoryLimit, Error); 789 // Treat a signal (usually SIGSEGV) or timeout as part of the program output 790 // so that crash-causing miscompilation is handled seamlessly. 791 if (ExitCode < -1) { 792 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app); 793 outFile << *Error << '\n'; 794 outFile.close(); 795 Error->clear(); 796 } 797 return ExitCode; 798 } else { 799 outs() << "<run remotely>"; outs().flush(); 800 return RunProgramRemotelyWithTimeout(RemoteClientPath, 801 &ProgramArgs[0], InputFile, OutputFile, 802 OutputFile, Timeout, MemoryLimit); 803 } 804 } 805 806 int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType, 807 std::string &OutputFile, 808 const std::vector<std::string> &ArgsForGCC, 809 std::string &Error) { 810 sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT); 811 std::string ErrMsg; 812 if (uniqueFilename.makeUnique(true, &ErrMsg)) { 813 errs() << "Error making unique filename: " << ErrMsg << "\n"; 814 exit(1); 815 } 816 OutputFile = uniqueFilename.str(); 817 818 std::vector<const char*> GCCArgs; 819 820 GCCArgs.push_back(GCCPath.c_str()); 821 822 if (TargetTriple.getArch() == Triple::x86) 823 GCCArgs.push_back("-m32"); 824 825 for (std::vector<std::string>::const_iterator 826 I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I) 827 GCCArgs.push_back(I->c_str()); 828 829 // Compile the C/asm file into a shared object 830 if (fileType != ObjectFile) { 831 GCCArgs.push_back("-x"); 832 GCCArgs.push_back(fileType == AsmFile ? "assembler" : "c"); 833 } 834 GCCArgs.push_back("-fno-strict-aliasing"); 835 GCCArgs.push_back(InputFile.c_str()); // Specify the input filename. 836 GCCArgs.push_back("-x"); 837 GCCArgs.push_back("none"); 838 if (TargetTriple.getArch() == Triple::sparc) 839 GCCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc 840 else if (TargetTriple.isOSDarwin()) { 841 // link all source files into a single module in data segment, rather than 842 // generating blocks. dynamic_lookup requires that you set 843 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for 844 // bugpoint to just pass that in the environment of GCC. 845 GCCArgs.push_back("-single_module"); 846 GCCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC 847 GCCArgs.push_back("-undefined"); 848 GCCArgs.push_back("dynamic_lookup"); 849 } else 850 GCCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others 851 852 if (TargetTriple.getArch() == Triple::x86_64) 853 GCCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC 854 855 if (TargetTriple.getArch() == Triple::sparc) 856 GCCArgs.push_back("-mcpu=v9"); 857 858 GCCArgs.push_back("-o"); 859 GCCArgs.push_back(OutputFile.c_str()); // Output to the right filename. 860 GCCArgs.push_back("-O2"); // Optimize the program a bit. 861 862 863 864 // Add any arguments intended for GCC. We locate them here because this is 865 // most likely -L and -l options that need to come before other libraries but 866 // after the source. Other options won't be sensitive to placement on the 867 // command line, so this should be safe. 868 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i) 869 GCCArgs.push_back(ArgsForGCC[i].c_str()); 870 GCCArgs.push_back(0); // NULL terminator 871 872 873 874 outs() << "<gcc>"; outs().flush(); 875 DEBUG(errs() << "\nAbout to run:\t"; 876 for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i) 877 errs() << " " << GCCArgs[i]; 878 errs() << "\n"; 879 ); 880 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "", "", "")) { 881 Error = ProcessFailure(GCCPath, &GCCArgs[0]); 882 return 1; 883 } 884 return 0; 885 } 886 887 /// create - Try to find the `gcc' executable 888 /// 889 GCC *GCC::create(std::string &Message, 890 const std::string &GCCBinary, 891 const std::vector<std::string> *Args) { 892 std::string GCCPath = sys::FindProgramByName(GCCBinary); 893 if (GCCPath.empty()) { 894 Message = "Cannot find `"+ GCCBinary +"' in PATH!\n"; 895 return 0; 896 } 897 898 std::string RemoteClientPath; 899 if (!RemoteClient.empty()) 900 RemoteClientPath = sys::FindProgramByName(RemoteClient); 901 902 Message = "Found gcc: " + GCCPath + "\n"; 903 return new GCC(GCCPath, RemoteClientPath, Args); 904 } 905