blob: 758bce99b9e753cdb9072f8a338d2868eef610cf [file] [log] [blame]
Javier Almansa Sobrinoc4ad5b02022-07-05 19:05:14 +01001#
2# SPDX-License-Identifier: BSD-3-Clause
3# SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
4#
5
6#[=======================================================================[.rst:
7rmm_build_unittest
8------------------
9
10.. default-domain:: unit tests
11
12.. command:: rmm_build_unittest
13
14Build a unit test group for a given target
15
16.. code:: cmake
17
18 rmm_build_unittest(NAME <name> TARGET <target> SOURCES <sources>
19 [RUN_ISOLATED_TESTS <LIST of tests to run>]
20 [LIBRARIES <libraries_to_link>]
21 [ITERATIONS <iterations>])
22
23This helper function simplifies the mechanics to setup and enable an unit test.
24
25Basics
26^^^^^^
27
28Every unit test configuration has the following parameters (defined as
29strings):
30
31- ``NAME`` Name of the test. It must match the name of the CppUtest test group.
32- ``TARGET`` Target where the tests will be linked against.
33- ``SOURCES`` Source files for the tests. This is usually a single C++ file.
34- ``RUN_ISOLATED_TESTS`` Optional parameter that specifies a list of tests
35 implemented within ``SOURCES`` to be run. When this
36 list is specified, the binary is re-spawned for each
37 test and only executed once (``ITERATIONS`` is
38 ignored). Any test not included on the list will be
39 ignored.
40 If this parameter is not used, all the tests included
41 in the group will be run automatically by CppUTest,
42 the number of times specified by ``ITERATIONS``
43- ``LIBRARIES`` Optional parameter to define libraries to link against
44 the tests.
45- ``ITERATIONS`` Optional parameter that defines how many times the test will
46 run. By default it is 1 times.
47 This option is ignored when using ``RUN_ISOLATED_TESTS``
48
49#]=======================================================================]
50
51if(RMM_UNITTESTS)
52 include("${CMAKE_SOURCE_DIR}/cmake/BuildCppUTest.cmake")
53
54 # Clean ${IMPORT_TEST_GROUPS}, used to generate test_groups.h later.
55 SET(IMPORT_TEST_GROUPS "" CACHE INTERNAL "IMPORT_TEST_GROUP List")
56
57 # Generate an empty test_groups.h, needed if we don't have unittests
58 configure_file(${CMAKE_SOURCE_DIR}/plat/host/host_test/src/test_groups.h.in
59 ${CMAKE_BINARY_DIR}/plat/host/host_test/src/test_groups.h
60 @ONLY)
61
62 # Include CTest for unittests
63 include(CTest)
64
65 # Custom target to run the unit tests
66 add_custom_target(run-unittests
67 WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
68 COMMAND ctest -C "$<CONFIG>"
69 DEPENDS rmm.elf rmm.map
70 )
71endif()
72
73function(rmm_build_unittest)
74 if(RMM_UNITTESTS)
75 set(_options "")
76 set(_multi_args "SOURCES;LIBRARIES;RUN_ISOLATED_TESTS")
77 set(_single_args "NAME;TARGET;ITERATIONS")
78
79 cmake_parse_arguments(
80 arg "${_options}" "${_single_args}" "${_multi_args}" ${ARGN})
81
82 if("NAME" IN_LIST arg_KEYWORDS_MISSING_VALUES OR
83 NOT DEFINED arg_NAME)
84 message(FATAL_ERROR "Missing unit test name")
85 endif()
86
87 if("TARGET" IN_LIST arg_KEYWORDS_MISSING_VALUES OR
88 NOT DEFINED arg_TARGET)
89 message(FATAL_ERROR "Missing test target")
90 endif()
91
92 if("SOURCES" IN_LIST arg_KEYWORDS_MISSING_VALUES OR
93 NOT DEFINED arg_SOURCES)
94 message(FATAL_ERROR "Missing test sources")
95 endif()
96
97 if("ITERATIONS" IN_LIST arg_KEYWORDS_MISSING_VALUES OR
98 NOT DEFINED arg_ITERATIONS)
99 set(arg_ITERATIONS "1")
100 endif()
101
102 target_sources("${arg_TARGET}"
103 PRIVATE ${arg_SOURCES})
104
105 target_link_libraries("${arg_TARGET}"
106 PRIVATE CppUTest ${arg_LIBRARIES})
107
108 # Add the test to the CMake test builder, so we can automate
109 # the test run process.
110 if("RUN_ISOLATED_TESTS" IN_LIST arg_KEYWORDS_MISSING_VALUES OR
111 NOT DEFINED arg_RUN_ISOLATED_TESTS)
112 # Run all tests at once
113 add_test(NAME "${arg_NAME}"
114 COMMAND ${CMAKE_BINARY_DIR}/rmm.elf
115 -g${arg_NAME}
116 -r${arg_ITERATIONS})
117 else()
118 # Register a test for each test case, so each one on them can
119 # run on isolation.
120 foreach(TEST IN LISTS arg_RUN_ISOLATED_TESTS)
121 add_test(NAME "${arg_NAME}::${TEST}"
122 COMMAND ${CMAKE_BINARY_DIR}/rmm.elf
123 -sg${arg_NAME}
124 -sn${TEST})
125 endforeach()
126 endif()
127
128 # Use CppUtest IMPORT_TEST_GROUP macro to explicitly include the new test
129 # group. This is needed as otherwise the linker will ignore the test code.
130 SET(IMPORT_TEST_GROUPS "${IMPORT_TEST_GROUPS} IMPORT_TEST_GROUP(${arg_NAME});"
131 CACHE INTERNAL "IMPORT_TEST_GROUP List")
132
133 # Generate the test_groups.h
134 configure_file(${CMAKE_SOURCE_DIR}/plat/host/host_test/src/test_groups.h.in
135 ${CMAKE_BINARY_DIR}/plat/host/host_test/src/test_groups.h
136 @ONLY)
137 endif()
138endfunction()