From 059aae7c9168328279657433d77ae0f1d0214f1b Mon Sep 17 00:00:00 2001 From: Mathieu Choplain Date: Tue, 30 Jul 2024 12:03:28 +0200 Subject: [PATCH] cmake: modules: dts: make Device Tree error messages more visible This commit modifies the DTS cmake module to capture `stderr` output of the `gen_defines.py` script and `dtc` program during their execution. The messages can then be printed as CMake `message`s, which improves QoL when debugging device tree errors, and reduces the risk of introducing malformed DTS, as the warning/error messages are made much more visible. Signed-off-by: Mathieu Choplain --- cmake/modules/dts.cmake | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/cmake/modules/dts.cmake b/cmake/modules/dts.cmake index ece7be588a3105..88abeb3ed43fa5 100644 --- a/cmake/modules/dts.cmake +++ b/cmake/modules/dts.cmake @@ -290,10 +290,25 @@ execute_process( COMMAND ${CMD_GEN_DEFINES} WORKING_DIRECTORY ${PROJECT_BINARY_DIR} RESULT_VARIABLE ret + ERROR_VARIABLE stderr ) if(NOT "${ret}" STREQUAL "0") + if (stderr) + # gen_defines.py failed after printing message(s) to stderr. + # Append stream content to the FATAL_ERROR message on a new line. + set(stderr "\n${stderr}") + else() + # gen_defines.py did not print anything on stderr. To inform users + # of this condition, set ${stderr} to "" to have this printed + # in the error message. Note that we do NOT want a newline, such that + # the error message is printed as a single line, e.g.: + # + # gen_defines.py failed with result code: 1 - stderr contents: + # + set(stderr "") + endif() message(STATUS "In: ${PROJECT_BINARY_DIR}, command: ${CMD_GEN_DEFINES}") - message(FATAL_ERROR "gen_defines.py failed with return code: ${ret}") + message(FATAL_ERROR "gen_defines.py failed with result code: ${ret} - stderr contents: ${stderr}") else() zephyr_file_copy(${ZEPHYR_DTS}.new ${ZEPHYR_DTS} ONLY_IF_DIFFERENT) zephyr_file_copy(${DEVICETREE_GENERATED_H}.new ${DEVICETREE_GENERATED_H} ONLY_IF_DIFFERENT) @@ -377,9 +392,14 @@ execute_process( OUTPUT_QUIET # Discard stdout WORKING_DIRECTORY ${PROJECT_BINARY_DIR} RESULT_VARIABLE ret + ERROR_VARIABLE stderr ) if(NOT "${ret}" STREQUAL "0") - message(FATAL_ERROR "command failed with return code: ${ret}") + message(FATAL_ERROR "dtc failed with return code: ${ret}") +elseif(stderr) + # dtc printed warnings on stderr but did not fail. + # Display them as CMake warnings to draw attention. + message(WARNING "dtc raised one or more warnings:\n${stderr}") endif() endif(DTC)