pkg-python.mk 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. ################################################################################
  2. # Python package infrastructure
  3. #
  4. # This file implements an infrastructure that eases development of
  5. # package .mk files for Python packages. It should be used for all
  6. # packages that use Python setup.py/setuptools as their build system.
  7. #
  8. # See the Buildroot documentation for details on the usage of this
  9. # infrastructure
  10. #
  11. # In terms of implementation, this Python infrastructure requires the
  12. # .mk file to only specify metadata information about the package:
  13. # name, version, download URL, etc.
  14. #
  15. # We still allow the package .mk file to override what the different
  16. # steps are doing, if needed. For example, if <PKG>_BUILD_CMDS is
  17. # already defined, it is used as the list of commands to perform to
  18. # build the package, instead of the default Python behaviour. The
  19. # package can also define some post operation hooks.
  20. #
  21. ################################################################################
  22. # basename does not evaluate if a file exists, so we must check to ensure
  23. # the _sysconfigdata__linux_*.py file exists. The "|| true" is added to return
  24. # an empty string if the file does not exist.
  25. PKG_PYTHON_SYSCONFIGDATA_PATH = $(PYTHON3_PATH)/_sysconfigdata__linux_*.py
  26. PKG_PYTHON_SYSCONFIGDATA_NAME = `{ [ -e $(PKG_PYTHON_SYSCONFIGDATA_PATH) ] && basename $(PKG_PYTHON_SYSCONFIGDATA_PATH) .py; } || true`
  27. # Target distutils-based packages
  28. PKG_PYTHON_DISTUTILS_ENV = \
  29. PATH=$(BR_PATH) \
  30. $(TARGET_CONFIGURE_OPTS) \
  31. LDSHARED="$(TARGET_CROSS)gcc -shared" \
  32. PYTHONPATH="$(if $(BR2_PACKAGE_PYTHON3),$(PYTHON3_PATH),$(PYTHON_PATH))" \
  33. PYTHONNOUSERSITE=1 \
  34. _PYTHON_PROJECT_BASE="$(if $(BR2_PACKAGE_PYTHON3),$(PYTHON3_DIR),$(PYTHON_DIR))" \
  35. _PYTHON_SYSCONFIGDATA_NAME="$(PKG_PYTHON_SYSCONFIGDATA_NAME)" \
  36. _python_sysroot=$(STAGING_DIR) \
  37. _python_prefix=/usr \
  38. _python_exec_prefix=/usr
  39. PKG_PYTHON_DISTUTILS_BUILD_OPTS = \
  40. --executable=/usr/bin/python
  41. PKG_PYTHON_DISTUTILS_INSTALL_TARGET_OPTS = \
  42. --prefix=/usr \
  43. --root=$(TARGET_DIR)
  44. PKG_PYTHON_DISTUTILS_INSTALL_STAGING_OPTS = \
  45. --prefix=/usr \
  46. --root=$(STAGING_DIR)
  47. # Host distutils-based packages
  48. HOST_PKG_PYTHON_DISTUTILS_ENV = \
  49. PATH=$(BR_PATH) \
  50. PYTHONNOUSERSITE=1 \
  51. $(HOST_CONFIGURE_OPTS)
  52. HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPTS = \
  53. --prefix=$(HOST_DIR)
  54. # Target setuptools-based packages
  55. PKG_PYTHON_SETUPTOOLS_ENV = \
  56. _PYTHON_PROJECT_BASE="$(if $(BR2_PACKAGE_PYTHON3),$(PYTHON3_DIR),$(PYTHON_DIR))" \
  57. _PYTHON_SYSCONFIGDATA_NAME="$(PKG_PYTHON_SYSCONFIGDATA_NAME)" \
  58. PATH=$(BR_PATH) \
  59. $(TARGET_CONFIGURE_OPTS) \
  60. PYTHONPATH="$(if $(BR2_PACKAGE_PYTHON3),$(PYTHON3_PATH),$(PYTHON_PATH))" \
  61. PYTHONNOUSERSITE=1 \
  62. _python_sysroot=$(STAGING_DIR) \
  63. _python_prefix=/usr \
  64. _python_exec_prefix=/usr
  65. PKG_PYTHON_SETUPTOOLS_INSTALL_TARGET_OPTS = \
  66. --prefix=/usr \
  67. --executable=/usr/bin/python \
  68. --single-version-externally-managed \
  69. --root=$(TARGET_DIR)
  70. PKG_PYTHON_SETUPTOOLS_INSTALL_STAGING_OPTS = \
  71. --prefix=/usr \
  72. --executable=/usr/bin/python \
  73. --single-version-externally-managed \
  74. --root=$(STAGING_DIR)
  75. # Host setuptools-based packages
  76. HOST_PKG_PYTHON_SETUPTOOLS_ENV = \
  77. PATH=$(BR_PATH) \
  78. PYTHONNOUSERSITE=1 \
  79. $(HOST_CONFIGURE_OPTS)
  80. HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPTS = \
  81. --prefix=$(HOST_DIR) \
  82. --root=/ \
  83. --single-version-externally-managed
  84. ################################################################################
  85. # inner-python-package -- defines how the configuration, compilation
  86. # and installation of a Python package should be done, implements a
  87. # few hooks to tune the build process and calls the generic package
  88. # infrastructure to generate the necessary make targets
  89. #
  90. # argument 1 is the lowercase package name
  91. # argument 2 is the uppercase package name, including a HOST_ prefix
  92. # for host packages
  93. # argument 3 is the uppercase package name, without the HOST_ prefix
  94. # for host packages
  95. # argument 4 is the type (target or host)
  96. ################################################################################
  97. define inner-python-package
  98. ifndef $(2)_SETUP_TYPE
  99. ifdef $(3)_SETUP_TYPE
  100. $(2)_SETUP_TYPE = $$($(3)_SETUP_TYPE)
  101. else
  102. $$(error "$(2)_SETUP_TYPE must be set")
  103. endif
  104. endif
  105. # Distutils
  106. ifeq ($$($(2)_SETUP_TYPE),distutils)
  107. ifeq ($(4),target)
  108. $(2)_BASE_ENV = $$(PKG_PYTHON_DISTUTILS_ENV)
  109. $(2)_BASE_BUILD_TGT = build
  110. $(2)_BASE_BUILD_OPTS = $$(PKG_PYTHON_DISTUTILS_BUILD_OPTS)
  111. $(2)_BASE_INSTALL_TARGET_OPTS = $$(PKG_PYTHON_DISTUTILS_INSTALL_TARGET_OPTS)
  112. $(2)_BASE_INSTALL_STAGING_OPTS = $$(PKG_PYTHON_DISTUTILS_INSTALL_STAGING_OPTS)
  113. else
  114. $(2)_BASE_ENV = $$(HOST_PKG_PYTHON_DISTUTILS_ENV)
  115. $(2)_BASE_BUILD_TGT = build
  116. $(2)_BASE_INSTALL_OPTS = $$(HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPTS)
  117. endif
  118. # Setuptools
  119. else ifeq ($$($(2)_SETUP_TYPE),setuptools)
  120. ifeq ($(4),target)
  121. $(2)_BASE_ENV = $$(PKG_PYTHON_SETUPTOOLS_ENV)
  122. $(2)_BASE_BUILD_TGT = build
  123. $(2)_BASE_INSTALL_TARGET_OPTS = $$(PKG_PYTHON_SETUPTOOLS_INSTALL_TARGET_OPTS)
  124. $(2)_BASE_INSTALL_STAGING_OPTS = $$(PKG_PYTHON_SETUPTOOLS_INSTALL_STAGING_OPTS)
  125. else
  126. $(2)_BASE_ENV = $$(HOST_PKG_PYTHON_SETUPTOOLS_ENV)
  127. $(2)_BASE_BUILD_TGT = build
  128. $(2)_BASE_INSTALL_OPTS = $$(HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPTS)
  129. endif
  130. else
  131. $$(error "Invalid $(2)_SETUP_TYPE. Valid options are 'distutils' or 'setuptools'")
  132. endif
  133. # Target packages need both the python interpreter on the target (for
  134. # runtime) and the python interpreter on the host (for
  135. # compilation). However, host packages only need the python
  136. # interpreter on the host.
  137. #
  138. ifeq ($(4),target)
  139. $(2)_DEPENDENCIES += host-python3 python3
  140. else
  141. $(2)_DEPENDENCIES += host-python3
  142. endif # ($(4),target)
  143. # Setuptools based packages will need setuptools for the host Python
  144. # interpreter (both host and target).
  145. #
  146. ifeq ($$($(2)_SETUP_TYPE),setuptools)
  147. $(2)_DEPENDENCIES += $$(if $$(filter host-python-setuptools,$(1)),,host-python-setuptools)
  148. endif # SETUP_TYPE
  149. # Python interpreter to use for building the package.
  150. #
  151. $(2)_PYTHON_INTERPRETER = $$(HOST_DIR)/bin/python
  152. #
  153. # Build step. Only define it if not already defined by the package .mk
  154. # file.
  155. #
  156. ifndef $(2)_BUILD_CMDS
  157. define $(2)_BUILD_CMDS
  158. (cd $$($$(PKG)_BUILDDIR)/; \
  159. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  160. $$($(2)_PYTHON_INTERPRETER) setup.py \
  161. $$($$(PKG)_BASE_BUILD_TGT) \
  162. $$($$(PKG)_BASE_BUILD_OPTS) $$($$(PKG)_BUILD_OPTS))
  163. endef
  164. endif
  165. #
  166. # Host installation step. Only define it if not already defined by the
  167. # package .mk file.
  168. #
  169. ifndef $(2)_INSTALL_CMDS
  170. define $(2)_INSTALL_CMDS
  171. (cd $$($$(PKG)_BUILDDIR)/; \
  172. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  173. $$($(2)_PYTHON_INTERPRETER) setup.py install \
  174. $$($$(PKG)_BASE_INSTALL_OPTS) $$($$(PKG)_INSTALL_OPTS))
  175. endef
  176. endif
  177. #
  178. # Target installation step. Only define it if not already defined by
  179. # the package .mk file.
  180. #
  181. ifndef $(2)_INSTALL_TARGET_CMDS
  182. define $(2)_INSTALL_TARGET_CMDS
  183. (cd $$($$(PKG)_BUILDDIR)/; \
  184. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  185. $$($(2)_PYTHON_INTERPRETER) setup.py install --no-compile \
  186. $$($$(PKG)_BASE_INSTALL_TARGET_OPTS) \
  187. $$($$(PKG)_INSTALL_TARGET_OPTS))
  188. endef
  189. endif
  190. #
  191. # Staging installation step. Only define it if not already defined by
  192. # the package .mk file.
  193. #
  194. ifndef $(2)_INSTALL_STAGING_CMDS
  195. define $(2)_INSTALL_STAGING_CMDS
  196. (cd $$($$(PKG)_BUILDDIR)/; \
  197. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  198. $$($(2)_PYTHON_INTERPRETER) setup.py install \
  199. $$($$(PKG)_BASE_INSTALL_STAGING_OPTS) \
  200. $$($$(PKG)_INSTALL_STAGING_OPTS))
  201. endef
  202. endif
  203. # Call the generic package infrastructure to generate the necessary
  204. # make targets
  205. $(call inner-generic-package,$(1),$(2),$(3),$(4))
  206. endef
  207. ################################################################################
  208. # python-package -- the target generator macro for Python packages
  209. ################################################################################
  210. python-package = $(call inner-python-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
  211. host-python-package = $(call inner-python-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)