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