pkg-python.mk 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. $(2)_DEPENDENCIES ?= $(filter-out host-python host-python3 host-python-setuptools host-toolchain $(1),$(patsubst host-host-%,host-%,$(addprefix host-,$($(3)_DEPENDENCIES))))
  126. # Target packages need both the python interpreter on the target (for
  127. # runtime) and the python interpreter on the host (for
  128. # compilation). However, host packages only need the python
  129. # interpreter on the host, whose version may be enforced by setting
  130. # the *_NEEDS_HOST_PYTHON variable.
  131. #
  132. # So:
  133. # - for target packages, we always depend on the default python interpreter
  134. # (the one selected by the config);
  135. # - for host packages:
  136. # - if *_NEEDS_HOST_PYTHON is not set, then we depend on use the default
  137. # interperter;
  138. # - otherwise, we depend on the one requested by *_NEEDS_HOST_PYTHON.
  139. #
  140. ifeq ($(4),target)
  141. $(2)_DEPENDENCIES += $(if $(BR2_PACKAGE_PYTHON3),host-python3 python3,host-python python)
  142. else
  143. ifeq ($($(2)_NEEDS_HOST_PYTHON),)
  144. $(2)_DEPENDENCIES += $(if $(BR2_PACKAGE_PYTHON3),host-python3,host-python)
  145. else
  146. ifeq ($($(2)_NEEDS_HOST_PYTHON),python2)
  147. $(2)_DEPENDENCIES += host-python
  148. else ifeq ($($(2)_NEEDS_HOST_PYTHON),python3)
  149. $(2)_DEPENDENCIES += host-python3
  150. else
  151. $$(error Incorrect value '$($(2)_NEEDS_HOST_PYTHON)' for $(2)_NEEDS_HOST_PYTHON)
  152. endif
  153. endif # ($($(2)_NEEDS_HOST_PYTHON),)
  154. endif # ($(4),target)
  155. # Setuptools based packages will need host-python-setuptools (both
  156. # host and target). We need to have a special exclusion for the
  157. # host-setuptools package itself: it is setuptools-based, but
  158. # shouldn't depend on host-setuptools (because it would otherwise
  159. # depend on itself!).
  160. ifeq ($$($(2)_SETUP_TYPE),setuptools)
  161. ifneq ($(2),HOST_PYTHON_SETUPTOOLS)
  162. $(2)_DEPENDENCIES += host-python-setuptools
  163. endif
  164. endif
  165. # Python interpreter to use for building the package.
  166. #
  167. # We may want to specify the python interpreter to be used for building a
  168. # package, especially for host-packages (target packages must be built using
  169. # the same version of the interpreter as the one installed on the target).
  170. #
  171. # So:
  172. # - for target packages, we always use the default python interpreter (which
  173. # is the same version as the one built and installed on the target);
  174. # - for host packages:
  175. # - if *_NEEDS_HOST_PYTHON is not set, then we use use the default
  176. # interperter;
  177. # - otherwise, we use the one requested by *_NEEDS_HOST_PYTHON.
  178. #
  179. ifeq ($(4),target)
  180. $(2)_PYTHON_INTERPRETER = $(HOST_DIR)/usr/bin/python
  181. else
  182. ifeq ($($(2)_NEEDS_HOST_PYTHON),)
  183. $(2)_PYTHON_INTERPRETER = $(HOST_DIR)/usr/bin/python
  184. else
  185. $(2)_PYTHON_INTERPRETER = $(HOST_DIR)/usr/bin/$($(2)_NEEDS_HOST_PYTHON)
  186. endif
  187. endif
  188. #
  189. # Build step. Only define it if not already defined by the package .mk
  190. # file.
  191. #
  192. ifndef $(2)_BUILD_CMDS
  193. define $(2)_BUILD_CMDS
  194. (cd $$($$(PKG)_BUILDDIR)/; \
  195. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  196. $$($(2)_PYTHON_INTERPRETER) setup.py \
  197. $$($$(PKG)_BASE_BUILD_TGT) \
  198. $$($$(PKG)_BASE_BUILD_OPT) $$($$(PKG)_BUILD_OPT))
  199. endef
  200. endif
  201. #
  202. # Host installation step. Only define it if not already defined by the
  203. # package .mk file.
  204. #
  205. ifndef $(2)_INSTALL_CMDS
  206. define $(2)_INSTALL_CMDS
  207. (cd $$($$(PKG)_BUILDDIR)/; \
  208. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  209. $$($(2)_PYTHON_INTERPRETER) setup.py install \
  210. $$($$(PKG)_BASE_INSTALL_OPT) $$($$(PKG)_INSTALL_OPT))
  211. endef
  212. endif
  213. #
  214. # Target installation step. Only define it if not already defined by
  215. # the package .mk file.
  216. #
  217. ifndef $(2)_INSTALL_TARGET_CMDS
  218. define $(2)_INSTALL_TARGET_CMDS
  219. (cd $$($$(PKG)_BUILDDIR)/; \
  220. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  221. $$($(2)_PYTHON_INTERPRETER) setup.py install \
  222. $$($$(PKG)_BASE_INSTALL_OPT) $$($$(PKG)_INSTALL_OPT))
  223. endef
  224. endif
  225. # Call the generic package infrastructure to generate the necessary
  226. # make targets
  227. $(call inner-generic-package,$(1),$(2),$(3),$(4))
  228. endef
  229. ################################################################################
  230. # python-package -- the target generator macro for Python packages
  231. ################################################################################
  232. python-package = $(call inner-python-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
  233. host-python-package = $(call inner-python-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)