chiark / gitweb /
libgpg-error: workaround no stdout atexit with constructor prior to N (#1017)
[termux-packages] / packages / python / build.sh
1 TERMUX_PKG_HOMEPAGE=https://python.org/
2 TERMUX_PKG_DESCRIPTION="Python 3 programming language intended to enable clear programs"
3 TERMUX_PKG_DEPENDS="libandroid-support, ncurses, readline, libffi, openssl, libutil, libbz2, libsqlite, gdbm, ncurses-ui-libs, libcrypt, liblzma"
4 TERMUX_PKG_HOSTBUILD=true
5
6 _MAJOR_VERSION=3.6
7 TERMUX_PKG_VERSION=${_MAJOR_VERSION}.1
8 TERMUX_PKG_SRCURL=https://www.python.org/ftp/python/${TERMUX_PKG_VERSION}/Python-${TERMUX_PKG_VERSION}.tar.xz
9 TERMUX_PKG_SHA256=a01810ddfcec216bcdb357a84bfaafdfaa0ca42bbdaa4cb7ff74f5a9961e4041
10
11 # The flag --with(out)-pymalloc (disable/enable specialized mallocs) is enabled by default and causes m suffix versions of python.
12 # Set ac_cv_func_wcsftime=no to avoid errors such as "character U+ca0025 is not in range [U+0000; U+10ffff]"
13 # when executing e.g. "from time import time, strftime, localtime; print(strftime(str('%Y-%m-%d %H:%M'), localtime()))"
14 TERMUX_PKG_EXTRA_CONFIGURE_ARGS="ac_cv_file__dev_ptmx=yes ac_cv_file__dev_ptc=no ac_cv_func_wcsftime=no"
15 # Avoid trying to include <sys/timeb.h> which does not exist on android-21:
16 TERMUX_PKG_EXTRA_CONFIGURE_ARGS+=" ac_cv_func_ftime=no"
17 # Avoid trying to use AT_EACCESS which is not defined:
18 TERMUX_PKG_EXTRA_CONFIGURE_ARGS+=" ac_cv_func_faccessat=no"
19 # The gethostbyname_r function does not exist on device libc:
20 TERMUX_PKG_EXTRA_CONFIGURE_ARGS+=" ac_cv_func_gethostbyname_r=no"
21 TERMUX_PKG_EXTRA_CONFIGURE_ARGS+=" --build=$TERMUX_BUILD_TUPLE --with-system-ffi --without-ensurepip"
22 # Hard links does not work on Android 6:
23 TERMUX_PKG_EXTRA_CONFIGURE_ARGS+=" ac_cv_func_linkat=no"
24 # Posix semaphores are not supported on Android:
25 TERMUX_PKG_EXTRA_CONFIGURE_ARGS+=" ac_cv_posix_semaphores_enabled=no"
26 # Do not assume getaddrinfo is buggy when cross compiling:
27 TERMUX_PKG_EXTRA_CONFIGURE_ARGS+=" ac_cv_buggy_getaddrinfo=no"
28 TERMUX_PKG_RM_AFTER_INSTALL="
29 bin/python${_MAJOR_VERSION}m bin/idle*
30 lib/python${_MAJOR_VERSION}/idlelib
31 lib/python${_MAJOR_VERSION}/test
32 lib/python${_MAJOR_VERSION}/tkinter
33 lib/python${_MAJOR_VERSION}/turtledemo
34 "
35
36 termux_step_host_build () {
37         # We need native Parser/pgen binary, copied into cross-compile 
38         # build in termux_step_post_configure().
39         $TERMUX_PKG_SRCDIR/configure
40         make
41         # We also need a python$_MAJOR_VERSION binary to be picked up
42         # by configure check.
43         ln -f -s python python$_MAJOR_VERSION
44 }
45
46 termux_step_pre_configure() {
47         # Put the host-built python in path:
48         export TERMUX_ORIG_PATH=$PATH
49         export PATH=$TERMUX_PKG_HOSTBUILD_DIR:$PATH
50
51         # Needed when building with clang, as setup.py only probes
52         # gcc for include paths when finding headers for determining
53         # if extension modules should be built (specifically, the
54         # zlib extension module is not built without this):
55         CPPFLAGS+=" -I$TERMUX_STANDALONE_TOOLCHAIN/sysroot/usr/include"
56         LDFLAGS+=" -L$TERMUX_STANDALONE_TOOLCHAIN/sysroot/usr/lib"
57 }
58
59 termux_step_post_configure () {
60         cp $TERMUX_PKG_HOSTBUILD_DIR/Parser/pgen $TERMUX_PKG_BUILDDIR/Parser/pgen
61         cp $TERMUX_PKG_HOSTBUILD_DIR/Programs/_freeze_importlib $TERMUX_PKG_BUILDDIR/Programs/_freeze_importlib
62         touch -d "next hour" $TERMUX_PKG_BUILDDIR/Parser/pgen
63         touch -d "next hour" $TERMUX_PKG_BUILDDIR/Programs/_freeze_importlib
64 }
65
66 termux_step_post_make_install () {
67         (cd $TERMUX_PREFIX/bin && rm -f python && ln -s python3 python)
68         (cd $TERMUX_PREFIX/share/man/man1 && rm -f python.1 && ln -s python3.1 python.1)
69         # Restore path which termux_step_host_build messed with
70         export PATH=$TERMUX_ORIG_PATH
71
72         # Save away pyconfig.h so that the python-dev subpackage does not take it.
73         # It is required by ensurepip so bundled with the main python package.
74         # Copied back in termux_step_post_massage() after the python-dev package has been built.
75         mv $TERMUX_PREFIX/include/python${_MAJOR_VERSION}m/pyconfig.h $TERMUX_PKG_TMPDIR/pyconfig.h
76 }
77
78 termux_step_post_massage () {
79         # Verify that desired modules have been included:
80         for module in _ssl _bz2 zlib _curses _sqlite3 _lzma; do
81                 if [ ! -f lib/python${_MAJOR_VERSION}/lib-dynload/${module}.*.so ]; then
82                         termux_error_exit "Python module library $module not built"
83                 fi
84         done
85
86         # Restore pyconfig.h saved away in termux_step_post_make_install() above:
87         mkdir -p $TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX/include/python${_MAJOR_VERSION}m/
88         cp $TERMUX_PKG_TMPDIR/pyconfig.h $TERMUX_PREFIX/include/python${_MAJOR_VERSION}m/
89         mv $TERMUX_PKG_TMPDIR/pyconfig.h $TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX/include/python${_MAJOR_VERSION}m/
90
91         cd $TERMUX_PKG_MASSAGEDIR
92         find . -path '*/__pycache__*' -delete
93 }
94
95 termux_step_create_debscripts () {
96         ## POST INSTALL:
97         echo 'echo "Setting up pip..."' > postinst
98         # Fix historical mistake which removed bin/pip but left site-packages/pip-*.dist-info,
99         # which causes ensurepip to avoid installing pip due to already existing pip install:
100         echo "if [ ! -f $TERMUX_PREFIX/bin/pip -a -d $TERMUX_PREFIX/lib/python${_MAJOR_VERSION}/site-packages/pip-*.dist-info ]; then rm -Rf $TERMUX_PREFIX/lib/python${_MAJOR_VERSION}/site-packages/pip-*.dist-info ; fi" >> postinst
101         # Setup bin/pip:
102         echo "$TERMUX_PREFIX/bin/python -m ensurepip --upgrade --default-pip" >> postinst
103
104         ## PRE RM:
105         # Avoid running on update:
106         echo 'if [ $1 != "remove" ]; then exit 0; fi' > prerm
107         # Uninstall everything installed through pip:
108         echo "pip freeze 2> /dev/null | xargs pip uninstall -y > /dev/null 2> /dev/null" >> prerm
109         # Cleanup __pycache__ folders:
110         echo "find $TERMUX_PREFIX/lib/python${_MAJOR_VERSION} -depth -name __pycache__ -exec rm -rf {} \;" >> prerm
111         # Remove contents of site-packages/ folder:
112         echo "rm -Rf $TERMUX_PREFIX/lib/python${_MAJOR_VERSION}/site-packages/*" >> prerm
113         # Remove bin/pip (and bin/pip3* variants) installed by ensurepip in postinst:
114         echo "rm -f $TERMUX_PREFIX/bin/pip $TERMUX_PREFIX/bin/pip3*" >> prerm
115
116         echo "exit 0" >> postinst
117         echo "exit 0" >> prerm
118         chmod 0755 postinst prerm
119 }