-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathbuild.zig
More file actions
120 lines (102 loc) · 4.04 KB
/
build.zig
File metadata and controls
120 lines (102 loc) · 4.04 KB
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
const std = @import("std");
const builtin = @import("builtin");
pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
const ecs_module = b.addModule("zig-ecs", .{
.root_source_file = b.path("src/ecs.zig"),
.optimize = optimize,
.target = target,
});
const examples = [_][2][]const u8{
[_][]const u8{ "view_vs_group", "examples/view_vs_group.zig" },
[_][]const u8{ "group_sort", "examples/group_sort.zig" },
[_][]const u8{ "simple", "examples/simple.zig" },
[_][]const u8{ "empty", "examples/empty.zig" },
};
for (examples, 0..) |example, i| {
const name = if (i == 0) "ecs" else example[0];
const source = example[1];
const example_module = b.createModule(.{
.root_source_file = b.path(source),
.optimize = optimize,
.target = target,
});
const exe = b.addExecutable(.{
.name = name,
.root_module = example_module,
});
exe.root_module.addImport("ecs", ecs_module);
exe.root_module.link_libc = true;
const docs = exe;
const doc = b.step(b.fmt("{s}-docs", .{name}), "Generate documentation");
doc.dependOn(&docs.step);
const run_cmd = b.addRunArtifact(exe);
b.installArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
const exe_step = b.step(name, b.fmt("run {s}.zig", .{name}));
exe_step.dependOn(&run_cmd.step);
// first element in the list is added as "run" so "zig build run" works
if (i == 0) {
const run_exe_step = b.step("run", b.fmt("run {s}.zig", .{name}));
run_exe_step.dependOn(&run_cmd.step);
}
}
// internal tests
const internal_test_mod = b.addModule("internal_tests", .{
.root_source_file = b.path("src/tests.zig"),
.target = target,
.optimize = optimize,
});
const internal_test = b.addTest(.{
.root_module = internal_test_mod,
});
b.installArtifact(internal_test);
// public api tests
const public_test_mod = b.addModule("public_tests", .{
.root_source_file = b.path("tests/tests.zig"),
.optimize = optimize,
.target = target,
});
public_test_mod.addImport("ecs", ecs_module);
const public_test = b.addTest(.{
.root_module = public_test_mod,
});
b.installArtifact(public_test);
const test_cmd = b.step("test", "Run the tests");
test_cmd.dependOn(b.getInstallStep());
test_cmd.dependOn(&b.addRunArtifact(internal_test).step);
test_cmd.dependOn(&b.addRunArtifact(public_test).step);
}
pub const LibType = enum(i32) {
static,
dynamic, // requires DYLD_LIBRARY_PATH to point to the dylib path
exe_compiled,
};
pub fn getModule(b: *std.Build, comptime prefix_path: []const u8) *std.Build.Module {
return b.addModule(.{
.root_source_file = b.path(prefix_path ++ "src/ecs.zig"),
.target = b.standardTargetOptions(.{}),
.optimize = b.standardOptimizeOption(.{}),
.name = "ecs",
});
}
/// prefix_path is used to add package paths. It should be the the same path used to include this build file
pub fn linkArtifact(b: *std.Build, artifact: *std.Build.Step.Compile, lib_type: LibType, comptime prefix_path: []const u8) void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
switch (lib_type) {
.static => {
const lib = b.addStaticLibrary(.{ .name = "ecs", .root_source_file = "ecs.zig", .optimize = optimize, .target = target });
b.installArtifact(lib);
artifact.linkLibrary(lib);
},
.dynamic => {
const lib = b.addSharedLibrary(.{ .name = "ecs", .root_source_file = "ecs.zig", .optimize = optimize, .target = target });
b.installArtifact(lib);
artifact.linkLibrary(lib);
},
else => {},
}
artifact.root_module.addImport("ecs", getModule(prefix_path));
}