1 # Copyright 1999-2025 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
7 inherit autotools check-reqs flag-o-matic multiprocessing pax-utils
8 inherit prefix python-utils-r1 toolchain-funcs verify-sig
11 MY_P="Python-${MY_PV%_p*}"
13 PATCHSET="python-gentoo-patches-${MY_PV}"
15 DESCRIPTION="An interpreted, interactive, object-oriented programming language"
17 https://www.python.org/
18 https://github.com/python/cpython/
21 https://www.python.org/ftp/python/${PV%%_*}/${MY_P}.tar.xz
22 https://dev.gentoo.org/~mgorny/dist/python/${PATCHSET}.tar.xz
24 https://www.python.org/ftp/python/${PV%%_*}/${MY_P}.tar.xz.asc
27 S="${WORKDIR}/${MY_P}"
31 KEYWORDS="~alpha amd64 arm arm64 hppa ~loong ~m68k ~mips ppc ppc64 ~riscv ~s390 sparc x86"
33 bluetooth build debug +ensurepip examples gdbm +ncurses pgo
34 +readline +sqlite +ssl test tk valgrind
36 RESTRICT="!test? ( test )"
38 # Do not add a dependency on dev-lang/python to this ebuild.
39 # If you need to apply a patch which requires python for bootstrapping, please
40 # run the bootstrap code on your dev box and include the results in the
41 # patchset. See bug 447752.
46 >=dev-libs/expat-2.1:=
49 dev-python/gentoo-common
50 >=sys-libs/zlib-1.1.3:=
53 ensurepip? ( dev-python/ensurepip-wheels )
54 gdbm? ( sys-libs/gdbm:=[berkdb] )
55 kernel_linux? ( sys-apps/util-linux:= )
56 ncurses? ( >=sys-libs/ncurses-5.2:= )
57 readline? ( >=sys-libs/readline-4.1:= )
58 sqlite? ( >=dev-db/sqlite-3.3.8:3= )
59 ssl? ( >=dev-libs/openssl-1.1.1:= )
67 # bluetooth requires headers from bluez
70 bluetooth? ( net-wireless/bluez )
71 test? ( app-arch/xz-utils )
72 valgrind? ( dev-debug/valgrind )
74 # autoconf-archive needed to eautoreconf
76 dev-build/autoconf-archive
79 verify-sig? ( sec-keys/openpgp-keys-python )
82 !build? ( app-misc/mime-types )
85 VERIFY_SIG_OPENPGP_KEY_PATH=/usr/share/openpgp-keys/python.org.asc
87 # large file tests involve a 2.5G file being copied (duplicated)
88 CHECKREQS_DISK_BUILD=5500M
90 QA_PKGCONFIG_VERSION=${PYVER}
91 # false positives -- functions specific to *BSD
92 QA_CONFIG_IMPL_DECL_SKIP=( chflags lchflags )
95 use test && check-reqs_pkg_pretend
99 use test && check-reqs_pkg_setup
103 if use verify-sig; then
104 verify-sig_verify_detached "${DISTDIR}"/${MY_P}.tar.xz{,.asc}
110 # Ensure that internal copies of expat and libffi are not used.
111 rm -r Modules/expat || die
112 rm -r Modules/_ctypes/libffi* || die
115 "${WORKDIR}/${PATCHSET}"
120 # https://bugs.gentoo.org/850151
121 sed -i -e "s:@@GENTOO_LIBDIR@@:$(get_libdir):g" setup.py || die
123 # force the correct number of jobs
124 # https://bugs.gentoo.org/737660
125 local jobs=$(makeopts_jobs)
126 sed -i -e "s:-j0:-j${jobs}:" Makefile.pre.in || die
127 sed -i -e "/self\.parallel/s:True:${jobs}:" setup.py || die
133 # disable automagic bluetooth headers detection
134 if ! use bluetooth; then
135 local -x ac_cv_header_bluetooth_bluetooth_h=no
138 use gdbm || disable+=" gdbm"
139 use ncurses || disable+=" _curses _curses_panel"
140 use readline || disable+=" readline"
141 use sqlite || disable+=" _sqlite3"
142 use ssl || export PYTHON_DISABLE_SSL="1"
143 use tk || disable+=" _tkinter"
144 export PYTHON_DISABLE_MODULES="${disable}"
146 if [[ -n "${PYTHON_DISABLE_MODULES}" ]]; then
147 einfo "Disabled modules: ${PYTHON_DISABLE_MODULES}"
151 filter-flags -malign-double
153 # Export CXX so it ends up in /usr/lib/python3.X/config/Makefile.
154 # PKG_CONFIG needed for cross.
155 tc-export CXX PKG_CONFIG
159 dbmliborder+="${dbmliborder:+:}gdbm"
162 # Set baseline test skip flags.
167 # Arch-specific skips. See #931888 for a collection of these.
176 -x test_free_threading
178 -x test_numeric_tower
189 -x test_external_inspection
193 powerpc64-*) # big endian
206 -x test_multiprocessing_fork
207 -x test_multiprocessing_forkserver
208 -x test_multiprocessing_spawn
218 # musl-specific skips
219 use elibc_musl && COMMON_TEST_SKIPS+=(
220 # various musl locale deficiencies
222 -x test_c_locale_coercion
226 # known issues with find_library on musl
227 # https://bugs.python.org/issue21622
230 # fpathconf, ttyname errno values
235 local profile_task_flags=(
241 # We use a timeout because of how often we've had hang issues
242 # here. It also matches the default upstream PROFILE_TASK.
245 "${COMMON_TEST_SKIPS[@]}"
249 # All of these seem to occasionally hang for PGO inconsistently
250 # They'll even hang here but be fine in src_test sometimes.
251 # bug #828535 (and related: bug #788022)
253 -x test_concurrent_futures
256 -x test_multiprocessing_fork
260 # Hangs (actually runs indefinitely executing itself w/ many cpython builds)
265 # Arch-specific skips. See #931888 for a collection of these.
268 profile_task_flags+=(
273 profile_task_flags+=(
280 powerpc64-*) # big endian
281 profile_task_flags+=(
287 profile_task_flags+=(
293 if has_version "app-arch/rpm" ; then
294 # Avoid sandbox failure (attempts to write to /var/lib/rpm)
295 profile_task_flags+=(
299 local -x PROFILE_TASK="${profile_task_flags[*]}"
303 # glibc-2.30 removes it; since we can't cleanly force-rebuild
304 # Python on glibc upgrade, remove it proactively to give
305 # a chance for users rebuilding python before glibc
306 ac_cv_header_stropts_h=no
310 --infodir='${prefix}/share/info'
311 --mandir='${prefix}/share/man'
312 --with-computed-gotos
313 --with-dbmliborder="${dbmliborder}"
315 --enable-loadable-sqlite-extensions
320 --with-system-libmpdec
321 --with-wheel-pkg-dir="${EPREFIX}"/usr/lib/python/ensurepip
323 $(use_with debug assertions)
324 $(use_enable pgo optimizations)
328 # disable implicit optimization/debugging flags
331 # https://bugs.gentoo.org/700012
333 append-cflags $(test-flags-CC -ffat-lto-objects)
339 if tc-is-cross-compiler ; then
340 # Hack to workaround get_libdir not being able to handle CBUILD, bug #794181
341 local cbuild_libdir=$(unset PKG_CONFIG_PATH ; $(tc-getBUILD_PKG_CONFIG) --keep-system-libs --libs-only-L libffi)
343 # pass system CFLAGS & LDFLAGS as _NODIST, otherwise they'll get
344 # propagated to sysconfig for built extensions
346 # -fno-lto to avoid bug #700012 (not like it matters for mini-CBUILD Python anyway)
347 local -x CFLAGS_NODIST="${BUILD_CFLAGS} -fno-lto"
348 local -x LDFLAGS_NODIST=${BUILD_LDFLAGS}
349 local -x CFLAGS= LDFLAGS=
350 local -x BUILD_CFLAGS="${CFLAGS_NODIST}"
351 local -x BUILD_LDFLAGS=${LDFLAGS_NODIST}
353 # We need to build our own Python on CBUILD first, and feed it in.
354 # bug #847910 and bug #864911.
355 local myeconfargs_cbuild=(
358 --libdir="${cbuild_libdir:2}"
360 # Avoid needing to load the right libpython.so.
363 # As minimal as possible for the mini CBUILD Python
364 # we build just for cross.
366 --disable-optimizations
369 # Point the imminent CHOST build to the Python we just
371 export PATH="${WORKDIR}/${P}-${CBUILD}:${PATH}"
373 mkdir "${WORKDIR}"/${P}-${CBUILD} || die
374 pushd "${WORKDIR}"/${P}-${CBUILD} &> /dev/null || die
375 # We disable _ctypes and _crypt for CBUILD because Python's setup.py can't handle locating
376 # libdir correctly for cross.
377 PYTHON_DISABLE_MODULES="${PYTHON_DISABLE_MODULES} _ctypes _crypt" \
378 ECONF_SOURCE="${S}" econf_build "${myeconfargs_cbuild[@]}"
380 # Avoid as many dependencies as possible for the cross build.
381 cat >> Makefile <<-EOF || die
384 MODULE__GDBM=disabled
386 MODULE__SQLITE3=disabled
387 MODULE__HASHLIB=disabled
389 MODULE__CURSES=disabled
390 MODULE__CURSES_PANEL=disabled
391 MODULE_READLINE=disabled
392 MODULE__TKINTER=disabled
393 MODULE_PYEXPAT=disabled
397 # Unfortunately, we do have to build this immediately, and
398 # not in src_compile, because CHOST configure for Python
399 # will check the existence of the Python it was pointed to
401 PYTHON_DISABLE_MODULES="${PYTHON_DISABLE_MODULES} _ctypes _crypt" emake
402 popd &> /dev/null || die
405 # pass system CFLAGS & LDFLAGS as _NODIST, otherwise they'll get
406 # propagated to sysconfig for built extensions
407 local -x CFLAGS_NODIST=${CFLAGS}
408 local -x LDFLAGS_NODIST=${LDFLAGS}
409 local -x CFLAGS= LDFLAGS=
411 # Fix implicit declarations on cross and prefix builds. Bug #674070.
413 append-cppflags -I"${ESYSROOT}"/usr/include/ncursesw
417 econf "${myeconfargs[@]}"
419 if grep -q "#define POSIX_SEMAPHORES_NOT_ENABLED 1" pyconfig.h; then
420 eerror "configure has detected that the sem_open function is broken."
421 eerror "Please ensure that /dev/shm is mounted as a tmpfs with mode 1777."
422 die "Broken sem_open function (bug 496328)"
425 # install epython.py as part of stdlib
426 echo "EPYTHON='python${PYVER}'" > Lib/epython.py || die
430 # Ensure sed works as expected
431 # https://bugs.gentoo.org/594768
433 # Prevent using distutils bundled by setuptools.
434 # https://bugs.gentoo.org/823728
435 export SETUPTOOLS_USE_DISTUTILS=stdlib
437 # Save PYTHONDONTWRITEBYTECODE so that 'has_version' doesn't
438 # end up writing bytecode & violating sandbox.
440 local -x _PYTHONDONTWRITEBYTECODE=${PYTHONDONTWRITEBYTECODE}
442 # Gentoo hack to disable accessing system site-packages
443 export GENTOO_CPYTHON_BUILD=1
448 local -x PYTHONDONTWRITEBYTECODE=
452 # also need to clear the flags explicitly here or they end up
454 emake CPPFLAGS= CFLAGS= LDFLAGS=
456 # Restore saved value from above.
457 local -x PYTHONDONTWRITEBYTECODE=${_PYTHONDONTWRITEBYTECODE}
459 # Work around bug 329499. See also bug 413751 and 457194.
460 if has_version dev-libs/libffi[pax-kernel]; then
468 # Tests will not work when cross compiling.
469 if tc-is-cross-compiler; then
470 elog "Disabling tests due to crosscompiling."
477 -j "$(makeopts_jobs)"
478 "${COMMON_TEST_SKIPS[@]}"
483 local -x PYTHONDONTWRITEBYTECODE=
486 nonfatal emake -Onone test EXTRATESTOPTS="${test_opts[*]}" \
487 CPPFLAGS= CFLAGS= LDFLAGS= < /dev/tty
490 [[ ${ret} -eq 0 ]] || die "emake test failed"
494 local libdir=${ED}/usr/lib/python${PYVER}
496 emake DESTDIR="${D}" altinstall
498 # Remove static library
499 rm "${ED}"/usr/$(get_libdir)/libpython*.a || die
501 # Fix collisions between different slots of Python.
502 rm "${ED}/usr/$(get_libdir)/libpython3.so" || die
504 # Cheap hack to get version with ABIFLAGS
505 local abiver=$(cd "${ED}/usr/include"; echo python*)
506 if [[ ${abiver} != python${PYVER} ]]; then
507 # Replace python3.X with a symlink to python3.Xm
508 rm "${ED}/usr/bin/python${PYVER}" || die
509 dosym "${abiver}" "/usr/bin/python${PYVER}"
510 # Create python3.X-config symlink
511 dosym "${abiver}-config" "/usr/bin/python${PYVER}-config"
512 # Create python-3.5m.pc symlink
513 dosym "python-${PYVER}.pc" "/usr/$(get_libdir)/pkgconfig/${abiver/${PYVER}/-${PYVER}}.pc"
516 # python seems to get rebuilt in src_install (bug 569908)
517 # Work around it for now.
518 if has_version dev-libs/libffi[pax-kernel]; then
519 pax-mark E "${ED}/usr/bin/${abiver}"
521 pax-mark m "${ED}/usr/bin/${abiver}"
524 rm -r "${libdir}"/ensurepip/_bundled || die
525 if ! use ensurepip; then
526 rm -r "${libdir}"/ensurepip || die
528 if ! use sqlite; then
529 rm -r "${libdir}/"{sqlite3,test/test_sqlite*} || die
532 rm -r "${ED}/usr/bin/idle${PYVER}" || die
533 rm -r "${libdir}/"{idlelib,tkinter,test/test_tk*} || die
536 ln -s ../python/EXTERNALLY-MANAGED "${libdir}/EXTERNALLY-MANAGED" || die
538 dodoc Misc/{ACKS,HISTORY,NEWS}
540 if use examples; then
542 find Tools -name __pycache__ -exec rm -fr {} + || die
545 insinto /usr/share/gdb/auto-load/usr/$(get_libdir) #443510
547 printf 'e:\n\t@echo $(INSTSONAME)\ninclude Makefile\n' |
548 emake --no-print-directory -s -f - 2>/dev/null
550 newins Tools/gdb/libpython.py "${libname}"-gdb.py
552 newconfd "${FILESDIR}/pydoc.conf" pydoc-${PYVER}
553 newinitd "${FILESDIR}/pydoc.init" pydoc-${PYVER}
555 -e "s:@PYDOC_PORT_VARIABLE@:PYDOC${PYVER/./_}_PORT:" \
556 -e "s:@PYDOC@:pydoc${PYVER}:" \
557 -i "${ED}/etc/conf.d/pydoc-${PYVER}" \
558 "${ED}/etc/init.d/pydoc-${PYVER}" || die "sed failed"
560 # python-exec wrapping support
561 local pymajor=${PYVER%.*}
562 local EPYTHON=python${PYVER}
563 local scriptdir=${D}$(python_get_scriptdir)
564 mkdir -p "${scriptdir}" || die
566 ln -s "../../../bin/${abiver}" "${scriptdir}/python${pymajor}" || die
567 ln -s "python${pymajor}" "${scriptdir}/python" || die
568 # python-config and pythonX-config
569 # note: we need to create a wrapper rather than symlinking it due
570 # to some random dirname(argv[0]) magic performed by python-config
571 cat > "${scriptdir}/python${pymajor}-config" <<-EOF || die
573 exec "${abiver}-config" "\${@}"
575 chmod +x "${scriptdir}/python${pymajor}-config" || die
576 ln -s "python${pymajor}-config" "${scriptdir}/python-config" || die
578 ln -s "../../../bin/2to3-${PYVER}" "${scriptdir}/2to3" || die
579 ln -s "../../../bin/pydoc${PYVER}" "${scriptdir}/pydoc" || die
582 ln -s "../../../bin/idle${PYVER}" "${scriptdir}/idle" || die