|
| 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 | +} |
0 commit comments