瀏覽代碼

Add option allowing to reduce number of python copy/compilation targets

By globally defining the variable CTK_COMPILE_PYTHON_SCRIPTS_GLOBAL_TARGET_NAME to a
non-empty string or by specifying the macro option 'GLOBAL_TARGET',
the following targets will be defined for the whole build system:
 - Copy<GLOBAL_TARGET_NAME>PythonResourceFiles
 - Copy<GLOBAL_TARGET_NAME>PythonScriptFiles
 - Compile<GLOBAL_TARGET_NAME>PythonFiles

For complex projects, this can help reducing the number of targets and
simplify the manual rebuild of copy and compile targets.

Also make the custom command in charge of compiling the script dependent
of Copy${target}PythonScriptFiles target. That building a compile target
implies the associated copy target will be executed.
Jean-Christophe Fillion-Robin 12 年之前
父節點
當前提交
68bf4c814e
共有 1 個文件被更改,包括 126 次插入67 次删除
  1. 126 67
      CMake/ctkMacroCompilePythonScript.cmake

+ 126 - 67
CMake/ctkMacroCompilePythonScript.cmake

@@ -5,21 +5,27 @@
 #          ParaView/VTK/Wrapping/Python/CMakeLists.txt
 #
 
+#
+# By globally defining the variable CTK_COMPILE_PYTHON_SCRIPTS_GLOBAL_TARGET_NAME to a 
+# non-empty string or by specifying the macro option 'GLOBAL_TARGET', 
+# the following targets will be defined for the whole build system:
+#  - Copy<GLOBAL_TARGET_NAME>PythonResourceFiles
+#  - Copy<GLOBAL_TARGET_NAME>PythonScriptFiles
+#  - Compile<GLOBAL_TARGET_NAME>PythonFiles
+#
+# For complex projects, this can help reducing the number of targets and
+# simplify the manual rebuild of copy and compile targets.
+#
+
 include(${CTK_CMAKE_DIR}/ctkMacroParseArguments.cmake)
 
 #! \ingroup CMakeAPI
 macro(ctkMacroCompilePythonScript)
   ctkMacroParseArguments(MY
     "TARGET_NAME;SCRIPTS;RESOURCES;SOURCE_DIR;DESTINATION_DIR;INSTALL_DIR"
-    "NO_INSTALL_SUBDIR"
+    "NO_INSTALL_SUBDIR;GLOBAL_TARGET"
     ${ARGN}
     )
-
-  find_package(PythonInterp REQUIRED)
-  find_package(PythonLibs REQUIRED)
-
-  # Extract python lib path
-  get_filename_component(PYTHON_LIBRARY_PATH ${PYTHON_LIBRARY} PATH)
   
   # Sanity checks
   foreach(varname TARGET_NAME SCRIPTS DESTINATION_DIR INSTALL_DIR)
@@ -31,6 +37,13 @@ macro(ctkMacroCompilePythonScript)
   if(NOT DEFINED MY_SOURCE_DIR)
     set(MY_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
   endif()
+  
+  if("${CTK_COMPILE_PYTHON_SCRIPTS_GLOBAL_TARGET_NAME}" STREQUAL "")
+    set(target ${MY_TARGET_NAME})
+  else()
+    set(MY_GLOBAL_TARGET TRUE)
+    set(target ${CTK_COMPILE_PYTHON_SCRIPTS_GLOBAL_TARGET_NAME})
+  endif()
 
   # Since 'add_custom_command' doesn't play nicely with path having multiple
   # consecutive slashes. Let's make sure there are no trailing slashes.
@@ -38,7 +51,6 @@ macro(ctkMacroCompilePythonScript)
   get_filename_component(MY_DESTINATION_DIR ${MY_DESTINATION_DIR} REALPATH)
 
   set(input_python_files)
-  set(copied_python_files)
   foreach(file ${MY_SCRIPTS})
     # Append "py" extension if needed
     get_filename_component(file_ext ${file} EXT)
@@ -53,22 +65,88 @@ macro(ctkMacroCompilePythonScript)
       file(RELATIVE_PATH tgt_file ${CMAKE_CURRENT_BINARY_DIR} ${file})
       set(tgt "${MY_DESTINATION_DIR}/${tgt_file}")
     endif()
-
-    list(APPEND input_python_files ${src})
-    add_custom_command(DEPENDS ${src}
-                        COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
-                        OUTPUT ${tgt}
-                        COMMENT "Copying python script: ${file}")
-    list(APPEND copied_python_files ${tgt})
+    
+    set_property(GLOBAL APPEND PROPERTY
+      _CTK_${target}_PYTHON_SCRIPTS "${src}|${tgt}")
   endforeach()
   
-  add_custom_target(Copy${MY_TARGET_NAME}PythonFiles DEPENDS ${input_python_files} ${copied_python_files})
   
-  # Byte compile the Python files.
-  set(compile_all_script "${CMAKE_CURRENT_BINARY_DIR}/compile_${MY_TARGET_NAME}_python_scripts.py")
+  set_property(GLOBAL APPEND PROPERTY
+      _CTK_${target}_PYTHON_DESTINATION_DIRS "${MY_DESTINATION_DIR}")
+      
+  if(DEFINED MY_RESOURCES)
+    set(resource_input_files)
+    foreach(file ${MY_RESOURCES})
+      set(src "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
+      set(tgt "${MY_DESTINATION_DIR}/${file}")
+      set_property(GLOBAL APPEND PROPERTY
+      _CTK_${target}_PYTHON_RESOURCES "${src}|${tgt}")
+    endforeach()
+  endif()
+
+  set(MY_DIRECTORY_TO_INSTALL ${MY_DESTINATION_DIR})
+  if(MY_NO_INSTALL_SUBDIR)
+    set(MY_DIRECTORY_TO_INSTALL ${MY_DESTINATION_DIR}/)
+  endif()
+
+  # Install python module / resources directory
+  install(DIRECTORY "${MY_DIRECTORY_TO_INSTALL}"
+    DESTINATION "${MY_INSTALL_DIR}" COMPONENT RuntimeLibraries
+    USE_SOURCE_PERMISSIONS)
   
-  # Generate compile_${MY_TARGET_NAME}_python_scripts.py
-  file(WRITE ${compile_all_script} "
+  if(NOT MY_GLOBAL_TARGET)
+    ctkFunctionAddCompilePythonScriptTargets(${target})
+  endif()
+endmacro()
+
+
+function(_ctk_add_copy_python_files_target target type)
+  # 'type' is expected to be either "Resource" or "Script"
+  set(target_name Copy${target}Python${type}Files)
+  if(NOT TARGET ${target_name})
+    string(TOUPPER ${type} type_upper)
+    get_property(entries GLOBAL PROPERTY _CTK_${target}_PYTHON_${type_upper}S)
+    set(input_files)
+    set(copied_files)
+    foreach(entry IN LISTS entries)
+      string(REPLACE "|" ";" tuple "${entry}")
+      list(GET tuple 0 src)
+      list(GET tuple 1 tgt)
+      get_filename_component(file ${src} NAME)
+      add_custom_command(DEPENDS ${src}
+                         COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
+                         OUTPUT ${tgt}
+                         COMMENT "Copying python ${type}: ${file}")
+      list(APPEND input_files ${src})
+      list(APPEND copied_files ${tgt})
+    endforeach()
+    if(entries)
+      add_custom_target(${target_name} ALL
+        DEPENDS
+          ${input_files}
+          ${copied_files}
+          )
+    endif()
+  endif()
+endfunction()
+
+
+function(_ctk_add_compile_python_directories_target target)
+  set(target_name Compile${target}PythonFiles)
+  if(NOT TARGET ${target_name}) 
+    # Byte compile the Python files.
+    set(compile_all_script "${CMAKE_CURRENT_BINARY_DIR}/compile_${target}_python_scripts.py")
+    
+    set(_compileall_code )
+    get_property(destination_dirs GLOBAL PROPERTY _CTK_${target}_PYTHON_DESTINATION_DIRS)
+    list(REMOVE_DUPLICATES destination_dirs)
+    foreach(destination_dir IN LISTS destination_dirs)
+      set(_compileall_code "${_compileall_code}\ncompileall.compile_dir('${destination_dir}')")
+    endforeach()
+  
+    message(STATUS "Generate [${target_name}] compileall script: ${compile_all_script}")
+    # Generate compile_${target}_python_scripts.py
+    file(WRITE ${compile_all_script} "
 #
 # Generated by ctkMacroCompilePythonScript CMAKE macro
 #
@@ -76,15 +154,21 @@ macro(ctkMacroCompilePythonScript)
 # Based on paraview/VTK/Wrapping/Python/compile_all_vtk.py.in
 
 import compileall
-compileall.compile_dir('@MY_DESTINATION_DIR@')
-file = open('@CMAKE_CURRENT_BINARY_DIR@/python_compile_@MY_TARGET_NAME@_complete', 'w')
+@_compileall_code@
+file = open('@CMAKE_CURRENT_BINARY_DIR@/python_compile_@target@_complete', 'w')
 file.write('Done')
 ")
 
-  # Configure cmake script associated with the custom command
-  # required to properly update the library path with PYTHON_LIBRARY_PATH
-  set(compile_all_cmake_script "${CMAKE_CURRENT_BINARY_DIR}/compile_${MY_TARGET_NAME}_python_scripts.cmake")
-  file(WRITE ${compile_all_cmake_script} "
+    find_package(PythonInterp REQUIRED)
+    find_package(PythonLibs REQUIRED)
+
+    # Extract python lib path
+    get_filename_component(PYTHON_LIBRARY_PATH ${PYTHON_LIBRARY} PATH)
+
+    # Configure cmake script associated with the custom command
+    # required to properly update the library path with PYTHON_LIBRARY_PATH
+    set(compile_all_cmake_script "${CMAKE_CURRENT_BINARY_DIR}/compile_${target}_python_scripts.cmake")
+    file(WRITE ${compile_all_cmake_script} "
 #
 # Generated by ctkMacroCompilePythonScript CMAKE macro
 #
@@ -102,49 +186,24 @@ execute_process(
   )
 ")
 
-  add_custom_command(
-    COMMAND ${CMAKE_COMMAND} -P ${compile_all_cmake_script}
-    DEPENDS ${copied_python_files}  ${compile_all_script}
-    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/python_compile_${MY_TARGET_NAME}_complete"
-    COMMENT "Compiling python scripts: ${MY_TARGET_NAME}"
-    )
-
-  add_custom_target(Compile${MY_TARGET_NAME}PythonFiles ALL
-    DEPENDS 
-      ${CMAKE_CURRENT_BINARY_DIR}/python_compile_${MY_TARGET_NAME}_complete
-      ${compile_all_script}
+    add_custom_command(
+      COMMAND ${CMAKE_COMMAND} -P ${compile_all_cmake_script}
+      DEPENDS Copy${target}PythonScriptFiles  ${compile_all_script}
+      OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/python_compile_${target}_complete"
+      COMMENT "Compiling python scripts: ${target}"
       )
-      
-  if(DEFINED MY_RESOURCES)
-    set(resource_input_files)
-    set(copied_resource_files)
-    foreach(file ${MY_RESOURCES})
-      set(src "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
-      set(tgt "${MY_DESTINATION_DIR}/${file}")
-      
-      list(APPEND resource_input_files ${src})
-      add_custom_command(DEPENDS ${src}
-                          COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
-                          OUTPUT ${tgt}
-                          COMMENT "Copying python resource: ${file}")
-      list(APPEND copied_resource_files ${tgt})
-    endforeach()
-    add_custom_target(Copy${MY_TARGET_NAME}PythonResourceFiles ALL
-      DEPENDS
-        ${resource_input_files} 
-        ${copied_resource_files}
-        )
-  endif()
 
-  set(MY_DIRECTORY_TO_INSTALL ${MY_DESTINATION_DIR})
-  if(MY_NO_INSTALL_SUBDIR)
-    set(MY_DIRECTORY_TO_INSTALL ${MY_DESTINATION_DIR}/)
+    add_custom_target(${target_name} ALL
+      DEPENDS 
+        ${CMAKE_CURRENT_BINARY_DIR}/python_compile_${target}_complete
+        ${compile_all_script}
+        )
   endif()
+endfunction()
 
-  # Install python module / resources directory
-  install(DIRECTORY "${MY_DIRECTORY_TO_INSTALL}"
-    DESTINATION "${MY_INSTALL_DIR}" COMPONENT RuntimeLibraries
-    USE_SOURCE_PERMISSIONS)
-       
-endmacro()
+function(ctkFunctionAddCompilePythonScriptTargets target)
+  _ctk_add_copy_python_files_target(${target} Script)
+  _ctk_add_copy_python_files_target(${target} Resource)
+  _ctk_add_compile_python_directories_target(${target})
+endfunction()