Python.cmake 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Filename: Python.cmake
  2. #
  3. # Description: This file provides support functions for building/installing
  4. # Python extension modules and/or pure-Python packages.
  5. #
  6. # Functions:
  7. # add_python_target(target [source1 [source2 ...]])
  8. # install_python_package(path [ARCH/LIB])
  9. #
  10. #
  11. # Function: add_python_target(target [source1 [source2 ...]])
  12. # Build the provided source(s) as a Python extension module, linked against the
  13. # Python runtime library.
  14. #
  15. # Note that this also takes care of installation, unlike other target creation
  16. # commands in CMake.
  17. #
  18. function(add_python_target target)
  19. if(NOT HAVE_PYTHON)
  20. return()
  21. endif()
  22. string(REGEX REPLACE "^.*\\." "" basename "${target}")
  23. set(sources ${ARGN})
  24. string(REGEX REPLACE "\\.[^.]+$" "" namespace "${target}")
  25. string(REPLACE "." "_" underscore_namespace "${namespace}")
  26. string(REPLACE "." "/" slash_namespace "${namespace}")
  27. add_library(${target} ${MODULE_TYPE} ${sources})
  28. target_link_libraries(${target} PKG::PYTHON)
  29. if(BUILD_SHARED_LIBS)
  30. set_target_properties(${target} PROPERTIES
  31. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${slash_namespace}"
  32. OUTPUT_NAME "${basename}"
  33. PREFIX ""
  34. SUFFIX "${PYTHON_EXTENSION_SUFFIX}")
  35. install(TARGETS ${target} DESTINATION "${PYTHON_ARCH_INSTALL_DIR}/${slash_namespace}")
  36. else()
  37. set_target_properties(${target} PROPERTIES
  38. OUTPUT_NAME "${basename}"
  39. PREFIX "libpython_${underscore_namespace}_")
  40. install(TARGETS ${target} DESTINATION lib)
  41. endif()
  42. endfunction(add_python_target)
  43. #
  44. # Function: install_python_package(path [ARCH/LIB])
  45. #
  46. # Installs the Python package which was built at `path`.
  47. #
  48. # Note that this handles more than just installation; it will also invoke
  49. # Python's compileall utility to pregenerate .pyc/.pyo files. This will only
  50. # happen if the Python interpreter is found.
  51. #
  52. # The ARCH or LIB keyword may be used to specify whether this package should be
  53. # installed into Python's architecture-dependent or architecture-independent
  54. # package path. The default, if unspecified, is LIB.
  55. #
  56. function(install_python_package path)
  57. if(ARGN STREQUAL "ARCH")
  58. set(type "ARCH")
  59. elseif(ARGN STREQUAL "LIB")
  60. set(type "LIB")
  61. elseif(ARGN STREQUAL "")
  62. set(type "LIB")
  63. else()
  64. message(FATAL_ERROR "install_python_package got unexpected argument: ${ARGN}")
  65. endif()
  66. get_filename_component(package_name "${path}" NAME)
  67. set(custom_target "bytecompile_${package_name}")
  68. file(RELATIVE_PATH relpath "${PROJECT_BINARY_DIR}" "${path}")
  69. if(PYTHON_EXECUTABLE)
  70. add_custom_target(${custom_target} ALL)
  71. add_custom_command(
  72. TARGET ${custom_target}
  73. WORKING_DIRECTORY "${PROJECT_BINARY_DIR}"
  74. COMMAND "${PYTHON_EXECUTABLE}" -m compileall -q "${relpath}")
  75. add_custom_command(
  76. TARGET ${custom_target}
  77. WORKING_DIRECTORY "${PROJECT_BINARY_DIR}"
  78. COMMAND "${PYTHON_EXECUTABLE}" -OO -m compileall -q "${relpath}")
  79. endif()
  80. set(dir ${PYTHON_${type}_INSTALL_DIR})
  81. if(dir)
  82. install(DIRECTORY "${path}" DESTINATION "${dir}"
  83. FILES_MATCHING REGEX "\\.py[co]?$")
  84. endif()
  85. endfunction(install_python_package)