ctkMacroWrapPythonQt.cmake 13 KB

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