llvm.org GIT mirror llvm / 6585b38
Have sys::FindProgramByName return a std::string. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@183928 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 6 years ago
6 changed file(s) with 28 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
2828 /// @returns A Path object initialized to the path of the program or a
2929 /// Path object that is empty (invalid) if the program could not be found.
3030 /// @brief Construct a Program by finding it by name.
31 Path FindProgramByName(const std::string& name);
31 std::string FindProgramByName(const std::string& name);
3232
3333 // These functions change the specified standard stream (stdin, stdout, or
3434 // stderr) to binary mode. They return errc::success if the specified stream
4747 using namespace sys;
4848
4949 // This function just uses the PATH environment variable to find the program.
50 Path
50 std::string
5151 sys::FindProgramByName(const std::string& progName) {
5252
5353 // Check some degenerate cases
5454 if (progName.length() == 0) // no program
55 return Path();
55 return "";
5656 Path temp;
5757 if (!temp.set(progName)) // invalid name
58 return Path();
58 return "";
5959 // Use the given path verbatim if it contains any slashes; this matches
6060 // the behavior of sh(1) and friends.
6161 if (progName.find('/') != std::string::npos)
62 return temp;
62 return temp.str();
6363
6464 // At this point, the file name is valid and does not contain slashes. Search
6565 // for it through the directories specified in the PATH environment variable.
6767 // Get the path. If its empty, we can't do anything to find it.
6868 const char *PathStr = getenv("PATH");
6969 if (PathStr == 0)
70 return Path();
70 return "";
7171
7272 // Now we have a colon separated list of directories to search; try them.
7373 size_t PathLen = strlen(PathStr);
8080 if (FilePath.set(std::string(PathStr,Colon))) {
8181 FilePath.appendComponent(progName);
8282 if (FilePath.canExecute())
83 return FilePath; // Found the executable!
83 return FilePath.str(); // Found the executable!
8484 }
8585
8686 // Nope it wasn't in this directory, check the next path in the list!
9393 PathLen--;
9494 }
9595 }
96 return Path();
96 return "";
9797 }
9898
9999 static bool RedirectIO(const Path *Path, int FD, std::string* ErrMsg) {
3232 using namespace sys;
3333
3434 // This function just uses the PATH environment variable to find the program.
35 Path sys::FindProgramByName(const std::string& progName) {
35 std::string sys::FindProgramByName(const std::string &progName) {
3636 // Check some degenerate cases
3737 if (progName.length() == 0) // no program
38 return Path();
38 return "";
3939 Path temp;
4040 if (!temp.set(progName)) // invalid name
41 return Path();
41 return "";
4242 // Return paths with slashes verbatim.
4343 if (progName.find('\\') != std::string::npos ||
4444 progName.find('/') != std::string::npos)
45 return temp;
45 return temp.str();
4646
4747 // At this point, the file name is valid and does not contain slashes.
4848 // Let Windows search for it.
5353
5454 // See if it wasn't found.
5555 if (len == 0)
56 return Path();
56 return "";
5757
5858 // See if we got the entire path.
5959 if (len < MAX_PATH)
60 return Path(buffer);
60 return std::string(buffer);
6161
6262 // Buffer was too small; grow and retry.
6363 while (true) {
6767 // It is unlikely the search failed, but it's always possible some file
6868 // was added or removed since the last search, so be paranoid...
6969 if (len2 == 0)
70 return Path();
70 return "";
7171 else if (len2 <= len)
72 return Path(b);
72 return std::string(b);
7373
7474 len = len2;
7575 }
147147 return 1;
148148 }
149149
150 sys::Path tool = sys::FindProgramByName("opt");
150 std::string tool = sys::FindProgramByName("opt");
151151 if (tool.empty()) {
152152 errs() << "Cannot find `opt' in PATH!\n";
153153 return 1;
158158
159159 // setup the child process' arguments
160160 SmallVector Args;
161 std::string Opt = tool.str();
162161 if (UseValgrind) {
163162 Args.push_back("valgrind");
164163 Args.push_back("--error-exitcode=1");
165164 Args.push_back("-q");
166165 Args.push_back(tool.c_str());
167166 } else
168 Args.push_back(Opt.c_str());
167 Args.push_back(tool.c_str());
169168
170169 Args.push_back("-o");
171170 Args.push_back(OutputFilename.c_str());
399399 pos = CommandLine.find_first_of(delimiters, lastPos);
400400 }
401401
402 CmdPath = sys::FindProgramByName(Command).str();
402 CmdPath = sys::FindProgramByName(Command);
403403 if (CmdPath.empty()) {
404404 Message =
405405 std::string("Cannot find '") + Command +
874874 GCC *GCC::create(std::string &Message,
875875 const std::string &GCCBinary,
876876 const std::vector *Args) {
877 sys::Path GCCPath = sys::FindProgramByName(GCCBinary);
878 if (GCCPath.isEmpty()) {
877 std::string GCCPath = sys::FindProgramByName(GCCBinary);
878 if (GCCPath.empty()) {
879879 Message = "Cannot find `"+ GCCBinary +"' in PATH!\n";
880880 return 0;
881881 }
882882
883 sys::Path RemoteClientPath;
883 std::string RemoteClientPath;
884884 if (!RemoteClient.empty())
885885 RemoteClientPath = sys::FindProgramByName(RemoteClient);
886886
887 Message = "Found gcc: " + GCCPath.str() + "\n";
888 return new GCC(GCCPath.str(), RemoteClientPath.str(), Args);
889 }
887 Message = "Found gcc: " + GCCPath + "\n";
888 return new GCC(GCCPath, RemoteClientPath, Args);
889 }
1212 using namespace llvm;
1313
1414 int main(int argc, const char **argv) {
15 sys::Path Program = sys::FindProgramByName(argv[1]);
15 std::string Program = sys::FindProgramByName(argv[1]);
1616
1717 std::string ErrMsg;
18 int Result = sys::ExecuteAndWait(Program, argv + 1, 0, 0, 0, 0, &ErrMsg);
18 int Result =
19 sys::ExecuteAndWait(sys::Path(Program), argv + 1, 0, 0, 0, 0, &ErrMsg);
1920 if (Result < 0) {
2021 errs() << "Error: " << ErrMsg << "\n";
2122 return 1;