Debian Codegraph @

linux/build-toolchain-android

raw
1 #!/bin/bash
2 #
3 # This file is part of nzbget. See <http://nzbget.net>.
4 #
5 # Copyright (C) 2018 Andrey Prygunkov <[email protected]>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 # This script builds cross-compiling toolchain, which can compile NZBGet for Android.
22 # The toolchain itself runs on Linux.
23
24 # Setup strict bash error handling
25 set -o nounset
26 set -o errexit
27
28 # Uncomment next line for debuging
29 #set -x
30
31
32 # Android API level
33 APILEVEL=21
34
35 # Architecture
36 ARCH=$1
37 case "$ARCH" in
38 i686)
39 NDK_ARCH="x86"
40 NDK_TARGET="i686-linux-android"
41 OPENSSL_TARGET="android-x86"
42 ;;
43 x86_64)
44 NDK_ARCH="x86_64"
45 NDK_TARGET="x86_64-linux-android"
46 OPENSSL_TARGET="android64"
47 ;;
48 armhf)
49 NDK_ARCH="arm"
50 NDK_TARGET="arm-linux-androideabi"
51 OPENSSL_TARGET="android-armeabi"
52 ;;
53 aarch64)
54 NDK_ARCH="arm64"
55 NDK_TARGET="aarch64-linux-android"
56 OPENSSL_TARGET="android64-aarch64"
57 ;;
58 *)
59 echo "Usage: $0 (i686|x86_64|armhf|aarch64)"
60 exit 1
61 ;;
62 esac
63
64 echo "Creating toolchain for $ARCH"
65
66 # Android NDK
67 NDK_VERSION="r17"
68 NDK_DIRNAME="android-ndk-$NDK_VERSION"
69 NDK_ARCHIVE="$NDK_DIRNAME-linux-x86_64.zip"
70 NDK_URL="https://dl.google.com/android/repository/$NDK_ARCHIVE"
71
72 # Libxml
73 LIBXML2_VERSION="2.9.4"
74 LIBXML2_ARCHIVE="libxml2-$LIBXML2_VERSION.tar.gz"
75 LIBXML2_URL="http://xmlsoft.org/sources/$LIBXML2_ARCHIVE"
76
77 # OpenSSL
78 OPENSSL_VERSION="1.1.0h"
79 OPENSSL_ARCHIVE="openssl-$OPENSSL_VERSION.tar.gz"
80 OPENSSL_URL="https://www.openssl.org/source/$OPENSSL_ARCHIVE"
81
82 # NCurses
83 NCURSES_VERSION="6.0"
84 NCURSES_ARCHIVE="ncurses-$NCURSES_VERSION.tar.gz"
85 NCURSES_URL="https://ftp.gnu.org/pub/gnu/ncurses/$NCURSES_ARCHIVE"
86
87 ### START OF THE SCRIPT
88
89 ROOTDIR=`pwd`
90 ROOTDIR="$ROOTDIR/$ARCH-ndk"
91
92 rm -rf $ROOTDIR
93 mkdir $ROOTDIR
94 cd $ROOTDIR
95
96 # Download all required tools and libraries
97 cd ..
98 mkdir -p downloads
99 cd downloads
100 if [ ! -d $NDK_DIRNAME -a ! -f $NDK_ARCHIVE ]; then
101 wget $NDK_URL
102 fi
103 if [ ! -f $LIBXML2_ARCHIVE ]; then
104 wget $LIBXML2_URL
105 fi
106 if [ ! -f $OPENSSL_ARCHIVE ]; then
107 wget $OPENSSL_URL
108 fi
109 if [ ! -f $NCURSES_ARCHIVE ]; then
110 wget $NCURSES_URL
111 fi
112 cd ..
113
114 # Unpack NDK
115 if [ ! -d ./$NDK_DIRNAME ]; then
116 echo "Unpacking NDK"
117 unzip ./downloads/$NDK_ARCHIVE
118 fi
119
120 # Create toolchain for target
121 echo "Preparing standalone NDK toolchain"
122 ./$NDK_DIRNAME/build/tools/make_standalone_toolchain.py --arch $NDK_ARCH --api $APILEVEL --install-dir $ROOTDIR/output/host/usr
123
124 cd $ROOTDIR
125
126 # Configure toolchain
127 export PATH=$PATH:$ROOTDIR/output/host/usr/bin
128 export CC=$NDK_TARGET-clang
129 export CXX=$NDK_TARGET-clang++
130 export AS=$NDK_TARGET-clang
131 export AR=$NDK_TARGET-ar
132 export LD=$NDK_TARGET-ld
133 export RANLIB=$NDK_TARGET-ranlib
134 export CFLAGS="-fPIE -fPIC"
135 export CXXFLAGS=$CFLAGS
136 export LDFLAGS=""
137
138 mkdir output/build
139 ln -s host/usr/sysroot output/staging
140
141 cd $ROOTDIR/output/build
142
143 # Build OpenSSL (5 minutes)
144 tar xf ../../../downloads/$OPENSSL_ARCHIVE
145 cd openssl-$OPENSSL_VERSION
146 ./Configure --prefix=$ROOTDIR/output/staging/usr --sysroot=$ROOTDIR/output/staging no-shared no-dso no-hw no-zlib no-unit-test "$OPENSSL_TARGET"
147 sed 's:-mandroid::' -i Makefile
148 make -j2
149 make install_sw
150 cd ..
151
152 # Build libxml2 (2 minutes)
153 tar xf ../../../downloads/$LIBXML2_ARCHIVE
154 cd libxml2-$LIBXML2_VERSION
155 ./configure --host=$NDK_TARGET -prefix=$ROOTDIR/output/staging/usr --disable-dependency-tracking --without-zlib --without-lzma --without-python --disable-shared
156 sed 's:^PROGRAMS =.*:PROGRAMS = :' -i Makefile
157 sed 's:^bin_PROGRAMS =.*:bin_PROGRAMS = :' -i Makefile
158 sed 's:^SUBDIRS =.*:SUBDIRS = include .:' -i Makefile
159 make -j2
160 make install
161 cd ..
162
163 # Build NCurses (2 minutes)
164 tar xf ../../../downloads/$NCURSES_ARCHIVE
165 cd ncurses-$NCURSES_VERSION
166 ./configure --host=$NDK_TARGET -prefix=$ROOTDIR/output/staging/usr --disable-dependency-tracking --disable-largefile
167 make -j2
168 make install
169 cd ..
170
171 cd ..
172
173 # Create missing package descriptions
174 echo "prefix=$ROOTDIR/output/staging/usr
175
176 Name: zlib
177 Description: zlib
178 Version: 1
179 Libs: -L\${prefix}/lib -lz
180 Cflags: -I\${prefix}/include
181 " > $ROOTDIR/output/staging/usr/lib/pkgconfig/zlib.pc
182
183 echo "prefix=$ROOTDIR/output/staging/usr
184
185 Name: ncurses
186 Description: ncurses
187 Version: 5
188 Libs: -L\${prefix}/lib -lncurses
189 Cflags: -I\${prefix}/include
190 " > $ROOTDIR/output/staging/usr/lib/pkgconfig/ncurses.pc
191
192 # Remove "-L${prefix}/lib" and "-L${libdir}" from all packages to fix strange linker error
193 find $ROOTDIR/output/staging/usr/lib/pkgconfig -type f -exec sed 's:-L\${prefix}/lib::' -i {} \;
194 find $ROOTDIR/output/staging/usr/lib/pkgconfig -type f -exec sed 's:-L\${libdir}::' -i {} \;
195
196 echo "Toolchain creation completed for $ARCH"
197