ctkScriptWrapPythonQt_Full.cmake 5.2 KB

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