From 5500e1f4c58b1df8c971d4a55adbba2b00d21240 Mon Sep 17 00:00:00 2001 From: Altagos Date: Sun, 26 Oct 2025 11:06:05 +0100 Subject: [PATCH] build: setup imgui --- .tangled/workflows/build.yml | 2 +- build.zig | 88 ++++++++++++++++++++++++++---------- build.zig.zon | 4 ++ 3 files changed, 68 insertions(+), 26 deletions(-) diff --git a/.tangled/workflows/build.yml b/.tangled/workflows/build.yml index ed9ceaf..27ad1f6 100644 --- a/.tangled/workflows/build.yml +++ b/.tangled/workflows/build.yml @@ -18,4 +18,4 @@ steps: mise exec zig -- zig build test --summary all - name: Build ReleaseSafe command: | - mise exec zig -- zig build -Doptimize=ReleaseSafe \ No newline at end of file + mise exec zig -- zig build -Doptimize=ReleaseSafe -Ddocking \ No newline at end of file diff --git a/build.zig b/build.zig index f64a007..6d059c1 100644 --- a/build.zig +++ b/build.zig @@ -1,22 +1,38 @@ const std = @import("std"); const Build = std.Build; + const sokol = @import("sokol"); +const cimgui = @import("cimgui"); const Options = struct { - mod: *Build.Module, + mod_ft: *Build.Module, + mod_exe: *Build.Module, dep_sokol: *Build.Dependency, + dep_cimgui: *Build.Dependency, + cimgui_clib_name: []const u8, }; pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + const opt_docking = b.option(bool, "docking", "Build with docking support") orelse false; + + const cimgui_conf = cimgui.getConfig(opt_docking); + const dep_sokol = b.dependency("sokol", .{ .target = target, .optimize = optimize, + .with_sokol_imgui = true, + }); + const dep_cimgui = b.dependency("cimgui", .{ + .target = target, + .optimize = optimize, }); - const mod = b.addModule("ft", .{ + dep_sokol.artifact("sokol_clib").addIncludePath(dep_cimgui.path(cimgui_conf.include_dir)); + + const mod_ft = b.addModule("ft", .{ .root_source_file = b.path("src/root.zig"), .target = target, }); @@ -26,40 +42,41 @@ pub fn build(b: *std.Build) !void { .target = target, .optimize = optimize, .imports = &.{ - .{ .name = "ft", .module = mod }, - .{ .name = "sokol", .module = dep_sokol.module("sokol") }, + .{ .name = "ft", .module = mod_ft }, + .{ + .name = "sokol", + .module = dep_sokol.module("sokol"), + }, + .{ + .name = cimgui_conf.module_name, + .module = dep_cimgui.module(cimgui_conf.module_name), + }, // .{ .name = "shader", .module = try createShaderModule(b, dep_sokol) }, }, }); - const opts = Options{ .mod = mod_exe, .dep_sokol = dep_sokol }; + const options_mod_exe = b.addOptions(); + options_mod_exe.addOption(bool, "docking", opt_docking); + mod_exe.addOptions("build_options", options_mod_exe); + + const opts = Options{ + .mod_ft = mod_ft, + .mod_exe = mod_exe, + .dep_sokol = dep_sokol, + .dep_cimgui = dep_cimgui, + .cimgui_clib_name = cimgui_conf.clib_name, + }; if (target.result.cpu.arch.isWasm()) { try buildWeb(b, opts); } else { try buildNative(b, opts); - - const mod_tests = b.addTest(.{ - .name = "mod tests", - .root_module = mod, - }); - const run_mod_tests = b.addRunArtifact(mod_tests); - - const exe_tests = b.addTest(.{ - .name = "exe tests", - .root_module = mod_exe, - }); - const run_exe_tests = b.addRunArtifact(exe_tests); - - const test_step = b.step("test", "Run tests"); - test_step.dependOn(&run_mod_tests.step); - test_step.dependOn(&run_exe_tests.step); } } fn buildNative(b: *Build, opts: Options) !void { const exe = b.addExecutable(.{ .name = "factorio_toolbox", - .root_module = opts.mod, + .root_module = opts.mod_exe, }); b.installArtifact(exe); @@ -73,19 +90,40 @@ fn buildNative(b: *Build, opts: Options) !void { if (b.args) |args| { run_cmd.addArgs(args); } + + const mod_tests = b.addTest(.{ + .name = "mod tests", + .root_module = opts.mod_ft, + }); + const run_mod_tests = b.addRunArtifact(mod_tests); + + const exe_tests = b.addTest(.{ + .name = "exe tests", + .root_module = opts.mod_exe, + }); + const run_exe_tests = b.addRunArtifact(exe_tests); + + const test_step = b.step("test", "Run tests"); + test_step.dependOn(&run_mod_tests.step); + test_step.dependOn(&run_exe_tests.step); } fn buildWeb(b: *Build, opts: Options) !void { const lib = b.addLibrary(.{ .name = "factorio_toolbox", - .root_module = opts.mod, + .root_module = opts.mod_exe, }); const emsdk = opts.dep_sokol.builder.dependency("emsdk", .{}); + + const emsdk_incl_path = emsdk.path("upstream/emscripten/cache/sysroot/include"); + opts.dep_cimgui.artifact(opts.cimgui_clib_name).addSystemIncludePath(emsdk_incl_path); + opts.dep_cimgui.artifact(opts.cimgui_clib_name).step.dependOn(&opts.dep_sokol.artifact("sokol_clib").step); + const link_step = try sokol.emLinkStep(b, .{ .lib_main = lib, - .target = opts.mod.resolved_target.?, - .optimize = opts.mod.optimize.?, + .target = opts.mod_exe.resolved_target.?, + .optimize = opts.mod_exe.optimize.?, .emsdk = emsdk, .use_webgl2 = true, .use_emmalloc = true, diff --git a/build.zig.zon b/build.zig.zon index 5021a4b..e76a097 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -8,6 +8,10 @@ .url = "git+https://github.com/floooh/sokol-zig.git#76d4afd25adfae9666d76a0d324cdb70ad178a88", .hash = "sokol-0.1.0-pb1HK3TYLgCx8lOxdLVWIVDcAzacxO8YDfyw2Er-_2nN", }, + .cimgui = .{ + .url = "git+https://github.com/floooh/dcimgui.git#d5fb4e3d27b79062dc5981db3631dadc4f204654", + .hash = "cimgui-0.1.0-44Clkd6YlAAYULKHDwsDX9EPmka-VAVEjUl-o6ve307E", + }, }, .paths = .{ "build.zig", -- 2.51.2