Skip to content

Commit

Permalink
[cpptest] Use find_package to locate GTest files (apache#9208)
Browse files Browse the repository at this point in the history
* [cpptest] Use find_package to locate GTest files

There is a standard CMake utility for finding GTest, use that instead
of doing a manual search.

Thanks @tkonolige for the suggestion!

* Use GTest:: targets instead of variable

* Add USE_GTEST to cmake/config.cmake

* Remove GTEST_INCLUDE_DIRS from target_include_directories

Cmake will automatically propagate the interface include directories
to the dependends of (in this case) GTest.

* Restart CI
  • Loading branch information
Krzysztof Parzyszek authored and masahi committed Oct 14, 2021
1 parent e7ac930 commit d678f3e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
36 changes: 19 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ tvm_option(INDEX_DEFAULT_I64 "Defaults the index datatype to int64" ON)
tvm_option(USE_LIBBACKTRACE "Build libbacktrace to supply linenumbers on stack traces" AUTO)
tvm_option(BUILD_STATIC_RUNTIME "Build static version of libtvm_runtime" OFF)
tvm_option(USE_PAPI "Use Performance Application Programming Interface (PAPI) to read performance counters" OFF)
tvm_option(USE_GTEST "Use GoogleTest for C++ sanity tests" AUTO)

# 3rdparty libraries
tvm_option(DLPACK_PATH "Path to DLPACK" "3rdparty/dlpack/include")
Expand Down Expand Up @@ -385,12 +386,22 @@ if(USE_PROFILER)
endif(USE_PROFILER)

# Enable ctest if gtest is available
find_path(GTEST_INCLUDE_DIR gtest/gtest.h)
find_library(GTEST_LIB gtest "$ENV{GTEST_LIB}")
if(GTEST_INCLUDE_DIR AND GTEST_LIB)
enable_testing()
include(CTest)
include(GoogleTest)
if(USE_GTEST)
# Check env var for backward compatibility. A better way to specify package
# locations is to use CMAKE_PREFIX_PATH or other standard cmake mechanism
# (see cmake documentation for `find_package`).
set(GTEST_ROOT "$ENV{GTEST_LIB}")
if("${USE_GTEST}" STREQUAL "AUTO")
# If USE_GTEST is AUTO, treat GTest as optional: enable if found.
find_package(GTest)
elseif("${USE_GTEST}" MATCHES ${IS_TRUE_PATTERN})
# USE_GTEST is set to ON, TRUE, etc. Treat GTest as a required package.
find_package(GTest REQUIRED)
endif()
if(GTEST_FOUND)
enable_testing()
include(CTest)
endif()
endif()

if(USE_PIPELINE_EXECUTOR)
Expand Down Expand Up @@ -585,22 +596,13 @@ endif()

# Create the `cpptest` target if we can find GTest. If not, we create dummy
# targets that give the user an informative error message.
if(GTEST_INCLUDE_DIR AND GTEST_LIB)
if(GTEST_FOUND)
file(GLOB_RECURSE TEST_SRCS tests/cpp/*.cc)
add_executable(cpptest ${TEST_SRCS})
target_include_directories(cpptest SYSTEM PUBLIC ${GTEST_INCLUDE_DIR})
target_link_libraries(cpptest PRIVATE ${TVM_TEST_LIBRARY_NAME} ${GTEST_LIB} gtest_main pthread dl)
target_link_libraries(cpptest PRIVATE ${TVM_TEST_LIBRARY_NAME} GTest::GTest GTest::Main pthread dl)
set_target_properties(cpptest PROPERTIES EXCLUDE_FROM_ALL 1)
set_target_properties(cpptest PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD 1)
gtest_discover_tests(cpptest)
elseif(NOT GTEST_INCLUDE_DIR)
add_custom_target(cpptest
COMMAND echo "Missing Google Test headers in include path"
COMMAND exit 1)
elseif(NOT GTEST_LIB)
add_custom_target(cpptest
COMMAND echo "Missing Google Test library"
COMMAND exit 1)
endif()

# Custom targets
Expand Down
12 changes: 12 additions & 0 deletions cmake/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,15 @@ set(USE_CCACHE AUTO)
# - OFF: disable PAPI support.
# - /path/to/folder/containing/: Path to folder containing papi.pc.
set(USE_PAPI OFF)

# Whether to use GoogleTest for C++ unit tests. When enabled, the generated
# build file (e.g. Makefile) will have a target "cpptest".
# Possible values:
# - ON: enable GoogleTest. The package `GTest` will be required for cmake
# to succeed.
# - OFF: disable GoogleTest.
# - AUTO: cmake will attempt to find the GTest package, if found GTest will
# be enabled, otherwise it will be disabled.
# Note that cmake will use `find_package` to find GTest. Please use cmake's
# predefined variables to specify the path to the GTest package if needed.
set(USE_GTEST AUTO)
14 changes: 3 additions & 11 deletions cmake/modules/StandaloneCrt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,14 @@ if(USE_MICRO)

# Create the `crttest` target if we can find GTest. If not, we create dummy
# targets that give the user an informative error message.
if(GTEST_INCLUDE_DIR AND GTEST_LIB)
if(GTEST_FOUND)
file(GLOB TEST_SRCS ${CMAKE_SOURCE_DIR}/tests/crt/*.cc)
add_executable(crttest ${TEST_SRCS})
target_include_directories(crttest SYSTEM PUBLIC ${GTEST_INCLUDE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/standalone_crt/include ${CMAKE_SOURCE_DIR}/src/runtime/micro)
target_link_libraries(crttest PRIVATE ${cmake_crt_libraries} ${GTEST_LIB} gtest_main pthread dl)
target_include_directories(crttest SYSTEM PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/standalone_crt/include ${CMAKE_SOURCE_DIR}/src/runtime/micro)
target_link_libraries(crttest PRIVATE ${cmake_crt_libraries} GTest::GTest GTest::Main pthread dl)
set_target_properties(crttest PROPERTIES EXCLUDE_FROM_ALL 1)
set_target_properties(crttest PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD 1)
gtest_discover_tests(crttest)
elseif(NOT GTEST_INCLUDE_DIR)
add_custom_target(crttest
COMMAND echo "Missing Google Test headers in include path"
COMMAND exit 1)
elseif(NOT GTEST_LIB)
add_custom_target(crttest
COMMAND echo "Missing Google Test library"
COMMAND exit 1)
endif()

endfunction()
Expand Down

0 comments on commit d678f3e

Please sign in to comment.