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