ctkMacroCompilePythonScript.cmake 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #
  2. # Based on ParaView/VTK/Utilities/vtkTclTest2Py/CMakeLists.txt and
  3. # ParaView/VTK/Wrapping/Python/CMakeLists.txt
  4. #
  5. #
  6. # By globally defining the variable CTK_COMPILE_PYTHON_SCRIPTS_GLOBAL_TARGET_NAME to a
  7. # non-empty string or by specifying the macro option 'GLOBAL_TARGET',
  8. # the following targets will be defined for the whole build system:
  9. # - Copy<GLOBAL_TARGET_NAME>PythonResourceFiles
  10. # - Copy<GLOBAL_TARGET_NAME>PythonScriptFiles
  11. # - Compile<GLOBAL_TARGET_NAME>PythonFiles
  12. #
  13. # For complex projects, this can help reducing the number of targets and
  14. # simplify the manual rebuild of copy and compile targets.
  15. #
  16. include(${CTK_CMAKE_DIR}/ctkMacroParseArguments.cmake)
  17. #! \ingroup CMakeAPI
  18. macro(ctkMacroCompilePythonScript)
  19. ctkMacroParseArguments(MY
  20. "TARGET_NAME;SCRIPTS;RESOURCES;SOURCE_DIR;DESTINATION_DIR;INSTALL_DIR"
  21. "NO_INSTALL_SUBDIR;GLOBAL_TARGET"
  22. ${ARGN}
  23. )
  24. # Sanity checks
  25. foreach(varname TARGET_NAME SCRIPTS DESTINATION_DIR INSTALL_DIR)
  26. if(NOT DEFINED MY_${varname})
  27. message(FATAL_ERROR "${varname} is mandatory")
  28. endif()
  29. endforeach()
  30. if(NOT DEFINED MY_SOURCE_DIR)
  31. set(MY_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  32. endif()
  33. if("${CTK_COMPILE_PYTHON_SCRIPTS_GLOBAL_TARGET_NAME}" STREQUAL "")
  34. set(target ${MY_TARGET_NAME})
  35. else()
  36. set(MY_GLOBAL_TARGET TRUE)
  37. set(target ${CTK_COMPILE_PYTHON_SCRIPTS_GLOBAL_TARGET_NAME})
  38. endif()
  39. # Since 'add_custom_command' doesn't play nicely with path having multiple
  40. # consecutive slashes. Let's make sure there are no trailing slashes.
  41. get_filename_component(MY_SOURCE_DIR ${MY_SOURCE_DIR} REALPATH)
  42. get_filename_component(MY_DESTINATION_DIR ${MY_DESTINATION_DIR} REALPATH)
  43. set(input_python_files)
  44. foreach(file ${MY_SCRIPTS})
  45. # Append "py" extension if needed
  46. get_filename_component(file_ext ${file} EXT)
  47. if(NOT "${file_ext}" MATCHES "py")
  48. set(file "${file}.py")
  49. endif()
  50. set(src "${MY_SOURCE_DIR}/${file}")
  51. set(tgt_file ${file})
  52. if(IS_ABSOLUTE ${file})
  53. set(src ${file})
  54. file(RELATIVE_PATH tgt_file ${CMAKE_CURRENT_BINARY_DIR} ${file})
  55. endif()
  56. set_property(GLOBAL APPEND PROPERTY
  57. _CTK_${target}_PYTHON_SCRIPTS "${src}|${tgt_file}|${MY_DESTINATION_DIR}")
  58. endforeach()
  59. if(DEFINED MY_RESOURCES)
  60. set(resource_input_files)
  61. foreach(file ${MY_RESOURCES})
  62. set(src "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
  63. set_property(GLOBAL APPEND PROPERTY
  64. _CTK_${target}_PYTHON_RESOURCES "${src}|${file}|${MY_DESTINATION_DIR}")
  65. endforeach()
  66. endif()
  67. set(MY_DIRECTORY_TO_INSTALL ${MY_DESTINATION_DIR})
  68. if(MY_NO_INSTALL_SUBDIR)
  69. set(MY_DIRECTORY_TO_INSTALL ${MY_DESTINATION_DIR}/)
  70. endif()
  71. # Install python module / resources directory
  72. install(DIRECTORY "${MY_DIRECTORY_TO_INSTALL}"
  73. DESTINATION "${MY_INSTALL_DIR}" COMPONENT RuntimeLibraries
  74. USE_SOURCE_PERMISSIONS)
  75. if(NOT MY_GLOBAL_TARGET)
  76. ctkFunctionAddCompilePythonScriptTargets(${target})
  77. endif()
  78. endmacro()
  79. function(_ctk_add_copy_python_files_target target type)
  80. # 'type' is expected to be either "Resource" or "Script"
  81. set(target_name Copy${target}Python${type}Files)
  82. if(NOT TARGET ${target_name})
  83. string(TOUPPER ${type} type_upper)
  84. get_property(entries GLOBAL PROPERTY _CTK_${target}_PYTHON_${type_upper}S)
  85. set(input_files)
  86. set(copied_files)
  87. foreach(entry IN LISTS entries)
  88. string(REPLACE "|" ";" tuple "${entry}")
  89. list(GET tuple 0 src)
  90. list(GET tuple 1 tgt_file)
  91. list(GET tuple 2 dest_dir)
  92. set(tgt ${dest_dir}/${tgt_file})
  93. add_custom_command(DEPENDS ${src}
  94. COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
  95. OUTPUT ${tgt}
  96. COMMENT "Copying python ${type}: ${tgt_file}")
  97. list(APPEND input_files ${src})
  98. list(APPEND copied_files ${tgt})
  99. endforeach()
  100. if(entries)
  101. add_custom_target(${target_name} ALL
  102. DEPENDS
  103. ${input_files}
  104. ${copied_files}
  105. )
  106. endif()
  107. endif()
  108. endfunction()
  109. function(_ctk_add_compile_python_directories_target target)
  110. set(target_name Compile${target}PythonFiles)
  111. if(NOT TARGET ${target_name})
  112. # Byte compile the Python files.
  113. set(compile_all_script "${CMAKE_CURRENT_BINARY_DIR}/compile_${target}_python_scripts.py")
  114. set(_compileall_code )
  115. get_property(entries GLOBAL PROPERTY _CTK_${target}_PYTHON_SCRIPTS)
  116. list(REMOVE_DUPLICATES entries)
  117. foreach(entry IN LISTS entries)
  118. string(REPLACE "|" ";" tuple "${entry}")
  119. list(GET tuple 1 tgt_file)
  120. list(GET tuple 2 dest_dir)
  121. set(tgt ${dest_dir}/${tgt_file})
  122. set(_compileall_code "${_compileall_code}\nctk_compile_file('${tgt}', force=1)")
  123. endforeach()
  124. message(STATUS "Generate [${target_name}] py_compile script: ${compile_all_script}")
  125. # Generate compile_${target}_python_scripts.py
  126. file(WRITE ${compile_all_script} "
  127. #
  128. # Generated by ctkMacroCompilePythonScript CMAKE macro
  129. #
  130. # Based on paraview/VTK/Wrapping/Python/compile_all_vtk.py.in
  131. import os
  132. import sys
  133. import py_compile
  134. import struct
  135. import imp
  136. #
  137. # Copied function 'compileall.compile_file' introduced in python 2.7 so that code compiled
  138. # using python 2.6 works.
  139. #
  140. # This version of the function has been copied from:
  141. # https://github.com/jonashaag/cpython/blob/ce5e5df0c9d8098da05dee26e12ffe2aa331889e/Lib/compileall.py#L61-111
  142. #
  143. def ctk_compile_file(fullname, ddir=None, force=0, rx=None, quiet=0):
  144. \"\"\"Byte-compile one file.
  145. Arguments (only fullname is required):
  146. fullname: the file to byte-compile
  147. ddir: if given, the directory name compiled in to the
  148. byte-code file.
  149. force: if 1, force compilation, even if timestamps are up-to-date
  150. quiet: if 1, be quiet during compilation
  151. \"\"\"
  152. success = 1
  153. name = os.path.basename(fullname)
  154. if ddir is not None:
  155. dfile = os.path.join(ddir, name)
  156. else:
  157. dfile = None
  158. if rx is not None:
  159. mo = rx.search(fullname)
  160. if mo:
  161. return success
  162. if os.path.isfile(fullname):
  163. head, tail = name[:-3], name[-3:]
  164. if tail == '.py':
  165. if not force:
  166. try:
  167. mtime = int(os.stat(fullname).st_mtime)
  168. expect = struct.pack('<4sl', imp.get_magic(), mtime)
  169. cfile = fullname + (__debug__ and 'c' or 'o')
  170. with open(cfile, 'rb') as chandle:
  171. actual = chandle.read(8)
  172. if expect == actual:
  173. return success
  174. except IOError:
  175. pass
  176. if not quiet:
  177. print 'Compiling', fullname, '...'
  178. try:
  179. ok = py_compile.compile(fullname, None, dfile, True)
  180. except py_compile.PyCompileError,err:
  181. if quiet:
  182. print 'Compiling', fullname, '...'
  183. print err.msg
  184. success = 0
  185. except IOError, e:
  186. print "Sorry", e
  187. success = 0
  188. else:
  189. if ok == 0:
  190. success = 0
  191. return success
  192. @_compileall_code@
  193. file = open('@CMAKE_CURRENT_BINARY_DIR@/python_compile_@target@_complete', 'w')
  194. file.write('Done')
  195. ")
  196. find_package(PythonInterp REQUIRED)
  197. find_package(PythonLibs REQUIRED)
  198. # Extract python lib path
  199. get_filename_component(PYTHON_LIBRARY_PATH ${PYTHON_LIBRARY} PATH)
  200. # Configure cmake script associated with the custom command
  201. # required to properly update the library path with PYTHON_LIBRARY_PATH
  202. set(compile_all_cmake_script "${CMAKE_CURRENT_BINARY_DIR}/compile_${target}_python_scripts.cmake")
  203. file(WRITE ${compile_all_cmake_script} "
  204. #
  205. # Generated by ctkMacroCompilePythonScript CMAKE macro
  206. #
  207. if(WIN32)
  208. set(ENV{PATH} \"@PYTHON_LIBRARY_PATH@;\$ENV{PATH}\")
  209. elseif(APPLE)
  210. set(ENV{DYLD_LIBRARY_PATH} \"@PYTHON_LIBRARY_PATH@:\$ENV{DYLD_LIBRARY_PATH}\")
  211. else()
  212. set(ENV{LD_LIBRARY_PATH} \"@PYTHON_LIBRARY_PATH@:\$ENV{LD_LIBRARY_PATH}\")
  213. endif()
  214. execute_process(
  215. COMMAND \"@PYTHON_EXECUTABLE@\" \"@compile_all_script@\"
  216. )
  217. ")
  218. add_custom_command(
  219. COMMAND ${CMAKE_COMMAND} -P ${compile_all_cmake_script}
  220. DEPENDS Copy${target}PythonScriptFiles ${compile_all_script}
  221. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/python_compile_${target}_complete"
  222. COMMENT "Compiling python scripts: ${target}"
  223. )
  224. add_custom_target(${target_name} ALL
  225. DEPENDS
  226. ${CMAKE_CURRENT_BINARY_DIR}/python_compile_${target}_complete
  227. ${compile_all_script}
  228. )
  229. endif()
  230. endfunction()
  231. function(ctkFunctionAddCompilePythonScriptTargets target)
  232. _ctk_add_copy_python_files_target(${target} Script)
  233. _ctk_add_copy_python_files_target(${target} Resource)
  234. _ctk_add_compile_python_directories_target(${target})
  235. endfunction()