From 1bcf11d8be2dfd2b66fc411b49695413694087a1 Mon Sep 17 00:00:00 2001
From: Christophe Guillon <christophe.guillon@inria.fr>
Date: Tue, 27 Aug 2024 16:01:27 +0200
Subject: [PATCH] [Build] Align build to new aidge_core dependencies

Simplify dependencies for binding and allow both TEST and PYBIND on
when Python embedded interpreter is available.
This aligns to the new aidge_core dependencies which are self-sufficient.
Update cmake requirements to >=3.18+ in order to support missing
Python embedded interpreter.

This change requires the installed aidge_core library to include the
merge request eclipse/aidge/aidge_core!187
---
 CMakeLists.txt                    | 21 +++++++--------------
 aidge_backend_cpu-config.cmake.in |  7 +++++++
 cmake/PybindModuleCreation.cmake  | 11 +++++------
 pyproject.toml                    |  2 +-
 setup.py                          |  3 ++-
 unit_tests/CMakeLists.txt         |  2 +-
 6 files changed, 23 insertions(+), 23 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6f5a8cbf..3574e25c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,4 +1,4 @@
-cmake_minimum_required(VERSION 3.15)
+cmake_minimum_required(VERSION 3.18)
 set(CXX_STANDARD 14)
 
 file(STRINGS "${CMAKE_SOURCE_DIR}/version.txt" version)
@@ -76,13 +76,6 @@ if (PYBIND)
 
     include(PybindModuleCreation)
     generate_python_binding(${pybind_module_name} ${module_name})
-
-    target_link_libraries(${module_name}
-        PUBLIC
-            pybind11::pybind11
-        PRIVATE
-            Python::Module
-        )
 endif()
 
 if( ${ENABLE_ASAN} )
@@ -106,7 +99,6 @@ target_include_directories(${module_name}
         ${CMAKE_CURRENT_SOURCE_DIR}/src
 )
 
-target_link_libraries(${module_name} PUBLIC fmt::fmt)
 target_compile_features(${module_name} PRIVATE cxx_std_14)
 
 target_compile_options(${module_name} PRIVATE
@@ -169,15 +161,16 @@ install(FILES
 ## Exporting from the build tree
 message(STATUS "Exporting created targets to use them in another build")
 export(EXPORT ${CMAKE_PROJECT_NAME}-targets
-    FILE "${CMAKE_CURRENT_BINARY_DIR}/${project}-targets.cmake")
+    FILE "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}-targets.cmake")
 
 
 ##############################################
 ## Add test
 if(TEST)
-    if(PYBIND)
-        message(FATAL_ERROR "PYBIND and TEST are both enabled. But cannot compile with catch_2.\nChoose between pybind and Catch2 for compilation.")
+    if (AIDGE_REQUIRES_PYTHON AND NOT AIDGE_PYTHON_HAS_EMBED)
+        message(WARNING "Skipping compilation of tests: missing Python embedded interpreter")
+    else()
+        enable_testing()
+        add_subdirectory(unit_tests)
     endif()
-    enable_testing()
-    add_subdirectory(unit_tests)
 endif()
diff --git a/aidge_backend_cpu-config.cmake.in b/aidge_backend_cpu-config.cmake.in
index f3604be1..d8e1372b 100644
--- a/aidge_backend_cpu-config.cmake.in
+++ b/aidge_backend_cpu-config.cmake.in
@@ -1,3 +1,10 @@
+@PACKAGE_INIT@
+
+include(CMakeFindDependencyMacro)
+find_dependency(aidge_core)
+
+include(CMakeFindDependencyMacro)
+
 include(${CMAKE_CURRENT_LIST_DIR}/aidge_backend_cpu-config-version.cmake)
 
 include(${CMAKE_CURRENT_LIST_DIR}/aidge_backend_cpu-targets.cmake)
diff --git a/cmake/PybindModuleCreation.cmake b/cmake/PybindModuleCreation.cmake
index 8f386bef..a520039f 100644
--- a/cmake/PybindModuleCreation.cmake
+++ b/cmake/PybindModuleCreation.cmake
@@ -1,9 +1,10 @@
 function(generate_python_binding pybind_module_name target_to_bind) 
-    add_definitions(-DPYBIND)
+
+    find_package(Python COMPONENTS Interpreter Development.Module)
+
     Include(FetchContent)
 
     set(PYBIND_VERSION v2.10.4)
-    set(PYBIND11_FINDPYTHON ON)
     message(STATUS "Retrieving pybind ${PYBIND_VERSION} from git")
 
     FetchContent_Declare(
@@ -12,14 +13,12 @@ function(generate_python_binding pybind_module_name target_to_bind)
         GIT_TAG        ${PYBIND_VERSION} # or a later release
     )
 
-    # Use the New FindPython mode, recommanded. Requires CMake 3.15+
-    find_package(Python COMPONENTS Interpreter Development.Module)
     FetchContent_MakeAvailable(PyBind11)
 
     message(STATUS "Creating binding for module ${pybind_module_name}")
     file(GLOB_RECURSE pybind_src_files "python_binding/*.cpp")
 
     pybind11_add_module(${pybind_module_name} MODULE ${pybind_src_files} "NO_EXTRAS") # NO EXTRA recquired for pip install
-    target_include_directories(${pybind_module_name} PUBLIC "python_binding")
-    target_link_libraries(${pybind_module_name} PUBLIC ${target_to_bind})
+    target_include_directories(${pybind_module_name} PRIVATE "python_binding")
+    target_link_libraries(${pybind_module_name} PRIVATE ${target_to_bind})
 endfunction()
diff --git a/pyproject.toml b/pyproject.toml
index ff7a1736..9dbdbede 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -17,7 +17,7 @@ dynamic = ["version"] # defined in tool.setuptools_scm
 requires = [
     "setuptools>=64",
     "setuptools_scm[toml]==7.1.0",
-    "cmake>=3.15.3.post1"
+    "cmake>=3.18.4.post1"
 ]
 build-backend = "setuptools.build_meta"
 
diff --git a/setup.py b/setup.py
index a2b50c38..22cbd973 100644
--- a/setup.py
+++ b/setup.py
@@ -65,13 +65,14 @@ class AidgePkgBuild(build_ext):
             if build_gen
             else []
         )
+        test_onoff = os.environ.get("AIDGE_BUILD_TEST", "OFF")
         
         self.spawn(
             [
                 "cmake",
                 *build_gen_opts,
                 str(cwd),
-                "-DTEST=OFF",
+                f"-DTEST={test_onoff}",
                 f"-DCMAKE_INSTALL_PREFIX:PATH={install_path}",
                 f"-DCMAKE_BUILD_TYPE={compile_type}",
                 "-DPYBIND=ON",
diff --git a/unit_tests/CMakeLists.txt b/unit_tests/CMakeLists.txt
index 671cdd5a..8178df93 100644
--- a/unit_tests/CMakeLists.txt
+++ b/unit_tests/CMakeLists.txt
@@ -12,7 +12,7 @@ file(GLOB_RECURSE src_files "*.cpp")
 
 add_executable(tests${module_name} ${src_files})
 
-target_link_libraries(tests${module_name} PUBLIC ${module_name})
+target_link_libraries(tests${module_name} PRIVATE ${module_name})
 
 target_link_libraries(tests${module_name} PRIVATE Catch2::Catch2WithMain)
 
-- 
GitLab