From 0916a6992e06ee799deec6b97a7fd1838dad2442 Mon Sep 17 00:00:00 2001
From: Jeremie Deray <jeremie.deray@pal-robotics.com>
Date: Tue, 4 Oct 2016 19:21:15 +0200
Subject: [PATCH] add gtest

---
 CMakeLists.txt            | 17 +++++++++++
 test/CMakeLists.txt       |  5 ++++
 test/gtest/CMakeLists.txt | 61 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 83 insertions(+)
 create mode 100644 test/CMakeLists.txt
 create mode 100644 test/gtest/CMakeLists.txt

diff --git a/CMakeLists.txt b/CMakeLists.txt
index c451d172d..fa9b6a77c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -65,6 +65,9 @@ else()
 endif()
 
 
+#OPTION(BUILD_DOC "Build Documentation" OFF)
+OPTION(BUILD_TESTS "Build Unit tests" ON)
+
 ADD_SUBDIRECTORY(src)
 
 FIND_PACKAGE(Doxygen)
@@ -124,3 +127,17 @@ ELSE(UNIX)
     TARGET  uninstall)
 ENDIF(UNIX)
 
+#############
+## Testing ##
+#############
+
+if(BUILD_TESTS)
+
+# Enables testing for this directory and below.
+# Note that ctest expects to find a test file in the build directory root.
+# Therefore, this command should be in the source directory root.
+enable_testing()
+
+add_subdirectory(${PROJECT_SOURCE_DIR}/test)
+
+endif()
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
new file mode 100644
index 000000000..441c2b428
--- /dev/null
+++ b/test/CMakeLists.txt
@@ -0,0 +1,5 @@
+# Retrieve googletest from github & compile
+add_subdirectory(${PROJECT_SOURCE_DIR}/test/gtest)
+
+include_directories(${GTEST_INCLUDE_DIRS})
+
diff --git a/test/gtest/CMakeLists.txt b/test/gtest/CMakeLists.txt
new file mode 100644
index 000000000..714d8195b
--- /dev/null
+++ b/test/gtest/CMakeLists.txt
@@ -0,0 +1,61 @@
+cmake_minimum_required(VERSION 2.8.8)
+project(gtest_builder C CXX)
+
+# We need thread support
+#find_package(Threads REQUIRED)
+
+# Enable ExternalProject CMake module
+include(ExternalProject)
+
+set(GTEST_FORCE_SHARED_CRT ON)
+set(GTEST_DISABLE_PTHREADS OFF)
+
+# For some reason I need to disable PTHREADS
+# with g++ (Ubuntu 4.9.3-8ubuntu2~14.04) 4.9.3
+# This is a known issue for MinGW :
+# https://github.com/google/shaderc/pull/174
+#if(MINGW)
+    set(GTEST_DISABLE_PTHREADS ON)
+#endif()
+
+# Download GoogleTest
+ExternalProject_Add(googletest
+    GIT_REPOSITORY https://github.com/google/googletest.git
+    CMAKE_ARGS -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG:PATH=DebugLibs
+    -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=ReleaseLibs
+    -DCMAKE_CXX_FLAGS=${MSVC_COMPILER_DEFS}
+    -Dgtest_force_shared_crt=${GTEST_FORCE_SHARED_CRT}
+    -Dgtest_disable_pthreads=${GTEST_DISABLE_PTHREADS}
+    -DBUILD_GTEST=ON
+    PREFIX "${CMAKE_CURRENT_BINARY_DIR}"
+    # Disable install step
+    INSTALL_COMMAND ""
+)
+
+# Get GTest source and binary directories from CMake project
+
+# Specify include dir
+ExternalProject_Get_Property(googletest source_dir)
+set(GTEST_INCLUDE_DIRS ${source_dir}/googletest/include PARENT_SCOPE)
+
+# Specify MainTest's link libraries
+ExternalProject_Get_Property(googletest binary_dir)
+set(GTEST_LIBS_DIR ${binary_dir}/googlemock/gtest PARENT_SCOPE)
+
+# Create a libgtest target to be used as a dependency by test programs
+add_library(libgtest IMPORTED STATIC GLOBAL)
+add_dependencies(libgtest googletest)
+
+# Set libgtest properties
+set_target_properties(libgtest PROPERTIES
+    "IMPORTED_LOCATION" "${binary_dir}/googlemock/gtest/libgtest.a"
+    "IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}"
+)
+
+function(wolf_add_gtest target)
+  add_executable(${target} ${ARGN})
+  add_dependencies(${target} libgtest)
+  target_link_libraries(${target} libgtest)
+
+  add_test(${target} ${target})
+endfunction()
-- 
GitLab