From a84fe4dbdfe2180996636669ad2a2dad6a63ad5c Mon Sep 17 00:00:00 2001
From: Rashad Kanavath <rashad.kanavath@c-s.fr>
Date: Thu, 22 Sep 2016 16:12:35 +0200
Subject: [PATCH] SuperBuild: special care needed to build curl and GDAL on
 msvc x64

---
 SuperBuild/CMake/External_curl.cmake            | 17 +++++------------
 SuperBuild/CMake/External_gdal.cmake            |  6 ++++++
 SuperBuild/CMakeLists.txt                       | 15 +++++++++++++++
 SuperBuild/patches/GDAL/nmake_gdal_extra.opt.in |  4 ++++
 4 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/SuperBuild/CMake/External_curl.cmake b/SuperBuild/CMake/External_curl.cmake
index ca006b1655..22638af0d5 100644
--- a/SuperBuild/CMake/External_curl.cmake
+++ b/SuperBuild/CMake/External_curl.cmake
@@ -15,18 +15,11 @@ if(MSVC)
     return()
   endif()
 
-  execute_process(
-  COMMAND ${CMAKE_C_COMPILER}
-  ERROR_VARIABLE ev
-  OUTPUT_VARIABLE ov
-  OUTPUT_QUIET
-  OUTPUT_STRIP_TRAILING_WHITESPACE)
-
-  if("${ev}" MATCHES "x86")
-	set(CURL_INSTALL_DIR_PREFIX "libcurl-vc-x86")
-  else()
-	set(CURL_INSTALL_DIR_PREFIX "libcurl-vc-x64")
-  endif()
+if("${OTB_MSVC_COMPILER_ARCH}" MATCHES "x86")
+  set(CURL_INSTALL_DIR_PREFIX "libcurl-vc-x86")
+else()
+  set(CURL_INSTALL_DIR_PREFIX "libcurl-vc-x64")
+endif()
 
   set(CURL_INSTALL_DIR_PREFIX "${CURL_INSTALL_DIR_PREFIX}-release-dll-zlib-dll-ipv6-sspi-winssl")
 
diff --git a/SuperBuild/CMake/External_gdal.cmake b/SuperBuild/CMake/External_gdal.cmake
index 05ec5dde9d..70dcc676d5 100644
--- a/SuperBuild/CMake/External_gdal.cmake
+++ b/SuperBuild/CMake/External_gdal.cmake
@@ -89,6 +89,12 @@ if(UNIX)
   #set(GDAL_INSTALL_COMMAND ${CMAKE_MAKE_PROGRAM} install)
 
 else(MSVC)
+  if("${OTB_MSVC_COMPILER_ARCH}" MATCHES "x86")
+    set(BUILDING_WIN64 "NO")
+  else()
+    set(BUILDING_WIN64 "YES")
+  endif()
+
   STRING(REGEX REPLACE "/$" "" CMAKE_WIN_INSTALL_PREFIX ${SB_INSTALL_PREFIX})
   STRING(REGEX REPLACE "/" "\\\\" CMAKE_WIN_INSTALL_PREFIX ${CMAKE_WIN_INSTALL_PREFIX})
   configure_file(${CMAKE_SOURCE_DIR}/patches/GDAL/nmake_gdal_extra.opt.in ${CMAKE_BINARY_DIR}/nmake_gdal_extra.opt)
diff --git a/SuperBuild/CMakeLists.txt b/SuperBuild/CMakeLists.txt
index 4a5593e774..7056f8763d 100644
--- a/SuperBuild/CMakeLists.txt
+++ b/SuperBuild/CMakeLists.txt
@@ -22,6 +22,21 @@ if(APPLE)
   set(CMAKE_MACOSX_RPATH TRUE)
 endif()
 
+if(MSVC)
+  execute_process(
+  COMMAND ${CMAKE_C_COMPILER}
+  ERROR_VARIABLE ev
+  OUTPUT_VARIABLE ov
+  OUTPUT_QUIET
+  OUTPUT_STRIP_TRAILING_WHITESPACE)
+  
+  set(OTB_MSVC_COMPILER_ARCH x64)
+  if("${ev}" MATCHES "x86")
+    set(OTB_MSVC_COMPILER_ARCH x86)
+  endif()
+  
+endif()
+
 # use, i.e. don't skip the full RPATH for the build tree
 set(CMAKE_SKIP_BUILD_RPATH  FALSE)
 # when building, don't use the install RPATH already
diff --git a/SuperBuild/patches/GDAL/nmake_gdal_extra.opt.in b/SuperBuild/patches/GDAL/nmake_gdal_extra.opt.in
index 733dc024b1..d20fd9a62b 100644
--- a/SuperBuild/patches/GDAL/nmake_gdal_extra.opt.in
+++ b/SuperBuild/patches/GDAL/nmake_gdal_extra.opt.in
@@ -13,6 +13,10 @@ JPEG_SUPPORTED = 1
 
 GDAL_HOME="@CMAKE_WIN_INSTALL_PREFIX@"
 
+# Uncomment the following if you are building for 64-bit windows
+# (x64). You'll need to have PATH, INCLUDE and LIB set up for 64-bit
+# compiles.
+WIN64=@BUILDING_WIN64@
 
 #if using an external jpeg library uncomment the follwing lines
 JPEG_EXTERNAL_LIB = 1
-- 
GitLab