2022-06-29 09:24:50 +00:00
|
|
|
{ lib, stdenv, makeWrapper
|
2022-06-29 09:24:19 +00:00
|
|
|
, runCommand, wrapBintoolsWith, wrapCCWith, autoPatchelfHook
|
2017-02-17 05:36:10 +00:00
|
|
|
, 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.
|
2022-06-29 09:24:19 +00:00
|
|
|
#
|
|
|
|
# FIXME:
|
|
|
|
# There's some dragons here. Build host and target concepts are being mixed up.
|
2017-02-17 05:36:10 +00:00
|
|
|
ndkInfoFun = { config, ... }: {
|
2019-08-13 21:52:01 +00:00
|
|
|
x86_64-apple-darwin = {
|
2018-09-18 02:34:37 +00:00
|
|
|
double = "darwin-x86_64";
|
|
|
|
};
|
2019-08-13 21:52:01 +00:00
|
|
|
x86_64-unknown-linux-gnu = {
|
2017-02-17 05:36:10 +00:00
|
|
|
double = "linux-x86_64";
|
|
|
|
};
|
2019-08-13 21:52:01 +00:00
|
|
|
i686-unknown-linux-android = {
|
2019-04-10 05:23:02 +00:00
|
|
|
triple = "i686-linux-android";
|
|
|
|
arch = "x86";
|
|
|
|
};
|
2019-08-13 21:52:01 +00:00
|
|
|
x86_64-unknown-linux-android = {
|
2019-04-10 05:23:02 +00:00
|
|
|
triple = "x86_64-linux-android";
|
|
|
|
arch = "x86_64";
|
2018-02-27 23:36:48 +00:00
|
|
|
};
|
2019-08-13 21:52:01 +00:00
|
|
|
armv7a-unknown-linux-androideabi = {
|
2017-02-17 05:36:10 +00:00
|
|
|
arch = "arm";
|
|
|
|
triple = "arm-linux-androideabi";
|
|
|
|
};
|
2019-08-13 21:52:01 +00:00
|
|
|
aarch64-unknown-linux-android = {
|
2017-02-17 05:36:10 +00:00
|
|
|
arch = "arm64";
|
|
|
|
triple = "aarch64-linux-android";
|
|
|
|
};
|
|
|
|
}.${config} or
|
|
|
|
(throw "Android NDK doesn't support ${config}, as far as we know");
|
|
|
|
|
2022-06-29 09:24:19 +00:00
|
|
|
buildInfo = ndkInfoFun stdenv.buildPlatform;
|
2018-08-20 18:43:41 +00:00
|
|
|
hostInfo = ndkInfoFun stdenv.hostPlatform;
|
|
|
|
targetInfo = ndkInfoFun stdenv.targetPlatform;
|
2019-04-10 05:23:02 +00:00
|
|
|
|
2022-06-29 09:24:19 +00:00
|
|
|
inherit (stdenv.targetPlatform) sdkVer;
|
|
|
|
suffixSalt = lib.replaceStrings ["-" "."] ["_" "_"] stdenv.targetPlatform.config;
|
|
|
|
|
|
|
|
# targetInfo.triple is what Google thinks the toolchain should be, this is a little
|
|
|
|
# different from what we use. We make it four parts to conform with the existing
|
|
|
|
# standard more properly.
|
2022-09-29 13:58:12 +00:00
|
|
|
targetPrefix = lib.optionalString (stdenv.targetPlatform != stdenv.hostPlatform) (stdenv.targetPlatform.config + "-");
|
2017-02-17 05:36:10 +00:00
|
|
|
in
|
|
|
|
|
|
|
|
rec {
|
|
|
|
# Misc tools
|
2022-06-29 09:24:19 +00:00
|
|
|
binaries = stdenv.mkDerivation {
|
2022-09-29 13:58:12 +00:00
|
|
|
pname = "${targetPrefix}ndk-toolchain";
|
2020-09-08 13:32:06 +00:00
|
|
|
inherit (androidndk) version;
|
2022-06-29 09:24:50 +00:00
|
|
|
nativeBuildInputs = [ makeWrapper autoPatchelfHook ];
|
2019-12-05 13:43:47 +00:00
|
|
|
propagatedBuildInputs = [ androidndk ];
|
2022-06-26 18:02:31 +00:00
|
|
|
passthru = {
|
2022-09-29 13:58:12 +00:00
|
|
|
inherit targetPrefix;
|
2022-06-26 18:02:31 +00:00
|
|
|
isClang = true; # clang based cc, but bintools ld
|
|
|
|
};
|
2022-06-29 09:24:19 +00:00
|
|
|
dontUnpack = true;
|
|
|
|
dontBuild = true;
|
|
|
|
dontStrip = true;
|
|
|
|
dontConfigure = true;
|
|
|
|
dontPatch = true;
|
|
|
|
autoPatchelfIgnoreMissingDeps = true;
|
|
|
|
installPhase = ''
|
2022-06-29 09:24:50 +00:00
|
|
|
# https://developer.android.com/ndk/guides/other_build_systems
|
|
|
|
mkdir -p $out
|
|
|
|
cp -r ${androidndk}/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/${buildInfo.double} $out/toolchain
|
|
|
|
find $out/toolchain -type d -exec chmod 777 {} \;
|
2022-06-29 09:24:19 +00:00
|
|
|
|
|
|
|
if [ ! -d $out/toolchain/sysroot/usr/lib/${targetInfo.triple}/${sdkVer} ]; then
|
|
|
|
echo "NDK does not contain libraries for SDK version ${sdkVer}";
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
ln -vfs $out/toolchain/sysroot/usr/lib $out/lib
|
|
|
|
ln -s $out/toolchain/sysroot/usr/lib/${targetInfo.triple}/*.so $out/lib/
|
|
|
|
ln -s $out/toolchain/sysroot/usr/lib/${targetInfo.triple}/*.a $out/lib/
|
|
|
|
chmod +w $out/lib/*
|
|
|
|
ln -s $out/toolchain/sysroot/usr/lib/${targetInfo.triple}/${sdkVer}/*.so $out/lib/
|
|
|
|
ln -s $out/toolchain/sysroot/usr/lib/${targetInfo.triple}/${sdkVer}/*.o $out/lib/
|
|
|
|
|
|
|
|
echo "INPUT(-lc++_static)" > $out/lib/libc++.a
|
|
|
|
|
|
|
|
ln -s $out/toolchain/bin $out/bin
|
|
|
|
ln -s $out/toolchain/${targetInfo.triple}/bin/* $out/bin/
|
|
|
|
for f in $out/bin/${targetInfo.triple}-*; do
|
2022-09-29 13:58:12 +00:00
|
|
|
ln -s $f ''${f/${targetInfo.triple}-/${targetPrefix}}
|
2022-06-29 09:24:19 +00:00
|
|
|
done
|
|
|
|
for f in $(find $out/toolchain -type d -name ${targetInfo.triple}); do
|
2022-09-29 13:58:12 +00:00
|
|
|
ln -s $f ''${f/${targetInfo.triple}/${targetPrefix}}
|
2022-06-29 09:24:19 +00:00
|
|
|
done
|
|
|
|
|
2022-09-29 13:58:12 +00:00
|
|
|
rm -f $out/bin/${targetPrefix}ld
|
|
|
|
ln -s $out/bin/lld $out/bin/${targetPrefix}ld
|
2022-06-29 09:24:19 +00:00
|
|
|
|
|
|
|
(cd $out/bin;
|
|
|
|
for tool in llvm-*; do
|
2022-09-29 13:58:12 +00:00
|
|
|
ln -sf $tool ${targetPrefix}$(echo $tool | sed 's/llvm-//')
|
2022-06-29 09:24:19 +00:00
|
|
|
ln -sf $tool $(echo $tool | sed 's/llvm-//')
|
|
|
|
done)
|
|
|
|
|
|
|
|
# handle last, as llvm-as is for llvm bytecode
|
2022-09-29 13:58:12 +00:00
|
|
|
ln -sf $out/bin/${targetInfo.triple}-as $out/bin/${targetPrefix}as
|
2022-06-29 09:24:19 +00:00
|
|
|
ln -sf $out/bin/${targetInfo.triple}-as $out/bin/as
|
|
|
|
|
|
|
|
patchShebangs $out/bin
|
|
|
|
'';
|
|
|
|
};
|
2017-02-17 05:36:10 +00:00
|
|
|
|
|
|
|
binutils = wrapBintoolsWith {
|
|
|
|
bintools = binaries;
|
|
|
|
libc = targetAndroidndkPkgs.libraries;
|
|
|
|
};
|
|
|
|
|
2019-04-10 05:23:02 +00:00
|
|
|
clang = wrapCCWith {
|
2020-05-16 09:22:24 +00: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-24 23:15:36 +00:00
|
|
|
extraBuildCommands = ''
|
2018-08-20 18:43:41 +00:00
|
|
|
echo "-D__ANDROID_API__=${stdenv.targetPlatform.sdkVer}" >> $out/nix-support/cc-cflags
|
2022-06-29 09:24:50 +00:00
|
|
|
# Android needs executables linked with -pie since version 5.0
|
|
|
|
# Use -fPIC for compilation, and link with -pie if no -shared flag used in ldflags
|
|
|
|
echo "-target ${targetInfo.triple} -fPIC" >> $out/nix-support/cc-cflags
|
2022-09-21 14:31:09 +00:00
|
|
|
echo "-z,noexecstack -z,relro -z,now -z,muldefs" >> $out/nix-support/cc-ldflags
|
2022-06-29 09:24:50 +00:00
|
|
|
echo 'if [[ ! " $@ " =~ " -shared " ]]; then NIX_LDFLAGS_${suffixSalt}+=" -pie"; fi' >> $out/nix-support/add-flags.sh
|
|
|
|
echo "-Xclang -mnoexecstack" >> $out/nix-support/cc-cxxflags
|
2022-06-29 09:24:19 +00:00
|
|
|
if [ ${targetInfo.triple} == arm-linux-androideabi ]; then
|
|
|
|
# https://android.googlesource.com/platform/external/android-cmake/+/refs/heads/cmake-master-dev/android.toolchain.cmake
|
|
|
|
echo "--fix-cortex-a8" >> $out/nix-support/cc-ldflags
|
|
|
|
fi
|
2019-04-09 18:21:54 +00: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 05:23:02 +00:00
|
|
|
libraries = runCommand "bionic-prebuilt" {} ''
|
2022-06-29 09:24:50 +00:00
|
|
|
lpath=${buildAndroidndk}/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/${buildInfo.double}/sysroot/usr/lib/${targetInfo.triple}/${sdkVer}
|
2022-06-29 09:24:19 +00:00
|
|
|
if [ ! -d $lpath ]; then
|
|
|
|
echo "NDK does not contain libraries for SDK version ${sdkVer} <$lpath>"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
mkdir -p $out/lib
|
|
|
|
cp $lpath/*.so $lpath/*.a $out/lib
|
|
|
|
chmod +w $out/lib/*
|
|
|
|
cp $lpath/* $out/lib
|
2019-04-10 05:23:02 +00:00
|
|
|
'';
|
2017-02-17 05:36:10 +00:00
|
|
|
}
|