ctkMacroBuildLibWrapper.cmake 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ###########################################################################
  2. #
  3. # Library: CTK
  4. #
  5. # Copyright (c) Kitware Inc.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0.txt
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. ###########################################################################
  20. #
  21. # Depends on:
  22. # CTK/CMake/ctkMacroParseArguments.cmake
  23. #
  24. #! When CTK is built as shared library, the following macro builds a python module
  25. #! associated with the generated PythonQt wrappers. When loaded, it will
  26. #! dynamically loads both the (1) generated decorators and the (2) hand written one.
  27. #! On the other hand, when CTK is built statically, it creates a
  28. #! static library providing a initialization function that will allow to load
  29. #! both (1) and (2).
  30. # Function copied from https://github.com/scikit-build/scikit-build/pull/299
  31. # XXX Update this CMake module to use function from scikit-build to build the wrapper
  32. function(_ctk_set_python_extension_symbol_visibility _target)
  33. if(PYTHON_VERSION_MAJOR VERSION_GREATER 2)
  34. set(_modinit_prefix "PyInit_")
  35. else()
  36. set(_modinit_prefix "init")
  37. endif()
  38. if("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
  39. set_target_properties(${_target} PROPERTIES LINK_FLAGS
  40. "/EXPORT:${_modinit_prefix}${_target}"
  41. )
  42. elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
  43. set(_script_path
  44. ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${_target}-version-script.map
  45. )
  46. file(WRITE ${_script_path}
  47. "{global: ${_modinit_prefix}${_target}; local: *; };"
  48. )
  49. set_property(TARGET ${_target} APPEND_STRING PROPERTY LINK_FLAGS
  50. " -Wl,--version-script=${_script_path}"
  51. )
  52. endif()
  53. endfunction()
  54. #! \ingroup CMakeAPI
  55. macro(ctkMacroBuildLibWrapper)
  56. ctkMacroParseArguments(MY
  57. "NAMESPACE;TARGET;SRCS;WRAPPER_LIBRARY_TYPE;ARCHIVE_OUTPUT_DIRECTORY;LIBRARY_OUTPUT_DIRECTORY;RUNTIME_OUTPUT_DIRECTORY;INSTALL_BIN_DIR;INSTALL_LIB_DIR"
  58. "NO_INSTALL"
  59. ${ARGN}
  60. )
  61. # Sanity checks
  62. if(NOT DEFINED MY_TARGET)
  63. message(FATAL_ERROR "NAME is mandatory")
  64. endif()
  65. if(NOT DEFINED MY_WRAPPER_LIBRARY_TYPE OR "${MY_WRAPPER_LIBRARY_TYPE}" STREQUAL "SHARED")
  66. set(MY_WRAPPER_LIBRARY_TYPE "MODULE")
  67. endif()
  68. if(NOT DEFINED MY_NAMESPACE)
  69. set(MY_NAMESPACE "org.commontk")
  70. endif()
  71. foreach(type RUNTIME LIBRARY ARCHIVE)
  72. if(NOT DEFINED MY_${type}_OUTPUT_DIRECTORY)
  73. set(MY_${type}_OUTPUT_DIRECTORY ${CMAKE_${type}_OUTPUT_DIRECTORY})
  74. endif()
  75. endforeach()
  76. if(NOT DEFINED MY_INSTALL_BIN_DIR)
  77. set(MY_INSTALL_BIN_DIR ${CTK_INSTALL_BIN_DIR})
  78. endif()
  79. if(NOT DEFINED MY_INSTALL_LIB_DIR)
  80. set(MY_INSTALL_LIB_DIR ${CTK_INSTALL_LIB_DIR})
  81. endif()
  82. # Define library name
  83. set(lib_name ${MY_TARGET})
  84. # Include dirs
  85. set(my_includes
  86. ${CMAKE_CURRENT_SOURCE_DIR}
  87. ${CMAKE_CURRENT_BINARY_DIR}
  88. )
  89. # Since the PythonQt decorator depends on PythonQt, Python and VTK, let's link against
  90. # these ones to avoid complaints of MSVC
  91. # Note: "LINK_DIRECTORIES" has to be invoked before "ADD_LIBRARY"
  92. set(my_EXTRA_PYTHON_LIBRARIES ${PYTHON_LIBRARY} ${PYTHONQT_LIBRARIES})
  93. # Does a header having the expected filename exists ?
  94. string(REGEX REPLACE "^CTK" "ctk" lib_name_lc_ctk ${lib_name})
  95. set(DECORATOR_HEADER ${lib_name_lc_ctk}PythonQtDecorators.h)
  96. set(HAS_DECORATOR FALSE)
  97. set(_msg "Looking for decorator header ${DECORATOR_HEADER}")
  98. message(STATUS "${_msg}")
  99. set(_status "Not found")
  100. if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${DECORATOR_HEADER})
  101. set(HAS_DECORATOR TRUE)
  102. set(DECORATOR_HEADER ${DECORATOR_HEADER})
  103. set_source_files_properties(${DECORATOR_HEADER} WRAP_EXCLUDE)
  104. set(_status "Found")
  105. endif()
  106. message(STATUS "${_msg} - ${_status}")
  107. #message("path/to/DECORATOR_HEADER:${CMAKE_CURRENT_SOURCE_DIR}/${DECORATOR_HEADER}")
  108. set(KIT_PYTHONQT_SRCS) # Clear variable
  109. ctkMacroWrapPythonQt(${MY_NAMESPACE} ${lib_name}
  110. KIT_PYTHONQT_SRCS "${MY_SRCS}" FALSE ${HAS_DECORATOR})
  111. if(HAS_DECORATOR)
  112. list(APPEND KIT_PYTHONQT_SRCS ${DECORATOR_HEADER})
  113. if (CTK_QT_VERSION VERSION_GREATER "4")
  114. qt5_wrap_cpp(KIT_PYTHONQT_SRCS ${DECORATOR_HEADER} OPTIONS -f${DECORATOR_HEADER})
  115. else()
  116. QT4_WRAP_CPP(KIT_PYTHONQT_SRCS ${DECORATOR_HEADER} OPTIONS -f${DECORATOR_HEADER})
  117. endif()
  118. endif()
  119. add_library(${lib_name}PythonQt ${MY_WRAPPER_LIBRARY_TYPE} ${KIT_PYTHONQT_SRCS})
  120. target_link_libraries(${lib_name}PythonQt ${lib_name} ${my_EXTRA_PYTHON_LIBRARIES})
  121. if(MY_WRAPPER_LIBRARY_TYPE STREQUAL "STATIC")
  122. if(CMAKE_SIZEOF_VOID_P EQUAL 8) # 64-bit
  123. set_target_properties(${lib_name}PythonQt PROPERTIES COMPILE_FLAGS "-fPIC")
  124. endif()
  125. endif()
  126. if(MY_WRAPPER_LIBRARY_TYPE STREQUAL "MODULE")
  127. # Make sure that no prefix is set on the library
  128. set_target_properties(${lib_name}PythonQt PROPERTIES PREFIX "")
  129. # Python extension modules on Windows must have the extension ".pyd"
  130. # instead of ".dll" as of Python 2.5. Older python versions do support
  131. # this suffix.
  132. # See http://docs.python.org/faq/windows.html#is-a-pyd-file-the-same-as-a-dll
  133. if(WIN32 AND NOT CYGWIN)
  134. set_target_properties(${lib_name}PythonQt PROPERTIES SUFFIX ".pyd")
  135. endif()
  136. endif()
  137. _ctk_set_python_extension_symbol_visibility(${lib_name}PythonQt)
  138. set_target_properties(${lib_name}PythonQt PROPERTIES
  139. RUNTIME_OUTPUT_DIRECTORY "${MY_RUNTIME_OUTPUT_DIRECTORY}"
  140. LIBRARY_OUTPUT_DIRECTORY "${MY_LIBRARY_OUTPUT_DIRECTORY}"
  141. ARCHIVE_OUTPUT_DIRECTORY "${MY_ARCHIVE_OUTPUT_DIRECTORY}"
  142. )
  143. # Set labels associated with the target.
  144. set_target_properties(${lib_name}PythonQt PROPERTIES LABELS ${lib_name})
  145. # Update list of libraries wrapped with PythonQt
  146. set(CTK_WRAPPED_LIBRARIES_PYTHONQT
  147. ${CTK_WRAPPED_LIBRARIES_PYTHONQT} ${lib_name}
  148. CACHE INTERNAL "CTK libraries wrapped using PythonQt" FORCE)
  149. # Install rules
  150. if(NOT MY_NO_INSTALL AND MY_WRAPPER_LIBRARY_TYPE STREQUAL "MODULE")
  151. install(TARGETS ${lib_name}PythonQt
  152. RUNTIME DESTINATION ${MY_INSTALL_LIB_DIR} COMPONENT RuntimePlugins
  153. LIBRARY DESTINATION ${MY_INSTALL_LIB_DIR} COMPONENT RuntimePlugins
  154. ARCHIVE DESTINATION ${MY_INSTALL_LIB_DIR} COMPONENT Development)
  155. endif()
  156. endmacro()