Skip to content

Commit

Permalink
Fixed build against packaged GTEST and BENCHMARK.
Browse files Browse the repository at this point in the history
  • Loading branch information
xvitaly committed Apr 29, 2024
1 parent 5c4b91e commit 9fd0c40
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 43 deletions.
88 changes: 49 additions & 39 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,64 @@ set(CMAKE_FIND_PACKAGE_SORT_ORDER NATURAL)
if (SCN_TESTS)
# GTest

FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG main
GIT_SHALLOW TRUE
)

# gtest CMake does some flag overriding we don't want, and it's also quite heavy
# Do it manually
if (SCN_USE_EXTERNAL_GTEST)
find_package(GTest CONFIG REQUIRED)
set(SCN_GTEST_LIBRARY "GTest::gtest_main")
else ()
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG main
GIT_SHALLOW TRUE
)

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# gtest CMake does some flag overriding we don't want, and it's also quite heavy
# Do it manually

FetchContent_GetProperties(googletest)
if (NOT googletest)
FetchContent_Populate(googletest)
endif ()
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

find_package(Threads)
FetchContent_GetProperties(googletest)
if (NOT googletest)
FetchContent_Populate(googletest)
endif ()

add_library(scn_gtest
"${googletest_SOURCE_DIR}/googletest/src/gtest-all.cc"
"${googletest_SOURCE_DIR}/googlemock/src/gmock-all.cc"
)
target_include_directories(scn_gtest SYSTEM
PUBLIC
"${googletest_SOURCE_DIR}/googletest/include"
"${googletest_SOURCE_DIR}/googlemock/include"
PRIVATE
"${googletest_SOURCE_DIR}/googletest"
"${googletest_SOURCE_DIR}/googlemock"
)
target_link_libraries(scn_gtest PRIVATE Threads::Threads)
target_compile_features(scn_gtest PUBLIC cxx_std_17)
target_compile_options(scn_gtest PRIVATE $<$<CXX_COMPILER_ID:GNU>: -Wno-psabi>)
find_package(Threads)

add_library(scn_gtest
"${googletest_SOURCE_DIR}/googletest/src/gtest-all.cc"
"${googletest_SOURCE_DIR}/googlemock/src/gmock-all.cc"
)
target_include_directories(scn_gtest SYSTEM
PUBLIC
"${googletest_SOURCE_DIR}/googletest/include"
"${googletest_SOURCE_DIR}/googlemock/include"
PRIVATE
"${googletest_SOURCE_DIR}/googletest"
"${googletest_SOURCE_DIR}/googlemock"
)
target_link_libraries(scn_gtest PRIVATE Threads::Threads)
target_compile_features(scn_gtest PUBLIC cxx_std_17)
target_compile_options(scn_gtest PRIVATE $<$<CXX_COMPILER_ID:GNU>: -Wno-psabi>)
set(SCN_GTEST_LIBRARY "scn_gtest")
endif ()
endif ()

if (SCN_BENCHMARKS)
# Google Benchmark

set(BENCHMARK_ENABLE_TESTING OFF CACHE INTERNAL "Turn off google benchmark tests")
set(BENCHMARK_ENABLE_INSTALL OFF CACHE INTERNAL "Turn off google benchmark install")
FetchContent_Declare(
google-benchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
GIT_TAG v1.8.3
GIT_SHALLOW TRUE
)
list(APPEND SCN_OPTIONAL_DEPENDENCIES "google-benchmark")
if (SCN_USE_EXTERNAL_BENCHMARK)
find_package(benchmark CONFIG REQUIRED)
else ()
set(BENCHMARK_ENABLE_TESTING OFF CACHE INTERNAL "Turn off google benchmark tests")
set(BENCHMARK_ENABLE_INSTALL OFF CACHE INTERNAL "Turn off google benchmark install")
FetchContent_Declare(
google-benchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
GIT_TAG v1.8.3
GIT_SHALLOW TRUE
)
list(APPEND SCN_OPTIONAL_DEPENDENCIES "google-benchmark")
endif ()
endif ()

# simdutf
Expand Down
2 changes: 2 additions & 0 deletions cmake/options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ option(SCN_REGEX_BOOST_USE_ICU "Use ICU with the regex backend (Boost SCN_REGEX_

option(SCN_USE_EXTERNAL_SIMDUTF "Use find_package for simdutf, instead of FetchContent" OFF)
option(SCN_USE_EXTERNAL_FAST_FLOAT "Use find_package for fast_float, instead of FetchContent" OFF)
option(SCN_USE_EXTERNAL_GTEST "Use find_package for GTest, instead of FetchContent" OFF)
option(SCN_USE_EXTERNAL_BENCHMARK "Use find_package for google-benchmark, instead of FetchContent" OFF)
option(SCN_USE_EXTERNAL_REGEX_BACKEND "Use find_package for SCN_REGEX_BACKEND, instead of FetchContent" ON)

option(SCN_USE_32BIT "Compile as 32-bit (gcc or clang only)" OFF)
Expand Down
8 changes: 4 additions & 4 deletions tests/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ add_executable(scn_tests
string_view_test.cpp
unicode_test.cpp
)
target_link_libraries(scn_tests scn_gtest scn_tests_base)
target_link_libraries(scn_tests ${SCN_GTEST_LIBRARY} scn_tests_base)
add_test(NAME scn_tests COMMAND scn_tests)

add_executable(scn_impl_tests
Expand All @@ -70,7 +70,7 @@ add_executable(scn_impl_tests
impl_tests/integer_reader_test.impl_wide_localized.cpp
)
target_link_libraries(scn_impl_tests PRIVATE
scn_gtest scn_tests_base scn_internal)
${SCN_GTEST_LIBRARY} scn_tests_base scn_internal)
add_test(NAME scn_impl_tests COMMAND scn_impl_tests)

if (SCN_TESTS_LOCALIZED)
Expand All @@ -79,7 +79,7 @@ if (SCN_TESTS_LOCALIZED)

localized_tests/integer_test.cpp
)
target_link_libraries(scn_localized_tests scn_gtest scn_tests_base)
target_link_libraries(scn_localized_tests ${SCN_GTEST_LIBRARY} scn_tests_base)
add_test(NAME scn_localized_tests COMMAND scn_localized_tests)
endif ()

Expand All @@ -88,7 +88,7 @@ add_executable(scn_stdin_test

stdin_test.cpp
)
target_link_libraries(scn_stdin_test scn_gtest scn_tests_base)
target_link_libraries(scn_stdin_test ${SCN_GTEST_LIBRARY} scn_tests_base)

add_custom_target(scn_stdin_test_prepare ALL
COMMAND ${CMAKE_COMMAND} -E copy
Expand Down

0 comments on commit 9fd0c40

Please sign in to comment.