From 9bb162d1d538d575beaa5f9083ae617ccd7081c9 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Thu, 16 Dec 2021 15:22:46 +0100 Subject: [PATCH] cmake: create a configuration_files.cmake CMake module The configuration_files.cmake CMake module remove boilerplate code and place it inside a dedicated configuration_files.cmake CMake module. This is part of a general CMake overhaul to allow better modularization and reuse of the Zephyr build system. Signed-off-by: Torsten Rasmussen --- cmake/app/boilerplate.cmake | 88 +------------------------- cmake/configuration_files.cmake | 108 ++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 87 deletions(-) create mode 100644 cmake/configuration_files.cmake diff --git a/cmake/app/boilerplate.cmake b/cmake/app/boilerplate.cmake index d13de44bb2..2747c51c38 100644 --- a/cmake/app/boilerplate.cmake +++ b/cmake/app/boilerplate.cmake @@ -184,90 +184,7 @@ zephyr_file(APPLICATION_ROOT ARCH_ROOT) include(${ZEPHYR_BASE}/cmake/boards.cmake) include(${ZEPHYR_BASE}/cmake/shields.cmake) include(${ZEPHYR_BASE}/cmake/arch.cmake) - -if(DEFINED APPLICATION_CONFIG_DIR) - string(CONFIGURE ${APPLICATION_CONFIG_DIR} APPLICATION_CONFIG_DIR) - if(NOT IS_ABSOLUTE ${APPLICATION_CONFIG_DIR}) - get_filename_component(APPLICATION_CONFIG_DIR ${APPLICATION_CONFIG_DIR} ABSOLUTE) - endif() -else() - # Application config dir is not set, so we default to the application - # source directory as configuration directory. - set(APPLICATION_CONFIG_DIR ${APPLICATION_SOURCE_DIR}) -endif() - -if(DEFINED CONF_FILE) - # This ensures that CACHE{CONF_FILE} will be set correctly to current scope - # variable CONF_FILE. An already current scope variable will stay the same. - set(CONF_FILE ${CONF_FILE}) - - # CONF_FILE has either been specified on the cmake CLI or is already - # in the CMakeCache.txt. This has precedence over the environment - # variable CONF_FILE and the default prj.conf - - # In order to support a `prj_.conf pattern for auto inclusion of board - # overlays, then we must first ensure only a single conf file is provided. - string(REPLACE " " ";" CONF_FILE_AS_LIST "${CONF_FILE}") - list(LENGTH CONF_FILE_AS_LIST CONF_FILE_LENGTH) - if(${CONF_FILE_LENGTH} EQUAL 1) - # Need the file name to look for match. - # Need path in order to check if it is absolute. - get_filename_component(CONF_FILE_NAME ${CONF_FILE} NAME) - if(${CONF_FILE_NAME} MATCHES "prj_(.*).conf") - set(CONF_FILE_BUILD_TYPE ${CMAKE_MATCH_1}) - set(CONF_FILE_INCLUDE_FRAGMENTS true) - endif() - endif() -elseif(CACHED_CONF_FILE) - # Cached conf file is present. - # That value has precedence over anything else than a new - # `cmake -DCONF_FILE=` invocation. - set(CONF_FILE ${CACHED_CONF_FILE}) -elseif(DEFINED ENV{CONF_FILE}) - set(CONF_FILE $ENV{CONF_FILE}) - -elseif(EXISTS ${APPLICATION_CONFIG_DIR}/prj_${BOARD}.conf) - set(CONF_FILE ${APPLICATION_CONFIG_DIR}/prj_${BOARD}.conf) - -elseif(EXISTS ${APPLICATION_CONFIG_DIR}/prj.conf) - set(CONF_FILE ${APPLICATION_CONFIG_DIR}/prj.conf) - set(CONF_FILE_INCLUDE_FRAGMENTS true) -endif() - -if(CONF_FILE_INCLUDE_FRAGMENTS) - zephyr_file(CONF_FILES ${APPLICATION_CONFIG_DIR}/boards KCONF CONF_FILE BUILD ${CONF_FILE_BUILD_TYPE}) -endif() - -set(APPLICATION_CONFIG_DIR ${APPLICATION_CONFIG_DIR} CACHE INTERNAL "The application configuration folder") -set(CACHED_CONF_FILE ${CONF_FILE} CACHE STRING "If desired, you can build the application using\ -the configuration settings specified in an alternate .conf file using this parameter. \ -These settings will override the settings in the application’s .config file or its default .conf file.\ -Multiple files may be listed, e.g. CONF_FILE=\"prj1.confi;prj2.conf\" \ -The CACHED_CONF_FILE is internal Zephyr variable used between CMake runs. \ -To change CONF_FILE, use the CONF_FILE variable.") -unset(CONF_FILE CACHE) - -zephyr_file(CONF_FILES ${APPLICATION_CONFIG_DIR}/boards DTS APP_BOARD_DTS) - -# The CONF_FILE variable is now set to its final value. -zephyr_boilerplate_watch(CONF_FILE) - -if(DTC_OVERLAY_FILE) - # DTC_OVERLAY_FILE has either been specified on the cmake CLI or is already - # in the CMakeCache.txt. -elseif(APP_BOARD_DTS) - set(DTC_OVERLAY_FILE ${APP_BOARD_DTS}) -elseif(EXISTS ${APPLICATION_CONFIG_DIR}/${BOARD}.overlay) - set(DTC_OVERLAY_FILE ${APPLICATION_CONFIG_DIR}/${BOARD}.overlay) -elseif(EXISTS ${APPLICATION_CONFIG_DIR}/app.overlay) - set(DTC_OVERLAY_FILE ${APPLICATION_CONFIG_DIR}/app.overlay) -endif() - -set(DTC_OVERLAY_FILE ${DTC_OVERLAY_FILE} CACHE STRING "If desired, you can \ -build the application using the DT configuration settings specified in an \ -alternate .overlay file using this parameter. These settings will override the \ -settings in the board's .dts file. Multiple files may be listed, e.g. \ -DTC_OVERLAY_FILE=\"dts1.overlay dts2.overlay\"") +include(${ZEPHYR_BASE}/cmake/configuration_files.cmake) include(${ZEPHYR_BASE}/cmake/user_cache.cmake) include(${ZEPHYR_BASE}/cmake/verify-toolchain.cmake) @@ -276,9 +193,6 @@ include(${ZEPHYR_BASE}/cmake/host-tools.cmake) # Include board specific device-tree flags before parsing. include(${BOARD_DIR}/pre_dt_board.cmake OPTIONAL) -# The DTC_OVERLAY_FILE variable is now set to its final value. -zephyr_boilerplate_watch(DTC_OVERLAY_FILE) - # DTS should be close to kconfig because CONFIG_ variables from # kconfig and dts should be available at the same time. # diff --git a/cmake/configuration_files.cmake b/cmake/configuration_files.cmake new file mode 100644 index 0000000000..69d6612a90 --- /dev/null +++ b/cmake/configuration_files.cmake @@ -0,0 +1,108 @@ +# SPDX-License-Identifier: Apache-2.0 +# +# Copyright (c) 2021, Nordic Semiconductor ASA + +# Zephyr build system configuration files. +# +# Locate the Kconfig and DT config files that are to be used. +# Also, locate the appropriate application config directory. +# +# Outcome: +# The following variables will be defined when this CMake module completes: +# +# - CONF_FILE: List of Kconfig fragments +# - DTC_OVERLAY_FILE: List of devicetree overlay files +# - APPLICATION_CONFIG_DIR: Root folder for application configuration +# +# If any of the above variables are already set when this CMake module is +# loaded, then no changes to the variable will happen. +# +# Variables set by this module and not mentioned above are considered internal +# use only and may be removed, renamed, or re-purposed without prior notice. + +if(DEFINED APPLICATION_CONFIG_DIR) + string(CONFIGURE ${APPLICATION_CONFIG_DIR} APPLICATION_CONFIG_DIR) + if(NOT IS_ABSOLUTE ${APPLICATION_CONFIG_DIR}) + get_filename_component(APPLICATION_CONFIG_DIR ${APPLICATION_CONFIG_DIR} ABSOLUTE) + endif() +else() + # Application config dir is not set, so we default to the application + # source directory as configuration directory. + set(APPLICATION_CONFIG_DIR ${APPLICATION_SOURCE_DIR}) +endif() + +if(DEFINED CONF_FILE) + # This ensures that CACHE{CONF_FILE} will be set correctly to current scope + # variable CONF_FILE. An already current scope variable will stay the same. + set(CONF_FILE ${CONF_FILE}) + + # CONF_FILE has either been specified on the cmake CLI or is already + # in the CMakeCache.txt. This has precedence over the environment + # variable CONF_FILE and the default prj.conf + + # In order to support a `prj_.conf pattern for auto inclusion of board + # overlays, then we must first ensure only a single conf file is provided. + string(REPLACE " " ";" CONF_FILE_AS_LIST "${CONF_FILE}") + list(LENGTH CONF_FILE_AS_LIST CONF_FILE_LENGTH) + if(${CONF_FILE_LENGTH} EQUAL 1) + # Need the file name to look for match. + # Need path in order to check if it is absolute. + get_filename_component(CONF_FILE_NAME ${CONF_FILE} NAME) + if(${CONF_FILE_NAME} MATCHES "prj_(.*).conf") + set(CONF_FILE_BUILD_TYPE ${CMAKE_MATCH_1}) + set(CONF_FILE_INCLUDE_FRAGMENTS true) + endif() + endif() +elseif(CACHED_CONF_FILE) + # Cached conf file is present. + # That value has precedence over anything else than a new + # `cmake -DCONF_FILE=` invocation. + set(CONF_FILE ${CACHED_CONF_FILE}) +elseif(DEFINED ENV{CONF_FILE}) + set(CONF_FILE $ENV{CONF_FILE}) + +elseif(EXISTS ${APPLICATION_CONFIG_DIR}/prj_${BOARD}.conf) + set(CONF_FILE ${APPLICATION_CONFIG_DIR}/prj_${BOARD}.conf) + +elseif(EXISTS ${APPLICATION_CONFIG_DIR}/prj.conf) + set(CONF_FILE ${APPLICATION_CONFIG_DIR}/prj.conf) + set(CONF_FILE_INCLUDE_FRAGMENTS true) +endif() + +if(CONF_FILE_INCLUDE_FRAGMENTS) + zephyr_file(CONF_FILES ${APPLICATION_CONFIG_DIR}/boards KCONF CONF_FILE BUILD ${CONF_FILE_BUILD_TYPE}) +endif() + +set(APPLICATION_CONFIG_DIR ${APPLICATION_CONFIG_DIR} CACHE INTERNAL "The application configuration folder") +set(CACHED_CONF_FILE ${CONF_FILE} CACHE STRING "If desired, you can build the application using\ +the configuration settings specified in an alternate .conf file using this parameter. \ +These settings will override the settings in the application’s .config file or its default .conf file.\ +Multiple files may be listed, e.g. CONF_FILE=\"prj1.confi;prj2.conf\" \ +The CACHED_CONF_FILE is internal Zephyr variable used between CMake runs. \ +To change CONF_FILE, use the CONF_FILE variable.") +unset(CONF_FILE CACHE) + +zephyr_file(CONF_FILES ${APPLICATION_CONFIG_DIR}/boards DTS APP_BOARD_DTS) + +# The CONF_FILE variable is now set to its final value. +zephyr_boilerplate_watch(CONF_FILE) + +if(DTC_OVERLAY_FILE) + # DTC_OVERLAY_FILE has either been specified on the cmake CLI or is already + # in the CMakeCache.txt. +elseif(APP_BOARD_DTS) + set(DTC_OVERLAY_FILE ${APP_BOARD_DTS}) +elseif(EXISTS ${APPLICATION_CONFIG_DIR}/${BOARD}.overlay) + set(DTC_OVERLAY_FILE ${APPLICATION_CONFIG_DIR}/${BOARD}.overlay) +elseif(EXISTS ${APPLICATION_CONFIG_DIR}/app.overlay) + set(DTC_OVERLAY_FILE ${APPLICATION_CONFIG_DIR}/app.overlay) +endif() + +set(DTC_OVERLAY_FILE ${DTC_OVERLAY_FILE} CACHE STRING "If desired, you can \ +build the application using the DT configuration settings specified in an \ +alternate .overlay file using this parameter. These settings will override the \ +settings in the board's .dts file. Multiple files may be listed, e.g. \ +DTC_OVERLAY_FILE=\"dts1.overlay dts2.overlay\"") + +# The DTC_OVERLAY_FILE variable is now set to its final value. +zephyr_boilerplate_watch(DTC_OVERLAY_FILE)