authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-03-10 12:01:58-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-03-11 04:57:25+01:00
logb4ffb402c082605c4b324e88120306fc8fb3cf32
tree60571e084227e6286ccbe3e782972f7f924f1fe6
parent77d76869029a3307531b67cdc2e82f63d84dd7e0

translate-c build step: handle system libraries

closes #31450

3 files changed, 118 insertions(+), 18 deletions(-)

lib/std/Build/Step/Compile.zig+4-4
...@@ -702,10 +702,10 @@ const PkgConfigResult = struct {...@@ -702,10 +702,10 @@ const PkgConfigResult = struct {
702702
703/// Run pkg-config for the given library name and parse the output, returning the arguments703/// Run pkg-config for the given library name and parse the output, returning the arguments
704/// that should be passed to zig to link the given library.704/// that should be passed to zig to link the given library.
705fn runPkgConfig(compile: *Compile, lib_name: []const u8) !PkgConfigResult {705pub fn runPkgConfig(step: *Step, lib_name: []const u8) !PkgConfigResult {
706 const wl_rpath_prefix = "-Wl,-rpath,";706 const wl_rpath_prefix = "-Wl,-rpath,";
707707
708 const b = compile.step.owner;708 const b = step.owner;
709 const pkg_name = match: {709 const pkg_name = match: {
710 // First we have to map the library name to pkg config name. Unfortunately,710 // First we have to map the library name to pkg config name. Unfortunately,
711 // there are several examples where this is not straightforward:711 // there are several examples where this is not straightforward:
...@@ -798,7 +798,7 @@ fn runPkgConfig(compile: *Compile, lib_name: []const u8) !PkgConfigResult {...@@ -798,7 +798,7 @@ fn runPkgConfig(compile: *Compile, lib_name: []const u8) !PkgConfigResult {
798 } else if (mem.startsWith(u8, arg, wl_rpath_prefix)) {798 } else if (mem.startsWith(u8, arg, wl_rpath_prefix)) {
799 try zig_cflags.appendSlice(&[_][]const u8{ "-rpath", arg[wl_rpath_prefix.len..] });799 try zig_cflags.appendSlice(&[_][]const u8{ "-rpath", arg[wl_rpath_prefix.len..] });
800 } else if (b.debug_pkg_config) {800 } else if (b.debug_pkg_config) {
801 return compile.step.fail("unknown pkg-config flag '{s}'", .{arg});801 return step.fail("unknown pkg-config flag '{s}'", .{arg});
802 }802 }
803 }803 }
804804
...@@ -1111,7 +1111,7 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {...@@ -1111,7 +1111,7 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
1111 switch (system_lib.use_pkg_config) {1111 switch (system_lib.use_pkg_config) {
1112 .no => try zig_args.append(b.fmt("{s}{s}", .{ prefix, system_lib.name })),1112 .no => try zig_args.append(b.fmt("{s}{s}", .{ prefix, system_lib.name })),
1113 .yes, .force => {1113 .yes, .force => {
1114 if (compile.runPkgConfig(system_lib.name)) |result| {1114 if (runPkgConfig(&compile.step, system_lib.name)) |result| {
1115 try zig_args.appendSlice(result.cflags);1115 try zig_args.appendSlice(result.cflags);
1116 try zig_args.appendSlice(result.libs);1116 try zig_args.appendSlice(result.libs);
1117 try seen_system_libs.put(arena, system_lib.name, result.cflags);1117 try seen_system_libs.put(arena, system_lib.name, result.cflags);
lib/std/Build/Step/TranslateC.zig+110-6
...@@ -11,6 +11,7 @@ pub const base_id: Step.Id = .translate_c;...@@ -11,6 +11,7 @@ pub const base_id: Step.Id = .translate_c;
11step: Step,11step: Step,
12source: std.Build.LazyPath,12source: std.Build.LazyPath,
13include_dirs: std.array_list.Managed(std.Build.Module.IncludeDir),13include_dirs: std.array_list.Managed(std.Build.Module.IncludeDir),
14system_libs: std.ArrayList(std.Build.Module.SystemLib),
14c_macros: std.array_list.Managed([]const u8),15c_macros: std.array_list.Managed([]const u8),
15out_basename: []const u8,16out_basename: []const u8,
16target: std.Build.ResolvedTarget,17target: std.Build.ResolvedTarget,
...@@ -46,6 +47,7 @@ pub fn create(owner: *std.Build, options: Options) *TranslateC {...@@ -46,6 +47,7 @@ pub fn create(owner: *std.Build, options: Options) *TranslateC {
46 .output_file = .{ .step = &translate_c.step },47 .output_file = .{ .step = &translate_c.step },
47 .link_libc = options.link_libc,48 .link_libc = options.link_libc,
48 .use_clang = options.use_clang,49 .use_clang = options.use_clang,
50 .system_libs = .empty,
49 };51 };
50 source.addStepDependencies(&translate_c.step);52 source.addStepDependencies(&translate_c.step);
51 return translate_c;53 return translate_c;
...@@ -67,24 +69,37 @@ pub fn getOutput(translate_c: *TranslateC) std.Build.LazyPath {...@@ -67,24 +69,37 @@ pub fn getOutput(translate_c: *TranslateC) std.Build.LazyPath {
67/// module set making it available to other packages which depend on this one.69/// module set making it available to other packages which depend on this one.
68/// `createModule` can be used instead to create a private module.70/// `createModule` can be used instead to create a private module.
69pub fn addModule(translate_c: *TranslateC, name: []const u8) *std.Build.Module {71pub fn addModule(translate_c: *TranslateC, name: []const u8) *std.Build.Module {
70 return translate_c.step.owner.addModule(name, .{72 return setUpModule(translate_c, translate_c.step.owner.addModule(name, .{
71 .root_source_file = translate_c.getOutput(),73 .root_source_file = translate_c.getOutput(),
72 .target = translate_c.target,74 .target = translate_c.target,
73 .optimize = translate_c.optimize,75 .optimize = translate_c.optimize,
74 .link_libc = translate_c.link_libc,76 .link_libc = translate_c.link_libc,
75 });77 }));
76}78}
7779
78/// Creates a private module from the translated source to be used by the80/// Creates a private module from the translated source to be used by the
79/// current package, but not exposed to other packages depending on this one.81/// current package, but not exposed to other packages depending on this one.
80/// `addModule` can be used instead to create a public module.82/// `addModule` can be used instead to create a public module.
81pub fn createModule(translate_c: *TranslateC) *std.Build.Module {83pub fn createModule(translate_c: *TranslateC) *std.Build.Module {
82 return translate_c.step.owner.createModule(.{84 return setUpModule(translate_c, translate_c.step.owner.createModule(.{
83 .root_source_file = translate_c.getOutput(),85 .root_source_file = translate_c.getOutput(),
84 .target = translate_c.target,86 .target = translate_c.target,
85 .optimize = translate_c.optimize,87 .optimize = translate_c.optimize,
86 .link_libc = translate_c.link_libc,88 .link_libc = translate_c.link_libc,
87 });89 }));
90}
91
92fn setUpModule(translate_c: *TranslateC, module: *std.Build.Module) *std.Build.Module {
93 const b = translate_c.step.owner;
94 const arena = b.graph.arena;
95
96 if (translate_c.link_libc) module.link_libc = true;
97
98 for (translate_c.system_libs.items) |system_lib| {
99 module.link_objects.append(arena, .{ .system_lib = system_lib }) catch @panic("OOM");
100 }
101
102 return module;
88}103}
89104
90pub fn addAfterIncludePath(translate_c: *TranslateC, lazy_path: LazyPath) void {105pub fn addAfterIncludePath(translate_c: *TranslateC, lazy_path: LazyPath) void {
...@@ -152,6 +167,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {...@@ -152,6 +167,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
152 const prog_node = options.progress_node;167 const prog_node = options.progress_node;
153 const b = step.owner;168 const b = step.owner;
154 const translate_c: *TranslateC = @fieldParentPtr("step", step);169 const translate_c: *TranslateC = @fieldParentPtr("step", step);
170 const arena = b.graph.arena;
155171
156 var argv_list = std.array_list.Managed([]const u8).init(b.allocator);172 var argv_list = std.array_list.Managed([]const u8).init(b.allocator);
157 try argv_list.append(b.graph.zig_exe);173 try argv_list.append(b.graph.zig_exe);
...@@ -169,8 +185,6 @@ fn make(step: *Step, options: Step.MakeOptions) !void {...@@ -169,8 +185,6 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
169 try argv_list.append("--global-cache-dir");185 try argv_list.append("--global-cache-dir");
170 try argv_list.append(b.graph.global_cache_root.path orelse ".");186 try argv_list.append(b.graph.global_cache_root.path orelse ".");
171187
172 try argv_list.append("--listen=-");
173
174 if (!translate_c.target.query.isNative()) {188 if (!translate_c.target.query.isNative()) {
175 try argv_list.append("-target");189 try argv_list.append("-target");
176 try argv_list.append(try translate_c.target.query.zigTriple(b.allocator));190 try argv_list.append(try translate_c.target.query.zigTriple(b.allocator));
...@@ -190,12 +204,102 @@ fn make(step: *Step, options: Step.MakeOptions) !void {...@@ -190,12 +204,102 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
190 try argv_list.append(c_macro);204 try argv_list.append(c_macro);
191 }205 }
192206
207 var prev_search_strategy: std.Build.Module.SystemLib.SearchStrategy = .paths_first;
208 var prev_preferred_link_mode: std.builtin.LinkMode = .dynamic;
209
210 for (translate_c.system_libs.items) |*system_lib| {
211 var seen_system_libs: std.StringHashMapUnmanaged([]const []const u8) = .empty;
212 const system_lib_gop = try seen_system_libs.getOrPut(arena, system_lib.name);
213 if (system_lib_gop.found_existing) {
214 try argv_list.appendSlice(system_lib_gop.value_ptr.*);
215 continue;
216 } else {
217 system_lib_gop.value_ptr.* = &.{};
218 }
219
220 if (system_lib.search_strategy != prev_search_strategy or
221 system_lib.preferred_link_mode != prev_preferred_link_mode)
222 {
223 switch (system_lib.search_strategy) {
224 .no_fallback => switch (system_lib.preferred_link_mode) {
225 .dynamic => try argv_list.append("-search_dylibs_only"),
226 .static => try argv_list.append("-search_static_only"),
227 },
228 .paths_first => switch (system_lib.preferred_link_mode) {
229 .dynamic => try argv_list.append("-search_paths_first"),
230 .static => try argv_list.append("-search_paths_first_static"),
231 },
232 .mode_first => switch (system_lib.preferred_link_mode) {
233 .dynamic => try argv_list.append("-search_dylibs_first"),
234 .static => try argv_list.append("-search_static_first"),
235 },
236 }
237 prev_search_strategy = system_lib.search_strategy;
238 prev_preferred_link_mode = system_lib.preferred_link_mode;
239 }
240
241 const prefix: []const u8 = prefix: {
242 if (system_lib.needed) break :prefix "-needed-l";
243 if (system_lib.weak) break :prefix "-weak-l";
244 break :prefix "-l";
245 };
246 switch (system_lib.use_pkg_config) {
247 .no => try argv_list.append(b.fmt("{s}{s}", .{ prefix, system_lib.name })),
248 .yes, .force => {
249 if (Step.Compile.runPkgConfig(&translate_c.step, system_lib.name)) |result| {
250 try argv_list.appendSlice(result.cflags);
251 try argv_list.appendSlice(result.libs);
252 try seen_system_libs.put(arena, system_lib.name, result.cflags);
253 } else |err| switch (err) {
254 error.PkgConfigInvalidOutput,
255 error.PkgConfigCrashed,
256 error.PkgConfigFailed,
257 error.PkgConfigNotInstalled,
258 error.PackageNotFound,
259 => switch (system_lib.use_pkg_config) {
260 .yes => {
261 // pkg-config failed, so fall back to linking the library
262 // by name directly.
263 try argv_list.append(b.fmt("{s}{s}", .{
264 prefix,
265 system_lib.name,
266 }));
267 },
268 .force => {
269 std.debug.panic("pkg-config failed for library {s}", .{system_lib.name});
270 },
271 .no => unreachable,
272 },
273
274 else => |e| return e,
275 }
276 },
277 }
278 }
279
193 const c_source_path = translate_c.source.getPath2(b, step);280 const c_source_path = translate_c.source.getPath2(b, step);
194 try argv_list.append(c_source_path);281 try argv_list.append(c_source_path);
195282
283 try argv_list.append("--listen=-");
196 const output_dir = try step.evalZigProcess(argv_list.items, prog_node, false, options.web_server, options.gpa);284 const output_dir = try step.evalZigProcess(argv_list.items, prog_node, false, options.web_server, options.gpa);
197285
198 const basename = std.fs.path.stem(std.fs.path.basename(c_source_path));286 const basename = std.fs.path.stem(std.fs.path.basename(c_source_path));
199 translate_c.out_basename = b.fmt("{s}.zig", .{basename});287 translate_c.out_basename = b.fmt("{s}.zig", .{basename});
200 translate_c.output_file.path = output_dir.?.joinString(b.allocator, translate_c.out_basename) catch @panic("OOM");288 translate_c.output_file.path = output_dir.?.joinString(b.allocator, translate_c.out_basename) catch @panic("OOM");
201}289}
290
291pub fn linkSystemLibrary(
292 translate_c: *TranslateC,
293 name: []const u8,
294 options: std.Build.Module.LinkSystemLibraryOptions,
295) void {
296 const b = translate_c.step.owner;
297 translate_c.system_libs.append(b.allocator, .{
298 .name = b.dupe(name),
299 .needed = options.needed,
300 .weak = options.weak,
301 .use_pkg_config = options.use_pkg_config,
302 .preferred_link_mode = options.preferred_link_mode,
303 .search_strategy = options.search_strategy,
304 }) catch @panic("OOM");
305}
src/main.zig+4-8
...@@ -4688,9 +4688,8 @@ fn cmdTranslateC(...@@ -4688,9 +4688,8 @@ fn cmdTranslateC(
46884688
4689 man.hash.add(@as(u16, 0xb945)); // Random number to distinguish translate-c from compiling C objects4689 man.hash.add(@as(u16, 0xb945)); // Random number to distinguish translate-c from compiling C objects
4690 man.hash.add(comp.config.c_frontend);4690 man.hash.add(comp.config.c_frontend);
4691 Compilation.cache_helpers.hashCSource(&man, c_source_file) catch |err| {4691 Compilation.cache_helpers.hashCSource(&man, c_source_file) catch |err|
4692 fatal("unable to process '{s}': {s}", .{ c_source_file.src_path, @errorName(err) });4692 fatal("unable to process '{s}': {t}", .{ c_source_file.src_path, err });
4693 };
46944693
4695 const result: Compilation.CImportResult = if (try man.hit()) .{4694 const result: Compilation.CImportResult = if (try man.hit()) .{
4696 .digest = man.finalBin(),4695 .digest = man.finalBin(),
...@@ -4732,11 +4731,8 @@ fn cmdTranslateC(...@@ -4732,11 +4731,8 @@ fn cmdTranslateC(
4732 const out_zig_path = try fs.path.join(arena, &.{ "o", &hex_digest, translated_basename });4731 const out_zig_path = try fs.path.join(arena, &.{ "o", &hex_digest, translated_basename });
4733 const zig_file = comp.dirs.local_cache.handle.openFile(io, out_zig_path, .{}) catch |err| {4732 const zig_file = comp.dirs.local_cache.handle.openFile(io, out_zig_path, .{}) catch |err| {
4734 const path = comp.dirs.local_cache.path orelse ".";4733 const path = comp.dirs.local_cache.path orelse ".";
4735 fatal("unable to open cached translated zig file '{s}{s}{s}': {s}", .{4734 fatal("unable to open cached translated zig file '{s}{s}{s}': {t}", .{
4736 path,4735 path, fs.path.sep_str, out_zig_path, err,
4737 fs.path.sep_str,
4738 out_zig_path,
4739 @errorName(err),
4740 });4736 });
4741 };4737 };
4742 defer zig_file.close(io);4738 defer zig_file.close(io);