-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
104 lines (85 loc) · 3.07 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
const std = @import("std");
pub fn build(b: *std.Build) void {
if (comptime !checkVersion())
@compileError("Please! Update zig toolchain >= 0.13!");
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// add library
const lib = b.addStaticLibrary(.{
.name = "ocispec",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = b.path("src/lib.zig"),
.target = target,
.optimize = optimize,
.single_threaded = false,
});
b.installArtifact(lib);
const oci_spec_module = b.addModule("ocispec", .{
.root_source_file = b.path("src/lib.zig"),
});
// step generate docs
const install_docs = b.addInstallDirectory(.{
.source_dir = lib.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "docs",
});
const docs_step = b.step("docs", "Install docs into zig-out/docs");
docs_step.dependOn(&install_docs.step);
// build examples
for ([_][]const u8{
"image_config",
"image_oci_layout",
"image_manifest",
"image_index",
"artifact_manifest",
"runtime_config",
}) |example_name| {
const example = b.addExecutable(.{
.name = example_name,
.root_source_file = b.path(b.fmt("examples/{s}.zig", .{example_name})),
.target = target,
.optimize = optimize,
});
example.root_module.addImport("ocispec", oci_spec_module);
b.installArtifact(example);
}
// step run tests
const lib_unit_tests = b.addTest(.{
// Assuming this needs to be the same root file as the library,
// since it's the library we're building tests for?
.root_source_file = b.path("tests/lib.zig"),
.target = target,
.optimize = optimize,
});
lib_unit_tests.root_module.addImport("ocispec", oci_spec_module);
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
// step generate code cov
const cov_step = b.step("cov", "Generate code coverage");
const cov_run = b.addSystemCommand(&.{ "kcov", "--clean", "--include-pattern=src/", ".coverage/" });
cov_run.addArtifactArg(lib_unit_tests);
cov_step.dependOn(&cov_run.step);
// step check formatting
const fmt_step = b.step("fmt", "Check formatting");
const fmt = b.addFmt(.{
.paths = &.{
"src/",
"build.zig",
"build.zig.zon",
},
.check = true,
});
fmt_step.dependOn(&fmt.step);
}
fn checkVersion() bool {
const builtin = @import("builtin");
if (!@hasDecl(builtin, "zig_version")) {
return false;
}
const needed_version = std.SemanticVersion.parse("0.14.0") catch unreachable;
const version = builtin.zig_version;
const order = version.order(needed_version);
return order != .lt;
}