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";
79 # harmonize with gcc default
80 (my $tridefault = $triarch) =~ s|^arm-|armv5te-|;
81 $cflags .= " -target $tridefault -gcc-toolchain "
82 . "\$(ANDROID_NDK)/toolchains/$triarch-4.9/prebuilt/$1";
83 $user{CROSS_COMPILE} = undef;
85 $cflags .= " -mandroid";
86 $user{CROSS_COMPILE} = "$triarch-";
89 if (!-d "$sysroot/usr/include") {
90 my $incroot = "$ndk/sysroot/usr/include";
91 die "no $incroot" if (!-d $incroot);
92 die "no $incroot/$triarch" if (!-d "$incroot/$triarch");
93 $incroot =~ s|^$ndk/||;
94 $cppflags = "-D__ANDROID_API__=$api";
95 $cppflags .= " -isystem \$(ANDROID_NDK)/$incroot/$triarch";
96 $cppflags .= " -isystem \$(ANDROID_NDK)/$incroot";
99 $sysroot =~ s|^$ndk/||;
101 cflags => "$cflags --sysroot=\$(ANDROID_NDK)/$sysroot",
102 cppflags => $cppflags,
103 bn_ops => $arch =~ m/64$/ ? "SIXTY_FOUR_BIT_LONG"
114 inherit_from => [ "linux-generic32" ],
116 ################################################################
117 # Special note about -pie. The underlying reason is that
118 # Lollipop refuses to run non-PIE. But what about older systems
119 # and NDKs? -fPIC was never problem, so the only concern is -pie.
120 # Older toolchains, e.g. r4, appear to handle it and binaries
121 # turn out mostly functional. "Mostly" means that oldest
122 # Androids, such as Froyo, fail to handle executable, but newer
123 # systems are perfectly capable of executing binaries targeting
124 # Froyo. Keep in mind that in the nutshell Android builds are
125 # about JNI, i.e. shared libraries, not applications.
126 cflags => add(sub { android_ndk()->{cflags} }),
127 cppflags => add(sub { android_ndk()->{cppflags} }),
128 cxxflags => add(sub { android_ndk()->{cflags} }),
129 bn_ops => sub { android_ndk()->{bn_ops} },
130 bin_cflags => "-pie",
133 ################################################################
134 # Contemporary Android applications can provide multiple JNI
135 # providers in .apk, targeting multiple architectures. Among
136 # them there is "place" for two ARM flavours: generic eabi and
137 # armv7-a/hard-float. However, it should be noted that OpenSSL's
138 # ability to engage NEON is not constrained by ABI choice, nor
139 # is your ability to call OpenSSL from your application code
140 # compiled with floating-point ABI other than default 'soft'.
141 # [Latter thanks to __attribute__((pcs("aapcs"))) declaration.]
142 # This means that choice of ARM libraries you provide in .apk
143 # is driven by application needs. For example if application
144 # itself benefits from NEON or is floating-point intensive, then
145 # it might be appropriate to provide both libraries. Otherwise
146 # just generic eabi would do. But in latter case it would be
149 # ./Configure android-arm -D__ARM_MAX_ARCH__=8
151 # in order to build "universal" binary and allow OpenSSL take
152 # advantage of NEON when it's available.
154 # Keep in mind that [just like with linux-armv4] we rely on
155 # compiler defaults, which is not necessarily what you had
156 # in mind, in which case you would have to pass additional
157 # -march and/or -mfloat-abi flags. NDK defaults to armv5te.
159 inherit_from => [ "android", asm("armv4_asm") ],
162 inherit_from => [ "android", asm("aarch64_asm") ],
163 perlasm_scheme => "linux64",
167 inherit_from => [ "android", asm("mips32_asm") ],
168 perlasm_scheme => "o32",
170 "android-mips64" => {
171 ################################################################
172 # You are more than likely have to specify target processor
173 # on ./Configure command line. Trouble is that toolchain's
174 # default is MIPS64r6 (at least in r10d), but there are no
175 # such processors around (or they are too rare to spot one).
176 # Actual problem is that MIPS64r6 is binary incompatible
177 # with previous MIPS ISA versions, in sense that unlike
178 # prior versions original MIPS binary code will fail.
180 inherit_from => [ "android", asm("mips64_asm") ],
181 perlasm_scheme => "64",
185 inherit_from => [ "android", asm("x86_asm") ],
186 CFLAGS => add(picker(release => "-fomit-frame-pointer")),
187 bn_ops => add("RC4_INT"),
188 perlasm_scheme => "android",
190 "android-x86_64" => {
191 inherit_from => [ "android", asm("x86_64_asm") ],
192 bn_ops => add("RC4_INT"),
193 perlasm_scheme => "elf",
196 ####################################################################
197 # Backward compatible targets, [might] requre $CROSS_SYSROOT
199 "android-armeabi" => {
200 inherit_from => [ "android-arm" ],
203 inherit_from => [ "android" ],
205 "android64-aarch64" => {
206 inherit_from => [ "android-arm64" ],
208 "android64-x86_64" => {
209 inherit_from => [ "android-x86_64" ],
211 "android64-mips64" => {
212 inherit_from => [ "android-mips64" ],