cmake: version.h generation performed at build time

Fixes: #39503
Fixes: #40167

This commit moves `BUILD_VERSION` CMake variable from a compile
definition to be a define inside version.h.

Besides the benefit of having related settings grouped in a common
header, it also means that an updated `BUILD_VERSION` value does not
need to trigger re-compilation of all source files.

When using compile definitions, CMake cannot tell whether a given source
files uses the definition or not, and hence all sources must be
recompiled to be sure they are up-to-date.

Placing `BUILD_VERSION` in version.h, the source dependencies ensures
that only source files including `version.h` gets recompiled.

As part of this, version.h generation is moved so that it is now done
at build time.
This means that re-generation of version.h is no longer depending on a
CMake re-run but can have it's own dependency in `.git/index` when git
described is used to obtain `BUILD_VERSION` information.

Generation of logging dictionary database has been updated to support
BUILD_VERSION from header file instead of CMake configure time variable.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
This commit is contained in:
Torsten Rasmussen 2022-02-04 10:27:13 +01:00 committed by Anas Nashif
commit 91709778a4
6 changed files with 61 additions and 42 deletions

View file

@ -145,12 +145,6 @@ if(CONFIG_STACK_CANARIES)
zephyr_compile_options($<TARGET_PROPERTY:compiler,security_canaries>)
endif()
if(BUILD_VERSION)
zephyr_compile_definitions(
BUILD_VERSION=${BUILD_VERSION}
)
endif()
# @Intent: Obtain compiler optimizations flags and store in variables
# @details:
# Kconfig.zephyr "Optimization level" is a kconfig choice, ensuring
@ -435,7 +429,23 @@ if(NOT EXISTS ${LINKER_SCRIPT})
message(FATAL_ERROR "Could not find linker script: '${LINKER_SCRIPT}'. Corrupted configuration?")
endif()
configure_file(version.h.in ${PROJECT_BINARY_DIR}/include/generated/version.h)
if(DEFINED BUILD_VERSION)
set(build_version_argument "-DBUILD_VERSION=${BUILD_VERSION}")
elseif(EXISTS ${ZEPHYR_BASE}/.git/index)
set(git_dependency ${ZEPHYR_BASE}/.git/index)
else()
message(WARNING "ZEPHYR_BASE=${ZEPHYR_BASE} doesn't appear to be a git "
"repository, please specify '-DBUILD_VERSION=<version>'")
endif()
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
${build_version_argument}
-P ${ZEPHYR_BASE}/cmake/gen_version_h.cmake
DEPENDS ${ZEPHYR_BASE}/VERSION ${git_dependency}
)
add_custom_target(version_h DEPENDS ${PROJECT_BINARY_DIR}/include/generated/version.h)
# Error-out when the deprecated naming convention is found (until
# after 1.14.0 has been released)
@ -705,7 +715,7 @@ gen_kobj(KOBJ_INCLUDE_PATH)
add_custom_target(zephyr_generated_headers)
add_dependencies(zephyr_generated_headers
offsets_h
offsets_h version_h
)
# Generate offsets.c.obj from offsets.c
@ -1652,7 +1662,7 @@ if(CONFIG_LOG_DICTIONARY_SUPPORT)
${ZEPHYR_BASE}/scripts/logging/dictionary/database_gen.py
${KERNEL_ELF_NAME}
${LOG_DICT_DB_NAME}
--build ${BUILD_VERSION}
--build-header ${PROJECT_BINARY_DIR}/include/generated/version.h
)
list(APPEND
post_build_byproducts