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");
9 pub fn build(b: *Build) !void {
10 const target = b.standardTargetOptions(.{});
11 const optimize = b.standardOptimizeOption(.{});
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);
17 // note that the sokol dependency is built with `.with_sokol_imgui = true`
18 const dep_sokol = b.dependency("sokol", .{
21 .with_sokol_imgui = true,
23 const dep_cimgui = b.dependency("cimgui", .{
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));
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, .{
36 .input = "shd/quad.glsl",
37 .output = "src/shd/quad.glsl.zig",
38 .slang = .{ .glsl430 = true },
41 // main module with sokol and cimgui imports
42 const mod_main = b.createModule(.{
43 .root_source_file = b.path("src/main.zig"),
47 .{ .name = "sokol", .module = dep_sokol.module("sokol") },
48 .{ .name = cimgui_conf.module_name, .module = dep_cimgui.module(cimgui_conf.module_name) },
53 mod_main.addIncludePath(b.path("src"));
54 mod_main.addCSourceFile(.{.file = b.path("src/stb_image.c")});
56 // from here on different handling for native vs wasm builds
57 if (target.result.cpu.arch.isWasm()) {
60 .dep_sokol = dep_sokol,
61 .dep_cimgui = dep_cimgui,
62 .cimgui_clib_name = cimgui_conf.clib_name,
65 const exe = try buildNative(b, mod_main);
66 exe.step.dependOn(shdc_step);
70 fn buildNative(b: *Build, mod: *Build.Module) !*Build.Step.Compile {
71 const exe = b.addExecutable(.{
75 b.installArtifact(exe);
76 b.step("run", "Run demo").dependOn(&b.addRunArtifact(exe).step);
80 const BuildWasmOptions = struct {
81 mod_main: *Build.Module,
82 dep_sokol: *Dependency,
83 dep_cimgui: *Dependency,
84 cimgui_clib_name: []const u8,
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(.{
92 .root_module = opts.mod_main,
95 // get the Emscripten SDK dependency from the sokol dependency
96 const dep_emsdk = opts.dep_sokol.builder.dependency("emsdk", .{});
98 // need to inject the Emscripten system header include path into
99 // the cimgui C library otherwise the C/C++ code won't find
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);
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);
110 // create a build step which invokes the Emscripten linker
111 const link_step = try sokol.emLinkStep(b, .{
113 .target = opts.mod_main.resolved_target.?,
114 .optimize = opts.mod_main.optimize.?,
117 .use_emmalloc = true,
118 .use_filesystem = false,
119 .shell_file_path = opts.dep_sokol.path("src/sokol/web/shell.html"),
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);