From 08dec6a9782ec73d575c4c842686e21040c93af5 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Sun, 7 May 2023 16:12:33 -0700 Subject: [PATCH] add library for stuff --- build.zig | 39 +++++++++++++++++++++++++++------------ src/main-lib.zig | 10 ++++++++++ 2 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 src/main-lib.zig diff --git a/build.zig b/build.zig index 455173a..d96cad2 100644 --- a/build.zig +++ b/build.zig @@ -24,11 +24,25 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); + const lib = b.addSharedLibrary(.{ + .name = "faas-proxy-sample-lib", + // In this case the main source file is merely a path, however, in more + // complicated build scripts, this could be a generated file. + .root_source_file = .{ .path = "src/main-lib.zig" }, + .target = target, + .optimize = optimize, + }); + // 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); + // This declares intent for the library to be installed into the standard + // location when the user invokes the "install" step (the default step when + // running `zig build`). + b.installArtifact(lib); + // This *creates* a RunStep 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. @@ -46,25 +60,26 @@ pub fn build(b: *std.Build) void { 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); - // Creates a step for unit testing. This only builds the test executable // but does not run it. - const unit_tests = b.addTest(.{ + const main_tests = b.addTest(.{ .root_source_file = .{ .path = "src/main.zig" }, .target = target, .optimize = optimize, }); + const lib_tests = b.addTest(.{ + .root_source_file = .{ .path = "src/main-lib.zig" }, + .target = target, + .optimize = optimize, + }); - const run_unit_tests = b.addRunArtifact(unit_tests); + const run_main_tests = b.addRunArtifact(main_tests); + const run_lib_tests = b.addRunArtifact(lib_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. + // This creates a build step. It will be visible in the `zig build --help` menu, + // and can be selected like this: `zig build test` + // This will evaluate the `test` step rather than the default, which is "install". const test_step = b.step("test", "Run unit tests"); - test_step.dependOn(&run_unit_tests.step); + test_step.dependOn(&run_main_tests.step); + test_step.dependOn(&run_lib_tests.step); } diff --git a/src/main-lib.zig b/src/main-lib.zig new file mode 100644 index 0000000..ecfeade --- /dev/null +++ b/src/main-lib.zig @@ -0,0 +1,10 @@ +const std = @import("std"); +const testing = std.testing; + +export fn add(a: i32, b: i32) i32 { + return a + b; +} + +test "basic add functionality" { + try testing.expect(add(3, 7) == 10); +}