2021-01-23 17:15:07 +00:00
|
|
|
{ lib, stdenv
|
2017-02-17 05:36:10 +00:00
|
|
|
, makeWrapper
|
|
|
|
, runCommand, wrapBintoolsWith, wrapCCWith
|
|
|
|
, buildAndroidndk, androidndk, targetAndroidndkPkgs
|
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
|
|
|
# Mapping from a platform to information needed to unpack NDK stuff for that
|
|
|
|
# platform.
|
|
|
|
#
|
|
|
|
# N.B. The Android NDK uses slightly different LLVM-style platform triples
|
|
|
|
# than we do. We don't just use theirs because ours are less ambiguous and
|
|
|
|
# some builds need that clarity.
|
|
|
|
ndkInfoFun = { config, ... }: {
|
2019-08-13 22:52:01 +01:00
|
|
|
x86_64-apple-darwin = {
|
2018-09-18 03:34:37 +01:00
|
|
|
double = "darwin-x86_64";
|
|
|
|
};
|
2019-08-13 22:52:01 +01:00
|
|
|
x86_64-unknown-linux-gnu = {
|
2017-02-17 05:36:10 +00:00
|
|
|
double = "linux-x86_64";
|
|
|
|
};
|
2019-08-13 22:52:01 +01:00
|
|
|
i686-unknown-linux-android = {
|
2019-04-10 06:23:02 +01:00
|
|
|
triple = "i686-linux-android";
|
|
|
|
arch = "x86";
|
|
|
|
toolchain = "x86";
|
|
|
|
gccVer = "4.9";
|
|
|
|
};
|
2019-08-13 22:52:01 +01:00
|
|
|
x86_64-unknown-linux-android = {
|
2019-04-10 06:23:02 +01:00
|
|
|
triple = "x86_64-linux-android";
|
|
|
|
arch = "x86_64";
|
2019-04-10 06:31:04 +01:00
|
|
|
toolchain = "x86_64";
|
2019-04-10 06:23:02 +01:00
|
|
|
gccVer = "4.9";
|
2018-02-27 23:36:48 +00:00
|
|
|
};
|
2019-08-13 22:52:01 +01:00
|
|
|
armv7a-unknown-linux-androideabi = {
|
2017-02-17 05:36:10 +00:00
|
|
|
arch = "arm";
|
|
|
|
triple = "arm-linux-androideabi";
|
2019-04-10 06:23:02 +01:00
|
|
|
toolchain = "arm-linux-androideabi";
|
2018-06-22 14:33:25 +01:00
|
|
|
gccVer = "4.9";
|
2017-02-17 05:36:10 +00:00
|
|
|
};
|
2019-08-13 22:52:01 +01:00
|
|
|
aarch64-unknown-linux-android = {
|
2017-02-17 05:36:10 +00:00
|
|
|
arch = "arm64";
|
|
|
|
triple = "aarch64-linux-android";
|
2019-04-10 06:23:02 +01:00
|
|
|
toolchain = "aarch64-linux-android";
|
2017-02-17 05:36:10 +00:00
|
|
|
gccVer = "4.9";
|
|
|
|
};
|
|
|
|
}.${config} or
|
|
|
|
(throw "Android NDK doesn't support ${config}, as far as we know");
|
|
|
|
|
2018-08-20 19:43:41 +01:00
|
|
|
hostInfo = ndkInfoFun stdenv.hostPlatform;
|
|
|
|
targetInfo = ndkInfoFun stdenv.targetPlatform;
|
2019-04-10 06:23:02 +01:00
|
|
|
|
2021-01-23 17:15:07 +00:00
|
|
|
prefix = lib.optionalString (stdenv.targetPlatform != stdenv.hostPlatform) (stdenv.targetPlatform.config + "-");
|
2017-02-17 05:36:10 +00:00
|
|
|
in
|
|
|
|
|
|
|
|
rec {
|
|
|
|
# Misc tools
|
2020-09-08 14:32:06 +01:00
|
|
|
binaries = runCommand "ndk-toolchain-binutils" {
|
|
|
|
pname = "ndk-toolchain-binutils";
|
|
|
|
inherit (androidndk) version;
|
2019-04-10 06:23:02 +01:00
|
|
|
isClang = true; # clang based cc, but bintools ld
|
|
|
|
nativeBuildInputs = [ makeWrapper ];
|
2019-12-05 13:43:47 +00:00
|
|
|
propagatedBuildInputs = [ androidndk ];
|
2019-04-10 06:23:02 +01:00
|
|
|
} ''
|
|
|
|
mkdir -p $out/bin
|
2018-12-18 21:58:12 +00:00
|
|
|
|
2019-04-10 06:23:02 +01:00
|
|
|
# llvm toolchain
|
|
|
|
for prog in ${androidndk}/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/${hostInfo.double}/bin/*; do
|
|
|
|
ln -s $prog $out/bin/$(basename $prog)
|
|
|
|
ln -s $prog $out/bin/${prefix}$(basename $prog)
|
|
|
|
done
|
2018-12-18 21:58:12 +00:00
|
|
|
|
2019-04-10 06:23:02 +01:00
|
|
|
# bintools toolchain
|
|
|
|
for prog in ${androidndk}/libexec/android-sdk/ndk-bundle/toolchains/${targetInfo.toolchain}-${targetInfo.gccVer}/prebuilt/${hostInfo.double}/bin/*; do
|
|
|
|
prog_suffix=$(basename $prog | sed 's/${targetInfo.triple}-//')
|
|
|
|
ln -s $prog $out/bin/${stdenv.targetPlatform.config}-$prog_suffix
|
|
|
|
done
|
|
|
|
|
|
|
|
# shitty googly wrappers
|
|
|
|
rm -f $out/bin/${stdenv.targetPlatform.config}-gcc $out/bin/${stdenv.targetPlatform.config}-g++
|
|
|
|
'';
|
2017-02-17 05:36:10 +00:00
|
|
|
|
|
|
|
binutils = wrapBintoolsWith {
|
|
|
|
bintools = binaries;
|
|
|
|
libc = targetAndroidndkPkgs.libraries;
|
|
|
|
};
|
|
|
|
|
2019-04-10 06:23:02 +01:00
|
|
|
clang = wrapCCWith {
|
2020-05-16 10:22:24 +01:00
|
|
|
cc = binaries // {
|
|
|
|
# for packages expecting libcompiler-rt, etc. to come from here (stdenv.cc.cc.lib)
|
|
|
|
lib = targetAndroidndkPkgs.libraries;
|
|
|
|
};
|
2017-02-17 05:36:10 +00:00
|
|
|
bintools = binutils;
|
|
|
|
libc = targetAndroidndkPkgs.libraries;
|
2018-06-25 00:15:36 +01:00
|
|
|
extraBuildCommands = ''
|
2018-08-20 19:43:41 +01:00
|
|
|
echo "-D__ANDROID_API__=${stdenv.targetPlatform.sdkVer}" >> $out/nix-support/cc-cflags
|
2019-04-10 06:23:02 +01:00
|
|
|
echo "-target ${stdenv.targetPlatform.config}" >> $out/nix-support/cc-cflags
|
|
|
|
echo "-resource-dir=$(echo ${androidndk}/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/${hostInfo.double}/lib*/clang/*)" >> $out/nix-support/cc-cflags
|
|
|
|
echo "--gcc-toolchain=${androidndk}/libexec/android-sdk/ndk-bundle/toolchains/${targetInfo.toolchain}-${targetInfo.gccVer}/prebuilt/${hostInfo.double}" >> $out/nix-support/cc-cflags
|
2019-04-09 19:21:54 +01:00
|
|
|
'';
|
2017-02-17 05:36:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# Bionic lib C and other libraries.
|
|
|
|
#
|
|
|
|
# We use androidndk from the previous stage, else we waste time or get cycles
|
|
|
|
# cross-compiling packages to wrap incorrectly wrap binaries we don't include
|
|
|
|
# anyways.
|
2019-04-10 06:23:02 +01:00
|
|
|
libraries = runCommand "bionic-prebuilt" {} ''
|
|
|
|
mkdir -p $out
|
|
|
|
cp -r ${buildAndroidndk}/libexec/android-sdk/ndk-bundle/sysroot/usr/include $out/include
|
|
|
|
chmod +w $out/include
|
|
|
|
cp -r ${buildAndroidndk}/libexec/android-sdk/ndk-bundle/sysroot/usr/include/${targetInfo.triple}/* $out/include
|
2019-09-23 18:40:45 +01:00
|
|
|
ln -s ${buildAndroidndk}/libexec/android-sdk/ndk-bundle/platforms/android-${stdenv.hostPlatform.sdkVer}/arch-${hostInfo.arch}/usr/${if hostInfo.arch == "x86_64" then "lib64" else "lib"} $out/lib
|
2019-04-10 06:23:02 +01:00
|
|
|
'';
|
2017-02-17 05:36:10 +00:00
|
|
|
}
|