bindesc: Update build time without re-running cmake entirely

With CONFIG_BINDESC_BUILD_TIME_ALWAYS_REBUILD a re-run was called for
the entire project.
This can result in issues with the zephyr linker mechanism.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
This commit is contained in:
Pieter De Gendt 2024-03-27 13:31:33 +01:00 committed by Fabio Baltieri
parent 10d9099356
commit 3102fdc8c1
5 changed files with 86 additions and 49 deletions

View File

@ -9,32 +9,6 @@ else()
zephyr_linker_sources(ROM_START SORT_KEY 0x1bindesc bindesc.ld)
endif()
# Wrapper macro around string(TIMESTAMP ...), that returns the time
# in either local time or UTC, depending on CONFIG_BINDESC_BUILD_TIME_USE_LOCAL_TIME.
macro(get_time out_var format)
if(CONFIG_BINDESC_BUILD_TIME_USE_LOCAL_TIME)
string(TIMESTAMP ${out_var} ${format})
else()
string(TIMESTAMP ${out_var} ${format} UTC)
endif()
endmacro()
macro(gen_build_time_int_definition def_name format)
if(CONFIG_BINDESC_${def_name})
get_time(${def_name} ${format})
# remove leading zeros so that the output will not be interpreted as octal
math(EXPR ${def_name} ${${def_name}})
zephyr_library_compile_definitions(${def_name}=${${def_name}})
endif()
endmacro()
macro(gen_build_time_str_definition def_name format)
if(CONFIG_BINDESC_${def_name})
get_time(${def_name} ${${format}})
zephyr_library_compile_definitions(${def_name}="${${def_name}}")
endif()
endmacro()
macro(gen_str_definition def_name value)
if(CONFIG_BINDESC_${def_name})
zephyr_library_compile_definitions(${def_name}="${value}")
@ -43,31 +17,37 @@ endmacro()
if(CONFIG_BINDESC_DEFINE_BUILD_TIME)
zephyr_library_sources(bindesc_build_time.c)
gen_build_time_int_definition(BUILD_TIME_YEAR "%Y")
gen_build_time_int_definition(BUILD_TIME_MONTH "%m")
gen_build_time_int_definition(BUILD_TIME_DAY "%d")
gen_build_time_int_definition(BUILD_TIME_HOUR "%H")
gen_build_time_int_definition(BUILD_TIME_MINUTE "%M")
gen_build_time_int_definition(BUILD_TIME_SECOND "%S")
gen_build_time_int_definition(BUILD_TIME_UNIX "%s")
gen_build_time_str_definition(BUILD_DATE_TIME_STRING
CONFIG_BINDESC_BUILD_DATE_TIME_STRING_FORMAT)
gen_build_time_str_definition(BUILD_DATE_STRING
CONFIG_BINDESC_BUILD_DATE_STRING_FORMAT)
gen_build_time_str_definition(BUILD_TIME_STRING
CONFIG_BINDESC_BUILD_TIME_STRING_FORMAT)
set(gen_header ${PROJECT_BINARY_DIR}/include/generated/bindesc_build_time.h)
if(CONFIG_BINDESC_BUILD_TIME_USE_LOCAL_TIME)
set(BUILD_TIME "LOCAL")
else()
set(BUILD_TIME "UTC")
endif()
set(GEN_COMMAND ${CMAKE_COMMAND}
-DIN_FILE=${CMAKE_CURRENT_LIST_DIR}/bindesc_build_time.h.in
-DOUT_FILE=${gen_header}
-DBUILD_TIME_TYPE="${BUILD_TIME}"
-DBUILD_DATE_TIME_STRING_FORMAT="${CONFIG_BINDESC_BUILD_DATE_TIME_STRING_FORMAT}"
-DBUILD_DATE_STRING_FORMAT="${CONFIG_BINDESC_BUILD_DATE_STRING_FORMAT}"
-DBUILD_TIME_STRING_FORMAT="${CONFIG_BINDESC_BUILD_TIME_STRING_FORMAT}"
-P ${CMAKE_CURRENT_LIST_DIR}/gen_bindesc_build_time_h.cmake)
if(CONFIG_BINDESC_BUILD_TIME_ALWAYS_REBUILD)
# By adding a custom target that invokes cmake,
# CMake is forced to rebuild this target on every build. This is
# done to ensure that the timestamp is always up to date.
add_custom_target(
bindesc_time_force_rebuild
COMMAND ${CMAKE_COMMAND} ${CMAKE_BINARY_DIR}
add_custom_target(gen_bindesc_build_time
COMMAND ${GEN_COMMAND}
BYPRODUCTS ${gen_header}
)
zephyr_library_add_dependencies(bindesc_time_force_rebuild)
else()
add_custom_command(OUTPUT ${gen_header}
COMMAND ${GEN_COMMAND}
)
add_custom_target(gen_bindesc_build_time DEPENDS ${gen_header})
endif()
zephyr_library_add_dependencies(gen_bindesc_build_time)
endif()
if(CONFIG_BINDESC_DEFINE_VERSION)

View File

@ -74,7 +74,6 @@ config BINDESC_BUILD_TIME_STRING
The time of compilation as a string, such as "T17:43:14+0000"
config BINDESC_BUILD_DATE_TIME_STRING_FORMAT
depends on BINDESC_BUILD_DATE_TIME_STRING
string "Date-Time format"
default "%Y-%m-%dT%H:%M:%S%z"
help
@ -89,7 +88,6 @@ config BINDESC_BUILD_DATE_TIME_STRING_FORMAT
Note: the default format complies with ISO-8601.
config BINDESC_BUILD_DATE_STRING_FORMAT
depends on BINDESC_BUILD_DATE_STRING
string "Date format"
default "%Y-%m-%d"
help
@ -102,7 +100,6 @@ config BINDESC_BUILD_DATE_STRING_FORMAT
Note: the default format complies with ISO-8601.
config BINDESC_BUILD_TIME_STRING_FORMAT
depends on BINDESC_BUILD_TIME_STRING
string "Time format"
default "T%H:%M:%S%z"
help

View File

@ -7,6 +7,9 @@
#include <zephyr/kernel.h>
#include <zephyr/bindesc.h>
/* Include generated header */
#include <bindesc_build_time.h>
#if IS_ENABLED(CONFIG_BINDESC_BUILD_TIME_YEAR)
BINDESC_UINT_DEFINE(build_time_year, BINDESC_ID_BUILD_TIME_YEAR, BUILD_TIME_YEAR);
#endif /* IS_ENABLED(CONFIG_BINDESC_BUILD_TIME_YEAR) */

View File

@ -0,0 +1,18 @@
/* SPDX-License-Identifier: Apache-2.0 */
#ifndef _BINDESC_BUILD_TIME_H_
#define _BINDESC_BUILD_TIME_H_
#define BUILD_TIME_YEAR @BUILD_TIME_YEAR@
#define BUILD_TIME_MONTH @BUILD_TIME_MONTH@
#define BUILD_TIME_DAY @BUILD_TIME_DAY@
#define BUILD_TIME_HOUR @BUILD_TIME_HOUR@
#define BUILD_TIME_MINUTE @BUILD_TIME_MINUTE@
#define BUILD_TIME_SECOND @BUILD_TIME_SECOND@
#define BUILD_TIME_UNIX @BUILD_TIME_UNIX@
#define BUILD_DATE_TIME_STRING "@BUILD_DATE_TIME_STRING@"
#define BUILD_DATE_STRING "@BUILD_DATE_STRING@"
#define BUILD_TIME_STRING "@BUILD_TIME_STRING@"
#endif /* _BINDESC_BUILD_TIME_H_ */

View File

@ -0,0 +1,39 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
# Wrapper macro around string(TIMESTAMP ...), that returns the time
# in either local time or UTC, depending on CONFIG_BINDESC_BUILD_TIME_USE_LOCAL_TIME.
macro(get_time out_var format)
if(BUILD_TIME_TYPE STREQUAL LOCAL)
string(TIMESTAMP ${out_var} ${format})
else()
string(TIMESTAMP ${out_var} ${format} UTC)
endif()
endmacro()
macro(gen_build_time_int_definition def_name format)
get_time(${def_name} ${format})
# remove leading zeros so that the output will not be interpreted as octal
math(EXPR ${def_name} ${${def_name}})
endmacro()
macro(gen_build_time_str_definition def_name format)
get_time(${def_name} ${${format}})
endmacro()
gen_build_time_int_definition(BUILD_TIME_YEAR "%Y")
gen_build_time_int_definition(BUILD_TIME_MONTH "%m")
gen_build_time_int_definition(BUILD_TIME_DAY "%d")
gen_build_time_int_definition(BUILD_TIME_HOUR "%H")
gen_build_time_int_definition(BUILD_TIME_MINUTE "%M")
gen_build_time_int_definition(BUILD_TIME_SECOND "%S")
gen_build_time_int_definition(BUILD_TIME_UNIX "%s")
gen_build_time_str_definition(BUILD_DATE_TIME_STRING BUILD_DATE_TIME_STRING_FORMAT)
gen_build_time_str_definition(BUILD_DATE_STRING BUILD_DATE_STRING_FORMAT)
gen_build_time_str_definition(BUILD_TIME_STRING BUILD_TIME_STRING_FORMAT)
file(READ ${IN_FILE} content)
string(CONFIGURE "${content}" content)
file(WRITE ${OUT_FILE} "${content}")