1# Copyright (c) 2021 Nordic Semiconductor
2#
3# SPDX-License-Identifier: Apache-2.0
4
5set(EXTRA_KCONFIG_TARGET_COMMAND_FOR_sysbuild_menuconfig
6  ${ZEPHYR_BASE}/scripts/kconfig/menuconfig.py
7)
8
9set(EXTRA_KCONFIG_TARGET_COMMAND_FOR_sysbuild_guiconfig
10  ${ZEPHYR_BASE}/scripts/kconfig/guiconfig.py
11)
12
13set(KCONFIG_TARGETS sysbuild_menuconfig sysbuild_guiconfig)
14foreach(extra_target ${EXTRA_KCONFIG_TARGETS})
15  set(EXTRA_KCONFIG_TARGET_COMMAND_FOR_sysbuild_${extra_target}
16      "${EXTRA_KCONFIG_TARGET_COMMAND_FOR_${extra_target}}"
17  )
18endforeach()
19list(TRANSFORM EXTRA_KCONFIG_TARGETS PREPEND "sysbuild_")
20
21zephyr_get(APPLICATION_CONFIG_DIR)
22if(DEFINED APPLICATION_CONFIG_DIR)
23  zephyr_file(APPLICATION_ROOT APPLICATION_CONFIG_DIR BASE_DIR ${APP_DIR})
24
25  # Sysbuild must add a locally defined APPLICATION_CONFIG_DIR in sysbuild/CMakeLists.txt
26  # to the cache in order for the setting to propagate to images.
27  set(APPLICATION_CONFIG_DIR ${APPLICATION_CONFIG_DIR} CACHE PATH
28      "Sysbuild adjusted APPLICATION_CONFIG_DIR" FORCE
29  )
30endif()
31
32zephyr_get(SB_APPLICATION_CONFIG_DIR)
33if(DEFINED SB_APPLICATION_CONFIG_DIR)
34  zephyr_file(APPLICATION_ROOT SB_APPLICATION_CONFIG_DIR BASE_DIR ${APP_DIR})
35  set(APPLICATION_CONFIG_DIR ${SB_APPLICATION_CONFIG_DIR})
36endif()
37set_ifndef(APPLICATION_CONFIG_DIR ${APP_DIR})
38string(CONFIGURE ${APPLICATION_CONFIG_DIR} APPLICATION_CONFIG_DIR)
39
40if(DEFINED SB_CONF_FILE)
41  # SB_CONF_FILE already set so nothing to do.
42elseif(DEFINED ENV{SB_CONF_FILE})
43  set(SB_CONF_FILE $ENV{SB_CONF_FILE})
44else()
45  # sysbuild.conf is an optional file, because sysbuild is an opt-in feature.
46  zephyr_file(CONF_FILES ${APPLICATION_CONFIG_DIR} KCONF SB_CONF_FILE NAMES "sysbuild.conf" SUFFIX ${FILE_SUFFIX})
47endif()
48
49if(NOT DEFINED SB_EXTRA_CONF_FILE AND DEFINED SB_OVERLAY_CONFIG)
50  set(SB_EXTRA_CONF_FILE ${SB_OVERLAY_CONFIG})
51endif()
52
53# Let SB_CONF_FILE and SB_EXTRA_CONF_FILE be relative to APP_DIR.
54# Either variable can be a list of paths, so we must make all of them absolute.
55foreach(conf_file_var SB_CONF_FILE SB_EXTRA_CONF_FILE)
56  if(DEFINED ${conf_file_var})
57    string(CONFIGURE "${${conf_file_var}}" conf_file_expanded)
58    set(${conf_file_var} "")
59    foreach(conf_file ${conf_file_expanded})
60      cmake_path(ABSOLUTE_PATH conf_file BASE_DIRECTORY ${APP_DIR})
61      list(APPEND ${conf_file_var} ${conf_file})
62    endforeach()
63  endif()
64endforeach()
65
66if(DEFINED SB_CONF_FILE AND NOT DEFINED CACHE{SB_CONF_FILE})
67  # We only want to set this in cache it has been defined and is not already there.
68  set(SB_CONF_FILE ${SB_CONF_FILE} CACHE STRING "If desired, you can build the application with \
69  SYSbuild configuration settings specified in an alternate .conf file using this parameter. \
70  These settings will override the settings in the application’s SYSBuild config file or its \
71  default .conf file. Multiple files may be listed, e.g. SB_CONF_FILE=\"sys1.conf sys2.conf\"")
72endif()
73
74if(NOT DEFINED SB_CONF_FILE)
75  # If there is no SB_CONF_FILE, then use empty.conf to make kconfiglib happy.
76  # Not adding it to CMake cache ensures that a later created sysbuild.conf
77  # will be automatically detected.
78  set(SB_CONF_FILE ${CMAKE_CURRENT_BINARY_DIR}/empty.conf)
79endif()
80
81if(NOT EXISTS "${APPLICATION_CONFIG_DIR}")
82  message(FATAL_ERROR "${APPLICATION_CONFIG_DIR} (APPLICATION_CONFIG_DIR) doesn't exist. "
83          "Unable to lookup sysbuild.conf or other related sysbuild configuration files. "
84	  "Please ensure APPLICATION_CONFIG_DIR points to a valid location."
85  )
86endif()
87
88# Empty files to make kconfig.py happy.
89file(TOUCH ${CMAKE_CURRENT_BINARY_DIR}/empty.conf)
90set(APPLICATION_SOURCE_DIR ${sysbuild_toplevel_SOURCE_DIR})
91set(AUTOCONF_H             ${CMAKE_CURRENT_BINARY_DIR}/autoconf.h)
92set(CONF_FILE              ${SB_CONF_FILE})
93set(EXTRA_CONF_FILE        "${SB_EXTRA_CONF_FILE}")
94set(BOARD_DEFCONFIG        "${CMAKE_CURRENT_BINARY_DIR}/empty.conf")
95if(DEFINED BOARD_REVISION)
96  set(BOARD_REVISION_CONFIG "${CMAKE_CURRENT_BINARY_DIR}/empty.conf")
97endif()
98
99# Unset shield configuration files if set to prevent including in sysbuild
100set(shield_conf_files)
101
102list(APPEND ZEPHYR_KCONFIG_MODULES_DIR BOARD=${BOARD})
103set(KCONFIG_NAMESPACE SB_CONFIG)
104
105if(EXISTS ${APP_DIR}/Kconfig.sysbuild)
106  set(KCONFIG_ROOT ${APP_DIR}/Kconfig.sysbuild)
107endif()
108
109# Apply any EXTRA_CONF_FILE variables from snippets
110zephyr_scope_exists(scope_defined snippets)
111if(scope_defined)
112  zephyr_get_scoped(snippets_EXTRA_CONF_FILE snippets SB_EXTRA_CONF_FILE)
113  list(APPEND EXTRA_CONF_FILE ${snippets_EXTRA_CONF_FILE})
114endif()
115
116include(${ZEPHYR_BASE}/cmake/modules/kconfig.cmake)
117set(CONF_FILE)
118set(EXTRA_CONF_FILE)
119set(SB_EXTRA_CONF_FILE)
120