1#!/bin/bash
2set -e
3
4top=$(cd $(dirname $0)/../../../.. && pwd)
5
6out=$top/out/python3
7python_src=$top/external/python/cpython3
8
9# On Linux, enter the Docker container and reinvoke this script.
10if [ "$(uname)" == "Linux" -a "$SKIP_DOCKER" == "" ]; then
11    docker build -t ndk-python3 $python_src/kokoro
12    export SKIP_DOCKER=1
13    docker run -v$top:$top -eKOKORO_BUILD_ID -eSKIP_DOCKER \
14      --entrypoint $python_src/kokoro/kokoro_build.sh \
15      ndk-python3
16    exit $?
17fi
18
19extra_ldflags=
20extra_notices=
21
22if [ "$(uname)" == "Darwin" ]; then
23    # The Kokoro big-sur builder has some extra x86-64 libraries installed in
24    # /usr/local (using homebrew), which override the MacOS SDK. At least 3
25    # modules are affected (_dbm, _gdbm, and _lzma).
26    brew_all_pkgs=$(brew list)
27    printf "Brew packages installed:\n%s\n\n" "$brew_all_pkgs"
28    brew_pkgs=
29    for name in gdbm xz; do
30        if echo "$brew_all_pkgs" | grep -q "^${name}\(@\|$\)"; then
31            brew_pkgs="$brew_pkgs $name"
32        fi
33    done
34    if [ -n "$brew_pkgs" ]; then
35        # A local developer probably won't have $KOKORO_ARTIFACTS_DIR set.
36        if [ -n "$KOKORO_ARTIFACTS_DIR" ]; then
37            # Pass --ignore-dependencies because some Homebrew packages still
38            # need the packages we want to remove, notably [email protected], which
39            # we're using later to run kokoro/build.py.
40            cmd="brew uninstall --ignore-dependencies $brew_pkgs"
41            echo "Will run in 5 seconds (press Ctrl-C to abort): $cmd"
42            sleep 5
43            $cmd
44        else
45            echo "!!! WARNING: Your machine has Homebrew packages installed that could"
46            echo "!!! affect how some extension modules are built:"
47            echo "!!!"
48            echo "!!!    $brew_pkgs"
49            echo "!!!"
50        fi
51    fi
52
53    # http://g3doc/devtools/kokoro/g3doc/userdocs/macos/selecting_xcode
54    if [ -d /Applications/Xcode_12.5.1.app ]; then
55        xcode=/Applications/Xcode_12.5.1.app
56        cmd="sudo xcode-select -s $xcode/Contents/Developer"
57        echo "Running: $cmd"
58        $cmd
59        export SDKROOT=$xcode/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
60    fi
61    echo "Selected Xcode: $(xcode-select -p)"
62elif [ "$(uname)" == "Linux" ]; then
63    # Build libffi.a for use with the _ctypes module.
64    (cd $top/external/libffi && ./autogen.sh)
65    rm -fr $top/out/libffi
66    mkdir -p $top/out/libffi/build
67    pushd $top/out/libffi/build
68    $top/external/libffi/configure \
69        --enable-static --disable-shared --with-pic --disable-docs \
70        --prefix=$top/out/libffi/install
71    make -j$(nproc) install
72    popd
73
74    # cpython's configure script will use pkg-config to set LIBFFI_INCLUDEDIR,
75    # which setup.py reads. It doesn't use pkg-config to add the library search
76    # dir. With no --prefix, libffi.a would install to /usr/local/lib64, which
77    # doesn't work because, even though setup.py links using -lffi, setup.py
78    # first searches for libffi.{a,so} and needs to find it. setup.py searches
79    # in /usr/local/lib and /usr/lib64, but not /usr/local/lib64.
80    #
81    # Use -Wl,--exclude-libs to hide libffi.a symbols in _ctypes.*.so.
82    export PKG_CONFIG_PATH=$top/out/libffi/install/lib/pkgconfig
83    extra_ldflags="$extra_ldflags -L$top/out/libffi/install/lib64 -Wl,--exclude-libs=libffi.a"
84    extra_notices="$extra_notices $top/external/libffi/LICENSE"
85fi
86
87rm -fr $out
88
89python3 --version
90python3 $python_src/kokoro/build.py $python_src $out $out/artifact \
91    "${KOKORO_BUILD_ID:-dev}" "$extra_ldflags" "$extra_notices"
92
93# Verify that some extensions can be loaded.
94$out/install/bin/python3 -c 'import binascii, bz2, ctypes, curses, curses.panel, hashlib, zlib'
95
96$top/toolchain/ndk-kokoro/gen_manifest.py --root $top \
97    -o "$out/artifact/manifest-${KOKORO_BUILD_ID:-dev}.xml"
98