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