1# Copyright (c) 2022 Google LLC
2# SPDX-License-Identifier: Apache-2.0
3
4cmake_minimum_required(VERSION 3.20.0)
5include(ExternalProject)
6
7# Add the sources and set up the build for either unit testing or native_sim
8list(APPEND SOURCES src/main.cpp)
9if(BOARD STREQUAL unit_testing)
10  find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE})
11  set(target testbinary)
12  # Set the target binary for the 'core' external project. The path to this must match the one set
13  # below in ExternalProject_Add's CMAKE_INSTALL_PREFIX
14  add_compile_definitions(FAIL_TARGET_BINARY="${CMAKE_BINARY_DIR}/core/bin/testbinary")
15else()
16  find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
17  set(target app)
18  # Set the target binary for the 'core' external project. The path to this must match the one set
19  # below in ExternalProject_Add's CMAKE_INSTALL_PREFIX
20  add_compile_definitions(FAIL_TARGET_BINARY="${CMAKE_BINARY_DIR}/core/bin/zephyr.exe")
21endif()
22
23# Create the project and set the sources for the target
24project(fail)
25target_sources(${target} PRIVATE ${SOURCES})
26
27# Find which CONFIG_ZTEST_FAIL_TEST_* choice was set so we can pass it to the external project
28# Once we find the config, we'll need to prepend a '-D' and append '=y' so we can pass it to the
29# 'core' project as a cmake argument.
30get_cmake_property(_vars VARIABLES)
31string(REGEX MATCHALL "(^|;)CONFIG_ZTEST_FAIL_TEST_[A-Za-z0-9_]+" fail_test_config "${_vars}")
32list(FILTER fail_test_config EXCLUDE REGEX "^$")
33list(TRANSFORM fail_test_config PREPEND "-D")
34list(TRANSFORM fail_test_config APPEND "=y")
35string(REPLACE ";" " " fail_test_config "${fail_test_config}")
36
37# Add the 'core' external project which will mirror the configs of this project.
38ExternalProject_Add(core
39    SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/core
40    CMAKE_ARGS
41      -DBOARD:STRING=${BOARD}
42      -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/core
43      ${fail_test_config}
44)
45add_dependencies(${target} core)
46