Skip to content

Commit

Permalink
cmake: only write devicetree files when there are changes.
Browse files Browse the repository at this point in the history
As part of zephyrproject-rtos#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]>
  • Loading branch information
tejlmand authored and carlescufi committed Feb 11, 2022
1 parent a17eeb2 commit ea082ac
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ set_ifndef(DTS_CAT_OF_FIXUP_FILES ${ZEPHYR_BINARY_DIR}/include/generated/devicet

# Concatenate the fixups into a single header file for easy
# #include'ing
file(WRITE ${DTS_CAT_OF_FIXUP_FILES} "/* May only be included by devicetree.h */\n\n")
file(WRITE ${DTS_CAT_OF_FIXUP_FILES}.new "/* May only be included by devicetree.h */\n\n")
set(DISCOVERED_FIXUP_FILES)
foreach(fixup_file
${DTS_BOARD_FIXUP_FILE}
Expand All @@ -481,10 +481,12 @@ foreach(fixup_file
)
if(EXISTS ${fixup_file})
file(READ ${fixup_file} contents)
file(APPEND ${DTS_CAT_OF_FIXUP_FILES} "${contents}")
file(APPEND ${DTS_CAT_OF_FIXUP_FILES}.new "${contents}")
string(APPEND DISCOVERED_FIXUP_FILES "- ${fixup_file}\n")
endif()
endforeach()
zephyr_file_copy(${DTS_CAT_OF_FIXUP_FILES}.new ${DTS_CAT_OF_FIXUP_FILES} ONLY_IF_DIFFERENT)
file(REMOVE ${DTS_CAT_OF_FIXUP_FILES}.new)

if (DISCOVERED_FIXUP_FILES)
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.")
Expand Down
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@
/subsys/portability/ @nashif
/lib/libc/ @nashif
/lib/libc/arcmwdt/ @abrodkin @ruuddw @evgeniy-paltsev
/misc/ @tejlmand
/modules/ @nashif
/modules/canopennode/ @henrikbrixandersen
/modules/mbedtls/ @ceolin @d3zd3z
Expand Down
15 changes: 10 additions & 5 deletions cmake/dts.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ if(SUPPORTS_DTS)
--dts ${DTS_POST_CPP}
--dtc-flags '${EXTRA_DTC_FLAGS_RAW}'
--bindings-dirs ${DTS_ROOT_BINDINGS}
--header-out ${DEVICETREE_UNFIXED_H}
--device-header-out ${DEVICE_EXTERN_H}
--dts-out ${ZEPHYR_DTS} # for debugging and dtc
--header-out ${DEVICETREE_UNFIXED_H}.new
--device-header-out ${DEVICE_EXTERN_H}.new
--dts-out ${ZEPHYR_DTS}.new # for debugging and dtc
--edt-pickle-out ${EDT_PICKLE}
${EXTRA_GEN_DEFINES_ARGS}
)
Expand All @@ -204,6 +204,10 @@ if(SUPPORTS_DTS)
if(NOT "${ret}" STREQUAL "0")
message(FATAL_ERROR "gen_defines.py failed with return code: ${ret}")
else()
zephyr_file_copy(${ZEPHYR_DTS}.new ${ZEPHYR_DTS} ONLY_IF_DIFFERENT)
zephyr_file_copy(${DEVICETREE_UNFIXED_H}.new ${DEVICETREE_UNFIXED_H} ONLY_IF_DIFFERENT)
zephyr_file_copy(${DEVICE_EXTERN_H}.new ${DEVICE_EXTERN_H})
file(REMOVE ${ZEPHYR_DTS}.new ${DEVICETREE_UNFIXED_H}.new ${DEVICE_EXTERN_H}.new)
message(STATUS "Generated zephyr.dts: ${ZEPHYR_DTS}")
message(STATUS "Generated devicetree_unfixed.h: ${DEVICETREE_UNFIXED_H}")
message(STATUS "Generated device_extern.h: ${DEVICE_EXTERN_H}")
Expand Down Expand Up @@ -267,6 +271,7 @@ if(SUPPORTS_DTS)
endif()
endif(DTC)
else()
file(WRITE ${DEVICETREE_UNFIXED_H} "/* WARNING. THIS FILE IS AUTO-GENERATED. DO NOT MODIFY! */")
file(WRITE ${DEVICE_EXTERN_H} "/* WARNING. THIS FILE IS AUTO-GENERATED. DO NOT MODIFY! */")
set(header_template ${ZEPHYR_BASE}/misc/generated/generated_header.template)
zephyr_file_copy(${header_template} ${DEVICETREE_UNFIXED_H} ONLY_IF_DIFFERENT)
zephyr_file_copy(${header_template} ${DEVICE_EXTERN_H} ONLY_IF_DIFFERENT)
endif(SUPPORTS_DTS)
36 changes: 36 additions & 0 deletions cmake/extensions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2282,6 +2282,42 @@ Relative paths are only allowed with `-D${ARGV1}=<path>`")
endif()
endfunction()

# Usage:
# zephyr_file_copy(<oldname> <newname> [ONLY_IF_DIFFERENT])
#
# Zephyr file copy extension.
# This function is similar to CMake function
# 'file(COPY_FILE <oldname> <newname> [ONLY_IF_DIFFERENT])'
# introduced with CMake 3.21.
#
# Because the minimal required CMake version with Zephyr is 3.20, this function
# is not guaranteed to be available.
#
# When using CMake version 3.21 or newer 'zephyr_file_copy()' simply calls
# 'file(COPY_FILE...)' directly.
# When using CMake version 3.20, the implementation will execute using CMake
# for running command line tool in a subprocess for identical functionality.
function(zephyr_file_copy oldname newname)
set(options ONLY_IF_DIFFERENT)
cmake_parse_arguments(ZEPHYR_FILE_COPY "${options}" "" "" ${ARGN})

if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.21.0)
if(ZEPHYR_FILE_COPY_ONLY_IF_DIFFERENT)
set(copy_file_options ONLY_IF_DIFFERENT)
endif()
file(COPY_FILE ${oldname} ${newname} ${copy_file_options})
else()
if(ZEPHYR_FILE_COPY_ONLY_IF_DIFFERENT)
set(copy_file_command copy_if_different)
else()
set(copy_file_command copy)
endif()
execute_process(
COMMAND ${CMAKE_COMMAND} -E ${copy_file_command} ${oldname} ${newname}
)
endif()
endfunction()

# Usage:
# zephyr_string(<mode> <out-var> <input> ...)
#
Expand Down
1 change: 1 addition & 0 deletions misc/generated/generated_header.template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* WARNING. THIS FILE IS AUTO-GENERATED. DO NOT MODIFY! */

0 comments on commit ea082ac

Please sign in to comment.