add buildroot (work in progress development)
[cmdllinux.git] / buildroot / _buildroot / engine0030 / pkg-python.mk
blobc0800e9add426e678039f062ba992eb6df7ae32c
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 define PKG_PYTHON_SYSCONFIGDATA_NAME
24 $(basename $(notdir $(wildcard $(STAGING_DIR)/usr/lib/python$(PYTHON3_VERSION_MAJOR)/_sysconfigdata_m_linux_*.py)))
25 endef
27 # Target distutils-based packages
28 PKG_PYTHON_DISTUTILS_ENV = \
29 PATH=$(BR_PATH) \
30 CC="$(TARGET_CC)" \
31 CFLAGS="$(TARGET_CFLAGS)" \
32 LDFLAGS="$(TARGET_LDFLAGS)" \
33 LDSHARED="$(TARGET_CROSS)gcc -shared" \
34 PYTHONPATH="$(if $(BR2_PACKAGE_PYTHON3),$(PYTHON3_PATH),$(PYTHON_PATH))" \
35 PYTHONNOUSERSITE=1 \
36 _PYTHON_SYSCONFIGDATA_NAME="$(PKG_PYTHON_SYSCONFIGDATA_NAME)" \
37 _python_sysroot=$(STAGING_DIR) \
38 _python_prefix=/usr \
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 = \
52 PATH=$(BR_PATH) \
53 PYTHONNOUSERSITE=1
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)" \
61 PATH=$(BR_PATH) \
62 PYTHONPATH="$(if $(BR2_PACKAGE_PYTHON3),$(PYTHON3_PATH),$(PYTHON_PATH))" \
63 PYTHONNOUSERSITE=1 \
64 _python_sysroot=$(STAGING_DIR) \
65 _python_prefix=/usr \
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 \
72 --root=/
74 PKG_PYTHON_SETUPTOOLS_INSTALL_STAGING_OPTS = \
75 --prefix=$(STAGING_DIR)/usr \
76 --executable=/usr/bin/python \
77 --single-version-externally-managed \
78 --root=/
80 # Host setuptools-based packages
81 HOST_PKG_PYTHON_SETUPTOOLS_ENV = \
82 PATH=$(BR_PATH) \
83 PYTHONNOUSERSITE=1
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
96 # for host packages
97 # argument 3 is the uppercase package name, without the HOST_ prefix
98 # for host packages
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)
107 $(2)_ENV ?=
108 $(2)_BUILD_OPTS ?=
109 $(2)_INSTALL_OPTS ?=
111 ifndef $(2)_SETUP_TYPE
112 ifdef $(3)_SETUP_TYPE
113 $(2)_SETUP_TYPE = $$($(3)_SETUP_TYPE)
114 else
115 $$(error "$(2)_SETUP_TYPE must be set")
116 endif
117 endif
119 # Distutils
120 ifeq ($$($(2)_SETUP_TYPE),distutils)
121 ifeq ($(4),target)
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)
127 else
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)
132 endif
133 # Setuptools
134 else ifeq ($$($(2)_SETUP_TYPE),setuptools)
135 ifeq ($(4),target)
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)
141 else
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)
146 endif
147 else
148 $$(error "Invalid $(2)_SETUP_TYPE. Valid options are 'distutils' or 'setuptools'")
149 endif
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.
157 # So:
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
162 # interperter;
163 # - otherwise, we depend on the one requested by *_NEEDS_HOST_PYTHON.
165 ifeq ($(4),target)
166 $(2)_DEPENDENCIES += $$(if $$(BR2_PACKAGE_PYTHON3),python3,python)
167 else
168 ifeq ($$($(2)_NEEDS_HOST_PYTHON),)
169 $(2)_DEPENDENCIES += $$(if $$(BR2_PACKAGE_PYTHON3),python3,python)
170 else
171 ifeq ($$($(2)_NEEDS_HOST_PYTHON),python2)
172 $(2)_DEPENDENCIES += python
173 else ifeq ($$($(2)_NEEDS_HOST_PYTHON),python3)
174 $(2)_DEPENDENCIES += python3
175 else
176 $$(error Incorrect value '$$($(2)_NEEDS_HOST_PYTHON)' for $(2)_NEEDS_HOST_PYTHON)
177 endif
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!).
186 ifeq ($$($(2)_SETUP_TYPE),setuptools)
187 ifneq ($(2),HOST_PYTHON_SETUPTOOLS)
188 $(2)_DEPENDENCIES += python-setuptools
189 endif
190 endif
192 # Python interpreter to use for building the package.
194 # We may want to specify the python interpreter to be used for building a
195 # package, especially for host-packages (target packages must be built using
196 # the same version of the interpreter as the one installed on the target).
198 # So:
199 # - for target packages, we always use the default python interpreter (which
200 # is the same version as the one built and installed on the target);
201 # - for host packages:
202 # - if *_NEEDS_HOST_PYTHON is not set, then we use use the default
203 # interperter;
204 # - otherwise, we use the one requested by *_NEEDS_HOST_PYTHON.
206 ifeq ($(4),target)
207 $(2)_PYTHON_INTERPRETER = $$(HOST_DIR)/usr/bin/python
208 else
209 ifeq ($$($(2)_NEEDS_HOST_PYTHON),)
210 $(2)_PYTHON_INTERPRETER = $$(HOST_DIR)/usr/bin/python
211 else
212 $(2)_PYTHON_INTERPRETER = $$(HOST_DIR)/usr/bin/$$($(2)_NEEDS_HOST_PYTHON)
213 endif
214 endif
217 # Build step. Only define it if not already defined by the package .mk
218 # file.
220 ifndef $(2)_BUILD_CMDS
221 define $(2)_BUILD_CMDS
222 (cd $$($$(PKG)_BUILDDIR)/; \
223 $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
224 $$($(2)_PYTHON_INTERPRETER) setup.py \
225 $$($$(PKG)_BASE_BUILD_TGT) \
226 $$($$(PKG)_BASE_BUILD_OPTS) $$($$(PKG)_BUILD_OPTS))
227 endef
228 endif
231 # Host installation step. Only define it if not already defined by the
232 # package .mk file.
234 ifndef $(2)_INSTALL_CMDS
235 define $(2)_INSTALL_CMDS
236 (cd $$($$(PKG)_BUILDDIR)/; \
237 $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
238 $$($(2)_PYTHON_INTERPRETER) setup.py install \
239 $$($$(PKG)_BASE_INSTALL_OPTS) $$($$(PKG)_INSTALL_OPTS))
240 endef
241 endif
244 # Target installation step. Only define it if not already defined by
245 # the package .mk file.
247 ifndef $(2)_INSTALL_TARGET_CMDS
248 define $(2)_INSTALL_TARGET_CMDS
249 (cd $$($$(PKG)_BUILDDIR)/; \
250 $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
251 $$($(2)_PYTHON_INTERPRETER) setup.py install --no-compile \
252 $$($$(PKG)_BASE_INSTALL_TARGET_OPTS) \
253 $$($$(PKG)_INSTALL_TARGET_OPTS))
254 endef
255 endif
258 # Staging installation step. Only define it if not already defined by
259 # the package .mk file.
261 ifndef $(2)_INSTALL_STAGING_CMDS
262 define $(2)_INSTALL_STAGING_CMDS
263 (cd $$($$(PKG)_BUILDDIR)/; \
264 $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
265 $$($(2)_PYTHON_INTERPRETER) setup.py install \
266 $$($$(PKG)_BASE_INSTALL_STAGING_OPTS) \
267 $$($$(PKG)_INSTALL_STAGING_OPTS))
268 endef
269 endif
271 # Call the generic package infrastructure to generate the necessary
272 # make targets
273 $(call inner-generic-package,$(1),$(2),$(3),$(4))
275 endef
277 ################################################################################
278 # python-package -- the target generator macro for Python packages
279 ################################################################################
281 python-package = $(call inner-python-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
282 host-python-package = $(call inner-python-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)