From 63c17979971b86f4a1ff8cb7808d3edae51c5b93 Mon Sep 17 00:00:00 2001 From: Federico Aponte Date: Sat, 17 Feb 2024 11:37:18 +0100 Subject: [PATCH] cleanup: simpler GoogleTest dependency management Signed-off-by: Federico Aponte --- CMakeListsGtestInclude.cmake | 33 --------------- cmake/modules/gtest.cmake | 53 +++++------------------- test/drivers/CMakeLists.txt | 56 ++++++++++---------------- test/libscap/CMakeLists.txt | 10 +---- userspace/libsinsp/test/CMakeLists.txt | 4 +- 5 files changed, 36 insertions(+), 120 deletions(-) delete mode 100644 CMakeListsGtestInclude.cmake diff --git a/CMakeListsGtestInclude.cmake b/CMakeListsGtestInclude.cmake deleted file mode 100644 index 01f6e5ed26..0000000000 --- a/CMakeListsGtestInclude.cmake +++ /dev/null @@ -1,33 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 -# -# Copyright (C) 2023 The Falco Authors. -# -# 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. -# - -cmake_minimum_required(VERSION 3.12) - -project(googletest-download NONE) - -include(ExternalProject) -ExternalProject_Add(googletest - GIT_REPOSITORY https://github.com/google/googletest.git - GIT_TAG "release-1.12.1" - SOURCE_DIR "${PROJECT_BINARY_DIR}/googletest-src" - BINARY_DIR "${PROJECT_BINARY_DIR}/googletest-build" - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - UPDATE_COMMAND "" - TEST_COMMAND "" -) diff --git a/cmake/modules/gtest.cmake b/cmake/modules/gtest.cmake index a94ed62828..272609e9cb 100644 --- a/cmake/modules/gtest.cmake +++ b/cmake/modules/gtest.cmake @@ -14,48 +14,15 @@ option(USE_BUNDLED_GTEST "Enable building of the bundled gtest" ${USE_BUNDLED_DEPS}) -if(GTEST_INCLUDE_DIR) - # we already have gtest -elseif(NOT USE_BUNDLED_GTEST) - find_path(GTEST_INCLUDE_DIR PATH_SUFFIXES gtest NAMES gtest.h) - find_library(GTEST_LIB NAMES gtest) - find_library(GTEST_MAIN_LIB NAMES gtest_main) - if(GTEST_INCLUDE_DIR AND GTEST_LIB AND GTEST_MAIN_LIB) - message(STATUS "Found gtest: include: ${GTEST_INCLUDE_DIR}, lib: ${GTEST_LIB}, main lib: ${GTEST_MAIN_LIB}") - else() - message(FATAL_ERROR "Couldn't find system gtest") - endif() +if(USE_BUNDLED_GTEST) + include(FetchContent) + FetchContent_Declare(googletest + URL https://github.com/google/googletest/archive/refs/tags/release-1.12.1.tar.gz + URL_HASH SHA256=81964fe578e9bd7c94dfdb09c8e4d6e6759e19967e397dbea48d1c10e45d0df2 + ) + # For Windows: Prevent overriding the parent project's compiler/linker settings + set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + FetchContent_MakeAvailable(googletest) else() - # https://github.com/google/googletest/tree/main/googletest#incorporating-into-an-existing-cmake-project - # Download and unpack googletest at configure time - configure_file(CMakeListsGtestInclude.cmake ${PROJECT_BINARY_DIR}/googletest-download/CMakeLists.txt) - execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . - RESULT_VARIABLE result - WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/googletest-download ) - if(result) - message(FATAL_ERROR "CMake step for googletest failed: ${result}") - endif() - execute_process(COMMAND ${CMAKE_COMMAND} --build . - RESULT_VARIABLE result - WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/googletest-download ) - if(result) - message(FATAL_ERROR "Build step for googletest failed: ${result}") - endif() - - # Add googletest directly to our build. This defines - # the gtest and gtest_main targets. - add_subdirectory(${PROJECT_BINARY_DIR}/googletest-src - ${PROJECT_BINARY_DIR}/googletest-build - EXCLUDE_FROM_ALL) - - set(GTEST_INCLUDE_DIR "${gtest_SOURCE_DIR}/include") - set(GTEST_MAIN_LIB "gtest_main") - install(DIRECTORY "${GTEST_INCLUDE_DIR}" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${LIBS_PACKAGE_NAME}" - COMPONENT "libs-deps") + find_package(GTest REQUIRED) endif() - -if(NOT TARGET gtest) - add_custom_target(gtest) -endif() - -include_directories("${GTEST_INCLUDE_DIR}") diff --git a/test/drivers/CMakeLists.txt b/test/drivers/CMakeLists.txt index a8ae4ad41a..a9350158e8 100644 --- a/test/drivers/CMakeLists.txt +++ b/test/drivers/CMakeLists.txt @@ -20,36 +20,6 @@ file(GLOB_RECURSE ACTIONS_TEST_SUITE ${CMAKE_CURRENT_SOURCE_DIR}/test_suites/act include(libscap) -set(DRIVERS_TEST_SOURCES - ./start_tests.cpp - ./event_class/event_class.cpp - ./flags/capabilities.cpp - ./helpers/proc_parsing.cpp - ./helpers/file_opener.cpp - "${SYSCALL_EXIT_TEST_SUITE}" - "${SYSCALL_ENTER_TEST_SUITE}" - "${GENERIC_TRACEPOINTS_TEST_SUITE}" - "${ACTIONS_TEST_SUITE}" -) - -set(DRIVERS_TEST_INCLUDE - PRIVATE - "${GTEST_INCLUDE}" - "${LIBS_DIR}/driver/" - "${LIBSCAP_INCLUDE_DIRS}" -) - -set(DRIVERS_TEST_LINK_LIBRARIES - scap - "${GTEST_LIB}" - "${GTEST_MAIN_LIB}" -) - -set(DRIVERS_TEST_DEPENDECIES - scap - gtest -) - ## IA32 tests are only available on x86_64 if(${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "x86_64" AND ENABLE_IA32_TESTS) add_executable(ia32 ./helpers/ia32.c) @@ -63,7 +33,25 @@ endif() add_compile_options(${FALCOSECURITY_LIBS_USERSPACE_COMPILE_FLAGS}) add_link_options(${FALCOSECURITY_LIBS_USERSPACE_LINK_FLAGS}) -add_executable(drivers_test ${DRIVERS_TEST_SOURCES}) -target_include_directories(drivers_test ${DRIVERS_TEST_INCLUDE}) -target_link_libraries(drivers_test ${DRIVERS_TEST_LINK_LIBRARIES}) -add_dependencies(drivers_test ${DRIVERS_TEST_DEPENDECIES}) + +add_executable(drivers_test + start_tests.cpp + event_class/event_class.cpp + flags/capabilities.cpp + helpers/proc_parsing.cpp + helpers/file_opener.cpp + ${SYSCALL_EXIT_TEST_SUITE} + ${SYSCALL_ENTER_TEST_SUITE} + ${GENERIC_TRACEPOINTS_TEST_SUITE} + ${ACTIONS_TEST_SUITE} +) +target_include_directories(drivers_test +PRIVATE + ${LIBS_DIR}/driver + ${LIBSCAP_INCLUDE_DIRS} +) +target_link_libraries(drivers_test + scap + GTest::gtest + GTest::gtest_main +) diff --git a/test/libscap/CMakeLists.txt b/test/libscap/CMakeLists.txt index e88603ebd0..1a2d055818 100644 --- a/test/libscap/CMakeLists.txt +++ b/test/libscap/CMakeLists.txt @@ -30,7 +30,6 @@ configure_file ( set(LIBSCAP_TESTS_INCLUDE PRIVATE - "${GTEST_INCLUDE}" "${CMAKE_CURRENT_SOURCE_DIR}" # for test helpers "${LIBS_DIR}" "${LIBS_DIR}/userspace" @@ -38,18 +37,13 @@ set(LIBSCAP_TESTS_INCLUDE "${CMAKE_CURRENT_BINARY_DIR}" # used to include `libscap_test_var.h` ) -# Needed by gtest -find_package(Threads) - set(LIBSCAP_TESTS_LIBRARIES - "${GTEST_LIB}" - "${GTEST_MAIN_LIB}" - "${CMAKE_THREAD_LIBS_INIT}" scap + GTest::gtest + GTest::gtest_main ) set(LIBSCAP_TESTS_DEPENDENCIES - gtest scap ) diff --git a/userspace/libsinsp/test/CMakeLists.txt b/userspace/libsinsp/test/CMakeLists.txt index 06ccdf475e..78f6bbb39e 100644 --- a/userspace/libsinsp/test/CMakeLists.txt +++ b/userspace/libsinsp/test/CMakeLists.txt @@ -188,8 +188,8 @@ target_include_directories(unit-test-libsinsp target_link_libraries(unit-test-libsinsp sinsp - "${GTEST_LIB}" - "${GTEST_MAIN_LIB}" + GTest::gtest + GTest::gtest_main "${TBB_LIB}" "${JSONCPP_LIB}" )