From 6467fc25e7142f88003b0b201728ec8c39402908 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Thu, 15 Dec 2022 03:33:43 -0400 Subject: [PATCH] feat: update build.zig zba-gdbstub now supports being used as a library --- build.zig | 60 +++++++++++++++++++++++++++++++++------------------- src/main.zig | 6 +----- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/build.zig b/build.zig index c51a434..7281e23 100644 --- a/build.zig +++ b/build.zig @@ -1,20 +1,45 @@ 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(.{}); +const pkgs = struct { + const Pkg = std.build.Pkg; - // Standard release options allow the person running `zig build` to select - // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. - const mode = b.standardReleaseOptions(); - - const exe = b.addExecutable("zba-gdbstub", "src/main.zig"); + pub const gdbstub: Pkg = .{ + .name = "gdbstub", + .source = .{ .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 = "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 +47,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);