Warning, /graphics/krita/cmake/modules/SIPMacros.cmake is written in an unsupported language. File is not indexed.

0001 # Macros for SIP
0002 # ~~~~~~~~~~~~~~
0003 # SPDX-FileCopyrightText: 2007 Simon Edwards <simon@simonzone.com>
0004 #
0005 # SPDX-License-Identifier: BSD-3-Clause
0006 #
0007 # SIP website: http://www.riverbankcomputing.co.uk/sip/index.php
0008 #
0009 # This file defines the following macros:
0010 #
0011 # ADD_SIP_PYTHON_MODULE (MODULE_NAME MODULE_SIP [library1, library2, ...])
0012 #     Specifies a SIP file to be built into a Python module and installed.
0013 #     MODULE_NAME is the name of Python module including any path name. (e.g.
0014 #     os.sys, Foo.bar etc). MODULE_SIP the path and filename of the .sip file
0015 #     to process and compile. libraryN are libraries that the Python module,
0016 #     which is typically a shared library, should be linked to. The built
0017 #     module will also be install into Python's site-packages directory.
0018 #
0019 # The behaviour of the ADD_SIP_PYTHON_MODULE macro can be controlled by a
0020 # number of variables:
0021 #
0022 # SIP_INCLUDES - List of directories which SIP will scan through when looking
0023 #     for included .sip files. (Corresponds to the -I option for SIP.)
0024 #
0025 # SIP_TAGS - List of tags to define when running SIP. (Corresponds to the -t
0026 #     option for SIP.)
0027 #
0028 # SIP_CONCAT_PARTS - An integer which defines the number of parts the C++ code
0029 #     of each module should be split into. Defaults to 8. (Corresponds to the
0030 #     -j option for SIP.)
0031 #
0032 # SIP_DISABLE_FEATURES - List of feature names which should be disabled
0033 #     running SIP. (Corresponds to the -x option for SIP.)
0034 #
0035 # SIP_EXTRA_OPTIONS - Extra command line options which should be passed on to
0036 #     SIP.
0037 
0038 SET(SIP_INCLUDES)
0039 SET(SIP_TAGS)
0040 SET(SIP_CONCAT_PARTS 8)
0041 SET(SIP_DISABLE_FEATURES)
0042 SET(SIP_EXTRA_OPTIONS)
0043 
0044 if (${SIP_VERSION_STR} VERSION_LESS 5)
0045 
0046 MACRO(ADD_SIP_PYTHON_MODULE MODULE_NAME MODULE_SIP)
0047 
0048     SET(EXTRA_LINK_LIBRARIES ${ARGN})
0049 
0050     STRING(REPLACE "." "/" _x ${MODULE_NAME})
0051     GET_FILENAME_COMPONENT(_parent_module_path ${_x}  PATH)
0052     GET_FILENAME_COMPONENT(_child_module_name ${_x} NAME)
0053 
0054     GET_FILENAME_COMPONENT(_module_path ${MODULE_SIP} PATH)
0055 
0056     if(_module_path STREQUAL "")
0057         set(CMAKE_CURRENT_SIP_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}")
0058     else(_module_path STREQUAL "")
0059         set(CMAKE_CURRENT_SIP_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/${_module_path}")
0060     endif(_module_path STREQUAL "")
0061 
0062     GET_FILENAME_COMPONENT(_abs_module_sip ${MODULE_SIP} ABSOLUTE)
0063 
0064     # We give this target a long logical target name.
0065     # (This is to avoid having the library name clash with any already
0066     # install library names. If that happens then cmake dependency
0067     # tracking get confused.)
0068     STRING(REPLACE "." "_" _logical_name ${MODULE_NAME})
0069     SET(_logical_name "python_module_${_logical_name}")
0070 
0071     FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_SIP_OUTPUT_DIR})    # Output goes in this dir.
0072 
0073     SET(_sip_includes)
0074     FOREACH (_inc ${SIP_INCLUDES})
0075         GET_FILENAME_COMPONENT(_abs_inc ${_inc} ABSOLUTE)
0076         LIST(APPEND _sip_includes -I ${_abs_inc})
0077     ENDFOREACH (_inc )
0078 
0079     SET(_sip_tags)
0080     FOREACH (_tag ${SIP_TAGS})
0081         LIST(APPEND _sip_tags -t ${_tag})
0082     ENDFOREACH (_tag)
0083 
0084     SET(_sip_x)
0085     FOREACH (_x ${SIP_DISABLE_FEATURES})
0086         LIST(APPEND _sip_x -x ${_x})
0087     ENDFOREACH (_x ${SIP_DISABLE_FEATURES})
0088 
0089     SET(_message "-DMESSAGE=Generating CPP code for module ${MODULE_NAME}")
0090     SET(_sip_output_files)
0091     FOREACH(CONCAT_NUM RANGE 0 ${SIP_CONCAT_PARTS} )
0092         IF( ${CONCAT_NUM} LESS ${SIP_CONCAT_PARTS} )
0093             SET(_sip_output_files ${_sip_output_files} ${CMAKE_CURRENT_SIP_OUTPUT_DIR}/sip${_child_module_name}part${CONCAT_NUM}.cpp )
0094         ENDIF( ${CONCAT_NUM} LESS ${SIP_CONCAT_PARTS} )
0095     ENDFOREACH(CONCAT_NUM RANGE 0 ${SIP_CONCAT_PARTS} )
0096 
0097     ADD_CUSTOM_COMMAND(
0098         OUTPUT ${_sip_output_files}
0099         COMMAND ${CMAKE_COMMAND} -E echo ${message}
0100         COMMAND ${CMAKE_COMMAND} -E touch ${_sip_output_files}
0101         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}
0102         DEPENDS ${_abs_module_sip} ${SIP_EXTRA_FILES_DEPEND}
0103     )
0104     # not sure if type MODULE could be uses anywhere, limit to cygwin for now
0105     IF (WIN32 OR CYGWIN OR APPLE)
0106         ADD_LIBRARY(${_logical_name} MODULE ${_sip_output_files} )
0107     ELSE (WIN32 OR CYGWIN OR APPLE)
0108         ADD_LIBRARY(${_logical_name} SHARED ${_sip_output_files} )
0109     ENDIF (WIN32 OR CYGWIN OR APPLE)
0110     TARGET_LINK_LIBRARIES(${_logical_name} ${PYTHON_LIBRARY})
0111     TARGET_LINK_LIBRARIES(${_logical_name} ${EXTRA_LINK_LIBRARIES})
0112     SET_TARGET_PROPERTIES(${_logical_name} PROPERTIES PREFIX "" OUTPUT_NAME ${_child_module_name})
0113 
0114     IF (MINGW)
0115         TARGET_COMPILE_DEFINITIONS(${_logical_name} PRIVATE _hypot=hypot)
0116     ENDIF (MINGW)
0117 
0118     if (MSVC)
0119         SET_TARGET_PROPERTIES(${_logical_name} PROPERTIES PDB_NAME "sip_v4_${_logical_name}")
0120     ENDIF (MSVC)
0121 
0122     IF (WIN32)
0123         SET_TARGET_PROPERTIES(${_logical_name} PROPERTIES SUFFIX ".pyd")
0124     ENDIF (WIN32)
0125 
0126     INSTALL(TARGETS ${_logical_name} DESTINATION "${PYTHON_SITE_PACKAGES_INSTALL_DIR}/${_parent_module_path}")
0127     if (MSVC)
0128         INSTALL(
0129             FILES $<TARGET_PDB_FILE:${_logical_name}>
0130             DESTINATION "${PYTHON_SITE_PACKAGES_INSTALL_DIR}/${_parent_module_path}"
0131             OPTIONAL
0132         )
0133     endif()
0134 
0135 ENDMACRO(ADD_SIP_PYTHON_MODULE)
0136 
0137 else()
0138     find_file(sip_generate "sip-generate.py" PATHS ${CMAKE_MODULE_PATH} NO_CMAKE_FIND_ROOT_PATH)
0139     find_file(pyproject_toml "pyproject.toml.in" PATHS ${CMAKE_MODULE_PATH} NO_CMAKE_FIND_ROOT_PATH)
0140 
0141     macro(add_sip_python_module_v5 MODULE_NAME MODULE_SIP)        
0142         get_filename_component(module_name_toml ${MODULE_SIP} NAME_WE)
0143         set(module_srcs "${SIP_EXTRA_FILES_DEPEND}")
0144 
0145         set(EXTRA_LINK_LIBRARIES ${ARGN})
0146 
0147         if (SIP_MODULE)
0148             set(sip_name ${SIP_MODULE})
0149         else()
0150             set(sip_name "sip")
0151         endif()
0152 
0153         set(module_tags)
0154         foreach(_tag ${SIP_TAGS})
0155             string(APPEND module_tags "\"${_tag}\",")
0156         endforeach()
0157         set(module_tags "[${module_tags}]")
0158 
0159         set(sip_include_dirs)
0160         foreach(_inc ${SIP_INCLUDES})
0161             get_filename_component(_abs_inc ${_inc} ABSOLUTE)
0162             string(APPEND sip_include_dirs "\"${_abs_inc}\",")
0163         endforeach()
0164         set(sip_include_dirs "[${sip_include_dirs}]")
0165 
0166         string(REPLACE "." "/" _x ${MODULE_NAME})
0167         get_filename_component(_parent_module_path ${_x} PATH)
0168         get_filename_component(_child_module_name ${_x} NAME)
0169 
0170         get_filename_component(_module_path ${MODULE_SIP} PATH)
0171 
0172         set(CMAKE_CURRENT_SIP_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/_tmp")
0173 
0174         get_filename_component(_abs_module_sip ${MODULE_SIP} ABSOLUTE)
0175 
0176         # We give this target a long logical target name.
0177         # (This is to avoid having the library name clash with any already
0178         # install library names. If that happens then cmake dependency
0179         # tracking get confused.)
0180         string(REPLACE "." "_" _logical_name ${MODULE_NAME})
0181         set(_logical_name "python_module_${_logical_name}")
0182 
0183         set(_sip_output_files)
0184         foreach(CONCAT_NUM RANGE 0 ${SIP_CONCAT_PARTS})
0185             if(${CONCAT_NUM} LESS ${SIP_CONCAT_PARTS})
0186                 set(_sip_output_files ${_sip_output_files} ${CMAKE_CURRENT_SIP_OUTPUT_DIR}/${_child_module_name}/sip${_child_module_name}part${CONCAT_NUM}.cpp )
0187             endif( ${CONCAT_NUM} LESS ${SIP_CONCAT_PARTS})
0188         endforeach(CONCAT_NUM RANGE 0 ${SIP_CONCAT_PARTS})
0189 
0190         configure_file(
0191             ${pyproject_toml}
0192             ${CMAKE_CURRENT_BINARY_DIR}/pyproject.toml
0193         )
0194         if (WIN32)
0195             set(_krita_python_path "${KRITA_PYTHONPATH_V5};$ENV{PYTHONPATH}")
0196         else()
0197             set(_krita_python_path "${KRITA_PYTHONPATH_V5}:$ENV{PYTHONPATH}")
0198         endif()
0199 
0200         add_custom_command(
0201             COMMAND
0202                 ${CMAKE_COMMAND} -E echo "Generating SIP 5+ bindings for ${MODULE_NAME}..."
0203             COMMAND
0204                 ${CMAKE_COMMAND} -E env
0205                 "PYTHONPATH=${_krita_python_path}"
0206                 ${Python_EXECUTABLE}
0207                 ${sip_generate}
0208                 --build-dir ${CMAKE_CURRENT_SIP_OUTPUT_DIR}
0209                 --target-dir ${PYTHON_SITE_PACKAGES_INSTALL_DIR}/${_parent_module_path}
0210                 --concatenate ${SIP_CONCAT_PARTS}
0211             WORKING_DIRECTORY
0212                 ${CMAKE_CURRENT_BINARY_DIR}
0213             DEPENDS
0214                 ${CMAKE_CURRENT_BINARY_DIR}/pyproject.toml
0215             OUTPUT
0216                 ${_sip_output_files}
0217         )
0218 
0219         # not sure if type MODULE could be usec anywhere, limit to cygwin for now
0220         if (WIN32 OR CYGWIN OR APPLE)
0221             add_library(${_logical_name} MODULE ${_sip_output_files})
0222         else ()
0223             add_library(${_logical_name} SHARED ${_sip_output_files})
0224         endif ()
0225         target_include_directories(${_logical_name} PRIVATE ${CMAKE_CURRENT_SIP_OUTPUT_DIR})
0226         target_link_libraries(${_logical_name} ${PYTHON_LIBRARY})
0227         target_link_libraries(${_logical_name} ${EXTRA_LINK_LIBRARIES})
0228         set_target_properties(${_logical_name} PROPERTIES PREFIX "" OUTPUT_NAME ${_child_module_name})
0229 
0230         if (MINGW)
0231             target_compile_definitions(${_logical_name} PRIVATE _hypot=hypot)
0232         elseif(NOT MSVC)
0233             # SIP v5+ redefines access to protected variables.
0234             target_compile_definitions(${_logical_name} PRIVATE SIP_PROTECTED_IS_PUBLIC)
0235             target_compile_definitions(${_logical_name} PRIVATE protected=public)
0236         endif()
0237 
0238         if(MSVC)
0239             set_target_properties(${_logical_name} PROPERTIES PDB_NAME "sip_v5_${_logical_name}")
0240         endif ()
0241 
0242         if (WIN32)
0243             SET_TARGET_PROPERTIES(${_logical_name} PROPERTIES SUFFIX ".pyd")
0244         ENDIF ()
0245 
0246         install(TARGETS ${_logical_name} DESTINATION "${PYTHON_SITE_PACKAGES_INSTALL_DIR}/${_parent_module_path}")
0247     endmacro()
0248 endif()