ENH: Centralized and globalized computation of CMake program locations. This eliminates startup paths that failed to produce this information.

This commit is contained in:
Brad King 2007-12-13 17:56:50 -05:00
parent ebc036bbda
commit 9f61e2a235
15 changed files with 68 additions and 110 deletions

View File

@ -126,6 +126,7 @@ int cpackDefinitionArgument(const char* argument, const char* cValue,
// this is CPack. // this is CPack.
int main (int argc, char *argv[]) int main (int argc, char *argv[])
{ {
cmSystemTools::FindExecutableDirectory(argv[0]);
cmCPackLog log; cmCPackLog log;
log.SetErrorPrefix("CPack Error: "); log.SetErrorPrefix("CPack Error: ");
log.SetWarningPrefix("CPack Warning: "); 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 // find out which system cpack is running on, so it can setup the search
// paths, so FIND_XXX() commands can be used in scripts // paths, so FIND_XXX() commands can be used in scripts
cminst.AddCMakePaths(argv[0]); cminst.AddCMakePaths();
std::string systemFile = std::string systemFile =
globalMF->GetModulesFile("CMakeDetermineSystem.cmake"); globalMF->GetModulesFile("CMakeDetermineSystem.cmake");
if (!globalMF->ReadListFile(0, systemFile.c_str())) if (!globalMF->ReadListFile(0, systemFile.c_str()))

View File

@ -293,7 +293,7 @@ void cmCTestScriptHandler::CreateCMake()
delete this->LocalGenerator; delete this->LocalGenerator;
} }
this->CMake = new cmake; this->CMake = new cmake;
this->CMake->AddCMakePaths(this->CTest->GetCTestExecutable()); this->CMake->AddCMakePaths();
this->GlobalGenerator = new cmGlobalGenerator; this->GlobalGenerator = new cmGlobalGenerator;
this->GlobalGenerator->SetCMakeInstance(this->CMake); this->GlobalGenerator->SetCMakeInstance(this->CMake);

View File

@ -105,6 +105,7 @@ void CMakeErrorHandler(const char* message, const char* title, bool&, void* clie
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
cmSystemTools::FindExecutableDirectory(argv[0]);
cmDocumentation doc; cmDocumentation doc;
if(doc.CheckOptions(argc, argv)) if(doc.CheckOptions(argc, argv))
{ {

View File

@ -19,8 +19,11 @@
#include "FL/Fl.H" #include "FL/Fl.H"
#include "FL/fl_ask.H" #include "FL/fl_ask.H"
#include "cmSystemTools.h"
int main(int argc, char * argv[] ) int main(int argc, char * argv[] )
{ {
cmSystemTools::FindExecutableDirectory(argv[0]);
fl_message_font(FL_HELVETICA,11); fl_message_font(FL_HELVETICA,11);

View File

@ -7,6 +7,7 @@
#include "CMakeCommandLineInfo.h" #include "CMakeCommandLineInfo.h"
#include "../cmDocumentation.h" #include "../cmDocumentation.h"
#include "../cmake.h" #include "../cmake.h"
#include "../cmSystemTools.h"
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -101,6 +102,7 @@ BOOL CMakeSetup::InitInstance()
#endif #endif
CMakeCommandLineInfo cmdInfo; CMakeCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo); ParseCommandLine(cmdInfo);
cmSystemTools::FindExecutableDirectory(cmdInfo.GetArgV()[0]);
// Check for documentation options. If there are no arguments skip // Check for documentation options. If there are no arguments skip
// the check because the GUI should be displayed instead of showing // the check because the GUI should be displayed instead of showing
@ -111,7 +113,7 @@ BOOL CMakeSetup::InitInstance()
{ {
// Construct and print requested documentation. // Construct and print requested documentation.
cmake hcm; cmake hcm;
hcm.AddCMakePaths(cmdInfo.GetArgV()[0]); hcm.AddCMakePaths();
doc.SetCMakeRoot(hcm.GetCacheDefinition("CMAKE_ROOT")); doc.SetCMakeRoot(hcm.GetCacheDefinition("CMAKE_ROOT"));
std::vector<cmDocumentationEntry> commands; std::vector<cmDocumentationEntry> commands;
std::vector<cmDocumentationEntry> compatCommands; std::vector<cmDocumentationEntry> compatCommands;

View File

@ -21,6 +21,7 @@
#include "CMakeSetupDialog.h" #include "CMakeSetupDialog.h"
#include "cmDocumentation.h" #include "cmDocumentation.h"
#include "cmSystemTools.h"
#include "cmake.h" #include "cmake.h"
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -61,6 +62,7 @@ static const char * cmDocumentationOptions[][3] =
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
cmSystemTools::FindExecutableDirectory(argv[0]);
QApplication app(argc, argv); QApplication app(argc, argv);
app.setApplicationName("CMakeSetup"); app.setApplicationName("CMakeSetup");
app.setOrganizationName("Kitware"); app.setOrganizationName("Kitware");
@ -72,7 +74,7 @@ int main(int argc, char** argv)
{ {
// Construct and print requested documentation. // Construct and print requested documentation.
cmake hcm; cmake hcm;
hcm.AddCMakePaths(app.argv()[0]); hcm.AddCMakePaths();
doc.SetCMakeRoot(hcm.GetCacheDefinition("CMAKE_ROOT")); doc.SetCMakeRoot(hcm.GetCacheDefinition("CMAKE_ROOT"));
std::vector<cmDocumentationEntry> commands; std::vector<cmDocumentationEntry> commands;
std::vector<cmDocumentationEntry> compatCommands; std::vector<cmDocumentationEntry> compatCommands;

View File

@ -1774,7 +1774,7 @@ void cmCTest::HandleScriptArguments(size_t &i,
// the main entry point of ctest, called from main // the main entry point of ctest, called from main
int cmCTest::Run(std::vector<std::string> &args, std::string* output) int cmCTest::Run(std::vector<std::string> &args, std::string* output)
{ {
this->FindRunningCMake(args[0].c_str()); this->FindRunningCMake();
const char* ctestExec = "ctest"; const char* ctestExec = "ctest";
bool cmakeAndTest = false; bool cmakeAndTest = false;
bool performSomeTest = true; bool performSomeTest = true;
@ -1970,70 +1970,25 @@ int cmCTest::Run(std::vector<std::string> &args, std::string* output)
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void cmCTest::FindRunningCMake(const char* arg0) void cmCTest::FindRunningCMake()
{ {
// Find our own executable. // Find our own executable.
std::vector<cmStdString> failures; this->CTestSelf = cmSystemTools::GetExecutableDirectory();
this->CTestSelf = arg0; this->CTestSelf += "/ctest";
cmSystemTools::ConvertToUnixSlashes(this->CTestSelf); this->CTestSelf += cmSystemTools::GetExecutableExtension();
failures.push_back(this->CTestSelf);
this->CTestSelf = cmSystemTools::FindProgram(this->CTestSelf.c_str());
if(!cmSystemTools::FileExists(this->CTestSelf.c_str())) if(!cmSystemTools::FileExists(this->CTestSelf.c_str()))
{ {
failures.push_back(this->CTestSelf); cmSystemTools::Error("CTest executable cannot be found at ",
this->CTestSelf = "/usr/local/bin/ctest"; this->CTestSelf.c_str());
} }
if(!cmSystemTools::FileExists(this->CTestSelf.c_str()))
{ this->CMakeSelf = cmSystemTools::GetExecutableDirectory();
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<cmStdString>::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 += "/cmake"; this->CMakeSelf += "/cmake";
this->CMakeSelf += cmSystemTools::GetExecutableExtension(); this->CMakeSelf += cmSystemTools::GetExecutableExtension();
#endif
if(!cmSystemTools::FileExists(this->CMakeSelf.c_str())) if(!cmSystemTools::FileExists(this->CMakeSelf.c_str()))
{ {
failures.push_back(this->CMakeSelf); cmSystemTools::Error("CMake executable cannot be found at ",
cmOStringStream msg; this->CMakeSelf.c_str());
msg << "CTEST can not find the command line program cmake.\n";
msg << " argv[0] = \"" << arg0 << "\"\n";
msg << " Attempted paths:\n";
std::vector<cmStdString>::iterator i;
for(i=failures.begin(); i != failures.end(); ++i)
{
msg << " \"" << i->c_str() << "\"\n";
}
cmSystemTools::Error(msg.str().c_str());
} }
} }

View File

@ -398,7 +398,7 @@ private:
const VectorOfStrings& files); const VectorOfStrings& files);
///! Find the running cmake ///! Find the running cmake
void FindRunningCMake(const char* arg0); void FindRunningCMake();
//! Check if the argument is the one specified //! Check if the argument is the one specified
bool CheckArgument(const std::string& arg, const char* varg1, bool CheckArgument(const std::string& arg, const char* varg1,

View File

@ -121,6 +121,7 @@ int DumpForCoverage(const char* outname)
int main(int ac, char** av) int main(int ac, char** av)
{ {
cmSystemTools::EnableMSVCDebugHook(); cmSystemTools::EnableMSVCDebugHook();
cmSystemTools::FindExecutableDirectory(av[0]);
const char* outname = "cmake.html"; const char* outname = "cmake.html";
bool coverage = false; bool coverage = false;
if(ac > 1) if(ac > 1)

View File

@ -2064,3 +2064,26 @@ bool cmSystemTools::CopyFileTime(const char* fromFile, const char* toFile)
#endif #endif
return true; 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();
}

View File

@ -350,6 +350,14 @@ public:
the first argument to that named by the second. */ the first argument to that named by the second. */
static bool CopyFileTime(const char* fromFile, const char* toFile); 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: private:
static bool s_ForceUnixPaths; static bool s_ForceUnixPaths;
static bool s_RunCommandHideConsole; static bool s_RunCommandHideConsole;

View File

@ -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 // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the
// cache // cache
int cmake::AddCMakePaths(const char *arg0) int cmake::AddCMakePaths()
{ {
// Find the cmake executable // Find the cmake executable
std::vector<cmStdString> failures; std::string cMakeSelf = cmSystemTools::GetExecutableDirectory();
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;
cMakeSelf += "/cmake"; cMakeSelf += "/cmake";
cMakeSelf += cmSystemTools::GetExecutableExtension(); cMakeSelf += cmSystemTools::GetExecutableExtension();
#endif
}
#ifdef CMAKE_PREFIX
if(!cmSystemTools::FileExists(cMakeSelf.c_str())) if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
{ {
failures.push_back(cMakeSelf); cmSystemTools::Error("CMake executable cannot be found at ",
cMakeSelf = CMAKE_PREFIX "/bin/cmake"; cMakeSelf.c_str());
}
#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<cmStdString>::iterator i;
for(i=failures.begin(); i != failures.end(); ++i)
{
msg << " \"" << i->c_str() << "\"\n";
}
cmSystemTools::Error(msg.str().c_str());
return 0; return 0;
} }
// Save the value in the cache // Save the value in the cache
@ -2157,7 +2117,7 @@ int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
} }
else else
{ {
this->AddCMakePaths(this->CMakeCommand.c_str()); this->AddCMakePaths();
} }
// Add any cache args // Add any cache args
@ -2376,7 +2336,7 @@ int cmake::LoadCache()
} }
// setup CMAKE_ROOT and CMAKE_COMMAND // setup CMAKE_ROOT and CMAKE_COMMAND
if(!this->AddCMakePaths(this->CMakeCommand.c_str())) if(!this->AddCMakePaths())
{ {
return -3; return -3;
} }
@ -3503,7 +3463,7 @@ int cmake::GetSystemInformation(std::vector<std::string>& args)
// we have to find the module directory, so we can copy the files // we have to find the module directory, so we can copy the files
this->AddCMakePaths(args[0].c_str()); this->AddCMakePaths();
std::string modulesPath = std::string modulesPath =
this->CacheManager->GetCacheValue("CMAKE_ROOT"); this->CacheManager->GetCacheValue("CMAKE_ROOT");
modulesPath += "/Modules"; modulesPath += "/Modules";

View File

@ -288,7 +288,7 @@ class cmake
/** /**
* Generate CMAKE_ROOT and CMAKE_COMMAND cache entries * Generate CMAKE_ROOT and CMAKE_COMMAND cache entries
*/ */
int AddCMakePaths(const char *arg0); int AddCMakePaths();
/** /**
* Get the file comparison class * Get the file comparison class

View File

@ -273,6 +273,7 @@ static void cmakemainProgressCallback(const char *m, float prog,
int main(int ac, char** av) int main(int ac, char** av)
{ {
cmSystemTools::EnableMSVCDebugHook(); cmSystemTools::EnableMSVCDebugHook();
cmSystemTools::FindExecutableDirectory(av[0]);
int ret = do_cmake(ac, av); int ret = do_cmake(ac, av);
#ifdef CMAKE_BUILD_WITH_CMAKE #ifdef CMAKE_BUILD_WITH_CMAKE
cmDynamicLoader::FlushCache(); cmDynamicLoader::FlushCache();
@ -299,7 +300,7 @@ int do_cmake(int ac, char** av)
{ {
// Construct and print requested documentation. // Construct and print requested documentation.
cmake hcm; cmake hcm;
hcm.AddCMakePaths(av[0]); hcm.AddCMakePaths();
doc.SetCMakeRoot(hcm.GetCacheDefinition("CMAKE_ROOT")); doc.SetCMakeRoot(hcm.GetCacheDefinition("CMAKE_ROOT"));
// the command line args are processed here so that you can do // the command line args are processed here so that you can do

View File

@ -210,6 +210,7 @@ int main (int argc, char *argv[])
{ {
cmSystemTools::DoNotInheritStdPipes(); cmSystemTools::DoNotInheritStdPipes();
cmSystemTools::EnableMSVCDebugHook(); cmSystemTools::EnableMSVCDebugHook();
cmSystemTools::FindExecutableDirectory(argv[0]);
int nocwd = 0; int nocwd = 0;
cmCTest inst; cmCTest inst;