2018-11-08 01:51:56 +00:00
#!/usr/bin/env bash
set -e
2020-03-08 12:38:05 +00:00
__NDK_Version = r21
2018-11-08 01:51:56 +00:00
usage( )
{
echo "Creates a toolchain and sysroot used for cross-compiling for Android."
echo.
echo " Usage: $0 [BuildArch] [ApiLevel] "
echo.
echo "BuildArch is the target architecture of Android. Currently only arm64 is supported."
echo "ApiLevel is the target Android API level. API levels usually match to Android releases. See https://source.android.com/source/build-numbers.html"
echo.
echo "By default, the toolchain and sysroot will be generated in cross/android-rootfs/toolchain/[BuildArch]. You can change this behavior"
echo "by setting the TOOLCHAIN_DIR environment variable"
echo.
echo " By default, the NDK will be downloaded into the cross/android-rootfs/android-ndk- $__NDK_Version directory. If you already have an NDK installation, "
echo "you can set the NDK_DIR environment variable to have this script use that installation of the NDK."
2020-03-08 12:38:05 +00:00
echo "By default, this script will generate a file, android_platform, in the root of the ROOTFS_DIR directory that contains the RID for the supported and tested Android build: android.28-arm64. This file is to replace '/etc/os-release', which is not available for Android."
2018-11-08 01:51:56 +00:00
exit 1
}
2020-03-08 12:38:05 +00:00
__ApiLevel = 28 # The minimum platform for arm64 is API level 21 but the minimum version that support glob(3) is 28. See $ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/glob.h
2018-11-08 01:51:56 +00:00
__BuildArch = arm64
__AndroidArch = aarch64
__AndroidToolchain = aarch64-linux-android
for i in " $@ "
do
2021-01-09 13:53:15 +00:00
lowerI = " $( echo $i | tr "[:upper:]" "[:lower:]" ) "
2018-11-08 01:51:56 +00:00
case $lowerI in
-?| -h| --help)
usage
exit 1
; ;
arm64)
__BuildArch = arm64
__AndroidArch = aarch64
__AndroidToolchain = aarch64-linux-android
; ;
arm)
__BuildArch = arm
__AndroidArch = arm
__AndroidToolchain = arm-linux-androideabi
; ;
*[ 0-9] )
__ApiLevel = $i
; ;
*)
__UnprocessedBuildArgs = " $__UnprocessedBuildArgs $i "
; ;
esac
done
# Obtain the location of the bash script to figure out where the root of the repo is.
2020-03-18 12:35:52 +00:00
__ScriptBaseDir = " $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " && pwd ) "
2018-11-08 01:51:56 +00:00
2020-03-18 12:35:52 +00:00
__CrossDir = " $__ScriptBaseDir /../../../.tools/android-rootfs "
if [ [ ! -f " $__CrossDir " ] ] ; then
mkdir -p " $__CrossDir "
fi
# Resolve absolute path to avoid `../` in build logs
__CrossDir = " $( cd " $__CrossDir " && pwd ) "
__NDK_Dir = " $__CrossDir /android-ndk- $__NDK_Version "
__lldb_Dir = " $__CrossDir /lldb "
__ToolchainDir = " $__CrossDir /android-ndk- $__NDK_Version "
2018-11-08 01:51:56 +00:00
if [ [ -n " $TOOLCHAIN_DIR " ] ] ; then
__ToolchainDir = $TOOLCHAIN_DIR
fi
if [ [ -n " $NDK_DIR " ] ] ; then
__NDK_Dir = $NDK_DIR
fi
echo " Target API level: $__ApiLevel "
echo " Target architecture: $__BuildArch "
echo " NDK location: $__NDK_Dir "
echo " Target Toolchain location: $__ToolchainDir "
# Download the NDK if required
if [ ! -d $__NDK_Dir ] ; then
echo Downloading the NDK into $__NDK_Dir
mkdir -p $__NDK_Dir
2020-03-18 12:35:52 +00:00
wget -q --progress= bar:force:noscroll --show-progress https://dl.google.com/android/repository/android-ndk-$__NDK_Version -linux-x86_64.zip -O $__CrossDir /android-ndk-$__NDK_Version -linux-x86_64.zip
unzip -q $__CrossDir /android-ndk-$__NDK_Version -linux-x86_64.zip -d $__CrossDir
2018-11-08 01:51:56 +00:00
fi
if [ ! -d $__lldb_Dir ] ; then
mkdir -p $__lldb_Dir
echo Downloading LLDB into $__lldb_Dir
2020-03-18 12:35:52 +00:00
wget -q --progress= bar:force:noscroll --show-progress https://dl.google.com/android/repository/lldb-2.3.3614996-linux-x86_64.zip -O $__CrossDir /lldb-2.3.3614996-linux-x86_64.zip
unzip -q $__CrossDir /lldb-2.3.3614996-linux-x86_64.zip -d $__lldb_Dir
2018-11-08 01:51:56 +00:00
fi
2020-03-08 12:38:05 +00:00
echo "Download dependencies..."
2020-03-18 12:35:52 +00:00
__TmpDir = $__CrossDir /tmp/$__BuildArch /
mkdir -p " $__TmpDir "
2018-11-08 01:51:56 +00:00
2020-03-08 12:38:05 +00:00
# combined dependencies for coreclr, installer and libraries
__AndroidPackages = "libicu"
__AndroidPackages += " libandroid-glob"
__AndroidPackages += " liblzma"
__AndroidPackages += " krb5"
__AndroidPackages += " openssl"
2021-04-08 13:19:46 +00:00
__AndroidPackages += " openldap"
2018-11-08 01:51:56 +00:00
2020-03-08 12:38:05 +00:00
for path in $( wget -qO- http://termux.net/dists/stable/main/binary-$__AndroidArch /Packages | \
grep -A15 " Package: \( ${ __AndroidPackages // / \\ | } \) " | grep -v "static\|tool" | grep Filename) ; do
2018-11-08 01:51:56 +00:00
2020-03-08 12:38:05 +00:00
if [ [ " $path " != "Filename:" ] ] ; then
echo " Working on: $path "
2020-03-18 12:35:52 +00:00
wget -qO- http://termux.net/$path | dpkg -x - " $__TmpDir "
2020-03-08 12:38:05 +00:00
fi
done
2018-11-08 01:51:56 +00:00
2020-03-18 12:35:52 +00:00
cp -R " $__TmpDir /data/data/com.termux/files/usr/ " * " $__ToolchainDir /sysroot/usr/ "
2018-11-08 01:51:56 +00:00
# Generate platform file for build.sh script to assign to __DistroRid
echo "Generating platform file..."
2020-03-08 12:38:05 +00:00
echo " RID=android. ${ __ApiLevel } - ${ __BuildArch } " > $__ToolchainDir /sysroot/android_platform
2018-11-08 01:51:56 +00:00
2020-03-08 12:38:05 +00:00
echo "Now to build coreclr, libraries and installers; run:"
echo ROOTFS_DIR = \$ \( realpath $__ToolchainDir /sysroot\) ./build.sh --cross --arch $__BuildArch \
2020-03-18 12:35:52 +00:00
--subsetCategory coreclr
echo ROOTFS_DIR = \$ \( realpath $__ToolchainDir /sysroot\) ./build.sh --cross --arch $__BuildArch \
--subsetCategory libraries
echo ROOTFS_DIR = \$ \( realpath $__ToolchainDir /sysroot\) ./build.sh --cross --arch $__BuildArch \
2020-03-08 12:38:05 +00:00
--subsetCategory installer