summaryrefslogtreecommitdiff
path: root/build.sh
blob: ce1192cda49502038f13dc0d3ffa7d56ce18996e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
#!/bin/bash

# Wanted versions
[ -z "$BINUTILS" ] && BINUTILS=2.23.52.0.2
[ -z "$GCC" ] && GCC=branches/linaro/gcc-4_8-branch/
[ -z "$GMP" ] && GMP=5.1.1
[ -z "$MPFR" ] && MPFR=3.1.1
[ -z "$MPC" ] && MPC=1.0.1
[ -z "$MAKE" ] && MAKE=3.82
[ -z "$NCURSES" ] && NCURSES=5.9
[ -z "$VIM" ] && VIM=7.3
# $ANDROID is irrelevant for in-tree builds
[ -z "$ANDROID" ] && ANDROID=4.1.2

# Everything below is needed only for rpm support
[ -z "$WITH_RPM" ] && WITH_RPM=false
[ -z "$DB" ] && DB=5.3.15
[ -z "$BEECRYPT" ] && BEECRYPT=4.2.1
[ -z "$POPT" ] && POPT=1.16
[ -z "$PCRE" ] && PCRE=8.31

# Installation location
if [ -z "$DEST" ]; then
	if which mktemp &>/dev/null; then
		DEST="`mktemp /tmp/android-native-toolchain.XXXXXX`"
	elif which id &>/dev/null; then
		DEST="/tmp/android-native-toolchain.`id -u`.$$"
	else
		DEST="/tmp/android-native-toolchain.$$"
	fi
fi
if [ -z "$HOSTDEST" ]; then
	if which mktemp &>/dev/null; then
		HOSTDEST="`mktemp /tmp/android-host-toolchain.XXXXXX`"
	elif which id &>/dev/null; then
		HOSTDEST="/tmp/android-host-toolchain.`id -u`.$$"
	else
		HOSTDEST="/tmp/android-host-toolchain.$$"
	fi
fi
rm -rf "$HOSTDEST"

# Parallel build flag passed to make
[ -z "$SMP" ] && SMP="-j`getconf _NPROCESSORS_ONLN`"

# Whether or not we're building inside an Android source tree
[ "$INTREE" != "true" ] && INTREE=false

# Set to true to keep Bionic. (This shouldn't be copied to the device
# because it's already there -- but it can be useful to do a sysroot-like
# install.)
KEEP_BIONIC=false

export TARGET_CFLAGS="$CFLAGS -Os -march=armv7-a"
export TARGET_CXXFLAGS="$CXXFLAGS -Os -march=armv7-a"

# Don't edit anything below unless you know exactly what you're doing.
set -e

export LC_ALL=C

DIR="$(readlink -f $(dirname $0))"
cd "$DIR"

rm -rf tc-wrapper
# Workaround for
#	1. toolchain not being properly sysrooted
#	2. gcc not making a difference between CPPFLAGS for build and host machine
mkdir tc-wrapper
if [ -z "$HOST_TOOLS" ]; then
	gcc -std=gnu99 -o tc-wrapper/arm-linux-androideabi-gcc tc-wrapper.c -DCCVERSION=\"4.8.1\" -DTCROOT=\"$HOSTDEST\" -DDESTDIR=\"$DEST\"
else
	HOSTGCCVER=$(${HOST_TOOLS}gcc -v 2>&1 |tail -n1 |sed -e 's,.* version ,,;s, .*,,')
	HOSTDEST=$(dirname $HOST_TOOLS)/..
	gcc -std=gnu99 -o tc-wrapper/arm-linux-androideabi-gcc tc-wrapper.c -DCCVERSION=\"$HOSTGCCVER\" -DTCROOT=\"$HOSTDEST\" -DDESTDIR=\"$DEST\"
fi
for i in cpp g++ c++; do
	ln -s arm-linux-androideabi-gcc tc-wrapper/arm-linux-androideabi-$i
done

SRC="$DIR/src"
[ -d src ] || mkdir src
cd src
if ! [ -d binutils ]; then
	git clone git://android.git.linaro.org/toolchain/binutils-current.git binutils
	cd binutils
	git checkout -b linaro-$BINUTILS origin/linaro-$BINUTILS
	cd ..
fi
if ! [ -d gmp ]; then
	git clone git://android.git.linaro.org/toolchain/gmp-current.git gmp
	cd gmp
	git checkout -b linaro-$GMP origin/linaro-$GMP
	aclocal
	automake -a
	autoconf
	cd ..
fi
if ! [ -d mpfr ]; then
	git clone git://android.git.linaro.org/toolchain/mpfr.git
	cd mpfr
	git checkout -b linaro-master origin/linaro-master
	cd mpfr-$MPFR
	aclocal -I m4
	automake -a
	autoconf
	cd ../..
fi
if ! [ -d mpc ]; then
	git clone git://android.git.linaro.org/toolchain/mpc-current.git mpc
	cd mpc
	git checkout -b linaro-$MPC origin/linaro-$MPC
	cd ..
fi
if ! [ -d gcc ]; then
	svn co svn://gcc.gnu.org/svn/gcc/$GCC gcc
	patch -p0 <"$DIR/gcc-4.7-android-workarounds.patch"
	patch -p0 <"$DIR/gcc-4.7-no-unneeded-multilib.patch"
	patch -p0 <"$DIR/gcc-4.7-stlport.patch"
	patch -p0 <"$DIR/gcc-4.8-android.patch"
	patch -p0 <"$DIR/gcc-4.8-workaround-android-build.patch"
fi
if ! [ -d make-$MAKE ]; then
	wget ftp://ftp.gnu.org/gnu/make/make-$MAKE.tar.bz2
	tar xf make-$MAKE.tar.bz2
	cd make-$MAKE
	patch -p1 <"$DIR/make-3.82-android-default-shell.patch"
	cd ..
fi
if ! [ -d ncurses-$NCURSES ]; then
	wget ftp://invisible-island.net/ncurses/ncurses-$NCURSES.tar.gz
	tar xf ncurses-$NCURSES.tar.gz
	cd ncurses-$NCURSES
	patch -p1 <"$DIR/ncurses-5.9-android.patch"
	cd ..
fi
if $INTREE; then
	BIONIC=../bionic
	STLPORT=../external/stlport
	ZLIB=../external/zlib
	cd mpfr/mpfr-$MPFR
	aclocal -I m4
	automake -a
	autoconf
	cd ../..
else
	BIONIC=src/android/bionic
	STLPORT=src/android/external/stlport
	ZLIB=src/android/external/zlib
	if ! [ -d android ]; then
		mkdir -p android
		cd android
		git clone git://android.git.linaro.org/platform/bionic.git
		cd bionic
		git checkout -b linaro_android_$ANDROID origin/linaro_android_$ANDROID
		cd ..
		git clone git://android.git.linaro.org/platform/build.git
		cd build
		git checkout -b linaro_android_$ANDROID origin/linaro_android_$ANDROID
		cd ..
		mkdir -p device/linaro
		cd device/linaro
		git clone git://android.git.linaro.org/device/linaro/common.git
		cd common
		git checkout -b linaro-ics origin/linaro-ics
		cd ..
		git clone git://android.git.linaro.org/device/linaro/pandaboard.git
		cd pandaboard
		git checkout -b linaro-jb origin/linaro-jb
		cd ..
		cd ../..
		mkdir frameworks
		cd frameworks
		git clone git://android.git.linaro.org/platform/frameworks/native.git
		cd native
		git checkout -b linaro_android_$ANDROID origin/linaro_android_$ANDROID
		cd ..
		cd ..
		mkdir -p hardware/ti
		cd hardware/ti
		git clone git://android.git.linaro.org/platform/hardware/ti/omap4xxx
		cd omap4xxx
		git checkout -b linaro_android_$ANDROID origin/linaro_android_$ANDROID
		cd ..
		cd ../..
		mkdir system
		cd system
		git clone git://android.git.linaro.org/platform/system/core.git
		cd core
		git checkout -b linaro_android_$ANDROID origin/linaro_android_$ANDROID
		cd ..
		cd ..
		mkdir external
		cd external
		git clone git://android.git.linaro.org/platform/external/stlport.git
		cd stlport
		git checkout -b linaro_android_$ANDROID origin/linaro_android_$ANDROID
		cd ..
		cd ..
		ln -sf build/core/root.mk Makefile
		cd ..
	fi
fi
if $WITH_RPM; then
	if ! [ -d db-$DB ]; then
		wget http://download.oracle.com/berkeley-db/db-$DB.tar.gz
		tar xf db-$DB.tar.gz
	fi
	if ! [ -d popt-$POPT ]; then
		wget http://rpm5.org/files/popt/popt-$POPT.tar.gz
		tar xf popt-$POPT.tar.gz
		# popt's config.sub doesn't know about androideabi
		cp -f /usr/share/automake-1.12/config.sub popt-$POPT/
	fi
	if ! [ -d beecrypt-$BEECRYPT ]; then
		wget http://downloads.sourceforge.net/project/beecrypt/beecrypt/$BEECRYPT/beecrypt-$BEECRYPT.tar.gz
		tar xf beecrypt-$BEECRYPT.tar.gz
		cd beecrypt-$BEECRYPT
		patch -p1 <"$DIR/beecrypt-4.2.1-compile.patch"
		patch -p1 <"$DIR/beecrypt-4.2.1-inline.patch"
		aclocal
		libtoolize --force
		automake -a
		autoconf
		# beecrypt's config.sub doesn't know about androideabi
		cp -f /usr/share/automake-1.13/config.sub . || cp -f /usr/share/automake-1.12/config.sub .
		cd ..
	fi
	if ! [ -d pcre-$PCRE ]; then
		wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-$PCRE.tar.bz2
		tar xf pcre-$PCRE.tar.bz2
	fi
	if ! [ -d rpm-5.4.10 ]; then
		svn co svn+ssh://svn.mandriva.com/svn/packages/cooker/rpm/current/SOURCES
		tar xf SOURCES/rpm-5.4.10.tar.gz
		cd rpm-5.4.10
		patch -p1 <"$DIR/rpm-5.4.10-android.patch"
		cd ..
	fi
	if ! $INTREE && ! [ -d android/external/zlib ]; then
		cd android/external
		git clone git://android.git.linaro.org/platform/external/zlib.git
		cd zlib
		git checkout -b $ANDROID android-${ANDROID}_r1
		sed -i -e 's,LOCAL_NDK,# LOCAL_NDK,;s,LOCAL_SDK,# LOCAL_SDK,' Android.mk
		cd ../../..
	fi
fi
VIMD=`echo $VIM |sed -e 's,\.,,'` # Directory name is actually vim73 for vim-7.3 etc.
if ! [ -d vim$VIMD ]; then
	wget ftp://ftp.vim.org/pub/vim/unix/vim-$VIM.tar.bz2
	tar xf vim-$VIM.tar.bz2
	cd vim$VIMD
	patch -p1 <"$DIR/vim-7.3-crosscompile.patch"
	cd ..
fi
cd ..

$INTREE || rm -rf "$DEST"
# FIXME this is a pretty awful hack to make sure gcc can find
# its headers even though it has been taught Android doesn't
# have system headers (no proper sysroot)
# At some point, we should build a properly sysrooted compiler
# even if AOSP doesn't.
#
# We can't just cp -a bionic/libc/include \
# 	bionic/libc/arch-arm-include ... \
#	"$TC"/lib/gcc/arm-linux-androideabi/*/include
# because some files in kernel/arch-arm are supposed to overwrite
# files from libc/include
mkdir -p "$DEST"/system/include
for i in libc/include libc/arch-arm/include libc/kernel/common libc/kernel/arch-arm libm/include; do
	cp -a $BIONIC/$i/* "$DEST"/system/include
done
mkdir -p "$DEST/system/include/libstdc++"
cp -a $BIONIC/libstdc++/include "$DEST"/system/include/libstdc++/
# We'll need stlport headers too, as we're disabling libstdc++ when
# building gcc
cp -a $STLPORT/stlport "$DEST"/system/include/
# Make them match the include directory structure we're building
sed -i -e 's,\.\./include/header,../header,g;s,usr/include,system/include,g' "$DEST"/system/include/stlport/stl/config/_android.h
# And don't insist on -DANDROID when gcc already defines __ANDROID__ for us
sed -i -e 's,defined (ANDROID),defined (ANDROID) || defined (__ANDROID__),g' "$DEST"/system/include/stlport/stl/config/_system.h

rm -rf build
mkdir build
cd build

export PATH="$DIR/tc-wrapper:$HOSTDEST/bin:$PATH"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$HOSTDEST/lib64:$HOSTDEST/lib"
if [ -z "$HOST_TOOLS" ]; then
	# First of all, build a cross-toolchain for the current host (properly sysrooted)
	rm -rf binutils-host
	mkdir -p binutils-host
	cd binutils-host
	$SRC/binutils/configure \
		--prefix="$HOSTDEST" \
		--target=arm-linux-androideabi \
		--enable-gold=default \
		--enable-shared \
		--disable-static \
		--disable-nls \
		--with-sysroot="$DEST"
	make $SMP
	make install
	cd ..

	rm -rf gmp-host
	mkdir -p gmp-host
	cd gmp-host
	$SRC/gmp/configure \
		--prefix="$HOSTDEST" \
		--disable-nls \
		--disable-static
	make $SMP
	make install
	rm -f "$HOSTDEST"/lib/*.la # libtool sucks, *.la files are harmful
	cd ..

	rm -rf mpfr-host
	mkdir -p mpfr-host
	cd mpfr-host
	$SRC/mpfr/mpfr-$MPFR/configure \
		--prefix="$HOSTDEST" \
		--disable-static \
		--with-sysroot="$DEST" \
		--with-gmp-include="$HOSTDEST"/include \
		--with-gmp-lib="$HOSTDEST"/lib
	make $SMP
	make install
	rm -f "$HOSTDEST"/lib/*.la # libtool sucks, *.la files are harmful
	cd ..

	rm -rf mpc-host
	mkdir -p mpc-host
	cd mpc-host
	# libtool rather sucks
	rm -f "$HOSTDEST"/lib/*.la
	$SRC/mpc/configure \
		--prefix="$HOSTDEST" \
		--disable-static \
		--with-gmp-include="$HOSTDEST"/include \
		--with-gmp-lib="$HOSTDEST"/lib \
		--with-mpfr-include="$HOSTDEST"/include \
		--with-mpfr-lib="$HOSTDEST"/lib
	make $SMP
	make install
	rm -f "$HOSTDEST"/lib/*.la # libtool sucks, *.la files are harmful
	cd ..

	# TODO build CLooG and friends for graphite

	mkdir -p gcc-host-bootstrap
	cd gcc-host-bootstrap
	$SRC/gcc/configure \
		--prefix="$HOSTDEST" \
		--target=arm-linux-androideabi \
		--enable-languages=c,c++ \
		--with-gnu-as \
		--with-gnu-ar \
		--with-gnu-ld \
		--disable-shared \
		--disable-libatomic \
		--disable-libsanitizer \
		--disable-libssp \
		--disable-libmudflap \
		--disable-libstdc__-v3 \
		--disable-libitm \
		--disable-nls \
		--disable-libquadmath \
		--disable-sjlj-exceptions \
		--disable-libgomp \
		--with-sysroot="$DEST" \
		--with-gmp="$HOSTDEST" \
		--with-mpfr="$HOSTDEST" \
		--with-mpc="$HOSTDEST" \
		--with-native-system-header-dir=/system/include
	make $SMP
	make install
	cd ..
fi

if $INTREE; then
	mkdir -p "$DEST"/system/lib
	cp $CRT/crt*.o "$DEST"/system/lib
else
	mkdir -p bionic
	cd bionic
	ONE_SHOT_MAKEFILE=build/libs/host/Android.mk make -C ../../src/android all_modules TARGET_TOOLS_PREFIX="$HOSTDEST"/bin/arm-linux-androideabi- TARGET_PRODUCT=pandaboard
	ONE_SHOT_MAKEFILE=build/tools/acp/Android.mk make -C ../../src/android all_modules TARGET_TOOLS_PREFIX="$HOSTDEST"/bin/arm-linux-androideabi- TARGET_PRODUCT=pandaboard
	ONE_SHOT_MAKEFILE=bionic/Android.mk make -C ../../src/android all_modules out/target/product/pandaboard/obj/lib/crtbegin_dynamic.o TARGET_TOOLS_PREFIX="$HOSTDEST"/bin/arm-linux-androideabi- TARGET_PRODUCT=pandaboard
	ONE_SHOT_MAKEFILE=external/stlport/Android.mk make -C ../../src/android all_modules TARGET_TOOLS_PREFIX="$HOSTDEST"/bin/arm-linux-androideabi- TARGET_PRODUCT=pandaboard
	mkdir -p "$DEST/system/lib"
	if $WITH_RPM; then
		ONE_SHOT_MAKEFILE=external/zlib/Android.mk make -C ../../src/android all_modules TARGET_TOOLS_PREFIX="$HOSTDEST"/bin/arm-linux-androideabi- TARGET_PRODUCT=pandaboard
		cp -L ../../src/android/external/zlib/*.h "$DEST"/system/include/
	fi
	cp ../../src/android/out/target/product/pandaboard/obj/lib/* "$DEST"/system/lib/
	cd ..
fi
if ! [ -d "$DEST"/system/lib/libpthread.a ]; then
	# Android's pthread bits are built into bionic libc -- but lots of traditional
	# Linux configure scripts just hardcode that there's a -lpthread...
	# Let's accomodate them
	touch dummy.c
	arm-linux-androideabi-gcc -O2 -o dummy.o -c dummy.c
	arm-linux-androideabi-ar cru "$DEST"/system/lib/libpthread.a dummy.o
	rm -f dummy.[co]
fi

export CFLAGS="$TARGET_CFLAGS"
export CXXFLAGS="$TARGET_CXXFLAGS"

echo "Relevant variables:"
echo "==================="
echo "export PATH=\"$PATH\""
echo "export CFLAGS=\"$CFLAGS\""
echo "export CXXFLAGS=\"$CXXFLAGS\""

mkdir -p binutils
cd binutils
# FIXME gold is disabled for now because it can't be built
# against stlport.
# Need to fix this, then --enable-gold=default
$SRC/binutils/configure \
	--prefix=/system \
	--target=arm-linux-androideabi \
	--host=arm-linux-androideabi \
	--enable-shared \
	--disable-static \
	--enable-gold=default \
	--disable-nls
make $SMP
make install DESTDIR="$DEST"
rm -f "$DEST"/system/lib/*.la # libtool sucks, *.la files are harmful
cd ..

rm -rf gmp
mkdir -p gmp
cd gmp
$SRC/gmp/configure \
	--prefix=/system \
	--disable-nls \
	--disable-static \
	--target=arm-linux-androideabi \
	--host=arm-linux-androideabi
make $SMP
make install DESTDIR="$DEST"
rm -f "$DEST"/system/lib/*.la # libtool sucks, *.la files are harmful
cd ..

rm -rf mpfr
mkdir -p mpfr
cd mpfr
$SRC/mpfr/mpfr-$MPFR/configure \
	--prefix=/system \
	--disable-static \
	--target=arm-linux-androideabi \
	--host=arm-linux-androideabi \
	--with-sysroot="$DEST" \
	--with-gmp-include="$DEST"/system/include \
	--with-gmp-lib="$DEST"/system/lib
make $SMP
make install DESTDIR="$DEST"
rm -f "$DEST"/system/lib/*.la # libtool sucks, *.la files are harmful
cd ..

rm -rf mpc
mkdir -p mpc
cd mpc
# libtool rather sucks
rm -f "$DEST"/system/lib/*.la
$SRC/mpc/configure \
	--prefix=/system \
	--disable-static \
	--target=arm-linux-androideabi \
	--host=arm-linux-androideabi
make $SMP
make install DESTDIR="$DEST"
rm -f "$DEST"/system/lib/*.la # libtool sucks, *.la files are harmful
cd ..

# TODO build CLooG and friends for graphite

export CXXFLAGS="-O2 -frtti"
rm -rf gcc
mkdir -p gcc
cd gcc
$SRC/gcc/configure \
	--prefix=/system \
	--target=arm-linux-androideabi \
	--host=arm-linux-androideabi \
	--enable-languages=c,c++ \
	--with-gnu-as \
	--with-gnu-ar \
	--with-gnu-ld \
	--disable-shared \
	--disable-libatomic \
	--disable-libsanitizer \
	--disable-libssp \
	--disable-libmudflap \
	--disable-libstdc__-v3 \
	--disable-libitm \
	--disable-nls \
	--disable-libquadmath \
	--disable-sjlj-exceptions
make $SMP
make install DESTDIR="$DEST"
cd ..

# Remove superfluous bits
rm -rf \
	"$DEST"/system/lib/gcc/arm-linux-androideabi/*/include-fixed \
	"$DEST"/system/share/gcc-*

# Merge gcc headers into the /system/include directory so stlport
# can make (invalid) assumptions about their locations
for i in "$DEST"/system/lib/gcc/arm-linux-androideabi/*/include/*.h; do
	[ -e "$DEST"/system/include/"`basename $i`" ] || mv $i "$DEST"/system/include/
done

# For compatibility with make defaults
ln -sf gcc "$DEST"/system/bin/cc

# Libtool sucks
rm -f "$DEST"/system/lib/*.la

rm -rf make
mkdir -p make
cd make
$SRC/make-$MAKE/configure \
	--prefix=/system \
	--target=arm-linux-androideabi \
	--host=arm-linux-androideabi
make $SMP
make install DESTDIR="$DEST"
cd ..

rm -rf ncurses
mkdir ncurses
cd ncurses
$SRC/ncurses-$NCURSES/configure \
	--prefix=/system \
	--target=arm-linux-androideabi \
	--host=arm-linux-androideabi \
	--enable-hard-tabs \
	--enable-const \
	--without-cxx-binding \
	--without-ada \
	--without-manpages \
	--with-shared
make $SMP
make install DESTDIR="$DEST"

# Get rid of components we don't need, we just need what we need to run vim
rm -rf \
	"$DEST"/system/lib/libform* \
	"$DEST"/system/lib/libmenu* \
	"$DEST"/system/lib/libpanel* \
	"$DEST"/system/lib/libncurses*.a \
	"$DEST"/system/bin/*captoinfo \
	"$DEST"/system/bin/*clear \
	"$DEST"/system/bin/*infocmp \
	"$DEST"/system/bin/*infotocap \
	"$DEST"/system/bin/*reset \
	"$DEST"/system/bin/*tabs \
	"$DEST"/system/bin/*tic \
	"$DEST"/system/bin/*toe \
	"$DEST"/system/bin/*tput \
	"$DEST"/system/bin/*tset

# Get rid of most terminfo files... We just want:
# screen -- used by Android Terminal Emulator and just generally useful
# linux, xterm and variants -- useful when ssh-ing in
# vt100 -- that's what we get from "adb shell"
rm -rf	"$DEST"/system/share/terminfo/[0-9]* \
	"$DEST"/system/share/terminfo/[a-k]* \
	"$DEST"/system/share/terminfo/l/l[a-h]* \
	"$DEST"/system/share/terminfo/l/li[a-m]* \
	"$DEST"/system/share/terminfo/l/li[o-z]* \
	"$DEST"/system/share/terminfo/l/l[j-z]* \
	"$DEST"/system/share/terminfo/[m-r]* \
	"$DEST"/system/share/terminfo/s/s[0-9]* \
	"$DEST"/system/share/terminfo/s/s[a-b]* \
	"$DEST"/system/share/terminfo/s/sc[0-9]* \
	"$DEST"/system/share/terminfo/s/sc[a-q]* \
	"$DEST"/system/share/terminfo/s/screwpoint \
	"$DEST"/system/share/terminfo/s/scrhp \
	"$DEST"/system/share/terminfo/s/s[d-z]* \
	"$DEST"/system/share/terminfo/[t-u]* \
	"$DEST"/system/share/terminfo/v/v[0-9]* \
	"$DEST"/system/share/terminfo/v/v[a-s]* \
	"$DEST"/system/share/terminfo/v/vt100-am \
	"$DEST"/system/share/terminfo/v/vt102* \
	"$DEST"/system/share/terminfo/v/vt1[1-9]* \
	"$DEST"/system/share/terminfo/v/vt[2-9]* \
	"$DEST"/system/share/terminfo/v/vt[a-z]* \
	"$DEST"/system/share/terminfo/v/vt-* \
	"$DEST"/system/share/terminfo/v/v[u-z]* \
	"$DEST"/system/share/terminfo/w* \
	"$DEST"/system/share/terminfo/x/x[0-9]* \
	"$DEST"/system/share/terminfo/x/x[a-s]* \
	"$DEST"/system/share/terminfo/x/xtalk* \
	"$DEST"/system/share/terminfo/x/x[u-z]* \
	"$DEST"/system/share/terminfo/[y-z]* \
	"$DEST"/system/share/terminfo/[A-Z]*
cd ..

rm -rf vim
# vim doesn't currently support out-of-source builds
cp -a $SRC/vim$VIMD vim
cd vim
vim_cv_toupper_broken=no vim_cv_terminfo=yes vim_cv_tgent=zero \
vim_cv_stat_ignores_slash=no vim_cv_tty_group=system vim_cv_tty_mode=0666 vim_cv_getcwd_broken=no \
vim_cv_memmove_handles_overlap=yes \
	./configure \
		--prefix=/system \
		--target=arm-linux-androideabi \
		--host=arm-linux-androideabi \
		--disable-selinux
make $SMP STRIP="$HOSTDEST"/bin/arm-linux-androideabi-strip
make install DESTDIR="$DEST" STRIP="$HOSTDEST"/bin/arm-linux-androideabi-strip
ln -sf vim "$DEST"/system/bin/vi
cd ..

# save space (from vim)
rm -rf \
	"$DEST"/system/share/vim/vim$VIMD/doc \
	"$DEST"/system/share/vim/vim$VIMD/tutor \
	"$DEST"/system/share/vim/vim$VIMD/print \
	"$DEST"/system/bin/vimtutor \
	"$DEST"/system/bin/vimdiff \
	"$DEST"/system/bin/view \
	"$DEST"/system/bin/rview \
	"$DEST"/system/bin/rvim

pushd "$DEST"/system/share/vim/vim$VIMD/syntax
for i in *; do
	[ "$i" = "config.vim" ] || \
	[ "$i" = "conf.vim" ] || \
	[ "$i" = "cpp.vim" ] || \
	[ "$i" = "c.vim" ] || \
	[ "$i" = "doxygen.vim" ] || \
	[ "$i" = "html.vim" ] || \
	[ "$i" = "javascript.vim" ] || \
	[ "$i" = "java.vim" ] || \
	[ "$i" = "manual.vim" ] || \
	[ "$i" = "sh.vim" ] || \
	[ "$i" = "syncolor.vim" ] || \
	[ "$i" = "synload.vim" ] || \
	[ "$i" = "syntax.vim" ] || \
	[ "$i" = "vim.vim" ] || \
		rm -f "$i"
done
popd

# Set some nice defaults
mv "$DEST"/system/share/vim/vim$VIMD/vimrc_example.vim "$DEST"/system/share/vim/vimrc

if $WITH_RPM; then
	mkdir db
	cd db
	../../src/db-$DB/dist/configure --prefix=/system --host=arm-linux-androideabi --target=arm-linux-androideabi --enable-shared --enable-posixmutexes --with-mutex=ARM/gcc-assembly
	make $SMP
	make install DESTDIR="$DEST"
	cd ..

	mkdir beecrypt
	cd beecrypt
	../../src/beecrypt-$BEECRYPT/configure --prefix=/system --host=arm-linux-androideabi --target=arm-linux-androideabi --without-cplusplus --without-java --without-python --with-gnu-ld
	make install DESTDIR="$DEST"
	cd ..

	mkdir popt
	cd popt
	../../src/popt-$POPT/configure --prefix=/system --host=arm-linux-androideabi --target=arm-linux-androideabi
	make $SMP
	make install DESTDIR="$DEST"
	cd ..

	mkdir pcre
	cd pcre
	../../src/pcre-$PCRE/configure --prefix=/system --host=arm-linux-androideabi --target=arm-linux-androideabi
	make $SMP
	make install DESTDIR="$DEST"
	cd ..

	mkdir rpm
	cd rpm
	# --without-pthreads isn't nice... But rpm uses pthread_cancel
	ac_cv_va_copy=C99 ../../src/rpm-5.4.10/configure --prefix=/system --host=arm-linux-androideabi --target=arm-linux-androideabi --disable-nls --enable-posixmutexes --without-python --without-perl --without-mozjs185 --with-glob --without-selinux --without-augeas --without-pthreads
	# rpm defaults to bison -y -- but bison 2.7 generates a duplicate
	# definition of yylval on getdate.y
	make $SMP YACC="byacc"
	make install DESTDIR="$DEST"
	cd ..
fi

# Save space (from stuff accumulated by all projects)
rm -rf \
	"$DEST"/system/share/doc \
	"$DEST"/system/share/info \
	"$DEST"/system/share/man

if ! $KEEP_BIONIC && ! $INTREE; then
	# Get rid of Bionic and stlport -- they're already included in Android
	rm -rf \
		"$DEST"/system/lib/libc.so \
		"$DEST"/system/lib/libstdc++.so \
		"$DEST"/system/lib/libstlport.so \
		"$DEST"/system/lib/libm.so \
		"$DEST"/system/lib/libthread_db.so \
		"$DEST"/system/lib/libc_malloc*.so \
		"$DEST"/system/lib/libdl.so
fi

if ! $INTREE; then
	# strip everything so we can fit into the limited
	# /system space on GNexus
	# set +e because the strip command will fail, given it will also get
	# to "strip" non-binaries.
	set +e
	find "$DEST" |xargs "$HOSTDEST"/bin/arm-linux-androideabi-strip --strip-unneeded
fi
[ "$DEST" != "$HOSTDEST" ] && [ -z "$HOST_TOOLS" ] && rm -rf "$HOSTDEST"
echo
echo Toolchain build successful.
echo The native toolchain can be found in $DEST.