[frameworks/extra-cmake-modules] /: ECMSetupVersion: support version args in <major>.<minor> and <major> forms

Friedrich W. H. Kossebau <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 55814526c938d54948c67f4a7ff05c430bcd43a9 by Friedrich W. H. Kossebau.
Committed on 14/08/2026 at 15:19.
Pushed by kossebau into branch 'master'.

ECMSetupVersion: support version args in <major>.<minor> and <major> forms

M  +20   -3    modules/ECMSetupVersion.cmake
M  +1    -0    tests/ECMSetupVersionTest/CMakeLists.txt
A  +69   -0    tests/ECMSetupVersionTest/new_explicit_version_file_no_patchlevel/CMakeLists.txt
A  +4    -0    tests/ECMSetupVersionTest/new_explicit_version_file_no_patchlevel/main.c  *

The files marked with a * at the end have a non valid license. Please read: https://community.kde.org/Policies/Licensing_Policy and use the headers which are listed at that page.


https://invent.kde.org/frameworks/extra-cmake-modules/-/commit/55814526c938d54948c67f4a7ff05c430bcd43a9

diff --git a/modules/ECMSetupVersion.cmake b/modules/ECMSetupVersion.cmake
index 8b04ec5e..8e3118e7 100644
--- a/modules/ECMSetupVersion.cmake
+++ b/modules/ECMSetupVersion.cmake
@@ -36,6 +36,10 @@ version of ECM is < 5.83)::
 
   <prefix>_VERSION_STRING - <version> (use <prefix>_VERSION instead)
 
+Since ECM 6.30 additionally the forms ``<major>.<minor>`` and ``<major>``
+are supported, the variables for the respective missing components are set to
+empty strings.
+
 With CMake versions older than 4.0.0 and if CMake policy CMP0048 is not ``NEW``,
 the following CMake variables will also be set::
 
@@ -150,9 +154,22 @@ function(ecm_setup_version _version)
         string(REGEX REPLACE "0*([0-9]+)" "\\1" _minor "${PROJECT_VERSION_MINOR}")
         string(REGEX REPLACE "0*([0-9]+)" "\\1" _patch "${PROJECT_VERSION_PATCH}")
     else()
-        string(REGEX REPLACE "^0*([0-9]+)\\.[0-9]+\\.[0-9]+.*" "\\1" _major "${_version}")
-        string(REGEX REPLACE "^[0-9]+\\.0*([0-9]+)\\.[0-9]+.*" "\\1" _minor "${_version}")
-        string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.0*([0-9]+).*" "\\1" _patch "${_version}")
+        # not applying final "$", to be able to ignore .<tweak>, possibly also other appendices in usage
+        string(REGEX MATCH "^0*([0-9]+)(\\.0*([0-9]+)(\\.0*([0-9]+))?)?" _match "${_version}")
+        if(_match STREQUAL "")
+            message(FATAL_ERROR "ecm_setup_version given version argument not matching <major>[.<minor>[.<patch>][.<tweak>]]: ${_version}")
+        endif()
+        set(_major ${CMAKE_MATCH_1})
+        if (CMAKE_MATCH_COUNT GREATER_EQUAL 3)
+            set(_minor ${CMAKE_MATCH_3})
+        else()
+            set(_minor "")
+        endif()
+        if (CMAKE_MATCH_COUNT GREATER_EQUAL 5)
+            set(_patch ${CMAKE_MATCH_5})
+        else()
+            set(_patch "")
+        endif()
     endif()
 
     if(NOT DEFINED ESV_SOVERSION) # use DEFINED, so "0" as valid SO version is not evaluated to FALSE
diff --git a/tests/ECMSetupVersionTest/CMakeLists.txt b/tests/ECMSetupVersionTest/CMakeLists.txt
index 20a4078b..2ddb0414 100644
--- a/tests/ECMSetupVersionTest/CMakeLists.txt
+++ b/tests/ECMSetupVersionTest/CMakeLists.txt
@@ -35,6 +35,7 @@ add_version_test(new_explicit_simple_no_version_string_vars dummy)
 add_version_test(new_explicit_soversion dummy)
 add_version_test(new_explicit_version_file dummy)
 add_version_test(new_explicit_version_file_abspath dummy)
+add_version_test(new_explicit_version_file_no_patchlevel dummy)
 add_version_test(new_project_header check_header)
 add_version_test(new_project_header_abspath check_header)
 add_version_test(new_project_header_prefix check_header)
diff --git a/tests/ECMSetupVersionTest/new_explicit_version_file_no_patchlevel/CMakeLists.txt b/tests/ECMSetupVersionTest/new_explicit_version_file_no_patchlevel/CMakeLists.txt
new file mode 100644
index 00000000..13a7b9a1
--- /dev/null
+++ b/tests/ECMSetupVersionTest/new_explicit_version_file_no_patchlevel/CMakeLists.txt
@@ -0,0 +1,69 @@
+cmake_minimum_required(VERSION 3.16)
+
+project(new_explicit_version_file_no_patchlevel VERSION 1.5.6.7)
+
+set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../modules)
+include(ECMSetupVersion)
+
+ecm_setup_version(2.3
+    VARIABLE_PREFIX Foo
+    PACKAGE_VERSION_FILE FooVersion.cmake
+)
+
+include(../version_helpers.cmake)
+project_version_var_checks(1.5.6.7)
+version_var_checks(Foo 2.3)
+assert_var_num_value(Foo_SOVERSION 2)
+
+macro(AnyNewer_2_3_checks filename)
+    # too old - fails
+    test_version_file("${filename}" "3.1")
+    assert_var_str_value(PACKAGE_VERSION "2.3")
+    assert_var_bool_value(PACKAGE_VERSION_COMPATIBLE FALSE ALLOW_UNDEFINED)
+    assert_var_bool_value(PACKAGE_VERSION_EXACT FALSE ALLOW_UNDEFINED)
+
+    # too old - fails
+    test_version_file("${filename}" "3")
+    assert_var_str_value(PACKAGE_VERSION "2.3")
+    assert_var_bool_value(PACKAGE_VERSION_COMPATIBLE FALSE ALLOW_UNDEFINED)
+    assert_var_bool_value(PACKAGE_VERSION_EXACT FALSE ALLOW_UNDEFINED)
+
+    # too old - fails
+    test_version_file("${filename}" "2.3.4")
+    assert_var_str_value(PACKAGE_VERSION "2.3")
+    assert_var_bool_value(PACKAGE_VERSION_COMPATIBLE FALSE ALLOW_UNDEFINED)
+    assert_var_bool_value(PACKAGE_VERSION_EXACT FALSE ALLOW_UNDEFINED)
+
+    # newer - succeeds
+    test_version_file("${filename}" "1.1")
+    assert_var_str_value(PACKAGE_VERSION "2.3")
+    assert_var_bool_value(PACKAGE_VERSION_COMPATIBLE TRUE)
+    assert_var_bool_value(PACKAGE_VERSION_EXACT FALSE ALLOW_UNDEFINED)
+
+    # newer - succeeds
+    test_version_file("${filename}" "2.1")
+    assert_var_str_value(PACKAGE_VERSION "2.3")
+    assert_var_bool_value(PACKAGE_VERSION_COMPATIBLE TRUE)
+    assert_var_bool_value(PACKAGE_VERSION_EXACT FALSE ALLOW_UNDEFINED)
+
+    # newer - succeeds
+    test_version_file("${filename}" "2")
+    assert_var_str_value(PACKAGE_VERSION "2.3")
+    assert_var_bool_value(PACKAGE_VERSION_COMPATIBLE TRUE)
+    assert_var_bool_value(PACKAGE_VERSION_EXACT FALSE ALLOW_UNDEFINED)
+
+    # unspecified - succeeds
+    test_version_file("${filename}" "")
+    assert_var_str_value(PACKAGE_VERSION "2.3")
+    assert_var_bool_value(PACKAGE_VERSION_COMPATIBLE TRUE)
+    assert_var_bool_value(PACKAGE_VERSION_EXACT FALSE ALLOW_UNDEFINED)
+
+    # exact - succeeds
+    test_version_file("${filename}" "2.3")
+    assert_var_str_value(PACKAGE_VERSION "2.3")
+    assert_var_bool_value(PACKAGE_VERSION_COMPATIBLE TRUE)
+    assert_var_bool_value(PACKAGE_VERSION_EXACT TRUE)
+endmacro()
+AnyNewer_2_3_checks("${CMAKE_CURRENT_BINARY_DIR}/FooVersion.cmake")
+
+add_executable(dummy main.c)
diff --git a/tests/ECMSetupVersionTest/new_explicit_version_file_no_patchlevel/main.c b/tests/ECMSetupVersionTest/new_explicit_version_file_no_patchlevel/main.c
new file mode 100644
index 00000000..c13815ce
--- /dev/null
+++ b/tests/ECMSetupVersionTest/new_explicit_version_file_no_patchlevel/main.c
@@ -0,0 +1,4 @@
+int main()
+{
+   return 0;
+}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.