ctkMacroWrapPythonQt.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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}\nPython exe: ${python_exe}\nPython lib path: ${python_library_path}")
  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. # compute the input filename
  185. if(IS_ABSOLUTE FILE)
  186. set(TMP_INPUT ${FILE})
  187. else()
  188. set(TMP_INPUT ${CMAKE_CURRENT_SOURCE_DIR}/${FILE})
  189. endif()
  190. list(APPEND SOURCES_TO_WRAP ${TMP_INPUT})
  191. endif()
  192. endforeach()
  193. # PythonQtGenerator expects a colon ':' separated list
  194. set(INCLUDE_DIRS_TO_WRAP)
  195. foreach(include ${CTK_BASE_INCLUDE_DIRS})
  196. set(INCLUDE_DIRS_TO_WRAP "${INCLUDE_DIRS_TO_WRAP}:${include}")
  197. endforeach()
  198. # Prepare custom_command argument
  199. set(SOURCES_TO_WRAP_ARG)
  200. foreach(source ${SOURCES_TO_WRAP})
  201. set(SOURCES_TO_WRAP_ARG "${SOURCES_TO_WRAP_ARG}^^${source}")
  202. endforeach()
  203. # Define wrap type and wrap intermediate directory
  204. set(wrap_type "Light")
  205. set(wrap_int_dir generated_cpp/${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}/)
  206. set(extra_files )
  207. if(${IS_WRAP_FULL})
  208. set(wrap_type "Full")
  209. set(extra_files ${wrap_int_dir}ctkPythonQt_${TARGET}_masterinclude.h)
  210. endif()
  211. #message("wrap_type:${wrap_type} - wrap_int_dir:${wrap_int_dir}")
  212. # Create intermediate output directory
  213. execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir})
  214. # On Windows, to avoid "too long input" error, dump INCLUDE_DIRS_TO_WRAP into a file
  215. if(WIN32)
  216. # File containing the moc flags
  217. set(include_dirs_to_wrap_filename includeDirsToWrap_${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}.txt)
  218. set(include_dirs_to_wrap_file ${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir}${include_dirs_to_wrap_filename})
  219. file(WRITE ${include_dirs_to_wrap_file} ${INCLUDE_DIRS_TO_WRAP})
  220. # The arg passed to the custom command will be the file containing the list of include dirs to wrap
  221. set(INCLUDE_DIRS_TO_WRAP ${include_dirs_to_wrap_file})
  222. endif()
  223. set(wrapper_init_cpp_filename ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_init.cpp)
  224. set(wrapper_init_cpp_file ${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir}${wrapper_init_cpp_filename})
  225. set(wrapper_module_init_cpp_filename ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_module_init.cpp)
  226. set(wrapper_module_init_cpp_file ${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir}${wrapper_module_init_cpp_filename})
  227. # Custom command allow to generate ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_init.cpp and
  228. # associated wrappers ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}{0-N}.cpp
  229. add_custom_command(
  230. OUTPUT
  231. ${wrap_int_dir}${wrapper_init_cpp_filename}
  232. ${wrap_int_dir}${wrapper_module_init_cpp_filename}
  233. ${extra_files}
  234. DEPENDS
  235. ${pythonqtgenerator_executable_depends}
  236. ${SOURCES_TO_WRAP}
  237. ${CTK_CMAKE_DIR}/ctkScriptWrapPythonQt_${wrap_type}.cmake
  238. ${CTK_CMAKE_DIR}/ctkMacroWrapPythonQtModuleInit.cpp.in
  239. COMMAND ${CMAKE_COMMAND}
  240. -DPYTHONQTGENERATOR_EXECUTABLE:FILEPATH=${PYTHONQTGENERATOR_EXECUTABLE}
  241. -DPYTHON_EXECUTABLE:FILEPATH=${PYTHON_EXECUTABLE}
  242. -DPYTHON_LIBRARY_PATH:PATH=${PYTHON_LIBRARY_PATH}
  243. -DWRAPPING_NAMESPACE:STRING=${WRAPPING_NAMESPACE}
  244. -DTARGET:STRING=${TARGET}
  245. -DSOURCES:STRING=${SOURCES_TO_WRAP_ARG}
  246. -DINCLUDE_DIRS:STRING=${INCLUDE_DIRS_TO_WRAP}
  247. -DOUTPUT_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}
  248. -DWRAP_INT_DIR:STRING=${wrap_int_dir}
  249. -DQT_QMAKE_EXECUTABLE:FILEPATH=${QT_QMAKE_EXECUTABLE}
  250. -DHAS_DECORATOR:BOOL=${HAS_DECORATOR}
  251. -P ${CTK_CMAKE_DIR}/ctkScriptWrapPythonQt_${wrap_type}.cmake
  252. COMMENT "PythonQt ${wrap_type} Wrapping - Generating ${wrapper_init_cpp_filename}"
  253. VERBATIM
  254. )
  255. # Clear variable
  256. set(moc_flags)
  257. # Grab moc flags
  258. QT4_GET_MOC_FLAGS(moc_flags)
  259. # Prepare custom_command argument
  260. set(moc_flags_arg)
  261. foreach(flag ${moc_flags})
  262. set(moc_flags_arg "${moc_flags_arg}^^${flag}")
  263. endforeach()
  264. # On Windows, to avoid "too long input" error, dump moc flags.
  265. if(WIN32)
  266. # File containing the moc flags
  267. set(wrapper_moc_flags_filename mocflags_${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_all.txt)
  268. set(wrapper_master_moc_flags_file ${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir}${wrapper_moc_flags_filename})
  269. file(WRITE ${wrapper_master_moc_flags_file} ${moc_flags_arg})
  270. # The arg passed to the custom command will be the file containing the list of moc flags
  271. set(moc_flags_arg ${wrapper_master_moc_flags_file})
  272. endif()
  273. # File to run through moc
  274. set(wrapper_master_moc_filename moc_${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_all.cpp)
  275. set(wrapper_master_moc_file ${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir}${wrapper_master_moc_filename})
  276. # Custom command allowing to call moc to process the wrapper headers
  277. add_custom_command(
  278. OUTPUT ${wrap_int_dir}${wrapper_master_moc_filename}
  279. DEPENDS
  280. ${wrap_int_dir}${wrapper_init_cpp_filename}
  281. ${wrap_int_dir}${wrapper_module_init_cpp_filename}
  282. ${extra_files} ${CTK_CMAKE_DIR}/ctkScriptMocPythonQtWrapper.cmake
  283. COMMAND ${CMAKE_COMMAND}
  284. -DWRAPPING_NAMESPACE:STRING=${WRAPPING_NAMESPACE}
  285. -DTARGET:STRING=${TARGET}
  286. -DMOC_FLAGS:STRING=${moc_flags_arg}
  287. -DWRAP_INT_DIR:STRING=${wrap_int_dir}
  288. -DWRAPPER_MASTER_MOC_FILE:STRING=${wrapper_master_moc_file}
  289. -DOUTPUT_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}
  290. -DQT_MOC_EXECUTABLE:FILEPATH=${QT_MOC_EXECUTABLE}
  291. -P ${CTK_CMAKE_DIR}/ctkScriptMocPythonQtWrapper.cmake
  292. COMMENT "PythonQt ${wrap_type} Wrapping - Moc'ing ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET} wrapper headers"
  293. VERBATIM
  294. )
  295. # The following files are generated
  296. set_source_files_properties(
  297. ${wrap_int_dir}${wrapper_init_cpp_filename}
  298. ${wrap_int_dir}${wrapper_module_init_cpp_filename}
  299. ${wrap_int_dir}${wrapper_master_moc_filename}
  300. PROPERTIES GENERATED TRUE)
  301. # Create the Init File
  302. set(${SRCS_LIST_NAME}
  303. ${${SRCS_LIST_NAME}}
  304. ${wrap_int_dir}${wrapper_init_cpp_filename}
  305. ${wrap_int_dir}${wrapper_module_init_cpp_filename}
  306. ${wrap_int_dir}${wrapper_master_moc_filename}
  307. )
  308. #
  309. # Let's include the headers associated with PythonQt
  310. #
  311. find_package(PythonQt)
  312. if(NOT PYTHONQT_FOUND)
  313. message(FATAL_ERROR "error: PythonQt package is required to build ${TARGET}PythonQt")
  314. endif()
  315. include_directories(${PYTHON_INCLUDE_DIRS} ${PYTHONQT_INCLUDE_DIR})
  316. endmacro()