1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
7 inherit autotools flag-o-matic pax-utils python-utils-r1 toolchain-funcs
11 PATCHSET="python-gentoo-patches-3.8.1-r2"
13 DESCRIPTION="An interpreted, interactive, object-oriented programming language"
14 HOMEPAGE="https://www.python.org/"
15 SRC_URI="https://www.python.org/ftp/python/${PV}/${MY_P}.tar.xz
16 https://dev.gentoo.org/~mgorny/dist/python/${PATCHSET}.tar.xz"
17 S="${WORKDIR}/${MY_P}"
21 KEYWORDS="~alpha amd64 arm arm64 hppa ~ia64 ~m68k ~mips ~ppc ppc64 ~riscv ~s390 ~sparc x86"
22 IUSE="bluetooth build examples gdbm hardened ipv6 libressl +ncurses +readline sqlite +ssl test tk wininst +xml"
23 RESTRICT="!test? ( test )"
25 # Do not add a dependency on dev-lang/python to this ebuild.
26 # If you need to apply a patch which requires python for bootstrapping, please
27 # run the bootstrap code on your dev box and include the results in the
28 # patchset. See bug 447752.
30 RDEPEND="app-arch/bzip2:=
34 >=sys-libs/zlib-1.1.3:=
37 gdbm? ( sys-libs/gdbm:=[berkdb] )
38 ncurses? ( >=sys-libs/ncurses-5.2:= )
39 readline? ( >=sys-libs/readline-4.1:= )
40 sqlite? ( >=dev-db/sqlite-3.3.8:3= )
42 !libressl? ( dev-libs/openssl:= )
43 libressl? ( dev-libs/libressl:= )
51 xml? ( >=dev-libs/expat-2.1:= )"
52 # bluetooth requires headers from bluez
54 bluetooth? ( net-wireless/bluez )
55 test? ( app-arch/xz-utils[extra-filters(+)] )
57 !sys-devel/gcc[libffi(-)]"
58 RDEPEND+=" !build? ( app-misc/mime-types )"
59 PDEPEND=">=app-eselect/eselect-python-20140125-r1"
62 # Ensure that internal copies of expat, libffi and zlib are not used.
63 rm -fr Modules/expat || die
64 rm -fr Modules/_ctypes/libffi* || die
65 rm -fr Modules/zlib || die
68 "${WORKDIR}/${PATCHSET}"
69 "${FILESDIR}/test.support.unlink-ignore-PermissionError.patch"
70 # add module importing numpy to blacklist
71 "${FILESDIR}/test-__all__-numpy.patch"
76 sed -i -e "s:@@GENTOO_LIBDIR@@:$(get_libdir):g" \
77 setup.py || die "sed failed to replace @@GENTOO_LIBDIR@@"
84 # disable automagic bluetooth headers detection
85 use bluetooth || export ac_cv_header_bluetooth_bluetooth_h=no
86 use gdbm || disable+=" gdbm"
87 use ncurses || disable+=" _curses _curses_panel"
88 use readline || disable+=" readline"
89 use sqlite || disable+=" _sqlite3"
90 use ssl || export PYTHON_DISABLE_SSL="1"
91 use tk || disable+=" _tkinter"
92 use xml || disable+=" _elementtree pyexpat" # _elementtree uses pyexpat.
93 export PYTHON_DISABLE_MODULES="${disable}"
96 ewarn "You have configured Python without XML support."
97 ewarn "This is NOT a recommended configuration as you"
98 ewarn "may face problems parsing any XML documents."
101 if [[ -n "${PYTHON_DISABLE_MODULES}" ]]; then
102 einfo "Disabled modules: ${PYTHON_DISABLE_MODULES}"
105 if [[ "$(gcc-major-version)" -ge 4 ]]; then
109 filter-flags -malign-double
111 # https://bugs.gentoo.org/show_bug.cgi?id=50309
112 if is-flagq -O3; then
113 is-flagq -fstack-protector-all && replace-flags -O3 -O2
114 use hardened && replace-flags -O3 -O2
117 # https://bugs.gentoo.org/700012
118 if is-flagq -flto || is-flagq '-flto=*'; then
119 append-cflags $(test-flags-CC -ffat-lto-objects)
122 # Export CXX so it ends up in /usr/lib/python3.X/config/Makefile.
125 # Set LDFLAGS so we link modules with -lpython3.2 correctly.
126 # Needed on FreeBSD unless Python 3.2 is already installed.
127 # Please query BSD team before removing this!
130 # Fix implicit declarations on cross and prefix builds. Bug #674070.
131 use ncurses && append-cppflags -I"${ESYSROOT}"/usr/include/ncursesw
135 dbmliborder+="${dbmliborder:+:}gdbm"
139 # glibc-2.30 removes it; since we can't cleanly force-rebuild
140 # Python on glibc upgrade, remove it proactively to give
141 # a chance for users rebuilding python before glibc
142 ac_cv_header_stropts_h=no
146 --infodir='${prefix}/share/info'
147 --mandir='${prefix}/share/man'
148 --with-computed-gotos
149 --with-dbmliborder="${dbmliborder}"
151 --enable-loadable-sqlite-extensions
157 OPT="" econf "${myeconfargs[@]}"
161 # Ensure sed works as expected
162 # https://bugs.gentoo.org/594768
165 emake CPPFLAGS= CFLAGS= LDFLAGS=
167 # Work around bug 329499. See also bug 413751 and 457194.
168 if has_version dev-libs/libffi[pax_kernel]; then
176 # Tests will not work when cross compiling.
177 if tc-is-cross-compiler; then
178 elog "Disabling tests due to crosscompiling."
182 # Skip failing tests.
183 local skipped_tests="gdb"
185 for test in ${skipped_tests}; do
186 mv "${S}"/Lib/test/test_${test}.py "${T}"
192 local -x PYTHONDONTWRITEBYTECODE=
194 emake test EXTRATESTOPTS="-u-network" CPPFLAGS= CFLAGS= LDFLAGS= < /dev/tty
197 for test in ${skipped_tests}; do
198 mv "${T}/test_${test}.py" "${S}"/Lib/test
201 elog "The following tests have been skipped:"
202 for test in ${skipped_tests}; do
203 elog "test_${test}.py"
206 elog "If you would like to run them, you may:"
207 elog "cd '${EPREFIX}/usr/lib/python${PYVER}/test'"
208 elog "and run the tests separately."
210 if [[ ${result} -ne 0 ]]; then
211 die "emake test failed"
216 local libdir=${ED}/usr/lib/python${PYVER}
218 emake DESTDIR="${D}" altinstall
220 # Remove static library
221 rm "${ED}"/usr/$(get_libdir)/libpython*.a || die
224 -e "s/\(CONFIGURE_LDFLAGS=\).*/\1/" \
225 -e "s/\(PY_LDFLAGS=\).*/\1/" \
226 -i "${libdir}/config-${PYVER}"*/Makefile || die "sed failed"
228 # Fix collisions between different slots of Python.
229 rm "${ED}/usr/$(get_libdir)/libpython3.so" || die
231 # Cheap hack to get version with ABIFLAGS
232 local abiver=$(cd "${ED}/usr/include"; echo python*)
233 if [[ ${abiver} != python${PYVER} ]]; then
234 # Replace python3.X with a symlink to python3.Xm
235 rm "${ED}/usr/bin/python${PYVER}" || die
236 dosym "${abiver}" "/usr/bin/python${PYVER}"
237 # Create python3.X-config symlink
238 dosym "${abiver}-config" "/usr/bin/python${PYVER}-config"
239 # Create python-3.5m.pc symlink
240 dosym "python-${PYVER}.pc" "/usr/$(get_libdir)/pkgconfig/${abiver/${PYVER}/-${PYVER}}.pc"
243 # python seems to get rebuilt in src_install (bug 569908)
244 # Work around it for now.
245 if has_version dev-libs/libffi[pax_kernel]; then
246 pax-mark E "${ED}/usr/bin/${abiver}"
248 pax-mark m "${ED}/usr/bin/${abiver}"
251 use sqlite || rm -r "${libdir}/"{sqlite3,test/test_sqlite*} || die
252 use tk || rm -r "${ED}/usr/bin/idle${PYVER}" "${libdir}/"{idlelib,tkinter,test/test_tk*} || die
254 use wininst || rm "${libdir}/distutils/command/"wininst-*.exe || die
256 dodoc Misc/{ACKS,HISTORY,NEWS}
258 if use examples; then
260 find Tools -name __pycache__ -exec rm -fr {} + || die
263 insinto /usr/share/gdb/auto-load/usr/$(get_libdir) #443510
264 local libname=$(printf 'e:\n\t@echo $(INSTSONAME)\ninclude Makefile\n' | \
265 emake --no-print-directory -s -f - 2>/dev/null)
266 newins "${S}"/Tools/gdb/libpython.py "${libname}"-gdb.py
268 newconfd "${FILESDIR}/pydoc.conf" pydoc-${PYVER}
269 newinitd "${FILESDIR}/pydoc.init" pydoc-${PYVER}
271 -e "s:@PYDOC_PORT_VARIABLE@:PYDOC${PYVER/./_}_PORT:" \
272 -e "s:@PYDOC@:pydoc${PYVER}:" \
273 -i "${ED}/etc/conf.d/pydoc-${PYVER}" \
274 "${ED}/etc/init.d/pydoc-${PYVER}" || die "sed failed"
277 local vars=( EPYTHON PYTHON_SITEDIR PYTHON_SCRIPTDIR )
279 # if not using a cross-compiler, use the fresh binary
280 if ! tc-is-cross-compiler; then
281 local -x PYTHON=./python
282 local -x LD_LIBRARY_PATH=${LD_LIBRARY_PATH+${LD_LIBRARY_PATH}:}${PWD}
284 vars=( PYTHON "${vars[@]}" )
287 python_export "python${PYVER}" "${vars[@]}"
288 echo "EPYTHON='${EPYTHON}'" > epython.py || die
289 python_domodule epython.py
291 # python-exec wrapping support
292 local pymajor=${PYVER%.*}
293 mkdir -p "${D}${PYTHON_SCRIPTDIR}" || die
295 ln -s "../../../bin/${abiver}" \
296 "${D}${PYTHON_SCRIPTDIR}/python${pymajor}" || die
297 ln -s "python${pymajor}" "${D}${PYTHON_SCRIPTDIR}/python" || die
298 # python-config and pythonX-config
299 # note: we need to create a wrapper rather than symlinking it due
300 # to some random dirname(argv[0]) magic performed by python-config
301 cat > "${D}${PYTHON_SCRIPTDIR}/python${pymajor}-config" <<-EOF || die
303 exec "${abiver}-config" "\${@}"
305 chmod +x "${D}${PYTHON_SCRIPTDIR}/python${pymajor}-config" || die
306 ln -s "python${pymajor}-config" \
307 "${D}${PYTHON_SCRIPTDIR}/python-config" || die
309 ln -s "../../../bin/2to3-${PYVER}" \
310 "${D}${PYTHON_SCRIPTDIR}/2to3" || die
311 ln -s "../../../bin/pydoc${PYVER}" \
312 "${D}${PYTHON_SCRIPTDIR}/pydoc" || die
315 ln -s "../../../bin/idle${PYVER}" \
316 "${D}${PYTHON_SCRIPTDIR}/idle" || die
321 if has_version "<${CATEGORY}/${PN}-${PYVER}" && ! has_version ">=${CATEGORY}/${PN}-${PYVER}_alpha"; then
322 python_updater_warning="1"
326 eselect_python_update() {
327 if [[ -z "$(eselect python show)" || \
328 ! -f "${EROOT}/usr/bin/$(eselect python show)" ]]; then
329 eselect python update
332 if [[ -z "$(eselect python show --python${PV%%.*})" || \
333 ! -f "${EROOT}/usr/bin/$(eselect python show --python${PV%%.*})" ]]
335 eselect python update --python${PV%%.*}
340 eselect_python_update
342 if [[ "${python_updater_warning}" == "1" ]]; then
343 ewarn "You have just upgraded from an older version of Python."
345 ewarn "Please adjust PYTHON_TARGETS (if so desired), and run emerge with the --newuse or --changed-use option to rebuild packages installing python modules."
350 eselect_python_update