1# Copyright (c) 2023 Nordic Semiconductor
2#
3# SPDX-License-Identifier: Apache-2.0
4
5# Usage:
6#   native_simulator_set_final_executable(<final_image>)
7#
8# When building for a native_simulator based target (including bsim targets),
9# this function adds an extra build target which will copy the executable produced by
10# `<final_image>` to the top level, as zephyr/zephyr.exe
11#
12# This final image is expected to have been set to assemble other dependent images into
13# itself if necessary, by calling native_simulator_set_child_images()
14# This will allow other tools, like twister, or the bsim test scripts, as well as users to find
15# this final executable in the same place as for non-sysbuild builds.
16#
17function(native_simulator_set_final_executable final_image)
18  if(("${BOARD}" MATCHES "native") OR ("${BOARD}" MATCHES "bsim"))
19    add_custom_target(final_executable
20      ALL
21      COMMAND
22      ${CMAKE_COMMAND} -E copy
23      ${CMAKE_BINARY_DIR}/${final_image}/zephyr/zephyr.exe
24      ${CMAKE_BINARY_DIR}/zephyr/zephyr.exe
25      DEPENDS ${final_image}
26    )
27  endif()
28endfunction()
29
30# Usage:
31#   native_simulator_set_child_images(<final_image> <child_image>)
32#
33# When building for a native_simulator based target (including bsim targets),
34# this function sets a `<child_image>` as dependencies of `<final_image>`
35# and configures the final image to assemble the child images into its final executable.
36#
37function(native_simulator_set_child_images final_image child_image)
38  if(("${BOARD}" MATCHES "native") OR ("${BOARD}" MATCHES "bsim"))
39    add_dependencies(${final_image} ${child_image})
40
41    set(CHILD_LIBRARY_PATH ${CMAKE_BINARY_DIR}/${child_image}/zephyr/zephyr.elf)
42    set_property(TARGET ${final_image} APPEND_STRING PROPERTY CONFIG
43      "CONFIG_NATIVE_SIMULATOR_EXTRA_IMAGE_PATHS=\"${CHILD_LIBRARY_PATH}\"\n"
44    )
45  endif()
46endfunction()
47
48# Usage:
49#   native_simulator_set_primary_mcu_index(<image> [<image2> ...])
50#
51# Propagate the SB_CONFIG_NATIVE_SIMULATOR_PRIMARY_MCU_INDEX setting,
52# if it is set, to each given image CONFIG_NATIVE_SIMULATOR_PRIMARY_MCU_INDEX
53#
54function(native_simulator_set_primary_mcu_index)
55  if (NOT ("${SB_CONFIG_NATIVE_SIMULATOR_PRIMARY_MCU_INDEX}" STREQUAL ""))
56    foreach(arg IN LISTS ARGV)
57      set_property(TARGET ${arg} APPEND_STRING PROPERTY CONFIG
58        "CONFIG_NATIVE_SIMULATOR_PRIMARY_MCU_INDEX=${SB_CONFIG_NATIVE_SIMULATOR_PRIMARY_MCU_INDEX}\n"
59      )
60    endforeach()
61  endif()
62endfunction()
63