-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.zig
92 lines (81 loc) · 3.44 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
const std = @import("std");
const Build = std.Build;
const OptimizeMode = std.builtin.OptimizeMode;
const ResolvedTarget = Build.ResolvedTarget;
const Dependency = Build.Dependency;
const sokol = @import("sokol");
pub fn build(b: *Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// note that the sokol dependency is built with `.with_sokol_imgui = true`
const dep_sokol = b.dependency("sokol", .{
.target = target,
.optimize = optimize,
.with_sokol_imgui = true,
});
const dep_cimgui = b.dependency("cimgui", .{
.target = target,
.optimize = optimize,
});
// inject the cimgui header search path into the sokol C library compile step
dep_sokol.artifact("sokol_clib").addIncludePath(dep_cimgui.path("src"));
// main module with sokol and cimgui imports
const mod_main = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "sokol", .module = dep_sokol.module("sokol") },
.{ .name = "cimgui", .module = dep_cimgui.module("cimgui") },
},
});
// from here on different handling for native vs wasm builds
if (target.result.isWasm()) {
try buildWasm(b, mod_main, dep_sokol, dep_cimgui);
} else {
try buildNative(b, mod_main);
}
}
fn buildNative(b: *Build, mod: *Build.Module) !void {
const exe = b.addExecutable(.{
.name = "demo",
.root_module = mod,
});
b.installArtifact(exe);
b.step("run", "Run demo").dependOn(&b.addRunArtifact(exe).step);
}
fn buildWasm(b: *Build, mod: *Build.Module, dep_sokol: *Dependency, dep_cimgui: *Dependency) !void {
// build the main file into a library, this is because the WASM 'exe'
// needs to be linked in a separate build step with the Emscripten linker
const demo = b.addStaticLibrary(.{
.name = "demo",
.root_module = mod,
});
// get the Emscripten SDK dependency from the sokol dependency
const dep_emsdk = dep_sokol.builder.dependency("emsdk", .{});
// need to inject the Emscripten system header include path into
// the cimgui C library otherwise the C/C++ code won't find
// C stdlib headers
const emsdk_incl_path = dep_emsdk.path("upstream/emscripten/cache/sysroot/include");
dep_cimgui.artifact("cimgui_clib").addSystemIncludePath(emsdk_incl_path);
// all C libraries need to depend on the sokol library, when building for
// WASM this makes sure that the Emscripten SDK has been setup before
// C compilation is attempted (since the sokol C library depends on the
// Emscripten SDK setup step)
dep_cimgui.artifact("cimgui_clib").step.dependOn(&dep_sokol.artifact("sokol_clib").step);
// create a build step which invokes the Emscripten linker
const link_step = try sokol.emLinkStep(b, .{
.lib_main = demo,
.target = mod.resolved_target.?,
.optimize = mod.optimize.?,
.emsdk = dep_emsdk,
.use_webgl2 = true,
.use_emmalloc = true,
.use_filesystem = false,
.shell_file_path = dep_sokol.path("src/sokol/web/shell.html"),
});
// ...and a special run step to start the web build output via 'emrun'
const run = sokol.emRunStep(b, .{ .name = "demo", .emsdk = dep_emsdk });
run.step.dependOn(&link_step.step);
b.step("run", "Run demo").dependOn(&run.step);
}