Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update build to zig 0.11.0 #454

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

mangas
Copy link

@mangas mangas commented Nov 16, 2023

Update zig build to use the v0.11.0 api

@mangas
Copy link
Author

mangas commented Nov 16, 2023

As far as I can see this is not tested on CI, perhaps it's worth adding a build step just keep it working?

@vshymanskyy
Copy link
Member

Thanks! I'd appreciate it if a CI job could be added along with this PR.

@kassane
Copy link

kassane commented Feb 28, 2024

Hi @mangas & @vshymanskyy

My patch for v0.12.0

diff-patch
diff --git a/build.zig b/build.zig
index 6679f06..0d99eff 100644
--- a/build.zig
+++ b/build.zig
@@ -1,39 +1,82 @@
 const std = @import("std");
 
-pub fn build(b: *std.build.Builder) !void {
+pub fn build(b: *std.Build) !void {
     const target = b.standardTargetOptions(.{});
-    const mode = b.standardReleaseOptions();
+    const optimize = b.standardOptimizeOption(.{
+        .preferred_optimize_mode = .ReleaseSmall,
+    });
 
-    const wasm3 = b.addExecutable("wasm3", null);
-    wasm3.setTarget(target);
-    wasm3.setBuildMode(mode);
-    wasm3.install();
-    wasm3.linkLibC();
+    const libwasm3 = b.addStaticLibrary(.{
+        .name = "m3",
+        .target = target,
+        .optimize = optimize,
+    });
+    libwasm3.root_module.sanitize_c = false; // fno-sanitize=undefined
+    libwasm3.defineCMacro("d_m3HasTracer", null);
 
-    if (target.getCpuArch() == .wasm32 and target.getOsTag() == .wasi) {
-        wasm3.linkSystemLibrary("wasi-emulated-process-clocks");
+    if (libwasm3.rootModuleTarget().isWasm()) {
+        if (libwasm3.rootModuleTarget().os.tag == .wasi) {
+            libwasm3.defineCMacro("d_m3HasWASI", null);
+            libwasm3.linkSystemLibrary("wasi-emulated-process-clocks");
+        }
     }
+    libwasm3.addIncludePath(.{ .path = "source" });
+    libwasm3.addCSourceFiles(.{
+        .files = &.{
+            "source/m3_api_libc.c",
+            "source/extensions/m3_extensions.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_env.c",
+            "source/m3_exec.c",
+            "source/m3_function.c",
+            "source/m3_info.c",
+            "source/m3_module.c",
+            "source/m3_parse.c",
+        },
+        .flags = if (libwasm3.rootModuleTarget().isWasm())
+            &cflags ++ [_][]const u8{
+                "-Xclang",
+                "-target-feature",
+                "-Xclang",
+                "+tail-call",
+            }
+        else
+            &cflags,
+    });
+    libwasm3.linkSystemLibrary("m");
+    libwasm3.linkLibC();
 
-    wasm3.addIncludePath("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_env.c",
-        "source/m3_exec.c",
-        "source/m3_function.c",
-        "source/m3_info.c",
-        "source/m3_module.c",
-        "source/m3_parse.c",
-        "platforms/app/main.c",
-    }, &.{
-        "-Dd_m3HasWASI",
-        "-fno-sanitize=undefined", // TODO investigate UB sites in the codebase, then delete this line.
+    const wasm3 = b.addExecutable(.{
+        .name = "wasm3",
+        .target = target,
+        .optimize = optimize,
     });
+    for (libwasm3.root_module.include_dirs.items) |dir| {
+        wasm3.addIncludePath(dir.path);
+    }
+    wasm3.addCSourceFile(.{
+        .file = .{ .path = "platforms/app/main.c" },
+        .flags = &cflags,
+    });
+
+    wasm3.linkLibrary(libwasm3);
+    b.installArtifact(wasm3);
 }
+
+const cflags = [_][]const u8{
+    "-Wall",
+    "-Wextra",
+    "-Wpedantic",
+    "-Wparentheses",
+    "-Wundef",
+    "-Wpointer-arith",
+    "-Wstrict-aliasing=2",
+    "-std=gnu11",
+};

Output:

$> zig build --summary all
Build Summary: 4/4 steps succeeded
install success
└─ install wasm3 success
   └─ zig build-exe wasm3 Debug native success 94ms MaxRSS:66M
      └─ zig build-lib m3 Debug native success 487ms MaxRSS:86M

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants