diff --git a/Source/kwsys/SystemTools.cxx b/Source/kwsys/SystemTools.cxx index 82c6525aa..9dc7e9809 100644 --- a/Source/kwsys/SystemTools.cxx +++ b/Source/kwsys/SystemTools.cxx @@ -17,6 +17,7 @@ #include KWSYS_HEADER(ios/iostream) #include KWSYS_HEADER(ios/fstream) +#include KWSYS_HEADER(ios/sstream) #ifdef _MSC_VER # pragma warning (disable: 4786) @@ -1358,6 +1359,64 @@ bool SystemTools::SplitProgramPath(const char* in_name, return true; } +bool SystemTools::FindProgramPath(const char* argv0, + kwsys_stl::string& pathOut, + kwsys_stl::string& errorMsg, + const char* exeName, + const char* buildDir, + const char* installPrefix ) +{ + kwsys_stl::vector failures; + kwsys_stl::string self = argv0; + SystemTools::ConvertToUnixSlashes(self); + failures.push_back(argv0); + self = SystemTools::FindProgram(self.c_str()); + if(!SystemTools::FileExists(self.c_str())) + { + if(buildDir) + { + kwsys_stl::string intdir = "."; +#ifdef CMAKE_INTDIR + intdir = CMAKE_INTDIR; +#endif + self = buildDir; + self += "/bin/"; + self += intdir; + self += "/"; + self += exeName; + self += SystemTools::GetExecutableExtension(); + } + } + if(installPrefix) + { + if(!SystemTools::FileExists(self.c_str())) + { + failures.push_back(self); + self = installPrefix; + self += "/bin/"; + self += exeName; + } + } + if(!SystemTools::FileExists(self.c_str())) + { + failures.push_back(self); + kwsys_ios::ostringstream msg; + msg << "Can not find the command line program " << exeName << "\n"; + msg << " argv[0] = \"" << argv0 << "\"\n"; + msg << " Attempted paths:\n"; + kwsys_stl::vector::iterator i; + for(i=failures.begin(); i != failures.end(); ++i) + { + msg << " \"" << i->c_str() << "\"\n"; + } + errorMsg = msg.str(); + return false; + } + pathOut = self; + return true; +} + + kwsys_stl::string SystemTools::CollapseFullPath(const char* in_relative) { return SystemTools::CollapseFullPath(in_relative, 0); diff --git a/Source/kwsys/SystemTools.hxx.in b/Source/kwsys/SystemTools.hxx.in index 1676ff27d..7c6e2ce0a 100644 --- a/Source/kwsys/SystemTools.hxx.in +++ b/Source/kwsys/SystemTools.hxx.in @@ -188,6 +188,26 @@ public: kwsys_stl::string& file, bool errorReport = true); + /** + * Given argv[0] for a unix program find the full path to a running + * executable. argv0 can be null for windows WinMain programs + * in this case GetModuleFileName will be used to find the path + * to the running executable. If argv0 is not a full path, + * then this will try to find the full path. If the path is not + * found false is returned, if found true is returned. An error + * message of the attempted paths is stored in errorMsg. + * exeName is the name of the executable. + * buildDir is a possibly null path to the build directory. + * installPrefix is a possibly null pointer to the install directory. + + */ + static bool FindProgramPath(const char* argv0, + kwsys_stl::string& pathOut, + kwsys_stl::string& errorMsg, + const char* exeName = 0, + const char* buildDir = 0, + const char* installPrefix = 0); + /** * Given a path to a file or directory, convert it to a full path. * This collapses away relative paths relative to the cwd argument