From ad03e7825b775a149a72512d7c3c52d3891f74e0 Mon Sep 17 00:00:00 2001
From: Olivier BICHLER <Olivier.bichler@cea.fr>
Date: Thu, 3 Aug 2023 19:19:12 +0200
Subject: [PATCH] Fixed compile issue with Pybind

---
 CMakeLists.txt                   | 16 ++++++----------
 cmake/PybindModuleCreation.cmake | 11 +++++++----
 2 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1f388a3c..10a033be 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,4 +1,4 @@
-cmake_minimum_required(VERSION 3.11)
+cmake_minimum_required(VERSION 3.15)
 
 set(project aidge_backend_cpu)  # This will also be python module name 
 set(module_name _${project})    # target name
@@ -46,17 +46,13 @@ target_include_directories(${module_name}
 generate_python_binding(${project} ${module_name})
 
 if (PYBIND)
-    message(STATUS "PYTHON INCLUDE DIR : ${PYTHON_INCLUDE_DIRS}")
-    message(STATUS "PYTHON PYTHON_LIBRARY : ${PYTHON_LIBRARIES}")
-
-    target_include_directories(${module_name}
-        PUBLIC
-            $<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS}>
-    )
+    # Handles Python + pybind11 headers dependencies
     target_link_libraries(${module_name}
+        PUBLIC 
+            pybind11::pybind11
         PRIVATE
-            ${PYTHON_LIBRARIES}
-    )
+            Python::Python
+        )
 endif()
 
 target_compile_features(${module_name} PRIVATE cxx_std_14)
diff --git a/cmake/PybindModuleCreation.cmake b/cmake/PybindModuleCreation.cmake
index b6422bee..18f4abc3 100644
--- a/cmake/PybindModuleCreation.cmake
+++ b/cmake/PybindModuleCreation.cmake
@@ -9,12 +9,15 @@ function(generate_python_binding name target_to_bind)
         GIT_TAG        v2.10.4 # or a later release
         )
 
+        # Use the New FindPython mode, recommanded. Requires CMake 3.15+
+        find_package(Python COMPONENTS Interpreter Development)
         FetchContent_MakeAvailable(PyBind11)
+
         message(STATUS "Creating binding for module ${name}")
         file(GLOB_RECURSE pybind_src_files "python_binding/*.cpp")
-        pybind11_add_module(${name}  MODULE ${pybind_src_files})
-        target_include_directories(${name}  PUBLIC ${pybind11_INCLUDE_DIRS} "python_binding")
-        target_link_libraries(${name} PUBLIC ${target_to_bind})
-        
+
+        pybind11_add_module(${name} MODULE ${pybind_src_files} "NO_EXTRAS") # NO EXTRA recquired for pip install
+        target_include_directories(${name} PUBLIC "python_binding")
+        target_link_libraries(${name} PUBLIC ${target_to_bind})        
     endif()
 endfunction()
-- 
GitLab