From 70166cb0b3018037bc07b4c16b56a5991a33f6cc Mon Sep 17 00:00:00 2001
From: Christoph Lehmann <christoph.lehmann@ufz.de>
Date: Fri, 17 Aug 2018 09:27:44 +0200
Subject: [PATCH] Check Python version mismatch, cleanup definitions

---
 CMakeLists.txt            |  7 +------
 ThirdParty/CMakeLists.txt | 31 +++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 97f86318c2d..add4d4668f4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -210,7 +210,7 @@ option(OGS_ENABLE_ELEMENT_PYRAMID "Build FEM elements for pyramids." ON)
 
 option(OGS_CHECK_HEADER_COMPILATION "Check header for standalone compilation." OFF)
 
-option(OGS_USE_PYTHON "Interface with python" OFF)
+option(OGS_USE_PYTHON "Interface with Python" OFF)
 
 ###################
 ### Definitions ###
@@ -266,11 +266,6 @@ if(MSVC AND OGS_32_BIT)
 endif()
 # End Eigen
 
-if(OGS_USE_PYTHON)
-    add_definitions(-DOGS_USE_PYTHON)
-    add_subdirectory(ThirdParty/pybind11)
-endif()
-
 if (OGS_FATAL_ABORT)
     add_definitions(-DOGS_FATAL_ABORT)
 endif()
diff --git a/ThirdParty/CMakeLists.txt b/ThirdParty/CMakeLists.txt
index ea686f5ee2f..2cdae58ab87 100644
--- a/ThirdParty/CMakeLists.txt
+++ b/ThirdParty/CMakeLists.txt
@@ -45,6 +45,37 @@ if(OGS_BUILD_SWMM)
     set_target_properties(SWMM SwmmInterface PROPERTIES COMPILE_FLAGS /W0)
 endif()
 
+if(OGS_USE_PYTHON)
+    add_subdirectory(pybind11)
+
+    function(check_python_version_compatibility)
+        if(NOT EXISTS ${VTK_DIR}/VTKConfig.cmake)
+            return()
+        endif()
+
+        include(${VTK_DIR}/VTKConfig.cmake)
+
+        if(NOT ${VTK_WRAP_PYTHON})
+            return()
+        endif()
+        if(NOT EXISTS ${VTK_MODULES_DIR}/vtkPython.cmake)
+            return()
+        endif()
+
+        include(${VTK_MODULES_DIR}/vtkPython.cmake)
+
+        if (NOT "${vtkPython_LIBRARIES}" STREQUAL "${PYTHON_LIBRARIES}")
+            message(SEND_ERROR "Mismatch between VTK's and OpenGeoSys' Python "
+                "libraries: ${vtkPython_LIBRARIES} vs. ${PYTHON_LIBRARIES}. "
+                "This will lead to compilation or linking errors. "
+                "You can fix this error by using the same Python version for "
+                "OpenGeoSys as VTK is built with.")
+        endif()
+    endfunction()
+
+    check_python_version_compatibility()
+endif()
+
 if(OGS_BUILD_UTILS)
     include(${PROJECT_SOURCE_DIR}/scripts/cmake/MetisSetup.cmake)
 endif()
-- 
GitLab