-
Notifications
You must be signed in to change notification settings - Fork 48
/
build.zig
409 lines (382 loc) · 14.4 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
const std = @import("std");
const builtin = @import("builtin");
const Build = std.Build;
const CompileStep = Build.Step.Compile;
const RunStep = Build.Step.Run;
const Module = Build.Module;
const CrossTarget = Build.ResolvedTarget;
const OptimizeMode = std.builtin.OptimizeMode;
pub const Backend = enum {
auto, // Windows: D3D11, macOS/iOS: Metal, otherwise: GL
d3d11,
metal,
gl,
gles3,
wgpu,
};
pub const LibSokolOptions = struct {
target: CrossTarget,
optimize: OptimizeMode,
build_root: ?[]const u8 = null,
backend: Backend = .auto,
force_egl: bool = false,
enable_x11: bool = true,
enable_wayland: bool = false,
sysroot: ?[]const u8 = null,
package: ?*Build.Dependency = null,
fn packagePath(self: LibSokolOptions, b: *Build) ?[]const u8 {
if (self.package) |dep|
return dep.path("").getPath(b);
return null;
}
};
pub fn build(b: *Build) void {
const force_gl = b.option(bool, "gl", "Force GL backend") orelse false;
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const mod_sokol = b.addModule("sokol", .{ .root_source_file = .{ .path = "src/sokol/sokol.zig" } });
const lib_sokol = buildLibSokol(b, .{
.target = target,
.optimize = optimize,
.package = b.dependency("emsdk", .{}),
.backend = if (force_gl) .gl else .auto,
.enable_wayland = b.option(bool, "wayland", "Compile with wayland-support (default: false)") orelse false,
.enable_x11 = b.option(bool, "x11", "Compile with x11-support (default: true)") orelse true,
.force_egl = b.option(bool, "egl", "Use EGL instead of GLX if possible (default: false)") orelse false,
}) catch |err| {
std.log.err("buildLibSokol return with error {}", .{err});
return;
};
const examples = .{
"clear",
"triangle",
"quad",
"bufferoffsets",
"cube",
"noninterleaved",
"texcube",
"blend",
"offscreen",
"instancing",
"mrt",
"saudio",
"sgl",
"sgl-context",
"sgl-points",
"debugtext",
"debugtext-print",
"debugtext-userfont",
"shapes",
};
inline for (examples) |example| {
buildExample(b, example, .{
.target = target,
.optimize = optimize,
.lib_sokol = lib_sokol,
.mod_sokol = mod_sokol,
.package = b.dependency("emsdk", .{}),
});
}
buildShaders(b);
}
const ExampleOptions = struct {
target: CrossTarget,
optimize: OptimizeMode,
lib_sokol: *CompileStep,
mod_sokol: *Module,
package: ?*Build.Dependency = null,
fn packagePath(self: ExampleOptions, b: *Build) ?[]const u8 {
if (self.package) |dep|
return dep.path("").getPath(b);
return null;
}
};
// build sokol into a static library
pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*CompileStep {
const is_wasm = options.target.result.isWasm();
var config = options;
// special case wasm, must compile as wasm32-emscripten, not wasm32-freestanding
var target = options.target;
if (is_wasm) {
target.result.os.tag = .emscripten;
}
const lib = b.addStaticLibrary(.{
.name = "sokol",
.target = target,
.optimize = options.optimize,
.link_libc = true,
});
if (is_wasm) {
// need to add Emscripten SDK include path (system or package)
if (b.sysroot) |sysroot| {
config.sysroot = sysroot;
} else if (options.packagePath(b)) |path| {
config.sysroot = b.pathJoin(&.{ path, "upstream", "emscripten", "cache", "sysroot" });
var cmds = std.ArrayList([]const u8).init(b.allocator);
defer cmds.deinit();
if (lib.rootModuleTarget().os.tag == .windows)
try cmds.append(b.pathJoin(&.{ options.packagePath(b).?, "emsdk.bat" }))
else {
try cmds.append("bash"); // or try chmod
try cmds.append(b.pathJoin(&.{ options.packagePath(b).?, "emsdk" }));
}
var emsdk_run = b.addSystemCommand(cmds.items);
emsdk_run.addArgs(&.{ "install", "latest" });
var emsdk_active = b.addSystemCommand(cmds.items);
emsdk_active.addArgs(&.{ "activate", "latest" });
emsdk_active.step.dependOn(&emsdk_run.step);
lib.step.dependOn(&emsdk_active.step);
} else {
std.log.err("Must provide Emscripten sysroot via '--sysroot [path/to/emsdk]/upstream/emscripten/cache/sysroot'", .{});
return error.Wasm32SysRootExpected;
}
const include_path = b.pathJoin(&.{ config.sysroot.?, "include" });
lib.addSystemIncludePath(.{ .path = include_path }); // isystem
// TODO: zig get emscripten libs (wgu, libGL-webgl2,...)
const lib_path = b.pathJoin(&.{ config.sysroot.?, "lib", "wasm32-emscripten" });
lib.addLibraryPath(.{ .path = lib_path });
lib.defineCMacro("__EMSCRIPTEN__", "1");
}
var sokol_path: []const u8 = "src/sokol/c";
if (options.build_root) |build_root| {
sokol_path = b.fmt("{s}/src/sokol/c", .{build_root});
}
const csources = [_][]const u8{
"sokol_log.c",
"sokol_app.c",
"sokol_gfx.c",
"sokol_time.c",
"sokol_audio.c",
"sokol_gl.c",
"sokol_debugtext.c",
"sokol_shape.c",
};
var _backend = options.backend;
if (_backend == .auto) {
if (lib.rootModuleTarget().isDarwin()) {
_backend = .metal;
} else if (lib.rootModuleTarget().os.tag == .windows) {
_backend = .d3d11;
} else if (lib.rootModuleTarget().isWasm()) {
_backend = .gles3;
} else if (lib.rootModuleTarget().abi == .android) {
_backend = .gles3;
} else {
_backend = .gl;
}
}
const backend_option = switch (_backend) {
.d3d11 => "-DSOKOL_D3D11",
.metal => "-DSOKOL_METAL",
.gl => "-DSOKOL_GLCORE33",
.gles3 => "-DSOKOL_GLES3",
.wgpu => "-DSOKOL_WGPU",
else => unreachable,
};
if (lib.rootModuleTarget().isDarwin()) {
for (csources) |csrc| {
lib.addCSourceFile(.{
.file = .{ .path = b.fmt("{s}/{s}", .{ sokol_path, csrc }) },
.flags = &[_][]const u8{ "-ObjC", "-DIMPL", backend_option },
});
}
lib.linkFramework("Foundation");
lib.linkFramework("AudioToolbox");
if (.metal == _backend) {
lib.linkFramework("MetalKit");
lib.linkFramework("Metal");
}
if (lib.rootModuleTarget().os.tag == .ios) {
lib.linkFramework("UIKit");
lib.linkFramework("AVFoundation");
if (.gl == _backend) {
lib.linkFramework("OpenGLES");
lib.linkFramework("GLKit");
}
} else if (lib.rootModuleTarget().os.tag == .macos) {
lib.linkFramework("Cocoa");
lib.linkFramework("QuartzCore");
if (.gl == _backend) {
lib.linkFramework("OpenGL");
}
}
} else if (lib.rootModuleTarget().abi == .android) {
if (.gles3 != _backend) {
@panic("For android targets, you must have backend set to GLES3");
}
for (csources) |csrc| {
lib.addCSourceFile(.{
.file = .{ .path = b.fmt("{s}/{s}", .{ sokol_path, csrc }) },
.flags = &[_][]const u8{ "-DIMPL", backend_option },
});
}
lib.linkSystemLibrary("GLESv3");
lib.linkSystemLibrary("EGL");
lib.linkSystemLibrary("android");
lib.linkSystemLibrary("log");
} else if (lib.rootModuleTarget().os.tag == .linux) {
const egl_flag = if (options.force_egl) "-DSOKOL_FORCE_EGL " else "";
const x11_flag = if (!options.enable_x11) "-DSOKOL_DISABLE_X11 " else "";
const wayland_flag = if (!options.enable_wayland) "-DSOKOL_DISABLE_WAYLAND" else "";
const link_egl = options.force_egl or options.enable_wayland;
for (csources) |csrc| {
lib.addCSourceFile(.{
.file = .{ .path = b.fmt("{s}/{s}", .{ sokol_path, csrc }) },
.flags = &[_][]const u8{ "-DIMPL", backend_option, egl_flag, x11_flag, wayland_flag },
});
}
lib.linkSystemLibrary("asound");
lib.linkSystemLibrary("GL");
if (options.enable_x11) {
lib.linkSystemLibrary("X11");
lib.linkSystemLibrary("Xi");
lib.linkSystemLibrary("Xcursor");
}
if (options.enable_wayland) {
lib.linkSystemLibrary("wayland-client");
lib.linkSystemLibrary("wayland-cursor");
lib.linkSystemLibrary("wayland-egl");
lib.linkSystemLibrary("xkbcommon");
}
if (link_egl) {
lib.linkSystemLibrary("egl");
}
} else if (lib.rootModuleTarget().os.tag == .windows) {
for (csources) |csrc| {
lib.addCSourceFile(.{
.file = .{ .path = b.fmt("{s}/{s}", .{ sokol_path, csrc }) },
.flags = &[_][]const u8{ "-DIMPL", backend_option },
});
}
lib.linkSystemLibrary("kernel32");
lib.linkSystemLibrary("user32");
lib.linkSystemLibrary("gdi32");
lib.linkSystemLibrary("ole32");
if (.d3d11 == _backend) {
lib.linkSystemLibrary("d3d11");
lib.linkSystemLibrary("dxgi");
}
} else {
for (csources) |csrc| {
lib.addCSourceFile(.{
.file = .{ .path = b.fmt("{s}/{s}", .{ sokol_path, csrc }) },
.flags = &[_][]const u8{ "-DIMPL", backend_option },
});
}
}
return lib;
}
// build one of the example exes
fn buildExample(b: *Build, comptime name: []const u8, options: ExampleOptions) void {
const e = if (options.target.result.isWasm()) b.addStaticLibrary(.{
.name = name,
.root_source_file = .{ .path = "src/examples/" ++ name ++ ".zig" },
.target = options.target,
.optimize = options.optimize,
}) else b.addExecutable(.{
.name = name,
.root_source_file = .{ .path = "src/examples/" ++ name ++ ".zig" },
.target = options.target,
.optimize = options.optimize,
});
e.linkLibrary(options.lib_sokol);
e.root_module.addImport("sokol", options.mod_sokol);
var run: ?*RunStep = null;
if (e.rootModuleTarget().isWasm()) {
run = buildWasm(b, e, options) catch |err| @panic(@errorName(err));
} else {
b.installArtifact(e);
run = b.addRunArtifact(e);
}
b.step("run-" ++ name, "Run " ++ name).dependOn(&run.?.step);
}
// a separate step to compile shaders, expects the shader compiler in ../sokol-tools-bin/
fn buildShaders(b: *Build) void {
const sokol_tools_bin_dir = "../sokol-tools-bin/bin/";
const shaders_dir = "src/examples/shaders/";
const shaders = .{
"bufferoffsets.glsl",
"cube.glsl",
"instancing.glsl",
"mrt.glsl",
"noninterleaved.glsl",
"offscreen.glsl",
"quad.glsl",
"shapes.glsl",
"texcube.glsl",
"blend.glsl",
};
const optional_shdc: ?[:0]const u8 = comptime switch (builtin.os.tag) {
.windows => "win32/sokol-shdc.exe",
.linux => "linux/sokol-shdc",
.macos => if (builtin.cpu.arch.isX86()) "osx/sokol-shdc" else "osx_arm64/sokol-shdc",
else => null,
};
if (optional_shdc == null) {
std.log.warn("unsupported host platform, skipping shader compiler step", .{});
return;
}
const shdc_path = sokol_tools_bin_dir ++ optional_shdc.?;
const shdc_step = b.step("shaders", "Compile shaders (needs ../sokol-tools-bin)");
inline for (shaders) |shader| {
const cmd = b.addSystemCommand(&.{
shdc_path,
"-i",
shaders_dir ++ shader,
"-o",
shaders_dir ++ shader ++ ".zig",
"-l",
"glsl330:metal_macos:hlsl4",
"-f",
"sokol_zig",
});
shdc_step.dependOn(&cmd.step);
}
}
fn buildWasm(b: *Build, example: *CompileStep, options: ExampleOptions) !*RunStep {
// system path or package path
const emcc_path = b.findProgram(&.{"emcc"}, &.{}) catch b.pathJoin(&.{ options.packagePath(b).?, "upstream", "emscripten", "emcc" });
const emrun_path = b.findProgram(&.{"emrun"}, &.{}) catch b.pathJoin(&.{ options.packagePath(b).?, "upstream", "emscripten", "emrun" });
if (options.lib_sokol.rootModuleTarget().os.tag != .emscripten) {
std.log.err("Please build with 'zig build -Dtarget=wasm32-emscripten", .{});
return error.Wasm32EmscriptenExpected;
}
// call the emcc linker step as a 'system command' zig build step which
// depends on the libsokol and libgame build steps
try std.fs.cwd().makePath(b.fmt("{s}/web", .{b.install_path}));
var emcc_cmds = std.ArrayList([]const u8).init(b.allocator);
defer emcc_cmds.deinit();
try emcc_cmds.append(emcc_path);
if (options.optimize != .Debug)
try emcc_cmds.append("-Oz")
else
try emcc_cmds.append("-Og");
try emcc_cmds.append("--closure");
try emcc_cmds.append("1");
try emcc_cmds.append(b.fmt("-o{s}/web/{s}.html", .{ b.install_path, example.name }));
try emcc_cmds.append("-sNO_FILESYSTEM=1");
try emcc_cmds.append("-sMALLOC='emmalloc'");
try emcc_cmds.append("-sASSERTIONS=0");
try emcc_cmds.append("-sERROR_ON_UNDEFINED_SYMBOLS=0");
// TODO: fix undefined references
// switch (options.backend) {
// .wgpu => {
// try emcc_cmds.append("-sUSE_WEBGPU=1");
// },
// else => {
try emcc_cmds.append("-sUSE_WEBGL2=1");
// },
// }
try emcc_cmds.append("-sEXPORTED_FUNCTIONS=['_malloc','_free','_main']");
const emcc = b.addSystemCommand(emcc_cmds.items);
emcc.setName("emcc"); // hide emcc path
// get artifacts from zig-cache, no need zig-out
emcc.addArtifactArg(options.lib_sokol);
emcc.addArtifactArg(example);
// get the emcc step to run on 'zig build'
b.getInstallStep().dependOn(&emcc.step);
// a seperate run step using emrun
const emrun = b.addSystemCommand(&.{ emrun_path, b.fmt("{s}/web/{s}.html", .{ b.install_path, example.name }) });
emrun.step.dependOn(&emcc.step);
return emrun;
}