pybind / pybind11_mkdoc

Pybind11 tool for making docstrings from C++ comments

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use CMake

tdegeus opened this issue · comments

I have a library that depends on a number of different libraries. I use CMake to find them, and I would like pybind11_mkdoc to do the same. How do I do that?

Could you be more specific? From my perspective, the way you phrased it its not clear whether you want cmake to find pybind11_mkdoc or pybind11_mkdoc to find libraries?

If you're asking asking how to use CMake to pass all of the INCLUDE_DIRECTORIES of their library to pybind11_mkdoc (and run pybind11_mkdoc automatically), my solution is like follows:

find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
find_package(pybind11 CONFIG REQUIRED)

add_custom_command(
  OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/auto-generated/docstrings.h"
  COMMAND Python::Interpreter
  ARGS
    -m pybind11_mkdoc
    --output auto-generated/docstrings.h
    # TODO: there's a better way of doing this in CMake 3.27
    # see https://cmake.org/cmake/help/v3.27/manual/cmake-generator-expressions.7.html#whitespace-and-quoting
    "-I;$<JOIN:$<TARGET_PROPERTY:my_module,INCLUDE_DIRECTORIES>,;-I;>"
    "${CMAKE_CURRENT_SOURCE_DIR}/src/my_module_headers.hpp"
  COMMAND_EXPAND_LISTS
  VERBATIM
)

python_add_library(my_module MODULE
  src/my_module.cpp auto-generated/docstrings.h
)
target_link_libraries(my_module PRIVATE pybind11::headers)

The key-bit is that we can use $<TARGET_PROPERTY:my_module,INCLUDE_DIRECTORIES> to get a list of all of the include directories for my_module.

This is pretty great if you're using scikit-build-core, as you can then run pybind11_mkdoc automatically whenever somebody installs your package (although it might have to wait until #32 gets merged)!