Skip to content

Commit 499cacb

Browse files
committed
Initial commit
0 parents  commit 499cacb

File tree

9 files changed

+637
-0
lines changed

9 files changed

+637
-0
lines changed

.github/workflows/build.yml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
name: Build Toolchain
2+
on:
3+
push:
4+
branches:
5+
- main
6+
tags:
7+
- v**
8+
pull_request:
9+
jobs:
10+
build:
11+
name: Build Toolchain
12+
runs-on: ubuntu-22.04
13+
strategy:
14+
matrix:
15+
target_arch: [aarch64, arm, i686, x86_64]
16+
steps:
17+
- name: Checkout Repo
18+
uses: actions/checkout@main
19+
with:
20+
path: ${{ github.workspace }}
21+
- name: Build Toolchain
22+
env:
23+
TOOLCHAIN_ARCH: ${{ matrix.target_arch }}
24+
run: |
25+
bash ./build.sh
26+
- name: Waiting for debugger
27+
if: ${{ failure() }}
28+
uses: mxschmitt/action-tmate@v3
29+
- name: Upload Build Archive
30+
uses: actions/upload-artifact@v2
31+
with:
32+
name: newer-toolchain-${{ matrix.target_arch }}-${{ github.sha }}
33+
path: ${{ github.workspace }}/build/
34+
release:
35+
name: Create Github Release
36+
if: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') }}
37+
needs: build
38+
runs-on: ubuntu-22.04
39+
steps:
40+
- name: Fetch archives
41+
uses: actions/download-artifact@v2
42+
with:
43+
path: ./
44+
- name: Copy archives
45+
run: |
46+
cp newer-toolchain-*-${{ github.sha }}/*.tar.bz2 ./
47+
- name: Get checksums
48+
id: checksums
49+
run: |
50+
checksums=$(printf 'SHA-256:\n```\n%s\n```\n' "$(sha256sum *.tar.bz2)")
51+
checksums="${checksums//'%'/'%25'}"
52+
checksums="${checksums//$'\n'/'%0A'}"
53+
checksums="${checksums//$'\r'/'%0D'}"
54+
echo "::set-output name=checksums::$checksums"
55+
- name: Get tag
56+
id: tag
57+
uses: dawidd6/action-get-tag@v1
58+
- name: Publish GitHub release
59+
uses: svenstaro/upload-release-action@v2
60+
with:
61+
repo_token: ${{ secrets.GITHUB_TOKEN }}
62+
file: "*.tar.bz2"
63+
file_glob: true
64+
release_name: "Android NDK toolchain version r17c with gcc"
65+
tag: ${{ steps.tag.outputs.tag }}
66+
body: ${{ steps.checksums.outputs.checksums }}

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Cache files
2+
/cache/
3+
4+
# Temp files
5+
/tmp/
6+
7+
# VSCode
8+
/.vscode/

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# NDK GCC Toolchain
2+

build.sh

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
_download_ndk_r17c() {
2+
if [ ! -f $_CACHE_DIR/.placeholder-android-ndk-r17c ]; then
3+
echo "Start downloading Android NDK toolchain (version r17c)..."
4+
mkdir -p $_CACHE_DIR/
5+
local _NDKARCHIVE_FILE=$_CACHE_DIR/android-ndk-r17c-linux-x86_64.zip
6+
local _NDK_URL=https://dl.google.com/android/repository/android-ndk-r17c-linux-x86_64.zip
7+
local _NDK_SHA256=3f541adbd0330a9205ba12697f6d04ec90752c53d6b622101a2a8a856e816589
8+
termux_download $_NDK_URL $_NDKARCHIVE_FILE $_NDK_SHA256
9+
unzip -d $_CACHE_DIR/ $_NDKARCHIVE_FILE > /dev/null 2>&1
10+
touch $_CACHE_DIR/.placeholder-android-ndk-r17c
11+
echo "Downloading completed."
12+
fi
13+
}
14+
15+
_setup_standalone_toolchain_ndk_r17c() {
16+
_download_ndk_r17c
17+
18+
local TOOLCHAIN_DIR="$1"
19+
rm -rf $TOOLCHAIN_DIR
20+
21+
local _NDKARCH
22+
if [ "$TOOLCHAIN_ARCH" == "aarch64" ]; then
23+
_NDKARCH="arm64"
24+
elif [ "$TOOLCHAIN_ARCH" == "arm" ]; then
25+
_NDKARCH="arm"
26+
elif [ "$TOOLCHAIN_ARCH" == "x86_64" ]; then
27+
_NDKARCH="x86_64"
28+
elif [ "$TOOLCHAIN_ARCH" == "i686" ]; then
29+
_NDKARCH="x86"
30+
fi
31+
32+
# Setup a standalone toolchain
33+
python $_CACHE_DIR/android-ndk-r17c/build/tools/make_standalone_toolchain.py \
34+
--arch $_NDKARCH --api $_API_LEVEL --install-dir $TOOLCHAIN_DIR
35+
36+
# Modify sysroot
37+
pushd $TOOLCHAIN_DIR
38+
39+
# See https://github.com/android/ndk/issues/215#issuecomment-524293090
40+
sed -i "s/include_next <stddef.h>/include <stddef.h>/" include/c++/4.9.x/cstddef
41+
42+
sed -i "s/define __ANDROID_API__ __ANDROID_API_FUTURE__/define __ANDROID_API__ $_API_LEVEL/" \
43+
sysroot/usr/include/android/api-level.h
44+
popd
45+
}

common-files/termux_download.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/bash
2+
##
3+
## Copyright 2020 Termux
4+
##
5+
## Licensed under the Apache License, Version 2.0 (the "License");
6+
## you may not use this file except in compliance with the License.
7+
## You may obtain a copy of the License at
8+
##
9+
## http://www.apache.org/licenses/LICENSE-2.0
10+
##
11+
## Unless required by applicable law or agreed to in writing, software
12+
## distributed under the License is distributed on an "AS IS" BASIS,
13+
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
## See the License for the specific language governing permissions and
15+
## limitations under the License.
16+
##
17+
18+
termux_download() {
19+
if [ $# != 3 ]; then
20+
termux_error_exit "termux_download(): Invalid arguments - expected \$URL \$DESTINATION \$CHECKSUM"
21+
fi
22+
local URL="$1"
23+
local DESTINATION="$2"
24+
local CHECKSUM="$3"
25+
26+
if [ -f "$DESTINATION" ] && [ "$CHECKSUM" != "SKIP_CHECKSUM" ]; then
27+
# Keep existing file if checksum matches.
28+
local EXISTING_CHECKSUM
29+
EXISTING_CHECKSUM=$(sha256sum "$DESTINATION" | cut -f 1 -d ' ')
30+
if [ "$EXISTING_CHECKSUM" = "$CHECKSUM" ]; then return; fi
31+
fi
32+
33+
local TMPFILE
34+
TMPFILE=$(mktemp "$TERMUX_PKG_TMPDIR/download.${TERMUX_PKG_NAME-unnamed}.XXXXXXXXX")
35+
echo "Downloading ${URL}"
36+
if curl --fail --retry 20 --retry-connrefused --retry-delay 30 --location --output "$TMPFILE" "$URL"; then
37+
local ACTUAL_CHECKSUM
38+
ACTUAL_CHECKSUM=$(sha256sum "$TMPFILE" | cut -f 1 -d ' ')
39+
if [ "$CHECKSUM" != "SKIP_CHECKSUM" ]; then
40+
if [ "$CHECKSUM" != "$ACTUAL_CHECKSUM" ]; then
41+
>&2 printf "Wrong checksum for %s:\nExpected: %s\nActual: %s\n" \
42+
"$URL" "$CHECKSUM" "$ACTUAL_CHECKSUM"
43+
return 1
44+
fi
45+
elif [ -z "$CHECKSUM" ]; then
46+
printf "WARNING: No checksum check for %s:\nActual: %s\n" \
47+
"$URL" "$ACTUAL_CHECKSUM"
48+
fi
49+
mv "$TMPFILE" "$DESTINATION"
50+
return 0
51+
fi
52+
53+
echo "Failed to download $URL" >&2
54+
return 1
55+
}
56+
57+
# Make script standalone executable as well as sourceable
58+
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
59+
termux_download "$@"
60+
fi

0 commit comments

Comments
 (0)