ctkMacroWrapPythonQt.cmake 13 KB

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