From cc6900981e6a25ac995104995cc9081128575e22 Mon Sep 17 00:00:00 2001
From: Julien Schueller <schueller@phimeca.com>
Date: Tue, 14 Jan 2025 11:11:13 +0100
Subject: [PATCH] CMake: Add option to select Python SABI

---
 CMakeLists.txt | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d4d0269792..773e853490 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -549,14 +549,17 @@ if (BUILD_PYTHON)
     endif ()
   endif ()
 
-  if (CMAKE_VERSION VERSION_LESS 3.26)
+  if (CMAKE_VERSION VERSION_LESS 3.18)
     find_package (Python 3.6 COMPONENTS Interpreter Development)
-  else ()
-    find_package (Python 3.6 COMPONENTS Interpreter Development.Module Development.SABIModule)
-  endif ()
-
-  if (NOT TARGET Python::Module)
     include (TargetLinkLibrariesWithDynamicLookup)
+  else ()
+    option (USE_PYTHON_SABI "Use Python stable ABI" OFF)
+    if (USE_PYTHON_SABI AND CMAKE_VERSION VERSION_GREATER_EQUAL 3.26)
+      find_package (Python 3.6 COMPONENTS Interpreter Development.SABIModule)
+      add_library (Python::Module ALIAS Python::SABIModule)
+    else ()
+      find_package (Python 3.6 COMPONENTS Interpreter Development.Module)
+    endif ()
   endif ()
 
   if (Python_FOUND)