diff gui_ze/gui_ze.bzl @ 154:bdcc610eeed8

[Markdown Converter][GuiZe] Added markdown coverter in C and wasm rule sets. Needs further view on this as I haven't taken a look. Written by Claude.
author June Park <parkjune1995@gmail.com>
date Mon, 12 Jan 2026 09:11:58 -0800
parents 7eb79fd91c7e
children cd35e600ae34
line wrap: on
line diff
--- a/gui_ze/gui_ze.bzl	Sun Jan 11 08:11:24 2026 -0800
+++ b/gui_ze/gui_ze.bzl	Mon Jan 12 09:11:58 2026 -0800
@@ -1,3 +1,69 @@
+def _wasm_binary_impl(ctx):
+    """
+    Compile C source to WASM using clang --target=wasm32.
+    No libc - suitable for standalone WASM modules.
+
+    Requires LLVM with wasm32 support (e.g., Homebrew LLVM on macOS).
+    """
+    src = ctx.file.src
+    out = ctx.actions.declare_file(ctx.label.name + ".wasm")
+
+    # Shell script to find clang with wasm support and set PATH for wasm-ld
+    setup_cmd = """
+    export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH"
+    if [ -x /opt/homebrew/opt/llvm/bin/clang ]; then
+      CLANG=/opt/homebrew/opt/llvm/bin/clang
+    elif [ -x /usr/local/opt/llvm/bin/clang ]; then
+      CLANG=/usr/local/opt/llvm/bin/clang
+    else
+      CLANG=clang
+    fi
+    """
+
+    # Build clang command
+    cmd_parts = [
+        setup_cmd,
+        "$CLANG",
+        "--target=wasm32",
+        "-nostdlib",
+        "-O2",
+        "-Wl,--no-entry",
+        "-Wl,--export-dynamic",
+    ]
+
+    # Add exported functions
+    for fn in ctx.attr.exports:
+        cmd_parts.append("-Wl,--export=" + fn)
+
+    cmd_parts.append("-o")
+    cmd_parts.append(out.path)
+    cmd_parts.append(src.path)
+
+    ctx.actions.run_shell(
+        inputs = [src],
+        outputs = [out],
+        command = " ".join(cmd_parts),
+        progress_message = "Compiling {} to WASM".format(src.basename),
+        execution_requirements = {"no-sandbox": "1"},
+    )
+
+    return [DefaultInfo(files = depset([out]))]
+
+wasm_binary = rule(
+    implementation = _wasm_binary_impl,
+    attrs = {
+        "src": attr.label(
+            allow_single_file = [".c"],
+            mandatory = True,
+            doc = "The C source file to compile",
+        ),
+        "exports": attr.string_list(
+            default = [],
+            doc = "List of function names to export (without leading underscore)",
+        ),
+    },
+)
+
 def _foo_impl(ctx):
   out = ctx.actions.declare_file(ctx.label.name)
   ctx.actions.write(