1# SPDX-License-Identifier: Apache-2.0
2#
3# Copyright (c) 2021, Nordic Semiconductor ASA
4
5# Zephyr build system configuration files.
6#
7# Locate the Kconfig and DT config files that are to be used.
8# Also, locate the appropriate application config directory.
9#
10# Outcome:
11# The following variables will be defined when this CMake module completes:
12#
13# - CONF_FILE:              List of Kconfig fragments
14# - EXTRA_CONF_FILE:        List of additional Kconfig fragments
15# - DTC_OVERLAY_FILE:       List of devicetree overlay files
16# - EXTRA_DTC_OVERLAY_FILE  List of additional devicetree overlay files
17# - DTS_EXTRA_CPPFLAGS      List of additional devicetree preprocessor defines
18# - APPLICATION_CONFIG_DIR: Root folder for application configuration
19#
20# If any of the above variables are already set when this CMake module is
21# loaded, then no changes to the variable will happen.
22#
23# Variables set by this module and not mentioned above are considered internal
24# use only and may be removed, renamed, or re-purposed without prior notice.
25
26include_guard(GLOBAL)
27
28include(extensions)
29
30# Merge in variables from other sources (e.g. sysbuild)
31zephyr_get(FILE_SUFFIX SYSBUILD GLOBAL)
32
33zephyr_get(APPLICATION_CONFIG_DIR SYSBUILD GLOBAL)
34zephyr_file(APPLICATION_ROOT APPLICATION_CONFIG_DIR)
35set_ifndef(APPLICATION_CONFIG_DIR ${APPLICATION_SOURCE_DIR})
36string(CONFIGURE ${APPLICATION_CONFIG_DIR} APPLICATION_CONFIG_DIR)
37
38zephyr_get(CONF_FILE SYSBUILD LOCAL)
39if(NOT DEFINED CONF_FILE)
40  zephyr_file(CONF_FILES ${APPLICATION_CONFIG_DIR} KCONF CONF_FILE NAMES "prj.conf" SUFFIX ${FILE_SUFFIX} REQUIRED)
41  zephyr_file(CONF_FILES ${APPLICATION_CONFIG_DIR}/socs KCONF CONF_FILE QUALIFIERS SUFFIX ${FILE_SUFFIX})
42  zephyr_file(CONF_FILES ${APPLICATION_CONFIG_DIR}/boards KCONF CONF_FILE SUFFIX ${FILE_SUFFIX})
43else()
44  string(CONFIGURE "${CONF_FILE}" CONF_FILE_EXPANDED)
45  string(REPLACE " " ";" CONF_FILE_AS_LIST "${CONF_FILE_EXPANDED}")
46  list(LENGTH CONF_FILE_AS_LIST CONF_FILE_LENGTH)
47  if(${CONF_FILE_LENGTH} EQUAL 1)
48    get_filename_component(CONF_FILE_NAME ${CONF_FILE} NAME)
49    if(${CONF_FILE_NAME} MATCHES "prj_(.*).conf")
50      set(CONF_FILE_BUILD_TYPE ${CMAKE_MATCH_1})
51      zephyr_file(CONF_FILES ${APPLICATION_CONFIG_DIR}/boards KCONF CONF_FILE
52                  BUILD ${CONF_FILE_BUILD_TYPE}
53      )
54      set(CONF_FILE_FORCE_CACHE FORCE)
55    endif()
56  endif()
57endif()
58
59set(APPLICATION_CONFIG_DIR ${APPLICATION_CONFIG_DIR} CACHE PATH "The application configuration folder" FORCE)
60set(CONF_FILE ${CONF_FILE} CACHE STRING "If desired, you can build the application using\
61the configuration settings specified in an alternate .conf file using this parameter. \
62These settings will override the settings in the application’s .config file or its default .conf file.\
63Multiple files may be listed, e.g. CONF_FILE=\"prj1.conf;prj2.conf\" \
64The CACHED_CONF_FILE is internal Zephyr variable used between CMake runs. \
65To change CONF_FILE, use the CONF_FILE variable." ${CONF_FILE_FORCE_CACHE})
66
67# The CONF_FILE variable is now set to its final value.
68zephyr_boilerplate_watch(CONF_FILE)
69
70zephyr_get(DTC_OVERLAY_FILE SYSBUILD LOCAL)
71
72# If DTC_OVERLAY_FILE is not set by the user, look for SoC and board-specific overlays
73# in the 'boards' and `soc` configuration subdirectories.
74if(NOT DEFINED DTC_OVERLAY_FILE)
75  zephyr_file(CONF_FILES ${APPLICATION_CONFIG_DIR}/socs DTS DTC_OVERLAY_FILE QUALIFIERS SUFFIX ${FILE_SUFFIX})
76  zephyr_file(CONF_FILES ${APPLICATION_CONFIG_DIR}/boards DTS DTC_OVERLAY_FILE SUFFIX ${FILE_SUFFIX})
77endif()
78
79# If still not found, search for other overlays in the configuration directory.
80if(NOT DEFINED DTC_OVERLAY_FILE)
81  zephyr_file(CONF_FILES ${APPLICATION_CONFIG_DIR} DTS DTC_OVERLAY_FILE)
82
83  if(NOT DEFINED DTC_OVERLAY_FILE)
84    zephyr_file(CONF_FILES ${APPLICATION_CONFIG_DIR} DTS DTC_OVERLAY_FILE
85                NAMES "app.overlay" SUFFIX ${FILE_SUFFIX}
86    )
87  endif()
88endif()
89
90set(DTC_OVERLAY_FILE ${DTC_OVERLAY_FILE} CACHE STRING "If desired, you can \
91build the application using the DT configuration settings specified in an \
92alternate .overlay file using this parameter. These settings will override the \
93settings in the board's .dts file. Multiple files may be listed, e.g. \
94DTC_OVERLAY_FILE=\"dts1.overlay dts2.overlay\"")
95
96# The DTC_OVERLAY_FILE variable is now set to its final value.
97zephyr_boilerplate_watch(DTC_OVERLAY_FILE)
98
99zephyr_get(EXTRA_CONF_FILE SYSBUILD LOCAL VAR EXTRA_CONF_FILE OVERLAY_CONFIG MERGE REVERSE)
100zephyr_get(EXTRA_DTC_OVERLAY_FILE SYSBUILD LOCAL MERGE REVERSE)
101zephyr_get(DTS_EXTRA_CPPFLAGS SYSBUILD LOCAL MERGE REVERSE)
102build_info(application source-dir VALUE ${APPLICATION_SOURCE_DIR})
103build_info(application configuration-dir VALUE ${APPLICATION_CONFIG_DIR})
104