diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..d5d9a4a --- /dev/null +++ b/build.zig @@ -0,0 +1,42 @@ +const std = @import("std"); + +pub fn build(b: *std.build.Builder) !void { + const target = b.standardTargetOptions(.{}); + const mode = b.standardReleaseOptions(); + + const wasm3 = b.addExecutable("wasm3", null); + wasm3.setTarget(target); + wasm3.setBuildMode(mode); + wasm3.install(); + wasm3.linkLibC(); + + if (target.getCpuArch() == .wasm32 and target.getOsTag() == .wasi) { + wasm3.linkSystemLibrary("wasi-emulated-process-clocks"); + } + + wasm3.addIncludeDir("source"); + wasm3.addCSourceFiles(&.{ + "source/m3_api_libc.c", + "source/m3_api_meta_wasi.c", + "source/m3_api_tracer.c", + "source/m3_api_uvwasi.c", + "source/m3_api_wasi.c", + "source/m3_bind.c", + "source/m3_code.c", + "source/m3_compile.c", + "source/m3_core.c", + "source/m3_emit.c", + "source/m3_env.c", + "source/m3_exec.c", + "source/m3_function.c", + "source/m3_info.c", + "source/m3_module.c", + "source/m3_optimize.c", + "source/m3_parse.c", + "source/extensions/m3_extensions.c", + "platforms/app/main.c", + }, &.{ + "-Dd_m3HasWASI", + "-fno-sanitize=undefined", // TODO investigate UB sites in the codebase, then delete this line. + }); +} diff --git a/docs/Development.md b/docs/Development.md index 0008221..59c6cdf 100644 --- a/docs/Development.md +++ b/docs/Development.md @@ -149,3 +149,33 @@ llc -march=wasm32 -mattr=help chromium-browser --single-process --js-flags="--help" 2>&1 | grep wasm ``` +## Build with Zig + +Grab the latest nightly Zig toolchain from [ziglang.org/download], and then simply run: + +[ziglang.org/download]: https://ziglang.org/download/ + +```sh +zig build +``` + +This will install `wasm3` compiled for your target architecture in `zig-out/bin/wasm3`. + +In order to build `wasm3` in a mode different than Debug, e.g., in ReleaseFast, pass in +an appropriate flag like so: + +```sh +zig build -Drelease-fast +``` + +If you want to cross-compile to some specific target, pass in the target with a flag like so: + +```sh +zig build -Dtarget=wasm32-wasi +``` + +Or if targeting Apple Silicon (this works from *any* host with Zig): + +```sh +zig build -Dtarget=aarch64-macos +```