Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/std/Build.zig: add standard linkage option #23239

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/init/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ pub fn build(b: *std.Build) void {
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});

// Standard linkage option allows the person running `zig build` to select
// between static or dynamic.
const linkage = b.standardLinkageOption(target.result);

// This creates a "module", which represents a collection of source files alongside
// some compilation options, such as optimization mode and linked system libraries.
// Every executable or library we compile will be based on one or more modules.
Expand Down Expand Up @@ -48,7 +52,7 @@ pub fn build(b: *std.Build) void {
// This creates a `std.Build.Step.Compile`, which is the build step responsible
// for actually invoking the compiler.
const lib = b.addLibrary(.{
.linkage = .static,
.linkage = linkage,
.name = ".NAME",
.root_module = lib_mod,
});
Expand All @@ -63,6 +67,7 @@ pub fn build(b: *std.Build) void {
const exe = b.addExecutable(.{
.name = ".NAME",
.root_module = exe_mod,
.linkage = linkage,
});

// This declares intent for the executable to be installed into the
Expand Down Expand Up @@ -99,12 +104,16 @@ pub fn build(b: *std.Build) void {
.root_module = lib_mod,
});

lib_unit_tests.linkage = linkage;

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

const exe_unit_tests = b.addTest(.{
.root_module = exe_mod,
});

exe_unit_tests.linkage = linkage;

const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);

// Similar to creating the run step earlier, this exposes a `test` step to
Expand Down
11 changes: 11 additions & 0 deletions lib/std/Build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,17 @@ pub fn step(b: *Build, name: []const u8, description: []const u8) *Step {
return &step_info.step;
}

pub fn standardLinkageOption(b: *Build, target: Target) std.builtin.LinkMode {
if (b.option(std.builtin.LinkMode, "linkage", "Method of linking binaries")) |linkage| {
return linkage;
}

return switch (target.os.tag) {
.linux, .macos, .ios, .windows => .dynamic,
else => .static,
};
}

pub const StandardOptimizeOptionOptions = struct {
preferred_optimize_mode: ?std.builtin.OptimizeMode = null,
};
Expand Down