2018-09-02 01:29:02 +02:00
|
|
|
{ lib, stdenv
|
2017-02-17 06:36:10 +01: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, ... }: {
|
2018-09-18 04:34:37 +02:00
|
|
|
"x86_64-apple-darwin" = {
|
|
|
|
double = "darwin-x86_64";
|
|
|
|
};
|
2017-02-17 06:36:10 +01:00
|
|
|
"x86_64-unknown-linux-gnu" = {
|
|
|
|
double = "linux-x86_64";
|
|
|
|
};
|
2018-02-28 00:36:48 +01:00
|
|
|
"armv5tel-unknown-linux-androideabi" = {
|
|
|
|
arch = "arm";
|
|
|
|
triple = "arm-linux-androideabi";
|
2018-06-22 16:21:43 +02:00
|
|
|
gccVer = "4.8";
|
2018-02-28 00:36:48 +01:00
|
|
|
};
|
|
|
|
"armv7a-unknown-linux-androideabi" = {
|
2017-02-17 06:36:10 +01:00
|
|
|
arch = "arm";
|
|
|
|
triple = "arm-linux-androideabi";
|
2018-06-22 15:33:25 +02:00
|
|
|
gccVer = "4.9";
|
2017-02-17 06:36:10 +01:00
|
|
|
};
|
|
|
|
"aarch64-unknown-linux-android" = {
|
|
|
|
arch = "arm64";
|
|
|
|
triple = "aarch64-linux-android";
|
|
|
|
gccVer = "4.9";
|
|
|
|
};
|
|
|
|
}.${config} or
|
|
|
|
(throw "Android NDK doesn't support ${config}, as far as we know");
|
|
|
|
|
2018-08-20 20:43:41 +02:00
|
|
|
hostInfo = ndkInfoFun stdenv.hostPlatform;
|
|
|
|
targetInfo = ndkInfoFun stdenv.targetPlatform;
|
2017-02-17 06:36:10 +01:00
|
|
|
in
|
|
|
|
|
|
|
|
rec {
|
|
|
|
# Misc tools
|
|
|
|
binaries = let
|
|
|
|
ndkBinDir =
|
2018-12-18 22:58:12 +01:00
|
|
|
"${androidndk}/libexec/android-sdk/ndk-bundle/toolchains/${targetInfo.triple}-${targetInfo.gccVer}/prebuilt/${hostInfo.double}/bin";
|
|
|
|
ndkGCCLibDir =
|
|
|
|
"${androidndk}/libexec/android-sdk/ndk-bundle/toolchains/${targetInfo.triple}-${targetInfo.gccVer}/prebuilt/${hostInfo.double}/lib/gcc/${targetInfo.triple}/4.9.x";
|
|
|
|
|
2017-02-17 06:36:10 +01:00
|
|
|
in runCommand "ndk-gcc-binutils" {
|
|
|
|
isGNU = true; # for cc-wrapper
|
|
|
|
nativeBuildInputs = [ makeWrapper ];
|
|
|
|
propgatedBuildInputs = [ androidndk ];
|
|
|
|
} ''
|
|
|
|
mkdir -p $out/bin
|
|
|
|
for prog in ${ndkBinDir}/${targetInfo.triple}-*; do
|
|
|
|
prog_suffix=$(basename $prog | sed 's/${targetInfo.triple}-//')
|
2018-12-18 22:58:12 +01:00
|
|
|
cat > $out/bin/${stdenv.targetPlatform.config}-$prog_suffix <<EOF
|
|
|
|
#! ${stdenv.shell} -e
|
|
|
|
$prog "\$@"
|
|
|
|
EOF
|
|
|
|
chmod +x $out/bin/${stdenv.targetPlatform.config}-$prog_suffix
|
2017-02-17 06:36:10 +01:00
|
|
|
done
|
2018-12-18 22:58:12 +01:00
|
|
|
|
|
|
|
ln -s $out/bin/${stdenv.targetPlatform.config}-ld $out/bin/ld
|
|
|
|
ln -s ${ndkGCCLibDir} $out/lib
|
2017-02-17 06:36:10 +01:00
|
|
|
'';
|
|
|
|
|
|
|
|
binutils = wrapBintoolsWith {
|
|
|
|
bintools = binaries;
|
|
|
|
libc = targetAndroidndkPkgs.libraries;
|
2018-06-24 22:33:20 +02:00
|
|
|
extraBuildCommands = ''
|
|
|
|
echo "--build-id" >> $out/nix-support/libc-ldflags
|
|
|
|
'';
|
2017-02-17 06:36:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
gcc = wrapCCWith {
|
|
|
|
cc = binaries;
|
|
|
|
bintools = binutils;
|
|
|
|
libc = targetAndroidndkPkgs.libraries;
|
2018-06-25 01:15:36 +02:00
|
|
|
extraBuildCommands = ''
|
2018-08-20 20:43:41 +02:00
|
|
|
echo "-D__ANDROID_API__=${stdenv.targetPlatform.sdkVer}" >> $out/nix-support/cc-cflags
|
2018-06-25 01:15:36 +02:00
|
|
|
''
|
2018-08-20 20:43:41 +02:00
|
|
|
+ lib.optionalString stdenv.targetPlatform.isAarch32 (let
|
|
|
|
p = stdenv.targetPlatform.platform.gcc or {}
|
|
|
|
// stdenv.targetPlatform.parsed.abi;
|
2018-02-28 00:36:48 +01:00
|
|
|
flags = lib.concatLists [
|
|
|
|
(lib.optional (p ? arch) "-march=${p.arch}")
|
|
|
|
(lib.optional (p ? cpu) "-mcpu=${p.cpu}")
|
|
|
|
(lib.optional (p ? abi) "-mabi=${p.abi}")
|
|
|
|
(lib.optional (p ? fpu) "-mfpu=${p.fpu}")
|
2018-05-12 21:07:31 +02:00
|
|
|
(lib.optional (p ? float) "-mfloat=${p.float}")
|
2018-02-28 00:36:48 +01:00
|
|
|
(lib.optional (p ? float-abi) "-mfloat-abi=${p.float-abi}")
|
|
|
|
(lib.optional (p ? mode) "-mmode=${p.mode}")
|
|
|
|
];
|
|
|
|
in ''
|
|
|
|
sed -E -i \
|
2018-08-20 20:43:41 +02:00
|
|
|
$out/bin/${stdenv.targetPlatform.config}-cc \
|
|
|
|
$out/bin/${stdenv.targetPlatform.config}-c++ \
|
|
|
|
$out/bin/${stdenv.targetPlatform.config}-gcc \
|
|
|
|
$out/bin/${stdenv.targetPlatform.config}-g++ \
|
2018-02-28 00:36:48 +01:00
|
|
|
-e '130i extraBefore+=(-Wl,--fix-cortex-a8)' \
|
|
|
|
-e 's|^(extraBefore=)\(\)$|\1(${builtins.toString flags})|'
|
|
|
|
'')
|
2017-02-17 06:36:10 +01:00
|
|
|
# GCC 4.9 is the first relase with "-fstack-protector"
|
2018-02-28 00:36:48 +01:00
|
|
|
+ lib.optionalString (lib.versionOlder targetInfo.gccVer "4.9") ''
|
2017-02-17 06:36:10 +01:00
|
|
|
sed -E \
|
|
|
|
-i $out/nix-support/add-hardening.sh \
|
|
|
|
-e 's|(-fstack-protector)-strong|\1|g'
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
# 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.
|
2018-05-17 17:22:27 +02:00
|
|
|
libraries =
|
|
|
|
let
|
2018-12-18 22:58:12 +01:00
|
|
|
includePath = "${buildAndroidndk}/libexec/android-sdk/ndk-bundle/sysroot/usr/include";
|
|
|
|
asmIncludePath = "${buildAndroidndk}/libexec/android-sdk/ndk-bundle/sysroot/usr/include/${targetInfo.triple}";
|
|
|
|
libPath = "${buildAndroidndk}/libexec/android-sdk/ndk-bundle/platforms/android-${stdenv.hostPlatform.sdkVer}/arch-${hostInfo.arch}/usr/lib/";
|
2018-05-17 17:22:27 +02:00
|
|
|
in
|
|
|
|
runCommand "bionic-prebuilt" {} ''
|
|
|
|
mkdir -p $out
|
|
|
|
cp -r ${includePath} $out/include
|
2018-12-18 22:58:12 +01:00
|
|
|
chmod u+w $out/include
|
|
|
|
cp -r ${asmIncludePath}/* $out/include
|
2018-05-17 17:22:27 +02:00
|
|
|
chmod +w $out/include
|
|
|
|
ln -s ${libPath} $out/lib
|
|
|
|
'';
|
2017-02-17 06:36:10 +01:00
|
|
|
}
|