diff --git a/examples/macro_example/BUILD.bazel b/examples/macro_example/BUILD.bazel deleted file mode 100644 index 6f5cf0ae..00000000 --- a/examples/macro_example/BUILD.bazel +++ /dev/null @@ -1,15 +0,0 @@ -"""wit-bindgen macro example (demonstration only).""" - -load("//wit:defs.bzl", "wit_library") -load("@rules_rust//rust:defs.bzl", "rust_binary") - -wit_library( - name = "macro_interfaces", - srcs = ["wit/macro-example.wit"], - world = "macro-world", -) - -rust_binary( - name = "host_app", - srcs = ["src/host.rs"], -) diff --git a/examples/macro_example/src/host.rs b/examples/macro_example/src/host.rs deleted file mode 100644 index b7f4d904..00000000 --- a/examples/macro_example/src/host.rs +++ /dev/null @@ -1,20 +0,0 @@ -// Example host application for macro-generated component -// -// This demonstrates a simple host that would interact with the WASM component. -// In a full implementation, this would use wasmtime or similar runtime. - -fn main() -> Result<(), Box> { - println!("wit-bindgen macro example host application"); - println!(); - println!("This host application demonstrates the macro-based approach."); - println!("The WASM component (macro_component) was built using:"); - println!(" - wit-bindgen's generate!() macro directly in source"); - println!(" - WIT files provided via CARGO_MANIFEST_DIR from Bazel"); - println!(); - println!("In a full implementation, this host would:"); - println!(" 1. Load the macro_component.wasm file"); - println!(" 2. Provide a logger implementation to the component"); - println!(" 3. Call the calculator's add() and multiply() functions"); - - Ok(()) -} diff --git a/examples/macro_example/src/lib.rs b/examples/macro_example/src/lib.rs deleted file mode 100644 index f9d8f472..00000000 --- a/examples/macro_example/src/lib.rs +++ /dev/null @@ -1,45 +0,0 @@ -// Example of using wit-bindgen generate!() macro directly in source code -// -// Note: The macro approach works but requires careful path configuration. -// For Bazel builds, the `rust_wasm_component_bindgen` rule is recommended -// as it handles WIT paths automatically. - -use wit_bindgen::generate; - -// Generate bindings using the macro approach -// The WIT files are made available via CARGO_MANIFEST_DIR environment variable -// which points to the wit_library output directory -generate!({ - world: "macro-world", - path: ".", // Points to wit_library output directory (set via CARGO_MANIFEST_DIR) -}); - -// Use generated imports -use exports::macro_::example::calculator::Guest as CalculatorGuest; -use macro_::example::logger; - -// Implement the exported calculator interface -struct CalculatorImpl; - -impl CalculatorGuest for CalculatorImpl { - fn add(a: f64, b: f64) -> f64 { - let result = a + b; - - // Use imported logger interface - logger::log("info", &format!("Adding {} + {} = {}", a, b, result)); - - result - } - - fn multiply(a: f64, b: f64) -> f64 { - let result = a * b; - - // Use imported logger interface - logger::log("info", &format!("Multiplying {} * {} = {}", a, b, result)); - - result - } -} - -// Export the component implementation -export!(CalculatorImpl); diff --git a/examples/macro_example/wit/macro-example.wit b/examples/macro_example/wit/macro-example.wit deleted file mode 100644 index dab95bd6..00000000 --- a/examples/macro_example/wit/macro-example.wit +++ /dev/null @@ -1,22 +0,0 @@ -package macro:example; - -/// A simple calculator interface for demonstrating macro usage -interface calculator { - /// Add two numbers - add: func(a: f64, b: f64) -> f64; - - /// Multiply two numbers - multiply: func(a: f64, b: f64) -> f64; -} - -/// Logger interface for macro examples -interface logger { - /// Log a message with a level - log: func(level: string, message: string); -} - -/// The main world that exports calculator and imports logger -world macro-world { - export calculator; - import logger; -} diff --git a/examples/oci_publishing/src/lib.rs b/examples/oci_publishing/src/lib.rs deleted file mode 100644 index 0874b88f..00000000 --- a/examples/oci_publishing/src/lib.rs +++ /dev/null @@ -1,23 +0,0 @@ -use wit_bindgen::generate; - -// Generate bindings for the hello-world interface -generate!({ - world: "hello-world", - exports: { - "component:hello-world/hello": HelloWorld, - }, -}); - -struct HelloWorld; - -impl Guest for HelloWorld { - fn hello() -> String { - "Hello from WebAssembly OCI component!".to_string() - } -} - -impl exports::component::hello_world::hello::Guest for HelloWorld { - fn hello() -> String { - "Hello from WebAssembly OCI component!".to_string() - } -} diff --git a/rust/defs.bzl b/rust/defs.bzl index 96ff12d8..aeb7c293 100644 --- a/rust/defs.bzl +++ b/rust/defs.bzl @@ -69,10 +69,6 @@ load( "//rust/private:rust_wasm_component_bindgen.bzl", _rust_wasm_component_bindgen = "rust_wasm_component_bindgen", ) -load( - "//rust/private:rust_wasm_component_macro.bzl", - _rust_wasm_component_macro = "rust_wasm_component_macro", -) load( "//rust/private:rust_wasm_component_test.bzl", _rust_wasm_component_test = "rust_wasm_component_test", @@ -88,6 +84,5 @@ rust_wasm_component_test = _rust_wasm_component_test rust_wasm_component_bindgen = _rust_wasm_component_bindgen rust_wasm_component_wizer = _rust_wasm_component_wizer rust_wasm_component_clippy = _rust_wasm_component_clippy -rust_wasm_component_macro = _rust_wasm_component_macro rust_clippy_all = _rust_clippy_all rust_wasm_binary = _rust_wasm_binary diff --git a/rust/private/BUILD.bazel b/rust/private/BUILD.bazel index 0f6d841a..e3be406d 100644 --- a/rust/private/BUILD.bazel +++ b/rust/private/BUILD.bazel @@ -138,15 +138,6 @@ bzl_library( ], ) -bzl_library( - name = "rust_wasm_component_macro", - srcs = ["rust_wasm_component_macro.bzl"], - visibility = [ - "//docs:__pkg__", - "//rust:__pkg__", - ], - deps = [":rust_wasm_component"], -) bzl_library( name = "debug_windows", diff --git a/rust/private/rust_wasm_component_macro.bzl b/rust/private/rust_wasm_component_macro.bzl deleted file mode 100644 index 093f1314..00000000 --- a/rust/private/rust_wasm_component_macro.bzl +++ /dev/null @@ -1,193 +0,0 @@ -"""Rust WASM component with WIT bindgen macro integration - -This file provides an alternative to rust_wasm_component_bindgen that uses -wit-bindgen's generate!() macro directly in source code instead of generating -separate crate files. -""" - -load("@rules_rust//rust:defs.bzl", "rust_library") -load(":rust_wasm_component.bzl", "rust_wasm_component") -load(":transitions.bzl", "wasm_transition") - -def _wasm_rust_library_macro_impl(ctx): - """Implementation that forwards a rust_library with WASM transition applied""" - target_info = ctx.attr.target[0] - - # Forward all providers from the transitioned target - providers = [] - - # Forward DefaultInfo (always needed) - if DefaultInfo in target_info: - providers.append(target_info[DefaultInfo]) - - # Forward CcInfo if present (Rust libraries often provide this) - if CcInfo in target_info: - providers.append(target_info[CcInfo]) - - # Forward InstrumentedFilesInfo if present - if InstrumentedFilesInfo in target_info: - providers.append(target_info[InstrumentedFilesInfo]) - - return providers - -_wasm_rust_library_macro = rule( - implementation = _wasm_rust_library_macro_impl, - attrs = { - "target": attr.label( - cfg = wasm_transition, - doc = "rust_library target to build for WASM", - ), - "_allowlist_function_transition": attr.label( - default = "@bazel_tools//tools/allowlists/function_transition_allowlist", - ), - }, - toolchains = ["@rules_rust//rust:toolchain_type"], -) - -def rust_wasm_component_macro( - name, - srcs, - wit, - wit_bindgen_crate = "@crates//:wit-bindgen", - deps = [], - crate_features = [], - rustc_flags = [], - profiles = ["release"], - visibility = None, - symmetric = False, - generation_mode = "guest", - **kwargs): - """ - Builds a Rust WebAssembly component using wit-bindgen generate!() macro. - - This macro allows developers to use wit-bindgen's generate!() macro directly - in their source code instead of generating separate binding crates. The WIT - files are made available to the macro via compile_data and environment variables. - - Generated targets: - - {name}_host: Host-platform rust_library for host applications - - {name}: The final WASM component - - Args: - name: Target name - srcs: Rust source files (must contain wit_bindgen::generate!() calls) - wit: WIT library target for the macro to access - wit_bindgen_crate: The wit-bindgen crate dependency (default: from crate_index) - deps: Additional Rust dependencies - crate_features: Rust crate features to enable - rustc_flags: Additional rustc flags - profiles: List of build profiles (e.g. ["debug", "release"]) - visibility: Target visibility - symmetric: Enable symmetric mode (requires cpetig's wit-bindgen fork) - generation_mode: Generation mode: "guest" or "native-guest" - **kwargs: Additional arguments passed to rust_wasm_component - - Example: - rust_wasm_component_macro( - name = "my_component", - srcs = ["src/lib.rs"], - wit = "//wit:my_interfaces", - profiles = ["debug", "release"], - ) - - # In src/lib.rs: - use wit_bindgen::generate; - - generate!({ - world: "my-world", - path: "../wit", // Resolved via CARGO_MANIFEST_DIR - }); - - // Use generated bindings... - - Requirements: - - Source files must use wit_bindgen::generate!() macro - - WIT files must be accessible relative to CARGO_MANIFEST_DIR - - The wit_bindgen crate must be available as a dependency - """ - - # Get WIT info to set up paths - wit_library_target = wit - - # Define wit-bindgen dependency - if symmetric: - # For symmetric mode, would need cpetig's fork - # This would require additional configuration - fail("Symmetric mode not yet implemented for macro approach") - - wit_bindgen_dep = wit_bindgen_crate - - # Create a rust_library for the host platform - host_lib = name + "_host" - rust_library( - name = host_lib, - srcs = srcs, - deps = deps + [wit_bindgen_dep], - crate_features = crate_features + _get_macro_features(generation_mode), - rustc_flags = rustc_flags, - edition = "2021", - compile_data = [wit_library_target], - rustc_env = _build_rustc_env(wit_library_target, generation_mode), - visibility = visibility, - ) - - # Create a WASM-platform version - wasm_lib_base = name + "_wasm_base" - rust_library( - name = wasm_lib_base, - srcs = srcs, - deps = deps + [wit_bindgen_dep], - crate_features = crate_features + _get_macro_features("guest"), - rustc_flags = rustc_flags, - edition = "2021", - compile_data = [wit_library_target], - rustc_env = _build_rustc_env(wit_library_target, "guest"), - visibility = ["//visibility:private"], - ) - - # Apply WASM transition - wasm_lib = name + "_wasm_lib" - _wasm_rust_library_macro( - name = wasm_lib, - target = ":" + wasm_lib_base, - visibility = ["//visibility:private"], - ) - - # Build the final WASM component - rust_wasm_component( - name = name, - srcs = srcs, - deps = [":" + wasm_lib], - wit = wit, - profiles = profiles, - visibility = visibility, - **kwargs - ) - -def _get_macro_features(generation_mode): - """Get additional crate features needed for wit-bindgen macro mode""" - if generation_mode == "native-guest": - return ["std"] # Enable std runtime for native execution - return [] # Default guest mode - -def _build_rustc_env(wit_target, generation_mode): - """Build rustc_env dictionary for wit-bindgen macro compilation""" - - # Base environment variables that wit-bindgen macros expect - env = { - # Standard Cargo environment variables - "CARGO_MANIFEST_DIR": "$(execpath " + wit_target + ")", - "CARGO_PKG_NAME": "generated", - "CARGO_PKG_VERSION": "0.1.0", - - # WIT-specific paths - wit-bindgen macros look for WIT files relative to CARGO_MANIFEST_DIR - "WIT_ROOT_DIR": "$(execpath " + wit_target + ")", - } - - # Generation mode specific configuration - if generation_mode == "native-guest": - env["WIT_BINDGEN_RT_MODE"] = "native" - else: - env["WIT_BINDGEN_RT_MODE"] = "wasm" - - return env