Skip to content

Commit

Permalink
Allow using local GTest (#328)
Browse files Browse the repository at this point in the history
* Use system version of GTest if available

* Make system GTest optional and add version
  • Loading branch information
CyanoKobalamyne authored Jul 5, 2024
1 parent c980d66 commit 0fa32fd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
6 changes: 6 additions & 0 deletions configure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Configures the CMAKE build environment.
--static-lib build a static library (default: shared)
--static build a static executable (default: dynamic); implies --static-lib
--with-profiling build with gperftools for profiling (default: off)
--no-system-gtest do not use system GTest sources; forces download (default: off)
EOF
exit 0
}
Expand All @@ -44,6 +45,7 @@ python=default
lib_type=SHARED
static_exec=NO
with_profiling=default
system_gtest=default

buildtype=Release

Expand Down Expand Up @@ -90,6 +92,7 @@ do
lib_type=STATIC;
;;
--with-profiling) with_profiling=ON;;
--no-system-gtest) system_gtest=no;;
*) die "unexpected argument: $1";;
esac
shift
Expand Down Expand Up @@ -121,6 +124,9 @@ cmake_opts="-DCMAKE_BUILD_TYPE=$buildtype -DPONO_LIB_TYPE=${lib_type} -DPONO_STA
[ $with_profiling != default ] \
&& cmake_opts="$cmake_opts -DWITH_PROFILING=$with_profiling"

[ $system_gtest != default ] \
&& cmake_opts="$cmake_opts -DSYSTEM_GTEST=$system_gtest"

root_dir=$(pwd)

[ -e "$build_dir" ] && rm -r "$build_dir"
Expand Down
50 changes: 30 additions & 20 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
# Set Up Google Tests

# build testing infrastructure
# 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()
option (SYSTEM_GTEST "Should we try to use the system GTest" ON)

# 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 (SYSTEM_GTEST)
# try finding a system installation of googletest
find_package(GTest 1.14 CONFIG)
else()
set(GTest_FOUND FALSE)
endif()

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)
else()
endif()
if (WITH_MSAT)
add_definitions(-DWITH_MSAT)
endif()
Expand All @@ -36,7 +46,7 @@ target_link_libraries(pono-test-lib pono-lib)

macro(pono_add_test name)
add_executable(${name} "${CMAKE_CURRENT_SOURCE_DIR}/${name}.cpp")
target_link_libraries(${name} gtest_main)
target_link_libraries(${name} gtest gtest_main)
target_link_libraries(${name} pono-test-lib) # also includes pono-lib because it's linked
add_test(NAME ${name} COMMAND ${name})
endmacro()
Expand Down

0 comments on commit 0fa32fd

Please sign in to comment.