Path: blob/main/crypto/openssl/Configurations/15-android.conf
34860 views
#### Android...1#2# See NOTES-Android.md for details, and don't miss platform-specific3# comments below...45{6use File::Spec::Functions;78my $android_ndk = {};9my %triplet = (10arm => "arm-linux-androideabi",11arm64 => "aarch64-linux-android",12mips => "mipsel-linux-android",13mips64 => "mips64el-linux-android",14riscv64 => "riscv64-linux-android",15x86 => "i686-linux-android",16x86_64 => "x86_64-linux-android",17);1819sub android_ndk {20unless (%$android_ndk) {21if ($now_printing =~ m|^android|) {22return $android_ndk = { bn_ops => "BN_AUTO" };23}2425my $ndk_var;26my $ndk;27foreach (qw(ANDROID_NDK_ROOT ANDROID_NDK)) {28$ndk_var = $_;29$ndk = $ENV{$ndk_var};30last if defined $ndk;31}32die "\$ANDROID_NDK_ROOT is not defined" if (!$ndk);33my $is_standalone_toolchain = -f "$ndk/AndroidVersion.txt";34my $ndk_src_props = "$ndk/source.properties";35my $is_ndk = -f $ndk_src_props;36if ($is_ndk == $is_standalone_toolchain) {37die "\$ANDROID_NDK_ROOT=$ndk is invalid";38}39$ndk = canonpath($ndk);4041my $ndkver = undef;4243if (open my $fh, "<$ndk_src_props") {44local $_;45while(<$fh>) {46if (m|Pkg\.Revision\s*=\s*([0-9]+)|) {47$ndkver = $1;48last;49}50}51close $fh;52}5354my ($sysroot, $api, $arch);5556$config{target} =~ m|[^-]+-([^-]+)$|; # split on dash57$arch = $1;5859if ($sysroot = $ENV{CROSS_SYSROOT}) {60$sysroot =~ m|/android-([0-9]+)/arch-(\w+)/?$|;61($api, $arch) = ($1, $2);62} elsif ($is_standalone_toolchain) {63$sysroot = "$ndk/sysroot";64} else {65$api = "*";6667# see if user passed -D__ANDROID_API__=N68foreach (@{$useradd{CPPDEFINES}}, @{$user{CPPFLAGS}}) {69if (m|__ANDROID_API__=([0-9]+)|) {70$api = $1;71last;72}73}7475if (-d "$ndk/platforms") {76# list available platforms (numerically)77my @platforms = sort { $a =~ m/-([0-9]+)$/; my $aa = $1;78$b =~ m/-([0-9]+)$/; $aa <=> $1;79} glob("$ndk/platforms/android-$api");80die "no $ndk/platforms/android-$api" if ($#platforms < 0);8182$sysroot = "@platforms[$#platforms]/arch-$arch";83$sysroot =~ m|/android-([0-9]+)/arch-$arch|;84$api = $1;85} elsif ($api eq "*") {86# r22 Removed platforms dir, use this JSON file87my $path = "$ndk/meta/platforms.json";88open my $fh, $path or die "Could not open '$path' $!";89while (<$fh>) {90if (/"max": (\d+),/) {91$api = $1;92last;93}94}95close $fh;96}97die "Could not get default API Level" if ($api eq "*");98}99die "no sysroot=$sysroot" if (length $sysroot && !-d $sysroot);100101my $triarch = $triplet{$arch};102my $cflags;103my $cppflags;104105# see if there is NDK clang on $PATH, "universal" or "standalone"106if (which("clang") =~ m|^$ndk/.*/prebuilt/([^/]+)/|) {107my $host=$1;108# harmonize with gcc default109my $arm = $ndkver > 16 ? "armv7a" : "armv5te";110(my $tridefault = $triarch) =~ s/^arm-/$arm-/;111(my $tritools = $triarch) =~ s/(?:x|i6)86(_64)?-.*/x86$1/;112if (length $sysroot) {113$cflags .= " -target $tridefault "114. "-gcc-toolchain \$($ndk_var)/toolchains"115. "/$tritools-4.9/prebuilt/$host";116$user{CC} = "clang" if ($user{CC} !~ m|clang|);117} else {118$user{CC} = "$tridefault$api-clang";119}120$user{CROSS_COMPILE} = undef;121if (which("llvm-ar") =~ m|^$ndk/.*/prebuilt/([^/]+)/|) {122$user{AR} = "llvm-ar";123$user{ARFLAGS} = [ "rs" ];124$user{RANLIB} = ":";125}126} elsif ($is_standalone_toolchain) {127my $cc = $user{CC} // "clang";128# One can probably argue that both clang and gcc should be129# probed, but support for "standalone toolchain" was added130# *after* announcement that gcc is being phased out, so131# favouring clang is considered adequate. Those who insist132# have option to enforce test for gcc with CC=gcc.133if (which("$triarch-$cc") !~ m|^$ndk|) {134die "no NDK $triarch-$cc on \$PATH";135}136$user{CC} = $cc;137$user{CROSS_COMPILE} = "$triarch-";138} elsif ($user{CC} eq "clang") {139die "no NDK clang on \$PATH";140} else {141if (which("$triarch-gcc") !~ m|^$ndk/.*/prebuilt/([^/]+)/|) {142die "no NDK $triarch-gcc on \$PATH";143}144$cflags .= " -mandroid";145$user{CROSS_COMPILE} = "$triarch-";146}147148if (length $sysroot) {149if (!-d "$sysroot/usr/include") {150my $incroot = "$ndk/sysroot/usr/include";151die "no $incroot" if (!-d $incroot);152die "no $incroot/$triarch" if (!-d "$incroot/$triarch");153$incroot =~ s|^$ndk/||;154$cppflags = "-D__ANDROID_API__=$api";155$cppflags .= " -isystem \$($ndk_var)/$incroot/$triarch";156$cppflags .= " -isystem \$($ndk_var)/$incroot";157}158$sysroot =~ s|^$ndk/||;159$sysroot = " --sysroot=\$($ndk_var)/$sysroot";160}161$android_ndk = {162cflags => $cflags . $sysroot,163cppflags => $cppflags,164bn_ops => $arch =~ m/64$/ ? "SIXTY_FOUR_BIT_LONG"165: "BN_LLONG",166};167}168169return $android_ndk;170}171}172173my %targets = (174"android" => {175inherit_from => [ "linux-generic32" ],176template => 1,177################################################################178# Special note about -pie. The underlying reason is that179# Lollipop refuses to run non-PIE. But what about older systems180# and NDKs? -fPIC was never problem, so the only concern is -pie.181# Older toolchains, e.g. r4, appear to handle it and binaries182# turn out mostly functional. "Mostly" means that oldest183# Androids, such as Froyo, fail to handle executable, but newer184# systems are perfectly capable of executing binaries targeting185# Froyo. Keep in mind that in the nutshell Android builds are186# about JNI, i.e. shared libraries, not applications.187cflags => add(sub { android_ndk()->{cflags} }),188cppflags => add(sub { android_ndk()->{cppflags} }),189cxxflags => add(sub { android_ndk()->{cflags} }),190bn_ops => sub { android_ndk()->{bn_ops} },191bin_cflags => "-fPIE",192bin_lflags => "-pie",193enable => [ ],194shared_extension => ".so",195},196"android-arm" => {197################################################################198# Contemporary Android applications can provide multiple JNI199# providers in .apk, targeting multiple architectures. Among200# them there is "place" for two ARM flavours: generic eabi and201# armv7-a/hard-float. However, it should be noted that OpenSSL's202# ability to engage NEON is not constrained by ABI choice, nor203# is your ability to call OpenSSL from your application code204# compiled with floating-point ABI other than default 'soft'.205# (Latter thanks to __attribute__((pcs("aapcs"))) declaration.)206# This means that choice of ARM libraries you provide in .apk207# is driven by application needs. For example if application208# itself benefits from NEON or is floating-point intensive, then209# it might be appropriate to provide both libraries. Otherwise210# just generic eabi would do. But in latter case it would be211# appropriate to212#213# ./Configure android-arm -D__ARM_MAX_ARCH__=8214#215# in order to build "universal" binary and allow OpenSSL take216# advantage of NEON when it's available.217#218# Keep in mind that (just like with linux-armv4) we rely on219# compiler defaults, which is not necessarily what you had220# in mind, in which case you would have to pass additional221# -march and/or -mfloat-abi flags. NDK defaults to armv5te.222# Newer NDK versions reportedly require additional -latomic.223#224inherit_from => [ "android" ],225bn_ops => add("RC4_CHAR"),226asm_arch => 'armv4',227perlasm_scheme => "void",228},229"android-arm64" => {230inherit_from => [ "android" ],231bn_ops => add("RC4_CHAR"),232asm_arch => 'aarch64',233perlasm_scheme => "linux64",234},235236"android-mips" => {237inherit_from => [ "android" ],238bn_ops => add("RC4_CHAR"),239asm_arch => 'mips32',240perlasm_scheme => "o32",241},242"android-mips64" => {243################################################################244# You are more than likely have to specify target processor245# on ./Configure command line. Trouble is that toolchain's246# default is MIPS64r6 (at least in r10d), but there are no247# such processors around (or they are too rare to spot one).248# Actual problem is that MIPS64r6 is binary incompatible249# with previous MIPS ISA versions, in sense that unlike250# prior versions original MIPS binary code will fail.251#252inherit_from => [ "android" ],253bn_ops => add("RC4_CHAR"),254asm_arch => 'mips64',255perlasm_scheme => "64",256},257258"android-x86" => {259inherit_from => [ "android" ],260CFLAGS => add(picker(release => "-fomit-frame-pointer")),261bn_ops => add("RC4_INT"),262asm_arch => 'x86',263perlasm_scheme => "android",264ex_libs => add(threads("-latomic")),265},266"android-x86_64" => {267inherit_from => [ "android" ],268bn_ops => add("RC4_INT"),269asm_arch => 'x86_64',270perlasm_scheme => "elf",271},272273"android-riscv64" => {274inherit_from => [ "android" ],275asm_arch => 'riscv64',276perlasm_scheme => "linux64",277},278279####################################################################280# Backward compatible targets, (might) require $CROSS_SYSROOT281#282"android-armeabi" => {283inherit_from => [ "android-arm" ],284},285"android64" => {286inherit_from => [ "android" ],287},288"android64-aarch64" => {289inherit_from => [ "android-arm64" ],290},291"android64-x86_64" => {292inherit_from => [ "android-x86_64" ],293},294"android64-mips64" => {295inherit_from => [ "android-mips64" ],296},297);298299300