Something went wrong. Try again.
Something went wrong. Try again.
factorio-toolbox build.zig
5.5 kB · 179 lines
Zig
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180const 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, }, });}