This commit is contained in:
agra
2026-02-22 17:24:04 +02:00
parent 775dcb44cc
commit d3e574eae5
38 changed files with 16135 additions and 33 deletions

View File

@@ -20,12 +20,26 @@ pub fn build(b: *std.Build) void {
});
mod.addSystemIncludePath(.{ .cwd_relative = include_dir });
mod.addSystemIncludePath(.{ .cwd_relative = "." }); // for clang_shim.h
mod.addLibraryPath(.{ .cwd_relative = lib_dir });
mod.link_libc = true;
mod.addCSourceFile(.{
.file = b.path("llvm_shim.c"),
.flags = &.{b.fmt("-I{s}", .{include_dir})},
});
mod.addCSourceFile(.{
.file = b.path("clang_shim.cpp"),
.flags = &.{
b.fmt("-I{s}", .{include_dir}),
"-std=c++17",
"-fno-rtti",
"-fno-exceptions",
"-D__STDC_CONSTANT_MACROS",
"-D__STDC_FORMAT_MACROS",
"-D__STDC_LIMIT_MACROS",
b.fmt("-DSX_LLVM_PREFIX=\"{s}\"", .{llvm_prefix}),
},
});
const target_os = target.result.os.tag;
@@ -68,12 +82,39 @@ pub fn build(b: *std.Build) void {
}
}
// System libraries LLVM depends on (zlib, zstd, curses, etc.)
// Clang static libraries (for clang_shim: header parsing + C compilation)
const clang_libs_raw = std.mem.trim(u8, b.run(&.{ "sh", "-c", b.fmt("ls {s}/lib/libclang*.a | xargs -n1 basename | sed 's/^lib//;s/\\.a$//'", .{llvm_prefix}) }), " \t\n\r");
var clang_libs_it = std.mem.tokenizeAny(u8, clang_libs_raw, "\n");
while (clang_libs_it.next()) |lib_name| {
const trimmed = std.mem.trim(u8, lib_name, " \t\r");
if (trimmed.len > 0) {
mod.linkSystemLibrary(trimmed, .{ .preferred_link_mode = .static });
}
}
// System libraries LLVM depends on — link statically where possible.
// Add homebrew lib paths for static archives.
if (builtin.os.tag == .macos) {
const homebrew_static_paths = [_][]const u8{
"/opt/homebrew/opt/zlib/lib",
"/opt/homebrew/opt/zstd/lib",
"/opt/homebrew/opt/ncurses/lib",
};
for (&homebrew_static_paths) |p| {
mod.addLibraryPath(.{ .cwd_relative = p });
}
}
const syslibs_raw = std.mem.trim(u8, b.run(&.{ llvm_config, "--system-libs", "--link-static" }), " \t\n\r");
var syslibs_it = std.mem.tokenizeAny(u8, syslibs_raw, " \t\n\r");
while (syslibs_it.next()) |flag| {
if (flag.len > 2 and std.mem.startsWith(u8, flag, "-l")) {
mod.linkSystemLibrary(flag[2..], .{});
const name = flag[2..];
// Skip xml2 — only used by LLVM's Windows manifest parser (not needed)
if (std.mem.eql(u8, name, "xml2")) continue;
// Skip m — part of libSystem on macOS, libc on Linux
if (std.mem.eql(u8, name, "m")) continue;
mod.linkSystemLibrary(name, .{ .preferred_link_mode = .static });
}
}
@@ -96,6 +137,11 @@ pub fn build(b: *std.Build) void {
}
} else {
mod.linkSystemLibrary("LLVM-18", .{});
mod.linkSystemLibrary("clang-cpp", .{});
// clang-cpp is C++ — need libc++ on macOS
if (target_os != .windows and target_os != .linux) {
mod.link_libcpp = true;
}
}
const exe = b.addExecutable(.{