Python.cmake 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. add_library(${target} ${MODULE_TYPE} ${sources})
  25. target_link_libraries(${target} PKG::PYTHON)
  26. if(BUILD_SHARED_LIBS)
  27. set_target_properties(${target} PROPERTIES
  28. LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/panda3d"
  29. OUTPUT_NAME "${basename}"
  30. PREFIX ""
  31. SUFFIX "${PYTHON_EXTENSION_SUFFIX}")
  32. install(TARGETS ${target} DESTINATION "${PYTHON_ARCH_INSTALL_DIR}/panda3d")
  33. else()
  34. set_target_properties(${target} PROPERTIES
  35. OUTPUT_NAME "${basename}"
  36. PREFIX "libpython_panda3d_")
  37. install(TARGETS ${target} DESTINATION lib)
  38. endif()
  39. endfunction(add_python_target)
  40. #
  41. # Function: install_python_package(path [ARCH/LIB])
  42. #
  43. # Installs the Python package which was built at `path`.
  44. #
  45. # Note that this handles more than just installation; it will also invoke
  46. # Python's compileall utility to pregenerate .pyc/.pyo files. This will only
  47. # happen if the Python interpreter is found.
  48. #
  49. # The ARCH or LIB keyword may be used to specify whether this package should be
  50. # installed into Python's architecture-dependent or architecture-independent
  51. # package path. The default, if unspecified, is LIB.
  52. #
  53. function(install_python_package path)
  54. if(ARGN STREQUAL "ARCH")
  55. set(type "ARCH")
  56. elseif(ARGN STREQUAL "LIB")
  57. set(type "LIB")
  58. elseif(ARGN STREQUAL "")
  59. set(type "LIB")
  60. else()
  61. message(FATAL_ERROR "install_python_package got unexpected argument: ${ARGN}")
  62. endif()
  63. get_filename_component(package_name "${path}" NAME)
  64. set(custom_target "bytecompile_${package_name}")
  65. file(RELATIVE_PATH relpath "${PROJECT_BINARY_DIR}" "${path}")
  66. if(PYTHON_EXECUTABLE)
  67. add_custom_target(${custom_target} ALL)
  68. add_custom_command(
  69. TARGET ${custom_target}
  70. WORKING_DIRECTORY "${PROJECT_BINARY_DIR}"
  71. COMMAND "${PYTHON_EXECUTABLE}" -m compileall -q "${relpath}")
  72. add_custom_command(
  73. TARGET ${custom_target}
  74. WORKING_DIRECTORY "${PROJECT_BINARY_DIR}"
  75. COMMAND "${PYTHON_EXECUTABLE}" -OO -m compileall -q "${relpath}")
  76. endif()
  77. set(dir ${PYTHON_${type}_INSTALL_DIR})
  78. if(dir)
  79. install(DIRECTORY "${path}" DESTINATION "${dir}"
  80. FILES_MATCHING REGEX "\\.py[co]?$")
  81. endif()
  82. endfunction(install_python_package)