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# - APPLICATION_CONFIG_DIR: Root folder for application configuration
17#
18# If any of the above variables are already set when this CMake module is
19# loaded, then no changes to the variable will happen.
20#
21# Variables set by this module and not mentioned above are considered internal
22# use only and may be removed, renamed, or re-purposed without prior notice.
23
24include_guard(GLOBAL)
25
26include(extensions)
27
28zephyr_get(APPLICATION_CONFIG_DIR)
29if(DEFINED APPLICATION_CONFIG_DIR)
30  string(CONFIGURE ${APPLICATION_CONFIG_DIR} APPLICATION_CONFIG_DIR)
31  if(NOT IS_ABSOLUTE ${APPLICATION_CONFIG_DIR})
32    get_filename_component(APPLICATION_CONFIG_DIR ${APPLICATION_CONFIG_DIR} ABSOLUTE)
33  endif()
34else()
35  # Application config dir is not set, so we default to the  application
36  # source directory as configuration directory.
37  set(APPLICATION_CONFIG_DIR ${APPLICATION_SOURCE_DIR})
38endif()
39
40zephyr_get(CONF_FILE SYSBUILD LOCAL)
41if(DEFINED CONF_FILE)
42  # This ensures that CACHE{CONF_FILE} will be set correctly to current scope
43  # variable CONF_FILE. An already current scope variable will stay the same.
44  set(CONF_FILE ${CONF_FILE})
45
46  # CONF_FILE has either been specified on the cmake CLI or is already
47  # in the CMakeCache.txt. This has precedence over the environment
48  # variable CONF_FILE and the default prj.conf
49
50  # In order to support a `prj_<name>.conf pattern for auto inclusion of board
51  # overlays, then we must first ensure only a single conf file is provided.
52  string(CONFIGURE "${CONF_FILE}" CONF_FILE_EXPANDED)
53  string(REPLACE " " ";" CONF_FILE_AS_LIST "${CONF_FILE_EXPANDED}")
54  list(LENGTH CONF_FILE_AS_LIST CONF_FILE_LENGTH)
55  if(${CONF_FILE_LENGTH} EQUAL 1)
56    # Need the file name to look for match.
57    # Need path in order to check if it is absolute.
58    get_filename_component(CONF_FILE_NAME ${CONF_FILE} NAME)
59    if(${CONF_FILE_NAME} MATCHES "prj_(.*).conf")
60      set(CONF_FILE_BUILD_TYPE ${CMAKE_MATCH_1})
61      set(CONF_FILE_INCLUDE_FRAGMENTS true)
62    endif()
63  endif()
64elseif(CACHED_CONF_FILE)
65  # Cached conf file is present.
66  # That value has precedence over anything else than a new
67  # `cmake -DCONF_FILE=<file>` invocation.
68  set(CONF_FILE ${CACHED_CONF_FILE})
69elseif(EXISTS   ${APPLICATION_CONFIG_DIR}/prj_${BOARD}.conf)
70  set(CONF_FILE ${APPLICATION_CONFIG_DIR}/prj_${BOARD}.conf)
71  find_package(Deprecated COMPONENTS PRJ_BOARD)
72elseif(EXISTS   ${APPLICATION_CONFIG_DIR}/prj.conf)
73  set(CONF_FILE ${APPLICATION_CONFIG_DIR}/prj.conf)
74  set(CONF_FILE_INCLUDE_FRAGMENTS true)
75else()
76  message(FATAL_ERROR "No prj.conf file was found in the ${APPLICATION_CONFIG_DIR} folder, "
77                      "please read the Zephyr documentation on application development.")
78endif()
79
80if(CONF_FILE_INCLUDE_FRAGMENTS)
81  zephyr_file(CONF_FILES ${APPLICATION_CONFIG_DIR}/boards KCONF CONF_FILE BUILD ${CONF_FILE_BUILD_TYPE})
82endif()
83
84set(APPLICATION_CONFIG_DIR ${APPLICATION_CONFIG_DIR} CACHE INTERNAL "The application configuration folder")
85set(CACHED_CONF_FILE ${CONF_FILE} CACHE STRING "If desired, you can build the application using\
86the configuration settings specified in an alternate .conf file using this parameter. \
87These settings will override the settings in the application’s .config file or its default .conf file.\
88Multiple files may be listed, e.g. CONF_FILE=\"prj1.conf;prj2.conf\" \
89The CACHED_CONF_FILE is internal Zephyr variable used between CMake runs. \
90To change CONF_FILE, use the CONF_FILE variable.")
91unset(CONF_FILE CACHE)
92
93zephyr_file(CONF_FILES ${APPLICATION_CONFIG_DIR}/boards DTS APP_BOARD_DTS)
94
95# The CONF_FILE variable is now set to its final value.
96zephyr_boilerplate_watch(CONF_FILE)
97
98zephyr_get(DTC_OVERLAY_FILE SYSBUILD LOCAL)
99if(DTC_OVERLAY_FILE)
100  # DTC_OVERLAY_FILE has either been specified on the cmake CLI or is already
101  # in the CMakeCache.txt.
102elseif(APP_BOARD_DTS)
103  set(DTC_OVERLAY_FILE ${APP_BOARD_DTS})
104elseif(EXISTS          ${APPLICATION_CONFIG_DIR}/${BOARD}.overlay)
105  set(DTC_OVERLAY_FILE ${APPLICATION_CONFIG_DIR}/${BOARD}.overlay)
106elseif(EXISTS          ${APPLICATION_CONFIG_DIR}/app.overlay)
107  set(DTC_OVERLAY_FILE ${APPLICATION_CONFIG_DIR}/app.overlay)
108endif()
109
110set(DTC_OVERLAY_FILE ${DTC_OVERLAY_FILE} CACHE STRING "If desired, you can \
111build the application using the DT configuration settings specified in an \
112alternate .overlay file using this parameter. These settings will override the \
113settings in the board's .dts file. Multiple files may be listed, e.g. \
114DTC_OVERLAY_FILE=\"dts1.overlay dts2.overlay\"")
115
116# The DTC_OVERLAY_FILE variable is now set to its final value.
117zephyr_boilerplate_watch(DTC_OVERLAY_FILE)
118
119zephyr_get(EXTRA_CONF_FILE SYSBUILD LOCAL VAR EXTRA_CONF_FILE OVERLAY_CONFIG MERGE REVERSE)
120zephyr_get(EXTRA_DTC_OVERLAY_FILE SYSBUILD LOCAL MERGE REVERSE)
121