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