From 39898379d46c4a356b8a52c6e5c9349db8e27f6f Mon Sep 17 00:00:00 2001
From: cmoineau <cyril.moineau@cea.fr>
Date: Thu, 3 Aug 2023 06:28:58 +0000
Subject: [PATCH] [CMake] Update CMakeLists to enable standalone compilation
 using Cmake package system.

---
 CMakeLists.txt                    | 101 +++++++++++-------------------
 OLD_CMakeLists.txt                |  57 -----------------
 aidge_backend_cpu-config.cmake.in |   3 +
 cmake/PybindModuleCreation.cmake  |   9 +--
 cpu-config.cmake.in               |   3 -
 python_binding/pybind_cpu.cpp     |   2 +-
 unit_tests/CMakeLists.txt         |  12 ++--
 7 files changed, 50 insertions(+), 137 deletions(-)
 delete mode 100644 OLD_CMakeLists.txt
 create mode 100644 aidge_backend_cpu-config.cmake.in
 delete mode 100644 cpu-config.cmake.in

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4c39f4b2..1f388a3c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,12 +1,10 @@
 cmake_minimum_required(VERSION 3.11)
 
-set(project aidge)
+set(project aidge_backend_cpu)  # This will also be python module name 
+set(module_name _${project})    # target name
+
 set(version 2.0.0)
 project(${project})
-enable_testing()
-# TODO : use a file to store module_name ?
-set(module_name aidge_cpu) # This will be python module name 
-set(component cpu) # Target name must be different than pybind target (prefix module name with _ ? _${module_name} ?
 
 ##############################################
 # Import utils CMakeLists
@@ -20,7 +18,7 @@ option(WERROR "Warning as error" OFF)
 
 ##############################################
 # Find system dependencies
-generate_python_binding(${module_name}) # TODO : cannot be component because of target name
+find_package(aidge_core REQUIRED)
 
 ##############################################
 # Create target and set properties
@@ -28,17 +26,15 @@ generate_python_binding(${module_name}) # TODO : cannot be component because of
 file(GLOB_RECURSE src_files "src/*.cpp")
 file(GLOB_RECURSE inc_files "include/*.hpp")
 
-add_library(${component} ${src_files} ${inc_files})
-
-
-# namespaced alias
-add_library(${project}::${component} ALIAS ${component})
-
+add_library(${module_name} ${src_files} ${inc_files})
+target_link_libraries(${module_name}
+    PUBLIC
+        _aidge_core # _ is added because we link the target not the project
+)
 #Set target properties
-set_property(TARGET ${component} PROPERTY POSITION_INDEPENDENT_CODE ON)
-
+set_property(TARGET ${module_name} PROPERTY POSITION_INDEPENDENT_CODE ON)
 
-target_include_directories(${component}
+target_include_directories(${module_name}
     PUBLIC
         $<INSTALL_INTERFACE:include>
         $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
@@ -46,45 +42,37 @@ target_include_directories(${component}
         ${CMAKE_CURRENT_SOURCE_DIR}/src
 )
 
-message(STATUS "INSTALL INCLUDE DIR : ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}")
-message(STATUS "INSTALL LIB DIR : ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
-
-# TODO : is it good ? Get headers already installed
-# Need to get shared objects !
-target_include_directories(${component}
-    PUBLIC
-        $<BUILD_INTERFACE:${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}> 
-    PRIVATE
-        ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}
-)
+# PYTHON BINDING
+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(${component}
+    target_include_directories(${module_name}
         PUBLIC
             $<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS}>
     )
-    target_link_libraries(${component}
+    target_link_libraries(${module_name}
         PRIVATE
             ${PYTHON_LIBRARIES}
     )
 endif()
-target_compile_features(${component} PRIVATE cxx_std_14)
+
+target_compile_features(${module_name} PRIVATE cxx_std_14)
 
 if(WERROR)
-    target_compile_options(${component} PRIVATE
+    target_compile_options(${module_name} PRIVATE
     $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
     -Wall -Wextra -Wold-style-cast -Winline -pedantic -Werror=narrowing -Wshadow -Werror>)
-    target_compile_options(${component} PRIVATE
+    target_compile_options(${module_name} PRIVATE
     $<$<CXX_COMPILER_ID:MSVC>:
     /W4>)
 else()
-    target_compile_options(${component} PRIVATE
+    target_compile_options(${module_name} PRIVATE
         $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
         -Wall -Wextra -Wold-style-cast -Winline -pedantic -Werror=narrowing -Wshadow -Wpedantic>)
-        target_compile_options(${component} PRIVATE
+        target_compile_options(${module_name} PRIVATE
         $<$<CXX_COMPILER_ID:MSVC>:
         /W4>)
 endif()
@@ -95,67 +83,50 @@ endif()
 include(GNUInstallDirs)
 set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${project})
 
-install(TARGETS ${component} EXPORT ${component}-targets
-  COMPONENT ${component}
+install(TARGETS ${module_name} EXPORT ${project}-targets
   LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
   ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
   RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
 )
 
-
 install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
 
 #Export the targets to a script
-install(EXPORT ${component}-targets
-  FILE "${project}-${component}-targets.cmake"
-  NAMESPACE ${project}::
-  DESTINATION ${INSTALL_CONFIGDIR}
-  COMPONENT ${component}
-)
+
+install(EXPORT ${project}-targets
+ FILE "${project}-targets.cmake"
+ DESTINATION ${INSTALL_CONFIGDIR}
+ COMPONENT ${module_name} 
+)  
 
 #Create a ConfigVersion.cmake file
 include(CMakePackageConfigHelpers)
 write_basic_package_version_file(
-    "${CMAKE_CURRENT_BINARY_DIR}/${project}-${component}-config-version.cmake"
+    "${CMAKE_CURRENT_BINARY_DIR}/${project}-config-version.cmake"
     VERSION ${version}
     COMPATIBILITY AnyNewerVersion
 )
 
-configure_package_config_file("${component}-config.cmake.in"
-    "${CMAKE_CURRENT_BINARY_DIR}/${project}-${component}-config.cmake"
+configure_package_config_file("${project}-config.cmake.in"
+    "${CMAKE_CURRENT_BINARY_DIR}/${project}-config.cmake"
     INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
 )
 
 #Install the config, configversion and custom find modules
 install(FILES
-    "${CMAKE_CURRENT_BINARY_DIR}/${project}-${component}-config.cmake"
-    "${CMAKE_CURRENT_BINARY_DIR}/${project}-${component}-config-version.cmake"
+    "${CMAKE_CURRENT_BINARY_DIR}/${project}-config.cmake"
+    "${CMAKE_CURRENT_BINARY_DIR}/${project}-config-version.cmake"
     DESTINATION ${INSTALL_CONFIGDIR}
-    COMPONENT ${component}
 )
 
 ##############################################
 ## Exporting from the build tree
-export(EXPORT ${component}-targets
-    FILE "${CMAKE_CURRENT_BINARY_DIR}/${project}-${component}-targets.cmake"
-    NAMESPACE ${project}::)
+export(EXPORT ${project}-targets
+    FILE "${CMAKE_CURRENT_BINARY_DIR}/${project}-targets.cmake")
 
 
 ##############################################
 ## Add test
+enable_testing()
 add_subdirectory(unit_tests)
 
-include(CMakePackageConfigHelpers)
-
-write_basic_package_version_file(
-  "${CMAKE_BINARY_DIR}/${project}-config-version.cmake"
-  VERSION ${version}
-  COMPATIBILITY AnyNewerVersion
-)
-
-
-# install(
-#   FILES
-#     "${CMAKE_BINARY_DIR}/${project}-config.cmake"
-#   DESTINATION lib/cmake/${project}
-# )
diff --git a/OLD_CMakeLists.txt b/OLD_CMakeLists.txt
deleted file mode 100644
index 6aadb08b..00000000
--- a/OLD_CMakeLists.txt
+++ /dev/null
@@ -1,57 +0,0 @@
-
-if (BUILD_CPU_ALONE)
-    project(Aidge_CPU)
-    cmake_minimum_required(VERSION 3.11)
-    add_compile_options(-Wall -Wextra -fPIC)
-
-    # Need the Core library to compile the CPU library
-    set(BUILD_CORE_ALONE ON)
-    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../_Core _Core)
-endif()
-
-if (PYBIND)
-    add_definitions(-DPYBIND)
-    Include(FetchContent)
-
-    FetchContent_Declare(
-    PyBind11
-    GIT_REPOSITORY https://github.com/pybind/pybind11.git
-    GIT_TAG        v2.10.4 # or a later release
-    )
-
-    FetchContent_MakeAvailable(PyBind11)
-    file(GLOB_RECURSE pybind_src_files "python_binding/*.cpp")
-    pybind11_add_module(aidge_cpu MODULE ${pybind_src_files} "NO_EXTRAS")
-    target_include_directories(aidge_cpu PUBLIC ${pybind11_INCLUDE_DIRS} "python_binding")
-    target_link_libraries(aidge_cpu PUBLIC cpu)
-    # generate_python_binding(aidge_cpu cpu)
-endif()
-
-add_library(cpu STATIC)
-
-# Add include directories 
-target_include_directories(cpu PUBLIC "include")
-
-# Containers module
-file(GLOB_RECURSE src_files "src/*.cpp")
-target_sources(cpu PRIVATE ${src_files})
-
-target_link_libraries(cpu PUBLIC core)
-
-set_property(TARGET cpu PROPERTY POSITION_INDEPENDENT_CODE ON)
-
-if (PYBIND)
-    target_include_directories(cpu PUBLIC $<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS}>)
-    target_link_libraries(cpu PRIVATE ${PYTHON_LIBRARIES})
-endif()
-
-if (NOT BUILD_CPU_ALONE)
-    # Activate compile time reducer for aidge_core
-    set_target_properties(cpu PROPERTIES COTIRE_ADD_UNITY_BUILD FALSE)
-    # set_target_properties(n2d2_cpu_lib PROPERTIES COTIRE_CXX_PREFIX_HEADER_INIT "include/utils/Precompiled.hpp")
-    cotire(cpu)
-endif()
-
-if (TESTS)
-    add_subdirectory(tests)
-endif()
diff --git a/aidge_backend_cpu-config.cmake.in b/aidge_backend_cpu-config.cmake.in
new file mode 100644
index 00000000..f3604be1
--- /dev/null
+++ b/aidge_backend_cpu-config.cmake.in
@@ -0,0 +1,3 @@
+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 6ca1aeff..b6422bee 100644
--- a/cmake/PybindModuleCreation.cmake
+++ b/cmake/PybindModuleCreation.cmake
@@ -1,4 +1,4 @@
-function(generate_python_binding name) 
+function(generate_python_binding name target_to_bind) 
     if (PYBIND)
         add_definitions(-DPYBIND)
         Include(FetchContent)
@@ -12,8 +12,9 @@ function(generate_python_binding name)
         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 ${component})
+        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})
+        
     endif()
 endfunction()
diff --git a/cpu-config.cmake.in b/cpu-config.cmake.in
deleted file mode 100644
index 5d850786..00000000
--- a/cpu-config.cmake.in
+++ /dev/null
@@ -1,3 +0,0 @@
-include(${CMAKE_CURRENT_LIST_DIR}/aidge-cpu-config-version.cmake)
-
-include(${CMAKE_CURRENT_LIST_DIR}/aidge-cpu-targets.cmake)
diff --git a/python_binding/pybind_cpu.cpp b/python_binding/pybind_cpu.cpp
index 8321ab93..9e7c9714 100644
--- a/python_binding/pybind_cpu.cpp
+++ b/python_binding/pybind_cpu.cpp
@@ -10,7 +10,7 @@ void init_Aidge(py::module& /*m*/){
 
 }
 
-PYBIND11_MODULE(aidge_cpu, m) {
+PYBIND11_MODULE(aidge_backend_cpu, m) {
     init_Aidge(m);
 }
 }
diff --git a/unit_tests/CMakeLists.txt b/unit_tests/CMakeLists.txt
index 754191d3..62f99c1c 100644
--- a/unit_tests/CMakeLists.txt
+++ b/unit_tests/CMakeLists.txt
@@ -1,6 +1,4 @@
 
-enable_testing()
-
 Include(FetchContent)
 
 FetchContent_Declare(
@@ -12,14 +10,14 @@ FetchContent_Declare(
 FetchContent_MakeAvailable(Catch2)
 
 file(GLOB_RECURSE src_files "*.cpp")
+message(STATUS "TEST FILES : ${src_files}")
+add_executable(tests${module_name} ${src_files})
 
-add_executable(tests_cpu ${src_files})
-
-target_link_libraries(tests_cpu PUBLIC cpu)
+target_link_libraries(tests${module_name} PUBLIC ${module_name})
 
-target_link_libraries(tests_cpu PRIVATE Catch2::Catch2WithMain)
+target_link_libraries(tests${module_name} PRIVATE Catch2::Catch2WithMain)
 
 list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
 include(CTest)
 include(Catch)
-catch_discover_tests(tests_cpu)
+catch_discover_tests(tests${module_name})
-- 
GitLab