blob: 013f32bdca0c4a48b842a9fc55ea71c80560e2eb [file] [log] [blame]
Mike Frysingerdc5029f2021-02-19 15:53:47 -05001# Copyright 1999-2021 Gentoo Authors
Chad Versace9c71f3c2018-10-24 16:49:31 -07002# Distributed under the terms of the GNU General Public License v2
3
Mike Frysinger0e245162021-02-19 15:39:45 -05004EAPI="7"
Chad Versace9c71f3c2018-10-24 16:49:31 -07005WANT_LIBTOOL="none"
6
Mike Frysingerdc5029f2021-02-19 15:53:47 -05007inherit autotools flag-o-matic multiprocessing pax-utils \
8 python-utils-r1 toolchain-funcs
Chad Versace9c71f3c2018-10-24 16:49:31 -07009
10MY_P="Python-${PV}"
Mike Frysinger0e245162021-02-19 15:39:45 -050011PYVER=$(ver_cut 1-2)
Mike Frysingerdc5029f2021-02-19 15:53:47 -050012PATCHSET="python-gentoo-patches-${PV}-r1"
Chad Versace9c71f3c2018-10-24 16:49:31 -070013
14DESCRIPTION="An interpreted, interactive, object-oriented programming language"
15HOMEPAGE="https://www.python.org/"
16SRC_URI="https://www.python.org/ftp/python/${PV}/${MY_P}.tar.xz
Mike Frysinger0e245162021-02-19 15:39:45 -050017 https://dev.gentoo.org/~mgorny/dist/python/${PATCHSET}.tar.xz"
18S="${WORKDIR}/${MY_P}"
Chad Versace9c71f3c2018-10-24 16:49:31 -070019
20LICENSE="PSF-2"
Mike Frysinger0e245162021-02-19 15:39:45 -050021SLOT="${PYVER}/${PYVER}m"
Daniel Campello7ded4e32019-02-06 11:03:46 -070022KEYWORDS="*"
Mike Frysinger432f2892021-02-19 15:22:02 -050023IUSE="bluetooth build examples gdbm hardened ipv6 libressl +ncurses +readline sqlite +ssl test +threads tk wininst +xml"
Chad Versace9c71f3c2018-10-24 16:49:31 -070024RESTRICT="!test? ( test )"
25
26# Do not add a dependency on dev-lang/python to this ebuild.
27# If you need to apply a patch which requires python for bootstrapping, please
28# run the bootstrap code on your dev box and include the results in the
29# patchset. See bug 447752.
30
Mike Frysinger0e245162021-02-19 15:39:45 -050031RDEPEND="app-arch/bzip2:=
32 app-arch/xz-utils:=
Mike Frysinger432f2892021-02-19 15:22:02 -050033 dev-libs/libffi:=
Mike Frysinger0e245162021-02-19 15:39:45 -050034 >=sys-libs/zlib-1.1.3:=
35 virtual/libcrypt:=
Chad Versace9c71f3c2018-10-24 16:49:31 -070036 virtual/libintl
Mike Frysinger0e245162021-02-19 15:39:45 -050037 gdbm? ( sys-libs/gdbm:=[berkdb] )
38 ncurses? ( >=sys-libs/ncurses-5.2:= )
39 readline? ( >=sys-libs/readline-4.1:= )
Chad Versace9c71f3c2018-10-24 16:49:31 -070040 sqlite? ( >=dev-db/sqlite-3.3.8:3= )
41 ssl? (
Mike Frysinger0e245162021-02-19 15:39:45 -050042 !libressl? ( dev-libs/openssl:= )
43 libressl? ( dev-libs/libressl:= )
Chad Versace9c71f3c2018-10-24 16:49:31 -070044 )
45 tk? (
Mike Frysinger0e245162021-02-19 15:39:45 -050046 >=dev-lang/tcl-8.0:=
47 >=dev-lang/tk-8.0:=
48 dev-tcltk/blt:=
Chad Versace9c71f3c2018-10-24 16:49:31 -070049 dev-tcltk/tix
50 )
Mike Frysinger0e245162021-02-19 15:39:45 -050051 xml? ( >=dev-libs/expat-2.1:= )"
Mike Frysinger432f2892021-02-19 15:22:02 -050052# bluetooth requires headers from bluez
Chad Versace9c71f3c2018-10-24 16:49:31 -070053DEPEND="${RDEPEND}
Mike Frysinger432f2892021-02-19 15:22:02 -050054 bluetooth? ( net-wireless/bluez )
Chad Versace9c71f3c2018-10-24 16:49:31 -070055 test? ( app-arch/xz-utils[extra-filters(+)] )
56 virtual/pkgconfig
57 !sys-devel/gcc[libffi(-)]"
58RDEPEND+=" !build? ( app-misc/mime-types )"
59PDEPEND=">=app-eselect/eselect-python-20140125-r1"
60
Chad Versace9c71f3c2018-10-24 16:49:31 -070061src_prepare() {
62 # Ensure that internal copies of expat, libffi and zlib are not used.
Mike Frysinger0e245162021-02-19 15:39:45 -050063 rm -fr Modules/expat || die
64 rm -fr Modules/_ctypes/libffi* || die
65 rm -fr Modules/zlib || die
Chad Versace9c71f3c2018-10-24 16:49:31 -070066
Mike Frysinger95c720e2021-02-19 15:09:00 -050067 local PATCHES=(
Mike Frysinger0e245162021-02-19 15:39:45 -050068 "${WORKDIR}/${PATCHSET}"
Mike Frysinger95c720e2021-02-19 15:09:00 -050069 )
Chad Versace9c71f3c2018-10-24 16:49:31 -070070
Mike Frysinger95c720e2021-02-19 15:09:00 -050071 default
Chad Versace9c71f3c2018-10-24 16:49:31 -070072
73 # START: Chromium OS
Mike Frysingerce0b8c02019-07-11 00:40:18 -040074 if tc-is-cross-compiler ; then
Mike Frysinger0e245162021-02-19 15:39:45 -050075 eapply "${FILESDIR}/python-3.6.5-cross-h2py.patch"
76 eapply "${FILESDIR}/python-3.6.5-cross-hack-compiler.patch"
Mike Frysingerce0b8c02019-07-11 00:40:18 -040077 fi
Mike Frysinger0e245162021-02-19 15:39:45 -050078 eapply "${FILESDIR}/python-3.6.5-cross-python-config.patch"
79 eapply "${FILESDIR}/python-3.6.5-cross-setup-sysroot.patch"
80 eapply "${FILESDIR}/python-3.6.5-cross-distutils.patch"
81 eapply "${FILESDIR}/python-3.6.5-cross-sysconfig.patch"
82 eapply "${FILESDIR}/python-3.6.5-ldshared.patch"
83 eapply "${FILESDIR}/python-3.6.5-system-libffi.patch"
84 eapply "${FILESDIR}/python-3.6.5-sigint-handler.patch"
Chad Versace9c71f3c2018-10-24 16:49:31 -070085
86 # Undo the @libdir@ change for portage's pym folder as it is always
87 # installed into /usr/lib/ and not the abi libdir.
88 sed -i \
89 -e '/portage.*pym/s:@@GENTOO_LIBDIR@@:lib:g' \
90 Lib/site.py || die
91
92 sed -i -e "s:sys.exec_prefix]:sys.exec_prefix, '/usr/local']:g" \
93 Lib/site.py || die "sed failed to add /usr/local to prefixes"
94 # END: Chromium OS
95
96 sed -i -e "s:@@GENTOO_LIBDIR@@:$(get_libdir):g" \
97 Lib/distutils/command/install.py \
98 Lib/distutils/sysconfig.py \
99 Lib/site.py \
100 Lib/sysconfig.py \
101 Lib/test/test_site.py \
102 Makefile.pre.in \
103 Modules/Setup.dist \
104 Modules/getpath.c \
105 configure.ac \
106 setup.py || die "sed failed to replace @@GENTOO_LIBDIR@@"
107
Mike Frysingerdc5029f2021-02-19 15:53:47 -0500108 # force correct number of jobs
109 # https://bugs.gentoo.org/737660
110 local jobs=$(makeopts_jobs "${MAKEOPTS}" "$(get_nproc)")
111 sed -i -e "/self\.parallel/s:True:${jobs}:" setup.py || die
112
Chad Versace9c71f3c2018-10-24 16:49:31 -0700113 eautoreconf
114}
115
116src_configure() {
117 local disable
Mike Frysinger432f2892021-02-19 15:22:02 -0500118 # disable automagic bluetooth headers detection
119 use bluetooth || export ac_cv_header_bluetooth_bluetooth_h=no
Mike Frysinger0e245162021-02-19 15:39:45 -0500120 use gdbm || disable+=" gdbm"
121 use ncurses || disable+=" _curses _curses_panel"
122 use readline || disable+=" readline"
123 use sqlite || disable+=" _sqlite3"
124 use ssl || export PYTHON_DISABLE_SSL="1"
125 use tk || disable+=" _tkinter"
126 use xml || disable+=" _elementtree pyexpat" # _elementtree uses pyexpat.
Chad Versace9c71f3c2018-10-24 16:49:31 -0700127 export PYTHON_DISABLE_MODULES="${disable}"
128
129 if ! use xml; then
130 ewarn "You have configured Python without XML support."
131 ewarn "This is NOT a recommended configuration as you"
132 ewarn "may face problems parsing any XML documents."
133 fi
134
135 if [[ -n "${PYTHON_DISABLE_MODULES}" ]]; then
136 einfo "Disabled modules: ${PYTHON_DISABLE_MODULES}"
137 fi
138
139 if [[ "$(gcc-major-version)" -ge 4 ]]; then
140 append-flags -fwrapv
141 fi
142
143 filter-flags -malign-double
144
145 # https://bugs.gentoo.org/show_bug.cgi?id=50309
146 if is-flagq -O3; then
147 is-flagq -fstack-protector-all && replace-flags -O3 -O2
148 use hardened && replace-flags -O3 -O2
149 fi
150
151 # Export CXX so it ends up in /usr/lib/python3.X/config/Makefile.
152 tc-export CXX
153
154 # Set LDFLAGS so we link modules with -lpython3.2 correctly.
155 # Needed on FreeBSD unless Python 3.2 is already installed.
156 # Please query BSD team before removing this!
157 append-ldflags "-L."
158
Mike Frysingerce0b8c02019-07-11 00:40:18 -0400159 tc-export CC
160
Chad Versace9c71f3c2018-10-24 16:49:31 -0700161 local dbmliborder
162 if use gdbm; then
163 dbmliborder+="${dbmliborder:+:}gdbm"
164 fi
165
166 local myeconfargs=(
Mike Frysinger0e245162021-02-19 15:39:45 -0500167 # glibc-2.30 removes it; since we can't cleanly force-rebuild
168 # Python on glibc upgrade, remove it proactively to give
169 # a chance for users rebuilding python before glibc
170 ac_cv_header_stropts_h=no
171
Chad Versace9c71f3c2018-10-24 16:49:31 -0700172 --with-fpectl
173 --enable-shared
174 $(use_enable ipv6)
175 $(use_with threads)
176 --infodir='${prefix}/share/info'
177 --mandir='${prefix}/share/man'
178 --with-computed-gotos
179 --with-dbmliborder="${dbmliborder}"
180 --with-libc=
181 --enable-loadable-sqlite-extensions
182 --without-ensurepip
183 --with-system-expat
184 --with-system-ffi
185 )
186
187 OPT="" econf "${myeconfargs[@]}"
188
189 if use threads && grep -q "#define POSIX_SEMAPHORES_NOT_ENABLED 1" pyconfig.h; then
190 eerror "configure has detected that the sem_open function is broken."
191 eerror "Please ensure that /dev/shm is mounted as a tmpfs with mode 1777."
192 die "Broken sem_open function (bug 496328)"
193 fi
194}
195
196src_compile() {
197 # Ensure sed works as expected
198 # https://bugs.gentoo.org/594768
199 local -x LC_ALL=C
200
201 emake CPPFLAGS= CFLAGS= LDFLAGS=
202
203 # Work around bug 329499. See also bug 413751 and 457194.
204 if has_version dev-libs/libffi[pax_kernel]; then
205 pax-mark E python
206 else
207 pax-mark m python
208 fi
209}
210
211src_test() {
212 # Tests will not work when cross compiling.
213 if tc-is-cross-compiler; then
214 elog "Disabling tests due to crosscompiling."
215 return
216 fi
217
218 # Skip failing tests.
Mike Frysingerdc5029f2021-02-19 15:53:47 -0500219 local skipped_tests="gdb faulthandler"
Chad Versace9c71f3c2018-10-24 16:49:31 -0700220
221 for test in ${skipped_tests}; do
222 mv "${S}"/Lib/test/test_${test}.py "${T}"
223 done
224
Mike Frysinger432f2892021-02-19 15:22:02 -0500225 # bug 660358
226 local -x COLUMNS=80
Chad Versace9c71f3c2018-10-24 16:49:31 -0700227 local -x PYTHONDONTWRITEBYTECODE=
228
Mike Frysingerdc5029f2021-02-19 15:53:47 -0500229 local jobs=$(makeopts_jobs "${MAKEOPTS}" "$(get_nproc)")
230
231 emake test EXTRATESTOPTS="-u-network -j${jobs}" \
232 CPPFLAGS= CFLAGS= LDFLAGS= < /dev/tty
Chad Versace9c71f3c2018-10-24 16:49:31 -0700233 local result=$?
234
235 for test in ${skipped_tests}; do
236 mv "${T}/test_${test}.py" "${S}"/Lib/test
237 done
238
239 elog "The following tests have been skipped:"
240 for test in ${skipped_tests}; do
241 elog "test_${test}.py"
242 done
243
244 elog "If you would like to run them, you may:"
245 elog "cd '${EPREFIX}/usr/$(get_libdir)/python${PYVER}/test'"
246 elog "and run the tests separately."
247
248 if [[ ${result} -ne 0 ]]; then
249 die "emake test failed"
250 fi
251}
252
253src_install() {
254 local libdir=${ED}/usr/$(get_libdir)/python${PYVER}
255
256 emake DESTDIR="${D}" altinstall
257
Mike Frysinger0e245162021-02-19 15:39:45 -0500258 # Remove static library
259 rm "${ED}"/usr/$(get_libdir)/libpython*.a || die
260
Chad Versace9c71f3c2018-10-24 16:49:31 -0700261 sed \
262 -e "s/\(CONFIGURE_LDFLAGS=\).*/\1/" \
263 -e "s/\(PY_LDFLAGS=\).*/\1/" \
264 -i "${libdir}/config-${PYVER}"*/Makefile || die "sed failed"
265
266 # Fix collisions between different slots of Python.
Mike Frysinger0e245162021-02-19 15:39:45 -0500267 rm "${ED}/usr/$(get_libdir)/libpython3.so" || die
Chad Versace9c71f3c2018-10-24 16:49:31 -0700268
269 # Cheap hack to get version with ABIFLAGS
Mike Frysinger0e245162021-02-19 15:39:45 -0500270 local abiver=$(cd "${ED}/usr/include"; echo python*)
Chad Versace9c71f3c2018-10-24 16:49:31 -0700271 if [[ ${abiver} != python${PYVER} ]]; then
272 # Replace python3.X with a symlink to python3.Xm
Mike Frysinger0e245162021-02-19 15:39:45 -0500273 rm "${ED}/usr/bin/python${PYVER}" || die
Chad Versace9c71f3c2018-10-24 16:49:31 -0700274 dosym "${abiver}" "/usr/bin/python${PYVER}"
275 # Create python3.X-config symlink
276 dosym "${abiver}-config" "/usr/bin/python${PYVER}-config"
277 # Create python-3.5m.pc symlink
278 dosym "python-${PYVER}.pc" "/usr/$(get_libdir)/pkgconfig/${abiver/${PYVER}/-${PYVER}}.pc"
279 fi
280
281 # python seems to get rebuilt in src_install (bug 569908)
282 # Work around it for now.
283 if has_version dev-libs/libffi[pax_kernel]; then
Mike Frysinger0e245162021-02-19 15:39:45 -0500284 pax-mark E "${ED}/usr/bin/${abiver}"
Chad Versace9c71f3c2018-10-24 16:49:31 -0700285 else
Mike Frysinger0e245162021-02-19 15:39:45 -0500286 pax-mark m "${ED}/usr/bin/${abiver}"
Chad Versace9c71f3c2018-10-24 16:49:31 -0700287 fi
288
289 use sqlite || rm -r "${libdir}/"{sqlite3,test/test_sqlite*} || die
Mike Frysinger0e245162021-02-19 15:39:45 -0500290 use tk || rm -r "${ED}/usr/bin/idle${PYVER}" "${libdir}/"{idlelib,tkinter,test/test_tk*} || die
Chad Versace9c71f3c2018-10-24 16:49:31 -0700291
292 use threads || rm -r "${libdir}/multiprocessing" || die
293 use wininst || rm "${libdir}/distutils/command/"wininst-*.exe || die
294
Mike Frysinger0e245162021-02-19 15:39:45 -0500295 dodoc Misc/{ACKS,HISTORY,NEWS}
Chad Versace9c71f3c2018-10-24 16:49:31 -0700296
297 if use examples; then
Mike Frysinger0e245162021-02-19 15:39:45 -0500298 docinto examples
299 find Tools -name __pycache__ -exec rm -fr {} + || die
300 dodoc -r Tools
Chad Versace9c71f3c2018-10-24 16:49:31 -0700301 fi
302 insinto /usr/share/gdb/auto-load/usr/$(get_libdir) #443510
303 local libname=$(printf 'e:\n\t@echo $(INSTSONAME)\ninclude Makefile\n' | \
304 emake --no-print-directory -s -f - 2>/dev/null)
305 newins "${S}"/Tools/gdb/libpython.py "${libname}"-gdb.py
306
307 newconfd "${FILESDIR}/pydoc.conf" pydoc-${PYVER}
308 newinitd "${FILESDIR}/pydoc.init" pydoc-${PYVER}
309 sed \
310 -e "s:@PYDOC_PORT_VARIABLE@:PYDOC${PYVER/./_}_PORT:" \
311 -e "s:@PYDOC@:pydoc${PYVER}:" \
Mike Frysinger0e245162021-02-19 15:39:45 -0500312 -i "${ED}/etc/conf.d/pydoc-${PYVER}" \
313 "${ED}/etc/init.d/pydoc-${PYVER}" || die "sed failed"
Chad Versace9c71f3c2018-10-24 16:49:31 -0700314
Mike Frysingerdc5029f2021-02-19 15:53:47 -0500315 local -x EPYTHON=python${PYVER}
Chad Versace9c71f3c2018-10-24 16:49:31 -0700316 # if not using a cross-compiler, use the fresh binary
317 if ! tc-is-cross-compiler; then
318 local -x PYTHON=./python
319 local -x LD_LIBRARY_PATH=${LD_LIBRARY_PATH+${LD_LIBRARY_PATH}:}${PWD}
320 else
Mike Frysingerdc5029f2021-02-19 15:53:47 -0500321 local -x PYTHON=${EPREFIX}/usr/bin/${EPYTHON}
Chad Versace9c71f3c2018-10-24 16:49:31 -0700322 fi
323
Chad Versace9c71f3c2018-10-24 16:49:31 -0700324 echo "EPYTHON='${EPYTHON}'" > epython.py || die
325 python_domodule epython.py
326
327 # python-exec wrapping support
328 local pymajor=${PYVER%.*}
Mike Frysingerdc5029f2021-02-19 15:53:47 -0500329 local scriptdir=${D}$(python_get_scriptdir)
330 mkdir -p "${scriptdir}" || die
Chad Versace9c71f3c2018-10-24 16:49:31 -0700331 # python and pythonX
332 ln -s "../../../bin/${abiver}" \
Mike Frysingerdc5029f2021-02-19 15:53:47 -0500333 "${scriptdir}/python${pymajor}" || die
334 ln -s "python${pymajor}" "${scriptdir}/python" || die
Chad Versace9c71f3c2018-10-24 16:49:31 -0700335 # python-config and pythonX-config
336 # note: we need to create a wrapper rather than symlinking it due
337 # to some random dirname(argv[0]) magic performed by python-config
Mike Frysingerdc5029f2021-02-19 15:53:47 -0500338 cat > "${scriptdir}/python${pymajor}-config" <<-EOF || die
Chad Versace9c71f3c2018-10-24 16:49:31 -0700339 #!/bin/sh
340 exec "${abiver}-config" "\${@}"
341 EOF
Mike Frysingerdc5029f2021-02-19 15:53:47 -0500342 chmod +x "${scriptdir}/python${pymajor}-config" || die
Chad Versace9c71f3c2018-10-24 16:49:31 -0700343 ln -s "python${pymajor}-config" \
Mike Frysingerdc5029f2021-02-19 15:53:47 -0500344 "${scriptdir}/python-config" || die
Chad Versace9c71f3c2018-10-24 16:49:31 -0700345 # 2to3, pydoc, pyvenv
346 ln -s "../../../bin/2to3-${PYVER}" \
Mike Frysingerdc5029f2021-02-19 15:53:47 -0500347 "${scriptdir}/2to3" || die
Chad Versace9c71f3c2018-10-24 16:49:31 -0700348 ln -s "../../../bin/pydoc${PYVER}" \
Mike Frysingerdc5029f2021-02-19 15:53:47 -0500349 "${scriptdir}/pydoc" || die
Chad Versace9c71f3c2018-10-24 16:49:31 -0700350 ln -s "../../../bin/pyvenv-${PYVER}" \
Mike Frysingerdc5029f2021-02-19 15:53:47 -0500351 "${scriptdir}/pyvenv" || die
Chad Versace9c71f3c2018-10-24 16:49:31 -0700352 # idle
353 if use tk; then
354 ln -s "../../../bin/idle${PYVER}" \
Mike Frysingerdc5029f2021-02-19 15:53:47 -0500355 "${scriptdir}/idle" || die
Chad Versace9c71f3c2018-10-24 16:49:31 -0700356 fi
Mike Frysinger12f1f692021-02-13 18:32:43 -0500357
358 # Delete unittests as they are a waste of space and are unused.
359 rm -rf "${libdir}/test" "${libdir}"/{ctypes,email,sqlite3,unittest}/test || die
Chad Versace9c71f3c2018-10-24 16:49:31 -0700360}
361
362pkg_preinst() {
363 if has_version "<${CATEGORY}/${PN}-${PYVER}" && ! has_version ">=${CATEGORY}/${PN}-${PYVER}_alpha"; then
364 python_updater_warning="1"
365 fi
366}
367
368eselect_python_update() {
Mike Frysinger0e245162021-02-19 15:39:45 -0500369 if [[ -z "$(eselect python show)" || \
370 ! -f "${EROOT}/usr/bin/$(eselect python show)" ]]; then
Chad Versace9c71f3c2018-10-24 16:49:31 -0700371 eselect python update
372 fi
373
Mike Frysinger0e245162021-02-19 15:39:45 -0500374 if [[ -z "$(eselect python show --python${PV%%.*})" || \
375 ! -f "${EROOT}/usr/bin/$(eselect python show --python${PV%%.*})" ]]
376 then
Chad Versace9c71f3c2018-10-24 16:49:31 -0700377 eselect python update --python${PV%%.*}
378 fi
379}
380
381pkg_postinst() {
382 eselect_python_update
383
384 if [[ "${python_updater_warning}" == "1" ]]; then
385 ewarn "You have just upgraded from an older version of Python."
386 ewarn
387 ewarn "Please adjust PYTHON_TARGETS (if so desired), and run emerge with the --newuse or --changed-use option to rebuild packages installing python modules."
388 fi
389}
390
391pkg_postrm() {
392 eselect_python_update
393}