1# Copyright (c) 2021-2023 Nordic Semiconductor
2#
3# SPDX-License-Identifier: Apache-2.0
4
5cmake_minimum_required(VERSION 3.20)
6
7if(NOT DEFINED APP_DIR)
8  message(FATAL_ERROR "No main application specified")
9endif()
10
11# This will update the APP_DIR cache variable to PATH type and apply a comment.
12# If APP_DIR is a relative path, then CMake will adjust to absolute path based
13# on current working dir.
14set(APP_DIR ${APP_DIR} CACHE PATH "Main Application Source Directory")
15
16# Add sysbuild/cmake/modules to CMAKE_MODULE_PATH which allows us to integrate
17# sysbuild CMake modules with general Zephyr CMake modules.
18list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake/modules)
19# List of Zephyr and sysbuild CMake modules we need for sysbuild.
20# Note: sysbuild_kconfig will internally load kconfig CMake module.
21set(zephyr_modules extensions sysbuild_extensions python west root zephyr_module boards shields sysbuild_kconfig)
22
23find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE} COMPONENTS ${zephyr_modules})
24
25project(sysbuild LANGUAGES)
26
27# Global list of images enabled in this multi image build system.
28set(IMAGES)
29
30get_filename_component(APP_DIR  ${APP_DIR} ABSOLUTE)
31get_filename_component(app_name ${APP_DIR} NAME)
32
33# Include zephyr modules generated sysbuild CMake file.
34foreach(SYSBUILD_CURRENT_MODULE_NAME ${SYSBUILD_MODULE_NAMES})
35  # Note the second, binary_dir parameter requires the added
36  # subdirectory to have its own, local cmake target(s). If not then
37  # this binary_dir is created but stays empty. Object files land in
38  # the main binary dir instead.
39  # https://cmake.org/pipermail/cmake/2019-June/069547.html
40  zephyr_string(SANITIZE TOUPPER MODULE_NAME_UPPER ${SYSBUILD_CURRENT_MODULE_NAME})
41  if(NOT ${SYSBUILD_${MODULE_NAME_UPPER}_CMAKE_DIR} STREQUAL "")
42    set(SYSBUILD_CURRENT_MODULE_DIR ${SYSBUILD_${MODULE_NAME_UPPER}_MODULE_DIR})
43    set(SYSBUILD_CURRENT_CMAKE_DIR ${SYSBUILD_${MODULE_NAME_UPPER}_CMAKE_DIR})
44    add_subdirectory(${SYSBUILD_CURRENT_CMAKE_DIR} ${CMAKE_BINARY_DIR}/modules/${SYSBUILD_CURRENT_MODULE_NAME})
45  endif()
46endforeach()
47# Done processing modules, clear SYSBUILD_CURRENT_MODULE_DIR and SYSBUILD_CURRENT_CMAKE_DIR.
48set(SYSBUILD_CURRENT_MODULE_DIR)
49set(SYSBUILD_CURRENT_CMAKE_DIR)
50
51# This adds the primary application to the build.
52ExternalZephyrProject_Add(
53  APPLICATION ${app_name}
54  SOURCE_DIR ${APP_DIR}
55  APP_TYPE MAIN
56)
57list(APPEND IMAGES "${app_name}")
58set(DEFAULT_IMAGE "${app_name}")
59
60add_subdirectory(bootloader)
61
62# This allows for board and app specific images to be included.
63include(${BOARD_DIR}/sysbuild.cmake OPTIONAL)
64
65# This allows image specific sysbuild.cmake to be processed.
66list(LENGTH IMAGES images_length)
67while(NOT "${images_length}" EQUAL "${processed_length}")
68  foreach(image ${IMAGES})
69    if(NOT image IN_LIST images_sysbuild_processed)
70      ExternalProject_Get_property(${image} SOURCE_DIR)
71      include(${SOURCE_DIR}/sysbuild.cmake OPTIONAL)
72      list(APPEND images_sysbuild_processed ${image})
73    endif()
74  endforeach()
75
76  list(LENGTH IMAGES images_length)
77  list(LENGTH images_sysbuild_processed processed_length_new)
78
79  # Check for any duplicate entries in image names to prevent an infinite loop
80  if("${processed_length_new}" EQUAL "${processed_length}")
81    # Image length was different than processed length, but no new images are processed.
82    message(FATAL_ERROR "A duplicate image name was provided, image names must be unique.")
83  endif()
84  set(processed_length ${processed_length_new})
85endwhile()
86
87sysbuild_module_call(PRE_CMAKE MODULES ${SYSBUILD_MODULE_NAMES} IMAGES ${IMAGES})
88foreach(image ${IMAGES})
89  include(image_config.cmake)
90  ExternalZephyrProject_Cmake(APPLICATION ${image})
91endforeach()
92sysbuild_module_call(POST_CMAKE MODULES ${SYSBUILD_MODULE_NAMES} IMAGES ${IMAGES})
93
94sysbuild_module_call(PRE_DOMAINS MODULES ${SYSBUILD_MODULE_NAMES} IMAGES ${IMAGES})
95include(cmake/domains.cmake)
96sysbuild_module_call(POST_DOMAINS MODULES ${SYSBUILD_MODULE_NAMES} IMAGES ${IMAGES})
97