ctk_compile_python_scripts.cmake.in 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Generate compile_@target@_python_scripts.py
  2. file(WRITE "@compile_all_script@" "
  3. #
  4. # Generated by ctkMacroCompilePythonScript CMake macro
  5. #
  6. #
  7. # Copied function 'compileall.compile_file' introduced in python 2.7 so that code compiled
  8. # using python 2.6 works.
  9. #
  10. # This version of the function has been copied from:
  11. # https://github.com/jonashaag/cpython/blob/ce5e5df0c9d8098da05dee26e12ffe2aa331889e/Lib/compileall.py#L61-111
  12. #
  13. import os
  14. import sys
  15. import py_compile
  16. import struct
  17. import imp
  18. def ctk_compile_file(fullname, ddir=None, force=0, rx=None, quiet=0):
  19. \"\"\"Byte-compile one file.
  20. Arguments (only fullname is required):
  21. fullname: the file to byte-compile
  22. ddir: if given, the directory name compiled in to the
  23. byte-code file.
  24. force: if 1, force compilation, even if timestamps are up-to-date
  25. quiet: if 1, be quiet during compilation
  26. \"\"\"
  27. success = 1
  28. name = os.path.basename(fullname)
  29. if ddir is not None:
  30. dfile = os.path.join(ddir, name)
  31. else:
  32. dfile = None
  33. if rx is not None:
  34. mo = rx.search(fullname)
  35. if mo:
  36. return success
  37. if os.path.isfile(fullname):
  38. head, tail = name[:-3], name[-3:]
  39. if tail == '.py':
  40. if not force:
  41. try:
  42. mtime = int(os.stat(fullname).st_mtime)
  43. expect = struct.pack('<4sl', imp.get_magic(), mtime)
  44. cfile = fullname + (__debug__ and 'c' or 'o')
  45. with open(cfile, 'rb') as chandle:
  46. actual = chandle.read(8)
  47. if expect == actual:
  48. return success
  49. except IOError:
  50. pass
  51. if not quiet:
  52. print 'Compiling', fullname, '...'
  53. try:
  54. ok = py_compile.compile(fullname, None, dfile, True)
  55. except py_compile.PyCompileError,err:
  56. if quiet:
  57. print 'Compiling', fullname, '...'
  58. print err.msg
  59. success = 0
  60. except IOError, e:
  61. print 'Sorry', e
  62. success = 0
  63. else:
  64. if ok == 0:
  65. success = 0
  66. return success
  67. # Based on paraview/VTK/Wrapping/Python/compile_all_vtk.py.in
  68. @_compileall_code@
  69. file = open('@CMAKE_CURRENT_BINARY_DIR@/python_compile_@target@_complete', 'w')
  70. file.write('Done')
  71. ")
  72. set(ENV{PYTHONPATH} "@CTK_PYTHON_COMPILE_FILE_SCRIPT_DIR@")
  73. if(WIN32)
  74. set(ENV{PATH} "@PYTHON_LIBRARY_PATH@;$ENV{PATH}")
  75. elseif(APPLE)
  76. set(ENV{DYLD_LIBRARY_PATH} "@PYTHON_LIBRARY_PATH@:$ENV{DYLD_LIBRARY_PATH}")
  77. else()
  78. set(ENV{LD_LIBRARY_PATH} "@PYTHON_LIBRARY_PATH@:$ENV{LD_LIBRARY_PATH}")
  79. endif()
  80. execute_process(
  81. COMMAND "@PYTHON_EXECUTABLE@" "@compile_all_script@"
  82. RESULT_VARIABLE result_var
  83. )
  84. if(NOT result_var STREQUAL 0)
  85. message(FATAL_ERROR "Failed to compile python scripts: @target@ ")
  86. endif()