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 informations 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_OPT
= \
36 --executable
=/usr
/bin
/python
38 PKG_PYTHON_DISTUTILS_INSTALL_OPT
= \
39 --prefix=$(TARGET_DIR
)/usr
41 # Host distutils-based packages
42 HOST_PKG_PYTHON_DISTUTILS_ENV
= \
45 HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPT
= \
46 --prefix=$(HOST_DIR
)/usr
48 # Target setuptools-based packages
49 PKG_PYTHON_SETUPTOOLS_ENV
= \
51 PYTHONPATH
="$(if $(BR2_PACKAGE_PYTHON3),$(PYTHON3_PATH),$(PYTHON_PATH))" \
52 _python_sysroot
=$(STAGING_DIR
) \
54 _python_exec_prefix
=/usr
56 PKG_PYTHON_SETUPTOOLS_INSTALL_OPT
= \
57 --prefix=$(TARGET_DIR
)/usr \
58 --executable
=/usr
/bin
/python \
59 --single-version-externally-managed \
62 # Host setuptools-based packages
63 HOST_PKG_PYTHON_SETUPTOOLS_ENV
= \
66 HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPT
= \
67 --prefix=$(HOST_DIR
)/usr
69 ################################################################################
70 # inner-python-package -- defines how the configuration, compilation
71 # and installation of a Python package should be done, implements a
72 # few hooks to tune the build process and calls the generic package
73 # infrastructure to generate the necessary make targets
75 # argument 1 is the lowercase package name
76 # argument 2 is the uppercase package name, including an HOST_ prefix
78 # argument 3 is the uppercase package name, without the HOST_ prefix
80 # argument 4 is the type (target or host)
81 ################################################################################
83 define inner-python-package
85 $(2)_SRCDIR
= $$($(2)_DIR
)/$($(2)_SUBDIR
)
86 $(2)_BUILDDIR
= $$($(2)_SRCDIR
)
92 ifndef $(2)_SETUP_TYPE
94 $(2)_SETUP_TYPE
= $($(3)_SETUP_TYPE
)
96 $$(error
"$(2)_SETUP_TYPE must be set")
101 ifeq ($$($(2)_SETUP_TYPE
),distutils
)
103 $(2)_BASE_ENV
= $$(PKG_PYTHON_DISTUTILS_ENV
)
104 $(2)_BASE_BUILD_TGT
= build
105 $(2)_BASE_BUILD_OPT
= $$(PKG_PYTHON_DISTUTILS_BUILD_OPT
)
106 $(2)_BASE_INSTALL_OPT
= $$(PKG_PYTHON_DISTUTILS_INSTALL_OPT
)
108 $(2)_BASE_ENV
= $$(HOST_PKG_PYTHON_DISTUTILS_ENV
)
109 $(2)_BASE_BUILD_TGT
= build
110 $(2)_BASE_BUILD_OPT
=
111 $(2)_BASE_INSTALL_OPT
= $$(HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPT
)
114 else ifeq ($$($(2)_SETUP_TYPE
),setuptools
)
116 $(2)_BASE_ENV
= $$(PKG_PYTHON_SETUPTOOLS_ENV
)
117 $(2)_BASE_BUILD_TGT
= build
118 $(2)_BASE_BUILD_OPT
=
119 $(2)_BASE_INSTALL_OPT
= $$(PKG_PYTHON_SETUPTOOLS_INSTALL_OPT
)
121 $(2)_BASE_ENV
= $$(HOST_PKG_PYTHON_SETUPTOOLS_ENV
)
122 $(2)_BASE_BUILD_TGT
= build
123 $(2)_BASE_BUILD_OPT
=
124 $(2)_BASE_INSTALL_OPT
= $$(HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPT
)
127 $$(error
"Invalid $(2)_SETUP_TYPE. Valid options are 'distutils' or 'setuptools'")
130 # The below statement intends to calculate the dependencies of host
131 # packages by derivating them from the dependencies of the
132 # corresponding target package, after adding the 'host-' prefix in
133 # front of the dependencies.
135 # However it must be repeated from inner-generic-package, as we need
136 # to exclude the python, host-python and host-python-setuptools
137 # packages, which are added below in the list of dependencies
138 # depending on the package characteristics, and shouldn't be derived
139 # automatically from the dependencies of the corresponding target
141 $(2)_DEPENDENCIES ?
= $(filter-out host-python host-python3 host-python-setuptools host-toolchain
$(1),$(patsubst host-host-
%,host-
%,$(addprefix host-
,$($(3)_DEPENDENCIES
))))
143 # Target packages need both the python interpreter on the target (for
144 # runtime) and the python interpreter on the host (for
145 # compilation). However, host packages only need the python
146 # interpreter on the host, whose version may be enforced by setting
147 # the *_NEEDS_HOST_PYTHON variable.
150 # - for target packages, we always depend on the default python interpreter
151 # (the one selected by the config);
152 # - for host packages:
153 # - if *_NEEDS_HOST_PYTHON is not set, then we depend on use the default
155 # - otherwise, we depend on the one requested by *_NEEDS_HOST_PYTHON.
158 $(2)_DEPENDENCIES
+= $(if
$(BR2_PACKAGE_PYTHON3
),host-python3 python3
,host-python python
)
160 ifeq ($($(2)_NEEDS_HOST_PYTHON
),)
161 $(2)_DEPENDENCIES
+= $(if
$(BR2_PACKAGE_PYTHON3
),host-python3
,host-python
)
163 ifeq ($($(2)_NEEDS_HOST_PYTHON
),python2
)
164 $(2)_DEPENDENCIES
+= host-python
165 else ifeq ($($(2)_NEEDS_HOST_PYTHON
),python3
)
166 $(2)_DEPENDENCIES
+= host-python3
168 $$(error Incorrect value
'$($(2)_NEEDS_HOST_PYTHON)' for
$(2)_NEEDS_HOST_PYTHON
)
170 endif # ($($(2)_NEEDS_HOST_PYTHON),)
171 endif # ($(4),target)
173 # Setuptools based packages will need host-python-setuptools (both
174 # host and target). We need to have a special exclusion for the
175 # host-setuptools package itself: it is setuptools-based, but
176 # shouldn't depend on host-setuptools (because it would otherwise
177 # depend on itself!).
178 ifeq ($$($(2)_SETUP_TYPE
),setuptools
)
179 ifneq ($(2),HOST_PYTHON_SETUPTOOLS
)
180 $(2)_DEPENDENCIES
+= host-python-setuptools
184 # Python interpreter to use for building the package.
186 # We may want to specify the python interpreter to be used for building a
187 # package, especially for host-packages (target packages must be built using
188 # the same version of the interpreter as the one installed on the target).
191 # - for target packages, we always use the default python interpreter (which
192 # is the same version as the one built and installed on the target);
193 # - for host packages:
194 # - if *_NEEDS_HOST_PYTHON is not set, then we use use the default
196 # - otherwise, we use the one requested by *_NEEDS_HOST_PYTHON.
199 $(2)_PYTHON_INTERPRETER
= $(HOST_DIR
)/usr
/bin
/python
201 ifeq ($($(2)_NEEDS_HOST_PYTHON
),)
202 $(2)_PYTHON_INTERPRETER
= $(HOST_DIR
)/usr
/bin
/python
204 $(2)_PYTHON_INTERPRETER
= $(HOST_DIR
)/usr
/bin
/$($(2)_NEEDS_HOST_PYTHON
)
209 # Build step. Only define it if not already defined by the package .mk
212 ifndef $(2)_BUILD_CMDS
213 define $(2)_BUILD_CMDS
214 (cd
$$($$(PKG
)_BUILDDIR
)/; \
215 $$($$(PKG
)_BASE_ENV
) $$($$(PKG
)_ENV
) \
216 $$($(2)_PYTHON_INTERPRETER
) setup.py \
217 $$($$(PKG
)_BASE_BUILD_TGT
) \
218 $$($$(PKG
)_BASE_BUILD_OPT
) $$($$(PKG
)_BUILD_OPT
))
223 # Host installation step. Only define it if not already defined by the
226 ifndef $(2)_INSTALL_CMDS
227 define $(2)_INSTALL_CMDS
228 (cd
$$($$(PKG
)_BUILDDIR
)/; \
229 $$($$(PKG
)_BASE_ENV
) $$($$(PKG
)_ENV
) \
230 $$($(2)_PYTHON_INTERPRETER
) setup.py
install \
231 $$($$(PKG
)_BASE_INSTALL_OPT
) $$($$(PKG
)_INSTALL_OPT
))
236 # Target installation step. Only define it if not already defined by
237 # the package .mk file.
239 ifndef $(2)_INSTALL_TARGET_CMDS
240 define $(2)_INSTALL_TARGET_CMDS
241 (cd
$$($$(PKG
)_BUILDDIR
)/; \
242 $$($$(PKG
)_BASE_ENV
) $$($$(PKG
)_ENV
) \
243 $$($(2)_PYTHON_INTERPRETER
) setup.py
install \
244 $$($$(PKG
)_BASE_INSTALL_OPT
) $$($$(PKG
)_INSTALL_OPT
))
248 # Call the generic package infrastructure to generate the necessary
250 $(call inner-generic-package
,$(1),$(2),$(3),$(4))
254 ################################################################################
255 # python-package -- the target generator macro for Python packages
256 ################################################################################
258 python-package
= $(call inner-python-package
,$(pkgname
),$(call UPPERCASE
,$(pkgname
)),$(call UPPERCASE
,$(pkgname
)),target
)
259 host-python-package
= $(call inner-python-package
,host-
$(pkgname
),$(call UPPERCASE
,host-
$(pkgname
)),$(call UPPERCASE
,$(pkgname
)),host
)