-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
56 lines (43 loc) · 1.79 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
enable_language(C ASM)
# Provides the unity library
add_subdirectory(Unity)
# Allows us to use ctest
enable_testing()
# ctest will not rebuild targets before running tests, so we instead make a target
# that invokes ctest.
add_custom_target(ctest COMMAND ${CMAKE_CTEST_COMMAND} --verbose)
# Function to add a unit test in a simple way
# Example usage:
#
# add_unit_test(
# TARGET_NAME test_minimal
# SOURCE_FILES ${CMAKE_SOURCE_DIR}/src/0_minimal/test/test_minimal.c
# )
#
function(add_unit_test)
# Parse keyword arguments
set(_flags)
set(_single_values TARGET_NAME)
set(_list_values SOURCE_FILES)
cmake_parse_arguments(ARG "${_flags}" "${_single_values}" "${_list_values}" ${ARGN})
# Assume the first source file is the test file
list(GET ARG_SOURCE_FILES 0 TEST_SOURCE)
# Place the autogenerated test runner in the build directory
set(TEST_RUNNER ${CMAKE_CURRENT_BINARY_DIR}/${ARG_TARGET_NAME}_runner.c)
# Define how the test runner should be generated
add_custom_command(
OUTPUT ${TEST_RUNNER}
COMMAND ruby ${CMAKE_SOURCE_DIR}/Unity/auto/generate_test_runner.rb ${TEST_SOURCE} ${TEST_RUNNER}
DEPENDS ${ARG_SOURCE_FILES}
)
# Define the executable for the test runner
add_executable(${ARG_TARGET_NAME} ${TEST_RUNNER} ${ARG_SOURCE_FILES})
# Enable testing with ctest. Running the test is just a matter of running the executable.
add_test(NAME ${ARG_TARGET_NAME} COMMAND ${ARG_TARGET_NAME})
# Always link in the Unity library
target_link_libraries(${ARG_TARGET_NAME} PUBLIC unity)
# Add dependencies so the test runner rebuilds everything when any of the sources change
add_dependencies(ctest ${ARG_TARGET_NAME})
endfunction()
add_subdirectory(src)