diff --git a/build.zig b/build.zig index 3beb330..040dcb3 100644 --- a/build.zig +++ b/build.zig @@ -1,67 +1,101 @@ const std = @import("std"); +const Build = std.Build; +const Step = Build.Step; +const Run = Step.Run; -pub fn build(b: *std.Build) !void { +const builtin = @import("builtin"); + +const name = "connect6"; + +pub fn build(b: *Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const mod = b.addModule("connect6", .{ - .root_source_file = b.path("src/root.zig"), + const raylib_dependency = b.dependency("raylib_zig", .{ .target = target, + .optimize = optimize, }); + const raylib_module = raylib_dependency.module("raylib"); + const raylib_artifact = raylib_dependency.artifact("raylib"); - const exe = b.addExecutable(.{ - .name = "connect6", - .root_module = b.createModule(.{ - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, - .imports = &.{ - .{ .name = "connect6", .module = mod }, - }, - }), + const root_module = b.createModule(.{ + .root_source_file = b.path("src/root.zig"), + .target = target, + .optimize = optimize, }); - - const raylib_dep = b.dependency("raylib_zig", .{ + const main_module = b.createModule(.{ + .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, + .imports = &.{ + .{ .name = name, .module = root_module }, + .{ .name = "raylib", .module = raylib_module }, + }, }); - if (b.graph.needed_lazy_dependencies.count() != 0) { - return error.LazyDependencyNeeded; + const run_step = b.step("run", "Run"); + + if (target.query.os_tag == .emscripten) { + const rlz = @import("raylib_zig"); + const emsdk = rlz.emsdk; + + const wasm_library = b.addLibrary(.{ + .name = name, + .root_module = main_module, + }); + + const emcc_step = try emsdk.emccStep( + b, + &.{}, + &.{ raylib_artifact, wasm_library }, + .{ + .optimize = optimize, + .flags = emsdk.emccDefaultFlags(b.allocator, .{ .optimize = optimize }), + .settings = emsdk.emccDefaultSettings(b.allocator, .{ .optimize = optimize }), + .shell_file_path = raylib_dependency.builder.dependency("raylib", .{}).path("src/shell.html"), + .install_dir = .{ .custom = "web" }, + .preload_paths = &.{.{ .src_path = b.path("resources"), .virtual_path = "resources" }}, + .out_file_name = "index.html", + }, + ); + + if (builtin.target.os.tag == .windows) { + for (emcc_step.dependencies.items) |dependency| { + const run = dependency.cast(Run) orelse continue; + run.argv.items[0].lazy_path.lazy_path = try emsdk.path(b, "upstream/emscripten/emcc.bat"); + } + } + b.getInstallStep().dependOn(emcc_step); + + const emrun_step = try emsdk.emrunStep( + b, + b.graph.path(.install_prefix, "web/index.html"), + &.{}, + ); + emrun_step.dependOn(emcc_step); + + run_step.dependOn(emrun_step); + } else { + const exe = b.addExecutable(.{ + .name = name, + .root_module = main_module, + }); + b.installArtifact(exe); + + const run_cmd = b.addRunArtifact(exe); + run_cmd.step.dependOn(b.getInstallStep()); + run_cmd.addPassthruArgs(); + + run_step.dependOn(&run_cmd.step); + + const root_test = b.addTest(.{ .root_module = root_module }); + const test_root = b.addRunArtifact(root_test); + + const main_test = b.addTest(.{ .root_module = main_module }); + const test_main = b.addRunArtifact(main_test); + + const test_step = b.step("test", "Test"); + test_step.dependOn(&test_root.step); + test_step.dependOn(&test_main.step); } - - const raylib = raylib_dep.module("raylib"); - const raygui = raylib_dep.module("raygui"); - const raylib_artifact = raylib_dep.artifact("raylib"); - - exe.root_module.linkLibrary(raylib_artifact); - exe.root_module.addImport("raylib", raylib); - exe.root_module.addImport("raygui", raygui); - - 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()); - - run_cmd.addPassthruArgs(); - - const mod_tests = b.addTest(.{ - .root_module = mod, - }); - - const run_mod_tests = b.addRunArtifact(mod_tests); - - const exe_tests = b.addTest(.{ - .root_module = exe.root_module, - }); - - 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); } diff --git a/build.zig.zon b/build.zig.zon index 5ccecfe..e1ced30 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -4,9 +4,13 @@ .fingerprint = 0x90c4b81875a73bf1, .minimum_zig_version = "0.17.0", .dependencies = .{ + .emsdk = .{ + .url = "git+https://github.com/emscripten-core/emsdk#4.0.9", + .hash = "N-V-__8AAJl1DwBezhYo_VE6f53mPVm00R-Fk28NPW7P14EQ", + }, .raylib_zig = .{ - .url = "git+https://github.com/raylib-zig/raylib-zig?ref=devel#50d450099da664bedd7dcc6ed40204ab0bb861a5", - .hash = "raylib_zig-6.0.0-KE8REDSmBQAbyUv_Fh71wgvhQSP2zsmVxtPRLzCgIXWK", + .url = "git+https://github.com/raylib-zig/raylib-zig?ref=devel#f6de93c03197260df941b36ddf3b8dab12bcd393", + .hash = "raylib_zig-6.0.0-KE8REN2mBQC2nQqk9ILHtZgMfQ-3tZpKCvyhmyUPra_3", }, }, .paths = .{ diff --git a/resources/.gitignore b/resources/.gitignore new file mode 100644 index 0000000..e69de29