From 26aad8d1aedfdece35f51c52f933fb06da8754d3 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Thu, 15 Dec 2022 03:33:43 -0400 Subject: [PATCH] feat: reconfigure zba-gdbstub as a library --- build.zig | 63 ++++++++++++++++++++++++++++++++++------------------ src/main.zig | 6 +---- 2 files changed, 43 insertions(+), 26 deletions(-) diff --git a/build.zig b/build.zig index c51a434..461252e 100644 --- a/build.zig +++ b/build.zig @@ -1,20 +1,50 @@ const std = @import("std"); -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 - // means any target is allowed, and the default is native. Other options - // for restricting supported target set are available. - const target = b.standardTargetOptions(.{}); +fn path(comptime suffix: []const u8) []const u8 { + if (suffix[0] == '/') @compileError("expected a relative path"); + return comptime (std.fs.path.dirname(@src().file) orelse ".") ++ std.fs.path.sep_str ++ suffix; +} - // Standard release options allow the person running `zig build` to select - // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. - const mode = b.standardReleaseOptions(); +const pkgs = struct { + const Pkg = std.build.Pkg; - const exe = b.addExecutable("zba-gdbstub", "src/main.zig"); + pub const gdbstub: Pkg = .{ + .name = "gdbstub", + .source = .{ .path = path("src/lib.zig") }, + .dependencies = &[_]Pkg{network}, + }; // https://github.com/MasterQ32/zig-network - exe.addPackagePath("network", "lib/zig-network/network.zig"); + pub const network: Pkg = .{ + .name = "network", + .source = .{ .path = path("lib/zig-network/network.zig") }, + }; +}; + +pub fn link(exe: *std.build.LibExeObjStep) void { + exe.addPackage(pkgs.gdbstub); +} + +pub fn build(b: *std.build.Builder) void { + const target = b.standardTargetOptions(.{}); + const mode = b.standardReleaseOptions(); + + // -- library -- + const lib = b.addStaticLibrary("gdbstub", "src/lib.zig"); + lib.addPackage(pkgs.network); + + lib.setBuildMode(mode); + lib.install(); + + const lib_tests = b.addTest("src/lib.zig"); + lib_tests.setBuildMode(mode); + + const test_step = b.step("lib-test", "Run Library Tests"); + test_step.dependOn(&lib_tests.step); + + // -- Executable -- + const exe = b.addExecutable("gdbserver", "src/main.zig"); + link(exe); exe.setTarget(target); exe.setBuildMode(mode); @@ -22,17 +52,8 @@ pub fn build(b: *std.build.Builder) void { const run_cmd = exe.run(); run_cmd.step.dependOn(b.getInstallStep()); - if (b.args) |args| { - run_cmd.addArgs(args); - } + if (b.args) |args| run_cmd.addArgs(args); const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); - - const exe_tests = b.addTest("src/main.zig"); - exe_tests.setTarget(target); - exe_tests.setBuildMode(mode); - - const test_step = b.step("test", "Run unit tests"); - test_step.dependOn(&exe_tests.step); } diff --git a/src/main.zig b/src/main.zig index 3737dbf..5087e3a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,9 +1,5 @@ const std = @import("std"); -const network = @import("network"); -const Server = @import("Server.zig"); - -const Allocator = std.mem.Allocator; -const Socket = network.Socket; +const Server = @import("gdbstub").Server; pub fn main() !void { const log = std.log.scoped(.Main);