ctkScriptMocPythonQtWrapper.cmake 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. # 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(FATAL_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(FATAL_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. # Read moc flags from file
  49. if(WIN32)
  50. if(NOT EXISTS ${MOC_FLAGS})
  51. message(FATAL_ERROR "On Windows, MOC_FLAGS should be the name of the file containing the moc flags !")
  52. endif()
  53. file(READ ${MOC_FLAGS} MOC_FLAGS)
  54. endif()
  55. # Convert ^^ separated string to list
  56. string(REPLACE "^^" ";" MOC_FLAGS "${MOC_FLAGS}")
  57. # Clear file where all moc'ified will be appended
  58. file(WRITE ${WRAPPER_MASTER_MOC_FILE} "//
  59. // File auto-generated by cmake macro ctkScriptMocPythonQtWrapper\n//\n")
  60. # Collect wrapper headers
  61. set(glob_expression ${OUTPUT_DIR}/${WRAP_INT_DIR}${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}*.h)
  62. file(GLOB wrapper_headers RELATIVE ${OUTPUT_DIR}/${WRAP_INT_DIR} ${glob_expression})
  63. if(NOT wrapper_headers)
  64. message(FATAL_ERROR "ctkScriptMocPythonQtWrapper - Failed to glob wrapper headers using expression:[${glob_expression}]")
  65. endif()
  66. # Moc'ified each one of them
  67. foreach(header ${wrapper_headers})
  68. # what is the filename without the extension
  69. get_filename_component(TMP_FILENAME ${header} NAME_WE)
  70. set(moc_options)
  71. set(wrapper_h_file ${OUTPUT_DIR}/${WRAP_INT_DIR}/${header})
  72. set(wrapper_moc_file ${OUTPUT_DIR}/${WRAP_INT_DIR}/moc_${header}.cpp)
  73. #message("wrapper_h_file: ${wrapper_h_file}")
  74. #message("wrapper_moc_file: ${wrapper_moc_file}")
  75. execute_process(
  76. COMMAND ${QT_MOC_EXECUTABLE} ${MOC_FLAGS} ${moc_options} -o ${wrapper_moc_file} ${wrapper_h_file}
  77. WORKING_DIRECTORY ${OUTPUT_DIR}
  78. RESULT_VARIABLE RESULT_VAR
  79. ERROR_VARIABLE error
  80. )
  81. if(RESULT_VAR)
  82. message(FATAL_ERROR "Failed to moc'ified .\n${RESULT_VAR}\n${error}")
  83. endif()
  84. # Append generated moc file to the master file
  85. file(READ ${wrapper_moc_file} file_content)
  86. file(APPEND ${WRAPPER_MASTER_MOC_FILE} "${file_content}")
  87. endforeach()