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}/PandaCmakeFunctions.cmake) 16 17function(common_add_gtest) 18 set(prefix ARG) 19 set(noValues CONTAINS_MAIN NO_CORES RAPIDCHECK_ON USE_CATCH2) 20 set(singleValues NAME OUTPUT_DIRECTORY ENCLOSING_TARGET TSAN_EXTRA_OPTIONS PANDA_STD_LIB ARK_BOOTCLASSPATH) 21 set(multiValues SOURCES INCLUDE_DIRS LIBRARIES SANITIZERS) 22 23 cmake_parse_arguments(${prefix} 24 "${noValues}" 25 "${singleValues}" 26 "${multiValues}" 27 ${ARGN}) 28 29 if (ARG_RAPIDCHECK_ON AND DEFINED DONT_USE_RAPIDCHECK) 30 return() 31 endif() 32 33 if (NOT DEFINED ARG_ENCLOSING_TARGET) 34 message(FATAL_ERROR "Enclosing target is not defined") 35 endif() 36 if (NOT DEFINED ARG_OUTPUT_DIRECTORY) 37 message(FATAL_ERROR "OUTPUT_DIRECTORY is not defined") 38 endif() 39 40 if(ARG_USE_CATCH2 AND NOT ARG_CONTAINS_MAIN) 41 set(ARG_SOURCES ${ARG_SOURCES} ${PANDA_ROOT}/third_party/rapidcheck/test/main.cpp) 42 endif() 43 44 if (ARG_RAPIDCHECK_ON) 45 panda_add_executable(${ARG_NAME} RAPIDCHECK_ON ${ARG_SOURCES}) 46 set_target_properties(${ARG_NAME} PROPERTIES LINK_FLAGS "-frtti -fexceptions") 47 target_compile_definitions(${ARG_NAME} PRIVATE PANDA_RAPIDCHECK) 48 target_compile_options(${ARG_NAME} PRIVATE "-frtti" "-fexceptions" "-fPIC") 49 target_compile_definitions(${ARG_NAME} PUBLIC PANDA_RAPIDCHECK) 50 else() 51 panda_add_executable(${ARG_NAME} ${ARG_SOURCES}) 52 endif() 53 54 if (ARG_USE_CATCH2) 55 target_compile_definitions(${ARG_NAME} PUBLIC PANDA_CATCH2) 56 else() 57 target_compile_definitions(${ARG_NAME} PUBLIC PANDA_GTEST) 58 endif() 59 60 if(PANDA_CI_TESTING_MODE STREQUAL "Nightly") 61 target_compile_definitions(${ARG_NAME} PUBLIC PANDA_NIGHTLY_TEST_ON) 62 endif() 63 # By default tests are just built, running is available either via an 64 # umbrella target or via `ctest -R ...`. But one can always do something 65 # like this if really needed: 66 # add_custom_target(${ARG_NAME}_run 67 # COMMENT "Run ${ARG_NAME}" 68 # COMMAND ${CMAKE_CTEST_COMMAND} 69 # DEPENDS ${ARG_NAME}) 70 if (ARG_USE_CATCH2) 71 foreach(include_dir ${ARG_INCLUDE_DIRS} ${PANDA_ROOT}/third_party/rapidcheck/include) 72 target_include_directories(${ARG_NAME} PUBLIC ${include_dir}) 73 endforeach() 74 else() 75 foreach(include_dir ${ARG_INCLUDE_DIRS} ${PANDA_THIRD_PARTY_SOURCES_DIR}/googletest/googlemock/include) 76 target_include_directories(${ARG_NAME} PUBLIC ${include_dir}) 77 endforeach() 78 endif() 79 80 if (NOT ARG_USE_CATCH2) 81 if (ARG_CONTAINS_MAIN) 82 target_link_libraries(${ARG_NAME} gtest) 83 else() 84 target_link_libraries(${ARG_NAME} gtest_main) 85 endif() 86 endif() 87 88 if (NOT (PANDA_TARGET_MOBILE OR PANDA_TARGET_OHOS)) 89 target_link_libraries(${ARG_NAME} pthread) 90 endif() 91 target_link_libraries(${ARG_NAME} ${ARG_LIBRARIES}) 92 add_dependencies(${ARG_ENCLOSING_TARGET} ${ARG_NAME}) 93 94 if (ARG_RAPIDCHECK_ON) 95 target_link_libraries(${ARG_NAME} rapidcheck) 96 target_link_libraries(${ARG_NAME} rapidcheck_catch) 97 target_link_libraries(${ARG_NAME} rapidcheck_gtest) 98 target_link_libraries(${ARG_NAME} rapidcheck_gmock) 99 add_dependencies(${ARG_NAME} rapidcheck) 100 endif() 101 102 panda_add_sanitizers(TARGET ${ARG_NAME} SANITIZERS ${ARG_SANITIZERS}) 103 104 set(prlimit_prefix "") 105 if (ARG_NO_CORES) 106 set(prlimit_prefix prlimit --core=0) 107 endif() 108 set_target_properties(${ARG_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${ARG_OUTPUT_DIRECTORY}") 109 110 set(tsan_options "") 111 if ("thread" IN_LIST PANDA_SANITIZERS_LIST) 112 if (DEFINED ENV{TSAN_OPTIONS}) 113 set(tsan_options "TSAN_OPTIONS=$ENV{TSAN_OPTIONS},${ARG_TSAN_EXTRA_OPTIONS}") 114 endif() 115 endif() 116 117 # Yes, this is hardcoded. No, do not ask for an option. Go and fix your tests. 118 if (PANDA_CI_TESTING_MODE STREQUAL "Nightly") 119 set(timeout_prefix timeout --preserve-status --signal=USR1 --kill-after=10s 40m) 120 else () 121 set(timeout_prefix timeout --preserve-status --signal=USR1 --kill-after=10s 20m) 122 endif() 123 124 if (ARG_RAPIDCHECK_ON) 125 add_custom_target(${ARG_NAME}_rapidcheck_tests 126 COMMAND ${tsan_options} ${prlimit_prefix} ${timeout_prefix} 127 ${PANDA_RUN_PREFIX} "${ARG_OUTPUT_DIRECTORY}/${ARG_NAME}" 128 DEPENDS ${ARG_ENCLOSING_TARGET} 129 ) 130 add_dependencies(gtests ${ARG_NAME}_rapidcheck_tests) 131 else() 132 set(PANDA_STD_LIB "") 133 if (DEFINED ARG_PANDA_STD_LIB) 134 set(PANDA_STD_LIB "PANDA_STD_LIB=${ARG_PANDA_STD_LIB}") 135 endif() 136 set(ARK_BOOTCLASSPATH "") 137 if (DEFINED ARG_ARK_BOOTCLASSPATH) 138 set(ARK_BOOTCLASSPATH "ARK_BOOTCLASSPATH=${ARG_ARK_BOOTCLASSPATH}") 139 endif() 140 add_custom_target(${ARG_NAME}_gtests 141 COMMAND ${PANDA_STD_LIB} ${ARK_BOOTCLASSPATH} 142 ${tsan_options} ${prlimit_prefix} ${timeout_prefix} 143 ${PANDA_RUN_PREFIX} "${ARG_OUTPUT_DIRECTORY}/${ARG_NAME}" 144 --gtest_shuffle --gtest_death_test_style=threadsafe 145 DEPENDS ${ARG_ENCLOSING_TARGET} 146 ) 147 add_dependencies(gtests ${ARG_NAME}_gtests) 148 endif() 149endfunction() 150