Skip to content

Commit 056c951

Browse files
committed
First commit
Signed-off-by: Pablo Alessandro Santos Hugen <phugen@redhat.com>
0 parents  commit 056c951

6 files changed

Lines changed: 234 additions & 0 deletions

File tree

.github/workflows/ci.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: CI
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
branches:
8+
- main
9+
workflow_dispatch:
10+
11+
jobs:
12+
build:
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
zig-version: [master]
17+
os: [ubuntu-latest]
18+
runs-on: ${{ matrix.os }}
19+
steps:
20+
- name: Checkout
21+
uses: actions/checkout@v4
22+
23+
- name: Setup Zig
24+
uses: mlugg/setup-zig@v2
25+
with:
26+
version: ${{ matrix.zig-version }}
27+
28+
- name: Check Formatting
29+
run: zig fmt --ast-check --check .
30+
31+
- name: Build
32+
run: zig build --summary all

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.zig-cache/
2+
zig-cache/
3+
zig-out/
4+
zig-pkg/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
libffi - Copyright (c) 1996-2025 Anthony Green, Red Hat, Inc and others.
2+
See source files for details.
3+
4+
Permission is hereby granted, free of charge, to any person obtaining
5+
a copy of this software and associated documentation files (the
6+
``Software''), to deal in the Software without restriction, including
7+
without limitation the rights to use, copy, modify, merge, publish,
8+
distribute, sublicense, and/or sell copies of the Software, and to
9+
permit persons to whom the Software is furnished to do so, subject to
10+
the following conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# libffi zig
2+
3+
[libffi](https://github.com/libffi/libffi), packaged for the Zig build system.
4+
5+
Supports x86_64, x86, aarch64, and arm on Linux, macOS, and BSDs.
6+
7+
## Using
8+
9+
First, update your `build.zig.zon`:
10+
11+
```
12+
zig fetch --save git+https://github.com/allyourcodebase/libffi.git
13+
```
14+
15+
Then in your `build.zig`:
16+
17+
```zig
18+
const libffi = b.dependency("libffi", .{ .target = target, .optimize = optimize });
19+
exe.linkLibrary(libffi.artifact("ffi"));
20+
```

build.zig

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
const std = @import("std");
2+
const manifest = @import("build.zig.zon");
3+
4+
pub fn build(b: *std.Build) !void {
5+
const target = b.standardTargetOptions(.{});
6+
const optimize = b.standardOptimizeOption(.{});
7+
const linkage = b.option(std.builtin.LinkMode, "linkage", "Library linkage type") orelse .static;
8+
9+
const upstream = b.dependency("upstream", .{});
10+
const src = upstream.path("");
11+
const arch = target.result.cpu.arch;
12+
const os = target.result.os.tag;
13+
const is_linux = os == .linux;
14+
const is_posix = is_linux or os.isBSD();
15+
16+
const arch_dir: []const u8 = switch (arch) {
17+
.x86_64, .x86 => "src/x86",
18+
.aarch64 => "src/aarch64",
19+
.arm => "src/arm",
20+
else => return error.UnsupportedArch,
21+
};
22+
23+
const target_name: []const u8 = switch (arch) {
24+
.x86_64 => "X86_64",
25+
.x86 => "X86",
26+
.aarch64 => "AARCH64",
27+
.arm => "ARM",
28+
else => return error.UnsupportedArch,
29+
};
30+
31+
// ffi.h (autoconf @VARIABLE@ substitution of upstream ffi.h.in)
32+
const ffi_h = b.addConfigHeader(.{
33+
.style = .{ .autoconf_at = upstream.path("include/ffi.h.in") },
34+
.include_path = "ffi.h",
35+
}, .{
36+
.VERSION = manifest.version,
37+
.TARGET = target_name,
38+
.HAVE_LONG_DOUBLE = 1,
39+
.FFI_EXEC_TRAMPOLINE_TABLE = 0,
40+
.FFI_VERSION_STRING = manifest.version,
41+
.FFI_VERSION_NUMBER = 30502,
42+
});
43+
44+
// fficonfig.h (generated via WriteFile for the FFI_HIDDEN macro block)
45+
const config_wf = b.addWriteFiles();
46+
_ = config_wf.add("fficonfig.h", b.fmt(
47+
\\#ifndef LIBFFI_CONFIG_H
48+
\\#define LIBFFI_CONFIG_H
49+
\\
50+
\\{s}
51+
\\{s}
52+
\\{s}
53+
\\{s}
54+
\\{s}
55+
\\{s}
56+
\\{s}
57+
\\{s}
58+
\\{s}
59+
\\{s}
60+
\\{s}
61+
\\{s}
62+
\\{s}
63+
\\
64+
\\#ifdef HAVE_HIDDEN_VISIBILITY_ATTRIBUTE
65+
\\#ifdef LIBFFI_ASM
66+
\\#ifdef __APPLE__
67+
\\#define FFI_HIDDEN(name) .private_extern name
68+
\\#else
69+
\\#define FFI_HIDDEN(name) .hidden name
70+
\\#endif
71+
\\#else
72+
\\#define FFI_HIDDEN __attribute__ ((visibility ("hidden")))
73+
\\#endif
74+
\\#else
75+
\\#ifdef LIBFFI_ASM
76+
\\#define FFI_HIDDEN(name)
77+
\\#else
78+
\\#define FFI_HIDDEN
79+
\\#endif
80+
\\#endif
81+
\\
82+
\\#endif
83+
\\
84+
, .{
85+
"#define HAVE_LONG_DOUBLE 1",
86+
"#define STDC_HEADERS 1",
87+
"#define HAVE_ALLOCA_H 1",
88+
"#define HAVE_INTTYPES_H 1",
89+
"#define HAVE_STDINT_H 1",
90+
"#define HAVE_STRING_H 1",
91+
if (is_posix) "#define HAVE_HIDDEN_VISIBILITY_ATTRIBUTE 1" else "",
92+
if (is_posix) "#define HAVE_MMAP 1" else "",
93+
if (is_posix) "#define HAVE_MPROTECT 1" else "",
94+
if (is_linux) "#define HAVE_MEMFD_CREATE 1" else "",
95+
if (is_posix) "#define FFI_MMAP_EXEC_WRIT 1" else "",
96+
if (is_linux) "#define FFI_EXEC_STATIC_TRAMP 1" else "",
97+
if (arch == .x86_64 or arch == .x86) "#define HAVE_AS_X86_PCREL 1" else "",
98+
}));
99+
100+
// Module
101+
const mod = b.createModule(.{ .target = target, .optimize = optimize, .link_libc = true });
102+
mod.addConfigHeader(ffi_h);
103+
mod.addIncludePath(config_wf.getDirectory());
104+
mod.addIncludePath(src.path(b, "include"));
105+
mod.addIncludePath(src.path(b, arch_dir));
106+
107+
const flags: []const []const u8 = &.{"-fvisibility=hidden"};
108+
109+
mod.addCSourceFiles(.{ .root = src, .flags = flags, .files = &.{
110+
"src/prep_cif.c",
111+
"src/types.c",
112+
"src/raw_api.c",
113+
"src/java_raw_api.c",
114+
"src/closures.c",
115+
"src/tramp.c",
116+
} });
117+
118+
switch (arch) {
119+
.x86_64 => {
120+
mod.addCSourceFiles(.{ .root = src, .flags = flags, .files = &.{ "src/x86/ffi64.c", "src/x86/ffiw64.c" } });
121+
mod.addAssemblyFile(src.path(b, "src/x86/unix64.S"));
122+
mod.addAssemblyFile(src.path(b, "src/x86/win64.S"));
123+
},
124+
.x86 => {
125+
mod.addCSourceFiles(.{ .root = src, .flags = flags, .files = &.{"src/x86/ffi.c"} });
126+
mod.addAssemblyFile(src.path(b, "src/x86/sysv.S"));
127+
},
128+
.aarch64 => {
129+
mod.addCSourceFiles(.{ .root = src, .flags = flags, .files = &.{"src/aarch64/ffi.c"} });
130+
mod.addAssemblyFile(src.path(b, "src/aarch64/sysv.S"));
131+
},
132+
.arm => {
133+
mod.addCSourceFiles(.{ .root = src, .flags = flags, .files = &.{"src/arm/ffi.c"} });
134+
mod.addAssemblyFile(src.path(b, "src/arm/sysv.S"));
135+
},
136+
else => return error.UnsupportedArch,
137+
}
138+
139+
// Library
140+
const lib = b.addLibrary(.{ .name = "ffi", .root_module = mod, .linkage = linkage });
141+
lib.installConfigHeader(ffi_h);
142+
lib.installHeader(src.path(b, b.pathJoin(&.{ arch_dir, "ffitarget.h" })), "ffitarget.h");
143+
b.installArtifact(lib);
144+
}

build.zig.zon

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.{
2+
.name = .libffi,
3+
.version = "3.5.2",
4+
.dependencies = .{
5+
.upstream = .{
6+
.url = "https://github.com/libffi/libffi/releases/download/v3.5.2/libffi-3.5.2.tar.gz",
7+
.hash = "N-V-__8AAFDiVQA3Q01T64sEaf0mSRyePtaGEqV9gZ37pSti",
8+
},
9+
},
10+
.minimum_zig_version = "0.16.0-dev.2653+784e89fd4",
11+
.paths = .{ "build.zig", "build.zig.zon" },
12+
.fingerprint = 0x806ae67927da1142,
13+
}

0 commit comments

Comments
 (0)