Repository navigation
Expand file tree
/
Copy pathbuild.zig
More file actions
150 lines (125 loc) · 4.62 KB
/
Copy pathbuild.zig
File metadata and controls
150 lines (125 loc) · 4.62 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
// SPDX-FileCopyrightText: NONE
// SPDX-License-Identifier: CC0-1.0
const std = @import("std");
const zigglgen = @import("zigglgen");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
var emscripten_system_include_path: ?std.Build.LazyPath = null;
switch (target.result.os.tag) {
.emscripten => {
if (b.sysroot) |sysroot| {
emscripten_system_include_path = .{ .cwd_relative = b.pathJoin(&.{ sysroot, "include" }) };
} else {
std.log.err("'--sysroot' is required when building for Emscripten", .{});
std.process.exit(1);
}
},
else => {},
}
const app_mod = b.createModule(.{
.root_source_file = b.path("main.zig"),
.target = target,
.optimize = optimize,
.link_libc = target.result.os.tag == .emscripten,
});
if (emscripten_system_include_path) |path| {
app_mod.addSystemIncludePath(path);
}
const sdl_dep = b.dependency("sdl", .{
.target = target,
.optimize = optimize,
.lto = optimize != .Debug,
});
const sdl_lib = sdl_dep.artifact("SDL3");
app_mod.linkLibrary(sdl_lib);
app_mod.addImport("gl", zigglgen.generateBindingsModule(b, if (target.result.os.tag == .emscripten) .{
.api = .gles,
.version = .@"3.0", // WebGL 2.0
} else .{
.api = .gl,
.version = .@"4.1", // The last OpenGL version supported on macOS
.profile = .core,
}));
const run = b.step("run", "Run the app");
if (target.result.os.tag == .emscripten) {
// Build for the Web.
const app_lib = b.addLibrary(.{
.linkage = .static,
.name = "opengl_hexagon",
.root_module = app_mod,
});
app_lib.want_lto = optimize != .Debug;
const run_emcc = b.addSystemCommand(&.{"emcc"});
// Pass the full set of linked artifacts as input files.
for (app_lib.getCompileDependencies(false)) |compile| {
run_emcc.addArtifactArg(compile);
}
if (target.result.cpu.arch == .wasm64) {
run_emcc.addArg("-sMEMORY64");
}
run_emcc.addArgs(switch (optimize) {
.Debug => &.{
"-O0",
// Preserve DWARF debug information.
"-g",
// Use UBSan (full runtime).
"-fsanitize=undefined",
},
.ReleaseSafe => &.{
"-O3",
// Use UBSan (minimal runtime).
"-fsanitize=undefined",
"-fsanitize-minimal-runtime",
},
.ReleaseFast => &.{
"-O3",
},
.ReleaseSmall => &.{
"-Oz",
},
});
if (optimize != .Debug) {
// Perform link time optimization.
run_emcc.addArg("-flto");
// Minify JavaScript code.
run_emcc.addArgs(&.{ "--closure", "1" });
}
run_emcc.addArgs(&.{
"-sUSE_OFFSET_CONVERTER", // Required by Zig's '@returnAddress'
"-sLEGACY_RUNTIME", // Currently required by SDL
"-sMIN_WEBGL_VERSION=2",
"-sFULL_ES3", // Currently required by zigglgen
});
// Patch the default HTML shell to also display messages printed to stderr.
run_emcc.addArg("--pre-js");
run_emcc.addFileArg(b.addWriteFiles().add("pre.js", (
\\Module['printErr'] ??= Module['print'];
\\
)));
run_emcc.addArg("-o");
const app_html = run_emcc.addOutputFileArg("opengl_hexagon.html");
b.getInstallStep().dependOn(&b.addInstallDirectory(.{
.source_dir = app_html.dirname(),
.install_dir = .{ .custom = "www" },
.install_subdir = "",
}).step);
const run_emrun = b.addSystemCommand(&.{"emrun"});
run_emrun.addArg(b.pathJoin(&.{ b.install_path, "www", "opengl_hexagon.html" }));
if (b.args) |args| run_emrun.addArgs(args);
run_emrun.step.dependOn(b.getInstallStep());
run.dependOn(&run_emrun.step);
} else {
// Build for desktop.
const app_exe = b.addExecutable(.{
.name = "opengl_hexagon",
.root_module = app_mod,
});
app_exe.want_lto = optimize != .Debug;
b.installArtifact(app_exe);
const run_app = b.addRunArtifact(app_exe);
if (b.args) |args| run_app.addArgs(args);
run_app.step.dependOn(b.getInstallStep());
run.dependOn(&run_app.step);
}
}