From 303af8661cb37f7a518e1c08518c8b66908186f3 Mon Sep 17 00:00:00 2001 From: Simon Hartcher Date: Thu, 13 Mar 2025 14:32:03 +1100 Subject: [PATCH 1/3] fix: make modules depend on codegen --- build.zig | 139 +++++++++++++++++++++++-------------------- src/servicemodel.zig | 2 +- 2 files changed, 75 insertions(+), 66 deletions(-) diff --git a/build.zig b/build.zig index 9de9394..8388869 100644 --- a/build.zig +++ b/build.zig @@ -64,17 +64,6 @@ pub fn build(b: *Builder) !void { const smithy_module = smithy_dep.module("smithy"); exe.root_module.addImport("smithy", smithy_module); // not sure this should be here... - // Expose module to others - _ = b.addModule("aws", .{ - .root_source_file = b.path("src/aws.zig"), - .imports = &.{.{ .name = "smithy", .module = smithy_module }}, - }); - - // Expose module to others - _ = b.addModule("aws-signing", .{ - .root_source_file = b.path("src/aws_signing.zig"), - .imports = &.{.{ .name = "smithy", .module = smithy_module }}, - }); // TODO: This does not work correctly due to https://github.com/ziglang/zig/issues/16354 // // We are working here with kind of a weird dependency though. So we can do this @@ -97,61 +86,81 @@ pub fn build(b: *Builder) !void { const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); - const gen_step = blk: { - const cg = b.step("gen", "Generate zig service code from smithy models"); + const cg = b.step("gen", "Generate zig service code from smithy models"); - const cg_exe = b.addExecutable(.{ - .name = "codegen", - .root_source_file = b.path("codegen/src/main.zig"), - // We need this generated for the host, not the real target - .target = b.graph.host, - .optimize = if (b.verbose) .Debug else .ReleaseSafe, - }); - cg_exe.root_module.addImport("smithy", smithy_dep.module("smithy")); - var cg_cmd = b.addRunArtifact(cg_exe); - cg_cmd.addArg("--models"); - const hash = hash_blk: { - for (b.available_deps) |dep| { - const dep_name = dep.@"0"; - const dep_hash = dep.@"1"; - if (std.mem.eql(u8, dep_name, "models")) - break :hash_blk dep_hash; - } - return error.DependencyNamedModelsNotFoundInBuildZigZon; - }; - cg_cmd.addArg(try std.fs.path.join( - b.allocator, - &[_][]const u8{ - b.graph.global_cache_root.path.?, - "p", - hash, - models_subdir, - }, - )); - cg_cmd.addArg("--output"); - cg_cmd.addDirectoryArg(b.path("src/models")); - if (b.verbose) - cg_cmd.addArg("--verbose"); - // cg_cmd.step.dependOn(&fetch_step.step); - // TODO: this should use zig_exe from std.Build - // codegen should store a hash in a comment - // this would be hash of the exe that created the file - // concatenated with hash of input json. this would - // allow skipping generated files. May not include hash - // of contents of output file as maybe we want to tweak - // manually?? - // - // All the hashes can be in service_manifest.zig, which - // could be fun to just parse and go nuts. Top of - // file, generator exe hash. Each import has comment - // with both input and output hash and we can decide - // later about warning on manual changes... - - cg.dependOn(&cg_cmd.step); - break :blk cg; + const cg_exe = b.addExecutable(.{ + .name = "codegen", + .root_source_file = b.path("codegen/src/main.zig"), + // We need this generated for the host, not the real target + .target = b.graph.host, + .optimize = if (b.verbose) .Debug else .ReleaseSafe, + }); + cg_exe.root_module.addImport("smithy", smithy_dep.module("smithy")); + var cg_cmd = b.addRunArtifact(cg_exe); + cg_cmd.addArg("--models"); + const hash = hash_blk: { + for (b.available_deps) |dep| { + const dep_name = dep.@"0"; + const dep_hash = dep.@"1"; + if (std.mem.eql(u8, dep_name, "models")) + break :hash_blk dep_hash; + } + return error.DependencyNamedModelsNotFoundInBuildZigZon; }; + cg_cmd.addArg(try std.fs.path.join( + b.allocator, + &[_][]const u8{ + b.graph.global_cache_root.path.?, + "p", + hash, + models_subdir, + }, + )); + cg_cmd.addArg("--output"); + const cg_output_dir = cg_cmd.addOutputDirectoryArg("src/models"); + if (b.verbose) + cg_cmd.addArg("--verbose"); + // cg_cmd.step.dependOn(&fetch_step.step); + // TODO: this should use zig_exe from std.Build + // codegen should store a hash in a comment + // this would be hash of the exe that created the file + // concatenated with hash of input json. this would + // allow skipping generated files. May not include hash + // of contents of output file as maybe we want to tweak + // manually?? + // + // All the hashes can be in service_manifest.zig, which + // could be fun to just parse and go nuts. Top of + // file, generator exe hash. Each import has comment + // with both input and output hash and we can decide + // later about warning on manual changes... - exe.step.dependOn(gen_step); + cg.dependOn(&cg_cmd.step); + + exe.step.dependOn(cg); + + // This allows us to have each module depend on the + // generated service manifest. + const service_manifest_module = b.createModule(.{ + .root_source_file = cg_output_dir.path(b, "service_manifest.zig"), + .target = target, + .optimize = optimize, + }); + + // Expose module to others + _ = b.addModule("aws", .{ + .root_source_file = b.path("src/aws.zig"), + .imports = &.{ + .{ .name = "smithy", .module = smithy_module }, + .{ .name = "service_manifest", .module = service_manifest_module }, + }, + }); + + // Expose module to others + _ = b.addModule("aws-signing", .{ + .root_source_file = b.path("src/aws_signing.zig"), + .imports = &.{.{ .name = "smithy", .module = smithy_module }}, + }); // Similar to creating the run step earlier, this exposes a `test` step to // the `zig build --help` menu, providing a way for the user to request @@ -182,7 +191,7 @@ pub fn build(b: *Builder) !void { .optimize = optimize, }); unit_tests.root_module.addImport("smithy", smithy_dep.module("smithy")); - unit_tests.step.dependOn(gen_step); + unit_tests.step.dependOn(cg); const run_unit_tests = b.addRunArtifact(unit_tests); run_unit_tests.skip_foreign_checks = true; @@ -205,7 +214,7 @@ pub fn build(b: *Builder) !void { .optimize = optimize, }); smoke_test.root_module.addImport("smithy", smithy_dep.module("smithy")); - smoke_test.step.dependOn(gen_step); + smoke_test.step.dependOn(cg); const run_smoke_test = b.addRunArtifact(smoke_test); diff --git a/src/servicemodel.zig b/src/servicemodel.zig index 0f26886..9c8a8c7 100644 --- a/src/servicemodel.zig +++ b/src/servicemodel.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const service_list = @import("models/service_manifest.zig"); +const service_list = @import("service_manifest"); const expectEqualStrings = std.testing.expectEqualStrings; pub fn Services(comptime service_imports: anytype) type { From 71495a4d1d7000815ba44e8d261ecd42d893a0de Mon Sep 17 00:00:00 2001 From: Simon Hartcher Date: Thu, 13 Mar 2025 14:46:15 +1100 Subject: [PATCH 2/3] chore: add codegen to paths --- build.zig.zon | 1 + 1 file changed, 1 insertion(+) diff --git a/build.zig.zon b/build.zig.zon index f678894..468f17d 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -5,6 +5,7 @@ "build.zig", "build.zig.zon", "src", + "codegen", "README.md", "LICENSE", }, From 220d45ab2075316216cfad824fc67ebbae84a9cd Mon Sep 17 00:00:00 2001 From: Simon Hartcher Date: Thu, 13 Mar 2025 15:43:32 +1100 Subject: [PATCH 3/3] fix: missing module imports --- build.zig | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/build.zig b/build.zig index 8388869..63bbf64 100644 --- a/build.zig +++ b/build.zig @@ -95,7 +95,7 @@ pub fn build(b: *Builder) !void { .target = b.graph.host, .optimize = if (b.verbose) .Debug else .ReleaseSafe, }); - cg_exe.root_module.addImport("smithy", smithy_dep.module("smithy")); + cg_exe.root_module.addImport("smithy", smithy_module); var cg_cmd = b.addRunArtifact(cg_exe); cg_cmd.addArg("--models"); const hash = hash_blk: { @@ -146,6 +146,9 @@ pub fn build(b: *Builder) !void { .target = target, .optimize = optimize, }); + service_manifest_module.addImport("smithy", smithy_module); + + exe.root_module.addImport("service_manifest", service_manifest_module); // Expose module to others _ = b.addModule("aws", .{ @@ -190,7 +193,8 @@ pub fn build(b: *Builder) !void { .target = b.resolveTargetQuery(t), .optimize = optimize, }); - unit_tests.root_module.addImport("smithy", smithy_dep.module("smithy")); + unit_tests.root_module.addImport("smithy", smithy_module); + unit_tests.root_module.addImport("service_manifest", service_manifest_module); unit_tests.step.dependOn(cg); const run_unit_tests = b.addRunArtifact(unit_tests); @@ -213,7 +217,8 @@ pub fn build(b: *Builder) !void { .target = target, .optimize = optimize, }); - smoke_test.root_module.addImport("smithy", smithy_dep.module("smithy")); + smoke_test.root_module.addImport("smithy", smithy_module); + smoke_test.root_module.addImport("service_manifest", service_manifest_module); smoke_test.step.dependOn(cg); const run_smoke_test = b.addRunArtifact(smoke_test);