From d4e4028060b71a60fbe3aa1a41445d38780c7900 Mon Sep 17 00:00:00 2001
From: Kasper Marstal <kaspermarstal@gmail.com>
Date: Wed, 29 Apr 2015 16:23:01 +0200
Subject: [PATCH] COMP: ELASTIX-1 Add Windows support for GoogleTest

---
 CMake/elxCompilerFlags.cmake       | 12 ++++++++++++
 CMake/elxGoogleTestWinConfig.cmake |  6 ++++++
 CMake/elxRequiredITKModules.cmake  |  2 +-
 CMakeLists.txt                     | 24 ++++++++++++------------
 SuperBuild/CMakeLists.txt          |  9 ++++-----
 SuperBuild/Elastix.cmake           |  4 ++--
 Testing/CMakeLists.txt             | 11 ++++++++++-
 7 files changed, 47 insertions(+), 21 deletions(-)
 create mode 100644 CMake/elxCompilerFlags.cmake
 create mode 100644 CMake/elxGoogleTestWinConfig.cmake

diff --git a/CMake/elxCompilerFlags.cmake b/CMake/elxCompilerFlags.cmake
new file mode 100644
index 00000000..171a36b1
--- /dev/null
+++ b/CMake/elxCompilerFlags.cmake
@@ -0,0 +1,12 @@
+set( CompilerFlags
+  CMAKE_CXX_FLAGS
+  CMAKE_CXX_FLAGS_DEBUG
+  CMAKE_CXX_FLAGS_MINSIZEREL
+  CMAKE_CXX_FLAGS_RELEASE
+  CMAKE_CXX_FLAGS_RELWITHDEBINFO
+  CMAKE_C_FLAGS
+  CMAKE_C_FLAGS_DEBUG
+  CMAKE_C_FLAGS_MINSIZEREL
+  CMAKE_C_FLAGS_RELEASE
+  CMAKE_C_FLAGS_RELWITHDEBINFO
+)
diff --git a/CMake/elxGoogleTestWinConfig.cmake b/CMake/elxGoogleTestWinConfig.cmake
new file mode 100644
index 00000000..e72a8726
--- /dev/null
+++ b/CMake/elxGoogleTestWinConfig.cmake
@@ -0,0 +1,6 @@
+include( ${CMAKE_SOURCE_DIR}/CMake/elxCompilerFlags.cmake )
+
+foreach( CompilerFlag ${CompilerFlags} )
+  # GoogleTest needs static linking
+  string( REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}" )
+endforeach()
diff --git a/CMake/elxRequiredITKModules.cmake b/CMake/elxRequiredITKModules.cmake
index 284dddb8..c9cce4a0 100644
--- a/CMake/elxRequiredITKModules.cmake
+++ b/CMake/elxRequiredITKModules.cmake
@@ -6,4 +6,4 @@ foreach( ITKModule ${ElastixRequiredITKModules} )
   if( NOT ${ITKModule}_LOADED )
     message( FATAL_ERROR "Elastix requires that ITK is build with ${ITKModule}. Please rebuild ITK with Module_${ITKModule} set to ON." )
   endif()
-endforeach()
\ No newline at end of file
+endforeach()
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9d4f8b65..c825fcac 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -13,20 +13,20 @@ include( "${CMAKE_CURRENT_SOURCE_DIR}/CMake/elxRequiredITKModules.cmake" )
 #---------------------------------------------------------------------
 # Testing
 
-if( BUILD_TESTING )
-  if( NOT EXISTS "${CMAKE_SOURCE_DIR}/Testing/GoogleTest/.git" )
-    message( FATAL_ERROR "Could not find GoogleTest submodule. Please run git submodule init to compile tests." )
-  endif()
+# Do not build tests by default since this requires GoogleTest submodule
+# to be initialized. This saves users that just clone and build from 
+# seeing FATAL_ERRORs out of the box. 
+option( ELASTIX_BUILD_TESTING "Enable building tests." OFF )
 
+if( ELASTIX_BUILD_TESTING )
   enable_testing()
-  
-  mark_as_advanced( BUILD_CTESTS )
-  option( BUILD_CTESTS "Enable CTests." ON )
-  if( BUILD_CTESTS )
+  add_subdirectory( Testing )
+
+  mark_as_advanced( ELASTIX_BUILD_CTESTS )
+  option( ELASTIX_BUILD_CTESTS "Enable CTests." ON )
+  if( ELASTIX_BUILD_CTESTS )
     include( CTest )
   endif()
-
-  add_subdirectory( Testing )
 endif()
 
 #---------------------------------------------------------------------
@@ -35,9 +35,9 @@ endif()
 #---------------------------------------------------------------------
 # Build Documentation
 
-mark_as_advanced( BUILD_DOXYGEN )
+mark_as_advanced( ELASTIX_BUILD_DOXYGEN )
 option( BUILD_DOXYGEN "Enable building Doxygen documentation." OFF )
 
-mark_as_advanced( BUILD_READTHEDOCS OFF )
+mark_as_advanced( ELASTIX_BUILD_READTHEDOCS )
 option( BUILD_READTHEDOCS "Enable building readthedocs.org documentation." OFF )
 
diff --git a/SuperBuild/CMakeLists.txt b/SuperBuild/CMakeLists.txt
index dc7142e3..c719db98 100644
--- a/SuperBuild/CMakeLists.txt
+++ b/SuperBuild/CMakeLists.txt
@@ -1,15 +1,12 @@
 cmake_minimum_required( VERSION 2.8 )
 
 #---------------------------------------------------------------------
-# Prerequisites
+project( elastixSuperBuild )
 
 find_package( Git REQUIRED )
 
 include( ExternalProject )
 
-enable_language(C)
-enable_language(CXX)
-
 set( CMAKE_MODULE_PATH
   ${CMAKE_CURRENT_SOURCE_DIR}
   ${CMAKE_CURRENT_SOURCE_DIR}/../CMake
@@ -28,7 +25,9 @@ endif()
 # Examples will be build as an external project to verify the installation of elastix
 option( ELASTIX_BUILD_EXAMPLES "Enable building examples." ON )
 
-# Do not build testing by default since this requires GoogleTest submodule to be initialized
+# Do not build tests by default since this requires GoogleTest submodule
+# to be initialized. This saves users that just clone and build from 
+# seeing FATAL_ERRORs out of the box.
 option( ELASTIX_BUILD_TESTING "Enable building tests." OFF )
 
 if( ELASTIX_BUILD_TESTING )
diff --git a/SuperBuild/Elastix.cmake b/SuperBuild/Elastix.cmake
index 5cc4b57b..130f5595 100644
--- a/SuperBuild/Elastix.cmake
+++ b/SuperBuild/Elastix.cmake
@@ -7,8 +7,8 @@ ExternalProject_Add( ${proj}
   INSTALL_DIR ${CMAKE_INSTALL_PREFIX}
   CMAKE_ARGS
     --no-warn-unused-cli
-    -DBUILD_TESTING:BOOL=${ELASTIX_BUILD_TESTING}
-    -DBUILD_CTESTS:BOOL=${ELASTIX_BUILD_CTESTS}
+    -DELASTIX_BUILD_TESTING:BOOL=${ELASTIX_BUILD_TESTING}
+    -DELASTIX_BUILD_CTESTS:BOOL=${ELASTIX_BUILD_CTESTS}
     -DITK_DIR:PATH=${ITK_DIR}
   DEPENDS ${ELASTIX_DEPENDENCIES}
   INSTALL_COMMAND ""
diff --git a/Testing/CMakeLists.txt b/Testing/CMakeLists.txt
index 626c54d7..83be7813 100644
--- a/Testing/CMakeLists.txt
+++ b/Testing/CMakeLists.txt
@@ -1,6 +1,15 @@
 #---------------------------------------------------------------------
 # Setup GoogleTest
 
+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()
+
+if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC")
+  include( ${CMAKE_SOURCE_DIR}/CMake/elxGoogleTestWinConfig.cmake )
+endif()
+
 add_subdirectory( GoogleTest )
 
 include_directories(
@@ -25,6 +34,6 @@ set( ElastixUnitTestSource
 foreach( ElastixUnitTestFilename ${ElastixUnitTestSource} )
   string( REPLACE ".cxx" "" ElastixUnitTestExe ${ElastixUnitTestFilename} )
   add_executable( ${ElastixUnitTestExe} ${ElastixUnitTestFilename} )
-  target_link_libraries( ${ElastixUnitTestExe} ${TEST_LIBRARIES} )
+  target_link_libraries( ${ElastixUnitTestExe} ${ELASTIX_LIBRARIES} ${TEST_LIBRARIES} )
   add_test( ElastixUnitTests ${ElastixUnitTestExe} )
 endforeach()
\ No newline at end of file
-- 
GitLab