ctkScriptWrapPythonQt_Full.cmake 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_Full
  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. # -DPYTHONQTGENERATOR_EXECUTABLE:FILEPATH=/path/to/exec
  35. # -DOUTPUT_DIR:PATH=/path -DQT_QMAKE_EXECUTABLE:PATH=/path/to/qt/qmake
  36. # -P ctkScriptWrapPythonQt_Full.cmake
  37. #
  38. # Check for non-defined var
  39. FOREACH(var SOURCES TARGET INCLUDE_DIRS WRAP_INT_DIR WRAPPING_NAMESPACE)
  40. IF(NOT DEFINED ${var})
  41. MESSAGE(SEND_ERROR "${var} not specified when calling ctkScriptWrapPythonQt")
  42. ENDIF()
  43. ENDFOREACH()
  44. # Check for non-existing ${var}
  45. FOREACH(var PYTHONQTGENERATOR_EXECUTABLE QT_QMAKE_EXECUTABLE OUTPUT_DIR)
  46. IF(NOT EXISTS ${${var}})
  47. MESSAGE(SEND_ERROR "Failed to find ${var} when calling ctkScriptWrapPythonQt")
  48. ENDIF()
  49. ENDFOREACH()
  50. # Convert wrapping namespace to subdir
  51. STRING(REPLACE "." "_" WRAPPING_NAMESPACE_UNDERSCORE ${WRAPPING_NAMESPACE})
  52. # Convert ^^ separated string to list
  53. STRING(REPLACE "^^" ";" SOURCES "${SOURCES}")
  54. FOREACH(FILE ${SOURCES})
  55. # what is the filename without the extension
  56. GET_FILENAME_COMPONENT(TMP_FILENAME ${FILE} NAME_WE)
  57. SET(includes
  58. "${includes}\n#include \"${TMP_FILENAME}.h\"")
  59. # Extract classname - NOTE: We assume the filename matches the associated class
  60. set(className ${TMP_FILENAME})
  61. #message(STATUS "FILE:${FILE}, className:${className}")
  62. SET(objectTypes "${objectTypes}\n <object-type name=\"${className}\"/>")
  63. ENDFOREACH()
  64. # Write master include file
  65. FILE(WRITE ${OUTPUT_DIR}/${WRAP_INT_DIR}ctkPythonQt_${TARGET}_masterinclude.h "
  66. #ifndef __ctkPythonQt_${TARGET}_masterinclude_h
  67. #define __ctkPythonQt_${TARGET}_masterinclude_h
  68. ${includes}
  69. #endif
  70. ")
  71. # Write Typesystem file
  72. FILE(WRITE ${OUTPUT_DIR}/${WRAP_INT_DIR}typesystem_${TARGET}.xml "
  73. <typesystem package=\"${WRAPPING_NAMESPACE}.${TARGET}\">
  74. ${objectTypes}
  75. </typesystem>
  76. ")
  77. # Extract PYTHONQTGENERATOR_DIR
  78. GET_FILENAME_COMPONENT(PYTHONQTGENERATOR_DIR ${PYTHONQTGENERATOR_EXECUTABLE} PATH)
  79. #message(PYTHONQTGENERATOR_DIR:${PYTHONQTGENERATOR_DIR})
  80. # Write Build file
  81. FILE(WRITE ${OUTPUT_DIR}/${WRAP_INT_DIR}build_${TARGET}.txt "
  82. <!-- File auto-generated by cmake macro ctkScriptWrapPythonQt_Full -->
  83. <typesystem>
  84. <load-typesystem name=\"${PYTHONQTGENERATOR_DIR}/typesystem_core.xml\" generate=\"no\" />
  85. <load-typesystem name=\"${PYTHONQTGENERATOR_DIR}/typesystem_gui.xml\" generate=\"no\" />
  86. <load-typesystem name=\"${OUTPUT_DIR}/${WRAP_INT_DIR}/typesystem_${TARGET}.xml\" generate=\"yes\" />
  87. </typesystem>
  88. ")
  89. # Read include dirs from file
  90. IF(WIN32)
  91. IF(NOT EXISTS ${INCLUDE_DIRS})
  92. MESSAGE(SEND_ERROR "On Windows, INCLUDE_DIRS should be the name of the file containing the include directories !")
  93. ENDIF()
  94. FILE(READ ${INCLUDE_DIRS} INCLUDE_DIRS)
  95. ENDIF()
  96. # Compute QTDIR
  97. GET_FILENAME_COMPONENT(QTDIR ${QT_QMAKE_EXECUTABLE}/../../ REALPATH)
  98. SET(ENV{QTDIR} ${QTDIR})
  99. EXECUTE_PROCESS(
  100. COMMAND ${PYTHONQTGENERATOR_EXECUTABLE} --debug-level=sparse --include-paths=${INCLUDE_DIRS} --output-directory=${OUTPUT_DIR} ${OUTPUT_DIR}/${WRAP_INT_DIR}ctkPythonQt_${TARGET}_masterinclude.h ${OUTPUT_DIR}/${WRAP_INT_DIR}build_${TARGET}.txt
  101. WORKING_DIRECTORY ${PYTHONQTGENERATOR_DIR}
  102. RESULT_VARIABLE result
  103. #OUTPUT_VARIABLE output
  104. ERROR_VARIABLE error
  105. OUTPUT_QUIET
  106. )
  107. #message(${error})
  108. IF(result)
  109. MESSAGE(FATAL_ERROR "Failed to generate ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_init.cpp\n${error}")
  110. ENDIF()
  111. # Since PythonQtGenerator or FILE(WRITE ) doesn't 'update the timestamp - Let's touch the files
  112. EXECUTE_PROCESS(
  113. COMMAND ${CMAKE_COMMAND} -E touch
  114. ${OUTPUT_DIR}/${WRAP_INT_DIR}${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_init.cpp
  115. ${OUTPUT_DIR}/${WRAP_INT_DIR}ctkPythonQt_${TARGET}_masterinclude.h
  116. )