11const std = @import ("std" );
2+ const LinkMode = std .builtin .LinkMode ;
3+
24const manifest = @import ("build.zig.zon" );
35
46pub fn build (b : * std.Build ) ! void {
57 const target = b .standardTargetOptions (.{});
68 const optimize = b .standardOptimizeOption (.{});
7- const linkage = b .option (std .builtin .LinkMode , "linkage" , "Library linkage type" ) orelse .static ;
89
9- const upstream = b .dependency ("upstream" , .{});
10- const src = upstream .path ("" );
1110 const arch = target .result .cpu .arch ;
1211 const os = target .result .os .tag ;
13- const is_linux = os == .linux ;
14- const is_posix = is_linux or os .isBSD ();
1512
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 ,
13+ const options = .{
14+ .linkage = b .option (LinkMode , "linkage" , "Library linkage type" ) orelse
15+ .static ,
2116 };
2217
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 ,
18+ const upstream = b .dependency ("libffi_c" , .{});
19+ const src = upstream .path ("" );
20+
21+ const arch_dir , const target_name , const arch_srcs : []const []const u8 , const arch_asm : []const []const u8 = switch (arch ) {
22+ .x86_64 = > .{ "src/x86" , "X86_64" , &.{ "src/x86/ffi64.c" , "src/x86/ffiw64.c" }, if (os == .windows ) &.{"src/x86/win64.S" } else &.{ "src/x86/unix64.S" , "src/x86/win64.S" } },
23+ .x86 = > .{ "src/x86" , "X86" , &.{"src/x86/ffi.c" }, &.{"src/x86/sysv.S" } },
24+ .aarch64 = > .{ "src/aarch64" , "AARCH64" , &.{"src/aarch64/ffi.c" }, &.{"src/aarch64/sysv.S" } },
25+ .arm = > .{ "src/arm" , "ARM" , &.{"src/arm/ffi.c" }, &.{"src/arm/sysv.S" } },
26+ else = > return ,
2927 };
3028
31- // ffi.h (autoconf @VARIABLE@ substitution of upstream ffi.h.in)
3229 const ffi_h = b .addConfigHeader (.{
3330 .style = .{ .autoconf_at = upstream .path ("include/ffi.h.in" ) },
3431 .include_path = "ffi.h" ,
3532 }, .{
3633 .VERSION = manifest .version ,
3734 .TARGET = target_name ,
3835 .HAVE_LONG_DOUBLE = 1 ,
39- .FFI_EXEC_TRAMPOLINE_TABLE = 0 ,
36+ .FFI_EXEC_TRAMPOLINE_TABLE = @as ( i64 , if ( os == .macos and arch == .aarch64 ) 1 else 0 ) ,
4037 .FFI_VERSION_STRING = manifest .version ,
4138 .FFI_VERSION_NUMBER = 30502 ,
4239 });
4340
44- // fficonfig.h (generated via WriteFile for the FFI_HIDDEN macro block)
4541 const config_wf = b .addWriteFiles ();
4642 _ = config_wf .add ("fficonfig.h" , b .fmt (
4743 \\#ifndef LIBFFI_CONFIG_H
4844 \\#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- \\
45+ \\#define HAVE_LONG_DOUBLE 1
46+ \\#define STDC_HEADERS 1
47+ \\#define HAVE_INTTYPES_H 1
48+ \\#define HAVE_STDINT_H 1
49+ \\#define HAVE_STRING_H 1
50+ \\{s}{s}{s}{s}{s}{s}
6451 \\#ifdef HAVE_HIDDEN_VISIBILITY_ATTRIBUTE
6552 \\#ifdef LIBFFI_ASM
6653 \\#ifdef __APPLE__
@@ -78,67 +65,40 @@ pub fn build(b: *std.Build) !void {
7865 \\#define FFI_HIDDEN
7966 \\#endif
8067 \\#endif
81- \\
8268 \\#endif
8369 \\
8470 , .{
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 "" ,
71+ if (os == .linux or os == .macos ) "#define HAVE_ALLOCA_H 1\n " else "" ,
72+ if (os != .linux and os != .windows ) "#define HAVE_HIDDEN_VISIBILITY_ATTRIBUTE 1\n " else "" ,
73+ if (os != .linux ) "#define HAVE_MMAP 1\n #define HAVE_MPROTECT 1\n #define FFI_MMAP_EXEC_WRIT 1\n " else "" ,
74+ if (os == .linux ) "#define HAVE_MEMFD_CREATE 1\n #define HAVE_SYS_MEMFD_H 1\n " else "" ,
75+ if (os == .linux ) "#define FFI_EXEC_STATIC_TRAMP 1\n " else "" ,
76+ if (arch == .x86_64 or arch == .x86 ) "#define HAVE_AS_X86_PCREL 1\n " else "" ,
9877 }));
9978
100- // Module
79+ const flags : []const []const u8 = &.{if (os != .windows ) "-fvisibility=hidden" else "" };
80+
10181 const mod = b .createModule (.{ .target = target , .optimize = optimize , .link_libc = true });
10282 mod .addConfigHeader (ffi_h );
10383 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- } });
84+ mod .addIncludePath (upstream .path ("include" ));
85+ mod .addIncludePath (upstream .path (arch_dir ));
86+ mod .addCSourceFiles (.{ .root = src , .flags = flags , .files = srcs });
87+ mod .addCSourceFiles (.{ .root = src , .flags = flags , .files = arch_srcs });
88+ for (arch_asm ) | asm_file | mod .addAssemblyFile (src .path (b , asm_file ));
11789
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 });
90+ const lib = b .addLibrary (.{
91+ .name = "ffi" ,
92+ .root_module = mod ,
93+ .linkage = options .linkage ,
94+ .version = try .parse (manifest .version ),
95+ });
14196 lib .installConfigHeader (ffi_h );
142- lib .installHeader (src .path (b , b .pathJoin (&.{ arch_dir , "ffitarget.h" })), "ffitarget.h" );
97+ lib .installHeader (upstream .path (b .pathJoin (&.{ arch_dir , "ffitarget.h" })), "ffitarget.h" );
14398 b .installArtifact (lib );
14499}
100+
101+ const srcs : []const []const u8 = &.{
102+ "src/prep_cif.c" , "src/types.c" , "src/raw_api.c" ,
103+ "src/java_raw_api.c" , "src/closures.c" , "src/tramp.c" ,
104+ };
0 commit comments