diff options
Diffstat (limited to 'Source/cmFindPackageCommand.cxx')
-rw-r--r-- | Source/cmFindPackageCommand.cxx | 322 |
1 files changed, 193 insertions, 129 deletions
diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index 55a61f5..7746980 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -26,6 +26,14 @@ #endif //---------------------------------------------------------------------------- +cmFindPackageCommand::PathLabel + cmFindPackageCommand::PathLabel::UserRegistry("PACKAGE_REGISTRY"); +cmFindPackageCommand::PathLabel + cmFindPackageCommand::PathLabel::Builds("BUILDS"); +cmFindPackageCommand::PathLabel + cmFindPackageCommand::PathLabel::SystemRegistry("SYSTEM_PACKAGE_REGISTRY"); + +//---------------------------------------------------------------------------- cmFindPackageCommand::cmFindPackageCommand() { this->CMakePathName = "PACKAGE"; @@ -51,6 +59,33 @@ cmFindPackageCommand::cmFindPackageCommand() this->VersionFoundTweak = 0; this->VersionFoundCount = 0; this->RequiredCMakeVersion = 0; + + this->AppendSearchPathGroups(); +} + +//---------------------------------------------------------------------------- +void cmFindPackageCommand::AppendSearchPathGroups() +{ + std::vector<cmFindCommon::PathLabel>* labels; + + // Update the All group with new paths + labels = &this->PathGroupLabelMap[PathGroup::All]; + labels->insert(std::find(labels->begin(), labels->end(), + PathLabel::CMakeSystem), + PathLabel::UserRegistry); + labels->insert(std::find(labels->begin(), labels->end(), + PathLabel::CMakeSystem), + PathLabel::Builds); + labels->insert(std::find(labels->begin(), labels->end(), PathLabel::Guess), + PathLabel::SystemRegistry); + + // Create the new path objects + this->LabeledPaths.insert(std::make_pair(PathLabel::UserRegistry, + cmSearchPath(this))); + this->LabeledPaths.insert(std::make_pair(PathLabel::Builds, + cmSearchPath(this))); + this->LabeledPaths.insert(std::make_pair(PathLabel::SystemRegistry, + cmSearchPath(this))); } //---------------------------------------------------------------------------- @@ -248,11 +283,11 @@ bool cmFindPackageCommand } else if(doing == DoingPaths) { - this->AddUserPath(args[i], this->UserPaths); + this->UserGuessArgs.push_back(args[i]); } else if(doing == DoingHints) { - this->AddUserPath(args[i], this->UserHints); + this->UserHintsArgs.push_back(args[i]); } else if(doing == DoingPathSuffixes) { @@ -263,7 +298,7 @@ bool cmFindPackageCommand if(args[i].find_first_of(":/\\") != args[i].npos || cmSystemTools::GetFilenameLastExtension(args[i]) != ".cmake") { - cmOStringStream e; + std::ostringstream e; e << "given CONFIGS option followed by invalid file name \"" << args[i] << "\". The names given must be file names without " << "a path and with a \".cmake\" extension."; @@ -279,7 +314,7 @@ bool cmFindPackageCommand } else { - cmOStringStream e; + std::ostringstream e; e << "called with invalid argument \"" << args[i] << "\""; this->SetError(e.str()); return false; @@ -292,7 +327,7 @@ bool cmFindPackageCommand std::back_inserter(doubledComponents)); if(!doubledComponents.empty()) { - cmOStringStream e; + std::ostringstream e; e << "called with components that are both required and optional:\n"; for(unsigned int i=0; i<doubledComponents.size(); ++i) { @@ -307,7 +342,7 @@ bool cmFindPackageCommand this->UseConfigFiles = moduleArgs.empty(); if(!this->UseFindModules && !this->UseConfigFiles) { - cmOStringStream e; + std::ostringstream e; e << "given options exclusive to Module mode:\n"; for(std::set<unsigned int>::const_iterator si = moduleArgs.begin(); si != moduleArgs.end(); ++si) @@ -389,7 +424,7 @@ bool cmFindPackageCommand { if (this->Required) { - cmOStringStream e; + std::ostringstream e; e << "for module " << this->Name << " called with REQUIRED, but " << disableFindPackageVar << " is enabled. A REQUIRED package cannot be disabled."; @@ -422,7 +457,7 @@ bool cmFindPackageCommand if(this->UseFindModules && this->UseConfigFiles && this->Makefile->IsOn("CMAKE_FIND_PACKAGE_WARN_NO_MODULE")) { - cmOStringStream aw; + std::ostringstream aw; if(this->RequiredCMakeVersion >= CMake_VERSION_ENCODE(2,8,8)) { aw << "find_package called without either MODULE or CONFIG option and " @@ -587,7 +622,7 @@ bool cmFindPackageCommand::FindModule(bool& found) module += this->Name; module += ".cmake"; std::string mfile = this->Makefile->GetModulesFile(module.c_str()); - if ( mfile.size() ) + if (!mfile.empty()) { // Load the module we found, and set "<name>_FIND_MODULE" to true // while inside it. @@ -713,8 +748,8 @@ bool cmFindPackageCommand::HandlePackageMode() if (result && !found && (!this->Quiet || this->Required)) { // The variable is not set. - cmOStringStream e; - cmOStringStream aw; + std::ostringstream e; + std::ostringstream aw; if (configFileSetFOUNDFalse) { e << "Found package configuration file:\n" @@ -728,7 +763,7 @@ bool cmFindPackageCommand::HandlePackageMode() } // If there are files in ConsideredConfigs, it means that FooConfig.cmake // have been found, but they didn't have appropriate versions. - else if (this->ConsideredConfigs.size() > 0) + else if (!this->ConsideredConfigs.empty()) { e << "Could not find a configuration file for package \"" << this->Name << "\" that " @@ -1111,86 +1146,97 @@ void cmFindPackageCommand::AppendSuccessInformation() //---------------------------------------------------------------------------- void cmFindPackageCommand::ComputePrefixes() { - this->AddPrefixesCMakeVariable(); - this->AddPrefixesCMakeEnvironment(); - this->AddPrefixesUserHints(); - this->AddPrefixesSystemEnvironment(); - this->AddPrefixesUserRegistry(); - this->AddPrefixesBuilds(); - this->AddPrefixesCMakeSystemVariable(); - this->AddPrefixesSystemRegistry(); - this->AddPrefixesUserGuess(); + if(!this->NoDefaultPath) + { + if(!this->NoCMakePath) + { + this->FillPrefixesCMakeVariable(); + } + if(!this->NoCMakeEnvironmentPath) + { + this->FillPrefixesCMakeEnvironment(); + } + if(!this->NoSystemEnvironmentPath) + { + this->FillPrefixesSystemEnvironment(); + } + if(!this->NoUserRegistry) + { + this->FillPrefixesUserRegistry(); + } + if(!this->NoBuilds) + { + this->FillPrefixesBuilds(); + } + if(!this->NoCMakeSystemPath) + { + this->FillPrefixesCMakeSystemVariable(); + } + if(!this->NoSystemRegistry) + { + this->FillPrefixesSystemRegistry(); + } + } + this->FillPrefixesUserHints(); + this->FillPrefixesUserGuess(); + this->ComputeFinalPaths(); } //---------------------------------------------------------------------------- -void cmFindPackageCommand::AddPrefixesCMakeEnvironment() +void cmFindPackageCommand::FillPrefixesCMakeEnvironment() { - if(!this->NoCMakeEnvironmentPath && !this->NoDefaultPath) - { - // Check the environment variable with the same name as the cache - // entry. - std::string env; - if(cmSystemTools::GetEnv(this->Variable.c_str(), env) && env.length() > 0) - { - cmSystemTools::ConvertToUnixSlashes(env); - this->AddPathInternal(env, EnvPath); - } + cmSearchPath &paths = this->LabeledPaths[PathLabel::CMakeEnvironment]; - this->AddEnvPath("CMAKE_PREFIX_PATH"); - this->AddEnvPath("CMAKE_FRAMEWORK_PATH"); - this->AddEnvPath("CMAKE_APPBUNDLE_PATH"); - } + // Check the environment variable with the same name as the cache + // entry. + paths.AddEnvPath(this->Variable); + + // And now the general CMake environment variables + paths.AddEnvPath("CMAKE_PREFIX_PATH"); + paths.AddEnvPath("CMAKE_FRAMEWORK_PATH"); + paths.AddEnvPath("CMAKE_APPBUNDLE_PATH"); } //---------------------------------------------------------------------------- -void cmFindPackageCommand::AddPrefixesCMakeVariable() +void cmFindPackageCommand::FillPrefixesCMakeVariable() { - if(!this->NoCMakePath && !this->NoDefaultPath) - { - this->AddCMakePath("CMAKE_PREFIX_PATH"); - this->AddCMakePath("CMAKE_FRAMEWORK_PATH"); - this->AddCMakePath("CMAKE_APPBUNDLE_PATH"); - } + cmSearchPath &paths = this->LabeledPaths[PathLabel::CMake]; + + paths.AddCMakePath("CMAKE_PREFIX_PATH"); + paths.AddCMakePath("CMAKE_FRAMEWORK_PATH"); + paths.AddCMakePath("CMAKE_APPBUNDLE_PATH"); } //---------------------------------------------------------------------------- -void cmFindPackageCommand::AddPrefixesSystemEnvironment() +void cmFindPackageCommand::FillPrefixesSystemEnvironment() { - if(!this->NoSystemEnvironmentPath && !this->NoDefaultPath) + cmSearchPath &paths = this->LabeledPaths[PathLabel::SystemEnvironment]; + + // Use the system search path to generate prefixes. + // Relative paths are interpreted with respect to the current + // working directory. + std::vector<std::string> tmp; + cmSystemTools::GetPath(tmp); + for(std::vector<std::string>::iterator i = tmp.begin(); + i != tmp.end(); ++i) { - // Use the system search path to generate prefixes. - // Relative paths are interpreted with respect to the current - // working directory. - std::vector<std::string> tmp; - cmSystemTools::GetPath(tmp); - for(std::vector<std::string>::iterator i = tmp.begin(); - i != tmp.end(); ++i) + // If the path is a PREFIX/bin case then add its parent instead. + if((cmHasLiteralSuffix(*i, "/bin")) || + (cmHasLiteralSuffix(*i, "/sbin"))) { - std::string const& d = *i; - - // If the path is a PREFIX/bin case then add its parent instead. - if((cmHasLiteralSuffix(d, "/bin")) || - (cmHasLiteralSuffix(d, "/sbin"))) - { - this->AddPathInternal(cmSystemTools::GetFilenamePath(d), EnvPath); - } - else - { - this->AddPathInternal(d, EnvPath); - } + paths.AddPath(cmSystemTools::GetFilenamePath(*i)); + } + else + { + paths.AddPath(*i); } } } //---------------------------------------------------------------------------- -void cmFindPackageCommand::AddPrefixesUserRegistry() +void cmFindPackageCommand::FillPrefixesUserRegistry() { - if(this->NoUserRegistry || this->NoDefaultPath) - { - return; - } - #if defined(_WIN32) && !defined(__CYGWIN__) this->LoadPackageRegistryWinUser(); #elif defined(__HAIKU__) @@ -1201,7 +1247,8 @@ void cmFindPackageCommand::AddPrefixesUserRegistry() std::string fname = dir; fname += "/cmake/packages/"; fname += Name; - this->LoadPackageRegistryDir(fname); + this->LoadPackageRegistryDir(fname, + this->LabeledPaths[PathLabel::UserRegistry]); } #else if(const char* home = cmSystemTools::GetEnv("HOME")) @@ -1209,13 +1256,14 @@ void cmFindPackageCommand::AddPrefixesUserRegistry() std::string dir = home; dir += "/.cmake/packages/"; dir += this->Name; - this->LoadPackageRegistryDir(dir); + this->LoadPackageRegistryDir(dir, + this->LabeledPaths[PathLabel::UserRegistry]); } #endif } //---------------------------------------------------------------------------- -void cmFindPackageCommand::AddPrefixesSystemRegistry() +void cmFindPackageCommand::FillPrefixesSystemRegistry() { if(this->NoSystemRegistry || this->NoDefaultPath) { @@ -1241,29 +1289,32 @@ void cmFindPackageCommand::AddPrefixesSystemRegistry() void cmFindPackageCommand::LoadPackageRegistryWinUser() { // HKEY_CURRENT_USER\\Software shares 32-bit and 64-bit views. - this->LoadPackageRegistryWin(true, 0); + this->LoadPackageRegistryWin(true, 0, + this->LabeledPaths[PathLabel::UserRegistry]); } //---------------------------------------------------------------------------- void cmFindPackageCommand::LoadPackageRegistryWinSystem() { + cmSearchPath &paths = this->LabeledPaths[PathLabel::SystemRegistry]; + // HKEY_LOCAL_MACHINE\\SOFTWARE has separate 32-bit and 64-bit views. // Prefer the target platform view first. if(this->Makefile->PlatformIs64Bit()) { - this->LoadPackageRegistryWin(false, KEY_WOW64_64KEY); - this->LoadPackageRegistryWin(false, KEY_WOW64_32KEY); + this->LoadPackageRegistryWin(false, KEY_WOW64_64KEY, paths); + this->LoadPackageRegistryWin(false, KEY_WOW64_32KEY, paths); } else { - this->LoadPackageRegistryWin(false, KEY_WOW64_32KEY); - this->LoadPackageRegistryWin(false, KEY_WOW64_64KEY); + this->LoadPackageRegistryWin(false, KEY_WOW64_32KEY, paths); + this->LoadPackageRegistryWin(false, KEY_WOW64_64KEY, paths); } } //---------------------------------------------------------------------------- -void cmFindPackageCommand::LoadPackageRegistryWin(bool user, - unsigned int view) +void cmFindPackageCommand::LoadPackageRegistryWin(bool user, unsigned int view, + cmSearchPath& outPaths) { std::wstring key = L"Software\\Kitware\\CMake\\Packages\\"; key += cmsys::Encoding::ToWide(this->Name); @@ -1289,8 +1340,8 @@ void cmFindPackageCommand::LoadPackageRegistryWin(bool user, if(valueType == REG_SZ) { data[dataSize] = 0; - cmsys_ios::stringstream ss(cmsys::Encoding::ToNarrow(&data[0])); - if(!this->CheckPackageRegistryEntry(ss)) + if(!this->CheckPackageRegistryEntry( + cmsys::Encoding::ToNarrow(&data[0]), outPaths)) { // The entry is invalid. bad.insert(name); @@ -1332,10 +1383,11 @@ public: }; //---------------------------------------------------------------------------- -void cmFindPackageCommand::LoadPackageRegistryDir(std::string const& dir) +void cmFindPackageCommand::LoadPackageRegistryDir(std::string const& dir, + cmSearchPath& outPaths) { cmsys::Directory files; - if(!files.Load(dir.c_str())) + if(!files.Load(dir)) { return; } @@ -1347,14 +1399,16 @@ void cmFindPackageCommand::LoadPackageRegistryDir(std::string const& dir) fname += "/"; fname += files.GetFile(i); - if(!cmSystemTools::FileIsDirectory(fname.c_str())) + if(!cmSystemTools::FileIsDirectory(fname)) { // Hold this file hostage until it behaves. cmFindPackageCommandHoldFile holdFile(fname.c_str()); // Load the file. cmsys::ifstream fin(fname.c_str(), std::ios::in | cmsys_ios_binary); - if(fin && this->CheckPackageRegistryEntry(fin)) + std::string fentry; + if(fin && cmSystemTools::GetLineFromStream(fin, fentry) && + this->CheckPackageRegistryEntry(fentry, outPaths)) { // The file references an existing package, so release it. holdFile.Release(); @@ -1367,23 +1421,25 @@ void cmFindPackageCommand::LoadPackageRegistryDir(std::string const& dir) #endif //---------------------------------------------------------------------------- -bool cmFindPackageCommand::CheckPackageRegistryEntry(std::istream& is) +bool cmFindPackageCommand::CheckPackageRegistryEntry(const std::string& fname, + cmSearchPath& outPaths) { // Parse the content of one package registry entry. - std::string fname; - if(cmSystemTools::GetLineFromStream(is, fname) && - cmSystemTools::FileIsFullPath(fname.c_str())) + if(cmSystemTools::FileIsFullPath(fname.c_str())) { // The first line in the stream is the full path to a file or // directory containing the package. if(cmSystemTools::FileExists(fname.c_str())) { // The path exists. Look for the package here. - if(!cmSystemTools::FileIsDirectory(fname.c_str())) + if(!cmSystemTools::FileIsDirectory(fname)) + { + outPaths.AddPath(cmSystemTools::GetFilenamePath(fname)); + } + else { - fname = cmSystemTools::GetFilenamePath(fname); + outPaths.AddPath(fname); } - this->AddPathInternal(fname, FullPath); return true; } else @@ -1404,52 +1460,60 @@ bool cmFindPackageCommand::CheckPackageRegistryEntry(std::istream& is) } //---------------------------------------------------------------------------- -void cmFindPackageCommand::AddPrefixesBuilds() +void cmFindPackageCommand::FillPrefixesBuilds() { - if(!this->NoBuilds && !this->NoDefaultPath) - { - // It is likely that CMake will have recently built the project. - for(int i=0; i <= 10; ++i) - { - cmOStringStream r; - r << - "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\" - "Settings\\StartPath;WhereBuild" << i << "]"; - std::string f = r.str(); - cmSystemTools::ExpandRegistryValues(f); - cmSystemTools::ConvertToUnixSlashes(f); - if(cmSystemTools::FileIsFullPath(f.c_str()) && - cmSystemTools::FileIsDirectory(f.c_str())) - { - this->AddPathInternal(f, FullPath); - } + cmSearchPath &paths = this->LabeledPaths[PathLabel::Builds]; + + // It is likely that CMake will have recently built the project. + for(int i=0; i <= 10; ++i) + { + std::ostringstream r; + r << + "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\" + "Settings\\StartPath;WhereBuild" << i << "]"; + std::string f = r.str(); + cmSystemTools::ExpandRegistryValues(f); + cmSystemTools::ConvertToUnixSlashes(f); + if(cmSystemTools::FileIsFullPath(f.c_str()) && + cmSystemTools::FileIsDirectory(f)) + { + paths.AddPath(f); } } } //---------------------------------------------------------------------------- -void cmFindPackageCommand::AddPrefixesCMakeSystemVariable() +void cmFindPackageCommand::FillPrefixesCMakeSystemVariable() { - if(!this->NoCMakeSystemPath && !this->NoDefaultPath) - { - this->AddCMakePath("CMAKE_SYSTEM_PREFIX_PATH"); - this->AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH"); - this->AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH"); - } + cmSearchPath &paths = this->LabeledPaths[PathLabel::CMakeSystem]; + + paths.AddCMakePath("CMAKE_SYSTEM_PREFIX_PATH"); + paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH"); + paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH"); } //---------------------------------------------------------------------------- -void cmFindPackageCommand::AddPrefixesUserGuess() +void cmFindPackageCommand::FillPrefixesUserGuess() { - // Add guesses specified by the caller. - this->AddPathsInternal(this->UserPaths, CMakePath); + cmSearchPath &paths = this->LabeledPaths[PathLabel::Guess]; + + for(std::vector<std::string>::const_iterator p = this->UserGuessArgs.begin(); + p != this->UserGuessArgs.end(); ++p) + { + paths.AddUserPath(*p); + } } //---------------------------------------------------------------------------- -void cmFindPackageCommand::AddPrefixesUserHints() +void cmFindPackageCommand::FillPrefixesUserHints() { - // Add hints specified by the caller. - this->AddPathsInternal(this->UserHints, CMakePath); + cmSearchPath &paths = this->LabeledPaths[PathLabel::Hints]; + + for(std::vector<std::string>::const_iterator p = this->UserHintsArgs.begin(); + p != this->UserHintsArgs.end(); ++p) + { + paths.AddUserPath(*p); + } } //---------------------------------------------------------------------------- @@ -1847,7 +1911,7 @@ private: // Construct a list of matches. std::vector<std::string> matches; cmsys::Directory d; - d.Load(parent.c_str()); + d.Load(parent); for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i) { const char* fname = d.GetFile(i); @@ -1901,7 +1965,7 @@ private: // Construct a list of matches. std::vector<std::string> matches; cmsys::Directory d; - d.Load(parent.c_str()); + d.Load(parent); for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i) { const char* fname = d.GetFile(i); @@ -1955,7 +2019,7 @@ private: // Look for matching files. std::vector<std::string> matches; cmsys::Directory d; - d.Load(parent.c_str()); + d.Load(parent); for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i) { const char* fname = d.GetFile(i); @@ -2007,7 +2071,7 @@ private: for(std::vector<std::string>::const_iterator fi = files.begin(); fi != files.end(); ++fi) { - if(cmSystemTools::FileIsDirectory(fi->c_str())) + if(cmSystemTools::FileIsDirectory(*fi)) { if(this->Consider(*fi, lister)) { @@ -2035,7 +2099,7 @@ bool cmFindPackageCommand::SearchPrefix(std::string const& prefix_in) } // Skip this if the prefix does not exist. - if(!cmSystemTools::FileIsDirectory(prefix_in.c_str())) + if(!cmSystemTools::FileIsDirectory(prefix_in)) { return false; } |