-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
pkg-python.mk
468 lines (383 loc) · 13.7 KB
/
pkg-python.mk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
################################################################################
# Python package infrastructure
#
# This file implements an infrastructure that eases development of
# package .mk files for Python packages. It should be used for all
# packages that use Python setup.py/setuptools as their build system.
#
# See the Buildroot documentation for details on the usage of this
# infrastructure
#
# In terms of implementation, this Python infrastructure requires the
# .mk file to only specify metadata information about the package:
# name, version, download URL, etc.
#
# We still allow the package .mk file to override what the different
# steps are doing, if needed. For example, if <PKG>_BUILD_CMDS is
# already defined, it is used as the list of commands to perform to
# build the package, instead of the default Python behaviour. The
# package can also define some post operation hooks.
#
################################################################################
ifeq ($(BR2_arm)$(BR2_armeb),y)
PKG_PYTHON_ARCH = arm
else
PKG_PYTHON_ARCH = $(ARCH)
endif
PKG_PYTHON_HOST_PLATFORM = linux-$(PKG_PYTHON_ARCH)
# basename does not evaluate if a file exists, so we must check to ensure
# the _sysconfigdata__linux_*.py file exists. The "|| true" is added to return
# an empty string if the file does not exist.
PKG_PYTHON_SYSCONFIGDATA_PATH = $(PYTHON3_PATH)/_sysconfigdata__linux_*.py
PKG_PYTHON_SYSCONFIGDATA_NAME = `{ [ -e $(PKG_PYTHON_SYSCONFIGDATA_PATH) ] && basename $(PKG_PYTHON_SYSCONFIGDATA_PATH) .py; } || true`
# Target python packages
PKG_PYTHON_ENV = \
_PYTHON_HOST_PLATFORM="$(PKG_PYTHON_HOST_PLATFORM)" \
_PYTHON_PROJECT_BASE="$(PYTHON3_DIR)" \
_PYTHON_SYSCONFIGDATA_NAME="$(PKG_PYTHON_SYSCONFIGDATA_NAME)" \
PATH=$(BR_PATH) \
$(TARGET_CONFIGURE_OPTS) \
PYTHONPATH="$(PYTHON3_PATH)" \
PYTHONNOUSERSITE=1
# Host python packages
HOST_PKG_PYTHON_ENV = \
PATH=$(BR_PATH) \
PYTHONNOUSERSITE=1 \
$(HOST_CONFIGURE_OPTS)
# Target pep517-based packages
PKG_PYTHON_PEP517_ENV = \
$(PKG_PYTHON_ENV)
PKG_PYTHON_PEP517_BUILD_CMD = \
-m build -n -w
PKG_PYTHON_PEP517_INSTALL_OPTS = \
--interpreter=/usr/bin/python \
--script-kind=posix
PKG_PYTHON_PEP517_INSTALL_TARGET_CMD = \
$(TOPDIR)/support/scripts/pyinstaller.py \
dist/* \
$(PKG_PYTHON_PEP517_INSTALL_OPTS) \
--purelib=$(TARGET_DIR)/usr/lib/python$(PYTHON3_VERSION_MAJOR)/site-packages \
--headers=$(TARGET_DIR)/usr/include/python$(PYTHON3_VERSION_MAJOR) \
--scripts=$(TARGET_DIR)/usr/bin \
--data=$(TARGET_DIR)
PKG_PYTHON_PEP517_INSTALL_STAGING_CMD = \
$(TOPDIR)/support/scripts/pyinstaller.py \
dist/* \
$(PKG_PYTHON_PEP517_INSTALL_OPTS) \
--purelib=$(STAGING_DIR)/usr/lib/python$(PYTHON3_VERSION_MAJOR)/site-packages \
--headers=$(STAGING_DIR)/usr/include/python$(PYTHON3_VERSION_MAJOR) \
--scripts=$(STAGING_DIR)/usr/bin \
--data=$(STAGING_DIR)
PKG_PYTHON_PEP517_DEPENDENCIES = \
host-python-pypa-build \
host-python-installer
# Host pep517-based packages
HOST_PKG_PYTHON_PEP517_ENV = \
$(HOST_PKG_PYTHON_ENV)
HOST_PKG_PYTHON_PEP517_BUILD_CMD = \
-m build -n -w
HOST_PKG_PYTHON_PEP517_INSTALL_CMD = \
$(TOPDIR)/support/scripts/pyinstaller.py \
dist/* \
--interpreter=$(HOST_DIR)/bin/python \
--script-kind=posix \
--purelib=$(HOST_DIR)/lib/python$(PYTHON3_VERSION_MAJOR)/site-packages \
--headers=$(HOST_DIR)/include/python$(PYTHON3_VERSION_MAJOR) \
--scripts=$(HOST_DIR)/bin \
--data=$(HOST_DIR)
# Target setuptools-based packages
PKG_PYTHON_SETUPTOOLS_ENV = \
$(PKG_PYTHON_PEP517_ENV)
PKG_PYTHON_SETUPTOOLS_BUILD_CMD = \
$(PKG_PYTHON_PEP517_BUILD_CMD)
PKG_PYTHON_SETUPTOOLS_INSTALL_OPTS = \
--install-headers=/usr/include/python$(PYTHON3_VERSION_MAJOR) \
--prefix=/usr \
--executable=/usr/bin/python \
--single-version-externally-managed
PKG_PYTHON_SETUPTOOLS_INSTALL_TARGET_CMD = \
$(PKG_PYTHON_PEP517_INSTALL_TARGET_CMD)
PKG_PYTHON_SETUPTOOLS_INSTALL_STAGING_CMD = \
$(PKG_PYTHON_PEP517_INSTALL_STAGING_CMD)
PKG_PYTHON_SETUPTOOLS_DEPENDENCIES = \
$(PKG_PYTHON_PEP517_DEPENDENCIES) \
host-python-setuptools
# Host setuptools-based packages
HOST_PKG_PYTHON_SETUPTOOLS_ENV = \
$(HOST_PKG_PYTHON_PEP517_ENV)
HOST_PKG_PYTHON_SETUPTOOLS_BUILD_CMD = \
$(HOST_PKG_PYTHON_PEP517_BUILD_CMD)
HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_CMD = \
$(HOST_PKG_PYTHON_PEP517_INSTALL_CMD)
# Target setuptools-rust-based packages
PKG_PYTHON_SETUPTOOLS_RUST_ENV = \
$(PKG_PYTHON_SETUPTOOLS_ENV) \
$(PKG_CARGO_ENV) \
PYO3_CROSS_LIB_DIR="$(STAGING_DIR)/usr/lib/python$(PYTHON3_VERSION_MAJOR)"
PKG_PYTHON_SETUPTOOLS_RUST_BUILD_CMD = \
$(PKG_PYTHON_SETUPTOOLS_BUILD_CMD)
PKG_PYTHON_SETUPTOOLS_RUST_INSTALL_TARGET_CMD = \
$(PKG_PYTHON_SETUPTOOLS_INSTALL_TARGET_CMD)
PKG_PYTHON_SETUPTOOLS_RUST_INSTALL_STAGING_CMD = \
$(PKG_PYTHON_SETUPTOOLS_INSTALL_STAGING_CMD)
PKG_PYTHON_SETUPTOOLS_RUST_DEPENDENCIES = \
$(PKG_PYTHON_SETUPTOOLS_DEPENDENCIES) \
host-python-setuptools-rust
# Host setuptools-rust-based packages
HOST_PKG_PYTHON_SETUPTOOLS_RUST_ENV = \
$(HOST_PKG_PYTHON_SETUPTOOLS_ENV) \
$(HOST_PKG_CARGO_ENV) \
PYO3_CROSS_LIB_DIR="$(HOST_DIR)/lib/python$(PYTHON3_VERSION_MAJOR)"
HOST_PKG_PYTHON_SETUPTOOLS_RUST_BUILD_CMD = \
$(HOST_PKG_PYTHON_SETUPTOOLS_BUILD_CMD)
HOST_PKG_PYTHON_SETUPTOOLS_RUST_INSTALL_CMD = \
$(HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_CMD)
# Target flit packages
PKG_PYTHON_FLIT_ENV = \
$(PKG_PYTHON_PEP517_ENV)
PKG_PYTHON_FLIT_BUILD_CMD = \
$(PKG_PYTHON_PEP517_BUILD_CMD)
PKG_PYTHON_FLIT_INSTALL_TARGET_CMD = \
$(PKG_PYTHON_PEP517_INSTALL_TARGET_CMD)
PKG_PYTHON_FLIT_INSTALL_STAGING_CMD = \
$(PKG_PYTHON_PEP517_INSTALL_STAGING_CMD)
PKG_PYTHON_FLIT_DEPENDENCIES = \
$(PKG_PYTHON_PEP517_DEPENDENCIES) \
host-python-flit-core
# Host flit packages
HOST_PKG_PYTHON_FLIT_ENV = \
$(HOST_PKG_PYTHON_PEP517_ENV)
HOST_PKG_PYTHON_FLIT_BUILD_CMD = \
$(HOST_PKG_PYTHON_PEP517_BUILD_CMD)
HOST_PKG_PYTHON_FLIT_INSTALL_CMD = \
$(HOST_PKG_PYTHON_PEP517_INSTALL_CMD)
# Host flit-bootstrap packages
HOST_PKG_PYTHON_FLIT_BOOTSTRAP_ENV = \
$(HOST_PKG_PYTHON_PEP517_ENV)
HOST_PKG_PYTHON_FLIT_BOOTSTRAP_BUILD_CMD = \
-m flit_core.wheel
HOST_PKG_PYTHON_FLIT_BOOTSTRAP_INSTALL_CMD = \
$(HOST_PKG_PYTHON_PEP517_INSTALL_CMD)
# Target hatch packages
PKG_PYTHON_HATCH_ENV = \
$(PKG_PYTHON_PEP517_ENV)
PKG_PYTHON_HATCH_BUILD_CMD = \
$(PKG_PYTHON_PEP517_BUILD_CMD)
PKG_PYTHON_HATCH_INSTALL_TARGET_CMD = \
$(PKG_PYTHON_PEP517_INSTALL_TARGET_CMD)
PKG_PYTHON_HATCH_INSTALL_STAGING_CMD = \
$(PKG_PYTHON_PEP517_INSTALL_STAGING_CMD)
PKG_PYTHON_HATCH_DEPENDENCIES = \
$(PKG_PYTHON_PEP517_DEPENDENCIES) \
host-python-hatchling
# Host hatch packages
HOST_PKG_PYTHON_HATCH_ENV = \
$(HOST_PKG_PYTHON_PEP517_ENV)
HOST_PKG_PYTHON_HATCH_BUILD_CMD = \
$(HOST_PKG_PYTHON_PEP517_BUILD_CMD)
HOST_PKG_PYTHON_HATCH_INSTALL_CMD = \
$(HOST_PKG_PYTHON_PEP517_INSTALL_CMD)
# Target maturin packages
PKG_PYTHON_MATURIN_ENV = \
$(PKG_PYTHON_PEP517_ENV) \
$(PKG_CARGO_ENV) \
PYO3_CROSS_LIB_DIR="$(STAGING_DIR)/usr/lib/python$(PYTHON3_VERSION_MAJOR)"
PKG_PYTHON_MATURIN_BUILD_CMD = \
$(PKG_PYTHON_PEP517_BUILD_CMD)
PKG_PYTHON_MATURIN_INSTALL_TARGET_CMD = \
$(PKG_PYTHON_PEP517_INSTALL_TARGET_CMD)
PKG_PYTHON_MATURIN_INSTALL_STAGING_CMD = \
$(PKG_PYTHON_PEP517_INSTALL_STAGING_CMD)
PKG_PYTHON_MATURIN_DEPENDENCIES = \
$(PKG_PYTHON_PEP517_DEPENDENCIES) \
host-python-maturin
# Host maturin packages
HOST_PKG_PYTHON_MATURIN_ENV = \
$(HOST_PKG_PYTHON_PEP517_ENV) \
$(HOST_PKG_CARGO_ENV) \
PYO3_CROSS_LIB_DIR="$(HOST_DIR)/lib/python$(PYTHON3_VERSION_MAJOR)"
HOST_PKG_PYTHON_MATURIN_BUILD_CMD = \
$(HOST_PKG_PYTHON_PEP517_BUILD_CMD)
HOST_PKG_PYTHON_MATURIN_INSTALL_CMD = \
$(HOST_PKG_PYTHON_PEP517_INSTALL_CMD)
# Target poetry packages
PKG_PYTHON_POETRY_ENV = \
$(PKG_PYTHON_PEP517_ENV)
PKG_PYTHON_POETRY_BUILD_CMD = \
$(PKG_PYTHON_PEP517_BUILD_CMD)
PKG_PYTHON_POETRY_INSTALL_TARGET_CMD = \
$(PKG_PYTHON_PEP517_INSTALL_TARGET_CMD)
PKG_PYTHON_POETRY_INSTALL_STAGING_CMD = \
$(PKG_PYTHON_PEP517_INSTALL_STAGING_CMD)
PKG_PYTHON_POETRY_DEPENDENCIES = \
$(PKG_PYTHON_PEP517_DEPENDENCIES) \
host-python-poetry-core
# Host poetry packages
HOST_PKG_PYTHON_POETRY_ENV = \
$(HOST_PKG_PYTHON_PEP517_ENV)
HOST_PKG_PYTHON_POETRY_BUILD_CMD = \
$(HOST_PKG_PYTHON_PEP517_BUILD_CMD)
HOST_PKG_PYTHON_POETRY_INSTALL_CMD = \
$(HOST_PKG_PYTHON_PEP517_INSTALL_CMD)
################################################################################
# inner-python-package -- defines how the configuration, compilation
# and installation of a Python package should be done, implements a
# few hooks to tune the build process and calls the generic package
# infrastructure to generate the necessary make targets
#
# argument 1 is the lowercase package name
# argument 2 is the uppercase package name, including a HOST_ prefix
# for host packages
# argument 3 is the uppercase package name, without the HOST_ prefix
# for host packages
# argument 4 is the type (target or host)
################################################################################
define inner-python-package
ifndef $(2)_SETUP_TYPE
ifdef $(3)_SETUP_TYPE
$(2)_SETUP_TYPE = $$($(3)_SETUP_TYPE)
else
$$(error "$(2)_SETUP_TYPE must be set")
endif
endif
$(2)_SETUP_TYPE_UPPER = $$(call UPPERCASE,$$($(2)_SETUP_TYPE))
ifneq ($$(filter-out setuptools setuptools-rust pep517 flit flit-bootstrap hatch maturin poetry,$$($(2)_SETUP_TYPE)),)
$$(error "Invalid $(2)_SETUP_TYPE. Valid options are 'maturin', 'setuptools', 'setuptools-rust', 'pep517', 'flit', 'hatch' or 'poetry'.")
endif
ifeq ($(4)-$$($(2)_SETUP_TYPE),target-flit-bootstrap)
$$(error flit-bootstrap setup type only supported for host packages)
endif
# We need to vendor the Cargo crates at download time for pyo3 based
# packages.
#
ifneq ($$(filter maturin setuptools-rust,$$($(2)_SETUP_TYPE)),)
ifeq ($(4),target)
$(2)_DL_ENV = $$(PKG_CARGO_ENV)
else
$(2)_DL_ENV = $$(HOST_PKG_CARGO_ENV)
endif
ifndef $(2)_CARGO_MANIFEST_PATH
ifdef $(3)_CARGO_MANIFEST_PATH
$(2)_DOWNLOAD_POST_PROCESS_OPTS += -m$$($(3)_CARGO_MANIFEST_PATH)
else
ifneq ($$($(2)_SUBDIR),)
$(2)_DOWNLOAD_POST_PROCESS_OPTS += -m$$($(2)_SUBDIR)/Cargo.toml
endif
endif
else
$(2)_DOWNLOAD_POST_PROCESS_OPTS += -m$$($(2)_CARGO_MANIFEST_PATH)
endif
endif
# Target packages need both the python interpreter on the target (for
# runtime) and the python interpreter on the host (for
# compilation). However, host packages only need the python
# interpreter on the host.
#
ifeq ($(4),target)
$(2)_DEPENDENCIES += host-python3 python3
else
$(2)_DEPENDENCIES += host-python3
endif # ($(4),target)
# Setup type specific dependencies are the same whether we are
# building for the host or the target.
#
ifeq ($$($(2)_SETUP_TYPE),flit-bootstrap)
# Don't add dependency on host-python-installer for
# host-python-installer itself, and its dependencies.
ifeq ($$(filter host-python-flit-core host-python-installer,$(1)),)
$(2)_DEPENDENCIES += host-python-installer
endif
else
$(2)_DEPENDENCIES += $$(PKG_PYTHON_$$($(2)_SETUP_TYPE_UPPER)_DEPENDENCIES)
endif
# Pyo3 based packages(setuptools-rust and maturin) will need rust
# toolchain dependencies for the host Python interpreter (both host
# and target).
#
ifneq ($$(filter maturin setuptools-rust,$$($(2)_SETUP_TYPE)),)
$(2)_DEPENDENCIES += host-rustc
$(2)_DOWNLOAD_POST_PROCESS = cargo
$(2)_DOWNLOAD_DEPENDENCIES = host-rustc
endif # SETUP_TYPE
ifeq ($(4),target)
#
# Build step. Only define it if not already defined by the package .mk
# file.
#
ifndef $(2)_BUILD_CMDS
define $(2)_BUILD_CMDS
(cd $$($$(PKG)_BUILDDIR)/; \
$$(PKG_PYTHON_$$($$(PKG)_SETUP_TYPE_UPPER)_ENV) \
$$($$(PKG)_ENV) \
$$(HOST_DIR)/bin/python3 \
$$(PKG_PYTHON_$$($$(PKG)_SETUP_TYPE_UPPER)_BUILD_CMD) \
$$($$(PKG)_BUILD_OPTS))
endef
endif
#
# Target installation step. Only define it if not already defined by
# the package .mk file.
#
ifndef $(2)_INSTALL_TARGET_CMDS
define $(2)_INSTALL_TARGET_CMDS
(cd $$($$(PKG)_BUILDDIR)/; \
$$(PKG_PYTHON_$$($$(PKG)_SETUP_TYPE_UPPER)_ENV) \
$$($$(PKG)_ENV) \
$$(HOST_DIR)/bin/python3 \
$$(PKG_PYTHON_$$($$(PKG)_SETUP_TYPE_UPPER)_INSTALL_TARGET_CMD) \
$$($$(PKG)_INSTALL_TARGET_OPTS))
endef
endif
#
# Staging installation step. Only define it if not already defined by
# the package .mk file.
#
ifndef $(2)_INSTALL_STAGING_CMDS
define $(2)_INSTALL_STAGING_CMDS
(cd $$($$(PKG)_BUILDDIR)/; \
$$(PKG_PYTHON_$$($$(PKG)_SETUP_TYPE_UPPER)_ENV) \
$$($$(PKG)_ENV) \
$$(HOST_DIR)/bin/python3 \
$$(PKG_PYTHON_$$($$(PKG)_SETUP_TYPE_UPPER)_INSTALL_STAGING_CMD) \
$$($$(PKG)_INSTALL_STAGING_OPTS))
endef
endif
else # host
#
# Host build step. Only define it if not already defined by the package .mk
# file.
#
ifndef $(2)_BUILD_CMDS
define $(2)_BUILD_CMDS
(cd $$($$(PKG)_BUILDDIR)/; \
$$(HOST_PKG_PYTHON_$$($$(PKG)_SETUP_TYPE_UPPER)_ENV) \
$$($$(PKG)_ENV) \
$$(HOST_DIR)/bin/python3 \
$$(HOST_PKG_PYTHON_$$($$(PKG)_SETUP_TYPE_UPPER)_BUILD_CMD) \
$$($$(PKG)_BUILD_OPTS))
endef
endif
#
# Host installation step. Only define it if not already defined by the
# package .mk file.
#
ifndef $(2)_INSTALL_CMDS
define $(2)_INSTALL_CMDS
(cd $$($$(PKG)_BUILDDIR)/; \
$$(HOST_PKG_PYTHON_$$($$(PKG)_SETUP_TYPE_UPPER)_ENV) \
$$($$(PKG)_ENV) \
$$(HOST_DIR)/bin/python3 \
$$(HOST_PKG_PYTHON_$$($$(PKG)_SETUP_TYPE_UPPER)_INSTALL_CMD) \
$$($$(PKG)_INSTALL_OPTS))
endef
endif
endif # host / target
# Call the generic package infrastructure to generate the necessary
# make targets
$(call inner-generic-package,$(1),$(2),$(3),$(4))
endef
################################################################################
# python-package -- the target generator macro for Python packages
################################################################################
python-package = $(call inner-python-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
host-python-package = $(call inner-python-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)