extract lambda parameters to seperate function

This commit is contained in:
Emil Lerch 2023-08-01 12:20:56 -07:00
parent 7b890d2458
commit fa13a08c4d
Signed by: lobo
GPG Key ID: A7B62D657EF764F8

View File

@ -8,27 +8,77 @@ pub fn build(b: *std.build.Builder) !void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{ var exe = b.addExecutable(.{
.name = "bootstrap", .name = "bootstrap",
.root_source_file = .{ .path = "src/main.zig" }, .root_source_file = .{ .path = "src/main.zig" },
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
// exe.setBuildMode(.ReleaseSafe); // TODO: ReleaseSmall is stripped. Maybe best to just leave this to user try lambdaBuildOptions(b, exe);
const debug = b.option(bool, "debug", "Debug mode (do not strip executable)") orelse false;
exe.strip = !debug;
b.installArtifact(exe); b.installArtifact(exe);
// TODO: We can cross-compile of course, but stripping and zip commands // TODO: We can cross-compile of course, but stripping and zip commands
// may vary // may vary
if (builtin.os.tag == .linux) { // TODO: Add test
}
fn fileExists(file_name: []const u8) bool {
const file = std.fs.openFileAbsolute(file_name, .{}) catch return false;
defer file.close();
return true;
}
fn addArgs(allocator: std.mem.Allocator, original: []const u8, args: [][]const u8) ![]const u8 {
var rc = original;
for (args) |arg| {
rc = try std.mem.concat(allocator, u8, &.{ rc, " ", arg });
}
return rc;
}
/// lambdaBuildOptions will add three build options to the build (if compiling
/// the code on a Linux host):
///
/// * package: Packages the function for deployment to Lambda
/// (dependencies are the zip executable and a shell)
/// * iam: Gets an IAM role for the Lambda function, and creates it if it does not exist
/// (dependencies are the AWS CLI, grep and a shell)
/// * deploy: Deploys the lambda function to a live AWS environment
/// (dependencies are the AWS CLI, and a shell)
/// * remoterun: Runs the lambda function in a live AWS environment
/// (dependencies are the AWS CLI, and a shell)
///
/// remoterun depends on deploy
/// deploy depends on iam and package
///
/// iam and package do not have any dependencies
///
/// At the moment, there I do not see a way utilize the zig package manager to
/// add modules for build.zig itself. There is some thinking in this area:
/// https://github.com/ziglang/zig/issues/8070
///
/// But for now I think this is a copy/paste job
/// TODO: Move this code into another file (temporary)
/// TODO: Determine a way to use packages within build.zig (permanent)
pub fn lambdaBuildOptions(b: *std.build.Builder, exe: *std.Build.Step.Compile) !void {
// The rest of this function is currently reliant on the use of Linux
// system being used to build the lambda function
//
// It is likely that much of this will work on other Unix-like OSs, but
// we will work this out later
//
// TODO: support other host OSs
if (builtin.os.tag != .linux) return;
// Package step // Package step
const package_step = b.step("package", "Package the function"); const package_step = b.step("package", "Package the function");
package_step.dependOn(b.getInstallStep()); package_step.dependOn(b.getInstallStep());
// const function_zip = b.getInstallPath(exe.installed_path.?, "function.zip"); // const function_zip = b.getInstallPath(exe.installed_path.?, "function.zip");
const function_zip = b.getInstallPath(.bin, "function.zip"); const function_zip = b.getInstallPath(.bin, "function.zip");
// TODO: https://github.com/hdorio/hwzip.zig/blob/master/src/hwzip.zig
// TODO: allow use of user-specified exe names
// TODO: Avoid use of system-installed zip, maybe using something like
// https://github.com/hdorio/hwzip.zig/blob/master/src/hwzip.zig
const zip = try std.fmt.allocPrint(b.allocator, "zip -qj9 {s} {s}", .{ function_zip, b.getInstallPath(.bin, exe.out_filename) }); const zip = try std.fmt.allocPrint(b.allocator, "zip -qj9 {s} {s}", .{ function_zip, b.getInstallPath(.bin, exe.out_filename) });
defer b.allocator.free(zip); defer b.allocator.free(zip);
package_step.dependOn(&b.addSystemCommand(&.{ "/bin/sh", "-c", zip }).step); package_step.dependOn(&b.addSystemCommand(&.{ "/bin/sh", "-c", zip }).step);
@ -44,6 +94,8 @@ pub fn build(b: *std.build.Builder) !void {
} }
} }
} }
// TODO: Allow custom lambda role names
var iam_role: []u8 = &.{}; var iam_role: []u8 = &.{};
const iam_step = b.step("iam", "Create/Get IAM role for function"); const iam_step = b.step("iam", "Create/Get IAM role for function");
deploy_step.dependOn(iam_step); // iam_step will either be a noop or all the stuff below deploy_step.dependOn(iam_step); // iam_step will either be a noop or all the stuff below
@ -144,21 +196,6 @@ pub fn build(b: *std.build.Builder) !void {
run_cmd.addArgs(args); run_cmd.addArgs(args);
} }
const run_step = b.step("run", "Run the app"); const run_step = b.step("remoterun", "Run the app in AWS lambda");
run_step.dependOn(&run_cmd.step); run_step.dependOn(&run_cmd.step);
// TODO: Add test
}
}
fn fileExists(file_name: []const u8) bool {
const file = std.fs.openFileAbsolute(file_name, .{}) catch return false;
defer file.close();
return true;
}
fn addArgs(allocator: std.mem.Allocator, original: []const u8, args: [][]const u8) ![]const u8 {
var rc = original;
for (args) |arg| {
rc = try std.mem.concat(allocator, u8, &.{ rc, " ", arg });
}
return rc;
} }