ctkMacroCompilePythonScript.cmake 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. "NO_INSTALL_SUBDIR"
  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. # Since 'add_custom_command' doesn't play nicely with path having multiple
  27. # consecutive slashes. Let's make sure there are no trailing slashes.
  28. get_filename_component(MY_SOURCE_DIR ${MY_SOURCE_DIR} REALPATH)
  29. get_filename_component(MY_DESTINATION_DIR ${MY_DESTINATION_DIR} REALPATH)
  30. set(input_python_files)
  31. set(copied_python_files)
  32. foreach(file ${MY_SCRIPTS})
  33. # Append "py" extension if needed
  34. get_filename_component(file_ext ${file} EXT)
  35. if(NOT "${file_ext}" MATCHES "py")
  36. set(file "${file}.py")
  37. endif()
  38. set(src "${MY_SOURCE_DIR}/${file}")
  39. set(tgt "${MY_DESTINATION_DIR}/${file}")
  40. if(IS_ABSOLUTE ${file})
  41. set(src ${file})
  42. file(RELATIVE_PATH tgt_file ${CMAKE_CURRENT_BINARY_DIR} ${file})
  43. set(tgt "${MY_DESTINATION_DIR}/${tgt_file}")
  44. endif()
  45. list(APPEND input_python_files ${src})
  46. add_custom_command(DEPENDS ${src}
  47. COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
  48. OUTPUT ${tgt}
  49. COMMENT "Copying python script: ${file}")
  50. list(APPEND copied_python_files ${tgt})
  51. endforeach()
  52. add_custom_target(Copy${MY_TARGET_NAME}PythonFiles DEPENDS ${input_python_files} ${copied_python_files})
  53. # Byte compile the Python files.
  54. set(compile_all_script "${CMAKE_CURRENT_BINARY_DIR}/compile_${MY_TARGET_NAME}_python_scripts.py")
  55. # Generate compile_${MY_TARGET_NAME}_python_scripts.py
  56. file(WRITE ${compile_all_script} "
  57. #
  58. # Generated by ctkMacroCompilePythonScript CMAKE macro
  59. #
  60. # Based on paraview/VTK/Wrapping/Python/compile_all_vtk.py.in
  61. import compileall
  62. compileall.compile_dir('@MY_DESTINATION_DIR@')
  63. file = open('@CMAKE_CURRENT_BINARY_DIR@/python_compile_@MY_TARGET_NAME@_complete', 'w')
  64. file.write('Done')
  65. ")
  66. # Configure cmake script associated with the custom command
  67. # required to properly update the library path with PYTHON_LIBRARY_PATH
  68. set(compile_all_cmake_script "${CMAKE_CURRENT_BINARY_DIR}/compile_${MY_TARGET_NAME}_python_scripts.cmake")
  69. file(WRITE ${compile_all_cmake_script} "
  70. #
  71. # Generated by ctkMacroCompilePythonScript CMAKE macro
  72. #
  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. )
  83. ")
  84. add_custom_command(
  85. COMMAND ${CMAKE_COMMAND} -P ${compile_all_cmake_script}
  86. DEPENDS ${copied_python_files} ${compile_all_script}
  87. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/python_compile_${MY_TARGET_NAME}_complete"
  88. COMMENT "Compiling python scripts: ${MY_TARGET_NAME}"
  89. )
  90. add_custom_target(Compile${MY_TARGET_NAME}PythonFiles ALL
  91. DEPENDS
  92. ${CMAKE_CURRENT_BINARY_DIR}/python_compile_${MY_TARGET_NAME}_complete
  93. ${compile_all_script}
  94. )
  95. if(DEFINED MY_RESOURCES)
  96. set(resource_input_files)
  97. set(copied_resource_files)
  98. foreach(file ${MY_RESOURCES})
  99. set(src "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
  100. set(tgt "${MY_DESTINATION_DIR}/${file}")
  101. list(APPEND resource_input_files ${src})
  102. add_custom_command(DEPENDS ${src}
  103. COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
  104. OUTPUT ${tgt}
  105. COMMENT "Copying python resource: ${file}")
  106. list(APPEND copied_resource_files ${tgt})
  107. endforeach()
  108. add_custom_target(Copy${MY_TARGET_NAME}PythonResourceFiles ALL
  109. DEPENDS
  110. ${resource_input_files}
  111. ${copied_resource_files}
  112. )
  113. endif()
  114. set(MY_DIRECTORY_TO_INSTALL ${MY_DESTINATION_DIR})
  115. if(MY_NO_INSTALL_SUBDIR)
  116. set(MY_DIRECTORY_TO_INSTALL ${MY_DESTINATION_DIR}/)
  117. endif()
  118. # Install python module / resources directory
  119. install(DIRECTORY "${MY_DIRECTORY_TO_INSTALL}"
  120. DESTINATION "${MY_INSTALL_DIR}" COMPONENT RuntimeLibraries
  121. USE_SOURCE_PERMISSIONS)
  122. endmacro()