Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ bazel_dep(name = "rules_cc", version = "0.2.14")
bazel_dep(name = "rules_go", version = "0.59.0")

# MoonBit language support for WebAssembly components
# Required for moonbit_wasm_cli and moonbit_wasm_binary rules
# Note: rules_moonbit is not yet in BCR - uses git_override
bazel_dep(name = "rules_moonbit", version = "0.1.0")
# Dev dependency: rules_moonbit is not yet in BCR. Downstream users who want
# MoonBit support must add their own bazel_dep + git_override for rules_moonbit.
# The moonbit/ rules are available but only resolve when rules_moonbit is present.
bazel_dep(name = "rules_moonbit", version = "0.1.0", dev_dependency = True)
git_override(
module_name = "rules_moonbit",
remote = "https://github.com/pulseengine/rules_moonbit.git",
commit = "b5bbfe8ef1f87b42c77f168640817f67ed8758a3",
commit = "e95c912e55094b90b6c02a6052c0a284b142e6b8", # includes chmod + bundle + dev_dependency fix
)

# OCI image signing capabilities
Expand Down Expand Up @@ -241,6 +242,12 @@ use_repo(componentize_py, "componentize_py_toolchain")

register_toolchains("@componentize_py_toolchain//:componentize_py_toolchain")

# MoonBit hermetic toolchain for WebAssembly component builds (dev only)
moonbit = use_extension("@rules_moonbit//moonbit:extensions.bzl", "moonbit_toolchain_extension", dev_dependency = True)
use_repo(moonbit, "moonbit_toolchain")

register_toolchains("@moonbit_toolchain//:moonbit_toolchain", dev_dependency = True)

# File Operations Component toolchain for universal file handling
register_toolchains("//toolchains:file_ops_toolchain_target")

Expand Down
13 changes: 11 additions & 2 deletions moonbit/private/moonbit_wasm_component.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def _moonbit_wasm_component_impl(ctx):

moonbit_toolchain = ctx.toolchains["@rules_moonbit//moonbit:moonbit_toolchain_type"]
moon = moonbit_toolchain.moonbit.moon_executable
moonbit_all_files = moonbit_toolchain.moonbit.all_files

wit_file = ctx.file.wit
srcs = ctx.files.srcs
Expand All @@ -70,6 +71,12 @@ WIT_FILE="$(pwd)/{wit_file}"
OUTPUT="$(pwd)/{output}"
ORIG_DIR="$(pwd)"

# Set MOON_HOME so moon build can find the core library
# MOON path: /path/to/external/moonbit_toolchain/bin/moon
# MOON_HOME: /path/to/external/moonbit_toolchain/.moon
TOOLCHAIN_ROOT=$(dirname $(dirname "$MOON"))
export MOON_HOME="$TOOLCHAIN_ROOT/.moon"

# Create temporary project directory
PROJECT_DIR=$(mktemp -d)
cleanup() {{ rm -rf "$PROJECT_DIR"; }}
Expand All @@ -94,7 +101,7 @@ if [ -d "$STUB_DIR" ]; then
fi

# Step 3: Compile with MoonBit
"$MOON" build --target wasm --release 2>&1 || {{
"$MOON" build --target wasm 2>&1 || {{
echo "MoonBit compilation failed" >&2
exit 1
}}
Expand All @@ -107,7 +114,9 @@ if [ -z "$CORE_WASM" ]; then
fi

# Step 4: Embed WIT metadata
# Use --encoding utf16 for MoonBit string encoding
"$WASM_TOOLS" component embed \\
--encoding utf16 \\
--world {world} \\
"$WIT_FILE" \\
"$CORE_WASM" \\
Expand Down Expand Up @@ -135,7 +144,7 @@ echo "Created component: $OUTPUT"

ctx.actions.run_shell(
command = script,
inputs = [wit_file, wit_bindgen, moon, wasm_tools] + srcs,
inputs = [wit_file, wit_bindgen, moon, wasm_tools] + srcs + moonbit_all_files.to_list(),
outputs = [component_wasm],
mnemonic = "MoonbitWasmComponent",
progress_message = "Building MoonBit WASM component %s" % ctx.label,
Expand Down
Loading