cmake: generalize VERSION infrastructure for better reuse

Generalize the VERSION and version.h generation so that the same
infrastructure can be reused for generating other version related
header files, such as an application version header.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
This commit is contained in:
Torsten Rasmussen 2023-03-23 08:43:01 +01:00 committed by Carles Cufí
parent 37c6136385
commit 2c757f9e7a
4 changed files with 103 additions and 73 deletions

View file

@ -544,6 +544,8 @@ add_custom_command(
OUTPUT ${PROJECT_BINARY_DIR}/include/generated/version.h
COMMAND ${CMAKE_COMMAND} -DZEPHYR_BASE=${ZEPHYR_BASE}
-DOUT_FILE=${PROJECT_BINARY_DIR}/include/generated/version.h
-DVERSION_TYPE=KERNEL
-DVERSION_FILE=${ZEPHYR_BASE}/VERSION
${build_version_argument}
-P ${ZEPHYR_BASE}/cmake/gen_version_h.cmake
DEPENDS ${ZEPHYR_BASE}/VERSION ${git_dependency}

View file

@ -2,13 +2,20 @@
cmake_minimum_required(VERSION 3.20.0)
if(NOT DEFINED BUILD_VERSION)
if(VERSION_TYPE STREQUAL KERNEL)
set(BUILD_VERSION_NAME BUILD_VERSION)
else()
set(BUILD_VERSION_NAME ${VERSION_TYPE}_BUILD_VERSION)
endif()
if(NOT DEFINED ${BUILD_VERSION_NAME})
cmake_path(GET VERSION_FILE PARENT_PATH work_dir)
find_package(Git QUIET)
if(GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --abbrev=12 --always
WORKING_DIRECTORY ${ZEPHYR_BASE}
OUTPUT_VARIABLE BUILD_VERSION
WORKING_DIRECTORY ${work_dir}
OUTPUT_VARIABLE ${BUILD_VERSION_NAME}
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_STRIP_TRAILING_WHITESPACE
ERROR_VARIABLE stderr
@ -23,4 +30,7 @@ if(NOT DEFINED BUILD_VERSION)
endif()
include(${ZEPHYR_BASE}/cmake/modules/version.cmake)
configure_file(${ZEPHYR_BASE}/version.h.in ${OUT_FILE})
file(READ ${ZEPHYR_BASE}/version.h.in version_content)
string(CONFIGURE "${version_content}" version_content)
string(CONFIGURE "${version_content}" version_content)
file(WRITE ${OUT_FILE} "${version_content}")

View file

@ -33,40 +33,77 @@
# The final load of `version.cmake` will setup correct build version values.
include(${ZEPHYR_BASE}/cmake/hex.cmake)
file(READ ${ZEPHYR_BASE}/VERSION ver)
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${ZEPHYR_BASE}/VERSION)
if(NOT DEFINED VERSION_FILE AND NOT DEFINED VERSION_TYPE)
set(VERSION_FILE ${ZEPHYR_BASE}/VERSION ${APPLICATION_SOURCE_DIR}/VERSION)
set(VERSION_TYPE KERNEL APP)
endif()
foreach(type file IN ZIP_LISTS VERSION_TYPE VERSION_FILE)
if(NOT EXISTS ${file})
break()
endif()
file(READ ${file} ver)
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${file})
string(REGEX MATCH "VERSION_MAJOR = ([0-9]*)" _ ${ver})
set(PROJECT_VERSION_MAJOR ${CMAKE_MATCH_1})
set(${type}_VERSION_MAJOR ${CMAKE_MATCH_1})
string(REGEX MATCH "VERSION_MINOR = ([0-9]*)" _ ${ver})
set(PROJECT_VERSION_MINOR ${CMAKE_MATCH_1})
set(${type}_VERSION_MINOR ${CMAKE_MATCH_1})
string(REGEX MATCH "PATCHLEVEL = ([0-9]*)" _ ${ver})
set(PROJECT_VERSION_PATCH ${CMAKE_MATCH_1})
set(${type}_PATCHLEVEL ${CMAKE_MATCH_1})
string(REGEX MATCH "VERSION_TWEAK = ([0-9]*)" _ ${ver})
set(PROJECT_VERSION_TWEAK ${CMAKE_MATCH_1})
set(${type}_VERSION_TWEAK ${CMAKE_MATCH_1})
string(REGEX MATCH "EXTRAVERSION = ([a-z0-9]*)" _ ${ver})
set(PROJECT_VERSION_EXTRA ${CMAKE_MATCH_1})
set(${type}_VERSION_EXTRA ${CMAKE_MATCH_1})
# Temporary convenience variable
set(PROJECT_VERSION_WITHOUT_TWEAK ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})
set(${type}_VERSION_WITHOUT_TWEAK ${${type}_VERSION_MAJOR}.${${type}_VERSION_MINOR}.${${type}_PATCHLEVEL})
set(MAJOR ${${type}_VERSION_MAJOR}) # Temporary convenience variable
set(MINOR ${${type}_VERSION_MINOR}) # Temporary convenience variable
set(PATCH ${${type}_PATCHLEVEL}) # Temporary convenience variable
set(TWEAK ${${type}_VERSION_TWEAK}) # Temporary convenience variable
math(EXPR ${type}_VERSION_NUMBER_INT "(${MAJOR} << 16) + (${MINOR} << 8) + (${PATCH})")
math(EXPR ${type}VERSION_INT "(${MAJOR} << 24) + (${MINOR} << 16) + (${PATCH} << 8) + (${TWEAK})")
to_hex(${${type}_VERSION_NUMBER_INT} ${type}_VERSION_NUMBER)
to_hex(${${type}VERSION_INT} ${type}VERSION)
if(${type}_VERSION_EXTRA)
set(${type}_VERSION_STRING "${${type}_VERSION_WITHOUT_TWEAK}-${${type}_VERSION_EXTRA}")
else()
set(${type}_VERSION_STRING "${${type}_VERSION_WITHOUT_TWEAK}")
endif()
if(type STREQUAL KERNEL)
set(PROJECT_VERSION_MAJOR ${${type}_VERSION_MAJOR})
set(PROJECT_VERSION_MINOR ${${type}_VERSION_MINOR})
set(PROJECT_VERSION_PATCH ${${type}_PATCHLEVEL})
set(PROJECT_VERSION_TWEAK ${${type}_VERSION_TWEAK})
set(PROJECT_VERSION_EXTRA ${${type}_VERSION_EXTRA})
if(PROJECT_VERSION_EXTRA)
set(PROJECT_VERSION_EXTRA_STR "-${PROJECT_VERSION_EXTRA}")
endif()
if(PROJECT_VERSION_TWEAK)
set(PROJECT_VERSION ${PROJECT_VERSION_WITHOUT_TWEAK}.${PROJECT_VERSION_TWEAK})
if(${type}_VERSION_TWEAK)
set(PROJECT_VERSION ${${type}_VERSION_WITHOUT_TWEAK}.${${type}_VERSION_TWEAK})
else()
set(PROJECT_VERSION ${PROJECT_VERSION_WITHOUT_TWEAK})
set(PROJECT_VERSION ${${type}_VERSION_WITHOUT_TWEAK})
endif()
set(PROJECT_VERSION_STR ${PROJECT_VERSION}${PROJECT_VERSION_EXTRA_STR})
set(ZEPHYR_VERSION_CODE ${${type}_VERSION_NUMBER_INT})
set(ZEPHYR_VERSION TRUE)
if(DEFINED BUILD_VERSION)
set(BUILD_VERSION_STR ", build: ${BUILD_VERSION}")
endif()
@ -74,31 +111,11 @@ endif()
if (NOT NO_PRINT_VERSION)
message(STATUS "Zephyr version: ${PROJECT_VERSION_STR} (${ZEPHYR_BASE})${BUILD_VERSION_STR}")
endif()
set(MAJOR ${PROJECT_VERSION_MAJOR}) # Temporary convenience variable
set(MINOR ${PROJECT_VERSION_MINOR}) # Temporary convenience variable
set(PATCH ${PROJECT_VERSION_PATCH}) # Temporary convenience variable
math(EXPR KERNEL_VERSION_NUMBER_INT "(${MAJOR} << 16) + (${MINOR} << 8) + (${PATCH})")
math(EXPR KERNELVERSION_INT "(${MAJOR} << 24) + (${MINOR} << 16) + (${PATCH} << 8) + (${PROJECT_VERSION_TWEAK})")
to_hex(${KERNEL_VERSION_NUMBER_INT} KERNEL_VERSION_NUMBER)
to_hex(${KERNELVERSION_INT} KERNELVERSION)
set(KERNEL_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
set(KERNEL_VERSION_MINOR ${PROJECT_VERSION_MINOR})
set(KERNEL_PATCHLEVEL ${PROJECT_VERSION_PATCH})
if(PROJECT_VERSION_EXTRA)
set(KERNEL_VERSION_STRING "\"${PROJECT_VERSION_WITHOUT_TWEAK}-${PROJECT_VERSION_EXTRA}\"")
else()
set(KERNEL_VERSION_STRING "\"${PROJECT_VERSION_WITHOUT_TWEAK}\"")
endif()
set(ZEPHYR_VERSION_CODE ${KERNEL_VERSION_NUMBER_INT})
# Cleanup convenience variables
unset(MAJOR)
unset(MINOR)
unset(PATCH)
unset(PROJECT_VERSION_WITHOUT_TWEAK)
unset(${type}_VERSION_WITHOUT_TWEAK)
endforeach()

View file

@ -1,20 +1,21 @@
#ifndef _KERNEL_VERSION_H_
#define _KERNEL_VERSION_H_
#ifndef _@VERSION_TYPE@_VERSION_H_
#define _@VERSION_TYPE@_VERSION_H_
/* KERNEL and ZEPHYR_VERSION @templates@ values come from cmake/version.cmake
* BUILD_VERSION @template@ will be 'git describe', alternatively user defined BUILD_VERSION.
/* @templates@ values come from cmake/version.cmake
* BUILD_VERSION related @template@ values will be 'git describe',
* alternatively user defined BUILD_VERSION.
*/
#cmakedefine ZEPHYR_VERSION_CODE @ZEPHYR_VERSION_CODE@
#define ZEPHYR_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
#cmakedefine ZEPHYR_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
#define KERNELVERSION @KERNELVERSION@
#define KERNEL_VERSION_NUMBER @KERNEL_VERSION_NUMBER@
#define KERNEL_VERSION_MAJOR @KERNEL_VERSION_MAJOR@
#define KERNEL_VERSION_MINOR @KERNEL_VERSION_MINOR@
#define KERNEL_PATCHLEVEL @KERNEL_PATCHLEVEL@
#define KERNEL_VERSION_STRING @KERNEL_VERSION_STRING@
#define @VERSION_TYPE@VERSION @@VERSION_TYPE@VERSION@
#define @VERSION_TYPE@_VERSION_NUMBER @@VERSION_TYPE@_VERSION_NUMBER@
#define @VERSION_TYPE@_VERSION_MAJOR @@VERSION_TYPE@_VERSION_MAJOR@
#define @VERSION_TYPE@_VERSION_MINOR @@VERSION_TYPE@_VERSION_MINOR@
#define @VERSION_TYPE@_PATCHLEVEL @@VERSION_TYPE@_PATCHLEVEL@
#define @VERSION_TYPE@_VERSION_STRING "@@VERSION_TYPE@_VERSION_STRING@"
#define BUILD_VERSION @BUILD_VERSION@
#define @BUILD_VERSION_NAME@ @@BUILD_VERSION_NAME@@
#endif /* _KERNEL_VERSION_H_ */
#endif /* _@VERSION_TYPE@_VERSION_H_ */