-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathbuild.zig
More file actions
94 lines (87 loc) · 3.28 KB
/
Copy pathbuild.zig
File metadata and controls
94 lines (87 loc) · 3.28 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
const std = @import("std");
const Build = std.Build;
const sokol = @import("sokol");
const Options = struct {
mod: *Build.Module,
dep_sokol: *Build.Dependency,
};
pub fn build(b: *Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const opt_use_gles3 = b.option(bool, "gles3", "Force OpenGL ES3 (default: false)") orelse false;
const opt_use_wayland = b.option(bool, "wayland", "Force Wayland (default: false, Linux only, not supported in main-line headers)") orelse false;
const dep_sokol = b.dependency("sokol", .{
.target = target,
.optimize = optimize,
.gles3 = opt_use_gles3,
.wayland = opt_use_wayland,
});
const mod_pacman = b.createModule(.{
.root_source_file = b.path("src/pacman.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "sokol", .module = dep_sokol.module("sokol") },
.{ .name = "shader", .module = try createShaderModule(b, dep_sokol) },
},
});
// special case handling for native vs web build
const opts = Options{ .mod = mod_pacman, .dep_sokol = dep_sokol };
if (target.result.cpu.arch.isWasm()) {
try buildWeb(b, opts);
} else {
try buildNative(b, opts);
}
}
// this is the regular build for all native platforms, nothing surprising here
fn buildNative(b: *Build, opts: Options) !void {
const exe = b.addExecutable(.{
.name = "pacman",
.root_module = opts.mod,
});
b.installArtifact(exe);
const run = b.addRunArtifact(exe);
b.step("run", "Run pacman").dependOn(&run.step);
}
// for web builds, the Zig code needs to be built into a library and linked with the Emscripten linker
fn buildWeb(b: *Build, opts: Options) !void {
const lib = b.addLibrary(.{
.name = "pacman",
.root_module = opts.mod,
});
// create a build step which invokes the Emscripten linker
const emsdk = opts.dep_sokol.builder.dependency("emsdk", .{});
const link_step = try sokol.emLinkStep(b, .{
.lib_main = lib,
.target = opts.mod.resolved_target.?,
.optimize = opts.mod.optimize.?,
.emsdk = emsdk,
.use_webgl2 = true,
.use_emmalloc = true,
.use_filesystem = true,
.shell_file_path = opts.dep_sokol.path("src/sokol/web/shell.html"),
});
// attach Emscripten linker output to default install step
b.getInstallStep().dependOn(&link_step.step);
// ...and a special run step to start the web build output via 'emrun'
const run = sokol.emRunStep(b, .{ .name = "pacman", .emsdk = emsdk });
run.step.dependOn(&link_step.step);
b.step("run", "Run pacman").dependOn(&run.step);
}
// compile shader via sokol-shdc
fn createShaderModule(b: *Build, dep_sokol: *Build.Dependency) !*Build.Module {
const mod_sokol = dep_sokol.module("sokol");
const dep_shdc = dep_sokol.builder.dependency("shdc", .{});
return sokol.shdc.createModule(b, "shader", mod_sokol, .{
.shdc_dep = dep_shdc,
.input = "src/shader.glsl",
.output = "shader.zig",
.slang = .{
.glsl410 = true,
.glsl300es = true,
.hlsl4 = true,
.metal_macos = true,
.wgsl = true,
},
});
}