Skip to content

Commit

Permalink
cmake: Improve Python native modules compilation configuration
Browse files Browse the repository at this point in the history
Use PYTHON3_ADD_LIBRARY cmake command to better configure the compilation
of sip and talipot native Python modules.
  • Loading branch information
anlambert committed Dec 29, 2024
1 parent b6d098e commit a7947b7
Showing 6 changed files with 33 additions and 56 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -158,7 +158,7 @@ SET(LibTalipotOGLName "talipot-ogl-${TalipotMMVersion}")
SET(LibTalipotGUIName "talipot-gui-${TalipotMMVersion}")
SET(LibTalipotPythonName "talipot-python-${TalipotMMVersion}")
SET(LibTalipotOGDFName "talipot-ogdf-${TalipotMMVersion}")
SET(LibTalipotCorePythonBindingsName "talipot-core-python-bindings")
SET(TalipotPythonModuleName "talipot-python")
SET(TalipotLibs ${LibTalipotCoreName} ${LibTalipotOGLName} ${LibTalipotGUIName}
${LibTalipotOGDFName})

5 changes: 1 addition & 4 deletions bundlers/linux/talipot_python_wheels_manylinux_build.sh
Original file line number Diff line number Diff line change
@@ -8,10 +8,6 @@ yum -y install epel-release ccache
# install talipot-core wheel deps
yum -y install zlib-devel libzstd-devel yajl-devel qhull-devel graphviz-devel libgit2-devel

# required to build upstream python cffi from pip
yum -y install libffi-devel


JSON=$(curl -s 'https://test.pypi.org/pypi/talipot/json')
LAST_VERSION=$(echo $JSON | /opt/python/cp311-cp311/bin/python3 -c "
import sys, json
@@ -74,6 +70,7 @@ do
then
continue
fi
rm -rf *
${CPYBIN}/pip install twine sip
# configure and build python wheel with specific Python version
cmake ${TALIPOT_SRC} \
2 changes: 1 addition & 1 deletion cmake/TalipotPython.cmake
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
# thirdparty.

IF(LINUX AND TALIPOT_ACTIVATE_PYTHON_WHEEL_TARGET)
SET(PYTHON_COMPONENTS Interpreter)
SET(PYTHON_COMPONENTS Interpreter Development.Module)
ELSE(LINUX AND TALIPOT_ACTIVATE_PYTHON_WHEEL_TARGET)
SET(PYTHON_COMPONENTS Interpreter Development)
ENDIF(LINUX AND TALIPOT_ACTIVATE_PYTHON_WHEEL_TARGET)
2 changes: 1 addition & 1 deletion doc/python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ ADD_CUSTOM_TARGET(
-DCMAKE_LIBRARY_PATH=${CMAKE_LIBRARY_PATH_STR}
-P
${CMAKE_CURRENT_SOURCE_DIR}/genSphinxDoc.cmake
DEPENDS ${LibTalipotCorePythonBindingsName} ${TALIPOT_PLUGIN_TARGETS}
DEPENDS ${TalipotPythonModuleName} ${TALIPOT_PLUGIN_TARGETS}
copyTalipotPythonPlugins
VERBATIM)

76 changes: 28 additions & 48 deletions library/talipot-python/bindings/talipot-core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -134,7 +134,7 @@ INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_DIR} ${PYTHON_INCLUDE_PATH})
INCLUDE_DIRECTORIES(${TalipotCoreBuildInclude} ${TalipotCoreInclude}
${TalipotPythonInclude})

ADD_LIBRARY(sip SHARED ${SIP_MODULE_SRC})
PYTHON3_ADD_LIBRARY(sip MODULE WITH_SOABI ${SIP_MODULE_SRC})

SET_TARGET_PROPERTIES(sip PROPERTIES LIBRARY_OUTPUT_DIRECTORY
${TALIPOT_PYTHON_NATIVE_FOLDER})
@@ -153,36 +153,36 @@ SET(TALIPOT_PYTHON_BINDINGS_SRC
${TALIPOT_PYTHON_BINDINGS_SRC}
${CMAKE_CURRENT_SOURCE_DIR}/../../src/PythonCppTypesConverter.cpp)

ADD_LIBRARY(${LibTalipotCorePythonBindingsName} SHARED
${TALIPOT_PYTHON_BINDINGS_SRC})
PYTHON3_ADD_LIBRARY(${TalipotPythonModuleName} MODULE WITH_SOABI
${TALIPOT_PYTHON_BINDINGS_SRC})

SET_TARGET_PROPERTIES(${TalipotPythonModuleName} PROPERTIES OUTPUT_NAME talipot
PREFIX "")

SET_TARGET_PROPERTIES(
${LibTalipotCorePythonBindingsName}
PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${TALIPOT_PYTHON_NATIVE_FOLDER})
${TalipotPythonModuleName} PROPERTIES LIBRARY_OUTPUT_DIRECTORY
${TALIPOT_PYTHON_NATIVE_FOLDER})
SET_TARGET_PROPERTIES(
${LibTalipotCorePythonBindingsName}
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${TALIPOT_PYTHON_NATIVE_FOLDER})
${TalipotPythonModuleName} PROPERTIES RUNTIME_OUTPUT_DIRECTORY
${TALIPOT_PYTHON_NATIVE_FOLDER})
SET_TARGET_PROPERTIES(
${LibTalipotCorePythonBindingsName}
PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${TALIPOT_PYTHON_NATIVE_FOLDER})
${TalipotPythonModuleName} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE
${TALIPOT_PYTHON_NATIVE_FOLDER})
SET_TARGET_PROPERTIES(
${LibTalipotCorePythonBindingsName}
PROPERTIES RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL
${TALIPOT_PYTHON_NATIVE_FOLDER})
${TalipotPythonModuleName} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL
${TALIPOT_PYTHON_NATIVE_FOLDER})
SET_TARGET_PROPERTIES(
${LibTalipotCorePythonBindingsName}
PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO
${TALIPOT_PYTHON_NATIVE_FOLDER})
${TalipotPythonModuleName} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO
${TALIPOT_PYTHON_NATIVE_FOLDER})
SET_TARGET_PROPERTIES(
${LibTalipotCorePythonBindingsName}
PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${TALIPOT_PYTHON_NATIVE_FOLDER})
${TalipotPythonModuleName} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG
${TALIPOT_PYTHON_NATIVE_FOLDER})

# Ensure bindings build output folder exists
ADD_CUSTOM_TARGET(
create-talipot-python-native-folder ALL
COMMAND ${CMAKE_COMMAND} -E make_directory ${TALIPOT_PYTHON_NATIVE_FOLDER})
ADD_DEPENDENCIES(${LibTalipotCorePythonBindingsName}
create-talipot-python-native-folder)
ADD_DEPENDENCIES(${TalipotPythonModuleName} create-talipot-python-native-folder)

ADD_CUSTOM_TARGET(
copyTalipotInitPy ALL
@@ -223,20 +223,6 @@ IF(TALIPOT_ACTIVATE_PYTHON_WHEEL_TARGET)
ENDIF(TALIPOT_GENERATE_TESTPYPI_WHEEL)
ENDIF(TALIPOT_ACTIVATE_PYTHON_WHEEL_TARGET)

SET_TARGET_PROPERTIES(${LibTalipotCorePythonBindingsName}
PROPERTIES OUTPUT_NAME talipot PREFIX "")
SET_TARGET_PROPERTIES(sip PROPERTIES OUTPUT_NAME sip PREFIX "")

IF(WIN32)
SET_TARGET_PROPERTIES(${LibTalipotCorePythonBindingsName} PROPERTIES SUFFIX
".pyd")
SET_TARGET_PROPERTIES(sip PROPERTIES SUFFIX ".pyd")
ELSE(WIN32)
SET_TARGET_PROPERTIES(${LibTalipotCorePythonBindingsName} PROPERTIES SUFFIX
".so")
SET_TARGET_PROPERTIES(sip PROPERTIES SUFFIX ".so")
ENDIF(WIN32)

# On MacOS, add the paths of dependencies dylibs in install rpaths of the
# talipot.so binary That way, the talipot module can be imported in a classical
# Python shell without having to modify the content of the DYLD_LIBRARY_PATH
@@ -246,10 +232,10 @@ FILE(RELATIVE_PATH TalipotLibsInstallRelPath
${CMAKE_INSTALL_PREFIX}/${TalipotLibInstallDir})

IF(APPLE AND NOT TALIPOT_ACTIVATE_PYTHON_WHEEL_TARGET)
GET_TARGET_PROPERTY(CURRENT_INSTALL_RPATHS
${LibTalipotCorePythonBindingsName} INSTALL_RPATH)
GET_TARGET_PROPERTY(CURRENT_INSTALL_RPATHS ${TalipotPythonModuleName}
INSTALL_RPATH)
SET_TARGET_PROPERTIES(
${LibTalipotCorePythonBindingsName}
${TalipotPythonModuleName}
PROPERTIES
INSTALL_RPATH
"${CURRENT_INSTALL_RPATHS};@loader_path/${TalipotLibsInstallRelPath};@loader_path/../../../../Frameworks"
@@ -261,10 +247,10 @@ IF(APPLE AND NOT TALIPOT_ACTIVATE_PYTHON_WHEEL_TARGET)
"${CURRENT_INSTALL_RPATHS};@loader_path/${TalipotLibsInstallRelPath};@loader_path/../../../../Frameworks"
)
ELSEIF(LINUX AND NOT TALIPOT_ACTIVATE_PYTHON_WHEEL_TARGET)
GET_TARGET_PROPERTY(CURRENT_INSTALL_RPATHS
${LibTalipotCorePythonBindingsName} INSTALL_RPATH)
GET_TARGET_PROPERTY(CURRENT_INSTALL_RPATHS ${TalipotPythonModuleName}
INSTALL_RPATH)
SET_TARGET_PROPERTIES(
${LibTalipotCorePythonBindingsName}
${TalipotPythonModuleName}
PROPERTIES INSTALL_RPATH
"${CURRENT_INSTALL_RPATHS}:$ORIGIN/${TalipotLibsInstallRelPath}")
SET_TARGET_PROPERTIES(
@@ -273,27 +259,21 @@ ELSEIF(LINUX AND NOT TALIPOT_ACTIVATE_PYTHON_WHEEL_TARGET)
"${CURRENT_INSTALL_RPATHS}:$ORIGIN/${TalipotLibsInstallRelPath}")
ENDIF(APPLE AND NOT TALIPOT_ACTIVATE_PYTHON_WHEEL_TARGET)

TARGET_LINK_LIBRARIES(${LibTalipotCorePythonBindingsName} ${LibTalipotCoreName})
TARGET_LINK_LIBRARIES(${TalipotPythonModuleName} PUBLIC ${LibTalipotCoreName})

# When building Python wheel for MacOS, don't link the C extension module with
# the Python library and use dynamic lookup for retrieving its symbols. That
# way, we can produce a C extension module that can be imported through the
# Python interpreter provided by Apple with the System and the one provided by
# Python.org
IF(APPLE AND TALIPOT_ACTIVATE_PYTHON_WHEEL_TARGET)
SET_TARGET_PROPERTIES(${LibTalipotCorePythonBindingsName}
SET_TARGET_PROPERTIES(${TalipotPythonModuleName}
PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
SET_TARGET_PROPERTIES(sip PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
ELSE(APPLE AND TALIPOT_ACTIVATE_PYTHON_WHEEL_TARGET)
IF(NOT LINUX OR NOT TALIPOT_ACTIVATE_PYTHON_WHEEL_TARGET)
TARGET_LINK_LIBRARIES(${LibTalipotCorePythonBindingsName} ${PYTHON_LIBRARY})
TARGET_LINK_LIBRARIES(sip ${PYTHON_LIBRARY})
ENDIF(NOT LINUX OR NOT TALIPOT_ACTIVATE_PYTHON_WHEEL_TARGET)
ENDIF(APPLE AND TALIPOT_ACTIVATE_PYTHON_WHEEL_TARGET)

TALIPOT_INSTALL_PYTHON_FILES(talipot ${CMAKE_CURRENT_SOURCE_DIR}/__init__.py)
TALIPOT_INSTALL_PYTHON_FILES(talipot/native sip
${LibTalipotCorePythonBindingsName})
TALIPOT_INSTALL_PYTHON_FILES(talipot/native sip ${TalipotPythonModuleName})

INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/build/talipot.api
DESTINATION ${TalipotShareInstallDir}/api)
2 changes: 1 addition & 1 deletion library/talipot-python/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -70,7 +70,7 @@ ADD_LIBRARY(
${${LibTalipotPythonName}_LIB_SRCS} ${${LibTalipotPythonName}_MOC_SRCS}
${${LibTalipotPythonName}_UI_SRCS})

ADD_DEPENDENCIES(${LibTalipotPythonName} ${LibTalipotCorePythonBindingsName})
ADD_DEPENDENCIES(${LibTalipotPythonName} ${TalipotPythonModuleName})

TARGET_LINK_LIBRARIES(${LibTalipotPythonName} ${LibTalipotCoreName})
TARGET_LINK_LIBRARIES(${LibTalipotPythonName} ${LibTalipotGUIName})

0 comments on commit a7947b7

Please sign in to comment.