@@ -202,50 +202,33 @@ Expected<int> LLI::ExecuteProgram(const std::string &Bitcode,
202
202
203
203
void AbstractInterpreter::anchor () {}
204
204
205
- #if defined(LLVM_ON_UNIX)
206
- const char EXESuffix[] = " " ;
207
- #elif defined(_WIN32)
208
- const char EXESuffix[] = " exe" ;
209
- #endif
210
-
211
- // / Prepend the path to the program being executed
212
- // / to \p ExeName, given the value of argv[0] and the address of main()
213
- // / itself. This allows us to find another LLVM tool if it is built in the same
214
- // / directory. An empty string is returned on error; note that this function
215
- // / just mainpulates the path and doesn't check for executability.
216
- // / Find a named executable.
217
- static std::string PrependMainExecutablePath (const std::string &ExeName,
205
+ ErrorOr<std::string> llvm::FindProgramByName (const std::string &ExeName,
218
206
const char *Argv0,
219
207
void *MainAddr) {
220
208
// Check the directory that the calling program is in. We can do
221
209
// this if ProgramPath contains at least one / character, indicating that it
222
210
// is a relative path to the executable itself.
223
211
std::string Main = sys::fs::getMainExecutable (Argv0, MainAddr);
224
212
StringRef Result = sys::path::parent_path (Main);
213
+ if (ErrorOr<std::string> Path = sys::findProgramByName (ExeName, Result))
214
+ return *Path;
225
215
226
- if (!Result.empty ()) {
227
- SmallString<128 > Storage = Result;
228
- sys::path::append (Storage, ExeName);
229
- sys::path::replace_extension (Storage, EXESuffix);
230
- return Storage.str ();
231
- }
232
-
233
- return Result.str ();
216
+ // Check the user PATH.
217
+ return sys::findProgramByName (ExeName);
234
218
}
235
219
236
220
// LLI create method - Try to find the LLI executable
237
221
AbstractInterpreter *
238
222
AbstractInterpreter::createLLI (const char *Argv0, std::string &Message,
239
223
const std::vector<std::string> *ToolArgs) {
240
- std::string LLIPath =
241
- PrependMainExecutablePath (" lli" , Argv0, (void *)(intptr_t )&createLLI);
242
- if (!LLIPath.empty ()) {
243
- Message = " Found lli: " + LLIPath + " \n " ;
244
- return new LLI (LLIPath, ToolArgs);
224
+ if (ErrorOr<std::string> LLIPath =
225
+ FindProgramByName (" lli" , Argv0, (void *)(intptr_t )&createLLI)) {
226
+ Message = " Found lli: " + *LLIPath + " \n " ;
227
+ return new LLI (*LLIPath, ToolArgs);
228
+ } else {
229
+ Message = LLIPath.getError ().message () + " \n " ;
230
+ return nullptr ;
245
231
}
246
-
247
- Message = " Cannot find `lli' in executable directory!\n " ;
248
- return nullptr ;
249
232
}
250
233
251
234
// ===---------------------------------------------------------------------===//
@@ -368,8 +351,9 @@ Expected<int> CustomExecutor::ExecuteProgram(
368
351
// '\ ' -> ' '
369
352
// 'exa\mple' -> 'example'
370
353
//
371
- static void lexCommand (std::string &Message, const std::string &CommandLine,
372
- std::string &CmdPath, std::vector<std::string> &Args) {
354
+ static void lexCommand (const char *Argv0, std::string &Message,
355
+ const std::string &CommandLine, std::string &CmdPath,
356
+ std::vector<std::string> &Args) {
373
357
374
358
std::string Token;
375
359
std::string Command;
@@ -402,7 +386,7 @@ static void lexCommand(std::string &Message, const std::string &CommandLine,
402
386
Token.push_back (CommandLine[Pos]);
403
387
}
404
388
405
- auto Path = sys::findProgramByName (Command);
389
+ auto Path = FindProgramByName (Command, Argv0, ( void *)( intptr_t )&lexCommand );
406
390
if (!Path) {
407
391
Message = std::string (" Cannot find '" ) + Command +
408
392
" ' in PATH: " + Path.getError ().message () + " \n " ;
@@ -416,11 +400,12 @@ static void lexCommand(std::string &Message, const std::string &CommandLine,
416
400
// Custom execution environment create method, takes the execution command
417
401
// as arguments
418
402
AbstractInterpreter *AbstractInterpreter::createCustomCompiler (
419
- std::string &Message, const std::string &CompileCommandLine) {
403
+ const char *Argv0, std::string &Message,
404
+ const std::string &CompileCommandLine) {
420
405
421
406
std::string CmdPath;
422
407
std::vector<std::string> Args;
423
- lexCommand (Message, CompileCommandLine, CmdPath, Args);
408
+ lexCommand (Argv0, Message, CompileCommandLine, CmdPath, Args);
424
409
if (CmdPath.empty ())
425
410
return nullptr ;
426
411
@@ -430,12 +415,13 @@ AbstractInterpreter *AbstractInterpreter::createCustomCompiler(
430
415
// Custom execution environment create method, takes the execution command
431
416
// as arguments
432
417
AbstractInterpreter *
433
- AbstractInterpreter::createCustomExecutor (std::string &Message,
418
+ AbstractInterpreter::createCustomExecutor (const char *Argv0,
419
+ std::string &Message,
434
420
const std::string &ExecCommandLine) {
435
421
436
422
std::string CmdPath;
437
423
std::vector<std::string> Args;
438
- lexCommand (Message, ExecCommandLine, CmdPath, Args);
424
+ lexCommand (Argv0, Message, ExecCommandLine, CmdPath, Args);
439
425
if (CmdPath.empty ())
440
426
return nullptr ;
441
427
@@ -524,20 +510,20 @@ LLC *AbstractInterpreter::createLLC(const char *Argv0, std::string &Message,
524
510
const std::vector<std::string> *Args,
525
511
const std::vector<std::string> *CCArgs,
526
512
bool UseIntegratedAssembler) {
527
- std::string LLCPath =
528
- PrependMainExecutablePath (" llc" , Argv0, (void *)(intptr_t )&createLLC);
529
- if (LLCPath. empty () ) {
530
- Message = " Cannot find `llc' in executable directory! \n " ;
513
+ ErrorOr< std::string> LLCPath =
514
+ FindProgramByName (" llc" , Argv0, (void *)(intptr_t )&createLLC);
515
+ if (! LLCPath) {
516
+ Message = LLCPath. getError (). message () + " \n " ;
531
517
return nullptr ;
532
518
}
533
519
534
- CC *cc = CC::create (Message, CCBinary, CCArgs);
520
+ CC *cc = CC::create (Argv0, Message, CCBinary, CCArgs);
535
521
if (!cc) {
536
522
errs () << Message << " \n " ;
537
523
exit (1 );
538
524
}
539
- Message = " Found llc: " + LLCPath + " \n " ;
540
- return new LLC (LLCPath, cc, Args, UseIntegratedAssembler);
525
+ Message = " Found llc: " + * LLCPath + " \n " ;
526
+ return new LLC (* LLCPath, cc, Args, UseIntegratedAssembler);
541
527
}
542
528
543
529
// ===---------------------------------------------------------------------===//
@@ -606,15 +592,14 @@ Expected<int> JIT::ExecuteProgram(const std::string &Bitcode,
606
592
AbstractInterpreter *
607
593
AbstractInterpreter::createJIT (const char *Argv0, std::string &Message,
608
594
const std::vector<std::string> *Args) {
609
- std::string LLIPath =
610
- PrependMainExecutablePath (" lli" , Argv0, (void *)(intptr_t )&createJIT);
611
- if (!LLIPath.empty ()) {
612
- Message = " Found lli: " + LLIPath + " \n " ;
613
- return new JIT (LLIPath, Args);
595
+ if (ErrorOr<std::string> LLIPath =
596
+ FindProgramByName (" lli" , Argv0, (void *)(intptr_t )&createJIT)) {
597
+ Message = " Found lli: " + *LLIPath + " \n " ;
598
+ return new JIT (*LLIPath, Args);
599
+ } else {
600
+ Message = LLIPath.getError ().message () + " \n " ;
601
+ return nullptr ;
614
602
}
615
-
616
- Message = " Cannot find `lli' in executable directory!\n " ;
617
- return nullptr ;
618
603
}
619
604
620
605
// ===---------------------------------------------------------------------===//
@@ -855,9 +840,10 @@ Error CC::MakeSharedObject(const std::string &InputFile, FileType fileType,
855
840
856
841
// / create - Try to find the CC executable
857
842
// /
858
- CC *CC::create (std::string &Message, const std::string &CCBinary,
843
+ CC *CC::create (const char *Argv0, std::string &Message,
844
+ const std::string &CCBinary,
859
845
const std::vector<std::string> *Args) {
860
- auto CCPath = sys::findProgramByName (CCBinary);
846
+ auto CCPath = FindProgramByName (CCBinary, Argv0, ( void *)( intptr_t )&create );
861
847
if (!CCPath) {
862
848
Message = " Cannot find `" + CCBinary + " ' in PATH: " +
863
849
CCPath.getError ().message () + " \n " ;
0 commit comments