From b099b2db60a7d677c215a2a9b96b1c47987de2c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81ron=20Ricardo=20Perez-Lopez?= Date: Sun, 16 Jun 2024 14:26:50 -0400 Subject: [PATCH] Allow using local GTest (#337) --- configure.sh | 8 ++++++ tests/CMakeLists.txt | 49 +++++++++++++++++++++------------- tests/btor/CMakeLists.txt | 2 +- tests/cvc5/CMakeLists.txt | 2 +- tests/msat/CMakeLists.txt | 2 +- tests/portfolio/CMakeLists.txt | 2 +- tests/unit/CMakeLists.txt | 2 +- tests/yices2/CMakeLists.txt | 2 +- tests/z3/CMakeLists.txt | 2 +- 9 files changed, 45 insertions(+), 26 deletions(-) diff --git a/configure.sh b/configure.sh index 4f6d65fb5..0604a3a40 100755 --- a/configure.sh +++ b/configure.sh @@ -28,6 +28,7 @@ Configures the CMAKE build environment. --debug build debug with debug symbols (default: off) --static create static libaries (default: off) --without-tests build without the smt-switch test suite (default: off) +--no-system-gtest do not use system GTest sources; forces download (default: off) --python compile with python bindings (default: off) --smtlib-reader include the smt-lib reader - requires bison/flex (default:off) --bison-dir=STR custom bison installation directory @@ -60,6 +61,7 @@ yices2_home=default z3_home=default static=default build_tests=default +system_gtest=default python=default smtlib_reader=default bison_dir=default @@ -180,6 +182,9 @@ do --without-tests) build_tests=no ;; + --no-system-gtest) + system_gtest=no + ;; --python) python=yes ;; @@ -282,6 +287,9 @@ cmake_opts="$cmake_opts -DCMAKE_BUILD_TYPE=$build_type" [ $build_tests != default ] \ && cmake_opts="$cmake_opts -DBUILD_TESTS=$build_tests" +[ $system_gtest != default ] \ + && cmake_opts="$cmake_opts -DSYSTEM_GTEST=$system_gtest" + [ $python != default ] \ && cmake_opts="$cmake_opts -DBUILD_PYTHON_BINDINGS=ON" diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 50148d596..22472c57d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -3,26 +3,37 @@ # build testing infrastructure enable_testing() -# Download and unpack googletest at configure time -configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt) -execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . - RESULT_VARIABLE result - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) -if(result) - message(FATAL_ERROR "CMake step for googletest failed: ${result}") -endif() -execute_process(COMMAND ${CMAKE_COMMAND} --build . - RESULT_VARIABLE result - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) -if(result) - message(FATAL_ERROR "Build step for googletest failed: ${result}") +option (SYSTEM_GTEST "Should we try to use the system GTest" ON) + +if (SYSTEM_GTEST) + # try finding a system installation of googletest + find_package(GTest 1.12 CONFIG) +else() + set(GTest_FOUND FALSE) endif() -# Add googletest directly to our build. This defines -# the gtest and gtest_main targets. -add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src - ${CMAKE_CURRENT_BINARY_DIR}/googletest-build - EXCLUDE_FROM_ALL) +if(NOT GTest_FOUND) + # Download and unpack googletest at configure time + configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt) + execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) + if(result) + message(FATAL_ERROR "CMake step for googletest failed: ${result}") + endif() + execute_process(COMMAND ${CMAKE_COMMAND} --build . + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) + if(result) + message(FATAL_ERROR "Build step for googletest failed: ${result}") + endif() + + # Add googletest directly to our build. This defines + # the gtest and gtest_main targets. + add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src + ${CMAKE_CURRENT_BINARY_DIR}/googletest-build + EXCLUDE_FROM_ALL) +endif() add_library(test-deps "${SMT_SWITCH_LIB_TYPE}" "${PROJECT_SOURCE_DIR}/tests/available_solvers.cpp" @@ -62,7 +73,7 @@ endif() macro(switch_add_test name) add_executable(${name} "${PROJECT_SOURCE_DIR}/tests/${name}.cpp") - target_link_libraries(${name} gtest_main) + target_link_libraries(${name} gtest gtest_main) target_link_libraries(${name} test-deps) add_test(NAME ${name}_test COMMAND ${name}) endmacro() diff --git a/tests/btor/CMakeLists.txt b/tests/btor/CMakeLists.txt index 26a859f1e..9eb55fdde 100644 --- a/tests/btor/CMakeLists.txt +++ b/tests/btor/CMakeLists.txt @@ -1,6 +1,6 @@ macro(switch_add_btor_test name) add_executable(${name} "${PROJECT_SOURCE_DIR}/tests/btor/${name}.cpp") - target_link_libraries(${name} gtest_main) + target_link_libraries(${name} gtest gtest_main) target_link_libraries(${name} test-deps) add_test(NAME ${name}_test COMMAND ${name}) endmacro() diff --git a/tests/cvc5/CMakeLists.txt b/tests/cvc5/CMakeLists.txt index 725d1eb9b..b8fc582e3 100644 --- a/tests/cvc5/CMakeLists.txt +++ b/tests/cvc5/CMakeLists.txt @@ -1,6 +1,6 @@ macro(switch_add_cvc5_test name) add_executable(${name} "${PROJECT_SOURCE_DIR}/tests/cvc5/${name}.cpp") - target_link_libraries(${name} gtest_main) + target_link_libraries(${name} gtest gtest_main) target_link_libraries(${name} test-deps) add_test(NAME ${name}_test COMMAND ${name}) endmacro() diff --git a/tests/msat/CMakeLists.txt b/tests/msat/CMakeLists.txt index 4d689bf88..335392068 100644 --- a/tests/msat/CMakeLists.txt +++ b/tests/msat/CMakeLists.txt @@ -1,6 +1,6 @@ macro(switch_add_msat_test name) add_executable(${name} "${PROJECT_SOURCE_DIR}/tests/msat/${name}.cpp") - target_link_libraries(${name} gtest_main) + target_link_libraries(${name} gtest gtest_main) target_link_libraries(${name} test-deps) add_test(NAME ${name}_test COMMAND ${name}) endmacro() diff --git a/tests/portfolio/CMakeLists.txt b/tests/portfolio/CMakeLists.txt index 21fb905bd..b0101de90 100644 --- a/tests/portfolio/CMakeLists.txt +++ b/tests/portfolio/CMakeLists.txt @@ -1,6 +1,6 @@ macro(switch_add_portfolio_test name) add_executable(${name} "${PROJECT_SOURCE_DIR}/tests/portfolio/${name}.cpp") - target_link_libraries(${name} gtest_main) + target_link_libraries(${name} gtest gtest_main) target_link_libraries(${name} test-deps) add_test(NAME ${name}_test COMMAND ${name}) endmacro() diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 3a046366d..e46ab089a 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -1,6 +1,6 @@ macro(switch_add_unit_test name) add_executable(${name} "${PROJECT_SOURCE_DIR}/tests/unit/${name}.cpp") - target_link_libraries(${name} gtest_main) + target_link_libraries(${name} gtest gtest_main) target_link_libraries(${name} test-deps) add_test(NAME ${name}_test COMMAND ${name}) endmacro() diff --git a/tests/yices2/CMakeLists.txt b/tests/yices2/CMakeLists.txt index 18ed3bba3..bd43a0a5b 100644 --- a/tests/yices2/CMakeLists.txt +++ b/tests/yices2/CMakeLists.txt @@ -1,6 +1,6 @@ macro(switch_add_yices2_test name) add_executable(${name} "${PROJECT_SOURCE_DIR}/tests/yices2/${name}.cpp") - target_link_libraries(${name} gtest_main) + target_link_libraries(${name} gtest gtest_main) target_link_libraries(${name} test-deps) add_test(NAME ${name}_test COMMAND ${name}) endmacro() diff --git a/tests/z3/CMakeLists.txt b/tests/z3/CMakeLists.txt index a0b555503..750e47814 100644 --- a/tests/z3/CMakeLists.txt +++ b/tests/z3/CMakeLists.txt @@ -1,6 +1,6 @@ macro(switch_add_z3_test name) add_executable(${name} "${PROJECT_SOURCE_DIR}/tests/z3/${name}.cpp") - target_link_libraries(${name} gtest_main) + target_link_libraries(${name} gtest gtest_main) target_link_libraries(${name} test-deps) add_test(NAME ${name}_test COMMAND ${name}) endmacro()