edid-decode: new package
[buildroot-gz.git] / package / pkg-python.mk
blobb7a702d0f79ca81e5b6d5c55941a31b2df138c19
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
9 # infrastructure
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 = \
25 PATH=$(BR_PATH) \
26 CC="$(TARGET_CC)" \
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) \
32 _python_prefix=/usr \
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 = \
46 PATH=$(BR_PATH)
48 HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPTS = \
49 --prefix=$(HOST_DIR)/usr
51 # Target setuptools-based packages
52 PKG_PYTHON_SETUPTOOLS_ENV = \
53 PATH=$(BR_PATH) \
54 PYTHONPATH="$(if $(BR2_PACKAGE_PYTHON3),$(PYTHON3_PATH),$(PYTHON_PATH))" \
55 _python_sysroot=$(STAGING_DIR) \
56 _python_prefix=/usr \
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 \
63 --root=/
65 PKG_PYTHON_SETUPTOOLS_INSTALL_STAGING_OPTS = \
66 --prefix=$(STAGING_DIR)/usr \
67 --executable=/usr/bin/python \
68 --single-version-externally-managed \
69 --root=/
71 # Host setuptools-based packages
72 HOST_PKG_PYTHON_SETUPTOOLS_ENV = \
73 PATH=$(BR_PATH)
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
86 # for host packages
87 # argument 3 is the uppercase package name, without the HOST_ prefix
88 # for host packages
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)
97 $(2)_ENV ?=
98 $(2)_BUILD_OPTS ?=
99 $(2)_INSTALL_OPTS ?=
101 ifndef $(2)_SETUP_TYPE
102 ifdef $(3)_SETUP_TYPE
103 $(2)_SETUP_TYPE = $$($(3)_SETUP_TYPE)
104 else
105 $$(error "$(2)_SETUP_TYPE must be set")
106 endif
107 endif
109 # Distutils
110 ifeq ($$($(2)_SETUP_TYPE),distutils)
111 ifeq ($(4),target)
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)
117 else
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)
122 endif
123 # Setuptools
124 else ifeq ($$($(2)_SETUP_TYPE),setuptools)
125 ifeq ($(4),target)
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)
131 else
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)
136 endif
137 else
138 $$(error "Invalid $(2)_SETUP_TYPE. Valid options are 'distutils' or 'setuptools'")
139 endif
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
151 # package.
152 ifeq ($(4),host)
153 $(2)_DEPENDENCIES ?= $$(filter-out host-python host-python3 host-python-setuptools host-skeleton host-toolchain $(1),$$(patsubst host-host-%,host-%,$$(addprefix host-,$$($(3)_DEPENDENCIES))))
154 endif
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.
162 # So:
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
167 # interperter;
168 # - otherwise, we depend on the one requested by *_NEEDS_HOST_PYTHON.
170 ifeq ($(4),target)
171 $(2)_DEPENDENCIES += $$(if $$(BR2_PACKAGE_PYTHON3),host-python3 python3,host-python python)
172 else
173 ifeq ($$($(2)_NEEDS_HOST_PYTHON),)
174 $(2)_DEPENDENCIES += $$(if $$(BR2_PACKAGE_PYTHON3),host-python3,host-python)
175 else
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
180 else
181 $$(error Incorrect value '$$($(2)_NEEDS_HOST_PYTHON)' for $(2)_NEEDS_HOST_PYTHON)
182 endif
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
194 endif
195 endif
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).
203 # So:
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
208 # interperter;
209 # - otherwise, we use the one requested by *_NEEDS_HOST_PYTHON.
211 ifeq ($(4),target)
212 $(2)_PYTHON_INTERPRETER = $$(HOST_DIR)/usr/bin/python
213 else
214 ifeq ($$($(2)_NEEDS_HOST_PYTHON),)
215 $(2)_PYTHON_INTERPRETER = $$(HOST_DIR)/usr/bin/python
216 else
217 $(2)_PYTHON_INTERPRETER = $$(HOST_DIR)/usr/bin/$$($(2)_NEEDS_HOST_PYTHON)
218 endif
219 endif
222 # Build step. Only define it if not already defined by the package .mk
223 # file.
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))
232 endef
233 endif
236 # Host installation step. Only define it if not already defined by the
237 # package .mk file.
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))
245 endef
246 endif
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))
259 endef
260 endif
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))
273 endef
274 endif
276 # Call the generic package infrastructure to generate the necessary
277 # make targets
278 $(call inner-generic-package,$(1),$(2),$(3),$(4))
280 endef
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)