From 8092ff349bc22d4c61068238eec29dfcf57659e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Bol=C3=ADvar?= Date: Mon, 27 Feb 2023 14:14:07 -0800 Subject: [PATCH 1/6] cmake: extensions: fix comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The name of each commented section should match the name in the "table of contents", for consistency and so people can jump from contents to implementations more easily with their editors' search functions. Signed-off-by: Martí Bolívar --- cmake/modules/extensions.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/modules/extensions.cmake b/cmake/modules/extensions.cmake index aa66e8f24342..77d852cae66e 100644 --- a/cmake/modules/extensions.cmake +++ b/cmake/modules/extensions.cmake @@ -3427,7 +3427,7 @@ function(target_sources_if_dt_node path target scope item) endfunction() ######################################################## -# 5. Zephyr linker function +# 5. Zephyr linker functions ######################################################## # 5.1. zephyr_linker* # From b09481b1d246a102bab6f6f20c1554aedf14b9fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Bol=C3=ADvar?= Date: Mon, 27 Feb 2023 14:27:28 -0800 Subject: [PATCH 2/6] cmake: modules: add generated_file_directories MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Due to Hyrum's Law, there are users out in the wild depending on this directory existing to place their own generated files, so it'd break things unnecessarily to not do this if we don't have a DTS, as explained in a source code comment. However, this doesn't really have anything to do with DTS processing, so split it into its own module to separate concerns. This isn't really a CMake module in the usual sense of something that defines functions you can use, and is therefore a form of technical debt. The decision was made to accept this because fixing this is a larger task for the files in cmake/modules/, since there are multiple other examples of this in here. This also paves the way for inserting another module in between the generated_file_directories and dts modules that itself depends on these directories existing. Signed-off-by: Martí Bolívar --- cmake/modules/dts.cmake | 8 ------- .../modules/generated_file_directories.cmake | 24 +++++++++++++++++++ cmake/modules/zephyr_default.cmake | 1 + 3 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 cmake/modules/generated_file_directories.cmake diff --git a/cmake/modules/dts.cmake b/cmake/modules/dts.cmake index fd41904e1603..24ec436d41c7 100644 --- a/cmake/modules/dts.cmake +++ b/cmake/modules/dts.cmake @@ -39,14 +39,6 @@ find_package(Dtc 1.4.6) # run the dtc tool if it is found, in order to catch any additional # warnings or errors it generates. -# We will place some generated include files in here. -set(BINARY_DIR_INCLUDE ${PROJECT_BINARY_DIR}/include) -set(BINARY_DIR_INCLUDE_GENERATED ${BINARY_DIR_INCLUDE}/generated) -# Unconditionally create it, even if we don't have DTS support. This -# is a historical artifact, and users expect this directory to exist -# to put their own generated content inside. -file(MAKE_DIRECTORY ${BINARY_DIR_INCLUDE_GENERATED}) - # The directory containing devicetree related scripts. set(DT_SCRIPTS ${ZEPHYR_BASE}/scripts/dts) diff --git a/cmake/modules/generated_file_directories.cmake b/cmake/modules/generated_file_directories.cmake new file mode 100644 index 000000000000..aac3b39a6f9b --- /dev/null +++ b/cmake/modules/generated_file_directories.cmake @@ -0,0 +1,24 @@ +# SPDX-License-Identifier: Apache-2.0 + +include_guard(GLOBAL) + +# This file creates locations in the build directory +# for placing generated files. +# +# Outcome: +# - BINARY_DIR_INCLUDE is set to ${PROJECT_BINARY_DIR}/include +# - BINARY_DIR_INCLUDE_GENERATED is set to ${BINARY_DIR_INCLUDE}/generated +# - BINARY_DIR_INCLUDE_GENERATED is a directory +# +# Required variables: +# None +# +# Optional variables: +# None +# +# Optional environment variables: +# None + +set(BINARY_DIR_INCLUDE ${PROJECT_BINARY_DIR}/include/generated) +set(BINARY_DIR_INCLUDE_GENERATED ${PROJECT_BINARY_DIR}/include/generated) +file(MAKE_DIRECTORY ${BINARY_DIR_INCLUDE_GENERATED}) diff --git a/cmake/modules/zephyr_default.cmake b/cmake/modules/zephyr_default.cmake index 0e41a253dd1f..8d651e0b8fee 100644 --- a/cmake/modules/zephyr_default.cmake +++ b/cmake/modules/zephyr_default.cmake @@ -84,6 +84,7 @@ list(APPEND zephyr_cmake_modules shields) list(APPEND zephyr_cmake_modules snippets) list(APPEND zephyr_cmake_modules arch) list(APPEND zephyr_cmake_modules configuration_files) +list(APPEND zephyr_cmake_modules generated_file_directories) # Include board specific device-tree flags before parsing. set(pre_dt_board "\${BOARD_DIR}/pre_dt_board.cmake" OPTIONAL) From a218379150e28541660686eaeda2ec0e41d3aa02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Bol=C3=ADvar?= Date: Tue, 28 Feb 2023 11:56:45 -0800 Subject: [PATCH 3/6] cmake: modules: add pre_dt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Create a separate module that sets up all our devicetree handling, by setting up common variables that would apply to any and all DT processing. This is then included in the regular dts module that we include in zephyr_default.cmake. The separation of this code from dts.cmake is groundwork for enabling system devicetree in Zephyr, which will need the same definitions included into its scope. Signed-off-by: Martí Bolívar --- cmake/modules/dts.cmake | 58 +----------------------- cmake/modules/pre_dt.cmake | 90 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 57 deletions(-) create mode 100644 cmake/modules/pre_dt.cmake diff --git a/cmake/modules/dts.cmake b/cmake/modules/dts.cmake index 24ec436d41c7..813d12102355 100644 --- a/cmake/modules/dts.cmake +++ b/cmake/modules/dts.cmake @@ -5,6 +5,7 @@ include_guard(GLOBAL) include(extensions) include(python) include(boards) +include(pre_dt) find_package(HostTools) find_package(Dtc 1.4.6) @@ -71,9 +72,6 @@ set(DTS_CMAKE ${PROJECT_BINARY_DIR}/dts.cmake) # modules. set(VENDOR_PREFIXES dts/bindings/vendor-prefixes.txt) -# The C preprocessor to use. -set_ifndef(CMAKE_DTS_PREPROCESSOR ${CMAKE_C_COMPILER}) - # # Halt execution early if there is no devicetree. # @@ -93,43 +91,6 @@ else() return() endif() -# -# Finalize the value of DTS_ROOT, so we know where all our -# DTS files, bindings, and vendor prefixes are. -# - -# Convert relative paths to absolute paths relative to the application -# source directory. -zephyr_file(APPLICATION_ROOT DTS_ROOT) - -# DTS_ROOT always includes the application directory, the board -# directory, shield directories, and ZEPHYR_BASE. -list(APPEND - DTS_ROOT - ${APPLICATION_SOURCE_DIR} - ${BOARD_DIR} - ${SHIELD_DIRS} - ${ZEPHYR_BASE} - ) - -# Convert the directories in DTS_ROOT to absolute paths without -# symlinks. -# -# DTS directories can come from multiple places. Some places, like a -# user's CMakeLists.txt can preserve symbolic links. Others, like -# scripts/zephyr_module.py --settings-out resolve them. -unset(real_dts_root) -foreach(dts_dir ${DTS_ROOT}) - file(REAL_PATH ${dts_dir} real_dts_dir) - list(APPEND real_dts_root ${real_dts_dir}) -endforeach() -set(DTS_ROOT ${real_dts_root}) - -# Finally, de-duplicate the list. -list(REMOVE_DUPLICATES - DTS_ROOT - ) - # # Find all the DTS files we need to concatenate and preprocess, as # well as all the devicetree bindings and vendor prefixes associated @@ -170,25 +131,8 @@ foreach(dts_file ${dts_files}) math(EXPR i "${i}+1") endforeach() -unset(DTS_ROOT_SYSTEM_INCLUDE_DIRS) unset(DTS_ROOT_BINDINGS) foreach(dts_root ${DTS_ROOT}) - foreach(dts_root_path - include - include/zephyr - dts/common - dts/${ARCH} - dts - ) - get_filename_component(full_path ${dts_root}/${dts_root_path} REALPATH) - if(EXISTS ${full_path}) - list(APPEND - DTS_ROOT_SYSTEM_INCLUDE_DIRS - -isystem ${full_path} - ) - endif() - endforeach() - set(bindings_path ${dts_root}/dts/bindings) if(EXISTS ${bindings_path}) list(APPEND diff --git a/cmake/modules/pre_dt.cmake b/cmake/modules/pre_dt.cmake new file mode 100644 index 000000000000..38ce53d2b904 --- /dev/null +++ b/cmake/modules/pre_dt.cmake @@ -0,0 +1,90 @@ +# SPDX-License-Identifier: Apache-2.0 +# +# Copyright (c) 2021, 2023 Nordic Semiconductor ASA + +include_guard(GLOBAL) +include(extensions) + +# Finalize the value of DTS_ROOT, so we know where all our +# DTS files, bindings, and vendor prefixes are. +# +# Outcome: +# The following variables will be defined when this CMake module completes: +# +# - CMAKE_DTS_PREPROCESSOR: the path to the preprocessor to use +# for devicetree files +# - DTS_ROOT: a deduplicated list of places where devicetree +# implementation files (like bindings, vendor prefixes, etc.) are +# found +# - DTS_ROOT_SYSTEM_INCLUDE_DIRS: set to "-isystem PATH1 -isystem PATH2 ...", +# with one path per potential location where C preprocessor #includes +# may be found for devicetree files +# +# Required variables: +# None. +# +# Optional variables: +# - APPLICATION_SOURCE_DIR: path to app (added to DTS_ROOT) +# - BOARD_DIR: directory containing the board definition (added to DTS_ROOT) +# - DTS_ROOT: initial contents may be populated here +# - ZEPHYR_BASE: path to zephyr repository (added to DTS_ROOT) +# - SHIELD_DIRS: paths to shield definitions (added to DTS_ROOT) + +# Using a function avoids polluting the parent scope unnecessarily. +function(pre_dt_module_run) + # Convert relative paths to absolute paths relative to the application + # source directory. + zephyr_file(APPLICATION_ROOT DTS_ROOT) + + # DTS_ROOT always includes the application directory, the board + # directory, shield directories, and ZEPHYR_BASE. + list(APPEND + DTS_ROOT + ${APPLICATION_SOURCE_DIR} + ${BOARD_DIR} + ${SHIELD_DIRS} + ${ZEPHYR_BASE} + ) + + # Convert the directories in DTS_ROOT to absolute paths without + # symlinks. + # + # DTS directories can come from multiple places. Some places, like a + # user's CMakeLists.txt can preserve symbolic links. Others, like + # scripts/zephyr_module.py --settings-out resolve them. + unset(real_dts_root) + foreach(dts_dir ${DTS_ROOT}) + file(REAL_PATH ${dts_dir} real_dts_dir) + list(APPEND real_dts_root ${real_dts_dir}) + endforeach() + set(DTS_ROOT ${real_dts_root}) + + # Finalize DTS_ROOT. + list(REMOVE_DUPLICATES DTS_ROOT) + + # Finalize DTS_ROOT_SYSTEM_INCLUDE_DIRS. + set(DTS_ROOT_SYSTEM_INCLUDE_DIRS) + foreach(dts_root ${DTS_ROOT}) + foreach(dts_root_path + include + include/zephyr + dts/common + dts/${ARCH} + dts + ) + get_filename_component(full_path ${dts_root}/${dts_root_path} REALPATH) + if(EXISTS ${full_path}) + list(APPEND + DTS_ROOT_SYSTEM_INCLUDE_DIRS + -isystem ${full_path} + ) + endif() + endforeach() + endforeach() + + # Set output variables. + set(DTS_ROOT ${DTS_ROOT} PARENT_SCOPE) + set(DTS_ROOT_SYSTEM_INCLUDE_DIRS ${DTS_ROOT_SYSTEM_INCLUDE_DIRS} PARENT_SCOPE) +endfunction() + +pre_dt_module_run() From b26f87ef85b5521d89e3cabb9b5e6a5041f02924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Bol=C3=ADvar?= Date: Tue, 28 Feb 2023 12:31:57 -0800 Subject: [PATCH 4/6] cmake: modules: dts: document outcome MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the build system was being split up into modules under cmake/modules, most of the resulting cmake modules had their inputs and outputs documented in top-of-file comments. The dts module is an exception, which makes it harder to use since its contracts aren't defined. Fix this by adding a contract. Signed-off-by: Martí Bolívar --- cmake/modules/dts.cmake | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/cmake/modules/dts.cmake b/cmake/modules/dts.cmake index 813d12102355..9dd138ad1575 100644 --- a/cmake/modules/dts.cmake +++ b/cmake/modules/dts.cmake @@ -39,6 +39,53 @@ find_package(Dtc 1.4.6) # files in scripts/dts to make all this work. We also optionally will # run the dtc tool if it is found, in order to catch any additional # warnings or errors it generates. +# +# Outcome: +# +# 1. The following has happened: +# +# - The pre_dt module has been included; refer to its outcome +# section for more information on the consequences +# - DTS_SOURCE: set to the path to the devicetree file which +# was used, if one was provided or found +# - ${BINARY_DIR_INCLUDE_GENERATED}/devicetree_generated.h exists +# +# 2. The following has happened if a devicetree was found and +# no errors occurred: +# +# - CACHED_DTS_ROOT_BINDINGS is set in the cache to the +# value of DTS_ROOT_BINDINGS +# - DTS_ROOT_BINDINGS is set to a ;-list of locations where DT +# bindings were found +# - ${PROJECT_BINARY_DIR}/zephyr.dts exists +# - ${PROJECT_BINARY_DIR}/edt.pickle exists +# - ${KCONFIG_BINARY_DIR}/Kconfig.dts exists +# - the build system will be regenerated if any devicetree files +# used in this build change, including transitive includes +# - the devicetree extensions in the extensions.cmake module +# will be ready for use in other CMake list files that run +# after this module +# +# Required variables: +# - BINARY_DIR_INCLUDE_GENERATED: where to put generated include files +# - KCONFIG_BINARY_DIR: where to put generated Kconfig files +# +# Optional variables: +# - BOARD: board name to use when looking for DTS_SOURCE +# - BOARD_DIR: board directory to use when looking for DTS_SOURCE +# - BOARD_REVISION_STRING: used when looking for a board revision's +# devicetree overlay file in BOARD_DIR +# - EXTRA_DTC_FLAGS: list of extra command line options to pass to +# dtc when using it to check for additional errors and warnings; +# invalid flags are automatically filtered out of the list +# - DTS_EXTRA_CPPFLAGS: extra command line options to pass to the +# C preprocessor when generating the devicetree from DTS_SOURCE +# - DTS_SOURCE: the devicetree source file to use may be pre-set +# with this variable; otherwise, it defaults to +# ${BOARD_DIR}/${BOARD.dts} +# +# Variables set by this module and not mentioned above are for internal +# use only, and may be removed, renamed, or re-purposed without prior notice. # The directory containing devicetree related scripts. set(DT_SCRIPTS ${ZEPHYR_BASE}/scripts/dts) From 8a4397f225290c91ce2431fe5843664f54741e88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Bol=C3=ADvar?= Date: Fri, 24 Feb 2023 14:40:44 -0800 Subject: [PATCH 5/6] cmake: modules: dts: extract overlay helper function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This list processing procedure will be useful elsewhere, so prep for not repeating ourselves. Put it in a new zephyr_list() function whose signature has room to grow if we keep adding list processing extensions. Signed-off-by: Martí Bolívar --- cmake/modules/dts.cmake | 9 ++---- cmake/modules/extensions.cmake | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/cmake/modules/dts.cmake b/cmake/modules/dts.cmake index 9dd138ad1575..cd2c2be0718d 100644 --- a/cmake/modules/dts.cmake +++ b/cmake/modules/dts.cmake @@ -150,13 +150,8 @@ set(dts_files ) if(DTC_OVERLAY_FILE) - # Convert from space-separated files into file list - string(CONFIGURE "${DTC_OVERLAY_FILE}" DTC_OVERLAY_FILE_EXPANDED) - string(REPLACE " " ";" DTC_OVERLAY_FILE_RAW_LIST "${DTC_OVERLAY_FILE_EXPANDED}") - foreach(file ${DTC_OVERLAY_FILE_RAW_LIST}) - file(TO_CMAKE_PATH "${file}" cmake_path_file) - list(APPEND DTC_OVERLAY_FILE_AS_LIST ${cmake_path_file}) - endforeach() + zephyr_list(TRANSFORM DTC_OVERLAY_FILE NORMALIZE_PATHS + OUTPUT_VARIABLE DTC_OVERLAY_FILE_AS_LIST) list(APPEND dts_files ${DTC_OVERLAY_FILE_AS_LIST} diff --git a/cmake/modules/extensions.cmake b/cmake/modules/extensions.cmake index 77d852cae66e..bc386c2592ab 100644 --- a/cmake/modules/extensions.cmake +++ b/cmake/modules/extensions.cmake @@ -2410,6 +2410,57 @@ function(zephyr_string) set(${return_arg} ${work_string} PARENT_SCOPE) endfunction() +# Usage: +# zephyr_list(TRANSFORM +# [OUTPUT_VARIABLE : This currently must be NORMALIZE_PATHS. This action +# converts the argument list to a ;-list with +# CMake path names, after passing its contents through +# a configure_file() transformation. The input list +# may be whitespace- or semicolon-separated. +# +# OUTPUT_VARIABLE: the result is normally stored in place, but +# an alternative variable to store the result +# can be provided with this. +function(zephyr_list transform list_var action) + # Parse arguments. + if(NOT "${transform}" STREQUAL "TRANSFORM") + message(FATAL_ERROR "the first argument must be TRANSFORM") + endif() + if(NOT "${action}" STREQUAL "NORMALIZE_PATHS") + message(FATAL_ERROR "the third argument must be NORMALIZE_PATHS") + endif() + set(single_args OUTPUT_VARIABLE) + cmake_parse_arguments(ZEPHYR_LIST "" "${single_args}" "" ${ARGN}) + if(DEFINED ZEPHYR_LIST_OUTPUT_VARIABLE) + set(out_var ${ZEPHYR_LIST_OUTPUT_VARIABLE}) + else() + set(out_var ${list_var}) + endif() + set(input ${${list_var}}) + + # Perform the transformation. + set(ret) + string(CONFIGURE "${input}" input_expanded) + string(REPLACE " " ";" input_raw_list "${input_expanded}") + foreach(file ${input_raw_list}) + file(TO_CMAKE_PATH "${file}" cmake_path_file) + list(APPEND ret ${cmake_path_file}) + endforeach() + + set(${out_var} ${ret} PARENT_SCOPE) +endfunction() + # Usage: # zephyr_get() # zephyr_get( SYSBUILD [LOCAL|GLOBAL]) From a089d60ef1fdcea2371f76f9eaed5532c9e71cf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Bol=C3=ADvar?= Date: Tue, 28 Feb 2023 13:07:46 -0800 Subject: [PATCH 6/6] cmake: modules: dts: extract preprocessing helper extension MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tighten up the interface boundaries by adding an extension function that separates *how* we preprocess the DTS from *what* DTS files we are preprocessing. This involves a functional change to the pre_dt module. This is useful cleanup but is also groundwork for relying on this helper function when adding system devicetree support. Signed-off-by: Martí Bolívar --- cmake/modules/dts.cmake | 38 +++++------- cmake/modules/extensions.cmake | 102 +++++++++++++++++++++++++++++++++ cmake/modules/pre_dt.cmake | 7 +-- 3 files changed, 117 insertions(+), 30 deletions(-) diff --git a/cmake/modules/dts.cmake b/cmake/modules/dts.cmake index cd2c2be0718d..42413ceca73d 100644 --- a/cmake/modules/dts.cmake +++ b/cmake/modules/dts.cmake @@ -159,11 +159,7 @@ if(DTC_OVERLAY_FILE) endif() set(i 0) -unset(DTC_INCLUDE_FLAG_FOR_DTS) foreach(dts_file ${dts_files}) - list(APPEND DTC_INCLUDE_FLAG_FOR_DTS - -include ${dts_file}) - if(i EQUAL 0) message(STATUS "Found BOARD.dts: ${dts_file}") else() @@ -203,29 +199,21 @@ set(CACHED_DTS_ROOT_BINDINGS ${DTS_ROOT_BINDINGS} CACHE INTERNAL # regeneration of devicetree_generated.h on every configure. How # challenging is this? Can we cache the dts dependencies? -# Run the preprocessor on the DTS input files. We are leaving -# linemarker directives enabled on purpose. This tells dtlib where -# each line actually came from, which improves error reporting. -execute_process( - COMMAND ${CMAKE_DTS_PREPROCESSOR} - -x assembler-with-cpp - -nostdinc - ${DTS_ROOT_SYSTEM_INCLUDE_DIRS} - ${DTC_INCLUDE_FLAG_FOR_DTS} # include the DTS source and overlays - ${NOSYSDEF_CFLAG} - -D__DTS__ - ${DTS_EXTRA_CPPFLAGS} - -E # Stop after preprocessing - -MD # Generate a dependency file as a side-effect - -MF ${DTS_DEPS} - -o ${DTS_POST_CPP} - ${ZEPHYR_BASE}/misc/empty_file.c +# Run the preprocessor on the DTS input files. +if(DEFINED CMAKE_DTS_PREPROCESSOR) + set(dts_preprocessor ${CMAKE_DTS_PREPROCESSOR}) +else() + set(dts_preprocessor ${CMAKE_C_COMPILER}) +endif() +zephyr_dt_preprocess( + CPP ${dts_preprocessor} + SOURCE_FILES ${dts_files} + OUT_FILE ${DTS_POST_CPP} + DEPS_FILE ${DTS_DEPS} + EXTRA_CPPFLAGS ${DTS_EXTRA_CPPFLAGS} + INCLUDE_DIRECTORIES ${DTS_ROOT_SYSTEM_INCLUDE_DIRS} WORKING_DIRECTORY ${APPLICATION_SOURCE_DIR} - RESULT_VARIABLE ret ) -if(NOT "${ret}" STREQUAL "0") - message(FATAL_ERROR "command failed with return code: ${ret}") -endif() # # Make sure we re-run CMake if any devicetree sources or transitive diff --git a/cmake/modules/extensions.cmake b/cmake/modules/extensions.cmake index bc386c2592ab..1261aa935faa 100644 --- a/cmake/modules/extensions.cmake +++ b/cmake/modules/extensions.cmake @@ -30,6 +30,7 @@ include(CheckCXXCompilerFlag) # 4. Devicetree extensions # 4.1 dt_* # 4.2. *_if_dt_node +# 4.3 zephyr_dt_* # 5. Zephyr linker functions # 5.1. zephyr_linker* # 6 Function helper macros @@ -3477,6 +3478,107 @@ function(target_sources_if_dt_node path target scope item) endif() endfunction() +######################################################## +# 4.3 zephyr_dt_* +# +# The following methods are common code for dealing +# with devicetree related files in CMake. +# +# Note that functions related to accessing the +# *contents* of the devicetree belong in section 4.1. +# This section is just for DT file processing at +# configuration time. +######################################################## + +# Usage: +# zephyr_dt_preprocess(CPP +# SOURCE_FILES +# OUT_FILE +# [DEPS_FILE ] +# [EXTRA_CPPFLAGS ] +# [INCLUDE_DIRECTORIES ] +# [WORKING_DIRECTORY ] +# +# Preprocess one or more devicetree source files. The preprocessor +# symbol __DTS__ will be defined. If the preprocessor command fails, a +# fatal error occurs. CMAKE_DTS_PREPROCESSOR is used as the +# preprocessor. +# +# Mandatory arguments: +# +# CPP : path to C preprocessor +# +# SOURCE_FILES : The source files to run the preprocessor on. +# These will, in effect, be concatenated in order +# and used as the preprocessor input. +# +# OUT_FILE : Where to store the preprocessor output. +# +# Optional arguments: +# +# DEPS_FILE : If set, generate a dependency file here. +# +# EXTRA_CPPFLAGS : Additional flags to pass the preprocessor. +# +# INCLUDE_DIRECTORIES : Additional directories containing #included +# files. +# +# WORKING_DIRECTORY : where to run the preprocessor. +function(zephyr_dt_preprocess) + set(req_single_args "CPP;OUT_FILE") + set(single_args "DEPS_FILE;WORKING_DIRECTORY") + set(multi_args "SOURCE_FILES;EXTRA_CPPFLAGS;INCLUDE_DIRECTORIES") + cmake_parse_arguments(DT_PREPROCESS "" "${req_single_args};${single_args}" "${multi_args}" ${ARGN}) + + foreach(arg ${req_single_args} SOURCE_FILES) + if(NOT DEFINED DT_PREPROCESS_${arg}) + message(FATAL_ERROR "dt_preprocess() missing required argument: ${arg}") + endif() + endforeach() + + set(include_opts) + foreach(dir ${DT_PREPROCESS_INCLUDE_DIRECTORIES}) + list(APPEND include_opts -isystem ${dir}) + endforeach() + + set(source_opts) + foreach(file ${DT_PREPROCESS_SOURCE_FILES}) + list(APPEND source_opts -include ${file}) + endforeach() + + set(deps_opts) + if(DEFINED DT_PREPROCESS_DEPS_FILE) + list(APPEND deps_opts -MD -MF ${DT_PREPROCESS_DEPS_FILE}) + endif() + + set(workdir_opts) + if(DEFINED DT_PREPROCESS_WORKING_DIRECTORY) + list(APPEND workdir_opts WORKING_DIRECTORY ${DT_PREPROCESS_WORKING_DIRECTORY}) + endif() + + # We are leaving linemarker directives enabled on purpose. This tells + # dtlib where each line actually came from, which improves error + # reporting. + set(preprocess_cmd ${DT_PREPROCESS_CPP} + -x assembler-with-cpp + -nostdinc + ${include_opts} + ${source_opts} + ${NOSYSDEF_CFLAG} + -D__DTS__ + ${DT_PREPROCESS_EXTRA_CPPFLAGS} + -E # Stop after preprocessing + ${deps_opts} + -o ${DT_PREPROCESS_OUT_FILE} + ${ZEPHYR_BASE}/misc/empty_file.c + ${workdir_opts}) + + execute_process(COMMAND ${preprocess_cmd} RESULT_VARIABLE ret) + if(NOT "${ret}" STREQUAL "0") + message(FATAL_ERROR "failed to preprocess devicetree files (error code ${ret}): ${DT_PREPROCESS_SOURCE_FILES}") + endif() +endfunction() + ######################################################## # 5. Zephyr linker functions ######################################################## diff --git a/cmake/modules/pre_dt.cmake b/cmake/modules/pre_dt.cmake index 38ce53d2b904..b19fd7f7fb57 100644 --- a/cmake/modules/pre_dt.cmake +++ b/cmake/modules/pre_dt.cmake @@ -16,7 +16,7 @@ include(extensions) # - DTS_ROOT: a deduplicated list of places where devicetree # implementation files (like bindings, vendor prefixes, etc.) are # found -# - DTS_ROOT_SYSTEM_INCLUDE_DIRS: set to "-isystem PATH1 -isystem PATH2 ...", +# - DTS_ROOT_SYSTEM_INCLUDE_DIRS: set to "PATH1 PATH2 ...", # with one path per potential location where C preprocessor #includes # may be found for devicetree files # @@ -74,10 +74,7 @@ function(pre_dt_module_run) ) get_filename_component(full_path ${dts_root}/${dts_root_path} REALPATH) if(EXISTS ${full_path}) - list(APPEND - DTS_ROOT_SYSTEM_INCLUDE_DIRS - -isystem ${full_path} - ) + list(APPEND DTS_ROOT_SYSTEM_INCLUDE_DIRS ${full_path}) endif() endforeach() endforeach()