Python.cmake 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. target_include_directories(${target} PRIVATE "${PROJECT_SOURCE_DIR}/dtool/src/interrogatedb")
  45. if(BUILD_SHARED_LIBS)
  46. set(_outdir "${PANDA_OUTPUT_DIR}/${slash_namespace}")
  47. set_target_properties(${target} PROPERTIES
  48. LIBRARY_OUTPUT_DIRECTORY "${_outdir}"
  49. OUTPUT_NAME "${basename}"
  50. PREFIX ""
  51. SUFFIX "${PYTHON_EXTENSION_SUFFIX}")
  52. # This is explained over in CompilerFlags.cmake
  53. foreach(_config ${CMAKE_CONFIGURATION_TYPES})
  54. string(TOUPPER "${_config}" _config)
  55. set_target_properties(${target} PROPERTIES
  56. LIBRARY_OUTPUT_DIRECTORY_${_config} "${_outdir}")
  57. endforeach(_config)
  58. if(PYTHON_ARCH_INSTALL_DIR)
  59. install(TARGETS ${target} EXPORT "${export}" COMPONENT "${component}" DESTINATION "${PYTHON_ARCH_INSTALL_DIR}/${slash_namespace}")
  60. endif()
  61. else()
  62. set_target_properties(${target} PROPERTIES
  63. OUTPUT_NAME "${basename}"
  64. PREFIX "libpy.${namespace}.")
  65. install(TARGETS ${target} EXPORT "${export}" COMPONENT "${component}" DESTINATION ${CMAKE_INSTALL_LIBDIR})
  66. endif()
  67. endfunction(add_python_target)
  68. #
  69. # Function: install_python_package(name [SOURCE path] [ARCH/LIB] [COMPONENT component])
  70. #
  71. # Installs the Python package `name` (which may have its source at `path`).
  72. #
  73. # The package is copied to (or created in) the build directory so that the user
  74. # may import it before the install step.
  75. #
  76. # Note that this handles more than just installation; it will also invoke
  77. # Python's compileall utility to pregenerate .pyc/.pyo files. This will only
  78. # happen if the Python interpreter is found.
  79. #
  80. # The ARCH or LIB keyword may be used to specify whether this package should be
  81. # installed into Python's architecture-dependent or architecture-independent
  82. # package path. The default, if unspecified, is LIB.
  83. #
  84. # The COMPONENT keyword overrides the install component (see CMake's
  85. # documentation for more information on what this does). The default is
  86. # "Python".
  87. #
  88. function(install_python_package package_name)
  89. set(type "LIB")
  90. unset(keyword)
  91. set(component "Python")
  92. unset(src_path)
  93. foreach(arg ${ARGN})
  94. if(arg STREQUAL "ARCH")
  95. set(type "ARCH")
  96. elseif(arg STREQUAL "LIB")
  97. set(type "LIB")
  98. elseif(arg STREQUAL "COMPONENT")
  99. set(keyword "${arg}")
  100. elseif(keyword STREQUAL "COMPONENT")
  101. set(component "${arg}")
  102. unset(keyword)
  103. elseif(arg STREQUAL "SOURCE")
  104. set(keyword "${arg}")
  105. elseif(keyword STREQUAL "SOURCE")
  106. set(src_path "${arg}")
  107. unset(keyword)
  108. else()
  109. message(FATAL_ERROR "install_python_package got unexpected argument: ${ARGN}")
  110. endif()
  111. endforeach(arg)
  112. if(NOT DEFINED src_path AND type STREQUAL "ARCH" AND WIN32 AND NOT CYGWIN)
  113. # Win32 needs a special fixup so the DLLs in "bin" can be on the path;
  114. # let's set src_path to the directory containing our fixup __init__.py
  115. set(src_path "${CMAKE_SOURCE_DIR}/cmake/templates/win32_python")
  116. endif()
  117. set(path "${PANDA_OUTPUT_DIR}/${package_name}")
  118. set(args -D "OUTPUT_DIR=${path}")
  119. if(src_path)
  120. list(APPEND args -D "SOURCE_DIR=${src_path}")
  121. endif()
  122. if(PYTHON_EXECUTABLE)
  123. list(APPEND args -D "PYTHON_EXECUTABLES=${PYTHON_EXECUTABLE}")
  124. endif()
  125. add_custom_target(${package_name} ALL
  126. COMMAND ${CMAKE_COMMAND}
  127. ${args}
  128. -P "${CMAKE_SOURCE_DIR}/cmake/scripts/CopyPython.cmake")
  129. set(dir "${PYTHON_${type}_INSTALL_DIR}")
  130. if(dir)
  131. install(DIRECTORY "${path}" DESTINATION "${dir}"
  132. COMPONENT "${component}"
  133. FILES_MATCHING REGEX "\\.py[co]?$")
  134. endif()
  135. endfunction(install_python_package)