Configurations/15-android.conf: add support for "standalone toolchain".
[openssl.git] / Configurations / 15-android.conf
1 #### Android...
2 #
3 # See NOTES.ANDROID for details, and don't miss platform-specific
4 # comments below...
5
6 {
7     use File::Spec::Functions;
8
9     my $android_ndk = {};
10     my %triplet = (
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",
17     );
18
19     sub android_ndk {
20         unless (%$android_ndk) {
21             if ($now_printing =~ m|^android|) {
22                 return $android_ndk = { bn_ops => "BN_AUTO" };
23             }
24
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";
32             }
33             $ndk = canonpath($ndk);
34
35             my $ndkver = undef;
36
37             if (open my $fh, "<$ndk/source.properties") {
38                 local $_;
39                 while(<$fh>) {
40                     if (m|Pkg\.Revision\s*=\s*([0-9]+)|) {
41                         $ndkver = $1;
42                         last;
43                     }
44                 }
45                 close $fh;
46             }
47
48             my ($sysroot, $api, $arch);
49
50             $config{target} =~ m|[^-]+-([^-]+)$|;       # split on dash
51             $arch = $1;
52
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";
58             } else {
59                 $api = "*";
60
61                 # see if user passed -D__ANDROID_API__=N
62                 foreach (@{$useradd{CPPDEFINES}}, @{$user{CPPFLAGS}}) {
63                     if (m|__ANDROID_API__=([0-9]+)|) {
64                         $api = $1;
65                         last;
66                     }
67                 }
68
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);
74
75                 $sysroot = "@platforms[$#platforms]/arch-$arch";
76             }
77             die "no sysroot=$sysroot"   if (!-d $sysroot);
78
79             my $triarch = $triplet{$arch};
80             my $cflags;
81             my $cppflags;
82
83             # see if there is NDK clang on $PATH, "universal" or "standalone"
84             if (which("clang") =~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
85                 my $host=$1;
86                 # harmonize with gcc default
87                 my $arm = $ndkver > 16 ? "armv7a" : "armv5te";
88                 (my $tridefault = $triarch) =~ s/^arm-/$arm-/;
89                 (my $tritools   = $triarch) =~ s/(?:x|i6)86(_64)?-.*/x86$1/;
90                 $cflags .= " -target $tridefault "
91                         .  "-gcc-toolchain \$(ANDROID_NDK)/toolchains"
92                         .  "/$tritools-4.9/prebuilt/$host";
93                 $user{CC} = "clang" if ($user{CC} !~ m|clang|);
94                 $user{CROSS_COMPILE} = undef;
95             } elsif (-f "$ndk/AndroidVersion.txt") {    #"standalone toolchain"
96                 my $cc = $user{CC} // "clang";
97                 # One can probably argue that both clang and gcc should be
98                 # probed, but support for "standalone toolchain" was added
99                 # *after* announcement that gcc is being phased out, so
100                 # favouring clang is considered adequate. Those who insist
101                 # have option to enforce test for gcc with CC=gcc.
102                 if (which("$triarch-$cc") !~ m|^$ndk|) {
103                     die "no NDK $triarch-$cc on \$PATH";
104                 }
105                 $user{CC} = $cc;
106                 $user{CROSS_COMPILE} = "$triarch-";
107             } elsif ($user{CC} eq "clang") {
108                 die "no NDK clang on \$PATH";
109             } else {
110                 if (which("$triarch-gcc") !~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
111                     die "no NDK $triarch-gcc on \$PATH";
112                 }
113                 $cflags .= " -mandroid";
114                 $user{CROSS_COMPILE} = "$triarch-";
115             }
116
117             if (!-d "$sysroot/usr/include") {
118                 my $incroot = "$ndk/sysroot/usr/include";
119                 die "no $incroot"          if (!-d $incroot);
120                 die "no $incroot/$triarch" if (!-d "$incroot/$triarch");
121                 $incroot =~ s|^$ndk/||;
122                 $cppflags  = "-D__ANDROID_API__=$api";
123                 $cppflags .= " -isystem \$(ANDROID_NDK)/$incroot/$triarch";
124                 $cppflags .= " -isystem \$(ANDROID_NDK)/$incroot";
125             }
126
127             $sysroot =~ s|^$ndk/||;
128             $android_ndk = {
129                 cflags   => "$cflags --sysroot=\$(ANDROID_NDK)/$sysroot",
130                 cppflags => $cppflags,
131                 bn_ops   => $arch =~ m/64$/ ? "SIXTY_FOUR_BIT_LONG"
132                                             : "BN_LLONG",
133             };
134         }
135
136         return $android_ndk;
137     }
138 }
139
140 my %targets = (
141     "android" => {
142         inherit_from     => [ "linux-generic32" ],
143         template         => 1,
144         ################################################################
145         # Special note about -pie. The underlying reason is that
146         # Lollipop refuses to run non-PIE. But what about older systems
147         # and NDKs? -fPIC was never problem, so the only concern is -pie.
148         # Older toolchains, e.g. r4, appear to handle it and binaries
149         # turn out mostly functional. "Mostly" means that oldest
150         # Androids, such as Froyo, fail to handle executable, but newer
151         # systems are perfectly capable of executing binaries targeting
152         # Froyo. Keep in mind that in the nutshell Android builds are
153         # about JNI, i.e. shared libraries, not applications.
154         cflags           => add(sub { android_ndk()->{cflags} }),
155         cppflags         => add(sub { android_ndk()->{cppflags} }),
156         cxxflags         => add(sub { android_ndk()->{cflags} }),
157         bn_ops           => sub { android_ndk()->{bn_ops} },
158         bin_cflags       => "-pie",
159         enable           => [ ],
160     },
161     "android-arm" => {
162         ################################################################
163         # Contemporary Android applications can provide multiple JNI
164         # providers in .apk, targeting multiple architectures. Among
165         # them there is "place" for two ARM flavours: generic eabi and
166         # armv7-a/hard-float. However, it should be noted that OpenSSL's
167         # ability to engage NEON is not constrained by ABI choice, nor
168         # is your ability to call OpenSSL from your application code
169         # compiled with floating-point ABI other than default 'soft'.
170         # (Latter thanks to __attribute__((pcs("aapcs"))) declaration.)
171         # This means that choice of ARM libraries you provide in .apk
172         # is driven by application needs. For example if application
173         # itself benefits from NEON or is floating-point intensive, then
174         # it might be appropriate to provide both libraries. Otherwise
175         # just generic eabi would do. But in latter case it would be
176         # appropriate to
177         #
178         #   ./Configure android-arm -D__ARM_MAX_ARCH__=8
179         #
180         # in order to build "universal" binary and allow OpenSSL take
181         # advantage of NEON when it's available.
182         #
183         # Keep in mind that (just like with linux-armv4) we rely on
184         # compiler defaults, which is not necessarily what you had
185         # in mind, in which case you would have to pass additional
186         # -march and/or -mfloat-abi flags. NDK defaults to armv5te.
187         # Newer NDK versions reportedly require additional -latomic.
188         #
189         inherit_from     => [ "android", asm("armv4_asm") ],
190         bn_ops           => add("RC4_CHAR"),
191     },
192     "android-arm64" => {
193         inherit_from     => [ "android", asm("aarch64_asm") ],
194         bn_ops           => add("RC4_CHAR"),
195         perlasm_scheme   => "linux64",
196     },
197
198     "android-mips" => {
199         inherit_from     => [ "android", asm("mips32_asm") ],
200         bn_ops           => add("RC4_CHAR"),
201         perlasm_scheme   => "o32",
202     },
203     "android-mips64" => {
204         ################################################################
205         # You are more than likely have to specify target processor
206         # on ./Configure command line. Trouble is that toolchain's
207         # default is MIPS64r6 (at least in r10d), but there are no
208         # such processors around (or they are too rare to spot one).
209         # Actual problem is that MIPS64r6 is binary incompatible
210         # with previous MIPS ISA versions, in sense that unlike
211         # prior versions original MIPS binary code will fail.
212         #
213         inherit_from     => [ "android", asm("mips64_asm") ],
214         bn_ops           => add("RC4_CHAR"),
215         perlasm_scheme   => "64",
216     },
217
218     "android-x86" => {
219         inherit_from     => [ "android", asm("x86_asm") ],
220         CFLAGS           => add(picker(release => "-fomit-frame-pointer")),
221         bn_ops           => add("RC4_INT"),
222         perlasm_scheme   => "android",
223     },
224     "android-x86_64" => {
225         inherit_from     => [ "android", asm("x86_64_asm") ],
226         bn_ops           => add("RC4_INT"),
227         perlasm_scheme   => "elf",
228     },
229
230     ####################################################################
231     # Backward compatible targets, (might) requre $CROSS_SYSROOT
232     #
233     "android-armeabi" => {
234         inherit_from     => [ "android-arm" ],
235     },
236     "android64" => {
237         inherit_from     => [ "android" ],
238     },
239     "android64-aarch64" => {
240         inherit_from     => [ "android-arm64" ],
241     },
242     "android64-x86_64" => {
243         inherit_from     => [ "android-x86_64" ],
244     },
245     "android64-mips64" => {
246         inherit_from     => [ "android-mips64" ],
247     },
248 );