xref: /aosp_15_r20/external/pigweed/targets/host/pw_add_test_executable.cmake (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2022 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14include_guard(GLOBAL)
15
16include("$ENV{PW_ROOT}/pw_build/pigweed.cmake")
17include("$ENV{PW_ROOT}/pw_unit_test/test.cmake")
18
19# Used by pw_add_test to instantiate unit test executables for the host.
20#
21# Required Args:
22#
23#   NAME: name for the desired executable
24#   TEST_DEP: the target which provides the tests for this executable
25#   TEST_MAIN: The test main to use, can be "" to use defaults.
26#
27function(pw_add_test_executable_with_main NAME TEST_DEP TEST_MAIN)
28  pw_parse_arguments(
29    NUM_POSITIONAL_ARGS
30      3
31  )
32
33  # CMake requires a source file to determine the LINKER_LANGUAGE.
34  add_executable("${NAME}" EXCLUDE_FROM_ALL
35                 $<TARGET_PROPERTY:pw_build.empty,SOURCES>)
36
37  # Temporarily redirect deprecated googletest pointer to new pointer.
38  if("${pw_unit_test_GOOGLETEST_BACKEND}" STREQUAL "pw_third_party.googletest")
39    message(DEPRECATION
40      "pw_unit_test_GOOGLETEST_BACKEND is deprecated. Set pw_unit_test_BACKEND "
41      "to pw_unit_test.googletest instead."
42    )
43    set(pw_unit_test_BACKEND pw_unit_test.googletest)
44  endif()
45
46  set(test_backend "${pw_unit_test_BACKEND}")
47  if ("${TEST_MAIN}" STREQUAL "")
48    if("${test_backend}" STREQUAL "pw_unit_test.light")
49      set(main pw_unit_test.logging_main)
50    elseif("${test_backend}" STREQUAL "pw_unit_test.googletest")
51      set(main pw_third_party.googletest.gmock_main)
52    elseif("${test_backend}" STREQUAL "pw_third_party.fuzztest")
53      set(main pw_third_party.fuzztest_gtest_main)
54    else()
55      message(FATAL_ERROR
56              "Unsupported test backend selected for host test executables")
57    endif()
58  else()
59    set(main ${TEST_MAIN})
60  endif()
61
62  pw_target_link_targets("${NAME}"
63    PRIVATE
64      "${main}"
65      "${TEST_DEP}"
66  )
67endfunction(pw_add_test_executable_with_main)
68
69# Used by pw_add_test to instantiate unit test executables for the host.
70#
71# Required Args:
72#
73#   NAME: name for the desired executable
74#   TEST_DEP: the target which provides the tests for this executable
75#
76function(pw_add_test_executable NAME TEST_DEP)
77  pw_add_test_executable_with_main(${NAME} ${TEST_DEP} "")
78endfunction(pw_add_test_executable)
79