From fc4d238d7afee29e9495c5b1826af314fb8b3afe Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 2 Oct 2025 14:05:23 -0400 Subject: [PATCH 1/4] Tests: Match RunCMake.find_package-CPS version candidates more precisely Since commit 3e6466eb16 (find_package: Honor version requests when finding CPS packages, 2025-02-12, v4.0.0-rc1~16^2) the test has relied on `.*` to absorb capitalized paths on case-insensitive filesystems. Match them explicitly. --- .../find_package-CPS/MissingVersion1-stderr.txt | 14 +++++++++----- .../find_package-CPS/MissingVersion2-stderr.txt | 14 +++++++++----- .../find_package-CPS/VersionLimit3-stderr.txt | 14 +++++++++----- .../find_package-CPS/VersionLimit4-stderr.txt | 14 +++++++++----- 4 files changed, 36 insertions(+), 20 deletions(-) diff --git a/Tests/RunCMake/find_package-CPS/MissingVersion1-stderr.txt b/Tests/RunCMake/find_package-CPS/MissingVersion1-stderr.txt index 76879a2468..b3bfd9bde2 100644 --- a/Tests/RunCMake/find_package-CPS/MissingVersion1-stderr.txt +++ b/Tests/RunCMake/find_package-CPS/MissingVersion1-stderr.txt @@ -3,11 +3,15 @@ CMake Error at MissingVersion1\.cmake:[0-9]+ \(find_package\): with requested version "1\.2\.7"\. The following configuration files were considered but not accepted: - - .*/Tests/RunCMake/find_package-CPS/cps/sample/1\.5\.0/sample\.cps, version: 1\.5\.0\+niven - .*/Tests/RunCMake/find_package-CPS/cps/sample/1\.4\.2/sample\.cps, version: 1\.4\.2\+adams - .*/Tests/RunCMake/find_package-CPS/cps/sample/1\.2\.3/sample\.cps, version: 1\.2\.3\+clarke - .*/Tests/RunCMake/find_package-CPS/cps/sample/1\.1\.0/sample\.cps, version: 1\.1\.0\+asimov +( + [^ +]*/Tests/RunCMake/find_package-CPS/cps/sample/1\.5\.0/[Ss]ample\.cps, version: 1\.5\.0\+niven)+( + [^ +]*/Tests/RunCMake/find_package-CPS/cps/sample/1\.4\.2/[Ss]ample\.cps, version: 1\.4\.2\+adams)+( + [^ +]*/Tests/RunCMake/find_package-CPS/cps/sample/1\.2\.3/[Ss]ample\.cps, version: 1\.2\.3\+clarke)+( + [^ +]*/Tests/RunCMake/find_package-CPS/cps/sample/1\.1\.0/[Ss]ample\.cps, version: 1\.1\.0\+asimov)+ Call Stack \(most recent call first\): CMakeLists\.txt:3 \(include\) diff --git a/Tests/RunCMake/find_package-CPS/MissingVersion2-stderr.txt b/Tests/RunCMake/find_package-CPS/MissingVersion2-stderr.txt index c984ce72ba..cdb3a8d406 100644 --- a/Tests/RunCMake/find_package-CPS/MissingVersion2-stderr.txt +++ b/Tests/RunCMake/find_package-CPS/MissingVersion2-stderr.txt @@ -3,11 +3,15 @@ CMake Error at MissingVersion2\.cmake:[0-9]+ \(find_package\): with requested version "1\.4\.9"\. The following configuration files were considered but not accepted: - - .*/Tests/RunCMake/find_package-CPS/cps/sample/1\.5\.0/sample\.cps, version: 1\.5\.0\+niven - .*/Tests/RunCMake/find_package-CPS/cps/sample/1\.4\.2/sample\.cps, version: 1\.4\.2\+adams - .*/Tests/RunCMake/find_package-CPS/cps/sample/1\.2\.3/sample\.cps, version: 1\.2\.3\+clarke - .*/Tests/RunCMake/find_package-CPS/cps/sample/1\.1\.0/sample\.cps, version: 1\.1\.0\+asimov +( + [^ +]*/Tests/RunCMake/find_package-CPS/cps/sample/1\.5\.0/[Ss]ample\.cps, version: 1\.5\.0\+niven)+( + [^ +]*/Tests/RunCMake/find_package-CPS/cps/sample/1\.4\.2/[Ss]ample\.cps, version: 1\.4\.2\+adams)+( + [^ +]*/Tests/RunCMake/find_package-CPS/cps/sample/1\.2\.3/[Ss]ample\.cps, version: 1\.2\.3\+clarke)+( + [^ +]*/Tests/RunCMake/find_package-CPS/cps/sample/1\.1\.0/[Ss]ample\.cps, version: 1\.1\.0\+asimov)+ Call Stack \(most recent call first\): CMakeLists\.txt:3 \(include\) diff --git a/Tests/RunCMake/find_package-CPS/VersionLimit3-stderr.txt b/Tests/RunCMake/find_package-CPS/VersionLimit3-stderr.txt index 3b1c5efa6c..e11db59f93 100644 --- a/Tests/RunCMake/find_package-CPS/VersionLimit3-stderr.txt +++ b/Tests/RunCMake/find_package-CPS/VersionLimit3-stderr.txt @@ -3,11 +3,15 @@ CMake Error at VersionLimit3\.cmake:[0-9]+ \(find_package\): with requested version range "1\.2\.0\.\.\.1\.2\.2"\. The following configuration files were considered but not accepted: - - .*/Tests/RunCMake/find_package-CPS/cps/sample/1\.5\.0/sample\.cps, version: 1\.5\.0\+niven - .*/Tests/RunCMake/find_package-CPS/cps/sample/1\.4\.2/sample\.cps, version: 1\.4\.2\+adams - .*/Tests/RunCMake/find_package-CPS/cps/sample/1\.2\.3/sample\.cps, version: 1\.2\.3\+clarke - .*/Tests/RunCMake/find_package-CPS/cps/sample/1\.1\.0/sample\.cps, version: 1\.1\.0\+asimov +( + [^ +]*/Tests/RunCMake/find_package-CPS/cps/sample/1\.5\.0/[Ss]ample\.cps, version: 1\.5\.0\+niven)+( + [^ +]*/Tests/RunCMake/find_package-CPS/cps/sample/1\.4\.2/[Ss]ample\.cps, version: 1\.4\.2\+adams)+( + [^ +]*/Tests/RunCMake/find_package-CPS/cps/sample/1\.2\.3/[Ss]ample\.cps, version: 1\.2\.3\+clarke)+( + [^ +]*/Tests/RunCMake/find_package-CPS/cps/sample/1\.1\.0/[Ss]ample\.cps, version: 1\.1\.0\+asimov)+ Call Stack \(most recent call first\): CMakeLists\.txt:3 \(include\) diff --git a/Tests/RunCMake/find_package-CPS/VersionLimit4-stderr.txt b/Tests/RunCMake/find_package-CPS/VersionLimit4-stderr.txt index 67cef1528d..3ae8004310 100644 --- a/Tests/RunCMake/find_package-CPS/VersionLimit4-stderr.txt +++ b/Tests/RunCMake/find_package-CPS/VersionLimit4-stderr.txt @@ -3,11 +3,15 @@ CMake Error at VersionLimit4\.cmake:[0-9]+ \(find_package\): with requested version range "1\.2\.0\.\.\.<1\.2\.3"\. The following configuration files were considered but not accepted: - - .*/Tests/RunCMake/find_package-CPS/cps/sample/1\.5\.0/sample\.cps, version: 1\.5\.0\+niven - .*/Tests/RunCMake/find_package-CPS/cps/sample/1\.4\.2/sample\.cps, version: 1\.4\.2\+adams - .*/Tests/RunCMake/find_package-CPS/cps/sample/1\.2\.3/sample\.cps, version: 1\.2\.3\+clarke - .*/Tests/RunCMake/find_package-CPS/cps/sample/1\.1\.0/sample\.cps, version: 1\.1\.0\+asimov +( + [^ +]*/Tests/RunCMake/find_package-CPS/cps/sample/1\.5\.0/[Ss]ample\.cps, version: 1\.5\.0\+niven)+( + [^ +]*/Tests/RunCMake/find_package-CPS/cps/sample/1\.4\.2/[Ss]ample\.cps, version: 1\.4\.2\+adams)+( + [^ +]*/Tests/RunCMake/find_package-CPS/cps/sample/1\.2\.3/[Ss]ample\.cps, version: 1\.2\.3\+clarke)+( + [^ +]*/Tests/RunCMake/find_package-CPS/cps/sample/1\.1\.0/[Ss]ample\.cps, version: 1\.1\.0\+asimov)+ Call Stack \(most recent call first\): CMakeLists\.txt:3 \(include\) From d029b828b2fd36a5a2e17ca3426a474e287dcc21 Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 2 Oct 2025 15:17:13 -0400 Subject: [PATCH 2/4] find_package: Avoid repeating search for lower-case .cps file --- Source/cmFindPackageCommand.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index bae7d38990..1879f2dd28 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -1257,7 +1257,7 @@ bool cmFindPackageCommand::FindPackageUsingConfigMode() this->Configs.emplace_back(std::move(config), pdt::Cps); config = cmStrCat(cmSystemTools::LowerCase(n), ".cps"); - if (config != this->Configs.front().Name) { + if (config != this->Configs.back().Name) { this->Configs.emplace_back(std::move(config), pdt::Cps); } } From ab0a44a0a0b54622f247f226c8f3347fcfd884b7 Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Wed, 1 Oct 2025 12:21:50 -0400 Subject: [PATCH 3/4] find_package: Fix regression on `_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 `_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 --- Source/cmFindPackageCommand.cxx | 4 ++-- Tests/RunCMake/find_package/PackageRootRelative-stdout.txt | 2 ++ Tests/RunCMake/find_package/PackageRootRelative.cmake | 7 +++++++ Tests/RunCMake/find_package/RunCMakeTest.cmake | 1 + 4 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 Tests/RunCMake/find_package/PackageRootRelative-stdout.txt create mode 100644 Tests/RunCMake/find_package/PackageRootRelative.cmake diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index 1879f2dd28..0081c6c7dc 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -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; diff --git a/Tests/RunCMake/find_package/PackageRootRelative-stdout.txt b/Tests/RunCMake/find_package/PackageRootRelative-stdout.txt new file mode 100644 index 0000000000..e99d37d447 --- /dev/null +++ b/Tests/RunCMake/find_package/PackageRootRelative-stdout.txt @@ -0,0 +1,2 @@ +-- Relative_ROOT='root' +-- Relative_DIR='[^']*/Tests/RunCMake/find_package/PackageRootRelative-build/root' diff --git a/Tests/RunCMake/find_package/PackageRootRelative.cmake b/Tests/RunCMake/find_package/PackageRootRelative.cmake new file mode 100644 index 0000000000..3d63753477 --- /dev/null +++ b/Tests/RunCMake/find_package/PackageRootRelative.cmake @@ -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}'") diff --git a/Tests/RunCMake/find_package/RunCMakeTest.cmake b/Tests/RunCMake/find_package/RunCMakeTest.cmake index 64f6b37323..78356be364 100644 --- a/Tests/RunCMake/find_package/RunCMakeTest.cmake +++ b/Tests/RunCMake/find_package/RunCMakeTest.cmake @@ -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) From 87a661a916f05d0334c8f9ef4e8719a6ca157c3e Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Wed, 1 Oct 2025 12:21:50 -0400 Subject: [PATCH 4/4] find_package: Fix regression on `_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 `_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 --- Source/cmFindPackageCommand.cxx | 13 ++++++------- .../find_package/PackageRootRelative-stdout.txt | 2 ++ .../RunCMake/find_package/PackageRootRelative.cmake | 7 +++++++ Tests/RunCMake/find_package/RunCMakeTest.cmake | 1 + 4 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 Tests/RunCMake/find_package/PackageRootRelative-stdout.txt create mode 100644 Tests/RunCMake/find_package/PackageRootRelative.cmake diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index f75361fbab..23083b0146 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -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; } diff --git a/Tests/RunCMake/find_package/PackageRootRelative-stdout.txt b/Tests/RunCMake/find_package/PackageRootRelative-stdout.txt new file mode 100644 index 0000000000..e99d37d447 --- /dev/null +++ b/Tests/RunCMake/find_package/PackageRootRelative-stdout.txt @@ -0,0 +1,2 @@ +-- Relative_ROOT='root' +-- Relative_DIR='[^']*/Tests/RunCMake/find_package/PackageRootRelative-build/root' diff --git a/Tests/RunCMake/find_package/PackageRootRelative.cmake b/Tests/RunCMake/find_package/PackageRootRelative.cmake new file mode 100644 index 0000000000..3d63753477 --- /dev/null +++ b/Tests/RunCMake/find_package/PackageRootRelative.cmake @@ -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}'") diff --git a/Tests/RunCMake/find_package/RunCMakeTest.cmake b/Tests/RunCMake/find_package/RunCMakeTest.cmake index 10f3453514..969d6d7bc1 100644 --- a/Tests/RunCMake/find_package/RunCMakeTest.cmake +++ b/Tests/RunCMake/find_package/RunCMakeTest.cmake @@ -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)