Debian Codegraph @

external/linker/eng/common/cross/build-android-rootfs.sh

raw
1 #!/usr/bin/env bash
2 set -e
3 __NDK_Version=r14
4
5 usage()
6 {
7 echo "Creates a toolchain and sysroot used for cross-compiling for Android."
8 echo.
9 echo "Usage: $0 [BuildArch] [ApiLevel]"
10 echo.
11 echo "BuildArch is the target architecture of Android. Currently only arm64 is supported."
12 echo "ApiLevel is the target Android API level. API levels usually match to Android releases. See https://source.android.com/source/build-numbers.html"
13 echo.
14 echo "By default, the toolchain and sysroot will be generated in cross/android-rootfs/toolchain/[BuildArch]. You can change this behavior"
15 echo "by setting the TOOLCHAIN_DIR environment variable"
16 echo.
17 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,"
18 echo "you can set the NDK_DIR environment variable to have this script use that installation of the NDK."
19 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.21-arm64. This file is to replace '/etc/os-release', which is not available for Android."
20 exit 1
21 }
22
23 __ApiLevel=21 # The minimum platform for arm64 is API level 21
24 __BuildArch=arm64
25 __AndroidArch=aarch64
26 __AndroidToolchain=aarch64-linux-android
27
28 for i in "$@"
29 do
30 lowerI="$(echo $i | awk '{print tolower($0)}')"
31 case $lowerI in
32 -?|-h|--help)
33 usage
34 exit 1
35 ;;
36 arm64)
37 __BuildArch=arm64
38 __AndroidArch=aarch64
39 __AndroidToolchain=aarch64-linux-android
40 ;;
41 arm)
42 __BuildArch=arm
43 __AndroidArch=arm
44 __AndroidToolchain=arm-linux-androideabi
45 ;;
46 *[0-9])
47 __ApiLevel=$i
48 ;;
49 *)
50 __UnprocessedBuildArgs="$__UnprocessedBuildArgs $i"
51 ;;
52 esac
53 done
54
55 # Obtain the location of the bash script to figure out where the root of the repo is.
56 __CrossDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
57
58 __Android_Cross_Dir="$__CrossDir/android-rootfs"
59 __NDK_Dir="$__Android_Cross_Dir/android-ndk-$__NDK_Version"
60 __libunwind_Dir="$__Android_Cross_Dir/libunwind"
61 __lldb_Dir="$__Android_Cross_Dir/lldb"
62 __ToolchainDir="$__Android_Cross_Dir/toolchain/$__BuildArch"
63
64 if [[ -n "$TOOLCHAIN_DIR" ]]; then
65 __ToolchainDir=$TOOLCHAIN_DIR
66 fi
67
68 if [[ -n "$NDK_DIR" ]]; then
69 __NDK_Dir=$NDK_DIR
70 fi
71
72 echo "Target API level: $__ApiLevel"
73 echo "Target architecture: $__BuildArch"
74 echo "NDK location: $__NDK_Dir"
75 echo "Target Toolchain location: $__ToolchainDir"
76
77 # Download the NDK if required
78 if [ ! -d $__NDK_Dir ]; then
79 echo Downloading the NDK into $__NDK_Dir
80 mkdir -p $__NDK_Dir
81 wget -nv -nc --show-progress https://dl.google.com/android/repository/android-ndk-$__NDK_Version-linux-x86_64.zip -O $__Android_Cross_Dir/android-ndk-$__NDK_Version-linux-x86_64.zip
82 unzip -q $__Android_Cross_Dir/android-ndk-$__NDK_Version-linux-x86_64.zip -d $__Android_Cross_Dir
83 fi
84
85 if [ ! -d $__lldb_Dir ]; then
86 mkdir -p $__lldb_Dir
87 echo Downloading LLDB into $__lldb_Dir
88 wget -nv -nc --show-progress https://dl.google.com/android/repository/lldb-2.3.3614996-linux-x86_64.zip -O $__Android_Cross_Dir/lldb-2.3.3614996-linux-x86_64.zip
89 unzip -q $__Android_Cross_Dir/lldb-2.3.3614996-linux-x86_64.zip -d $__lldb_Dir
90 fi
91
92 # Create the RootFS for both arm64 as well as aarch
93 rm -rf $__Android_Cross_Dir/toolchain
94
95 echo Generating the $__BuildArch toolchain
96 $__NDK_Dir/build/tools/make_standalone_toolchain.py --arch $__BuildArch --api $__ApiLevel --install-dir $__ToolchainDir
97
98 # Install the required packages into the toolchain
99 # TODO: Add logic to get latest pkg version instead of specific version number
100 rm -rf $__Android_Cross_Dir/deb/
101 rm -rf $__Android_Cross_Dir/tmp
102
103 mkdir -p $__Android_Cross_Dir/deb/
104 mkdir -p $__Android_Cross_Dir/tmp/$arch/
105 wget -nv -nc http://termux.net/dists/stable/main/binary-$__AndroidArch/libicu_60.2_$__AndroidArch.deb -O $__Android_Cross_Dir/deb/libicu_60.2_$__AndroidArch.deb
106 wget -nv -nc http://termux.net/dists/stable/main/binary-$__AndroidArch/libicu-dev_60.2_$__AndroidArch.deb -O $__Android_Cross_Dir/deb/libicu-dev_60.2_$__AndroidArch.deb
107
108 wget -nv -nc http://termux.net/dists/stable/main/binary-$__AndroidArch/libandroid-glob-dev_0.4_$__AndroidArch.deb -O $__Android_Cross_Dir/deb/libandroid-glob-dev_0.4_$__AndroidArch.deb
109 wget -nv -nc http://termux.net/dists/stable/main/binary-$__AndroidArch/libandroid-glob_0.4_$__AndroidArch.deb -O $__Android_Cross_Dir/deb/libandroid-glob_0.4_$__AndroidArch.deb
110 wget -nv -nc http://termux.net/dists/stable/main/binary-$__AndroidArch/libandroid-support-dev_22_$__AndroidArch.deb -O $__Android_Cross_Dir/deb/libandroid-support-dev_22_$__AndroidArch.deb
111 wget -nv -nc http://termux.net/dists/stable/main/binary-$__AndroidArch/libandroid-support_22_$__AndroidArch.deb -O $__Android_Cross_Dir/deb/libandroid-support_22_$__AndroidArch.deb
112 wget -nv -nc http://termux.net/dists/stable/main/binary-$__AndroidArch/liblzma-dev_5.2.3_$__AndroidArch.deb -O $__Android_Cross_Dir/deb/liblzma-dev_5.2.3_$__AndroidArch.deb
113 wget -nv -nc http://termux.net/dists/stable/main/binary-$__AndroidArch/liblzma_5.2.3_$__AndroidArch.deb -O $__Android_Cross_Dir/deb/liblzma_5.2.3_$__AndroidArch.deb
114 wget -nv -nc http://termux.net/dists/stable/main/binary-$__AndroidArch/libunwind-dev_1.2.20170304_$__AndroidArch.deb -O $__Android_Cross_Dir/deb/libunwind-dev_1.2.20170304_$__AndroidArch.deb
115 wget -nv -nc http://termux.net/dists/stable/main/binary-$__AndroidArch/libunwind_1.2.20170304_$__AndroidArch.deb -O $__Android_Cross_Dir/deb/libunwind_1.2.20170304_$__AndroidArch.deb
116
117 echo Unpacking Termux packages
118 dpkg -x $__Android_Cross_Dir/deb/libicu_60.2_$__AndroidArch.deb $__Android_Cross_Dir/tmp/$__AndroidArch/
119 dpkg -x $__Android_Cross_Dir/deb/libicu-dev_60.2_$__AndroidArch.deb $__Android_Cross_Dir/tmp/$__AndroidArch/
120 dpkg -x $__Android_Cross_Dir/deb/libandroid-glob-dev_0.4_$__AndroidArch.deb $__Android_Cross_Dir/tmp/$__AndroidArch/
121 dpkg -x $__Android_Cross_Dir/deb/libandroid-glob_0.4_$__AndroidArch.deb $__Android_Cross_Dir/tmp/$__AndroidArch/
122 dpkg -x $__Android_Cross_Dir/deb/libandroid-support-dev_22_$__AndroidArch.deb $__Android_Cross_Dir/tmp/$__AndroidArch/
123 dpkg -x $__Android_Cross_Dir/deb/libandroid-support_22_$__AndroidArch.deb $__Android_Cross_Dir/tmp/$__AndroidArch/
124 dpkg -x $__Android_Cross_Dir/deb/liblzma-dev_5.2.3_$__AndroidArch.deb $__Android_Cross_Dir/tmp/$__AndroidArch/
125 dpkg -x $__Android_Cross_Dir/deb/liblzma_5.2.3_$__AndroidArch.deb $__Android_Cross_Dir/tmp/$__AndroidArch/
126 dpkg -x $__Android_Cross_Dir/deb/libunwind-dev_1.2.20170304_$__AndroidArch.deb $__Android_Cross_Dir/tmp/$__AndroidArch/
127 dpkg -x $__Android_Cross_Dir/deb/libunwind_1.2.20170304_$__AndroidArch.deb $__Android_Cross_Dir/tmp/$__AndroidArch/
128
129 cp -R $__Android_Cross_Dir/tmp/$__AndroidArch/data/data/com.termux/files/usr/* $__ToolchainDir/sysroot/usr/
130
131 # Generate platform file for build.sh script to assign to __DistroRid
132 echo "Generating platform file..."
133
134 echo "RID=android.21-arm64" > $__ToolchainDir/sysroot/android_platform
135 echo Now run:
136 echo CONFIG_DIR=\`realpath cross/android/$__BuildArch\` ROOTFS_DIR=\`realpath $__ToolchainDir/sysroot\` ./build.sh cross $__BuildArch skipgenerateversion skipnuget cmakeargs -DENABLE_LLDBPLUGIN=0
137