ctkScriptMocPythonQtWrapper.cmake 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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
  28. # -DTARGET:STRING=MyLib
  29. # -DOUTPUT_DIR:PATH=/path
  30. # -DMOC_FLAGS:STRING=-I/path/to/lib1^^-I/path/to/lib2^^-DOPT1
  31. # -DWRAPPER_MASTER_MOC_FILE:FILEPATH=/path/to/file
  32. # -DQT_QMAKE_EXECUTABLE:PATH=/path/to/qt/qmake
  33. # -P ctkScriptMocPythonQtWrapper.cmake
  34. #
  35. #
  36. # Check for non-defined var
  37. foreach(var WRAPPING_NAMESPACE TARGET MOC_FLAGS WRAPPER_MASTER_MOC_FILE WRAP_INT_DIR)
  38. if(NOT DEFINED ${var})
  39. message(FATAL_ERROR "${var} not specified when calling ctkScriptMocPythonQtWrapper")
  40. endif()
  41. endforeach()
  42. # Check for non-existing ${var}
  43. foreach(var OUTPUT_DIR QT_MOC_EXECUTABLE)
  44. if(NOT EXISTS ${${var}})
  45. message(FATAL_ERROR "Failed to find ${var} when calling ctkScriptWrapPythonQt")
  46. endif()
  47. endforeach()
  48. # Convert wrapping namespace to subdir
  49. string(REPLACE "." "_" WRAPPING_NAMESPACE_UNDERSCORE ${WRAPPING_NAMESPACE})
  50. # Read moc flags from file
  51. if(WIN32)
  52. if(NOT EXISTS ${MOC_FLAGS})
  53. message(FATAL_ERROR "On Windows, MOC_FLAGS should be the name of the file containing the moc flags !")
  54. endif()
  55. file(READ ${MOC_FLAGS} MOC_FLAGS)
  56. endif()
  57. # Convert ^^ separated string to list
  58. string(REPLACE "^^" ";" MOC_FLAGS "${MOC_FLAGS}")
  59. # Clear file where all moc'ified will be appended
  60. file(WRITE ${WRAPPER_MASTER_MOC_FILE} "//
  61. // File auto-generated by cmake macro ctkScriptMocPythonQtWrapper\n//\n")
  62. # Collect wrapper headers
  63. set(glob_expression ${OUTPUT_DIR}/${WRAP_INT_DIR}${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}*.h)
  64. file(GLOB wrapper_headers RELATIVE ${OUTPUT_DIR}/${WRAP_INT_DIR} ${glob_expression})
  65. if(NOT wrapper_headers)
  66. message(FATAL_ERROR "ctkScriptMocPythonQtWrapper - Failed to glob wrapper headers using expression:[${glob_expression}]")
  67. endif()
  68. # Moc'ified each one of them
  69. foreach(header ${wrapper_headers})
  70. set(moc_extra_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. # Dump moc executable options into a file to avoid command line length limitations
  74. # See http://qt-project.org/doc/qt-4.8/moc.html#command-line-options
  75. # As required when using the @ option of moc executable, process MOC_FLAGS list
  76. # to obtain a string with one flag per line
  77. set(moc_flags_with_newline "")
  78. foreach(flag ${MOC_FLAGS})
  79. set(moc_flags_with_newline "${moc_flags_with_newline}\n${flag}")
  80. endforeach()
  81. # Dump flags into a file
  82. get_filename_component(header_we ${header} NAME_WE)
  83. set(wrapper_moc_options_file ${OUTPUT_DIR}/${WRAP_INT_DIR}/${header_we}_moc_opts.txt)
  84. file(WRITE ${wrapper_moc_options_file}
  85. "${moc_flags_with_newline}
  86. ${moc_extra_options}")
  87. message("${QT_MOC_EXECUTABLE} @${wrapper_moc_options_file} -o ${wrapper_moc_file} ${wrapper_h_file}")
  88. # Invoke moc executable passing "@" parameter
  89. execute_process(
  90. COMMAND ${QT_MOC_EXECUTABLE} @${wrapper_moc_options_file} -o ${wrapper_moc_file} ${wrapper_h_file}
  91. WORKING_DIRECTORY ${OUTPUT_DIR}
  92. RESULT_VARIABLE RESULT_VAR
  93. ERROR_VARIABLE error
  94. )
  95. if(RESULT_VAR)
  96. message(FATAL_ERROR "Failed to moc'ified .\n${RESULT_VAR}\n${error}")
  97. endif()
  98. # Append generated moc file to the master file
  99. file(READ ${wrapper_moc_file} file_content)
  100. file(APPEND ${WRAPPER_MASTER_MOC_FILE} "${file_content}")
  101. endforeach()