Skip to content

Commit

Permalink
Integrate libffi in the build.
Browse files Browse the repository at this point in the history
Closes #19.
  • Loading branch information
alexrp committed Jun 21, 2024
1 parent cbd3852 commit 5a378f1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
32 changes: 30 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub fn build(b: *std.Build) anyerror!void {
const target_opt = b.standardTargetOptions(.{});
const optimize_opt = b.standardOptimizeOption(.{});
const disable_aro_opt = b.option(bool, "disable-aro", "Disable Aro C compiler integration") orelse false;
const disable_ffi_opt = b.option(bool, "disable-ffi", "Disable libffi interpreter integration") orelse false;

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

const target = target_opt.result;

const npm_install_doc_step = b.addSystemCommand(&.{ "npm", "install" });
npm_install_doc_step.setName("npm install");

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

// TODO: libffi should be a lazy dependency, but this causes HTTP problems on macOS.
const ffi_dep = b.dependency("ffi", .{
.target = target_opt,
.optimize = optimize_opt,
});

if (!disable_ffi_opt) {
if (b.systemIntegrationOption("ffi", .{})) {
graf_mod.linkSystemLibrary("ffi", .{});
} else if (switch (target.cpu.arch) {
// libffi only supports MSVC for Windows on Arm.
.aarch64, .aarch64_be, .aarch64_32 => target.os.tag != .windows,
// TODO: https://github.com/ziglang/zig/issues/10411
.arm, .armeb => target.getFloatAbi() != .soft and target.os.tag != .windows,
// TODO: https://github.com/llvm/llvm-project/issues/58377
.mips, .mipsel, .mips64, .mips64el => false,
// TODO: https://github.com/ziglang/zig/issues/19107
.riscv32, .riscv64 => false,
// TODO: https://github.com/ziglang/zig/issues/20361
else => !target.isDarwin(),
}) {
graf_mod.linkLibrary(ffi_dep.artifact("ffi"));
}
}

b.installDirectory(.{
.source_dir = aro_dep.path("include"),
.install_dir = .header,
Expand All @@ -121,7 +149,7 @@ pub fn build(b: *std.Build) anyerror!void {

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

// PIE is off by default; enable it for hardening purposes.
exe_step.pie = switch (target_opt.result.cpu.arch) {
exe_step.pie = switch (target.cpu.arch) {
// TODO: https://github.com/ziglang/zig/issues/20305
.mips, .mipsel, .mips64, .mips64el => false,
.powerpc, .powerpcle, .powerpc64, .powerpc64le => false,
Expand Down
4 changes: 4 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
.url = "https://github.com/Hejsil/zig-clap/archive/7a51c11319b3892b81784149fb2cb2915073710c.tar.gz",
.hash = "1220c900c70daf3e715fad6f266ec14b1d0f5e6c2d3f34b32142f60306cb9b5e5f05",
},
.ffi = .{
.url = "https://github.com/alexrp/libffi/archive/be74524b614693f612fa0c3951d08bb70f4bb4ea.tar.gz",
.hash = "12206c26416ba568c5b932411d21d908e0afea670687245cc568a235dc5a9d028396",
},
.mecha = .{
.url = "https://github.com/Hejsil/mecha/archive/2edc8ad6b5bb1728f4c6fae358438006b0ce04ee.tar.gz",
.hash = "12206311fbaae8a0adc35e4835ddd9bcc1ff8fff756c9df40e19133f007fa05d3948",
Expand Down

0 comments on commit 5a378f1

Please sign in to comment.