From 33a586b25bb297a511bb0b99d0d6e7c34bb87056 Mon Sep 17 00:00:00 2001
From: Paul Gessinger <hello@paulgessinger.com>
Date: Tue, 15 Mar 2022 09:10:36 +0100
Subject: [PATCH] Add flag to relax exact python version requirement

As it stands, this project requires to find a python version matching
the one ROOT was build against EXACTLY. This has been discussed in #842.
This adds an option `DD4HEP_RELAX_PYVER` that will accept **any** python
version. It will then perform a custom check if major and minor version
of the ROOT python version and the one found match (in which it should
be safe to assume ABI compatibility). If not, it prints a warning but
continues with the build.
---
 CMakeLists.txt          |  1 +
 cmake/DD4hepBuild.cmake | 19 ++++++++++++++++---
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index cb25314f4..70fbc0a2d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -81,6 +81,7 @@ option(DD4HEP_LOAD_ASSIMP       "Download and build ASSIMP from github"
 option(BUILD_TESTING            "Enable and build tests"                        ON)
 option(BUILD_SHARED_LIBS        "If OFF build STATIC Libraries"                 ON)
 option(DD4HEP_SET_RPATH         "Link libraries with built-in RPATH (run-time search path)" ON)
+option(DD4HEP_RELAX_PYVER       "Do not require exact python version match with ROOT" OFF)
 
 SET(DD4HEP_BUILD_PACKAGES "DDRec DDDetectors DDCond DDAlign DDCAD DDDigi DDG4 DDEve UtilityApps"
   CACHE STRING "List of DD4hep packages to build")
diff --git a/cmake/DD4hepBuild.cmake b/cmake/DD4hepBuild.cmake
index 3d5d4bd8f..20d08880c 100644
--- a/cmake/DD4hepBuild.cmake
+++ b/cmake/DD4hepBuild.cmake
@@ -686,9 +686,22 @@ macro(DD4HEP_SETUP_ROOT_TARGETS)
       SET(REQUIRE_PYTHON_VERSION ${ROOT_PYTHON_VERSION})
     ENDIF()
     dd4hep_debug("D++> Python version used for building ROOT ${ROOT_PYTHON_VERSION}" )
-    dd4hep_debug("D++> Required python version ${REQUIRE_PYTHON_VERSION}")
-    FIND_PACKAGE(Python ${REQUIRE_PYTHON_VERSION} EXACT REQUIRED COMPONENTS Development)
-    FIND_PACKAGE(Python ${REQUIRE_PYTHON_VERSION} EXACT QUIET COMPONENTS Interpreter)
+    if (NOT DD4HEP_RELAX_PYVER)
+      dd4hep_debug("D++> Required python version ${REQUIRE_PYTHON_VERSION}")
+      FIND_PACKAGE(Python ${REQUIRE_PYTHON_VERSION} EXACT REQUIRED COMPONENTS Development)
+      FIND_PACKAGE(Python ${REQUIRE_PYTHON_VERSION} EXACT QUIET COMPONENTS Interpreter)
+    else()
+      FIND_PACKAGE(Python REQUIRED COMPONENTS Development)
+      FIND_PACKAGE(Python QUIET COMPONENTS Interpreter)
+      dd4hep_debug("D++> Found python version ${Python_VERSION}")
+      string(REPLACE "." ";" _root_pyver_tuple ${REQUIRE_PYTHON_VERSION})
+      list(GET _root_pyver_tuple 0 _root_pyver_major)
+      list(GET _root_pyver_tuple 1 _root_pyver_minor)
+      if (NOT "${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}" VERSION_EQUAL "${_root_pyver_major}.${_root_pyver_minor}")
+        dd4hep_print("WARNING: Mismatch in Python version: ${Python_VERSION} vs. ${REQUIRE_PYTHON_VERSION}")
+        dd4hep_print("         ABI compatibility should not be assumed!")
+      endif()
+    endif()
   ELSE()
     FIND_PACKAGE(Python COMPONENTS Development)
     FIND_PACKAGE(Python QUIET COMPONENTS Interpreter)
-- 
GitLab