const std = @import("std"); const Build = std.Build; const sokol = @import("sokol"); const cimgui = @import("cimgui"); const Options = struct { build_gui: bool, 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 build_gui_opt = b.option(bool, "build-gui", "When disabled only build FT library and not the gui app") orelse true; 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, }); 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, }); const mod_exe = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .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 options_mod_exe = b.addOptions(); options_mod_exe.addOption(bool, "docking", opt_docking); mod_exe.addOptions("build_options", options_mod_exe); const opts = Options{ .build_gui = build_gui_opt, .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 buildWebGui(b, opts); } else { if (opts.build_gui) { try buildNativeGui(b, opts); } else { buildFTLib(b, opts); } tests(b, opts); } } fn buildFTLib(b: *Build, opts: Options) void { const lib = b.addLibrary(.{ .name = "factorio-toolbox", .root_module = opts.mod_ft, .linkage = if (b.option(bool, "ft-lib-dynamic", "Set linkage option for FT lib to dynamic") orelse false) .dynamic else .static, }); b.installArtifact(lib); } fn buildNativeGui(b: *Build, opts: Options) !void { const exe = b.addExecutable(.{ .name = "factorio_toolbox", .root_module = opts.mod_exe, }); b.installArtifact(exe); const run_step = b.step("run", "Run the app"); const run_cmd = b.addRunArtifact(exe); run_step.dependOn(&run_cmd.step); run_cmd.step.dependOn(b.getInstallStep()); if (b.args) |args| { run_cmd.addArgs(args); } } fn buildWebGui(b: *Build, opts: Options) !void { const lib = b.addLibrary(.{ .name = "factorio_toolbox", .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_exe.resolved_target.?, .optimize = opts.mod_exe.optimize.?, .emsdk = emsdk, .use_webgl2 = true, .use_emmalloc = true, .use_filesystem = false, .shell_file_path = opts.dep_sokol.path("src/sokol/web/shell.html"), }); b.getInstallStep().dependOn(&link_step.step); const run = sokol.emRunStep(b, .{ .name = "factorio_toolbox", .emsdk = emsdk, }); run.step.dependOn(&link_step.step); b.step("run", "Run the web app").dependOn(&run.step); } fn tests(b: *Build, opts: Options) void { const test_step = b.step("test", "Run tests"); const mod_tests = b.addTest(.{ .name = "mod tests", .root_module = opts.mod_ft, }); const run_mod_tests = b.addRunArtifact(mod_tests); test_step.dependOn(&run_mod_tests.step); if (opts.build_gui) { const exe_tests = b.addTest(.{ .name = "exe tests", .root_module = opts.mod_exe, }); const run_exe_tests = b.addRunArtifact(exe_tests); test_step.dependOn(&run_exe_tests.step); } } 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/shader.glsl", .output = "shader.zig", .slang = .{ .glsl410 = true, .glsl300es = true, .hlsl4 = true, .metal_macos = true, .wgsl = true, }, }); }