Archived
3
0

Zig implementation: only build static or shared library.

This commit is contained in:
Philippe Pittoli 2023-02-06 06:06:48 +01:00
parent 49e0430fb5
commit 7ef4c43d60

View File

@ -6,6 +6,11 @@ const VERSION = "0.1.0";
// declaratively construct a build graph that will be executed by an external // declaratively construct a build graph that will be executed by an external
// runner. // runner.
pub fn build(b: *std.Build) void { 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 // Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which // 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 // 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. // set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
const lib = b.addStaticLibrary(.{ const static_lib = b.addStaticLibrary(.{
.name = "ipc", .name = "ipc",
// In this case the main source file is merely a path, however, in more // In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file. // 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 // Link with the libc of the target system since the C allocator
// is required in the bindings. // is required in the bindings.
lib.linkLibC(); static_lib.linkLibC();
// This declares intent for the library to be installed into the standard // This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when // location when the user invokes the "install" step (the default step when
// running `zig build`). // running `zig build`).
lib.install(); static_lib.install();
const solib = b.addSharedLibrary(.{ const shared_lib = b.addSharedLibrary(.{
.name = "ipc", .name = "ipc",
.root_source_file = .{ .path = "src/bindings.zig" }, .root_source_file = .{ .path = "src/bindings.zig" },
.version = comptime (try std.builtin.Version.parse(VERSION)), .version = comptime (try std.builtin.Version.parse(VERSION)),
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
solib.linkLibC(); shared_lib.linkLibC();
solib.install(); shared_lib.install();
// Creates a step for unit testing. // Creates a step for unit testing.
const main_tests = b.addTest(.{ const main_tests = b.addTest(.{
@ -51,10 +56,20 @@ pub fn build(b: *std.Build) void {
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
main_tests.linkLibC();
// This creates a build step. It will be visible in the `zig build --help` menu, // This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build test` // and can be selected like this: `zig build test`
// This will evaluate the `test` step rather than the default, which is "install". // This will evaluate the `test` step rather than the default, which is "install".
const test_step = b.step("test", "Run library tests"); const test_step = b.step("test", "Run library tests");
test_step.dependOn(&main_tests.step); 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);
} }