ctkScriptWrapPythonQt_Light.cmake 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. # ctkScriptWrapPythonQt_Light
  22. #
  23. #
  24. # Depends on:
  25. # CTK/CMake/ctkMacroWrapPythonQt.cmake
  26. #
  27. #
  28. # This script should be invoked either as a CUSTOM_COMMAND
  29. # or from the command line using the following syntax:
  30. #
  31. # cmake -DWRAPPING_NAMESPACE:STRING=org.commontk -DTARGET:STRING=MyLib
  32. # -DSOURCES:STRING="file1^^file2" -DINCLUDE_DIRS:STRING=/path1:/path2
  33. # -DWRAP_INT_DIR:STRING=subir/subir/
  34. # -DOUTPUT_DIR:PATH=/path -DQT_QMAKE_EXECUTABLE:PATH=/path/to/qt/qmake
  35. # -DPYTHON_EXECUTABLE:FILEPATH=/path/to/python
  36. # -DPYTHON_LIBRARY_PATH:PATH=/path/to/pythonlib
  37. # -DHAS_DECORATOR:BOOL=True
  38. # -P ctkScriptWrapPythonQt_Light.cmake
  39. #
  40. #
  41. # LOG FILE:
  42. # File ctkScriptWrapPythonQt_Light_log.txt will be created in the current directory.
  43. # It will contain the list of class and the constructor signature that will be wrapped.
  44. #
  45. set(verbose 0)
  46. #
  47. # Convenient function allowing to log the reason why a given class hasn't been wrapped
  48. # If verbose=1, it will also be displayed on the standard output
  49. #
  50. function(log msg)
  51. if(verbose)
  52. message(${msg})
  53. endif()
  54. file(APPEND "${CMAKE_CURRENT_BINARY_DIR}/ctkScriptWrapPythonQt_Light_log.txt" "${msg}\n")
  55. endfunction()
  56. #
  57. # Convenient function allowing to invoke re.search(regex, string) using the given interpreter.
  58. # Note that is_matching will be set to True if there is a match
  59. #
  60. function(reSearchFile python_exe python_library_path regex file is_matching)
  61. set(python_cmd "import re\; f = open('${file}', 'r')\;
  62. res = re.search\(\"${regex}\", f.read(), re.MULTILINE\)\;
  63. if res == None: print \"FALSE\"
  64. else: print \"TRUE\"
  65. ")
  66. #message("python_cmd: ${python_cmd}")
  67. if(WIN32)
  68. set(ENV{PATH} ${python_library_path};$ENV{PATH})
  69. elseif(APPLE)
  70. set(ENV{DYLD_LIBRARY_PATH} ${python_library_path}:$ENV{DYLD_LIBRARY_PATH})
  71. else()
  72. set(ENV{LD_LIBRARY_PATH} ${python_library_path}:$ENV{LD_LIBRARY_PATH})
  73. endif()
  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}")
  83. endif()
  84. #message(${output})
  85. set(is_matching ${output} PARENT_SCOPE)
  86. endfunction()
  87. if(NOT DEFINED CMAKE_CURRENT_LIST_DIR)
  88. get_filename_component(CMAKE_CURRENT_LIST_DIR ${CMAKE_CURRENT_LIST_FILE} PATH)
  89. endif()
  90. if(NOT DEFINED CMAKE_CURRENT_LIST_FILENAME)
  91. get_filename_component(CMAKE_CURRENT_LIST_FILENAME ${CMAKE_CURRENT_LIST_FILE} NAME)
  92. endif()
  93. # Check for non-defined var
  94. foreach(var WRAPPING_NAMESPACE TARGET SOURCES INCLUDE_DIRS WRAP_INT_DIR HAS_DECORATOR)
  95. if(NOT DEFINED ${var})
  96. message(FATAL_ERROR "${var} not specified when calling ctkScriptWrapPythonQt")
  97. endif()
  98. endforeach()
  99. # Check for non-existing ${var}
  100. foreach(var QT_QMAKE_EXECUTABLE OUTPUT_DIR PYTHON_EXECUTABLE PYTHON_LIBRARY_PATH)
  101. if(NOT EXISTS ${${var}})
  102. message(FATAL_ERROR "Failed to find ${var}=\"${${var}}\" when calling ctkScriptWrapPythonQt")
  103. endif()
  104. endforeach()
  105. # Clear log file
  106. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/ctkScriptWrapPythonQt_Light_log.txt" "")
  107. # Convert wrapping namespace to subdir
  108. string(REPLACE "." "_" WRAPPING_NAMESPACE_UNDERSCORE ${WRAPPING_NAMESPACE})
  109. # Convert ^^ separated string to list
  110. string(REPLACE "^^" ";" SOURCES "${SOURCES}")
  111. foreach(FILE ${SOURCES})
  112. # what is the filename without the extension
  113. get_filename_component(TMP_FILENAME ${FILE} NAME_WE)
  114. set(includes
  115. "${includes}\n#include \"${TMP_FILENAME}.h\"")
  116. # Extract classname - NOTE: We assume the filename matches the associated class
  117. set(className ${TMP_FILENAME})
  118. #message(STATUS "FILE:${FILE}, className:${className}")
  119. # Extract parent classname
  120. set(parentClassName)
  121. if("${parentClassName}" STREQUAL "")
  122. # Does constructor signature is of the form: myclass()
  123. set(regex "[^~]${className}[\\s\\n]*\\([\\s\\n]*\\)")
  124. reSearchfile(${PYTHON_EXECUTABLE} ${PYTHON_LIBRARY_PATH} ${regex} ${FILE} is_matching)
  125. if(is_matching)
  126. set(parentClassName "No")
  127. log("${TMP_FILENAME} - constructor of the form: ${className}\(\)")
  128. endif()
  129. endif()
  130. if("${parentClassName}" STREQUAL "")
  131. # Does constructor signature is of the form: myclass(QObject * parent ...)
  132. set(regex "${className}[\\s\\n]*\\([\\s\\n]*QObject[\\s\\n]*\\*[\\s\\n]*\\w+[\\s\\n]*(\\=[\\s\\n]*(0|NULL)|,.*\\=.*\\)|\\))")
  133. reSearchfile(${PYTHON_EXECUTABLE} ${PYTHON_LIBRARY_PATH} ${regex} ${FILE} is_matching)
  134. if(is_matching)
  135. set(parentClassName "QObject")
  136. log("${TMP_FILENAME} - constructor of the form: ${className}\(QObject * parent ... \)")
  137. endif()
  138. endif()
  139. if("${parentClassName}" STREQUAL "")
  140. # Does constructor signature is of the form: myclass(QWidget * parent ...)
  141. set(regex "${className}[\\s\\n]*\\([\\s\\n]*QWidget[\\s\\n]*\\*[\\s\\n]*\\w+[\\s\\n]*(\\=[\\s\\n]*(0|NULL)|,.*\\=.*\\)|\\))")
  142. reSearchfile(${PYTHON_EXECUTABLE} ${PYTHON_LIBRARY_PATH} ${regex} ${FILE} is_matching)
  143. if(is_matching)
  144. set(parentClassName "QWidget")
  145. log("${TMP_FILENAME} - constructor of the form: ${className}\(QWidget * parent ... \)")
  146. endif()
  147. endif()
  148. # Generate PythonQtWrapper class
  149. if("${parentClassName}" STREQUAL "QObject" OR "${parentClassName}" STREQUAL "QWidget")
  150. set(pythonqtWrappers
  151. "${pythonqtWrappers}
  152. //-----------------------------------------------------------------------------
  153. class PythonQtWrapper_${className} : public QObject
  154. {
  155. Q_OBJECT
  156. public:
  157. public Q_SLOTS:
  158. ${className}* new_${className}(${parentClassName}* parent = 0)
  159. {
  160. return new ${className}(parent);
  161. }
  162. void delete_${className}(${className}* obj) { delete obj; }
  163. };
  164. ")
  165. elseif("${parentClassName}" STREQUAL "No")
  166. set(pythonqtWrappers
  167. "${pythonqtWrappers}
  168. //-----------------------------------------------------------------------------
  169. class Q_DECL_EXPORT PythonQtWrapper_${className} : public QObject
  170. {
  171. Q_OBJECT
  172. public:
  173. public Q_SLOTS:
  174. ${className}* new_${className}()
  175. {
  176. return new ${className}();
  177. }
  178. void delete_${className}(${className}* obj) { delete obj; }
  179. };
  180. ")
  181. else() # Case parentClassName is empty
  182. message(WARNING "ctkScriptWrapPythonQt_Light - Problem wrapping ${FILE}")
  183. endif()
  184. # Generate code allowing to register the class metaobject and its associated "light" wrapper
  185. set(registerclasses "${registerclasses}
  186. PythonQt::self()->registerClass(
  187. &${className}::staticMetaObject, \"${TARGET}\",
  188. PythonQtCreateObject<PythonQtWrapper_${className}>);\n")
  189. endforeach()
  190. # Write master include file
  191. file(WRITE ${OUTPUT_DIR}/${WRAP_INT_DIR}${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}0.h "//
  192. // File auto-generated by cmake macro ctkScriptWrapPythonQt_Light
  193. //
  194. #ifndef __${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}0_h
  195. #define __${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}0_h
  196. #include <QWidget>
  197. ${includes}
  198. ${pythonqtWrappers}
  199. #endif
  200. ")
  201. # Write wrapper header
  202. file(WRITE ${OUTPUT_DIR}/${WRAP_INT_DIR}${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_init.cpp "//
  203. // File auto-generated by cmake macro ctkScriptWrapPythonQt_Light
  204. //
  205. #include <PythonQt.h>
  206. #include \"${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}0.h\"
  207. void PythonQt_init_${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}(PyObject* module)
  208. {
  209. Q_UNUSED(module);
  210. ${registerclasses}
  211. }
  212. ")
  213. # Configure 'ctkMacroWrapPythonQtModuleInit.cpp.in' replacing TARGET and
  214. # WRAPPING_NAMESPACE_UNDERSCORE.
  215. configure_file(
  216. ${CMAKE_CURRENT_LIST_DIR}/ctkMacroWrapPythonQtModuleInit.cpp.in
  217. ${OUTPUT_DIR}/${WRAP_INT_DIR}${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_module_init.cpp
  218. )
  219. # Since file(WRITE ) doesn't update the timestamp - Let's touch the files
  220. execute_process(
  221. COMMAND ${CMAKE_COMMAND} -E touch
  222. ${OUTPUT_DIR}/${WRAP_INT_DIR}${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_init.cpp
  223. )