build: Use new gen-purpose python cmake fns in py md proj
Significantly simplifies CMakeLists.txt
This commit is contained in:
1 parent
1237de2a60
commit
933444ffc7
1 file changed
+11
-150
@@ -1,158 +1,19 @@
|
||||
cmake_minimum_required(VERSION 3.23)
|
||||
# This lists file configures 3 primary targets:
|
||||
# 1) python_md -> libpython_md.so
|
||||
# * Normal c++ libs/exes (e.g., libspark, sparkd) can link this
|
||||
# 2) python_md_module -> python_md_module.cpython-310-x86_64-linux-gnu.so
|
||||
# * Exports a py module with the MdProvider base class
|
||||
# * Py md providers must import this and define a class that derives from 'python_md_module.MdProvider'
|
||||
# * Normal c++ libs/exes shouldn't link this
|
||||
# * For more info on the cpython api:
|
||||
# - https://docs.python.org/3/extending/building.html
|
||||
# - https://docs.python.org/3/c-api/module.html#c.PyModuleDef
|
||||
# 3) python_md_providers
|
||||
# * Copies py md provider plugins to a runtime dir where sparkd can find them
|
||||
# * Doesn't output a c++ lib or exe
|
||||
|
||||
project(python_md)
|
||||
include(GNUInstallDirs)
|
||||
include(${CMAKE_SOURCE_DIR}/cmake/InitLibrary.cmake)
|
||||
|
||||
if (NOT DEFINED PROJECT_VERSION)
|
||||
set(PROJECT_VERSION 1.0.0)
|
||||
endif()
|
||||
include(${CMAKE_SOURCE_DIR}/cmake/InitPythonModule.cmake)
|
||||
include(${CMAKE_SOURCE_DIR}/cmake/InitPythonPlugin.cmake)
|
||||
|
||||
# Find Python from the virtual environment
|
||||
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
|
||||
find_package(pybind11 REQUIRED)
|
||||
|
||||
# 1) python_md
|
||||
# Shared lib (for c++ to link)
|
||||
init_library(SHARED)
|
||||
target_link_libraries(
|
||||
${PROJECT_NAME}
|
||||
PUBLIC
|
||||
spark
|
||||
python_core
|
||||
)
|
||||
|
||||
target_include_directories(
|
||||
${PROJECT_NAME}
|
||||
PUBLIC
|
||||
${Python3_INCLUDE_DIRS}
|
||||
)
|
||||
target_link_directories(
|
||||
${PROJECT_NAME}
|
||||
PUBLIC
|
||||
${Python3_LIBRARY_DIRS}
|
||||
)
|
||||
target_link_libraries(
|
||||
${PROJECT_NAME}
|
||||
PUBLIC
|
||||
pybind11::embed
|
||||
${Python3_LIBRARIES}
|
||||
)
|
||||
|
||||
# 2) python_md_module
|
||||
set(module_proj ${PROJECT_NAME}_module)
|
||||
set(module_proj_SOURCES ${${PROJECT_NAME}_SOURCES})
|
||||
pybind11_add_module(${module_proj} SHARED ${module_proj_SOURCES})
|
||||
target_link_libraries(
|
||||
${module_proj}
|
||||
PUBLIC
|
||||
pybind11::embed
|
||||
spark
|
||||
python_core
|
||||
)
|
||||
target_include_directories(
|
||||
${module_proj}
|
||||
PRIVATE
|
||||
$<BUILD_INTERFACE:${pybind11_INCLUDE_DIRS}>
|
||||
)
|
||||
target_include_directories(
|
||||
${module_proj}
|
||||
PUBLIC
|
||||
${Python3_INCLUDE_DIRS}
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
||||
)
|
||||
add_custom_command(
|
||||
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/stubs/${PROJECT_NAME}.pyi
|
||||
DEPENDS ${PROJECT_NAME} ${module_proj}
|
||||
COMMAND PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR} pybind11-stubgen --numpy-array-use-type-var ${PROJECT_NAME}_module
|
||||
COMMENT "Generating py stubs"
|
||||
)
|
||||
add_custom_target(
|
||||
"${PROJECT_NAME}_py_stubs"
|
||||
ALL
|
||||
DEPENDS
|
||||
${CMAKE_CURRENT_BINARY_DIR}/stubs/${PROJECT_NAME}.pyi
|
||||
)
|
||||
install(
|
||||
TARGETS ${module_proj}
|
||||
EXPORT ${module_proj}Targets
|
||||
RUNTIME
|
||||
DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
LIBRARY
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
ARCHIVE
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
)
|
||||
foreach(dir ${pybind11_INCLUDE_DIRS})
|
||||
install(DIRECTORY ${dir} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
||||
endforeach()
|
||||
install(
|
||||
EXPORT ${module_proj}Targets
|
||||
FILE ${module_proj}Targets.cmake
|
||||
NAMESPACE ${PROJECT_NAME}::
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
|
||||
)
|
||||
|
||||
# 3) python_md_providers
|
||||
set(providers_proj ${PROJECT_NAME}_providers)
|
||||
file(
|
||||
GLOB_RECURSE providers
|
||||
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${providers_proj}/*.py)
|
||||
set(provider_outs)
|
||||
foreach(_provider ${providers})
|
||||
set(_provider_in ${CMAKE_CURRENT_SOURCE_DIR}/${_provider})
|
||||
set(_provider_out ${CMAKE_CURRENT_BINARY_DIR}/${_provider})
|
||||
add_custom_command(
|
||||
OUTPUT ${_provider_out}
|
||||
COMMAND ${CMAKE_COMMAND} -E copy ${_provider_in} ${_provider_out}
|
||||
DEPENDS ${_provider_in}
|
||||
COMMENT "Copying md provider plugin ${_provider} to ${_provider_out}"
|
||||
)
|
||||
list(APPEND provider_outs ${_provider_out})
|
||||
endforeach()
|
||||
|
||||
add_custom_target(copy_${providers_proj} ALL DEPENDS ${provider_outs})
|
||||
|
||||
add_library(${providers_proj} INTERFACE)
|
||||
add_library(${PROJECT_NAME}::${providers_proj} ALIAS ${providers_proj})
|
||||
|
||||
set_target_properties(
|
||||
${providers_proj}
|
||||
PROPERTIES
|
||||
INTERFACE_PYTHON_SCRIPTS_DIR
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/${providers_proj}>$<INSTALL_INTERFACE:${CMAKE_INSTALL_BINDIR}/${providers_proj}>
|
||||
)
|
||||
|
||||
set_target_properties(
|
||||
${providers_proj}
|
||||
PROPERTIES
|
||||
INTERFACE_PYTHON_SCRIPTS $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/${providers}>$<INSTALL_INTERFACE:${CMAKE_INSTALL_BINDIR}/${providers}>
|
||||
)
|
||||
|
||||
install(
|
||||
TARGETS ${providers_proj}
|
||||
EXPORT ${providers_proj}Targets
|
||||
DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
)
|
||||
|
||||
install(
|
||||
EXPORT ${providers_proj}Targets
|
||||
FILE ${providers_proj}Targets.cmake
|
||||
NAMESPACE ${PROJECT_NAME}::
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
|
||||
)
|
||||
install(DIRECTORY ${providers_proj} DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC ${Python3_INCLUDE_DIRS})
|
||||
target_link_directories(${PROJECT_NAME} PUBLIC ${Python3_LIBRARY_DIRS})
|
||||
target_link_libraries(${PROJECT_NAME} PUBLIC spark python_core pybind11::embed
|
||||
${Python3_LIBRARIES})
|
||||
# CPython module (for .py files to import)
|
||||
init_python_module()
|
||||
# Py plugin (for sparkd to load at runtime)
|
||||
init_python_plugin(PLUGIN_NAME python_md_providers)
|
||||
Reference in new issue
Block a user