From d678f3e8ff3d40bd63a69549d0473978ca2557c4 Mon Sep 17 00:00:00 2001 From: Krzysztof Parzyszek Date: Fri, 8 Oct 2021 16:57:54 -0500 Subject: [PATCH] [cpptest] Use find_package to locate GTest files (#9208) * [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 --- CMakeLists.txt | 36 ++++++++++++++++--------------- cmake/config.cmake | 12 +++++++++++ cmake/modules/StandaloneCrt.cmake | 14 +++--------- 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 87fa2e059abb9..c40b0c8789059 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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") @@ -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) @@ -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 diff --git a/cmake/config.cmake b/cmake/config.cmake index 0ed9dd221e32e..ade9d5c815c11 100644 --- a/cmake/config.cmake +++ b/cmake/config.cmake @@ -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) diff --git a/cmake/modules/StandaloneCrt.cmake b/cmake/modules/StandaloneCrt.cmake index 1ced2b8ad8927..9f79c7da3cdf6 100644 --- a/cmake/modules/StandaloneCrt.cmake +++ b/cmake/modules/StandaloneCrt.cmake @@ -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()