Skip to content

Commit

Permalink
cleanup: simpler GoogleTest dependency management
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Aponte <[email protected]>
  • Loading branch information
federico-sysdig committed Jun 10, 2024
1 parent 7472524 commit 63c1797
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 120 deletions.
33 changes: 0 additions & 33 deletions CMakeListsGtestInclude.cmake

This file was deleted.

53 changes: 10 additions & 43 deletions cmake/modules/gtest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
56 changes: 22 additions & 34 deletions test/drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
)
10 changes: 2 additions & 8 deletions test/libscap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,20 @@ configure_file (

set(LIBSCAP_TESTS_INCLUDE
PRIVATE
"${GTEST_INCLUDE}"
"${CMAKE_CURRENT_SOURCE_DIR}" # for test helpers <helpers/...>
"${LIBS_DIR}"
"${LIBS_DIR}/userspace"
"${PROJECT_BINARY_DIR}"
"${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
)

Expand Down
4 changes: 2 additions & 2 deletions userspace/libsinsp/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
)
Expand Down

0 comments on commit 63c1797

Please sign in to comment.