Python.cmake 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 [EXPORT exp] [COMPONENT comp]
  12. # [source1 [source2 ...]])
  13. # Build the provided source(s) as a Python extension module, linked against the
  14. # Python runtime library.
  15. #
  16. # Note that this also takes care of installation, unlike other target creation
  17. # commands in CMake. The EXPORT and COMPONENT keywords allow passing the
  18. # corresponding options to install(), but default to "Python" otherwise.
  19. #
  20. function(add_python_target target)
  21. if(NOT HAVE_PYTHON)
  22. return()
  23. endif()
  24. string(REGEX REPLACE "^.*\\." "" basename "${target}")
  25. set(sources)
  26. set(component "Python")
  27. set(export "Python")
  28. foreach(arg ${ARGN})
  29. if(arg STREQUAL "COMPONENT")
  30. set(keyword "component")
  31. elseif(arg STREQUAL "EXPORT")
  32. set(keyword "export")
  33. elseif(keyword)
  34. set(${keyword} "${arg}")
  35. unset(keyword)
  36. else()
  37. list(APPEND sources "${arg}")
  38. endif()
  39. endforeach(arg)
  40. string(REGEX REPLACE "\\.[^.]+$" "" namespace "${target}")
  41. string(REPLACE "." "/" slash_namespace "${namespace}")
  42. add_library(${target} ${MODULE_TYPE} ${sources})
  43. target_link_libraries(${target} PKG::PYTHON)
  44. if(BUILD_SHARED_LIBS)
  45. set(_outdir "${PROJECT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${slash_namespace}")
  46. set_target_properties(${target} PROPERTIES
  47. LIBRARY_OUTPUT_DIRECTORY "${_outdir}"
  48. OUTPUT_NAME "${basename}"
  49. PREFIX ""
  50. SUFFIX "${PYTHON_EXTENSION_SUFFIX}")
  51. # This is explained over in CompilerFlags.cmake
  52. foreach(_config ${CMAKE_CONFIGURATION_TYPES})
  53. string(TOUPPER "${_config}" _config)
  54. set_target_properties(${target} PROPERTIES
  55. LIBRARY_OUTPUT_DIRECTORY_${_config} "${_outdir}")
  56. endforeach(_config)
  57. if(PYTHON_ARCH_INSTALL_DIR)
  58. install(TARGETS ${target} EXPORT "${export}" COMPONENT "${component}" DESTINATION "${PYTHON_ARCH_INSTALL_DIR}/${slash_namespace}")
  59. endif()
  60. else()
  61. set_target_properties(${target} PROPERTIES
  62. OUTPUT_NAME "${basename}"
  63. PREFIX "libpy.${namespace}.")
  64. install(TARGETS ${target} EXPORT "${export}" COMPONENT "${component}" DESTINATION lib)
  65. endif()
  66. endfunction(add_python_target)
  67. #
  68. # Function: install_python_package(name [SOURCE path] [ARCH/LIB] [COMPONENT component])
  69. #
  70. # Installs the Python package `name` (which may have its source at `path`).
  71. #
  72. # The package is copied to (or created in) the build directory so that the user
  73. # may import it before the install step.
  74. #
  75. # Note that this handles more than just installation; it will also invoke
  76. # Python's compileall utility to pregenerate .pyc/.pyo files. This will only
  77. # happen if the Python interpreter is found.
  78. #
  79. # The ARCH or LIB keyword may be used to specify whether this package should be
  80. # installed into Python's architecture-dependent or architecture-independent
  81. # package path. The default, if unspecified, is LIB.
  82. #
  83. # The COMPONENT keyword overrides the install component (see CMake's
  84. # documentation for more information on what this does). The default is
  85. # "Python".
  86. #
  87. function(install_python_package package_name)
  88. set(type "LIB")
  89. unset(keyword)
  90. set(component "Python")
  91. unset(src_path)
  92. foreach(arg ${ARGN})
  93. if(arg STREQUAL "ARCH")
  94. set(type "ARCH")
  95. elseif(arg STREQUAL "LIB")
  96. set(type "LIB")
  97. elseif(arg STREQUAL "COMPONENT")
  98. set(keyword "${arg}")
  99. elseif(keyword STREQUAL "COMPONENT")
  100. set(component "${arg}")
  101. unset(keyword)
  102. elseif(arg STREQUAL "SOURCE")
  103. set(keyword "${arg}")
  104. elseif(keyword STREQUAL "SOURCE")
  105. set(src_path "${arg}")
  106. unset(keyword)
  107. else()
  108. message(FATAL_ERROR "install_python_package got unexpected argument: ${ARGN}")
  109. endif()
  110. endforeach(arg)
  111. if(NOT DEFINED src_path AND type STREQUAL "ARCH" AND WIN32 AND NOT CYGWIN)
  112. # Win32 needs a special fixup so the DLLs in "bin" can be on the path;
  113. # let's set src_path to the directory containing our fixup __init__.py
  114. set(src_path "${CMAKE_SOURCE_DIR}/cmake/templates/win32_python")
  115. endif()
  116. set(path "${PROJECT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${package_name}")
  117. set(args -D "OUTPUT_DIR=${path}")
  118. if(src_path)
  119. list(APPEND args -D "SOURCE_DIR=${src_path}")
  120. endif()
  121. if(PYTHON_EXECUTABLE)
  122. list(APPEND args -D "PYTHON_EXECUTABLES=${PYTHON_EXECUTABLE}")
  123. endif()
  124. add_custom_target(${package_name} ALL
  125. COMMAND ${CMAKE_COMMAND}
  126. ${args}
  127. -P "${CMAKE_SOURCE_DIR}/cmake/scripts/CopyPython.cmake")
  128. set(dir "${PYTHON_${type}_INSTALL_DIR}")
  129. if(dir)
  130. install(DIRECTORY "${path}" DESTINATION "${dir}"
  131. COMPONENT "${component}"
  132. FILES_MATCHING REGEX "\\.py[co]?$")
  133. endif()
  134. endfunction(install_python_package)