-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
189 lines (175 loc) · 6.66 KB
/
build.zig
File metadata and controls
189 lines (175 loc) · 6.66 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
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
const std = @import("std");
const Build = std.Build;
const Module = std.Build.Module;
const Target = std.Build.ResolvedTarget;
const Optimize = std.builtin.OptimizeMode;
const Imports = []const Module.Import;
const zbgfx = @import("zbgfx");
pub fn build(b: *Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{ .preferred_optimize_mode = .Debug });
const thirdparty = buildThirdparty(b, target, optimize);
const runtime = b.addModule("runtime", .{
.link_libc = true,
.link_libcpp = true,
.optimize = optimize,
.target = target,
.root_source_file = b.path("src/root.zig"),
});
runtime.addImport("thirdparty", thirdparty.root_module);
runtime.linkLibrary(thirdparty);
linkSimpleModDep(b, runtime, "entt", "ecs", "zig-ecs");
linkSimpleModDep(b, runtime, "zaudio", "zaudio", "root");
linkSimpleModDep(b, runtime, "slime", "slime", "slime");
try linkBgfx(b, target, optimize, runtime);
const sandbox = buildSandbox(b, target, optimize, &.{.{ .name = "runtime", .module = runtime }});
const mod_tests = b.addTest(.{
.root_module = runtime,
});
const run_mod_tests = b.addRunArtifact(mod_tests);
const exe_tests = b.addTest(.{
.root_module = sandbox.root_module,
});
const run_exe_tests = b.addRunArtifact(exe_tests);
const test_step = b.step("test", "Run tests");
test_step.dependOn(&run_mod_tests.step);
test_step.dependOn(&run_exe_tests.step);
setupExamples(b, target, optimize, &.{.{ .name = "runtime", .module = runtime }});
}
fn setupExamples(b: *std.Build, target: Target, optimize: Optimize, imports: Imports) void {
const Example = struct {
name: []const u8,
path: []const u8,
};
const names: []const Example = &.{
.{ .name = "snake", .path = "examples/snake.zig" },
.{ .name = "basic", .path = "examples/basic.zig" },
.{ .name = "physics", .path = "examples/physics.zig" },
// .{ .name = "ui", .path = "examples/ui.zig" },
};
for (names) |example| {
const exe = b.addExecutable(.{
.name = example.name,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.imports = imports,
.root_source_file = b.path(example.path),
}),
});
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step(example.name, "Run an example");
run_step.dependOn(&run_cmd.step);
}
}
fn buildThirdparty(b: *Build, target: Target, optimize: Optimize) *Build.Step.Compile {
const thirdparty = b.addLibrary(.{ .name = "framework-thirdparty", .root_module = b.createModule(.{
.link_libc = true,
.root_source_file = b.path("thirdparty/root.zig"),
.optimize = optimize,
.target = target,
}) });
thirdparty.addIncludePath(b.path("thirdparty/"));
thirdparty.addCSourceFiles(.{
.files = &.{ "RGFW_Impl.c", "stb_truetype_impl.c", "microui.c", "miniaudio.c" },
.root = b.path("thirdparty/"),
});
switch (target.result.os.tag) {
.windows => {
// RGFW
thirdparty.root_module.linkSystemLibrary("gdi32", .{ .needed = true });
},
.linux => {
// RGFW
thirdparty.root_module.linkSystemLibrary("X11", .{ .needed = true });
thirdparty.root_module.linkSystemLibrary("xrandr", .{ .needed = true });
},
else => {},
}
return thirdparty;
}
fn buildSandbox(b: *Build, target: Target, optimize: Optimize, imports: Imports) *Build.Step.Compile {
const exe = b.addExecutable(.{
.name = "sandbox",
.root_module = b.createModule(.{
.optimize = optimize,
.target = target,
.root_source_file = b.path("sandbox/main.zig"),
.imports = imports,
}),
});
b.installArtifact(exe);
// Add run step
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
return exe;
}
fn linkSimpleModDep(b: *std.Build, module: *std.Build.Module, dep_name: []const u8, name: []const u8, mod_name: []const u8) void {
const dep = b.dependency(dep_name, .{});
const mod = dep.module(mod_name);
module.addImport(name, mod);
}
fn linkSimpleModDepWithArtifact(b: *std.Build, module: *std.Build.Module, dep_name: []const u8, name: []const u8, mod_name: []const u8, artifact: []const u8) void {
const dep = b.dependency(dep_name, .{});
const mod = dep.module(mod_name);
module.addImport(name, mod);
module.linkLibrary(dep.artifact(artifact));
}
fn linkBgfx(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, runtime: *std.Build.Module) !void {
_ = optimize;
const zbgfx_dep = b.dependency("zbgfx", .{});
runtime.addImport("bgfx", zbgfx_dep.module("zbgfx"));
runtime.linkLibrary(zbgfx_dep.artifact("bgfx"));
const install_shaderc_step = try zbgfx.build_step.installShaderc(b, zbgfx_dep);
const shaders_includes = &.{zbgfx_dep.path("shaders")};
const shaders_module = try zbgfx.build_step.compileShaders(
b,
target,
install_shaderc_step,
zbgfx_dep,
shaders_includes,
&.{
.{
.name = "fs_basic",
.shaderType = .fragment,
.path = b.path("src/renderer/shaders/fs_basic.sc"),
},
.{
.name = "vs_basic",
.shaderType = .vertex,
.path = b.path("src/renderer/shaders/vs_basic.sc"),
},
.{
.name = "fs_diffuse",
.shaderType = .fragment,
.path = b.path("src/renderer/shaders/fs_diffuse.sc"),
},
.{
.name = "vs_skybox",
.shaderType = .vertex,
.path = b.path("src/renderer/shaders/skybox/vs_skybox.sc"),
},
.{
.name = "fs_skybox",
.shaderType = .fragment,
.path = b.path("src/renderer/shaders/skybox/fs_skybox.sc"),
},
.{
.name = "vs_diffuse",
.shaderType = .vertex,
.path = b.path("src/renderer/shaders/vs_diffuse.sc"),
},
},
);
runtime.addImport("shader_module", shaders_module);
}