commit b4ffb402c082605c4b324e88120306fc8fb3cf32 (tree)
parent 77d76869029a3307531b67cdc2e82f63d84dd7e0
Author: Andrew Kelley <andrew@ziglang.org>
Date: Tue, 10 Mar 2026 12:01:58 -0700
translate-c build step: handle system libraries
closes #31450
Diffstat:
3 files changed, 118 insertions(+), 18 deletions(-)
diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig
@@ -702,10 +702,10 @@ const PkgConfigResult = struct {
/// Run pkg-config for the given library name and parse the output, returning the arguments
/// that should be passed to zig to link the given library.
-fn runPkgConfig(compile: *Compile, lib_name: []const u8) !PkgConfigResult {
+pub fn runPkgConfig(step: *Step, lib_name: []const u8) !PkgConfigResult {
const wl_rpath_prefix = "-Wl,-rpath,";
- const b = compile.step.owner;
+ const b = step.owner;
const pkg_name = match: {
// First we have to map the library name to pkg config name. Unfortunately,
// there are several examples where this is not straightforward:
@@ -798,7 +798,7 @@ fn runPkgConfig(compile: *Compile, lib_name: []const u8) !PkgConfigResult {
} else if (mem.startsWith(u8, arg, wl_rpath_prefix)) {
try zig_cflags.appendSlice(&[_][]const u8{ "-rpath", arg[wl_rpath_prefix.len..] });
} else if (b.debug_pkg_config) {
- return compile.step.fail("unknown pkg-config flag '{s}'", .{arg});
+ return step.fail("unknown pkg-config flag '{s}'", .{arg});
}
}
@@ -1111,7 +1111,7 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
switch (system_lib.use_pkg_config) {
.no => try zig_args.append(b.fmt("{s}{s}", .{ prefix, system_lib.name })),
.yes, .force => {
- if (compile.runPkgConfig(system_lib.name)) |result| {
+ if (runPkgConfig(&compile.step, system_lib.name)) |result| {
try zig_args.appendSlice(result.cflags);
try zig_args.appendSlice(result.libs);
try seen_system_libs.put(arena, system_lib.name, result.cflags);
diff --git a/lib/std/Build/Step/TranslateC.zig b/lib/std/Build/Step/TranslateC.zig
@@ -11,6 +11,7 @@ pub const base_id: Step.Id = .translate_c;
step: Step,
source: std.Build.LazyPath,
include_dirs: std.array_list.Managed(std.Build.Module.IncludeDir),
+system_libs: std.ArrayList(std.Build.Module.SystemLib),
c_macros: std.array_list.Managed([]const u8),
out_basename: []const u8,
target: std.Build.ResolvedTarget,
@@ -46,6 +47,7 @@ pub fn create(owner: *std.Build, options: Options) *TranslateC {
.output_file = .{ .step = &translate_c.step },
.link_libc = options.link_libc,
.use_clang = options.use_clang,
+ .system_libs = .empty,
};
source.addStepDependencies(&translate_c.step);
return translate_c;
@@ -67,24 +69,37 @@ pub fn getOutput(translate_c: *TranslateC) std.Build.LazyPath {
/// module set making it available to other packages which depend on this one.
/// `createModule` can be used instead to create a private module.
pub fn addModule(translate_c: *TranslateC, name: []const u8) *std.Build.Module {
- return translate_c.step.owner.addModule(name, .{
+ return setUpModule(translate_c, translate_c.step.owner.addModule(name, .{
.root_source_file = translate_c.getOutput(),
.target = translate_c.target,
.optimize = translate_c.optimize,
.link_libc = translate_c.link_libc,
- });
+ }));
}
/// Creates a private module from the translated source to be used by the
/// current package, but not exposed to other packages depending on this one.
/// `addModule` can be used instead to create a public module.
pub fn createModule(translate_c: *TranslateC) *std.Build.Module {
- return translate_c.step.owner.createModule(.{
+ return setUpModule(translate_c, translate_c.step.owner.createModule(.{
.root_source_file = translate_c.getOutput(),
.target = translate_c.target,
.optimize = translate_c.optimize,
.link_libc = translate_c.link_libc,
- });
+ }));
+}
+
+fn setUpModule(translate_c: *TranslateC, module: *std.Build.Module) *std.Build.Module {
+ const b = translate_c.step.owner;
+ const arena = b.graph.arena;
+
+ if (translate_c.link_libc) module.link_libc = true;
+
+ for (translate_c.system_libs.items) |system_lib| {
+ module.link_objects.append(arena, .{ .system_lib = system_lib }) catch @panic("OOM");
+ }
+
+ return module;
}
pub fn addAfterIncludePath(translate_c: *TranslateC, lazy_path: LazyPath) void {
@@ -152,6 +167,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
const prog_node = options.progress_node;
const b = step.owner;
const translate_c: *TranslateC = @fieldParentPtr("step", step);
+ const arena = b.graph.arena;
var argv_list = std.array_list.Managed([]const u8).init(b.allocator);
try argv_list.append(b.graph.zig_exe);
@@ -169,8 +185,6 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
try argv_list.append("--global-cache-dir");
try argv_list.append(b.graph.global_cache_root.path orelse ".");
- try argv_list.append("--listen=-");
-
if (!translate_c.target.query.isNative()) {
try argv_list.append("-target");
try argv_list.append(try translate_c.target.query.zigTriple(b.allocator));
@@ -190,12 +204,102 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
try argv_list.append(c_macro);
}
+ var prev_search_strategy: std.Build.Module.SystemLib.SearchStrategy = .paths_first;
+ var prev_preferred_link_mode: std.builtin.LinkMode = .dynamic;
+
+ for (translate_c.system_libs.items) |*system_lib| {
+ var seen_system_libs: std.StringHashMapUnmanaged([]const []const u8) = .empty;
+ const system_lib_gop = try seen_system_libs.getOrPut(arena, system_lib.name);
+ if (system_lib_gop.found_existing) {
+ try argv_list.appendSlice(system_lib_gop.value_ptr.*);
+ continue;
+ } else {
+ system_lib_gop.value_ptr.* = &.{};
+ }
+
+ if (system_lib.search_strategy != prev_search_strategy or
+ system_lib.preferred_link_mode != prev_preferred_link_mode)
+ {
+ switch (system_lib.search_strategy) {
+ .no_fallback => switch (system_lib.preferred_link_mode) {
+ .dynamic => try argv_list.append("-search_dylibs_only"),
+ .static => try argv_list.append("-search_static_only"),
+ },
+ .paths_first => switch (system_lib.preferred_link_mode) {
+ .dynamic => try argv_list.append("-search_paths_first"),
+ .static => try argv_list.append("-search_paths_first_static"),
+ },
+ .mode_first => switch (system_lib.preferred_link_mode) {
+ .dynamic => try argv_list.append("-search_dylibs_first"),
+ .static => try argv_list.append("-search_static_first"),
+ },
+ }
+ prev_search_strategy = system_lib.search_strategy;
+ prev_preferred_link_mode = system_lib.preferred_link_mode;
+ }
+
+ const prefix: []const u8 = prefix: {
+ if (system_lib.needed) break :prefix "-needed-l";
+ if (system_lib.weak) break :prefix "-weak-l";
+ break :prefix "-l";
+ };
+ switch (system_lib.use_pkg_config) {
+ .no => try argv_list.append(b.fmt("{s}{s}", .{ prefix, system_lib.name })),
+ .yes, .force => {
+ if (Step.Compile.runPkgConfig(&translate_c.step, system_lib.name)) |result| {
+ try argv_list.appendSlice(result.cflags);
+ try argv_list.appendSlice(result.libs);
+ try seen_system_libs.put(arena, system_lib.name, result.cflags);
+ } else |err| switch (err) {
+ error.PkgConfigInvalidOutput,
+ error.PkgConfigCrashed,
+ error.PkgConfigFailed,
+ error.PkgConfigNotInstalled,
+ error.PackageNotFound,
+ => switch (system_lib.use_pkg_config) {
+ .yes => {
+ // pkg-config failed, so fall back to linking the library
+ // by name directly.
+ try argv_list.append(b.fmt("{s}{s}", .{
+ prefix,
+ system_lib.name,
+ }));
+ },
+ .force => {
+ std.debug.panic("pkg-config failed for library {s}", .{system_lib.name});
+ },
+ .no => unreachable,
+ },
+
+ else => |e| return e,
+ }
+ },
+ }
+ }
+
const c_source_path = translate_c.source.getPath2(b, step);
try argv_list.append(c_source_path);
+ try argv_list.append("--listen=-");
const output_dir = try step.evalZigProcess(argv_list.items, prog_node, false, options.web_server, options.gpa);
const basename = std.fs.path.stem(std.fs.path.basename(c_source_path));
translate_c.out_basename = b.fmt("{s}.zig", .{basename});
translate_c.output_file.path = output_dir.?.joinString(b.allocator, translate_c.out_basename) catch @panic("OOM");
}
+
+pub fn linkSystemLibrary(
+ translate_c: *TranslateC,
+ name: []const u8,
+ options: std.Build.Module.LinkSystemLibraryOptions,
+) void {
+ const b = translate_c.step.owner;
+ translate_c.system_libs.append(b.allocator, .{
+ .name = b.dupe(name),
+ .needed = options.needed,
+ .weak = options.weak,
+ .use_pkg_config = options.use_pkg_config,
+ .preferred_link_mode = options.preferred_link_mode,
+ .search_strategy = options.search_strategy,
+ }) catch @panic("OOM");
+}
diff --git a/src/main.zig b/src/main.zig
@@ -4688,9 +4688,8 @@ fn cmdTranslateC(
man.hash.add(@as(u16, 0xb945)); // Random number to distinguish translate-c from compiling C objects
man.hash.add(comp.config.c_frontend);
- Compilation.cache_helpers.hashCSource(&man, c_source_file) catch |err| {
- fatal("unable to process '{s}': {s}", .{ c_source_file.src_path, @errorName(err) });
- };
+ Compilation.cache_helpers.hashCSource(&man, c_source_file) catch |err|
+ fatal("unable to process '{s}': {t}", .{ c_source_file.src_path, err });
const result: Compilation.CImportResult = if (try man.hit()) .{
.digest = man.finalBin(),
@@ -4732,11 +4731,8 @@ fn cmdTranslateC(
const out_zig_path = try fs.path.join(arena, &.{ "o", &hex_digest, translated_basename });
const zig_file = comp.dirs.local_cache.handle.openFile(io, out_zig_path, .{}) catch |err| {
const path = comp.dirs.local_cache.path orelse ".";
- fatal("unable to open cached translated zig file '{s}{s}{s}': {s}", .{
- path,
- fs.path.sep_str,
- out_zig_path,
- @errorName(err),
+ fatal("unable to open cached translated zig file '{s}{s}{s}': {t}", .{
+ path, fs.path.sep_str, out_zig_path, err,
});
};
defer zig_file.close(io);