forked from termux/termux-packages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-package.sh
executable file
·1544 lines (1377 loc) · 57.8 KB
/
build-package.sh
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# shellcheck disable=SC1117
set -e -o pipefail -u
# Utility function to log an error message and exit with an error code.
termux_error_exit() {
echo "ERROR: $*" 1>&2
exit 1
}
if [ "$(uname -o)" = Android ]; then
termux_error_exit "On-device builds are not supported - see README.md"
fi
# Utility function to download a resource with an expected checksum.
termux_download() {
if [ $# != 3 ]; then
termux_error_exit "termux_download(): Invalid arguments - expected \$URL \$DESTINATION \$CHECKSUM"
fi
local URL="$1"
local DESTINATION="$2"
local CHECKSUM="$3"
if [ -f "$DESTINATION" ] && [ "$CHECKSUM" != "SKIP_CHECKSUM" ]; then
# Keep existing file if checksum matches.
local EXISTING_CHECKSUM
EXISTING_CHECKSUM=$(sha256sum "$DESTINATION" | cut -f 1 -d ' ')
if [ "$EXISTING_CHECKSUM" = "$CHECKSUM" ]; then return; fi
fi
local TMPFILE
TMPFILE=$(mktemp "$TERMUX_PKG_TMPDIR/download.$TERMUX_PKG_NAME.XXXXXXXXX")
echo "Downloading ${URL}"
local TRYMAX=6
for try in $(seq 1 $TRYMAX); do
if curl -L --fail --retry 2 -o "$TMPFILE" "$URL"; then
local ACTUAL_CHECKSUM
ACTUAL_CHECKSUM=$(sha256sum "$TMPFILE" | cut -f 1 -d ' ')
if [ "$CHECKSUM" != "SKIP_CHECKSUM" ]; then
if [ "$CHECKSUM" != "$ACTUAL_CHECKSUM" ]; then
>&2 printf "Wrong checksum for %s:\nExpected: %s\nActual: %s\n" \
"$URL" "$CHECKSUM" "$ACTUAL_CHECKSUM"
exit 1
fi
else
printf "WARNING: No checksum check for %s:\nActual: %s\n" \
"$URL" "$ACTUAL_CHECKSUM"
fi
mv "$TMPFILE" "$DESTINATION"
return
else
echo "Download of $URL failed (attempt $try/$TRYMAX)" 1>&2
sleep 45
fi
done
termux_error_exit "Failed to download $URL"
}
# Utility function for golang-using packages to setup a go toolchain.
termux_setup_golang() {
export GOOS=android
export CGO_ENABLED=1
export GO_LDFLAGS="-extldflags=-pie"
if [ "$TERMUX_ARCH" = "arm" ]; then
export GOARCH=arm
export GOARM=7
elif [ "$TERMUX_ARCH" = "i686" ]; then
export GOARCH=386
export GO386=sse2
elif [ "$TERMUX_ARCH" = "aarch64" ]; then
export GOARCH=arm64
elif [ "$TERMUX_ARCH" = "x86_64" ]; then
export GOARCH=amd64
else
termux_error_exit "Unsupported arch: $TERMUX_ARCH"
fi
local TERMUX_GO_VERSION=go1.11.5
local TERMUX_GO_PLATFORM=linux-amd64
local TERMUX_BUILDGO_FOLDER=$TERMUX_COMMON_CACHEDIR/${TERMUX_GO_VERSION}
export GOROOT=$TERMUX_BUILDGO_FOLDER
export PATH=$GOROOT/bin:$PATH
if [ -d "$TERMUX_BUILDGO_FOLDER" ]; then return; fi
local TERMUX_BUILDGO_TAR=$TERMUX_COMMON_CACHEDIR/${TERMUX_GO_VERSION}.${TERMUX_GO_PLATFORM}.tar.gz
rm -Rf "$TERMUX_COMMON_CACHEDIR/go" "$TERMUX_BUILDGO_FOLDER"
termux_download https://storage.googleapis.com/golang/${TERMUX_GO_VERSION}.${TERMUX_GO_PLATFORM}.tar.gz \
"$TERMUX_BUILDGO_TAR" \
ff54aafedff961eb94792487e827515da683d61a5f9482f668008832631e5d25
( cd "$TERMUX_COMMON_CACHEDIR"; tar xf "$TERMUX_BUILDGO_TAR"; mv go "$TERMUX_BUILDGO_FOLDER"; rm "$TERMUX_BUILDGO_TAR" )
}
# Utility function for rust-using packages to setup a rust toolchain.
termux_setup_rust() {
if [ $TERMUX_ARCH = "arm" ]; then
CARGO_TARGET_NAME=armv7-linux-androideabi
else
CARGO_TARGET_NAME=$TERMUX_ARCH-linux-android
fi
local ENV_NAME=CARGO_TARGET_${CARGO_TARGET_NAME^^}_LINKER
ENV_NAME=${ENV_NAME//-/_}
export $ENV_NAME=$CC
curl https://sh.rustup.rs -sSf > $TERMUX_PKG_TMPDIR/rustup.sh
sh $TERMUX_PKG_TMPDIR/rustup.sh -y
export PATH=$HOME/.cargo/bin:$PATH
rustup target add $CARGO_TARGET_NAME
}
# Utility function to setup a current ninja build system.
termux_setup_ninja() {
local NINJA_VERSION=1.9.0
local NINJA_FOLDER=$TERMUX_COMMON_CACHEDIR/ninja-$NINJA_VERSION
if [ ! -x "$NINJA_FOLDER/ninja" ]; then
mkdir -p "$NINJA_FOLDER"
local NINJA_ZIP_FILE=$TERMUX_PKG_TMPDIR/ninja-$NINJA_VERSION.zip
termux_download https://github.com/ninja-build/ninja/releases/download/v$NINJA_VERSION/ninja-linux.zip \
"$NINJA_ZIP_FILE" \
609cc10d0f226a4d9050e4d4a57be9ea706858cce64b9132102c3789c868da92
unzip "$NINJA_ZIP_FILE" -d "$NINJA_FOLDER"
chmod 755 $NINJA_FOLDER/ninja
fi
export PATH=$NINJA_FOLDER:$PATH
}
# Utility function to setup a current meson build system.
termux_setup_meson() {
termux_setup_ninja
local MESON_VERSION=0.49.0
local MESON_FOLDER=$TERMUX_COMMON_CACHEDIR/meson-$MESON_VERSION-v1
if [ ! -d "$MESON_FOLDER" ]; then
local MESON_TAR_NAME=meson-$MESON_VERSION.tar.gz
local MESON_TAR_FILE=$TERMUX_PKG_TMPDIR/$MESON_TAR_NAME
local MESON_TMP_FOLDER=$TERMUX_PKG_TMPDIR/meson-$MESON_VERSION
termux_download \
"https://github.com/mesonbuild/meson/releases/download/$MESON_VERSION/meson-$MESON_VERSION.tar.gz" \
"$MESON_TAR_FILE" \
fb0395c4ac208eab381cd1a20571584bdbba176eb562a7efa9cb17cace0e1551
tar xf "$MESON_TAR_FILE" -C "$TERMUX_PKG_TMPDIR"
mv "$MESON_TMP_FOLDER" "$MESON_FOLDER"
fi
TERMUX_MESON="$MESON_FOLDER/meson.py"
TERMUX_MESON_CROSSFILE=$TERMUX_PKG_TMPDIR/meson-crossfile-$TERMUX_ARCH.txt
local MESON_CPU MESON_CPU_FAMILY
if [ "$TERMUX_ARCH" = "arm" ]; then
MESON_CPU_FAMILY="arm"
MESON_CPU="armv7"
elif [ "$TERMUX_ARCH" = "i686" ]; then
MESON_CPU_FAMILY="x86"
MESON_CPU="i686"
elif [ "$TERMUX_ARCH" = "x86_64" ]; then
MESON_CPU_FAMILY="x86_64"
MESON_CPU="x86_64"
elif [ "$TERMUX_ARCH" = "aarch64" ]; then
MESON_CPU_FAMILY="arm"
MESON_CPU="aarch64"
else
termux_error_exit "Unsupported arch: $TERMUX_ARCH"
fi
local CONTENT=""
echo "[binaries]" > $TERMUX_MESON_CROSSFILE
echo "ar = '$AR'" >> $TERMUX_MESON_CROSSFILE
echo "c = '$CC'" >> $TERMUX_MESON_CROSSFILE
echo "cpp = '$CXX'" >> $TERMUX_MESON_CROSSFILE
echo "ld = '$LD'" >> $TERMUX_MESON_CROSSFILE
echo "pkgconfig = '$PKG_CONFIG'" >> $TERMUX_MESON_CROSSFILE
echo "strip = '$STRIP'" >> $TERMUX_MESON_CROSSFILE
echo '' >> $TERMUX_MESON_CROSSFILE
echo "[properties]" >> $TERMUX_MESON_CROSSFILE
echo "needs_exe_wrapper = true" >> $TERMUX_MESON_CROSSFILE
echo -n "c_args = [" >> $TERMUX_MESON_CROSSFILE
local word first=true
for word in $CFLAGS $CPPFLAGS; do
if [ "$first" = "true" ]; then
first=false
else
echo -n ", " >> $TERMUX_MESON_CROSSFILE
fi
echo -n "'$word'" >> $TERMUX_MESON_CROSSFILE
done
echo ']' >> $TERMUX_MESON_CROSSFILE
echo -n "cpp_args = [" >> $TERMUX_MESON_CROSSFILE
local word first=true
for word in $CXXFLAGS $CPPFLAGS; do
if [ "$first" = "true" ]; then
first=false
else
echo -n ", " >> $TERMUX_MESON_CROSSFILE
fi
echo -n "'$word'" >> $TERMUX_MESON_CROSSFILE
done
echo ']' >> $TERMUX_MESON_CROSSFILE
local property
for property in c_link_args cpp_link_args; do
echo -n "$property = [" >> $TERMUX_MESON_CROSSFILE
first=true
for word in $LDFLAGS; do
if [ "$first" = "true" ]; then
first=false
else
echo -n ", " >> $TERMUX_MESON_CROSSFILE
fi
echo -n "'$word'" >> $TERMUX_MESON_CROSSFILE
done
echo ']' >> $TERMUX_MESON_CROSSFILE
done
echo '' >> $TERMUX_MESON_CROSSFILE
echo "[host_machine]" >> $TERMUX_MESON_CROSSFILE
echo "cpu_family = '$MESON_CPU_FAMILY'" >> $TERMUX_MESON_CROSSFILE
echo "cpu = '$MESON_CPU'" >> $TERMUX_MESON_CROSSFILE
echo "endian = 'little'" >> $TERMUX_MESON_CROSSFILE
echo "system = 'android'" >> $TERMUX_MESON_CROSSFILE
}
# Utility function to setup a current cmake build system
termux_setup_cmake() {
local TERMUX_CMAKE_MAJORVESION=3.13
local TERMUX_CMAKE_MINORVERSION=4
local TERMUX_CMAKE_VERSION=$TERMUX_CMAKE_MAJORVESION.$TERMUX_CMAKE_MINORVERSION
local TERMUX_CMAKE_TARNAME=cmake-${TERMUX_CMAKE_VERSION}-Linux-x86_64.tar.gz
local TERMUX_CMAKE_TARFILE=$TERMUX_PKG_TMPDIR/$TERMUX_CMAKE_TARNAME
local TERMUX_CMAKE_FOLDER=$TERMUX_COMMON_CACHEDIR/cmake-$TERMUX_CMAKE_VERSION
if [ ! -d "$TERMUX_CMAKE_FOLDER" ]; then
termux_download https://cmake.org/files/v$TERMUX_CMAKE_MAJORVESION/$TERMUX_CMAKE_TARNAME \
"$TERMUX_CMAKE_TARFILE" \
563a39e0a7c7368f81bfa1c3aff8b590a0617cdfe51177ddc808f66cc0866c76
rm -Rf "$TERMUX_PKG_TMPDIR/cmake-${TERMUX_CMAKE_VERSION}-Linux-x86_64"
tar xf "$TERMUX_CMAKE_TARFILE" -C "$TERMUX_PKG_TMPDIR"
mv "$TERMUX_PKG_TMPDIR/cmake-${TERMUX_CMAKE_VERSION}-Linux-x86_64" \
"$TERMUX_CMAKE_FOLDER"
fi
export PATH=$TERMUX_CMAKE_FOLDER/bin:$PATH
export CMAKE_INSTALL_ALWAYS=1
}
# First step is to handle command-line arguments. Not to be overridden by packages.
termux_step_handle_arguments() {
_show_usage() {
echo "Usage: ./build-package.sh [-a ARCH] [-d] [-D] [-f] [-i] [-I] [-q] [-s] [-o DIR] PACKAGE"
echo "Build a package by creating a .deb file in the debs/ folder."
echo " -a The architecture to build for: aarch64(default), arm, i686, x86_64 or all."
echo " -d Build with debug symbols."
echo " -D Build a disabled package in disabled-packages/."
echo " -f Force build even if package has already been built."
echo " -i Download and extract dependencies instead of building them."
echo " -I Download and extract dependencies instead of building them, keep existing /data/data/com.termux files."
echo " -q Quiet build."
echo " -s Skip dependency check."
echo " -o Specify deb directory. Default: debs/."
exit 1
}
while getopts :a:hdDfiIqso: option; do
case "$option" in
a) TERMUX_ARCH="$OPTARG";;
h) _show_usage;;
d) export TERMUX_DEBUG=true;;
D) local TERMUX_IS_DISABLED=true;;
f) TERMUX_FORCE_BUILD=true;;
i) export TERMUX_INSTALL_DEPS=true;;
I) export TERMUX_INSTALL_DEPS=true && export TERMUX_NO_CLEAN=true;;
q) export TERMUX_QUIET_BUILD=true;;
s) export TERMUX_SKIP_DEPCHECK=true;;
o) TERMUX_DEBDIR="$(realpath -m $OPTARG)";;
?) termux_error_exit "./build-package.sh: illegal option -$OPTARG";;
esac
done
shift $((OPTIND-1))
if [ "$#" -ne 1 ]; then _show_usage; fi
unset -f _show_usage
# Handle 'all' arch:
if [ -n "${TERMUX_ARCH+x}" ] && [ "${TERMUX_ARCH}" = 'all' ]; then
for arch in 'aarch64' 'arm' 'i686' 'x86_64'; do
./build-package.sh ${TERMUX_FORCE_BUILD+-f} -a $arch ${TERMUX_INSTALL_DEPS+-i} \
${TERMUX_DEBUG+-d} ${TERMUX_DEBDIR+-o $TERMUX_DEBDIR} "$1"
done
exit
fi
# Check the package to build:
TERMUX_PKG_NAME=$(basename "$1")
export TERMUX_SCRIPTDIR
TERMUX_SCRIPTDIR=$(cd "$(dirname "$0")"; pwd)
if [[ $1 == *"/"* ]]; then
# Path to directory which may be outside this repo:
if [ ! -d "$1" ]; then termux_error_exit "'$1' seems to be a path but is not a directory"; fi
export TERMUX_PKG_BUILDER_DIR
TERMUX_PKG_BUILDER_DIR=$(realpath "$1")
else
# Package name:
if [ -n "${TERMUX_IS_DISABLED=""}" ]; then
export TERMUX_PKG_BUILDER_DIR=$TERMUX_SCRIPTDIR/disabled-packages/$TERMUX_PKG_NAME
else
export TERMUX_PKG_BUILDER_DIR=$TERMUX_SCRIPTDIR/packages/$TERMUX_PKG_NAME
fi
fi
TERMUX_PKG_BUILDER_SCRIPT=$TERMUX_PKG_BUILDER_DIR/build.sh
if test ! -f "$TERMUX_PKG_BUILDER_SCRIPT"; then
termux_error_exit "No build.sh script at package dir $TERMUX_PKG_BUILDER_DIR!"
fi
}
# Setup variables used by the build. Not to be overridden by packages.
termux_step_setup_variables() {
# shellcheck source=scripts/properties.sh
. "$TERMUX_SCRIPTDIR/scripts/properties.sh"
: "${TERMUX_MAKE_PROCESSES:="$(nproc)"}"
: "${TERMUX_TOPDIR:="$HOME/.termux-build"}"
: "${TERMUX_ARCH:="aarch64"}" # arm, aarch64, i686 or x86_64.
: "${TERMUX_PREFIX:="/data/data/com.termux/files/usr"}"
: "${TERMUX_ANDROID_HOME:="/data/data/com.termux/files/home"}"
: "${TERMUX_DEBUG:=""}"
: "${TERMUX_PKG_API_LEVEL:="21"}"
: "${TERMUX_NO_CLEAN:="false"}"
: "${TERMUX_QUIET_BUILD:="false"}"
: "${TERMUX_DEBDIR:="${TERMUX_SCRIPTDIR}/debs"}"
: "${TERMUX_SKIP_DEPCHECK:="false"}"
: "${TERMUX_INSTALL_DEPS:="false"}"
: "${TERMUX_REPO_SIGNING_KEYS:="packages/apt/trusted.gpg packages/termux-keyring/grimler.gpg packages/termux-keyring/xeffyr.gpg"}"
: "${TERMUX_PKG_MAINTAINER:="Fredrik Fornwall @fornwall"}"
if [ -z ${TERMUX_REPO_URL+x} ]; then
TERMUX_REPO_URL=(https://termux.net)
# TERMUX_REPO_URL=(https://termux.net https://grimler.se https://dl.bintray.com/xeffyr/unstable-packages)
fi
if [ -z ${TERMUX_REPO_DISTRIBUTION+x} ]; then
TERMUX_REPO_DISTRIBUTION=(stable)
# TERMUX_REPO_DISTRIBUTION=(stable root unstable)
fi
if [ -z ${TERMUX_REPO_COMPONENT+x} ]; then
TERMUX_REPO_COMPONENT=(main)
# TERMUX_REPO_COMPONENT=(main stable main)
fi
if [ "x86_64" = "$TERMUX_ARCH" ] || [ "aarch64" = "$TERMUX_ARCH" ]; then
TERMUX_ARCH_BITS=64
else
TERMUX_ARCH_BITS=32
fi
TERMUX_HOST_PLATFORM="${TERMUX_ARCH}-linux-android"
if [ "$TERMUX_ARCH" = "arm" ]; then TERMUX_HOST_PLATFORM="${TERMUX_HOST_PLATFORM}eabi"; fi
if [ ! -d "$NDK" ]; then
termux_error_exit 'NDK not pointing at a directory!'
fi
if ! grep -s -q "Pkg.Revision = $TERMUX_NDK_VERSION_NUM" "$NDK/source.properties"; then
termux_error_exit "Wrong NDK version - we need $TERMUX_NDK_VERSION"
fi
# The build tuple that may be given to --build configure flag:
TERMUX_BUILD_TUPLE=$(sh "$TERMUX_SCRIPTDIR/scripts/config.guess")
# We do not put all of build-tools/$TERMUX_ANDROID_BUILD_TOOLS_VERSION/ into PATH
# to avoid stuff like arm-linux-androideabi-ld there to conflict with ones from
# the standalone toolchain.
TERMUX_D8=$ANDROID_HOME/build-tools/$TERMUX_ANDROID_BUILD_TOOLS_VERSION/d8
TERMUX_COMMON_CACHEDIR="$TERMUX_TOPDIR/_cache"
TERMUX_ELF_CLEANER=$TERMUX_COMMON_CACHEDIR/termux-elf-cleaner
export prefix=${TERMUX_PREFIX}
export PREFIX=${TERMUX_PREFIX}
TERMUX_PKG_BUILDDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/build
TERMUX_PKG_CACHEDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/cache
TERMUX_PKG_MASSAGEDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/massage
TERMUX_PKG_PACKAGEDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/package
TERMUX_PKG_SRCDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/src
TERMUX_PKG_SHA256=""
TERMUX_PKG_TMPDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/tmp
TERMUX_PKG_HOSTBUILD_DIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/host-build
TERMUX_PKG_PLATFORM_INDEPENDENT=""
TERMUX_PKG_NO_DEVELSPLIT=""
TERMUX_PKG_REVISION="0" # http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version
TERMUX_PKG_EXTRA_CONFIGURE_ARGS=""
TERMUX_PKG_EXTRA_HOSTBUILD_CONFIGURE_ARGS=""
TERMUX_PKG_EXTRA_MAKE_ARGS=""
TERMUX_PKG_BUILD_IN_SRC=""
TERMUX_PKG_RM_AFTER_INSTALL=""
TERMUX_PKG_BREAKS="" # https://www.debian.org/doc/debian-policy/ch-relationships.html#s-binarydeps
TERMUX_PKG_DEPENDS=""
TERMUX_PKG_BUILD_DEPENDS=""
TERMUX_PKG_HOMEPAGE=""
TERMUX_PKG_DESCRIPTION="FIXME:Add description"
TERMUX_PKG_KEEP_STATIC_LIBRARIES="false"
TERMUX_PKG_ESSENTIAL=""
TERMUX_PKG_CONFLICTS="" # https://www.debian.org/doc/debian-policy/ch-relationships.html#s-conflicts
TERMUX_PKG_RECOMMENDS="" # https://www.debian.org/doc/debian-policy/ch-relationships.html#s-binarydeps
TERMUX_PKG_SUGGESTS=""
TERMUX_PKG_REPLACES=""
TERMUX_PKG_PROVIDES="" #https://www.debian.org/doc/debian-policy/#virtual-packages-provides
TERMUX_PKG_CONFFILES=""
TERMUX_PKG_INCLUDE_IN_DEVPACKAGE=""
TERMUX_PKG_DEVPACKAGE_DEPENDS=""
# Set if a host build should be done in TERMUX_PKG_HOSTBUILD_DIR:
TERMUX_PKG_HOSTBUILD=""
TERMUX_PKG_FORCE_CMAKE=no # if the package has autotools as well as cmake, then set this to prefer cmake
TERMUX_CMAKE_BUILD=Ninja # Which cmake generator to use
TERMUX_PKG_HAS_DEBUG=yes # set to no if debug build doesn't exist or doesn't work, for example for python based packages
unset CFLAGS CPPFLAGS LDFLAGS CXXFLAGS
}
# Save away and restore build setups which may change between builds.
termux_step_handle_buildarch() {
# If $TERMUX_PREFIX already exists, it may have been built for a different arch
local TERMUX_ARCH_FILE=/data/TERMUX_ARCH
if [ -f "${TERMUX_ARCH_FILE}" ]; then
local TERMUX_PREVIOUS_ARCH
TERMUX_PREVIOUS_ARCH=$(cat $TERMUX_ARCH_FILE)
if [ "$TERMUX_PREVIOUS_ARCH" != "$TERMUX_ARCH" ]; then
local TERMUX_DATA_BACKUPDIRS=$TERMUX_TOPDIR/_databackups
mkdir -p "$TERMUX_DATA_BACKUPDIRS"
local TERMUX_DATA_PREVIOUS_BACKUPDIR=$TERMUX_DATA_BACKUPDIRS/$TERMUX_PREVIOUS_ARCH
local TERMUX_DATA_CURRENT_BACKUPDIR=$TERMUX_DATA_BACKUPDIRS/$TERMUX_ARCH
# Save current /data (removing old backup if any)
if test -e "$TERMUX_DATA_PREVIOUS_BACKUPDIR"; then
termux_error_exit "Directory already exists"
fi
if [ -d /data/data ]; then
mv /data/data "$TERMUX_DATA_PREVIOUS_BACKUPDIR"
fi
# Restore new one (if any)
if [ -d "$TERMUX_DATA_CURRENT_BACKUPDIR" ]; then
mv "$TERMUX_DATA_CURRENT_BACKUPDIR" /data/data
fi
fi
fi
# Keep track of current arch we are building for.
echo "$TERMUX_ARCH" > $TERMUX_ARCH_FILE
}
# Function to get TERMUX_PKG_VERSION from build.sh
source scripts/termux_extract_dep_info.sh
termux_download_deb() {
local package=$1
local package_arch=$2
local version=$3
local deb_file=${package}_${version}_${package_arch}.deb
for idx in $(seq ${#TERMUX_REPO_URL[@]}); do
local TERMUX_REPO_NAME=$(echo ${TERMUX_REPO_URL[$idx-1]} | sed -e 's%https://%%g' -e 's%http://%%g' -e 's%/%-%g')
local PACKAGE_FILE_PATH="${TERMUX_REPO_NAME}-${TERMUX_REPO_DISTRIBUTION[$idx-1]}-${TERMUX_REPO_COMPONENT[$idx-1]}-Packages"
read -d "\n" PKG_PATH PKG_HASH <<<$(./scripts/get_hash_from_file.py "${TERMUX_COMMON_CACHEDIR}-$arch/$PACKAGE_FILE_PATH" $package $version)
if ! [ "$PKG_HASH" = "" ]; then
if [ ! "$TERMUX_QUIET_BUILD" = true ]; then
echo "Found $package in ${TERMUX_REPO_URL[$idx-1]}/dists/${TERMUX_REPO_DISTRIBUTION[$idx-1]}"
fi
break
fi
done
if [ "$PKG_HASH" = "" ]; then
return 1
else
termux_download ${TERMUX_REPO_URL[$idx-1]}/${PKG_PATH} \
$TERMUX_COMMON_CACHEDIR-$package_arch/${deb_file} \
$PKG_HASH
return 0
fi
}
# Script to download InRelease, verify it's signature and then download Packages.xz by hash
termux_step_get_repo_files() {
# Ensure folders present (but not $TERMUX_PKG_SRCDIR, it will be created in build)
mkdir -p "$TERMUX_COMMON_CACHEDIR" \
"$TERMUX_COMMON_CACHEDIR-$TERMUX_ARCH" \
"$TERMUX_COMMON_CACHEDIR-all" \
"$TERMUX_DEBDIR" \
"$TERMUX_PKG_BUILDDIR" \
"$TERMUX_PKG_PACKAGEDIR" \
"$TERMUX_PKG_TMPDIR" \
"$TERMUX_PKG_CACHEDIR" \
"$TERMUX_PKG_MASSAGEDIR" \
$TERMUX_PREFIX/{bin,etc,lib,libexec,share,tmp,include}
if [ "$TERMUX_INSTALL_DEPS" = true ]; then
if [ "$TERMUX_NO_CLEAN" = false ]; then
# Remove all previously extracted/built files from $TERMUX_PREFIX:
rm -rf $TERMUX_PREFIX
rm -f /data/data/.built-packages/*
# Setup bootstrap
if [ $TERMUX_ARCH == aarch64 ]; then
local bootstrap_sha256=2944ad699814329007d1f9c056e7c8323243c8b4a257cbd05904216f89fc3746
elif [ $TERMUX_ARCH == i686 ]; then
local bootstrap_sha256=8f4dee0b1e161689b60f330ac0cc813b56ab479f2cd789eb8459165a3be13bdb
elif [ $TERMUX_ARCH == arm ]; then
local bootstrap_sha256=f471c0af326677d87ca4926d54860d10d751dd4f8d615d5b1de902841601b41e
elif [ $TERMUX_ARCH == x86_64 ]; then
local bootstrap_sha256=93384f0343c13f604dbacd069276291bd7042fc6d42c6d7514c7e573d968c614
fi
termux_download https://termux.net/bootstrap/bootstrap-${TERMUX_ARCH}.zip \
${TERMUX_COMMON_CACHEDIR}/bootstrap-${TERMUX_ARCH}.zip \
$bootstrap_sha256
unzip -qo ${TERMUX_COMMON_CACHEDIR}/bootstrap-${TERMUX_ARCH}.zip -d $TERMUX_PREFIX
(
cd $TERMUX_PREFIX
while read link; do
ln -sf ${link/←/ }
done<SYMLINKS.txt
rm SYMLINKS.txt
)
fi
# Import signing keys from files
gpg --import ${TERMUX_REPO_SIGNING_KEYS}
for idx in $(seq ${#TERMUX_REPO_URL[@]}); do
local TERMUX_REPO_NAME=$(echo ${TERMUX_REPO_URL[$idx-1]} | sed -e 's%https://%%g' -e 's%http://%%g' -e 's%/%-%g')
curl --fail -L "${TERMUX_REPO_URL[$idx-1]}/dists/${TERMUX_REPO_DISTRIBUTION[$idx-1]}/InRelease" \
-o ${TERMUX_COMMON_CACHEDIR}/${TERMUX_REPO_NAME}-${TERMUX_REPO_DISTRIBUTION[$idx-1]}-InRelease \
|| curl --fail -L "${TERMUX_REPO_URL[$idx-1]}/dists/${TERMUX_REPO_DISTRIBUTION[$idx-1]}/Release.gpg" \
-o ${TERMUX_COMMON_CACHEDIR}/${TERMUX_REPO_NAME}-${TERMUX_REPO_DISTRIBUTION[$idx-1]}-Release.gpg \
&& curl --fail -L "${TERMUX_REPO_URL[$idx-1]}/dists/${TERMUX_REPO_DISTRIBUTION[$idx-1]}/Release" \
-o ${TERMUX_COMMON_CACHEDIR}/${TERMUX_REPO_NAME}-${TERMUX_REPO_DISTRIBUTION[$idx-1]}-Release \
|| termux_error_exit "Download of InRelease and Release.gpg from ${TERMUX_REPO_URL[$idx-1]}/dists/${TERMUX_REPO_DISTRIBUTION[$idx-1]} failed"
if [ -f ${TERMUX_COMMON_CACHEDIR}/${TERMUX_REPO_NAME}-${TERMUX_REPO_DISTRIBUTION[$idx-1]}-InRelease ]; then
local RELEASE_FILE=${TERMUX_COMMON_CACHEDIR}/${TERMUX_REPO_NAME}-${TERMUX_REPO_DISTRIBUTION[$idx-1]}-InRelease
gpg --verify $RELEASE_FILE
else
local RELEASE_FILE=${TERMUX_COMMON_CACHEDIR}/${TERMUX_REPO_NAME}-${TERMUX_REPO_DISTRIBUTION[$idx-1]}-Release
gpg --verify ${RELEASE_FILE}.gpg $RELEASE_FILE
fi
for arch in all $TERMUX_ARCH; do
local packages_hash=$(./scripts/get_hash_from_file.py ${RELEASE_FILE} $arch ${TERMUX_REPO_COMPONENT[$idx-1]})
# If packages_hash = "" then the repo probably doesn't contain debs for $arch
if ! [ "$packages_hash" = "" ]; then
termux_download "${TERMUX_REPO_URL[$idx-1]}/dists/${TERMUX_REPO_DISTRIBUTION[$idx-1]}/${TERMUX_REPO_COMPONENT[$idx-1]}/binary-$arch/Packages" \
"${TERMUX_COMMON_CACHEDIR}-$arch/${TERMUX_REPO_NAME}-${TERMUX_REPO_DISTRIBUTION[$idx-1]}-${TERMUX_REPO_COMPONENT[$idx-1]}-Packages" \
$packages_hash
fi
done
done
fi
}
# Source the package build script and start building. No to be overridden by packages.
termux_step_start_build() {
# shellcheck source=/dev/null
source "$TERMUX_PKG_BUILDER_SCRIPT"
TERMUX_STANDALONE_TOOLCHAIN="$TERMUX_COMMON_CACHEDIR/${TERMUX_NDK_VERSION}-${TERMUX_ARCH}-${TERMUX_PKG_API_LEVEL}"
# Bump the below version if a change is made in toolchain setup to ensure
# that everyone gets an updated toolchain:
TERMUX_STANDALONE_TOOLCHAIN+="-v1"
if [ -n "${TERMUX_PKG_BLACKLISTED_ARCHES:=""}" ] && [ "$TERMUX_PKG_BLACKLISTED_ARCHES" != "${TERMUX_PKG_BLACKLISTED_ARCHES/$TERMUX_ARCH/}" ]; then
echo "Skipping building $TERMUX_PKG_NAME for arch $TERMUX_ARCH"
exit 0
fi
if [ "$TERMUX_SKIP_DEPCHECK" = false ] && [ "$TERMUX_INSTALL_DEPS" = true ]; then
# Download dependencies
local PKG DEP_ARCH DEP_VERSION DEB_FILE _PKG_DEPENDS _PKG_BUILD_DEPENDS _SUBPKG_DEPENDS
# remove (>= 1.0) and similar version tags:
_PKG_DEPENDS=$(echo ${TERMUX_PKG_DEPENDS// /} | sed "s/[(][^)]*[)]//g")
_PKG_BUILD_DEPENDS=${TERMUX_PKG_BUILD_DEPENDS// /}
# Also download subpackages dependencies (except the mother package):
for SUBPKG in packages/$TERMUX_PKG_NAME/*.subpackage.sh; do
_SUBPKG_DEPENDS+=" $(. $SUBPKG; echo $TERMUX_SUBPKG_DEPENDS | sed s%$TERMUX_PKG_NAME%%g)"
done
for PKG in $(echo ${_PKG_DEPENDS//,/ } ${_SUBPKG_DEPENDS//,/ } ${_PKG_BUILD_DEPENDS//,/ } | tr ' ' '\n' | sort -u); do
# handle "or" in dependencies (use first one):
if [ ! "$PKG" = "${PKG/|/}" ]; then PKG=$(echo "$PKG" | sed "s%|.*%%"); fi
# llvm doesn't build if ndk-sysroot is installed:
if [ "$PKG" = "ndk-sysroot" ]; then continue; fi
read DEP_ARCH DEP_VERSION <<< $(termux_extract_dep_info "$PKG")
if [ ! "$TERMUX_QUIET_BUILD" = true ]; then
echo "Downloading dependency $PKG@$DEP_VERSION if necessary..."
fi
if ! termux_download_deb $PKG $DEP_ARCH $DEP_VERSION; then
echo "Download of $PKG@$DEP_VERSION from $TERMUX_REPO_URL failed, building instead"
./build-package.sh -a $TERMUX_ARCH -I "$PKG"
continue
else
if [ ! "$TERMUX_QUIET_BUILD" = true ]; then echo "extracting $PKG..."; fi
(
cd $TERMUX_COMMON_CACHEDIR-$DEP_ARCH
ar x ${PKG}_${DEP_VERSION}_${DEP_ARCH}.deb data.tar.xz
tar -xf data.tar.xz --no-overwrite-dir -C /
)
fi
if termux_download_deb $PKG-dev $DEP_ARCH $DEP_VERSION; then
(
cd $TERMUX_COMMON_CACHEDIR-$DEP_ARCH
ar x $PKG-dev_${DEP_VERSION}_${DEP_ARCH}.deb data.tar.xz
tar xf data.tar.xz --no-overwrite-dir -C /
)
else
echo "Download of $PKG-dev@$DEP_VERSION from $TERMUX_REPO_URL failed"
fi
mkdir -p /data/data/.built-packages
echo "$DEP_VERSION" > "/data/data/.built-packages/$PKG"
done
elif [ "$TERMUX_SKIP_DEPCHECK" = false ] && [ "$TERMUX_INSTALL_DEPS" = false ]; then
# Build dependencies
for PKG in $(./scripts/buildorder.py "$TERMUX_PKG_BUILDER_DIR"); do
echo "Building dependency $PKG if necessary..."
# Built dependencies are put in the default TERMUX_DEBDIR instead of the specified one
./build-package.sh -a $TERMUX_ARCH -s "$PKG"
done
fi
TERMUX_PKG_FULLVERSION=$TERMUX_PKG_VERSION
if [ "$TERMUX_PKG_REVISION" != "0" ] || [ "$TERMUX_PKG_FULLVERSION" != "${TERMUX_PKG_FULLVERSION/-/}" ]; then
# "0" is the default revision, so only include it if the upstream versions contains "-" itself
TERMUX_PKG_FULLVERSION+="-$TERMUX_PKG_REVISION"
fi
if [ "$TERMUX_DEBUG" = true ]; then
if [ "$TERMUX_PKG_HAS_DEBUG" == "yes" ]; then
DEBUG="-dbg"
else
echo "Skipping building debug build for $TERMUX_PKG_NAME"
exit 0
fi
else
DEBUG=""
fi
if [ -z "$TERMUX_DEBUG" ] &&
[ -z "${TERMUX_FORCE_BUILD+x}" ] &&
[ -e "/data/data/.built-packages/$TERMUX_PKG_NAME" ]; then
if [ "$(cat "/data/data/.built-packages/$TERMUX_PKG_NAME")" = "$TERMUX_PKG_FULLVERSION" ]; then
echo "$TERMUX_PKG_NAME@$TERMUX_PKG_FULLVERSION built - skipping (rm /data/data/.built-packages/$TERMUX_PKG_NAME to force rebuild)"
exit 0
fi
fi
# Cleanup old state:
rm -Rf "$TERMUX_PKG_BUILDDIR" \
"$TERMUX_PKG_PACKAGEDIR" \
"$TERMUX_PKG_SRCDIR" \
"$TERMUX_PKG_TMPDIR" \
"$TERMUX_PKG_MASSAGEDIR"
# Ensure folders present (but not $TERMUX_PKG_SRCDIR, it will be created in build)
mkdir -p "$TERMUX_COMMON_CACHEDIR" \
"$TERMUX_DEBDIR" \
"$TERMUX_PKG_BUILDDIR" \
"$TERMUX_PKG_PACKAGEDIR" \
"$TERMUX_PKG_TMPDIR" \
"$TERMUX_PKG_CACHEDIR" \
"$TERMUX_PKG_MASSAGEDIR" \
$TERMUX_PREFIX/{bin,etc,lib,libexec,share,tmp,include}
# Make $TERMUX_PREFIX/bin/sh executable on the builder, so that build
# scripts can assume that it works on both builder and host later on:
ln -f -s /bin/sh "$TERMUX_PREFIX/bin/sh"
local TERMUX_ELF_CLEANER_SRC=$TERMUX_COMMON_CACHEDIR/termux-elf-cleaner.cpp
local TERMUX_ELF_CLEANER_VERSION
TERMUX_ELF_CLEANER_VERSION=$(bash -c ". $TERMUX_SCRIPTDIR/packages/termux-elf-cleaner/build.sh; echo \$TERMUX_PKG_VERSION")
termux_download \
"https://raw.githubusercontent.com/termux/termux-elf-cleaner/v$TERMUX_ELF_CLEANER_VERSION/termux-elf-cleaner.cpp" \
"$TERMUX_ELF_CLEANER_SRC" \
62c3cf9813756a1b262108fbc39684c5cfd3f9a69b376276bb1ac6af138f5fa2
if [ "$TERMUX_ELF_CLEANER_SRC" -nt "$TERMUX_ELF_CLEANER" ]; then
g++ -std=c++11 -Wall -Wextra -pedantic -Os "$TERMUX_ELF_CLEANER_SRC" -o "$TERMUX_ELF_CLEANER"
fi
if [ -n "$TERMUX_PKG_BUILD_IN_SRC" ]; then
echo "Building in src due to TERMUX_PKG_BUILD_IN_SRC being set" > "$TERMUX_PKG_BUILDDIR/BUILDING_IN_SRC.txt"
TERMUX_PKG_BUILDDIR=$TERMUX_PKG_SRCDIR
fi
echo "termux - building $TERMUX_PKG_NAME for arch $TERMUX_ARCH..."
test -t 1 && printf "\033]0;%s...\007" "$TERMUX_PKG_NAME"
# Avoid exporting PKG_CONFIG_LIBDIR until after termux_step_host_build.
export TERMUX_PKG_CONFIG_LIBDIR=$TERMUX_PREFIX/lib/pkgconfig
# Add a pkg-config file for the system zlib.
mkdir -p "$TERMUX_PKG_CONFIG_LIBDIR"
cat > "$TERMUX_PKG_CONFIG_LIBDIR/zlib.pc" <<-HERE
Name: zlib
Description: zlib compression library
Version: 1.2.8
Requires:
Libs: -lz
HERE
ln -sf $TERMUX_STANDALONE_TOOLCHAIN/sysroot/usr/lib/$TERMUX_HOST_PLATFORM/$TERMUX_PKG_API_LEVEL/libz.so $TERMUX_PREFIX/lib/libz.so
# Keep track of when build started so we can see what files have been created.
# We start by sleeping so that any generated files above (such as zlib.pc) get
# an older timestamp than the TERMUX_BUILD_TS_FILE.
sleep 1
TERMUX_BUILD_TS_FILE=$TERMUX_PKG_TMPDIR/timestamp_$TERMUX_PKG_NAME
touch "$TERMUX_BUILD_TS_FILE"
}
# Run just after sourcing $TERMUX_PKG_BUILDER_SCRIPT. May be overridden by packages.
termux_step_extract_package() {
if [ -z "${TERMUX_PKG_SRCURL:=""}" ] || [ -n "${TERMUX_PKG_SKIP_SRC_EXTRACT:=""}" ]; then
mkdir -p "$TERMUX_PKG_SRCDIR"
return
fi
cd "$TERMUX_PKG_TMPDIR"
local PKG_SRCURL=(${TERMUX_PKG_SRCURL[@]})
local PKG_SHA256=(${TERMUX_PKG_SHA256[@]})
if [ ! ${#PKG_SRCURL[@]} == ${#PKG_SHA256[@]} ] && [ ! ${#PKG_SHA256[@]} == 0 ]; then
termux_error_exit "Error: length of TERMUX_PKG_SRCURL isn't equal to length of TERMUX_PKG_SHA256."
fi
# STRIP=1 extracts archives straight into TERMUX_PKG_SRCDIR while STRIP=0 puts them in subfolders. zip has same behaviour per default
# If this isn't desired then this can be fixed in termux_step_post_extract_package.
local STRIP=1
for i in $(seq 0 $(( ${#PKG_SRCURL[@]}-1 ))); do
test "$i" -gt 0 && STRIP=0
local filename
filename=$(basename "${PKG_SRCURL[$i]}")
local file="$TERMUX_PKG_CACHEDIR/$filename"
# Allow TERMUX_PKG_SHA256 to be empty:
set +u
termux_download "${PKG_SRCURL[$i]}" "$file" "${PKG_SHA256[$i]}"
set -u
local folder
set +o pipefail
if [ "${file##*.}" = zip ]; then
folder=$(unzip -qql "$file" | head -n1 | tr -s ' ' | cut -d' ' -f5-)
rm -Rf $folder
unzip -q "$file"
mv $folder "$TERMUX_PKG_SRCDIR"
else
mkdir -p "$TERMUX_PKG_SRCDIR"
tar xf "$file" -C "$TERMUX_PKG_SRCDIR" --strip-components=$STRIP
fi
set -o pipefail
done
}
# Hook for packages to act just after the package has been extracted.
# Invoked in $TERMUX_PKG_SRCDIR.
termux_step_post_extract_package() {
return
}
# Optional host build. Not to be overridden by packages.
termux_step_handle_hostbuild() {
if [ "x$TERMUX_PKG_HOSTBUILD" = "x" ]; then return; fi
cd "$TERMUX_PKG_SRCDIR"
for patch in $TERMUX_PKG_BUILDER_DIR/*.patch.beforehostbuild; do
test -f "$patch" && sed "s%\@TERMUX_PREFIX\@%${TERMUX_PREFIX}%g" "$patch" | patch --silent -p1
done
local TERMUX_HOSTBUILD_MARKER="$TERMUX_PKG_HOSTBUILD_DIR/TERMUX_BUILT_FOR_$TERMUX_PKG_VERSION"
if [ ! -f "$TERMUX_HOSTBUILD_MARKER" ]; then
rm -Rf "$TERMUX_PKG_HOSTBUILD_DIR"
mkdir -p "$TERMUX_PKG_HOSTBUILD_DIR"
cd "$TERMUX_PKG_HOSTBUILD_DIR"
termux_step_host_build
touch "$TERMUX_HOSTBUILD_MARKER"
fi
}
# Perform a host build. Will be called in $TERMUX_PKG_HOSTBUILD_DIR.
# After termux_step_post_extract_package() and before termux_step_patch_package()
termux_step_host_build() {
"$TERMUX_PKG_SRCDIR/configure" ${TERMUX_PKG_EXTRA_HOSTBUILD_CONFIGURE_ARGS}
make -j "$TERMUX_MAKE_PROCESSES"
}
# Setup a standalone Android NDK toolchain. Not to be overridden by packages.
termux_step_setup_toolchain() {
# We put this after system PATH to avoid picking up toolchain stripped python
export PATH=$PATH:$TERMUX_STANDALONE_TOOLCHAIN/bin
export CFLAGS=""
export LDFLAGS="-L${TERMUX_PREFIX}/lib"
export AS=${TERMUX_HOST_PLATFORM}-clang
export CC=$TERMUX_HOST_PLATFORM-clang
export CXX=$TERMUX_HOST_PLATFORM-clang++
export CCTERMUX_HOST_PLATFORM=$TERMUX_HOST_PLATFORM$TERMUX_PKG_API_LEVEL
if [ $TERMUX_ARCH = arm ]; then
CCTERMUX_HOST_PLATFORM=armv7a-linux-androideabi$TERMUX_PKG_API_LEVEL
fi
export AR=$TERMUX_HOST_PLATFORM-ar
export CPP=${TERMUX_HOST_PLATFORM}-cpp
export CC_FOR_BUILD=gcc
export LD=$TERMUX_HOST_PLATFORM-ld
export OBJDUMP=$TERMUX_HOST_PLATFORM-objdump
# Setup pkg-config for cross-compiling:
export PKG_CONFIG=$TERMUX_STANDALONE_TOOLCHAIN/bin/${TERMUX_HOST_PLATFORM}-pkg-config
export RANLIB=$TERMUX_HOST_PLATFORM-ranlib
export READELF=$TERMUX_HOST_PLATFORM-readelf
export STRIP=$TERMUX_HOST_PLATFORM-strip
# Android 7 started to support DT_RUNPATH (but not DT_RPATH), so we may want
# LDFLAGS+="-Wl,-rpath=$TERMUX_PREFIX/lib -Wl,--enable-new-dtags"
# and no longer remove DT_RUNPATH in termux-elf-cleaner.
if [ "$TERMUX_ARCH" = "arm" ]; then
# https://developer.android.com/ndk/guides/standalone_toolchain.html#abi_compatibility:
# "We recommend using the -mthumb compiler flag to force the generation of 16-bit Thumb-2 instructions".
# With r13 of the ndk ruby 2.4.0 segfaults when built on arm with clang without -mthumb.
CFLAGS+=" -march=armv7-a -mfpu=neon -mfloat-abi=softfp -mthumb"
LDFLAGS+=" -march=armv7-a"
elif [ "$TERMUX_ARCH" = "i686" ]; then
# From $NDK/docs/CPU-ARCH-ABIS.html:
CFLAGS+=" -march=i686 -msse3 -mstackrealign -mfpmath=sse"
elif [ "$TERMUX_ARCH" = "aarch64" ]; then
:
elif [ "$TERMUX_ARCH" = "x86_64" ]; then
:
else
termux_error_exit "Invalid arch '$TERMUX_ARCH' - support arches are 'arm', 'i686', 'aarch64', 'x86_64'"
fi
if [ -n "$TERMUX_DEBUG" ]; then
CFLAGS+=" -g3 -O1 -fstack-protector --param ssp-buffer-size=4 -D_FORTIFY_SOURCE=2"
else
if [ $TERMUX_ARCH = arm ]; then
CFLAGS+=" -Os"
else
CFLAGS+=" -Oz"
fi
fi
export CXXFLAGS="$CFLAGS"
export CPPFLAGS="-I${TERMUX_PREFIX}/include"
# If libandroid-support is declared as a dependency, link to it explicitly:
if [ "$TERMUX_PKG_DEPENDS" != "${TERMUX_PKG_DEPENDS/libandroid-support/}" ]; then
LDFLAGS+=" -landroid-support"
fi
export ac_cv_func_getpwent=no
export ac_cv_func_getpwnam=no
export ac_cv_func_getpwuid=no
export ac_cv_func_sigsetmask=no
export ac_cv_c_bigendian=no
if [ ! -d $TERMUX_STANDALONE_TOOLCHAIN ]; then
# Do not put toolchain in place until we are done with setup, to avoid having a half setup
# toolchain left in place if something goes wrong (or process is just aborted):
local _TERMUX_TOOLCHAIN_TMPDIR=${TERMUX_STANDALONE_TOOLCHAIN}-tmp
rm -Rf $_TERMUX_TOOLCHAIN_TMPDIR
local _NDK_ARCHNAME=$TERMUX_ARCH
if [ "$TERMUX_ARCH" = "aarch64" ]; then
_NDK_ARCHNAME=arm64
elif [ "$TERMUX_ARCH" = "i686" ]; then
_NDK_ARCHNAME=x86
fi
"$NDK/build/tools/make_standalone_toolchain.py" \
--api "$TERMUX_PKG_API_LEVEL" \
--arch $_NDK_ARCHNAME \
--stl=libc++ \
--install-dir $_TERMUX_TOOLCHAIN_TMPDIR
# Remove android-support header wrapping not needed on android-21:
rm -Rf $_TERMUX_TOOLCHAIN_TMPDIR/sysroot/usr/local
if [ "$TERMUX_ARCH" = "aarch64" ]; then
# Use gold by default to work around https://github.com/android-ndk/ndk/issues/148
cp $_TERMUX_TOOLCHAIN_TMPDIR/bin/aarch64-linux-android-ld.gold \
$_TERMUX_TOOLCHAIN_TMPDIR/bin/aarch64-linux-android-ld
cp $_TERMUX_TOOLCHAIN_TMPDIR/aarch64-linux-android/bin/ld.gold \
$_TERMUX_TOOLCHAIN_TMPDIR/aarch64-linux-android/bin/ld
fi
if [ "$TERMUX_ARCH" = "arm" ]; then
# Linker wrapper script to add '--exclude-libs libgcc.a', see
# https://github.com/android-ndk/ndk/issues/379
# https://android-review.googlesource.com/#/c/389852/
local linker
for linker in ld ld.bfd ld.gold; do
local wrap_linker=$_TERMUX_TOOLCHAIN_TMPDIR/$TERMUX_HOST_PLATFORM/bin/$linker
local real_linker=$_TERMUX_TOOLCHAIN_TMPDIR/$TERMUX_HOST_PLATFORM/bin/$linker.real
cp $wrap_linker $real_linker
echo '#!/bin/bash' > $wrap_linker
echo -n '$(dirname $0)/' >> $wrap_linker
echo -n $linker.real >> $wrap_linker
echo ' --exclude-libs libgcc.a "$@"' >> $wrap_linker
done
fi
# Setup the cpp preprocessor:
cp $_TERMUX_TOOLCHAIN_TMPDIR/bin/$TERMUX_HOST_PLATFORM-clang \
$_TERMUX_TOOLCHAIN_TMPDIR/bin/$TERMUX_HOST_PLATFORM-cpp
sed -i 's/clang80/clang80 -E/' \
$_TERMUX_TOOLCHAIN_TMPDIR/bin/$TERMUX_HOST_PLATFORM-cpp
cd $_TERMUX_TOOLCHAIN_TMPDIR/sysroot
for f in $TERMUX_SCRIPTDIR/ndk-patches/*.patch; do
sed "s%\@TERMUX_PREFIX\@%${TERMUX_PREFIX}%g" "$f" | \
sed "s%\@TERMUX_HOME\@%${TERMUX_ANDROID_HOME}%g" | \
patch --silent -p1;
done
# elf.h: Taken from glibc since the elf.h in the NDK is lacking.
# ifaddrs.h: Added in android-24 unified headers, use a inline implementation for now.
# langinfo.h: Inline implementation of nl_langinfo().
# iconv.h: Header for iconv, implemented in libandroid-support.
cp "$TERMUX_SCRIPTDIR"/ndk-patches/{ifaddrs.h,libintl.h,langinfo.h,iconv.h} usr/include
# Remove <sys/shm.h> from the NDK in favour of that from the libandroid-shmem.
# Remove <sys/sem.h> as it doesn't work for non-root.
# Remove <glob.h> as we currently provide it from libandroid-glob.
# Remove <spawn.h> as it's only for future (later than android-27).
rm usr/include/sys/{shm.h,sem.h} usr/include/{glob.h,spawn.h}
sed -i "s/define __ANDROID_API__ __ANDROID_API_FUTURE__/define __ANDROID_API__ $TERMUX_PKG_API_LEVEL/" \
usr/include/android/api-level.h
$TERMUX_ELF_CLEANER usr/lib/*/*/*.so
# zlib is really version 1.2.8 in the Android platform (at least
# starting from Android 5), not older as the NDK headers claim.
for file in zconf.h zlib.h; do
curl -o usr/include/$file \
https://raw.githubusercontent.com/madler/zlib/v1.2.8/$file
done
unset file
grep -lrw $_TERMUX_TOOLCHAIN_TMPDIR/sysroot/usr/include/c++/v1 -e '<version>' | xargs -n 1 sed -i 's/<version>/\"version\"/g'
mv $_TERMUX_TOOLCHAIN_TMPDIR $TERMUX_STANDALONE_TOOLCHAIN
fi
local _STL_LIBFILE_NAME=libc++_shared.so
if [ ! -f $TERMUX_PREFIX/lib/libstdc++.so ]; then
# Setup libc++_shared.so in $PREFIX/lib and libstdc++.so as a link to it,
# so that other C++ using packages links to it instead of the default android
# C++ library which does not support exceptions or STL:
# https://developer.android.com/ndk/guides/cpp-support.html
# We do however want to avoid installing this, to avoid problems where e.g.
# libm.so on some i686 devices links against libstdc++.so.
# The libc++_shared.so library will be packaged in the libc++ package
# which is part of the base Termux installation.
mkdir -p "$TERMUX_PREFIX/lib"
cd "$TERMUX_PREFIX/lib"
local _STL_LIBFILE=$TERMUX_STANDALONE_TOOLCHAIN/sysroot/usr/lib/${TERMUX_HOST_PLATFORM}/$_STL_LIBFILE_NAME
cp "$_STL_LIBFILE" .
$STRIP --strip-unneeded $_STL_LIBFILE_NAME
$TERMUX_ELF_CLEANER $_STL_LIBFILE_NAME
if [ $TERMUX_ARCH = "arm" ]; then
# Use a linker script to get libunwind.a.
echo 'INPUT(-lunwind -lc++_shared)' > libstdc++.so
else
ln -f $_STL_LIBFILE_NAME libstdc++.so
fi
fi
export PKG_CONFIG_LIBDIR="$TERMUX_PKG_CONFIG_LIBDIR"
# Create a pkg-config wrapper. We use path to host pkg-config to
# avoid picking up a cross-compiled pkg-config later on.
local _HOST_PKGCONFIG
_HOST_PKGCONFIG=$(which pkg-config)
mkdir -p $TERMUX_STANDALONE_TOOLCHAIN/bin "$PKG_CONFIG_LIBDIR"
cat > "$PKG_CONFIG" <<-HERE
#!/bin/sh
export PKG_CONFIG_DIR=
export PKG_CONFIG_LIBDIR=$PKG_CONFIG_LIBDIR
exec $_HOST_PKGCONFIG "\$@"
HERE
chmod +x "$PKG_CONFIG"
}
# Apply all *.patch files for the package. Not to be overridden by packages.
termux_step_patch_package() {
cd "$TERMUX_PKG_SRCDIR"
local DEBUG_PATCHES=""
if [ "$TERMUX_DEBUG" == "true" ] && [ -f $TERMUX_PKG_BUILDER_DIR/*.patch.debug ] ; then
DEBUG_PATCHES="$(ls $TERMUX_PKG_BUILDER_DIR/*.patch.debug)"
fi
# Suffix patch with ".patch32" or ".patch64" to only apply for these bitnesses:
shopt -s nullglob
for patch in $TERMUX_PKG_BUILDER_DIR/*.patch{$TERMUX_ARCH_BITS,} $DEBUG_PATCHES; do
test -f "$patch" && sed "s%\@TERMUX_PREFIX\@%${TERMUX_PREFIX}%g" "$patch" | \
sed "s%\@TERMUX_HOME\@%${TERMUX_ANDROID_HOME}%g" | \
patch --silent -p1
done
shopt -u nullglob
}
# Replace autotools build-aux/config.{sub,guess} with ours to add android targets.
termux_step_replace_guess_scripts() {
cd "$TERMUX_PKG_SRCDIR"
find . -name config.sub -exec chmod u+w '{}' \; -exec cp "$TERMUX_SCRIPTDIR/scripts/config.sub" '{}' \;
find . -name config.guess -exec chmod u+w '{}' \; -exec cp "$TERMUX_SCRIPTDIR/scripts/config.guess" '{}' \;