Zig implementation: only build static or shared library.

master
Philippe Pittoli 2023-02-06 06:06:48 +01:00
parent 49e0430fb5
commit 7ef4c43d60
1 changed files with 21 additions and 6 deletions

View File

@ -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);
}