瀏覽代碼

Regenerate python compile script at build time

Jean-Christophe Fillion-Robin 12 年之前
父節點
當前提交
720397f789
共有 1 個文件被更改,包括 33 次插入30 次删除
  1. 33 30
      CMake/ctkMacroCompilePythonScript.cmake

+ 33 - 30
CMake/ctkMacroCompilePythonScript.cmake

@@ -19,6 +19,8 @@
 
 include(${CTK_CMAKE_DIR}/ctkMacroParseArguments.cmake)
 
+set(CTK_PYTHON_COMPILE_FILE_SCRIPT_DIR "${CMAKE_BINARY_DIR}/CMakeFiles")
+
 #! \ingroup CMakeAPI
 macro(ctkMacroCompilePythonScript)
   ctkMacroParseArguments(MY
@@ -141,21 +143,26 @@ function(_ctk_add_compile_python_directories_target target)
       set(tgt ${dest_dir}/${tgt_file})
       set(_compileall_code "${_compileall_code}\nctk_compile_file('${tgt}', force=1)")
     endforeach()
-  
-    message(STATUS "Generate [${target_name}] py_compile script: ${compile_all_script}")
-    # Generate compile_${target}_python_scripts.py
-    file(WRITE ${compile_all_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
 #
 
-# Based on paraview/VTK/Wrapping/Python/compile_all_vtk.py.in
-
-import os
-import sys
-import py_compile
-import struct
-import imp
+# Generate compile_${target}_python_scripts.py
+file(WRITE \"@compile_all_script@\" \"
+#
+# Generated by ctkMacroCompilePythonScript CMake macro
+#
 
 #
 # Copied function 'compileall.compile_file' introduced in python 2.7 so that code compiled
@@ -164,8 +171,15 @@ import imp
 # This version of the function has been copied from:
 #   https://github.com/jonashaag/cpython/blob/ce5e5df0c9d8098da05dee26e12ffe2aa331889e/Lib/compileall.py#L61-111
 #
+
+import os
+import sys
+import py_compile
+import struct
+import imp
+
 def ctk_compile_file(fullname, ddir=None, force=0, rx=None, quiet=0):
-    \"\"\"Byte-compile one file.
+    \\\"\\\"\\\"Byte-compile one file.
 
     Arguments (only fullname is required):
 
@@ -174,7 +188,7 @@ def ctk_compile_file(fullname, ddir=None, force=0, rx=None, quiet=0):
                byte-code file.
     force:     if 1, force compilation, even if timestamps are up-to-date
     quiet:     if 1, be quiet during compilation
-    \"\"\"
+    \\\"\\\"\\\"
     success = 1
     name = os.path.basename(fullname)
     if ddir is not None:
@@ -216,28 +230,17 @@ def ctk_compile_file(fullname, ddir=None, force=0, rx=None, quiet=0):
                     success = 0
     return success
 
+# Based on paraview/VTK/Wrapping/Python/compile_all_vtk.py.in
 
 @_compileall_code@
 file = open('@CMAKE_CURRENT_BINARY_DIR@/python_compile_@target@_complete', 'w')
 file.write('Done')
-")
-
-    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
-#
+set(ENV{PYTHONPATH} \"@CTK_PYTHON_COMPILE_FILE_SCRIPT_DIR@\")
 
 if(WIN32)
-    set(ENV{PATH} \"@PYTHON_LIBRARY_PATH@;\$ENV{PATH}\")
+  set(ENV{PATH} \"@PYTHON_LIBRARY_PATH@;\$ENV{PATH}\")
 elseif(APPLE)
   set(ENV{DYLD_LIBRARY_PATH} \"@PYTHON_LIBRARY_PATH@:\$ENV{DYLD_LIBRARY_PATH}\")
 else()
@@ -251,7 +254,7 @@ execute_process(
 
     add_custom_command(
       COMMAND ${CMAKE_COMMAND} -P ${compile_all_cmake_script}
-      DEPENDS Copy${target}PythonScriptFiles  ${compile_all_script}
+      DEPENDS Copy${target}PythonScriptFiles
       OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/python_compile_${target}_complete"
       COMMENT "Compiling python scripts: ${target}"
       )
@@ -259,7 +262,7 @@ execute_process(
     add_custom_target(${target_name} ALL
       DEPENDS 
         ${CMAKE_CURRENT_BINARY_DIR}/python_compile_${target}_complete
-        ${compile_all_script}
+        ${compile_all_cmake_script}
         )
   endif()
 endfunction()