blob: 6bd3a6aba847f6da4466e3f70754a68c193716cd [file] [log] [blame]
Andrew Geissler9aee5002022-03-30 16:27:02 +00001SUMMARY = "The Python Programming Language"
2HOMEPAGE = "http://www.python.org"
3DESCRIPTION = "Python is a programming language that lets you work more quickly and integrate your systems more effectively."
4LICENSE = "PSF-2.0"
5SECTION = "devel/python"
6
7LIC_FILES_CHKSUM = "file://LICENSE;md5=4b8801e752a2c70ac41a5f9aa243f766"
8
9SRC_URI = "http://www.python.org/ftp/python/${PV}/Python-${PV}.tar.xz \
10 file://run-ptest \
11 file://create_manifest3.py \
12 file://get_module_deps3.py \
13 file://python3-manifest.json \
14 file://check_build_completeness.py \
15 file://reformat_sysconfig.py \
16 file://cgi_py.patch \
17 file://0001-Do-not-add-usr-lib-termcap-to-linker-flags-to-avoid-.patch \
18 ${@bb.utils.contains('PACKAGECONFIG', 'tk', '', 'file://avoid_warning_about_tkinter.patch', d)} \
19 file://0001-Do-not-use-the-shell-version-of-python-config-that-w.patch \
20 file://python-config.patch \
21 file://0001-Makefile.pre-use-qemu-wrapper-when-gathering-profile.patch \
22 file://0001-python3-use-cc_basename-to-replace-CC-for-checking-c.patch \
23 file://0001-bpo-36852-proper-detection-of-mips-architecture-for-.patch \
24 file://crosspythonpath.patch \
25 file://0001-Use-FLAG_REF-always-for-interned-strings.patch \
26 file://0001-test_locale.py-correct-the-test-output-format.patch \
27 file://0017-setup.py-do-not-report-missing-dependencies-for-disa.patch \
28 file://0001-Makefile-do-not-compile-.pyc-in-parallel.patch \
29 file://0020-configure.ac-setup.py-do-not-add-a-curses-include-pa.patch \
30 file://0001-Skip-failing-tests-due-to-load-variability-on-YP-AB.patch \
31 file://0001-test_ctypes.test_find-skip-without-tools-sdk.patch \
32 file://makerace.patch \
33 file://0001-sysconfig.py-use-platlibdir-also-for-purelib.patch \
34 file://0001-Lib-pty.py-handle-stdin-I-O-errors-same-way-as-maste.patch \
Patrick Williams03907ee2022-05-01 06:28:52 -050035 file://0001-setup.py-Do-not-detect-multiarch-paths-when-cross-co.patch \
Andrew Geissler78b72792022-06-14 06:47:25 -050036 file://deterministic_imports.patch \
Andrew Geissler9aee5002022-03-30 16:27:02 +000037 "
38
39SRC_URI:append:class-native = " \
40 file://0001-Lib-sysconfig.py-use-prefix-value-from-build-configu.patch \
41 file://0001-distutils-sysconfig-append-STAGING_LIBDIR-python-sys.patch \
42 file://12-distutils-prefix-is-inside-staging-area.patch \
43 file://0001-Don-t-search-system-for-headers-libraries.patch \
44 "
Patrick Williams45852732022-04-02 08:58:32 -050045SRC_URI[sha256sum] = "80bf925f571da436b35210886cf79f6eb5fa5d6c571316b73568343451f77a19"
Andrew Geissler9aee5002022-03-30 16:27:02 +000046
47# exclude pre-releases for both python 2.x and 3.x
48UPSTREAM_CHECK_REGEX = "[Pp]ython-(?P<pver>\d+(\.\d+)+).tar"
49UPSTREAM_CHECK_URI = "https://www.python.org/downloads/source/"
50
51CVE_PRODUCT = "python"
52
53# Upstream consider this expected behaviour
54CVE_CHECK_IGNORE += "CVE-2007-4559"
55# This is not exploitable when glibc has CVE-2016-10739 fixed.
56CVE_CHECK_IGNORE += "CVE-2019-18348"
57# These are specific to Microsoft Windows
58CVE_CHECK_IGNORE += "CVE-2020-15523 CVE-2022-26488"
Patrick Williams03907ee2022-05-01 06:28:52 -050059# The mailcap module is insecure by design, so this can't be fixed in a meaningful way.
60# The module will be removed in the future and flaws documented.
61CVE_CHECK_IGNORE += "CVE-2015-20107"
Andrew Geissler9aee5002022-03-30 16:27:02 +000062
63PYTHON_MAJMIN = "3.10"
64
65S = "${WORKDIR}/Python-${PV}"
66
67BBCLASSEXTEND = "native nativesdk"
68
69inherit autotools pkgconfig qemu ptest multilib_header update-alternatives
70
71MULTILIB_SUFFIX = "${@d.getVar('base_libdir',1).split('/')[-1]}"
72
73ALTERNATIVE:${PN}-dev = "python3-config"
74ALTERNATIVE_LINK_NAME[python3-config] = "${bindir}/python${PYTHON_MAJMIN}-config"
75ALTERNATIVE_TARGET[python3-config] = "${bindir}/python${PYTHON_MAJMIN}-config-${MULTILIB_SUFFIX}"
76
77
78DEPENDS = "bzip2-replacement-native libffi bzip2 openssl sqlite3 zlib virtual/libintl xz virtual/crypt util-linux-libuuid libtirpc libnsl2 autoconf-archive-native ncurses"
79DEPENDS:append:class-target = " python3-native"
80DEPENDS:append:class-nativesdk = " python3-native"
81
82# force to use the mutex+cond implementation (https://bugs.python.org/issue41710)
83CFLAGS += "-DHAVE_BROKEN_POSIX_SEMAPHORES"
84
85EXTRA_OECONF = " --without-ensurepip --enable-shared --with-platlibdir=${baselib}"
86EXTRA_OECONF:append:class-native = " --bindir=${bindir}/${PN}"
87
88export CROSSPYTHONPATH="${STAGING_LIBDIR_NATIVE}/python${PYTHON_MAJMIN}/lib-dynload/"
89
90EXTRANATIVEPATH += "python3-native"
91
92# LTO will be enabled via packageconfig depending upong distro features
93LTO:class-target = ""
94
95CACHED_CONFIGUREVARS = " \
96 ac_cv_file__dev_ptmx=yes \
97 ac_cv_file__dev_ptc=no \
98 ac_cv_working_tzset=yes \
99"
100
101# PGO currently causes builds to not be reproducible so disable by default, see YOCTO #13407
102PACKAGECONFIG:class-target ??= "readline gdbm ${@bb.utils.filter('DISTRO_FEATURES', 'lto', d)}"
103PACKAGECONFIG:class-native ??= "readline gdbm"
104PACKAGECONFIG:class-nativesdk ??= "readline gdbm"
105PACKAGECONFIG[readline] = ",,readline"
106# Use profile guided optimisation by running PyBench inside qemu-user
107PACKAGECONFIG[pgo] = "--enable-optimizations,,qemu-native"
108PACKAGECONFIG[tk] = ",,tk"
109PACKAGECONFIG[gdbm] = ",,gdbm"
110PACKAGECONFIG[lto] = "--with-lto,,"
111
112do_configure:prepend () {
113 mkdir -p ${B}/Modules
114 cat > ${B}/Modules/Setup.local << EOF
115*disabled*
116${@bb.utils.contains('PACKAGECONFIG', 'gdbm', '', '_gdbm _dbm', d)}
117${@bb.utils.contains('PACKAGECONFIG', 'readline', '', 'readline', d)}
118EOF
119}
120
121CPPFLAGS:append = " -I${STAGING_INCDIR}/ncursesw -I${STAGING_INCDIR}/uuid"
122
123EXTRA_OEMAKE = '\
124 STAGING_LIBDIR=${STAGING_LIBDIR} \
125 STAGING_INCDIR=${STAGING_INCDIR} \
126 LIB=${baselib} \
127'
128
129do_compile:prepend:class-target() {
130 if ${@bb.utils.contains('PACKAGECONFIG', 'pgo', 'true', 'false', d)}; then
131 qemu_binary="${@qemu_wrapper_cmdline(d, '${STAGING_DIR_TARGET}', ['${B}', '${STAGING_DIR_TARGET}/${base_libdir}'])}"
132 cat >pgo-wrapper <<EOF
133#!/bin/sh
134cd ${B}
135$qemu_binary "\$@"
136EOF
137 chmod +x pgo-wrapper
138 fi
139}
140
141do_install:prepend() {
142 ${WORKDIR}/check_build_completeness.py ${T}/log.do_compile
143}
144
145do_install:append:class-target() {
146 oe_multilib_header python${PYTHON_MAJMIN}/pyconfig.h
147}
148
149do_install:append:class-native() {
150 # Make sure we use /usr/bin/env python
151 for PYTHSCRIPT in `grep -rIl ${bindir}/${PN}/python ${D}${bindir}/${PN}`; do
152 sed -i -e '1s|^#!.*|#!/usr/bin/env python3|' $PYTHSCRIPT
153 done
154 # Add a symlink to the native Python so that scripts can just invoke
155 # "nativepython" and get the right one without needing absolute paths
156 # (these often end up too long for the #! parser in the kernel as the
157 # buffer is 128 bytes long).
158 ln -s python3-native/python3 ${D}${bindir}/nativepython3
159
160 # Remove the opt-1.pyc and opt-2.pyc files. There are over 3,000 of them
161 # and the overhead in each recipe-sysroot-native isn't worth it, particularly
162 # when they're only used for python called with -O or -OO.
163 #find ${D} -name *opt-*.pyc -delete
164 # Remove all pyc files. There are a ton of them and it is probably faster to let
165 # python create the ones it wants at runtime rather than manage in the sstate
166 # tarballs and sysroot creation.
167 find ${D} -name *.pyc -delete
168
Andrew Geissler78b72792022-06-14 06:47:25 -0500169 # Nothing should be looking into ${B} for python3-native
170 sed -i -e 's:${B}:/build/path/unavailable/:g' \
171 ${D}/${libdir}/python${PYTHON_MAJMIN}/config-${PYTHON_MAJMIN}${PYTHON_ABI}*/Makefile
Andrew Geissler9aee5002022-03-30 16:27:02 +0000172}
173
174do_install:append() {
175 for c in ${D}/${libdir}/python${PYTHON_MAJMIN}/_sysconfigdata*.py; do
176 python3 ${WORKDIR}/reformat_sysconfig.py $c
177 done
178 rm -f ${D}${libdir}/python${PYTHON_MAJMIN}/__pycache__/_sysconfigdata*.cpython*
179
180 mkdir -p ${D}${libdir}/python-sysconfigdata
181 sysconfigfile=`find ${D} -name _sysconfig*.py`
Andrew Geissler9aee5002022-03-30 16:27:02 +0000182 sed -i \
183 -e "s,^ 'LIBDIR'.*, 'LIBDIR': '${STAGING_LIBDIR}'\,,g" \
184 -e "s,^ 'INCLUDEDIR'.*, 'INCLUDEDIR': '${STAGING_INCDIR}'\,,g" \
185 -e "s,^ 'CONFINCLUDEDIR'.*, 'CONFINCLUDEDIR': '${STAGING_INCDIR}'\,,g" \
186 -e "/^ 'INCLDIRSTOMAKE'/{N; s,/usr/include,${STAGING_INCDIR},g}" \
187 -e "/^ 'INCLUDEPY'/s,/usr/include,${STAGING_INCDIR},g" \
Andrew Geissler78b72792022-06-14 06:47:25 -0500188 -e "s,${B},/build/path/unavailable/,g" \
189 $sysconfigfile
190 cp $sysconfigfile ${D}${libdir}/python-sysconfigdata/_sysconfigdata.py
191
Andrew Geissler9aee5002022-03-30 16:27:02 +0000192
193 # Unfortunately the following pyc files are non-deterministc due to 'frozenset'
194 # being written without strict ordering, even with PYTHONHASHSEED = 0
195 # Upstream is discussing ways to solve the issue properly, until then let's
196 # just not install the problematic files.
197 # More info: http://benno.id.au/blog/2013/01/15/python-determinism
198 rm -f ${D}${libdir}/python${PYTHON_MAJMIN}/test/__pycache__/test_range.cpython*
199 rm -f ${D}${libdir}/python${PYTHON_MAJMIN}/test/__pycache__/test_xml_etree.cpython*
200
Andrew Geisslerd5838332022-05-27 11:33:10 -0500201 # Similar to the above, we're getting reproducibility issues with
202 # /usr/lib/python3.10/__pycache__/traceback.cpython-310.pyc
203 # so remove it too
204 rm -f ${D}${libdir}/python${PYTHON_MAJMIN}/__pycache__/traceback.cpython*
205
Andrew Geissler9aee5002022-03-30 16:27:02 +0000206 # Remove the opt-1.pyc and opt-2.pyc files. They effectively waste space on embedded
207 # style targets as they're only used when python is called with the -O or -OO options
208 # which is rare.
209 find ${D} -name *opt-*.pyc -delete
210}
211
212do_install:append:class-nativesdk () {
213 # Make sure we use /usr/bin/env python
214 for PYTHSCRIPT in `grep -rIl ${bindir}/python ${D}${bindir}`; do
215 sed -i -e '1s|^#!.*|#!/usr/bin/env python3|' $PYTHSCRIPT
216 done
217 create_wrapper ${D}${bindir}/python${PYTHON_MAJMIN} TERMINFO_DIRS='${sysconfdir}/terminfo:/etc/terminfo:/usr/share/terminfo:/usr/share/misc/terminfo:/lib/terminfo' PYTHONNOUSERSITE='1'
218}
219
220SSTATE_SCAN_FILES += "Makefile _sysconfigdata.py"
221SSTATE_HASHEQUIV_FILEMAP = " \
222 populate_sysroot:*/lib*/python3*/_sysconfigdata*.py:${TMPDIR} \
223 populate_sysroot:*/lib*/python3*/_sysconfigdata*.py:${COREBASE} \
224 populate_sysroot:*/lib*/python3*/config-*/Makefile:${TMPDIR} \
225 populate_sysroot:*/lib*/python3*/config-*/Makefile:${COREBASE} \
226 populate_sysroot:*/lib*/python-sysconfigdata/_sysconfigdata.py:${TMPDIR} \
227 populate_sysroot:*/lib*/python-sysconfigdata/_sysconfigdata.py:${COREBASE} \
228 "
229PACKAGE_PREPROCESS_FUNCS += "py_package_preprocess"
230
231py_package_preprocess () {
232 # Remove references to buildmachine paths in target Makefile and _sysconfigdata
233 sed -i -e 's:--sysroot=${STAGING_DIR_TARGET}::g' -e s:'--with-libtool-sysroot=${STAGING_DIR_TARGET}'::g \
234 -e 's|${DEBUG_PREFIX_MAP}||g' \
235 -e 's:${HOSTTOOLS_DIR}/::g' \
236 -e 's:${RECIPE_SYSROOT_NATIVE}::g' \
237 -e 's:${RECIPE_SYSROOT}::g' \
238 -e 's:${BASE_WORKDIR}/${MULTIMACH_TARGET_SYS}::g' \
239 ${PKGD}/${libdir}/python${PYTHON_MAJMIN}/config-${PYTHON_MAJMIN}${PYTHON_ABI}*/Makefile \
240 ${PKGD}/${libdir}/python${PYTHON_MAJMIN}/_sysconfigdata*.py \
241 ${PKGD}/${bindir}/python${PYTHON_MAJMIN}-config
242
243 # Reformat _sysconfigdata after modifying it so that it remains
244 # reproducible
245 for c in ${PKGD}/${libdir}/python${PYTHON_MAJMIN}/_sysconfigdata*.py; do
246 python3 ${WORKDIR}/reformat_sysconfig.py $c
247 done
248
249 # Recompile _sysconfigdata after modifying it
250 cd ${PKGD}
251 sysconfigfile=`find . -name _sysconfigdata_*.py`
252 ${STAGING_BINDIR_NATIVE}/python3-native/python3 \
253 -c "from py_compile import compile; compile('$sysconfigfile')"
254 ${STAGING_BINDIR_NATIVE}/python3-native/python3 \
255 -c "from py_compile import compile; compile('$sysconfigfile', optimize=1)"
256 ${STAGING_BINDIR_NATIVE}/python3-native/python3 \
257 -c "from py_compile import compile; compile('$sysconfigfile', optimize=2)"
258 cd -
259
260 mv ${PKGD}/${bindir}/python${PYTHON_MAJMIN}-config ${PKGD}/${bindir}/python${PYTHON_MAJMIN}-config-${MULTILIB_SUFFIX}
261
262 #Remove the unneeded copy of target sysconfig data
263 rm -rf ${PKGD}/${libdir}/python-sysconfigdata
264}
265
266# We want bytecode precompiled .py files (.pyc's) by default
267# but the user may set it on their own conf
268INCLUDE_PYCS ?= "1"
269
270python(){
271 import collections, json
272
273 filename = os.path.join(d.getVar('THISDIR'), 'python3', 'python3-manifest.json')
274 # This python changes the datastore based on the contents of a file, so mark
275 # that dependency.
276 bb.parse.mark_dependency(d, filename)
277
278 with open(filename) as manifest_file:
279 manifest_str = manifest_file.read()
280 json_start = manifest_str.find('# EOC') + 6
281 manifest_file.seek(json_start)
282 manifest_str = manifest_file.read()
283 python_manifest = json.loads(manifest_str, object_pairs_hook=collections.OrderedDict)
284
285 # First set RPROVIDES for -native case
286 # Hardcoded since it cant be python3-native-foo, should be python3-foo-native
287 pn = 'python3'
288 rprovides = (d.getVar('RPROVIDES') or "").split()
289
290 # ${PN}-misc-native is not in the manifest
291 rprovides.append(pn + '-misc-native')
292
293 for key in python_manifest:
294 pypackage = pn + '-' + key + '-native'
295 if pypackage not in rprovides:
296 rprovides.append(pypackage)
297
298 d.setVar('RPROVIDES:class-native', ' '.join(rprovides))
299
300 # Then work on the target
301 include_pycs = d.getVar('INCLUDE_PYCS')
302
303 packages = d.getVar('PACKAGES').split()
304 pn = d.getVar('PN')
305
306 newpackages=[]
307 for key in python_manifest:
308 pypackage = pn + '-' + key
309
310 if pypackage not in packages:
311 # We need to prepend, otherwise python-misc gets everything
312 # so we use a new variable
313 newpackages.append(pypackage)
314
315 # "Build" python's manifest FILES, RDEPENDS and SUMMARY
316 d.setVar('FILES:' + pypackage, '')
317 for value in python_manifest[key]['files']:
318 d.appendVar('FILES:' + pypackage, ' ' + value)
319
320 # Add cached files
321 if include_pycs == '1':
322 for value in python_manifest[key]['cached']:
323 d.appendVar('FILES:' + pypackage, ' ' + value)
324
325 for value in python_manifest[key]['rdepends']:
326 # Make it work with or without $PN
327 if '${PN}' in value:
328 value=value.split('-', 1)[1]
329 d.appendVar('RDEPENDS:' + pypackage, ' ' + pn + '-' + value)
330
331 for value in python_manifest[key].get('rrecommends', ()):
332 if '${PN}' in value:
333 value=value.split('-', 1)[1]
334 d.appendVar('RRECOMMENDS:' + pypackage, ' ' + pn + '-' + value)
335
336 d.setVar('SUMMARY:' + pypackage, python_manifest[key]['summary'])
337
338 # Prepending so to avoid python-misc getting everything
339 packages = newpackages + packages
340 d.setVar('PACKAGES', ' '.join(packages))
341 d.setVar('ALLOW_EMPTY:${PN}-modules', '1')
342 d.setVar('ALLOW_EMPTY:${PN}-pkgutil', '1')
343
344 if "pgo" in d.getVar("PACKAGECONFIG").split() and not bb.utils.contains('MACHINE_FEATURES', 'qemu-usermode', True, False, d):
345 bb.fatal("pgo cannot be enabled as there is no qemu-usermode support for this architecture/machine")
346}
347
348# Files needed to create a new manifest
349
350do_create_manifest() {
351 # This task should be run with every new release of Python.
352 # We must ensure that PACKAGECONFIG enables everything when creating
353 # a new manifest, this is to base our new manifest on a complete
354 # native python build, containing all dependencies, otherwise the task
355 # wont be able to find the required files.
356 # e.g. BerkeleyDB is an optional build dependency so it may or may not
357 # be present, we must ensure it is.
358
359 cd ${WORKDIR}
360 # This needs to be executed by python-native and NOT by HOST's python
361 nativepython3 create_manifest3.py ${PYTHON_MAJMIN}
362 cp python3-manifest.json.new ${THISDIR}/python3/python3-manifest.json
363}
364
365# bitbake python -c create_manifest
366# Make sure we have native python ready when we create a new manifest
367addtask do_create_manifest after do_patch do_prepare_recipe_sysroot
368
369# manual dependency additions
370RRECOMMENDS:${PN}-core:append:class-nativesdk = " nativesdk-python3-modules"
371RRECOMMENDS:${PN}-crypt:append:class-target = " ${MLPREFIX}openssl ${MLPREFIX}ca-certificates"
372RRECOMMENDS:${PN}-crypt:append:class-nativesdk = " ${MLPREFIX}openssl ${MLPREFIX}ca-certificates"
373
374# For historical reasons PN is empty and provided by python3-modules
375FILES:${PN} = ""
376RPROVIDES:${PN}-modules = "${PN}"
377
378FILES:${PN}-pydoc += "${bindir}/pydoc${PYTHON_MAJMIN} ${bindir}/pydoc3"
379FILES:${PN}-idle += "${bindir}/idle3 ${bindir}/idle${PYTHON_MAJMIN}"
380
381# provide python-pyvenv from python3-venv
382RPROVIDES:${PN}-venv += "${MLPREFIX}python3-pyvenv"
383
384# package libpython3
385PACKAGES =+ "libpython3 libpython3-staticdev"
386FILES:libpython3 = "${libdir}/libpython*.so.*"
387FILES:libpython3-staticdev += "${libdir}/python${PYTHON_MAJMIN}/config-${PYTHON_MAJMIN}-*/libpython${PYTHON_MAJMIN}.a"
388INSANE_SKIP:${PN}-dev += "dev-elf"
389INSANE_SKIP:${PN}-ptest = "dev-deps"
390
391# catch all the rest (unsorted)
392PACKAGES += "${PN}-misc"
393RDEPENDS:${PN}-misc += "\
394 ${PN}-core \
395 ${PN}-email \
396 ${PN}-codecs \
397 ${PN}-pydoc \
398 ${PN}-pickle \
399 ${PN}-audio \
400 ${PN}-numbers \
401"
402RDEPENDS:${PN}-modules:append:class-target = " ${MLPREFIX}python3-misc"
403RDEPENDS:${PN}-modules:append:class-nativesdk = " ${MLPREFIX}python3-misc"
404FILES:${PN}-misc = "${libdir}/python${PYTHON_MAJMIN} ${libdir}/python${PYTHON_MAJMIN}/lib-dynload"
405
406# catch manpage
407PACKAGES += "${PN}-man"
408FILES:${PN}-man = "${datadir}/man"
409
410# See https://bugs.python.org/issue18748 and https://bugs.python.org/issue37395
411RDEPENDS:libpython3:append:libc-glibc = " libgcc"
412RDEPENDS:${PN}-ctypes:append:libc-glibc = " ${MLPREFIX}ldconfig"
413RDEPENDS:${PN}-ptest = "${PN}-modules ${PN}-tests ${PN}-dev unzip bzip2 libgcc tzdata-europe coreutils sed"
414RDEPENDS:${PN}-ptest:append:libc-glibc = " locale-base-tr-tr.iso-8859-9"
415RDEPENDS:${PN}-tkinter += "${@bb.utils.contains('PACKAGECONFIG', 'tk', '${MLPREFIX}tk ${MLPREFIX}tk-lib', '', d)}"
416RDEPENDS:${PN}-idle += "${@bb.utils.contains('PACKAGECONFIG', 'tk', '${PN}-tkinter ${MLPREFIX}tcl', '', d)}"
417RDEPENDS:${PN}-dev = ""
418RDEPENDS:${PN}-pydoc += "${PN}-io"
419
420RDEPENDS:${PN}-tests:append:class-target = " ${MLPREFIX}bash"
421RDEPENDS:${PN}-tests:append:class-nativesdk = " ${MLPREFIX}bash"
422
423# Python's tests contain large numbers of files we don't need in the recipe sysroots
424SYSROOT_PREPROCESS_FUNCS += " py3_sysroot_cleanup"
425py3_sysroot_cleanup () {
426 rm -rf ${SYSROOT_DESTDIR}${libdir}/python${PYTHON_MAJMIN}/test
427}