Skip to content

Commit

Permalink
cmake: generalize VERSION infrastructure for better reuse
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
tejlmand authored and carlescufi committed May 17, 2023
1 parent 37c6136 commit 2c757f9
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 67 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
18 changes: 14 additions & 4 deletions cmake/gen_version_h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}")
117 changes: 67 additions & 50 deletions cmake/modules/version.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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})
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()

string(REGEX MATCH "VERSION_MINOR = ([0-9]*)" _ ${ver})
set(PROJECT_VERSION_MINOR ${CMAKE_MATCH_1})
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 "PATCHLEVEL = ([0-9]*)" _ ${ver})
set(PROJECT_VERSION_PATCH ${CMAKE_MATCH_1})
string(REGEX MATCH "VERSION_MAJOR = ([0-9]*)" _ ${ver})
set(${type}_VERSION_MAJOR ${CMAKE_MATCH_1})

string(REGEX MATCH "VERSION_TWEAK = ([0-9]*)" _ ${ver})
set(PROJECT_VERSION_TWEAK ${CMAKE_MATCH_1})
string(REGEX MATCH "VERSION_MINOR = ([0-9]*)" _ ${ver})
set(${type}_VERSION_MINOR ${CMAKE_MATCH_1})

string(REGEX MATCH "EXTRAVERSION = ([a-z0-9]*)" _ ${ver})
set(PROJECT_VERSION_EXTRA ${CMAKE_MATCH_1})
string(REGEX MATCH "PATCHLEVEL = ([0-9]*)" _ ${ver})
set(${type}_PATCHLEVEL ${CMAKE_MATCH_1})

# Temporary convenience variable
set(PROJECT_VERSION_WITHOUT_TWEAK ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})
string(REGEX MATCH "VERSION_TWEAK = ([0-9]*)" _ ${ver})
set(${type}_VERSION_TWEAK ${CMAKE_MATCH_1})

string(REGEX MATCH "EXTRAVERSION = ([a-z0-9]*)" _ ${ver})
set(${type}_VERSION_EXTRA ${CMAKE_MATCH_1})

if(PROJECT_VERSION_EXTRA)
set(PROJECT_VERSION_EXTRA_STR "-${PROJECT_VERSION_EXTRA}")
endif()
# Temporary convenience variable
set(${type}_VERSION_WITHOUT_TWEAK ${${type}_VERSION_MAJOR}.${${type}_VERSION_MINOR}.${${type}_PATCHLEVEL})

if(PROJECT_VERSION_TWEAK)
set(PROJECT_VERSION ${PROJECT_VERSION_WITHOUT_TWEAK}.${PROJECT_VERSION_TWEAK})
else()
set(PROJECT_VERSION ${PROJECT_VERSION_WITHOUT_TWEAK})
endif()

set(PROJECT_VERSION_STR ${PROJECT_VERSION}${PROJECT_VERSION_EXTRA_STR})
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

if(DEFINED BUILD_VERSION)
set(BUILD_VERSION_STR ", build: ${BUILD_VERSION}")
endif()
math(EXPR ${type}_VERSION_NUMBER_INT "(${MAJOR} << 16) + (${MINOR} << 8) + (${PATCH})")
math(EXPR ${type}VERSION_INT "(${MAJOR} << 24) + (${MINOR} << 16) + (${PATCH} << 8) + (${TWEAK})")

if (NOT NO_PRINT_VERSION)
message(STATUS "Zephyr version: ${PROJECT_VERSION_STR} (${ZEPHYR_BASE})${BUILD_VERSION_STR}")
endif()
to_hex(${${type}_VERSION_NUMBER_INT} ${type}_VERSION_NUMBER)
to_hex(${${type}VERSION_INT} ${type}VERSION)

set(MAJOR ${PROJECT_VERSION_MAJOR}) # Temporary convenience variable
set(MINOR ${PROJECT_VERSION_MINOR}) # Temporary convenience variable
set(PATCH ${PROJECT_VERSION_PATCH}) # Temporary convenience variable
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()

math(EXPR KERNEL_VERSION_NUMBER_INT "(${MAJOR} << 16) + (${MINOR} << 8) + (${PATCH})")
math(EXPR KERNELVERSION_INT "(${MAJOR} << 24) + (${MINOR} << 16) + (${PATCH} << 8) + (${PROJECT_VERSION_TWEAK})")
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})

to_hex(${KERNEL_VERSION_NUMBER_INT} KERNEL_VERSION_NUMBER)
to_hex(${KERNELVERSION_INT} KERNELVERSION)
if(PROJECT_VERSION_EXTRA)
set(PROJECT_VERSION_EXTRA_STR "-${PROJECT_VERSION_EXTRA}")
endif()

set(KERNEL_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
set(KERNEL_VERSION_MINOR ${PROJECT_VERSION_MINOR})
set(KERNEL_PATCHLEVEL ${PROJECT_VERSION_PATCH})
if(${type}_VERSION_TWEAK)
set(PROJECT_VERSION ${${type}_VERSION_WITHOUT_TWEAK}.${${type}_VERSION_TWEAK})
else()
set(PROJECT_VERSION ${${type}_VERSION_WITHOUT_TWEAK})
endif()

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(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()

set(ZEPHYR_VERSION_CODE ${KERNEL_VERSION_NUMBER_INT})
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(PROJECT_VERSION_WITHOUT_TWEAK)
# Cleanup convenience variables
unset(MAJOR)
unset(MINOR)
unset(PATCH)
unset(${type}_VERSION_WITHOUT_TWEAK)
endforeach()
27 changes: 14 additions & 13 deletions version.h.in
Original file line number Diff line number Diff line change
@@ -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_ */

0 comments on commit 2c757f9

Please sign in to comment.