3 # See NOTES.ANDROID 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 x86 => "i686-linux-android",
16 x86_64 => "x86_64-linux-android",
20 unless (%$android_ndk) {
21 if ($now_printing =~ m|^android|) {
22 return $android_ndk = { bn_ops => "BN_AUTO" };
25 my $ndk = $ENV{ANDROID_NDK};
26 die "\$ANDROID_NDK is not defined" if (!$ndk);
27 if (!-d "$ndk/platforms" && !-f "$ndk/AndroidVersion.txt") {
28 # $ndk/platforms is traditional "all-inclusive" NDK, while
29 # $ndk/AndroidVersion.txt is so-called standalone toolchain
30 # tailored for specific target down to API level.
31 die "\$ANDROID_NDK=$ndk is invalid";
33 $ndk = canonpath($ndk);
37 if (open my $fh, "<$ndk/source.properties") {
40 if (m|Pkg\.Revision\s*=\s*([0-9]+)|) {
48 my ($sysroot, $api, $arch);
50 $config{target} =~ m|[^-]+-([^-]+)$|; # split on dash
53 if ($sysroot = $ENV{CROSS_SYSROOT}) {
54 $sysroot =~ m|/android-([0-9]+)/arch-(\w+)/?$|;
55 ($api, $arch) = ($1, $2);
56 } elsif (-f "$ndk/AndroidVersion.txt") {
57 $sysroot = "$ndk/sysroot";
61 # see if user passed -D__ANDROID_API__=N
62 foreach (@{$useradd{CPPDEFINES}}, @{$user{CPPFLAGS}}) {
63 if (m|__ANDROID_API__=([0-9]+)|) {
69 # list available platforms (numerically)
70 my @platforms = sort { $a =~ m/-([0-9]+)$/; my $aa = $1;
71 $b =~ m/-([0-9]+)$/; $aa <=> $1;
72 } glob("$ndk/platforms/android-$api");
73 die "no $ndk/platforms/android-$api" if ($#platforms < 0);
75 $sysroot = "@platforms[$#platforms]/arch-$arch";
77 die "no sysroot=$sysroot" if (!-d $sysroot);
79 my $triarch = $triplet{$arch};
83 # see if there is NDK clang on $PATH, "universal" or "standalone"
84 if (which("clang") =~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
86 # harmonize with gcc default
87 my $arm = $ndkver > 16 ? "armv7a" : "armv5te";
88 (my $tridefault = $triarch) =~ s/^arm-/$arm-/;
89 (my $tritools = $triarch) =~ s/(?:x|i6)86(_64)?-.*/x86$1/;
90 $cflags .= " -target $tridefault "
91 . "-gcc-toolchain \$(ANDROID_NDK)/toolchains"
92 . "/$tritools-4.9/prebuilt/$host";
93 $user{CC} = "clang" if ($user{CC} !~ m|clang|);
94 $user{CROSS_COMPILE} = undef;
95 } elsif (-f "$ndk/AndroidVersion.txt") { #"standalone toolchain"
96 my $cc = $user{CC} // "clang";
97 # One can probably argue that both clang and gcc should be
98 # probed, but support for "standalone toolchain" was added
99 # *after* announcement that gcc is being phased out, so
100 # favouring clang is considered adequate. Those who insist
101 # have option to enforce test for gcc with CC=gcc.
102 if (which("$triarch-$cc") !~ m|^$ndk|) {
103 die "no NDK $triarch-$cc on \$PATH";
106 $user{CROSS_COMPILE} = "$triarch-";
107 } elsif ($user{CC} eq "clang") {
108 die "no NDK clang on \$PATH";
110 if (which("$triarch-gcc") !~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
111 die "no NDK $triarch-gcc on \$PATH";
113 $cflags .= " -mandroid";
114 $user{CROSS_COMPILE} = "$triarch-";
117 if (!-d "$sysroot/usr/include") {
118 my $incroot = "$ndk/sysroot/usr/include";
119 die "no $incroot" if (!-d $incroot);
120 die "no $incroot/$triarch" if (!-d "$incroot/$triarch");
121 $incroot =~ s|^$ndk/||;
122 $cppflags = "-D__ANDROID_API__=$api";
123 $cppflags .= " -isystem \$(ANDROID_NDK)/$incroot/$triarch";
124 $cppflags .= " -isystem \$(ANDROID_NDK)/$incroot";
127 $sysroot =~ s|^$ndk/||;
129 cflags => "$cflags --sysroot=\$(ANDROID_NDK)/$sysroot",
130 cppflags => $cppflags,
131 bn_ops => $arch =~ m/64$/ ? "SIXTY_FOUR_BIT_LONG"
142 inherit_from => [ "linux-generic32" ],
144 ################################################################
145 # Special note about -pie. The underlying reason is that
146 # Lollipop refuses to run non-PIE. But what about older systems
147 # and NDKs? -fPIC was never problem, so the only concern is -pie.
148 # Older toolchains, e.g. r4, appear to handle it and binaries
149 # turn out mostly functional. "Mostly" means that oldest
150 # Androids, such as Froyo, fail to handle executable, but newer
151 # systems are perfectly capable of executing binaries targeting
152 # Froyo. Keep in mind that in the nutshell Android builds are
153 # about JNI, i.e. shared libraries, not applications.
154 cflags => add(sub { android_ndk()->{cflags} }),
155 cppflags => add(sub { android_ndk()->{cppflags} }),
156 cxxflags => add(sub { android_ndk()->{cflags} }),
157 bn_ops => sub { android_ndk()->{bn_ops} },
158 bin_cflags => "-pie",
162 ################################################################
163 # Contemporary Android applications can provide multiple JNI
164 # providers in .apk, targeting multiple architectures. Among
165 # them there is "place" for two ARM flavours: generic eabi and
166 # armv7-a/hard-float. However, it should be noted that OpenSSL's
167 # ability to engage NEON is not constrained by ABI choice, nor
168 # is your ability to call OpenSSL from your application code
169 # compiled with floating-point ABI other than default 'soft'.
170 # (Latter thanks to __attribute__((pcs("aapcs"))) declaration.)
171 # This means that choice of ARM libraries you provide in .apk
172 # is driven by application needs. For example if application
173 # itself benefits from NEON or is floating-point intensive, then
174 # it might be appropriate to provide both libraries. Otherwise
175 # just generic eabi would do. But in latter case it would be
178 # ./Configure android-arm -D__ARM_MAX_ARCH__=8
180 # in order to build "universal" binary and allow OpenSSL take
181 # advantage of NEON when it's available.
183 # Keep in mind that (just like with linux-armv4) we rely on
184 # compiler defaults, which is not necessarily what you had
185 # in mind, in which case you would have to pass additional
186 # -march and/or -mfloat-abi flags. NDK defaults to armv5te.
187 # Newer NDK versions reportedly require additional -latomic.
189 inherit_from => [ "android", asm("armv4_asm") ],
190 bn_ops => add("RC4_CHAR"),
193 inherit_from => [ "android", asm("aarch64_asm") ],
194 bn_ops => add("RC4_CHAR"),
195 perlasm_scheme => "linux64",
199 inherit_from => [ "android", asm("mips32_asm") ],
200 bn_ops => add("RC4_CHAR"),
201 perlasm_scheme => "o32",
203 "android-mips64" => {
204 ################################################################
205 # You are more than likely have to specify target processor
206 # on ./Configure command line. Trouble is that toolchain's
207 # default is MIPS64r6 (at least in r10d), but there are no
208 # such processors around (or they are too rare to spot one).
209 # Actual problem is that MIPS64r6 is binary incompatible
210 # with previous MIPS ISA versions, in sense that unlike
211 # prior versions original MIPS binary code will fail.
213 inherit_from => [ "android", asm("mips64_asm") ],
214 bn_ops => add("RC4_CHAR"),
215 perlasm_scheme => "64",
219 inherit_from => [ "android", asm("x86_asm") ],
220 CFLAGS => add(picker(release => "-fomit-frame-pointer")),
221 bn_ops => add("RC4_INT"),
222 perlasm_scheme => "android",
224 "android-x86_64" => {
225 inherit_from => [ "android", asm("x86_64_asm") ],
226 bn_ops => add("RC4_INT"),
227 perlasm_scheme => "elf",
230 ####################################################################
231 # Backward compatible targets, (might) requre $CROSS_SYSROOT
233 "android-armeabi" => {
234 inherit_from => [ "android-arm" ],
237 inherit_from => [ "android" ],
239 "android64-aarch64" => {
240 inherit_from => [ "android-arm64" ],
242 "android64-x86_64" => {
243 inherit_from => [ "android-x86_64" ],
245 "android64-mips64" => {
246 inherit_from => [ "android-mips64" ],