ctkMacroWrapPythonQt.cmake 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. # Clear log file
  73. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/ctkMacroWrapPythonQt_log.txt" "")
  74. set(SOURCES_TO_WRAP)
  75. # For each class
  76. foreach(FILE ${SOURCES})
  77. set(skip_wrapping FALSE)
  78. if(NOT skip_wrapping)
  79. # Skip wrapping if file is NOT regular header
  80. if(NOT ${FILE} MATCHES "^.*\\.[hH]$")
  81. set(skip_wrapping TRUE)
  82. ctkMacroWrapPythonQt_log("${FILE}: skipping - Not a regular header")
  83. endif()
  84. endif()
  85. if(NOT skip_wrapping)
  86. # Skip wrapping if file is a pimpl header
  87. if(${FILE} MATCHES "^.*_[pP]\\.[hH]$")
  88. set(skip_wrapping TRUE)
  89. ctkMacroWrapPythonQt_log("${FILE}: skipping - Pimpl header (*._p.h)")
  90. endif()
  91. endif()
  92. if(NOT skip_wrapping)
  93. # Skip wrapping if file should excluded
  94. set(skip_wrapping TRUE)
  95. get_source_file_property(TMP_WRAP_EXCLUDE ${FILE} WRAP_EXCLUDE)
  96. if(NOT TMP_WRAP_EXCLUDE)
  97. set(skip_wrapping FALSE)
  98. endif()
  99. if(skip_wrapping)
  100. ctkMacroWrapPythonQt_log("${FILE}: skipping - WRAP_EXCLUDE")
  101. endif()
  102. endif()
  103. # if we should wrap it
  104. if(NOT skip_wrapping)
  105. # compute the input filename
  106. if(IS_ABSOLUTE FILE)
  107. set(TMP_INPUT ${FILE})
  108. else()
  109. set(TMP_INPUT ${CMAKE_CURRENT_SOURCE_DIR}/${FILE})
  110. endif()
  111. list(APPEND SOURCES_TO_WRAP ${TMP_INPUT})
  112. endif()
  113. endforeach()
  114. # Convert wrapping namespace to subdir
  115. string(REPLACE "." "_" WRAPPING_NAMESPACE_UNDERSCORE ${WRAPPING_NAMESPACE})
  116. # Define wrap type and wrap intermediate directory
  117. set(wrap_int_dir generated_cpp/${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}/)
  118. set(wrapper_module_init_cpp_filename ${wrap_int_dir}${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_module_init.cpp)
  119. # Configure 'ctkMacroWrapPythonQtModuleInit.cpp.in' using TARGET, HAS_DECORATOR and
  120. # WRAPPING_NAMESPACE_UNDERSCORE.
  121. set(TARGET_CONFIG ${TARGET})
  122. configure_file(
  123. ${CTK_CMAKE_DIR}/ctkMacroWrapPythonQtModuleInit.cpp.in
  124. ${wrapper_module_init_cpp_filename}
  125. @ONLY
  126. )
  127. set(extra_args)
  128. if(verbose)
  129. set(extra_args --extra-verbose)
  130. endif()
  131. # Custom command allow to generate ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_init.cpp and
  132. # associated wrappers ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}.cpp
  133. set(wrapper_init_cpp_filename ${wrap_int_dir}${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_init.cpp)
  134. set(wrapper_h_filename ${wrap_int_dir}${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}.h)
  135. add_custom_command(
  136. OUTPUT
  137. ${wrapper_init_cpp_filename}
  138. ${wrapper_h_filename}
  139. DEPENDS
  140. ${SOURCES_TO_WRAP}
  141. ${CTK_CMAKE_DIR}/ctkWrapPythonQt.py
  142. COMMAND ${PYTHON_EXECUTABLE} ${CTK_CMAKE_DIR}/ctkWrapPythonQt.py
  143. --target=${TARGET}
  144. --namespace=${WRAPPING_NAMESPACE}
  145. --output-dir=${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir} ${extra_args}
  146. ${SOURCES_TO_WRAP}
  147. COMMENT "PythonQt Wrapping - Generating ${wrapper_init_cpp_filename}"
  148. VERBATIM
  149. )
  150. QT4_WRAP_CPP(${TARGET}_MOC_CXX ${CMAKE_CURRENT_BINARY_DIR}/${wrapper_h_filename})
  151. # The following files are generated
  152. set_source_files_properties(
  153. ${wrapper_init_cpp_filename}
  154. ${wrapper_h_filename}
  155. ${wrapper_module_init_cpp_filename}
  156. PROPERTIES GENERATED TRUE)
  157. # Create the Init File
  158. set(${SRCS_LIST_NAME}
  159. ${${SRCS_LIST_NAME}}
  160. ${wrapper_init_cpp_filename}
  161. ${wrapper_module_init_cpp_filename}
  162. ${${TARGET}_MOC_CXX}
  163. )
  164. #
  165. # Let's include the headers associated with PythonQt
  166. #
  167. find_package(PythonQt)
  168. if(NOT PYTHONQT_FOUND)
  169. message(FATAL_ERROR "error: PythonQt package is required to build ${TARGET}PythonQt")
  170. endif()
  171. include_directories(${PYTHON_INCLUDE_DIRS} ${PYTHONQT_INCLUDE_DIR})
  172. endmacro()