ctkMacroCompilePythonScript.cmake 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #
  2. # Based on ParaView/VTK/Utilities/vtkTclTest2Py/CMakeLists.txt and
  3. # ParaView/VTK/Wrapping/Python/CMakeLists.txt
  4. #
  5. INCLUDE(${CTK_CMAKE_DIR}/ctkMacroParseArguments.cmake)
  6. #! \ingroup CMakeAPI
  7. MACRO(ctkMacroCompilePythonScript)
  8. ctkMacroParseArguments(MY
  9. "TARGET_NAME;SCRIPTS;RESOURCES;SOURCE_DIR;DESTINATION_DIR;INSTALL_DIR"
  10. ""
  11. ${ARGN}
  12. )
  13. FIND_PACKAGE(PythonInterp REQUIRED)
  14. FIND_PACKAGE(PythonLibs REQUIRED)
  15. # Extract python lib path
  16. get_filename_component(PYTHON_LIBRARY_PATH ${PYTHON_LIBRARY} PATH)
  17. # Sanity checks
  18. FOREACH(varname TARGET_NAME SCRIPTS DESTINATION_DIR INSTALL_DIR)
  19. IF(NOT DEFINED MY_${varname})
  20. MESSAGE(FATAL_ERROR "${varname} is mandatory")
  21. ENDIF()
  22. ENDFOREACH()
  23. IF(NOT DEFINED MY_SOURCE_DIR)
  24. SET(MY_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  25. ENDIF()
  26. SET(input_python_files)
  27. SET(copied_python_files)
  28. FOREACH(file ${MY_SCRIPTS})
  29. # Append "py" extension if needed
  30. get_filename_component(file_ext ${file} EXT)
  31. IF(NOT "${file_ext}" MATCHES "py")
  32. SET(file "${file}.py")
  33. ENDIF()
  34. SET(src "${MY_SOURCE_DIR}/${file}")
  35. SET(tgt "${MY_DESTINATION_DIR}/${file}")
  36. IF(IS_ABSOLUTE ${file})
  37. SET(src ${file})
  38. file(RELATIVE_PATH tgt_file ${CMAKE_CURRENT_BINARY_DIR} ${file})
  39. SET(tgt "${MY_DESTINATION_DIR}/${tgt_file}")
  40. ENDIF()
  41. LIST(APPEND input_python_files ${src})
  42. ADD_CUSTOM_COMMAND(DEPENDS ${src}
  43. COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
  44. OUTPUT ${tgt}
  45. COMMENT "Copying python script: ${file}")
  46. LIST(APPEND copied_python_files ${tgt})
  47. ENDFOREACH()
  48. ADD_CUSTOM_TARGET(Copy${MY_TARGET_NAME}PythonFiles DEPENDS ${input_python_files} ${copied_python_files})
  49. # Byte compile the Python files.
  50. SET(compile_all_script "${CMAKE_CURRENT_BINARY_DIR}/compile_${MY_TARGET_NAME}_python_scripts.py")
  51. # Generate compile_${MY_TARGET_NAME}_python_scripts.py
  52. FILE(WRITE ${compile_all_script} "
  53. #
  54. # Generated by ctkMacroCompilePythonScript CMAKE macro
  55. #
  56. # Based on paraview/VTK/Wrapping/Python/compile_all_vtk.py.in
  57. import compileall
  58. compileall.compile_dir('@MY_DESTINATION_DIR@')
  59. file = open('@CMAKE_CURRENT_BINARY_DIR@/python_compile_@MY_TARGET_NAME@_complete', 'w')
  60. file.write('Done')
  61. ")
  62. # Configure cmake script associated with the custom command
  63. # required to properly update the library path with PYTHON_LIBRARY_PATH
  64. SET(compile_all_cmake_script "${CMAKE_CURRENT_BINARY_DIR}/compile_${MY_TARGET_NAME}_python_scripts.cmake")
  65. FILE(WRITE ${compile_all_cmake_script} "
  66. #
  67. # Generated by ctkMacroCompilePythonScript CMAKE macro
  68. #
  69. IF(WIN32)
  70. SET(ENV{PATH} \"@PYTHON_LIBRARY_PATH@;\$ENV{PATH}\")
  71. ELSEIF(APPLE)
  72. SET(ENV{DYLD_LIBRARY_PATH} \"@PYTHON_LIBRARY_PATH@:\$ENV{DYLD_LIBRARY_PATH}\")
  73. ELSE()
  74. SET(ENV{LD_LIBRARY_PATH} \"@PYTHON_LIBRARY_PATH@:\$ENV{LD_LIBRARY_PATH}\")
  75. ENDIF()
  76. EXECUTE_PROCESS(
  77. COMMAND \"@PYTHON_EXECUTABLE@\" \"@compile_all_script@\"
  78. )
  79. ")
  80. ADD_CUSTOM_COMMAND(
  81. COMMAND ${CMAKE_COMMAND} -P ${compile_all_cmake_script}
  82. DEPENDS ${copied_python_files} ${compile_all_script}
  83. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/python_compile_${MY_TARGET_NAME}_complete"
  84. COMMENT "Compiling python scripts: ${MY_TARGET_NAME}"
  85. )
  86. ADD_CUSTOM_TARGET(Compile${MY_TARGET_NAME}PythonFiles ALL
  87. DEPENDS
  88. ${CMAKE_CURRENT_BINARY_DIR}/python_compile_${MY_TARGET_NAME}_complete
  89. ${compile_all_script}
  90. )
  91. IF(DEFINED MY_RESOURCES)
  92. SET(resource_input_files)
  93. SET(copied_resource_files)
  94. FOREACH(file ${MY_RESOURCES})
  95. SET(src "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
  96. SET(tgt "${MY_DESTINATION_DIR}/${file}")
  97. LIST(APPEND resource_input_files ${src})
  98. ADD_CUSTOM_COMMAND(DEPENDS ${src}
  99. COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
  100. OUTPUT ${tgt}
  101. COMMENT "Copying python resource: ${file}")
  102. LIST(APPEND copied_resource_files ${tgt})
  103. ENDFOREACH()
  104. ADD_CUSTOM_TARGET(Copy${MY_TARGET_NAME}PythonResourceFiles ALL
  105. DEPENDS
  106. ${resource_input_files}
  107. ${copied_resource_files}
  108. )
  109. ENDIF()
  110. # Install python module / resources directory
  111. INSTALL(DIRECTORY "${MY_DESTINATION_DIR}"
  112. DESTINATION "${MY_INSTALL_DIR}" COMPONENT Runtime
  113. USE_SOURCE_PERMISSIONS)
  114. ENDMACRO()