From 2c757f9e7a03cd640c79da1e807b7ad79429ec59 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Thu, 23 Mar 2023 08:43:01 +0100 Subject: [PATCH] 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 --- CMakeLists.txt | 2 + cmake/gen_version_h.cmake | 18 +++-- cmake/modules/version.cmake | 129 ++++++++++++++++++++---------------- version.h.in | 27 ++++---- 4 files changed, 103 insertions(+), 73 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ea64339f6..4122249cc1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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} diff --git a/cmake/gen_version_h.cmake b/cmake/gen_version_h.cmake index 0d302b1e46..d7749aa267 100644 --- a/cmake/gen_version_h.cmake +++ b/cmake/gen_version_h.cmake @@ -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}") diff --git a/cmake/modules/version.cmake b/cmake/modules/version.cmake index 0969a269e6..b94bcf492b 100644 --- a/cmake/modules/version.cmake +++ b/cmake/modules/version.cmake @@ -33,72 +33,89 @@ # 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) -string(REGEX MATCH "VERSION_MAJOR = ([0-9]*)" _ ${ver}) -set(PROJECT_VERSION_MAJOR ${CMAKE_MATCH_1}) - -string(REGEX MATCH "VERSION_MINOR = ([0-9]*)" _ ${ver}) -set(PROJECT_VERSION_MINOR ${CMAKE_MATCH_1}) - -string(REGEX MATCH "PATCHLEVEL = ([0-9]*)" _ ${ver}) -set(PROJECT_VERSION_PATCH ${CMAKE_MATCH_1}) - -string(REGEX MATCH "VERSION_TWEAK = ([0-9]*)" _ ${ver}) -set(PROJECT_VERSION_TWEAK ${CMAKE_MATCH_1}) - -string(REGEX MATCH "EXTRAVERSION = ([a-z0-9]*)" _ ${ver}) -set(PROJECT_VERSION_EXTRA ${CMAKE_MATCH_1}) - -# Temporary convenience variable -set(PROJECT_VERSION_WITHOUT_TWEAK ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}) - - -if(PROJECT_VERSION_EXTRA) - set(PROJECT_VERSION_EXTRA_STR "-${PROJECT_VERSION_EXTRA}") +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() -if(PROJECT_VERSION_TWEAK) - set(PROJECT_VERSION ${PROJECT_VERSION_WITHOUT_TWEAK}.${PROJECT_VERSION_TWEAK}) -else() - set(PROJECT_VERSION ${PROJECT_VERSION_WITHOUT_TWEAK}) -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}) -set(PROJECT_VERSION_STR ${PROJECT_VERSION}${PROJECT_VERSION_EXTRA_STR}) + string(REGEX MATCH "VERSION_MAJOR = ([0-9]*)" _ ${ver}) + set(${type}_VERSION_MAJOR ${CMAKE_MATCH_1}) -if(DEFINED BUILD_VERSION) - set(BUILD_VERSION_STR ", build: ${BUILD_VERSION}") -endif() + string(REGEX MATCH "VERSION_MINOR = ([0-9]*)" _ ${ver}) + set(${type}_VERSION_MINOR ${CMAKE_MATCH_1}) -if (NOT NO_PRINT_VERSION) - message(STATUS "Zephyr version: ${PROJECT_VERSION_STR} (${ZEPHYR_BASE})${BUILD_VERSION_STR}") -endif() + string(REGEX MATCH "PATCHLEVEL = ([0-9]*)" _ ${ver}) + set(${type}_PATCHLEVEL ${CMAKE_MATCH_1}) -set(MAJOR ${PROJECT_VERSION_MAJOR}) # Temporary convenience variable -set(MINOR ${PROJECT_VERSION_MINOR}) # Temporary convenience variable -set(PATCH ${PROJECT_VERSION_PATCH}) # Temporary convenience variable + string(REGEX MATCH "VERSION_TWEAK = ([0-9]*)" _ ${ver}) + set(${type}_VERSION_TWEAK ${CMAKE_MATCH_1}) -math(EXPR KERNEL_VERSION_NUMBER_INT "(${MAJOR} << 16) + (${MINOR} << 8) + (${PATCH})") -math(EXPR KERNELVERSION_INT "(${MAJOR} << 24) + (${MINOR} << 16) + (${PATCH} << 8) + (${PROJECT_VERSION_TWEAK})") + string(REGEX MATCH "EXTRAVERSION = ([a-z0-9]*)" _ ${ver}) + set(${type}_VERSION_EXTRA ${CMAKE_MATCH_1}) -to_hex(${KERNEL_VERSION_NUMBER_INT} KERNEL_VERSION_NUMBER) -to_hex(${KERNELVERSION_INT} KERNELVERSION) + # Temporary convenience variable + set(${type}_VERSION_WITHOUT_TWEAK ${${type}_VERSION_MAJOR}.${${type}_VERSION_MINOR}.${${type}_PATCHLEVEL}) -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(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 -set(ZEPHYR_VERSION_CODE ${KERNEL_VERSION_NUMBER_INT}) + math(EXPR ${type}_VERSION_NUMBER_INT "(${MAJOR} << 16) + (${MINOR} << 8) + (${PATCH})") + math(EXPR ${type}VERSION_INT "(${MAJOR} << 24) + (${MINOR} << 16) + (${PATCH} << 8) + (${TWEAK})") -# Cleanup convenience variables -unset(MAJOR) -unset(MINOR) -unset(PATCH) -unset(PROJECT_VERSION_WITHOUT_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(${type}_VERSION_TWEAK) + set(PROJECT_VERSION ${${type}_VERSION_WITHOUT_TWEAK}.${${type}_VERSION_TWEAK}) + else() + 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() + + if (NOT NO_PRINT_VERSION) + message(STATUS "Zephyr version: ${PROJECT_VERSION_STR} (${ZEPHYR_BASE})${BUILD_VERSION_STR}") + endif() + endif() + + # Cleanup convenience variables + unset(MAJOR) + unset(MINOR) + unset(PATCH) + unset(${type}_VERSION_WITHOUT_TWEAK) +endforeach() diff --git a/version.h.in b/version.h.in index 2099c57a0c..96b8ecb204 100644 --- a/version.h.in +++ b/version.h.in @@ -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_ */