comparison tools/bugpoint/ToolRunner.cpp @ 147:c2174574ed3a

LLVM 10
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 16:55:33 +0900
parents 803732b1fca8
children
comparison
equal deleted inserted replaced
134:3a76565eade5 147:c2174574ed3a
1 //===-- ToolRunner.cpp ----------------------------------------------------===// 1 //===-- ToolRunner.cpp ----------------------------------------------------===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // 4 // See https://llvm.org/LICENSE.txt for license information.
5 // This file is distributed under the University of Illinois Open Source 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 // License. See LICENSE.TXT for details.
7 // 6 //
8 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
9 // 8 //
10 // This file implements the interfaces described in the ToolRunner.h file. 9 // This file implements the interfaces described in the ToolRunner.h file.
11 // 10 //
51 } 50 }
52 51
53 /// RunProgramWithTimeout - This function provides an alternate interface 52 /// RunProgramWithTimeout - This function provides an alternate interface
54 /// to the sys::Program::ExecuteAndWait interface. 53 /// to the sys::Program::ExecuteAndWait interface.
55 /// @see sys::Program::ExecuteAndWait 54 /// @see sys::Program::ExecuteAndWait
56 static int RunProgramWithTimeout(StringRef ProgramPath, const char **Args, 55 static int RunProgramWithTimeout(StringRef ProgramPath,
57 StringRef StdInFile, StringRef StdOutFile, 56 ArrayRef<StringRef> Args, StringRef StdInFile,
58 StringRef StdErrFile, unsigned NumSeconds = 0, 57 StringRef StdOutFile, StringRef StdErrFile,
58 unsigned NumSeconds = 0,
59 unsigned MemoryLimit = 0, 59 unsigned MemoryLimit = 0,
60 std::string *ErrMsg = nullptr) { 60 std::string *ErrMsg = nullptr) {
61 Optional<StringRef> Redirects[3] = {StdInFile, StdOutFile, StdErrFile}; 61 Optional<StringRef> Redirects[3] = {StdInFile, StdOutFile, StdErrFile};
62 return sys::ExecuteAndWait(ProgramPath, Args, nullptr, Redirects, NumSeconds, 62 return sys::ExecuteAndWait(ProgramPath, Args, None, Redirects, NumSeconds,
63 MemoryLimit, ErrMsg); 63 MemoryLimit, ErrMsg);
64 } 64 }
65 65
66 /// RunProgramRemotelyWithTimeout - This function runs the given program 66 /// RunProgramRemotelyWithTimeout - This function runs the given program
67 /// remotely using the given remote client and the sys::Program::ExecuteAndWait. 67 /// remotely using the given remote client and the sys::Program::ExecuteAndWait.
68 /// Returns the remote program exit code or reports a remote client error if it 68 /// Returns the remote program exit code or reports a remote client error if it
69 /// fails. Remote client is required to return 255 if it failed or program exit 69 /// fails. Remote client is required to return 255 if it failed or program exit
70 /// code otherwise. 70 /// code otherwise.
71 /// @see sys::Program::ExecuteAndWait 71 /// @see sys::Program::ExecuteAndWait
72 static int RunProgramRemotelyWithTimeout(StringRef RemoteClientPath, 72 static int RunProgramRemotelyWithTimeout(
73 const char **Args, StringRef StdInFile, 73 StringRef RemoteClientPath, ArrayRef<StringRef> Args, StringRef StdInFile,
74 StringRef StdOutFile, 74 StringRef StdOutFile, StringRef StdErrFile, unsigned NumSeconds = 0,
75 StringRef StdErrFile, 75 unsigned MemoryLimit = 0) {
76 unsigned NumSeconds = 0,
77 unsigned MemoryLimit = 0) {
78 Optional<StringRef> Redirects[3] = {StdInFile, StdOutFile, StdErrFile}; 76 Optional<StringRef> Redirects[3] = {StdInFile, StdOutFile, StdErrFile};
79 77
80 // Run the program remotely with the remote client 78 // Run the program remotely with the remote client
81 int ReturnCode = sys::ExecuteAndWait(RemoteClientPath, Args, nullptr, 79 int ReturnCode = sys::ExecuteAndWait(RemoteClientPath, Args, None, Redirects,
82 Redirects, NumSeconds, MemoryLimit); 80 NumSeconds, MemoryLimit);
83 81
84 // Has the remote client fail? 82 // Has the remote client fail?
85 if (255 == ReturnCode) { 83 if (255 == ReturnCode) {
86 std::ostringstream OS; 84 std::ostringstream OS;
87 OS << "\nError running remote client:\n "; 85 OS << "\nError running remote client:\n ";
88 for (const char **Arg = Args; *Arg; ++Arg) 86 for (StringRef Arg : Args)
89 OS << " " << *Arg; 87 OS << " " << Arg.str();
90 OS << "\n"; 88 OS << "\n";
91 89
92 // The error message is in the output file, let's print it out from there. 90 // The error message is in the output file, let's print it out from there.
93 std::string StdOutFileName = StdOutFile.str(); 91 std::string StdOutFileName = StdOutFile.str();
94 std::ifstream ErrorFile(StdOutFileName.c_str()); 92 std::ifstream ErrorFile(StdOutFileName.c_str());
103 } 101 }
104 102
105 return ReturnCode; 103 return ReturnCode;
106 } 104 }
107 105
108 static Error ProcessFailure(StringRef ProgPath, const char **Args, 106 static Error ProcessFailure(StringRef ProgPath, ArrayRef<StringRef> Args,
109 unsigned Timeout = 0, unsigned MemoryLimit = 0) { 107 unsigned Timeout = 0, unsigned MemoryLimit = 0) {
110 std::ostringstream OS; 108 std::ostringstream OS;
111 OS << "\nError running tool:\n "; 109 OS << "\nError running tool:\n ";
112 for (const char **Arg = Args; *Arg; ++Arg) 110 for (StringRef Arg : Args)
113 OS << " " << *Arg; 111 OS << " " << Arg.str();
114 OS << "\n"; 112 OS << "\n";
115 113
116 // Rerun the compiler, capturing any error messages to print them. 114 // Rerun the compiler, capturing any error messages to print them.
117 SmallString<128> ErrorFilename; 115 SmallString<128> ErrorFilename;
118 std::error_code EC = sys::fs::createTemporaryFile( 116 std::error_code EC = sys::fs::createTemporaryFile(
169 const std::string &InputFile, 167 const std::string &InputFile,
170 const std::string &OutputFile, 168 const std::string &OutputFile,
171 const std::vector<std::string> &CCArgs, 169 const std::vector<std::string> &CCArgs,
172 const std::vector<std::string> &SharedLibs, 170 const std::vector<std::string> &SharedLibs,
173 unsigned Timeout, unsigned MemoryLimit) { 171 unsigned Timeout, unsigned MemoryLimit) {
174 std::vector<const char *> LLIArgs; 172 std::vector<StringRef> LLIArgs;
175 LLIArgs.push_back(LLIPath.c_str()); 173 LLIArgs.push_back(LLIPath.c_str());
176 LLIArgs.push_back("-force-interpreter=true"); 174 LLIArgs.push_back("-force-interpreter=true");
177 175
178 for (std::vector<std::string>::const_iterator i = SharedLibs.begin(), 176 for (std::vector<std::string>::const_iterator i = SharedLibs.begin(),
179 e = SharedLibs.end(); 177 e = SharedLibs.end();
180 i != e; ++i) { 178 i != e; ++i) {
181 LLIArgs.push_back("-load"); 179 LLIArgs.push_back("-load");
182 LLIArgs.push_back((*i).c_str()); 180 LLIArgs.push_back(*i);
183 } 181 }
184 182
185 // Add any extra LLI args. 183 // Add any extra LLI args.
186 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) 184 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
187 LLIArgs.push_back(ToolArgs[i].c_str()); 185 LLIArgs.push_back(ToolArgs[i]);
188 186
189 LLIArgs.push_back(Bitcode.c_str()); 187 LLIArgs.push_back(Bitcode);
190 // Add optional parameters to the running program from Argv 188 // Add optional parameters to the running program from Argv
191 for (unsigned i = 0, e = Args.size(); i != e; ++i) 189 for (unsigned i = 0, e = Args.size(); i != e; ++i)
192 LLIArgs.push_back(Args[i].c_str()); 190 LLIArgs.push_back(Args[i]);
193 LLIArgs.push_back(nullptr);
194 191
195 outs() << "<lli>"; 192 outs() << "<lli>";
196 outs().flush(); 193 outs().flush();
197 DEBUG(errs() << "\nAbout to run:\t"; 194 LLVM_DEBUG(errs() << "\nAbout to run:\t";
198 for (unsigned i = 0, e = LLIArgs.size() - 1; i != e; ++i) errs() 195 for (unsigned i = 0, e = LLIArgs.size() - 1; i != e; ++i) errs()
199 << " " << LLIArgs[i]; 196 << " " << LLIArgs[i];
200 errs() << "\n";); 197 errs() << "\n";);
201 return RunProgramWithTimeout(LLIPath, &LLIArgs[0], InputFile, OutputFile, 198 return RunProgramWithTimeout(LLIPath, LLIArgs, InputFile, OutputFile,
202 OutputFile, Timeout, MemoryLimit); 199 OutputFile, Timeout, MemoryLimit);
203 } 200 }
204 201
205 void AbstractInterpreter::anchor() {} 202 void AbstractInterpreter::anchor() {}
206 203
207 #if defined(LLVM_ON_UNIX) 204 ErrorOr<std::string> llvm::FindProgramByName(const std::string &ExeName,
208 const char EXESuffix[] = "";
209 #elif defined(LLVM_ON_WIN32)
210 const char EXESuffix[] = "exe";
211 #endif
212
213 /// Prepend the path to the program being executed
214 /// to \p ExeName, given the value of argv[0] and the address of main()
215 /// itself. This allows us to find another LLVM tool if it is built in the same
216 /// directory. An empty string is returned on error; note that this function
217 /// just mainpulates the path and doesn't check for executability.
218 /// @brief Find a named executable.
219 static std::string PrependMainExecutablePath(const std::string &ExeName,
220 const char *Argv0, 205 const char *Argv0,
221 void *MainAddr) { 206 void *MainAddr) {
222 // Check the directory that the calling program is in. We can do 207 // Check the directory that the calling program is in. We can do
223 // this if ProgramPath contains at least one / character, indicating that it 208 // this if ProgramPath contains at least one / character, indicating that it
224 // is a relative path to the executable itself. 209 // is a relative path to the executable itself.
225 std::string Main = sys::fs::getMainExecutable(Argv0, MainAddr); 210 std::string Main = sys::fs::getMainExecutable(Argv0, MainAddr);
226 StringRef Result = sys::path::parent_path(Main); 211 StringRef Result = sys::path::parent_path(Main);
227 212 if (ErrorOr<std::string> Path = sys::findProgramByName(ExeName, Result))
228 if (!Result.empty()) { 213 return *Path;
229 SmallString<128> Storage = Result; 214
230 sys::path::append(Storage, ExeName); 215 // Check the user PATH.
231 sys::path::replace_extension(Storage, EXESuffix); 216 return sys::findProgramByName(ExeName);
232 return Storage.str();
233 }
234
235 return Result.str();
236 } 217 }
237 218
238 // LLI create method - Try to find the LLI executable 219 // LLI create method - Try to find the LLI executable
239 AbstractInterpreter * 220 AbstractInterpreter *
240 AbstractInterpreter::createLLI(const char *Argv0, std::string &Message, 221 AbstractInterpreter::createLLI(const char *Argv0, std::string &Message,
241 const std::vector<std::string> *ToolArgs) { 222 const std::vector<std::string> *ToolArgs) {
242 std::string LLIPath = 223 if (ErrorOr<std::string> LLIPath =
243 PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createLLI); 224 FindProgramByName("lli", Argv0, (void *)(intptr_t)&createLLI)) {
244 if (!LLIPath.empty()) { 225 Message = "Found lli: " + *LLIPath + "\n";
245 Message = "Found lli: " + LLIPath + "\n"; 226 return new LLI(*LLIPath, ToolArgs);
246 return new LLI(LLIPath, ToolArgs); 227 } else {
247 } 228 Message = LLIPath.getError().message() + "\n";
248 229 return nullptr;
249 Message = "Cannot find `lli' in executable directory!\n"; 230 }
250 return nullptr;
251 } 231 }
252 232
253 //===---------------------------------------------------------------------===// 233 //===---------------------------------------------------------------------===//
254 // Custom compiler command implementation of AbstractIntepreter interface 234 // Custom compiler command implementation of AbstractIntepreter interface
255 // 235 //
283 } 263 }
284 264
285 Error CustomCompiler::compileProgram(const std::string &Bitcode, 265 Error CustomCompiler::compileProgram(const std::string &Bitcode,
286 unsigned Timeout, unsigned MemoryLimit) { 266 unsigned Timeout, unsigned MemoryLimit) {
287 267
288 std::vector<const char *> ProgramArgs; 268 std::vector<StringRef> ProgramArgs;
289 ProgramArgs.push_back(CompilerCommand.c_str()); 269 ProgramArgs.push_back(CompilerCommand.c_str());
290 270
291 for (std::size_t i = 0; i < CompilerArgs.size(); ++i) 271 for (std::size_t i = 0; i < CompilerArgs.size(); ++i)
292 ProgramArgs.push_back(CompilerArgs.at(i).c_str()); 272 ProgramArgs.push_back(CompilerArgs.at(i).c_str());
293 ProgramArgs.push_back(Bitcode.c_str()); 273 ProgramArgs.push_back(Bitcode);
294 ProgramArgs.push_back(nullptr);
295 274
296 // Add optional parameters to the running program from Argv 275 // Add optional parameters to the running program from Argv
297 for (unsigned i = 0, e = CompilerArgs.size(); i != e; ++i) 276 for (unsigned i = 0, e = CompilerArgs.size(); i != e; ++i)
298 ProgramArgs.push_back(CompilerArgs[i].c_str()); 277 ProgramArgs.push_back(CompilerArgs[i].c_str());
299 278
300 if (RunProgramWithTimeout(CompilerCommand, &ProgramArgs[0], "", "", "", 279 if (RunProgramWithTimeout(CompilerCommand, ProgramArgs, "", "", "", Timeout,
301 Timeout, MemoryLimit)) 280 MemoryLimit))
302 return ProcessFailure(CompilerCommand, &ProgramArgs[0], Timeout, 281 return ProcessFailure(CompilerCommand, ProgramArgs, Timeout, MemoryLimit);
303 MemoryLimit);
304 return Error::success(); 282 return Error::success();
305 } 283 }
306 284
307 //===---------------------------------------------------------------------===// 285 //===---------------------------------------------------------------------===//
308 // Custom execution command implementation of AbstractIntepreter interface 286 // Custom execution command implementation of AbstractIntepreter interface
334 const std::string &InputFile, const std::string &OutputFile, 312 const std::string &InputFile, const std::string &OutputFile,
335 const std::vector<std::string> &CCArgs, 313 const std::vector<std::string> &CCArgs,
336 const std::vector<std::string> &SharedLibs, unsigned Timeout, 314 const std::vector<std::string> &SharedLibs, unsigned Timeout,
337 unsigned MemoryLimit) { 315 unsigned MemoryLimit) {
338 316
339 std::vector<const char *> ProgramArgs; 317 std::vector<StringRef> ProgramArgs;
340 ProgramArgs.push_back(ExecutionCommand.c_str()); 318 ProgramArgs.push_back(ExecutionCommand);
341 319
342 for (std::size_t i = 0; i < ExecutorArgs.size(); ++i) 320 for (std::size_t i = 0; i < ExecutorArgs.size(); ++i)
343 ProgramArgs.push_back(ExecutorArgs.at(i).c_str()); 321 ProgramArgs.push_back(ExecutorArgs[i]);
344 ProgramArgs.push_back(Bitcode.c_str()); 322 ProgramArgs.push_back(Bitcode);
345 ProgramArgs.push_back(nullptr);
346 323
347 // Add optional parameters to the running program from Argv 324 // Add optional parameters to the running program from Argv
348 for (unsigned i = 0, e = Args.size(); i != e; ++i) 325 for (unsigned i = 0, e = Args.size(); i != e; ++i)
349 ProgramArgs.push_back(Args[i].c_str()); 326 ProgramArgs.push_back(Args[i]);
350 327
351 return RunProgramWithTimeout(ExecutionCommand, &ProgramArgs[0], InputFile, 328 return RunProgramWithTimeout(ExecutionCommand, ProgramArgs, InputFile,
352 OutputFile, OutputFile, Timeout, MemoryLimit); 329 OutputFile, OutputFile, Timeout, MemoryLimit);
353 } 330 }
354 331
355 // Tokenize the CommandLine to the command and the args to allow 332 // Tokenize the CommandLine to the command and the args to allow
356 // defining a full command line as the command instead of just the 333 // defining a full command line as the command instead of just the
371 // Example: 348 // Example:
372 // '\\' -> '\' 349 // '\\' -> '\'
373 // '\ ' -> ' ' 350 // '\ ' -> ' '
374 // 'exa\mple' -> 'example' 351 // 'exa\mple' -> 'example'
375 // 352 //
376 static void lexCommand(std::string &Message, const std::string &CommandLine, 353 static void lexCommand(const char *Argv0, std::string &Message,
377 std::string &CmdPath, std::vector<std::string> &Args) { 354 const std::string &CommandLine, std::string &CmdPath,
355 std::vector<std::string> &Args) {
378 356
379 std::string Token; 357 std::string Token;
380 std::string Command; 358 std::string Command;
381 bool FoundPath = false; 359 bool FoundPath = false;
382 360
405 continue; 383 continue;
406 } 384 }
407 Token.push_back(CommandLine[Pos]); 385 Token.push_back(CommandLine[Pos]);
408 } 386 }
409 387
410 auto Path = sys::findProgramByName(Command); 388 auto Path = FindProgramByName(Command, Argv0, (void *)(intptr_t)&lexCommand);
411 if (!Path) { 389 if (!Path) {
412 Message = std::string("Cannot find '") + Command + 390 Message = std::string("Cannot find '") + Command +
413 "' in PATH: " + Path.getError().message() + "\n"; 391 "' in PATH: " + Path.getError().message() + "\n";
414 return; 392 return;
415 } 393 }
419 } 397 }
420 398
421 // Custom execution environment create method, takes the execution command 399 // Custom execution environment create method, takes the execution command
422 // as arguments 400 // as arguments
423 AbstractInterpreter *AbstractInterpreter::createCustomCompiler( 401 AbstractInterpreter *AbstractInterpreter::createCustomCompiler(
424 std::string &Message, const std::string &CompileCommandLine) { 402 const char *Argv0, std::string &Message,
403 const std::string &CompileCommandLine) {
425 404
426 std::string CmdPath; 405 std::string CmdPath;
427 std::vector<std::string> Args; 406 std::vector<std::string> Args;
428 lexCommand(Message, CompileCommandLine, CmdPath, Args); 407 lexCommand(Argv0, Message, CompileCommandLine, CmdPath, Args);
429 if (CmdPath.empty()) 408 if (CmdPath.empty())
430 return nullptr; 409 return nullptr;
431 410
432 return new CustomCompiler(CmdPath, Args); 411 return new CustomCompiler(CmdPath, Args);
433 } 412 }
434 413
435 // Custom execution environment create method, takes the execution command 414 // Custom execution environment create method, takes the execution command
436 // as arguments 415 // as arguments
437 AbstractInterpreter * 416 AbstractInterpreter *
438 AbstractInterpreter::createCustomExecutor(std::string &Message, 417 AbstractInterpreter::createCustomExecutor(const char *Argv0,
418 std::string &Message,
439 const std::string &ExecCommandLine) { 419 const std::string &ExecCommandLine) {
440 420
441 std::string CmdPath; 421 std::string CmdPath;
442 std::vector<std::string> Args; 422 std::vector<std::string> Args;
443 lexCommand(Message, ExecCommandLine, CmdPath, Args); 423 lexCommand(Argv0, Message, ExecCommandLine, CmdPath, Args);
444 if (CmdPath.empty()) 424 if (CmdPath.empty())
445 return nullptr; 425 return nullptr;
446 426
447 return new CustomExecutor(CmdPath, Args); 427 return new CustomExecutor(CmdPath, Args);
448 } 428 }
461 if (EC) { 441 if (EC) {
462 errs() << "Error making unique filename: " << EC.message() << "\n"; 442 errs() << "Error making unique filename: " << EC.message() << "\n";
463 exit(1); 443 exit(1);
464 } 444 }
465 OutputAsmFile = UniqueFile.str(); 445 OutputAsmFile = UniqueFile.str();
466 std::vector<const char *> LLCArgs; 446 std::vector<StringRef> LLCArgs;
467 LLCArgs.push_back(LLCPath.c_str()); 447 LLCArgs.push_back(LLCPath);
468 448
469 // Add any extra LLC args. 449 // Add any extra LLC args.
470 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) 450 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
471 LLCArgs.push_back(ToolArgs[i].c_str()); 451 LLCArgs.push_back(ToolArgs[i]);
472 452
473 LLCArgs.push_back("-o"); 453 LLCArgs.push_back("-o");
474 LLCArgs.push_back(OutputAsmFile.c_str()); // Output to the Asm file 454 LLCArgs.push_back(OutputAsmFile); // Output to the Asm file
475 LLCArgs.push_back(Bitcode.c_str()); // This is the input bitcode 455 LLCArgs.push_back(Bitcode); // This is the input bitcode
476 456
477 if (UseIntegratedAssembler) 457 if (UseIntegratedAssembler)
478 LLCArgs.push_back("-filetype=obj"); 458 LLCArgs.push_back("-filetype=obj");
479 459
480 LLCArgs.push_back(nullptr);
481
482 outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>"); 460 outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>");
483 outs().flush(); 461 outs().flush();
484 DEBUG(errs() << "\nAbout to run:\t"; 462 LLVM_DEBUG(errs() << "\nAbout to run:\t";
485 for (unsigned i = 0, e = LLCArgs.size() - 1; i != e; ++i) errs() 463 for (unsigned i = 0, e = LLCArgs.size() - 1; i != e; ++i) errs()
486 << " " << LLCArgs[i]; 464 << " " << LLCArgs[i];
487 errs() << "\n";); 465 errs() << "\n";);
488 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], "", "", "", Timeout, 466 if (RunProgramWithTimeout(LLCPath, LLCArgs, "", "", "", Timeout, MemoryLimit))
489 MemoryLimit)) 467 return ProcessFailure(LLCPath, LLCArgs, Timeout, MemoryLimit);
490 return ProcessFailure(LLCPath, &LLCArgs[0], Timeout, MemoryLimit);
491 return UseIntegratedAssembler ? CC::ObjectFile : CC::AsmFile; 468 return UseIntegratedAssembler ? CC::ObjectFile : CC::AsmFile;
492 } 469 }
493 470
494 Error LLC::compileProgram(const std::string &Bitcode, unsigned Timeout, 471 Error LLC::compileProgram(const std::string &Bitcode, unsigned Timeout,
495 unsigned MemoryLimit) { 472 unsigned MemoryLimit) {
530 LLC *AbstractInterpreter::createLLC(const char *Argv0, std::string &Message, 507 LLC *AbstractInterpreter::createLLC(const char *Argv0, std::string &Message,
531 const std::string &CCBinary, 508 const std::string &CCBinary,
532 const std::vector<std::string> *Args, 509 const std::vector<std::string> *Args,
533 const std::vector<std::string> *CCArgs, 510 const std::vector<std::string> *CCArgs,
534 bool UseIntegratedAssembler) { 511 bool UseIntegratedAssembler) {
535 std::string LLCPath = 512 ErrorOr<std::string> LLCPath =
536 PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t)&createLLC); 513 FindProgramByName("llc", Argv0, (void *)(intptr_t)&createLLC);
537 if (LLCPath.empty()) { 514 if (!LLCPath) {
538 Message = "Cannot find `llc' in executable directory!\n"; 515 Message = LLCPath.getError().message() + "\n";
539 return nullptr; 516 return nullptr;
540 } 517 }
541 518
542 CC *cc = CC::create(Message, CCBinary, CCArgs); 519 CC *cc = CC::create(Argv0, Message, CCBinary, CCArgs);
543 if (!cc) { 520 if (!cc) {
544 errs() << Message << "\n"; 521 errs() << Message << "\n";
545 exit(1); 522 exit(1);
546 } 523 }
547 Message = "Found llc: " + LLCPath + "\n"; 524 Message = "Found llc: " + *LLCPath + "\n";
548 return new LLC(LLCPath, cc, Args, UseIntegratedAssembler); 525 return new LLC(*LLCPath, cc, Args, UseIntegratedAssembler);
549 } 526 }
550 527
551 //===---------------------------------------------------------------------===// 528 //===---------------------------------------------------------------------===//
552 // JIT Implementation of AbstractIntepreter interface 529 // JIT Implementation of AbstractIntepreter interface
553 // 530 //
579 const std::string &OutputFile, 556 const std::string &OutputFile,
580 const std::vector<std::string> &CCArgs, 557 const std::vector<std::string> &CCArgs,
581 const std::vector<std::string> &SharedLibs, 558 const std::vector<std::string> &SharedLibs,
582 unsigned Timeout, unsigned MemoryLimit) { 559 unsigned Timeout, unsigned MemoryLimit) {
583 // Construct a vector of parameters, incorporating those from the command-line 560 // Construct a vector of parameters, incorporating those from the command-line
584 std::vector<const char *> JITArgs; 561 std::vector<StringRef> JITArgs;
585 JITArgs.push_back(LLIPath.c_str()); 562 JITArgs.push_back(LLIPath.c_str());
586 JITArgs.push_back("-force-interpreter=false"); 563 JITArgs.push_back("-force-interpreter=false");
587 564
588 // Add any extra LLI args. 565 // Add any extra LLI args.
589 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) 566 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
590 JITArgs.push_back(ToolArgs[i].c_str()); 567 JITArgs.push_back(ToolArgs[i]);
591 568
592 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) { 569 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
593 JITArgs.push_back("-load"); 570 JITArgs.push_back("-load");
594 JITArgs.push_back(SharedLibs[i].c_str()); 571 JITArgs.push_back(SharedLibs[i]);
595 } 572 }
596 JITArgs.push_back(Bitcode.c_str()); 573 JITArgs.push_back(Bitcode.c_str());
597 // Add optional parameters to the running program from Argv 574 // Add optional parameters to the running program from Argv
598 for (unsigned i = 0, e = Args.size(); i != e; ++i) 575 for (unsigned i = 0, e = Args.size(); i != e; ++i)
599 JITArgs.push_back(Args[i].c_str()); 576 JITArgs.push_back(Args[i]);
600 JITArgs.push_back(nullptr);
601 577
602 outs() << "<jit>"; 578 outs() << "<jit>";
603 outs().flush(); 579 outs().flush();
604 DEBUG(errs() << "\nAbout to run:\t"; 580 LLVM_DEBUG(errs() << "\nAbout to run:\t";
605 for (unsigned i = 0, e = JITArgs.size() - 1; i != e; ++i) errs() 581 for (unsigned i = 0, e = JITArgs.size() - 1; i != e; ++i) errs()
606 << " " << JITArgs[i]; 582 << " " << JITArgs[i];
607 errs() << "\n";); 583 errs() << "\n";);
608 DEBUG(errs() << "\nSending output to " << OutputFile << "\n"); 584 LLVM_DEBUG(errs() << "\nSending output to " << OutputFile << "\n");
609 return RunProgramWithTimeout(LLIPath, &JITArgs[0], InputFile, OutputFile, 585 return RunProgramWithTimeout(LLIPath, JITArgs, InputFile, OutputFile,
610 OutputFile, Timeout, MemoryLimit); 586 OutputFile, Timeout, MemoryLimit);
611 } 587 }
612 588
613 /// createJIT - Try to find the LLI executable 589 /// createJIT - Try to find the LLI executable
614 /// 590 ///
615 AbstractInterpreter * 591 AbstractInterpreter *
616 AbstractInterpreter::createJIT(const char *Argv0, std::string &Message, 592 AbstractInterpreter::createJIT(const char *Argv0, std::string &Message,
617 const std::vector<std::string> *Args) { 593 const std::vector<std::string> *Args) {
618 std::string LLIPath = 594 if (ErrorOr<std::string> LLIPath =
619 PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createJIT); 595 FindProgramByName("lli", Argv0, (void *)(intptr_t)&createJIT)) {
620 if (!LLIPath.empty()) { 596 Message = "Found lli: " + *LLIPath + "\n";
621 Message = "Found lli: " + LLIPath + "\n"; 597 return new JIT(*LLIPath, Args);
622 return new JIT(LLIPath, Args); 598 } else {
623 } 599 Message = LLIPath.getError().message() + "\n";
624 600 return nullptr;
625 Message = "Cannot find `lli' in executable directory!\n"; 601 }
626 return nullptr;
627 } 602 }
628 603
629 //===---------------------------------------------------------------------===// 604 //===---------------------------------------------------------------------===//
630 // CC abstraction 605 // CC abstraction
631 // 606 //
632 607
633 static bool IsARMArchitecture(std::vector<const char *> Args) { 608 static bool IsARMArchitecture(std::vector<StringRef> Args) {
634 for (std::vector<const char *>::const_iterator I = Args.begin(), 609 for (size_t I = 0; I < Args.size(); ++I) {
635 E = Args.end(); 610 if (!Args[I].equals_lower("-arch"))
636 I != E; ++I) { 611 continue;
637 if (StringRef(*I).equals_lower("-arch")) { 612 ++I;
638 ++I; 613 if (I == Args.size())
639 if (I != E && StringRef(*I).startswith_lower("arm")) 614 break;
640 return true; 615 if (Args[I].startswith_lower("arm"))
641 } 616 return true;
642 } 617 }
643 618
644 return false; 619 return false;
645 } 620 }
646 621
649 FileType fileType, 624 FileType fileType,
650 const std::string &InputFile, 625 const std::string &InputFile,
651 const std::string &OutputFile, 626 const std::string &OutputFile,
652 const std::vector<std::string> &ArgsForCC, 627 const std::vector<std::string> &ArgsForCC,
653 unsigned Timeout, unsigned MemoryLimit) { 628 unsigned Timeout, unsigned MemoryLimit) {
654 std::vector<const char *> CCArgs; 629 std::vector<StringRef> CCArgs;
655 630
656 CCArgs.push_back(CCPath.c_str()); 631 CCArgs.push_back(CCPath);
657 632
658 if (TargetTriple.getArch() == Triple::x86) 633 if (TargetTriple.getArch() == Triple::x86)
659 CCArgs.push_back("-m32"); 634 CCArgs.push_back("-m32");
660 635
661 for (std::vector<std::string>::const_iterator I = ccArgs.begin(), 636 for (std::vector<std::string>::const_iterator I = ccArgs.begin(),
662 E = ccArgs.end(); 637 E = ccArgs.end();
663 I != E; ++I) 638 I != E; ++I)
664 CCArgs.push_back(I->c_str()); 639 CCArgs.push_back(*I);
665 640
666 // Specify -x explicitly in case the extension is wonky 641 // Specify -x explicitly in case the extension is wonky
667 if (fileType != ObjectFile) { 642 if (fileType != ObjectFile) {
668 CCArgs.push_back("-x"); 643 CCArgs.push_back("-x");
669 if (fileType == CFile) { 644 if (fileType == CFile) {
678 if (TargetTriple.isOSDarwin() && !IsARMArchitecture(CCArgs)) 653 if (TargetTriple.isOSDarwin() && !IsARMArchitecture(CCArgs))
679 CCArgs.push_back("-force_cpusubtype_ALL"); 654 CCArgs.push_back("-force_cpusubtype_ALL");
680 } 655 }
681 } 656 }
682 657
683 CCArgs.push_back(ProgramFile.c_str()); // Specify the input filename. 658 CCArgs.push_back(ProgramFile); // Specify the input filename.
684 659
685 CCArgs.push_back("-x"); 660 CCArgs.push_back("-x");
686 CCArgs.push_back("none"); 661 CCArgs.push_back("none");
687 CCArgs.push_back("-o"); 662 CCArgs.push_back("-o");
688 663
691 sys::fs::createUniqueFile(ProgramFile + "-%%%%%%%.cc.exe", OutputBinary); 666 sys::fs::createUniqueFile(ProgramFile + "-%%%%%%%.cc.exe", OutputBinary);
692 if (EC) { 667 if (EC) {
693 errs() << "Error making unique filename: " << EC.message() << "\n"; 668 errs() << "Error making unique filename: " << EC.message() << "\n";
694 exit(1); 669 exit(1);
695 } 670 }
696 CCArgs.push_back(OutputBinary.c_str()); // Output to the right file... 671 CCArgs.push_back(OutputBinary); // Output to the right file...
697 672
698 // Add any arguments intended for CC. We locate them here because this is 673 // Add any arguments intended for CC. We locate them here because this is
699 // most likely -L and -l options that need to come before other libraries but 674 // most likely -L and -l options that need to come before other libraries but
700 // after the source. Other options won't be sensitive to placement on the 675 // after the source. Other options won't be sensitive to placement on the
701 // command line, so this should be safe. 676 // command line, so this should be safe.
702 for (unsigned i = 0, e = ArgsForCC.size(); i != e; ++i) 677 for (unsigned i = 0, e = ArgsForCC.size(); i != e; ++i)
703 CCArgs.push_back(ArgsForCC[i].c_str()); 678 CCArgs.push_back(ArgsForCC[i]);
704 679
705 CCArgs.push_back("-lm"); // Hard-code the math library... 680 CCArgs.push_back("-lm"); // Hard-code the math library...
706 CCArgs.push_back("-O2"); // Optimize the program a bit... 681 CCArgs.push_back("-O2"); // Optimize the program a bit...
707 if (TargetTriple.getArch() == Triple::sparc) 682 if (TargetTriple.getArch() == Triple::sparc)
708 CCArgs.push_back("-mcpu=v9"); 683 CCArgs.push_back("-mcpu=v9");
709 CCArgs.push_back(nullptr); // NULL terminator
710 684
711 outs() << "<CC>"; 685 outs() << "<CC>";
712 outs().flush(); 686 outs().flush();
713 DEBUG(errs() << "\nAbout to run:\t"; 687 LLVM_DEBUG(errs() << "\nAbout to run:\t";
714 for (unsigned i = 0, e = CCArgs.size() - 1; i != e; ++i) errs() 688 for (unsigned i = 0, e = CCArgs.size() - 1; i != e; ++i) errs()
715 << " " << CCArgs[i]; 689 << " " << CCArgs[i];
716 errs() << "\n";); 690 errs() << "\n";);
717 if (RunProgramWithTimeout(CCPath, &CCArgs[0], "", "", "")) 691 if (RunProgramWithTimeout(CCPath, CCArgs, "", "", ""))
718 return ProcessFailure(CCPath, &CCArgs[0]); 692 return ProcessFailure(CCPath, CCArgs);
719 693
720 std::vector<const char *> ProgramArgs; 694 std::vector<StringRef> ProgramArgs;
721 695
722 // Declared here so that the destructor only runs after 696 // Declared here so that the destructor only runs after
723 // ProgramArgs is used. 697 // ProgramArgs is used.
724 std::string Exec; 698 std::string Exec;
725 699
726 if (RemoteClientPath.empty()) 700 if (RemoteClientPath.empty())
727 ProgramArgs.push_back(OutputBinary.c_str()); 701 ProgramArgs.push_back(OutputBinary);
728 else { 702 else {
729 ProgramArgs.push_back(RemoteClientPath.c_str()); 703 ProgramArgs.push_back(RemoteClientPath);
730 ProgramArgs.push_back(RemoteHost.c_str()); 704 ProgramArgs.push_back(RemoteHost);
731 if (!RemoteUser.empty()) { 705 if (!RemoteUser.empty()) {
732 ProgramArgs.push_back("-l"); 706 ProgramArgs.push_back("-l");
733 ProgramArgs.push_back(RemoteUser.c_str()); 707 ProgramArgs.push_back(RemoteUser);
734 } 708 }
735 if (!RemotePort.empty()) { 709 if (!RemotePort.empty()) {
736 ProgramArgs.push_back("-p"); 710 ProgramArgs.push_back("-p");
737 ProgramArgs.push_back(RemotePort.c_str()); 711 ProgramArgs.push_back(RemotePort);
738 } 712 }
739 if (!RemoteExtra.empty()) { 713 if (!RemoteExtra.empty()) {
740 ProgramArgs.push_back(RemoteExtra.c_str()); 714 ProgramArgs.push_back(RemoteExtra);
741 } 715 }
742 716
743 // Full path to the binary. We need to cd to the exec directory because 717 // Full path to the binary. We need to cd to the exec directory because
744 // there is a dylib there that the exec expects to find in the CWD 718 // there is a dylib there that the exec expects to find in the CWD
745 char *env_pwd = getenv("PWD"); 719 char *env_pwd = getenv("PWD");
746 Exec = "cd "; 720 Exec = "cd ";
747 Exec += env_pwd; 721 Exec += env_pwd;
748 Exec += "; ./"; 722 Exec += "; ./";
749 Exec += OutputBinary.c_str(); 723 Exec += OutputBinary.c_str();
750 ProgramArgs.push_back(Exec.c_str()); 724 ProgramArgs.push_back(Exec);
751 } 725 }
752 726
753 // Add optional parameters to the running program from Argv 727 // Add optional parameters to the running program from Argv
754 for (unsigned i = 0, e = Args.size(); i != e; ++i) 728 for (unsigned i = 0, e = Args.size(); i != e; ++i)
755 ProgramArgs.push_back(Args[i].c_str()); 729 ProgramArgs.push_back(Args[i]);
756 ProgramArgs.push_back(nullptr); // NULL terminator
757 730
758 // Now that we have a binary, run it! 731 // Now that we have a binary, run it!
759 outs() << "<program>"; 732 outs() << "<program>";
760 outs().flush(); 733 outs().flush();
761 DEBUG(errs() << "\nAbout to run:\t"; 734 LLVM_DEBUG(
762 for (unsigned i = 0, e = ProgramArgs.size() - 1; i != e; ++i) errs() 735 errs() << "\nAbout to run:\t";
763 << " " << ProgramArgs[i]; 736 for (unsigned i = 0, e = ProgramArgs.size() - 1; i != e; ++i) errs()
764 errs() << "\n";); 737 << " " << ProgramArgs[i];
738 errs() << "\n";);
765 739
766 FileRemover OutputBinaryRemover(OutputBinary.str(), !SaveTemps); 740 FileRemover OutputBinaryRemover(OutputBinary.str(), !SaveTemps);
767 741
768 if (RemoteClientPath.empty()) { 742 if (RemoteClientPath.empty()) {
769 DEBUG(errs() << "<run locally>"); 743 LLVM_DEBUG(errs() << "<run locally>");
770 std::string Error; 744 std::string Error;
771 int ExitCode = RunProgramWithTimeout(OutputBinary.str(), &ProgramArgs[0], 745 int ExitCode = RunProgramWithTimeout(OutputBinary.str(), ProgramArgs,
772 InputFile, OutputFile, OutputFile, 746 InputFile, OutputFile, OutputFile,
773 Timeout, MemoryLimit, &Error); 747 Timeout, MemoryLimit, &Error);
774 // Treat a signal (usually SIGSEGV) or timeout as part of the program output 748 // Treat a signal (usually SIGSEGV) or timeout as part of the program output
775 // so that crash-causing miscompilation is handled seamlessly. 749 // so that crash-causing miscompilation is handled seamlessly.
776 if (ExitCode < -1) { 750 if (ExitCode < -1) {
780 } 754 }
781 return ExitCode; 755 return ExitCode;
782 } else { 756 } else {
783 outs() << "<run remotely>"; 757 outs() << "<run remotely>";
784 outs().flush(); 758 outs().flush();
785 return RunProgramRemotelyWithTimeout(RemoteClientPath, &ProgramArgs[0], 759 return RunProgramRemotelyWithTimeout(RemoteClientPath, ProgramArgs,
786 InputFile, OutputFile, OutputFile, 760 InputFile, OutputFile, OutputFile,
787 Timeout, MemoryLimit); 761 Timeout, MemoryLimit);
788 } 762 }
789 } 763 }
790 764
798 errs() << "Error making unique filename: " << EC.message() << "\n"; 772 errs() << "Error making unique filename: " << EC.message() << "\n";
799 exit(1); 773 exit(1);
800 } 774 }
801 OutputFile = UniqueFilename.str(); 775 OutputFile = UniqueFilename.str();
802 776
803 std::vector<const char *> CCArgs; 777 std::vector<StringRef> CCArgs;
804 778
805 CCArgs.push_back(CCPath.c_str()); 779 CCArgs.push_back(CCPath);
806 780
807 if (TargetTriple.getArch() == Triple::x86) 781 if (TargetTriple.getArch() == Triple::x86)
808 CCArgs.push_back("-m32"); 782 CCArgs.push_back("-m32");
809 783
810 for (std::vector<std::string>::const_iterator I = ccArgs.begin(), 784 for (std::vector<std::string>::const_iterator I = ccArgs.begin(),
811 E = ccArgs.end(); 785 E = ccArgs.end();
812 I != E; ++I) 786 I != E; ++I)
813 CCArgs.push_back(I->c_str()); 787 CCArgs.push_back(*I);
814 788
815 // Compile the C/asm file into a shared object 789 // Compile the C/asm file into a shared object
816 if (fileType != ObjectFile) { 790 if (fileType != ObjectFile) {
817 CCArgs.push_back("-x"); 791 CCArgs.push_back("-x");
818 CCArgs.push_back(fileType == AsmFile ? "assembler" : "c"); 792 CCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
819 } 793 }
820 CCArgs.push_back("-fno-strict-aliasing"); 794 CCArgs.push_back("-fno-strict-aliasing");
821 CCArgs.push_back(InputFile.c_str()); // Specify the input filename. 795 CCArgs.push_back(InputFile); // Specify the input filename.
822 CCArgs.push_back("-x"); 796 CCArgs.push_back("-x");
823 CCArgs.push_back("none"); 797 CCArgs.push_back("none");
824 if (TargetTriple.getArch() == Triple::sparc) 798 if (TargetTriple.getArch() == Triple::sparc)
825 CCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc 799 CCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc
826 else if (TargetTriple.isOSDarwin()) { 800 else if (TargetTriple.isOSDarwin()) {
840 814
841 if (TargetTriple.getArch() == Triple::sparc) 815 if (TargetTriple.getArch() == Triple::sparc)
842 CCArgs.push_back("-mcpu=v9"); 816 CCArgs.push_back("-mcpu=v9");
843 817
844 CCArgs.push_back("-o"); 818 CCArgs.push_back("-o");
845 CCArgs.push_back(OutputFile.c_str()); // Output to the right filename. 819 CCArgs.push_back(OutputFile); // Output to the right filename.
846 CCArgs.push_back("-O2"); // Optimize the program a bit. 820 CCArgs.push_back("-O2"); // Optimize the program a bit.
847 821
848 // Add any arguments intended for CC. We locate them here because this is 822 // Add any arguments intended for CC. We locate them here because this is
849 // most likely -L and -l options that need to come before other libraries but 823 // most likely -L and -l options that need to come before other libraries but
850 // after the source. Other options won't be sensitive to placement on the 824 // after the source. Other options won't be sensitive to placement on the
851 // command line, so this should be safe. 825 // command line, so this should be safe.
852 for (unsigned i = 0, e = ArgsForCC.size(); i != e; ++i) 826 for (unsigned i = 0, e = ArgsForCC.size(); i != e; ++i)
853 CCArgs.push_back(ArgsForCC[i].c_str()); 827 CCArgs.push_back(ArgsForCC[i]);
854 CCArgs.push_back(nullptr); // NULL terminator
855 828
856 outs() << "<CC>"; 829 outs() << "<CC>";
857 outs().flush(); 830 outs().flush();
858 DEBUG(errs() << "\nAbout to run:\t"; 831 LLVM_DEBUG(errs() << "\nAbout to run:\t";
859 for (unsigned i = 0, e = CCArgs.size() - 1; i != e; ++i) errs() 832 for (unsigned i = 0, e = CCArgs.size() - 1; i != e; ++i) errs()
860 << " " << CCArgs[i]; 833 << " " << CCArgs[i];
861 errs() << "\n";); 834 errs() << "\n";);
862 if (RunProgramWithTimeout(CCPath, &CCArgs[0], "", "", "")) 835 if (RunProgramWithTimeout(CCPath, CCArgs, "", "", ""))
863 return ProcessFailure(CCPath, &CCArgs[0]); 836 return ProcessFailure(CCPath, CCArgs);
864 return Error::success(); 837 return Error::success();
865 } 838 }
866 839
867 /// create - Try to find the CC executable 840 /// create - Try to find the CC executable
868 /// 841 ///
869 CC *CC::create(std::string &Message, const std::string &CCBinary, 842 CC *CC::create(const char *Argv0, std::string &Message,
843 const std::string &CCBinary,
870 const std::vector<std::string> *Args) { 844 const std::vector<std::string> *Args) {
871 auto CCPath = sys::findProgramByName(CCBinary); 845 auto CCPath = FindProgramByName(CCBinary, Argv0, (void *)(intptr_t)&create);
872 if (!CCPath) { 846 if (!CCPath) {
873 Message = "Cannot find `" + CCBinary + "' in PATH: " + 847 Message = "Cannot find `" + CCBinary + "' in PATH: " +
874 CCPath.getError().message() + "\n"; 848 CCPath.getError().message() + "\n";
875 return nullptr; 849 return nullptr;
876 } 850 }