ctkScriptWrapPythonQt_Full.cmake 5.3 KB

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