From ac07c9300fa55e46932fc1208c05fa3bcd01ed2f Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Wed, 30 Jun 2021 09:05:21 -0700 Subject: [PATCH] extract smithy to separate package --- .gitignore | 2 ++ codegen/build.zig | 35 ++++++++++++++++++++++++++++-- codegen/src/main.zig | 2 +- smithy/build.zig | 17 +++++++++++++++ {codegen => smithy}/src/smithy.zig | 4 ++-- 5 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 smithy/build.zig rename {codegen => smithy}/src/smithy.zig (99%) diff --git a/.gitignore b/.gitignore index c77072b..091df22 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ codegen/codegen *.tgz service_manifest.zig demo +src/models/ +smithy/zig-out/ diff --git a/codegen/build.zig b/codegen/build.zig index a413bb8..b899573 100644 --- a/codegen/build.zig +++ b/codegen/build.zig @@ -1,6 +1,6 @@ const std = @import("std"); -pub fn build(b: *std.build.Builder) void { +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 @@ -12,9 +12,22 @@ pub fn build(b: *std.build.Builder) void { const mode = b.standardReleaseOptions(); const exe = b.addExecutable("codegen", "src/main.zig"); + exe.addPackagePath("smithy", "../smithy/src/smithy.zig"); exe.setTarget(target); exe.setBuildMode(mode); - exe.override_dest_dir = .{ .Custom = ".." }; + // This line works as of c5d412268 + // Earliest nightly is 05b5e49bc on 2021-06-12 + // https://ziglang.org/builds/zig-linux-x86_64-0.9.0-dev.113+05b5e49bc.tar.xz + // exe.override_dest_dir = .{ .Custom = ".." }; + exe.override_dest_dir = .{ .custom = ".." }; + + // Static linkage flag was nonfunctional until 2b2efa24d0855 + // Did not notice this until 2021-06-28, and that nightly is: + // https://ziglang.org/builds/zig-linux-x86_64-0.9.0-dev.321+15a030ef3.tar.xz + exe.linkage = .static; + + const is_strip = b.option(bool, "strip", "strip exe") orelse true; + exe.strip = !is_strip; exe.install(); const run_cmd = exe.run(); @@ -25,4 +38,22 @@ pub fn build(b: *std.build.Builder) void { const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); + + const test_step = b.step("test", "Run library tests"); + var build_dir = try std.fs.openDirAbsolute(b.build_root, .{}); + defer build_dir.close(); + var src_dir = try build_dir.openDir("src", .{ .iterate = true }); + defer src_dir.close(); + var iterator = src_dir.iterate(); + while (try iterator.next()) |entry| { + if (std.mem.endsWith(u8, entry.name, ".zig") and + !std.mem.eql(u8, entry.name, "main.zig")) + { + const name = try std.fmt.allocPrint(b.allocator, "src/{s}", .{entry.name}); + defer b.allocator.free(name); + const t = b.addTest(name); + t.setBuildMode(mode); + test_step.dependOn(&t.step); + } + } } diff --git a/codegen/src/main.zig b/codegen/src/main.zig index 0de4904..485ca86 100644 --- a/codegen/src/main.zig +++ b/codegen/src/main.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const smithy = @import("smithy.zig"); +const smithy = @import("smithy"); const snake = @import("snake.zig"); pub fn main() anyerror!void { diff --git a/smithy/build.zig b/smithy/build.zig new file mode 100644 index 0000000..dab121d --- /dev/null +++ b/smithy/build.zig @@ -0,0 +1,17 @@ +const std = @import("std"); + +pub fn build(b: *std.build.Builder) void { + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const lib = b.addStaticLibrary("smithy", "src/smithy.zig"); + lib.setBuildMode(mode); + lib.install(); + + var main_tests = b.addTest("src/smithy.zig"); + main_tests.setBuildMode(mode); + + const test_step = b.step("test", "Run library tests"); + test_step.dependOn(&main_tests.step); +} diff --git a/codegen/src/smithy.zig b/smithy/src/smithy.zig similarity index 99% rename from codegen/src/smithy.zig rename to smithy/src/smithy.zig index 7c7005e..e30b69e 100644 --- a/codegen/src/smithy.zig +++ b/smithy/src/smithy.zig @@ -659,7 +659,7 @@ fn read_file_to_string(allocator: *std.mem.Allocator, file_name: []const u8, max var test_data: ?[]const u8 = null; const intrinsic_type_count: usize = 5; // 5 intrinsic types are added to every model -fn getTestData(allocator: *std.mem.Allocator) []const u8 { +fn getTestData(_: *std.mem.Allocator) []const u8 { if (test_data) |d| return d; var gpa = std.heap.GeneralPurposeAllocator(.{}){}; test_data = read_file_to_string(&gpa.allocator, "test.json", 150000) catch @panic("could not read test.json"); @@ -669,7 +669,7 @@ test "read file" { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer if (gpa.deinit()) @panic("leak"); const allocator = &gpa.allocator; - const test_string = getTestData(allocator); + _ = getTestData(allocator); // test stuff } test "parse string" {