ctkScriptWrapPythonQt_Light.cmake 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. # 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. # -P ctkScriptWrapPythonQt_Light.cmake
  38. #
  39. #
  40. # LOG FILE:
  41. # File ctkScriptWrapPythonQt_Light_log.txt will be created in the current directory.
  42. # It will contain the list of class and the constructor signature that will be wrapped.
  43. #
  44. set(verbose 0)
  45. #
  46. # Convenient function allowing to log the reason why a given class hasn't been wrapped
  47. # If verbose=1, it will also be displayed on the standard output
  48. #
  49. FUNCTION(log msg)
  50. IF(verbose)
  51. MESSAGE(${msg})
  52. ENDIF()
  53. FILE(APPEND "${CMAKE_CURRENT_BINARY_DIR}/ctkScriptWrapPythonQt_Light_log.txt" "${msg}\n")
  54. ENDFUNCTION()
  55. #
  56. # Convenient function allowing to invoke re.search(regex, string) using the given interpreter.
  57. # Note that is_matching will be set to True if there is a match
  58. #
  59. FUNCTION(reSearchFile python_exe python_library_path regex file is_matching)
  60. set(python_cmd "import re\; f = open('${file}', 'r')\;
  61. res = re.search\(\"${regex}\", f.read(), re.MULTILINE\)\;
  62. if res == None: print \"FALSE\"
  63. else: print \"TRUE\"
  64. ")
  65. #message("python_cmd: ${python_cmd}")
  66. IF(WIN32)
  67. SET(ENV{PATH} ${python_library_path};$ENV{PATH})
  68. ELSEIF(APPLE)
  69. SET(ENV{DYLD_LIBRARY_PATH} ${python_library_path}:$ENV{DYLD_LIBRARY_PATH})
  70. ELSE()
  71. SET(ENV{LD_LIBRARY_PATH} ${python_library_path}:$ENV{LD_LIBRARY_PATH})
  72. ENDIF()
  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. #message(${output})
  84. SET(is_matching ${output} PARENT_SCOPE)
  85. ENDFUNCTION()
  86. # Check for non-defined var
  87. FOREACH(var WRAPPING_NAMESPACE TARGET SOURCES INCLUDE_DIRS WRAP_INT_DIR)
  88. IF(NOT DEFINED ${var})
  89. MESSAGE(SEND_ERROR "${var} not specified when calling ctkScriptWrapPythonQt")
  90. ENDIF()
  91. ENDFOREACH()
  92. # Check for non-existing ${var}
  93. FOREACH(var QT_QMAKE_EXECUTABLE OUTPUT_DIR PYTHON_EXECUTABLE PYTHON_LIBRARY_PATH)
  94. IF(NOT EXISTS ${${var}})
  95. MESSAGE(SEND_ERROR "Failed to find ${var} when calling ctkScriptWrapPythonQt")
  96. ENDIF()
  97. ENDFOREACH()
  98. # Clear log file
  99. FILE(WRITE "${CMAKE_CURRENT_BINARY_DIR}/ctkScriptWrapPythonQt_Light_log.txt" "")
  100. # Convert wrapping namespace to subdir
  101. STRING(REPLACE "." "_" WRAPPING_NAMESPACE_UNDERSCORE ${WRAPPING_NAMESPACE})
  102. # Convert ^^ separated string to list
  103. STRING(REPLACE "^^" ";" SOURCES "${SOURCES}")
  104. FOREACH(FILE ${SOURCES})
  105. # what is the filename without the extension
  106. GET_FILENAME_COMPONENT(TMP_FILENAME ${FILE} NAME_WE)
  107. SET(includes
  108. "${includes}\n#include \"${TMP_FILENAME}.h\"")
  109. # Extract classname - NOTE: We assume the filename matches the associated class
  110. set(className ${TMP_FILENAME})
  111. #message(STATUS "FILE:${FILE}, className:${className}")
  112. # Extract parent classname
  113. SET(parentClassName)
  114. IF("${parentClassName}" STREQUAL "")
  115. # Does constructor signature is of the form: myclass()
  116. SET(regex "[^~]${className}[\\s\\n]*\\([\\s\\n]*\\)")
  117. reSearchFile(${PYTHON_EXECUTABLE} ${PYTHON_LIBRARY_PATH} ${regex} ${FILE} is_matching)
  118. IF(is_matching)
  119. SET(parentClassName "No")
  120. log("${TMP_FILENAME} - constructor of the form: ${className}\(\)")
  121. ENDIF()
  122. ENDIF()
  123. IF("${parentClassName}" STREQUAL "")
  124. # Does constructor signature is of the form: myclass(QObject * parent ...)
  125. SET(regex "${className}[\\s\\n]*\\([\\s\\n]*QObject[\\s\\n]*\\*[\\s\\n]*\\w+[\\s\\n]*(\\=[\\s\\n]*(0|NULL)|,.*\\=.*\\)|\\))")
  126. reSearchFile(${PYTHON_EXECUTABLE} ${PYTHON_LIBRARY_PATH} ${regex} ${FILE} is_matching)
  127. IF(is_matching)
  128. SET(parentClassName "QObject")
  129. log("${TMP_FILENAME} - constructor of the form: ${className}\(QObject * parent ... \)")
  130. ENDIF()
  131. ENDIF()
  132. IF("${parentClassName}" STREQUAL "")
  133. # Does constructor signature is of the form: myclass(QWidget * parent ...)
  134. SET(regex "${className}[\\s\\n]*\\([\\s\\n]*QWidget[\\s\\n]*\\*[\\s\\n]*\\w+[\\s\\n]*(\\=[\\s\\n]*(0|NULL)|,.*\\=.*\\)|\\))")
  135. reSearchFile(${PYTHON_EXECUTABLE} ${PYTHON_LIBRARY_PATH} ${regex} ${FILE} is_matching)
  136. IF(is_matching)
  137. SET(parentClassName "QWidget")
  138. log("${TMP_FILENAME} - constructor of the form: ${className}\(QWidget * parent ... \)")
  139. ENDIF()
  140. ENDIF()
  141. # Generate PythonQtWrapper class
  142. IF("${parentClassName}" STREQUAL "QObject" OR "${parentClassName}" STREQUAL "QWidget")
  143. SET(pythonqtWrappers
  144. "${pythonqtWrappers}
  145. //-----------------------------------------------------------------------------
  146. class PythonQtWrapper_${className} : public QObject
  147. {
  148. Q_OBJECT
  149. public:
  150. public slots:
  151. ${className}* new_${className}(${parentClassName}* parent = 0)
  152. {
  153. return new ${className}(parent);
  154. }
  155. void delete_${className}(${className}* obj) { delete obj; }
  156. };
  157. ")
  158. ELSEIF("${parentClassName}" STREQUAL "No")
  159. SET(pythonqtWrappers
  160. "${pythonqtWrappers}
  161. //-----------------------------------------------------------------------------
  162. class PythonQtWrapper_${className} : public QObject
  163. {
  164. Q_OBJECT
  165. public:
  166. public slots:
  167. ${className}* new_${className}()
  168. {
  169. return new ${className}();
  170. }
  171. void delete_${className}(${className}* obj) { delete obj; }
  172. };
  173. ")
  174. ELSE() # Case parentClassName is empty
  175. MESSAGE(WARNING "ctkScriptWrapPythonQt_Light - Problem wrapping ${FILE}")
  176. ENDIF()
  177. # Generate code allowing to register the class metaobject and its associated "light" wrapper
  178. SET(registerclasses "${registerclasses}
  179. PythonQt::self()->registerClass(
  180. &${className}::staticMetaObject, \"${TARGET}\",
  181. PythonQtCreateObject<PythonQtWrapper_${className}>);\n")
  182. ENDFOREACH()
  183. # Write master include file
  184. FILE(WRITE ${OUTPUT_DIR}/${WRAP_INT_DIR}${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}0.h "//
  185. // File auto-generated by cmake macro ctkScriptWrapPythonQt_Light
  186. //
  187. #ifndef __${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}0_h
  188. #define __${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}0_h
  189. #include <QWidget>
  190. ${includes}
  191. ${pythonqtWrappers}
  192. #endif
  193. ")
  194. # Write wrapper header
  195. FILE(WRITE ${OUTPUT_DIR}/${WRAP_INT_DIR}${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_init.cpp "//
  196. // File auto-generated by cmake macro ctkScriptWrapPythonQt_Light
  197. //
  198. #include <PythonQt.h>
  199. #include \"${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}0.h\"
  200. void PythonQt_init_${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}(PyObject* module)
  201. {
  202. Q_UNUSED(module);
  203. ${registerclasses}
  204. }
  205. ")
  206. # Since FILE(WRITE ) doesn't update the timestamp - Let's touch the files
  207. EXECUTE_PROCESS(
  208. COMMAND ${CMAKE_COMMAND} -E touch
  209. ${OUTPUT_DIR}/${WRAP_INT_DIR}${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_init.cpp
  210. )