From 9f61e2a235fc04e57237dc932aee61b264ef8916 Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 13 Dec 2007 17:56:50 -0500 Subject: ENH: Centralized and globalized computation of CMake program locations. This eliminates startup paths that failed to produce this information. --- Source/CPack/cpack.cxx | 3 +- Source/CTest/cmCTestScriptHandler.cxx | 2 +- Source/CursesDialog/ccmake.cxx | 1 + Source/FLTKDialog/FLTKDialog.cxx | 3 ++ Source/MFCDialog/CMakeSetup.cpp | 4 ++- Source/QtDialog/CMakeSetup.cxx | 4 ++- Source/cmCTest.cxx | 67 ++++++----------------------------- Source/cmCTest.h | 2 +- Source/cmDumpDocumentation.cxx | 1 + Source/cmSystemTools.cxx | 23 ++++++++++++ Source/cmSystemTools.h | 8 +++++ Source/cmake.cxx | 54 ++++------------------------ Source/cmake.h | 2 +- Source/cmakemain.cxx | 3 +- Source/ctest.cxx | 1 + 15 files changed, 68 insertions(+), 110 deletions(-) diff --git a/Source/CPack/cpack.cxx b/Source/CPack/cpack.cxx index 4da903e..b36e230 100644 --- a/Source/CPack/cpack.cxx +++ b/Source/CPack/cpack.cxx @@ -126,6 +126,7 @@ int cpackDefinitionArgument(const char* argument, const char* cValue, // this is CPack. int main (int argc, char *argv[]) { + cmSystemTools::FindExecutableDirectory(argv[0]); cmCPackLog log; log.SetErrorPrefix("CPack Error: "); log.SetWarningPrefix("CPack Warning: "); @@ -246,7 +247,7 @@ int main (int argc, char *argv[]) { // find out which system cpack is running on, so it can setup the search // paths, so FIND_XXX() commands can be used in scripts - cminst.AddCMakePaths(argv[0]); + cminst.AddCMakePaths(); std::string systemFile = globalMF->GetModulesFile("CMakeDetermineSystem.cmake"); if (!globalMF->ReadListFile(0, systemFile.c_str())) diff --git a/Source/CTest/cmCTestScriptHandler.cxx b/Source/CTest/cmCTestScriptHandler.cxx index 64cf92d..c30e281 100644 --- a/Source/CTest/cmCTestScriptHandler.cxx +++ b/Source/CTest/cmCTestScriptHandler.cxx @@ -293,7 +293,7 @@ void cmCTestScriptHandler::CreateCMake() delete this->LocalGenerator; } this->CMake = new cmake; - this->CMake->AddCMakePaths(this->CTest->GetCTestExecutable()); + this->CMake->AddCMakePaths(); this->GlobalGenerator = new cmGlobalGenerator; this->GlobalGenerator->SetCMakeInstance(this->CMake); diff --git a/Source/CursesDialog/ccmake.cxx b/Source/CursesDialog/ccmake.cxx index 7f9b3f6..a2f86a3 100644 --- a/Source/CursesDialog/ccmake.cxx +++ b/Source/CursesDialog/ccmake.cxx @@ -105,6 +105,7 @@ void CMakeErrorHandler(const char* message, const char* title, bool&, void* clie int main(int argc, char** argv) { + cmSystemTools::FindExecutableDirectory(argv[0]); cmDocumentation doc; if(doc.CheckOptions(argc, argv)) { diff --git a/Source/FLTKDialog/FLTKDialog.cxx b/Source/FLTKDialog/FLTKDialog.cxx index dedb2e4..df7df77 100644 --- a/Source/FLTKDialog/FLTKDialog.cxx +++ b/Source/FLTKDialog/FLTKDialog.cxx @@ -19,8 +19,11 @@ #include "FL/Fl.H" #include "FL/fl_ask.H" +#include "cmSystemTools.h" + int main(int argc, char * argv[] ) { + cmSystemTools::FindExecutableDirectory(argv[0]); fl_message_font(FL_HELVETICA,11); diff --git a/Source/MFCDialog/CMakeSetup.cpp b/Source/MFCDialog/CMakeSetup.cpp index 6d25ba2..8d318a6 100644 --- a/Source/MFCDialog/CMakeSetup.cpp +++ b/Source/MFCDialog/CMakeSetup.cpp @@ -7,6 +7,7 @@ #include "CMakeCommandLineInfo.h" #include "../cmDocumentation.h" #include "../cmake.h" +#include "../cmSystemTools.h" //---------------------------------------------------------------------------- @@ -101,6 +102,7 @@ BOOL CMakeSetup::InitInstance() #endif CMakeCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); + cmSystemTools::FindExecutableDirectory(cmdInfo.GetArgV()[0]); // Check for documentation options. If there are no arguments skip // the check because the GUI should be displayed instead of showing @@ -111,7 +113,7 @@ BOOL CMakeSetup::InitInstance() { // Construct and print requested documentation. cmake hcm; - hcm.AddCMakePaths(cmdInfo.GetArgV()[0]); + hcm.AddCMakePaths(); doc.SetCMakeRoot(hcm.GetCacheDefinition("CMAKE_ROOT")); std::vector commands; std::vector compatCommands; diff --git a/Source/QtDialog/CMakeSetup.cxx b/Source/QtDialog/CMakeSetup.cxx index c7f5f54..3543903 100644 --- a/Source/QtDialog/CMakeSetup.cxx +++ b/Source/QtDialog/CMakeSetup.cxx @@ -21,6 +21,7 @@ #include "CMakeSetupDialog.h" #include "cmDocumentation.h" +#include "cmSystemTools.h" #include "cmake.h" //---------------------------------------------------------------------------- @@ -61,6 +62,7 @@ static const char * cmDocumentationOptions[][3] = int main(int argc, char** argv) { + cmSystemTools::FindExecutableDirectory(argv[0]); QApplication app(argc, argv); app.setApplicationName("CMakeSetup"); app.setOrganizationName("Kitware"); @@ -72,7 +74,7 @@ int main(int argc, char** argv) { // Construct and print requested documentation. cmake hcm; - hcm.AddCMakePaths(app.argv()[0]); + hcm.AddCMakePaths(); doc.SetCMakeRoot(hcm.GetCacheDefinition("CMAKE_ROOT")); std::vector commands; std::vector compatCommands; diff --git a/Source/cmCTest.cxx b/Source/cmCTest.cxx index 75c2fa7..97132ae 100644 --- a/Source/cmCTest.cxx +++ b/Source/cmCTest.cxx @@ -1774,7 +1774,7 @@ void cmCTest::HandleScriptArguments(size_t &i, // the main entry point of ctest, called from main int cmCTest::Run(std::vector &args, std::string* output) { - this->FindRunningCMake(args[0].c_str()); + this->FindRunningCMake(); const char* ctestExec = "ctest"; bool cmakeAndTest = false; bool performSomeTest = true; @@ -1970,70 +1970,25 @@ int cmCTest::Run(std::vector &args, std::string* output) } //---------------------------------------------------------------------- -void cmCTest::FindRunningCMake(const char* arg0) +void cmCTest::FindRunningCMake() { // Find our own executable. - std::vector failures; - this->CTestSelf = arg0; - cmSystemTools::ConvertToUnixSlashes(this->CTestSelf); - failures.push_back(this->CTestSelf); - this->CTestSelf = cmSystemTools::FindProgram(this->CTestSelf.c_str()); + this->CTestSelf = cmSystemTools::GetExecutableDirectory(); + this->CTestSelf += "/ctest"; + this->CTestSelf += cmSystemTools::GetExecutableExtension(); if(!cmSystemTools::FileExists(this->CTestSelf.c_str())) { - failures.push_back(this->CTestSelf); - this->CTestSelf = "/usr/local/bin/ctest"; + cmSystemTools::Error("CTest executable cannot be found at ", + this->CTestSelf.c_str()); } - if(!cmSystemTools::FileExists(this->CTestSelf.c_str())) - { - failures.push_back(this->CTestSelf); - cmOStringStream msg; - msg << "CTEST can not find the command line program ctest.\n"; - msg << " argv[0] = \"" << arg0 << "\"\n"; - msg << " Attempted paths:\n"; - std::vector::iterator i; - for(i=failures.begin(); i != failures.end(); ++i) - { - msg << " \"" << i->c_str() << "\"\n"; - } - cmSystemTools::Error(msg.str().c_str()); - } - std::string dir; - std::string file; - if(cmSystemTools::SplitProgramPath(this->CTestSelf.c_str(), - dir, file, true)) - { - this->CMakeSelf = dir += "/cmake"; - this->CMakeSelf += cmSystemTools::GetExecutableExtension(); - if(cmSystemTools::FileExists(this->CMakeSelf.c_str())) - { - return; - } - } - failures.push_back(this->CMakeSelf); -#ifdef CMAKE_BUILD_DIR - std::string intdir = "."; -#ifdef CMAKE_INTDIR - intdir = CMAKE_INTDIR; -#endif - this->CMakeSelf = CMAKE_BUILD_DIR; - this->CMakeSelf += "/bin/"; - this->CMakeSelf += intdir; + + this->CMakeSelf = cmSystemTools::GetExecutableDirectory(); this->CMakeSelf += "/cmake"; this->CMakeSelf += cmSystemTools::GetExecutableExtension(); -#endif if(!cmSystemTools::FileExists(this->CMakeSelf.c_str())) { - failures.push_back(this->CMakeSelf); - cmOStringStream msg; - msg << "CTEST can not find the command line program cmake.\n"; - msg << " argv[0] = \"" << arg0 << "\"\n"; - msg << " Attempted paths:\n"; - std::vector::iterator i; - for(i=failures.begin(); i != failures.end(); ++i) - { - msg << " \"" << i->c_str() << "\"\n"; - } - cmSystemTools::Error(msg.str().c_str()); + cmSystemTools::Error("CMake executable cannot be found at ", + this->CMakeSelf.c_str()); } } diff --git a/Source/cmCTest.h b/Source/cmCTest.h index 99ccf0a..2a1169a 100644 --- a/Source/cmCTest.h +++ b/Source/cmCTest.h @@ -398,7 +398,7 @@ private: const VectorOfStrings& files); ///! Find the running cmake - void FindRunningCMake(const char* arg0); + void FindRunningCMake(); //! Check if the argument is the one specified bool CheckArgument(const std::string& arg, const char* varg1, diff --git a/Source/cmDumpDocumentation.cxx b/Source/cmDumpDocumentation.cxx index b59b114..838dbe9 100644 --- a/Source/cmDumpDocumentation.cxx +++ b/Source/cmDumpDocumentation.cxx @@ -121,6 +121,7 @@ int DumpForCoverage(const char* outname) int main(int ac, char** av) { cmSystemTools::EnableMSVCDebugHook(); + cmSystemTools::FindExecutableDirectory(av[0]); const char* outname = "cmake.html"; bool coverage = false; if(ac > 1) diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 52031ea..52e1ca2 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -2064,3 +2064,26 @@ bool cmSystemTools::CopyFileTime(const char* fromFile, const char* toFile) #endif return true; } + +//---------------------------------------------------------------------------- +static std::string cmSystemToolsExecutableDirectory; +void cmSystemTools::FindExecutableDirectory(const char* argv0) +{ + std::string errorMsg; + std::string exe; + if(cmSystemTools::FindProgramPath(argv0, exe, errorMsg)) + { + cmSystemToolsExecutableDirectory = + cmSystemTools::GetFilenamePath(exe.c_str()); + } + else + { + // ??? + } +} + +//---------------------------------------------------------------------------- +const char* cmSystemTools::GetExecutableDirectory() +{ + return cmSystemToolsExecutableDirectory.c_str(); +} diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h index 9007537..bbc9ba6 100644 --- a/Source/cmSystemTools.h +++ b/Source/cmSystemTools.h @@ -350,6 +350,14 @@ public: the first argument to that named by the second. */ static bool CopyFileTime(const char* fromFile, const char* toFile); + /** Find the directory containing the running executable. Save it + in a global location to be queried by GetExecutableDirectory + later. */ + static void FindExecutableDirectory(const char* argv0); + + /** Get the directory containing the currently running executable. */ + static const char* GetExecutableDirectory(); + private: static bool s_ForceUnixPaths; static bool s_RunCommandHideConsole; diff --git a/Source/cmake.cxx b/Source/cmake.cxx index a3c95be..fd59808 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -739,56 +739,16 @@ void cmake::SetDirectoriesFromFile(const char* arg) // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the // cache -int cmake::AddCMakePaths(const char *arg0) +int cmake::AddCMakePaths() { // Find the cmake executable - std::vector failures; - std::string cMakeSelf = arg0; - cmSystemTools::ConvertToUnixSlashes(cMakeSelf); - if ((strstr(arg0, "cpack")!=0) || (strstr(arg0, "ctest")!=0)) - { - // when called from cpack or ctest CMAKE_COMMAND would otherwise point - // to cpack or ctest and not cmake - cMakeSelf = cmSystemTools::GetFilenamePath(cMakeSelf) + - "/cmake" + cmSystemTools::GetFilenameExtension(cMakeSelf); - } - failures.push_back(cMakeSelf); - cMakeSelf = cmSystemTools::FindProgram(cMakeSelf.c_str()); - cmSystemTools::ConvertToUnixSlashes(cMakeSelf); - if(!cmSystemTools::FileExists(cMakeSelf.c_str())) - { -#ifdef CMAKE_BUILD_DIR - std::string intdir = "."; -#ifdef CMAKE_INTDIR - intdir = CMAKE_INTDIR; -#endif - cMakeSelf = CMAKE_BUILD_DIR; - cMakeSelf += "/bin/"; - cMakeSelf += intdir; + std::string cMakeSelf = cmSystemTools::GetExecutableDirectory(); cMakeSelf += "/cmake"; cMakeSelf += cmSystemTools::GetExecutableExtension(); -#endif - } -#ifdef CMAKE_PREFIX if(!cmSystemTools::FileExists(cMakeSelf.c_str())) { - failures.push_back(cMakeSelf); - cMakeSelf = CMAKE_PREFIX "/bin/cmake"; - } -#endif - if(!cmSystemTools::FileExists(cMakeSelf.c_str())) - { - failures.push_back(cMakeSelf); - cmOStringStream msg; - msg << "CMAKE can not find the command line program cmake.\n"; - msg << " argv[0] = \"" << arg0 << "\"\n"; - msg << " Attempted paths:\n"; - std::vector::iterator i; - for(i=failures.begin(); i != failures.end(); ++i) - { - msg << " \"" << i->c_str() << "\"\n"; - } - cmSystemTools::Error(msg.str().c_str()); + cmSystemTools::Error("CMake executable cannot be found at ", + cMakeSelf.c_str()); return 0; } // Save the value in the cache @@ -2157,7 +2117,7 @@ int cmake::Run(const std::vector& args, bool noconfigure) } else { - this->AddCMakePaths(this->CMakeCommand.c_str()); + this->AddCMakePaths(); } // Add any cache args @@ -2376,7 +2336,7 @@ int cmake::LoadCache() } // setup CMAKE_ROOT and CMAKE_COMMAND - if(!this->AddCMakePaths(this->CMakeCommand.c_str())) + if(!this->AddCMakePaths()) { return -3; } @@ -3503,7 +3463,7 @@ int cmake::GetSystemInformation(std::vector& args) // we have to find the module directory, so we can copy the files - this->AddCMakePaths(args[0].c_str()); + this->AddCMakePaths(); std::string modulesPath = this->CacheManager->GetCacheValue("CMAKE_ROOT"); modulesPath += "/Modules"; diff --git a/Source/cmake.h b/Source/cmake.h index be03bdf..265fc0a 100644 --- a/Source/cmake.h +++ b/Source/cmake.h @@ -288,7 +288,7 @@ class cmake /** * Generate CMAKE_ROOT and CMAKE_COMMAND cache entries */ - int AddCMakePaths(const char *arg0); + int AddCMakePaths(); /** * Get the file comparison class diff --git a/Source/cmakemain.cxx b/Source/cmakemain.cxx index ebc547a..839c3f3 100644 --- a/Source/cmakemain.cxx +++ b/Source/cmakemain.cxx @@ -273,6 +273,7 @@ static void cmakemainProgressCallback(const char *m, float prog, int main(int ac, char** av) { cmSystemTools::EnableMSVCDebugHook(); + cmSystemTools::FindExecutableDirectory(av[0]); int ret = do_cmake(ac, av); #ifdef CMAKE_BUILD_WITH_CMAKE cmDynamicLoader::FlushCache(); @@ -299,7 +300,7 @@ int do_cmake(int ac, char** av) { // Construct and print requested documentation. cmake hcm; - hcm.AddCMakePaths(av[0]); + hcm.AddCMakePaths(); doc.SetCMakeRoot(hcm.GetCacheDefinition("CMAKE_ROOT")); // the command line args are processed here so that you can do diff --git a/Source/ctest.cxx b/Source/ctest.cxx index 27971cf..11e8dd2 100644 --- a/Source/ctest.cxx +++ b/Source/ctest.cxx @@ -210,6 +210,7 @@ int main (int argc, char *argv[]) { cmSystemTools::DoNotInheritStdPipes(); cmSystemTools::EnableMSVCDebugHook(); + cmSystemTools::FindExecutableDirectory(argv[0]); int nocwd = 0; cmCTest inst; -- cgit v0.12