pkg-python.mk 8.8 KB

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