|
3 | 3 | load("@rules_rust//rust:defs.bzl", "rust_library") |
4 | 4 | load("//wit:wit_bindgen.bzl", "wit_bindgen") |
5 | 5 | load(":rust_wasm_component.bzl", "rust_wasm_component") |
| 6 | +load(":transitions.bzl", "wasm_transition") |
6 | 7 |
|
7 | 8 | def _generate_wrapper_impl(ctx): |
8 | 9 | """Generate a wrapper that includes both bindings and runtime shim""" |
@@ -85,6 +86,31 @@ _generate_wrapper = rule( |
85 | 86 | }, |
86 | 87 | ) |
87 | 88 |
|
| 89 | +def _wasm_rust_library_impl(ctx): |
| 90 | + """Implementation of wasm_rust_library rule""" |
| 91 | + # This rule just passes through to the rust_library target |
| 92 | + # The transition is handled by the cfg attribute |
| 93 | + target_info = ctx.attr.target[0] |
| 94 | + |
| 95 | + # Return the providers we need |
| 96 | + providers = [target_info[DefaultInfo]] |
| 97 | + |
| 98 | + # Add Rust-specific providers if they exist |
| 99 | + if CcInfo in target_info: |
| 100 | + providers.append(target_info[CcInfo]) |
| 101 | + |
| 102 | + return providers |
| 103 | + |
| 104 | +_wasm_rust_library = rule( |
| 105 | + implementation = _wasm_rust_library_impl, |
| 106 | + attrs = { |
| 107 | + "target": attr.label( |
| 108 | + cfg = wasm_transition, |
| 109 | + doc = "rust_library target to build for WASM", |
| 110 | + ), |
| 111 | + }, |
| 112 | +) |
| 113 | + |
88 | 114 | def rust_wasm_component_bindgen( |
89 | 115 | name, |
90 | 116 | srcs, |
@@ -150,11 +176,21 @@ def rust_wasm_component_bindgen( |
150 | 176 |
|
151 | 177 | # Create a rust_library from the generated bindings |
152 | 178 | bindings_lib = name + "_bindings" |
| 179 | + bindings_lib_host = bindings_lib + "_host" |
| 180 | + |
| 181 | + # Create the bindings library for host platform first |
153 | 182 | rust_library( |
154 | | - name = bindings_lib, |
| 183 | + name = bindings_lib_host, |
155 | 184 | srcs = [":" + wrapper_target], |
156 | 185 | crate_name = name.replace("-", "_") + "_bindings", |
157 | 186 | edition = "2021", |
| 187 | + visibility = ["//visibility:private"], |
| 188 | + ) |
| 189 | + |
| 190 | + # Create a WASM-transitioned version of the bindings library |
| 191 | + _wasm_rust_library( |
| 192 | + name = bindings_lib, |
| 193 | + target = ":" + bindings_lib_host, |
158 | 194 | visibility = ["//visibility:public"], |
159 | 195 | ) |
160 | 196 |
|
|
0 commit comments