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 define PKG_PYTHON_SYSCONFIGDATA_NAME
24 $(basename $(notdir $(wildcard $(STAGING_DIR
)/usr
/lib
/python
$(PYTHON3_VERSION_MAJOR
)/_sysconfigdata_m_linux_
*.py
)))
27 # Target distutils-based packages
28 PKG_PYTHON_DISTUTILS_ENV
= \
31 CFLAGS
="$(TARGET_CFLAGS)" \
32 LDFLAGS
="$(TARGET_LDFLAGS)" \
33 LDSHARED
="$(TARGET_CROSS)gcc -shared" \
34 PYTHONPATH
="$(if $(BR2_PACKAGE_PYTHON3),$(PYTHON3_PATH),$(PYTHON_PATH))" \
36 _PYTHON_SYSCONFIGDATA_NAME
="$(PKG_PYTHON_SYSCONFIGDATA_NAME)" \
37 _python_sysroot
=$(STAGING_DIR
) \
39 _python_exec_prefix
=/usr
41 PKG_PYTHON_DISTUTILS_BUILD_OPTS
= \
42 --executable
=/usr
/bin
/python
44 PKG_PYTHON_DISTUTILS_INSTALL_TARGET_OPTS
= \
45 --prefix=$(TARGET_DIR
)/usr
47 PKG_PYTHON_DISTUTILS_INSTALL_STAGING_OPTS
= \
48 --prefix=$(STAGING_DIR
)/usr
50 # Host distutils-based packages
51 HOST_PKG_PYTHON_DISTUTILS_ENV
= \
55 HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPTS
= \
56 --prefix=$(HOST_DIR
)/usr
58 # Target setuptools-based packages
59 PKG_PYTHON_SETUPTOOLS_ENV
= \
60 _PYTHON_SYSCONFIGDATA_NAME
="$(PKG_PYTHON_SYSCONFIGDATA_NAME)" \
62 PYTHONPATH
="$(if $(BR2_PACKAGE_PYTHON3),$(PYTHON3_PATH),$(PYTHON_PATH))" \
64 _python_sysroot
=$(STAGING_DIR
) \
66 _python_exec_prefix
=/usr
68 PKG_PYTHON_SETUPTOOLS_INSTALL_TARGET_OPTS
= \
69 --prefix=$(TARGET_DIR
)/usr \
70 --executable
=/usr
/bin
/python \
71 --single-version-externally-managed \
74 PKG_PYTHON_SETUPTOOLS_INSTALL_STAGING_OPTS
= \
75 --prefix=$(STAGING_DIR
)/usr \
76 --executable
=/usr
/bin
/python \
77 --single-version-externally-managed \
80 # Host setuptools-based packages
81 HOST_PKG_PYTHON_SETUPTOOLS_ENV
= \
85 HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPTS
= \
86 --prefix=$(HOST_DIR
)/usr
88 ################################################################################
89 # inner-python-package -- defines how the configuration, compilation
90 # and installation of a Python package should be done, implements a
91 # few hooks to tune the build process and calls the generic package
92 # infrastructure to generate the necessary make targets
94 # argument 1 is the lowercase package name
95 # argument 2 is the uppercase package name, including a HOST_ prefix
97 # argument 3 is the uppercase package name, without the HOST_ prefix
99 # argument 4 is the type (target or host)
100 ################################################################################
102 define inner-python-package
104 $(2)_SRCDIR
= $$($(2)_DIR
)/$$($(2)_SUBDIR
)
105 $(2)_BUILDDIR
= $$($(2)_SRCDIR
)
111 ifndef $(2)_SETUP_TYPE
112 ifdef $(3)_SETUP_TYPE
113 $(2)_SETUP_TYPE
= $$($(3)_SETUP_TYPE
)
115 $$(error
"$(2)_SETUP_TYPE must be set")
120 ifeq ($$($(2)_SETUP_TYPE
),distutils
)
122 $(2)_BASE_ENV
= $$(PKG_PYTHON_DISTUTILS_ENV
)
123 $(2)_BASE_BUILD_TGT
= build
124 $(2)_BASE_BUILD_OPTS
= $$(PKG_PYTHON_DISTUTILS_BUILD_OPTS
)
125 $(2)_BASE_INSTALL_TARGET_OPTS
= $$(PKG_PYTHON_DISTUTILS_INSTALL_TARGET_OPTS
)
126 $(2)_BASE_INSTALL_STAGING_OPTS
= $$(PKG_PYTHON_DISTUTILS_INSTALL_STAGING_OPTS
)
128 $(2)_BASE_ENV
= $$(HOST_PKG_PYTHON_DISTUTILS_ENV
)
129 $(2)_BASE_BUILD_TGT
= build
130 $(2)_BASE_BUILD_OPTS
=
131 $(2)_BASE_INSTALL_OPTS
= $$(HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPTS
)
134 else ifeq ($$($(2)_SETUP_TYPE
),setuptools
)
136 $(2)_BASE_ENV
= $$(PKG_PYTHON_SETUPTOOLS_ENV
)
137 $(2)_BASE_BUILD_TGT
= build
138 $(2)_BASE_BUILD_OPTS
=
139 $(2)_BASE_INSTALL_TARGET_OPTS
= $$(PKG_PYTHON_SETUPTOOLS_INSTALL_TARGET_OPTS
)
140 $(2)_BASE_INSTALL_STAGING_OPTS
= $$(PKG_PYTHON_SETUPTOOLS_INSTALL_STAGING_OPTS
)
142 $(2)_BASE_ENV
= $$(HOST_PKG_PYTHON_SETUPTOOLS_ENV
)
143 $(2)_BASE_BUILD_TGT
= build
144 $(2)_BASE_BUILD_OPTS
=
145 $(2)_BASE_INSTALL_OPTS
= $$(HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPTS
)
148 $$(error
"Invalid $(2)_SETUP_TYPE. Valid options are 'distutils' or 'setuptools'")
151 # Target packages need both the python interpreter on the target (for
152 # runtime) and the python interpreter on the host (for
153 # compilation). However, host packages only need the python
154 # interpreter on the host, whose version may be enforced by setting
155 # the *_NEEDS_HOST_PYTHON variable.
158 # - for target packages, we always depend on the default python interpreter
159 # (the one selected by the config);
160 # - for host packages:
161 # - if *_NEEDS_HOST_PYTHON is not set, then we depend on use the default
163 # - otherwise, we depend on the one requested by *_NEEDS_HOST_PYTHON.
166 $(2)_DEPENDENCIES
+= $$(if
$$(BR2_PACKAGE_PYTHON3
),python3
,python
)
168 ifeq ($$($(2)_NEEDS_HOST_PYTHON
),)
169 $(2)_DEPENDENCIES
+= $$(if
$$(BR2_PACKAGE_PYTHON3
),python3
,python
)
171 ifeq ($$($(2)_NEEDS_HOST_PYTHON
),python2
)
172 $(2)_DEPENDENCIES
+= python
173 else ifeq ($$($(2)_NEEDS_HOST_PYTHON
),python3
)
174 $(2)_DEPENDENCIES
+= python3
176 $$(error Incorrect value
'$$($(2)_NEEDS_HOST_PYTHON)' for
$(2)_NEEDS_HOST_PYTHON
)
178 endif # ($$($(2)_NEEDS_HOST_PYTHON),)
179 endif # ($(4),target)
181 # Setuptools based packages will need host-python-setuptools (both
182 # host and target). We need to have a special exclusion for the
183 # host-setuptools package itself: it is setuptools-based, but
184 # shouldn't depend on host-setuptools (because it would otherwise
185 # depend on itself!).
188 ifeq ($$($(2)_SETUP_TYPE
),setuptools
)
189 ifneq ($(2),HOST_PYTHON_SETUPTOOLS
)
190 $(2)_DEPENDENCIES
+= python-setuptools
194 # Python interpreter to use for building the package.
196 # We may want to specify the python interpreter to be used for building a
197 # package, especially for host-packages (target packages must be built using
198 # the same version of the interpreter as the one installed on the target).
201 # - for target packages, we always use the default python interpreter (which
202 # is the same version as the one built and installed on the target);
203 # - for host packages:
204 # - if *_NEEDS_HOST_PYTHON is not set, then we use use the default
206 # - otherwise, we use the one requested by *_NEEDS_HOST_PYTHON.
209 $(2)_PYTHON_INTERPRETER
= $$(HOST_DIR
)/usr
/bin
/python
211 ifeq ($$($(2)_NEEDS_HOST_PYTHON
),)
212 $(2)_PYTHON_INTERPRETER
= $$(HOST_DIR
)/usr
/bin
/python
214 $(2)_PYTHON_INTERPRETER
= $$(HOST_DIR
)/usr
/bin
/$$($(2)_NEEDS_HOST_PYTHON
)
219 # Build step. Only define it if not already defined by the package .mk
222 ifndef $(2)_BUILD_CMDS
223 define $(2)_BUILD_CMDS
224 (cd
$$($$(PKG
)_BUILDDIR
)/; \
225 $$($$(PKG
)_BASE_ENV
) $$($$(PKG
)_ENV
) \
226 $$($(2)_PYTHON_INTERPRETER
) setup.py \
227 $$($$(PKG
)_BASE_BUILD_TGT
) \
228 $$($$(PKG
)_BASE_BUILD_OPTS
) $$($$(PKG
)_BUILD_OPTS
))
233 # Host installation step. Only define it if not already defined by the
236 ifndef $(2)_INSTALL_CMDS
237 define $(2)_INSTALL_CMDS
238 (cd
$$($$(PKG
)_BUILDDIR
)/; \
239 $$($$(PKG
)_BASE_ENV
) $$($$(PKG
)_ENV
) \
240 $$($(2)_PYTHON_INTERPRETER
) setup.py
install \
241 $$($$(PKG
)_BASE_INSTALL_OPTS
) $$($$(PKG
)_INSTALL_OPTS
))
246 # Target installation step. Only define it if not already defined by
247 # the package .mk file.
249 ifndef $(2)_INSTALL_TARGET_CMDS
250 define $(2)_INSTALL_TARGET_CMDS
251 (cd
$$($$(PKG
)_BUILDDIR
)/; \
252 $$($$(PKG
)_BASE_ENV
) $$($$(PKG
)_ENV
) \
253 $$($(2)_PYTHON_INTERPRETER
) setup.py
install --no-compile \
254 $$($$(PKG
)_BASE_INSTALL_TARGET_OPTS
) \
255 $$($$(PKG
)_INSTALL_TARGET_OPTS
))
260 # Staging installation step. Only define it if not already defined by
261 # the package .mk file.
263 ifndef $(2)_INSTALL_STAGING_CMDS
264 define $(2)_INSTALL_STAGING_CMDS
265 (cd
$$($$(PKG
)_BUILDDIR
)/; \
266 $$($$(PKG
)_BASE_ENV
) $$($$(PKG
)_ENV
) \
267 $$($(2)_PYTHON_INTERPRETER
) setup.py
install \
268 $$($$(PKG
)_BASE_INSTALL_STAGING_OPTS
) \
269 $$($$(PKG
)_INSTALL_STAGING_OPTS
))
273 # Call the generic package infrastructure to generate the necessary
275 $(call inner-generic-package
,$(1),$(2),$(3),$(4))
279 ################################################################################
280 # python-package -- the target generator macro for Python packages
281 ################################################################################
283 python-package
= $(call inner-python-package
,$(pkgname
),$(call UPPERCASE
,$(pkgname
)),$(call UPPERCASE
,$(pkgname
)),target
)
284 host-python-package
= $(call inner-python-package
,host-
$(pkgname
),$(call UPPERCASE
,host-
$(pkgname
)),$(call UPPERCASE
,$(pkgname
)),host
)