From 96ad32e689be15d658c15439c0dffd4a7c3b05af Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Thu, 4 Jun 2015 14:41:09 +0200
Subject: [PATCH] Crosscompiling to Windows on Mac OS with MXE.

- Raised minimum CMake to 3.1 because of better crosscompiling support
  and imported target feature
- When Eigen is not found it gets downloaded before BaseLib is build
- Silenced OpenMP CMake output

Executables can be run with Wine.
---
 Applications/CLI/CMakeLists.txt               |  2 +-
 Applications/DataExplorer/Base/CMakeLists.txt |  2 +-
 Applications/DataExplorer/DataExplorer.cmake  | 14 ++++++--
 .../DataExplorer/DataView/CMakeLists.txt      |  2 +-
 .../DataView/DiagramView/CMakeLists.txt       |  2 +-
 .../DataView/StratView/CMakeLists.txt         |  2 +-
 .../DataExplorer/VtkAct/CMakeLists.txt        |  2 +-
 .../DataExplorer/VtkVis/CMakeLists.txt        |  2 +-
 Applications/DataExplorer/exe-icon.rc         |  2 +-
 BaseLib/CMakeLists.txt                        |  4 +++
 CMakeLists.txt                                |  6 ++--
 FileIO/CMakeLists.txt                         |  3 ++
 Tests/CMakeLists.txt                          | 11 +++---
 scripts/cmake/CompilerSetup.cmake             |  7 ++--
 scripts/cmake/Find.cmake                      | 34 +++++++++++--------
 scripts/cmake/packaging/PackagingWin.cmake    |  5 ++-
 16 files changed, 64 insertions(+), 36 deletions(-)

diff --git a/Applications/CLI/CMakeLists.txt b/Applications/CLI/CMakeLists.txt
index 203f3d522b3..72b8fbad59e 100644
--- a/Applications/CLI/CMakeLists.txt
+++ b/Applications/CLI/CMakeLists.txt
@@ -12,7 +12,7 @@ target_link_libraries(ogs
     NumLib
     ${Boost_LIBRARIES}
     ${CATALYST_LIBRARIES}
-
+    Threads::Threads
 )
 
 if(OGS_BUILD_GUI)
diff --git a/Applications/DataExplorer/Base/CMakeLists.txt b/Applications/DataExplorer/Base/CMakeLists.txt
index f64ff321f6d..a86ef47ce4d 100644
--- a/Applications/DataExplorer/Base/CMakeLists.txt
+++ b/Applications/DataExplorer/Base/CMakeLists.txt
@@ -47,6 +47,6 @@ add_library(QtBase STATIC
 	${MOC_SOURCES}
 )
 
-target_link_libraries(QtBase ${QT_LIBRARIES})
+target_link_libraries(QtBase Qt4::QtGui)
 
 set_property(TARGET QtBase PROPERTY FOLDER "DataExplorer")
diff --git a/Applications/DataExplorer/DataExplorer.cmake b/Applications/DataExplorer/DataExplorer.cmake
index b8c43398f05..d4c8d9e2113 100644
--- a/Applications/DataExplorer/DataExplorer.cmake
+++ b/Applications/DataExplorer/DataExplorer.cmake
@@ -74,14 +74,16 @@ add_executable(DataExplorer MACOSX_BUNDLE
 )
 
 target_link_libraries(DataExplorer
-	${QT_LIBRARIES}
+	Qt4::QtCore
+	Qt4::QtGui
+	Qt4::QtXml
+	Qt4::QtNetwork
 	ApplicationsLib
 	BaseLib
 	GeoLib
 	FileIO
 	InSituLib
 	MeshLib
-	#MSHGEOTOOLS
 	QtBase
 	QtDataView
 	QtStratView
@@ -93,6 +95,14 @@ target_link_libraries(DataExplorer
 	shp
 )
 
+if(CMAKE_CROSSCOMPILING)
+	target_link_libraries(DataExplorer
+		${QT_XML_DEPS_LIBRARIES}
+		${QT_GUI_DEPS_LIBRARIES}
+		${QT_NETWORK_DEPS_LIBRARIES}
+	)
+endif()
+
 if(VTK_NETCDF_FOUND)
 	target_link_libraries(DataExplorer vtkNetCDF vtkNetCDF_cxx )
 else()
diff --git a/Applications/DataExplorer/DataView/CMakeLists.txt b/Applications/DataExplorer/DataView/CMakeLists.txt
index 8f38ba422e9..bfcb7a8da0f 100644
--- a/Applications/DataExplorer/DataView/CMakeLists.txt
+++ b/Applications/DataExplorer/DataView/CMakeLists.txt
@@ -176,7 +176,7 @@ add_library(QtDataView STATIC
 
 # Link Qt library
 target_link_libraries(QtDataView
-	${QT_LIBRARIES}
+	Qt4::QtCore
 	FileIO
 	GeoLib
 	MeshLib
diff --git a/Applications/DataExplorer/DataView/DiagramView/CMakeLists.txt b/Applications/DataExplorer/DataView/DiagramView/CMakeLists.txt
index a1aad85c51e..9df92e614c8 100644
--- a/Applications/DataExplorer/DataView/DiagramView/CMakeLists.txt
+++ b/Applications/DataExplorer/DataView/DiagramView/CMakeLists.txt
@@ -65,7 +65,7 @@ add_library(QtDiagramView STATIC
 
 # Link Qt library
 target_link_libraries(QtDiagramView
-	${QT_LIBRARIES}
+	Qt4::QtGui
 	GeoLib
 )
 
diff --git a/Applications/DataExplorer/DataView/StratView/CMakeLists.txt b/Applications/DataExplorer/DataView/StratView/CMakeLists.txt
index 1f07213bcf1..ee464388472 100644
--- a/Applications/DataExplorer/DataView/StratView/CMakeLists.txt
+++ b/Applications/DataExplorer/DataView/StratView/CMakeLists.txt
@@ -57,7 +57,7 @@ add_library(QtStratView STATIC
 
 # Link Qt library
 target_link_libraries(QtStratView
-	${QT_LIBRARIES}
+	Qt4::QtGui
 	BaseLib
 	GeoLib
 	MathLib
diff --git a/Applications/DataExplorer/VtkAct/CMakeLists.txt b/Applications/DataExplorer/VtkAct/CMakeLists.txt
index 55a17f8932c..ed364873447 100644
--- a/Applications/DataExplorer/VtkAct/CMakeLists.txt
+++ b/Applications/DataExplorer/VtkAct/CMakeLists.txt
@@ -39,6 +39,6 @@ add_library( VtkAct STATIC
 include(AddCatalystDependency)
 ADD_CATALYST_DEPENDENCY(VtkAct)
 
-target_link_libraries( VtkAct ${QT_LIBRARIES} )
+target_link_libraries( VtkAct Qt4::QtGui )
 
 set_property(TARGET VtkAct PROPERTY FOLDER "DataExplorer")
diff --git a/Applications/DataExplorer/VtkVis/CMakeLists.txt b/Applications/DataExplorer/VtkVis/CMakeLists.txt
index 82038dc9018..ad3a74b1173 100644
--- a/Applications/DataExplorer/VtkVis/CMakeLists.txt
+++ b/Applications/DataExplorer/VtkVis/CMakeLists.txt
@@ -156,7 +156,7 @@ if(GEOTIFF_FOUND)
 	target_link_libraries(VtkVis ${GEOTIFF_LIBRARIES})
 endif() # GEOTIFF_FOUND
 
-target_link_libraries(VtkVis ${QT_LIBRARIES})
+target_link_libraries(VtkVis Qt4::QtGui)
 
 add_dependencies(VtkVis QtDataView)
 
diff --git a/Applications/DataExplorer/exe-icon.rc b/Applications/DataExplorer/exe-icon.rc
index 1e5fc317137..b6cc85448d6 100644
--- a/Applications/DataExplorer/exe-icon.rc
+++ b/Applications/DataExplorer/exe-icon.rc
@@ -1 +1 @@
- IDI_ICON1               ICON    DISCARDABLE     "..\..\..\scripts\packaging\ogs-de-icon.ico"
\ No newline at end of file
+ IDI_ICON1               ICON    DISCARDABLE     "../../../scripts/packaging/ogs-de-icon.ico"
diff --git a/BaseLib/CMakeLists.txt b/BaseLib/CMakeLists.txt
index f6feac94ef8..d05aad29d88 100644
--- a/BaseLib/CMakeLists.txt
+++ b/BaseLib/CMakeLists.txt
@@ -18,3 +18,7 @@ target_link_libraries(BaseLib
 if(WIN32)
 	target_link_libraries(BaseLib WinMM)
 endif()
+
+if(TARGET Eigen)
+	add_dependencies(BaseLib Eigen)
+endif()
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 98cb5e89e7e..a4ce939d92c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3,13 +3,11 @@
 #####################
 
 # Specify minimum CMake version
-cmake_minimum_required(VERSION 2.8.8)
+cmake_minimum_required(VERSION 3.1)
 
 # Set CMake policies
 cmake_policy(SET CMP0011 OLD)
-if(CMAKE_VERSION VERSION_GREATER 3.0.2)
-	cmake_policy(SET CMP0054 NEW)
-endif()
+cmake_policy(SET CMP0054 NEW)
 
 # Project name
 project( OGS-6 )
diff --git a/FileIO/CMakeLists.txt b/FileIO/CMakeLists.txt
index 96435c22b1f..b5a08d1f30b 100644
--- a/FileIO/CMakeLists.txt
+++ b/FileIO/CMakeLists.txt
@@ -59,6 +59,9 @@ target_link_libraries(FileIO
 	zlib
 	shp
 )
+if(QT4_FOUND)
+	target_link_libraries(FileIO Qt4::QtXml Qt4::QtXmlPatterns)
+endif()
 
 ADD_CATALYST_DEPENDENCY(FileIO)
 
diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt
index 4e62a27879f..438296035dc 100644
--- a/Tests/CMakeLists.txt
+++ b/Tests/CMakeLists.txt
@@ -41,9 +41,6 @@ include_directories(
 
 add_executable(testrunner testrunner.cpp ${TEST_SOURCES})
 set_target_properties(testrunner PROPERTIES FOLDER Testing)
-if(TARGET Eigen)
-	add_dependencies(testrunner Eigen)
-endif()
 
 target_link_libraries(testrunner
 	GTest
@@ -81,7 +78,13 @@ if(OGS_BUILD_GUI)
 endif()
 
 if(QT4_FOUND)
-	target_link_libraries(testrunner ${QT_LIBRARIES})
+	target_link_libraries(testrunner Qt4::QtCore Qt4::QtGui Qt4::QtXml Qt4::QtNetwork)
+	if(CMAKE_CROSSCOMPILING)
+		target_link_libraries(testrunner
+			${QT_XML_DEPS_LIBRARIES}
+			${QT_GUI_DEPS_LIBRARIES}
+			${QT_NETWORK_DEPS_LIBRARIES})
+	endif()
 endif()
 
 include(AddCatalystDependency)
diff --git a/scripts/cmake/CompilerSetup.cmake b/scripts/cmake/CompilerSetup.cmake
index bc38d8fc396..4b72f76fbe3 100644
--- a/scripts/cmake/CompilerSetup.cmake
+++ b/scripts/cmake/CompilerSetup.cmake
@@ -100,8 +100,11 @@ if (WIN32)
 		DisableCompilerFlag(DEBUG /RTC1)
 	# cygwin
 	else()
-		message (STATUS "Might be GCC under cygwin.")
-		add_definitions( -DGCC )
+		if(CMAKE_CROSSCOMPILING)
+			message(STATUS "Crosscompiling for Windows with MinGW.")
+		else()
+			message(STATUS "Might be GCC under cygwin.")
+		endif()
 	endif()
 endif ()
 
diff --git a/scripts/cmake/Find.cmake b/scripts/cmake/Find.cmake
index d0269a98ded..bd2dd5ed3ca 100644
--- a/scripts/cmake/Find.cmake
+++ b/scripts/cmake/Find.cmake
@@ -71,33 +71,37 @@ if(OGS_NO_EXTERNAL_LIBS)
 	return()
 endif() # OGS_NO_EXTERNAL_LIBS
 
-find_package(OpenMP)
+find_package(OpenMP QUIET)
 if(OPENMP_FOUND)
 	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
 	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
+	message(STATUS "OpenMP enabled.")
 endif()
 
 find_package(Metis QUIET)
 
 ## Qt4 library ##
-if(NOT OGS_DONT_USE_QT)
-	find_package( Qt4 4.7)
-endif()
-
-if(QT4_FOUND)
-	# Enable more modules
-	set(QT_USE_QTOPENGL TRUE)
-	set(QT_USE_QTSQL TRUE)
-	set(QT_USE_QTTEST TRUE)
-	set(QT_USE_QTXML TRUE)
-	set(QT_USE_QTXMLPATTERNS TRUE)
-	include( ${QT_USE_FILE} )
-	add_definitions(${QT_DEFINITIONS} -DQT4_FOUND)
+if(OGS_BUILD_GUI)
+	find_package( Qt4 4.7 REQUIRED QtGui QtXml QtXmlPatterns)
+	cmake_policy(SET CMP0020 NEW)
+	if(CMAKE_CROSSCOMPILING)
+		find_package(PkgConfig REQUIRED)
+		pkg_check_modules(QT_XML_DEPS REQUIRED QtXml)
+		list(REMOVE_ITEM QT_XML_DEPS_LIBRARIES QtXml QtCore)
+		pkg_check_modules(QT_GUI_DEPS REQUIRED QtGui)
+		list(REMOVE_ITEM QT_GUI_DEPS_LIBRARIES QtGui QtCore)
+		pkg_check_modules(QT_NETWORK_DEPS REQUIRED QtNetwork)
+		list(REMOVE_ITEM QT_NETWORK_DEPS_LIBRARIES QtNetwork QtCore)
+	endif()
 endif()
 
 ## pthread ##
+if(CMAKE_CROSSCOMPILING)
+	set(THREADS_PTHREAD_ARG 0 CACHE STRING "Result from TRY_RUN" FORCE)
+endif()
 set(CMAKE_THREAD_PREFER_PTHREAD ON)
-find_package(Threads)
+set(THREADS_PREFER_PTHREAD_FLAG ON)
+find_package(Threads REQUIRED)
 if(CMAKE_USE_PTHREADS_INIT)
 	set(HAVE_PTHREADS TRUE)
 	add_definitions(-DHAVE_PTHREADS)
diff --git a/scripts/cmake/packaging/PackagingWin.cmake b/scripts/cmake/packaging/PackagingWin.cmake
index 26894f30b89..d92206d56b6 100644
--- a/scripts/cmake/packaging/PackagingWin.cmake
+++ b/scripts/cmake/packaging/PackagingWin.cmake
@@ -1,6 +1,9 @@
 set(CMAKE_INSTALL_OPENMP_LIBRARIES ON)
 include(InstallRequiredSystemLibraries)
-set(CPACK_GENERATOR NSIS ZIP)
+set(CPACK_GENERATOR ZIP)
+if(NOT CMAKE_CROSSCOMPILING)
+	set(CPACK_GENERATOR NSIS ZIP)
+endif()
 set(CPACK_NSIS_MUI_ICON ${CMAKE_SOURCE_DIR}/scripts/packaging/ogs-de-icon.ico)
 file(TO_NATIVE_PATH "${CMAKE_SOURCE_DIR}/Documentation/OpenGeoSys-Logo.bmp" BACKGROUND_IMAGE)
 set(CPACK_PACKAGE_ICON ${BACKGROUND_IMAGE})
-- 
GitLab