pkg-python.mk 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. # basename does not evaluate if a file exists, so we must check to ensure
  23. # the _sysconfigdata__linux_*.py file exists. The "|| true" is added to return
  24. # an empty string if the file does not exist.
  25. PKG_PYTHON_SYSCONFIGDATA_PATH = $(PYTHON3_PATH)/_sysconfigdata__linux_*.py
  26. PKG_PYTHON_SYSCONFIGDATA_NAME = `{ [ -e $(PKG_PYTHON_SYSCONFIGDATA_PATH) ] && basename $(PKG_PYTHON_SYSCONFIGDATA_PATH) .py; } || true`
  27. # Target distutils-based packages
  28. PKG_PYTHON_DISTUTILS_ENV = \
  29. PATH=$(BR_PATH) \
  30. $(TARGET_CONFIGURE_OPTS) \
  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_CONFIGURE_OPTS)
  51. HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPTS = \
  52. --prefix=$(HOST_DIR)
  53. # Target setuptools-based packages
  54. PKG_PYTHON_SETUPTOOLS_ENV = \
  55. _PYTHON_SYSCONFIGDATA_NAME="$(PKG_PYTHON_SYSCONFIGDATA_NAME)" \
  56. PATH=$(BR_PATH) \
  57. $(TARGET_CONFIGURE_OPTS) \
  58. PYTHONPATH="$(if $(BR2_PACKAGE_PYTHON3),$(PYTHON3_PATH),$(PYTHON_PATH))" \
  59. PYTHONNOUSERSITE=1 \
  60. _python_sysroot=$(STAGING_DIR) \
  61. _python_prefix=/usr \
  62. _python_exec_prefix=/usr
  63. PKG_PYTHON_SETUPTOOLS_INSTALL_TARGET_OPTS = \
  64. --prefix=/usr \
  65. --executable=/usr/bin/python \
  66. --single-version-externally-managed \
  67. --root=$(TARGET_DIR)
  68. PKG_PYTHON_SETUPTOOLS_INSTALL_STAGING_OPTS = \
  69. --prefix=/usr \
  70. --executable=/usr/bin/python \
  71. --single-version-externally-managed \
  72. --root=$(STAGING_DIR)
  73. # Host setuptools-based packages
  74. HOST_PKG_PYTHON_SETUPTOOLS_ENV = \
  75. PATH=$(BR_PATH) \
  76. PYTHONNOUSERSITE=1 \
  77. $(HOST_CONFIGURE_OPTS)
  78. HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPTS = \
  79. --prefix=$(HOST_DIR) \
  80. --root=/ \
  81. --single-version-externally-managed
  82. ifeq ($(BR2_PER_PACKAGE_DIRECTORIES),y)
  83. define PKG_PYTHON_FIXUP_SYSCONFIGDATA
  84. find $(HOST_DIR)/lib/python* $(STAGING_DIR)/usr/lib/python* \
  85. -name "_sysconfigdata*.py" | xargs --no-run-if-empty \
  86. $(SED) "s:$(PER_PACKAGE_DIR)/[^/]\+/:$(PER_PACKAGE_DIR)/$($(PKG)_NAME)/:g"
  87. endef
  88. endif
  89. ################################################################################
  90. # inner-python-package -- defines how the configuration, compilation
  91. # and installation of a Python package should be done, implements a
  92. # few hooks to tune the build process and calls the generic package
  93. # infrastructure to generate the necessary make targets
  94. #
  95. # argument 1 is the lowercase package name
  96. # argument 2 is the uppercase package name, including a HOST_ prefix
  97. # for host packages
  98. # argument 3 is the uppercase package name, without the HOST_ prefix
  99. # for host packages
  100. # argument 4 is the type (target or host)
  101. ################################################################################
  102. define inner-python-package
  103. $(2)_ENV ?=
  104. $(2)_BUILD_OPTS ?=
  105. $(2)_INSTALL_OPTS ?=
  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_BUILD_OPTS =
  125. $(2)_BASE_INSTALL_OPTS = $$(HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPTS)
  126. endif
  127. # Setuptools
  128. else ifeq ($$($(2)_SETUP_TYPE),setuptools)
  129. ifeq ($(4),target)
  130. $(2)_BASE_ENV = $$(PKG_PYTHON_SETUPTOOLS_ENV)
  131. $(2)_BASE_BUILD_TGT = build
  132. $(2)_BASE_BUILD_OPTS =
  133. $(2)_BASE_INSTALL_TARGET_OPTS = $$(PKG_PYTHON_SETUPTOOLS_INSTALL_TARGET_OPTS)
  134. $(2)_BASE_INSTALL_STAGING_OPTS = $$(PKG_PYTHON_SETUPTOOLS_INSTALL_STAGING_OPTS)
  135. else
  136. $(2)_BASE_ENV = $$(HOST_PKG_PYTHON_SETUPTOOLS_ENV)
  137. $(2)_BASE_BUILD_TGT = build
  138. $(2)_BASE_BUILD_OPTS =
  139. $(2)_BASE_INSTALL_OPTS = $$(HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPTS)
  140. endif
  141. else
  142. $$(error "Invalid $(2)_SETUP_TYPE. Valid options are 'distutils' or 'setuptools'")
  143. endif
  144. # Target packages need both the python interpreter on the target (for
  145. # runtime) and the python interpreter on the host (for
  146. # compilation). However, host packages only need the python
  147. # interpreter on the host, whose version may be enforced by setting
  148. # the *_NEEDS_HOST_PYTHON variable.
  149. #
  150. # So:
  151. # - for target packages, we always depend on the default python interpreter
  152. # (the one selected by the config);
  153. # - for host packages:
  154. # - if *_NEEDS_HOST_PYTHON is not set, then we depend on use the default
  155. # interperter;
  156. # - otherwise, we depend on the one requested by *_NEEDS_HOST_PYTHON.
  157. #
  158. ifeq ($(4),target)
  159. $(2)_DEPENDENCIES += $$(if $$(BR2_PACKAGE_PYTHON3),host-python3 python3,host-python python)
  160. else
  161. ifeq ($$($(2)_NEEDS_HOST_PYTHON),)
  162. $(2)_DEPENDENCIES += $$(if $$(BR2_PACKAGE_PYTHON3),host-python3,host-python)
  163. else
  164. ifeq ($$($(2)_NEEDS_HOST_PYTHON),python2)
  165. $(2)_DEPENDENCIES += host-python
  166. else ifeq ($$($(2)_NEEDS_HOST_PYTHON),python3)
  167. $(2)_DEPENDENCIES += host-python3
  168. else
  169. $$(error Incorrect value '$$($(2)_NEEDS_HOST_PYTHON)' for $(2)_NEEDS_HOST_PYTHON)
  170. endif
  171. endif # ($$($(2)_NEEDS_HOST_PYTHON),)
  172. endif # ($(4),target)
  173. # Setuptools based packages will need setuptools for the host Python
  174. # interpreter (both host and target).
  175. #
  176. # If we have a host package that says "I need Python 3", we install
  177. # setuptools for python3.
  178. #
  179. # If we have a host packge that says "I need Python 2", we install
  180. # setuptools for python2.
  181. #
  182. # If we have a target package, or a host package that doesn't have any
  183. # <pkg>_NEEDS_HOST_PYTHON, and BR2_PACKAGE_PYTHON3 is used, then
  184. # Python 3.x is the default Python interpreter, so we install
  185. # setuptools for python3.
  186. #
  187. # In all other cases, we install setuptools for python2. Those other
  188. # cases are: a target package or host package with
  189. # BR2_PACKAGE_PYTHON=y, or a host-package with neither
  190. # BR2_PACKAGE_PYTHON3=y or BR2_PACKAGE_PYTHON=y.
  191. ifeq ($$($(2)_SETUP_TYPE),setuptools)
  192. ifeq ($(4):$$($(2)_NEEDS_HOST_PYTHON),host:python3)
  193. $(2)_DEPENDENCIES += $$(if $$(filter host-python3-setuptools,$(1)),,host-python3-setuptools)
  194. else ifeq ($(4):$$($(2)_NEEDS_HOST_PYTHON),host:python2)
  195. $(2)_DEPENDENCIES += $$(if $$(filter host-python-setuptools,$(1)),,host-python-setuptools)
  196. else ifeq ($$(BR2_PACKAGE_PYTHON3),y)
  197. $(2)_DEPENDENCIES += $$(if $$(filter host-python3-setuptools,$(1)),,host-python3-setuptools)
  198. else
  199. $(2)_DEPENDENCIES += $$(if $$(filter host-python-setuptools,$(1)),,host-python-setuptools)
  200. endif
  201. endif # SETUP_TYPE
  202. # Python interpreter to use for building the package.
  203. #
  204. # We may want to specify the python interpreter to be used for building a
  205. # package, especially for host-packages (target packages must be built using
  206. # the same version of the interpreter as the one installed on the target).
  207. #
  208. # So:
  209. # - for target packages, we always use the default python interpreter (which
  210. # is the same version as the one built and installed on the target);
  211. # - for host packages:
  212. # - if *_NEEDS_HOST_PYTHON is not set, then we use use the default
  213. # interperter;
  214. # - otherwise, we use the one requested by *_NEEDS_HOST_PYTHON.
  215. #
  216. ifeq ($(4),target)
  217. $(2)_PYTHON_INTERPRETER = $$(HOST_DIR)/bin/python
  218. else
  219. ifeq ($$($(2)_NEEDS_HOST_PYTHON),)
  220. $(2)_PYTHON_INTERPRETER = $$(HOST_DIR)/bin/python
  221. else
  222. $(2)_PYTHON_INTERPRETER = $$(HOST_DIR)/bin/$$($(2)_NEEDS_HOST_PYTHON)
  223. endif
  224. endif
  225. $(2)_PRE_CONFIGURE_HOOKS += PKG_PYTHON_FIXUP_SYSCONFIGDATA
  226. #
  227. # Build step. Only define it if not already defined by the package .mk
  228. # file.
  229. #
  230. ifndef $(2)_BUILD_CMDS
  231. define $(2)_BUILD_CMDS
  232. (cd $$($$(PKG)_BUILDDIR)/; \
  233. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  234. $$($(2)_PYTHON_INTERPRETER) setup.py \
  235. $$($$(PKG)_BASE_BUILD_TGT) \
  236. $$($$(PKG)_BASE_BUILD_OPTS) $$($$(PKG)_BUILD_OPTS))
  237. endef
  238. endif
  239. #
  240. # Host installation step. Only define it if not already defined by the
  241. # package .mk file.
  242. #
  243. ifndef $(2)_INSTALL_CMDS
  244. define $(2)_INSTALL_CMDS
  245. (cd $$($$(PKG)_BUILDDIR)/; \
  246. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  247. $$($(2)_PYTHON_INTERPRETER) setup.py install \
  248. $$($$(PKG)_BASE_INSTALL_OPTS) $$($$(PKG)_INSTALL_OPTS))
  249. endef
  250. endif
  251. #
  252. # Target installation step. Only define it if not already defined by
  253. # the package .mk file.
  254. #
  255. ifndef $(2)_INSTALL_TARGET_CMDS
  256. define $(2)_INSTALL_TARGET_CMDS
  257. (cd $$($$(PKG)_BUILDDIR)/; \
  258. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  259. $$($(2)_PYTHON_INTERPRETER) setup.py install --no-compile \
  260. $$($$(PKG)_BASE_INSTALL_TARGET_OPTS) \
  261. $$($$(PKG)_INSTALL_TARGET_OPTS))
  262. endef
  263. endif
  264. #
  265. # Staging installation step. Only define it if not already defined by
  266. # the package .mk file.
  267. #
  268. ifndef $(2)_INSTALL_STAGING_CMDS
  269. define $(2)_INSTALL_STAGING_CMDS
  270. (cd $$($$(PKG)_BUILDDIR)/; \
  271. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  272. $$($(2)_PYTHON_INTERPRETER) setup.py install \
  273. $$($$(PKG)_BASE_INSTALL_STAGING_OPTS) \
  274. $$($$(PKG)_INSTALL_STAGING_OPTS))
  275. endef
  276. endif
  277. # Call the generic package infrastructure to generate the necessary
  278. # make targets
  279. $(call inner-generic-package,$(1),$(2),$(3),$(4))
  280. endef
  281. ################################################################################
  282. # python-package -- the target generator macro for Python packages
  283. ################################################################################
  284. python-package = $(call inner-python-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
  285. host-python-package = $(call inner-python-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)