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