Skip to content

Commit

Permalink
CMake: add option to use Google Test already installed on system (abs…
Browse files Browse the repository at this point in the history
…eil#969)

As of this change, you can use `-DABSL_USE_EXTERNAL_GOOGLETEST=ON -DABSL_FIND_GOOGLETEST=ON` to have Abseil use the standard CMake find_package(GTest) mechanism.
  • Loading branch information
florin-crisan authored Jun 9, 2021
1 parent f729726 commit 8f92175
Show file tree
Hide file tree
Showing 21 changed files with 295 additions and 225 deletions.
4 changes: 2 additions & 2 deletions CMake/AbseilHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ endfunction()
# "awesome_test.cc"
# DEPS
# absl::awesome
# gmock
# gtest_main
# GTest::gmock
# GTest::gtest_main
# )
function(absl_cc_test)
if(NOT BUILD_TESTING)
Expand Down
45 changes: 45 additions & 0 deletions CMake/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,48 @@ absl::synchronization
absl::time
absl::utility
```

## Traditional CMake Set-Up

For larger projects, it may make sense to use the traditional CMake set-up where you build and install projects separately.

First, you'd need to build and install Google Test:
```
cmake -S /source/googletest -B /build/googletest -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/installation/dir -DBUILD_GMOCK=ON
cmake --build /build/googletest --target install
```

Then you need to configure and build Abseil. Make sure you enable `ABSL_USE_EXTERNAL_GOOGLETEST` and `ABSL_FIND_GOOGLETEST`. You also need to enable `ABSL_ENABLE_INSTALL` so that you can install Abseil itself.
```
cmake -S /source/abseil-cpp -B /build/abseil-cpp -DCMAKE_PREFIX_PATH=/installation/dir -DCMAKE_INSTALL_PREFIX=/installation/dir -DABSL_ENABLE_INSTALL=ON -DABSL_USE_EXTERNAL_GOOGLETEST=ON -DABSL_FIND_GOOGLETEST=ON
cmake --build /temporary/build/abseil-cpp
```

(`CMAKE_PREFIX_PATH` is where you already have Google Test installed; `CMAKE_INSTALL_PREFIX` is where you want to have Abseil installed; they can be different.)

Run the tests:
```
ctest --test-dir /temporary/build/abseil-cpp
```

And finally install:
```
cmake --build /temporary/build/abseil-cpp --target install
```

# CMake Option Synposis

## Enable Standard CMake Installation

`-DABSL_ENABLE_INSTALL=ON`

## Google Test Options

`-DBUILD_TESTING=ON` must be set to enable testing

- Have Abseil download and build Google Test for you: `-DABSL_USE_EXTERNAL_GOOGLETEST=OFF` (default)
- Download and build latest Google Test: `-DABSL_USE_GOOGLETEST_HEAD=ON`
- Download specific Google Test version (ZIP archive): `-DABSL_GOOGLETEST_DOWNLOAD_URL=https://.../version.zip`
- Use Google Test from specific local directory: `-DABSL_LOCAL_GOOGLETEST_DIR=/path/to/googletest`
- Use Google Test included elsewhere in your project: `-DABSL_USE_EXTERNAL_GOOGLETEST=ON`
- Use standard CMake `find_package(CTest)` to find installed Google Test: `-DABSL_USE_EXTERNAL_GOOGLETEST=ON -DABSL_FIND_GOOGLETEST=ON`
39 changes: 32 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,18 @@ endif()
## pthread
find_package(Threads REQUIRED)

include(CMakeDependentOption)

option(ABSL_USE_EXTERNAL_GOOGLETEST
"If ON, Abseil will assume that the targets for GoogleTest are already provided by the including project. This makes sense when Abseil is used with add_subproject." OFF)

cmake_dependent_option(ABSL_FIND_GOOGLETEST
"If ON, Abseil will use find_package(GTest) rather than assuming that GoogleTest is already provided by the including project."
ON
"ABSL_USE_EXTERNAL_GOOGLETEST"
OFF)


option(ABSL_USE_GOOGLETEST_HEAD
"If ON, abseil will download HEAD from GoogleTest at config time." OFF)

Expand All @@ -116,7 +125,15 @@ set(ABSL_LOCAL_GOOGLETEST_DIR "/usr/src/googletest" CACHE PATH

if(BUILD_TESTING)
## check targets
if (NOT ABSL_USE_EXTERNAL_GOOGLETEST)
if (ABSL_USE_EXTERNAL_GOOGLETEST)
if (ABSL_FIND_GOOGLETEST)
find_package(GTest REQUIRED)
else()
if (NOT TARGET gtest AND NOT TARGET GTest::gtest)
message(FATAL_ERROR "ABSL_USE_EXTERNAL_GOOGLETEST is ON and ABSL_FIND_GOOGLETEST is OFF, which means that the top-level project must build the Google Test project. However, the target gtest was not found.")
endif()
endif()
else()
set(absl_gtest_build_dir ${CMAKE_BINARY_DIR}/googletest-build)
if(ABSL_USE_GOOGLETEST_HEAD AND ABSL_GOOGLETEST_DOWNLOAD_URL)
message(FATAL_ERROR "Do not set both ABSL_USE_GOOGLETEST_HEAD and ABSL_GOOGLETEST_DOWNLOAD_URL")
Expand All @@ -134,14 +151,22 @@ if(BUILD_TESTING)
include(CMake/Googletest/DownloadGTest.cmake)
endif()

check_target(gtest)
check_target(gtest_main)
check_target(gmock)
if (NOT ABSL_FIND_GOOGLETEST)
# When Google Test is included directly rather than through find_package, the aliases are missing.
add_library(GTest::gtest_main ALIAS gtest_main)
add_library(GTest::gtest ALIAS gtest)
add_library(GTest::gmock ALIAS gmock)
endif()

check_target(GTest::gtest)
check_target(GTest::gtest_main)
check_target(GTest::gmock)
check_target(GTest::gmock_main)

list(APPEND ABSL_TEST_COMMON_LIBRARIES
gtest_main
gtest
gmock
GTest::gtest_main
GTest::gtest
GTest::gmock
${CMAKE_THREAD_LIBS_INIT}
)
endif()
Expand Down
4 changes: 2 additions & 2 deletions absl/algorithm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ absl_cc_test(
${ABSL_TEST_COPTS}
DEPS
absl::algorithm
gmock_main
GTest::gmock_main
)

absl_cc_library(
Expand Down Expand Up @@ -65,5 +65,5 @@ absl_cc_test(
absl::core_headers
absl::memory
absl::span
gmock_main
GTest::gmock_main
)
58 changes: 29 additions & 29 deletions absl/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ absl_cc_library(
${ABSL_DEFAULT_COPTS}
DEPS
absl::config
gtest
GTest::gtest
TESTONLY
)

Expand Down Expand Up @@ -259,7 +259,7 @@ absl_cc_library(
absl::meta
absl::strings
absl::utility
gtest
GTest::gtest
TESTONLY
)

Expand All @@ -273,7 +273,7 @@ absl_cc_test(
DEPS
absl::exception_safety_testing
absl::memory
gtest_main
GTest::gtest_main
)

absl_cc_library(
Expand All @@ -300,8 +300,8 @@ absl_cc_test(
absl::atomic_hook_test_helper
absl::atomic_hook
absl::core_headers
gmock
gtest_main
GTest::gmock
GTest::gtest_main
)

absl_cc_test(
Expand All @@ -314,7 +314,7 @@ absl_cc_test(
DEPS
absl::base
absl::core_headers
gtest_main
GTest::gtest_main
)

absl_cc_test(
Expand All @@ -327,8 +327,8 @@ absl_cc_test(
DEPS
absl::errno_saver
absl::strerror
gmock
gtest_main
GTest::gmock
GTest::gtest_main
)

absl_cc_test(
Expand All @@ -342,7 +342,7 @@ absl_cc_test(
absl::base
absl::config
absl::throw_delegate
gtest_main
GTest::gtest_main
)

absl_cc_test(
Expand All @@ -357,7 +357,7 @@ absl_cc_test(
${ABSL_TEST_COPTS}
DEPS
absl::base_internal
gtest_main
GTest::gtest_main
)

absl_cc_test(
Expand All @@ -371,8 +371,8 @@ absl_cc_test(
absl::base_internal
absl::memory
absl::strings
gmock
gtest_main
GTest::gmock
GTest::gtest_main
)

absl_cc_library(
Expand All @@ -388,7 +388,7 @@ absl_cc_library(
absl::base_internal
absl::core_headers
absl::synchronization
gtest
GTest::gtest
TESTONLY
)

Expand All @@ -406,7 +406,7 @@ absl_cc_test(
absl::config
absl::core_headers
absl::synchronization
gtest_main
GTest::gtest_main
)

absl_cc_library(
Expand Down Expand Up @@ -435,7 +435,7 @@ absl_cc_test(
absl::base
absl::config
absl::endian
gtest_main
GTest::gtest_main
)

absl_cc_test(
Expand All @@ -448,7 +448,7 @@ absl_cc_test(
DEPS
absl::config
absl::synchronization
gtest_main
GTest::gtest_main
)

absl_cc_test(
Expand All @@ -462,7 +462,7 @@ absl_cc_test(
absl::base
absl::core_headers
absl::synchronization
gtest_main
GTest::gtest_main
)

absl_cc_test(
Expand All @@ -475,7 +475,7 @@ absl_cc_test(
DEPS
absl::raw_logging_internal
absl::strings
gtest_main
GTest::gtest_main
)

absl_cc_test(
Expand All @@ -488,7 +488,7 @@ absl_cc_test(
DEPS
absl::base
absl::synchronization
gtest_main
GTest::gtest_main
)

absl_cc_test(
Expand Down Expand Up @@ -516,7 +516,7 @@ absl_cc_test(
absl::core_headers
absl::synchronization
Threads::Threads
gtest_main
GTest::gtest_main
)

absl_cc_library(
Expand All @@ -543,7 +543,7 @@ absl_cc_test(
DEPS
absl::exponential_biased
absl::strings
gmock_main
GTest::gmock_main
)

absl_cc_library(
Expand All @@ -570,7 +570,7 @@ absl_cc_test(
DEPS
absl::core_headers
absl::periodic_sampler
gmock_main
GTest::gmock_main
)

absl_cc_library(
Expand All @@ -596,7 +596,7 @@ absl_cc_test(
${ABSL_TEST_COPTS}
DEPS
absl::scoped_set_env
gtest_main
GTest::gtest_main
)

absl_cc_test(
Expand All @@ -620,8 +620,8 @@ absl_cc_test(
absl::flags_marshalling
absl::log_severity
absl::strings
gmock
gtest_main
GTest::gmock
GTest::gtest_main
)

absl_cc_library(
Expand Down Expand Up @@ -651,8 +651,8 @@ absl_cc_test(
DEPS
absl::strerror
absl::strings
gmock
gtest_main
GTest::gmock
GTest::gtest_main
)

absl_cc_library(
Expand All @@ -677,7 +677,7 @@ absl_cc_test(
${ABSL_TEST_COPTS}
DEPS
absl::fast_type_id
gtest_main
GTest::gtest_main
)

absl_cc_test(
Expand All @@ -690,5 +690,5 @@ absl_cc_test(
DEPS
absl::core_headers
absl::optional
gtest_main
GTest::gtest_main
)
2 changes: 1 addition & 1 deletion absl/cleanup/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ absl_cc_test(
absl::cleanup
absl::config
absl::utility
gmock_main
GTest::gmock_main
)
Loading

0 comments on commit 8f92175

Please sign in to comment.