-
Notifications
You must be signed in to change notification settings - Fork 104
/
zproject_android.gsl
997 lines (815 loc) · 34.7 KB
/
zproject_android.gsl
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
# Generate Android build system files
#
# This is a code generator built using the iMatix GSL code generation
# language. See https://github.com/zeromq/gsl for details.
#
# Copyright (c) the Contributors as noted in the AUTHORS file.
# This file is part of zproject.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
register_target ("android", "Native shared library for Android")
.macro target_android
.directory.create ("builds/android")
.
.output "builds/android/README.md"
# Android Build
## Preamble
The last known NDK is automatically downloaded, if not specified otherwise.
As indicated in LIBZMQ main [README](https://github.com/zeromq/libzmq/blob/master/README.md#supported-platforms-with-primary-CI),
Android support is still DRAFT.
## Configuration
### Basics
Basically, $(PROJECT.NAME) build for Android, relies on exported variables.
Provided build scripts can mainly be used like
export XXX=xxx
export YYY=yyy
...
cd <$(project.name:c)>/builds/android
./<build_script>
### Android NDK
$(PROJECT.NAME) is tested against Android NDK versions r19 to r25.
By default, $(PROJECT.NAME) uses NDK `$(project.android_ndk_version)`, but you can specify
a different one:
export NDK_VERSION=android-ndk-r23c
If you already have installed your favorite NDK somewhere, all you have to
do is to export and set NDK_VERSION and ANDROID_NDK_ROOT environment
variables, e.g:
export NDK_VERSION="android-ndk-r23b"
export ANDROID_NDK_ROOT=$HOME/${NDK_VERSION}
**Important:** ANDROID_NDK_ROOT must be an absolute path !
If you specify only NDK_VERSION, ANDROID_NDK_ROOT will be automatically set
to its default:
export ANDROID_NDK_ROOT=/tmp/${NDK_VERSION}
To specify the minimum SDK version set the environment variable below:
export MIN_SDK_VERSION=$(project.android_min_sdk_version)\ \ \ # Default value if unset
To specify the build directory set the environment variable below:
export ANDROID_BUILD_DIR=${HOME}/android_build
**Important:** ANDROID_BUILD_ROOT must be an absolute path !
### Android build folder
All Android libraries will be generated under:
${ANDROID_BUILD_DIR}/prefix/<arch>/lib
where <arch> is one of `arm`, `arm64`, `x86` or `x86_64`.
### Android build cleanup
Build and Dependency storage folders are automatically cleaned,
by `ci_build.sh`. This can be avoided with the help of
ANDROID_BUILD_DIR="no"
If you turn this to "no", make sure to clean what has to be, before
calling `build.sh` or `ci_build.sh`.
### Prebuilt Android libraries
Android prebuilt libraries have to be stored under
ANDROID_BUILD_DIR/prefix/<arch>/lib
Do not forget to disable [Android cleanup](#android-build-cleanup).
### Dependencies
By default, `build.sh` download dependencies under `/tmp/tmp-deps`.
You can specify another folder with the help of ANDROID_DEPENDENCIES_DIR:
ANDROID_DEPENDENCIES_DIR=${HOME}/my_dependencies
If you place your own dependency source trees there,
do not forget to disable [Android cleanup](#android-build-cleanup).
## Build
See chapter [Configuration](#configuration) for configuration options and
other details.
Select your preferred parameters:
export XXX=xxx
export YYY=yyy
...
and run:
cd <$(project.name:c)>/builds/android
./build.sh [ arm | arm64 | x86 | x86_64 ]
Parameter selection and the calls to build.sh can be located in a
SHELL script, like in ci_build.sh.
## CI build
Basically, it will call `build.sh` once, for each Android target.
This script accepts the same configuration variables, but some are set
with different default values. For instance, the dependencies are not
downloaded or cloned in `/tmp/tmp-deps, but inside LIBZMQ clone.
It can be used in the same way as build.sh
export XXX=xxx
export YYY=yyy
cd <$(project.name:c)>/builds/android
./ci_build.sh
.close
.
.output "builds/android/build.sh"
#!/usr/bin/env bash
$(project.GENERATED_WARNING_HEADER:)
#
# Exit if any step fails
set -e
# Use directory of current script as the working directory
cd "\$( dirname "${BASH_SOURCE[0]}" )"
PROJECT_ROOT="\$(cd ../.. && pwd)"
########################################################################
# Configuration & tuning options.
########################################################################
# Set default values used in ci builds
export NDK_VERSION="${NDK_VERSION:-$(project.android_ndk_version)}"
# Set default path to find Android NDK.
# Must be of the form <path>/${NDK_VERSION} !!
export ANDROID_NDK_ROOT="${ANDROID_NDK_ROOT:-/tmp/${NDK_VERSION}}"
# With NDK r22b, the minimum SDK version range is [16, 31].
# Since NDK r24, the minimum SDK version range is [19, 31].
# SDK version 21 is the minimum version for 64-bit builds.
export MIN_SDK_VERSION=${MIN_SDK_VERSION:-$(project.android_min_sdk_version)}
# Where to download our dependencies: default to /tmp/tmp-deps
export ANDROID_DEPENDENCIES_DIR="${ANDROID_DEPENDENCIES_DIR:-/tmp/tmp-deps}"
# Use directory of current script as the build directory
# ${ANDROID_BUILD_DIR}/prefix/<build_arch>/lib will contain produced libraries
export ANDROID_BUILD_DIR="${ANDROID_BUILD_DIR:-${PWD}}"
# Clean before processing
export ANDROID_BUILD_CLEAN="${ANDROID_BUILD_CLEAN:-no}"
# Set this to 'no', to enable verbose ./configure
export CI_CONFIG_QUIET="${CI_CONFIG_QUIET:-no}"
# Set this to enable verbose profiling
export CI_TIME="${CI_TIME:-}"
# Set this to enable verbose tracing
export CI_TRACE="${CI_TRACE:-no}"
# By default, dependencies will be cloned to /tmp/tmp-deps.
# If you have your own source tree for XXX, uncomment its
# XXX_ROOT configuration line below, and provide its absolute tree:
.for use where defined (use.repository)
# export $(USE.PROJECT)_ROOT="<absolute_path_to_$(USE.PROJECT)_source_tree>"
.endfor
########################################################################
# Utilities
########################################################################
# Get access to android_build functions and variables
# Perform some sanity checks and calculate some variables.
source "${PROJECT_ROOT}/builds/android/android_build_helper.sh"
function usage {
echo "$(PROJECT.NAME) - Usage:"
echo " export XXX=xxx"
echo " ./build.sh [ arm | arm64 | x86 | x86_64 ]"
echo ""
echo "See this file (configuration & tuning options) for details"
echo "on variables XXX and their values xxx"
exit 1
}
########################################################################
# Sanity checks
########################################################################
BUILD_ARCH="$1"
[ -z "${BUILD_ARCH}" ] && usage
# Initialize our dependency _ROOT variables:
. for use where defined (use.repository)
android_init_dependency_root "$(use.project)" # Check or initialize $(USE.PROJECT)_ROOT
. endfor
# Fetch required dependencies:
. for use where defined (use.repository)
. if defined (use.tarball)
[ ! -d "${$(USE.PROJECT)_ROOT}" ] && android_download_library "$(USE.PROJECT)" "${$(USE.PROJECT)_ROOT}" "$(use.tarball)"
. else
[ ! -d "${$(USE.PROJECT)_ROOT}" ] && android_clone_library "$(USE.PROJECT)" "${$(USE.PROJECT)_ROOT}" "$(use.repository)" "$(use.release?)"
. endif
. endfor
case "$CI_TIME" in
[Yy][Ee][Ss]|[Oo][Nn]|[Tt][Rr][Uu][Ee])
CI_TIME="time -p " ;;
[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee])
CI_TIME="" ;;
esac
case "$CI_TRACE" in
[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee])
set +x ;;
[Yy][Ee][Ss]|[Oo][Nn]|[Tt][Rr][Uu][Ee])
set -x ;;
esac
########################################################################
# Compilation
########################################################################
# Choose a C++ standard library implementation from the ndk
export ANDROID_BUILD_CXXSTL="gnustl_shared_49"
# Additional flags for LIBTOOL, for LIBZMQ and other dependencies.
export LIBTOOL_EXTRA_LDFLAGS='-avoid-version'
# Set up android build environment and set ANDROID_BUILD_OPTS array
android_build_set_env "${BUILD_ARCH}"
android_download_ndk
android_build_env
android_build_opts
# Check for environment variable to clear the prefix and do a clean build
if [ "${ANDROID_BUILD_CLEAN}" = "yes" ]; then
android_build_trace "Doing a clean build (removing previous build and dependencies)..."
rm -rf "${ANDROID_BUILD_PREFIX}"/*
# Called shells MUST not clean after ourselves !
export ANDROID_BUILD_CLEAN="no"
fi
DEPENDENCIES=()
.for use where use.implied = 0
########################################################################
# Make sure $(USE.PROJECT) is built and copy the prefix
DEPENDENCIES+=("$(use.libname).so")
(android_build_verify_so "$(use.libname).so" &> /dev/null) || {
if [ -f "${$(USE.PROJECT)_ROOT}/builds/android/build.sh" ] ; then
(
bash "${$(USE.PROJECT)_ROOT}/builds/android/build.sh" "${BUILD_ARCH}"
) || exit 1
else
(
CONFIG_OPTS=()
[ "${CI_CONFIG_QUIET}" = "yes" ] && CONFIG_OPTS+=("--quiet")
CONFIG_OPTS+=("${ANDROID_BUILD_OPTS[@]}")
CONFIG_OPTS+=("--without-docs")
. if count (use.add_config_opts) > 0
# Custom additional options for $(USE.PROJECT)
. for use.add_config_opts as add_cfgopt
CONFIG_OPTS+=("$(add_cfgopt)")
. endfor
. endif
android_build_library "$(USE.PROJECT)" "${$(USE.PROJECT)_ROOT}"
) || exit 1
fi
UPSTREAM_PREFIX="${$(USE.PROJECT)_ROOT}/builds/android/prefix/${TOOLCHAIN_ARCH}"
cp -rn "${UPSTREAM_PREFIX}"/* "${ANDROID_BUILD_PREFIX}" || :
}
.endfor
########################################################################
[ -z "$CI_TIME" ] || echo "`date`: Build $(project.name) from local source"
(android_build_verify_so "$(project.libname).so" "${DEPENDENCIES[@]}" &> /dev/null) || {
(
CONFIG_OPTS=()
[ "${CI_CONFIG_QUIET}" = "yes" ] && CONFIG_OPTS+=("--quiet")
CONFIG_OPTS+=("${ANDROID_BUILD_OPTS[@]}")
CONFIG_OPTS+=("--without-docs")
. if count (project.add_config_opts) > 0
# Custom additional options for $(PROJECT.LIBNAME)
. for project.add_config_opts as add_cfgopt
CONFIG_OPTS+=("$(add_cfgopt)")
. endfor
. endif
android_build_library "$(PROJECT.LIBNAME)" "${PROJECT_ROOT}"
) || exit 1
}
##
# Verify shared libraries in prefix
for library in "$(project.libname).so" "${DEPENDENCIES[@]}" ; do
android_build_verify_so "${library}"
done
android_build_verify_so "$(project.libname).so" "${DEPENDENCIES[@]}"
android_build_trace "Android build successful"
$(project.GENERATED_WARNING_HEADER:)
.close
.chmod_x ("builds/android/build.sh")
.#
.output "builds/android/ci_build.sh"
#!/usr/bin/env bash
$(project.GENERATED_WARNING_HEADER:)
#
# Exit if any step fails
set -e
# Use directory of current script as the working directory
cd "\$( dirname "${BASH_SOURCE[0]}" )"
# Configuration
export NDK_VERSION="${NDK_VERSION:-$(project.android_ndk_version)}"
export ANDROID_NDK_ROOT="${ANDROID_NDK_ROOT:-/tmp/${NDK_VERSION}}"
export MIN_SDK_VERSION=${MIN_SDK_VERSION:-$(project.android_min_sdk_version)}
export ANDROID_BUILD_DIR="${ANDROID_BUILD_DIR:-${PWD}/.build}"
export ANDROID_BUILD_CLEAN="${ANDROID_BUILD_CLEAN:-yes}"
export ANDROID_DEPENDENCIES_DIR="${ANDROID_DEPENDENCIES_DIR:-${PWD}/.deps}"
# Cleanup.
if [ "${ANDROID_BUILD_CLEAN}" = "yes" ] ; then
rm -rf "${ANDROID_BUILD_DIR}/prefix"
mkdir -p "${ANDROID_BUILD_DIR}/prefix"
rm -rf "${ANDROID_DEPENDENCIES_DIR}"
mkdir -p "${ANDROID_DEPENDENCIES_DIR}"
# Called shells MUST not clean after ourselves !
export ANDROID_BUILD_CLEAN="no"
fi
\./build.sh "arm"
\./build.sh "arm64"
\./build.sh "x86"
\./build.sh "x86_64"
$(project.GENERATED_WARNING_HEADER:)
.close
.chmod_x ("builds/android/ci_build.sh")
.#
.output "builds/android/android_build_helper.sh"
#!/usr/bin/env bash
$(project.GENERATED_WARNING_HEADER:)
#
# Courtesy of Joe Eli McIlvain; original code at:
# https://github.com/jemc/android_build_helper
# android_build_helper.sh
#
# The following is a helper script for setting up android builds for
# "native" libraries maintained with an autotools build system.
# It merely helps to create the proper cross-compile environment.
# It makes no attempt to wrap the library or make it accessible to Java code;
# the intention is to make the bare library available to other "native" code.
#
# To get the latest version of this script, please download from:
# https://github.com/jemc/android_build_helper
#
# You are free to modify and redistribute this script, but if you add
# improvements, please consider submitting a pull request or patch to the
# aforementioned upstream repository for the benefit of other users.
#
# This script is provided with no express or implied warranties.
#
########################################################################
# Utilities & helper functions
########################################################################
function android_build_trace {
if [ -n "${BUILD_ARCH}" ] ; then
echo "$(PROJECT.PREFIX) (${BUILD_ARCH}) - \$*"
else
echo "$(PROJECT.PREFIX) - \$*"
fi
}
.literal << .endliteral
function android_build_check_fail {
if [ ! ${#ANDROID_BUILD_FAIL[@]} -eq 0 ]; then
android_build_trace "Android build failed for the following reasons:"
for reason in "${ANDROID_BUILD_FAIL[@]}"; do
local formatted_reason=" ${reason}"
echo "${formatted_reason}"
done
exit 1
fi
}
function android_download_ndk {
if [ -d "${ANDROID_NDK_ROOT}" ] ; then
# NDK folder detected, let's assume it's valid ...
android_build_trace "Using existing NDK folder '${ANDROID_NDK_ROOT}'."
return
fi
if [ ! -d "$(dirname "${ANDROID_NDK_ROOT}")" ] ; then
ANDROID_BUILD_FAIL+=("Cannot download NDK in a non existing folder")
ANDROID_BUILD_FAIL+=(" $(dirname "${ANDROID_NDK_ROOT}/")")
fi
android_build_check_fail
local filename
local platform="$(uname | tr '[:upper:]' '[:lower:]')"
case "${platform}" in
linux*)
if [ "${NDK_NUMBER}" -ge 2300 ] ; then
# Since NDK 23, NDK archives are renamed.
filename=${NDK_VERSION}-linux.zip
else
filename=${NDK_VERSION}-linux-x86_64.zip
fi
;;
darwin*)
if [ "${NDK_NUMBER}" -ge 2300 ] ; then
# Since NDK 23, NDK archives are renamed.
filename=${NDK_VERSION}-darwin.zip
else
filename=${NDK_VERSION}-darwin-x86_64.zip
fi
;;
*) android_build_trace "Unsupported platform ('${platform}')" ; exit 1 ;;
esac
if [ -z "${filename}" ] ; then
ANDROID_BUILD_FAIL+=("Unable to detect NDK filename.")
fi
android_build_check_fail
android_build_trace "Downloading NDK '${NDK_VERSION}'..."
(
cd "$(dirname "${ANDROID_NDK_ROOT}")" \
&& rm -f "${filename}" \
&& wget -q "http://dl.google.com/android/repository/${filename}" -O "${filename}" \
&& android_build_trace "Extracting NDK '${filename}'..." \
&& unzip -q "${filename}" \
&& android_build_trace "NDK extracted under '${ANDROID_NDK_ROOT}'."
) || {
ANDROID_BUILD_FAIL+=("Failed to install NDK ('${NDK_VERSION}')")
ANDROID_BUILD_FAIL+=(" ${filename}")
}
android_build_check_fail
}
function android_build_set_env {
BUILD_ARCH=$1
local platform="$(uname | tr '[:upper:]' '[:lower:]')"
case "${platform}" in
linux*)
export ANDROID_BUILD_PLATFORM=linux-x86_64
;;
darwin*)
export ANDROID_BUILD_PLATFORM=darwin-x86_64
;;
*) android_build_trace "Unsupported platform ('${platform}')" ; exit 1 ;;
esac
export ANDROID_BUILD_TOOLCHAIN="${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/${ANDROID_BUILD_PLATFORM}"
export TOOLCHAIN_PATH="${ANDROID_BUILD_TOOLCHAIN}/bin"
# Set variables for each architecture
if [ "${BUILD_ARCH}" == "arm" ]; then
export TOOLCHAIN_HOST="arm-linux-androideabi"
export TOOLCHAIN_COMP="armv7a-linux-androideabi${MIN_SDK_VERSION}"
export TOOLCHAIN_ABI="armeabi-v7a"
export TOOLCHAIN_ARCH="arm"
elif [ "${BUILD_ARCH}" == "x86" ]; then
export TOOLCHAIN_HOST="i686-linux-android"
export TOOLCHAIN_COMP="i686-linux-android${MIN_SDK_VERSION}"
export TOOLCHAIN_ABI="x86"
export TOOLCHAIN_ARCH="x86"
elif [ "${BUILD_ARCH}" == "arm64" ]; then
export TOOLCHAIN_HOST="aarch64-linux-android"
export TOOLCHAIN_COMP="aarch64-linux-android${MIN_SDK_VERSION}"
export TOOLCHAIN_ABI="arm64-v8a"
export TOOLCHAIN_ARCH="arm64"
elif [ "${BUILD_ARCH}" == "x86_64" ]; then
export TOOLCHAIN_HOST="x86_64-linux-android"
export TOOLCHAIN_COMP="x86_64-linux-android${MIN_SDK_VERSION}"
export TOOLCHAIN_ABI="x86_64"
export TOOLCHAIN_ARCH="x86_64"
fi
# Since NDK r22 the "platforms" dir got removed
if [ -d "${ANDROID_NDK_ROOT}/platforms" ]; then
export ANDROID_BUILD_SYSROOT="${ANDROID_NDK_ROOT}/platforms/android-${MIN_SDK_VERSION}/arch-${TOOLCHAIN_ARCH}"
else
export ANDROID_BUILD_SYSROOT="${ANDROID_BUILD_TOOLCHAIN}/sysroot"
fi
export ANDROID_BUILD_PREFIX="${ANDROID_BUILD_DIR}/prefix/${TOOLCHAIN_ARCH}"
# Since NDK r25, libc++_shared.so is no more in 'sources/cxx-stl/...'
export ANDROID_STL="libc++_shared.so"
if [ -x "${ANDROID_NDK_ROOT}/sources/cxx-stl/llvm-libc++/libs/${TOOLCHAIN_ABI}/${ANDROID_STL}" ] ; then
export ANDROID_STL_ROOT="${ANDROID_NDK_ROOT}/sources/cxx-stl/llvm-libc++/libs/${TOOLCHAIN_ABI}"
else
export ANDROID_STL_ROOT="${ANDROID_BUILD_SYSROOT}/usr/lib/${TOOLCHAIN_HOST}"
# NDK 25 requires -L<path-to-libc.so> ...
# I don't understand why, but without it, ./configure fails to build a valid 'conftest'.
export ANDROID_LIBC_ROOT="${ANDROID_BUILD_SYSROOT}/usr/lib/${TOOLCHAIN_HOST}/${MIN_SDK_VERSION}"
fi
}
function android_build_env {
##
# Check that necessary environment variables are set
.endliteral
if [ -z "$ANDROID_NDK_ROOT" ]; then
ANDROID_BUILD_FAIL+=("Please set the ANDROID_NDK_ROOT environment variable")
ANDROID_BUILD_FAIL+=(" (eg. \\"/home/user/android/$(project.android_ndk_version)\\")")
fi
if [ -z "$ANDROID_BUILD_TOOLCHAIN" ]; then
ANDROID_BUILD_FAIL+=("Please set the ANDROID_BUILD_TOOLCHAIN environment variable")
ANDROID_BUILD_FAIL+=(" (eg. \\"/home/user/android/$(project.android_ndk_version)/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64\\")")
fi
if [ -z "$TOOLCHAIN_PATH" ]; then
ANDROID_BUILD_FAIL+=("Please set the TOOLCHAIN_PATH environment variable")
ANDROID_BUILD_FAIL+=(" (eg. \\"/home/user/android/$(project.android_ndk_version)/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin\\")")
fi
.literal << .endliteral
if [ -z "$TOOLCHAIN_HOST" ]; then
ANDROID_BUILD_FAIL+=("Please set the TOOLCHAIN_HOST environment variable")
ANDROID_BUILD_FAIL+=(" (eg. \"arm-linux-androideabi\")")
fi
if [ -z "$TOOLCHAIN_COMP" ]; then
ANDROID_BUILD_FAIL+=("Please set the TOOLCHAIN_COMP environment variable")
ANDROID_BUILD_FAIL+=(" (eg. \"armv7a-linux-androideabi\")")
fi
if [ -z "$TOOLCHAIN_ABI" ]; then
ANDROID_BUILD_FAIL+=("Please set the TOOLCHAIN_ABI environment variable")
ANDROID_BUILD_FAIL+=(" (eg. \"armeabi-v7a\")")
fi
if [ -z "$TOOLCHAIN_ARCH" ]; then
ANDROID_BUILD_FAIL+=("Please set the TOOLCHAIN_ARCH environment variable")
ANDROID_BUILD_FAIL+=(" (eg. \"arm\")")
fi
android_build_check_fail
##
# Check that directories given by environment variables exist
if [ ! -d "$ANDROID_NDK_ROOT" ]; then
ANDROID_BUILD_FAIL+=("The ANDROID_NDK_ROOT directory does not exist")
ANDROID_BUILD_FAIL+=(" ${ANDROID_NDK_ROOT}")
fi
if [ ! -d "$ANDROID_STL_ROOT" ]; then
ANDROID_BUILD_FAIL+=("The ANDROID_STL_ROOT directory does not exist")
ANDROID_BUILD_FAIL+=(" ${ANDROID_STL_ROOT}")
fi
if [ -n "${ANDROID_LIBC_ROOT}" ] && [ ! -d "${ANDROID_LIBC_ROOT}" ]; then
ANDROID_BUILD_FAIL+=("The ANDROID_LIBC_ROOT directory does not exist")
ANDROID_BUILD_FAIL+=(" ${ANDROID_LIBC_ROOT}")
fi
if [ ! -d "${ANDROID_BUILD_TOOLCHAIN}" ]; then
ANDROID_BUILD_FAIL+=("The ANDROID_BUILD_TOOLCHAIN directory does not exist")
ANDROID_BUILD_FAIL+=(" ${ANDROID_BUILD_TOOLCHAIN}")
fi
if [ ! -d "$TOOLCHAIN_PATH" ]; then
ANDROID_BUILD_FAIL+=("The TOOLCHAIN_PATH directory does not exist")
ANDROID_BUILD_FAIL+=(" ${TOOLCHAIN_PATH}")
fi
##
# Set up some local variables and check them
if [ ! -d "$ANDROID_BUILD_SYSROOT" ]; then
ANDROID_BUILD_FAIL+=("The ANDROID_BUILD_SYSROOT directory does not exist")
ANDROID_BUILD_FAIL+=(" ${ANDROID_BUILD_SYSROOT}")
fi
mkdir -p "$ANDROID_BUILD_PREFIX" || {
ANDROID_BUILD_FAIL+=("Failed to make ANDROID_BUILD_PREFIX directory")
ANDROID_BUILD_FAIL+=(" ${ANDROID_BUILD_PREFIX}")
}
android_build_check_fail
}
function _android_build_opts_process_binaries {
export ANDROID_BUILD_CC="${TOOLCHAIN_PATH}/${TOOLCHAIN_COMP}-clang"
export ANDROID_BUILD_CXX="${TOOLCHAIN_PATH}/${TOOLCHAIN_COMP}-clang++"
# Since NDK r22 the "platforms" dir got removed and the default linker is LLD
if [ -d "${ANDROID_NDK_ROOT}/platforms" ]; then
export ANDROID_BUILD_LD="${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-ld"
else
export ANDROID_BUILD_LD="${TOOLCHAIN_PATH}/ld"
fi
# Since NDK r24 this binary was removed due to LLVM being now the default
if [ ! -x "${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-as" ]; then
export ANDROID_BUILD_AS="${TOOLCHAIN_PATH}/llvm-as"
else
export ANDROID_BUILD_AS="${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-as"
fi
# Since NDK r23 those binaries were removed due to LLVM being now the default
if [ ! -x "${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-ar" ]; then
export ANDROID_BUILD_AR="${TOOLCHAIN_PATH}/llvm-ar"
export ANDROID_BUILD_RANLIB="${TOOLCHAIN_PATH}/llvm-ranlib"
export ANDROID_BUILD_STRIP="${TOOLCHAIN_PATH}/llvm-strip"
else
export ANDROID_BUILD_AR="${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-ar"
export ANDROID_BUILD_RANLIB="${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-ranlib"
export ANDROID_BUILD_STRIP="${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-strip"
fi
if [ ! -x "${ANDROID_BUILD_CC}" ]; then
ANDROID_BUILD_FAIL+=("The CC binary does not exist or is not executable")
ANDROID_BUILD_FAIL+=(" ${ANDROID_BUILD_CC}")
fi
if [ ! -x "${ANDROID_BUILD_CXX}" ]; then
ANDROID_BUILD_FAIL+=("The CXX binary does not exist or is not executable")
ANDROID_BUILD_FAIL+=(" ${ANDROID_BUILD_CXX}")
fi
if [ ! -x "${ANDROID_BUILD_LD}" ]; then
ANDROID_BUILD_FAIL+=("The LD binary does not exist or is not executable")
ANDROID_BUILD_FAIL+=(" ${ANDROID_BUILD_LD}")
fi
if [ ! -x "${ANDROID_BUILD_AS}" ]; then
ANDROID_BUILD_FAIL+=("The AS binary does not exist or is not executable")
ANDROID_BUILD_FAIL+=(" ${ANDROID_BUILD_AS}")
fi
if [ ! -x "${ANDROID_BUILD_AR}" ]; then
ANDROID_BUILD_FAIL+=("The AR binary does not exist or is not executable")
ANDROID_BUILD_FAIL+=(" ${ANDROID_BUILD_AR}")
fi
if [ ! -x "${ANDROID_BUILD_RANLIB}" ]; then
ANDROID_BUILD_FAIL+=("The RANLIB binary does not exist or is not executable")
ANDROID_BUILD_FAIL+=(" ${ANDROID_BUILD_RANLIB}")
fi
if [ ! -x "${ANDROID_BUILD_STRIP}" ]; then
ANDROID_BUILD_FAIL+=("The STRIP binary does not exist or is not executable")
ANDROID_BUILD_FAIL+=(" ${ANDROID_BUILD_STRIP}")
fi
ANDROID_BUILD_OPTS+=("TOOLCHAIN=${ANDROID_BUILD_TOOLCHAIN}")
ANDROID_BUILD_OPTS+=("CC=${ANDROID_BUILD_CC}")
ANDROID_BUILD_OPTS+=("CXX=${ANDROID_BUILD_CXX}")
ANDROID_BUILD_OPTS+=("LD=${ANDROID_BUILD_LD}")
ANDROID_BUILD_OPTS+=("AS=${ANDROID_BUILD_AS}")
ANDROID_BUILD_OPTS+=("AR=${ANDROID_BUILD_AR}")
ANDROID_BUILD_OPTS+=("RANLIB=${ANDROID_BUILD_RANLIB}")
ANDROID_BUILD_OPTS+=("STRIP=${ANDROID_BUILD_STRIP}")
android_build_check_fail
}
# Set the ANDROID_BUILD_OPTS variable to a bash array of configure options
function android_build_opts {
ANDROID_BUILD_OPTS=()
_android_build_opts_process_binaries
# Since NDK r23 we don't need -lgcc due to LLVM being now the default
if [ ! -x "${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-ar" ]; then
export ANDROID_BUILD_LIBS="-lc -ldl -lm -llog -lc++_shared"
else
export ANDROID_BUILD_LIBS="-lc -lgcc -ldl -lm -llog -lc++_shared"
fi
export ANDROID_BUILD_LDFLAGS="-L${ANDROID_BUILD_PREFIX}/lib"
if [ -n "${ANDROID_LIBC_ROOT}" ] ; then
ANDROID_BUILD_LDFLAGS+=" -L${ANDROID_LIBC_ROOT}"
fi
ANDROID_BUILD_LDFLAGS+=" -L${ANDROID_STL_ROOT}"
export ANDROID_BUILD_CFLAGS+=" -D_GNU_SOURCE -D_REENTRANT -D_THREAD_SAFE"
export ANDROID_BUILD_CPPFLAGS+=" -I${ANDROID_BUILD_PREFIX}/include"
if [ "${NDK_NUMBER}" -ge 2400 ] ; then
if [ "${BUILD_ARCH}" = "arm64" ] ; then
export ANDROID_BUILD_CXXFLAGS+=" -mno-outline-atomics"
fi
fi
ANDROID_BUILD_OPTS+=("CFLAGS=${ANDROID_BUILD_CFLAGS} ${ANDROID_BUILD_EXTRA_CFLAGS}")
ANDROID_BUILD_OPTS+=("CPPFLAGS=${ANDROID_BUILD_CPPFLAGS} ${ANDROID_BUILD_EXTRA_CPPFLAGS}")
ANDROID_BUILD_OPTS+=("CXXFLAGS=${ANDROID_BUILD_CXXFLAGS} ${ANDROID_BUILD_EXTRA_CXXFLAGS}")
ANDROID_BUILD_OPTS+=("LDFLAGS=${ANDROID_BUILD_LDFLAGS} ${ANDROID_BUILD_EXTRA_LDFLAGS}")
ANDROID_BUILD_OPTS+=("LIBS=${ANDROID_BUILD_LIBS} ${ANDROID_BUILD_EXTRA_LIBS}")
ANDROID_BUILD_OPTS+=("PKG_CONFIG_LIBDIR=${ANDROID_NDK_ROOT}/prebuilt/${ANDROID_BUILD_PLATFORM}/lib/pkgconfig")
ANDROID_BUILD_OPTS+=("PKG_CONFIG_PATH=${ANDROID_BUILD_PREFIX}/lib/pkgconfig")
ANDROID_BUILD_OPTS+=("PKG_CONFIG_SYSROOT_DIR=${ANDROID_BUILD_SYSROOT}")
ANDROID_BUILD_OPTS+=("PKG_CONFIG_DIR=")
ANDROID_BUILD_OPTS+=("--with-sysroot=${ANDROID_BUILD_SYSROOT}")
ANDROID_BUILD_OPTS+=("--host=${TOOLCHAIN_HOST}")
ANDROID_BUILD_OPTS+=("--prefix=${ANDROID_BUILD_PREFIX}")
android_build_check_fail
}
# Parse readelf output to verify the correct linking of libraries.
# The first argument should be the soname of the newly built library.
# The rest of the arguments should be the sonames of dependencies.
# All sonames should be unversioned for android (no trailing numbers).
function android_build_verify_so {
local soname="$1"
shift # Get rid of first argument - the rest represent dependencies
local sofile="${ANDROID_BUILD_PREFIX}/lib/${soname}"
if [ ! -f "${sofile}" ]; then
ANDROID_BUILD_FAIL+=("Found no library named ${soname}")
ANDROID_BUILD_FAIL+=(" ${sofile}")
fi
android_build_check_fail
local readelf="${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-readelf"
if command -v "${readelf}" >/dev/null 2>&1 ; then
export ANDROID_BUILD_READELF="${readelf}"
elif command -v readelf >/dev/null 2>&1 ; then
export ANDROID_BUILD_READELF="readelf"
elif command -v greadelf >/dev/null 2>&1 ; then
export ANDROID_BUILD_READELF="greadelf"
else
ANDROID_BUILD_FAIL+=("Could not find any of readelf, greadelf, or ${readelf}")
fi
android_build_check_fail
local elfoutput
elfoutput=$(LC_ALL=C ${ANDROID_BUILD_READELF} -d "${sofile}")
local soname_regexp='soname: \[([[:alnum:]\.]+)\]'
if [[ $elfoutput =~ $soname_regexp ]]; then
local parsed_soname="${BASH_REMATCH[1]}"
if [ "${parsed_soname}" != "${soname}" ]; then
ANDROID_BUILD_FAIL+=("Actual soname of library ${soname} is incorrect (or versioned):")
ANDROID_BUILD_FAIL+=(" ${parsed_soname}")
fi
else
ANDROID_BUILD_FAIL+=("Failed to meaningfully parse readelf output for library ${soname}:")
ANDROID_BUILD_FAIL+=(" ${elfoutput}")
fi
for dep_soname in "$@" ; do
local dep_sofile="${ANDROID_BUILD_PREFIX}/lib/${dep_soname}"
if [ ! -f "${dep_sofile}" ]; then
ANDROID_BUILD_FAIL+=("Found no library named ${dep_soname}")
ANDROID_BUILD_FAIL+=(" ${dep_sofile}")
elif [[ $elfoutput != *"library: [${dep_soname}]"* ]]; then
ANDROID_BUILD_FAIL+=("Library ${soname} was expected to be linked to library with soname:")
ANDROID_BUILD_FAIL+=(" ${dep_soname}")
fi
done
android_build_check_fail
}
function android_show_configure_opts {
local tag=$1
shift
android_build_trace "./configure options to build '${tag}':"
for opt in "$@"; do
echo " > ${opt}"
done
echo ""
}
# Initialize env variable XXX_ROOT, given dependency name "xxx".
# If XXX_ROOT is not set:
# If ${PROJECT_ROOT}/../xxx exists
# set XXX_ROOT with it.
# Else
# set XXX_ROOT with ${ANDROID_DEPENDENCIES_DIR}/xxx.
# Else
# Verify that folder XXX_ROOT exists.
function android_init_dependency_root {
local lib_name
lib_name="$1"
local variable_name
variable_name="$(echo "${lib_name}" | tr '[:lower:]' '[:upper:]')_ROOT"
local variable_value
variable_value="$(eval echo "\${${variable_name}}")"
if [ -z "${PROJECT_ROOT}" ] ; then
android_build_trace "Error: Variable PROJECT_ROOT is not set."
exit 1
fi
if [ ! -d "${PROJECT_ROOT}" ] ; then
android_build_trace "Error: Cannot find folder '${PROJECT_ROOT}'."
exit 1
fi
if [ -z "${variable_value}" ] ; then
if [ -d "${PROJECT_ROOT}/../${lib_name}" ] ; then
eval "export ${variable_name}=\"$(cd "${PROJECT_ROOT}/../${lib_name}" && pwd)\""
else
eval "export ${variable_name}=\"${ANDROID_DEPENDENCIES_DIR}/${lib_name}\""
fi
variable_value="$(eval echo "\${${variable_name}}")"
elif [ ! -d "${variable_value}" ] ; then
android_build_trace "Error: Folder '${variable_value}' does not exist."
exit 1
fi
android_build_trace "${variable_name}=${variable_value}"
}
function android_download_library {
local tag="$1" ; shift
local root="$1" ; shift
local url="$1" ; shift
local parent="$(dirname "${root}")"
local archive="$(basename "${url}")"
mkdir -p "${parent}"
cd "${parent}"
android_build_trace "Downloading ${tag} from '${url}' ..."
rm -f "${archive}"
wget -q "${url}"
case "${archive}" in
*."tar.gz" ) folder="$(basename "${archive}" ".tar.gz")" ;;
*."tgz" ) folder="$(basename "${archive}" ".tgz")" ;;
* ) android_build_trace "Unsupported extension for '${archive}'." ; exit 1 ;;
esac
android_build_trace "Extracting '${archive}' ..."
tar -xzf "${archive}"
if [ ! -d "${root}" ] ; then
mv "${folder}" "${root}"
fi
android_build_trace "${tag} extracted under under '${root}'."
}
function android_clone_library {
local tag="$1" ; shift
local root="$1" ; shift
local url="$1" ; shift
local branch="$1" ; shift
mkdir -p "$(dirname "${root}")"
if [ -n "${branch}" ] ; then
android_build_trace "Cloning '${url}' (branch '${branch}') under '${root}'."
git clone --quiet --depth 1 -b "${branch}" "${url}" "${root}"
else
android_build_trace "Cloning '${url}' (default branch) under '${root}'."
git clone --quiet --depth 1 "${url}" "${root}"
fi
( cd "${root}" && git log --oneline -n 1) || exit 1
}
# Caller must set CONFIG_OPTS[], before call.
function android_build_library {
local tag=$1 ; shift
local root=$1 ; shift
android_build_trace "Cleaning library '${tag}'."
(
if [ -n "${ANDROID_BUILD_PREFIX}" ] && [ -d "${ANDROID_BUILD_PREFIX}" ] ; then
# Remove *.la files as they might cause errors with cross compiled libraries
find "${ANDROID_BUILD_PREFIX}" -name '*.la' -exec rm {} +
fi
cd "${root}" \
&& ( make clean || : ) \
&& rm -f config.status
) &> /dev/null
android_build_trace "Building library '${tag}'."
(
set -e
android_show_configure_opts "${tag}" "${CONFIG_OPTS[@]}"
cd "${root}"
if [ -e autogen.sh ]; then
./autogen.sh 2> /dev/null
fi
if [ -e buildconf ]; then
./buildconf 2> /dev/null
fi
if [ ! -e autogen.sh ] && [ ! -e buildconf ] && [ ! -e ./configure ] && [ -s ./configure.ac ] ; then
libtoolize --copy --force && \
aclocal -I . && \
autoheader && \
automake --add-missing --copy && \
autoconf || \
autoreconf -fiv
fi
./configure "${CONFIG_OPTS[@]}"
make -j 4
make install
)
}
########################################################################
# Initialization
########################################################################
# Get directory of current script (if not already set)
# This directory is also the basis for the build directories the get created.
if [ -z "$ANDROID_BUILD_DIR" ]; then
export ANDROID_BUILD_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
fi
# Where to download our dependencies
export ANDROID_DEPENDENCIES_DIR="${ANDROID_DEPENDENCIES_DIR:-/tmp/tmp-deps}"
# Set up a variable to hold the global failure reasons, separated by newlines
# (Empty string indicates no failure)
ANDROID_BUILD_FAIL=()
########################################################################
# Sanity checks
########################################################################
case "${NDK_VERSION}" in
"android-ndk-r"[0-9][0-9] ) : ;;
"android-ndk-r"[0-9][0-9][a-z] ) : ;;
"" ) android_build_trace "Variable NDK_VERSION not set." ; exit 1 ;;
* ) android_build_trace "Invalid format for NDK_VERSION ('${NDK_VERSION}')" ; exit 1 ;;
esac
if [ -z "${ANDROID_NDK_ROOT}" ] ; then
android_build_trace "ANDROID_NDK_ROOT not set !"
exit 1
fi
########################################################################
# Compute NDK version into a numeric form:
# android-ndk-r21e -> 2105
# android-ndk-r25 -> 2500
########################################################################
export NDK_NUMBER="$(( $(echo "${NDK_VERSION}"|sed -e 's|android-ndk-r||g' -e 's|[a-z]||g') * 100 ))"
NDK_VERSION_LETTER="$(echo "${NDK_VERSION}"|sed -e 's|android-ndk-r[0-9][0-9]||g'|tr '[:lower:]' '[:upper:]')"
if [ -n "${NDK_VERSION_LETTER}" ] ; then
NDK_NUMBER=$(( $(( NDK_NUMBER + $(printf '%d' \'"${NDK_VERSION_LETTER}") )) - 64 ))
fi
android_build_trace "Configured NDK_VERSION: ${NDK_VERSION} ($NDK_NUMBER)."
.endliteral
$(project.GENERATED_WARNING_HEADER:)
.close
.chmod_x ("builds/android/android_build_helper.sh")
.endmacro
# Set default NDK and min SDK.
project.android_ndk_version ?= "android-ndk-r25"
project.android_min_sdk_version ?= 21