ctkScriptMocPythonQtWrapper.cmake 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. # ctkScriptMocPythonQtWrapper
  22. #
  23. #
  24. # This script should be invoked either as a CUSTOM_COMMAND
  25. # or from the command line using the following syntax:
  26. #
  27. # cmake -DWRAPPING_NAMESPACE:STRING=org.commontk -DTARGET:STRING=MyLib
  28. # -DOUTPUT_DIR:PATH=/path -DINCLUDE_DIRS:STRING=/path1:/path2
  29. # -DWRAPPER_MASTER_MOC_FILE:FILEPATH=/path/to/file
  30. # -DQT_QMAKE_EXECUTABLE:PATH=/path/to/qt/qmake
  31. # -P ctkScriptWrapPythonQt.cmake
  32. #
  33. #
  34. # Check for non-defined var
  35. FOREACH(var WRAPPING_NAMESPACE TARGET MOC_FLAGS WRAPPER_MASTER_MOC_FILE WRAP_INT_DIR)
  36. IF(NOT DEFINED ${var})
  37. MESSAGE(SEND_ERROR "${var} not specified when calling ctkScriptMocPythonQtWrapper")
  38. ENDIF()
  39. ENDFOREACH()
  40. # Check for non-existing ${var}
  41. FOREACH(var OUTPUT_DIR QT_MOC_EXECUTABLE)
  42. IF(NOT EXISTS ${${var}})
  43. MESSAGE(SEND_ERROR "Failed to find ${var} when calling ctkScriptWrapPythonQt")
  44. ENDIF()
  45. ENDFOREACH()
  46. # Convert wrapping namespace to subdir
  47. STRING(REPLACE "." "_" WRAPPING_NAMESPACE_UNDERSCORE ${WRAPPING_NAMESPACE})
  48. # Convert ^^ separated string to list
  49. STRING(REPLACE "^^" ";" MOC_FLAGS "${MOC_FLAGS}")
  50. # Clear file where all moc'ified will be appended
  51. FILE(WRITE ${WRAPPER_MASTER_MOC_FILE} "//
  52. // File auto-generated by cmake macro ctkScriptMocPythonQtWrapper\n//\n")
  53. # Collect wrapper headers
  54. set(glob_expression ${OUTPUT_DIR}/${WRAP_INT_DIR}${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}*.h)
  55. FILE(GLOB wrapper_headers RELATIVE ${OUTPUT_DIR}/${WRAP_INT_DIR} ${glob_expression})
  56. IF(NOT wrapper_headers)
  57. MESSAGE(SEND_ERROR "ctkScriptMocPythonQtWrapper - Failed to glob wrapper headers using expression:[${glob_expression}]")
  58. ENDIF()
  59. # Moc'ified each one of them
  60. FOREACH(header ${wrapper_headers})
  61. # what is the filename without the extension
  62. GET_FILENAME_COMPONENT(TMP_FILENAME ${header} NAME_WE)
  63. set(moc_options)
  64. set(wrapper_h_file ${OUTPUT_DIR}/${WRAP_INT_DIR}/${header})
  65. set(wrapper_moc_file ${OUTPUT_DIR}/${WRAP_INT_DIR}/moc_${header}.cpp)
  66. #message("wrapper_h_file: ${wrapper_h_file}")
  67. #message("wrapper_moc_file: ${wrapper_moc_file}")
  68. EXECUTE_PROCESS(
  69. COMMAND ${QT_MOC_EXECUTABLE} ${MOC_FLAGS} ${moc_options} -o ${wrapper_moc_file} ${wrapper_h_file}
  70. WORKING_DIRECTORY ${OUTPUT_DIR}
  71. RESULT_VARIABLE RESULT_VAR
  72. ERROR_VARIABLE error
  73. )
  74. IF(RESULT_VAR)
  75. MESSAGE(FATAL_ERROR "Failed to moc'ified .\n${RESULT_VAR}\n${error}")
  76. ENDIF()
  77. # Append generated moc file to the master file
  78. FILE(READ ${wrapper_moc_file} file_content)
  79. FILE(APPEND ${WRAPPER_MASTER_MOC_FILE} "${file_content}")
  80. ENDFOREACH()