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";
76 $sysroot =~ m|/android-([0-9]+)/arch-$arch|;
79 die "no sysroot=$sysroot" if (!-d $sysroot);
81 my $triarch = $triplet{$arch};
85 # see if there is NDK clang on $PATH, "universal" or "standalone"
86 if (which("clang") =~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
88 # harmonize with gcc default
89 my $arm = $ndkver > 16 ? "armv7a" : "armv5te";
90 (my $tridefault = $triarch) =~ s/^arm-/$arm-/;
91 (my $tritools = $triarch) =~ s/(?:x|i6)86(_64)?-.*/x86$1/;
92 $cflags .= " -target $tridefault "
93 . "-gcc-toolchain \$(ANDROID_NDK)/toolchains"
94 . "/$tritools-4.9/prebuilt/$host";
95 $user{CC} = "clang" if ($user{CC} !~ m|clang|);
96 $user{CROSS_COMPILE} = undef;
97 } elsif (-f "$ndk/AndroidVersion.txt") { #"standalone toolchain"
98 my $cc = $user{CC} // "clang";
99 # One can probably argue that both clang and gcc should be
100 # probed, but support for "standalone toolchain" was added
101 # *after* announcement that gcc is being phased out, so
102 # favouring clang is considered adequate. Those who insist
103 # have option to enforce test for gcc with CC=gcc.
104 if (which("$triarch-$cc") !~ m|^$ndk|) {
105 die "no NDK $triarch-$cc on \$PATH";
108 $user{CROSS_COMPILE} = "$triarch-";
109 } elsif ($user{CC} eq "clang") {
110 die "no NDK clang on \$PATH";
112 if (which("$triarch-gcc") !~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
113 die "no NDK $triarch-gcc on \$PATH";
115 $cflags .= " -mandroid";
116 $user{CROSS_COMPILE} = "$triarch-";
119 if (!-d "$sysroot/usr/include") {
120 my $incroot = "$ndk/sysroot/usr/include";
121 die "no $incroot" if (!-d $incroot);
122 die "no $incroot/$triarch" if (!-d "$incroot/$triarch");
123 $incroot =~ s|^$ndk/||;
124 $cppflags = "-D__ANDROID_API__=$api";
125 $cppflags .= " -isystem \$(ANDROID_NDK)/$incroot/$triarch";
126 $cppflags .= " -isystem \$(ANDROID_NDK)/$incroot";
129 $sysroot =~ s|^$ndk/||;
131 cflags => "$cflags --sysroot=\$(ANDROID_NDK)/$sysroot",
132 cppflags => $cppflags,
133 bn_ops => $arch =~ m/64$/ ? "SIXTY_FOUR_BIT_LONG"
144 inherit_from => [ "linux-generic32" ],
146 ################################################################
147 # Special note about -pie. The underlying reason is that
148 # Lollipop refuses to run non-PIE. But what about older systems
149 # and NDKs? -fPIC was never problem, so the only concern is -pie.
150 # Older toolchains, e.g. r4, appear to handle it and binaries
151 # turn out mostly functional. "Mostly" means that oldest
152 # Androids, such as Froyo, fail to handle executable, but newer
153 # systems are perfectly capable of executing binaries targeting
154 # Froyo. Keep in mind that in the nutshell Android builds are
155 # about JNI, i.e. shared libraries, not applications.
156 cflags => add(sub { android_ndk()->{cflags} }),
157 cppflags => add(sub { android_ndk()->{cppflags} }),
158 cxxflags => add(sub { android_ndk()->{cflags} }),
159 bn_ops => sub { android_ndk()->{bn_ops} },
160 bin_cflags => "-pie",
164 ################################################################
165 # Contemporary Android applications can provide multiple JNI
166 # providers in .apk, targeting multiple architectures. Among
167 # them there is "place" for two ARM flavours: generic eabi and
168 # armv7-a/hard-float. However, it should be noted that OpenSSL's
169 # ability to engage NEON is not constrained by ABI choice, nor
170 # is your ability to call OpenSSL from your application code
171 # compiled with floating-point ABI other than default 'soft'.
172 # (Latter thanks to __attribute__((pcs("aapcs"))) declaration.)
173 # This means that choice of ARM libraries you provide in .apk
174 # is driven by application needs. For example if application
175 # itself benefits from NEON or is floating-point intensive, then
176 # it might be appropriate to provide both libraries. Otherwise
177 # just generic eabi would do. But in latter case it would be
180 # ./Configure android-arm -D__ARM_MAX_ARCH__=8
182 # in order to build "universal" binary and allow OpenSSL take
183 # advantage of NEON when it's available.
185 # Keep in mind that (just like with linux-armv4) we rely on
186 # compiler defaults, which is not necessarily what you had
187 # in mind, in which case you would have to pass additional
188 # -march and/or -mfloat-abi flags. NDK defaults to armv5te.
189 # Newer NDK versions reportedly require additional -latomic.
191 inherit_from => [ "android", asm("armv4_asm") ],
192 bn_ops => add("RC4_CHAR"),
195 inherit_from => [ "android", asm("aarch64_asm") ],
196 bn_ops => add("RC4_CHAR"),
197 perlasm_scheme => "linux64",
201 inherit_from => [ "android", asm("mips32_asm") ],
202 bn_ops => add("RC4_CHAR"),
203 perlasm_scheme => "o32",
205 "android-mips64" => {
206 ################################################################
207 # You are more than likely have to specify target processor
208 # on ./Configure command line. Trouble is that toolchain's
209 # default is MIPS64r6 (at least in r10d), but there are no
210 # such processors around (or they are too rare to spot one).
211 # Actual problem is that MIPS64r6 is binary incompatible
212 # with previous MIPS ISA versions, in sense that unlike
213 # prior versions original MIPS binary code will fail.
215 inherit_from => [ "android", asm("mips64_asm") ],
216 bn_ops => add("RC4_CHAR"),
217 perlasm_scheme => "64",
221 inherit_from => [ "android", asm("x86_asm") ],
222 CFLAGS => add(picker(release => "-fomit-frame-pointer")),
223 bn_ops => add("RC4_INT"),
224 perlasm_scheme => "android",
226 "android-x86_64" => {
227 inherit_from => [ "android", asm("x86_64_asm") ],
228 bn_ops => add("RC4_INT"),
229 perlasm_scheme => "elf",
232 ####################################################################
233 # Backward compatible targets, (might) requre $CROSS_SYSROOT
235 "android-armeabi" => {
236 inherit_from => [ "android-arm" ],
239 inherit_from => [ "android" ],
241 "android64-aarch64" => {
242 inherit_from => [ "android-arm64" ],
244 "android64-x86_64" => {
245 inherit_from => [ "android-x86_64" ],
247 "android64-mips64" => {
248 inherit_from => [ "android-mips64" ],