From 33eff94456ca1b5fbde966b7ffce4a3e83aa1213 Mon Sep 17 00:00:00 2001 From: AnErrupTion Date: Sat, 25 Apr 2026 14:21:03 +0200 Subject: [PATCH] Migrate away from @cImport() Signed-off-by: AnErrupTion --- ly-core/build.zig | 41 +++++++++++++++++++++++++++++++++ ly-core/build.zig.zon | 4 ++++ ly-core/src/interop.zig | 50 ++++++++--------------------------------- 3 files changed, 54 insertions(+), 41 deletions(-) diff --git a/ly-core/build.zig b/ly-core/build.zig index 0671528..614404e 100644 --- a/ly-core/build.zig +++ b/ly-core/build.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const Translator = @import("translate_c").Translator; pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); @@ -12,6 +13,29 @@ pub fn build(b: *std.Build) void { const zigini = b.dependency("zigini", .{ .target = target, .optimize = optimize }); mod.addImport("zigini", zigini.module("zigini")); + const translate_c = b.dependency("translate_c", .{ + .target = target, + .optimize = optimize, + }); + + addCImport(b, mod, translate_c, target, optimize, "pam", "#include "); + addCImport(b, mod, translate_c, target, optimize, "utmp", "#include "); + addCImport(b, mod, translate_c, target, optimize, "xcb", "#include "); + if (target.result.os.tag == .freebsd) { + addCImport(b, mod, translate_c, target, optimize, "pwd", + \\#include + \\#include + \\#include + ); + } else { + addCImport(b, mod, translate_c, target, optimize, "pwd", "#include "); + } + addCImport(b, mod, translate_c, target, optimize, "stdlib", "#include "); + addCImport(b, mod, translate_c, target, optimize, "unistd", "#include "); + addCImport(b, mod, translate_c, target, optimize, "grp", "#include "); + addCImport(b, mod, translate_c, target, optimize, "system_time", "#include "); + addCImport(b, mod, translate_c, target, optimize, "time", "#include "); + const mod_tests = b.addTest(.{ .root_module = mod, }); @@ -20,3 +44,20 @@ pub fn build(b: *std.Build) void { const test_step = b.step("test", "Run tests"); test_step.dependOn(&run_mod_tests.step); } + +fn addCImport( + b: *std.Build, + mod: *std.Build.Module, + translate_c: *std.Build.Dependency, + target: std.Build.ResolvedTarget, + optimize: std.builtin.OptimizeMode, + comptime name: []const u8, + comptime bytes: []const u8, +) void { + const pam: Translator = .init(translate_c, .{ + .c_source_file = b.addWriteFiles().add(name ++ ".h", bytes), + .target = target, + .optimize = optimize, + }); + mod.addImport(name, pam.mod); +} diff --git a/ly-core/build.zig.zon b/ly-core/build.zig.zon index 55950da..a389405 100644 --- a/ly-core/build.zig.zon +++ b/ly-core/build.zig.zon @@ -8,6 +8,10 @@ .url = "git+https://github.com/AshAmetrine/zigini?ref=master#a665d081dda42664a96da2840ea09c5ccf9d0692", .hash = "zigini-0.5.0-BSkB7e9WAACfyCBABNZiWL3gFMw18GKn3qBcPs8L1Ec1", }, + .translate_c = .{ + .url = "git+https://codeberg.org/ziglang/translate-c#7a1a9fdc4ab00835748a6657ecbb835e3d5d45f7", + .hash = "translate_c-0.0.0-Q_BUWvP1BgCjAk6PWv5286tOlvzD9-X-NkuTzh0KxY0Q", + }, }, .paths = .{ "build.zig", diff --git a/ly-core/src/interop.zig b/ly-core/src/interop.zig index 6a9a6de..c580a49 100644 --- a/ly-core/src/interop.zig +++ b/ly-core/src/interop.zig @@ -1,49 +1,17 @@ const std = @import("std"); const builtin = @import("builtin"); const UidRange = @import("UidRange.zig"); +const pwd = @import("pwd"); +const stdlib = @import("stdlib"); +const unistd = @import("unistd"); +const grp = @import("grp"); +const system_time = @import("system_time"); +const time = @import("time"); -pub const pam = @cImport({ - @cInclude("security/pam_appl.h"); -}); - -pub const utmp = @cImport({ - @cInclude("utmpx.h"); -}); - +pub const pam = @import("pam"); +pub const utmp = @import("utmp"); // Exists for X11 support only -pub const xcb = @cImport({ - @cInclude("xcb/xcb.h"); -}); - -const pwd = @cImport({ - @cInclude("pwd.h"); - // We include a FreeBSD-specific header here since login_cap.h references - // the passwd struct directly, so we can't import it separately - if (builtin.os.tag == .freebsd) { - @cInclude("sys/types.h"); - @cInclude("login_cap.h"); - } -}); - -const stdlib = @cImport({ - @cInclude("stdlib.h"); -}); - -const unistd = @cImport({ - @cInclude("unistd.h"); -}); - -const grp = @cImport({ - @cInclude("grp.h"); -}); - -const system_time = @cImport({ - @cInclude("sys/time.h"); -}); - -const time = @cImport({ - @cInclude("time.h"); -}); +pub const xcb = @import("xcb"); pub const TimeOfDay = struct { seconds: i64,