From d40ea44cbc68aad566cd2803c83472114a4960f8 Mon Sep 17 00:00:00 2001
From: Kasper Marstal <kaspermarstal@gmail.com>
Date: Tue, 10 Nov 2015 05:13:48 -0800
Subject: [PATCH] COMP:  Add GoogleTest to SuperBuild

Model the GoogleTest build after SimpleITK. This fixes previous build
errors.
---
 SuperBuild/CMakeLists.txt             | 15 ++++++++--
 SuperBuild/Elastix.cmake              |  1 +
 SuperBuild/ExternalGoogleTest.cmake   | 42 +++++++++++++++++++++++++++
 SuperBuild/ExternalITK.cmake          |  1 -
 Testing/CMakeLists.txt                | 29 ++++--------------
 Testing/Unit/CMakeLists.txt           | 32 +++++---------------
 Testing/Unit/HelloWorld.cxx           |  7 -----
 Testing/Unit/HelloWorldBlueprint.cxx  | 12 --------
 Testing/Unit/HelloWorldGoogleTest.cxx |  7 -----
 9 files changed, 69 insertions(+), 77 deletions(-)
 create mode 100644 SuperBuild/ExternalGoogleTest.cmake
 delete mode 100644 Testing/Unit/HelloWorld.cxx
 delete mode 100644 Testing/Unit/HelloWorldBlueprint.cxx
 delete mode 100644 Testing/Unit/HelloWorldGoogleTest.cxx

diff --git a/SuperBuild/CMakeLists.txt b/SuperBuild/CMakeLists.txt
index 0043bd71..74fd55b1 100644
--- a/SuperBuild/CMakeLists.txt
+++ b/SuperBuild/CMakeLists.txt
@@ -23,8 +23,6 @@ if( NOT DEFINED CMAKE_BUILD_TYPE )
   set( CMAKE_BUILD_TYPE "Release" )
 endif()
 
-option( BUILD_SHARED_LIBS OFF )
-
 # Build examples by default
 # Examples will be build as an external project to verify the installation of elastix
 option( ELASTIX_BUILD_EXAMPLES "Enable building examples." ON )
@@ -69,6 +67,19 @@ else()
   include( ExternalBoost )
 endif()
 
+# ---------------------------------------------------------------------
+# Build GoogleTest
+
+option( USE_SYSTEM_GOOGLETEST "Use a pre-compiled version of GoogleTest. " OFF ) 
+mark_as_advanced(USE_SYSTEM_GOOGLETEST) 
+if ( ELASTIX_BUILD_TESTING ) 
+  if ( USE_SYSTEM_GOOGLETEST ) 
+    find_package( GTest REQUIRED ) 
+  else() 
+    include(ExternalGoogleTest) 
+  endif() 
+endif() 
+
 # ---------------------------------------------------------------------
 # Build Elastix
 
diff --git a/SuperBuild/Elastix.cmake b/SuperBuild/Elastix.cmake
index 1f1d7393..c96535d2 100644
--- a/SuperBuild/Elastix.cmake
+++ b/SuperBuild/Elastix.cmake
@@ -12,6 +12,7 @@ ExternalProject_Add( ${proj}
     -DELASTIX_BUILD_DASHBOARD:BOOL=${ELASTIX_BUILD_DASHBOARD}
     -DITK_DIR:PATH=${ITK_DIR}
     -DBOOST_ROOT:PATH=${BOOST_ROOT}
+    -DGTEST_ROOT:PATH=${GTEST_ROOT}
   DEPENDS ${ELASTIX_DEPENDENCIES}
   INSTALL_COMMAND ""
 )
diff --git a/SuperBuild/ExternalGoogleTest.cmake b/SuperBuild/ExternalGoogleTest.cmake
new file mode 100644
index 00000000..dbdd33a1
--- /dev/null
+++ b/SuperBuild/ExternalGoogleTest.cmake
@@ -0,0 +1,42 @@
+set( proj GoogleTest )
+
+set( GTEST_TARGET_VERSION 1.7.0 )
+set( GTEST_DOWNLOAD_SOURCE_HASH "2d6ec8ccdf5c46b05ba54a9fd1d130d7" )
+
+set( GTEST_binary_dir ${CMAKE_CURRENT_BINARY_DIR}/${proj}-prefix/src/${proj}-build )
+set( GTEST_source_dir ${CMAKE_CURRENT_BINARY_DIR}/${proj}-prefix/src/${proj} )
+set( GTEST_install_dir ${CMAKE_CURRENT_BINARY_DIR}/${proj} )
+
+set( ${proj}_ARCHIVE_OUTPUT_DIRECTORY "<BINARY_DIR>/lib" )
+if( MSVC )
+  set( ${proj}_ARCHIVE_OUTPUT_DIRECTORY "<BINARY_DIR>/lib/$<CONFIGURATION>" )
+endif()
+
+set( MSVS_ARGS )
+if(MSVC_VERSION EQUAL 1700)
+  set(MSVS_ARGS ${MSVS_ARGS} -D CMAKE_CXX_FLAGS=-DGTEST_HAS_TR1_TUPLE=0 ${CMAKE_CXX_FLAGS})
+endif()
+
+if( MSVC)
+  set( MSVS_ARGS ${MSVS_ARGS} -D gtest_force_shared_crt:BOOL=ON )
+endif()
+
+ExternalProject_Add(${proj}
+  URL http://midas3.kitware.com/midas/api/rest?method=midas.bitstream.download&checksum=${GTEST_DOWNLOAD_SOURCE_HASH}&name=swig-${GTEST_TARGET_VERSION}.zip
+  URL_MD5 ${GTEST_DOWNLOAD_SOURCE_HASH}
+  INSTALL_DIR ${GTEST_install_dir}
+  CMAKE_GENERATOR ${gen}
+  CMAKE_ARGS
+    --no-warn-unused-cli
+    ${MSVS_ARGS}
+    -D BUILD_SHARED_LIBS:BOOL=OFF
+    -D CMAKE_ARCHIVE_OUTPUT_DIRECTORY:PATH=<BINARY_DIR>/lib
+  INSTALL_COMMAND
+      ${CMAKE_COMMAND} -E copy_directory ${${proj}_ARCHIVE_OUTPUT_DIRECTORY} <INSTALL_DIR>/lib
+    COMMAND
+      ${CMAKE_COMMAND} -E copy_directory <SOURCE_DIR>/include <INSTALL_DIR>/include
+)
+
+set( GTEST_ROOT ${GTEST_install_dir} )
+list( APPEND ELASTIX_DEPENDENCIES ${proj} )
+
diff --git a/SuperBuild/ExternalITK.cmake b/SuperBuild/ExternalITK.cmake
index 1cd9197d..6ab8438b 100644
--- a/SuperBuild/ExternalITK.cmake
+++ b/SuperBuild/ExternalITK.cmake
@@ -14,7 +14,6 @@ ExternalProject_Add( ${proj}
     -DBUILD_TESTING:BOOL=OFF
     -DITK_BUILD_DEFAULT_MODULES:BOOL=ON
     -DModule_ITKReview:BOOL=ON
-    -DBUILD_SHARED_LIBS:BOOL=${BUILD_SHARED_LIBS}
     -DCMAKE_SKIP_RPATH:BOOL=ON
     -DITK_LEGACY_REMOVE:BOOL=ON
     -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
diff --git a/Testing/CMakeLists.txt b/Testing/CMakeLists.txt
index 3aea1e20..71f3fea9 100644
--- a/Testing/CMakeLists.txt
+++ b/Testing/CMakeLists.txt
@@ -30,37 +30,20 @@ endforeach()
 ExternalData_Add_Target( ElastixData )
 
 # ---------------------------------------------------------------------
-# Add GoogleTest
+# Find GoogleTest library
 
-find_package( Git )
-if( NOT EXISTS "${CMAKE_SOURCE_DIR}/Testing/GoogleTest/.git" AND GIT_EXECUTABLE )
-  execute_process( 
-  	COMMAND ${GIT_EXECUTABLE} submodule update --init
-    WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
-    OUTPUT_QUIET
-  )
-endif()
-
-if( NOT EXISTS ${CMAKE_SOURCE_DIR}/Testing/GoogleTest/.git )
-  message( 
-  	FATAL_ERROR "Could not find GoogleTest submodule. Please run 
-  	git submodule update --init in the source directory to compile tests."
-  )
-endif()
-
-add_subdirectory( GoogleTest )
-
-include_directories(
-  ${gtest_SOURCE_DIR}/include
-)
+find_package( GTest REQUIRED )
+include_directories( ${GTEST_INCLUDE_DIRS} )
 
 set( TEST_LIBRARIES
-  gtest_main
+  ${GTEST_BOTH_LIBRARIES}
 )
 
 # ---------------------------------------------------------------------
 # Build test suite
 
+set( CTEST_CONFIGURATION_TYPE Debug )
+
 # Unit tests
 add_subdirectory( Unit )
 
diff --git a/Testing/Unit/CMakeLists.txt b/Testing/Unit/CMakeLists.txt
index 720b58c5..06f783c2 100644
--- a/Testing/Unit/CMakeLists.txt
+++ b/Testing/Unit/CMakeLists.txt
@@ -3,26 +3,10 @@
 # Any GoogleTests in these files are automatically added to CTest and
 # the elastix dashboard. 
 
-# Not compiling elastix tests while we debug windows build. See minimum viable examples below
-# set( ElastixUnitTestFilenames
-#   elxExampleUnitTest.cxx
-#   elxBlueprintTest.cxx
-# )         
-
-# Pass
-add_executable( HelloWorld HelloWorld.cxx)
-
-# Pass
-add_executable( HelloWorldBlueprint HelloWorldBlueprint.cxx )
-target_link_libraries( HelloWorldBlueprint ${ELASTIX_LIBRARIES} ${ITK_LIBRARIES} )
-
-# Fail. It seems GoogleTest is compiled statically (like it should), while the executable is compiled dynamically (it should also be static) Why? BUILD_SHARED_LIBS is set to OFF!
-
-# And why is the static linker option not recongnized (VS2013)?
-set(CMAKE_EXE_LINKER_FLAGS /MTd ) 
-
-add_executable( HelloWorldGoogleTest HelloWorldGoogleTest.cxx )
-target_link_libraries( HelloWorldGoogleTest ${TEST_LIBRARIES} )
+set( ElastixUnitTestFilenames
+  elxExampleUnitTest.cxx
+  elxBlueprintTest.cxx
+)         
 
 # ---------------------------------------------------------------------
 # Set data directories
@@ -60,11 +44,9 @@ list( APPEND TEST_LIBRARIES
 foreach( ElastixUnitTestFilename ${ElastixUnitTestFilenames} )
   # Build tests executables
   string( REPLACE ".cxx" "" ElastixUnitTest ${ElastixUnitTestFilename} )
-  add_executable( "${ElastixUnitTest}" ${ElastixUnitTestFilename} )
+  add_executable( ${ElastixUnitTest} ${ElastixUnitTestFilename} )
   target_link_libraries( "${ElastixUnitTest}" ${ELASTIX_LIBRARIES} ${ITK_LIBRARIES} ${TEST_LIBRARIES} )
 
-  ExternalData_Add_Test( ElastixData
-    NAME ${ElastixUnitTest}
-    COMMAND ${ElastixUnitTest} "--gtest_output=xml:${CMAKE_BINARY_DIR}/Testing/Unit/${ElastixUnitTest}.xml"
-  )
+  # Add GoogleTest to CTest
+  GTEST_ADD_TESTS( ${ElastixUnitTest} "--gtest_output=xml:${CMAKE_BINARY_DIR}/Testing/Unit/${ElastixUnitTest}.xml" ${ElastixUnitTestFilename} )
 endforeach()
diff --git a/Testing/Unit/HelloWorld.cxx b/Testing/Unit/HelloWorld.cxx
deleted file mode 100644
index 9d908982..00000000
--- a/Testing/Unit/HelloWorld.cxx
+++ /dev/null
@@ -1,7 +0,0 @@
-#include <iostream>
- 
-int main()
-{
-  std::cout << "Hello World!" << std::endl;
-  return 0;
-}
\ No newline at end of file
diff --git a/Testing/Unit/HelloWorldBlueprint.cxx b/Testing/Unit/HelloWorldBlueprint.cxx
deleted file mode 100644
index 1644eb24..00000000
--- a/Testing/Unit/HelloWorldBlueprint.cxx
+++ /dev/null
@@ -1,12 +0,0 @@
-#include <iostream>
-#include "elxBlueprint.h"
- 
-int main()
-{
-  elx::Blueprint::Pointer blueprint = elx::Blueprint::New();
-  elx::Blueprint::ComponentIndexType index0 = blueprint->AddComponent();
-
-  std::cout << "Hello World!" << std::endl;
-  std::cout << "Loaded blueprint!" << std::endl;
-  return 0;
-}
diff --git a/Testing/Unit/HelloWorldGoogleTest.cxx b/Testing/Unit/HelloWorldGoogleTest.cxx
deleted file mode 100644
index 7dcd6d3f..00000000
--- a/Testing/Unit/HelloWorldGoogleTest.cxx
+++ /dev/null
@@ -1,7 +0,0 @@
-#include <iostream>
-#include "gtest/gtest.h"
- 
-TEST( HelloWorld, GoogleTest )
-{
-  int a = 1;
-}
-- 
GitLab