diff --git a/CMakeLists.txt b/CMakeLists.txt index 27c40f2f1529c17455d29fdd720a2f3b1d815ce8..3206c1934f56ad803f265d27bbd534e3afdf22a6 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 32f67e0d969b39197b45392a2e013710c58e2171..8f386bef59ed86dfa366eca5d4fccae24b28d24e 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()