-
Notifications
You must be signed in to change notification settings - Fork 3
/
buildZig.nix
332 lines (316 loc) · 11.1 KB
/
buildZig.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
{ lib, zig, stdenvNoCC, pkg-config, system }:
{ name ? "${args'.pname}-${args'.version}"
# pkg-config prefix within buildInputs. this may depend on
# the target system, e.g. debian on arm uses /usr/lib/arm-linux-gnueabihf/pkgconfig
, pkgConfigPrefix ? "/lib/pkgconfig"
# Build inputs in the format of the target system.
# May be foreign packages, e.g. from Debian.
, buildInputs ? [ ]
# as specified for mkDerivation
, nativeBuildInputs ? [ ]
# buildZig autogenerates a `build.zig` file.
# You can customize it with the following two attributes:
# - buildZigAdditionalHeader is inserted after the std import
# - buildZigAdditional is inserted into the build function after
# all steps have been declared.
, buildZigAdditionalHeader ? "", buildZigAdditional ? ""
# list of executables to be built from zig code.
# each list item is to have the following structure:
#
# {
# name = <output name>;
# file = <string: relative path to zig file containing `pub fn main`>;
# dependencies = <list of zig packages>; # optional
# install = <bool>; # optional
# target = <attrset>; # optional
# stage1 = <bool>; # optional, only for zig stage2 compiler
# generators = [
# {
# name = <output name>; # name of a zigExecutable
# # TODO: args? working directory?
# } ...
# ]; # optional
# }
, zigExecutables ? [ ]
# list of libraries to be built from zig code.
# each list item is to have the following structure:
#
# {
# name = <output name>;
# file = <string: relative path to main zig file>;
# dependencies = <list of zig packages>; # optional
# install = <bool>; # optional
# target = <attrset>; # optional
# stage1 = <bool>; # optional, only for zig stage2 compiler
# generators = [ … ]; # optional, see above
# }
, zigLibraries ? [ ]
# list of tests to run after building.
# each list item is to have the following structure:
#
# {
# name = <name the tests can be called with (via `zig build $name`)>;
# description = <string that describes the test>;
# file = <string: relative path to zig file containing the test(s)>;
# dependencies = <list of zig packages>;
# stage1 = <bool>; # optional, only for zig stage2 compiler
# generators = [ … ]; # optional, see above
# }
, zigTests ? [ ], ... }@args':
let
declZigPackage = with builtins;
ctx: package:
if hasAttr package.name ctx.state then
ctx
else
let deps = foldl' declZigPackage ctx (package.dependencies or [ ]);
in rec {
state = deps.state // {
"${package.name}" = "p${toString (length (attrNames deps.state))}";
};
code = deps.code + ''
const ${state.${package.name}} = std.build.Pkg{
.name = "${package.name}",
.source = .{.path = "${
if (builtins.hasAttr "src" package) then "${package.src}/" else ""
}${package.main}"},
.dependencies = &.{
${
lib.concatStringsSep ''
,
'' (map (package: state.${package.name})
(package.dependencies or [ ]))
}
}
};
'';
};
in stdenvNoCC.mkDerivation ((builtins.removeAttrs args' [
"zigExecutables"
"zigLibraries"
"zigTests"
"pkgConfigPrefix"
"buildZigAdditional"
"buildZigAdditionalHeader"
]) // {
# needed because args' doesn't contain the default values
inherit buildZigAdditional buildZigAdditionalHeader pkgConfigPrefix;
nativeBuildInputs = nativeBuildInputs ++ [ pkg-config zig ];
doCheck = builtins.length zigTests > 0;
configurePhase = let
fullDeps = builtins.foldl' declZigPackage {
state = { };
code = "";
} (lib.lists.flatten (builtins.catAttrs "dependencies"
(zigExecutables ++ zigLibraries ++ zigTests)));
needsTarget =
builtins.foldl' (x: y: x || (!(builtins.hasAttr "target" y))) false
(zigLibraries ++ zigExecutables);
toStructLiteral = set: ''
.{${
lib.concatStrings
(lib.mapAttrsToList (name: value: ".${name} = .${value}, ") set)
}}
'';
in ''
targetSharePath=${
if builtins.hasAttr "targetSharePath" args' then
args'.targetSharePath
else
"$out/share"
}
runHook preConfigure
mycat() {
local REPLY
read -r -d "" || printf '%s' "$REPLY"
}
PKG_CONFIG_PATH=
PKG_CONFIG_LIBS=
for input in $buildInputs; do
curPath=$input$pkgConfigPrefix
PKG_CONFIG_PATH=$curPath:$PKG_CONFIG_PATH
for file in $curPath/*.pc; do
[ -e "$file" ] || continue
filename=$(basename -- $file)
PKG_CONFIG_LIBS="$PKG_CONFIG_LIBS ''${filename%.pc}"
done
done
export PKG_CONFIG_PATH
mycat >build.zig <<-EOF
const std = @import("std");
EOF
printenv buildZigAdditionalHeader >>build.zig
mycat >>build.zig <<-EOF
${fullDeps.code}
fn addPkgConfigLibs(step: *std.build.LibExeObjStep) void {
EOF
for lib in $PKG_CONFIG_LIBS; do
mycat >>build.zig <<-EOF
step.linkSystemLibraryPkgConfigOnly("$lib");
EOF
done
mycat >>build.zig <<-EOF
step.linkLibC();
}
fn testEmitOption(
emit_bin: bool,
name: []const u8,
) std.build.LibExeObjStep.EmitOption {
return if (!emit_bin) std.build.LibExeObjStep.EmitOption.default else
std.build.LibExeObjStep.EmitOption{.emit_to = name};
}
pub fn build(b: *std.build.Builder) !void {
${
if needsTarget then
"const target = b.standardTargetOptions(.{});"
else
""
}
const mode = b.standardReleaseOptions();
${if builtins.length zigTests > 0 then ''
const test_filter =
b.option([]const u8, "test-filter", "filters tests when testing");
const emit_bin = (
b.option(bool, "emit_bin", "emit binaries for tests")
) orelse false;
'' else
""}
${lib.concatStrings (lib.imap1 (i: exec:
let v = "exec${toString i}";
in ''
const ${v} = b.addExecutable("${exec.name}", "${exec.file}");
${v}.setTarget(${
if (builtins.hasAttr "target" exec) then
(toStructLiteral exec.target)
else
"target"
});
${v}.setBuildMode(mode);
${v}.main_pkg_path = ".";
${if (exec.install or false) then ''
${v}.linkage = .dynamic;
${v}.install();
${if buildInputs != [ ] then "addPkgConfigLibs(${v});" else ""}
'' else
""}
${lib.concatStrings (builtins.map (pkg: ''
${v}.addPackage(${fullDeps.state.${pkg.name}});
'') (exec.dependencies or [ ]))}
${lib.concatStrings (builtins.map (gen: ''
${v}.step.dependOn(&exec${
builtins.toString (lib.findFirst (x: x.name == gen.name) null
(lib.imap1 (i: v: {
i = i;
name = v.name;
}) zigExecutables)).i
}_run.step);
'') (exec.generators or [ ]))}
${if (exec.stage1 or false) then "${v}.use_stage1 = true;" else ""}
const ${v}_step = b.step("${exec.name}", "${exec.description or ""}");
${v}_step.dependOn(&${v}.step);
const ${v}_run = ${v}.run();
${v}_run.cwd = ".";
${v}_run.step.dependOn(&${v}.step);
'') zigExecutables)}
${lib.concatStrings (lib.imap1 (i: ziglib:
let v = "lib${toString i}";
in ''
const ${v} = b.addSharedLibrary("${ziglib.name}", "${ziglib.file}", .unversioned);
${v}.setTarget(${
if (builtins.hasAttr "target" ziglib) then
(toStructLiteral ziglib.target)
else
"target"
});
${v}.setBuildMode(mode);
${v}.main_pkg_path = ".";
${if (ziglib.install or false) then ''
${v}.linkage = .dynamic;
${v}.install();
${if buildInputs != [ ] then "addPkgConfigLibs(${v});" else ""}
'' else
""}
${lib.concatStrings (builtins.map (pkg: ''
${v}.addPackage(${fullDeps.state.${pkg.name}});
'') (ziglib.dependencies or [ ]))}
${lib.concatStrings (builtins.map (gen: ''
${v}.step.dependOn(&exec${
builtins.toString (lib.findFirst (x: x.name == gen.name) null
(lib.imap1 (i: v: {
i = i;
name = v.name;
}) zigExecutables)).i
}_run.step);
'') (ziglib.generators or [ ]))}
${if (ziglib.stage1 or false) then "${v}.use_stage1 = true;" else ""}
const ${v}_step = b.step("${ziglib.name}", "${
ziglib.description or ""
}");
${v}_step.dependOn(&${v}.step);
'') zigLibraries)}
${lib.concatStrings (lib.imap1 (i: test:
let v = "test${toString i}";
in ''
const ${v} = b.addTest("${test.file}");
${lib.concatStrings (builtins.map (pkg: ''
${v}.addPackage(${fullDeps.state.${pkg.name}});
'') (test.dependencies or [ ]))}
${lib.concatStrings (builtins.map (gen: ''
${v}.step.dependOn(&exec${
builtins.toString (lib.findFirst (x: x.name == gen.name) null
(lib.imap1 (i: v: {
i = i;
name = v.name;
}) zigExecutables)).i
}_run.step);
'') (test.generators or [ ]))}
${v}.setFilter(test_filter);
${v}.emit_bin = testEmitOption(emit_bin, "${test.name}");
${v}.main_pkg_path = ".";
${if (test.stage1 or false) then "${v}.use_stage1 = true;" else ""}
const ${v}_step = b.step("${test.name}", "${test.description or ""}");
${v}_step.dependOn(&${v}.step);
'') zigTests)}
${if zigTests != [ ] then ''
const test_step = b.step("test", "run all tests");
${lib.concatStrings (lib.imap1 (i: _: ''
test_step.dependOn(&test${toString i}.step);
'') zigTests)}
'' else
""}
EOF
printenv buildZigAdditional >>build.zig
echo "}" >>build.zig
runHook postConfigure
'';
buildPhase = ''
runHook preBuild
export ZIG_LOCAL_CACHE_DIR=$(pwd)/zig-cache
export ZIG_GLOBAL_CACHE_DIR=$ZIG_LOCAL_CACHE_DIR
export NIX_CFLAGS_COMPILE=
export NIX_LDFLAGS=
ADDITIONAL_FLAGS=
if ! [ -z ''${ZIG_TARGET+x} ]; then
ADDITIONAL_FLAGS="$ADDITIONAL_FLAGS -Dtarget=$ZIG_TARGET"
fi
export PATH=${pkg-config}/bin:$PATH # so that zig build sees it
${zig}/bin/zig build $ADDITIONAL_FLAGS
runHook postBuild
'';
# don't check when cross-compiling
checkPhase = if zigTests == [ ] then
""
else ''
if [ -z ''${ZIG_TARGET+x} ]; then
runHook preCheck
${zig}/bin/zig build test $ADDITIONAL_FLAGS
runHook postCheck
fi
'';
installPhase = ''
runHook preInstall
mkdir -p $out
cp -r zig-out/* $out/
runHook postInstall
'';
})