pkg-python.mk 8.2 KB

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