1b1994897Sopenharmony_ci# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
2b1994897Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
3b1994897Sopenharmony_ci# you may not use this file except in compliance with the License.
4b1994897Sopenharmony_ci# You may obtain a copy of the License at
5b1994897Sopenharmony_ci#
6b1994897Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0
7b1994897Sopenharmony_ci#
8b1994897Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
9b1994897Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
10b1994897Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11b1994897Sopenharmony_ci# See the License for the specific language governing permissions and
12b1994897Sopenharmony_ci# limitations under the License.
13b1994897Sopenharmony_ci# Convenience functions for testing Panda.
14b1994897Sopenharmony_ci
15b1994897Sopenharmony_ciinclude(${CMAKE_CURRENT_LIST_DIR}/CommonTesting.cmake)
16b1994897Sopenharmony_ci
17b1994897Sopenharmony_ciset(PANDA_CI_TESTING_MODE "Default")
18b1994897Sopenharmony_ciif(DEFINED ENV{SCHEDULE_NAME})
19b1994897Sopenharmony_ci    if($ENV{SCHEDULE_NAME} STREQUAL "Nightly")
20b1994897Sopenharmony_ci        set(PANDA_CI_TESTING_MODE "Nightly")
21b1994897Sopenharmony_ci    endif()
22b1994897Sopenharmony_ciendif()
23b1994897Sopenharmony_cimessage(STATUS "PANDA_CI_TESTING_MODE         = ${PANDA_CI_TESTING_MODE}")
24b1994897Sopenharmony_ci
25b1994897Sopenharmony_ciif(PANDA_WITH_TESTS)
26b1994897Sopenharmony_ci    # Target for building all Googletest-based tests:
27b1994897Sopenharmony_ci    add_custom_target(gtests_build COMMENT "Building gtests")
28b1994897Sopenharmony_ci
29b1994897Sopenharmony_ci    # Use a custom target instead of `test` to ensure that running
30b1994897Sopenharmony_ci    # Googletest-based tests depends on building them:
31b1994897Sopenharmony_ci    add_custom_target(gtests
32b1994897Sopenharmony_ci                      COMMENT "Running gtests after building them")
33b1994897Sopenharmony_ci
34b1994897Sopenharmony_ci    if(NOT PANDA_SKIP_GTESTS)
35b1994897Sopenharmony_ci        add_dependencies(tests gtests)
36b1994897Sopenharmony_ci    endif()
37b1994897Sopenharmony_ciendif()
38b1994897Sopenharmony_ci
39b1994897Sopenharmony_ci# Add Googletest-based tests to the source tree.
40b1994897Sopenharmony_ci#
41b1994897Sopenharmony_ci# Example usage:
42b1994897Sopenharmony_ci#
43b1994897Sopenharmony_ci#   panda_add_gtest(NAME test_name
44b1994897Sopenharmony_ci#     OUTPUT_DIRECTORY /path/to/output/dir
45b1994897Sopenharmony_ci#     SOURCES
46b1994897Sopenharmony_ci#       tests/unit1_test.c
47b1994897Sopenharmony_ci#       tests/unit2_test.c
48b1994897Sopenharmony_ci#     INCLUDE_DIRS
49b1994897Sopenharmony_ci#       path/to/include1
50b1994897Sopenharmony_ci#       path/to/include2
51b1994897Sopenharmony_ci#     LIBRARIES
52b1994897Sopenharmony_ci#       component1 component2
53b1994897Sopenharmony_ci#     SANITIZERS sanitizer1,sanitizer2,..
54b1994897Sopenharmony_ci#   )
55b1994897Sopenharmony_ci#
56b1994897Sopenharmony_ci# Available sanitizers: address, thread, undefined
57b1994897Sopenharmony_ci#
58b1994897Sopenharmony_ci# This will create a target test_name which consists of the sources defined
59b1994897Sopenharmony_ci# in SOURCES and linked with libraries defined in LIBRARIES. If the list of
60b1994897Sopenharmony_ci# paths is specified in INCLUDE_DIRS, these paths will be added as include
61b1994897Sopenharmony_ci# directories for the test_name target.
62b1994897Sopenharmony_ci#
63b1994897Sopenharmony_ci# If OUTPUT_DIRECTORY is not defined, the binary will be put to bin-gtests
64b1994897Sopenharmony_ci# directory at the build tree root.
65b1994897Sopenharmony_ci#
66b1994897Sopenharmony_ci# Notes:
67b1994897Sopenharmony_ci#    * This function is a no-op if Googletest is not found.
68b1994897Sopenharmony_ci#    * test_name behaves as a standard CMake target, i.e. such operations as
69b1994897Sopenharmony_ci#      target_compile_options, etc. are supported.
70b1994897Sopenharmony_ci#
71b1994897Sopenharmony_ci# Additional actions on test_name include:
72b1994897Sopenharmony_ci#    * Target-specific definition PANDA_GTEST is added.
73b1994897Sopenharmony_ci#    * Googletest-specific libraries are linked to test_name by default,
74b1994897Sopenharmony_ci#      no need to set them explicitly.
75b1994897Sopenharmony_ci
76b1994897Sopenharmony_cifunction(panda_add_gtest)
77b1994897Sopenharmony_ci    if(NOT PANDA_WITH_TESTS)
78b1994897Sopenharmony_ci        return()
79b1994897Sopenharmony_ci    endif()
80b1994897Sopenharmony_ci    if(NOT "OUTPUT_DIRECTORY" IN_LIST ARGV)
81b1994897Sopenharmony_ci        list(APPEND ARGV "OUTPUT_DIRECTORY" "${PANDA_BINARY_ROOT}/bin-gtests")
82b1994897Sopenharmony_ci    endif()
83b1994897Sopenharmony_ci    common_add_gtest(${ARGV})
84b1994897Sopenharmony_ciendfunction()
85