ctkMacroWrapPythonQt.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 (See function reSearchFile)
  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. include(${CTK_CMAKE_DIR}/ctkMacroSetPaths.cmake)
  60. #!
  61. #! Convenient function allowing to invoke re.search(regex, string) using the given interpreter.
  62. #! Note that is_matching will be set to True if there is a match
  63. #!
  64. #! \ingroup CMakeUtilities
  65. function(ctkMacroWrapPythonQt_reSearchFile python_exe python_library_path regex file is_matching)
  66. set(python_cmd "import re
  67. f = open('${file}', 'r')
  68. res = re.search('${regex}', f.read(), re.MULTILINE)
  69. if res == None: print 'FALSE'
  70. else: print 'TRUE'
  71. ")
  72. #message("python_cmd: ${python_cmd}")
  73. ctkMacroSetPaths("${python_library_path}")
  74. execute_process(
  75. COMMAND ${python_exe} -c ${python_cmd}
  76. RESULT_VARIABLE result
  77. OUTPUT_VARIABLE output
  78. ERROR_VARIABLE error
  79. OUTPUT_STRIP_TRAILING_WHITESPACE
  80. )
  81. if(result)
  82. message(FATAL_ERROR "reSearchFile - Problem with regex: ${regex}\n${error}\nPython exe: ${python_exe}\nPython lib path: ${python_library_path}")
  83. endif()
  84. set(is_matching ${output} PARENT_SCOPE)
  85. endfunction()
  86. #! \ingroup CMakeUtilities
  87. macro(ctkMacroWrapPythonQt WRAPPING_NAMESPACE TARGET SRCS_LIST_NAME SOURCES IS_WRAP_FULL HAS_DECORATOR)
  88. # Sanity check
  89. if(IS_WRAP_FULL)
  90. message(FATAL_ERROR "IS_WRAP_FULL option is not supported anymore. See https://github.com/commontk/CTK/issues/449")
  91. endif()
  92. # TODO: this find package seems not to work when called form a superbuild, but the call is needed
  93. # in general to find the python interpreter. In CTK, the toplevel CMakeLists.txt does the find
  94. # package so this is a no-op. Other uses of this file may need to have this call so it is still enabled.
  95. find_package(PythonInterp)
  96. if(NOT PYTHONINTERP_FOUND)
  97. message(FATAL_ERROR "PYTHON_EXECUTABLE not specified or inexistent when calling ctkMacroWrapPythonQt")
  98. endif()
  99. # Extract python lib path
  100. get_filename_component(PYTHON_DIR_PATH ${PYTHON_EXECUTABLE} PATH)
  101. set(PYTHON_LIBRARY_PATH ${PYTHON_DIR_PATH}/../lib)
  102. if(WIN32)
  103. set(PYTHON_LIBRARY_PATH ${PYTHON_DIR_PATH})
  104. endif()
  105. # Clear log file
  106. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/ctkMacroWrapPythonQt_log.txt" "")
  107. # Convert wrapping namespace to subdir
  108. string(REPLACE "." "_" WRAPPING_NAMESPACE_UNDERSCORE ${WRAPPING_NAMESPACE})
  109. set(SOURCES_TO_WRAP)
  110. # For each class
  111. foreach(FILE ${SOURCES})
  112. set(skip_wrapping FALSE)
  113. if(NOT skip_wrapping)
  114. # Skip wrapping if file is NOT regular header
  115. if(NOT ${FILE} MATCHES "^.*\\.[hH]$")
  116. set(skip_wrapping TRUE)
  117. ctkMacroWrapPythonQt_log("${FILE}: skipping - Not a regular header")
  118. endif()
  119. endif()
  120. if(NOT skip_wrapping)
  121. # Skip wrapping if file is a pimpl header
  122. if(${FILE} MATCHES "^.*_[pP]\\.[hH]$")
  123. set(skip_wrapping TRUE)
  124. ctkMacroWrapPythonQt_log("${FILE}: skipping - Pimpl header (*._p.h)")
  125. endif()
  126. endif()
  127. if(NOT skip_wrapping)
  128. # Skip wrapping if file should excluded
  129. set(skip_wrapping TRUE)
  130. get_source_file_property(TMP_WRAP_EXCLUDE ${FILE} WRAP_EXCLUDE)
  131. if(NOT TMP_WRAP_EXCLUDE)
  132. set(skip_wrapping FALSE)
  133. endif()
  134. if(skip_wrapping)
  135. ctkMacroWrapPythonQt_log("${FILE}: skipping - WRAP_EXCLUDE")
  136. endif()
  137. endif()
  138. # what is the filename without the extension
  139. get_filename_component(TMP_FILENAME ${FILE} NAME_WE)
  140. # Extract classname - NOTE: We assume the filename matches the associated class
  141. set(className ${TMP_FILENAME})
  142. if(NOT skip_wrapping)
  143. # Skip wrapping if file do NOT contain Q_OBJECT
  144. file(READ ${CMAKE_CURRENT_SOURCE_DIR}/${FILE} file_content)
  145. if(NOT "${file_content}" MATCHES "Q_OBJECT")
  146. set(skip_wrapping TRUE)
  147. ctkMacroWrapPythonQt_log("${FILE}: skipping - No Q_OBJECT macro")
  148. endif()
  149. endif()
  150. if(NOT skip_wrapping)
  151. # Skip wrapping if constructor doesn't match:
  152. # my_class()
  153. # my_class(QObject* newParent ...)
  154. # my_class(QWidget* newParent ...)
  155. # Constructor with either QWidget or QObject as first parameter
  156. set(regex "[^~]${className}[\\s\\n]*\\([\\s\\n]*((QObject|QWidget)[\\s\\n]*\\*[\\s\\n]*\\w+[\\s\\n]*(\\=[\\s\\n]*(0|NULL)|,.*\\=.*\\)|\\)|\\)))")
  157. ctkMacroWrapPythonQt_reSearchfile(${PYTHON_EXECUTABLE} ${PYTHON_LIBRARY_PATH}
  158. ${regex} ${CMAKE_CURRENT_SOURCE_DIR}/${FILE} is_matching)
  159. if(NOT is_matching)
  160. set(skip_wrapping TRUE)
  161. ctkMacroWrapPythonQt_log("${FILE}: skipping - Missing expected constructor signature")
  162. endif()
  163. endif()
  164. if(NOT skip_wrapping)
  165. # Skip wrapping if object has a virtual pure method
  166. # "x3b" is the unicode for semicolon
  167. set(regex "virtual[\\w\\n\\s\\*\\(\\)]+\\=[\\s\\n]*(0|NULL)[\\s\\n]*\\x3b")
  168. ctkMacroWrapPythonQt_reSearchfile(${PYTHON_EXECUTABLE} ${PYTHON_LIBRARY_PATH}
  169. ${regex} ${CMAKE_CURRENT_SOURCE_DIR}/${FILE} is_matching)
  170. if(is_matching)
  171. set(skip_wrapping TRUE)
  172. ctkMacroWrapPythonQt_log("${FILE}: skipping - Contains a virtual pure method")
  173. endif()
  174. endif()
  175. # if we should wrap it
  176. IF (NOT skip_wrapping)
  177. # compute the input filename
  178. if(IS_ABSOLUTE FILE)
  179. set(TMP_INPUT ${FILE})
  180. else()
  181. set(TMP_INPUT ${CMAKE_CURRENT_SOURCE_DIR}/${FILE})
  182. endif()
  183. list(APPEND SOURCES_TO_WRAP ${TMP_INPUT})
  184. endif()
  185. endforeach()
  186. # PythonQtGenerator expects a colon ':' separated list
  187. set(INCLUDE_DIRS_TO_WRAP)
  188. foreach(include ${CTK_BASE_INCLUDE_DIRS})
  189. set(INCLUDE_DIRS_TO_WRAP "${INCLUDE_DIRS_TO_WRAP}:${include}")
  190. endforeach()
  191. # Prepare custom_command argument
  192. set(SOURCES_TO_WRAP_ARG)
  193. foreach(source ${SOURCES_TO_WRAP})
  194. set(SOURCES_TO_WRAP_ARG "${SOURCES_TO_WRAP_ARG}^^${source}")
  195. endforeach()
  196. # Define wrap type and wrap intermediate directory
  197. set(wrap_int_dir generated_cpp/${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}/)
  198. #message("wrap_int_dir:${wrap_int_dir}")
  199. # Create intermediate output directory
  200. execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir})
  201. # On Windows, to avoid "too long input" error, dump INCLUDE_DIRS_TO_WRAP into a file
  202. if(WIN32)
  203. # File containing the moc flags
  204. set(include_dirs_to_wrap_filename includeDirsToWrap_${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}.txt)
  205. set(include_dirs_to_wrap_file ${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir}${include_dirs_to_wrap_filename})
  206. file(WRITE ${include_dirs_to_wrap_file} ${INCLUDE_DIRS_TO_WRAP})
  207. # The arg passed to the custom command will be the file containing the list of include dirs to wrap
  208. set(INCLUDE_DIRS_TO_WRAP ${include_dirs_to_wrap_file})
  209. endif()
  210. set(wrapper_init_cpp_filename ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_init.cpp)
  211. set(wrapper_init_cpp_file ${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir}${wrapper_init_cpp_filename})
  212. set(wrapper_module_init_cpp_filename ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_module_init.cpp)
  213. set(wrapper_module_init_cpp_file ${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir}${wrapper_module_init_cpp_filename})
  214. # Custom command allow to generate ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_init.cpp and
  215. # associated wrappers ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}{0-N}.cpp
  216. add_custom_command(
  217. OUTPUT
  218. ${wrap_int_dir}${wrapper_init_cpp_filename}
  219. ${wrap_int_dir}${wrapper_module_init_cpp_filename}
  220. DEPENDS
  221. ${SOURCES_TO_WRAP}
  222. ${CTK_CMAKE_DIR}/ctkScriptWrapPythonQt_Light.cmake
  223. ${CTK_CMAKE_DIR}/ctkMacroWrapPythonQtModuleInit.cpp.in
  224. COMMAND ${CMAKE_COMMAND}
  225. -DPYTHONQTGENERATOR_EXECUTABLE:FILEPATH=${PYTHONQTGENERATOR_EXECUTABLE}
  226. -DPYTHON_EXECUTABLE:FILEPATH=${PYTHON_EXECUTABLE}
  227. -DPYTHON_LIBRARY_PATH:PATH=${PYTHON_LIBRARY_PATH}
  228. -DWRAPPING_NAMESPACE:STRING=${WRAPPING_NAMESPACE}
  229. -DTARGET:STRING=${TARGET}
  230. -DSOURCES:STRING=${SOURCES_TO_WRAP_ARG}
  231. -DINCLUDE_DIRS:STRING=${INCLUDE_DIRS_TO_WRAP}
  232. -DOUTPUT_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}
  233. -DWRAP_INT_DIR:STRING=${wrap_int_dir}
  234. -DQT_QMAKE_EXECUTABLE:FILEPATH=${QT_QMAKE_EXECUTABLE}
  235. -DHAS_DECORATOR:BOOL=${HAS_DECORATOR}
  236. -P ${CTK_CMAKE_DIR}/ctkScriptWrapPythonQt_Light.cmake
  237. COMMENT "PythonQt Wrapping - Generating ${wrapper_init_cpp_filename}"
  238. VERBATIM
  239. )
  240. # Clear variable
  241. set(moc_flags)
  242. # Grab moc flags
  243. QT4_GET_MOC_FLAGS(moc_flags)
  244. # Prepare custom_command argument
  245. set(moc_flags_arg)
  246. foreach(flag ${moc_flags})
  247. set(moc_flags_arg "${moc_flags_arg}^^${flag}")
  248. endforeach()
  249. # On Windows, to avoid "too long input" error, dump moc flags.
  250. if(WIN32)
  251. # File containing the moc flags
  252. set(wrapper_moc_flags_filename mocflags_${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_all.txt)
  253. set(wrapper_master_moc_flags_file ${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir}${wrapper_moc_flags_filename})
  254. file(WRITE ${wrapper_master_moc_flags_file} ${moc_flags_arg})
  255. # The arg passed to the custom command will be the file containing the list of moc flags
  256. set(moc_flags_arg ${wrapper_master_moc_flags_file})
  257. endif()
  258. # File to run through moc
  259. set(wrapper_master_moc_filename moc_${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_all.cpp)
  260. set(wrapper_master_moc_file ${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir}${wrapper_master_moc_filename})
  261. # Custom command allowing to call moc to process the wrapper headers
  262. add_custom_command(
  263. OUTPUT ${wrap_int_dir}${wrapper_master_moc_filename}
  264. DEPENDS
  265. ${wrap_int_dir}${wrapper_init_cpp_filename}
  266. ${wrap_int_dir}${wrapper_module_init_cpp_filename}
  267. ${extra_files} ${CTK_CMAKE_DIR}/ctkScriptMocPythonQtWrapper.cmake
  268. COMMAND ${CMAKE_COMMAND}
  269. -DWRAPPING_NAMESPACE:STRING=${WRAPPING_NAMESPACE}
  270. -DTARGET:STRING=${TARGET}
  271. -DMOC_FLAGS:STRING=${moc_flags_arg}
  272. -DWRAP_INT_DIR:STRING=${wrap_int_dir}
  273. -DWRAPPER_MASTER_MOC_FILE:STRING=${wrapper_master_moc_file}
  274. -DOUTPUT_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}
  275. -DQT_MOC_EXECUTABLE:FILEPATH=${QT_MOC_EXECUTABLE}
  276. -P ${CTK_CMAKE_DIR}/ctkScriptMocPythonQtWrapper.cmake
  277. COMMENT "PythonQt Wrapping - Moc'ing ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET} wrapper headers"
  278. VERBATIM
  279. )
  280. # The following files are generated
  281. set_source_files_properties(
  282. ${wrap_int_dir}${wrapper_init_cpp_filename}
  283. ${wrap_int_dir}${wrapper_module_init_cpp_filename}
  284. ${wrap_int_dir}${wrapper_master_moc_filename}
  285. PROPERTIES GENERATED TRUE)
  286. # Create the Init File
  287. set(${SRCS_LIST_NAME}
  288. ${${SRCS_LIST_NAME}}
  289. ${wrap_int_dir}${wrapper_init_cpp_filename}
  290. ${wrap_int_dir}${wrapper_module_init_cpp_filename}
  291. ${wrap_int_dir}${wrapper_master_moc_filename}
  292. )
  293. #
  294. # Let's include the headers associated with PythonQt
  295. #
  296. find_package(PythonQt)
  297. if(NOT PYTHONQT_FOUND)
  298. message(FATAL_ERROR "error: PythonQt package is required to build ${TARGET}PythonQt")
  299. endif()
  300. include_directories(${PYTHON_INCLUDE_DIRS} ${PYTHONQT_INCLUDE_DIR})
  301. endmacro()