Skip to content
Permalink
Browse files

cmake: modernise GTest integration

Change-Id: I02a24c1586d205d3128f24e8b74606b4af6191bd
  • Loading branch information...
riebl committed Dec 29, 2016
1 parent 3656add commit 135df088a5081df52361ac30da04c46ffdfaee12
@@ -75,7 +75,7 @@ install:
script:
- mkdir -p "${HOME}/build"
- pushd "${HOME}/build"
- cmake -DENABLE_TESTS=ON ${CMAKE_GEOGRAPHICLIB_FLAGS} ${CMAKE_CRYPTOPP_FLAGS} ${TRAVIS_BUILD_DIR}
- cmake -DBUILD_TESTS=ON ${CMAKE_GEOGRAPHICLIB_FLAGS} ${CMAKE_CRYPTOPP_FLAGS} ${TRAVIS_BUILD_DIR}
- cmake --build .
- ctest
- popd
@@ -13,10 +13,10 @@ if(ENABLE_CONAN)
conan_basic_setup()
endif()

option(ENABLE_TESTS "Build unit tests" OFF)
if(ENABLE_TESTS)
option(BUILD_TESTS "Build unit tests" OFF)
if(BUILD_TESTS)
enable_testing()
include(gtest/GTestFramework.cmake)
add_subdirectory(gtest)
endif()

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
@@ -38,14 +38,6 @@ find_package(OpenSSL 1.1.0)
include(CompatBoostTargets)
include(CompatOpenSSLTargets)

# Add subdirectory only when tests are enabled
## DIR subdirectory name
macro(add_test_subdirectory DIR)
if(ENABLE_TESTS)
add_subdirectory(${DIR})
endif(ENABLE_TESTS)
endmacro(add_test_subdirectory)

add_subdirectory(vanetza/asn1)
add_subdirectory(vanetza/btp)
add_subdirectory(vanetza/common)
@@ -0,0 +1,53 @@
set(GTest_VERSION 1.8.0)
set(GTest_ARCHIVE_NAME release-${GTest_VERSION})
set(GTest_ARCHIVE ${GTest_ARCHIVE_NAME}.tar.gz)
set(GTest_ARCHIVE_SHA1 e7e646a6204638fe8e87e165292b8dd9cd4c36ed)

set(GTest_DIR ${PROJECT_SOURCE_DIR}/gtest)
set(GTest_ARCHIVE_DIR ${GTest_DIR}/googletest-${GTest_ARCHIVE_NAME}/googletest)
set(GTest_SOURCE_DIR ${GTest_ARCHIVE_DIR}/src)
set(GTest_INCLUDE_DIR ${GTest_ARCHIVE_DIR}/include)

set(GTest_LIBRARY gtest)
set(GTest_LIBRARY_SOURCES ${GTest_SOURCE_DIR}/gtest-all.cc)
set(GTest_MAIN_LIBRARY gtest_main)
set(GTest_MAIN_LIBRARY_SOURCES ${GTest_SOURCE_DIR}/gtest_main.cc)

file(DOWNLOAD
https://github.com/google/googletest/archive/${GTest_ARCHIVE}
${GTest_DIR}/${GTest_ARCHIVE}
EXPECTED_HASH SHA1=${GTest_ARCHIVE_SHA1})
add_custom_target(download_gtest DEPENDS ${GTest_DIR}/${GTest_ARCHIVE})

add_custom_command(
OUTPUT ${GTest_LIBRARY_SOURCES} ${GTest_MAIN_LIBRARY_SOURCES}
COMMAND ${CMAKE_COMMAND} -E tar xfz ${GTest_DIR}/${GTest_ARCHIVE}
DEPENDS download_gtest
COMMENT "Extracting ${GTest_ARCHIVE} in ${GTest_DIR}"
WORKING_DIRECTORY ${GTest_DIR}
VERBATIM)
add_custom_target(extract_gtest DEPENDS ${GTest_LIBRARY_SOURCES} ${GTest_MAIN_LIBRARY_SOURCES})

# GTest library for tests with own main function
add_library(${GTest_LIBRARY} ${GTest_LIBRARY_SOURCES})
target_include_directories(${GTest_LIBRARY} PRIVATE ${GTest_ARCHIVE_DIR})
target_include_directories(${GTest_LIBRARY} SYSTEM PUBLIC ${GTest_INCLUDE_DIR})
add_dependencies(${GTest_LIBRARY} extract_gtest)

# GTest library providing main function for tests
add_library(${GTest_MAIN_LIBRARY} ${GTest_MAIN_LIBRARY_SOURCES})
target_link_libraries(${GTest_MAIN_LIBRARY} PUBLIC ${GTest_LIBRARY})
add_dependencies(${GTest_MAIN_LIBRARY} extract_gtest)

# Look for threading library and configure GTest accordingly
set(CMAKE_THREAD_PREFER_PTHREAD True)
set(THREADS_PREFER_PTHREAD_FLAG True)
find_package(Threads QUIET)
if(CMAKE_USE_PTHREADS_INIT)
message(STATUS "Build GTest with pthread support")
target_compile_definitions(${GTest_LIBRARY} PUBLIC "GTEST_HAS_PTHREAD=1")
target_link_libraries(${GTest_LIBRARY} PUBLIC Threads::Threads)
else()
message(STATUS "Build GTest without threading")
target_compile_definitions(${GTest_LIBRARY} PUBLIC "GTEST_HAS_PTHREAD=0")
endif()
@@ -1,48 +1,60 @@
# Prepare GTest environment and create MODULE_test interface library
## MODULE module under test
## SRCS... optional list of sources to link with each subsequent gtest
macro(prepare_gtest MODULE)
add_library(${MODULE}_test INTERFACE)
target_link_libraries(${MODULE}_test INTERFACE ${MODULE})
set(VANETZA_GTEST_LINK_LIBRARIES ${MODULE}_test)
if("${ARGC}" GREATER "1")
add_library(${MODULE}_test_objs OBJECT ${ARGN})
get_target_property(_module_inc_dirs ${MODULE} INTERFACE_INCLUDE_DIRECTORIES)
target_include_directories(${MODULE}_test_objs PUBLIC ${_module_inc_dirs} ${GTest_INCLUDE_DIR})
set(VANETZA_GTEST_OBJECTS $<TARGET_OBJECTS:${MODULE}_test_objs>)
else()
set(VANETZA_GTEST_OBJECTS "")
endif()
endmacro()
include(CMakeParseArguments)

# Add a test case using Google Testing Framework
## NAME name of the test case
## SRCS... variable number of source files
macro(add_gtest NAME)
add_executable(GTest_${NAME} ${ARGN} ${VANETZA_GTEST_OBJECTS})
set_target_properties(GTest_${NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/tests)
target_link_libraries(GTest_${NAME} ${GTest_MAIN_LIBRARY})
if(VANETZA_GTEST_LINK_LIBRARIES)
target_link_libraries(GTest_${NAME} ${VANETZA_GTEST_LINK_LIBRARIES})
#
# configure_gtest_directory(
# [SOURCES <srcs...>]
# [LINK_LIBRARIES <libs...>]
# [INCLUDE_DIRECTORIES <dirs...>]
# [COMPILE_DEFINITIONS <defs...>])
#
# Set GTest properties common for all unit test executables in current directory.
#
function(configure_gtest_directory)
cmake_parse_arguments(args "" "" "SOURCES;LINK_LIBRARIES;INCLUDE_DIRECTORIES;COMPILE_DEFINITIONS" ${ARGN})
if(args_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "unknown arguments given to configure_gtest_directory: ${args_UNPARSED_ARGUMENTS}")
endif()
add_test(NAME ${NAME}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND GTest_${NAME})
endmacro(add_gtest)

# Set target property for a GTest
## NAME name of the test case
## PROP property name
## ARGS passed to set_property(TARGET) command
macro(set_gtest_property NAME PROP)
set_property(TARGET GTest_${NAME} PROPERTY ${PROP} ${ARGN})
endmacro(set_gtest_property)
set_property(DIRECTORY PROPERTY GTEST_SOURCES ${args_SOURCES})
set_property(DIRECTORY PROPERTY GTEST_LINK_LIBRARIES ${args_LINK_LIBRARIES})
set_property(DIRECTORY PROPERTY GTEST_INCLUDE_DIRECTORIES ${args_INCLUDE_DIRECTORIES})
set_property(DIRECTORY PROPERTY GTEST_COMPILE_DEFINITIONS ${args_COMPILE_DEFINITIONS})
endfunction()

#
# add_gtest(<name>
# [SOURCES] <srcs...>
# [LINK_LIBRARIES <libs...>]
# [INCLUDE_DIRECTORIES <dirs...>]
# [COMPILE_DEFINITIONS <defs...>])
#
# Add a CTest unit test executable using Google Test.
# The executable will be built as specified by given arguments and directory properties.
#
function(add_gtest name)
set(target GTest_${name})
cmake_parse_arguments(args "" "" "SOURCES;LINK_LIBRARIES;INCLUDE_DIRECTORIES;COMPILE_DEFINITIONS" ${ARGN})

# Link libraries to a GTest
## NAME name of the test case
## LIBS... variable number of libraries
macro(link_gtest NAME)
target_link_libraries(GTest_${NAME} ${ARGN})
endmacro(link_gtest)
get_directory_property(sources GTEST_SOURCES)
list(APPEND sources "${args_SOURCES}" "${args_UNPARSED_ARGUMENTS}")
get_directory_property(link_libraries GTEST_LINK_LIBRARIES)
list(APPEND link_libraries "${args_LINK_LIBRARIES}" ${GTest_MAIN_LIBRARY})
get_directory_property(include_directories GTEST_INCLUDE_DIRECTORIES)
list(APPEND include_directories "${args_INCLUDE_DIRECTORIES}" ${GTest_INCLUDE_DIR})
get_directory_property(compile_definitions GTEST_COMPILE_DEFINITIONS)
list(APPEND compile_definitions "${args_COMPILE_DEFINITIONS}")

if(NOT sources)
message(FATAL_ERROR "no sources given to add_gtest")
endif()

add_executable(${target} ${sources})
set_target_properties(${target} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/tests
LINK_LIBRARIES "${link_libraries}"
INCLUDE_DIRECTORIES "${include_directories}"
COMPILE_DEFINITIONS "${compile_definitions}")
add_test(NAME ${name}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${target})
endfunction()
@@ -13,3 +13,14 @@ function(add_vanetza_component name)
install(TARGETS ${name} EXPORT ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR})
set_property(GLOBAL APPEND PROPERTY VANETZA_COMPONENTS ${name})
endfunction()

#
# add_test_subdirectory(<directory>)
#
# Add subdirectory only when tests are enabled via BUILD_TESTS
#
function(add_test_subdirectory directory)
if(BUILD_TESTS)
add_subdirectory(${directory})
endif()
endfunction(add_test_subdirectory)
@@ -0,0 +1,6 @@
include(GoogleTestTargets)

# export some variables into parent scope
set(GTest_LIBRARY ${GTest_LIBRARY} PARENT_SCOPE)
set(GTest_MAIN_LIBRARY ${GTest_MAIN_LIBRARY} PARENT_SCOPE)
set(GTest_INCLUDE_DIR ${GTest_INCLUDE_DIR} PARENT_SCOPE)

This file was deleted.

@@ -1,5 +1,5 @@
include(UseGTest)
configure_gtest_directory(LINK_LIBRARIES asn1)

prepare_gtest(asn1)
add_gtest(asn1c_wrapper asn1c_wrapper.cpp)

@@ -1,5 +1,5 @@
include(UseGTest)
prepare_gtest(btp)
configure_gtest_directory(LINK_LIBRARIES btp)

add_gtest(BtpDataIndication data_indication.cpp)
add_gtest(BtpHeader header.cpp)
@@ -1,5 +1,5 @@
include(UseGTest)
prepare_gtest(common)
configure_gtest_directory(LINK_LIBRARIES common)

add_gtest(BitNumber bit_number.cpp)
add_gtest(ByteBuffer byte_buffer.cpp)
@@ -1,5 +1,5 @@
include(UseGTest)
prepare_gtest(dcc)
configure_gtest_directory(LINK_LIBRARIES dcc)

add_gtest(AccessControl access_control.cpp)
add_gtest(BurstBudget burst_budget.cpp)
@@ -1,5 +1,5 @@
include(UseGTest)
prepare_gtest(facilities)
configure_gtest_directory(LINK_LIBRARIES facilities)

add_gtest(PathHistory path_history.cpp)
add_gtest(PathPoint path_point.cpp)
@@ -1,5 +1,5 @@
include(UseGTest)
prepare_gtest(geonet)
configure_gtest_directory(LINK_LIBRARIES geonet)

add_gtest(Address address.cpp)
add_gtest(Areas areas.cpp)
@@ -18,7 +18,7 @@ add_gtest(RouterIndicate router_indicate.cpp)
add_gtest(SequenceNumber sequence_number.cpp)
add_gtest(Timestamp timestamp.cpp)
add_gtest(TrafficClass traffic_class.cpp)
add_gtest(Routing network_topology.cpp routing.cpp)
set_gtest_property(Routing INCLUDE_DIRECTORIES ${GeographicLib_INCLUDE_DIR})
link_gtest(Routing ${GeographicLib_LIBRARY})
add_gtest(Routing
SOURCES network_topology.cpp routing.cpp
LINK_LIBRARIES GeographicLib::GeographicLib)
add_gtest(RouterRequest router_request)
@@ -1,5 +1,5 @@
include(UseGTest)
prepare_gtest(gnss)
configure_gtest_directory(LINK_LIBRARIES gnss)

add_gtest(NMEA nmea.cpp)

@@ -1,12 +1,11 @@
include(UseGTest)
prepare_gtest(net)
configure_gtest_directory(LINK_LIBRARIES net)

add_gtest(ChunkPacket chunk_packet.cpp)
add_gtest(CohesivePacket cohesive_packet.cpp)
add_gtest(MacAddress mac_address.cpp)
add_gtest(OsiLayer osi_layer.cpp)

if(VANETZA_NET_WITH_POSIX)
set(VANETZA_MODULE_TEST net-posix)
add_gtest(EthernetHeader ethernet_header.cpp)
endif()

0 comments on commit 135df08

Please sign in to comment.
You can’t perform that action at this time.