Explorar el Código

Simplify moc'ing of generated PythonQt wrapper. See #449

Jean-Christophe Fillion-Robin hace 11 años
padre
commit
732b362924

+ 6 - 47
CMake/ctkMacroWrapPythonQt.cmake

@@ -158,11 +158,13 @@ macro(ctkMacroWrapPythonQt WRAPPING_NAMESPACE TARGET SRCS_LIST_NAME SOURCES IS_W
     )
 
   # Custom command allow to generate ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_init.cpp and
-  # associated wrappers ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}{0-N}.cpp
+  # associated wrappers ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}.cpp
   set(wrapper_init_cpp_filename ${wrap_int_dir}${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_init.cpp)
+  set(wrapper_h_filename ${wrap_int_dir}${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}.h)
   add_custom_command(
     OUTPUT
       ${wrapper_init_cpp_filename}
+      ${wrapper_h_filename}
     DEPENDS
       ${SOURCES_TO_WRAP}
       ${CTK_CMAKE_DIR}/ctkScriptWrapPythonQt_Light.cmake
@@ -179,56 +181,13 @@ macro(ctkMacroWrapPythonQt WRAPPING_NAMESPACE TARGET SRCS_LIST_NAME SOURCES IS_W
     VERBATIM
     )
 
-  # Clear variable
-  set(moc_flags)
-
-  # Grab moc flags
-  QT4_GET_MOC_FLAGS(moc_flags)
-
-  # Prepare custom_command argument
-  set(moc_flags_arg)
-  foreach(flag ${moc_flags})
-    set(moc_flags_arg "${moc_flags_arg}^^${flag}")
-  endforeach()
-
-  # On Windows, to avoid "too long input" error, dump moc flags.
-  if(WIN32)
-    # File containing the moc flags
-    set(wrapper_moc_flags_filename mocflags_${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_all.txt)
-    set(wrapper_master_moc_flags_file ${CMAKE_CURRENT_BINARY_DIR}/${wrap_int_dir}${wrapper_moc_flags_filename})
-    file(WRITE ${wrapper_master_moc_flags_file} ${moc_flags_arg})
-    # The arg passed to the custom command will be the file containing the list of moc flags
-    set(moc_flags_arg ${wrapper_master_moc_flags_file})
-  endif()
-
-  # File to run through moc
-  set(wrapper_master_moc_filename ${wrap_int_dir}moc_${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_all.cpp)
-  set(wrapper_master_moc_file ${CMAKE_CURRENT_BINARY_DIR}/${wrapper_master_moc_filename})
-
-  # Custom command allowing to call moc to process the wrapper headers
-  add_custom_command(
-    OUTPUT ${wrapper_master_moc_filename}
-    DEPENDS
-      ${wrapper_init_cpp_filename}
-      ${CTK_CMAKE_DIR}/ctkScriptMocPythonQtWrapper.cmake
-    COMMAND ${CMAKE_COMMAND}
-      -DWRAPPING_NAMESPACE:STRING=${WRAPPING_NAMESPACE}
-      -DTARGET:STRING=${TARGET}
-      -DMOC_FLAGS:STRING=${moc_flags_arg}
-      -DWRAP_INT_DIR:STRING=${wrap_int_dir}
-      -DWRAPPER_MASTER_MOC_FILE:STRING=${wrapper_master_moc_file}
-      -DOUTPUT_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}
-      -DQT_MOC_EXECUTABLE:FILEPATH=${QT_MOC_EXECUTABLE}
-      -P ${CTK_CMAKE_DIR}/ctkScriptMocPythonQtWrapper.cmake
-    COMMENT "PythonQt Wrapping - Moc'ing ${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET} wrapper headers"
-    VERBATIM
-    )
+  QT4_WRAP_CPP(${TARGET}_MOC_CXX ${CMAKE_CURRENT_BINARY_DIR}/${wrapper_h_filename})
 
   # The following files are generated
   set_source_files_properties(
     ${wrapper_init_cpp_filename}
+    ${wrapper_h_filename}
     ${wrapper_module_init_cpp_filename}
-    ${wrapper_master_moc_filename}
     PROPERTIES GENERATED TRUE)
 
   # Create the Init File
@@ -236,7 +195,7 @@ macro(ctkMacroWrapPythonQt WRAPPING_NAMESPACE TARGET SRCS_LIST_NAME SOURCES IS_W
     ${${SRCS_LIST_NAME}}
     ${wrapper_init_cpp_filename}
     ${wrapper_module_init_cpp_filename}
-    ${wrapper_master_moc_filename}
+    ${${TARGET}_MOC_CXX}
     )
 
   #

+ 0 - 120
CMake/ctkScriptMocPythonQtWrapper.cmake

@@ -1,120 +0,0 @@
-###########################################################################
-#
-#  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()
-

+ 4 - 4
CMake/ctkWrapPythonQt.py

@@ -153,7 +153,7 @@ def ctk_wrap_pythonqt(target, namespace, output_dir, input_files, extra_verbose)
   PythonQtCreateObject<PythonQtWrapper_${className}>);
   """).substitute(className = className, target = target))
 
-    output_header = output_dir + "/" + namespace + "_" + target + "0.h"
+    output_header = output_dir + "/" + namespace + "_" + target + ".h"
     if extra_verbose:
         print("output_header: %s" % output_header)
     # Write master include file
@@ -164,8 +164,8 @@ def ctk_wrap_pythonqt(target, namespace, output_dir, input_files, extra_verbose)
 // File auto-generated by ctkWrapPythonQt.py
 //
 
-#ifndef __${namespace}_${target}0_h
-#define __${namespace}_${target}0_h
+#ifndef __${namespace}_${target}_h
+#define __${namespace}_${target}_h
 
 #include <QWidget>
 ${includes}
@@ -185,7 +185,7 @@ ${pythonqtWrappers}
 //
 
 #include <PythonQt.h>
-#include "${namespace}_${target}0.h"
+#include "${namespace}_${target}.h"
 
 void PythonQt_init_${namespace}_${target}(PyObject* module)
 {