Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 1 | # Integrates googletest at configure time. Based on the instructions at |
| 2 | # https://github.com/google/googletest/tree/master/googletest#incorporating-into-an-existing-cmake-project |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 3 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 4 | # Set up the external googletest project, downloading the latest from Github |
| 5 | # master if requested. |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 6 | configure_file( |
| 7 | ${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt.in |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 8 | ${CMAKE_BINARY_DIR}/googletest-external/CMakeLists.txt |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 9 | ) |
| 10 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 11 | set(ABSL_SAVE_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) |
| 12 | set(ABSL_SAVE_CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}) |
| 13 | if (BUILD_SHARED_LIBS) |
| 14 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) |
| 15 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DGTEST_CREATE_SHARED_LIBRARY=1") |
| 16 | endif() |
| 17 | |
| 18 | # Configure and build the googletest source. |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 19 | execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . |
| 20 | RESULT_VARIABLE result |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 21 | WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-external ) |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 22 | if(result) |
| 23 | message(FATAL_ERROR "CMake step for googletest failed: ${result}") |
| 24 | endif() |
| 25 | |
| 26 | execute_process(COMMAND ${CMAKE_COMMAND} --build . |
| 27 | RESULT_VARIABLE result |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 28 | WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-external) |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 29 | if(result) |
| 30 | message(FATAL_ERROR "Build step for googletest failed: ${result}") |
| 31 | endif() |
| 32 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 33 | set(CMAKE_CXX_FLAGS ${ABSL_SAVE_CMAKE_CXX_FLAGS}) |
| 34 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${ABSL_SAVE_CMAKE_RUNTIME_OUTPUT_DIRECTORY}) |
| 35 | |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 36 | # Prevent overriding the parent project's compiler/linker settings on Windows |
| 37 | set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) |
| 38 | |
| 39 | # Add googletest directly to our build. This defines the gtest and gtest_main |
| 40 | # targets. |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 41 | add_subdirectory(${absl_gtest_src_dir} ${absl_gtest_build_dir} EXCLUDE_FROM_ALL) |