From dd7fdacfad27a9e25ece8c68f2f4d00dc68852ba Mon Sep 17 00:00:00 2001
From: Christophe Guillon <christophe.guillon@inria.fr>
Date: Thu, 18 Jul 2024 14:22:00 +0200
Subject: [PATCH] [CMake] Add installation of python binding

Add installation of the python bindings target to the build system.
This avoids to have a specific postprocessing in the
python package management tools.
---
 CMakeLists.txt | 13 ++++++++++++-
 setup.py       | 18 ++++--------------
 2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index fca382c01..776c4e3be 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -16,6 +16,7 @@ message(STATUS "Project version: ${version}")
 
 # Note : project name is {project} and python module name is also {project}
 set(module_name _${CMAKE_PROJECT_NAME}) # target name
+set(pybind_module_name ${CMAKE_PROJECT_NAME}) # name of submodule for python bindings
 
 ##############################################
 # Define options
@@ -84,8 +85,12 @@ endif()
 
 # PYTHON BINDING
 if (PYBIND)
+    # Python binding lib is by default installed in <prefix>/python_packages/<package>/
+    # When installed from python, setup.py should set it to the python package dir
+    set(PYBIND_INSTALL_PREFIX python_packages/${pybind_module_name} CACHE PATH "Python package install prefix")
+
     include(PybindModuleCreation)
-    generate_python_binding(${CMAKE_PROJECT_NAME} ${module_name})
+    generate_python_binding(${pybind_module_name} ${module_name})
 
     # Handles Python + pybind11 headers dependencies
     target_link_libraries(${module_name}
@@ -156,6 +161,12 @@ install(TARGETS ${module_name} EXPORT ${CMAKE_PROJECT_NAME}-targets
 
 install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
 
+if (PYBIND)
+    install(TARGETS ${pybind_module_name}
+        DESTINATION ${PYBIND_INSTALL_PREFIX}
+    )
+endif()
+
 #Export the targets to a script
 
 install(EXPORT ${CMAKE_PROJECT_NAME}-targets
diff --git a/setup.py b/setup.py
index 96ccac47a..3c8411352 100644
--- a/setup.py
+++ b/setup.py
@@ -33,6 +33,9 @@ class CMakeBuild(build_ext):
         if not build_lib.exists():
             build_lib.mkdir(parents=True, exist_ok=True)
 
+        package_prefix = build_lib
+        pybind_install_prefix = (package_prefix / PROJECT_NAME).absolute()
+
         os.chdir(str(build_temp))
 
         compile_type = os.environ.get("AIDGE_PYTHON_BUILD_TYPE", "Release")
@@ -57,6 +60,7 @@ class CMakeBuild(build_ext):
                 f"-DCMAKE_INSTALL_PREFIX:PATH={install_path}",
                 f"-DCMAKE_BUILD_TYPE={compile_type}",
                 "-DPYBIND=ON",
+                f"-DPYBIND_INSTALL_PREFIX:PATH={pybind_install_prefix}",
                 "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON",
                 "-DCOVERAGE=OFF",
             ]
@@ -69,20 +73,6 @@ class CMakeBuild(build_ext):
             self.spawn(["cmake", "--install", ".", "--config", compile_type])
         os.chdir(str(cwd))
 
-        aidge_package = build_lib / PROJECT_NAME
-
-        # Get "aidge core" package
-        # ext_lib = build_temp
-        print(build_temp.absolute())
-        # Copy all shared object files from build_temp/lib to aidge_package
-        for root, _, files in os.walk(build_temp.absolute()):
-            for file in files:
-                if (file.endswith(".so") or file.endswith(".pyd")) and (
-                    root != str(aidge_package.absolute())
-                ):
-                    currentFile = os.path.join(root, file)
-                    shutil.copy(currentFile, str(aidge_package.absolute()))
-
 
 if __name__ == "__main__":
     setup(
-- 
GitLab