ctkMacroWrapPythonQt.cmake 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. # ctkMacroWrapPythonQt
  22. #
  23. #!
  24. #! Depends on:
  25. #! PythonQt
  26. #! PythonInterp
  27. #!
  28. #!
  29. #! The different parameters are:
  30. #!
  31. #! WRAPPING_NAMESPACE: Namespace that should contain the library. For example: org.commontk
  32. #!
  33. #! TARGET ...........: Name of the wrapped library. For example: CTKWidget
  34. #!
  35. #! SRCS_LIST_NAME ...: Name of the variable that should contain the generated wrapper source.
  36. #! For example: KIT_PYTHONQT_SRCS
  37. #!
  38. #! SOURCES ..........: List of source files that should be wrapped.
  39. #!
  40. #! HAS_DECORATOR ....: Indicate if a custom PythonQt decorator header is expected.
  41. #!
  42. #!
  43. #! LOG FILE:
  44. #! File ctkMacroWrapPythonQt_log.txt will be created in the current directory.
  45. #! It will contain the list of file and the reason why a given class hasn't been wrapped.
  46. #!
  47. set(verbose 0)
  48. #!
  49. #! Convenient function allowing to log the reason why a given class hasn't been wrapped
  50. #! If verbose=1, it will also be displayed on the standard output
  51. #!
  52. #! \ingroup CMakeUtilities
  53. function(ctkMacroWrapPythonQt_log msg)
  54. if(verbose)
  55. message(${msg})
  56. endif()
  57. file(APPEND "${CMAKE_CURRENT_BINARY_DIR}/ctkMacroWrapPythonQt_log.txt" "${msg}\n")
  58. endfunction()
  59. #! \ingroup CMakeUtilities
  60. macro(ctkMacroWrapPythonQt WRAPPING_NAMESPACE TARGET SRCS_LIST_NAME SOURCES IS_WRAP_FULL HAS_DECORATOR)
  61. # Sanity check
  62. if(IS_WRAP_FULL)
  63. message(FATAL_ERROR "IS_WRAP_FULL option is not supported anymore. See https://github.com/commontk/CTK/issues/449")
  64. endif()
  65. # TODO: this find package seems not to work when called form a superbuild, but the call is needed
  66. # in general to find the python interpreter. In CTK, the toplevel CMakeLists.txt does the find
  67. # package so this is a no-op. Other uses of this file may need to have this call so it is still enabled.
  68. find_package(PythonInterp)
  69. if(NOT PYTHONINTERP_FOUND)
  70. message(FATAL_ERROR "PYTHON_EXECUTABLE not specified or inexistent when calling ctkMacroWrapPythonQt")
  71. endif()
  72. # Extract python lib path
  73. get_filename_component(PYTHON_DIR_PATH ${PYTHON_EXECUTABLE} PATH)
  74. set(PYTHON_LIBRARY_PATH ${PYTHON_DIR_PATH}/../lib)
  75. if(WIN32)
  76. set(PYTHON_LIBRARY_PATH ${PYTHON_DIR_PATH})
  77. endif()
  78. # Clear log file
  79. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/ctkMacroWrapPythonQt_log.txt" "")
  80. # Convert wrapping namespace to subdir
  81. string(REPLACE "." "_" WRAPPING_NAMESPACE_UNDERSCORE ${WRAPPING_NAMESPACE})
  82. set(SOURCES_TO_WRAP)
  83. # For each class
  84. foreach(FILE ${SOURCES})
  85. set(skip_wrapping FALSE)
  86. if(NOT skip_wrapping)
  87. # Skip wrapping if file is NOT regular header
  88. if(NOT ${FILE} MATCHES "^.*\\.[hH]$")
  89. set(skip_wrapping TRUE)
  90. ctkMacroWrapPythonQt_log("${FILE}: skipping - Not a regular header")
  91. endif()
  92. endif()
  93. if(NOT skip_wrapping)
  94. # Skip wrapping if file is a pimpl header
  95. if(${FILE} MATCHES "^.*_[pP]\\.[hH]$")
  96. set(skip_wrapping TRUE)
  97. ctkMacroWrapPythonQt_log("${FILE}: skipping - Pimpl header (*._p.h)")
  98. endif()
  99. endif()
  100. if(NOT skip_wrapping)
  101. # Skip wrapping if file should excluded
  102. set(skip_wrapping TRUE)
  103. get_source_file_property(TMP_WRAP_EXCLUDE ${FILE} WRAP_EXCLUDE)
  104. if(NOT TMP_WRAP_EXCLUDE)
  105. set(skip_wrapping FALSE)
  106. endif()
  107. if(skip_wrapping)
  108. ctkMacroWrapPythonQt_log("${FILE}: skipping - WRAP_EXCLUDE")
  109. endif()
  110. endif()
  111. # if we should wrap it
  112. if(NOT skip_wrapping)
  113. # compute the input filename
  114. if(IS_ABSOLUTE FILE)
  115. set(TMP_INPUT ${FILE})
  116. else()
  117. set(TMP_INPUT ${CMAKE_CURRENT_SOURCE_DIR}/${FILE})
  118. endif()
  119. list(APPEND SOURCES_TO_WRAP ${TMP_INPUT})
  120. endif()
  121. endforeach()
  122. # Prepare custom_command argument
  123. set(SOURCES_TO_WRAP_ARG)
  124. foreach(source ${SOURCES_TO_WRAP})
  125. set(SOURCES_TO_WRAP_ARG "${SOURCES_TO_WRAP_ARG}^^${source}")
  126. endforeach()
  127. # Define wrap type and wrap intermediate directory
  128. set(wrap_int_dir generated_cpp/${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}/)
  129. #message("wrap_int_dir:${wrap_int_dir}")
  130. set(wrapper_init_cpp_filename ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_init.cpp)
  131. set(wrapper_init_cpp_file ${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir}${wrapper_init_cpp_filename})
  132. set(wrapper_module_init_cpp_filename ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_module_init.cpp)
  133. set(wrapper_module_init_cpp_file ${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir}${wrapper_module_init_cpp_filename})
  134. # Custom command allow to generate ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_init.cpp and
  135. # associated wrappers ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}{0-N}.cpp
  136. add_custom_command(
  137. OUTPUT
  138. ${wrap_int_dir}${wrapper_init_cpp_filename}
  139. ${wrap_int_dir}${wrapper_module_init_cpp_filename}
  140. DEPENDS
  141. ${SOURCES_TO_WRAP}
  142. ${CTK_CMAKE_DIR}/ctkScriptWrapPythonQt_Light.cmake
  143. ${CTK_CMAKE_DIR}/ctkMacroWrapPythonQtModuleInit.cpp.in
  144. COMMAND ${CMAKE_COMMAND}
  145. -DPYTHON_EXECUTABLE:FILEPATH=${PYTHON_EXECUTABLE}
  146. -DPYTHON_LIBRARY_PATH:PATH=${PYTHON_LIBRARY_PATH}
  147. -DWRAPPING_SCRIPT:FILEPATH=${CTK_CMAKE_DIR}/ctkWrapPythonQt.py
  148. -DWRAPPING_NAMESPACE:STRING=${WRAPPING_NAMESPACE}
  149. -DTARGET:STRING=${TARGET}
  150. -DSOURCES:STRING=${SOURCES_TO_WRAP_ARG}
  151. -DOUTPUT_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir}
  152. -DHAS_DECORATOR:BOOL=${HAS_DECORATOR}
  153. -P ${CTK_CMAKE_DIR}/ctkScriptWrapPythonQt_Light.cmake
  154. COMMENT "PythonQt Wrapping - Generating ${wrapper_init_cpp_filename}"
  155. VERBATIM
  156. )
  157. # Clear variable
  158. set(moc_flags)
  159. # Grab moc flags
  160. QT4_GET_MOC_FLAGS(moc_flags)
  161. # Prepare custom_command argument
  162. set(moc_flags_arg)
  163. foreach(flag ${moc_flags})
  164. set(moc_flags_arg "${moc_flags_arg}^^${flag}")
  165. endforeach()
  166. # On Windows, to avoid "too long input" error, dump moc flags.
  167. if(WIN32)
  168. # File containing the moc flags
  169. set(wrapper_moc_flags_filename mocflags_${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_all.txt)
  170. set(wrapper_master_moc_flags_file ${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir}${wrapper_moc_flags_filename})
  171. file(WRITE ${wrapper_master_moc_flags_file} ${moc_flags_arg})
  172. # The arg passed to the custom command will be the file containing the list of moc flags
  173. set(moc_flags_arg ${wrapper_master_moc_flags_file})
  174. endif()
  175. # File to run through moc
  176. set(wrapper_master_moc_filename moc_${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_all.cpp)
  177. set(wrapper_master_moc_file ${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir}${wrapper_master_moc_filename})
  178. # Custom command allowing to call moc to process the wrapper headers
  179. add_custom_command(
  180. OUTPUT ${wrap_int_dir}${wrapper_master_moc_filename}
  181. DEPENDS
  182. ${wrap_int_dir}${wrapper_init_cpp_filename}
  183. ${wrap_int_dir}${wrapper_module_init_cpp_filename}
  184. ${extra_files} ${CTK_CMAKE_DIR}/ctkScriptMocPythonQtWrapper.cmake
  185. COMMAND ${CMAKE_COMMAND}
  186. -DWRAPPING_NAMESPACE:STRING=${WRAPPING_NAMESPACE}
  187. -DTARGET:STRING=${TARGET}
  188. -DMOC_FLAGS:STRING=${moc_flags_arg}
  189. -DWRAP_INT_DIR:STRING=${wrap_int_dir}
  190. -DWRAPPER_MASTER_MOC_FILE:STRING=${wrapper_master_moc_file}
  191. -DOUTPUT_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}
  192. -DQT_MOC_EXECUTABLE:FILEPATH=${QT_MOC_EXECUTABLE}
  193. -P ${CTK_CMAKE_DIR}/ctkScriptMocPythonQtWrapper.cmake
  194. COMMENT "PythonQt Wrapping - Moc'ing ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET} wrapper headers"
  195. VERBATIM
  196. )
  197. # The following files are generated
  198. set_source_files_properties(
  199. ${wrap_int_dir}${wrapper_init_cpp_filename}
  200. ${wrap_int_dir}${wrapper_module_init_cpp_filename}
  201. ${wrap_int_dir}${wrapper_master_moc_filename}
  202. PROPERTIES GENERATED TRUE)
  203. # Create the Init File
  204. set(${SRCS_LIST_NAME}
  205. ${${SRCS_LIST_NAME}}
  206. ${wrap_int_dir}${wrapper_init_cpp_filename}
  207. ${wrap_int_dir}${wrapper_module_init_cpp_filename}
  208. ${wrap_int_dir}${wrapper_master_moc_filename}
  209. )
  210. #
  211. # Let's include the headers associated with PythonQt
  212. #
  213. find_package(PythonQt)
  214. if(NOT PYTHONQT_FOUND)
  215. message(FATAL_ERROR "error: PythonQt package is required to build ${TARGET}PythonQt")
  216. endif()
  217. include_directories(${PYTHON_INCLUDE_DIRS} ${PYTHONQT_INCLUDE_DIR})
  218. endmacro()