From d0a48ee194718a02a1fc3d8d23f4b4caea82d366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dietrich=20Kr=C3=B6nke?= Date: Tue, 19 Jul 2022 14:57:04 +0200 Subject: [PATCH] iox-#1516 Use GTest detection in own cmake macro Write a central macro used by other libraries to detect if GTest or gtest_vendor package is present --- iceoryx_binding_c/CMakeLists.txt | 3 +- iceoryx_binding_c/package.xml | 2 ++ iceoryx_binding_c/test/CMakeLists.txt | 4 +-- iceoryx_dds/CMakeLists.txt | 3 +- iceoryx_dds/test/CMakeLists.txt | 4 +-- iceoryx_hoofs/CMakeLists.txt | 14 +++------ iceoryx_hoofs/cmake/IceoryxPlatform.cmake | 18 +++++++++++ iceoryx_hoofs/package.xml | 2 ++ iceoryx_hoofs/test/CMakeLists.txt | 3 +- iceoryx_hoofs/testing/CMakeLists.txt | 3 +- iceoryx_posh/CMakeLists.txt | 24 +++------------ iceoryx_posh/package.xml | 2 ++ iceoryx_posh/test/CMakeLists.txt | 7 +---- iceoryx_posh/testing/CMakeLists.txt | 37 +++++++++++++++++++++++ 14 files changed, 78 insertions(+), 48 deletions(-) create mode 100644 iceoryx_posh/testing/CMakeLists.txt diff --git a/iceoryx_binding_c/CMakeLists.txt b/iceoryx_binding_c/CMakeLists.txt index abccf073767..38900d6cb1a 100644 --- a/iceoryx_binding_c/CMakeLists.txt +++ b/iceoryx_binding_c/CMakeLists.txt @@ -75,6 +75,7 @@ iox_add_library( # ########## build test executables ########## # -if(BUILD_TEST) +if(BUILD_TESTING) + detect_gtest_package() add_subdirectory(test) endif() diff --git a/iceoryx_binding_c/package.xml b/iceoryx_binding_c/package.xml index 7d5472f3597..1919f698721 100644 --- a/iceoryx_binding_c/package.xml +++ b/iceoryx_binding_c/package.xml @@ -17,6 +17,8 @@ doxygen + gtest_vendor + cmake diff --git a/iceoryx_binding_c/test/CMakeLists.txt b/iceoryx_binding_c/test/CMakeLists.txt index 97008bc5955..eca56d81274 100644 --- a/iceoryx_binding_c/test/CMakeLists.txt +++ b/iceoryx_binding_c/test/CMakeLists.txt @@ -19,7 +19,6 @@ project(test_binding_c VERSION 0) find_package(iceoryx_hoofs_testing REQUIRED) find_package(iceoryx_posh_testing REQUIRED) -find_package(GTest CONFIG REQUIRED) set(PROJECT_PREFIX "binding_c") set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${PROJECT_PREFIX}/test) @@ -28,8 +27,7 @@ file(GLOB_RECURSE MODULETESTS_SRC "${CMAKE_CURRENT_SOURCE_DIR}/moduletests/*.cpp set(TEST_LINK_LIBS ${CODE_COVERAGE_LIBS} - GTest::gtest - GTest::gmock + ${IOX_GTEST_LIBS} iceoryx_binding_c::iceoryx_binding_c iceoryx_hoofs::iceoryx_hoofs iceoryx_hoofs_testing::iceoryx_hoofs_testing diff --git a/iceoryx_dds/CMakeLists.txt b/iceoryx_dds/CMakeLists.txt index 0552354781e..0c3f75fa40f 100644 --- a/iceoryx_dds/CMakeLists.txt +++ b/iceoryx_dds/CMakeLists.txt @@ -119,6 +119,7 @@ iox_add_executable( # ########## build test executables ########## # -if(BUILD_TEST) +if(BUILD_TESTING) + detect_gtest_package() add_subdirectory(test) endif() diff --git a/iceoryx_dds/test/CMakeLists.txt b/iceoryx_dds/test/CMakeLists.txt index 347a15d9f52..3d2d6107b85 100644 --- a/iceoryx_dds/test/CMakeLists.txt +++ b/iceoryx_dds/test/CMakeLists.txt @@ -19,7 +19,6 @@ project(test_iox_to_dds VERSION 0) find_package(iceoryx_hoofs_testing REQUIRED) find_package(iceoryx_posh_testing REQUIRED) -find_package(GTest CONFIG REQUIRED) set(PROJECT_PREFIX "dds_gateway") set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${PROJECT_PREFIX}/test) @@ -28,8 +27,7 @@ file(GLOB_RECURSE MODULETESTS_SRC "${CMAKE_CURRENT_SOURCE_DIR}/moduletests/*.cpp set(TEST_LINK_LIBS ${CODE_COVERAGE_LIBS} - GTest::gtest - GTest::gmock + ${IOX_GTEST_LIBS} iceoryx_hoofs::iceoryx_hoofs iceoryx_hoofs_testing::iceoryx_hoofs_testing iceoryx_posh::iceoryx_posh diff --git a/iceoryx_hoofs/CMakeLists.txt b/iceoryx_hoofs/CMakeLists.txt index 745dfbc46f0..c8ca55b55ed 100644 --- a/iceoryx_hoofs/CMakeLists.txt +++ b/iceoryx_hoofs/CMakeLists.txt @@ -93,18 +93,12 @@ iox_add_library( ########## hoofs testing ########## # -# Finding gtest and adding the subdirectories is split to support the use case of -# building the testing lib without the tests by providing gtest externally -if(NOT GTest_FOUND AND BUILD_TEST) - find_package(GTest CONFIG REQUIRED) -endif() -if(GTest_FOUND) - message(STATUS "GTest was found, building iceoryx_hoofs_testing enabled.") + +if(BUILD_TESTING) + detect_gtest_package() add_subdirectory(testing) - if(BUILD_TEST) - add_subdirectory(test) - endif() + add_subdirectory(test) endif() install( diff --git a/iceoryx_hoofs/cmake/IceoryxPlatform.cmake b/iceoryx_hoofs/cmake/IceoryxPlatform.cmake index f37ba7220f7..6ba21a92afb 100644 --- a/iceoryx_hoofs/cmake/IceoryxPlatform.cmake +++ b/iceoryx_hoofs/cmake/IceoryxPlatform.cmake @@ -154,3 +154,21 @@ if(COVERAGE) message( FATAL_ERROR "You need to run gcov with gcc compiler." ) endif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") endif() + +Macro(detect_gtest_package) + if(NOT GTest_FOUND) + find_package(GTest 1.10.0 EXACT CONFIG QUIET) + if(NOT GTest_FOUND) + message(STATUS "GTest not found, trying to find gtest_vendor package...") + find_package(gtest_vendor CONFIG REQUIRED) + set(IOX_GTEST_LIBS + gtest + gmock) + message(STATUS "gtest_vendor package found successfully") + else() + set(IOX_GTEST_LIBS + GTest::gtest + GTest::gmock) + endif() + endif() +endMacro() diff --git a/iceoryx_hoofs/package.xml b/iceoryx_hoofs/package.xml index 0cfd4847c53..ba7c05ab3b5 100644 --- a/iceoryx_hoofs/package.xml +++ b/iceoryx_hoofs/package.xml @@ -20,6 +20,8 @@ doxygen + gtest_vendor + cmake diff --git a/iceoryx_hoofs/test/CMakeLists.txt b/iceoryx_hoofs/test/CMakeLists.txt index 14449394907..5d961d1a1fd 100644 --- a/iceoryx_hoofs/test/CMakeLists.txt +++ b/iceoryx_hoofs/test/CMakeLists.txt @@ -32,8 +32,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${PROJECT_PREFIX}/test) set(TEST_LINK_LIBS ${CODE_COVERAGE_LIBS} - GTest::gtest - GTest::gmock + ${IOX_GTEST_LIBS} iceoryx_hoofs::iceoryx_hoofs iceoryx_hoofs_testing::iceoryx_hoofs_testing ) diff --git a/iceoryx_hoofs/testing/CMakeLists.txt b/iceoryx_hoofs/testing/CMakeLists.txt index 8b3ef33e642..832b1e4053d 100644 --- a/iceoryx_hoofs/testing/CMakeLists.txt +++ b/iceoryx_hoofs/testing/CMakeLists.txt @@ -25,8 +25,7 @@ iox_add_library( TARGET ${PROJECT_NAME}_testing NAMESPACE iceoryx_hoofs_testing PROJECT_PREFIX ${PREFIX} - PUBLIC_LIBS GTest::gtest - GTest::gmock + PUBLIC_LIBS ${IOX_GTEST_LIBS} PRIVATE_LIBS ${CODE_COVERAGE_LIBS} iceoryx_hoofs ${CMAKE_DL_LIBS} diff --git a/iceoryx_posh/CMakeLists.txt b/iceoryx_posh/CMakeLists.txt index 9d064e3b162..ef5540f7df9 100644 --- a/iceoryx_posh/CMakeLists.txt +++ b/iceoryx_posh/CMakeLists.txt @@ -297,27 +297,11 @@ setup_install_directories_and_export_package( INCLUDE_DIRECTORY include/ ) -if(ROUDI_ENVIRONMENT OR BUILD_TEST) - # - ######### posh roudi environment ########## - # - iox_add_library( - STATIC - TARGET iceoryx_posh_testing - NAMESPACE iceoryx_posh_testing - PROJECT_PREFIX ${PREFIX} - BUILD_INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/testing/include - INSTALL_INTERFACE include/${PREFIX} - PRIVATE_LIBS iceoryx_posh::iceoryx_posh - iceoryx_hoofs::iceoryx_hoofs - iceoryx_posh::iceoryx_posh_roudi - iceoryx_hoofs_testing::iceoryx_hoofs_testing - FILES - testing/roudi_environment/runtime_test_interface.cpp - testing/roudi_environment/roudi_environment.cpp - ) +if(ROUDI_ENVIRONMENT OR BUILD_TESTING) + detect_gtest_package() + add_subdirectory(testing) endif() -if(BUILD_TEST) +if(BUILD_TESTING) add_subdirectory(test) endif() diff --git a/iceoryx_posh/package.xml b/iceoryx_posh/package.xml index 03f7c812ade..03667e891f1 100644 --- a/iceoryx_posh/package.xml +++ b/iceoryx_posh/package.xml @@ -17,6 +17,8 @@ doxygen + gtest_vendor + cmake diff --git a/iceoryx_posh/test/CMakeLists.txt b/iceoryx_posh/test/CMakeLists.txt index 44f51b81266..e94c3d7ce8b 100644 --- a/iceoryx_posh/test/CMakeLists.txt +++ b/iceoryx_posh/test/CMakeLists.txt @@ -24,10 +24,6 @@ find_package(iceoryx_posh_testing REQUIRED) set(ICEORYX_POSH_TEST_STACK_SIZE 5000000) -if(BUILD_TEST AND NOT GTest_FOUND) - find_package(GTest CONFIG REQUIRED) -endif(BUILD_TEST AND NOT GTest_FOUND) - set(PROJECT_PREFIX "posh") # Set path for input files in test @@ -46,8 +42,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${PROJECT_PREFIX}/test) set(TEST_LINK_LIBS ${CODE_COVERAGE_LIBS} - GTest::gtest - GTest::gmock + ${IOX_GTEST_LIBS} iceoryx_hoofs::iceoryx_hoofs iceoryx_hoofs::iceoryx_platform iceoryx_hoofs_testing::iceoryx_hoofs_testing diff --git a/iceoryx_posh/testing/CMakeLists.txt b/iceoryx_posh/testing/CMakeLists.txt new file mode 100644 index 00000000000..16521f616f1 --- /dev/null +++ b/iceoryx_posh/testing/CMakeLists.txt @@ -0,0 +1,37 @@ +# Copyright (c) 2020 - 2022 by Apex.AI Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +cmake_minimum_required(VERSION 3.16) + +find_package(iceoryx_hoofs_testing REQUIRED) +# +######### posh roudi environment ########## +# +iox_add_library( + STATIC + TARGET iceoryx_posh_testing + NAMESPACE iceoryx_posh_testing + PROJECT_PREFIX ${PREFIX} + BUILD_INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include + INSTALL_INTERFACE include/${PREFIX} + PRIVATE_LIBS iceoryx_posh::iceoryx_posh + iceoryx_hoofs::iceoryx_hoofs + iceoryx_posh::iceoryx_posh_roudi + iceoryx_hoofs_testing::iceoryx_hoofs_testing + FILES + roudi_environment/runtime_test_interface.cpp + roudi_environment/roudi_environment.cpp +) +