ctkMacroBuildLibWrapper.cmake 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. # Should we link against VTK
  73. #if(CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
  74. # list(APPEND my_EXTRA_PYTHON_LIBRARIES vtkCommon vtkPythonCore)
  75. #endif()
  76. # The current library might not be wrapped. Nevertheless, if one of its dependent library
  77. # is linked using vtkCommon or vtkPythonCore, VTK_LIBRARY_DIRS should be added
  78. # as a link directories.
  79. #if(NOT CTK_BUILD_SHARED_LIBS
  80. # AND CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
  81. # link_directories(${VTK_LIBRARY_DIRS})
  82. #endif()
  83. # Does a header having the expected filename exists ?
  84. string(REGEX REPLACE "^CTK" "ctk" lib_name_lc_ctk ${lib_name})
  85. set(DECORATOR_HEADER ${lib_name_lc_ctk}PythonQtDecorators.h)
  86. set(HAS_DECORATOR FALSE)
  87. if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${DECORATOR_HEADER})
  88. set(HAS_DECORATOR TRUE)
  89. set(DECORATOR_HEADER ${DECORATOR_HEADER})
  90. set_source_files_properties(${DECORATOR_HEADER} WRAP_EXCLUDE)
  91. endif()
  92. #message("HAS_DECORATOR:${HAS_DECORATOR}")
  93. #message("path/to/DECORATOR_HEADER:${CMAKE_CURRENT_SOURCE_DIR}/${DECORATOR_HEADER}")
  94. set(KIT_PYTHONQT_SRCS) # Clear variable
  95. ctkMacroWrapPythonQt(${MY_NAMESPACE} ${lib_name}
  96. KIT_PYTHONQT_SRCS "${MY_SRCS}" ${CTK_WRAP_PYTHONQT_FULL} ${HAS_DECORATOR})
  97. if(HAS_DECORATOR)
  98. list(APPEND KIT_PYTHONQT_SRCS ${DECORATOR_HEADER})
  99. QT4_WRAP_CPP(KIT_PYTHONQT_SRCS ${DECORATOR_HEADER} OPTIONS -f${DECORATOR_HEADER})
  100. endif()
  101. add_library(${lib_name}PythonQt ${MY_WRAPPER_LIBRARY_TYPE} ${KIT_PYTHONQT_SRCS})
  102. target_link_libraries(${lib_name}PythonQt ${lib_name} ${my_EXTRA_PYTHON_LIBRARIES})
  103. if(MY_WRAPPER_LIBRARY_TYPE STREQUAL "STATIC")
  104. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  105. set_target_properties(${lib_name}PythonQt PROPERTIES COMPILE_FLAGS "-fPIC")
  106. endif()
  107. endif()
  108. if(MY_WRAPPER_LIBRARY_TYPE STREQUAL "MODULE")
  109. # Make sure that no prefix is set on the library
  110. set_target_properties(${lib_name}PythonQt PROPERTIES PREFIX "")
  111. # Python extension modules on Windows must have the extension ".pyd"
  112. # instead of ".dll" as of Python 2.5. Older python versions do support
  113. # this suffix.
  114. # See http://docs.python.org/faq/windows.html#is-a-pyd-file-the-same-as-a-dll
  115. if(WIN32 AND NOT CYGWIN)
  116. set_target_properties(${lib_name}PythonQt PROPERTIES SUFFIX ".pyd")
  117. endif()
  118. endif()
  119. set_target_properties(${lib_name}PythonQt PROPERTIES
  120. RUNTIME_OUTPUT_DIRECTORY "${MY_RUNTIME_OUTPUT_DIRECTORY}"
  121. LIBRARY_OUTPUT_DIRECTORY "${MY_LIBRARY_OUTPUT_DIRECTORY}"
  122. ARCHIVE_OUTPUT_DIRECTORY "${MY_ARCHIVE_OUTPUT_DIRECTORY}"
  123. )
  124. # Set labels associated with the target.
  125. set_target_properties(${lib_name}PythonQt PROPERTIES LABELS ${lib_name})
  126. # Update list of libraries wrapped with PythonQt
  127. set(CTK_WRAPPED_LIBRARIES_PYTHONQT
  128. ${CTK_WRAPPED_LIBRARIES_PYTHONQT} ${lib_name}
  129. CACHE INTERNAL "CTK libraries wrapped using PythonQt" FORCE)
  130. # Install rules
  131. if(NOT MY_NO_INSTALL AND MY_WRAPPER_LIBRARY_TYPE STREQUAL "MODULE")
  132. install(TARGETS ${lib_name}PythonQt
  133. RUNTIME DESTINATION ${MY_INSTALL_LIB_DIR} COMPONENT RuntimePlugins
  134. LIBRARY DESTINATION ${MY_INSTALL_LIB_DIR} COMPONENT RuntimePlugins
  135. ARCHIVE DESTINATION ${MY_INSTALL_LIB_DIR} COMPONENT Development)
  136. endif()
  137. endmacro()