Make Google Test usage configurable in CMake files (#3628)
authorLogan Weber <36520469+weberlo@users.noreply.github.com>
Fri, 26 Jul 2019 22:14:18 +0000 (15:14 -0700)
committerTianqi Chen <tqchen@users.noreply.github.com>
Fri, 26 Jul 2019 22:14:18 +0000 (15:14 -0700)
* Add USE_GTEST as a CMake variable

* Add GTest section in installation docs

* Incorporate feedback

CMakeLists.txt
cmake/config.cmake
docs/install/from_source.rst

index c347833..04e1f2e 100644 (file)
@@ -253,20 +253,31 @@ target_include_directories(
 # Tests
 set(TEST_EXECS "")
 file(GLOB TEST_SRCS tests/cpp/*.cc)
+find_path(GTEST_INCLUDE_DIR gtest/gtest.h)
 find_library(GTEST_LIB gtest "$ENV{GTEST_LIB}")
 
-if(GTEST_LIB)
+# 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)
   foreach(__srcpath ${TEST_SRCS})
     get_filename_component(__srcname ${__srcpath} NAME)
     string(REPLACE ".cc" "" __execname ${__srcname})
     add_executable(${__execname} ${__srcpath})
     list(APPEND TEST_EXECS ${__execname})
-    target_link_libraries(${__execname}
-      tvm ${GTEST_LIB} pthread dl)
+    target_include_directories(${__execname} PUBLIC ${GTEST_INCLUDE_DIR})
+    target_link_libraries(${__execname} tvm ${GTEST_LIB} pthread dl)
     set_target_properties(${__execname} PROPERTIES EXCLUDE_FROM_ALL 1)
     set_target_properties(${__execname} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD 1)
   endforeach()
   add_custom_target(cpptest DEPENDS ${TEST_EXECS})
+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
index 2227232..78febba 100644 (file)
@@ -137,4 +137,3 @@ set(USE_ANTLR OFF)
 
 # Whether use Relay debug mode
 set(USE_RELAY_DEBUG OFF)
-
index 1ea8f34..d32f768 100644 (file)
@@ -39,6 +39,8 @@ For windows users who use github tools, you can open the git shell, and type the
    git submodule update
 
 
+.. _build-shared-library:
+
 Build the Shared Library
 ------------------------
 
@@ -192,7 +194,7 @@ Python dependencies
    .. code:: bash
 
        pip install --user tornado psutil xgboost
-       
+
    * If you want to parse Relay text format progams, you must use Python 3 and run the following
 
    .. code:: bash
@@ -207,3 +209,28 @@ Install Contrib Libraries
    :maxdepth: 1
 
    nnpack
+
+
+Enable C++ Tests
+----------------
+We use `Google Test <https://github.com/google/googletest>`_ to drive the C++
+tests in TVM. The easiest way to install GTest is from source.
+
+   .. code:: bash
+
+       git clone https://github.com/google/googletest
+       cd googletest
+       mkdir build
+       cd build
+       cmake ..
+       make
+       make install
+
+Now, you'll need to modify ``build/config.cmake`` and change ``set(USE_GTEST
+OFF)`` to ``set(USE_GTEST ON)``.
+
+TVM can then be built `as usual`__.
+
+__ build-shared-library_
+
+After building, the C++ tests can be run with ``make cpptest``.