pkg-python.mk 10 KB

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