|
| 1 | +# This function is used to run pybind11-mkdoc for the headers of a pybind11 module. |
| 2 | +# In addition, this will also add target dependencies so the pybind11-mkdoc header |
| 3 | +# file is generated before the pybind11 module. Also, this will automatically add |
| 4 | +# the current binary directory to the pybind11 module's includes, so it can |
| 5 | +# easily be included when compiling the module. |
| 6 | +# |
| 7 | +# The required parameters are: |
| 8 | +# * OUTPUT - The name of the output file. |
| 9 | +# * PYBIND11_MODULE - The pybind11 module target that these docs will be used for. |
| 10 | +# * HEADERS - The header files to create docs for. These can be absoulte paths or relative to the |
| 11 | +# current source directory. |
| 12 | +# |
| 13 | +# The optional parameters are: |
| 14 | +# * EXTRA_ARGS - This string argument will be added verbatim to the pybind11-mkdoc command. |
| 15 | +# |
| 16 | +# Example usage: |
| 17 | +# pybind11_add_module(my_pybind11_module src/my_pybind11_module.cc) |
| 18 | +# pybind11_mkdoc( |
| 19 | +# OUTPUT my_pybind11_module_doc.h |
| 20 | +# PYBIND11_MODULE my_pybind11_module |
| 21 | +# HEADERS |
| 22 | +# include/my_header_1.h |
| 23 | +# /absolute/path/to/header.h |
| 24 | +# ) |
| 25 | +function (pybind11_mkdoc) |
| 26 | + set(options) |
| 27 | + set(oneValueArgs OUTPUT PYBIND11_MODULE EXTRA_ARGS) |
| 28 | + set(multiValueArgs HEADERS) |
| 29 | + cmake_parse_arguments(PARSE_ARGV 0 arg |
| 30 | + "${options}" "${oneValueArgs}" "${multiValueArgs}" |
| 31 | + ) |
| 32 | + |
| 33 | + # Include directories for the pybind11 module |
| 34 | + set(prop "$<TARGET_PROPERTY:${arg_PYBIND11_MODULE},INCLUDE_DIRECTORIES>") |
| 35 | + |
| 36 | + # Remove the header file from the list |
| 37 | + set(HEADERS "") |
| 38 | + # Run through all the other arguments. |
| 39 | + foreach(header ${arg_HEADERS}) |
| 40 | + if(IS_ABSOLUTE ${header}) |
| 41 | + # If it is an absolute path, then add it as is. |
| 42 | + list(APPEND HEADERS ${header}) |
| 43 | + else() |
| 44 | + # Otherwise, assume it is relative to the current source directory. |
| 45 | + list(APPEND HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/${header}") |
| 46 | + endif() |
| 47 | + endforeach() |
| 48 | + |
| 49 | + # Add a custom target and command for the full header file location that runs pybind11-mkdoc |
| 50 | + # We automatically include the source directory and build/include |
| 51 | + add_custom_target( |
| 52 | + pybind11_mkdoc_${arg_OUTPUT} |
| 53 | + DEPENDS ${arg_OUTPUT} |
| 54 | + ) |
| 55 | + |
| 56 | + add_custom_command( |
| 57 | + OUTPUT ${arg_OUTPUT} |
| 58 | + COMMAND ${Python_EXECUTABLE} -m pybind11_mkdoc ${arg_EXTRA_ARGS} -o ${arg_OUTPUT} "$<$<BOOL:${prop}>:-I$<JOIN:${prop},;-I>>" ${HEADERS} |
| 59 | + DEPENDS ${HEADERS} |
| 60 | + COMMAND_EXPAND_LISTS |
| 61 | + ) |
| 62 | + |
| 63 | + # Add a dependency so that the pybind11-mkdoc command runs before we try to compile the pybind11 module |
| 64 | + add_dependencies(${arg_PYBIND11_MODULE} pybind11_mkdoc_${arg_OUTPUT}) |
| 65 | + |
| 66 | + # Add the current binary directory to the pybind11 module so it can be included easily |
| 67 | + target_include_directories(${arg_PYBIND11_MODULE} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) |
| 68 | +endfunction() |
0 commit comments