Skip to content

Commit 9170977

Browse files
tejlmandnashif
authored andcommitted
cmake: version.h generation performed at build time
Fixes: zephyrproject-rtos#39503 Fixes: zephyrproject-rtos#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>
1 parent 643084d commit 9170977

File tree

6 files changed

+61
-42
lines changed

6 files changed

+61
-42
lines changed

CMakeLists.txt

+19-9
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,6 @@ if(CONFIG_STACK_CANARIES)
145145
zephyr_compile_options($<TARGET_PROPERTY:compiler,security_canaries>)
146146
endif()
147147

148-
if(BUILD_VERSION)
149-
zephyr_compile_definitions(
150-
BUILD_VERSION=${BUILD_VERSION}
151-
)
152-
endif()
153-
154148
# @Intent: Obtain compiler optimizations flags and store in variables
155149
# @details:
156150
# Kconfig.zephyr "Optimization level" is a kconfig choice, ensuring
@@ -435,7 +429,23 @@ if(NOT EXISTS ${LINKER_SCRIPT})
435429
message(FATAL_ERROR "Could not find linker script: '${LINKER_SCRIPT}'. Corrupted configuration?")
436430
endif()
437431

438-
configure_file(version.h.in ${PROJECT_BINARY_DIR}/include/generated/version.h)
432+
if(DEFINED BUILD_VERSION)
433+
set(build_version_argument "-DBUILD_VERSION=${BUILD_VERSION}")
434+
elseif(EXISTS ${ZEPHYR_BASE}/.git/index)
435+
set(git_dependency ${ZEPHYR_BASE}/.git/index)
436+
else()
437+
message(WARNING "ZEPHYR_BASE=${ZEPHYR_BASE} doesn't appear to be a git "
438+
"repository, please specify '-DBUILD_VERSION=<version>'")
439+
endif()
440+
add_custom_command(
441+
OUTPUT ${PROJECT_BINARY_DIR}/include/generated/version.h
442+
COMMAND ${CMAKE_COMMAND} -DZEPHYR_BASE=${ZEPHYR_BASE}
443+
-DOUT_FILE=${PROJECT_BINARY_DIR}/include/generated/version.h
444+
${build_version_argument}
445+
-P ${ZEPHYR_BASE}/cmake/gen_version_h.cmake
446+
DEPENDS ${ZEPHYR_BASE}/VERSION ${git_dependency}
447+
)
448+
add_custom_target(version_h DEPENDS ${PROJECT_BINARY_DIR}/include/generated/version.h)
439449

440450
# Error-out when the deprecated naming convention is found (until
441451
# after 1.14.0 has been released)
@@ -705,7 +715,7 @@ gen_kobj(KOBJ_INCLUDE_PATH)
705715

706716
add_custom_target(zephyr_generated_headers)
707717
add_dependencies(zephyr_generated_headers
708-
offsets_h
718+
offsets_h version_h
709719
)
710720

711721
# Generate offsets.c.obj from offsets.c
@@ -1652,7 +1662,7 @@ if(CONFIG_LOG_DICTIONARY_SUPPORT)
16521662
${ZEPHYR_BASE}/scripts/logging/dictionary/database_gen.py
16531663
${KERNEL_ELF_NAME}
16541664
${LOG_DICT_DB_NAME}
1655-
--build ${BUILD_VERSION}
1665+
--build-header ${PROJECT_BINARY_DIR}/include/generated/version.h
16561666
)
16571667
list(APPEND
16581668
post_build_byproducts

cmake/app/boilerplate.cmake

-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${AUTOCONF_H})
139139
include(CheckCCompilerFlag)
140140
include(CheckCXXCompilerFlag)
141141
include(${ZEPHYR_BASE}/cmake/extensions.cmake)
142-
include(${ZEPHYR_BASE}/cmake/git.cmake)
143142
include(${ZEPHYR_BASE}/cmake/version.cmake) # depends on hex.cmake
144143

145144
#

cmake/gen_version_h.cmake

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
if(NOT DEFINED BUILD_VERSION)
6+
find_package(Git QUIET)
7+
if(GIT_FOUND)
8+
execute_process(
9+
COMMAND ${GIT_EXECUTABLE} describe --abbrev=12 --always
10+
WORKING_DIRECTORY ${ZEPHYR_BASE}
11+
OUTPUT_VARIABLE BUILD_VERSION
12+
OUTPUT_STRIP_TRAILING_WHITESPACE
13+
ERROR_STRIP_TRAILING_WHITESPACE
14+
ERROR_VARIABLE stderr
15+
RESULT_VARIABLE return_code
16+
)
17+
if(return_code)
18+
message(STATUS "git describe failed: ${stderr}")
19+
elseif(NOT "${stderr}" STREQUAL "")
20+
message(STATUS "git describe warned: ${stderr}")
21+
endif()
22+
endif()
23+
endif()
24+
25+
include(${ZEPHYR_BASE}/cmake/version.cmake)
26+
configure_file(${ZEPHYR_BASE}/version.h.in ${OUT_FILE})

cmake/git.cmake

-31
This file was deleted.

scripts/logging/dictionary/database_gen.py

+11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import argparse
1616
import logging
1717
import os
18+
import re
1819
import struct
1920
import sys
2021

@@ -47,6 +48,8 @@ def parse_args():
4748
argparser.add_argument("elffile", help="Zephyr ELF binary")
4849
argparser.add_argument("dbfile", help="Dictionary Logging Database file")
4950
argparser.add_argument("--build", help="Build ID")
51+
argparser.add_argument("--build-header",
52+
help="Header file containing BUILD_VERSION define")
5053
argparser.add_argument("--debug", action="store_true",
5154
help="Print extra debugging information")
5255
argparser.add_argument("-v", "--verbose", action="store_true",
@@ -264,6 +267,14 @@ def main():
264267

265268
database = LogDatabase()
266269

270+
if args.build_header:
271+
with open(args.build_header) as f:
272+
for l in f:
273+
match = re.match(r'\s*#define\s+BUILD_VERSION\s+(.*)', l)
274+
if match:
275+
database.set_build_id(match.group(1))
276+
break
277+
267278
if args.build:
268279
database.set_build_id(args.build)
269280
logger.info("Build ID: %s", args.build)

version.h.in

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#ifndef _KERNEL_VERSION_H_
22
#define _KERNEL_VERSION_H_
33

4-
/* @templates@ values come from cmake/version.cmake */
4+
/* KERNEL and ZEPHYR_VERSION @templates@ values come from cmake/version.cmake
5+
* BUILD_VERSION @template@ will be 'git describe', alternatively user defined BUILD_VERSION.
6+
*/
57

68
#cmakedefine ZEPHYR_VERSION_CODE @ZEPHYR_VERSION_CODE@
79
#define ZEPHYR_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
@@ -13,4 +15,6 @@
1315
#define KERNEL_PATCHLEVEL @KERNEL_PATCHLEVEL@
1416
#define KERNEL_VERSION_STRING @KERNEL_VERSION_STRING@
1517

18+
#define BUILD_VERSION @BUILD_VERSION@
19+
1620
#endif /* _KERNEL_VERSION_H_ */

0 commit comments

Comments
 (0)