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 die "\$ANDROID_NDK=$ndk is invalid" if (!-d "$ndk/platforms");
28 $ndk = canonpath($ndk);
32 if (open my $fh, "<$ndk/source.properties") {
35 if (m|Pkg\.Revision\s*=\s*([0-9]+)|) {
45 if (!($sysroot = $ENV{CROSS_SYSROOT})) {
48 # see if user passed -D__ANDROID_API__=N
49 foreach (@{$useradd{CPPDEFINES}}, @{$user{CPPFLAGS}}) {
50 if (m|__ANDROID_API__=([0-9]+)|) {
56 # list available platforms (numerically)
57 my @platforms = sort { $a =~ m/-([0-9]+)$/; my $aa = $1;
58 $b =~ m/-([0-9]+)$/; $aa <=> $1;
59 } glob("$ndk/platforms/android-$api");
60 die "no $ndk/platforms/android-$api" if ($#platforms < 0);
62 $config{target} =~ m|[^-]+-([^-]+)$|; # split on dash
63 $sysroot = "@platforms[$#platforms]/arch-$1";
65 die "no sysroot=$sysroot" if (!-d $sysroot);
67 $sysroot =~ m|/android-([0-9]+)/arch-(\w+)/?$|;
68 my ($api, $arch) = ($1, $2);
70 my $triarch = $triplet{$arch};
74 # see if there is NDK clang on $PATH
75 if (which("clang") =~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
77 # harmonize with gcc default
78 my $arm = $ndkver > 16 ? "armv7a" : "armv5te";
79 (my $tridefault = $triarch) =~ s/^arm-/$arm-/;
80 (my $tritools = $triarch) =~ s/(?:x|i6)86(_64)?-.*/x86$1/;
81 $cflags .= " -target $tridefault "
82 . "-gcc-toolchain \$(ANDROID_NDK)/toolchains"
83 . "/$tritools-4.9/prebuilt/$host";
84 $user{CC} = "clang" if ($user{CC} !~ m|clang|);
85 $user{CROSS_COMPILE} = undef;
86 } elsif ($user{CC} eq "clang") {
87 die "no NDK clang on \$PATH";
89 if (which("$triarch-gcc") !~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
90 die "no NDK $triarch-gcc on \$PATH";
92 $cflags .= " -mandroid";
93 $user{CROSS_COMPILE} = "$triarch-";
96 if (!-d "$sysroot/usr/include") {
97 my $incroot = "$ndk/sysroot/usr/include";
98 die "no $incroot" if (!-d $incroot);
99 die "no $incroot/$triarch" if (!-d "$incroot/$triarch");
100 $incroot =~ s|^$ndk/||;
101 $cppflags = "-D__ANDROID_API__=$api";
102 $cppflags .= " -isystem \$(ANDROID_NDK)/$incroot/$triarch";
103 $cppflags .= " -isystem \$(ANDROID_NDK)/$incroot";
106 $sysroot =~ s|^$ndk/||;
108 cflags => "$cflags --sysroot=\$(ANDROID_NDK)/$sysroot",
109 cppflags => $cppflags,
110 bn_ops => $arch =~ m/64$/ ? "SIXTY_FOUR_BIT_LONG"
121 inherit_from => [ "linux-generic32" ],
123 ################################################################
124 # Special note about -pie. The underlying reason is that
125 # Lollipop refuses to run non-PIE. But what about older systems
126 # and NDKs? -fPIC was never problem, so the only concern is -pie.
127 # Older toolchains, e.g. r4, appear to handle it and binaries
128 # turn out mostly functional. "Mostly" means that oldest
129 # Androids, such as Froyo, fail to handle executable, but newer
130 # systems are perfectly capable of executing binaries targeting
131 # Froyo. Keep in mind that in the nutshell Android builds are
132 # about JNI, i.e. shared libraries, not applications.
133 cflags => add(sub { android_ndk()->{cflags} }),
134 cppflags => add(sub { android_ndk()->{cppflags} }),
135 cxxflags => add(sub { android_ndk()->{cflags} }),
136 bn_ops => sub { android_ndk()->{bn_ops} },
137 bin_cflags => "-pie",
141 ################################################################
142 # Contemporary Android applications can provide multiple JNI
143 # providers in .apk, targeting multiple architectures. Among
144 # them there is "place" for two ARM flavours: generic eabi and
145 # armv7-a/hard-float. However, it should be noted that OpenSSL's
146 # ability to engage NEON is not constrained by ABI choice, nor
147 # is your ability to call OpenSSL from your application code
148 # compiled with floating-point ABI other than default 'soft'.
149 # (Latter thanks to __attribute__((pcs("aapcs"))) declaration.)
150 # This means that choice of ARM libraries you provide in .apk
151 # is driven by application needs. For example if application
152 # itself benefits from NEON or is floating-point intensive, then
153 # it might be appropriate to provide both libraries. Otherwise
154 # just generic eabi would do. But in latter case it would be
157 # ./Configure android-arm -D__ARM_MAX_ARCH__=8
159 # in order to build "universal" binary and allow OpenSSL take
160 # advantage of NEON when it's available.
162 # Keep in mind that (just like with linux-armv4) we rely on
163 # compiler defaults, which is not necessarily what you had
164 # in mind, in which case you would have to pass additional
165 # -march and/or -mfloat-abi flags. NDK defaults to armv5te.
166 # Newer NDK versions reportedly require additional -latomic.
168 inherit_from => [ "android", asm("armv4_asm") ],
169 bn_ops => add("RC4_CHAR"),
172 inherit_from => [ "android", asm("aarch64_asm") ],
173 bn_ops => add("RC4_CHAR"),
174 perlasm_scheme => "linux64",
178 inherit_from => [ "android", asm("mips32_asm") ],
179 bn_ops => add("RC4_CHAR"),
180 perlasm_scheme => "o32",
182 "android-mips64" => {
183 ################################################################
184 # You are more than likely have to specify target processor
185 # on ./Configure command line. Trouble is that toolchain's
186 # default is MIPS64r6 (at least in r10d), but there are no
187 # such processors around (or they are too rare to spot one).
188 # Actual problem is that MIPS64r6 is binary incompatible
189 # with previous MIPS ISA versions, in sense that unlike
190 # prior versions original MIPS binary code will fail.
192 inherit_from => [ "android", asm("mips64_asm") ],
193 bn_ops => add("RC4_CHAR"),
194 perlasm_scheme => "64",
198 inherit_from => [ "android", asm("x86_asm") ],
199 CFLAGS => add(picker(release => "-fomit-frame-pointer")),
200 bn_ops => add("RC4_INT"),
201 perlasm_scheme => "android",
203 "android-x86_64" => {
204 inherit_from => [ "android", asm("x86_64_asm") ],
205 bn_ops => add("RC4_INT"),
206 perlasm_scheme => "elf",
209 ####################################################################
210 # Backward compatible targets, (might) requre $CROSS_SYSROOT
212 "android-armeabi" => {
213 inherit_from => [ "android-arm" ],
216 inherit_from => [ "android" ],
218 "android64-aarch64" => {
219 inherit_from => [ "android-arm64" ],
221 "android64-x86_64" => {
222 inherit_from => [ "android-x86_64" ],
224 "android64-mips64" => {
225 inherit_from => [ "android-mips64" ],