-
Notifications
You must be signed in to change notification settings - Fork 77
/
build.zig
545 lines (482 loc) · 19.2 KB
/
build.zig
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
const std = @import("std");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
b.reference_trace = 64;
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
const skip_crun_build = b.option(bool, "skip_crun_build", "Skip crun build") orelse false;
const dockerc_version = b.option([]const u8, "dockerc_version", "Set dockerc version") orelse "HEAD";
const build_info = b.addOptions();
build_info.addOption([]const u8, "dockerc_version", dockerc_version);
const zstd = b.createModule(.{});
zstd.addAssemblyFile(b.path("zstd/lib/decompress/huf_decompress_amd64.S"));
zstd.addCSourceFiles(.{
.files = &[_][]const u8{
"zstd/lib/common/debug.c",
"zstd/lib/common/entropy_common.c",
"zstd/lib/common/error_private.c",
"zstd/lib/common/fse_decompress.c",
"zstd/lib/common/pool.c",
"zstd/lib/common/threading.c",
"zstd/lib/common/xxhash.c",
"zstd/lib/common/zstd_common.c",
"zstd/lib/compress/fse_compress.c",
"zstd/lib/compress/hist.c",
"zstd/lib/compress/huf_compress.c",
"zstd/lib/compress/zstd_compress.c",
"zstd/lib/compress/zstd_compress_literals.c",
"zstd/lib/compress/zstd_compress_sequences.c",
"zstd/lib/compress/zstd_compress_superblock.c",
"zstd/lib/compress/zstd_double_fast.c",
"zstd/lib/compress/zstd_fast.c",
"zstd/lib/compress/zstd_lazy.c",
"zstd/lib/compress/zstd_ldm.c",
"zstd/lib/compress/zstdmt_compress.c",
"zstd/lib/compress/zstd_opt.c",
"zstd/lib/decompress/huf_decompress.c",
"zstd/lib/decompress/zstd_ddict.c",
"zstd/lib/decompress/zstd_decompress_block.c",
"zstd/lib/decompress/zstd_decompress.c",
},
});
const fuse_fss = b.createModule(.{});
fuse_fss.addIncludePath(b.path("zstd/lib"));
fuse_fss.addIncludePath(b.path("libfuse/include"));
fuse_fss.addIncludePath(b.path("libfuse_include"));
fuse_fss.addCSourceFiles(.{
.files = &[_][]const u8{
"libfuse/lib/fuse_opt.c",
"libfuse/lib/helper.c",
"libfuse/lib/fuse_log.c",
"libfuse/lib/fuse_lowlevel.c",
"libfuse/lib/mount_util.c",
"libfuse/lib/fuse.c",
"libfuse/lib/fuse_signals.c",
"libfuse/lib/fuse_loop_mt.c",
"libfuse/lib/buffer.c",
"libfuse/lib/mount.c",
"libfuse/lib/fuse_loop.c",
"libfuse/lib/modules/subdir.c",
"libfuse/lib/modules/iconv.c",
"libfuse/lib/cuse_lowlevel.c",
},
.flags = &[_][]const u8{
// TODO: figure out where to get this value from
"-DFUSE_USE_VERSION=317",
// TODO: make sure this is correct value as well, or maybe we're supposed to dynamically link
"-DFUSERMOUNT_DIR=\"/usr/local/bin\"",
},
});
fuse_fss.addIncludePath(b.path("fuse-overlayfs"));
fuse_fss.addIncludePath(b.path("fuse-overlayfs/lib"));
fuse_fss.addCSourceFiles(.{
.files = &[_][]const u8{
"fuse-overlayfs/main.c",
"fuse-overlayfs/lib/hash.c",
"fuse-overlayfs/lib/bitrotate.c",
"fuse-overlayfs/utils.c",
"fuse-overlayfs/plugin-manager.c",
"fuse-overlayfs/direct.c",
},
.flags = &[_][]const u8{
"-Dmain=overlayfs_main",
// collision with libcrun
"-Dsafe_openat=overlayfs_safe_openat",
"-DPKGLIBEXECDIR=\"\"",
"-Wno-format",
"-Wno-switch",
},
});
fuse_fss.addCSourceFiles(.{
.files = &[_][]const u8{
"squashfuse/ll_main.c",
"squashfuse/ll.c",
"squashfuse/ll_inode.c",
"squashfuse/fs.c",
"squashfuse/fuseprivate.c",
"squashfuse/stat.c",
"squashfuse/dir.c",
"squashfuse/file.c",
"squashfuse/xattr.c",
"squashfuse/nonstd-enoattr.c",
"squashfuse/nonstd-makedev.c",
"squashfuse/util.c",
"squashfuse/nonstd-daemon.c",
"squashfuse/nonstd-pread.c",
"squashfuse/swap.c",
"squashfuse/table.c",
"squashfuse/cache_mt.c",
"squashfuse/decompress.c",
"squashfuse/nonstd-stat.c",
},
.flags = &[_][]const u8{
"-Dmain=squashfuse_main",
"-D_FILE_OFFSET_BITS=64",
},
});
const clap = b.dependency("clap", .{
.optimize = optimize,
.target = target,
});
const cc = std.fmt.allocPrint(
b.allocator,
"{s} cc --target={s}",
.{
b.graph.zig_exe,
target.result.zigTriple(b.allocator) catch @panic("OOM"),
},
) catch @panic("OOM");
const squashfuse_autogen = b.addSystemCommand(&[_][]const u8{
"./autogen.sh",
});
squashfuse_autogen.setCwd(b.path("squashfuse"));
const squashfuse_configure = b.addSystemCommand(&[_][]const u8{
"./configure",
"--without-zlib",
"--without-xz",
"--without-lzo",
"--without-lz4",
"--with-zstd",
});
squashfuse_configure.setCwd(b.path("squashfuse"));
squashfuse_configure.step.dependOn(&squashfuse_autogen.step);
const squashfuse_make_generate_swap = b.addSystemCommand(&[_][]const u8{
"make",
"swap.h.inc",
"swap.c.inc",
});
squashfuse_make_generate_swap.setCwd(b.path("squashfuse"));
squashfuse_make_generate_swap.step.dependOn(&squashfuse_configure.step);
const overlayfs_autogen = b.addSystemCommand(&[_][]const u8{
"./autogen.sh",
});
overlayfs_autogen.setCwd(b.path("fuse-overlayfs"));
const overlayfs_configure = b.addSystemCommand(&[_][]const u8{
"./configure",
});
overlayfs_configure.setCwd(b.path("fuse-overlayfs"));
overlayfs_configure.step.dependOn(&overlayfs_autogen.step);
const runtime = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.link_libc = true,
});
runtime.addOptions("build_info", build_info);
runtime.addImport("zstd", zstd);
runtime.addImport("fuse-overlayfs", fuse_fss);
runtime.addIncludePath(b.path("crun_include"));
runtime.addIncludePath(b.path("crun"));
runtime.addIncludePath(b.path("crun/src"));
runtime.addIncludePath(b.path("crun/libocispec/src"));
runtime.addIncludePath(b.path("argp-standalone"));
runtime.addCSourceFiles(.{
.files = &[_][]const u8{
"crun/src/libcrun/container.c",
"crun/src/libcrun/status.c",
"crun/src/libcrun/linux.c",
"crun/src/libcrun/utils.c",
"crun/src/libcrun/cgroup-utils.c",
"crun/src/libcrun/cgroup.c",
"crun/src/libcrun/intelrdt.c",
"crun/src/libcrun/cgroup-resources.c",
"crun/src/libcrun/ebpf.c",
"crun/src/libcrun/cgroup-cgroupfs.c",
"crun/src/libcrun/chroot_realpath.c",
"crun/src/libcrun/cloned_binary.c",
"crun/src/libcrun/custom-handler.c",
"crun/src/libcrun/terminal.c",
"crun/src/libcrun/cgroup-systemd.c",
"crun/src/libcrun/error.c",
"crun/src/libcrun/mount_flags.c",
"crun/src/libcrun/seccomp.c",
"crun/src/libcrun/seccomp_notify.c",
"crun/src/libcrun/scheduler.c",
"crun/src/libcrun/io_priority.c",
"crun/src/libcrun/cgroup-setup.c",
"crun/src/libcrun/signals.c",
"crun/src/libcrun/blake3/blake3.c",
"crun/src/libcrun/blake3/blake3_portable.c",
"crun/libocispec/src/ocispec/read-file.c",
"crun/libocispec/src/ocispec/json_common.c",
"crun/libocispec/src/ocispec/runtime_spec_schema_config_schema.c",
"crun/libocispec/src/ocispec/runtime_spec_schema_config_zos.c",
"crun/libocispec/src/ocispec/runtime_spec_schema_config_vm.c",
"crun/libocispec/src/ocispec/runtime_spec_schema_config_windows.c",
"crun/libocispec/src/ocispec/runtime_spec_schema_config_solaris.c",
"crun/libocispec/src/ocispec/runtime_spec_schema_config_linux.c",
"crun/libocispec/src/ocispec/runtime_spec_schema_defs.c",
"crun/libocispec/src/ocispec/runtime_spec_schema_defs_linux.c",
"crun/libocispec/src/ocispec/runtime_spec_schema_defs_windows.c",
"crun/libocispec/src/ocispec/runtime_spec_schema_defs_zos.c",
"crun/libocispec/yajl/src/yajl.c",
"crun/libocispec/yajl/src/yajl_gen.c",
"crun/libocispec/yajl/src/yajl_buf.c",
"crun/libocispec/yajl/src/yajl_alloc.c",
"crun/libocispec/yajl/src/yajl_encode.c",
"crun/libocispec/yajl/src/yajl_tree.c",
"crun/libocispec/yajl/src/yajl_parser.c",
"crun/libocispec/yajl/src/yajl_lex.c",
},
});
runtime.addIncludePath(b.dependency("shadow", .{}).path("libsubid"));
runtime.addIncludePath(b.dependency("shadow", .{}).path("lib"));
runtime.addIncludePath(b.dependency("shadow", .{}).path(""));
runtime.addCSourceFiles(.{
.root = b.dependency("shadow", .{}).path(""),
.files = &[_][]const u8{
"libsubid/api.c",
"lib/shadowlog.c",
"lib/subordinateio.c",
"lib/commonio.c",
"lib/write_full.c",
"lib/nss.c",
"lib/get_pid.c",
"lib/memzero.c",
"lib/alloc.c",
"lib/atoi/str2i.c",
"lib/atoi/a2i.c",
"lib/atoi/strtou_noneg.c",
"lib/atoi/strtoi.c",
"lib/string/sprintf.c",
},
.flags = &[_][]const u8{
"-DENABLE_SUBIDS",
// duplicate symbol with crun
"-Dxasprintf=shadow_xasprintf",
},
});
const aarch64_target = b.resolveTargetQuery(.{
.cpu_arch = .aarch64,
.abi = target.result.abi,
.os_tag = .linux,
});
const x86_64_target = b.resolveTargetQuery(.{
.cpu_arch = .x86_64,
.abi = target.result.abi,
.os_tag = .linux,
});
const runtime_x86_64 = b.addExecutable(.{
.name = "runtime_x86-64",
.target = x86_64_target,
.optimize = optimize,
});
const runtime_aarch64 = b.addExecutable(.{
.name = "runtime_aarch64",
.target = aarch64_target,
// FIXME: When compiled with ReleaseSafe reading files in the overlayfs
// will give EINVAL (Invalid Argument)
.optimize = .Debug,
});
runtime_x86_64.root_module.addImport("runtime", runtime);
runtime_aarch64.root_module.addImport("runtime", runtime);
const crun_autogen = b.addSystemCommand(&[_][]const u8{
"./autogen.sh",
});
crun_autogen.setCwd(b.path("crun"));
const crun_configure = b.addSystemCommand(&[_][]const u8{
"./configure",
"--enable-embedded-yajl",
"--disable-systemd",
"--disable-caps",
"--disable-seccomp",
});
crun_configure.setEnvironmentVariable(
"CC",
cc,
);
crun_configure.setCwd(b.path("crun"));
crun_configure.step.dependOn(&crun_autogen.step);
const libocspec_generate_files = b.addSystemCommand(&[_][]const u8{
"make",
"src/ocispec/runtime_spec_schema_config_schema.c",
"src/ocispec/runtime_spec_schema_config_zos.c",
"src/ocispec/runtime_spec_schema_config_vm.c",
"src/ocispec/runtime_spec_schema_config_windows.c",
"src/ocispec/runtime_spec_schema_config_solaris.c",
"src/ocispec/runtime_spec_schema_config_linux.c",
"src/ocispec/runtime_spec_schema_defs.c",
"src/ocispec/runtime_spec_schema_defs_linux.c",
"src/ocispec/runtime_spec_schema_defs_windows.c",
"src/ocispec/runtime_spec_schema_defs_zos.c",
});
libocspec_generate_files.setCwd(b.path("crun/libocispec"));
libocspec_generate_files.step.dependOn(&crun_configure.step);
if (!skip_crun_build) {
runtime_x86_64.step.dependOn(&squashfuse_make_generate_swap.step);
runtime_x86_64.step.dependOn(&overlayfs_configure.step);
runtime_x86_64.step.dependOn(&libocspec_generate_files.step);
runtime_aarch64.step.dependOn(&squashfuse_make_generate_swap.step);
runtime_aarch64.step.dependOn(&overlayfs_configure.step);
runtime_aarch64.step.dependOn(&libocspec_generate_files.step);
}
const go_cpu_arch = switch (target.query.cpu_arch orelse target.result.cpu.arch) {
.x86_64 => "amd64",
.aarch64 => "arm64",
else => @panic("unimplemented"),
};
const umoci = b.addSystemCommand(&[_][]const u8{
"go",
"build",
"-tags",
"",
"-ldflags",
"-s -extldflags '-static'",
"-o",
});
umoci.setCwd(b.path("umoci"));
const umoci_output = umoci.addOutputFileArg(
std.fmt.allocPrint(
b.allocator,
"umoci_{s}",
.{go_cpu_arch},
) catch @panic("OOM"),
);
umoci.addArg("github.com/opencontainers/umoci/cmd/umoci");
umoci.setEnvironmentVariable(
"CGO_ENABLED",
"0",
);
umoci.setEnvironmentVariable("GOARCH", go_cpu_arch);
const skopeo = b.addSystemCommand(&[_][]const u8{
"go",
"build",
"-gcflags",
"",
"-tags",
"containers_image_openpgp",
"-o",
});
skopeo.setCwd(b.path("skopeo"));
const skopeo_output = skopeo.addOutputFileArg(
std.fmt.allocPrint(
b.allocator,
"skopeo_{s}",
.{go_cpu_arch},
) catch @panic("OOM"),
);
skopeo.addArg("./cmd/skopeo");
skopeo.setEnvironmentVariable(
"CGO_ENABLED",
"0",
);
skopeo.setEnvironmentVariable("GOARCH", go_cpu_arch);
skopeo.setEnvironmentVariable("DISABLE_DOCS", "1");
const dockerc = b.addExecutable(.{
.name = "dockerc",
.root_source_file = b.path("src/dockerc.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
dockerc.root_module.addOptions("build_info", build_info);
dockerc.addIncludePath(b.path("zstd/lib"));
dockerc.root_module.addImport("zstd", zstd);
dockerc.addCSourceFiles(.{
.files = &[_][]const u8{
"squashfs-tools/squashfs-tools/mksquashfs.c",
"squashfs-tools/squashfs-tools/progressbar.c",
"squashfs-tools/squashfs-tools/caches-queues-lists.c",
"squashfs-tools/squashfs-tools/date.c",
"squashfs-tools/squashfs-tools/pseudo.c",
"squashfs-tools/squashfs-tools/action.c",
"squashfs-tools/squashfs-tools/sort.c",
"squashfs-tools/squashfs-tools/restore.c",
"squashfs-tools/squashfs-tools/info.c",
"squashfs-tools/squashfs-tools/mksquashfs_help.c",
"squashfs-tools/squashfs-tools/print_pager.c",
"squashfs-tools/squashfs-tools/compressor.c",
"squashfs-tools/squashfs-tools/tar.c",
"squashfs-tools/squashfs-tools/reader.c",
"squashfs-tools/squashfs-tools/read_fs.c",
"squashfs-tools/squashfs-tools/memory.c",
"squashfs-tools/squashfs-tools/process_fragments.c",
"squashfs-tools/squashfs-tools/zstd_wrapper.c",
},
.flags = &[_][]const u8{
// avoid collision of main function
"-Dmain=mksquashfs_main",
"-DZSTD_SUPPORT",
"-D_GNU_SOURCE",
"-DVERSION=\"dockerc\"",
"-DDATE=\"2024/07/21\"",
"-DYEAR=\"2024\"",
"-DCOMP_DEFAULT=\"zstd\"",
"-DCOMPRESSORS=\"zstd\"",
// There's UB in squashfs. This deals with it.
"-fno-sanitize=undefined",
},
});
dockerc.root_module.addAnonymousImport(
"runtime_x86_64",
.{ .root_source_file = runtime_x86_64.getEmittedBin() },
);
dockerc.root_module.addAnonymousImport(
"runtime_aarch64",
.{ .root_source_file = runtime_aarch64.getEmittedBin() },
);
dockerc.root_module.addAnonymousImport(
"umoci",
.{ .root_source_file = umoci_output },
);
dockerc.root_module.addAnonymousImport(
"skopeo",
.{ .root_source_file = skopeo_output },
);
dockerc.root_module.addImport("clap", clap.module("clap"));
b.installArtifact(dockerc);
const replace_bin = b.addExecutable(.{
.name = "replace",
.root_source_file = b.path("src/replace.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
replace_bin.root_module.addAnonymousImport(
"runtime_x86_64",
.{ .root_source_file = runtime_x86_64.getEmittedBin() },
);
replace_bin.root_module.addAnonymousImport(
"runtime_aarch64",
.{ .root_source_file = runtime_aarch64.getEmittedBin() },
);
b.installArtifact(replace_bin);
const extract_bin = b.addExecutable(.{
.name = "extract",
.root_source_file = b.path("src/extract_squashfs.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
b.installArtifact(extract_bin);
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
// b.installArtifact(exe);
// This *creates* a Run step in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
// const run_cmd = b.addRunArtifact(exe);
// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
// run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
// if (b.args) |args| {
// run_cmd.addArgs(args);
// }
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
// const run_step = b.step("run", "Run the app");
// run_step.dependOn(&run_cmd.step);
}