-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
147 lines (128 loc) · 4.93 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
const std = @import("std");
const zemscripten = @import("zemscripten");
const Options = struct {
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
};
pub fn build(b: *std.Build) !void {
const options = Options{
.target = b.standardTargetOptions(.{}),
.optimize = b.standardOptimizeOption(.{}),
};
if (options.target.result.isWasm()) {
try buildWasm(b, options);
} else {
try buildNative(b, options);
}
}
fn buildNative(b: *std.Build, options: Options) !void {
const exe = b.addExecutable(.{
.name = "zig-pacman",
.root_source_file = b.path("src/main.zig"),
.target = options.target,
.optimize = options.optimize,
.link_libc = true,
});
b.installArtifact(exe);
// Add dependencies to the executable.
addDependencies(b, exe, options);
// Run executable.
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);
// Declare executable tests.
const exe_unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = options.target,
.optimize = options.optimize,
});
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
// Run tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_exe_unit_tests.step);
}
fn buildWasm(b: *std.Build, options: Options) !void {
const install_dir = "web";
const emsdk_dep = b.dependency("emsdk", .{});
const wasm = b.addStaticLibrary(.{
.name = "index",
.root_source_file = b.path("src/main.zig"),
.target = options.target,
.optimize = options.optimize,
});
// When compiling for WASM, make sure to set `sysroot` before raylib
// dependency is loaded.
b.sysroot = zemscripten.emsdkPath(emsdk_dep, "");
// Add dependencies to wasm library.
addDependencies(b, wasm, options);
const zemscripten_dep = b.dependency("zemscripten", .{});
wasm.root_module.addImport("zemscripten", zemscripten_dep.module("root"));
const shell_file_path: ?[]const u8 = if (options.optimize == .Debug)
null
else
zemscripten.minimalShellFilePath(zemscripten_dep);
var emcc_settings = zemscripten.emccDefaultSettings(b.allocator, .{ .optimize = options.optimize });
// Raylib requires some of the following settings to be set in order to
// successfully compile for WASM.
try emcc_settings.put("ALLOW_MEMORY_GROWTH", "1");
try emcc_settings.put("FULL-ES3", "1");
try emcc_settings.put("USE_GLFW", "3");
try emcc_settings.put("ASYNCIFY", "1");
var emcc_step = zemscripten.emccStep(
b,
emsdk_dep,
wasm,
.{
.optimize = options.optimize,
.flags = zemscripten.emccDefaultFlags(b.allocator, options.optimize),
.settings = emcc_settings,
.use_preload_plugins = true,
.embed_paths = &.{},
.preload_paths = &.{},
.install_dir = .{ .custom = install_dir },
.shell_file_path = shell_file_path,
},
);
emcc_step.dependOn(zemscripten.activateEmsdkStep(b, emsdk_dep, "3.1.70"));
b.getInstallStep().dependOn(emcc_step);
const emrun_step = zemscripten.emrunStep(
b,
emsdk_dep,
b.getInstallPath(.{ .custom = install_dir }, "index.html"),
&.{},
);
emrun_step.dependOn(emcc_step);
b.step("emrun", "Build and open the web app locally using emrun").dependOn(emrun_step);
}
fn addDependencies(
b: *std.Build,
artifact: *std.Build.Step.Compile,
options: Options,
) void {
// Dependencies
const zalgebra_dep = b.dependency("zalgebra", options);
const zalgebra_mod = zalgebra_dep.module("zalgebra");
const entt_dep = b.dependency("entt", options);
const raylib_dep = b.dependency("raylib-zig", options);
const raylib_mod = raylib_dep.module("raylib");
// Define internal modules.
const math_mod = b.createModule(.{ .root_source_file = b.path("src/math/main.zig") });
math_mod.addImport("zalgebra", zalgebra_mod);
const graphics_mod = b.createModule(.{ .root_source_file = b.path("src/graphics/main.zig") });
graphics_mod.addImport("math", math_mod);
graphics_mod.addImport("raylib", raylib_mod);
// Add dependencies as imports.
// HACK: Add `zalgebra` module to executable explicitly for zls to provide
// code completion for zalgebra.
artifact.root_module.addImport("zalgebra", zalgebra_mod);
artifact.root_module.addImport("entt", entt_dep.module("zig-ecs"));
artifact.root_module.addImport("raylib", raylib_mod);
artifact.root_module.addImport("math", math_mod);
artifact.root_module.addImport("graphics", graphics_mod);
// Link libraries.
artifact.linkLibrary(raylib_dep.artifact("raylib"));
}