1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
{
config,
lib,
stdenv,
makeWrapper,
runCommand,
wrapBintoolsWith,
wrapCCWith,
autoPatchelfHook,
llvmPackages,
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.
#
ndkBuildInfoFun =
fallback:
{
x86_64-apple-darwin = {
double = "darwin-x86_64";
};
x86_64-unknown-linux-gnu = {
double = "linux-x86_64";
};
arm64-apple-darwin = {
# the difference in `arm64` attribute vs `x86_64` double is purposeful.
# the Android NDK contains a single Universal 2 Darwin binary
# which supports arm64 and x86. The NDK was never updated to differentiate
# or unify architecture with its folder structure, so the binary supporting both architectures
# is stored under a single directory indicating `x86_64` despite also supporting darwin arm64.
double = "darwin-x86_64";
};
}
.${stdenv.buildPlatform.config} or fallback;
ndkTargetInfoFun =
fallback:
{
i686-unknown-linux-android = {
triple = "i686-linux-android";
arch = "x86";
};
x86_64-unknown-linux-android = {
triple = "x86_64-linux-android";
arch = "x86_64";
};
armv7a-unknown-linux-androideabi = {
arch = "arm";
triple = "arm-linux-androideabi";
};
aarch64-unknown-linux-android = {
arch = "arm64";
triple = "aarch64-linux-android";
};
}
.${stdenv.targetPlatform.config} or fallback;
buildInfo = ndkBuildInfoFun (
throw "Android NDK doesn't support building on ${stdenv.buildPlatform.config}, as far as we know"
);
targetInfo = ndkTargetInfoFun (
throw "Android NDK doesn't support targetting ${stdenv.targetPlatform.config}, as far as we know"
);
androidSdkVersion =
if
(stdenv.targetPlatform ? androidSdkVersion && stdenv.targetPlatform.androidSdkVersion != null)
then
stdenv.targetPlatform.androidSdkVersion
else
(throw "`androidSdkVersion` is not set during the importing of nixpkgs");
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.
targetPrefix = lib.optionalString (stdenv.targetPlatform != stdenv.hostPlatform) (
stdenv.targetPlatform.config + "-"
);
in
if !config.allowAliases && (ndkBuildInfoFun null == null || ndkTargetInfoFun null == null) then
# Don't throw without aliases to not break CI.
null
else
lib.recurseIntoAttrs rec {
# Misc tools
binaries = stdenv.mkDerivation {
pname = "${targetPrefix}ndk-toolchain";
inherit (androidndk) version;
nativeBuildInputs = [
makeWrapper
]
++ lib.optionals stdenv.buildPlatform.isLinux [ autoPatchelfHook ];
propagatedBuildInputs = [ androidndk ];
passthru = {
inherit targetPrefix;
isClang = true; # clang based cc, but bintools ld
inherit (llvmPackages.clang.cc) hardeningUnsupportedFlagsByTargetPlatform;
};
dontUnpack = true;
dontBuild = true;
dontStrip = true;
dontConfigure = true;
dontPatch = true;
autoPatchelfIgnoreMissingDeps = stdenv.buildPlatform.isLinux;
installPhase = ''
# 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 {} \;
if [ ! -d $out/toolchain/sysroot/usr/lib/${targetInfo.triple}/${androidSdkVersion} ]; then
echo "NDK does not contain libraries for SDK version ${androidSdkVersion}";
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}/${androidSdkVersion}/*.so $out/lib/
ln -s $out/toolchain/sysroot/usr/lib/${targetInfo.triple}/${androidSdkVersion}/*.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
ln -s $f ''${f/${targetInfo.triple}-/${targetPrefix}}
done
for f in $(find $out/toolchain -type d -name ${targetInfo.triple}); do
ln -s $f ''${f/${targetInfo.triple}/${targetPrefix}}
done
rm -f $out/bin/${targetPrefix}ld
ln -s $out/bin/lld $out/bin/${targetPrefix}ld
(cd $out/bin;
for tool in llvm-*; do
ln -sf $tool ${targetPrefix}$(echo $tool | sed 's/llvm-//')
ln -sf $tool $(echo $tool | sed 's/llvm-//')
done)
ln -sf $out/bin/yasm $out/bin/${targetPrefix}as
ln -sf $out/bin/yasm $out/bin/as
patchShebangs $out/bin
'';
meta = {
description = "Android NDK toolchain, tuned for other platforms";
license = with lib.licenses; [ unfree ];
teams = [ lib.teams.android ];
};
};
binutils = wrapBintoolsWith {
bintools = binaries;
libc = targetAndroidndkPkgs.libraries;
};
clang = wrapCCWith {
cc = binaries // {
# for packages expecting libcompiler-rt, etc. to come from here (stdenv.cc.cc.lib)
lib = targetAndroidndkPkgs.libraries;
};
bintools = binutils;
libc = targetAndroidndkPkgs.libraries;
extraBuildCommands = ''
echo "-D__ANDROID_API__=${stdenv.targetPlatform.androidSdkVersion}" >> $out/nix-support/cc-cflags
# 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
echo "-z,noexecstack -z,relro -z,now -z,muldefs" >> $out/nix-support/cc-ldflags
echo 'expandResponseParams "$@"' >> $out/nix-support/add-flags.sh
echo 'if [[ ! (" ''${params[@]} " =~ " -shared ") && ! (" ''${params[@]} " =~ " -no-pie ") ]]; then NIX_LDFLAGS_${suffixSalt}+=" -pie"; fi' >> $out/nix-support/add-flags.sh
echo "-Xclang -mnoexecstack" >> $out/nix-support/cc-cxxflags
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
'';
};
# 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.
libraries = runCommand "bionic-prebuilt" { } ''
lpath=${buildAndroidndk}/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/${buildInfo.double}/sysroot/usr/lib/${targetInfo.triple}/${androidSdkVersion}
if [ ! -d $lpath ]; then
echo "NDK does not contain libraries for SDK version ${androidSdkVersion} <$lpath>"
exit 1
fi
mkdir -p $out/lib
cp $lpath/*.so $lpath/*.a $out/lib
chmod +w $out/lib/*
cp $lpath/* $out/lib
'';
}
|