cb5a355
# Macros for SIP
cb5a355
# ~~~~~~~~~~~~~~
cb5a355
# Copyright (c) 2007, Simon Edwards <simon@simonzone.com>
cb5a355
# Redistribution and use is allowed according to the terms of the BSD license.
cb5a355
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
cb5a355
#
cb5a355
# SIP website: http://www.riverbankcomputing.co.uk/sip/index.php
cb5a355
#
cb5a355
# This file defines the following macros:
cb5a355
#
cb5a355
# ADD_SIP_PYTHON_MODULE (MODULE_NAME MODULE_SIP [library1, libaray2, ...])
cb5a355
#     Specifies a SIP file to be built into a Python module and installed.
cb5a355
#     MODULE_NAME is the name of Python module including any path name. (e.g.
cb5a355
#     os.sys, Foo.bar etc). MODULE_SIP the path and filename of the .sip file
cb5a355
#     to process and compile. libraryN are libraries that the Python module,
cb5a355
#     which is typically a shared library, should be linked to. The built
cb5a355
#     module will also be install into Python's site-packages directory.
cb5a355
#
cb5a355
# The behaviour of the ADD_SIP_PYTHON_MODULE macro can be controlled by a
cb5a355
# number of variables:
cb5a355
#
cb5a355
# SIP_INCLUDE_DIRS - List of directories which SIP will scan through when looking
cb5a355
#     for included .sip files. (Corresponds to the -I option for SIP.)
cb5a355
#
cb5a355
# SIP_TAGS - List of tags to define when running SIP. (Corresponds to the -t
cb5a355
#     option for SIP.)
cb5a355
#
cb5a355
# SIP_CONCAT_PARTS - An integer which defines the number of parts the C++ code
cb5a355
#     of each module should be split into. Defaults to 8. (Corresponds to the
cb5a355
#     -j option for SIP.)
cb5a355
#
cb5a355
# SIP_DISABLE_FEATURES - List of feature names which should be disabled
cb5a355
#     running SIP. (Corresponds to the -x option for SIP.)
cb5a355
#
cb5a355
# SIP_EXTRA_OPTIONS - Extra command line options which should be passed on to
cb5a355
#     SIP.
cb5a355
cb5a355
SET(SIP_INCLUDE_DIRS)
cb5a355
SET(SIP_TAGS)
cb5a355
SET(SIP_CONCAT_PARTS 8)
cb5a355
SET(SIP_DISABLE_FEATURES)
cb5a355
SET(SIP_EXTRA_OPTIONS)
cb5a355
cb5a355
MACRO(ADD_SIP_PYTHON_MODULE MODULE_NAME MODULE_SIP)
cb5a355
cb5a355
    SET(EXTRA_LINK_LIBRARIES ${ARGN})
cb5a355
cb5a355
    STRING(REPLACE "." "/" _x ${MODULE_NAME})
cb5a355
    GET_FILENAME_COMPONENT(_parent_module_path ${_x}  PATH)
cb5a355
    GET_FILENAME_COMPONENT(_child_module_name ${_x} NAME)
cb5a355
cb5a355
    GET_FILENAME_COMPONENT(_module_path ${MODULE_SIP} PATH)
cb5a355
    GET_FILENAME_COMPONENT(_abs_module_sip ${MODULE_SIP} ABSOLUTE)
cb5a355
cb5a355
    # We give this target a long logical target name.
cb5a355
    # (This is to avoid having the library name clash with any already
cb5a355
    # install library names. If that happens then cmake dependency
cb5a355
    # tracking get confused.)
cb5a355
    STRING(REPLACE "." "_" _logical_name ${MODULE_NAME})
cb5a355
    SET(_logical_name "python_module_${_logical_name}")
cb5a355
cb5a355
    FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${_module_path})    # Output goes in this dir.
cb5a355
cb5a355
    SET(_sip_includes)
cb5a355
    FOREACH (_inc ${SIP_INCLUDES})
cb5a355
        GET_FILENAME_COMPONENT(_abs_inc ${_inc} ABSOLUTE)
cb5a355
        LIST(APPEND _sip_includes -I ${_abs_inc})
cb5a355
    ENDFOREACH (_inc )
cb5a355
cb5a355
    SET(_sip_tags)
cb5a355
    FOREACH (_tag ${SIP_TAGS})
cb5a355
        LIST(APPEND _sip_tags -t ${_tag})
cb5a355
    ENDFOREACH (_tag)
cb5a355
cb5a355
    SET(_sip_x)
cb5a355
    FOREACH (_x ${SIP_DISABLE_FEATURES})
cb5a355
        LIST(APPEND _sip_x -x ${_x})
cb5a355
    ENDFOREACH (_x ${SIP_DISABLE_FEATURES})
cb5a355
cb5a355
    SET(_message "-DMESSAGE=Generating CPP code for module ${MODULE_NAME}")
cb5a355
    SET(_sip_output_files)
cb5a355
    FOREACH(CONCAT_NUM RANGE 0 ${SIP_CONCAT_PARTS} )
cb5a355
        IF( ${CONCAT_NUM} LESS ${SIP_CONCAT_PARTS} )
cb5a355
            SET(_sip_output_files ${_sip_output_files} ${CMAKE_CURRENT_BINARY_DIR}/${_module_path}/sip${_child_module_name}part${CONCAT_NUM}.cpp )
cb5a355
        ENDIF( ${CONCAT_NUM} LESS ${SIP_CONCAT_PARTS} )
cb5a355
    ENDFOREACH(CONCAT_NUM RANGE 0 ${SIP_CONCAT_PARTS} )
cb5a355
cb5a355
    # Suppress warnings
cb5a355
    IF(PEDANTIC)
cb5a355
      IF(MSVC)
cb5a355
        # 4996 deprecation warnings (bindings re-export deprecated methods)
cb5a355
        # 4701 potentially uninitialized variable used (sip generated code)
cb5a355
        # 4702 unreachable code (sip generated code)
cb5a355
        ADD_DEFINITIONS( /wd4996 /wd4701 /wd4702 )
cb5a355
      ELSE(MSVC)
cb5a355
        # disable all warnings
cb5a355
        ADD_DEFINITIONS( -w )
cb5a355
      ENDIF(MSVC)
cb5a355
    ENDIF(PEDANTIC)
cb5a355
cb5a355
    ADD_CUSTOM_COMMAND(
cb5a355
        OUTPUT ${_sip_output_files}
cb5a355
        COMMAND ${CMAKE_COMMAND} -E echo ${message}
cb5a355
        COMMAND ${CMAKE_COMMAND} -E touch ${_sip_output_files}
cb5a355
        COMMAND ${SIP_EXECUTABLE} ${_sip_tags} ${_sip_x} ${SIP_EXTRA_OPTIONS} -j ${SIP_CONCAT_PARTS} -c ${CMAKE_CURRENT_BINARY_DIR}/${_module_path} ${_sip_includes} ${_abs_module_sip}
cb5a355
        DEPENDS ${_abs_module_sip} ${SIP_EXTRA_FILES_DEPEND}
cb5a355
    )
cb5a355
    ADD_LIBRARY(${_logical_name} MODULE ${_sip_output_files} ${SIP_EXTRA_SOURCE_FILES})
cb5a355
    IF (NOT APPLE)
cb5a355
        IF ("${Python3_VERSION_MINOR}" GREATER 7)
cb5a355
            MESSAGE(STATUS "Python > 3.7 - not linking to libpython")
cb5a355
        ELSE ()
cb5a355
            TARGET_LINK_LIBRARIES(${_logical_name} ${Python3_LIBRARIES})
cb5a355
        ENDIF ()
cb5a355
    ENDIF (NOT APPLE)
cb5a355
    TARGET_LINK_LIBRARIES(${_logical_name} ${EXTRA_LINK_LIBRARIES})
cb5a355
    IF (APPLE)
cb5a355
        SET_TARGET_PROPERTIES(${_logical_name} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
cb5a355
    ENDIF (APPLE)
cb5a355
    SET_TARGET_PROPERTIES(${_logical_name} PROPERTIES PREFIX "" OUTPUT_NAME ${_child_module_name})
cb5a355
cb5a355
    IF (WIN32)
cb5a355
        SET_TARGET_PROPERTIES(${_logical_name} PROPERTIES SUFFIX ".pyd" IMPORT_PREFIX "_")
cb5a355
    ENDIF (WIN32)
cb5a355
cb5a355
    INSTALL(TARGETS ${_logical_name} DESTINATION "${Python3_SITEARCH}/${_parent_module_path}")
cb5a355
cb5a355
ENDMACRO(ADD_SIP_PYTHON_MODULE)