ctkMacroCompilePythonScript.cmake 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_files)
  27. SET(copied_files)
  28. SET(copied_python_files)
  29. FOREACH(file ${MY_SCRIPTS})
  30. # Append "py" extension if needed
  31. get_filename_component(file_ext ${file} EXT)
  32. IF(NOT "${file_ext}" MATCHES "py")
  33. SET(file "${file}.py")
  34. ENDIF()
  35. SET(src "${MY_SOURCE_DIR}/${file}")
  36. SET(tgt "${MY_DESTINATION_DIR}/${file}")
  37. IF(IS_ABSOLUTE ${file})
  38. SET(src ${file})
  39. file(RELATIVE_PATH tgt_file ${CMAKE_CURRENT_BINARY_DIR} ${file})
  40. SET(tgt "${MY_DESTINATION_DIR}/${tgt_file}")
  41. ENDIF()
  42. LIST(APPEND input_files ${src})
  43. ADD_CUSTOM_COMMAND(DEPENDS ${src}
  44. COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
  45. OUTPUT ${tgt}
  46. COMMENT "Copying python script: ${file}")
  47. LIST(APPEND copied_python_files ${tgt})
  48. LIST(APPEND copied_files ${tgt})
  49. ENDFOREACH()
  50. IF(DEFINED MY_RESOURCES)
  51. FOREACH(file ${MY_RESOURCES})
  52. SET(src "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
  53. SET(tgt "${MY_DESTINATION_DIR}/${file}")
  54. LIST(APPEND input_files ${src})
  55. ADD_CUSTOM_COMMAND(DEPENDS ${src}
  56. COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
  57. OUTPUT ${tgt}
  58. COMMENT "Copying python resource: ${file}")
  59. LIST(APPEND copied_files ${tgt})
  60. ENDFOREACH()
  61. ENDIF()
  62. IF(input_files)
  63. ADD_CUSTOM_TARGET(Copy${MY_TARGET_NAME}PythonFiles DEPENDS ${input_files} ${copied_files})
  64. ENDIF()
  65. # Byte compile the Python files.
  66. SET(compile_all_script "${CMAKE_CURRENT_BINARY_DIR}/compile_${MY_TARGET_NAME}_python_scripts.py")
  67. # Generate compile_${MY_TARGET_NAME}_python_scripts.py
  68. FILE(WRITE ${compile_all_script} "
  69. #
  70. # Generated by ctkMacroCompilePythonScript CMAKE macro
  71. #
  72. # Based on paraview/VTK/Wrapping/Python/compile_all_vtk.py.in
  73. import compileall
  74. compileall.compile_dir('@MY_DESTINATION_DIR@')
  75. file = open('@CMAKE_CURRENT_BINARY_DIR@/python_compile_@MY_TARGET_NAME@_complete', 'w')
  76. file.write('Done')
  77. ")
  78. # Configure cmake script associated with the custom command
  79. # required to properly update the library path with PYTHON_LIBRARY_PATH
  80. SET(compile_all_cmake_script "${CMAKE_CURRENT_BINARY_DIR}/compile_${MY_TARGET_NAME}_python_scripts.cmake")
  81. FILE(WRITE ${compile_all_cmake_script} "
  82. #
  83. # Generated by ctkMacroCompilePythonScript CMAKE macro
  84. #
  85. IF(WIN32)
  86. SET(ENV{PATH} \"@PYTHON_LIBRARY_PATH@;\$ENV{PATH}\")
  87. ELSEIF(APPLE)
  88. SET(ENV{DYLD_LIBRARY_PATH} \"@PYTHON_LIBRARY_PATH@:\$ENV{DYLD_LIBRARY_PATH}\")
  89. ELSE()
  90. SET(ENV{LD_LIBRARY_PATH} \"@PYTHON_LIBRARY_PATH@:\$ENV{LD_LIBRARY_PATH}\")
  91. ENDIF()
  92. EXECUTE_PROCESS(
  93. COMMAND \"@PYTHON_EXECUTABLE@\" \"@compile_all_script@\"
  94. )
  95. ")
  96. ADD_CUSTOM_COMMAND(
  97. COMMAND ${CMAKE_COMMAND} -P ${compile_all_cmake_script}
  98. DEPENDS ${copied_python_files} ${compile_all_script}
  99. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/python_compile_${MY_TARGET_NAME}_complete"
  100. COMMENT "Compiling python scripts: ${MY_TARGET_NAME}"
  101. )
  102. ADD_CUSTOM_TARGET(Compile${MY_TARGET_NAME}PythonFiles ALL
  103. DEPENDS
  104. ${CMAKE_CURRENT_BINARY_DIR}/python_compile_${MY_TARGET_NAME}_complete
  105. ${compile_all_script}
  106. )
  107. # Install python module directory
  108. INSTALL(DIRECTORY "${MY_DESTINATION_DIR}"
  109. DESTINATION "${MY_INSTALL_DIR}" COMPONENT Runtime
  110. USE_SOURCE_PERMISSIONS)
  111. ENDMACRO()