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
parent 643084de0b
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

View File

@ -139,7 +139,6 @@ set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${AUTOCONF_H})
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
include(${ZEPHYR_BASE}/cmake/extensions.cmake)
include(${ZEPHYR_BASE}/cmake/git.cmake)
include(${ZEPHYR_BASE}/cmake/version.cmake) # depends on hex.cmake
#

26
cmake/gen_version_h.cmake Normal file
View File

@ -0,0 +1,26 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
if(NOT DEFINED BUILD_VERSION)
find_package(Git QUIET)
if(GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --abbrev=12 --always
WORKING_DIRECTORY ${ZEPHYR_BASE}
OUTPUT_VARIABLE BUILD_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_STRIP_TRAILING_WHITESPACE
ERROR_VARIABLE stderr
RESULT_VARIABLE return_code
)
if(return_code)
message(STATUS "git describe failed: ${stderr}")
elseif(NOT "${stderr}" STREQUAL "")
message(STATUS "git describe warned: ${stderr}")
endif()
endif()
endif()
include(${ZEPHYR_BASE}/cmake/version.cmake)
configure_file(${ZEPHYR_BASE}/version.h.in ${OUT_FILE})

View File

@ -1,31 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
#.rst:
# git.cmake
# ---------
# If the user didn't already define BUILD_VERSION then try to initialize
# it with the output of "git describe". Warn but don't error if
# everything fails and leave BUILD_VERSION undefined.
#
# See also: independent and more static ``KERNEL_VERSION_*`` in
# ``version.cmake`` and ``kernel_version.h``
# https://cmake.org/cmake/help/latest/module/FindGit.html
find_package(Git QUIET)
if(NOT DEFINED BUILD_VERSION AND GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --abbrev=12 --always
WORKING_DIRECTORY ${ZEPHYR_BASE}
OUTPUT_VARIABLE BUILD_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_STRIP_TRAILING_WHITESPACE
ERROR_VARIABLE stderr
RESULT_VARIABLE return_code
)
if(return_code)
message(STATUS "git describe failed: ${stderr}")
elseif(NOT "${stderr}" STREQUAL "")
message(STATUS "git describe warned: ${stderr}")
endif()
endif()

View File

@ -15,6 +15,7 @@ with the parser to decode binary log messages.
import argparse
import logging
import os
import re
import struct
import sys
@ -47,6 +48,8 @@ def parse_args():
argparser.add_argument("elffile", help="Zephyr ELF binary")
argparser.add_argument("dbfile", help="Dictionary Logging Database file")
argparser.add_argument("--build", help="Build ID")
argparser.add_argument("--build-header",
help="Header file containing BUILD_VERSION define")
argparser.add_argument("--debug", action="store_true",
help="Print extra debugging information")
argparser.add_argument("-v", "--verbose", action="store_true",
@ -264,6 +267,14 @@ def main():
database = LogDatabase()
if args.build_header:
with open(args.build_header) as f:
for l in f:
match = re.match(r'\s*#define\s+BUILD_VERSION\s+(.*)', l)
if match:
database.set_build_id(match.group(1))
break
if args.build:
database.set_build_id(args.build)
logger.info("Build ID: %s", args.build)

View File

@ -1,7 +1,9 @@
#ifndef _KERNEL_VERSION_H_
#define _KERNEL_VERSION_H_
/* @templates@ values come from cmake/version.cmake */
/* KERNEL and ZEPHYR_VERSION @templates@ values come from cmake/version.cmake
* BUILD_VERSION @template@ 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))
@ -13,4 +15,6 @@
#define KERNEL_PATCHLEVEL @KERNEL_PATCHLEVEL@
#define KERNEL_VERSION_STRING @KERNEL_VERSION_STRING@
#define BUILD_VERSION @BUILD_VERSION@
#endif /* _KERNEL_VERSION_H_ */