pkg-python.mk 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. # Target distutils-based packages
  23. PKG_PYTHON_DISTUTILS_ENV = \
  24. PATH=$(BR_PATH) \
  25. CC="$(TARGET_CC)" \
  26. CFLAGS="$(TARGET_CFLAGS)" \
  27. LDFLAGS="$(TARGET_LDFLAGS)" \
  28. LDSHARED="$(TARGET_CROSS)gcc -shared" \
  29. PYTHONPATH="$(if $(BR2_PACKAGE_PYTHON3),$(PYTHON3_PATH),$(PYTHON_PATH))" \
  30. _python_sysroot=$(STAGING_DIR) \
  31. _python_prefix=/usr \
  32. _python_exec_prefix=/usr
  33. PKG_PYTHON_DISTUTILS_BUILD_OPTS = \
  34. --executable=/usr/bin/python
  35. PKG_PYTHON_DISTUTILS_INSTALL_TARGET_OPTS = \
  36. --prefix=$(TARGET_DIR)/usr
  37. PKG_PYTHON_DISTUTILS_INSTALL_STAGING_OPTS = \
  38. --prefix=$(STAGING_DIR)/usr
  39. # Host distutils-based packages
  40. HOST_PKG_PYTHON_DISTUTILS_ENV = \
  41. PATH=$(BR_PATH)
  42. HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPTS = \
  43. --prefix=$(HOST_DIR)/usr
  44. # Target setuptools-based packages
  45. PKG_PYTHON_SETUPTOOLS_ENV = \
  46. PATH=$(BR_PATH) \
  47. PYTHONPATH="$(if $(BR2_PACKAGE_PYTHON3),$(PYTHON3_PATH),$(PYTHON_PATH))" \
  48. _python_sysroot=$(STAGING_DIR) \
  49. _python_prefix=/usr \
  50. _python_exec_prefix=/usr
  51. PKG_PYTHON_SETUPTOOLS_INSTALL_TARGET_OPTS = \
  52. --prefix=$(TARGET_DIR)/usr \
  53. --executable=/usr/bin/python \
  54. --single-version-externally-managed \
  55. --root=/
  56. PKG_PYTHON_SETUPTOOLS_INSTALL_STAGING_OPTS = \
  57. --prefix=$(STAGING_DIR)/usr \
  58. --executable=/usr/bin/python \
  59. --single-version-externally-managed \
  60. --root=/
  61. # Host setuptools-based packages
  62. HOST_PKG_PYTHON_SETUPTOOLS_ENV = \
  63. PATH=$(BR_PATH)
  64. HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPTS = \
  65. --prefix=$(HOST_DIR)/usr
  66. ################################################################################
  67. # inner-python-package -- defines how the configuration, compilation
  68. # and installation of a Python package should be done, implements a
  69. # few hooks to tune the build process and calls the generic package
  70. # infrastructure to generate the necessary make targets
  71. #
  72. # argument 1 is the lowercase package name
  73. # argument 2 is the uppercase package name, including a HOST_ prefix
  74. # for host packages
  75. # argument 3 is the uppercase package name, without the HOST_ prefix
  76. # for host packages
  77. # argument 4 is the type (target or host)
  78. ################################################################################
  79. define inner-python-package
  80. $(2)_SRCDIR = $$($(2)_DIR)/$$($(2)_SUBDIR)
  81. $(2)_BUILDDIR = $$($(2)_SRCDIR)
  82. $(2)_ENV ?=
  83. $(2)_BUILD_OPTS ?=
  84. $(2)_INSTALL_OPTS ?=
  85. ifndef $(2)_SETUP_TYPE
  86. ifdef $(3)_SETUP_TYPE
  87. $(2)_SETUP_TYPE = $$($(3)_SETUP_TYPE)
  88. else
  89. $$(error "$(2)_SETUP_TYPE must be set")
  90. endif
  91. endif
  92. # Distutils
  93. ifeq ($$($(2)_SETUP_TYPE),distutils)
  94. ifeq ($(4),target)
  95. $(2)_BASE_ENV = $$(PKG_PYTHON_DISTUTILS_ENV)
  96. $(2)_BASE_BUILD_TGT = build
  97. $(2)_BASE_BUILD_OPTS = $$(PKG_PYTHON_DISTUTILS_BUILD_OPTS)
  98. $(2)_BASE_INSTALL_TARGET_OPTS = $$(PKG_PYTHON_DISTUTILS_INSTALL_TARGET_OPTS)
  99. $(2)_BASE_INSTALL_STAGING_OPTS = $$(PKG_PYTHON_DISTUTILS_INSTALL_STAGING_OPTS)
  100. else
  101. $(2)_BASE_ENV = $$(HOST_PKG_PYTHON_DISTUTILS_ENV)
  102. $(2)_BASE_BUILD_TGT = build
  103. $(2)_BASE_BUILD_OPTS =
  104. $(2)_BASE_INSTALL_OPTS = $$(HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPTS)
  105. endif
  106. # Setuptools
  107. else ifeq ($$($(2)_SETUP_TYPE),setuptools)
  108. ifeq ($(4),target)
  109. $(2)_BASE_ENV = $$(PKG_PYTHON_SETUPTOOLS_ENV)
  110. $(2)_BASE_BUILD_TGT = build
  111. $(2)_BASE_BUILD_OPTS =
  112. $(2)_BASE_INSTALL_TARGET_OPTS = $$(PKG_PYTHON_SETUPTOOLS_INSTALL_TARGET_OPTS)
  113. $(2)_BASE_INSTALL_STAGING_OPTS = $$(PKG_PYTHON_SETUPTOOLS_INSTALL_STAGING_OPTS)
  114. else
  115. $(2)_BASE_ENV = $$(HOST_PKG_PYTHON_SETUPTOOLS_ENV)
  116. $(2)_BASE_BUILD_TGT = build
  117. $(2)_BASE_BUILD_OPTS =
  118. $(2)_BASE_INSTALL_OPTS = $$(HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPTS)
  119. endif
  120. else
  121. $$(error "Invalid $(2)_SETUP_TYPE. Valid options are 'distutils' or 'setuptools'")
  122. endif
  123. # The below statement intends to calculate the dependencies of host
  124. # packages by derivating them from the dependencies of the
  125. # corresponding target package, after adding the 'host-' prefix in
  126. # front of the dependencies.
  127. #
  128. # However it must be repeated from inner-generic-package, as we need
  129. # to exclude the python, host-python and host-python-setuptools
  130. # packages, which are added below in the list of dependencies
  131. # depending on the package characteristics, and shouldn't be derived
  132. # automatically from the dependencies of the corresponding target
  133. # package.
  134. ifeq ($(4),host)
  135. $(2)_DEPENDENCIES ?= $$(filter-out host-python host-python3 host-python-setuptools host-skeleton host-toolchain $(1),$$(patsubst host-host-%,host-%,$$(addprefix host-,$$($(3)_DEPENDENCIES))))
  136. endif
  137. # Target packages need both the python interpreter on the target (for
  138. # runtime) and the python interpreter on the host (for
  139. # compilation). However, host packages only need the python
  140. # interpreter on the host, whose version may be enforced by setting
  141. # the *_NEEDS_HOST_PYTHON variable.
  142. #
  143. # So:
  144. # - for target packages, we always depend on the default python interpreter
  145. # (the one selected by the config);
  146. # - for host packages:
  147. # - if *_NEEDS_HOST_PYTHON is not set, then we depend on use the default
  148. # interperter;
  149. # - otherwise, we depend on the one requested by *_NEEDS_HOST_PYTHON.
  150. #
  151. ifeq ($(4),target)
  152. $(2)_DEPENDENCIES += $$(if $$(BR2_PACKAGE_PYTHON3),host-python3 python3,host-python python)
  153. else
  154. ifeq ($$($(2)_NEEDS_HOST_PYTHON),)
  155. $(2)_DEPENDENCIES += $$(if $$(BR2_PACKAGE_PYTHON3),host-python3,host-python)
  156. else
  157. ifeq ($$($(2)_NEEDS_HOST_PYTHON),python2)
  158. $(2)_DEPENDENCIES += host-python
  159. else ifeq ($$($(2)_NEEDS_HOST_PYTHON),python3)
  160. $(2)_DEPENDENCIES += host-python3
  161. else
  162. $$(error Incorrect value '$$($(2)_NEEDS_HOST_PYTHON)' for $(2)_NEEDS_HOST_PYTHON)
  163. endif
  164. endif # ($$($(2)_NEEDS_HOST_PYTHON),)
  165. endif # ($(4),target)
  166. # Setuptools based packages will need host-python-setuptools (both
  167. # host and target). We need to have a special exclusion for the
  168. # host-setuptools package itself: it is setuptools-based, but
  169. # shouldn't depend on host-setuptools (because it would otherwise
  170. # depend on itself!).
  171. ifeq ($$($(2)_SETUP_TYPE),setuptools)
  172. ifneq ($(2),HOST_PYTHON_SETUPTOOLS)
  173. $(2)_DEPENDENCIES += host-python-setuptools
  174. endif
  175. endif
  176. # Python interpreter to use for building the package.
  177. #
  178. # We may want to specify the python interpreter to be used for building a
  179. # package, especially for host-packages (target packages must be built using
  180. # the same version of the interpreter as the one installed on the target).
  181. #
  182. # So:
  183. # - for target packages, we always use the default python interpreter (which
  184. # is the same version as the one built and installed on the target);
  185. # - for host packages:
  186. # - if *_NEEDS_HOST_PYTHON is not set, then we use use the default
  187. # interperter;
  188. # - otherwise, we use the one requested by *_NEEDS_HOST_PYTHON.
  189. #
  190. ifeq ($(4),target)
  191. $(2)_PYTHON_INTERPRETER = $$(HOST_DIR)/usr/bin/python
  192. else
  193. ifeq ($$($(2)_NEEDS_HOST_PYTHON),)
  194. $(2)_PYTHON_INTERPRETER = $$(HOST_DIR)/usr/bin/python
  195. else
  196. $(2)_PYTHON_INTERPRETER = $$(HOST_DIR)/usr/bin/$$($(2)_NEEDS_HOST_PYTHON)
  197. endif
  198. endif
  199. #
  200. # Build step. Only define it if not already defined by the package .mk
  201. # file.
  202. #
  203. ifndef $(2)_BUILD_CMDS
  204. define $(2)_BUILD_CMDS
  205. (cd $$($$(PKG)_BUILDDIR)/; \
  206. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  207. $$($(2)_PYTHON_INTERPRETER) setup.py \
  208. $$($$(PKG)_BASE_BUILD_TGT) \
  209. $$($$(PKG)_BASE_BUILD_OPTS) $$($$(PKG)_BUILD_OPTS))
  210. endef
  211. endif
  212. #
  213. # Host installation step. Only define it if not already defined by the
  214. # package .mk file.
  215. #
  216. ifndef $(2)_INSTALL_CMDS
  217. define $(2)_INSTALL_CMDS
  218. (cd $$($$(PKG)_BUILDDIR)/; \
  219. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  220. $$($(2)_PYTHON_INTERPRETER) setup.py install \
  221. $$($$(PKG)_BASE_INSTALL_OPTS) $$($$(PKG)_INSTALL_OPTS))
  222. endef
  223. endif
  224. #
  225. # Target installation step. Only define it if not already defined by
  226. # the package .mk file.
  227. #
  228. ifndef $(2)_INSTALL_TARGET_CMDS
  229. define $(2)_INSTALL_TARGET_CMDS
  230. (cd $$($$(PKG)_BUILDDIR)/; \
  231. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  232. $$($(2)_PYTHON_INTERPRETER) setup.py install \
  233. $$($$(PKG)_BASE_INSTALL_TARGET_OPTS) \
  234. $$($$(PKG)_INSTALL_TARGET_OPTS))
  235. endef
  236. endif
  237. #
  238. # Staging installation step. Only define it if not already defined by
  239. # the package .mk file.
  240. #
  241. ifndef $(2)_INSTALL_STAGING_CMDS
  242. define $(2)_INSTALL_STAGING_CMDS
  243. (cd $$($$(PKG)_BUILDDIR)/; \
  244. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  245. $$($(2)_PYTHON_INTERPRETER) setup.py install \
  246. $$($$(PKG)_BASE_INSTALL_STAGING_OPTS) \
  247. $$($$(PKG)_INSTALL_STAGING_OPTS))
  248. endef
  249. endif
  250. # Call the generic package infrastructure to generate the necessary
  251. # make targets
  252. $(call inner-generic-package,$(1),$(2),$(3),$(4))
  253. endef
  254. ################################################################################
  255. # python-package -- the target generator macro for Python packages
  256. ################################################################################
  257. python-package = $(call inner-python-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
  258. host-python-package = $(call inner-python-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)