From 854ee75606a849649c6e1b7adfb8f4d091fc5b95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Kubler?= <gregoire.kubler@proton.me> Date: Tue, 21 May 2024 12:34:37 +0200 Subject: [PATCH] feat: homogeinized cmakelists w backend_cpu --- CMakeLists.txt | 13 ++++++++++- cmake/PybindModuleCreation.cmake | 37 +++++++++++++------------------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 27c40f2..3206c19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,8 +62,16 @@ target_include_directories(${module_name} # PYTHON BINDING if (PYBIND) + # Handles Python + pybind11 headers dependencies include(PybindModuleCreation) - generate_python_binding() + generate_python_binding(${CMAKE_PROJECT_NAME} ${module_name}) + + target_link_libraries(${module_name} + PUBLIC + pybind11::pybind11 + PRIVATE + Python::Module + ) endif() target_compile_features(${module_name} PRIVATE cxx_std_14) @@ -133,6 +141,9 @@ export(EXPORT ${project}-targets ############################################## ## 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.") + endif() enable_testing() add_subdirectory(unit_tests) endif() diff --git a/cmake/PybindModuleCreation.cmake b/cmake/PybindModuleCreation.cmake index 32f67e0..8f386be 100644 --- a/cmake/PybindModuleCreation.cmake +++ b/cmake/PybindModuleCreation.cmake @@ -1,32 +1,25 @@ -macro(generate_python_binding ) +function(generate_python_binding pybind_module_name target_to_bind) add_definitions(-DPYBIND) Include(FetchContent) - # Use the New FindPython mode, recommanded. Requires CMake 3.15+ - find_package(Python 3.7.0 - COMPONENTS Interpreter Development.Module - REQUIRED) - set(PYBIND11_FINDPYTHON ON) set(PYBIND_VERSION v2.10.4) + set(PYBIND11_FINDPYTHON ON) message(STATUS "Retrieving pybind ${PYBIND_VERSION} from git") + FetchContent_Declare( - PyBind11 - GIT_REPOSITORY https://github.com/pybind/pybind11.git - GIT_TAG ${PYBIND_VERSION} # or a later release + PyBind11 + GIT_REPOSITORY https://github.com/pybind/pybind11.git + 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 ${project}") + + message(STATUS "Creating binding for module ${pybind_module_name}") file(GLOB_RECURSE pybind_src_files "python_binding/*.cpp") - pybind11_add_module(${project} MODULE ${pybind_src_files} "NO_EXTRAS") # NO EXTRA required for pip install - target_include_directories(${project} PUBLIC "python_binding" ${pybind11_INCLUDE_DIRECTORIES}) - # Handles Python + pybind11 headers dependencies - target_link_libraries(${module_name} - PRIVATE - Python::Module - PUBLIC - pybind11::pybind11 - ) - target_link_libraries(${project} PUBLIC ${module_name}) -endmacro() + 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}) +endfunction() -- GitLab