########################################################################### # # Library: CTK # # Copyright (c) Kitware Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0.txt # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ########################################################################### # # ctkScriptMocPythonQtWrapper # # # This script should be invoked either as a CUSTOM_COMMAND # or from the command line using the following syntax: # # cmake -DWRAPPING_NAMESPACE:STRING=org.commontk # -DTARGET:STRING=MyLib # -DOUTPUT_DIR:PATH=/path # -DMOC_FLAGS:STRING=-I/path/to/lib1^^-I/path/to/lib2^^-DOPT1 # -DWRAPPER_MASTER_MOC_FILE:FILEPATH=/path/to/file # -DQT_QMAKE_EXECUTABLE:PATH=/path/to/qt/qmake # -P ctkScriptMocPythonQtWrapper.cmake # # # Check for non-defined var foreach(var WRAPPING_NAMESPACE TARGET MOC_FLAGS WRAPPER_MASTER_MOC_FILE WRAP_INT_DIR) if(NOT DEFINED ${var}) message(FATAL_ERROR "${var} not specified when calling ctkScriptMocPythonQtWrapper") endif() endforeach() # Check for non-existing ${var} foreach(var OUTPUT_DIR QT_MOC_EXECUTABLE) if(NOT EXISTS ${${var}}) message(FATAL_ERROR "Failed to find ${var} when calling ctkScriptWrapPythonQt") endif() endforeach() # Convert wrapping namespace to subdir string(REPLACE "." "_" WRAPPING_NAMESPACE_UNDERSCORE ${WRAPPING_NAMESPACE}) # Read moc flags from file if(WIN32) if(NOT EXISTS ${MOC_FLAGS}) message(FATAL_ERROR "On Windows, MOC_FLAGS should be the name of the file containing the moc flags !") endif() file(READ ${MOC_FLAGS} MOC_FLAGS) endif() # Convert ^^ separated string to list string(REPLACE "^^" ";" MOC_FLAGS "${MOC_FLAGS}") # Clear file where all moc'ified will be appended file(WRITE ${WRAPPER_MASTER_MOC_FILE} "// // File auto-generated by cmake macro ctkScriptMocPythonQtWrapper\n//\n") # Collect wrapper headers set(glob_expression ${OUTPUT_DIR}/${WRAP_INT_DIR}${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}*.h) file(GLOB wrapper_headers RELATIVE ${OUTPUT_DIR}/${WRAP_INT_DIR} ${glob_expression}) if(NOT wrapper_headers) message(FATAL_ERROR "ctkScriptMocPythonQtWrapper - Failed to glob wrapper headers using expression:[${glob_expression}]") endif() # Moc'ified each one of them foreach(header ${wrapper_headers}) set(moc_extra_options) set(wrapper_h_file ${OUTPUT_DIR}/${WRAP_INT_DIR}/${header}) set(wrapper_moc_file ${OUTPUT_DIR}/${WRAP_INT_DIR}/moc_${header}.cpp) # Dump moc executable options into a file to avoid command line length limitations # See http://qt-project.org/doc/qt-4.8/moc.html#command-line-options # As required when using the @ option of moc executable, process MOC_FLAGS list # to obtain a string with one flag per line set(moc_flags_with_newline "") foreach(flag ${MOC_FLAGS}) set(moc_flags_with_newline "${moc_flags_with_newline}\n${flag}") endforeach() # Dump flags into a file get_filename_component(header_we ${header} NAME_WE) set(wrapper_moc_options_file ${OUTPUT_DIR}/${WRAP_INT_DIR}/${header_we}_moc_opts.txt) file(WRITE ${wrapper_moc_options_file} "${moc_flags_with_newline} ${moc_extra_options}") message("${QT_MOC_EXECUTABLE} @${wrapper_moc_options_file} -o ${wrapper_moc_file} ${wrapper_h_file}") # Invoke moc executable passing "@" parameter execute_process( COMMAND ${QT_MOC_EXECUTABLE} @${wrapper_moc_options_file} -o ${wrapper_moc_file} ${wrapper_h_file} WORKING_DIRECTORY ${OUTPUT_DIR} RESULT_VARIABLE RESULT_VAR ERROR_VARIABLE error ) if(RESULT_VAR) message(FATAL_ERROR "Failed to moc'ified .\n${RESULT_VAR}\n${error}") endif() # Append generated moc file to the master file file(READ ${wrapper_moc_file} file_content) file(APPEND ${WRAPPER_MASTER_MOC_FILE} "${file_content}") endforeach()