Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CMake #23

Open
tdegeus opened this issue Sep 20, 2021 · 2 comments
Open

Use CMake #23

tdegeus opened this issue Sep 20, 2021 · 2 comments

Comments

@tdegeus
Copy link

tdegeus commented Sep 20, 2021

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?

@cluttrdev
Copy link
Contributor

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?

@aloisklink
Copy link

aloisklink commented Jul 18, 2023

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)!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants