1 ################################################################################
2 # Python package infrastructure
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.
8 # See the Buildroot documentation for details on the usage of this
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.
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.
21 ################################################################################
23 # Target distutils-based packages
24 PKG_PYTHON_DISTUTILS_ENV
= \
27 CFLAGS
="$(TARGET_CFLAGS)" \
28 LDFLAGS
="$(TARGET_LDFLAGS)" \
29 LDSHARED
="$(TARGET_CROSS)gcc -shared" \
30 PYTHONPATH
="$(if $(BR2_PACKAGE_PYTHON3),$(PYTHON3_PATH),$(PYTHON_PATH))" \
31 _python_sysroot
=$(STAGING_DIR
) \
33 _python_exec_prefix
=/usr
35 PKG_PYTHON_DISTUTILS_BUILD_OPTS
= \
36 --executable
=/usr
/bin
/python
38 PKG_PYTHON_DISTUTILS_INSTALL_TARGET_OPTS
= \
39 --prefix=$(TARGET_DIR
)/usr
41 PKG_PYTHON_DISTUTILS_INSTALL_STAGING_OPTS
= \
42 --prefix=$(STAGING_DIR
)/usr
44 # Host distutils-based packages
45 HOST_PKG_PYTHON_DISTUTILS_ENV
= \
48 HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPTS
= \
49 --prefix=$(HOST_DIR
)/usr
51 # Target setuptools-based packages
52 PKG_PYTHON_SETUPTOOLS_ENV
= \
54 PYTHONPATH
="$(if $(BR2_PACKAGE_PYTHON3),$(PYTHON3_PATH),$(PYTHON_PATH))" \
55 _python_sysroot
=$(STAGING_DIR
) \
57 _python_exec_prefix
=/usr
59 PKG_PYTHON_SETUPTOOLS_INSTALL_TARGET_OPTS
= \
60 --prefix=$(TARGET_DIR
)/usr \
61 --executable
=/usr
/bin
/python \
62 --single-version-externally-managed \
65 PKG_PYTHON_SETUPTOOLS_INSTALL_STAGING_OPTS
= \
66 --prefix=$(STAGING_DIR
)/usr \
67 --executable
=/usr
/bin
/python \
68 --single-version-externally-managed \
71 # Host setuptools-based packages
72 HOST_PKG_PYTHON_SETUPTOOLS_ENV
= \
75 HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPTS
= \
76 --prefix=$(HOST_DIR
)/usr
78 ################################################################################
79 # inner-python-package -- defines how the configuration, compilation
80 # and installation of a Python package should be done, implements a
81 # few hooks to tune the build process and calls the generic package
82 # infrastructure to generate the necessary make targets
84 # argument 1 is the lowercase package name
85 # argument 2 is the uppercase package name, including a HOST_ prefix
87 # argument 3 is the uppercase package name, without the HOST_ prefix
89 # argument 4 is the type (target or host)
90 ################################################################################
92 define inner-python-package
94 $(2)_SRCDIR
= $$($(2)_DIR
)/$$($(2)_SUBDIR
)
95 $(2)_BUILDDIR
= $$($(2)_SRCDIR
)
101 ifndef $(2)_SETUP_TYPE
102 ifdef $(3)_SETUP_TYPE
103 $(2)_SETUP_TYPE
= $$($(3)_SETUP_TYPE
)
105 $$(error
"$(2)_SETUP_TYPE must be set")
110 ifeq ($$($(2)_SETUP_TYPE
),distutils
)
112 $(2)_BASE_ENV
= $$(PKG_PYTHON_DISTUTILS_ENV
)
113 $(2)_BASE_BUILD_TGT
= build
114 $(2)_BASE_BUILD_OPTS
= $$(PKG_PYTHON_DISTUTILS_BUILD_OPTS
)
115 $(2)_BASE_INSTALL_TARGET_OPTS
= $$(PKG_PYTHON_DISTUTILS_INSTALL_TARGET_OPTS
)
116 $(2)_BASE_INSTALL_STAGING_OPTS
= $$(PKG_PYTHON_DISTUTILS_INSTALL_STAGING_OPTS
)
118 $(2)_BASE_ENV
= $$(HOST_PKG_PYTHON_DISTUTILS_ENV
)
119 $(2)_BASE_BUILD_TGT
= build
120 $(2)_BASE_BUILD_OPTS
=
121 $(2)_BASE_INSTALL_OPTS
= $$(HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPTS
)
124 else ifeq ($$($(2)_SETUP_TYPE
),setuptools
)
126 $(2)_BASE_ENV
= $$(PKG_PYTHON_SETUPTOOLS_ENV
)
127 $(2)_BASE_BUILD_TGT
= build
128 $(2)_BASE_BUILD_OPTS
=
129 $(2)_BASE_INSTALL_TARGET_OPTS
= $$(PKG_PYTHON_SETUPTOOLS_INSTALL_TARGET_OPTS
)
130 $(2)_BASE_INSTALL_STAGING_OPTS
= $$(PKG_PYTHON_SETUPTOOLS_INSTALL_STAGING_OPTS
)
132 $(2)_BASE_ENV
= $$(HOST_PKG_PYTHON_SETUPTOOLS_ENV
)
133 $(2)_BASE_BUILD_TGT
= build
134 $(2)_BASE_BUILD_OPTS
=
135 $(2)_BASE_INSTALL_OPTS
= $$(HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPTS
)
138 $$(error
"Invalid $(2)_SETUP_TYPE. Valid options are 'distutils' or 'setuptools'")
141 # The below statement intends to calculate the dependencies of host
142 # packages by derivating them from the dependencies of the
143 # corresponding target package, after adding the 'host-' prefix in
144 # front of the dependencies.
146 # However it must be repeated from inner-generic-package, as we need
147 # to exclude the python, host-python and host-python-setuptools
148 # packages, which are added below in the list of dependencies
149 # depending on the package characteristics, and shouldn't be derived
150 # automatically from the dependencies of the corresponding target
153 $(2)_DEPENDENCIES ?
= $$(filter-out host-python host-python3 host-python-setuptools host-toolchain
$(1),$$(patsubst host-host-
%,host-
%,$$(addprefix host-
,$$($(3)_DEPENDENCIES
))))
156 # Target packages need both the python interpreter on the target (for
157 # runtime) and the python interpreter on the host (for
158 # compilation). However, host packages only need the python
159 # interpreter on the host, whose version may be enforced by setting
160 # the *_NEEDS_HOST_PYTHON variable.
163 # - for target packages, we always depend on the default python interpreter
164 # (the one selected by the config);
165 # - for host packages:
166 # - if *_NEEDS_HOST_PYTHON is not set, then we depend on use the default
168 # - otherwise, we depend on the one requested by *_NEEDS_HOST_PYTHON.
171 $(2)_DEPENDENCIES
+= $$(if
$$(BR2_PACKAGE_PYTHON3
),host-python3 python3
,host-python python
)
173 ifeq ($$($(2)_NEEDS_HOST_PYTHON
),)
174 $(2)_DEPENDENCIES
+= $$(if
$$(BR2_PACKAGE_PYTHON3
),host-python3
,host-python
)
176 ifeq ($$($(2)_NEEDS_HOST_PYTHON
),python2
)
177 $(2)_DEPENDENCIES
+= host-python
178 else ifeq ($$($(2)_NEEDS_HOST_PYTHON
),python3
)
179 $(2)_DEPENDENCIES
+= host-python3
181 $$(error Incorrect value
'$$($(2)_NEEDS_HOST_PYTHON)' for
$(2)_NEEDS_HOST_PYTHON
)
183 endif # ($$($(2)_NEEDS_HOST_PYTHON),)
184 endif # ($(4),target)
186 # Setuptools based packages will need host-python-setuptools (both
187 # host and target). We need to have a special exclusion for the
188 # host-setuptools package itself: it is setuptools-based, but
189 # shouldn't depend on host-setuptools (because it would otherwise
190 # depend on itself!).
191 ifeq ($$($(2)_SETUP_TYPE
),setuptools
)
192 ifneq ($(2),HOST_PYTHON_SETUPTOOLS
)
193 $(2)_DEPENDENCIES
+= host-python-setuptools
197 # Python interpreter to use for building the package.
199 # We may want to specify the python interpreter to be used for building a
200 # package, especially for host-packages (target packages must be built using
201 # the same version of the interpreter as the one installed on the target).
204 # - for target packages, we always use the default python interpreter (which
205 # is the same version as the one built and installed on the target);
206 # - for host packages:
207 # - if *_NEEDS_HOST_PYTHON is not set, then we use use the default
209 # - otherwise, we use the one requested by *_NEEDS_HOST_PYTHON.
212 $(2)_PYTHON_INTERPRETER
= $$(HOST_DIR
)/usr
/bin
/python
214 ifeq ($$($(2)_NEEDS_HOST_PYTHON
),)
215 $(2)_PYTHON_INTERPRETER
= $$(HOST_DIR
)/usr
/bin
/python
217 $(2)_PYTHON_INTERPRETER
= $$(HOST_DIR
)/usr
/bin
/$$($(2)_NEEDS_HOST_PYTHON
)
222 # Build step. Only define it if not already defined by the package .mk
225 ifndef $(2)_BUILD_CMDS
226 define $(2)_BUILD_CMDS
227 (cd
$$($$(PKG
)_BUILDDIR
)/; \
228 $$($$(PKG
)_BASE_ENV
) $$($$(PKG
)_ENV
) \
229 $$($(2)_PYTHON_INTERPRETER
) setup.py \
230 $$($$(PKG
)_BASE_BUILD_TGT
) \
231 $$($$(PKG
)_BASE_BUILD_OPTS
) $$($$(PKG
)_BUILD_OPTS
))
236 # Host installation step. Only define it if not already defined by the
239 ifndef $(2)_INSTALL_CMDS
240 define $(2)_INSTALL_CMDS
241 (cd
$$($$(PKG
)_BUILDDIR
)/; \
242 $$($$(PKG
)_BASE_ENV
) $$($$(PKG
)_ENV
) \
243 $$($(2)_PYTHON_INTERPRETER
) setup.py
install \
244 $$($$(PKG
)_BASE_INSTALL_OPTS
) $$($$(PKG
)_INSTALL_OPTS
))
249 # Target installation step. Only define it if not already defined by
250 # the package .mk file.
252 ifndef $(2)_INSTALL_TARGET_CMDS
253 define $(2)_INSTALL_TARGET_CMDS
254 (cd
$$($$(PKG
)_BUILDDIR
)/; \
255 $$($$(PKG
)_BASE_ENV
) $$($$(PKG
)_ENV
) \
256 $$($(2)_PYTHON_INTERPRETER
) setup.py
install \
257 $$($$(PKG
)_BASE_INSTALL_TARGET_OPTS
) \
258 $$($$(PKG
)_INSTALL_TARGET_OPTS
))
263 # Staging installation step. Only define it if not already defined by
264 # the package .mk file.
266 ifndef $(2)_INSTALL_STAGING_CMDS
267 define $(2)_INSTALL_STAGING_CMDS
268 (cd
$$($$(PKG
)_BUILDDIR
)/; \
269 $$($$(PKG
)_BASE_ENV
) $$($$(PKG
)_ENV
) \
270 $$($(2)_PYTHON_INTERPRETER
) setup.py
install \
271 $$($$(PKG
)_BASE_INSTALL_STAGING_OPTS
) \
272 $$($$(PKG
)_INSTALL_STAGING_OPTS
))
276 # Call the generic package infrastructure to generate the necessary
278 $(call inner-generic-package
,$(1),$(2),$(3),$(4))
282 ################################################################################
283 # python-package -- the target generator macro for Python packages
284 ################################################################################
286 python-package
= $(call inner-python-package
,$(pkgname
),$(call UPPERCASE
,$(pkgname
)),$(call UPPERCASE
,$(pkgname
)),target
)
287 host-python-package
= $(call inner-python-package
,host-
$(pkgname
),$(call UPPERCASE
,host-
$(pkgname
)),$(call UPPERCASE
,$(pkgname
)),host
)