From adfb0623cb68e59a398816f34fa43386adbc46e9 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 18 Dec 2024 09:37:59 -0500 Subject: [PATCH] find_package: Fix assertion failure on empty sysroots Previously we crashed if at least one root variable was set to empty and at least one to non-empty. Fixes: #26538 --- Source/cmFindCommon.cxx | 8 ++++---- Tests/RunCMake/find_package/EmptyRoots.cmake | 10 ++++++++++ Tests/RunCMake/find_package/RunCMakeTest.cmake | 1 + 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 Tests/RunCMake/find_package/EmptyRoots.cmake diff --git a/Source/cmFindCommon.cxx b/Source/cmFindCommon.cxx index 01c87c2ed1..f57564e02d 100644 --- a/Source/cmFindCommon.cxx +++ b/Source/cmFindCommon.cxx @@ -248,19 +248,19 @@ void cmFindCommon::RerootPaths(std::vector& paths, // Construct the list of path roots with no trailing slashes. cmList roots; debugRoot("CMAKE_FIND_ROOT_PATH", rootPath); - if (rootPath) { + if (cmNonempty(rootPath)) { roots.assign(*rootPath); } debugRoot("CMAKE_SYSROOT_COMPILE", sysrootCompile); - if (sysrootCompile) { + if (cmNonempty(sysrootCompile)) { roots.emplace_back(*sysrootCompile); } debugRoot("CMAKE_SYSROOT_LINK", sysrootLink); - if (sysrootLink) { + if (cmNonempty(sysrootLink)) { roots.emplace_back(*sysrootLink); } debugRoot("CMAKE_SYSROOT", sysroot); - if (sysroot) { + if (cmNonempty(sysroot)) { roots.emplace_back(*sysroot); } for (auto& r : roots) { diff --git a/Tests/RunCMake/find_package/EmptyRoots.cmake b/Tests/RunCMake/find_package/EmptyRoots.cmake new file mode 100644 index 0000000000..22d4e1668d --- /dev/null +++ b/Tests/RunCMake/find_package/EmptyRoots.cmake @@ -0,0 +1,10 @@ +set(vars CMAKE_SYSROOT CMAKE_SYSROOT_COMPILE CMAKE_SYSROOT_LINK CMAKE_FIND_ROOT_PATH) +foreach(v IN LISTS vars) + set("${v}" "") +endforeach() +foreach(v IN LISTS vars) + block() + set("${v}" "/dummy") + find_package(dummy CONFIG NO_DEFAULT_PATH PATHS "/") + endblock() +endforeach() diff --git a/Tests/RunCMake/find_package/RunCMakeTest.cmake b/Tests/RunCMake/find_package/RunCMakeTest.cmake index 1995ac40b2..0552535e61 100644 --- a/Tests/RunCMake/find_package/RunCMakeTest.cmake +++ b/Tests/RunCMake/find_package/RunCMakeTest.cmake @@ -3,6 +3,7 @@ include(RunCMake) run_cmake(CMP0074-WARN) run_cmake(CMP0074-OLD) run_cmake(ComponentRequiredAndOptional) +run_cmake(EmptyRoots) run_cmake(FromPATHEnv) run_cmake_with_options(FromPATHEnvDebugPkg --debug-find-pkg=Resolved) run_cmake(FromPrefixPath)