cmake: bump version to 3.6.3
[buildroot-gz.git] / package / pkg-python.mk
blob38bc45ffa33fa52d0e393a9915cc9af1a10d416c
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 # Target packages need both the python interpreter on the target (for
142 # runtime) and the python interpreter on the host (for
143 # compilation). However, host packages only need the python
144 # interpreter on the host, whose version may be enforced by setting
145 # the *_NEEDS_HOST_PYTHON variable.
147 # So:
148 # - for target packages, we always depend on the default python interpreter
149 # (the one selected by the config);
150 # - for host packages:
151 # - if *_NEEDS_HOST_PYTHON is not set, then we depend on use the default
152 # interperter;
153 # - otherwise, we depend on the one requested by *_NEEDS_HOST_PYTHON.
155 ifeq ($(4),target)
156 $(2)_DEPENDENCIES += $$(if $$(BR2_PACKAGE_PYTHON3),host-python3 python3,host-python python)
157 else
158 ifeq ($$($(2)_NEEDS_HOST_PYTHON),)
159 $(2)_DEPENDENCIES += $$(if $$(BR2_PACKAGE_PYTHON3),host-python3,host-python)
160 else
161 ifeq ($$($(2)_NEEDS_HOST_PYTHON),python2)
162 $(2)_DEPENDENCIES += host-python
163 else ifeq ($$($(2)_NEEDS_HOST_PYTHON),python3)
164 $(2)_DEPENDENCIES += host-python3
165 else
166 $$(error Incorrect value '$$($(2)_NEEDS_HOST_PYTHON)' for $(2)_NEEDS_HOST_PYTHON)
167 endif
168 endif # ($$($(2)_NEEDS_HOST_PYTHON),)
169 endif # ($(4),target)
171 # Setuptools based packages will need host-python-setuptools (both
172 # host and target). We need to have a special exclusion for the
173 # host-setuptools package itself: it is setuptools-based, but
174 # shouldn't depend on host-setuptools (because it would otherwise
175 # depend on itself!).
176 ifeq ($$($(2)_SETUP_TYPE),setuptools)
177 ifneq ($(2),HOST_PYTHON_SETUPTOOLS)
178 $(2)_DEPENDENCIES += host-python-setuptools
179 endif
180 endif
182 # Python interpreter to use for building the package.
184 # We may want to specify the python interpreter to be used for building a
185 # package, especially for host-packages (target packages must be built using
186 # the same version of the interpreter as the one installed on the target).
188 # So:
189 # - for target packages, we always use the default python interpreter (which
190 # is the same version as the one built and installed on the target);
191 # - for host packages:
192 # - if *_NEEDS_HOST_PYTHON is not set, then we use use the default
193 # interperter;
194 # - otherwise, we use the one requested by *_NEEDS_HOST_PYTHON.
196 ifeq ($(4),target)
197 $(2)_PYTHON_INTERPRETER = $$(HOST_DIR)/usr/bin/python
198 else
199 ifeq ($$($(2)_NEEDS_HOST_PYTHON),)
200 $(2)_PYTHON_INTERPRETER = $$(HOST_DIR)/usr/bin/python
201 else
202 $(2)_PYTHON_INTERPRETER = $$(HOST_DIR)/usr/bin/$$($(2)_NEEDS_HOST_PYTHON)
203 endif
204 endif
207 # Build step. Only define it if not already defined by the package .mk
208 # file.
210 ifndef $(2)_BUILD_CMDS
211 define $(2)_BUILD_CMDS
212 (cd $$($$(PKG)_BUILDDIR)/; \
213 $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
214 $$($(2)_PYTHON_INTERPRETER) setup.py \
215 $$($$(PKG)_BASE_BUILD_TGT) \
216 $$($$(PKG)_BASE_BUILD_OPTS) $$($$(PKG)_BUILD_OPTS))
217 endef
218 endif
221 # Host installation step. Only define it if not already defined by the
222 # package .mk file.
224 ifndef $(2)_INSTALL_CMDS
225 define $(2)_INSTALL_CMDS
226 (cd $$($$(PKG)_BUILDDIR)/; \
227 $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
228 $$($(2)_PYTHON_INTERPRETER) setup.py install \
229 $$($$(PKG)_BASE_INSTALL_OPTS) $$($$(PKG)_INSTALL_OPTS))
230 endef
231 endif
234 # Target installation step. Only define it if not already defined by
235 # the package .mk file.
237 ifndef $(2)_INSTALL_TARGET_CMDS
238 define $(2)_INSTALL_TARGET_CMDS
239 (cd $$($$(PKG)_BUILDDIR)/; \
240 $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
241 $$($(2)_PYTHON_INTERPRETER) setup.py install --no-compile \
242 $$($$(PKG)_BASE_INSTALL_TARGET_OPTS) \
243 $$($$(PKG)_INSTALL_TARGET_OPTS))
244 endef
245 endif
248 # Staging installation step. Only define it if not already defined by
249 # the package .mk file.
251 ifndef $(2)_INSTALL_STAGING_CMDS
252 define $(2)_INSTALL_STAGING_CMDS
253 (cd $$($$(PKG)_BUILDDIR)/; \
254 $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
255 $$($(2)_PYTHON_INTERPRETER) setup.py install \
256 $$($$(PKG)_BASE_INSTALL_STAGING_OPTS) \
257 $$($$(PKG)_INSTALL_STAGING_OPTS))
258 endef
259 endif
261 # Call the generic package infrastructure to generate the necessary
262 # make targets
263 $(call inner-generic-package,$(1),$(2),$(3),$(4))
265 endef
267 ################################################################################
268 # python-package -- the target generator macro for Python packages
269 ################################################################################
271 python-package = $(call inner-python-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
272 host-python-package = $(call inner-python-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)