ctkMacroCompilePythonScript.cmake 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. set(CTK_PYTHON_COMPILE_FILE_SCRIPT_DIR "${CMAKE_BINARY_DIR}/CMakeFiles")
  18. #! \ingroup CMakeAPI
  19. macro(ctkMacroCompilePythonScript)
  20. ctkMacroParseArguments(MY
  21. "TARGET_NAME;SCRIPTS;RESOURCES;SOURCE_DIR;DESTINATION_DIR;INSTALL_DIR"
  22. "NO_INSTALL_SUBDIR;GLOBAL_TARGET"
  23. ${ARGN}
  24. )
  25. # Sanity checks
  26. foreach(varname TARGET_NAME SCRIPTS DESTINATION_DIR INSTALL_DIR)
  27. if(NOT DEFINED MY_${varname})
  28. message(FATAL_ERROR "${varname} is mandatory")
  29. endif()
  30. endforeach()
  31. if(NOT DEFINED MY_SOURCE_DIR)
  32. set(MY_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  33. endif()
  34. if("${CTK_COMPILE_PYTHON_SCRIPTS_GLOBAL_TARGET_NAME}" STREQUAL "")
  35. set(target ${MY_TARGET_NAME})
  36. else()
  37. set(MY_GLOBAL_TARGET TRUE)
  38. set(target ${CTK_COMPILE_PYTHON_SCRIPTS_GLOBAL_TARGET_NAME})
  39. endif()
  40. # Since 'add_custom_command' doesn't play nicely with path having multiple
  41. # consecutive slashes. Let's make sure there are no trailing slashes.
  42. get_filename_component(MY_SOURCE_DIR ${MY_SOURCE_DIR} REALPATH)
  43. get_filename_component(MY_DESTINATION_DIR ${MY_DESTINATION_DIR} REALPATH)
  44. set(input_python_files)
  45. foreach(file ${MY_SCRIPTS})
  46. # Append "py" extension if needed
  47. get_filename_component(file_ext ${file} EXT)
  48. if(NOT "${file_ext}" MATCHES "py")
  49. set(file "${file}.py")
  50. endif()
  51. set(src "${MY_SOURCE_DIR}/${file}")
  52. set(tgt_file ${file})
  53. if(IS_ABSOLUTE ${file})
  54. set(src ${file})
  55. file(RELATIVE_PATH tgt_file ${CMAKE_CURRENT_BINARY_DIR} ${file})
  56. endif()
  57. set_property(GLOBAL APPEND PROPERTY
  58. _CTK_${target}_PYTHON_SCRIPTS "${src}|${tgt_file}|${MY_DESTINATION_DIR}")
  59. endforeach()
  60. if(DEFINED MY_RESOURCES)
  61. set(resource_input_files)
  62. foreach(file ${MY_RESOURCES})
  63. set(src "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
  64. set_property(GLOBAL APPEND PROPERTY
  65. _CTK_${target}_PYTHON_RESOURCES "${src}|${file}|${MY_DESTINATION_DIR}")
  66. endforeach()
  67. endif()
  68. set(MY_DIRECTORY_TO_INSTALL ${MY_DESTINATION_DIR})
  69. if(MY_NO_INSTALL_SUBDIR)
  70. set(MY_DIRECTORY_TO_INSTALL ${MY_DESTINATION_DIR}/)
  71. endif()
  72. # Install python module / resources directory
  73. install(DIRECTORY "${MY_DIRECTORY_TO_INSTALL}"
  74. DESTINATION "${MY_INSTALL_DIR}" COMPONENT RuntimeLibraries
  75. USE_SOURCE_PERMISSIONS)
  76. if(NOT MY_GLOBAL_TARGET)
  77. ctkFunctionAddCompilePythonScriptTargets(${target})
  78. endif()
  79. endmacro()
  80. function(_ctk_add_copy_python_files_target target type)
  81. # 'type' is expected to be either "Resource" or "Script"
  82. set(target_name Copy${target}Python${type}Files)
  83. if(NOT TARGET ${target_name})
  84. string(TOUPPER ${type} type_upper)
  85. get_property(entries GLOBAL PROPERTY _CTK_${target}_PYTHON_${type_upper}S)
  86. set(input_files)
  87. set(copied_files)
  88. foreach(entry IN LISTS entries)
  89. string(REPLACE "|" ";" tuple "${entry}")
  90. list(GET tuple 0 src)
  91. list(GET tuple 1 tgt_file)
  92. list(GET tuple 2 dest_dir)
  93. set(tgt ${dest_dir}/${tgt_file})
  94. add_custom_command(DEPENDS ${src}
  95. COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
  96. OUTPUT ${tgt}
  97. COMMENT "Copying python ${type}: ${tgt_file}")
  98. list(APPEND input_files ${src})
  99. list(APPEND copied_files ${tgt})
  100. endforeach()
  101. if(entries)
  102. add_custom_target(${target_name} ALL
  103. DEPENDS
  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. find_package(PythonInterp REQUIRED)
  125. find_package(PythonLibs REQUIRED)
  126. # Extract python lib path
  127. get_filename_component(PYTHON_LIBRARY_PATH ${PYTHON_LIBRARY} PATH)
  128. # Configure cmake script associated with the custom command
  129. # required to properly update the library path with PYTHON_LIBRARY_PATH
  130. set(compile_all_cmake_script "${CMAKE_CURRENT_BINARY_DIR}/compile_${target}_python_scripts.cmake")
  131. configure_file(
  132. ${CTK_CMAKE_DIR}/ctk_compile_python_scripts.cmake.in
  133. ${compile_all_cmake_script}
  134. @ONLY
  135. )
  136. add_custom_command(
  137. COMMAND ${CMAKE_COMMAND} -P ${compile_all_cmake_script}
  138. DEPENDS Copy${target}PythonScriptFiles ${compile_all_cmake_script}
  139. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/python_compile_${target}_complete"
  140. COMMENT "Compiling python scripts: ${target}"
  141. )
  142. add_custom_target(${target_name} ALL
  143. DEPENDS
  144. ${CMAKE_CURRENT_BINARY_DIR}/python_compile_${target}_complete
  145. )
  146. endif()
  147. endfunction()
  148. function(ctkFunctionAddCompilePythonScriptTargets target)
  149. _ctk_add_copy_python_files_target(${target} Script)
  150. _ctk_add_copy_python_files_target(${target} Resource)
  151. _ctk_add_compile_python_directories_target(${target})
  152. endfunction()