diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 245b084ec..795208d3d 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -923,8 +923,35 @@ std::string cmSystemTools::TemporaryFileName() risk of setting rights with 0666 */ return tempnam(0, "cmake"); } - +/** + * Find the file the given name. Searches the given path and then + * the system search path. Returns the full path to the file if it is + * found. Otherwise, the empty string is returned. + */ +std::string cmSystemTools::FindFile(const char* name, + const std::vector& userPaths) +{ + // Add the system search path to our path. + std::vector path = userPaths; + cmSystemTools::GetPath(path); + + std::string tryPath; + for(std::vector::const_iterator p = path.begin(); + p != path.end(); ++p) + { + tryPath = *p; + tryPath += "/"; + tryPath += name; + if(cmSystemTools::FileExists(tryPath.c_str()) && + !cmSystemTools::FileIsDirectory(tryPath.c_str())) + { + return cmSystemTools::CollapseFullPath(tryPath.c_str()); + } + } + // Couldn't find the file. + return ""; +} /** * Find the executable with the given name. Searches the given path and then @@ -947,11 +974,11 @@ std::string cmSystemTools::FindProgram(const char* name, { return cmSystemTools::CollapseFullPath(tryPath.c_str()); } - + // Add the system search path to our path. std::vector path = userPaths; cmSystemTools::GetPath(path); - + for(std::vector::const_iterator p = path.begin(); p != path.end(); ++p) { @@ -970,7 +997,7 @@ std::string cmSystemTools::FindProgram(const char* name, return cmSystemTools::CollapseFullPath(tryPath.c_str()); } } - + // Couldn't find the program. return ""; } diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h index f6b435684..c7c44afb2 100644 --- a/Source/cmSystemTools.h +++ b/Source/cmSystemTools.h @@ -205,6 +205,10 @@ public: */ static bool IsOff(const char* val); + ///! Find a file in the system PATH, with optional extra paths. + static std::string FindFile(const char* name, + const std::vector& path= std::vector()); + ///! Find an executable in the system PATH, with optional extra paths. static std::string FindProgram(const char* name, const std::vector& path= std::vector());