1# SPDX-License-Identifier: Apache-2.0
2
3#.rst:
4# version.cmake
5# -------------
6#
7# Inputs:
8#
9#   ``*VERSION*`` and other constants set by
10#   maintainers in ``${ZEPHYR_BASE}/VERSION``
11#
12# Outputs with examples::
13#
14#   PROJECT_VERSION           1.14.99.07
15#   KERNEL_VERSION_STRING    "1.14.99-extraver"
16#
17#   KERNEL_VERSION_MAJOR      1
18#   KERNEL_VERSION_MINOR        14
19#   KERNEL_PATCHLEVEL              99
20#   KERNEL_VERSION_TWEAK              07
21#   KERNELVERSION            0x10E6307
22#   KERNEL_VERSION_NUMBER    0x10E63
23#   ZEPHYR_VERSION_CODE        69219
24#
25# Most outputs are converted to C macros, see ``version.h.in``
26#
27# See also: independent and more dynamic ``BUILD_VERSION`` in
28# ``git.cmake``.
29
30# Note: version.cmake is loaded multiple times by ZephyrConfigVersion.cmake to
31# determine this Zephyr package version and thus the correct Zephyr CMake
32# package to load.
33# Therefore `version.cmake` should not use include_guard(GLOBAL).
34# The final load of `version.cmake` will setup correct build version values.
35
36include(${ZEPHYR_BASE}/cmake/hex.cmake)
37
38if(NOT DEFINED VERSION_FILE AND NOT DEFINED VERSION_TYPE)
39  set(VERSION_FILE ${ZEPHYR_BASE}/VERSION ${APPLICATION_SOURCE_DIR}/VERSION)
40  set(VERSION_TYPE KERNEL                 APP)
41endif()
42
43foreach(type file IN ZIP_LISTS VERSION_TYPE VERSION_FILE)
44  if(NOT EXISTS ${file})
45    break()
46  endif()
47  file(READ ${file} ver)
48  set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${file})
49
50  string(REGEX MATCH "VERSION_MAJOR = ([0-9]*)" _ ${ver})
51  set(${type}_VERSION_MAJOR ${CMAKE_MATCH_1})
52
53  string(REGEX MATCH "VERSION_MINOR = ([0-9]*)" _ ${ver})
54  set(${type}_VERSION_MINOR ${CMAKE_MATCH_1})
55
56  string(REGEX MATCH "PATCHLEVEL = ([0-9]*)" _ ${ver})
57  set(${type}_PATCHLEVEL ${CMAKE_MATCH_1})
58
59  string(REGEX MATCH "VERSION_TWEAK = ([0-9]*)" _ ${ver})
60  set(${type}_VERSION_TWEAK ${CMAKE_MATCH_1})
61
62  string(REGEX MATCH "EXTRAVERSION = ([a-z0-9]*)" _ ${ver})
63  set(${type}_VERSION_EXTRA ${CMAKE_MATCH_1})
64
65  # Temporary convenience variable
66  set(${type}_VERSION_WITHOUT_TWEAK ${${type}_VERSION_MAJOR}.${${type}_VERSION_MINOR}.${${type}_PATCHLEVEL})
67
68
69  set(MAJOR ${${type}_VERSION_MAJOR}) # Temporary convenience variable
70  set(MINOR ${${type}_VERSION_MINOR}) # Temporary convenience variable
71  set(PATCH ${${type}_PATCHLEVEL})    # Temporary convenience variable
72  set(TWEAK ${${type}_VERSION_TWEAK}) # Temporary convenience variable
73
74  math(EXPR ${type}_VERSION_NUMBER_INT "(${MAJOR} << 16) + (${MINOR} << 8)  + (${PATCH})")
75  math(EXPR ${type}VERSION_INT         "(${MAJOR} << 24) + (${MINOR} << 16) + (${PATCH} << 8) + (${TWEAK})")
76
77  to_hex(${${type}_VERSION_NUMBER_INT} ${type}_VERSION_NUMBER)
78  to_hex(${${type}VERSION_INT}         ${type}VERSION)
79
80  if(${type}_VERSION_EXTRA)
81    set(${type}_VERSION_STRING     "${${type}_VERSION_WITHOUT_TWEAK}-${${type}_VERSION_EXTRA}")
82  else()
83    set(${type}_VERSION_STRING     "${${type}_VERSION_WITHOUT_TWEAK}")
84  endif()
85
86  if(type STREQUAL KERNEL)
87    set(PROJECT_VERSION_MAJOR      ${${type}_VERSION_MAJOR})
88    set(PROJECT_VERSION_MINOR      ${${type}_VERSION_MINOR})
89    set(PROJECT_VERSION_PATCH      ${${type}_PATCHLEVEL})
90    set(PROJECT_VERSION_TWEAK      ${${type}_VERSION_TWEAK})
91    set(PROJECT_VERSION_EXTRA      ${${type}_VERSION_EXTRA})
92
93    if(PROJECT_VERSION_EXTRA)
94      set(PROJECT_VERSION_EXTRA_STR "-${PROJECT_VERSION_EXTRA}")
95    endif()
96
97    if(${type}_VERSION_TWEAK)
98      set(PROJECT_VERSION ${${type}_VERSION_WITHOUT_TWEAK}.${${type}_VERSION_TWEAK})
99    else()
100      set(PROJECT_VERSION ${${type}_VERSION_WITHOUT_TWEAK})
101    endif()
102
103    set(PROJECT_VERSION_STR ${PROJECT_VERSION}${PROJECT_VERSION_EXTRA_STR})
104
105    set(ZEPHYR_VERSION_CODE ${${type}_VERSION_NUMBER_INT})
106    set(ZEPHYR_VERSION TRUE)
107
108    if(DEFINED BUILD_VERSION)
109      set(BUILD_VERSION_STR ", build: ${BUILD_VERSION}")
110    endif()
111
112    if (NOT NO_PRINT_VERSION)
113        message(STATUS "Zephyr version: ${PROJECT_VERSION_STR} (${ZEPHYR_BASE})${BUILD_VERSION_STR}")
114    endif()
115  endif()
116
117  # Cleanup convenience variables
118  unset(MAJOR)
119  unset(MINOR)
120  unset(PATCH)
121  unset(${type}_VERSION_WITHOUT_TWEAK)
122endforeach()
123