Zig bindings for orca (still WIP) #140
|
@ -1,22 +0,0 @@
|
||||||
@echo off
|
|
||||||
|
|
||||||
:: compile wasm module
|
|
||||||
set ORCA_DIR=..\..
|
|
||||||
set STDLIB_DIR=%ORCA_DIR%\src\libc-shim
|
|
||||||
|
|
||||||
set wasmFlags=--target=wasm32^
|
|
||||||
--no-standard-libraries ^
|
|
||||||
-mbulk-memory ^
|
|
||||||
-g -O2 ^
|
|
||||||
-D__ORCA__ ^
|
|
||||||
-Wl,--no-entry ^
|
|
||||||
-Wl,--export-dynamic ^
|
|
||||||
-Wl,--relocatable ^
|
|
||||||
-isystem %STDLIB_DIR%\include ^
|
|
||||||
-I%ORCA_DIR%\src ^
|
|
||||||
-I%ORCA_DIR%\src\ext
|
|
||||||
|
|
||||||
clang %wasmFlags% -o .\liborca.a ..\..\src\orca.c ..\..\src\libc-shim\src\*.c
|
|
||||||
IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL%
|
|
||||||
|
|
||||||
zig build bundle
|
|
|
@ -1,4 +1,5 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const builtin = @import("builtin");
|
||||||
|
|
||||||
fn addSourceString(str: []const u8, strings: *std.ArrayList(u8), sources: *std.ArrayList([]const u8)) !void {
|
fn addSourceString(str: []const u8, strings: *std.ArrayList(u8), sources: *std.ArrayList([]const u8)) !void {
|
||||||
var begin = strings.items.len;
|
var begin = strings.items.len;
|
||||||
|
@ -9,67 +10,63 @@ fn addSourceString(str: []const u8, strings: *std.ArrayList(u8), sources: *std.A
|
||||||
|
|
||||||
pub fn build(b: *std.Build) !void {
|
pub fn build(b: *std.Build) !void {
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
const wasm_target = std.zig.CrossTarget{
|
var wasm_target = std.zig.CrossTarget{
|
||||||
.cpu_arch = .wasm32,
|
.cpu_arch = .wasm32,
|
||||||
.os_tag = .freestanding,
|
.os_tag = .freestanding,
|
||||||
};
|
};
|
||||||
|
wasm_target.cpu_features_add.addFeature(@intFromEnum(std.Target.wasm.Feature.bulk_memory));
|
||||||
|
|
||||||
//NOTE(reuben) - Ideally we would build the orca wasm lib and link it all within build.zig, but there
|
var orca_source_strings = try std.ArrayList(u8).initCapacity(b.allocator, 1024 * 4);
|
||||||
// seems to be a bug where the -mbulk-memory flag is ignored and memory.copy and
|
var orca_sources = try std.ArrayList([]const u8).initCapacity(b.allocator, 128);
|
||||||
// memory.fill instructions aren't generated, leading the memcpy and memset instructions
|
defer orca_source_strings.deinit();
|
||||||
// to become infinitely recursive, crashing the program. So for now we build the orca
|
defer orca_sources.deinit();
|
||||||
// wasm lib with clang and link it in here.
|
|
||||||
|
|
||||||
// var orca_source_strings = try std.ArrayList(u8).initCapacity(b.allocator, 1024 * 4);
|
{
|
||||||
// var orca_sources = try std.ArrayList([]const u8).initCapacity(b.allocator, 128);
|
try addSourceString("../../src/orca.c", &orca_source_strings, &orca_sources);
|
||||||
// defer orca_source_strings.deinit();
|
|
||||||
// defer orca_sources.deinit();
|
|
||||||
|
|
||||||
// {
|
var libc_shim_dir = try std.fs.cwd().openIterableDir("../../src/libc-shim/src", .{});
|
||||||
// try addSourceString("../../src/orca.c", &orca_source_strings, &orca_sources);
|
var walker = try libc_shim_dir.walk(b.allocator);
|
||||||
|
defer walker.deinit();
|
||||||
|
|
||||||
// var libc_shim_dir = try std.fs.cwd().openIterableDir("../../src/libc-shim/src", .{});
|
while (try walker.next()) |entry| {
|
||||||
// var walker = try libc_shim_dir.walk(b.allocator);
|
const extension = std.fs.path.extension(entry.path);
|
||||||
// defer walker.deinit();
|
if (std.mem.eql(u8, extension, ".c")) {
|
||||||
|
var path_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
|
||||||
|
var abs_path = try libc_shim_dir.dir.realpath(entry.path, &path_buffer);
|
||||||
|
try addSourceString(abs_path, &orca_source_strings, &orca_sources);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// while (try walker.next()) |entry| {
|
const orca_compile_opts = [_][]const u8{
|
||||||
// const extension = std.fs.path.extension(entry.path);
|
"-D__ORCA__",
|
||||||
// if (std.mem.eql(u8, extension, ".c")) {
|
"--no-standard-libraries",
|
||||||
// var path_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
|
"-fno-builtin",
|
||||||
// var abs_path = try libc_shim_dir.dir.realpath(entry.path, &path_buffer);
|
"-g",
|
||||||
// try addSourceString(abs_path, &orca_source_strings, &orca_sources);
|
"-O2",
|
||||||
// // std.debug.print("adding libc shim source: {s}\n", .{abs_path});
|
"-mexec-model=reactor",
|
||||||
// }
|
"-fno-sanitize=undefined",
|
||||||
// }
|
"-isystem ../../src/libc-shim/include",
|
||||||
// }
|
"-I../../src",
|
||||||
|
"-I../../src/ext",
|
||||||
|
"-Wl,--export-dynamic",
|
||||||
|
};
|
||||||
|
|
||||||
// const orca_compile_opts = [_][]const u8{
|
var orca_lib = b.addStaticLibrary(.{
|
||||||
// "-D__ORCA__",
|
.name = "orca",
|
||||||
// "--no-standard-libraries",
|
.target = wasm_target,
|
||||||
// "-fno-builtin",
|
.optimize = optimize,
|
||||||
// "-isystem ../../src/libc-shim/include",
|
});
|
||||||
// "-I../../src",
|
orca_lib.rdynamic = true;
|
||||||
// "-I../../ext",
|
orca_lib.addIncludePath(.{ .path = "../../src" });
|
||||||
// "-O2",
|
orca_lib.addIncludePath(.{ .path = "../../src/libc-shim/include" });
|
||||||
// "-mbulk-memory",
|
orca_lib.addIncludePath(.{ .path = "../../src/ext" });
|
||||||
// "-Wl,--no-entry",
|
orca_lib.addCSourceFiles(orca_sources.items, &orca_compile_opts);
|
||||||
// "-Wl,--export-all",
|
|
||||||
// "-g",
|
|
||||||
// };
|
|
||||||
|
|
||||||
// var orca_lib = b.addStaticLibrary(.{
|
|
||||||
// .name = "orca",
|
|
||||||
// .target = wasm_target,
|
|
||||||
// .optimize = optimize,
|
|
||||||
// });
|
|
||||||
// orca_lib.rdynamic = true;
|
|
||||||
// orca_lib.addIncludePath(.{ .path = "../../src" });
|
|
||||||
// orca_lib.addIncludePath(.{ .path = "../../src/libc-shim/include" });
|
|
||||||
// orca_lib.addIncludePath(.{ .path = "../../ext" });
|
|
||||||
// orca_lib.addCSourceFiles(orca_sources.items, &orca_compile_opts);
|
|
||||||
// b.installArtifact(orca_lib);
|
|
||||||
|
|
||||||
// builds the wasm module out of the orca C sources and main.zig
|
// builds the wasm module out of the orca C sources and main.zig
|
||||||
|
const orca_module: *std.Build.Module = b.createModule(.{
|
||||||
|
.source_file = .{ .path = "../../src/orca.zig" },
|
||||||
|
});
|
||||||
const wasm_lib = b.addSharedLibrary(.{
|
const wasm_lib = b.addSharedLibrary(.{
|
||||||
.name = "module",
|
.name = "module",
|
||||||
.root_source_file = .{ .path = "src/main.zig" },
|
.root_source_file = .{ .path = "src/main.zig" },
|
||||||
|
@ -77,17 +74,11 @@ pub fn build(b: *std.Build) !void {
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
wasm_lib.rdynamic = true;
|
wasm_lib.rdynamic = true;
|
||||||
|
|
||||||
const orca_module: *std.Build.Module = b.createModule(.{
|
|
||||||
.source_file = .{ .path = "../../src/orca.zig" },
|
|
||||||
});
|
|
||||||
wasm_lib.addIncludePath(.{ .path = "../../src" });
|
wasm_lib.addIncludePath(.{ .path = "../../src" });
|
||||||
wasm_lib.addIncludePath(.{ .path = "../../src/libc-shim/include" });
|
wasm_lib.addIncludePath(.{ .path = "../../src/libc-shim/include" });
|
||||||
wasm_lib.addIncludePath(.{ .path = "../../ext" });
|
wasm_lib.addIncludePath(.{ .path = "../../ext" });
|
||||||
wasm_lib.addModule("orca", orca_module);
|
wasm_lib.addModule("orca", orca_module);
|
||||||
// wasm_lib.linkLibrary(orca_lib);
|
wasm_lib.linkLibrary(orca_lib);
|
||||||
wasm_lib.addLibraryPath(.{ .path = "." });
|
|
||||||
wasm_lib.linkSystemLibrary("orca");
|
|
||||||
|
|
||||||
// copies the wasm module into zig-out/wasm_lib
|
// copies the wasm module into zig-out/wasm_lib
|
||||||
b.installArtifact(wasm_lib);
|
b.installArtifact(wasm_lib);
|
||||||
|
@ -99,4 +90,18 @@ pub fn build(b: *std.Build) !void {
|
||||||
|
|
||||||
const bundle_step = b.step("bundle", "Runs the orca toolchain to bundle the wasm module into an orca app.");
|
const bundle_step = b.step("bundle", "Runs the orca toolchain to bundle the wasm module into an orca app.");
|
||||||
bundle_step.dependOn(&bundle_cmd.step);
|
bundle_step.dependOn(&bundle_cmd.step);
|
||||||
|
|
||||||
|
// Runs the app
|
||||||
|
const run_cmd_windows = [_][]const u8{"Sample/bin/Sample.exe"};
|
||||||
|
const run_cmd_macos = [_][]const u8{ "open", "Sample.app" };
|
||||||
|
const run_cmd_str: []const []const u8 = switch (builtin.os.tag) {
|
||||||
|
.windows => &run_cmd_windows,
|
||||||
|
.macos => &run_cmd_macos,
|
||||||
|
else => @compileError("unsupported platform"),
|
||||||
|
};
|
||||||
|
var run_cmd = b.addSystemCommand(run_cmd_str);
|
||||||
|
run_cmd.step.dependOn(&bundle_cmd.step);
|
||||||
|
|
||||||
|
const run_step = b.step("run", "Runs the bundled app using the Orca runtime.");
|
||||||
|
run_step.dependOn(&run_cmd.step);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue