1# Copyright (c) 2021-2022 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 34 if(NOT PANDA_SKIP_GTESTS) 35 add_dependencies(tests gtests) 36 endif() 37endif() 38 39# Add Googletest-based tests to the source tree. 40# 41# Example usage: 42# 43# panda_add_gtest(NAME test_name 44# OUTPUT_DIRECTORY /path/to/output/dir 45# SOURCES 46# tests/unit1_test.c 47# tests/unit2_test.c 48# INCLUDE_DIRS 49# path/to/include1 50# path/to/include2 51# LIBRARIES 52# component1 component2 53# SANITIZERS sanitizer1,sanitizer2,.. 54# ) 55# 56# Available sanitizers: address, thread, undefined 57# 58# This will create a target test_name which consists of the sources defined 59# in SOURCES and linked with libraries defined in LIBRARIES. If the list of 60# paths is specified in INCLUDE_DIRS, these paths will be added as include 61# directories for the test_name target. 62# 63# If OUTPUT_DIRECTORY is not defined, the binary will be put to bin-gtests 64# directory at the build tree root. 65# 66# Notes: 67# * This function is a no-op if Googletest is not found. 68# * test_name behaves as a standard CMake target, i.e. such operations as 69# target_compile_options, etc. are supported. 70# 71# Additional actions on test_name include: 72# * Target-specific definition PANDA_GTEST is added. 73# * Googletest-specific libraries are linked to test_name by default, 74# no need to set them explicitly. 75 76function(panda_add_gtest) 77 if(NOT PANDA_WITH_TESTS) 78 return() 79 endif() 80 if(NOT "OUTPUT_DIRECTORY" IN_LIST ARGV) 81 list(APPEND ARGV "OUTPUT_DIRECTORY" "${PANDA_BINARY_ROOT}/bin-gtests") 82 endif() 83 common_add_gtest(${ARGV}) 84endfunction() 85