From e6adb593a05e8b660905dfcfaf9e62114aaed5b0 Mon Sep 17 00:00:00 2001 From: Tristan Ross Date: Thu, 13 Mar 2025 20:41:15 -0700 Subject: [PATCH 1/2] lib/std/Build.zig: add standard linkage option --- lib/std/Build.zig | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/std/Build.zig b/lib/std/Build.zig index 4afdcd2bff8c..aa26a613dcc5 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -1525,6 +1525,21 @@ 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, }; From 30b0cba3ce6ee3d5896ce52d6894ba94d23ab90e Mon Sep 17 00:00:00 2001 From: Tristan Ross Date: Thu, 13 Mar 2025 20:42:25 -0700 Subject: [PATCH 2/2] lib/init/build.zig: use standard linkage option --- lib/init/build.zig | 11 ++++++++++- lib/std/Build.zig | 6 +----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/init/build.zig b/lib/init/build.zig index 4db31713e436..454c6d220756 100644 --- a/lib/init/build.zig +++ b/lib/init/build.zig @@ -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. @@ -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, }); @@ -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 @@ -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 diff --git a/lib/std/Build.zig b/lib/std/Build.zig index aa26a613dcc5..cf86ba4d5ff9 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -1526,11 +1526,7 @@ pub fn step(b: *Build, name: []const u8, description: []const u8) *Step { } pub fn standardLinkageOption(b: *Build, target: Target) std.builtin.LinkMode { - if (b.option( - std.builtin.LinkMode, - "linkage", - "Method of linking binaries" - )) |linkage| { + if (b.option(std.builtin.LinkMode, "linkage", "Method of linking binaries")) |linkage| { return linkage; }