From 5164d94669ef4f54ffccc1350fad8151e1dfec04 Mon Sep 17 00:00:00 2001 From: zenith391 <39484230+zenith391@users.noreply.github.com> Date: Tue, 18 Apr 2023 19:46:47 +0200 Subject: [PATCH] update to new build api --- build.zig | 103 +++------------------------------------------------- zigmod.lock | 3 +- zigmod.yml | 1 - 3 files changed, 6 insertions(+), 101 deletions(-) diff --git a/build.zig b/build.zig index 2d4cec9..cc9ae8d 100644 --- a/build.zig +++ b/build.zig @@ -1,8 +1,6 @@ const std = @import("std"); const deps = @import("deps.zig"); -const PATH_TO_CAPY = ".zigmod/deps/git/github.com/capy-ui/capy/"; - pub fn build(b: *std.build.Builder) !void { // Standard target options allows the person running `zig build` to choose // what target to build for. Here we do not override the defaults, which @@ -20,17 +18,11 @@ pub fn build(b: *std.build.Builder) !void { .target = target, .optimize = optimize, }); - try deps.imports.capy.install(exe, .{}); exe.install(); - const run_cmd = exe.run(); - run_cmd.step.dependOn(b.getInstallStep()); - if (b.args) |args| { - run_cmd.addArgs(args); - } - + const run_cmd = try deps.imports.capy.install(exe, .{ .args = b.args }); const run_step = b.step("run", "Run the app"); - run_step.dependOn(&run_cmd.step); + run_step.dependOn(run_cmd); // Building for WebAssembly // WebAssembly doesn't have a concept of executables, so the way it works is that we make a shared library and capy exports a '_start' function automatically @@ -41,18 +33,9 @@ pub fn build(b: *std.build.Builder) !void { .target = comptime std.zig.CrossTarget.parse(.{ .arch_os_abi = "wasm32-freestanding" }) catch unreachable, .optimize = optimize, }); - try deps.imports.capy.install(wasm, .{}); - - if (@import("builtin").zig_backend != .stage2_llvm) { - const serve = WebServerStep.create(b, wasm); - serve.step.dependOn(&wasm.step); - const serve_step = b.step("serve", "Start a local web server to run this application"); - serve_step.dependOn(&serve.step); - } else { - var step = b.addLog("Please use 'zig build serve -fstage1'", .{}); - const serve_step = b.step("serve", "Start a local web server to run this application"); - serve_step.dependOn(&step.step); - } + const serve = try deps.imports.capy.install(wasm, .{}); + const serve_step = b.step("serve", "Start a local web server to run this application"); + serve_step.dependOn(serve); const exe_tests = b.addTest(.{ .root_source_file = .{ .path = "src/main.zig" }, @@ -63,79 +46,3 @@ pub fn build(b: *std.build.Builder) !void { const test_step = b.step("test", "Run unit tests"); test_step.dependOn(&exe_tests.step); } - -const http = deps.imports.apple_pie; - -/// Step used for running a web server that can be run with 'zig build serve' -const WebServerStep = struct { - step: std.build.Step, - exe: *std.build.LibExeObjStep, - builder: *std.build.Builder, - - pub fn create(builder: *std.build.Builder, exe: *std.build.LibExeObjStep) *WebServerStep { - const self = builder.allocator.create(WebServerStep) catch unreachable; - self.* = .{ - .step = std.build.Step.init(.custom, "webserver", builder.allocator, WebServerStep.make), - .exe = exe, - .builder = builder, - }; - return self; - } - - const Context = struct { - exe: *std.build.LibExeObjStep, - builder: *std.build.Builder, - }; - - pub fn make(step: *std.build.Step) !void { - const self = @fieldParentPtr(WebServerStep, "step", step); - const allocator = self.builder.allocator; - - var context = Context{ .builder = self.builder, .exe = self.exe }; - const builder = http.router.Builder(*Context); - std.debug.print("Web server opened at http://localhost:8080/\n", .{}); - try http.listenAndServe( - allocator, - try std.net.Address.parseIp("127.0.0.1", 8080), - &context, - comptime http.router.Router(*Context, &.{ - builder.get("/", index), - builder.get("/capy.js", indexJs), - builder.get("/zig-app.wasm", wasmFile), - }), - ); - } - - fn index(context: *Context, response: *http.Response, request: http.Request) !void { - const allocator = request.arena; - const buildRoot = context.builder.build_root; - const file = try std.fs.cwd().openFile(try std.fs.path.join(allocator, &.{ buildRoot, PATH_TO_CAPY ++ "src/backends/wasm/index.html" }), .{}); - defer file.close(); - const text = try file.readToEndAlloc(allocator, std.math.maxInt(usize)); - - try response.headers.put("Content-Type", "text/html"); - try response.writer().writeAll(text); - } - - fn indexJs(context: *Context, response: *http.Response, request: http.Request) !void { - const allocator = request.arena; - const buildRoot = context.builder.build_root; - const file = try std.fs.cwd().openFile(try std.fs.path.join(allocator, &.{ buildRoot, PATH_TO_CAPY ++ "src/backends/wasm/capy.js" }), .{}); - defer file.close(); - const text = try file.readToEndAlloc(allocator, std.math.maxInt(usize)); - - try response.headers.put("Content-Type", "application/javascript"); - try response.writer().writeAll(text); - } - - fn wasmFile(context: *Context, response: *http.Response, request: http.Request) !void { - const allocator = request.arena; - const path = context.exe.getOutputSource().getPath(context.builder); - const file = try std.fs.cwd().openFile(path, .{}); - defer file.close(); - const text = try file.readToEndAlloc(allocator, std.math.maxInt(usize)); - - try response.headers.put("Content-Type", "application/wasm"); - try response.writer().writeAll(text); - } -}; diff --git a/zigmod.lock b/zigmod.lock index 28bf3c7..11c5e53 100644 --- a/zigmod.lock +++ b/zigmod.lock @@ -1,3 +1,2 @@ 2 -git https://github.com/Luukdegram/apple_pie commit-5eaaabdced4f9b8d6cee947b465e7ea16ea61f42 -git https://github.com/capy-ui/capy commit-e1f8ee5ad77e4c9d71eaf8d877535e33346ccdc4 +git https://github.com/capy-ui/capy commit-91efc6f305398d305d56da8ddb0f7a0bd7cfa06e diff --git a/zigmod.yml b/zigmod.yml index 9d32108..fd69d23 100644 --- a/zigmod.yml +++ b/zigmod.yml @@ -4,5 +4,4 @@ license: Unlicense description: Simple template for Capy apps build_dependencies: - src: git https://github.com/capy-ui/capy - - src: git https://github.com/Luukdegram/apple_pie # this is only necessary if you want 'zig build serve' root_depedencies: