blob: e9069204173cd64f87d8a240bf6f377c6d694c4a [file] [log] [blame]
Thomas Petazzonia6bba672013-12-11 21:26:36 +01001################################################################################
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
Thomas De Schampheleire60714bb2014-07-24 20:07:02 +020012# .mk file to only specify metadata information about the package:
Thomas Petazzonia6bba672013-12-11 21:26:36 +010013# 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
Andrey Smirnovd2afa012017-03-23 08:21:12 -070023define PKG_PYTHON_SYSCONFIGDATA_NAME
24$(basename $(notdir $(wildcard $(STAGING_DIR)/usr/lib/python$(PYTHON3_VERSION_MAJOR)/_sysconfigdata_m_linux_*.py)))
25endef
26
Thomas Petazzonia6bba672013-12-11 21:26:36 +010027# Target distutils-based packages
28PKG_PYTHON_DISTUTILS_ENV = \
Samuel Martin84455012014-04-15 00:31:06 +020029 PATH=$(BR_PATH) \
Thomas Petazzonia6bba672013-12-11 21:26:36 +010030 CC="$(TARGET_CC)" \
31 CFLAGS="$(TARGET_CFLAGS)" \
32 LDFLAGS="$(TARGET_LDFLAGS)" \
33 LDSHARED="$(TARGET_CROSS)gcc -shared" \
Thomas Petazzoni24cbcf12014-02-18 21:40:03 +010034 PYTHONPATH="$(if $(BR2_PACKAGE_PYTHON3),$(PYTHON3_PATH),$(PYTHON_PATH))" \
Yegor Yefremovf5da1952017-04-06 20:46:08 +020035 PYTHONNOUSERSITE=1 \
Andrey Smirnovd2afa012017-03-23 08:21:12 -070036 _PYTHON_SYSCONFIGDATA_NAME="$(PKG_PYTHON_SYSCONFIGDATA_NAME)" \
Thomas Petazzonia6bba672013-12-11 21:26:36 +010037 _python_sysroot=$(STAGING_DIR) \
Thomas Petazzonia6bba672013-12-11 21:26:36 +010038 _python_prefix=/usr \
39 _python_exec_prefix=/usr
40
Thomas De Schampheleirea603eb12014-09-27 21:32:45 +020041PKG_PYTHON_DISTUTILS_BUILD_OPTS = \
Thomas Petazzonia6bba672013-12-11 21:26:36 +010042 --executable=/usr/bin/python
43
Thomas De Schampheleire57f2b8d2014-09-27 21:32:40 +020044PKG_PYTHON_DISTUTILS_INSTALL_TARGET_OPTS = \
Angelo Compagnuccie94280e2018-11-25 00:21:39 +010045 --prefix=/usr \
46 --root=$(TARGET_DIR)
Thomas Petazzonia6bba672013-12-11 21:26:36 +010047
Thomas De Schampheleired6c32da2014-09-27 21:32:41 +020048PKG_PYTHON_DISTUTILS_INSTALL_STAGING_OPTS = \
Angelo Compagnuccie94280e2018-11-25 00:21:39 +010049 --prefix=/usr \
50 --root=$(STAGING_DIR)
Thomas Petazzoni643627f2014-07-16 22:23:59 +020051
Thomas Petazzonia6bba672013-12-11 21:26:36 +010052# Host distutils-based packages
53HOST_PKG_PYTHON_DISTUTILS_ENV = \
Yegor Yefremovf5da1952017-04-06 20:46:08 +020054 PATH=$(BR_PATH) \
55 PYTHONNOUSERSITE=1
Thomas Petazzonia6bba672013-12-11 21:26:36 +010056
Thomas De Schampheleireb1993432014-09-27 21:32:39 +020057HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPTS = \
Arnout Vandecappelle048689b2017-07-04 16:04:03 +020058 --prefix=$(HOST_DIR)
Thomas Petazzonia6bba672013-12-11 21:26:36 +010059
60# Target setuptools-based packages
61PKG_PYTHON_SETUPTOOLS_ENV = \
Andrey Smirnovd2afa012017-03-23 08:21:12 -070062 _PYTHON_SYSCONFIGDATA_NAME="$(PKG_PYTHON_SYSCONFIGDATA_NAME)" \
Samuel Martin84455012014-04-15 00:31:06 +020063 PATH=$(BR_PATH) \
Thomas Petazzoni24cbcf12014-02-18 21:40:03 +010064 PYTHONPATH="$(if $(BR2_PACKAGE_PYTHON3),$(PYTHON3_PATH),$(PYTHON_PATH))" \
Yegor Yefremovf5da1952017-04-06 20:46:08 +020065 PYTHONNOUSERSITE=1 \
Thomas Petazzonib07b9d12013-12-22 18:02:10 +010066 _python_sysroot=$(STAGING_DIR) \
67 _python_prefix=/usr \
68 _python_exec_prefix=/usr
Thomas Petazzonia6bba672013-12-11 21:26:36 +010069
Thomas De Schampheleire57f2b8d2014-09-27 21:32:40 +020070PKG_PYTHON_SETUPTOOLS_INSTALL_TARGET_OPTS = \
Angelo Compagnuccie94280e2018-11-25 00:21:39 +010071 --prefix=/usr \
Thomas Petazzonia6bba672013-12-11 21:26:36 +010072 --executable=/usr/bin/python \
73 --single-version-externally-managed \
Angelo Compagnuccie94280e2018-11-25 00:21:39 +010074 --root=$(TARGET_DIR)
Thomas Petazzonia6bba672013-12-11 21:26:36 +010075
Thomas De Schampheleired6c32da2014-09-27 21:32:41 +020076PKG_PYTHON_SETUPTOOLS_INSTALL_STAGING_OPTS = \
Angelo Compagnuccie94280e2018-11-25 00:21:39 +010077 --prefix=/usr \
Thomas Petazzoni643627f2014-07-16 22:23:59 +020078 --executable=/usr/bin/python \
79 --single-version-externally-managed \
Angelo Compagnuccie94280e2018-11-25 00:21:39 +010080 --root=$(STAGING_DIR)
Thomas Petazzoni643627f2014-07-16 22:23:59 +020081
Thomas Petazzonia6bba672013-12-11 21:26:36 +010082# Host setuptools-based packages
83HOST_PKG_PYTHON_SETUPTOOLS_ENV = \
Yegor Yefremovf5da1952017-04-06 20:46:08 +020084 PATH=$(BR_PATH) \
85 PYTHONNOUSERSITE=1
Thomas Petazzonia6bba672013-12-11 21:26:36 +010086
Thomas De Schampheleireb1993432014-09-27 21:32:39 +020087HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPTS = \
Thomas Petazzonic577ad62018-12-28 18:01:31 +010088 --prefix=$(HOST_DIR) \
89 --root=/ \
90 --single-version-externally-managed
Thomas Petazzonia6bba672013-12-11 21:26:36 +010091
92################################################################################
93# inner-python-package -- defines how the configuration, compilation
94# and installation of a Python package should be done, implements a
95# few hooks to tune the build process and calls the generic package
96# infrastructure to generate the necessary make targets
97#
98# argument 1 is the lowercase package name
Thomas De Schampheleire5bd9ed62014-07-24 20:57:41 +020099# argument 2 is the uppercase package name, including a HOST_ prefix
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100100# for host packages
101# argument 3 is the uppercase package name, without the HOST_ prefix
102# for host packages
Thomas De Schampheleire26aef882014-02-05 10:44:03 +0100103# argument 4 is the type (target or host)
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100104################################################################################
105
106define inner-python-package
107
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100108$(2)_ENV ?=
Thomas De Schampheleirea603eb12014-09-27 21:32:45 +0200109$(2)_BUILD_OPTS ?=
Thomas De Schampheleireb1993432014-09-27 21:32:39 +0200110$(2)_INSTALL_OPTS ?=
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100111
112ifndef $(2)_SETUP_TYPE
113 ifdef $(3)_SETUP_TYPE
Thomas De Schampheleire54456cc2014-06-11 21:12:24 +0200114 $(2)_SETUP_TYPE = $$($(3)_SETUP_TYPE)
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100115 else
Peter Korsgaard27bc8862013-12-15 16:17:28 +0100116 $$(error "$(2)_SETUP_TYPE must be set")
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100117 endif
118endif
119
120# Distutils
121ifeq ($$($(2)_SETUP_TYPE),distutils)
Thomas De Schampheleire26aef882014-02-05 10:44:03 +0100122ifeq ($(4),target)
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100123$(2)_BASE_ENV = $$(PKG_PYTHON_DISTUTILS_ENV)
124$(2)_BASE_BUILD_TGT = build
Thomas De Schampheleirea603eb12014-09-27 21:32:45 +0200125$(2)_BASE_BUILD_OPTS = $$(PKG_PYTHON_DISTUTILS_BUILD_OPTS)
Thomas De Schampheleire57f2b8d2014-09-27 21:32:40 +0200126$(2)_BASE_INSTALL_TARGET_OPTS = $$(PKG_PYTHON_DISTUTILS_INSTALL_TARGET_OPTS)
Thomas De Schampheleired6c32da2014-09-27 21:32:41 +0200127$(2)_BASE_INSTALL_STAGING_OPTS = $$(PKG_PYTHON_DISTUTILS_INSTALL_STAGING_OPTS)
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100128else
129$(2)_BASE_ENV = $$(HOST_PKG_PYTHON_DISTUTILS_ENV)
130$(2)_BASE_BUILD_TGT = build
Thomas De Schampheleirea603eb12014-09-27 21:32:45 +0200131$(2)_BASE_BUILD_OPTS =
Thomas De Schampheleireb1993432014-09-27 21:32:39 +0200132$(2)_BASE_INSTALL_OPTS = $$(HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPTS)
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100133endif
134# Setuptools
135else ifeq ($$($(2)_SETUP_TYPE),setuptools)
Thomas De Schampheleire26aef882014-02-05 10:44:03 +0100136ifeq ($(4),target)
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100137$(2)_BASE_ENV = $$(PKG_PYTHON_SETUPTOOLS_ENV)
Thomas Petazzonic7bf3372014-02-18 21:39:54 +0100138$(2)_BASE_BUILD_TGT = build
Thomas De Schampheleirea603eb12014-09-27 21:32:45 +0200139$(2)_BASE_BUILD_OPTS =
Thomas De Schampheleire57f2b8d2014-09-27 21:32:40 +0200140$(2)_BASE_INSTALL_TARGET_OPTS = $$(PKG_PYTHON_SETUPTOOLS_INSTALL_TARGET_OPTS)
Thomas De Schampheleired6c32da2014-09-27 21:32:41 +0200141$(2)_BASE_INSTALL_STAGING_OPTS = $$(PKG_PYTHON_SETUPTOOLS_INSTALL_STAGING_OPTS)
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100142else
143$(2)_BASE_ENV = $$(HOST_PKG_PYTHON_SETUPTOOLS_ENV)
144$(2)_BASE_BUILD_TGT = build
Thomas De Schampheleirea603eb12014-09-27 21:32:45 +0200145$(2)_BASE_BUILD_OPTS =
Thomas De Schampheleireb1993432014-09-27 21:32:39 +0200146$(2)_BASE_INSTALL_OPTS = $$(HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPTS)
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100147endif
Peter Korsgaard27bc8862013-12-15 16:17:28 +0100148else
149$$(error "Invalid $(2)_SETUP_TYPE. Valid options are 'distutils' or 'setuptools'")
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100150endif
151
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100152# Target packages need both the python interpreter on the target (for
153# runtime) and the python interpreter on the host (for
154# compilation). However, host packages only need the python
Samuel Martin05754fa2014-03-05 23:04:42 +0100155# interpreter on the host, whose version may be enforced by setting
156# the *_NEEDS_HOST_PYTHON variable.
157#
158# So:
159# - for target packages, we always depend on the default python interpreter
160# (the one selected by the config);
161# - for host packages:
162# - if *_NEEDS_HOST_PYTHON is not set, then we depend on use the default
163# interperter;
164# - otherwise, we depend on the one requested by *_NEEDS_HOST_PYTHON.
165#
Thomas De Schampheleire26aef882014-02-05 10:44:03 +0100166ifeq ($(4),target)
Thomas De Schampheleire54456cc2014-06-11 21:12:24 +0200167$(2)_DEPENDENCIES += $$(if $$(BR2_PACKAGE_PYTHON3),host-python3 python3,host-python python)
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100168else
Thomas De Schampheleire54456cc2014-06-11 21:12:24 +0200169ifeq ($$($(2)_NEEDS_HOST_PYTHON),)
170$(2)_DEPENDENCIES += $$(if $$(BR2_PACKAGE_PYTHON3),host-python3,host-python)
Samuel Martin05754fa2014-03-05 23:04:42 +0100171else
Thomas De Schampheleire54456cc2014-06-11 21:12:24 +0200172ifeq ($$($(2)_NEEDS_HOST_PYTHON),python2)
Samuel Martin05754fa2014-03-05 23:04:42 +0100173$(2)_DEPENDENCIES += host-python
Thomas De Schampheleire54456cc2014-06-11 21:12:24 +0200174else ifeq ($$($(2)_NEEDS_HOST_PYTHON),python3)
Samuel Martin05754fa2014-03-05 23:04:42 +0100175$(2)_DEPENDENCIES += host-python3
176else
Thomas De Schampheleire54456cc2014-06-11 21:12:24 +0200177$$(error Incorrect value '$$($(2)_NEEDS_HOST_PYTHON)' for $(2)_NEEDS_HOST_PYTHON)
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100178endif
Thomas De Schampheleire54456cc2014-06-11 21:12:24 +0200179endif # ($$($(2)_NEEDS_HOST_PYTHON),)
Samuel Martin05754fa2014-03-05 23:04:42 +0100180endif # ($(4),target)
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100181
Thomas Petazzonif75239d2018-12-28 18:01:30 +0100182# Setuptools based packages will need setuptools for the host Python
183# interpreter (both host and target).
184#
185# If we have a host package that says "I need Python 3", we install
186# setuptools for python3.
187#
188# If we have a host packge that says "I need Python 2", we install
189# setuptools for python2.
190#
191# If we have a target package, or a host package that doesn't have any
192# <pkg>_NEEDS_HOST_PYTHON, and BR2_PACKAGE_PYTHON3 is used, then
193# Python 3.x is the default Python interpreter, so we install
194# setuptools for python3.
195#
196# In all other cases, we install setuptools for python2. Those other
197# cases are: a target package or host package with
198# BR2_PACKAGE_PYTHON=y, or a host-package with neither
199# BR2_PACKAGE_PYTHON3=y or BR2_PACKAGE_PYTHON=y.
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100200ifeq ($$($(2)_SETUP_TYPE),setuptools)
Thomas Petazzonif75239d2018-12-28 18:01:30 +0100201ifeq ($(4):$$($(2)_NEEDS_HOST_PYTHON),host:python3)
202$(2)_DEPENDENCIES += $$(if $$(filter host-python3-setuptools,$(1)),,host-python3-setuptools)
203else ifeq ($(4):$$($(2)_NEEDS_HOST_PYTHON),host:python2)
204$(2)_DEPENDENCIES += $$(if $$(filter host-python-setuptools,$(1)),,host-python-setuptools)
205else ifeq ($$(BR2_PACKAGE_PYTHON3),y)
206$(2)_DEPENDENCIES += $$(if $$(filter host-python3-setuptools,$(1)),,host-python3-setuptools)
207else
208$(2)_DEPENDENCIES += $$(if $$(filter host-python-setuptools,$(1)),,host-python-setuptools)
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100209endif
Thomas Petazzonif75239d2018-12-28 18:01:30 +0100210endif # SETUP_TYPE
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100211
Samuel Martin05754fa2014-03-05 23:04:42 +0100212# Python interpreter to use for building the package.
213#
Bernd Kuhls017f53d2014-04-12 13:57:15 +0200214# We may want to specify the python interpreter to be used for building a
Samuel Martin05754fa2014-03-05 23:04:42 +0100215# package, especially for host-packages (target packages must be built using
216# the same version of the interpreter as the one installed on the target).
217#
218# So:
219# - for target packages, we always use the default python interpreter (which
220# is the same version as the one built and installed on the target);
221# - for host packages:
222# - if *_NEEDS_HOST_PYTHON is not set, then we use use the default
223# interperter;
224# - otherwise, we use the one requested by *_NEEDS_HOST_PYTHON.
225#
226ifeq ($(4),target)
Arnout Vandecappelle0f9c0bf2017-07-05 13:14:19 +0200227$(2)_PYTHON_INTERPRETER = $$(HOST_DIR)/bin/python
Samuel Martin05754fa2014-03-05 23:04:42 +0100228else
Thomas De Schampheleire54456cc2014-06-11 21:12:24 +0200229ifeq ($$($(2)_NEEDS_HOST_PYTHON),)
Arnout Vandecappelle0f9c0bf2017-07-05 13:14:19 +0200230$(2)_PYTHON_INTERPRETER = $$(HOST_DIR)/bin/python
Samuel Martin05754fa2014-03-05 23:04:42 +0100231else
Arnout Vandecappelle0f9c0bf2017-07-05 13:14:19 +0200232$(2)_PYTHON_INTERPRETER = $$(HOST_DIR)/bin/$$($(2)_NEEDS_HOST_PYTHON)
Samuel Martin05754fa2014-03-05 23:04:42 +0100233endif
234endif
235
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100236#
237# Build step. Only define it if not already defined by the package .mk
238# file.
239#
240ifndef $(2)_BUILD_CMDS
241define $(2)_BUILD_CMDS
242 (cd $$($$(PKG)_BUILDDIR)/; \
243 $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
Samuel Martin05754fa2014-03-05 23:04:42 +0100244 $$($(2)_PYTHON_INTERPRETER) setup.py \
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100245 $$($$(PKG)_BASE_BUILD_TGT) \
Thomas De Schampheleirea603eb12014-09-27 21:32:45 +0200246 $$($$(PKG)_BASE_BUILD_OPTS) $$($$(PKG)_BUILD_OPTS))
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100247endef
248endif
249
250#
251# Host installation step. Only define it if not already defined by the
252# package .mk file.
253#
254ifndef $(2)_INSTALL_CMDS
255define $(2)_INSTALL_CMDS
256 (cd $$($$(PKG)_BUILDDIR)/; \
257 $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
Samuel Martin05754fa2014-03-05 23:04:42 +0100258 $$($(2)_PYTHON_INTERPRETER) setup.py install \
Thomas De Schampheleireb1993432014-09-27 21:32:39 +0200259 $$($$(PKG)_BASE_INSTALL_OPTS) $$($$(PKG)_INSTALL_OPTS))
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100260endef
261endif
262
263#
264# Target installation step. Only define it if not already defined by
265# the package .mk file.
266#
267ifndef $(2)_INSTALL_TARGET_CMDS
268define $(2)_INSTALL_TARGET_CMDS
269 (cd $$($$(PKG)_BUILDDIR)/; \
270 $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
Yegor Yefremov549bbba2016-05-17 23:19:15 +0200271 $$($(2)_PYTHON_INTERPRETER) setup.py install --no-compile \
Thomas De Schampheleire57f2b8d2014-09-27 21:32:40 +0200272 $$($$(PKG)_BASE_INSTALL_TARGET_OPTS) \
273 $$($$(PKG)_INSTALL_TARGET_OPTS))
Thomas Petazzoni643627f2014-07-16 22:23:59 +0200274endef
275endif
276
277#
278# Staging installation step. Only define it if not already defined by
279# the package .mk file.
280#
281ifndef $(2)_INSTALL_STAGING_CMDS
282define $(2)_INSTALL_STAGING_CMDS
283 (cd $$($$(PKG)_BUILDDIR)/; \
284 $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
285 $$($(2)_PYTHON_INTERPRETER) setup.py install \
Thomas De Schampheleired6c32da2014-09-27 21:32:41 +0200286 $$($$(PKG)_BASE_INSTALL_STAGING_OPTS) \
287 $$($$(PKG)_INSTALL_STAGING_OPTS))
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100288endef
289endif
290
291# Call the generic package infrastructure to generate the necessary
292# make targets
Thomas De Schampheleire26aef882014-02-05 10:44:03 +0100293$(call inner-generic-package,$(1),$(2),$(3),$(4))
Thomas Petazzonia6bba672013-12-11 21:26:36 +0100294
295endef
296
297################################################################################
298# python-package -- the target generator macro for Python packages
299################################################################################
300
Thomas De Schampheleire26aef882014-02-05 10:44:03 +0100301python-package = $(call inner-python-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
302host-python-package = $(call inner-python-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)