[frameworks/extra-cmake-modules] modules: ECMSetupVersion: use final hex number in version header, not calculation

Friedrich W. H. Kossebau <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 4e4ef3ffc72b1a4b49f4d0adee58c5416502579b by Friedrich W. H. Kossebau.
Committed on 12/08/2026 at 23:27.
Pushed by kossebau into branch 'master'.

ECMSetupVersion: use final hex number in version header, not calculation

Note:
Old code would generate code even without pqtch and minor level set in the
version argument to ecm_setup_version(). But the generated macro
<prefix>_VERSION would be defined to broken code (e.g. "|()"), which
would fail only once something uses the <prefix>_VERSION macro in their
code, but not before or otherwise.

M  +16   -0    modules/ECMSetupVersion.cmake
M  +2    -1    modules/ECMVersionHeader.h.in

https://invent.kde.org/frameworks/extra-cmake-modules/-/commit/4e4ef3ffc72b1a4b49f4d0adee58c5416502579b

diff --git a/modules/ECMSetupVersion.cmake b/modules/ECMSetupVersion.cmake
index a40c4484..8b04ec5e 100644
--- a/modules/ECMSetupVersion.cmake
+++ b/modules/ECMSetupVersion.cmake
@@ -192,6 +192,22 @@ function(ecm_setup_version _version)
         set(HEADER_VERSION_MAJOR "${_major}")
         set(HEADER_VERSION_MINOR "${_minor}")
         set(HEADER_VERSION_PATCH "${_patch}")
+        # handle patch or even minor level not being set
+        if (_minor STREQUAL "")
+            set(_minor_number 0)
+        else()
+            set(_minor_number "${_minor}")
+        endif()
+        if (_patch STREQUAL "")
+            set(_patch_number 0)
+        else()
+            set(_patch_number "${_patch}")
+        endif()
+        math(
+            EXPR HEADER_VERSION_HEXNUMBER "${_major}*65536 + ${_minor_number}*256 + ${_patch_number}"
+            OUTPUT_FORMAT HEXADECIMAL
+        )
+
         configure_file("${_ECM_SETUP_VERSION_HEADER_TEMPLATE}" "${ESV_VERSION_HEADER}")
     endif()
 
diff --git a/modules/ECMVersionHeader.h.in b/modules/ECMVersionHeader.h.in
index 9d25ecba..853c2a5a 100644
--- a/modules/ECMVersionHeader.h.in
+++ b/modules/ECMVersionHeader.h.in
@@ -7,6 +7,7 @@
 #define @HEADER_PREFIX@_VERSION_MAJOR @HEADER_VERSION_MAJOR@
 #define @HEADER_PREFIX@_VERSION_MINOR @HEADER_VERSION_MINOR@
 #define @HEADER_PREFIX@_VERSION_PATCH @HEADER_VERSION_PATCH@
-#define @HEADER_PREFIX@_VERSION ((@HEADER_VERSION_MAJOR@<<16)|(@HEADER_VERSION_MINOR@<<8)|(@HEADER_VERSION_PATCH@))
+// Packed format 0xMMmmpp, 0xMM == major, 0xmm == minor, 0xpp == patch
+#define @HEADER_PREFIX@_VERSION @HEADER_VERSION_HEXNUMBER@
 
 #endif
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.