From 247eb61fd0b3423089bc2e432113b02df123496d Mon Sep 17 00:00:00 2001
From: Vincent TEMPLIER <vincent.templier@cea.fr>
Date: Wed, 26 Jul 2023 13:47:44 +0000
Subject: [PATCH] Add BUILD_CPU_ALONE mode to CPU library

---
 aidge/_CPU/CMakeLists.txt | 36 +++++++++++++++++++++++++++++-------
 aidge/_CPU/Makefile       | 36 ++++++++++++++++++++++++++++++++++++
 aidge/_CPU/README.md      | 38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 103 insertions(+), 7 deletions(-)
 create mode 100644 aidge/_CPU/Makefile
 create mode 100644 aidge/_CPU/README.md

diff --git a/aidge/_CPU/CMakeLists.txt b/aidge/_CPU/CMakeLists.txt
index c6a8d775..60be107c 100644
--- a/aidge/_CPU/CMakeLists.txt
+++ b/aidge/_CPU/CMakeLists.txt
@@ -1,8 +1,29 @@
 
-# project(Aidge_Core)
+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)
-    generate_python_binding(aidge_cpu cpu)
+    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)
@@ -23,8 +44,9 @@ if (PYBIND)
     target_link_libraries(cpu PRIVATE ${PYTHON_LIBRARIES})
 endif()
 
-
-# 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)
\ No newline at end of file
+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()
\ No newline at end of file
diff --git a/aidge/_CPU/Makefile b/aidge/_CPU/Makefile
new file mode 100644
index 00000000..8aed5f74
--- /dev/null
+++ b/aidge/_CPU/Makefile
@@ -0,0 +1,36 @@
+# This makefile does nothing but delegating the actual building to cmake
+BUILDDIR := build
+MAKEFLAGS := --no-print-directory
+
+all: cpu_with_pybind
+
+cpu_only:
+	mkdir -p ${BUILDDIR}; \
+	cd ${BUILDDIR}; \
+	cmake -DBUILD_CPU_ALONE=ON -DCMAKE_BUILD_TYPE=Release -DPYBIND=OFF -DTESTS=OFF ..; \
+	${MAKE} ${MAKEFLAGS};
+
+cpu_tests:
+	mkdir -p ${BUILDDIR}; \
+	cd ${BUILDDIR}; \
+	cmake -DBUILD_CPU_ALONE=ON -DCMAKE_BUILD_TYPE=Debug -DPYBIND=OFF -DTESTS=ON ..; \
+	${MAKE} ${MAKEFLAGS}; \
+	cd tests; \
+	ctest --output-on-failure || true;
+
+cpu_with_pybind:
+	mkdir -p ${BUILDDIR}; \
+	cd ${BUILDDIR}; \
+	cmake -DBUILD_CPU_ALONE=ON -DCMAKE_BUILD_TYPE=Release -DPYBIND=ON -DTESTS=OFF ..; \
+	${MAKE} ${MAKEFLAGS};
+
+cpu_with_pybind_tests:
+	mkdir -p ${BUILDDIR}; \
+	cd ${BUILDDIR}; \
+	cmake -DBUILD_CPU_ALONE=ON -DCMAKE_BUILD_TYPE=Debug -DPYBIND=ON -DTESTS=ON ..; \
+	${MAKE} ${MAKEFLAGS}; \
+	cd tests; \
+	ctest --output-on-failure || true;
+
+clean:
+	if [ -d "${BUILDDIR}" ]; then rm -rf ${BUILDDIR}; fi
\ No newline at end of file
diff --git a/aidge/_CPU/README.md b/aidge/_CPU/README.md
new file mode 100644
index 00000000..a4eb6a15
--- /dev/null
+++ b/aidge/_CPU/README.md
@@ -0,0 +1,38 @@
+# Aidge CPU library
+
+You can find in this folder the library that implements the CPU operators. <br>
+Those operators can be used on any machine with an Linux OS.
+
+So far be sure to have the correct requirements to use this library
+- GCC
+- Make
+- CMake
+- Python (optional, if you have no intend to use this library in python with pybind)
+
+
+## Compilation
+
+You will need to compile first the Core library before compiling the CPU one.
+The makefile is designed to do it for you.
+
+To only compile the CPU library, run
+```
+make cpu_only
+```
+
+To compile the CPU library + the associated unitary tests, run
+```
+make cpu_tests
+```
+
+To compile the CPU library with the python binding, run
+```
+make cpu_with_pybind
+```
+Important: this command can also be run with `make`.
+
+
+To compile the CPU library with the python binding + the associated unitary tests, run
+```
+make cpu_with_pybind_tests
+```
\ No newline at end of file
-- 
GitLab