update build.zig and README

This commit is contained in:
Emil Lerch 2021-06-30 09:37:14 -07:00
parent 3c9f91c012
commit e98d64f39c
Signed by: lobo
GPG Key ID: A7B62D657EF764F8
2 changed files with 78 additions and 35 deletions

View File

@ -1,23 +1,17 @@
# AWS SDK for Zig
Ok, so it's not actually an SDK (yet). Right now this is SDK supports sts
get-caller-identity action only. Why? Because it's one of the easiest to
support, so I started there. From here, the next major step is to codegen
the types necessary to support the various services. Currently this code is
dynamically generating the sts types so we are somewhat codegen ready, but
current comptime limitations might trip us up. The advantage of comptime is
that only types actually used would be generated vs the whole surface area
of AWS. That said, with most of the heavy lifting now coded, the addition
of the request/response types, even if all of them are added, should not
balloon the size beyond "reasonable". Of course this still needs to be be seen.
Ok, so it's not actually an SDK (yet). Right now the SDK should support
any "query-based" operation and probably EC2, though this isn't tested yet.
Total service count should be around 18 services supported. If you use an
unsupported service, you'll get a compile error.
This is my first serious zig effort, so please issue a PR if the code isn't
"ziggy" or if there's a better way.
This is designed to be built statically using the `aws_c_*` libraries, so
we inherit a lot of the goodness of the work going on there. Implementing
get-caller-identity with all dependencies statically linked gives us a stripped
executable size of 5.3M for x86_linux (which is all that's tested at the moment).
we inherit a lot of the goodness of the work going on there. Current
executable size is 10.3M, about half of which is due to the SSL library.
This is for x86_linux (which is all that's tested at the moment).
## Building
@ -31,12 +25,15 @@ I'm also hoping/expecting AWS to factor out that library sometime in
the future.
Once that's done, you'll have an alpine image with all dependencies ready
to go and zig 0.7.1 installed. The build.zig currently relies on
[this PR to allow stripping -static](https://github.com/ziglang/zig/pull/8248),
so either:
to go and zig master installed. There are some build-related things still
broken in 0.8.0 and hopefully 0.8.1 will address those and we can be on
a standard release.
* Modify build.zig, then strip (or not) after the fact
* Install make and use the included Makefile
* `zig build` should work. It will build the code generation project, run
the code generation, then build the main project with the generated code.
* Install make and use the included Makefile. Going this path should be fine
with zig 0.8.0 release, but code generation has not been added to the
Makefile yet (ever?), so you'll be on your own for that.
## Running
@ -96,7 +93,7 @@ TODO List:
* ✓ Implement codegen for services with xml structures (using Smithy models)
* ✓ Implement codegen for others (using Smithy models)
* Switch to aws-c-cal upstream once [PR for full static musl build support is merged](https://github.com/awslabs/aws-c-cal/pull/89) (see Dockerfile)
* Remove compiler 0.7.1 shims when 0.8.0 is released
* Move to compiler on tagged release (hopefully 0.8.1)
(new 2021-05-29. I will proceed in this order unless I get other requests)
* ✓ Implement [AWS query protocol](https://awslabs.github.io/smithy/1.0/spec/aws/aws-query-protocol.html). This is the protocol in use by sts.getcalleridentity. Total service count 18
* Implement [AWS Json 1.0 protocol](https://awslabs.github.io/smithy/1.0/spec/aws/aws-json-1_0-protocol.html). Includes dynamodb. Total service count 18
@ -106,7 +103,6 @@ TODO List:
Compiler wishlist/watchlist:
* Fix the weirdness we see with comptime type generation (see aws.zig around line 135)
* ~~[Allow declarations for comptime type generation](https://github.com/ziglang/zig/issues/6709)~~
This is no longer as important. The primary issue was in the return value, but

View File

@ -1,7 +1,8 @@
// const std = @import("std");
const std = @import("std");
const builtin = @import("builtin");
const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void {
pub fn build(b: *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
@ -13,20 +14,45 @@ pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("demo", "src/main.zig");
// TODO: Generate src/models.zig
// https://github.com/ziglang/zig/issues/855
exe.addPackagePath("smithy", "smithy/src/smithy.zig");
// TODO: Support > linux
// TODO: Get a better cache in place
if (std.builtin.os.tag == .linux) {
const codegen = b.step("gen", "Generate zig service code from smithy models");
codegen.dependOn(&b.addSystemCommand(&.{ "/bin/sh", "-c", "cd codegen && zig build" }).step);
codegen.dependOn(&b.addSystemCommand(&.{
"/bin/sh", "-c",
\\ mkdir -p src/models/ && \
\\ [ -f src/models/service_manifest.zig ] || \
\\ ( cd codegen/models && ../codegen *.json && mv *.zig ../../src/models )
}).step);
b.getInstallStep().dependOn(codegen);
}
exe.addCSourceFile("src/bitfield-workaround.c", &[_][]const u8{"-std=c99"});
exe.addIncludeDir("./src/");
exe.addIncludeDir("/usr/local/include");
exe.addObjectFile("/usr/local/lib64/libs2n.a");
exe.addObjectFile("/usr/local/lib64/libcrypto.a");
exe.addObjectFile("/usr/local/lib64/libssl.a");
exe.addObjectFile("/usr/local/lib64/libaws-c-auth.a");
exe.addObjectFile("/usr/local/lib64/libaws-c-cal.a");
exe.addObjectFile("/usr/local/lib64/libaws-c-common.a");
exe.addObjectFile("/usr/local/lib64/libaws-c-compression.a");
exe.addObjectFile("/usr/local/lib64/libaws-c-http.a");
exe.addObjectFile("/usr/local/lib64/libaws-c-io.a");
const c_include_dirs = .{
"./src/",
"/usr/local/include",
};
inline for (c_include_dirs) |dir|
exe.addIncludeDir(dir);
const dependent_objects = .{
"/usr/local/lib64/libs2n.a",
"/usr/local/lib64/libcrypto.a",
"/usr/local/lib64/libssl.a",
"/usr/local/lib64/libaws-c-auth.a",
"/usr/local/lib64/libaws-c-cal.a",
"/usr/local/lib64/libaws-c-common.a",
"/usr/local/lib64/libaws-c-compression.a",
"/usr/local/lib64/libaws-c-http.a",
"/usr/local/lib64/libaws-c-io.a",
};
inline for (dependent_objects) |obj|
exe.addObjectFile(obj);
exe.linkSystemLibrary("c");
exe.setTarget(target);
exe.setBuildMode(mode);
@ -36,9 +62,14 @@ pub fn build(b: *Builder) void {
// 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;
exe.strip = true;
const is_strip = b.option(bool, "strip", "strip exe") orelse true;
exe.strip = !is_strip;
exe.install();
const run_cmd = exe.run();
@ -49,4 +80,20 @@ pub fn build(b: *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")) {
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);
}
}
}