| 1 |
# Macros for SIP |
| 2 |
# ~~~~~~~~~~~~~~ |
| 3 |
# SPDX-FileCopyrightText: 2007 Simon Edwards <[email protected]> |
| 4 |
# |
| 5 |
# SPDX-License-Identifier: BSD-3-Clause |
| 6 |
# |
| 7 |
# SIP website: http://www.riverbankcomputing.co.uk/sip/index.php |
| 8 |
# |
| 9 |
# This file defines the following macros: |
| 10 |
# |
| 11 |
# ADD_SIP_PYTHON_MODULE (MODULE_NAME MODULE_SIP [library1, library2, ...]) |
| 12 |
# Specifies a SIP file to be built into a Python module and installed. |
| 13 |
# MODULE_NAME is the name of Python module including any path name. (e.g. |
| 14 |
# os.sys, Foo.bar etc). MODULE_SIP the path and filename of the .sip file |
| 15 |
# to process and compile. libraryN are libraries that the Python module, |
| 16 |
# which is typically a shared library, should be linked to. The built |
| 17 |
# module will also be install into Python's site-packages directory. |
| 18 |
# |
| 19 |
# The behaviour of the ADD_SIP_PYTHON_MODULE macro can be controlled by a |
| 20 |
# number of variables: |
| 21 |
# |
| 22 |
# SIP_INCLUDES - List of directories which SIP will scan through when looking |
| 23 |
# for included .sip files. (Corresponds to the -I option for SIP.) |
| 24 |
# |
| 25 |
# SIP_TAGS - List of tags to define when running SIP. (Corresponds to the -t |
| 26 |
# option for SIP.) |
| 27 |
# |
| 28 |
# SIP_CONCAT_PARTS - An integer which defines the number of parts the C++ code |
| 29 |
# of each module should be split into. Defaults to 8. (Corresponds to the |
| 30 |
# -j option for SIP.) |
| 31 |
# |
| 32 |
# SIP_DISABLE_FEATURES - List of feature names which should be disabled |
| 33 |
# running SIP. (Corresponds to the -x option for SIP.) |
| 34 |
# |
| 35 |
# SIP_EXTRA_OPTIONS - Extra command line options which should be passed on to |
| 36 |
# SIP. |
| 37 |
|
| 38 |
SET(SIP_INCLUDES) |
| 39 |
SET(SIP_TAGS) |
| 40 |
SET(SIP_CONCAT_PARTS 8) |
| 41 |
SET(SIP_DISABLE_FEATURES) |
| 42 |
SET(SIP_EXTRA_OPTIONS) |
| 43 |
|
| 44 |
if (${SIP_VERSION_STR} VERSION_LESS 5) |
| 45 |
|
| 46 |
MACRO(ADD_SIP_PYTHON_MODULE MODULE_NAME MODULE_SIP) |
| 47 |
|
| 48 |
SET(EXTRA_LINK_LIBRARIES ${ARGN}) |
| 49 |
|
| 50 |
STRING(REPLACE "." "/" _x ${MODULE_NAME}) |
| 51 |
GET_FILENAME_COMPONENT(_parent_module_path ${_x} PATH) |
| 52 |
GET_FILENAME_COMPONENT(_child_module_name ${_x} NAME) |
| 53 |
|
| 54 |
GET_FILENAME_COMPONENT(_module_path ${MODULE_SIP} PATH) |
| 55 |
|
| 56 |
if(_module_path STREQUAL "") |
| 57 |
set(CMAKE_CURRENT_SIP_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}") |
| 58 |
else(_module_path STREQUAL "") |
| 59 |
set(CMAKE_CURRENT_SIP_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/${_module_path}") |
| 60 |
endif(_module_path STREQUAL "") |
| 61 |
|
| 62 |
GET_FILENAME_COMPONENT(_abs_module_sip ${MODULE_SIP} ABSOLUTE) |
| 63 |
|
| 64 |
# We give this target a long logical target name. |
| 65 |
# (This is to avoid having the library name clash with any already |
| 66 |
# install library names. If that happens then cmake dependency |
| 67 |
# tracking get confused.) |
| 68 |
STRING(REPLACE "." "_" _logical_name ${MODULE_NAME}) |
| 69 |
SET(_logical_name "python_module_${_logical_name}") |
| 70 |
|
| 71 |
FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_SIP_OUTPUT_DIR}) # Output goes in this dir. |
| 72 |
|
| 73 |
SET(_sip_includes) |
| 74 |
FOREACH (_inc ${SIP_INCLUDES}) |
| 75 |
GET_FILENAME_COMPONENT(_abs_inc ${_inc} ABSOLUTE) |
| 76 |
LIST(APPEND _sip_includes -I ${_abs_inc}) |
| 77 |
ENDFOREACH (_inc ) |
| 78 |
|
| 79 |
SET(_sip_tags) |
| 80 |
FOREACH (_tag ${SIP_TAGS}) |
| 81 |
LIST(APPEND _sip_tags -t ${_tag}) |
| 82 |
ENDFOREACH (_tag) |
| 83 |
|
| 84 |
SET(_sip_x) |
| 85 |
FOREACH (_x ${SIP_DISABLE_FEATURES}) |
| 86 |
LIST(APPEND _sip_x -x ${_x}) |
| 87 |
ENDFOREACH (_x ${SIP_DISABLE_FEATURES}) |
| 88 |
|
| 89 |
SET(_message "-DMESSAGE=Generating CPP code for module ${MODULE_NAME}") |
| 90 |
SET(_sip_output_files) |
| 91 |
FOREACH(CONCAT_NUM RANGE 0 ${SIP_CONCAT_PARTS} ) |
| 92 |
IF( ${CONCAT_NUM} LESS ${SIP_CONCAT_PARTS} ) |
| 93 |
SET(_sip_output_files ${_sip_output_files} ${CMAKE_CURRENT_SIP_OUTPUT_DIR}/sip${_child_module_name}part${CONCAT_NUM}.cpp ) |
| 94 |
ENDIF( ${CONCAT_NUM} LESS ${SIP_CONCAT_PARTS} ) |
| 95 |
ENDFOREACH(CONCAT_NUM RANGE 0 ${SIP_CONCAT_PARTS} ) |
| 96 |
|
| 97 |
ADD_CUSTOM_COMMAND( |
| 98 |
OUTPUT ${_sip_output_files} |
| 99 |
COMMAND ${CMAKE_COMMAND} -E echo ${message} |
| 100 |
COMMAND ${CMAKE_COMMAND} -E touch ${_sip_output_files} |
| 101 |
COMMAND ${SIP_EXECUTABLE} ${_sip_tags} ${_sip_x} ${SIP_EXTRA_OPTIONS} -j ${SIP_CONCAT_PARTS} -c ${CMAKE_CURRENT_SIP_OUTPUT_DIR} ${_sip_includes} ${_abs_module_sip} |
| 102 |
DEPENDS ${_abs_module_sip} ${SIP_EXTRA_FILES_DEPEND} |
| 103 |
) |
| 104 |
# not sure if type MODULE could be uses anywhere, limit to cygwin for now |
| 105 |
IF (WIN32 OR CYGWIN OR APPLE) |
| 106 |
ADD_LIBRARY(${_logical_name} MODULE ${_sip_output_files} ) |
| 107 |
ELSE (WIN32 OR CYGWIN OR APPLE) |
| 108 |
ADD_LIBRARY(${_logical_name} SHARED ${_sip_output_files} ) |
| 109 |
ENDIF (WIN32 OR CYGWIN OR APPLE) |
| 110 |
TARGET_LINK_LIBRARIES(${_logical_name} ${PYTHON_LIBRARY}) |
| 111 |
TARGET_LINK_LIBRARIES(${_logical_name} ${EXTRA_LINK_LIBRARIES}) |
| 112 |
SET_TARGET_PROPERTIES(${_logical_name} PROPERTIES PREFIX "" OUTPUT_NAME ${_child_module_name}) |
| 113 |
|
| 114 |
IF (MINGW) |
| 115 |
TARGET_COMPILE_DEFINITIONS(${_logical_name} PRIVATE _hypot=hypot) |
| 116 |
ENDIF (MINGW) |
| 117 |
|
| 118 |
if (MSVC) |
| 119 |
SET_TARGET_PROPERTIES(${_logical_name} PROPERTIES PDB_NAME "sip_v4_${_logical_name}") |
| 120 |
ENDIF (MSVC) |
| 121 |
|
| 122 |
IF (WIN32) |
| 123 |
SET_TARGET_PROPERTIES(${_logical_name} PROPERTIES SUFFIX ".pyd") |
| 124 |
ENDIF (WIN32) |
| 125 |
|
| 126 |
INSTALL(TARGETS ${_logical_name} DESTINATION "${PYTHON_SITE_PACKAGES_INSTALL_DIR}/${_parent_module_path}") |
| 127 |
if (MSVC) |
| 128 |
INSTALL( |
| 129 |
FILES $<TARGET_PDB_FILE:${_logical_name}> |
| 130 |
DESTINATION "${PYTHON_SITE_PACKAGES_INSTALL_DIR}/${_parent_module_path}" |
| 131 |
OPTIONAL |
| 132 |
) |
| 133 |
endif() |
| 134 |
|
| 135 |
ENDMACRO(ADD_SIP_PYTHON_MODULE) |
| 136 |
|
| 137 |
else() |
| 138 |
find_file(join_files "join-files.py" PATHS ${CMAKE_MODULE_PATH} NO_CMAKE_FIND_ROOT_PATH) # ugh |
| 139 |
find_file(sip_generate "sip-generate.py" PATHS ${CMAKE_MODULE_PATH} NO_CMAKE_FIND_ROOT_PATH) |
| 140 |
find_file(pyproject_toml "pyproject.toml.in" PATHS ${CMAKE_MODULE_PATH} NO_CMAKE_FIND_ROOT_PATH) |
| 141 |
|
| 142 |
macro(add_sip_python_module_v5 MODULE_NAME MODULE_SIP) |
| 143 |
get_filename_component(module_name_toml ${MODULE_SIP} NAME_WE) |
| 144 |
set(module_srcs "${SIP_EXTRA_FILES_DEPEND}") |
| 145 |
|
| 146 |
set(EXTRA_LINK_LIBRARIES ${ARGN}) |
| 147 |
|
| 148 |
if (SIP_MODULE) |
| 149 |
set(sip_name ${SIP_MODULE}) |
| 150 |
else() |
| 151 |
set(sip_name "PyQt${QT_MAJOR_VERSION}.sip") |
| 152 |
endif() |
| 153 |
|
| 154 |
if (QT_MAJOR_VERSION STREQUAL "6") |
| 155 |
set(abi_version "13.0") |
| 156 |
set(sip_disabled_features "[\"Krita_Qt5\"]") |
| 157 |
else() |
| 158 |
set(abi_version "12.8") |
| 159 |
set(sip_disabled_features "[]") |
| 160 |
endif() |
| 161 |
|
| 162 |
set(module_tags) |
| 163 |
if (QT_MAJOR_VERSION STREQUAL "5") |
| 164 |
foreach(_tag ${SIP_TAGS}) |
| 165 |
string(APPEND module_tags "\"${_tag}\",") |
| 166 |
endforeach() |
| 167 |
endif() |
| 168 |
set(module_tags "[${module_tags}]") |
| 169 |
|
| 170 |
set(sip_include_dirs) |
| 171 |
foreach(_inc ${SIP_INCLUDES}) |
| 172 |
get_filename_component(_abs_inc ${_inc} ABSOLUTE) |
| 173 |
string(APPEND sip_include_dirs "\"${_abs_inc}\",") |
| 174 |
endforeach() |
| 175 |
set(sip_include_dirs "[${sip_include_dirs}]") |
| 176 |
|
| 177 |
string(REPLACE "." "/" _x ${MODULE_NAME}) |
| 178 |
get_filename_component(_parent_module_path ${_x} PATH) |
| 179 |
get_filename_component(_child_module_name ${_x} NAME) |
| 180 |
|
| 181 |
get_filename_component(_module_path ${MODULE_SIP} PATH) |
| 182 |
|
| 183 |
set(CMAKE_CURRENT_SIP_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/_tmp") |
| 184 |
|
| 185 |
get_filename_component(_abs_module_sip ${MODULE_SIP} ABSOLUTE) |
| 186 |
|
| 187 |
# We give this target a long logical target name. |
| 188 |
# (This is to avoid having the library name clash with any already |
| 189 |
# install library names. If that happens then cmake dependency |
| 190 |
# tracking get confused.) |
| 191 |
string(REPLACE "." "_" _logical_name ${MODULE_NAME}) |
| 192 |
set(_logical_name "python_module_${_logical_name}") |
| 193 |
|
| 194 |
set(_sip_output_files) |
| 195 |
foreach(CONCAT_NUM RANGE 0 ${SIP_CONCAT_PARTS}) |
| 196 |
if(${CONCAT_NUM} LESS ${SIP_CONCAT_PARTS}) |
| 197 |
set(_sip_output_files ${_sip_output_files} ${CMAKE_CURRENT_SIP_OUTPUT_DIR}/${_child_module_name}/sip${_child_module_name}part${CONCAT_NUM}.cpp ) |
| 198 |
endif( ${CONCAT_NUM} LESS ${SIP_CONCAT_PARTS}) |
| 199 |
endforeach(CONCAT_NUM RANGE 0 ${SIP_CONCAT_PARTS}) |
| 200 |
|
| 201 |
set(_sip_output_stubs ${CMAKE_CURRENT_SIP_OUTPUT_DIR}/${_child_module_name}/${_child_module_name}.pyi ) |
| 202 |
set(_complete_stubs ${CMAKE_CURRENT_SIP_OUTPUT_DIR}/${_child_module_name}/${_child_module_name}_complete.pyi ) |
| 203 |
|
| 204 |
configure_file( |
| 205 |
${pyproject_toml} |
| 206 |
${CMAKE_CURRENT_BINARY_DIR}/pyproject.toml |
| 207 |
) |
| 208 |
if (WIN32) |
| 209 |
set(_krita_python_path "${KRITA_PYTHONPATH_V5};$ENV{PYTHONPATH}") |
| 210 |
else() |
| 211 |
set(_krita_python_path "${KRITA_PYTHONPATH_V5}:$ENV{PYTHONPATH}") |
| 212 |
endif() |
| 213 |
|
| 214 |
add_custom_command( |
| 215 |
COMMAND |
| 216 |
${CMAKE_COMMAND} -E echo "Generating SIP 5+ bindings for ${MODULE_NAME}..." |
| 217 |
COMMAND |
| 218 |
${CMAKE_COMMAND} -E env |
| 219 |
"PYTHONPATH=${_krita_python_path}" |
| 220 |
${Python_EXECUTABLE} |
| 221 |
${sip_generate} |
| 222 |
--build-dir ${CMAKE_CURRENT_SIP_OUTPUT_DIR} |
| 223 |
--target-dir ${PYTHON_SITE_PACKAGES_INSTALL_DIR}/${_parent_module_path} |
| 224 |
--concatenate ${SIP_CONCAT_PARTS} |
| 225 |
COMMAND |
| 226 |
${CMAKE_COMMAND} -E env |
| 227 |
"PYTHONPATH=${_krita_python_path}" |
| 228 |
${Python_EXECUTABLE} |
| 229 |
${join_files} |
| 230 |
${_sip_output_stubs} ${SIP_EXTRA_STUBS} |
| 231 |
${_complete_stubs} |
| 232 |
WORKING_DIRECTORY |
| 233 |
${CMAKE_CURRENT_BINARY_DIR} |
| 234 |
DEPENDS |
| 235 |
${CMAKE_CURRENT_BINARY_DIR}/pyproject.toml ${SIP_EXTRA_STUBS} |
| 236 |
OUTPUT |
| 237 |
${_sip_output_files} ${_complete_stubs} |
| 238 |
) |
| 239 |
|
| 240 |
# not sure if type MODULE could be usec anywhere, limit to cygwin for now |
| 241 |
if (WIN32 OR CYGWIN OR APPLE) |
| 242 |
add_library(${_logical_name} MODULE ${_sip_output_files}) |
| 243 |
else () |
| 244 |
add_library(${_logical_name} SHARED ${_sip_output_files}) |
| 245 |
endif () |
| 246 |
target_include_directories(${_logical_name} PRIVATE ${CMAKE_CURRENT_SIP_OUTPUT_DIR}) |
| 247 |
target_link_libraries(${_logical_name} ${PYTHON_LIBRARY}) |
| 248 |
target_link_libraries(${_logical_name} ${EXTRA_LINK_LIBRARIES}) |
| 249 |
set_target_properties(${_logical_name} PROPERTIES PREFIX "" OUTPUT_NAME ${_child_module_name}) |
| 250 |
|
| 251 |
if (MINGW) |
| 252 |
target_compile_definitions(${_logical_name} PRIVATE _hypot=hypot) |
| 253 |
elseif(NOT MSVC) |
| 254 |
# SIP v5+ redefines access to protected variables. |
| 255 |
target_compile_definitions(${_logical_name} PRIVATE SIP_PROTECTED_IS_PUBLIC) |
| 256 |
target_compile_definitions(${_logical_name} PRIVATE protected=public) |
| 257 |
endif() |
| 258 |
|
| 259 |
if(MSVC) |
| 260 |
set_target_properties(${_logical_name} PROPERTIES PDB_NAME "sip_v5_${_logical_name}") |
| 261 |
endif () |
| 262 |
|
| 263 |
if (WIN32) |
| 264 |
SET_TARGET_PROPERTIES(${_logical_name} PROPERTIES SUFFIX ".pyd") |
| 265 |
ENDIF () |
| 266 |
|
| 267 |
install(FILES ${_complete_stubs} DESTINATION "${PYTHON_SITE_PACKAGES_INSTALL_DIR}/${_parent_module_path}" RENAME "${_child_module_name}.pyi") |
| 268 |
install(TARGETS ${_logical_name} DESTINATION "${PYTHON_SITE_PACKAGES_INSTALL_DIR}/${_parent_module_path}") |
| 269 |
endmacro() |
| 270 |
endif() |