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" };
27 foreach (qw(ANDROID_NDK_HOME ANDROID_NDK)) {
29 $ndk = $ENV{$ndk_var};
32 die "\$ANDROID_NDK_HOME is not defined" if (!$ndk);
33 if (!-d "$ndk/platforms" && !-f "$ndk/AndroidVersion.txt") {
34 # $ndk/platforms is traditional "all-inclusive" NDK, while
35 # $ndk/AndroidVersion.txt is so-called standalone toolchain
36 # tailored for specific target down to API level.
37 die "\$ANDROID_NDK_HOME=$ndk is invalid";
39 $ndk = canonpath($ndk);
43 if (open my $fh, "<$ndk/source.properties") {
46 if (m|Pkg\.Revision\s*=\s*([0-9]+)|) {
54 my ($sysroot, $api, $arch);
56 $config{target} =~ m|[^-]+-([^-]+)$|; # split on dash
59 if ($sysroot = $ENV{CROSS_SYSROOT}) {
60 $sysroot =~ m|/android-([0-9]+)/arch-(\w+)/?$|;
61 ($api, $arch) = ($1, $2);
62 } elsif (-f "$ndk/AndroidVersion.txt") {
63 $sysroot = "$ndk/sysroot";
67 # see if user passed -D__ANDROID_API__=N
68 foreach (@{$useradd{CPPDEFINES}}, @{$user{CPPFLAGS}}) {
69 if (m|__ANDROID_API__=([0-9]+)|) {
75 # list available platforms (numerically)
76 my @platforms = sort { $a =~ m/-([0-9]+)$/; my $aa = $1;
77 $b =~ m/-([0-9]+)$/; $aa <=> $1;
78 } glob("$ndk/platforms/android-$api");
79 die "no $ndk/platforms/android-$api" if ($#platforms < 0);
81 $sysroot = "@platforms[$#platforms]/arch-$arch";
82 $sysroot =~ m|/android-([0-9]+)/arch-$arch|;
85 die "no sysroot=$sysroot" if (!-d $sysroot);
87 my $triarch = $triplet{$arch};
91 # see if there is NDK clang on $PATH, "universal" or "standalone"
92 if (which("clang") =~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
94 # harmonize with gcc default
95 my $arm = $ndkver > 16 ? "armv7a" : "armv5te";
96 (my $tridefault = $triarch) =~ s/^arm-/$arm-/;
97 (my $tritools = $triarch) =~ s/(?:x|i6)86(_64)?-.*/x86$1/;
98 $cflags .= " -target $tridefault "
99 . "-gcc-toolchain \$($ndk_var)/toolchains"
100 . "/$tritools-4.9/prebuilt/$host";
101 $user{CC} = "clang" if ($user{CC} !~ m|clang|);
102 $user{CROSS_COMPILE} = undef;
103 if (which("llvm-ar") =~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
104 $user{AR} = "llvm-ar";
105 $user{ARFLAGS} = [ "rs" ];
108 } elsif (-f "$ndk/AndroidVersion.txt") { #"standalone toolchain"
109 my $cc = $user{CC} // "clang";
110 # One can probably argue that both clang and gcc should be
111 # probed, but support for "standalone toolchain" was added
112 # *after* announcement that gcc is being phased out, so
113 # favouring clang is considered adequate. Those who insist
114 # have option to enforce test for gcc with CC=gcc.
115 if (which("$triarch-$cc") !~ m|^$ndk|) {
116 die "no NDK $triarch-$cc on \$PATH";
119 $user{CROSS_COMPILE} = "$triarch-";
120 } elsif ($user{CC} eq "clang") {
121 die "no NDK clang on \$PATH";
123 if (which("$triarch-gcc") !~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
124 die "no NDK $triarch-gcc on \$PATH";
126 $cflags .= " -mandroid";
127 $user{CROSS_COMPILE} = "$triarch-";
130 if (!-d "$sysroot/usr/include") {
131 my $incroot = "$ndk/sysroot/usr/include";
132 die "no $incroot" if (!-d $incroot);
133 die "no $incroot/$triarch" if (!-d "$incroot/$triarch");
134 $incroot =~ s|^$ndk/||;
135 $cppflags = "-D__ANDROID_API__=$api";
136 $cppflags .= " -isystem \$($ndk_var)/$incroot/$triarch";
137 $cppflags .= " -isystem \$($ndk_var)/$incroot";
140 $sysroot =~ s|^$ndk/||;
142 cflags => "$cflags --sysroot=\$($ndk_var)/$sysroot",
143 cppflags => $cppflags,
144 bn_ops => $arch =~ m/64$/ ? "SIXTY_FOUR_BIT_LONG"
155 inherit_from => [ "linux-generic32" ],
157 ################################################################
158 # Special note about -pie. The underlying reason is that
159 # Lollipop refuses to run non-PIE. But what about older systems
160 # and NDKs? -fPIC was never problem, so the only concern is -pie.
161 # Older toolchains, e.g. r4, appear to handle it and binaries
162 # turn out mostly functional. "Mostly" means that oldest
163 # Androids, such as Froyo, fail to handle executable, but newer
164 # systems are perfectly capable of executing binaries targeting
165 # Froyo. Keep in mind that in the nutshell Android builds are
166 # about JNI, i.e. shared libraries, not applications.
167 cflags => add(sub { android_ndk()->{cflags} }),
168 cppflags => add(sub { android_ndk()->{cppflags} }),
169 cxxflags => add(sub { android_ndk()->{cflags} }),
170 bn_ops => sub { android_ndk()->{bn_ops} },
171 bin_cflags => "-pie",
175 ################################################################
176 # Contemporary Android applications can provide multiple JNI
177 # providers in .apk, targeting multiple architectures. Among
178 # them there is "place" for two ARM flavours: generic eabi and
179 # armv7-a/hard-float. However, it should be noted that OpenSSL's
180 # ability to engage NEON is not constrained by ABI choice, nor
181 # is your ability to call OpenSSL from your application code
182 # compiled with floating-point ABI other than default 'soft'.
183 # (Latter thanks to __attribute__((pcs("aapcs"))) declaration.)
184 # This means that choice of ARM libraries you provide in .apk
185 # is driven by application needs. For example if application
186 # itself benefits from NEON or is floating-point intensive, then
187 # it might be appropriate to provide both libraries. Otherwise
188 # just generic eabi would do. But in latter case it would be
191 # ./Configure android-arm -D__ARM_MAX_ARCH__=8
193 # in order to build "universal" binary and allow OpenSSL take
194 # advantage of NEON when it's available.
196 # Keep in mind that (just like with linux-armv4) we rely on
197 # compiler defaults, which is not necessarily what you had
198 # in mind, in which case you would have to pass additional
199 # -march and/or -mfloat-abi flags. NDK defaults to armv5te.
200 # Newer NDK versions reportedly require additional -latomic.
202 inherit_from => [ "android" ],
203 bn_ops => add("RC4_CHAR"),
205 perlasm_scheme => "void",
208 inherit_from => [ "android" ],
209 bn_ops => add("RC4_CHAR"),
210 asm_arch => 'aarch64',
211 perlasm_scheme => "linux64",
215 inherit_from => [ "android" ],
216 bn_ops => add("RC4_CHAR"),
217 asm_arch => 'mips32',
218 perlasm_scheme => "o32",
220 "android-mips64" => {
221 ################################################################
222 # You are more than likely have to specify target processor
223 # on ./Configure command line. Trouble is that toolchain's
224 # default is MIPS64r6 (at least in r10d), but there are no
225 # such processors around (or they are too rare to spot one).
226 # Actual problem is that MIPS64r6 is binary incompatible
227 # with previous MIPS ISA versions, in sense that unlike
228 # prior versions original MIPS binary code will fail.
230 inherit_from => [ "android" ],
231 bn_ops => add("RC4_CHAR"),
232 asm_arch => 'mips64',
233 perlasm_scheme => "64",
237 inherit_from => [ "android" ],
238 CFLAGS => add(picker(release => "-fomit-frame-pointer")),
239 bn_ops => add("RC4_INT"),
241 perlasm_scheme => "android",
243 "android-x86_64" => {
244 inherit_from => [ "android" ],
245 bn_ops => add("RC4_INT"),
246 asm_arch => 'x86_64',
247 perlasm_scheme => "elf",
250 ####################################################################
251 # Backward compatible targets, (might) requre $CROSS_SYSROOT
253 "android-armeabi" => {
254 inherit_from => [ "android-arm" ],
257 inherit_from => [ "android" ],
259 "android64-aarch64" => {
260 inherit_from => [ "android-arm64" ],
262 "android64-x86_64" => {
263 inherit_from => [ "android-x86_64" ],
265 "android64-mips64" => {
266 inherit_from => [ "android-mips64" ],