Bladeren bron

Re-create python compile script only if needed and fix dependency. See #449

Jean-Christophe Fillion-Robin 11 jaren geleden
bovenliggende
commit
db3f4d9929
3 gewijzigde bestanden met toevoegingen van 104 en 104 verwijderingen
  1. 6 104
      CMake/ctkMacroCompilePythonScript.cmake
  2. 97 0
      CMake/ctk_compile_python_scripts.cmake.in
  3. 1 0
      CMakeLists.txt

+ 6 - 104
CMake/ctkMacroCompilePythonScript.cmake

@@ -153,112 +153,15 @@ function(_ctk_add_compile_python_directories_target target)
     # 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
-#
-
-# 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
-# using python 2.6 works.
-#
-# 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.
-
-    Arguments (only fullname is required):
-
-    fullname:  the file to byte-compile
-    ddir:      if given, the directory name compiled in to the
-               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:
-        dfile = os.path.join(ddir, name)
-    else:
-        dfile = None
-    if rx is not None:
-        mo = rx.search(fullname)
-        if mo:
-            return success
-    if os.path.isfile(fullname):
-        head, tail = name[:-3], name[-3:]
-        if tail == '.py':
-            if not force:
-                try:
-                    mtime = int(os.stat(fullname).st_mtime)
-                    expect = struct.pack('<4sl', imp.get_magic(), mtime)
-                    cfile = fullname + (__debug__ and 'c' or 'o')
-                    with open(cfile, 'rb') as chandle:
-                        actual = chandle.read(8)
-                    if expect == actual:
-                        return success
-                except IOError:
-                    pass
-            if not quiet:
-                print 'Compiling', fullname, '...'
-            try:
-                ok = py_compile.compile(fullname, None, dfile, True)
-            except py_compile.PyCompileError,err:
-                if quiet:
-                    print 'Compiling', fullname, '...'
-                print err.msg
-                success = 0
-            except IOError, e:
-                print 'Sorry', e
-                success = 0
-            else:
-                if ok == 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')
-\")
-
-set(ENV{PYTHONPATH} \"@CTK_PYTHON_COMPILE_FILE_SCRIPT_DIR@\")
-
-if(WIN32)
-  set(ENV{PATH} \"@PYTHON_LIBRARY_PATH@;\$ENV{PATH}\")
-elseif(APPLE)
-  set(ENV{DYLD_LIBRARY_PATH} \"@PYTHON_LIBRARY_PATH@:\$ENV{DYLD_LIBRARY_PATH}\")
-else()
-  set(ENV{LD_LIBRARY_PATH} \"@PYTHON_LIBRARY_PATH@:\$ENV{LD_LIBRARY_PATH}\")
-endif()
-
-execute_process(
-  COMMAND \"@PYTHON_EXECUTABLE@\" \"@compile_all_script@\"
-  RESULT_VARIABLE result_var
-  )
-if(NOT result_var STREQUAL 0)
-  message(FATAL_ERROR \"Failed to compile python scripts: ${target} \")
-endif()
-")
+    configure_file(
+      ${CTK_CMAKE_DIR}/ctk_compile_python_scripts.cmake.in
+      ${compile_all_cmake_script}
+      @ONLY
+      )
 
     add_custom_command(
       COMMAND ${CMAKE_COMMAND} -P ${compile_all_cmake_script}
-      DEPENDS Copy${target}PythonScriptFiles
+      DEPENDS Copy${target}PythonScriptFiles ${compile_all_cmake_script}
       OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/python_compile_${target}_complete"
       COMMENT "Compiling python scripts: ${target}"
       )
@@ -266,7 +169,6 @@ endif()
     add_custom_target(${target_name} ALL
       DEPENDS 
         ${CMAKE_CURRENT_BINARY_DIR}/python_compile_${target}_complete
-        ${compile_all_cmake_script}
         )
   endif()
 endfunction()

+ 97 - 0
CMake/ctk_compile_python_scripts.cmake.in

@@ -0,0 +1,97 @@
+
+# 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
+# using python 2.6 works.
+#
+# 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.
+
+    Arguments (only fullname is required):
+
+    fullname:  the file to byte-compile
+    ddir:      if given, the directory name compiled in to the
+               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:
+        dfile = os.path.join(ddir, name)
+    else:
+        dfile = None
+    if rx is not None:
+        mo = rx.search(fullname)
+        if mo:
+            return success
+    if os.path.isfile(fullname):
+        head, tail = name[:-3], name[-3:]
+        if tail == '.py':
+            if not force:
+                try:
+                    mtime = int(os.stat(fullname).st_mtime)
+                    expect = struct.pack('<4sl', imp.get_magic(), mtime)
+                    cfile = fullname + (__debug__ and 'c' or 'o')
+                    with open(cfile, 'rb') as chandle:
+                        actual = chandle.read(8)
+                    if expect == actual:
+                        return success
+                except IOError:
+                    pass
+            if not quiet:
+                print 'Compiling', fullname, '...'
+            try:
+                ok = py_compile.compile(fullname, None, dfile, True)
+            except py_compile.PyCompileError,err:
+                if quiet:
+                    print 'Compiling', fullname, '...'
+                print err.msg
+                success = 0
+            except IOError, e:
+                print 'Sorry', e
+                success = 0
+            else:
+                if ok == 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')
+")
+
+set(ENV{PYTHONPATH} "@CTK_PYTHON_COMPILE_FILE_SCRIPT_DIR@")
+
+if(WIN32)
+  set(ENV{PATH} "@PYTHON_LIBRARY_PATH@;$ENV{PATH}")
+elseif(APPLE)
+  set(ENV{DYLD_LIBRARY_PATH} "@PYTHON_LIBRARY_PATH@:$ENV{DYLD_LIBRARY_PATH}")
+else()
+  set(ENV{LD_LIBRARY_PATH} "@PYTHON_LIBRARY_PATH@:$ENV{LD_LIBRARY_PATH}")
+endif()
+
+execute_process(
+  COMMAND "@PYTHON_EXECUTABLE@" "@compile_all_script@"
+  RESULT_VARIABLE result_var
+  )
+if(NOT result_var STREQUAL 0)
+  message(FATAL_ERROR "Failed to compile python scripts: @target@ ")
+endif()

+ 1 - 0
CMakeLists.txt

@@ -229,6 +229,7 @@ endforeach()
 foreach(file
   Libs/ctkExport.h.in
   CMake/ctkLinkerAsNeededFlagCheck.cmake
+  CMake/ctk_compile_python_scripts.cmake.in
   )
   install(FILES ${file} DESTINATION ${CTK_INSTALL_CMAKE_DIR} COMPONENT Development)
 endforeach()