#!/bin/bash
# Initially based on a snippet from the greenlet project.
# This needs to be run from the root of the project.
# To update: docker pull quay.io/pypa/manylinux2010_x86_64
set -e
export PYTHONUNBUFFERED=1
export PYTHONDONTWRITEBYTECODE=1
# Use a fixed hash seed for reproducability
export PYTHONHASHSEED=8675309
# Disable tests that use external network resources;
# too often we get failures to resolve DNS names or failures
# to connect on AppVeyor.
export GEVENTTEST_USE_RESOURCES="-network"
export CI=1
export TRAVIS=true
export GEVENT_MANYLINUX=1
# Don't get warnings about Python 2 support being deprecated. We
# know. The env var works for pip 20.
export PIP_NO_PYTHON_VERSION_WARNING=1
export PIP_NO_WARN_SCRIPT_LOCATION=1

# Build configuration.
export CC="ccache gcc"
export LDSHARED="$CC -shared"
export LDCCSHARED="$LDSHARED"
export LDCXXSHARED="$LDSHARED"
export CCACHE_NOCPP2=true
export CCACHE_SLOPPINESS=file_macro,time_macros,include_file_ctime,include_file_mtime
export CCACHE_NOHASHDIR=true
export CCACHE_BASEDIR="/gevent"
export BUILD_LIBS=$HOME/.libs
# Share the ccache directory
export CCACHE_DIR="/ccache"
# Disable some warnings produced by libev especially and also some Cython generated code.
# Note that changing the value of these variables invalidates configure caches
export CFLAGS="-Ofast -pipe -Wno-strict-aliasing -Wno-comment -Wno-unused-value -Wno-unused-but-set-variable -Wno-sign-compare -Wno-parentheses -Wno-unused-function -Wno-tautological-compare -Wno-strict-prototypes"


if [ -d /gevent -a -d /opt/python ]; then
    # Running inside docker

    # Set a cache directory for pip. This was
    # mounted to be the same as it is outside docker so it
    # can be persisted.
    # XXX: This works for macOS, where everything bind-mounted
    # is seen as owned by root in the container. But when the host is Linux
    # the actual UIDs come through to the container, triggering
    # pip to disable the cache when it detects that the owner doesn't match.
    export XDG_CACHE_HOME="/cache"
    ls -ld /cache
    ls -ld /cache/pip
    yum -y install libffi-devel ccache
    # On Fedora Rawhide (F33)
    # yum install python39 python3-devel gcc kernel-devel kernel-headers make diffutils file

    mkdir /tmp/build
    cd /tmp/build
    git clone /gevent gevent
    cd gevent
    echo Configuring cares
    time (cd deps/c-ares && ./configure --disable-dependency-tracking -C > /dev/null)
    rm -rf /gevent/wheelhouse
    mkdir /gevent/wheelhouse
    OPATH="$PATH"
    which auditwheel
    for variant in `ls -d /opt/python/cp{27,35,36,37,38,39}*`; do
        export PATH="$variant/bin:$OPATH"
        echo "Building $variant $(python --version)"

        python -mpip install -U pip
        # Build the wheel *in place*. This helps with cahching.
        # The downside is that we must install dependencies manually.
        # NOTE: We can't upgrade ``wheel`` because ``auditwheel`` depends on
        # it, and auditwheel is installed in one of these environments.
        python -mpip install -U "cython >= 3.0a5" cffi greenlet setuptools
        time (python setup.py bdist_wheel -q > /dev/null)
        PATH="$OPATH" auditwheel repair --plat manylinux2010_x86_64 dist/gevent*.whl
        cp wheelhouse/gevent*.whl /gevent/wheelhouse

        python -mpip install -U --no-compile `ls dist/gevent*whl`[test]
        echo 'Installation details'
        python -c 'from __future__ import print_function; import gevent; print(gevent, gevent.__version__)'
        python -c 'from __future__ import print_function; from gevent._compat import get_clock_info; print("clock info", get_clock_info("perf_counter"))'
        python -c 'from __future__ import print_function; import greenlet; print(greenlet, greenlet.__version__)'
        python -c 'from __future__ import print_function; import gevent.core; print("loop", gevent.core.loop)'
        python -c 'from __future__ import print_function; import gevent.ares; print("ares", gevent.ares)'

        python -mgevent.tests
        rm -rf build
        rm -f dist/gevent*.whl
        ccache -s
    done
    ccache -s
    exit 0
fi

# Mount the current directory as /gevent
# Mount the pip cache directory as /cache
# `pip cache` requires pip 20.1
echo Setting up caching
python --version
python -mpip --version
LCACHE="$(dirname `python -mpip cache dir`)"
echo Sharing pip cache at $LCACHE $(ls -ld $LCACHE)
echo Sharing ccache dir at $HOME/.ccache
if [ ! -d $HOME/.ccache ]; then
    mkdir $HOME/.ccache
fi
docker run --rm -ti -v "$(pwd):/gevent" -v "$LCACHE:/cache" -v "$HOME/.ccache:/ccache" quay.io/pypa/manylinux2010_x86_64 /gevent/scripts/releases/$(basename $0)
ls -l wheelhouse
