91 lines
3.1 KiB
Zig
91 lines
3.1 KiB
Zig
const std = @import("std");
|
|
const math = @import("math");
|
|
|
|
pub fn build(b: *std.Build) void {
|
|
const target = b.standardTargetOptions(.{});
|
|
const optimize = b.standardOptimizeOption(.{});
|
|
|
|
const static_llvm = b.option(bool, "static-llvm", "Statically link LLVM (self-contained binary, no LLVM needed at runtime)") orelse false;
|
|
const llvm_prefix = b.option([]const u8, "llvm-prefix", "Path to LLVM installation") orelse "/opt/homebrew/opt/llvm@18";
|
|
|
|
const include_dir = b.fmt("{s}/include", .{llvm_prefix});
|
|
const lib_dir = b.fmt("{s}/lib", .{llvm_prefix});
|
|
const llvm_config = b.fmt("{s}/bin/llvm-config", .{llvm_prefix});
|
|
|
|
const mod = b.addModule("sx", .{
|
|
.root_source_file = b.path("src/root.zig"),
|
|
.target = target,
|
|
.optimize = optimize,
|
|
});
|
|
|
|
mod.addSystemIncludePath(.{ .cwd_relative = include_dir });
|
|
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})},
|
|
});
|
|
|
|
if (static_llvm) {
|
|
// Query llvm-config for the static libraries needed
|
|
const libs_raw = std.mem.trim(u8, b.run(&.{ llvm_config, "--libs", "--link-static" }), " \t\n\r");
|
|
var libs_it = std.mem.tokenizeAny(u8, libs_raw, " \t\n\r");
|
|
while (libs_it.next()) |flag| {
|
|
if (flag.len > 2 and std.mem.startsWith(u8, flag, "-l")) {
|
|
mod.linkSystemLibrary(flag[2..], .{ .preferred_link_mode = .static });
|
|
}
|
|
}
|
|
|
|
// System libraries LLVM depends on (zlib, zstd, curses, etc.)
|
|
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..], .{});
|
|
}
|
|
}
|
|
|
|
// LLVM is C++ — link the C++ standard library
|
|
mod.link_libcpp = true;
|
|
} else {
|
|
mod.linkSystemLibrary("LLVM-18", .{});
|
|
}
|
|
|
|
const exe = b.addExecutable(.{
|
|
.name = "sx",
|
|
.root_module = b.createModule(.{
|
|
.root_source_file = b.path("src/main.zig"),
|
|
.target = target,
|
|
.optimize = optimize,
|
|
.imports = &.{
|
|
.{ .name = "sx", .module = mod },
|
|
},
|
|
}),
|
|
});
|
|
|
|
b.installArtifact(exe);
|
|
|
|
const run_step = b.step("run", "Run the app");
|
|
const run_cmd = b.addRunArtifact(exe);
|
|
run_step.dependOn(&run_cmd.step);
|
|
run_cmd.step.dependOn(b.getInstallStep());
|
|
if (b.args) |args| {
|
|
run_cmd.addArgs(args);
|
|
}
|
|
|
|
const mod_tests = b.addTest(.{
|
|
.root_module = mod,
|
|
});
|
|
const run_mod_tests = b.addRunArtifact(mod_tests);
|
|
|
|
const exe_tests = b.addTest(.{
|
|
.root_module = exe.root_module,
|
|
});
|
|
const run_exe_tests = b.addRunArtifact(exe_tests);
|
|
|
|
const test_step = b.step("test", "Run tests");
|
|
test_step.dependOn(&run_mod_tests.step);
|
|
test_step.dependOn(&run_exe_tests.step);
|
|
|
|
}
|