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