1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-15 20:46:37 +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 d029b828b2
commit ab0a44a0a0
4 changed files with 12 additions and 2 deletions

View File

@@ -2730,14 +2730,14 @@ bool cmFindPackageCommand::FindConfigFile(std::string const& dir,
if (this->DebugMode) {
this->DebugBuffer = cmStrCat(this->DebugBuffer, " ", file, '\n');
}
if (cmSystemTools::FileExists(file, true) && this->CheckVersion(file)) {
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);
}
return true;
return this->CheckVersion(file);
}
}
return false;

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

@@ -28,6 +28,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(PolicyPush)
run_cmake(PolicyPop)
run_cmake(RequiredOptionValuesClash)