xref: /aosp_15_r20/external/pigweed/pw_unit_test/test.cmake (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker# Copyright 2022 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker#
3*61c4878aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker# use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker# the License at
6*61c4878aSAndroid Build Coastguard Worker#
7*61c4878aSAndroid Build Coastguard Worker#     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker#
9*61c4878aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker# License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker# the License.
14*61c4878aSAndroid Build Coastguard Workerinclude_guard(GLOBAL)
15*61c4878aSAndroid Build Coastguard Worker
16*61c4878aSAndroid Build Coastguard Workerinclude($ENV{PW_ROOT}/pw_build/pigweed.cmake)
17*61c4878aSAndroid Build Coastguard Worker
18*61c4878aSAndroid Build Coastguard Workerset(pw_unit_test_ENABLE_PW_ADD_TEST ON CACHE BOOL
19*61c4878aSAndroid Build Coastguard Worker    "Enable or disable pw_add_test calls. This is useful if you would like to \
20*61c4878aSAndroid Build Coastguard Worker     disable test generation when adding Pigweed to an existing project. Set to \
21*61c4878aSAndroid Build Coastguard Worker     OFF before the add_subdirectory(third_party/pigweed) call to prevent tests \
22*61c4878aSAndroid Build Coastguard Worker     from being generated.")
23*61c4878aSAndroid Build Coastguard Worker
24*61c4878aSAndroid Build Coastguard Workerset(pw_unit_test_BACKEND pw_unit_test.light CACHE STRING
25*61c4878aSAndroid Build Coastguard Worker    "CMake target which implements GoogleTest, by default pw_unit_test.light \
26*61c4878aSAndroid Build Coastguard Worker     is used. You could, for example, point this at pw_unit_test.googletest \
27*61c4878aSAndroid Build Coastguard Worker     if using upstream GoogleTest directly on your host for GoogleMock.")
28*61c4878aSAndroid Build Coastguard Worker
29*61c4878aSAndroid Build Coastguard Workerset(pw_unit_test_GOOGLETEST_BACKEND "" CACHE STRING
30*61c4878aSAndroid Build Coastguard Worker    "Warning: Deprecated build argument. When using googletest backend set
31*61c4878aSAndroid Build Coastguard Worker    pw_unit_test_BACKEND to pw_unit_test.googletest instead.")
32*61c4878aSAndroid Build Coastguard Worker
33*61c4878aSAndroid Build Coastguard Worker# TODO(ewout): Remove the default.
34*61c4878aSAndroid Build Coastguard Workerset(pw_unit_test_ADD_EXECUTABLE_FUNCTION "pw_add_test_executable" CACHE STRING
35*61c4878aSAndroid Build Coastguard Worker    "The name of the CMake function used to instantiate pw_unit_test \
36*61c4878aSAndroid Build Coastguard Worker     executables")
37*61c4878aSAndroid Build Coastguard Worker
38*61c4878aSAndroid Build Coastguard Worker# TODO(ewout): Remove the default.
39*61c4878aSAndroid Build Coastguard Workerset(pw_unit_test_ADD_EXECUTABLE_FUNCTION_FILE
40*61c4878aSAndroid Build Coastguard Worker    "$ENV{PW_ROOT}/targets/host/pw_add_test_executable.cmake" CACHE STRING
41*61c4878aSAndroid Build Coastguard Worker    "The path to the .cmake file that defines \
42*61c4878aSAndroid Build Coastguard Worker     pw_unit_test_ADD_EXECUTABLE_FUNCTION.")
43*61c4878aSAndroid Build Coastguard Worker
44*61c4878aSAndroid Build Coastguard Worker# TODO(ewout): Remove the default to match GN and support Windows.
45*61c4878aSAndroid Build Coastguard Workerset(pw_unit_test_AUTOMATIC_RUNNER "$ENV{PW_ROOT}/targets/host/run_test" CACHE
46*61c4878aSAndroid Build Coastguard Worker    STRING
47*61c4878aSAndroid Build Coastguard Worker    "Path to a test runner to automatically run unit tests after they are \
48*61c4878aSAndroid Build Coastguard Worker     built. \
49*61c4878aSAndroid Build Coastguard Worker     If set, a pw_add_test's {NAME}.run action will invoke the test runner \
50*61c4878aSAndroid Build Coastguard Worker     specified by this variable, passing the path to the unit test to run. If \
51*61c4878aSAndroid Build Coastguard Worker     set to an empty string, the {NAME}.run step will fail to build.")
52*61c4878aSAndroid Build Coastguard Worker
53*61c4878aSAndroid Build Coastguard Workerset(pw_unit_test_AUTOMATIC_RUNNER_TIMEOUT_SECONDS "" CACHE STRING
54*61c4878aSAndroid Build Coastguard Worker    "Optional timeout to apply when running tests via the automatic runner. \
55*61c4878aSAndroid Build Coastguard Worker     Timeout is in seconds. Defaults to empty which means no timeout.")
56*61c4878aSAndroid Build Coastguard Worker
57*61c4878aSAndroid Build Coastguard Workerset(pw_unit_test_AUTOMATIC_RUNNER_ARGS "" CACHE STRING
58*61c4878aSAndroid Build Coastguard Worker    "Optional list of arguments to forward to the automatic runner")
59*61c4878aSAndroid Build Coastguard Worker
60*61c4878aSAndroid Build Coastguard Worker# pw_add_test: Declares a single unit test suite with Pigweed naming rules and
61*61c4878aSAndroid Build Coastguard Worker#              compiler warning options.
62*61c4878aSAndroid Build Coastguard Worker#
63*61c4878aSAndroid Build Coastguard Worker#   {NAME} depends on ${NAME}.run if pw_unit_test_AUTOMATIC_RUNNER is set, else
64*61c4878aSAndroid Build Coastguard Worker#          it depends on ${NAME}.bin
65*61c4878aSAndroid Build Coastguard Worker#   {NAME}.lib contains the provided test sources as a library target, which can
66*61c4878aSAndroid Build Coastguard Worker#              then be linked into a test executable.
67*61c4878aSAndroid Build Coastguard Worker#   {NAME}.bin is a standalone executable which contains only the test sources
68*61c4878aSAndroid Build Coastguard Worker#              specified in the pw_unit_test_template.
69*61c4878aSAndroid Build Coastguard Worker#   {NAME}.run which runs the unit test executable after building it if
70*61c4878aSAndroid Build Coastguard Worker#              pw_unit_test_AUTOMATIC_RUNNER is set, else it fails to build.
71*61c4878aSAndroid Build Coastguard Worker#
72*61c4878aSAndroid Build Coastguard Worker# Required Arguments:
73*61c4878aSAndroid Build Coastguard Worker#
74*61c4878aSAndroid Build Coastguard Worker#   NAME: name to use for the produced test targets specified above
75*61c4878aSAndroid Build Coastguard Worker#
76*61c4878aSAndroid Build Coastguard Worker# Optional Arguments:
77*61c4878aSAndroid Build Coastguard Worker#
78*61c4878aSAndroid Build Coastguard Worker#   SOURCES - source files for this library
79*61c4878aSAndroid Build Coastguard Worker#   HEADERS - header files for this library
80*61c4878aSAndroid Build Coastguard Worker#   PRIVATE_DEPS - private pw_target_link_targets arguments
81*61c4878aSAndroid Build Coastguard Worker#   PRIVATE_INCLUDES - public target_include_directories argument
82*61c4878aSAndroid Build Coastguard Worker#   PRIVATE_DEFINES - private target_compile_definitions arguments
83*61c4878aSAndroid Build Coastguard Worker#   PRIVATE_COMPILE_OPTIONS - private target_compile_options arguments
84*61c4878aSAndroid Build Coastguard Worker#   PRIVATE_LINK_OPTIONS - private target_link_options arguments
85*61c4878aSAndroid Build Coastguard Worker#
86*61c4878aSAndroid Build Coastguard Worker#  TODO(ewout, hepler): Deprecate the following legacy arguments
87*61c4878aSAndroid Build Coastguard Worker#   GROUPS - groups to which to add this test.
88*61c4878aSAndroid Build Coastguard Worker#
89*61c4878aSAndroid Build Coastguard Workerfunction(pw_add_test NAME)
90*61c4878aSAndroid Build Coastguard Workerif("${pw_unit_test_ENABLE_PW_ADD_TEST}")
91*61c4878aSAndroid Build Coastguard Worker  pw_parse_arguments(
92*61c4878aSAndroid Build Coastguard Worker    NUM_POSITIONAL_ARGS
93*61c4878aSAndroid Build Coastguard Worker      1
94*61c4878aSAndroid Build Coastguard Worker    MULTI_VALUE_ARGS
95*61c4878aSAndroid Build Coastguard Worker      SOURCES HEADERS PRIVATE_DEPS PRIVATE_INCLUDES
96*61c4878aSAndroid Build Coastguard Worker      PRIVATE_DEFINES PRIVATE_COMPILE_OPTIONS
97*61c4878aSAndroid Build Coastguard Worker      PRIVATE_LINK_OPTIONS GROUPS
98*61c4878aSAndroid Build Coastguard Worker  )
99*61c4878aSAndroid Build Coastguard Worker
100*61c4878aSAndroid Build Coastguard Worker  _pw_check_name_is_relative_to_root("${NAME}" "$ENV{PW_ROOT}"
101*61c4878aSAndroid Build Coastguard Worker    REMAP_PREFIXES
102*61c4878aSAndroid Build Coastguard Worker      third_party pw_third_party
103*61c4878aSAndroid Build Coastguard Worker  )
104*61c4878aSAndroid Build Coastguard Worker
105*61c4878aSAndroid Build Coastguard Worker  pw_add_test_generic(${NAME}
106*61c4878aSAndroid Build Coastguard Worker    SOURCES
107*61c4878aSAndroid Build Coastguard Worker      ${arg_SOURCES}
108*61c4878aSAndroid Build Coastguard Worker    HEADERS
109*61c4878aSAndroid Build Coastguard Worker      ${arg_HEADERS}
110*61c4878aSAndroid Build Coastguard Worker    PRIVATE_DEPS
111*61c4878aSAndroid Build Coastguard Worker      # TODO: b/232141950 - Apply compilation options that affect ABI
112*61c4878aSAndroid Build Coastguard Worker      # globally in the CMake build instead of injecting them into libraries.
113*61c4878aSAndroid Build Coastguard Worker      pw_build
114*61c4878aSAndroid Build Coastguard Worker      ${arg_PRIVATE_DEPS}
115*61c4878aSAndroid Build Coastguard Worker    PRIVATE_INCLUDES
116*61c4878aSAndroid Build Coastguard Worker      ${arg_PRIVATE_INCLUDES}
117*61c4878aSAndroid Build Coastguard Worker    PRIVATE_DEFINES
118*61c4878aSAndroid Build Coastguard Worker      ${arg_PRIVATE_DEFINES}
119*61c4878aSAndroid Build Coastguard Worker    PRIVATE_COMPILE_OPTIONS_DEPS_BEFORE
120*61c4878aSAndroid Build Coastguard Worker      pw_build.warnings
121*61c4878aSAndroid Build Coastguard Worker    PRIVATE_COMPILE_OPTIONS
122*61c4878aSAndroid Build Coastguard Worker      ${arg_PRIVATE_COMPILE_OPTIONS}
123*61c4878aSAndroid Build Coastguard Worker    PRIVATE_LINK_OPTIONS
124*61c4878aSAndroid Build Coastguard Worker      ${arg_PRIVATE_LINK_OPTIONS}
125*61c4878aSAndroid Build Coastguard Worker    GROUPS
126*61c4878aSAndroid Build Coastguard Worker      ${arg_GROUPS}
127*61c4878aSAndroid Build Coastguard Worker  )
128*61c4878aSAndroid Build Coastguard Workerendif()
129*61c4878aSAndroid Build Coastguard Workerendfunction()
130*61c4878aSAndroid Build Coastguard Worker
131*61c4878aSAndroid Build Coastguard Worker# pw_add_test_generic: Declares a single unit test suite.
132*61c4878aSAndroid Build Coastguard Worker#
133*61c4878aSAndroid Build Coastguard Worker#   {NAME} depends on ${NAME}.run if pw_unit_test_AUTOMATIC_RUNNER is set, else
134*61c4878aSAndroid Build Coastguard Worker#          it depends on ${NAME}.bin
135*61c4878aSAndroid Build Coastguard Worker#   {NAME}.lib contains the provided test sources as a library target, which can
136*61c4878aSAndroid Build Coastguard Worker#              then be linked into a test executable.
137*61c4878aSAndroid Build Coastguard Worker#   {NAME}.bin is a standalone executable which contains only the test sources
138*61c4878aSAndroid Build Coastguard Worker#              specified in the pw_unit_test_template.
139*61c4878aSAndroid Build Coastguard Worker#   {NAME}.run which runs the unit test executable after building it if
140*61c4878aSAndroid Build Coastguard Worker#              pw_unit_test_AUTOMATIC_RUNNER is set, else it fails to build.
141*61c4878aSAndroid Build Coastguard Worker#
142*61c4878aSAndroid Build Coastguard Worker# Required Arguments:
143*61c4878aSAndroid Build Coastguard Worker#
144*61c4878aSAndroid Build Coastguard Worker#   NAME: name to use for the produced test targets specified above
145*61c4878aSAndroid Build Coastguard Worker#
146*61c4878aSAndroid Build Coastguard Worker# Optional Arguments:
147*61c4878aSAndroid Build Coastguard Worker#
148*61c4878aSAndroid Build Coastguard Worker#   SOURCES - source files for this library
149*61c4878aSAndroid Build Coastguard Worker#   HEADERS - header files for this library
150*61c4878aSAndroid Build Coastguard Worker#   PRIVATE_DEPS - private pw_target_link_targets arguments
151*61c4878aSAndroid Build Coastguard Worker#   PRIVATE_INCLUDES - public target_include_directories argument
152*61c4878aSAndroid Build Coastguard Worker#   PRIVATE_DEFINES - private target_compile_definitions arguments
153*61c4878aSAndroid Build Coastguard Worker#   PRIVATE_COMPILE_OPTIONS_DEPS_BEFORE - private target_compile_options BEFORE
154*61c4878aSAndroid Build Coastguard Worker#     arguments from the specified deps's INTERFACE_COMPILE_OPTIONS. Note that
155*61c4878aSAndroid Build Coastguard Worker#     these deps are not pulled in as target_link_libraries. This should not be
156*61c4878aSAndroid Build Coastguard Worker#     exposed by the non-generic API.
157*61c4878aSAndroid Build Coastguard Worker#   PRIVATE_COMPILE_OPTIONS - private target_compile_options arguments
158*61c4878aSAndroid Build Coastguard Worker#   PRIVATE_LINK_OPTIONS - private target_link_options arguments
159*61c4878aSAndroid Build Coastguard Worker#   TEST_MAIN - overrides the default test main dependency
160*61c4878aSAndroid Build Coastguard Worker#
161*61c4878aSAndroid Build Coastguard Worker#  TODO(ewout, hepler): Deprecate the following legacy arguments
162*61c4878aSAndroid Build Coastguard Worker#   GROUPS - groups to which to add this test.
163*61c4878aSAndroid Build Coastguard Worker#
164*61c4878aSAndroid Build Coastguard Workerfunction(pw_add_test_generic NAME)
165*61c4878aSAndroid Build Coastguard Worker  pw_parse_arguments(
166*61c4878aSAndroid Build Coastguard Worker    NUM_POSITIONAL_ARGS
167*61c4878aSAndroid Build Coastguard Worker      1
168*61c4878aSAndroid Build Coastguard Worker    ONE_VALUE_ARGS
169*61c4878aSAndroid Build Coastguard Worker      TEST_MAIN
170*61c4878aSAndroid Build Coastguard Worker    MULTI_VALUE_ARGS
171*61c4878aSAndroid Build Coastguard Worker      SOURCES HEADERS PRIVATE_DEPS PRIVATE_INCLUDES
172*61c4878aSAndroid Build Coastguard Worker      PRIVATE_DEFINES
173*61c4878aSAndroid Build Coastguard Worker      PRIVATE_COMPILE_OPTIONS_DEPS_BEFORE PRIVATE_COMPILE_OPTIONS
174*61c4878aSAndroid Build Coastguard Worker      PRIVATE_LINK_OPTIONS GROUPS
175*61c4878aSAndroid Build Coastguard Worker  )
176*61c4878aSAndroid Build Coastguard Worker
177*61c4878aSAndroid Build Coastguard Worker  # Add the library target under "${NAME}.lib".
178*61c4878aSAndroid Build Coastguard Worker  # OBJECT libraries require at least one source file.
179*61c4878aSAndroid Build Coastguard Worker  if("${arg_SOURCES}" STREQUAL "")
180*61c4878aSAndroid Build Coastguard Worker    set(lib_type "INTERFACE")
181*61c4878aSAndroid Build Coastguard Worker  else()
182*61c4878aSAndroid Build Coastguard Worker    set(lib_type "OBJECT")
183*61c4878aSAndroid Build Coastguard Worker  endif()
184*61c4878aSAndroid Build Coastguard Worker  pw_add_library_generic("${NAME}.lib" ${lib_type}
185*61c4878aSAndroid Build Coastguard Worker    SOURCES
186*61c4878aSAndroid Build Coastguard Worker      ${arg_SOURCES}
187*61c4878aSAndroid Build Coastguard Worker    HEADERS
188*61c4878aSAndroid Build Coastguard Worker      ${arg_HEADERS}
189*61c4878aSAndroid Build Coastguard Worker    PRIVATE_DEPS
190*61c4878aSAndroid Build Coastguard Worker      pw_unit_test
191*61c4878aSAndroid Build Coastguard Worker      ${arg_PRIVATE_DEPS}
192*61c4878aSAndroid Build Coastguard Worker    PRIVATE_INCLUDES
193*61c4878aSAndroid Build Coastguard Worker      ${arg_PRIVATE_INCLUDES}
194*61c4878aSAndroid Build Coastguard Worker    PRIVATE_DEFINES
195*61c4878aSAndroid Build Coastguard Worker      ${arg_PRIVATE_DEFINES}
196*61c4878aSAndroid Build Coastguard Worker    PRIVATE_COMPILE_OPTIONS_DEPS_BEFORE
197*61c4878aSAndroid Build Coastguard Worker      ${arg_PRIVATE_COMPILE_OPTIONS_DEPS_BEFORE}
198*61c4878aSAndroid Build Coastguard Worker    PRIVATE_COMPILE_OPTIONS
199*61c4878aSAndroid Build Coastguard Worker      ${arg_PRIVATE_COMPILE_OPTIONS}
200*61c4878aSAndroid Build Coastguard Worker    PRIVATE_LINK_OPTIONS
201*61c4878aSAndroid Build Coastguard Worker      ${arg_PRIVATE_LINK_OPTIONS}
202*61c4878aSAndroid Build Coastguard Worker  )
203*61c4878aSAndroid Build Coastguard Worker
204*61c4878aSAndroid Build Coastguard Worker  # Add the executable target under "${NAME}.bin".
205*61c4878aSAndroid Build Coastguard Worker  if(("${pw_unit_test_ADD_EXECUTABLE_FUNCTION}" STREQUAL "") OR
206*61c4878aSAndroid Build Coastguard Worker     ("${pw_unit_test_ADD_EXECUTABLE_FUNCTION_FILE}" STREQUAL ""))
207*61c4878aSAndroid Build Coastguard Worker    pw_add_error_target("${NAME}.bin"
208*61c4878aSAndroid Build Coastguard Worker      MESSAGE
209*61c4878aSAndroid Build Coastguard Worker        "Attempted to build the ${NAME}.bin without enabling the unit "
210*61c4878aSAndroid Build Coastguard Worker        "test executable function via pw_unit_test_ADD_EXECUTABLE_FUNCTION "
211*61c4878aSAndroid Build Coastguard Worker        "and pw_unit_test_ADD_EXECUTABLE_FUNCTION_FILE. "
212*61c4878aSAndroid Build Coastguard Worker        "See https://pigweed.dev/pw_unit_test for more details."
213*61c4878aSAndroid Build Coastguard Worker    )
214*61c4878aSAndroid Build Coastguard Worker  else()
215*61c4878aSAndroid Build Coastguard Worker    include("${pw_unit_test_ADD_EXECUTABLE_FUNCTION_FILE}")
216*61c4878aSAndroid Build Coastguard Worker    if ("${arg_TEST_MAIN}" STREQUAL "")
217*61c4878aSAndroid Build Coastguard Worker      cmake_language(CALL "${pw_unit_test_ADD_EXECUTABLE_FUNCTION}"
218*61c4878aSAndroid Build Coastguard Worker                    "${NAME}.bin" "${NAME}.lib")
219*61c4878aSAndroid Build Coastguard Worker    else()
220*61c4878aSAndroid Build Coastguard Worker      cmake_language(CALL "${pw_unit_test_ADD_EXECUTABLE_FUNCTION}_with_main"
221*61c4878aSAndroid Build Coastguard Worker                    "${NAME}.bin" "${NAME}.lib" "${arg_TEST_MAIN}")
222*61c4878aSAndroid Build Coastguard Worker    endif()
223*61c4878aSAndroid Build Coastguard Worker  endif()
224*61c4878aSAndroid Build Coastguard Worker
225*61c4878aSAndroid Build Coastguard Worker  # Add the ${NAME} target and optionally the run target under ${NAME}.run.
226*61c4878aSAndroid Build Coastguard Worker  add_custom_target("${NAME}")
227*61c4878aSAndroid Build Coastguard Worker  if("${pw_unit_test_AUTOMATIC_RUNNER}" STREQUAL "")
228*61c4878aSAndroid Build Coastguard Worker    # Test runner is not provided, only build the executable.
229*61c4878aSAndroid Build Coastguard Worker    add_dependencies("${NAME}" "${NAME}.bin")
230*61c4878aSAndroid Build Coastguard Worker
231*61c4878aSAndroid Build Coastguard Worker    pw_add_error_target("${NAME}.run"
232*61c4878aSAndroid Build Coastguard Worker      MESSAGE
233*61c4878aSAndroid Build Coastguard Worker        "Attempted to build ${NAME}.run which is not available because "
234*61c4878aSAndroid Build Coastguard Worker        "pw_unit_test_AUTOMATIC_RUNNER has not been configured. "
235*61c4878aSAndroid Build Coastguard Worker        "See https://pigweed.dev/pw_unit_test."
236*61c4878aSAndroid Build Coastguard Worker    )
237*61c4878aSAndroid Build Coastguard Worker  else()  # pw_unit_test_AUTOMATIC_RUNNER is provided.
238*61c4878aSAndroid Build Coastguard Worker    # Define a target for running the test. The target creates a stamp file to
239*61c4878aSAndroid Build Coastguard Worker    # indicate successful test completion. This allows running tests in parallel
240*61c4878aSAndroid Build Coastguard Worker    # with Ninja's full dependency resolution.
241*61c4878aSAndroid Build Coastguard Worker    if(NOT "${pw_unit_test_AUTOMATIC_RUNNER_TIMEOUT_SECONDS}" STREQUAL "")
242*61c4878aSAndroid Build Coastguard Worker      set(optional_timeout_arg
243*61c4878aSAndroid Build Coastguard Worker          "--timeout" "${pw_unit_test_AUTOMATIC_RUNNER_TIMEOUT_SECONDS}")
244*61c4878aSAndroid Build Coastguard Worker    endif()
245*61c4878aSAndroid Build Coastguard Worker    if(NOT "${pw_unit_test_AUTOMATIC_RUNNER_ARGS}" STREQUAL "")
246*61c4878aSAndroid Build Coastguard Worker      set(optional_runner_args "--" "${pw_unit_test_AUTOMATIC_RUNNER_ARGS}")
247*61c4878aSAndroid Build Coastguard Worker    endif()
248*61c4878aSAndroid Build Coastguard Worker    add_custom_command(
249*61c4878aSAndroid Build Coastguard Worker      COMMAND
250*61c4878aSAndroid Build Coastguard Worker        python3 -m pw_unit_test.test_runner
251*61c4878aSAndroid Build Coastguard Worker        --runner "${pw_unit_test_AUTOMATIC_RUNNER}"
252*61c4878aSAndroid Build Coastguard Worker        --test "$<TARGET_FILE:${NAME}.bin>"
253*61c4878aSAndroid Build Coastguard Worker        ${optional_timeout_arg}
254*61c4878aSAndroid Build Coastguard Worker        ${optional_runner_args}
255*61c4878aSAndroid Build Coastguard Worker      COMMAND
256*61c4878aSAndroid Build Coastguard Worker        "${CMAKE_COMMAND}" -E touch "${NAME}.stamp"
257*61c4878aSAndroid Build Coastguard Worker      DEPENDS
258*61c4878aSAndroid Build Coastguard Worker        "${NAME}.bin"
259*61c4878aSAndroid Build Coastguard Worker      OUTPUT
260*61c4878aSAndroid Build Coastguard Worker        "${NAME}.stamp"
261*61c4878aSAndroid Build Coastguard Worker    )
262*61c4878aSAndroid Build Coastguard Worker    add_custom_target("${NAME}.run" DEPENDS "${NAME}.stamp")
263*61c4878aSAndroid Build Coastguard Worker    add_dependencies("${NAME}" "${NAME}.run")
264*61c4878aSAndroid Build Coastguard Worker  endif()
265*61c4878aSAndroid Build Coastguard Worker
266*61c4878aSAndroid Build Coastguard Worker  if(arg_GROUPS)
267*61c4878aSAndroid Build Coastguard Worker    pw_add_test_to_groups("${NAME}" ${arg_GROUPS})
268*61c4878aSAndroid Build Coastguard Worker  endif()
269*61c4878aSAndroid Build Coastguard Workerendfunction(pw_add_test_generic)
270*61c4878aSAndroid Build Coastguard Worker
271*61c4878aSAndroid Build Coastguard Worker# pw_add_test_group: Defines a collection of tests or other test groups.
272*61c4878aSAndroid Build Coastguard Worker#
273*61c4878aSAndroid Build Coastguard Worker# Creates the following targets:
274*61c4878aSAndroid Build Coastguard Worker#
275*61c4878aSAndroid Build Coastguard Worker#   {NAME} depends on ${NAME}.run if pw_unit_test_AUTOMATIC_RUNNER is set, else
276*61c4878aSAndroid Build Coastguard Worker#          it depends on ${NAME}.bin
277*61c4878aSAndroid Build Coastguard Worker#   {NAME}.bundle depends on ${NAME}.bundle.run if pw_unit_test_AUTOMATIC_RUNNER
278*61c4878aSAndroid Build Coastguard Worker#                 is set, else it depends on ${NAME}.bundle.bin
279*61c4878aSAndroid Build Coastguard Worker#   {NAME}.lib depends on ${NAME}.bundle.lib.
280*61c4878aSAndroid Build Coastguard Worker#   {NAME}.bin depends on the provided TESTS's <test_dep>.bin targets.
281*61c4878aSAndroid Build Coastguard Worker#   {NAME}.run depends on the provided TESTS's <test_dep>.run targets if
282*61c4878aSAndroid Build Coastguard Worker#              pw_unit_test_AUTOMATIC_RUNNER is set, else it fails to build.
283*61c4878aSAndroid Build Coastguard Worker#   {NAME}.bundle.lib contains the provided tests bundled as a library target,
284*61c4878aSAndroid Build Coastguard Worker#                     which can then be linked into a test executable.
285*61c4878aSAndroid Build Coastguard Worker#   {NAME}.bundle.bin standalone executable which contains the bundled tests.
286*61c4878aSAndroid Build Coastguard Worker#   {NAME}.bundle.run runs the {NAME}.bundle.bin test bundle executable after
287*61c4878aSAndroid Build Coastguard Worker#                     building it if pw_unit_test_AUTOMATIC_RUNNER is set, else
288*61c4878aSAndroid Build Coastguard Worker#                     it fails to build.
289*61c4878aSAndroid Build Coastguard Worker#
290*61c4878aSAndroid Build Coastguard Worker# Required Arguments:
291*61c4878aSAndroid Build Coastguard Worker#
292*61c4878aSAndroid Build Coastguard Worker#   NAME - The name of the executable target to be created.
293*61c4878aSAndroid Build Coastguard Worker#   TESTS - pw_add_test targets and pw_add_test_group bundles to be included in
294*61c4878aSAndroid Build Coastguard Worker#           this test bundle
295*61c4878aSAndroid Build Coastguard Worker#
296*61c4878aSAndroid Build Coastguard Workerfunction(pw_add_test_group NAME)
297*61c4878aSAndroid Build Coastguard Worker  pw_parse_arguments(
298*61c4878aSAndroid Build Coastguard Worker    NUM_POSITIONAL_ARGMENTS
299*61c4878aSAndroid Build Coastguard Worker      1
300*61c4878aSAndroid Build Coastguard Worker    MULTI_VALUE_ARGS
301*61c4878aSAndroid Build Coastguard Worker      TESTS
302*61c4878aSAndroid Build Coastguard Worker    REQUIRED_ARGS
303*61c4878aSAndroid Build Coastguard Worker      TESTS
304*61c4878aSAndroid Build Coastguard Worker  )
305*61c4878aSAndroid Build Coastguard Worker
306*61c4878aSAndroid Build Coastguard Worker  set(test_lib_targets "")
307*61c4878aSAndroid Build Coastguard Worker  set(test_bin_targets "")
308*61c4878aSAndroid Build Coastguard Worker  set(test_run_targets "")
309*61c4878aSAndroid Build Coastguard Worker  foreach(test IN LISTS arg_TESTS)
310*61c4878aSAndroid Build Coastguard Worker    list(APPEND test_lib_targets "${test}.lib")
311*61c4878aSAndroid Build Coastguard Worker    list(APPEND test_bin_targets "${test}.bin")
312*61c4878aSAndroid Build Coastguard Worker    list(APPEND test_run_targets "${test}.run")
313*61c4878aSAndroid Build Coastguard Worker  endforeach()
314*61c4878aSAndroid Build Coastguard Worker
315*61c4878aSAndroid Build Coastguard Worker  # This produces ${NAME}.bundle, ${NAME}.bundle.lib, ${NAME}.bundle.bin, and
316*61c4878aSAndroid Build Coastguard Worker  # ${NAME}.bundle.run.
317*61c4878aSAndroid Build Coastguard Worker  pw_add_test("${NAME}.bundle"
318*61c4878aSAndroid Build Coastguard Worker    PRIVATE_DEPS
319*61c4878aSAndroid Build Coastguard Worker      ${test_lib_targets}
320*61c4878aSAndroid Build Coastguard Worker  )
321*61c4878aSAndroid Build Coastguard Worker
322*61c4878aSAndroid Build Coastguard Worker  # Produce ${NAME}.lib.
323*61c4878aSAndroid Build Coastguard Worker  pw_add_library_generic("${NAME}.lib" INTERFACE
324*61c4878aSAndroid Build Coastguard Worker    PUBLIC_DEPS
325*61c4878aSAndroid Build Coastguard Worker      ${NAME}.bundle.lib
326*61c4878aSAndroid Build Coastguard Worker  )
327*61c4878aSAndroid Build Coastguard Worker
328*61c4878aSAndroid Build Coastguard Worker  # Produce ${NAME}.bin.
329*61c4878aSAndroid Build Coastguard Worker  add_custom_target("${NAME}.bin")
330*61c4878aSAndroid Build Coastguard Worker  add_dependencies("${NAME}.bin" ${test_bin_targets})
331*61c4878aSAndroid Build Coastguard Worker
332*61c4878aSAndroid Build Coastguard Worker  # Produce ${NAME} and ${NAME}.run.
333*61c4878aSAndroid Build Coastguard Worker  add_custom_target("${NAME}")
334*61c4878aSAndroid Build Coastguard Worker  if("${pw_unit_test_AUTOMATIC_RUNNER}" STREQUAL "")
335*61c4878aSAndroid Build Coastguard Worker    # Test runner is not provided, only build the executable.
336*61c4878aSAndroid Build Coastguard Worker    add_dependencies("${NAME}" "${NAME}.bin")
337*61c4878aSAndroid Build Coastguard Worker
338*61c4878aSAndroid Build Coastguard Worker    pw_add_error_target("${NAME}.run"
339*61c4878aSAndroid Build Coastguard Worker      MESSAGE
340*61c4878aSAndroid Build Coastguard Worker        "Attempted to build ${NAME}.run which is not available because "
341*61c4878aSAndroid Build Coastguard Worker        "pw_unit_test_AUTOMATIC_RUNNER has not been configured. "
342*61c4878aSAndroid Build Coastguard Worker        "See https://pigweed.dev/pw_unit_test."
343*61c4878aSAndroid Build Coastguard Worker    )
344*61c4878aSAndroid Build Coastguard Worker  else()  # pw_unit_test_AUTOMATIC_RUNNER is provided, build and run the test.
345*61c4878aSAndroid Build Coastguard Worker    add_custom_target("${NAME}.run")
346*61c4878aSAndroid Build Coastguard Worker    add_dependencies("${NAME}.run" ${test_run_targets})
347*61c4878aSAndroid Build Coastguard Worker
348*61c4878aSAndroid Build Coastguard Worker    add_dependencies("${NAME}" "${NAME}.run")
349*61c4878aSAndroid Build Coastguard Worker  endif()
350*61c4878aSAndroid Build Coastguard Workerendfunction(pw_add_test_group)
351*61c4878aSAndroid Build Coastguard Worker
352*61c4878aSAndroid Build Coastguard Worker# Adds a test target to the specified test groups. Test groups can be built with
353*61c4878aSAndroid Build Coastguard Worker# the pw_tests_GROUP_NAME target or executed with the pw_run_tests_GROUP_NAME
354*61c4878aSAndroid Build Coastguard Worker# target.
355*61c4878aSAndroid Build Coastguard Workerfunction(pw_add_test_to_groups TEST_NAME)
356*61c4878aSAndroid Build Coastguard Worker  foreach(group IN LISTS ARGN)
357*61c4878aSAndroid Build Coastguard Worker    if(NOT TARGET "pw_tests.${group}")
358*61c4878aSAndroid Build Coastguard Worker      add_custom_target("pw_tests.${group}")
359*61c4878aSAndroid Build Coastguard Worker      add_custom_target("pw_run_tests.${group}")
360*61c4878aSAndroid Build Coastguard Worker    endif()
361*61c4878aSAndroid Build Coastguard Worker
362*61c4878aSAndroid Build Coastguard Worker    add_dependencies("pw_tests.${group}" "${TEST_NAME}.bin")
363*61c4878aSAndroid Build Coastguard Worker    add_dependencies("pw_run_tests.${group}" "${TEST_NAME}.run")
364*61c4878aSAndroid Build Coastguard Worker  endforeach()
365*61c4878aSAndroid Build Coastguard Workerendfunction(pw_add_test_to_groups)
366