Skip to content

Commit

Permalink
feat: MIR built with zig
Browse files Browse the repository at this point in the history
WIP: currently fails at runtim
  • Loading branch information
giann committed Jul 30, 2024
1 parent 65d43ec commit 0f8f5ca
Showing 1 changed file with 83 additions and 6 deletions.
89 changes: 83 additions & 6 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ pub fn build(b: *Build) !void {
try buildLinenoise(b, target, build_mode)
else
null;
const lib_mir = if (!is_wasm)
try buildMir(b, target, build_mode)
else
null;

var exe = b.addExecutable(.{
.name = "buzz",
Expand All @@ -302,12 +306,16 @@ pub fn build(b: *Build) !void {
});
b.installArtifact(exe);

var exe_check = b.addExecutable(.{
.name = "buzz",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = build_mode,
});
var exe_check = b.addExecutable(
.{
.name = "buzz",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = build_mode,
},
);

exe.root_module.sanitize_c = false;

const check = b.step("check", "Check if buzz compiles");
check.dependOn(&exe_check.step);
Expand Down Expand Up @@ -386,13 +394,18 @@ pub fn build(b: *Build) !void {
if (lib_pcre2) |pcre| {
lib.linkLibrary(pcre);
}

if (lib_mimalloc) |mimalloc| {
lib.linkLibrary(mimalloc);
if (lib.root_module.resolved_target.?.result.os.tag == .windows) {
lib.linkSystemLibrary("bcrypt");
}
}

if (lib_mir) |mir| {
lib.linkLibrary(mir);
}

// So that JIT compiled function can reference buzz_api
exe.linkLibrary(lib);
exe_check.linkLibrary(lib);
Expand Down Expand Up @@ -484,6 +497,11 @@ pub fn build(b: *Build) !void {
std_lib.linkSystemLibrary("bcrypt");
}
}

if (lib_mir) |mir| {
std_lib.linkLibrary(mir);
}

std_lib.linkLibrary(lib);
std_lib.root_module.addImport("build_options", build_option_module);

Expand Down Expand Up @@ -519,6 +537,9 @@ pub fn build(b: *Build) !void {
tests.linkSystemLibrary("bcrypt");
}
}
if (lib_mir) |mir| {
tests.linkLibrary(mir);
}
tests.root_module.addImport("build_options", build_option_module);

const test_step = b.step("test", "Run all the tests");
Expand Down Expand Up @@ -699,3 +720,59 @@ pub fn buildWasmReplDemo(b: *Build, exe: *Build.Step.Compile) void {
);
b.getInstallStep().dependOn(&copyRepl.step);
}

pub fn buildMir(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) !*Build.Step.Compile {
const lib = b.addStaticLibrary(
.{
.name = "mir",
.target = target,
.optimize = optimize,
},
);

lib.addIncludePath(b.path("./vendors/mir"));
lib.linkLibC();

if (lib.root_module.resolved_target.?.result.os.tag == .windows) {
lib.addSystemIncludePath(
b.path((std.process.Child.run(.{
.allocator = b.allocator,
.argv = &.{
"xcrun",
"--show-sdk-path",
},
}) catch {
std.debug.print("Failed to get MacOSX sdk path", .{});
unreachable;
}).stdout),
);
// Github macos-12 runner (https://github.com/actions/runner-images/blob/main/images/macos/macos-12-Readme.md).
lib.addSystemIncludePath(b.path("/Applications/Xcode_14.0.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include"));
lib.addSystemIncludePath(b.path("/Library/Developer/CommandLineTools/SDKs/MacOSX14.0.sdk/usr/include"));
lib.addSystemIncludePath(b.path("/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include"));
lib.addSystemIncludePath(b.path("/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include"));
lib.addSystemIncludePath(b.path("/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include"));
}

lib.addCSourceFiles(
.{
.files = &.{
"./vendors/mir/mir.c",
"./vendors/mir/mir-gen.c",
"./vendors/mir/c2mir/c2mir.c",
},
.flags = &.{
"-fsigned-char",
"-O3",
"-DNDEBUG=1",
"-DMIR_PARALLEL_GEN=1",
// "-DADDITIONAL_INCLUDE_PATH=\"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include\"",
},
},
);

// lib.linkSystemLibrary("m");
// lib.linkSystemLibrary("dl");

return lib;
}

0 comments on commit 0f8f5ca

Please sign in to comment.