-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_ctypes.cmake
32 lines (29 loc) · 1.49 KB
/
generate_ctypes.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# CMake function to generate Python ctypes bindings from C headers (using ctypeslib2)
# Requires ${PYTHON_COMMAND} to be set to an appropriate invocation of the python interpreter.
# E.g. if using CMake's FindPython, then ${Python_EXECUTABLE}, if using pipenv then "pipenv run python" or similar, etc.
set(CTYPESGEN2_DIR "${CMAKE_CURRENT_LIST_DIR}")
# Generate Python bindings from C header(s) and corresponding shared library.
# Requires ctypeslib2, llvm clang
function(ctypesgen2_generate_python_bindings INPUT_LIBRARY_TARGET INPUT_LIBRARY_FILE OUTPUT_PY INPUT_HEADERS INPUT_FLAGS)
add_custom_command(
OUTPUT ${OUTPUT_PY}
COMMAND ${PYTHON_COMMAND} ${CTYPESGEN2_DIR}/generate_ctypes.py
--libclang-directory ${CLANG_SHARED_OBJECT_DIRECTORY}
--libraries ${INPUT_LIBRARY_FILE}
--flags="${FLAGS}"
--headers ${INPUT_HEADERS}
--output ${OUTPUT_PY}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating Python ctypes bindings: ${OUTPUT_PY}"
DEPENDS ${INPUT_HEADERS} ${INPUT_LIBRARY_TARGET} ${CTYPESGEN2_DIR}/generate_ctypes.py
USES_TERMINAL
)
set_source_files_properties(${OUTPUT_PY} PROPERTIES GENERATED TRUE)
# With no executable or library targets depending on the bindings, we use the ALL target
add_custom_target(generate-${INPUT_LIBRARY_TARGET}-python ALL
DEPENDS
${OUTPUT_PY}
${INPUT_LIBRARY_TARGET}
${CTYPESGEN2_DIR}/generate_ctypes.py
)
endfunction()