Skip to content
Snippets Groups Projects
PybindModuleCreation.cmake 804 B
Newer Older
function(generate_python_binding name target_to_bind) 
Olivier BICHLER's avatar
Olivier BICHLER committed
    add_definitions(-DPYBIND)
    Include(FetchContent)
Olivier BICHLER's avatar
Olivier BICHLER committed
    FetchContent_Declare(
    PyBind11
    GIT_REPOSITORY https://github.com/pybind/pybind11.git
    GIT_TAG        v2.10.4 # or a later release
    )
Olivier BICHLER's avatar
Olivier BICHLER committed
    # Use the New FindPython mode, recommanded. Requires CMake 3.15+
    find_package(Python COMPONENTS Interpreter Development)
    FetchContent_MakeAvailable(PyBind11)
Olivier BICHLER's avatar
Olivier BICHLER committed
    message(STATUS "Creating binding for module ${name}")
    file(GLOB_RECURSE pybind_src_files "python_binding/*.cpp")
Olivier BICHLER's avatar
Olivier BICHLER committed
    pybind11_add_module(${name} MODULE ${pybind_src_files} "NO_EXTRAS") # NO EXTRA recquired for pip install
    target_include_directories(${name} PUBLIC "python_binding")
    target_link_libraries(${name} PUBLIC ${target_to_bind})
endfunction()