From 70706ffb5b4b6fba95c98d22335092c1cbd58b6d Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Wed, 9 Jun 2021 08:26:42 +0200 Subject: [PATCH] Add minimal build.zig build script (#247) * Add minimal build.zig build script The minimal `build.zig` script effectively can act as a (nicer?) alternative to CMake; however, it is minimal in the sense, it currently only includes the necessary ingredients to build `wasm3` with Zig as the build system. As an added bonus though, you get the full cross-compilation power of Zig for free. Example invocation to build for the host: ``` zig build ``` To target `wasm32-wasi`: ``` zig build -Dtarget=wasm32-wasi ``` To target `aarch64-macos` from *anywhere*: ``` zig build -Dtarget=aarch64-macos ``` * Include Zig in build instructions --- build.zig | 42 ++++++++++++++++++++++++++++++++++++++++++ docs/Development.md | 30 ++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 build.zig 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 +```