3 # It takes *one* prior-set environment variable to make it work:
5 # ANDROID_NDK=/some/where/android-ndk-<ver>
7 # As well as PATH *adjusted* to cover ${CROSS_COMPILE}gcc and company.
9 # Note that it's different from original instructions that required to
10 # set CROSS_SYSROOT [to $ANDROID_NDK/platforms/android-<api>/arch-<arch>]
11 # and CROSS_COMPILE. CROSS_SYSROOT is still recognized [and even required
12 # for some legacy targets], but if not set, it's detected and set to the
13 # latest Android platform available with appointed NDK automatically. If
14 # you need to target older platform, pass additional -D__ANDROID_API__=N
15 # to Configure. For example, to compile for ICS on ARM with NDK 10d:
17 # ANDROID_NDK=/some/where/android-ndk-10d
18 # PATH=$ANDROID_NDK/toolchains/arm-linux-androideabi-4.8/prebuild/linux-x86_64/bin:$PATH
19 # [..]./Configure android-arm -D__ANDROID_API__=14
21 # One can engage clang by passing CC=clang to Configure. In such case
22 # PATH needs even more adjustments to cover NDK's clang itself, as well
23 # as unprefixed, yet target-specific ar and ranlib [or not, if you use
24 # binutils-multiarch].
29 arm => "arm-linux-androideabi",
30 arm64 => "aarch64-linux-android",
31 mips => "mipsel-linux-android",
32 mips64 => "mips64el-linux-android",
33 x86 => "i686-linux-android",
34 x86_64 => "x86_64-linux-android",
38 unless (%$android_ndk) {
39 my $ndk = $ENV{ANDROID_NDK};
40 die "\$ANDROID_NDK is not defined" if (!$ndk);
41 die "\$ANDROID_NDK=$ndk is invalid" if (!-d "$ndk/platforms");
45 if (!($sysroot = $ENV{CROSS_SYSROOT})) {
48 # see if user passed -D__ANDROID_API__=N
49 foreach (@{$useradd{CPPDEFINES}}) {
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};
71 my $cflags = "-Wa,--noexecstack";
74 # see if user passed CC=clang
75 if ($user{CC} eq "clang") {
76 if (which("clang") !~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
77 die "no NDK clang on \$PATH";
80 # harmonize with gcc default
81 (my $tridefault = $triarch) =~ s/^arm-/armv5te-/;
82 (my $tritools = $triarch) =~ s/(?:x|i6)86(_64)?-.*/x86$1/;
83 $cflags .= " -target $tridefault "
84 . "-gcc-toolchain \$(ANDROID_NDK)/toolchains"
85 . "/$tritools-4.9/prebuilt/$host";
86 $user{CROSS_COMPILE} = undef;
88 $cflags .= " -mandroid";
89 $user{CROSS_COMPILE} = "$triarch-";
92 if (!-d "$sysroot/usr/include") {
93 my $incroot = "$ndk/sysroot/usr/include";
94 die "no $incroot" if (!-d $incroot);
95 die "no $incroot/$triarch" if (!-d "$incroot/$triarch");
96 $incroot =~ s|^$ndk/||;
97 $cppflags = "-D__ANDROID_API__=$api";
98 $cppflags .= " -isystem \$(ANDROID_NDK)/$incroot/$triarch";
99 $cppflags .= " -isystem \$(ANDROID_NDK)/$incroot";
102 $sysroot =~ s|^$ndk/||;
104 cflags => "$cflags --sysroot=\$(ANDROID_NDK)/$sysroot",
105 cppflags => $cppflags,
106 bn_ops => $arch =~ m/64$/ ? "SIXTY_FOUR_BIT_LONG"
117 inherit_from => [ "linux-generic32" ],
119 ################################################################
120 # Special note about -pie. The underlying reason is that
121 # Lollipop refuses to run non-PIE. But what about older systems
122 # and NDKs? -fPIC was never problem, so the only concern is -pie.
123 # Older toolchains, e.g. r4, appear to handle it and binaries
124 # turn out mostly functional. "Mostly" means that oldest
125 # Androids, such as Froyo, fail to handle executable, but newer
126 # systems are perfectly capable of executing binaries targeting
127 # Froyo. Keep in mind that in the nutshell Android builds are
128 # about JNI, i.e. shared libraries, not applications.
129 cflags => add(sub { android_ndk()->{cflags} }),
130 cppflags => add(sub { android_ndk()->{cppflags} }),
131 cxxflags => add(sub { android_ndk()->{cflags} }),
132 bn_ops => sub { android_ndk()->{bn_ops} },
133 bin_cflags => "-pie",
136 ################################################################
137 # Contemporary Android applications can provide multiple JNI
138 # providers in .apk, targeting multiple architectures. Among
139 # them there is "place" for two ARM flavours: generic eabi and
140 # armv7-a/hard-float. However, it should be noted that OpenSSL's
141 # ability to engage NEON is not constrained by ABI choice, nor
142 # is your ability to call OpenSSL from your application code
143 # compiled with floating-point ABI other than default 'soft'.
144 # [Latter thanks to __attribute__((pcs("aapcs"))) declaration.]
145 # This means that choice of ARM libraries you provide in .apk
146 # is driven by application needs. For example if application
147 # itself benefits from NEON or is floating-point intensive, then
148 # it might be appropriate to provide both libraries. Otherwise
149 # just generic eabi would do. But in latter case it would be
152 # ./Configure android-arm -D__ARM_MAX_ARCH__=8
154 # in order to build "universal" binary and allow OpenSSL take
155 # advantage of NEON when it's available.
157 # Keep in mind that [just like with linux-armv4] we rely on
158 # compiler defaults, which is not necessarily what you had
159 # in mind, in which case you would have to pass additional
160 # -march and/or -mfloat-abi flags. NDK defaults to armv5te.
162 inherit_from => [ "android", asm("armv4_asm") ],
165 inherit_from => [ "android", asm("aarch64_asm") ],
166 perlasm_scheme => "linux64",
170 inherit_from => [ "android", asm("mips32_asm") ],
171 perlasm_scheme => "o32",
173 "android-mips64" => {
174 ################################################################
175 # You are more than likely have to specify target processor
176 # on ./Configure command line. Trouble is that toolchain's
177 # default is MIPS64r6 (at least in r10d), but there are no
178 # such processors around (or they are too rare to spot one).
179 # Actual problem is that MIPS64r6 is binary incompatible
180 # with previous MIPS ISA versions, in sense that unlike
181 # prior versions original MIPS binary code will fail.
183 inherit_from => [ "android", asm("mips64_asm") ],
184 perlasm_scheme => "64",
188 inherit_from => [ "android", asm("x86_asm") ],
189 CFLAGS => add(picker(release => "-fomit-frame-pointer")),
190 bn_ops => add("RC4_INT"),
191 perlasm_scheme => "android",
193 "android-x86_64" => {
194 inherit_from => [ "android", asm("x86_64_asm") ],
195 bn_ops => add("RC4_INT"),
196 perlasm_scheme => "elf",
199 ####################################################################
200 # Backward compatible targets, [might] requre $CROSS_SYSROOT
202 "android-armeabi" => {
203 inherit_from => [ "android-arm" ],
206 inherit_from => [ "android" ],
208 "android64-aarch64" => {
209 inherit_from => [ "android-arm64" ],
211 "android64-x86_64" => {
212 inherit_from => [ "android-x86_64" ],
214 "android64-mips64" => {
215 inherit_from => [ "android-mips64" ],