diff options
author | Brad King <brad.king@kitware.com> | 2022-04-22 12:58:59 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2022-04-22 12:59:09 (GMT) |
commit | 1bd85e8f3f57d4c279dcd2a3ea4c24a0cf3c5f58 (patch) | |
tree | 54736201fe3a5d967355197eb836a21f23d1ea5c /Source | |
parent | 5e9c3e2f0734097aa7820a212723f8ef1ecb27a3 (diff) | |
parent | 42f7e397894c5132b4706f478e62ce5d648119c1 (diff) | |
download | CMake-1bd85e8f3f57d4c279dcd2a3ea4c24a0cf3c5f58.zip CMake-1bd85e8f3f57d4c279dcd2a3ea4c24a0cf3c5f58.tar.gz CMake-1bd85e8f3f57d4c279dcd2a3ea4c24a0cf3c5f58.tar.bz2 |
Merge topic 'NO_CMAKE_INSTALL_PREFIX'
42f7e39789 Find: Support per call disabling of CMAKE_INSTALL_PREFIX
Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Merge-request: !7163
Diffstat (limited to 'Source')
-rw-r--r-- | Source/cmFindBase.cxx | 59 | ||||
-rw-r--r-- | Source/cmFindCommon.cxx | 8 | ||||
-rw-r--r-- | Source/cmFindCommon.h | 1 | ||||
-rw-r--r-- | Source/cmFindPackageCommand.cxx | 52 |
4 files changed, 115 insertions, 5 deletions
diff --git a/Source/cmFindBase.cxx b/Source/cmFindBase.cxx index efc4e3a..a8db63d 100644 --- a/Source/cmFindBase.cxx +++ b/Source/cmFindBase.cxx @@ -267,8 +267,61 @@ void cmFindBase::FillCMakeSystemVariablePath() { cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeSystem]; + const bool install_prefix_in_list = + !this->Makefile->IsOn("CMAKE_FIND_NO_INSTALL_PREFIX"); + const bool remove_install_prefix = this->NoCMakeInstallPath; + const bool add_install_prefix = !this->NoCMakeInstallPath && + this->Makefile->IsDefinitionSet("CMAKE_FIND_USE_INSTALL_PREFIX"); + + // We have 3 possible states for `CMAKE_SYSTEM_PREFIX_PATH` and + // `CMAKE_INSTALL_PREFIX`. + // Either we need to remove `CMAKE_INSTALL_PREFIX`, add + // `CMAKE_INSTALL_PREFIX`, or do nothing. + // + // When we need to remove `CMAKE_INSTALL_PREFIX` we remove the Nth occurrence + // of `CMAKE_INSTALL_PREFIX` from `CMAKE_SYSTEM_PREFIX_PATH`, where `N` is + // computed by `CMakeSystemSpecificInformation.cmake` while constructing + // `CMAKE_SYSTEM_PREFIX_PATH`. This ensures that if projects / toolchains + // have removed `CMAKE_INSTALL_PREFIX` from the list, we don't remove + // some other entry by mistake + long install_prefix_count = -1; + std::string install_path_to_remove; + if (cmValue to_skip = this->Makefile->GetDefinition( + "_CMAKE_SYSTEM_PREFIX_PATH_INSTALL_PREFIX_COUNT")) { + cmStrToLong(to_skip, &install_prefix_count); + } + if (cmValue install_value = this->Makefile->GetDefinition( + "_CMAKE_SYSTEM_PREFIX_PATH_INSTALL_PREFIX_VALUE")) { + install_path_to_remove = *install_value; + } + + if (remove_install_prefix && install_prefix_in_list && + install_prefix_count > 0 && !install_path_to_remove.empty()) { + cmValue prefix_paths = + this->Makefile->GetDefinition("CMAKE_SYSTEM_PREFIX_PATH"); + + // remove entry from CMAKE_SYSTEM_PREFIX_PATH + std::vector<std::string> expanded = cmExpandedList(*prefix_paths); + long index_to_remove = 0; + for (const auto& path : expanded) { + if (path == install_path_to_remove && --install_prefix_count == 0) { + break; + } + ++index_to_remove; + } + expanded.erase(expanded.begin() + index_to_remove); + paths.AddPrefixPaths(expanded, + this->Makefile->GetCurrentSourceDirectory().c_str()); + } else if (add_install_prefix && !install_prefix_in_list) { + + paths.AddCMakePrefixPath("CMAKE_INSTALL_PREFIX"); + paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH"); + } else { + // Otherwise the current setup of `CMAKE_SYSTEM_PREFIX_PATH` is correct + paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH"); + } + std::string var = cmStrCat("CMAKE_SYSTEM_", this->CMakePathName, "_PATH"); - paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH"); paths.AddCMakePath(var); if (this->CMakePathName == "PROGRAM") { @@ -496,7 +549,9 @@ cmFindBaseDebugState::~cmFindBaseDebugState() " CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: ", !this->FindCommand->NoSystemEnvironmentPath, "\n", " CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: ", - !this->FindCommand->NoCMakeSystemPath, "\n"); + !this->FindCommand->NoCMakeSystemPath, "\n", + " CMAKE_FIND_USE_INSTALL_PREFIX: ", + !this->FindCommand->NoCMakeInstallPath, "\n"); } buffer += diff --git a/Source/cmFindCommon.cxx b/Source/cmFindCommon.cxx index 7106e4b..9fd712a 100644 --- a/Source/cmFindCommon.cxx +++ b/Source/cmFindCommon.cxx @@ -39,6 +39,7 @@ cmFindCommon::cmFindCommon(cmExecutionStatus& status) this->NoCMakeEnvironmentPath = false; this->NoSystemEnvironmentPath = false; this->NoCMakeSystemPath = false; + this->NoCMakeInstallPath = false; // OS X Bundle and Framework search policy. The default is to // search frameworks first on apple. @@ -179,14 +180,15 @@ void cmFindCommon::SelectDefaultMacMode() void cmFindCommon::SelectDefaultSearchModes() { - const std::array<std::pair<bool&, std::string>, 5> search_paths = { + const std::array<std::pair<bool&, std::string>, 6> search_paths = { { { this->NoPackageRootPath, "CMAKE_FIND_USE_PACKAGE_ROOT_PATH" }, { this->NoCMakePath, "CMAKE_FIND_USE_CMAKE_PATH" }, { this->NoCMakeEnvironmentPath, "CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH" }, { this->NoSystemEnvironmentPath, "CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH" }, - { this->NoCMakeSystemPath, "CMAKE_FIND_USE_CMAKE_SYSTEM_PATH" } } + { this->NoCMakeSystemPath, "CMAKE_FIND_USE_CMAKE_SYSTEM_PATH" }, + { this->NoCMakeInstallPath, "CMAKE_FIND_USE_INSTALL_PREFIX" } } }; for (auto const& path : search_paths) { @@ -348,6 +350,8 @@ bool cmFindCommon::CheckCommonArgument(std::string const& arg) this->NoSystemEnvironmentPath = true; } else if (arg == "NO_CMAKE_SYSTEM_PATH") { this->NoCMakeSystemPath = true; + } else if (arg == "NO_CMAKE_INSTALL_PREFIX") { + this->NoCMakeInstallPath = true; } else if (arg == "NO_CMAKE_FIND_ROOT_PATH") { this->FindRootPathMode = RootPathModeNever; } else if (arg == "ONLY_CMAKE_FIND_ROOT_PATH") { diff --git a/Source/cmFindCommon.h b/Source/cmFindCommon.h index 5d9b3e1..4c02df0 100644 --- a/Source/cmFindCommon.h +++ b/Source/cmFindCommon.h @@ -130,6 +130,7 @@ protected: bool NoCMakeEnvironmentPath; bool NoSystemEnvironmentPath; bool NoCMakeSystemPath; + bool NoCMakeInstallPath; std::vector<std::string> SearchPathSuffixes; diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index 18457a7..9a89935 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -1682,7 +1682,57 @@ void cmFindPackageCommand::FillPrefixesCMakeSystemVariable() { cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeSystem]; - paths.AddCMakePath("CMAKE_SYSTEM_PREFIX_PATH"); + const bool install_prefix_in_list = + !this->Makefile->IsOn("CMAKE_FIND_NO_INSTALL_PREFIX"); + const bool remove_install_prefix = this->NoCMakeInstallPath; + const bool add_install_prefix = !this->NoCMakeInstallPath && + this->Makefile->IsDefinitionSet("CMAKE_FIND_USE_INSTALL_PREFIX"); + + // We have 3 possible states for `CMAKE_SYSTEM_PREFIX_PATH` and + // `CMAKE_INSTALL_PREFIX`. + // Either we need to remove `CMAKE_INSTALL_PREFIX`, add + // `CMAKE_INSTALL_PREFIX`, or do nothing. + // + // When we need to remove `CMAKE_INSTALL_PREFIX` we remove the Nth occurrence + // of `CMAKE_INSTALL_PREFIX` from `CMAKE_SYSTEM_PREFIX_PATH`, where `N` is + // computed by `CMakeSystemSpecificInformation.cmake` while constructing + // `CMAKE_SYSTEM_PREFIX_PATH`. This ensures that if projects / toolchains + // have removed `CMAKE_INSTALL_PREFIX` from the list, we don't remove + // some other entry by mistake + long install_prefix_count = -1; + std::string install_path_to_remove; + if (cmValue to_skip = this->Makefile->GetDefinition( + "_CMAKE_SYSTEM_PREFIX_PATH_INSTALL_PREFIX_COUNT")) { + cmStrToLong(to_skip, &install_prefix_count); + } + if (cmValue install_value = this->Makefile->GetDefinition( + "_CMAKE_SYSTEM_PREFIX_PATH_INSTALL_PREFIX_VALUE")) { + install_path_to_remove = *install_value; + } + + if (remove_install_prefix && install_prefix_in_list && + install_prefix_count > 0 && !install_path_to_remove.empty()) { + + cmValue prefix_paths = + this->Makefile->GetDefinition("CMAKE_SYSTEM_PREFIX_PATH"); + // remove entry from CMAKE_SYSTEM_PREFIX_PATH + std::vector<std::string> expanded = cmExpandedList(*prefix_paths); + long count = 0; + for (const auto& path : expanded) { + bool to_add = + !(path == install_path_to_remove && ++count == install_prefix_count); + if (to_add) { + paths.AddPath(path); + } + } + } else if (add_install_prefix && !install_prefix_in_list) { + paths.AddCMakePath("CMAKE_INSTALL_PREFIX"); + paths.AddCMakePath("CMAKE_SYSTEM_PREFIX_PATH"); + } else { + // Otherwise the current setup of `CMAKE_SYSTEM_PREFIX_PATH` is correct + paths.AddCMakePath("CMAKE_SYSTEM_PREFIX_PATH"); + } + paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH"); paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH"); |