commit:     cc2d8f761caf42ffe401a250b6b81b6ff4263fa5
Author:     Andreas Sturmlechner <asturm <AT> gentoo <DOT> org>
AuthorDate: Fri Jun 13 14:42:37 2025 +0000
Commit:     Andreas Sturmlechner <asturm <AT> gentoo <DOT> org>
CommitDate: Sun Jun 22 10:57:09 2025 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=cc2d8f76

cmake.eclass: mv _CMAKE_MINREQVER_UNSUPPORTED detection to src_prepare

- _cmake_minverreq-lt(): New function for reusability of regex
- Optimise #1: Reuse existing loop over CMakeLists.txt files
- Optimise #2: If _CMAKE_MINREQVER_UNSUPPORTED is already set, then no
  further run of _cmake_minverreq-lt is necessary since we currently throw
  away the info on affected files
- Relegate "CMake 4 detected; ..." to be a non-QA Notice, it is just an
  addendum

Signed-off-by: Andreas Sturmlechner <asturm <AT> gentoo.org>

 eclass/cmake.eclass | 70 ++++++++++++++++++++++++++++++++---------------------
 1 file changed, 42 insertions(+), 28 deletions(-)

diff --git a/eclass/cmake.eclass b/eclass/cmake.eclass
index 4104882005a3..ffd11bc0c863 100644
--- a/eclass/cmake.eclass
+++ b/eclass/cmake.eclass
@@ -126,7 +126,8 @@ fi
 # @ECLASS_VARIABLE: _CMAKE_MINREQVER_UNSUPPORTED
 # @DEFAULT_UNSET
 # @DESCRIPTION:
-# Is set to true if an unsupported cmake_minimum_required value was detected.
+# Internal status set by _cmake_minreqver-lt(); is true if an unsupported
+# cmake_minimum_required value was detected.
 
 # @ECLASS_VARIABLE: CMAKE_QA_SRC_DIR_READONLY
 # @USER_VARIABLE
@@ -326,6 +327,25 @@ _cmake_check_build_dir() {
        mkdir -p "${BUILD_DIR}" || die
 }
 
+# @FUNCTION: _cmake_minreqver-lt
+# @USAGE: <lt-version> <path>
+# @INTERNAL
+# @DESCRIPTION:
+# Internal function for detecting occurrence of lower-than-specified
+# <lt-version> in cmake_minimum_required of a given CMake file <path>.
+# Returns 0 if the regex matched (a lower-than-specified version found).
+_cmake_minreqver-lt() {
+       local ver chk=1
+       ver=$(sed -ne 
"/cmake_minimum_required/I{s/.*\(\.\.\.*\|\s\)\([0-9.]*\)\([)]\|\s\).*$/\2/p;q}"
 \
+               "${2}" 2>/dev/null \
+       )
+       if [[ -n ${ver} ]] && ver_test "${ver}" -lt "${1}"; then
+               _CMAKE_MINREQVER_UNSUPPORTED=true
+               chk=0
+       fi
+       return ${chk}
+}
+
 # @FUNCTION: _cmake_modify-cmakelists
 # @INTERNAL
 # @DESCRIPTION:
@@ -337,9 +357,9 @@ _cmake_modify-cmakelists() {
        # Only edit the files once
        grep -qs "<<< Gentoo configuration >>>" 
"${CMAKE_USE_DIR}"/CMakeLists.txt && return 0
 
-       # Comment out all set (<some_should_be_user_defined_variable> value)
        local file
        while read -d '' -r file ; do
+               # Comment out all set (<some_should_be_user_defined_variable> 
value)
                sed \
                        -e 
'/^[[:space:]]*set[[:space:]]*([[:space:]]*CMAKE_BUILD_TYPE\([[:space:]].*)\|)\)/I{s/^/#_cmake_modify_IGNORE
 /g}' \
                        -e 
'/^[[:space:]]*set[[:space:]]*([[:space:]]*CMAKE_\(COLOR_MAKEFILE\|INSTALL_PREFIX\|VERBOSE_MAKEFILE\)[[:space:]].*)/I{s/^/#_cmake_modify_IGNORE
 /g}' \
@@ -352,6 +372,10 @@ _cmake_modify-cmakelists() {
                                einfo "${mod_line:22:99}"
                        done
                fi
+               # Detect unsupported minimum CMake versions unless 
CMAKE_QA_COMPAT_SKIP is set
+               if [[ -z ${_CMAKE_MINREQVER_UNSUPPORTED} ]] && ! [[ 
${CMAKE_QA_COMPAT_SKIP} ]]; then
+                       _cmake_minreqver-lt "3.5" "${file}"
+               fi
        done < <(find "${CMAKE_USE_DIR}" -type f -iname "CMakeLists.txt" 
-print0 || die)
 
        # NOTE Append some useful summary here
@@ -412,6 +436,20 @@ cmake_src_prepare() {
        # Remove dangerous things.
        _cmake_modify-cmakelists
 
+       if [[ ${_CMAKE_MINREQVER_UNSUPPORTED} ]]; then
+               eqawarn "QA Notice: Compatibility with CMake < 3.5 has been 
removed from CMake 4,"
+               eqawarn "${CATEGORY}/${PN} will fail to build w/o a fix."
+               eqawarn "See also tracker bug #951350; check existing bug or 
file a new one for"
+               eqawarn "this package, and take it upstream."
+               if has_version -b ">=dev-build/cmake-4"; then
+                       eqawarn "CMake 4 detected; building with 
-DCMAKE_POLICY_VERSION_MINIMUM=3.5"
+                       eqawarn "This is merely a workaround and *not* a 
permanent fix."
+               fi
+               if [[ ${EAPI} == 7 ]]; then
+                       eqawarn "QA Notice: EAPI=7 detected; this package is 
now a prime last-rites target."
+               fi
+       fi
+
        if [[ ${EAPI} == 7 ]]; then
                popd > /dev/null || die
        fi
@@ -460,19 +498,6 @@ cmake_src_configure() {
        # Fix xdg collision with sandbox
        xdg_environment_reset
 
-       local file ver
-       if ! [[ ${CMAKE_QA_COMPAT_SKIP} ]]; then
-               while read -d '' -r file ; do
-                       ver=$(sed -ne 
"/cmake_minimum_required/I{s/.*\(\.\.\.*\|\s\)\([0-9.]*\)\([)]\|\s\).*$/\2/p;q}"
 \
-                               "${file}" 2>/dev/null \
-                       )
-
-                       if [[ -n $ver ]] && ver_test $ver -lt "3.5"; then
-                               _CMAKE_MINREQVER_UNSUPPORTED=true
-                       fi
-               done < <(find "${CMAKE_USE_DIR}" -type f -iname 
"CMakeLists.txt" -print0)
-       fi
-
        # Prepare Gentoo override rules (set valid compiler, append CPPFLAGS 
etc.)
        local build_rules=${BUILD_DIR}/gentoo_rules.cmake
 
@@ -653,19 +678,8 @@ cmake_src_configure() {
                cmakeargs+=( -C "${CMAKE_EXTRA_CACHE_FILE}" )
        fi
 
-       if [[ ${_CMAKE_MINREQVER_UNSUPPORTED} ]]; then
-               eqawarn "QA Notice: Compatibility with CMake < 3.5 has been 
removed from CMake 4,"
-               eqawarn "${CATEGORY}/${PN} will fail to build w/o a fix."
-               eqawarn "See also tracker bug #951350; check existing bug or 
file a new one for"
-               eqawarn "this package, and take it upstream."
-               if [[ ${EAPI} == 7 ]]; then
-                       eqawarn "QA Notice: EAPI=7 detected; this package is 
now a prime last-rites target."
-               fi
-               if has_version -b ">=dev-build/cmake-4"; then
-                       eqawarn "QA Notice: CMake 4 detected; building with 
-DCMAKE_POLICY_VERSION_MINIMUM=3.5"
-                       eqawarn "This is merely a workaround and *not* a 
permanent fix."
-                       cmakeargs+=( -DCMAKE_POLICY_VERSION_MINIMUM=3.5 )
-               fi
+       if [[ ${_CMAKE_MINREQVER_UNSUPPORTED} ]] && has_version -b 
">=dev-build/cmake-4"; then
+               cmakeargs+=( -DCMAKE_POLICY_VERSION_MINIMUM=3.5 )
        fi
 
        pushd "${BUILD_DIR}" > /dev/null || die

Reply via email to