Skip to content

Commit ea082ac

Browse files
tejlmandcarlescufi
authored andcommitted
cmake: only write devicetree files when there are changes.
As part of #40167 is was discovered that devicetree headers are always generated when CMake re-runs. This causes all source files that directly or indirectly through included headers to recompile when CMake re-runs, even if there are no changes to devicetree. This commits introduces `zephyr_file_copy(...)` similar to `file(COPY_FILE ...)` from CMake 3.21. However, as CMake 3.20 is supported by Zephyr we need a zephyr variant of this function to allow usage with CMake 3.20. This ensures that only when there are changes to devicetree headers, then source files will recompile. Signed-off-by: Torsten Rasmussen <[email protected]>
1 parent a17eeb2 commit ea082ac

File tree

5 files changed

+52
-7
lines changed

5 files changed

+52
-7
lines changed

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ set_ifndef(DTS_CAT_OF_FIXUP_FILES ${ZEPHYR_BINARY_DIR}/include/generated/devicet
471471

472472
# Concatenate the fixups into a single header file for easy
473473
# #include'ing
474-
file(WRITE ${DTS_CAT_OF_FIXUP_FILES} "/* May only be included by devicetree.h */\n\n")
474+
file(WRITE ${DTS_CAT_OF_FIXUP_FILES}.new "/* May only be included by devicetree.h */\n\n")
475475
set(DISCOVERED_FIXUP_FILES)
476476
foreach(fixup_file
477477
${DTS_BOARD_FIXUP_FILE}
@@ -481,10 +481,12 @@ foreach(fixup_file
481481
)
482482
if(EXISTS ${fixup_file})
483483
file(READ ${fixup_file} contents)
484-
file(APPEND ${DTS_CAT_OF_FIXUP_FILES} "${contents}")
484+
file(APPEND ${DTS_CAT_OF_FIXUP_FILES}.new "${contents}")
485485
string(APPEND DISCOVERED_FIXUP_FILES "- ${fixup_file}\n")
486486
endif()
487487
endforeach()
488+
zephyr_file_copy(${DTS_CAT_OF_FIXUP_FILES}.new ${DTS_CAT_OF_FIXUP_FILES} ONLY_IF_DIFFERENT)
489+
file(REMOVE ${DTS_CAT_OF_FIXUP_FILES}.new)
488490

489491
if (DISCOVERED_FIXUP_FILES)
490492
message(WARNING "One or more dts_fixup.h files detected:\n${DISCOVERED_FIXUP_FILES}Use of these files is deprecated; use the devicetree.h API instead.")

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@
613613
/subsys/portability/ @nashif
614614
/lib/libc/ @nashif
615615
/lib/libc/arcmwdt/ @abrodkin @ruuddw @evgeniy-paltsev
616+
/misc/ @tejlmand
616617
/modules/ @nashif
617618
/modules/canopennode/ @henrikbrixandersen
618619
/modules/mbedtls/ @ceolin @d3zd3z

cmake/dts.cmake

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ if(SUPPORTS_DTS)
189189
--dts ${DTS_POST_CPP}
190190
--dtc-flags '${EXTRA_DTC_FLAGS_RAW}'
191191
--bindings-dirs ${DTS_ROOT_BINDINGS}
192-
--header-out ${DEVICETREE_UNFIXED_H}
193-
--device-header-out ${DEVICE_EXTERN_H}
194-
--dts-out ${ZEPHYR_DTS} # for debugging and dtc
192+
--header-out ${DEVICETREE_UNFIXED_H}.new
193+
--device-header-out ${DEVICE_EXTERN_H}.new
194+
--dts-out ${ZEPHYR_DTS}.new # for debugging and dtc
195195
--edt-pickle-out ${EDT_PICKLE}
196196
${EXTRA_GEN_DEFINES_ARGS}
197197
)
@@ -204,6 +204,10 @@ if(SUPPORTS_DTS)
204204
if(NOT "${ret}" STREQUAL "0")
205205
message(FATAL_ERROR "gen_defines.py failed with return code: ${ret}")
206206
else()
207+
zephyr_file_copy(${ZEPHYR_DTS}.new ${ZEPHYR_DTS} ONLY_IF_DIFFERENT)
208+
zephyr_file_copy(${DEVICETREE_UNFIXED_H}.new ${DEVICETREE_UNFIXED_H} ONLY_IF_DIFFERENT)
209+
zephyr_file_copy(${DEVICE_EXTERN_H}.new ${DEVICE_EXTERN_H})
210+
file(REMOVE ${ZEPHYR_DTS}.new ${DEVICETREE_UNFIXED_H}.new ${DEVICE_EXTERN_H}.new)
207211
message(STATUS "Generated zephyr.dts: ${ZEPHYR_DTS}")
208212
message(STATUS "Generated devicetree_unfixed.h: ${DEVICETREE_UNFIXED_H}")
209213
message(STATUS "Generated device_extern.h: ${DEVICE_EXTERN_H}")
@@ -267,6 +271,7 @@ if(SUPPORTS_DTS)
267271
endif()
268272
endif(DTC)
269273
else()
270-
file(WRITE ${DEVICETREE_UNFIXED_H} "/* WARNING. THIS FILE IS AUTO-GENERATED. DO NOT MODIFY! */")
271-
file(WRITE ${DEVICE_EXTERN_H} "/* WARNING. THIS FILE IS AUTO-GENERATED. DO NOT MODIFY! */")
274+
set(header_template ${ZEPHYR_BASE}/misc/generated/generated_header.template)
275+
zephyr_file_copy(${header_template} ${DEVICETREE_UNFIXED_H} ONLY_IF_DIFFERENT)
276+
zephyr_file_copy(${header_template} ${DEVICE_EXTERN_H} ONLY_IF_DIFFERENT)
272277
endif(SUPPORTS_DTS)

cmake/extensions.cmake

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,6 +2282,42 @@ Relative paths are only allowed with `-D${ARGV1}=<path>`")
22822282
endif()
22832283
endfunction()
22842284

2285+
# Usage:
2286+
# zephyr_file_copy(<oldname> <newname> [ONLY_IF_DIFFERENT])
2287+
#
2288+
# Zephyr file copy extension.
2289+
# This function is similar to CMake function
2290+
# 'file(COPY_FILE <oldname> <newname> [ONLY_IF_DIFFERENT])'
2291+
# introduced with CMake 3.21.
2292+
#
2293+
# Because the minimal required CMake version with Zephyr is 3.20, this function
2294+
# is not guaranteed to be available.
2295+
#
2296+
# When using CMake version 3.21 or newer 'zephyr_file_copy()' simply calls
2297+
# 'file(COPY_FILE...)' directly.
2298+
# When using CMake version 3.20, the implementation will execute using CMake
2299+
# for running command line tool in a subprocess for identical functionality.
2300+
function(zephyr_file_copy oldname newname)
2301+
set(options ONLY_IF_DIFFERENT)
2302+
cmake_parse_arguments(ZEPHYR_FILE_COPY "${options}" "" "" ${ARGN})
2303+
2304+
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.21.0)
2305+
if(ZEPHYR_FILE_COPY_ONLY_IF_DIFFERENT)
2306+
set(copy_file_options ONLY_IF_DIFFERENT)
2307+
endif()
2308+
file(COPY_FILE ${oldname} ${newname} ${copy_file_options})
2309+
else()
2310+
if(ZEPHYR_FILE_COPY_ONLY_IF_DIFFERENT)
2311+
set(copy_file_command copy_if_different)
2312+
else()
2313+
set(copy_file_command copy)
2314+
endif()
2315+
execute_process(
2316+
COMMAND ${CMAKE_COMMAND} -E ${copy_file_command} ${oldname} ${newname}
2317+
)
2318+
endif()
2319+
endfunction()
2320+
22852321
# Usage:
22862322
# zephyr_string(<mode> <out-var> <input> ...)
22872323
#
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* WARNING. THIS FILE IS AUTO-GENERATED. DO NOT MODIFY! */

0 commit comments

Comments
 (0)