this repo has no description

build: setup sokol

altagos.dev 32f1a8e6 ee363b39

verified
+96 -21
+90 -20
build.zig
··· 1 1 const std = @import("std"); 2 + const Build = std.Build; 3 + const sokol = @import("sokol"); 2 4 3 - pub fn build(b: *std.Build) void { 5 + const Options = struct { 6 + mod: *Build.Module, 7 + dep_sokol: *Build.Dependency, 8 + }; 9 + 10 + pub fn build(b: *std.Build) !void { 4 11 const target = b.standardTargetOptions(.{}); 5 12 const optimize = b.standardOptimizeOption(.{}); 13 + 14 + const dep_sokol = b.dependency("sokol", .{ 15 + .target = target, 16 + .optimize = optimize, 17 + }); 6 18 7 19 const mod = b.addModule("ft", .{ 8 20 .root_source_file = b.path("src/root.zig"), 9 21 .target = target, 10 22 }); 11 23 24 + const mod_exe = b.createModule(.{ 25 + .root_source_file = b.path("src/main.zig"), 26 + .target = target, 27 + .optimize = optimize, 28 + .imports = &.{ 29 + .{ .name = "ft", .module = mod }, 30 + .{ .name = "sokol", .module = dep_sokol.module("sokol") }, 31 + // .{ .name = "shader", .module = try createShaderModule(b, dep_sokol) }, 32 + }, 33 + }); 34 + 35 + const opts = Options{ .mod = mod_exe, .dep_sokol = dep_sokol }; 36 + if (target.result.cpu.arch.isWasm()) { 37 + try buildWeb(b, opts); 38 + } else { 39 + try buildNative(b, opts); 40 + 41 + const mod_tests = b.addTest(.{ 42 + .name = "mod tests", 43 + .root_module = mod, 44 + }); 45 + const run_mod_tests = b.addRunArtifact(mod_tests); 46 + 47 + const exe_tests = b.addTest(.{ 48 + .name = "exe tests", 49 + .root_module = mod_exe, 50 + }); 51 + const run_exe_tests = b.addRunArtifact(exe_tests); 52 + 53 + const test_step = b.step("test", "Run tests"); 54 + test_step.dependOn(&run_mod_tests.step); 55 + test_step.dependOn(&run_exe_tests.step); 56 + } 57 + } 58 + 59 + fn buildNative(b: *Build, opts: Options) !void { 12 60 const exe = b.addExecutable(.{ 13 61 .name = "factorio_toolbox", 14 - .root_module = b.createModule(.{ 15 - .root_source_file = b.path("src/main.zig"), 16 - .target = target, 17 - .optimize = optimize, 18 - .imports = &.{ 19 - .{ .name = "ft", .module = mod }, 20 - }, 21 - }), 62 + .root_module = opts.mod, 22 63 }); 64 + 23 65 b.installArtifact(exe); 24 66 25 67 const run_step = b.step("run", "Run the app"); ··· 31 73 if (b.args) |args| { 32 74 run_cmd.addArgs(args); 33 75 } 76 + } 34 77 35 - const mod_tests = b.addTest(.{ 36 - .name = "mod tests", 37 - .root_module = mod, 78 + fn buildWeb(b: *Build, opts: Options) !void { 79 + const lib = b.addLibrary(.{ 80 + .name = "factorio_toolbox", 81 + .root_module = opts.mod, 38 82 }); 39 - const run_mod_tests = b.addRunArtifact(mod_tests); 40 83 41 - const exe_tests = b.addTest(.{ 42 - .name = "exe tests", 43 - .root_module = exe.root_module, 84 + const emsdk = opts.dep_sokol.builder.dependency("emsdk", .{}); 85 + const link_step = try sokol.emLinkStep(b, .{ 86 + .lib_main = lib, 87 + .target = opts.mod.resolved_target.?, 88 + .optimize = opts.mod.optimize.?, 89 + .emsdk = emsdk, 90 + .use_webgl2 = true, 91 + .use_emmalloc = true, 92 + .use_filesystem = false, 93 + .shell_file_path = opts.dep_sokol.path("src/sokol/web/shell.html"), 94 + }); 95 + b.getInstallStep().dependOn(&link_step.step); 96 + 97 + const run = sokol.emRunStep(b, .{ 98 + .name = "factorio_toolbox", 99 + .emsdk = emsdk, 44 100 }); 45 - const run_exe_tests = b.addRunArtifact(exe_tests); 101 + run.step.dependOn(&link_step.step); 102 + b.step("run", "Run the web app").dependOn(&run.step); 103 + } 46 104 47 - const test_step = b.step("test", "Run tests"); 48 - test_step.dependOn(&run_mod_tests.step); 49 - test_step.dependOn(&run_exe_tests.step); 105 + fn createShaderModule(b: *Build, dep_sokol: *Build.Dependency) !*Build.Module { 106 + const mod_sokol = dep_sokol.module("sokol"); 107 + const dep_shdc = dep_sokol.builder.dependency("shdc", .{}); 108 + return sokol.shdc.createModule(b, "shader", mod_sokol, .{ 109 + .shdc_dep = dep_shdc, 110 + .input = "src/shader/shader.glsl", 111 + .output = "shader.zig", 112 + .slang = .{ 113 + .glsl410 = true, 114 + .glsl300es = true, 115 + .hlsl4 = true, 116 + .metal_macos = true, 117 + .wgsl = true, 118 + }, 119 + }); 50 120 }
+6 -1
build.zig.zon
··· 3 3 .version = "0.0.0", 4 4 .fingerprint = 0x5506658b7a972311, // Changing this has security and trust implications. 5 5 .minimum_zig_version = "0.16.0-dev.747+493ad58ff", 6 - .dependencies = .{}, 6 + .dependencies = .{ 7 + .sokol = .{ 8 + .url = "git+https://github.com/floooh/sokol-zig.git#76d4afd25adfae9666d76a0d324cdb70ad178a88", 9 + .hash = "sokol-0.1.0-pb1HK3TYLgCx8lOxdLVWIVDcAzacxO8YDfyw2Er-_2nN", 10 + }, 11 + }, 7 12 .paths = .{ 8 13 "build.zig", 9 14 "build.zig.zon",