Skip to content
This repository was archived by the owner on May 16, 2025. It is now read-only.

Commit 5a378f1

Browse files
committed
Integrate libffi in the build.
Closes #19.
1 parent cbd3852 commit 5a378f1

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

build.zig

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub fn build(b: *std.Build) anyerror!void {
99
const target_opt = b.standardTargetOptions(.{});
1010
const optimize_opt = b.standardOptimizeOption(.{});
1111
const disable_aro_opt = b.option(bool, "disable-aro", "Disable Aro C compiler integration") orelse false;
12+
const disable_ffi_opt = b.option(bool, "disable-ffi", "Disable libffi interpreter integration") orelse false;
1213

1314
// TODO: https://github.com/ziglang/zig/issues/15373
1415
const pandoc_prog = b.findProgram(&.{"pandoc"}, &.{}) catch @panic("Could not locate `pandoc` program.");
@@ -21,6 +22,8 @@ pub fn build(b: *std.Build) anyerror!void {
2122
const install_vscode_step = b.step("install-vscode", "Install VS Code extension");
2223
const uninstall_vscode_step = b.step("uninstall-vscode", "Uninstall VS Code extension");
2324

25+
const target = target_opt.result;
26+
2427
const npm_install_doc_step = b.addSystemCommand(&.{ "npm", "install" });
2528
npm_install_doc_step.setName("npm install");
2629

@@ -111,6 +114,31 @@ pub fn build(b: *std.Build) anyerror!void {
111114
graf_mod.addImport("aro", aro_dep.module("aro"));
112115
}
113116

117+
// TODO: libffi should be a lazy dependency, but this causes HTTP problems on macOS.
118+
const ffi_dep = b.dependency("ffi", .{
119+
.target = target_opt,
120+
.optimize = optimize_opt,
121+
});
122+
123+
if (!disable_ffi_opt) {
124+
if (b.systemIntegrationOption("ffi", .{})) {
125+
graf_mod.linkSystemLibrary("ffi", .{});
126+
} else if (switch (target.cpu.arch) {
127+
// libffi only supports MSVC for Windows on Arm.
128+
.aarch64, .aarch64_be, .aarch64_32 => target.os.tag != .windows,
129+
// TODO: https://github.com/ziglang/zig/issues/10411
130+
.arm, .armeb => target.getFloatAbi() != .soft and target.os.tag != .windows,
131+
// TODO: https://github.com/llvm/llvm-project/issues/58377
132+
.mips, .mipsel, .mips64, .mips64el => false,
133+
// TODO: https://github.com/ziglang/zig/issues/19107
134+
.riscv32, .riscv64 => false,
135+
// TODO: https://github.com/ziglang/zig/issues/20361
136+
else => !target.isDarwin(),
137+
}) {
138+
graf_mod.linkLibrary(ffi_dep.artifact("ffi"));
139+
}
140+
}
141+
114142
b.installDirectory(.{
115143
.source_dir = aro_dep.path("include"),
116144
.install_dir = .header,
@@ -121,7 +149,7 @@ pub fn build(b: *std.Build) anyerror!void {
121149

122150
const stlib_step = b.addStaticLibrary(.{
123151
// Avoid name clash with the DLL import library on Windows.
124-
.name = if (target_opt.result.os.tag == .windows) "libgraf" else "graf",
152+
.name = if (target.os.tag == .windows) "libgraf" else "graf",
125153
.root_source_file = b.path(b.pathJoin(&.{ "lib", "c.zig" })),
126154
.target = target_opt,
127155
.optimize = optimize_opt,
@@ -187,7 +215,7 @@ pub fn build(b: *std.Build) anyerror!void {
187215
});
188216

189217
// PIE is off by default; enable it for hardening purposes.
190-
exe_step.pie = switch (target_opt.result.cpu.arch) {
218+
exe_step.pie = switch (target.cpu.arch) {
191219
// TODO: https://github.com/ziglang/zig/issues/20305
192220
.mips, .mipsel, .mips64, .mips64el => false,
193221
.powerpc, .powerpcle, .powerpc64, .powerpc64le => false,

build.zig.zon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
.url = "https://github.com/Hejsil/zig-clap/archive/7a51c11319b3892b81784149fb2cb2915073710c.tar.gz",
1515
.hash = "1220c900c70daf3e715fad6f266ec14b1d0f5e6c2d3f34b32142f60306cb9b5e5f05",
1616
},
17+
.ffi = .{
18+
.url = "https://github.com/alexrp/libffi/archive/be74524b614693f612fa0c3951d08bb70f4bb4ea.tar.gz",
19+
.hash = "12206c26416ba568c5b932411d21d908e0afea670687245cc568a235dc5a9d028396",
20+
},
1721
.mecha = .{
1822
.url = "https://github.com/Hejsil/mecha/archive/2edc8ad6b5bb1728f4c6fae358438006b0ce04ee.tar.gz",
1923
.hash = "12206311fbaae8a0adc35e4835ddd9bcc1ff8fff756c9df40e19133f007fa05d3948",

0 commit comments

Comments
 (0)