From 86051343eb98c26ab7dd72053a9ab123bb8cb191 Mon Sep 17 00:00:00 2001 From: Krzysztof Parzyszek Date: Tue, 5 Oct 2021 09:52:38 -0500 Subject: [PATCH 1/5] [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! --- CMakeLists.txt | 37 +++++++++++++++++-------------- cmake/modules/StandaloneCrt.cmake | 14 +++--------- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 87fa2e059abb..8765d1630a27 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,14 @@ 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_include_directories(cpptest SYSTEM PUBLIC ${GTEST_INCLUDE_DIRS}) + target_link_libraries(cpptest PRIVATE ${TVM_TEST_LIBRARY_NAME} ${GTEST_BOTH_LIBRARIES} 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/modules/StandaloneCrt.cmake b/cmake/modules/StandaloneCrt.cmake index 1ced2b8ad892..40b14f95dc1d 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 ${GTEST_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR}/standalone_crt/include ${CMAKE_SOURCE_DIR}/src/runtime/micro) + target_link_libraries(crttest PRIVATE ${cmake_crt_libraries} ${GTEST_BOTH_LIBRARIES} 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() From b19d1822c05b5b2ff636fb2a7483975096ed0e57 Mon Sep 17 00:00:00 2001 From: Krzysztof Parzyszek Date: Wed, 6 Oct 2021 13:11:42 -0500 Subject: [PATCH 2/5] Use GTest:: targets instead of variable --- CMakeLists.txt | 2 +- cmake/modules/StandaloneCrt.cmake | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8765d1630a27..9de2f51af44e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -600,7 +600,7 @@ if(GTEST_FOUND) file(GLOB_RECURSE TEST_SRCS tests/cpp/*.cc) add_executable(cpptest ${TEST_SRCS}) target_include_directories(cpptest SYSTEM PUBLIC ${GTEST_INCLUDE_DIRS}) - target_link_libraries(cpptest PRIVATE ${TVM_TEST_LIBRARY_NAME} ${GTEST_BOTH_LIBRARIES} 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) diff --git a/cmake/modules/StandaloneCrt.cmake b/cmake/modules/StandaloneCrt.cmake index 40b14f95dc1d..e292d9346cc4 100644 --- a/cmake/modules/StandaloneCrt.cmake +++ b/cmake/modules/StandaloneCrt.cmake @@ -138,7 +138,7 @@ if(USE_MICRO) file(GLOB TEST_SRCS ${CMAKE_SOURCE_DIR}/tests/crt/*.cc) add_executable(crttest ${TEST_SRCS}) target_include_directories(crttest SYSTEM PUBLIC ${GTEST_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR}/standalone_crt/include ${CMAKE_SOURCE_DIR}/src/runtime/micro) - target_link_libraries(crttest PRIVATE ${cmake_crt_libraries} ${GTEST_BOTH_LIBRARIES} pthread dl) + 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) From 15f390650d61aa52e047484bf3d923ffe7e1cace Mon Sep 17 00:00:00 2001 From: Krzysztof Parzyszek Date: Wed, 6 Oct 2021 13:12:11 -0500 Subject: [PATCH 3/5] Add USE_GTEST to cmake/config.cmake --- cmake/config.cmake | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cmake/config.cmake b/cmake/config.cmake index 0ed9dd221e32..ade9d5c815c1 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) From 52b5e8e7bfde0f98d17c568e450747e131d0a2d7 Mon Sep 17 00:00:00 2001 From: Krzysztof Parzyszek Date: Wed, 6 Oct 2021 13:52:17 -0500 Subject: [PATCH 4/5] Remove GTEST_INCLUDE_DIRS from target_include_directories Cmake will automatically propagate the interface include directories to the dependends of (in this case) GTest. --- CMakeLists.txt | 1 - cmake/modules/StandaloneCrt.cmake | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9de2f51af44e..c40b0c878905 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -599,7 +599,6 @@ endif() if(GTEST_FOUND) file(GLOB_RECURSE TEST_SRCS tests/cpp/*.cc) add_executable(cpptest ${TEST_SRCS}) - target_include_directories(cpptest SYSTEM PUBLIC ${GTEST_INCLUDE_DIRS}) 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) diff --git a/cmake/modules/StandaloneCrt.cmake b/cmake/modules/StandaloneCrt.cmake index e292d9346cc4..9f79c7da3cdf 100644 --- a/cmake/modules/StandaloneCrt.cmake +++ b/cmake/modules/StandaloneCrt.cmake @@ -137,7 +137,7 @@ if(USE_MICRO) 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_DIRS} ${CMAKE_CURRENT_BINARY_DIR}/standalone_crt/include ${CMAKE_SOURCE_DIR}/src/runtime/micro) + 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) From a227fcca81e55a25e7095e7be4247df9aecbfdca Mon Sep 17 00:00:00 2001 From: Krzysztof Parzyszek Date: Wed, 6 Oct 2021 19:24:18 -0500 Subject: [PATCH 5/5] Restart CI