|
| 1 | +#!/bin/bash |
| 2 | +# build.sh - script to build a custom NDK toolchain |
| 3 | +# |
| 4 | +# Copyright 2022 Chongyun Lee <[email protected]> |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | +set -e -u -o pipefail |
| 20 | + |
| 21 | +_SCRIPTDIR=$(cd "$(realpath "$(dirname "$0")")"; pwd) |
| 22 | +source $_SCRIPTDIR/common-files/setup_toolchain_ndk_r17c.sh |
| 23 | +source $_SCRIPTDIR/common-files/termux_download.sh |
| 24 | + |
| 25 | +: ${TOOLCHAIN_ARCH:=aarch64} |
| 26 | +: ${_CACHE_DIR:=$_SCRIPTDIR/cache} |
| 27 | +: ${_TMP_DIR:=$_SCRIPTDIR/tmp} |
| 28 | +: ${_API_LEVEL:=21} |
| 29 | +: ${_MAKE_PROCESSES:=$(nproc)} |
| 30 | +: ${GCC_VERSION:=12.1.0} |
| 31 | +: ${GCC_SHA256:=e88a004a14697bbbaba311f38a938c716d9a652fd151aaaa4cf1b5b99b90e2de} |
| 32 | + |
| 33 | +export TOOLCHAIN_ARCH |
| 34 | + |
| 35 | +TERMUX_PKG_TMPDIR=$_TMP_DIR |
| 36 | +mkdir -p $_CACHE_DIR |
| 37 | +rm -rf $_TMP_DIR |
| 38 | +mkdir -p $_TMP_DIR |
| 39 | + |
| 40 | +_HOST_PLATFORM="${TOOLCHAIN_ARCH}-linux-android" |
| 41 | + |
| 42 | +_EXTRA_HOST_BUILD="" |
| 43 | +if [ "$TOOLCHAIN_ARCH" = "arm" ]; then |
| 44 | + _HOST_PLATFORM="${_HOST_PLATFORM}eabi" |
| 45 | + _EXTRA_HOST_BUILD="--with-arch=armv7-a --with-float=soft --with-fpu=vfp" |
| 46 | +elif [ "$TOOLCHAIN_ARCH" = "aarch64" ]; then |
| 47 | + _EXTRA_HOST_BUILD="--enable-fix-cortex-a53-835769 --enable-fix-cortex-a53-843419" |
| 48 | +elif [ "$TOOLCHAIN_ARCH" = "i686" ]; then |
| 49 | + _EXTRA_HOST_BUILD="--with-arch=i686 --with-fpmath=sse " |
| 50 | +elif [ "$TOOLCHAIN_ARCH" = "x86_64" ]; then |
| 51 | + _EXTRA_HOST_BUILD="--with-arch=x86-64 --with-fpmath=sse" |
| 52 | +fi |
| 53 | + |
| 54 | +# Install dependencies |
| 55 | +sudo apt update |
| 56 | +sudo apt install -y build-essential curl |
| 57 | +sudo apt install -y libgmp-dev libmpfr-dev libmpc-dev zlib1g-dev libisl-dev libtinfo5 libncurses5 |
| 58 | + |
| 59 | +pushd $_TMP_DIR |
| 60 | + |
| 61 | +# Download source |
| 62 | +SRC_URL=https://ftp.gnu.org/gnu/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.gz |
| 63 | +SRC_FILE=$_CACHE_DIR/gcc-${GCC_VERSION}.tar.gz |
| 64 | +SRC_DIR=$_TMP_DIR/gcc-${GCC_VERSION} |
| 65 | +termux_download $SRC_URL $SRC_FILE $GCC_SHA256 |
| 66 | + |
| 67 | +# Setup a standalone toolchain |
| 68 | +_setup_standalone_toolchain_ndk_r17c $_TMP_DIR/standalone-toolchain |
| 69 | +cp -R $_TMP_DIR/standalone-toolchain/sysroot/usr/include/$_HOST_PLATFORM/* $_TMP_DIR/standalone-toolchain/sysroot/usr/include/ |
| 70 | + |
| 71 | +PATH="$_TMP_DIR/standalone-toolchain/bin:$PATH" |
| 72 | + |
| 73 | +# Extract source |
| 74 | +tar -xf $SRC_FILE -C $_TMP_DIR/ |
| 75 | +pushd $_TMP_DIR |
| 76 | +PATCHES="$(find "$_SCRIPTDIR/patches/" -maxdepth 1 -type f -name *.patch | sort)" |
| 77 | +for f in $PATCHES; do |
| 78 | + echo "Applying patch: $(basename $f)" |
| 79 | + patch -d "$SRC_DIR/" -p1 < "$f"; |
| 80 | +done |
| 81 | +popd |
| 82 | + |
| 83 | +# Build a custom toolchain |
| 84 | +mkdir -p $_TMP_DIR/newer-toolchain |
| 85 | +cp -R $_TMP_DIR/standalone-toolchain/sysroot $_TMP_DIR/newer-toolchain/ |
| 86 | + |
| 87 | +mkdir -p newer-toolchain-build |
| 88 | +pushd newer-toolchain-build |
| 89 | + |
| 90 | +export CFLAGS="-D__ANDROID_API__=$_API_LEVEL" |
| 91 | +export CPPFLAGS="-D__ANDROID_API__=$_API_LEVEL" |
| 92 | +export CXXFLAGS="-D__ANDROID_API__=$_API_LEVEL" |
| 93 | + |
| 94 | +$SRC_DIR/configure \ |
| 95 | + --host=x86_64-linux-gnu \ |
| 96 | + --build=x86_64-linux-gnu \ |
| 97 | + --target=$_HOST_PLATFORM \ |
| 98 | + --disable-shared \ |
| 99 | + --disable-nls \ |
| 100 | + --enable-default-pie \ |
| 101 | + --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' \ |
| 102 | + --with-gnu-as --with-gnu-ld \ |
| 103 | + --disable-libstdc__-v3 \ |
| 104 | + --disable-tls \ |
| 105 | + --disable-bootstrap \ |
| 106 | + --enable-initfini-array \ |
| 107 | + --enable-libatomic-ifuncs=no \ |
| 108 | + --prefix=$_TMP_DIR/newer-toolchain \ |
| 109 | + --with-gmp --with-mpfr --with-mpc --with-system-zlib \ |
| 110 | + --enable-languages=c,c++,fortran \ |
| 111 | + --enable-plugins --enable-libgomp \ |
| 112 | + --enable-gnu-indirect-function \ |
| 113 | + --disable-libcilkrts --disable-libsanitizer \ |
| 114 | + --enable-gold --enable-threads \ |
| 115 | + --enable-eh-frame-hdr-for-static \ |
| 116 | + --enable-graphite=yes --with-isl \ |
| 117 | + --disable-multilib \ |
| 118 | + $_EXTRA_HOST_BUILD \ |
| 119 | + --with-sysroot=$_TMP_DIR/newer-toolchain/sysroot \ |
| 120 | + --with-gxx-include-dir=$_TMP_DIR/newer-toolchain/include/c++/$GCC_VERSION |
| 121 | + |
| 122 | +make -j $_MAKE_PROCESSES |
| 123 | +make -j $_MAKE_PROCESSES install |
| 124 | + |
| 125 | +popd # newer-toolchain-build |
| 126 | + |
| 127 | +# Make the archive |
| 128 | +mv newer-toolchain gcc-$GCC_VERSION-$TOOLCHAIN_ARCH |
| 129 | +tar -cjvf gcc-$GCC_VERSION-$TOOLCHAIN_ARCH.tar.bz2 gcc-$GCC_VERSION-$TOOLCHAIN_ARCH |
| 130 | + |
| 131 | +popd # $_TMP_DIR |
| 132 | + |
| 133 | +# Copy the archive |
| 134 | +mkdir -p build |
| 135 | +cp $_TMP_DIR/gcc-$GCC_VERSION-$TOOLCHAIN_ARCH.tar.bz2 ./build |
0 commit comments