Skip to content

Commit 3102fdc

Browse files
pdgendtfabiobaltieri
authored andcommitted
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 <[email protected]>
1 parent 10d9099 commit 3102fdc

File tree

5 files changed

+86
-49
lines changed

5 files changed

+86
-49
lines changed

subsys/bindesc/CMakeLists.txt

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,6 @@ else()
99
zephyr_linker_sources(ROM_START SORT_KEY 0x1bindesc bindesc.ld)
1010
endif()
1111

12-
# Wrapper macro around string(TIMESTAMP ...), that returns the time
13-
# in either local time or UTC, depending on CONFIG_BINDESC_BUILD_TIME_USE_LOCAL_TIME.
14-
macro(get_time out_var format)
15-
if(CONFIG_BINDESC_BUILD_TIME_USE_LOCAL_TIME)
16-
string(TIMESTAMP ${out_var} ${format})
17-
else()
18-
string(TIMESTAMP ${out_var} ${format} UTC)
19-
endif()
20-
endmacro()
21-
22-
macro(gen_build_time_int_definition def_name format)
23-
if(CONFIG_BINDESC_${def_name})
24-
get_time(${def_name} ${format})
25-
# remove leading zeros so that the output will not be interpreted as octal
26-
math(EXPR ${def_name} ${${def_name}})
27-
zephyr_library_compile_definitions(${def_name}=${${def_name}})
28-
endif()
29-
endmacro()
30-
31-
macro(gen_build_time_str_definition def_name format)
32-
if(CONFIG_BINDESC_${def_name})
33-
get_time(${def_name} ${${format}})
34-
zephyr_library_compile_definitions(${def_name}="${${def_name}}")
35-
endif()
36-
endmacro()
37-
3812
macro(gen_str_definition def_name value)
3913
if(CONFIG_BINDESC_${def_name})
4014
zephyr_library_compile_definitions(${def_name}="${value}")
@@ -43,31 +17,37 @@ endmacro()
4317

4418
if(CONFIG_BINDESC_DEFINE_BUILD_TIME)
4519
zephyr_library_sources(bindesc_build_time.c)
46-
gen_build_time_int_definition(BUILD_TIME_YEAR "%Y")
47-
gen_build_time_int_definition(BUILD_TIME_MONTH "%m")
48-
gen_build_time_int_definition(BUILD_TIME_DAY "%d")
49-
gen_build_time_int_definition(BUILD_TIME_HOUR "%H")
50-
gen_build_time_int_definition(BUILD_TIME_MINUTE "%M")
51-
gen_build_time_int_definition(BUILD_TIME_SECOND "%S")
52-
gen_build_time_int_definition(BUILD_TIME_UNIX "%s")
5320

54-
gen_build_time_str_definition(BUILD_DATE_TIME_STRING
55-
CONFIG_BINDESC_BUILD_DATE_TIME_STRING_FORMAT)
56-
gen_build_time_str_definition(BUILD_DATE_STRING
57-
CONFIG_BINDESC_BUILD_DATE_STRING_FORMAT)
58-
gen_build_time_str_definition(BUILD_TIME_STRING
59-
CONFIG_BINDESC_BUILD_TIME_STRING_FORMAT)
21+
set(gen_header ${PROJECT_BINARY_DIR}/include/generated/bindesc_build_time.h)
22+
23+
if(CONFIG_BINDESC_BUILD_TIME_USE_LOCAL_TIME)
24+
set(BUILD_TIME "LOCAL")
25+
else()
26+
set(BUILD_TIME "UTC")
27+
endif()
28+
29+
set(GEN_COMMAND ${CMAKE_COMMAND}
30+
-DIN_FILE=${CMAKE_CURRENT_LIST_DIR}/bindesc_build_time.h.in
31+
-DOUT_FILE=${gen_header}
32+
-DBUILD_TIME_TYPE="${BUILD_TIME}"
33+
-DBUILD_DATE_TIME_STRING_FORMAT="${CONFIG_BINDESC_BUILD_DATE_TIME_STRING_FORMAT}"
34+
-DBUILD_DATE_STRING_FORMAT="${CONFIG_BINDESC_BUILD_DATE_STRING_FORMAT}"
35+
-DBUILD_TIME_STRING_FORMAT="${CONFIG_BINDESC_BUILD_TIME_STRING_FORMAT}"
36+
-P ${CMAKE_CURRENT_LIST_DIR}/gen_bindesc_build_time_h.cmake)
6037

6138
if(CONFIG_BINDESC_BUILD_TIME_ALWAYS_REBUILD)
62-
# By adding a custom target that invokes cmake,
63-
# CMake is forced to rebuild this target on every build. This is
64-
# done to ensure that the timestamp is always up to date.
65-
add_custom_target(
66-
bindesc_time_force_rebuild
67-
COMMAND ${CMAKE_COMMAND} ${CMAKE_BINARY_DIR}
39+
add_custom_target(gen_bindesc_build_time
40+
COMMAND ${GEN_COMMAND}
41+
BYPRODUCTS ${gen_header}
6842
)
69-
zephyr_library_add_dependencies(bindesc_time_force_rebuild)
43+
else()
44+
add_custom_command(OUTPUT ${gen_header}
45+
COMMAND ${GEN_COMMAND}
46+
)
47+
add_custom_target(gen_bindesc_build_time DEPENDS ${gen_header})
7048
endif()
49+
50+
zephyr_library_add_dependencies(gen_bindesc_build_time)
7151
endif()
7252

7353
if(CONFIG_BINDESC_DEFINE_VERSION)

subsys/bindesc/Kconfig.build_time

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ config BINDESC_BUILD_TIME_STRING
7474
The time of compilation as a string, such as "T17:43:14+0000"
7575

7676
config BINDESC_BUILD_DATE_TIME_STRING_FORMAT
77-
depends on BINDESC_BUILD_DATE_TIME_STRING
7877
string "Date-Time format"
7978
default "%Y-%m-%dT%H:%M:%S%z"
8079
help
@@ -89,7 +88,6 @@ config BINDESC_BUILD_DATE_TIME_STRING_FORMAT
8988
Note: the default format complies with ISO-8601.
9089

9190
config BINDESC_BUILD_DATE_STRING_FORMAT
92-
depends on BINDESC_BUILD_DATE_STRING
9391
string "Date format"
9492
default "%Y-%m-%d"
9593
help
@@ -102,7 +100,6 @@ config BINDESC_BUILD_DATE_STRING_FORMAT
102100
Note: the default format complies with ISO-8601.
103101

104102
config BINDESC_BUILD_TIME_STRING_FORMAT
105-
depends on BINDESC_BUILD_TIME_STRING
106103
string "Time format"
107104
default "T%H:%M:%S%z"
108105
help

subsys/bindesc/bindesc_build_time.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#include <zephyr/kernel.h>
88
#include <zephyr/bindesc.h>
99

10+
/* Include generated header */
11+
#include <bindesc_build_time.h>
12+
1013
#if IS_ENABLED(CONFIG_BINDESC_BUILD_TIME_YEAR)
1114
BINDESC_UINT_DEFINE(build_time_year, BINDESC_ID_BUILD_TIME_YEAR, BUILD_TIME_YEAR);
1215
#endif /* IS_ENABLED(CONFIG_BINDESC_BUILD_TIME_YEAR) */
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* SPDX-License-Identifier: Apache-2.0 */
2+
3+
#ifndef _BINDESC_BUILD_TIME_H_
4+
#define _BINDESC_BUILD_TIME_H_
5+
6+
#define BUILD_TIME_YEAR @BUILD_TIME_YEAR@
7+
#define BUILD_TIME_MONTH @BUILD_TIME_MONTH@
8+
#define BUILD_TIME_DAY @BUILD_TIME_DAY@
9+
#define BUILD_TIME_HOUR @BUILD_TIME_HOUR@
10+
#define BUILD_TIME_MINUTE @BUILD_TIME_MINUTE@
11+
#define BUILD_TIME_SECOND @BUILD_TIME_SECOND@
12+
#define BUILD_TIME_UNIX @BUILD_TIME_UNIX@
13+
14+
#define BUILD_DATE_TIME_STRING "@BUILD_DATE_TIME_STRING@"
15+
#define BUILD_DATE_STRING "@BUILD_DATE_STRING@"
16+
#define BUILD_TIME_STRING "@BUILD_TIME_STRING@"
17+
18+
#endif /* _BINDESC_BUILD_TIME_H_ */
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
# Wrapper macro around string(TIMESTAMP ...), that returns the time
6+
# in either local time or UTC, depending on CONFIG_BINDESC_BUILD_TIME_USE_LOCAL_TIME.
7+
macro(get_time out_var format)
8+
if(BUILD_TIME_TYPE STREQUAL LOCAL)
9+
string(TIMESTAMP ${out_var} ${format})
10+
else()
11+
string(TIMESTAMP ${out_var} ${format} UTC)
12+
endif()
13+
endmacro()
14+
15+
macro(gen_build_time_int_definition def_name format)
16+
get_time(${def_name} ${format})
17+
# remove leading zeros so that the output will not be interpreted as octal
18+
math(EXPR ${def_name} ${${def_name}})
19+
endmacro()
20+
21+
macro(gen_build_time_str_definition def_name format)
22+
get_time(${def_name} ${${format}})
23+
endmacro()
24+
25+
gen_build_time_int_definition(BUILD_TIME_YEAR "%Y")
26+
gen_build_time_int_definition(BUILD_TIME_MONTH "%m")
27+
gen_build_time_int_definition(BUILD_TIME_DAY "%d")
28+
gen_build_time_int_definition(BUILD_TIME_HOUR "%H")
29+
gen_build_time_int_definition(BUILD_TIME_MINUTE "%M")
30+
gen_build_time_int_definition(BUILD_TIME_SECOND "%S")
31+
gen_build_time_int_definition(BUILD_TIME_UNIX "%s")
32+
33+
gen_build_time_str_definition(BUILD_DATE_TIME_STRING BUILD_DATE_TIME_STRING_FORMAT)
34+
gen_build_time_str_definition(BUILD_DATE_STRING BUILD_DATE_STRING_FORMAT)
35+
gen_build_time_str_definition(BUILD_TIME_STRING BUILD_TIME_STRING_FORMAT)
36+
37+
file(READ ${IN_FILE} content)
38+
string(CONFIGURE "${content}" content)
39+
file(WRITE ${OUT_FILE} "${content}")

0 commit comments

Comments
 (0)