diff --git a/codegen/build.zig b/codegen/build.zig index bd0f3ee..4763371 100644 --- a/codegen/build.zig +++ b/codegen/build.zig @@ -1,5 +1,8 @@ const std = @import("std"); +// Although this function looks imperative, note that its job is to +// declaratively construct a build graph that will be executed by an external +// runner. 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 @@ -7,53 +10,65 @@ pub fn build(b: *std.build.Builder) !void { // for restricting supported target set are available. const target = b.standardTargetOptions(.{}); - // Standard release options allow the person running `zig build` to select - // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. - const mode = b.standardReleaseOptions(); + // Standard optimization options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not + // set a preferred release mode, allowing the user to decide how to optimize. + const optimize = b.standardOptimizeOption(.{}); - const exe = b.addExecutable("codegen", "src/main.zig"); - exe.addPackagePath("smithy", "../smithy/src/smithy.zig"); - exe.setTarget(target); - exe.setBuildMode(mode); - // 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 = ".." }; + const exe = b.addExecutable(.{ + .name = "codegen", + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); - // 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 smithy_dep = b.dependency("smithy", .{ + .target = target, + .optimize = optimize, + }); + exe.addModule("smithy", smithy_dep.module("smithy")); - const is_strip = b.option(bool, "strip", "strip exe") orelse true; - exe.strip = !is_strip; - exe.install(); + // This declares intent for the executable to be installed into the + // standard location when the user invokes the "install" step (the default + // step when running `zig build`). + b.installArtifact(exe); - const run_cmd = exe.run(); + // This *creates* a Run step in the build graph, to be executed when another + // step is evaluated that depends on it. The next line below will establish + // such a dependency. + const run_cmd = b.addRunArtifact(exe); + + // By making the run step depend on the install step, it will be run from the + // installation directory rather than directly from within the cache directory. + // This is not necessary, however, if the application depends on other installed + // files, this ensures they will be present and in the expected location. run_cmd.step.dependOn(b.getInstallStep()); + + // This allows the user to pass arguments to the application in the build + // command itself, like this: `zig build run -- arg1 arg2 etc` if (b.args) |args| { run_cmd.addArgs(args); } + // This creates a build step. It will be visible in the `zig build --help` menu, + // and can be selected like this: `zig build run` + // This will evaluate the `run` step rather than the default, which is "install". 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 src_dir = try std.fs.openDirAbsolute(b.build_root, .{}); - defer src_dir.close(); - var iterable = try src_dir.openIterableDir(".", .{}); - defer iterable.close(); - var iterator = iterable.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); - } - } + // Creates a step for unit testing. This only builds the test executable + // but does not run it. + const unit_tests = b.addTest(.{ + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + + const run_unit_tests = b.addRunArtifact(unit_tests); + + // 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 + // running the unit tests. + const test_step = b.step("test", "Run unit tests"); + test_step.dependOn(&run_unit_tests.step); } diff --git a/codegen/build.zig.zon b/codegen/build.zig.zon new file mode 100644 index 0000000..e7a073c --- /dev/null +++ b/codegen/build.zig.zon @@ -0,0 +1,11 @@ +.{ + .name = "aws-zig-codegen", + .version = "0.0.1", + + .dependencies = .{ + .smithy = .{ + .url = "https://git.lerch.org/lobo/smithy/archive/41b61745d25a65817209dd5dddbb5f9b66896a99.tar.gz", + .hash = "122087deb0ae309b2258d59b40d82fe5921fdfc35b420bb59033244851f7f276fa34", + }, + }, +} diff --git a/codegen/src/json.zig b/codegen/src/json.zig index 2b38b9c..c9ff0ea 100644 --- a/codegen/src/json.zig +++ b/codegen/src/json.zig @@ -19,7 +19,7 @@ pub fn serializeMap(map: anytype, key: []const u8, options: anytype, out_stream: try out_stream.writeByte('{'); if (options.whitespace) |_| try out_stream.writeByte('\n'); - for (map) |tag, i| { + for (map, 0..) |tag, i| { if (tag.key == null or tag.value == null) continue; // TODO: Deal with escaping and general "json.stringify" the values... if (child_options.whitespace) |ws| @@ -100,8 +100,8 @@ fn outputUnicodeEscape( assert(codepoint <= 0x10FFFF); // To escape an extended character that is not in the Basic Multilingual Plane, // the character is represented as a 12-character sequence, encoding the UTF-16 surrogate pair. - const high = @intCast(u16, (codepoint - 0x10000) >> 10) + 0xD800; - const low = @intCast(u16, codepoint & 0x3FF) + 0xDC00; + const high = @as(u16, @intCast((codepoint - 0x10000) >> 10)) + 0xD800; + const low = @as(u16, @intCast(codepoint & 0x3FF)) + 0xDC00; try out_stream.writeAll("\\u"); try std.fmt.formatIntValue(high, "x", std.fmt.FormatOptions{ .width = 4, .fill = '0' }, out_stream); try out_stream.writeAll("\\u"); diff --git a/codegen/src/snake.zig b/codegen/src/snake.zig index fba9dfa..299d6a3 100644 --- a/codegen/src/snake.zig +++ b/codegen/src/snake.zig @@ -72,7 +72,7 @@ fn isAcronymChar(char: u8) bool { fn isAscii(codepoint: ?u21) !?u8 { if (codepoint) |cp| { if (cp > 0xff) return error.UnicodeNotSupported; - return @truncate(u8, cp); + return @as(u8, @truncate(cp)); } return null; }