Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake: version.h generation performed at build time #42527

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
SebastianBoe marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion cmake/app/boilerplate.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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

#
Expand Down
26 changes: 26 additions & 0 deletions cmake/gen_version_h.cmake
Original file line number Diff line number Diff line change
@@ -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})
31 changes: 0 additions & 31 deletions cmake/git.cmake

This file was deleted.

11 changes: 11 additions & 0 deletions scripts/logging/dictionary/database_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import argparse
import logging
import os
import re
import struct
import sys

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion version.h.in
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -13,4 +15,6 @@
#define KERNEL_PATCHLEVEL @KERNEL_PATCHLEVEL@
#define KERNEL_VERSION_STRING @KERNEL_VERSION_STRING@

#define BUILD_VERSION @BUILD_VERSION@
SebastianBoe marked this conversation as resolved.
Show resolved Hide resolved

#endif /* _KERNEL_VERSION_H_ */