1# SPDX-License-Identifier: Apache-2.0
2#
3# Copyright (c) 2022, Nordic Semiconductor ASA
4
5# Setup basic settings for a Zephyr project.
6#
7# Basic settings are:
8# - sysbuild defined configuration settings
9#
10# Details for sysbuild settings:
11#
12# Sysbuild is a higher level build system used by Zephyr.
13# Sysbuild allows users to build multiple samples for a given system.
14#
15# For this to work, sysbuild manages other Zephyr CMake build systems by setting
16# dedicated build variables.
17# This CMake modules loads the sysbuild cache variables as target properties on
18# a sysbuild_cache target.
19#
20# This ensures that qoutes and lists are correctly preserved.
21
22include_guard(GLOBAL)
23
24if(SYSBUILD)
25  add_custom_target(sysbuild_cache)
26  file(STRINGS "${SYSBUILD_CACHE}" sysbuild_cache_strings ENCODING UTF-8)
27  foreach(str ${sysbuild_cache_strings})
28    # Using a regex for matching whole 'VAR_NAME:TYPE=VALUE' will strip semi-colons
29    # thus resulting in lists to become strings.
30    # Therefore we first fetch VAR_NAME and TYPE, and afterwards extract
31    # remaining of string into a value that populates the property.
32    # This method ensures that both quoted values and ;-separated list stays intact.
33    string(REGEX MATCH "([^:]*):([^=]*)=" variable_identifier ${str})
34    string(LENGTH ${variable_identifier} variable_identifier_length)
35    string(SUBSTRING "${str}" ${variable_identifier_length} -1 variable_value)
36    set_property(TARGET sysbuild_cache APPEND PROPERTY "SYSBUILD_CACHE:VARIABLES" "${CMAKE_MATCH_1}")
37    set_property(TARGET sysbuild_cache PROPERTY "${CMAKE_MATCH_1}:TYPE" "${CMAKE_MATCH_2}")
38    set_property(TARGET sysbuild_cache PROPERTY "${CMAKE_MATCH_1}" "${variable_value}")
39  endforeach()
40endif()
41