]> gitweb.ps.run Git - sporegirl/blob - build.zig
update main.zig
[sporegirl] / build.zig
1 const std = @import("std");
2 const Build = std.Build;
3 const OptimizeMode = std.builtin.OptimizeMode;
4 const ResolvedTarget = Build.ResolvedTarget;
5 const Dependency = Build.Dependency;
6 const sokol = @import("sokol");
7 const cimgui = @import("cimgui");
8
9 pub fn build(b: *Build) !void {
10     const target = b.standardTargetOptions(.{});
11     const optimize = b.standardOptimizeOption(.{});
12
13     // Get the matching Zig module name, C header search path and C library for
14     // vanilla imgui vs the imgui docking branch.
15     const cimgui_conf = cimgui.getConfig(true);
16
17     // note that the sokol dependency is built with `.with_sokol_imgui = true`
18     const dep_sokol = b.dependency("sokol", .{
19         .target = target,
20         .optimize = optimize,
21         .with_sokol_imgui = true,
22     });
23     const dep_cimgui = b.dependency("cimgui", .{
24         .target = target,
25         .optimize = optimize,
26     });
27
28     // inject the cimgui header search path into the sokol C library compile step
29     dep_sokol.artifact("sokol_clib").addIncludePath(dep_cimgui.path(cimgui_conf.include_dir));
30
31     // shaders
32     dep_sokol.artifact("sokol_clib").addIncludePath(b.path("ext/cimgui"));
33     const dep_shdc = dep_sokol.builder.dependency("shdc", .{});
34     const shdc_step = try sokol.shdc.createSourceFile(b, .{
35         .shdc_dep = dep_shdc,
36         .input = "shd/quad.glsl",
37         .output = "src/shd/quad.glsl.zig",
38         .slang = .{ .glsl430 = true },
39     });
40
41     // main module with sokol and cimgui imports
42     const mod_main = b.createModule(.{
43         .root_source_file = b.path("src/main.zig"),
44         .target = target,
45         .optimize = optimize,
46         .imports = &.{
47             .{ .name = "sokol", .module = dep_sokol.module("sokol") },
48             .{ .name = cimgui_conf.module_name, .module = dep_cimgui.module(cimgui_conf.module_name) },
49         },
50         .link_libc = true,
51     });
52     
53     mod_main.addIncludePath(b.path("src"));
54     mod_main.addCSourceFile(.{.file = b.path("src/stb_image.c")});
55
56     // from here on different handling for native vs wasm builds
57     if (target.result.cpu.arch.isWasm()) {
58         try buildWasm(b, .{
59             .mod_main = mod_main,
60             .dep_sokol = dep_sokol,
61             .dep_cimgui = dep_cimgui,
62             .cimgui_clib_name = cimgui_conf.clib_name,
63         });
64     } else {
65         const exe = try buildNative(b, mod_main);
66         exe.step.dependOn(shdc_step);
67     }
68 }
69
70 fn buildNative(b: *Build, mod: *Build.Module) !*Build.Step.Compile {
71     const exe = b.addExecutable(.{
72         .name = "demo",
73         .root_module = mod,
74     });
75     b.installArtifact(exe);
76     b.step("run", "Run demo").dependOn(&b.addRunArtifact(exe).step);
77     return exe;
78 }
79
80 const BuildWasmOptions = struct {
81     mod_main: *Build.Module,
82     dep_sokol: *Dependency,
83     dep_cimgui: *Dependency,
84     cimgui_clib_name: []const u8,
85 };
86
87 fn buildWasm(b: *Build, opts: BuildWasmOptions) !void {
88     // build the main file into a library, this is because the WASM 'exe'
89     // needs to be linked in a separate build step with the Emscripten linker
90     const demo = b.addLibrary(.{
91         .name = "demo",
92         .root_module = opts.mod_main,
93     });
94
95     // get the Emscripten SDK dependency from the sokol dependency
96     const dep_emsdk = opts.dep_sokol.builder.dependency("emsdk", .{});
97
98     // need to inject the Emscripten system header include path into
99     // the cimgui C library otherwise the C/C++ code won't find
100     // C stdlib headers
101     const emsdk_incl_path = dep_emsdk.path("upstream/emscripten/cache/sysroot/include");
102     opts.dep_cimgui.artifact(opts.cimgui_clib_name).addSystemIncludePath(emsdk_incl_path);
103
104     // all C libraries need to depend on the sokol library, when building for
105     // WASM this makes sure that the Emscripten SDK has been setup before
106     // C compilation is attempted (since the sokol C library depends on the
107     // Emscripten SDK setup step)
108     opts.dep_cimgui.artifact(opts.cimgui_clib_name).step.dependOn(&opts.dep_sokol.artifact("sokol_clib").step);
109
110     // create a build step which invokes the Emscripten linker
111     const link_step = try sokol.emLinkStep(b, .{
112         .lib_main = demo,
113         .target = opts.mod_main.resolved_target.?,
114         .optimize = opts.mod_main.optimize.?,
115         .emsdk = dep_emsdk,
116         .use_webgl2 = true,
117         .use_emmalloc = true,
118         .use_filesystem = false,
119         .shell_file_path = opts.dep_sokol.path("src/sokol/web/shell.html"),
120     });
121     // attach to default target
122     b.getInstallStep().dependOn(&link_step.step);
123     // ...and a special run step to start the web build output via 'emrun'
124     const run = sokol.emRunStep(b, .{ .name = "demo", .emsdk = dep_emsdk });
125     run.step.dependOn(&link_step.step);
126     b.step("run", "Run demo").dependOn(&run.step);
127 }