From 7ef4c43d600dcb46c97604c4c81bb890073cc37e Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Mon, 6 Feb 2023 06:06:48 +0100 Subject: [PATCH] Zig implementation: only build static or shared library. --- zig-impl/build.zig | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/zig-impl/build.zig b/zig-impl/build.zig index 4ff6f3a..3de83fb 100644 --- a/zig-impl/build.zig +++ b/zig-impl/build.zig @@ -6,6 +6,11 @@ const VERSION = "0.1.0"; // declaratively construct a build graph that will be executed by an external // runner. pub fn build(b: *std.Build) void { + const is_release = b.option(bool, "release", "Compile a release build.") orelse false; + + if (is_release) { + std.log.err("hello, this is the release stuff", .{}); + } // 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 @@ -17,7 +22,7 @@ pub fn build(b: *std.Build) void { // set a preferred release mode, allowing the user to decide how to optimize. const optimize = b.standardOptimizeOption(.{}); - const lib = b.addStaticLibrary(.{ + const static_lib = b.addStaticLibrary(.{ .name = "ipc", // In this case the main source file is merely a path, however, in more // complicated build scripts, this could be a generated file. @@ -28,22 +33,22 @@ pub fn build(b: *std.Build) void { // Link with the libc of the target system since the C allocator // is required in the bindings. - lib.linkLibC(); + static_lib.linkLibC(); // 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`). - lib.install(); + static_lib.install(); - const solib = b.addSharedLibrary(.{ + const shared_lib = b.addSharedLibrary(.{ .name = "ipc", .root_source_file = .{ .path = "src/bindings.zig" }, .version = comptime (try std.builtin.Version.parse(VERSION)), .target = target, .optimize = optimize, }); - solib.linkLibC(); - solib.install(); + shared_lib.linkLibC(); + shared_lib.install(); // Creates a step for unit testing. const main_tests = b.addTest(.{ @@ -51,10 +56,20 @@ pub fn build(b: *std.Build) void { .target = target, .optimize = optimize, }); + main_tests.linkLibC(); // 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 library tests"); test_step.dependOn(&main_tests.step); + + const install_static_lib = b.addInstallArtifact(static_lib); + const static_lib_step = b.step("static", "Compile LibIPC as a static library."); + static_lib_step.dependOn(&install_static_lib.step); + + const install_shared_lib = b.addInstallArtifact(shared_lib); + // b.getInstallStep().dependOn(&install_shared_lib.step); + const shared_lib_step = b.step("shared", "Compile LibIPC as a shared library."); + shared_lib_step.dependOn(&install_shared_lib.step); }