diff gui_ze/gui_ze.bzl @ 173:827c6ac504cd hg-web

Merged in default here.
author MrJuneJune <me@mrjunejune.com>
date Mon, 19 Jan 2026 18:59:10 -0800
parents f3084bca7317
children 71ad34a8bc9a 8c74204fd362
line wrap: on
line diff
--- a/gui_ze/gui_ze.bzl	Sat Jan 10 13:35:09 2026 -0800
+++ b/gui_ze/gui_ze.bzl	Mon Jan 19 18:59:10 2026 -0800
@@ -1,3 +1,68 @@
+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-unknown-unknown",
+        "-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(
@@ -137,6 +202,69 @@
   },
 )
 
+def _bun_run_impl(ctx):
+    actual_exe = ctx.actions.declare_file(ctx.label.name)
+    
+    # 1. Get the workspace name (crucial for runfiles paths)
+    workspace_name = ctx.workspace_name
+
+    # 2. Define the paths relative to the runfiles root
+    bun_path = ctx.executable._bun.short_path
+    src_path = ctx.file.src.short_path
+
+    # 3. Create the launcher script
+    # We use a template to handle the environment and pathing
+    script_content = """#!/bin/bash
+# Find the runfiles directory
+if [[ -z "$RUNFILES_DIR" ]]; then
+  if [[ -d "$0.runfiles" ]]; then
+    RUNFILES_DIR="$0.runfiles"
+  fi
+fi
+
+# Navigate to the workspace root inside runfiles
+cd "$RUNFILES_DIR/{workspace}"
+pwd
+# Execute bun with the src file and any extra arguments
+exec "./{bun_bin}" run "./{src_file}" "$@"
+""".format(
+        workspace = workspace_name,
+        bun_bin = bun_path,
+        src_file = src_path
+    )
+
+    ctx.actions.write(
+        output = actual_exe,
+        content = script_content,
+        is_executable = True,
+    )
+
+    return [
+        DefaultInfo(
+            executable = actual_exe,
+            runfiles = ctx.runfiles(
+                files = [ctx.file.src] + ctx.files.data + ctx.files._bun_files
+            ).merge(ctx.attr._bun[DefaultInfo].default_runfiles),
+        ),
+    ]
+
+# TODO: Fix the rules so that it can do relative import.
+bun_run = rule(
+  implementation = _bun_run_impl,
+  executable = True,
+  attrs = {
+    "src": attr.label(allow_single_file = True, mandatory = True),
+    "data": attr.label_list(allow_files = True),
+    "_bun": attr.label(
+      default = Label("//third_party/bun:bun"), 
+      executable = True, 
+      cfg = "exec"
+    ),
+    "_bun_files": attr.label(default = Label("//third_party/bun:bun_files")),
+  },
+)
+
+
 def _move_files_into_dir_impl(ctx):
   srcs = ctx.files.srcs
   outs = []
@@ -315,12 +443,15 @@
         {name}_signed_app: The signed .app bundle
         {name}_dmg: The final DMG file
     """
+    macos_constraint = ["@platforms//os:macos"]
+
     # 1. Build the .app bundle
     _macos_app(
         name = name + "_app",
         binary = binary,
         app_name = name,
         bundle_id = bundle_id,
+        target_compatible_with = macos_constraint,
     )
 
     # 2. Sign the .app
@@ -328,6 +459,7 @@
         name = name + "_signed_app",
         app = ":" + name + "_app",
         app_name = name,
+        target_compatible_with = macos_constraint,
     )
 
     # 3. Create the DMG
@@ -347,4 +479,5 @@
         """.format(name = name),
         local = 1,  # Disable sandboxing for hdiutil
         tags = ["no-sandbox"],
+        target_compatible_with = macos_constraint,
     )