ctkMacroBuildLibWrapper.cmake 5.4 KB

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