Skip to content

Commit 468db18

Browse files
57300kartben
authored andcommitted
cmake: modules: Add zephyr_dt_import
Extract the part of `dts.cmake` that invokes `gen_dts_cmake.py`, then generalize it into a CMake extension, which can be reused by sysbuild. The Python script itself is also updated, so that the generated CMake file can accept an input variable DEVICETREE_TARGET, which comes from the `zephyr_dt_import(TARGET ...)` argument. Signed-off-by: Grzegorz Swiderski <[email protected]>
1 parent 438ed6d commit 468db18

File tree

3 files changed

+51
-42
lines changed

3 files changed

+51
-42
lines changed

cmake/modules/dts.cmake

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,6 @@ set(GEN_DRIVER_KCONFIG_SCRIPT ${DT_SCRIPTS}/gen_driver_kconfig_dts.py)
122122
# Generated Kconfig symbols go here.
123123
set(DTS_KCONFIG ${KCONFIG_BINARY_DIR}/Kconfig.dts)
124124

125-
# This generates DT information needed by the CMake APIs.
126-
set(GEN_DTS_CMAKE_SCRIPT ${DT_SCRIPTS}/gen_dts_cmake.py)
127-
# The generated information itself, which we include() after
128-
# creating it.
129-
set(DTS_CMAKE ${PROJECT_BINARY_DIR}/dts.cmake)
130-
131125
# The location of a file containing known vendor prefixes, relative to
132126
# each element of DTS_ROOT. Users can define their own in their own
133127
# modules.
@@ -283,7 +277,6 @@ set_property(DIRECTORY APPEND PROPERTY
283277
${GEN_EDT_SCRIPT}
284278
${GEN_DEFINES_SCRIPT}
285279
${GEN_DRIVER_KCONFIG_SCRIPT}
286-
${GEN_DTS_CMAKE_SCRIPT}
287280
)
288281

289282
#
@@ -356,31 +349,12 @@ if(NOT "${ret}" STREQUAL "0")
356349
endif()
357350

358351
#
359-
# Run GEN_DTS_CMAKE_SCRIPT.
360-
#
361-
# A temporary file is copied to the original file if it differs. This prevents issue such as a
362-
# cycle when sysbuild is used of configuring and building multiple times due to the dts.cmake file
363-
# of images having a newer modification time than the sysbuild build.ninja file, despite the
364-
# output having not changed
352+
# Import devicetree contents into CMake.
353+
# This enables the CMake dt_* API.
365354
#
366-
set(dts_cmake_tmp ${DTS_CMAKE}.new)
367355

368-
execute_process(
369-
COMMAND ${PYTHON_EXECUTABLE} ${GEN_DTS_CMAKE_SCRIPT}
370-
--edt-pickle ${EDT_PICKLE}
371-
--cmake-out ${dts_cmake_tmp}
372-
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
373-
RESULT_VARIABLE ret
374-
)
375-
if(NOT "${ret}" STREQUAL "0")
376-
message(FATAL_ERROR "gen_dts_cmake.py failed with return code: ${ret}")
377-
else()
378-
zephyr_file_copy(${dts_cmake_tmp} ${DTS_CMAKE} ONLY_IF_DIFFERENT)
379-
file(REMOVE ${dts_cmake_tmp})
380-
set(dts_cmake_tmp)
381-
message(STATUS "Including generated dts.cmake file: ${DTS_CMAKE}")
382-
include(${DTS_CMAKE})
383-
endif()
356+
add_custom_target(devicetree_target)
357+
zephyr_dt_import(EDT_PICKLE_FILE ${EDT_PICKLE} TARGET devicetree_target)
384358

385359
#
386360
# Run dtc if it was found.

cmake/modules/extensions.cmake

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4625,6 +4625,45 @@ function(zephyr_dt_preprocess)
46254625
endif()
46264626
endfunction()
46274627

4628+
# Usage:
4629+
# zephyr_dt_import(EDT_PICKLE_FILE <file> TARGET <target>)
4630+
#
4631+
# Parse devicetree information and make it available to CMake, so that
4632+
# it can be accessed by the dt_* CMake extensions from section 4.1.
4633+
#
4634+
# This requires running a Python script, which can take the output of
4635+
# edtlib and generate a CMake source file from it. If that script fails,
4636+
# a fatal error occurs.
4637+
#
4638+
# EDT_PICKLE_FILE <file> : Input edtlib.EDT object in pickle format
4639+
# TARGET <target> : Target to populate with devicetree properties
4640+
#
4641+
function(zephyr_dt_import)
4642+
set(req_single_args "EDT_PICKLE_FILE;TARGET")
4643+
cmake_parse_arguments(arg "" "${req_single_args}" "" ${ARGN})
4644+
zephyr_check_arguments_required_all(${CMAKE_CURRENT_FUNCTION} arg ${req_single_args})
4645+
4646+
set(gen_dts_cmake_script ${ZEPHYR_BASE}/scripts/dts/gen_dts_cmake.py)
4647+
set(gen_dts_cmake_output ${arg_EDT_PICKLE_FILE}.cmake)
4648+
4649+
if((${arg_EDT_PICKLE_FILE} IS_NEWER_THAN ${gen_dts_cmake_output}) OR
4650+
(${gen_dts_cmake_script} IS_NEWER_THAN ${gen_dts_cmake_output})
4651+
)
4652+
execute_process(
4653+
COMMAND ${PYTHON_EXECUTABLE} ${gen_dts_cmake_script}
4654+
--edt-pickle ${arg_EDT_PICKLE_FILE}
4655+
--cmake-out ${gen_dts_cmake_output}
4656+
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
4657+
RESULT_VARIABLE ret
4658+
COMMAND_ERROR_IS_FATAL ANY
4659+
)
4660+
endif()
4661+
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${gen_dts_cmake_script})
4662+
4663+
set(DEVICETREE_TARGET ${arg_TARGET})
4664+
include(${gen_dts_cmake_output})
4665+
endfunction()
4666+
46284667
########################################################
46294668
# 5. Zephyr linker functions
46304669
########################################################

scripts/dts/gen_dts_cmake.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
1212
The generated CMake file looks like this:
1313
14-
add_custom_target(devicetree_target)
15-
set_target_properties(devicetree_target PROPERTIES
14+
set_target_properties(${DEVICETREE_TARGET} PROPERTIES
1615
"DT_PROP|/soc|compatible" "vnd,soc;")
1716
...
1817
19-
It defines a special CMake target, and saves various values in the
20-
devicetree as CMake target properties.
18+
It takes an input variable - DEVICETREE_TARGET - and saves various
19+
values in the devicetree as properties of this CMake target.
2120
2221
Be careful:
2322
@@ -170,15 +169,12 @@ def main():
170169
cmake_comp = f'DT_COMP|{comp}'
171170
cmake_props.append(f'"{cmake_comp}" "{cmake_path}"')
172171

172+
cmake_props = map(
173+
'set_target_properties(${{DEVICETREE_TARGET}} PROPERTIES {})'.format,
174+
cmake_props
175+
)
173176
with open(args.cmake_out, "w", encoding="utf-8") as cmake_file:
174-
print('add_custom_target(devicetree_target)', file=cmake_file)
175-
print(file=cmake_file)
176-
177-
for prop in cmake_props:
178-
print(
179-
f'set_target_properties(devicetree_target PROPERTIES {prop})',
180-
file=cmake_file
181-
)
177+
print("\n".join(cmake_props), file=cmake_file)
182178

183179

184180
if __name__ == "__main__":

0 commit comments

Comments
 (0)