Skip to content

Commit 2b7c61e

Browse files
mbolivar-nordicnashif
authored andcommitted
cmake: re-work devicetree preprocessing steps
This causes any devicetree error messages to refer to the source files which contain the errors instead of intermediate <BOARD>.dts.pre.tmp files in the build directory. Do this using a new python-devicetree feature which can correctly handle cpp's generated line marker directives. To use this feature, rework the way that the C preprocessor is run on devicetree inputs so that line marker directives are preserved by removing the -P option. This is a backwards incompatible change to the way the generated files in the build directory used to work, as not all tools can consume line markers. In particular, dtc can't handle these lines. We therefore pass dtc zephyr.dts instead (the final parsed devicetree output from python-devicetree). Since <BOARD>.dts.pre.tmp is a publicly documented file, this is changing existing behavior, so use a new file name for the intermediate files to make it more obvious that something changed. In particular, use zephyr.dts.pre instead of <BOARD>.dts.pre.tmp. (The $BOARD.dts.pre.tmp name is a little cumbersome anyway.) Signed-off-by: Martí Bolívar <[email protected]>
1 parent fc416ca commit 2b7c61e

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

cmake/dts.cmake

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ set(ZEPHYR_DTS ${PROJECT_BINARY_DIR}/zephyr.dts)
2222
set(EDT_PICKLE ${PROJECT_BINARY_DIR}/edt.pickle)
2323
set(DEVICETREE_UNFIXED_H ${PROJECT_BINARY_DIR}/include/generated/devicetree_unfixed.h)
2424
set(DEVICE_EXTERN_H ${PROJECT_BINARY_DIR}/include/generated/device_extern.h)
25-
set(DTS_POST_CPP ${PROJECT_BINARY_DIR}/${BOARD}.dts.pre.tmp)
25+
set(DTS_POST_CPP ${PROJECT_BINARY_DIR}/zephyr.dts.pre)
26+
set(DTS_DEPS ${PROJECT_BINARY_DIR}/zephyr.dts.d)
2627
# The location of a list of known vendor prefixes.
2728
# This is relative to each element of DTS_ROOT.
2829
set(VENDOR_PREFIXES dts/bindings/vendor-prefixes.txt)
@@ -141,10 +142,9 @@ if(SUPPORTS_DTS)
141142
# preprocessor, and it seems to be including all kinds of
142143
# directories with who-knows how many header files.
143144

144-
# Run the C preprocessor on an empty C source file that has one or
145-
# more DTS source files -include'd into it to create the
146-
# intermediary file *.dts.pre.tmp. Also, generate a dependency file
147-
# so that changes to DT sources are detected.
145+
# Run the preprocessor on the DTS input files. We are leaving
146+
# linemarker directives enabled on purpose. This tells dtlib where
147+
# each line actually came from, which improves error reporting.
148148
execute_process(
149149
COMMAND ${CMAKE_DTS_PREPROCESSOR}
150150
-x assembler-with-cpp
@@ -154,11 +154,10 @@ if(SUPPORTS_DTS)
154154
${NOSYSDEF_CFLAG}
155155
-D__DTS__
156156
${DTS_EXTRA_CPPFLAGS}
157-
-P
158157
-E # Stop after preprocessing
159158
-MD # Generate a dependency file as a side-effect
160-
-MF ${PROJECT_BINARY_DIR}/${BOARD}.dts.pre.d
161-
-o ${PROJECT_BINARY_DIR}/${BOARD}.dts.pre.tmp
159+
-MF ${DTS_DEPS}
160+
-o ${DTS_POST_CPP}
162161
${ZEPHYR_BASE}/misc/empty_file.c
163162
WORKING_DIRECTORY ${APPLICATION_SOURCE_DIR}
164163
RESULT_VARIABLE ret
@@ -168,10 +167,9 @@ if(SUPPORTS_DTS)
168167
endif()
169168

170169
# Parse the generated dependency file to find the DT sources that
171-
# were included and then add them to the list of files that trigger
172-
# a re-run of CMake.
173-
toolchain_parse_make_rule(
174-
${PROJECT_BINARY_DIR}/${BOARD}.dts.pre.d
170+
# were included, including any transitive includes, and then add
171+
# them to the list of files that trigger a re-run of CMake.
172+
toolchain_parse_make_rule(${DTS_DEPS}
175173
include_files # Output parameter
176174
)
177175

@@ -188,12 +186,12 @@ if(SUPPORTS_DTS)
188186

189187
string(REPLACE ";" " " EXTRA_DTC_FLAGS_RAW "${EXTRA_DTC_FLAGS}")
190188
set(CMD_EXTRACT ${PYTHON_EXECUTABLE} ${GEN_DEFINES_SCRIPT}
191-
--dts ${BOARD}.dts.pre.tmp
189+
--dts ${DTS_POST_CPP}
192190
--dtc-flags '${EXTRA_DTC_FLAGS_RAW}'
193191
--bindings-dirs ${DTS_ROOT_BINDINGS}
194192
--header-out ${DEVICETREE_UNFIXED_H}
195193
--device-header-out ${DEVICE_EXTERN_H}
196-
--dts-out ${ZEPHYR_DTS} # As a debugging aid
194+
--dts-out ${ZEPHYR_DTS} # for debugging and dtc
197195
--edt-pickle-out ${EDT_PICKLE}
198196
${EXTRA_GEN_DEFINES_ARGS}
199197
)
@@ -226,9 +224,9 @@ if(SUPPORTS_DTS)
226224
endif()
227225

228226
#
229-
# Run the C devicetree compiler on *.dts.pre.tmp, just to catch any
227+
# Run dtc on the final devicetree source, just to catch any
230228
# warnings/errors from it. dtlib and edtlib parse the devicetree files
231-
# themselves, so we don't rely on the C compiler otherwise.
229+
# themselves, so we don't rely on dtc otherwise.
232230
#
233231

234232
if(DTC)
@@ -259,7 +257,7 @@ if(SUPPORTS_DTS)
259257
${DTC_NO_WARN_UNIT_ADDR}
260258
${DTC_WARN_UNIT_ADDR_IF_ENABLED}
261259
${EXTRA_DTC_FLAGS} # User settable
262-
${BOARD}.dts.pre.tmp
260+
${ZEPHYR_DTS}
263261
OUTPUT_QUIET # Discard stdout
264262
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
265263
RESULT_VARIABLE ret

0 commit comments

Comments
 (0)