2018-03-12 02:08:56 +08:00
|
|
|
#### Android...
|
|
|
|
#
|
2020-06-10 20:15:28 +08:00
|
|
|
# See NOTES-Android.md for details, and don't miss platform-specific
|
2018-03-16 19:14:28 +08:00
|
|
|
# comments below...
|
2018-03-12 02:08:56 +08:00
|
|
|
|
|
|
|
{
|
2018-08-12 16:14:06 +08:00
|
|
|
use File::Spec::Functions;
|
|
|
|
|
2018-03-12 02:08:56 +08:00
|
|
|
my $android_ndk = {};
|
|
|
|
my %triplet = (
|
|
|
|
arm => "arm-linux-androideabi",
|
|
|
|
arm64 => "aarch64-linux-android",
|
|
|
|
mips => "mipsel-linux-android",
|
|
|
|
mips64 => "mips64el-linux-android",
|
|
|
|
x86 => "i686-linux-android",
|
|
|
|
x86_64 => "x86_64-linux-android",
|
|
|
|
);
|
|
|
|
|
|
|
|
sub android_ndk {
|
|
|
|
unless (%$android_ndk) {
|
2018-03-26 18:35:57 +08:00
|
|
|
if ($now_printing =~ m|^android|) {
|
|
|
|
return $android_ndk = { bn_ops => "BN_AUTO" };
|
|
|
|
}
|
|
|
|
|
2019-01-28 21:53:19 +08:00
|
|
|
my $ndk_var;
|
|
|
|
my $ndk;
|
2020-02-29 13:43:26 +08:00
|
|
|
foreach (qw(ANDROID_NDK_ROOT ANDROID_NDK)) {
|
2019-02-02 18:45:06 +08:00
|
|
|
$ndk_var = $_;
|
2019-01-28 21:53:19 +08:00
|
|
|
$ndk = $ENV{$ndk_var};
|
|
|
|
last if defined $ndk;
|
|
|
|
}
|
2020-02-29 13:43:26 +08:00
|
|
|
die "\$ANDROID_NDK_ROOT is not defined" if (!$ndk);
|
2020-11-18 12:20:43 +08:00
|
|
|
my $is_standalone_toolchain = -f "$ndk/AndroidVersion.txt";
|
|
|
|
my $ndk_src_props = "$ndk/source.properties";
|
|
|
|
my $is_ndk = -f $ndk_src_props;
|
|
|
|
if ($is_ndk == $is_standalone_toolchain) {
|
2020-02-29 13:43:26 +08:00
|
|
|
die "\$ANDROID_NDK_ROOT=$ndk is invalid";
|
2018-10-14 21:19:41 +08:00
|
|
|
}
|
2018-08-12 20:22:16 +08:00
|
|
|
$ndk = canonpath($ndk);
|
2018-03-12 02:08:56 +08:00
|
|
|
|
2018-03-17 17:59:57 +08:00
|
|
|
my $ndkver = undef;
|
|
|
|
|
2020-11-18 12:20:43 +08:00
|
|
|
if (open my $fh, "<$ndk_src_props") {
|
2018-03-17 17:59:57 +08:00
|
|
|
local $_;
|
|
|
|
while(<$fh>) {
|
|
|
|
if (m|Pkg\.Revision\s*=\s*([0-9]+)|) {
|
|
|
|
$ndkver = $1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close $fh;
|
|
|
|
}
|
|
|
|
|
2018-10-14 21:19:41 +08:00
|
|
|
my ($sysroot, $api, $arch);
|
|
|
|
|
|
|
|
$config{target} =~ m|[^-]+-([^-]+)$|; # split on dash
|
|
|
|
$arch = $1;
|
2018-03-12 02:08:56 +08:00
|
|
|
|
2018-10-14 21:19:41 +08:00
|
|
|
if ($sysroot = $ENV{CROSS_SYSROOT}) {
|
|
|
|
$sysroot =~ m|/android-([0-9]+)/arch-(\w+)/?$|;
|
|
|
|
($api, $arch) = ($1, $2);
|
2020-11-18 12:20:43 +08:00
|
|
|
} elsif ($is_standalone_toolchain) {
|
2018-10-14 21:19:41 +08:00
|
|
|
$sysroot = "$ndk/sysroot";
|
|
|
|
} else {
|
|
|
|
$api = "*";
|
2018-03-12 02:08:56 +08:00
|
|
|
|
|
|
|
# see if user passed -D__ANDROID_API__=N
|
2018-03-17 17:59:57 +08:00
|
|
|
foreach (@{$useradd{CPPDEFINES}}, @{$user{CPPFLAGS}}) {
|
2018-03-12 02:08:56 +08:00
|
|
|
if (m|__ANDROID_API__=([0-9]+)|) {
|
|
|
|
$api = $1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 12:20:43 +08:00
|
|
|
if (-d "$ndk/platforms") {
|
|
|
|
# list available platforms (numerically)
|
|
|
|
my @platforms = sort { $a =~ m/-([0-9]+)$/; my $aa = $1;
|
|
|
|
$b =~ m/-([0-9]+)$/; $aa <=> $1;
|
|
|
|
} glob("$ndk/platforms/android-$api");
|
|
|
|
die "no $ndk/platforms/android-$api" if ($#platforms < 0);
|
|
|
|
|
|
|
|
$sysroot = "@platforms[$#platforms]/arch-$arch";
|
|
|
|
$sysroot =~ m|/android-([0-9]+)/arch-$arch|;
|
|
|
|
$api = $1;
|
|
|
|
} elsif ($api eq "*") {
|
|
|
|
# r22 Removed platforms dir, use this JSON file
|
|
|
|
my $path = "$ndk/meta/platforms.json";
|
|
|
|
open my $fh, $path or die "Could not open '$path' $!";
|
|
|
|
while (<$fh>) {
|
|
|
|
if (/"max": (\d+),/) {
|
|
|
|
$api = $1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close $fh;
|
|
|
|
}
|
|
|
|
die "Could not get default API Level" if ($api eq "*");
|
2018-03-12 02:08:56 +08:00
|
|
|
}
|
2020-11-18 12:20:43 +08:00
|
|
|
die "no sysroot=$sysroot" if (length $sysroot && !-d $sysroot);
|
2018-03-12 02:08:56 +08:00
|
|
|
|
|
|
|
my $triarch = $triplet{$arch};
|
2018-05-04 20:06:44 +08:00
|
|
|
my $cflags;
|
2018-03-12 02:08:56 +08:00
|
|
|
my $cppflags;
|
|
|
|
|
2018-10-14 21:19:41 +08:00
|
|
|
# see if there is NDK clang on $PATH, "universal" or "standalone"
|
2018-03-17 17:59:57 +08:00
|
|
|
if (which("clang") =~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
|
2018-03-14 16:45:31 +08:00
|
|
|
my $host=$1;
|
2018-03-12 02:08:56 +08:00
|
|
|
# harmonize with gcc default
|
2018-03-17 17:59:57 +08:00
|
|
|
my $arm = $ndkver > 16 ? "armv7a" : "armv5te";
|
|
|
|
(my $tridefault = $triarch) =~ s/^arm-/$arm-/;
|
2018-03-14 16:45:31 +08:00
|
|
|
(my $tritools = $triarch) =~ s/(?:x|i6)86(_64)?-.*/x86$1/;
|
2020-11-18 12:20:43 +08:00
|
|
|
if (length $sysroot) {
|
|
|
|
$cflags .= " -target $tridefault "
|
|
|
|
. "-gcc-toolchain \$($ndk_var)/toolchains"
|
|
|
|
. "/$tritools-4.9/prebuilt/$host";
|
|
|
|
$user{CC} = "clang" if ($user{CC} !~ m|clang|);
|
|
|
|
} else {
|
|
|
|
$user{CC} = "$tridefault$api-clang";
|
|
|
|
}
|
2018-03-12 02:08:56 +08:00
|
|
|
$user{CROSS_COMPILE} = undef;
|
2018-10-20 04:24:30 +08:00
|
|
|
if (which("llvm-ar") =~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
|
|
|
|
$user{AR} = "llvm-ar";
|
|
|
|
$user{ARFLAGS} = [ "rs" ];
|
|
|
|
$user{RANLIB} = ":";
|
|
|
|
}
|
2020-11-18 12:20:43 +08:00
|
|
|
} elsif ($is_standalone_toolchain) {
|
2018-10-14 21:19:41 +08:00
|
|
|
my $cc = $user{CC} // "clang";
|
|
|
|
# One can probably argue that both clang and gcc should be
|
|
|
|
# probed, but support for "standalone toolchain" was added
|
|
|
|
# *after* announcement that gcc is being phased out, so
|
|
|
|
# favouring clang is considered adequate. Those who insist
|
|
|
|
# have option to enforce test for gcc with CC=gcc.
|
|
|
|
if (which("$triarch-$cc") !~ m|^$ndk|) {
|
|
|
|
die "no NDK $triarch-$cc on \$PATH";
|
|
|
|
}
|
|
|
|
$user{CC} = $cc;
|
|
|
|
$user{CROSS_COMPILE} = "$triarch-";
|
2018-03-17 17:59:57 +08:00
|
|
|
} elsif ($user{CC} eq "clang") {
|
|
|
|
die "no NDK clang on \$PATH";
|
2018-03-12 02:08:56 +08:00
|
|
|
} else {
|
2018-03-17 17:59:57 +08:00
|
|
|
if (which("$triarch-gcc") !~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
|
|
|
|
die "no NDK $triarch-gcc on \$PATH";
|
|
|
|
}
|
2018-03-12 02:08:56 +08:00
|
|
|
$cflags .= " -mandroid";
|
|
|
|
$user{CROSS_COMPILE} = "$triarch-";
|
|
|
|
}
|
|
|
|
|
2020-11-18 12:20:43 +08:00
|
|
|
if (length $sysroot) {
|
|
|
|
if (!-d "$sysroot/usr/include") {
|
|
|
|
my $incroot = "$ndk/sysroot/usr/include";
|
|
|
|
die "no $incroot" if (!-d $incroot);
|
|
|
|
die "no $incroot/$triarch" if (!-d "$incroot/$triarch");
|
|
|
|
$incroot =~ s|^$ndk/||;
|
|
|
|
$cppflags = "-D__ANDROID_API__=$api";
|
|
|
|
$cppflags .= " -isystem \$($ndk_var)/$incroot/$triarch";
|
|
|
|
$cppflags .= " -isystem \$($ndk_var)/$incroot";
|
|
|
|
}
|
|
|
|
$sysroot =~ s|^$ndk/||;
|
|
|
|
$sysroot = " --sysroot=\$($ndk_var)/$sysroot";
|
2018-03-12 02:08:56 +08:00
|
|
|
}
|
|
|
|
$android_ndk = {
|
2020-11-18 12:20:43 +08:00
|
|
|
cflags => $cflags . $sysroot,
|
2018-03-12 02:08:56 +08:00
|
|
|
cppflags => $cppflags,
|
|
|
|
bn_ops => $arch =~ m/64$/ ? "SIXTY_FOUR_BIT_LONG"
|
|
|
|
: "BN_LLONG",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return $android_ndk;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my %targets = (
|
|
|
|
"android" => {
|
|
|
|
inherit_from => [ "linux-generic32" ],
|
|
|
|
template => 1,
|
|
|
|
################################################################
|
|
|
|
# Special note about -pie. The underlying reason is that
|
|
|
|
# Lollipop refuses to run non-PIE. But what about older systems
|
|
|
|
# and NDKs? -fPIC was never problem, so the only concern is -pie.
|
|
|
|
# Older toolchains, e.g. r4, appear to handle it and binaries
|
|
|
|
# turn out mostly functional. "Mostly" means that oldest
|
|
|
|
# Androids, such as Froyo, fail to handle executable, but newer
|
|
|
|
# systems are perfectly capable of executing binaries targeting
|
|
|
|
# Froyo. Keep in mind that in the nutshell Android builds are
|
|
|
|
# about JNI, i.e. shared libraries, not applications.
|
|
|
|
cflags => add(sub { android_ndk()->{cflags} }),
|
|
|
|
cppflags => add(sub { android_ndk()->{cppflags} }),
|
|
|
|
cxxflags => add(sub { android_ndk()->{cflags} }),
|
|
|
|
bn_ops => sub { android_ndk()->{bn_ops} },
|
2020-03-04 05:55:09 +08:00
|
|
|
bin_cflags => "-fPIE",
|
|
|
|
bin_lflags => "-pie",
|
2018-04-23 22:37:03 +08:00
|
|
|
enable => [ ],
|
2021-03-29 18:36:34 +08:00
|
|
|
shared_extension => ".so",
|
2018-03-12 02:08:56 +08:00
|
|
|
},
|
|
|
|
"android-arm" => {
|
|
|
|
################################################################
|
|
|
|
# Contemporary Android applications can provide multiple JNI
|
|
|
|
# providers in .apk, targeting multiple architectures. Among
|
|
|
|
# them there is "place" for two ARM flavours: generic eabi and
|
|
|
|
# armv7-a/hard-float. However, it should be noted that OpenSSL's
|
|
|
|
# ability to engage NEON is not constrained by ABI choice, nor
|
|
|
|
# is your ability to call OpenSSL from your application code
|
|
|
|
# compiled with floating-point ABI other than default 'soft'.
|
2018-03-16 19:14:28 +08:00
|
|
|
# (Latter thanks to __attribute__((pcs("aapcs"))) declaration.)
|
2018-03-12 02:08:56 +08:00
|
|
|
# This means that choice of ARM libraries you provide in .apk
|
|
|
|
# is driven by application needs. For example if application
|
|
|
|
# itself benefits from NEON or is floating-point intensive, then
|
|
|
|
# it might be appropriate to provide both libraries. Otherwise
|
|
|
|
# just generic eabi would do. But in latter case it would be
|
|
|
|
# appropriate to
|
|
|
|
#
|
|
|
|
# ./Configure android-arm -D__ARM_MAX_ARCH__=8
|
|
|
|
#
|
|
|
|
# in order to build "universal" binary and allow OpenSSL take
|
|
|
|
# advantage of NEON when it's available.
|
|
|
|
#
|
2018-03-16 19:14:28 +08:00
|
|
|
# Keep in mind that (just like with linux-armv4) we rely on
|
2018-03-12 02:08:56 +08:00
|
|
|
# compiler defaults, which is not necessarily what you had
|
|
|
|
# in mind, in which case you would have to pass additional
|
|
|
|
# -march and/or -mfloat-abi flags. NDK defaults to armv5te.
|
2018-03-17 17:59:57 +08:00
|
|
|
# Newer NDK versions reportedly require additional -latomic.
|
2018-03-12 02:08:56 +08:00
|
|
|
#
|
2019-06-17 05:01:10 +08:00
|
|
|
inherit_from => [ "android" ],
|
2018-03-15 00:30:22 +08:00
|
|
|
bn_ops => add("RC4_CHAR"),
|
2019-06-16 19:32:58 +08:00
|
|
|
asm_arch => 'armv4',
|
|
|
|
perlasm_scheme => "void",
|
2018-03-12 02:08:56 +08:00
|
|
|
},
|
|
|
|
"android-arm64" => {
|
2019-06-17 05:01:10 +08:00
|
|
|
inherit_from => [ "android" ],
|
2018-03-15 00:30:22 +08:00
|
|
|
bn_ops => add("RC4_CHAR"),
|
2019-06-16 19:32:58 +08:00
|
|
|
asm_arch => 'aarch64',
|
2018-03-12 02:08:56 +08:00
|
|
|
perlasm_scheme => "linux64",
|
|
|
|
},
|
|
|
|
|
|
|
|
"android-mips" => {
|
2019-06-17 05:01:10 +08:00
|
|
|
inherit_from => [ "android" ],
|
2018-03-15 00:30:22 +08:00
|
|
|
bn_ops => add("RC4_CHAR"),
|
2019-06-16 19:32:58 +08:00
|
|
|
asm_arch => 'mips32',
|
2018-03-12 02:08:56 +08:00
|
|
|
perlasm_scheme => "o32",
|
|
|
|
},
|
|
|
|
"android-mips64" => {
|
|
|
|
################################################################
|
|
|
|
# You are more than likely have to specify target processor
|
|
|
|
# on ./Configure command line. Trouble is that toolchain's
|
|
|
|
# default is MIPS64r6 (at least in r10d), but there are no
|
|
|
|
# such processors around (or they are too rare to spot one).
|
|
|
|
# Actual problem is that MIPS64r6 is binary incompatible
|
|
|
|
# with previous MIPS ISA versions, in sense that unlike
|
|
|
|
# prior versions original MIPS binary code will fail.
|
|
|
|
#
|
2019-06-17 05:01:10 +08:00
|
|
|
inherit_from => [ "android" ],
|
2018-03-15 00:30:22 +08:00
|
|
|
bn_ops => add("RC4_CHAR"),
|
2019-06-16 19:32:58 +08:00
|
|
|
asm_arch => 'mips64',
|
2018-03-12 02:08:56 +08:00
|
|
|
perlasm_scheme => "64",
|
|
|
|
},
|
|
|
|
|
|
|
|
"android-x86" => {
|
2019-06-17 05:01:10 +08:00
|
|
|
inherit_from => [ "android" ],
|
2018-03-12 02:08:56 +08:00
|
|
|
CFLAGS => add(picker(release => "-fomit-frame-pointer")),
|
|
|
|
bn_ops => add("RC4_INT"),
|
2019-06-16 19:32:58 +08:00
|
|
|
asm_arch => 'x86',
|
2018-03-12 02:08:56 +08:00
|
|
|
perlasm_scheme => "android",
|
|
|
|
},
|
|
|
|
"android-x86_64" => {
|
2019-06-17 05:01:10 +08:00
|
|
|
inherit_from => [ "android" ],
|
2018-03-12 02:08:56 +08:00
|
|
|
bn_ops => add("RC4_INT"),
|
2019-06-16 19:32:58 +08:00
|
|
|
asm_arch => 'x86_64',
|
2018-03-12 02:08:56 +08:00
|
|
|
perlasm_scheme => "elf",
|
|
|
|
},
|
|
|
|
|
|
|
|
####################################################################
|
2020-02-03 05:54:01 +08:00
|
|
|
# Backward compatible targets, (might) require $CROSS_SYSROOT
|
2018-03-12 02:08:56 +08:00
|
|
|
#
|
|
|
|
"android-armeabi" => {
|
|
|
|
inherit_from => [ "android-arm" ],
|
|
|
|
},
|
|
|
|
"android64" => {
|
|
|
|
inherit_from => [ "android" ],
|
|
|
|
},
|
|
|
|
"android64-aarch64" => {
|
|
|
|
inherit_from => [ "android-arm64" ],
|
|
|
|
},
|
|
|
|
"android64-x86_64" => {
|
|
|
|
inherit_from => [ "android-x86_64" ],
|
|
|
|
},
|
|
|
|
"android64-mips64" => {
|
|
|
|
inherit_from => [ "android-mips64" ],
|
|
|
|
},
|
|
|
|
);
|