1# SPDX-License-Identifier: Apache-2.0 2 3cmake_minimum_required(VERSION 3.20.0) 4 5include(extensions) 6include(west) 7include(yaml) 8include(root) 9include(zephyr_module) 10include(boards) 11include(hwm_v2) 12include(configuration_files) 13 14include(kconfig) 15include(arch_v2) 16include(soc_v2) 17 18find_package(TargetTools) 19 20enable_language(C CXX ASM) 21 22include(${ZEPHYR_BASE}/cmake/target_toolchain_flags.cmake) 23 24# Parameters: 25# SOURCES: list of source files, default main.c 26# INCLUDE: list of additional include paths relative to ZEPHYR_BASE 27 28foreach(extra_flags EXTRA_CPPFLAGS EXTRA_LDFLAGS EXTRA_CFLAGS EXTRA_CXXFLAGS EXTRA_AFLAGS) 29 list(LENGTH ${extra_flags} flags_length) 30 if(flags_length LESS_EQUAL 1) 31 # A length of zero means no argument. 32 # A length of one means a single argument or a space separated list was provided. 33 # In both cases, it is safe to do a separate_arguments on the argument. 34 separate_arguments(${extra_flags}_AS_LIST UNIX_COMMAND ${${extra_flags}}) 35 else() 36 # Already a proper list, no conversion needed. 37 set(${extra_flags}_AS_LIST "${${extra_flags}}") 38 endif() 39endforeach() 40 41set(ENV_ZEPHYR_BASE $ENV{ZEPHYR_BASE}) 42# This add support for old style boilerplate include. 43if((NOT DEFINED ZEPHYR_BASE) AND (DEFINED ENV_ZEPHYR_BASE)) 44 set(ZEPHYR_BASE ${ENV_ZEPHYR_BASE} CACHE PATH "Zephyr base") 45endif() 46 47find_package(Deprecated COMPONENTS SOURCES) 48 49if(NOT SOURCES AND EXISTS main.c) 50 set(SOURCES main.c) 51endif() 52 53add_executable(testbinary ${SOURCES}) 54add_library(test_interface INTERFACE) 55target_link_libraries(testbinary PRIVATE test_interface) 56 57set(KOBJ_TYPES_H_TARGET kobj_types_h_target) 58include(${ZEPHYR_BASE}/cmake/kobj.cmake) 59add_dependencies(test_interface ${KOBJ_TYPES_H_TARGET}) 60gen_kobj(KOBJ_GEN_DIR) 61 62# Generates empty header files to build 63set(INCL_GENERATED_DIR ${APPLICATION_BINARY_DIR}/zephyr/include/generated/zephyr) 64set(INCL_GENERATED_SYSCALL_DIR ${INCL_GENERATED_DIR}/syscalls) 65list(APPEND INCL_GENERATED_HEADERS 66 ${INCL_GENERATED_DIR}/devicetree_generated.h 67 ${INCL_GENERATED_DIR}/offsets.h 68 ${INCL_GENERATED_DIR}/syscall_list.h 69 ${INCL_GENERATED_DIR}/syscall_macros.h 70 ${INCL_GENERATED_SYSCALL_DIR}/kernel.h 71 ${INCL_GENERATED_SYSCALL_DIR}/kobject.h 72 ${INCL_GENERATED_SYSCALL_DIR}/log_core.h 73 ${INCL_GENERATED_SYSCALL_DIR}/log_ctrl.h 74 ${INCL_GENERATED_SYSCALL_DIR}/log_msg.h 75 ${INCL_GENERATED_SYSCALL_DIR}/sys_clock.h 76) 77 78file(MAKE_DIRECTORY ${INCL_GENERATED_SYSCALL_DIR}) 79foreach(header ${INCL_GENERATED_HEADERS}) 80 file(TOUCH ${header}) 81endforeach() 82 83list(APPEND INCLUDE 84 subsys/testsuite/ztest/include/zephyr 85 subsys/testsuite/ztest/unittest/include 86 subsys/testsuite/include/zephyr 87 subsys/testsuite/ztest/include 88 subsys/testsuite/include 89 include/zephyr 90 include 91 . 92) 93 94if(CMAKE_HOST_APPLE) 95else() 96 97if(M64_MODE) 98set (CMAKE_C_FLAGS "-m64") 99set (CMAKE_CXX_FLAGS "-m64") 100else() 101set (CMAKE_C_FLAGS "-m32") #deprecated on macOS 102set (CMAKE_CXX_FLAGS "-m32") #deprecated on macOS 103endif(M64_MODE) 104 105endif() 106 107target_compile_options(test_interface INTERFACE 108 -imacros ${AUTOCONF_H} 109 -Wall 110 -I ${KOBJ_GEN_DIR} 111 ${EXTRA_CPPFLAGS_AS_LIST} 112 ${EXTRA_CFLAGS_AS_LIST} 113 $<$<COMPILE_LANGUAGE:CXX>:${EXTRA_CXXFLAGS_AS_LIST}> 114 $<$<COMPILE_LANGUAGE:ASM>:${EXTRA_AFLAGS_AS_LIST}> 115 -Wno-format-zero-length 116 ) 117 118target_link_options(testbinary PRIVATE 119 -T "${ZEPHYR_BASE}/subsys/testsuite/include/zephyr/ztest_unittest.ld" 120 ) 121 122target_link_libraries(testbinary PRIVATE 123 ${EXTRA_LDFLAGS_AS_LIST} 124 ) 125 126target_compile_options(test_interface INTERFACE $<TARGET_PROPERTY:compiler,debug>) 127 128if(CONFIG_COVERAGE) 129 target_compile_options(test_interface INTERFACE $<TARGET_PROPERTY:compiler,coverage>) 130 131 target_link_libraries(testbinary PRIVATE $<TARGET_PROPERTY:linker,coverage>) 132endif() 133 134if (CONFIG_COMPILER_WARNINGS_AS_ERRORS) 135 target_compile_options(test_interface INTERFACE $<TARGET_PROPERTY:compiler,warnings_as_errors>) 136endif() 137 138if(LIBS) 139 message(FATAL_ERROR "This variable is not supported, see SOURCES instead") 140endif() 141 142target_sources(testbinary PRIVATE 143 ${ZEPHYR_BASE}/subsys/testsuite/ztest/src/ztest.c 144 ${ZEPHYR_BASE}/subsys/testsuite/ztest/src/ztest_mock.c 145 ${ZEPHYR_BASE}/subsys/testsuite/ztest/src/ztest_rules.c 146 ${ZEPHYR_BASE}/subsys/testsuite/ztest/src/ztest_defaults.c 147) 148 149target_compile_definitions(test_interface INTERFACE ZTEST_UNITTEST) 150 151foreach(inc ${INCLUDE}) 152 target_include_directories(test_interface INTERFACE ${ZEPHYR_BASE}/${inc}) 153endforeach() 154 155find_program(VALGRIND_PROGRAM valgrind) 156if(VALGRIND_PROGRAM) 157 set(VALGRIND ${VALGRIND_PROGRAM}) 158 set(VALGRIND_FLAGS 159 --leak-check=full 160 --error-exitcode=1 161 --log-file=valgrind.log 162 ) 163endif() 164 165add_custom_target(run-test 166 COMMAND 167 ${VALGRIND} ${VALGRIND_FLAGS} 168 $<TARGET_FILE:testbinary> 169 DEPENDS testbinary 170 WORKING_DIRECTORY ${APPLICATION_BINARY_DIR} 171 ) 172# TODO: Redirect output to unit.log 173