diff --git a/build.zig b/build.zig index 9cc86c4..f64a007 100644 --- a/build.zig +++ b/build.zig @@ -1,25 +1,67 @@ const std = @import("std"); +const Build = std.Build; +const sokol = @import("sokol"); -pub fn build(b: *std.Build) void { +const Options = struct { + mod: *Build.Module, + dep_sokol: *Build.Dependency, +}; + +pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + const dep_sokol = b.dependency("sokol", .{ + .target = target, + .optimize = optimize, + }); + const mod = 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 }, + .{ .name = "sokol", .module = dep_sokol.module("sokol") }, + // .{ .name = "shader", .module = try createShaderModule(b, dep_sokol) }, + }, + }); + + const opts = Options{ .mod = mod_exe, .dep_sokol = dep_sokol }; + 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 = b.createModule(.{ - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, - .imports = &.{ - .{ .name = "ft", .module = mod }, - }, - }), + .root_module = opts.mod, }); + b.installArtifact(exe); const run_step = b.step("run", "Run the app"); @@ -31,20 +73,48 @@ pub fn build(b: *std.Build) void { if (b.args) |args| { run_cmd.addArgs(args); } +} + +fn buildWeb(b: *Build, opts: Options) !void { + const lib = b.addLibrary(.{ + .name = "factorio_toolbox", + .root_module = opts.mod, + }); - const mod_tests = b.addTest(.{ - .name = "mod tests", - .root_module = mod, + const emsdk = opts.dep_sokol.builder.dependency("emsdk", .{}); + const link_step = try sokol.emLinkStep(b, .{ + .lib_main = lib, + .target = opts.mod.resolved_target.?, + .optimize = opts.mod.optimize.?, + .emsdk = emsdk, + .use_webgl2 = true, + .use_emmalloc = true, + .use_filesystem = false, + .shell_file_path = opts.dep_sokol.path("src/sokol/web/shell.html"), }); - const run_mod_tests = b.addRunArtifact(mod_tests); + b.getInstallStep().dependOn(&link_step.step); - const exe_tests = b.addTest(.{ - .name = "exe tests", - .root_module = exe.root_module, + const run = sokol.emRunStep(b, .{ + .name = "factorio_toolbox", + .emsdk = emsdk, }); - const run_exe_tests = b.addRunArtifact(exe_tests); + run.step.dependOn(&link_step.step); + b.step("run", "Run the web app").dependOn(&run.step); +} - const test_step = b.step("test", "Run tests"); - test_step.dependOn(&run_mod_tests.step); - 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, + }, + }); } diff --git a/build.zig.zon b/build.zig.zon index 5708706..5021a4b 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -3,7 +3,12 @@ .version = "0.0.0", .fingerprint = 0x5506658b7a972311, // Changing this has security and trust implications. .minimum_zig_version = "0.16.0-dev.747+493ad58ff", - .dependencies = .{}, + .dependencies = .{ + .sokol = .{ + .url = "git+https://github.com/floooh/sokol-zig.git#76d4afd25adfae9666d76a0d324cdb70ad178a88", + .hash = "sokol-0.1.0-pb1HK3TYLgCx8lOxdLVWIVDcAzacxO8YDfyw2Er-_2nN", + }, + }, .paths = .{ "build.zig", "build.zig.zon",