ctkMacroWrapPythonQt.cmake 13 KB

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