1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-14 10:47:59 +08:00

find_package: Fix regression on <PackageName>_ROOT relative to CWD

In commit 9d44a77454 (find_*: Explicitly normalize found paths as they
exist on disk, 2024-10-17, v4.0.0-rc1~597^2~1), we removed path
normalization from the internal `cmSearchPath::AddPathInternal` helper.
Most call sites were updated to normalize input paths first, but search
paths derived from `<PackageName>_ROOT` CMake variables are no longer
normalized.  Instead we normalize the path to the file found after
searching.

When `find_package` "config" mode considers a candidate CMake package
configuration file, normalize its path before loading the adjacent
package version file so that the latter is loaded by absolute path.
Otherwise `cmMakefile::ReadDependentFile` interprets a relative path
with respect to the current source directory rather than the current
working directory.

Fixes: #27279
This commit is contained in:
Robert Maynard
2025-10-01 12:21:50 -04:00
committed by Brad King
parent fc059e1978
commit 87a661a916
4 changed files with 16 additions and 7 deletions

View File

@@ -2873,14 +2873,13 @@ bool cmFindPackageCommand::FindConfigFile(std::string const& dir,
this->DebugBuffer = cmStrCat(this->DebugBuffer, " ", file, '\n');
}
if (cmSystemTools::FileExists(file, true)) {
// Allow resolving symlinks when the config file is found through a link
if (this->UseRealPath) {
file = cmSystemTools::GetRealPath(file);
} else {
file = cmSystemTools::ToNormalizedPathOnDisk(file);
}
if (this->CheckVersion(file)) {
// Allow resolving symlinks when the config file is found through a
// link
if (this->UseRealPath) {
file = cmSystemTools::GetRealPath(file);
} else {
file = cmSystemTools::ToNormalizedPathOnDisk(file);
}
foundMode = cmFindPackageCommand::FoundMode(config.Type);
return true;
}

View File

@@ -0,0 +1,2 @@
-- Relative_ROOT='root'
-- Relative_DIR='[^']*/Tests/RunCMake/find_package/PackageRootRelative-build/root'

View File

@@ -0,0 +1,7 @@
cmake_policy(SET CMP0074 NEW)
set(Relative_ROOT root)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/root/RelativeConfig.cmake" "")
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/root/RelativeConfigVersion.cmake" "set(PACKAGE_VERSION 1)")
find_package(Relative)
message(STATUS "Relative_ROOT='${Relative_ROOT}'")
message(STATUS "Relative_DIR='${Relative_DIR}'")

View File

@@ -39,6 +39,7 @@ run_cmake_with_options(ModuleModeDebugPkg --debug-find-pkg=Foo,Zot)
run_cmake(PackageRoot)
run_cmake(PackageRootNestedConfig)
run_cmake(PackageRootNestedModule)
run_cmake(PackageRootRelative)
run_cmake(PackageVarOverridesOptional)
run_cmake(PolicyPush)
run_cmake(PolicyPop)