This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
toolchain.nix
127 lines (113 loc) · 4.05 KB
/
toolchain.nix
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
{ utils, lib, writeShellScript, emptyFile, gnugrep, coreutils, localSystem, zig, llvm }:
{
mkDerivation,
wrapCCWith,
wrapBintoolsWith,
libc ? null,
targetSystem,
targetPkgs,
}:
with lib;
with builtins;
let
zig-target = utils.nixTargetToZigTarget targetSystem.parsed;
# FIXME: some quirks here should be fixed upstream
# meson: -Wl,--version
# v8/gn: inserts --target=, we do not want to ever compile to a platform we do not expect
# v8/gn: -latomic already built into compiler_rt, perhaps v8/gn just thinks we are gcc instead?
write-cc-wrapper = cmd: writeShellScript "zig-${cmd}" ''
shopt -s extglob
args=()
while [[ $# -gt 0 ]]; do
case "$1" in
-Wl,--version)
echo "LLD 11.1.0 (compatible with GNU linkers)"
exit 0;;
-Wl,--no-undefined-version)
shift;;
-Wl,--unresolved-symbols=ignore-in-object-files)
args+=("-Wl,-undefined,dynamic_lookup")
shift;;
-Wl,--unresolved-symbols=*)
shift;;
-target)
shift;shift;;
--target=*)
shift;;
-latomic)
shift;;
*)
args+=("$1")
shift;;
esac
done
${zig}/bin/zig ${cmd} -target ${zig-target} "''${args[@]}"
'';
write-wrapper = cmd: writeShellScript "zig-${cmd}" ''${zig}/bin/zig ${cmd} "$@"'';
toolchain-unwrapped = let
prefix =
if localSystem.config != targetSystem.config
then "${targetSystem.config}-"
else "";
in mkDerivation {
name = "zig-toolchain";
inherit (zig) version;
isClang = true;
dontUnpack = true;
dontConfigure = true;
dontBuild = true;
dontFixup = true;
installPhase = ''
mkdir -p $out/bin $out/lib
for prog in ${llvm}/bin/*; do
ln -sf $prog $out/bin/${prefix}$(basename $prog)
done
ln -s ${llvm}/bin/llvm-as $out/bin/${prefix}as
ln -s ${llvm}/bin/llvm-dwp $out/bin/${prefix}dwp
ln -s ${llvm}/bin/llvm-nm $out/bin/${prefix}nm
ln -s ${llvm}/bin/llvm-objcopy $out/bin/${prefix}objcopy
ln -s ${llvm}/bin/llvm-objdump $out/bin/${prefix}objdump
ln -s ${llvm}/bin/llvm-readelf $out/bin/${prefix}readelf
ln -s ${llvm}/bin/llvm-size $out/bin/${prefix}size
ln -s ${llvm}/bin/llvm-strip $out/bin/${prefix}strip
ln -s ${llvm}/bin/llvm-rc $out/bin/${prefix}windres
for f in ar ranlib dlltool lib; do
rm -f ${prefix}$f
done
ln -s ${write-cc-wrapper "cc"} $out/bin/clang
ln -s ${write-cc-wrapper "c++"} $out/bin/clang++
ln -s ${write-wrapper "ar"} $out/bin/${prefix}ar
ln -s ${write-wrapper "ranlib"} $out/bin/${prefix}ranlib
ln -s ${write-wrapper "dlltool"} $out/bin/${prefix}dlltool
ln -s ${write-wrapper "lib"} $out/bin/${prefix}lib
'';
# Compatibility packages here:
propagatedBuildInputs = [] ++ optionals (isAttrs targetPkgs) (with targetPkgs; []
++ optionals (targetSystem.parsed.kernel.name == "darwin") [
# TODO: zig seems to be missing <err.h>
]);
};
in wrapCCWith {
inherit gnugrep coreutils libc;
cc = toolchain-unwrapped;
bintools = wrapBintoolsWith {
inherit gnugrep coreutils libc;
bintools = toolchain-unwrapped;
postLinkSignHook = emptyFile;
signingUtils = emptyFile;
};
# XXX: -march and -mcpu are not compatible
# https://github.com/ziglang/zig/issues/4911
extraBuildCommands = ''
rm -f $out/nix-support/add-local-cc-flags.before.sh
sed -i 's/\([^ ]\)-\([^ ]\)/\1_\2/g' $out/nix-support/cc-cflags-before || true
sed -i 's/-arch [^ ]* *//g' $out/nix-support/cc-cflags || true
'' + (optionalString (
targetSystem.parsed.cpu.name == "aarch64" || targetSystem.parsed.cpu.name == "aarch64_be" ||
targetSystem.parsed.cpu.name == "armv5tel" || targetSystem.parsed.cpu.name == "mipsel") ''
# error: Unknown CPU: ...
sed -i 's/-march[^ ]* *//g' $out/nix-support/cc-cflags-before || true
'') + (optionalString (targetSystem.parsed.cpu.name == "s390x") ''
printf " -march=''${S390X_MARCH:-arch8}" >> $out/nix-support/cc-cflags-before
'');
}