Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cpptest] Use find_package to locate GTest files #9208

Merged
merged 5 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
kparzysz-quic marked this conversation as resolved.
Show resolved Hide resolved

# 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