3 # See NOTES-Android.md for details, and don't miss platform-specific
7 use File::Spec::Functions;
11 arm => "arm-linux-androideabi",
12 arm64 => "aarch64-linux-android",
13 mips => "mipsel-linux-android",
14 mips64 => "mips64el-linux-android",
15 riscv64 => "riscv64-linux-android",
16 x86 => "i686-linux-android",
17 x86_64 => "x86_64-linux-android",
21 unless (%$android_ndk) {
22 if ($now_printing =~ m|^android|) {
23 return $android_ndk = { bn_ops => "BN_AUTO" };
28 foreach (qw(ANDROID_NDK_ROOT ANDROID_NDK)) {
30 $ndk = $ENV{$ndk_var};
33 die "\$ANDROID_NDK_ROOT is not defined" if (!$ndk);
34 my $is_standalone_toolchain = -f "$ndk/AndroidVersion.txt";
35 my $ndk_src_props = "$ndk/source.properties";
36 my $is_ndk = -f $ndk_src_props;
37 if ($is_ndk == $is_standalone_toolchain) {
38 die "\$ANDROID_NDK_ROOT=$ndk is invalid";
40 $ndk = canonpath($ndk);
44 if (open my $fh, "<$ndk_src_props") {
47 if (m|Pkg\.Revision\s*=\s*([0-9]+)|) {
55 my ($sysroot, $api, $arch);
57 $config{target} =~ m|[^-]+-([^-]+)$|; # split on dash
60 if ($sysroot = $ENV{CROSS_SYSROOT}) {
61 $sysroot =~ m|/android-([0-9]+)/arch-(\w+)/?$|;
62 ($api, $arch) = ($1, $2);
63 } elsif ($is_standalone_toolchain) {
64 $sysroot = "$ndk/sysroot";
68 # see if user passed -D__ANDROID_API__=N
69 foreach (@{$useradd{CPPDEFINES}}, @{$user{CPPFLAGS}}) {
70 if (m|__ANDROID_API__=([0-9]+)|) {
76 if (-d "$ndk/platforms") {
77 # list available platforms (numerically)
78 my @platforms = sort { $a =~ m/-([0-9]+)$/; my $aa = $1;
79 $b =~ m/-([0-9]+)$/; $aa <=> $1;
80 } glob("$ndk/platforms/android-$api");
81 die "no $ndk/platforms/android-$api" if ($#platforms < 0);
83 $sysroot = "@platforms[$#platforms]/arch-$arch";
84 $sysroot =~ m|/android-([0-9]+)/arch-$arch|;
86 } elsif ($api eq "*") {
87 # r22 Removed platforms dir, use this JSON file
88 my $path = "$ndk/meta/platforms.json";
89 open my $fh, $path or die "Could not open '$path' $!";
91 if (/"max": (\d+),/) {
98 die "Could not get default API Level" if ($api eq "*");
100 die "no sysroot=$sysroot" if (length $sysroot && !-d $sysroot);
102 my $triarch = $triplet{$arch};
106 # see if there is NDK clang on $PATH, "universal" or "standalone"
107 if (which("clang") =~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
109 # harmonize with gcc default
110 my $arm = $ndkver > 16 ? "armv7a" : "armv5te";
111 (my $tridefault = $triarch) =~ s/^arm-/$arm-/;
112 (my $tritools = $triarch) =~ s/(?:x|i6)86(_64)?-.*/x86$1/;
113 if (length $sysroot) {
114 $cflags .= " -target $tridefault "
115 . "-gcc-toolchain \$($ndk_var)/toolchains"
116 . "/$tritools-4.9/prebuilt/$host";
117 $user{CC} = "clang" if ($user{CC} !~ m|clang|);
119 $user{CC} = "$tridefault$api-clang";
121 $user{CROSS_COMPILE} = undef;
122 if (which("llvm-ar") =~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
123 $user{AR} = "llvm-ar";
124 $user{ARFLAGS} = [ "rs" ];
127 } elsif ($is_standalone_toolchain) {
128 my $cc = $user{CC} // "clang";
129 # One can probably argue that both clang and gcc should be
130 # probed, but support for "standalone toolchain" was added
131 # *after* announcement that gcc is being phased out, so
132 # favouring clang is considered adequate. Those who insist
133 # have option to enforce test for gcc with CC=gcc.
134 if (which("$triarch-$cc") !~ m|^$ndk|) {
135 die "no NDK $triarch-$cc on \$PATH";
138 $user{CROSS_COMPILE} = "$triarch-";
139 } elsif ($user{CC} eq "clang") {
140 die "no NDK clang on \$PATH";
142 if (which("$triarch-gcc") !~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
143 die "no NDK $triarch-gcc on \$PATH";
145 $cflags .= " -mandroid";
146 $user{CROSS_COMPILE} = "$triarch-";
149 if (length $sysroot) {
150 if (!-d "$sysroot/usr/include") {
151 my $incroot = "$ndk/sysroot/usr/include";
152 die "no $incroot" if (!-d $incroot);
153 die "no $incroot/$triarch" if (!-d "$incroot/$triarch");
154 $incroot =~ s|^$ndk/||;
155 $cppflags = "-D__ANDROID_API__=$api";
156 $cppflags .= " -isystem \$($ndk_var)/$incroot/$triarch";
157 $cppflags .= " -isystem \$($ndk_var)/$incroot";
159 $sysroot =~ s|^$ndk/||;
160 $sysroot = " --sysroot=\$($ndk_var)/$sysroot";
163 cflags => $cflags . $sysroot,
164 cppflags => $cppflags,
165 bn_ops => $arch =~ m/64$/ ? "SIXTY_FOUR_BIT_LONG"
176 inherit_from => [ "linux-generic32" ],
178 ################################################################
179 # Special note about -pie. The underlying reason is that
180 # Lollipop refuses to run non-PIE. But what about older systems
181 # and NDKs? -fPIC was never problem, so the only concern is -pie.
182 # Older toolchains, e.g. r4, appear to handle it and binaries
183 # turn out mostly functional. "Mostly" means that oldest
184 # Androids, such as Froyo, fail to handle executable, but newer
185 # systems are perfectly capable of executing binaries targeting
186 # Froyo. Keep in mind that in the nutshell Android builds are
187 # about JNI, i.e. shared libraries, not applications.
188 cflags => add(sub { android_ndk()->{cflags} }),
189 cppflags => add(sub { android_ndk()->{cppflags} }),
190 cxxflags => add(sub { android_ndk()->{cflags} }),
191 bn_ops => sub { android_ndk()->{bn_ops} },
192 bin_cflags => "-fPIE",
193 bin_lflags => "-pie",
195 shared_extension => ".so",
198 ################################################################
199 # Contemporary Android applications can provide multiple JNI
200 # providers in .apk, targeting multiple architectures. Among
201 # them there is "place" for two ARM flavours: generic eabi and
202 # armv7-a/hard-float. However, it should be noted that OpenSSL's
203 # ability to engage NEON is not constrained by ABI choice, nor
204 # is your ability to call OpenSSL from your application code
205 # compiled with floating-point ABI other than default 'soft'.
206 # (Latter thanks to __attribute__((pcs("aapcs"))) declaration.)
207 # This means that choice of ARM libraries you provide in .apk
208 # is driven by application needs. For example if application
209 # itself benefits from NEON or is floating-point intensive, then
210 # it might be appropriate to provide both libraries. Otherwise
211 # just generic eabi would do. But in latter case it would be
214 # ./Configure android-arm -D__ARM_MAX_ARCH__=8
216 # in order to build "universal" binary and allow OpenSSL take
217 # advantage of NEON when it's available.
219 # Keep in mind that (just like with linux-armv4) we rely on
220 # compiler defaults, which is not necessarily what you had
221 # in mind, in which case you would have to pass additional
222 # -march and/or -mfloat-abi flags. NDK defaults to armv5te.
223 # Newer NDK versions reportedly require additional -latomic.
225 inherit_from => [ "android" ],
226 bn_ops => add("RC4_CHAR"),
228 perlasm_scheme => "void",
231 inherit_from => [ "android" ],
232 bn_ops => add("RC4_CHAR"),
233 asm_arch => 'aarch64',
234 perlasm_scheme => "linux64",
238 inherit_from => [ "android" ],
239 bn_ops => add("RC4_CHAR"),
240 asm_arch => 'mips32',
241 perlasm_scheme => "o32",
243 "android-mips64" => {
244 ################################################################
245 # You are more than likely have to specify target processor
246 # on ./Configure command line. Trouble is that toolchain's
247 # default is MIPS64r6 (at least in r10d), but there are no
248 # such processors around (or they are too rare to spot one).
249 # Actual problem is that MIPS64r6 is binary incompatible
250 # with previous MIPS ISA versions, in sense that unlike
251 # prior versions original MIPS binary code will fail.
253 inherit_from => [ "android" ],
254 bn_ops => add("RC4_CHAR"),
255 asm_arch => 'mips64',
256 perlasm_scheme => "64",
260 inherit_from => [ "android" ],
261 CFLAGS => add(picker(release => "-fomit-frame-pointer")),
262 bn_ops => add("RC4_INT"),
264 perlasm_scheme => "android",
265 ex_libs => add(threads("-latomic")),
267 "android-x86_64" => {
268 inherit_from => [ "android" ],
269 bn_ops => add("RC4_INT"),
270 asm_arch => 'x86_64',
271 perlasm_scheme => "elf",
274 "android-riscv64" => {
275 inherit_from => [ "android" ],
276 asm_arch => 'riscv64',
277 perlasm_scheme => "linux64",
280 ####################################################################
281 # Backward compatible targets, (might) require $CROSS_SYSROOT
283 "android-armeabi" => {
284 inherit_from => [ "android-arm" ],
287 inherit_from => [ "android" ],
289 "android64-aarch64" => {
290 inherit_from => [ "android-arm64" ],
292 "android64-x86_64" => {
293 inherit_from => [ "android-x86_64" ],
295 "android64-mips64" => {
296 inherit_from => [ "android-mips64" ],