ctkMacroBuildLibWrapper.cmake 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #! \ingroup CMakeAPI
  31. macro(ctkMacroBuildLibWrapper)
  32. ctkMacroParseArguments(MY
  33. "NAMESPACE;TARGET;SRCS;WRAPPER_LIBRARY_TYPE;ARCHIVE_OUTPUT_DIRECTORY;LIBRARY_OUTPUT_DIRECTORY;RUNTIME_OUTPUT_DIRECTORY;INSTALL_BIN_DIR;INSTALL_LIB_DIR"
  34. "NO_INSTALL"
  35. ${ARGN}
  36. )
  37. # Sanity checks
  38. if(NOT DEFINED MY_TARGET)
  39. message(FATAL_ERROR "NAME is mandatory")
  40. endif()
  41. if(NOT DEFINED MY_WRAPPER_LIBRARY_TYPE OR "${MY_WRAPPER_LIBRARY_TYPE}" STREQUAL "SHARED")
  42. set(MY_WRAPPER_LIBRARY_TYPE "MODULE")
  43. endif()
  44. if(NOT DEFINED MY_NAMESPACE)
  45. set(MY_NAMESPACE "org.commontk")
  46. endif()
  47. foreach(type RUNTIME LIBRARY ARCHIVE)
  48. if(NOT DEFINED MY_${type}_OUTPUT_DIRECTORY)
  49. set(MY_${type}_OUTPUT_DIRECTORY ${CMAKE_${type}_OUTPUT_DIRECTORY})
  50. endif()
  51. endforeach()
  52. if(NOT DEFINED MY_INSTALL_BIN_DIR)
  53. set(MY_INSTALL_BIN_DIR ${CTK_INSTALL_BIN_DIR})
  54. endif()
  55. if(NOT DEFINED MY_INSTALL_LIB_DIR)
  56. set(MY_INSTALL_LIB_DIR ${CTK_INSTALL_LIB_DIR})
  57. endif()
  58. # Define library name
  59. set(lib_name ${MY_TARGET})
  60. # Include dirs
  61. set(my_includes
  62. ${CMAKE_CURRENT_SOURCE_DIR}
  63. ${CMAKE_CURRENT_BINARY_DIR}
  64. )
  65. # Since the PythonQt decorator depends on PythonQt, Python and VTK, let's link against
  66. # these ones to avoid complaints of MSVC
  67. # Note: "LINK_DIRECTORIES" has to be invoked before "ADD_LIBRARY"
  68. set(my_EXTRA_PYTHON_LIBRARIES ${PYTHON_LIBRARY} ${PYTHONQT_LIBRARIES})
  69. # Does a header having the expected filename exists ?
  70. string(REGEX REPLACE "^CTK" "ctk" lib_name_lc_ctk ${lib_name})
  71. set(DECORATOR_HEADER ${lib_name_lc_ctk}PythonQtDecorators.h)
  72. set(HAS_DECORATOR FALSE)
  73. if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${DECORATOR_HEADER})
  74. set(HAS_DECORATOR TRUE)
  75. set(DECORATOR_HEADER ${DECORATOR_HEADER})
  76. set_source_files_properties(${DECORATOR_HEADER} WRAP_EXCLUDE)
  77. endif()
  78. #message("HAS_DECORATOR:${HAS_DECORATOR}")
  79. #message("path/to/DECORATOR_HEADER:${CMAKE_CURRENT_SOURCE_DIR}/${DECORATOR_HEADER}")
  80. set(KIT_PYTHONQT_SRCS) # Clear variable
  81. ctkMacroWrapPythonQt(${MY_NAMESPACE} ${lib_name}
  82. KIT_PYTHONQT_SRCS "${MY_SRCS}" FALSE ${HAS_DECORATOR})
  83. if(HAS_DECORATOR)
  84. list(APPEND KIT_PYTHONQT_SRCS ${DECORATOR_HEADER})
  85. if (CTK_QT_VERSION VERSION_GREATER "4")
  86. qt5_wrap_cpp(KIT_PYTHONQT_SRCS ${DECORATOR_HEADER} OPTIONS -f${DECORATOR_HEADER})
  87. else()
  88. QT4_WRAP_CPP(KIT_PYTHONQT_SRCS ${DECORATOR_HEADER} OPTIONS -f${DECORATOR_HEADER})
  89. endif()
  90. endif()
  91. add_library(${lib_name}PythonQt ${MY_WRAPPER_LIBRARY_TYPE} ${KIT_PYTHONQT_SRCS})
  92. target_link_libraries(${lib_name}PythonQt ${lib_name} ${my_EXTRA_PYTHON_LIBRARIES})
  93. if(MY_WRAPPER_LIBRARY_TYPE STREQUAL "STATIC")
  94. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  95. set_target_properties(${lib_name}PythonQt PROPERTIES COMPILE_FLAGS "-fPIC")
  96. endif()
  97. endif()
  98. if(MY_WRAPPER_LIBRARY_TYPE STREQUAL "MODULE")
  99. # Make sure that no prefix is set on the library
  100. set_target_properties(${lib_name}PythonQt PROPERTIES PREFIX "")
  101. # Python extension modules on Windows must have the extension ".pyd"
  102. # instead of ".dll" as of Python 2.5. Older python versions do support
  103. # this suffix.
  104. # See http://docs.python.org/faq/windows.html#is-a-pyd-file-the-same-as-a-dll
  105. if(WIN32 AND NOT CYGWIN)
  106. set_target_properties(${lib_name}PythonQt PROPERTIES SUFFIX ".pyd")
  107. endif()
  108. endif()
  109. set_target_properties(${lib_name}PythonQt PROPERTIES
  110. RUNTIME_OUTPUT_DIRECTORY "${MY_RUNTIME_OUTPUT_DIRECTORY}"
  111. LIBRARY_OUTPUT_DIRECTORY "${MY_LIBRARY_OUTPUT_DIRECTORY}"
  112. ARCHIVE_OUTPUT_DIRECTORY "${MY_ARCHIVE_OUTPUT_DIRECTORY}"
  113. )
  114. # Set labels associated with the target.
  115. set_target_properties(${lib_name}PythonQt PROPERTIES LABELS ${lib_name})
  116. # Update list of libraries wrapped with PythonQt
  117. set(CTK_WRAPPED_LIBRARIES_PYTHONQT
  118. ${CTK_WRAPPED_LIBRARIES_PYTHONQT} ${lib_name}
  119. CACHE INTERNAL "CTK libraries wrapped using PythonQt" FORCE)
  120. # Install rules
  121. if(NOT MY_NO_INSTALL AND MY_WRAPPER_LIBRARY_TYPE STREQUAL "MODULE")
  122. install(TARGETS ${lib_name}PythonQt
  123. RUNTIME DESTINATION ${MY_INSTALL_LIB_DIR} COMPONENT RuntimePlugins
  124. LIBRARY DESTINATION ${MY_INSTALL_LIB_DIR} COMPONENT RuntimePlugins
  125. ARCHIVE DESTINATION ${MY_INSTALL_LIB_DIR} COMPONENT Development)
  126. endif()
  127. endmacro()