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 {
702702
703703/// Run pkg-config for the given library name and parse the output, returning the arguments
704704/// 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 {
706706 const wl_rpath_prefix = "-Wl,-rpath,";
707707
708 const b = compile.step.owner;
708 const b = step.owner;
709709 const pkg_name = match: {
710710 // First we have to map the library name to pkg config name. Unfortunately,
711711 // there are several examples where this is not straightforward:
......@@ -798,7 +798,7 @@ fn runPkgConfig(compile: *Compile, lib_name: []const u8) !PkgConfigResult {
798798 } else if (mem.startsWith(u8, arg, wl_rpath_prefix)) {
799799 try zig_cflags.appendSlice(&[_][]const u8{ "-rpath", arg[wl_rpath_prefix.len..] });
800800 } 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});
802802 }
803803 }
804804
......@@ -1111,7 +1111,7 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
11111111 switch (system_lib.use_pkg_config) {
11121112 .no => try zig_args.append(b.fmt("{s}{s}", .{ prefix, system_lib.name })),
11131113 .yes, .force => {
1114 if (compile.runPkgConfig(system_lib.name)) |result| {
1114 if (runPkgConfig(&compile.step, system_lib.name)) |result| {
11151115 try zig_args.appendSlice(result.cflags);
11161116 try zig_args.appendSlice(result.libs);
11171117 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;
1111step: Step,
1212source: std.Build.LazyPath,
1313include_dirs: std.array_list.Managed(std.Build.Module.IncludeDir),
14system_libs: std.ArrayList(std.Build.Module.SystemLib),
1415c_macros: std.array_list.Managed([]const u8),
1516out_basename: []const u8,
1617target: std.Build.ResolvedTarget,
......@@ -46,6 +47,7 @@ pub fn create(owner: *std.Build, options: Options) *TranslateC {
4647 .output_file = .{ .step = &translate_c.step },
4748 .link_libc = options.link_libc,
4849 .use_clang = options.use_clang,
50 .system_libs = .empty,
4951 };
5052 source.addStepDependencies(&translate_c.step);
5153 return translate_c;
......@@ -67,24 +69,37 @@ pub fn getOutput(translate_c: *TranslateC) std.Build.LazyPath {
6769/// module set making it available to other packages which depend on this one.
6870/// `createModule` can be used instead to create a private module.
6971pub 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, .{
7173 .root_source_file = translate_c.getOutput(),
7274 .target = translate_c.target,
7375 .optimize = translate_c.optimize,
7476 .link_libc = translate_c.link_libc,
75 });
77 }));
7678}
7779
7880/// Creates a private module from the translated source to be used by the
7981/// current package, but not exposed to other packages depending on this one.
8082/// `addModule` can be used instead to create a public module.
8183pub 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(.{
8385 .root_source_file = translate_c.getOutput(),
8486 .target = translate_c.target,
8587 .optimize = translate_c.optimize,
8688 .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;
88103}
89104
90105pub fn addAfterIncludePath(translate_c: *TranslateC, lazy_path: LazyPath) void {
......@@ -152,6 +167,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
152167 const prog_node = options.progress_node;
153168 const b = step.owner;
154169 const translate_c: *TranslateC = @fieldParentPtr("step", step);
170 const arena = b.graph.arena;
155171
156172 var argv_list = std.array_list.Managed([]const u8).init(b.allocator);
157173 try argv_list.append(b.graph.zig_exe);
......@@ -169,8 +185,6 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
169185 try argv_list.append("--global-cache-dir");
170186 try argv_list.append(b.graph.global_cache_root.path orelse ".");
171187
172 try argv_list.append("--listen=-");
173
174188 if (!translate_c.target.query.isNative()) {
175189 try argv_list.append("-target");
176190 try argv_list.append(try translate_c.target.query.zigTriple(b.allocator));
......@@ -190,12 +204,102 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
190204 try argv_list.append(c_macro);
191205 }
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
193280 const c_source_path = translate_c.source.getPath2(b, step);
194281 try argv_list.append(c_source_path);
195282
283 try argv_list.append("--listen=-");
196284 const output_dir = try step.evalZigProcess(argv_list.items, prog_node, false, options.web_server, options.gpa);
197285
198286 const basename = std.fs.path.stem(std.fs.path.basename(c_source_path));
199287 translate_c.out_basename = b.fmt("{s}.zig", .{basename});
200288 translate_c.output_file.path = output_dir.?.joinString(b.allocator, translate_c.out_basename) catch @panic("OOM");
201289}
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(
46884688
46894689 man.hash.add(@as(u16, 0xb945)); // Random number to distinguish translate-c from compiling C objects
46904690 man.hash.add(comp.config.c_frontend);
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) });
4693 };
4691 Compilation.cache_helpers.hashCSource(&man, c_source_file) catch |err|
4692 fatal("unable to process '{s}': {t}", .{ c_source_file.src_path, err });
46944693
46954694 const result: Compilation.CImportResult = if (try man.hit()) .{
46964695 .digest = man.finalBin(),
......@@ -4732,11 +4731,8 @@ fn cmdTranslateC(
47324731 const out_zig_path = try fs.path.join(arena, &.{ "o", &hex_digest, translated_basename });
47334732 const zig_file = comp.dirs.local_cache.handle.openFile(io, out_zig_path, .{}) catch |err| {
47344733 const path = comp.dirs.local_cache.path orelse ".";
4735 fatal("unable to open cached translated zig file '{s}{s}{s}': {s}", .{
4736 path,
4737 fs.path.sep_str,
4738 out_zig_path,
4739 @errorName(err),
4734 fatal("unable to open cached translated zig file '{s}{s}{s}': {t}", .{
4735 path, fs.path.sep_str, out_zig_path, err,
47404736 });
47414737 };
47424738 defer zig_file.close(io);