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 deb5a37
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 39 deletions.
87 changes: 48 additions & 39 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,63 @@ 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)
add_library(scn_gtest ALIAS 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>)
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

0 comments on commit deb5a37

Please sign in to comment.