fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
13746789
main
1"""Bazel rules for [wasm-bindgen](https://crates.io/crates/wasm-bindgen)""" 2 3load ( "@rules_rust//rust:defs.bzl" , "rust_common" ) 4 5# buildifier: disable=bzl-visibility 6load ( "@rules_rust//rust/private:providers.bzl" , "RustAnalyzerGroupInfo" , "RustAnalyzerInfo" ) 7load ( "//:providers.bzl" , "RustWasmBindgenInfo" ) 8load ( ":transitions.bzl" , "wasm_bindgen_transition" ) 9 10def rust_wasm_bindgen_action ( * , ctx , toolchain , wasm_file , target_output , flags = []): 11"""Spawn a `RustWasmBindgen` action. 12 13Args: 14ctx (ctx): The rule's context object. 15toolchain (ToolchainInfo): The current `rust_wasm_bindgen_toolchain`. 16wasm_file (Target): The target representing the `.wasm` file. 17target_output (str): _description_ 18flags (list, optional): Flags to pass to `wasm-bindgen`. 19 20Returns: 21RustWasmBindgenInfo: A provider containing action outputs. 22""" 23bindgen_bin = toolchain . bindgen 24 25# Since the `wasm_file` attribute is behind a transition, it will be converted 26# to a list. 27if len ( wasm_file ) == 1 : 28if rust_common . crate_info in wasm_file [ 0 ]: 29target = wasm_file [ 0 ] 30crate_info = target [ rust_common . crate_info ] 31 32# Provide a helpful warning informing users how to use the rule 33if rust_common . crate_info in target : 34supported_types = [ "cdylib" , "bin" ] 35if crate_info . type not in supported_types : 36fail ( "The target '{}' is not a supported type: {}" . format ( 37ctx . attr . crate . label , 38supported_types , 39)) 40 41progress_message_label = target . label 42input_file = crate_info . output 43else : 44wasm_files = wasm_file [ 0 ][ DefaultInfo ]. files . to_list () 45if len ( wasm_files ) != 1 : 46fail ( "Unexpected number of wasm files: {}" . format ( wasm_files )) 47 48progress_message_label = wasm_files [ 0 ]. path 49input_file = wasm_files [ 0 ] 50else : 51fail ( "wasm_file is expected to be a transitioned label attr on `{}`. Got `{}`" . format ( 52ctx . label , 53wasm_file , 54)) 55 56bindgen_wasm_module = ctx . actions . declare_file ( ctx . label . name + "_bg.wasm" ) 57 58js_out = [ ctx . actions . declare_file ( ctx . label . name + ".js" )] 59ts_out = [] 60if not "--no-typescript" in flags : 61ts_out . append ( ctx . actions . declare_file ( ctx . label . name + ".d.ts" )) 62 63if target_output == "bundler" : 64js_out . append ( ctx . actions . declare_file ( ctx . label . name + "_bg.js" )) 65if not "--no-typescript" in flags : 66ts_out . append ( ctx . actions . declare_file ( ctx . label . name + "_bg.wasm.d.ts" )) 67 68outputs = [ bindgen_wasm_module ] + js_out + ts_out 69 70args = ctx . actions . args () 71args . add ( "--target" , target_output ) 72args . add ( "--out-dir" , bindgen_wasm_module . dirname ) 73args . add ( "--out-name" , ctx . label . name ) 74args . add_all ( flags ) 75args . add ( input_file ) 76 77ctx . actions . run ( 78executable = bindgen_bin , 79inputs = [ input_file ], 80outputs = outputs , 81mnemonic = "RustWasmBindgen" , 82progress_message = "Generating WebAssembly bindings for {}" . format ( progress_message_label ), 83arguments = [ args ], 84toolchain = str ( Label ( "//:toolchain_type" )), 85) 86 87return RustWasmBindgenInfo ( 88wasm = bindgen_wasm_module , 89js = depset ( js_out ), 90ts = depset ( ts_out ), 91) 92 93def _rust_wasm_bindgen_impl ( ctx ): 94toolchain = ctx . toolchains [ Label ( "//:toolchain_type" )] 95 96info = rust_wasm_bindgen_action ( 97ctx = ctx , 98toolchain = toolchain , 99wasm_file = ctx . attr . wasm_file , 100target_output = ctx . attr . target , 101flags = ctx . attr . bindgen_flags , 102) 103 104providers = [ 105DefaultInfo ( 106files = depset ([ info . wasm ], transitive = [ info . js , info . ts ]), 107), 108info , 109] 110 111if RustAnalyzerGroupInfo in ctx . attr . wasm_file : 112providers . append ( ctx . attr . wasm_file [ RustAnalyzerGroupInfo ]) 113 114if RustAnalyzerInfo in ctx . attr . wasm_file : 115providers . append ( ctx . attr . wasm_file [ RustAnalyzerInfo ]) 116 117return providers 118 119WASM_BINDGEN_ATTR = { 120"bindgen_flags" : attr . string_list ( 121doc = "Flags to pass directly to the wasm-bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details." , 122), 123"target" : attr . string ( 124doc = "The type of output to generate. See https://rustwasm.github.io/wasm-bindgen/reference/deployment.html for details." , 125default = "bundler" , 126values = [ "web" , "bundler" , "nodejs" , "no-modules" , "deno" ], 127), 128"target_arch" : attr . string ( 129doc = "The target architecture to use for the wasm-bindgen command line option." , 130default = "wasm32" , 131values = [ "wasm32" , "wasm64" ], 132), 133"wasm_file" : attr . label ( 134doc = "The `.wasm` file or crate to generate bindings for." , 135allow_single_file = True , 136cfg = wasm_bindgen_transition , 137mandatory = True , 138), 139"_allowlist_function_transition" : attr . label ( 140default = "@bazel_tools//tools/allowlists/function_transition_allowlist" , 141), 142} 143 144rust_wasm_bindgen = rule ( 145implementation = _rust_wasm_bindgen_impl , 146doc = """\ 147Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws]. 148 149[ws]: https://rustwasm.github.io/docs/wasm-bindgen/ 150 151An example of this rule in use can be seen at [@rules_rust//examples/wasm](../examples/wasm) 152""" , 153attrs = { 154"bindgen_flags" : attr . string_list ( 155doc = "Flags to pass directly to the bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details." , 156), 157"target" : attr . string ( 158doc = "The type of output to generate. See https://rustwasm.github.io/wasm-bindgen/reference/deployment.html for details." , 159default = "bundler" , 160values = [ "web" , "bundler" , "nodejs" , "no-modules" , "deno" ], 161), 162"target_arch" : attr . string ( 163doc = "The target architecture to use for the wasm-bindgen command line option." , 164default = "wasm32" , 165values = [ "wasm32" , "wasm64" ], 166), 167"wasm_file" : attr . label ( 168doc = "The `.wasm` file or crate to generate bindings for." , 169allow_single_file = True , 170cfg = wasm_bindgen_transition , 171mandatory = True , 172), 173"_allowlist_function_transition" : attr . label ( 174default = "@bazel_tools//tools/allowlists/function_transition_allowlist" , 175), 176}, 177toolchains = [ 178str ( Label ( "//:toolchain_type" )), 179], 180) 181 182def _rust_wasm_bindgen_toolchain_impl ( ctx ): 183return platform_common . ToolchainInfo ( 184bindgen = ctx . executable . bindgen , 185) 186 187rust_wasm_bindgen_toolchain = rule ( 188implementation = _rust_wasm_bindgen_toolchain_impl , 189doc = """\ 190The tools required for the `rust_wasm_bindgen` rule. 191 192In cases where users want to control or change the version of `wasm-bindgen` used by [rust_wasm_bindgen](#rust_wasm_bindgen), 193a unique toolchain can be created as in the example below: 194 195```python 196load("@rules_rust_bindgen//:defs.bzl", "rust_bindgen_toolchain") 197 198rust_bindgen_toolchain( 199bindgen = "//3rdparty/crates:wasm_bindgen_cli__bin", 200) 201 202toolchain( 203name = "wasm_bindgen_toolchain", 204toolchain = "wasm_bindgen_toolchain_impl", 205toolchain_type = "@rules_rust_wasm_bindgen//:toolchain_type", 206) 207``` 208 209Now that you have your own toolchain, you need to register it by 210inserting the following statement in your `WORKSPACE` file: 211 212```python 213register_toolchains("//my/toolchains:wasm_bindgen_toolchain") 214``` 215 216For additional information, see the [Bazel toolchains documentation][toolchains]. 217 218[toolchains]: https://docs.bazel.build/versions/master/toolchains.html 219""" , 220attrs = { 221"bindgen" : attr . label ( 222doc = "The label of a `wasm-bindgen-cli` executable." , 223executable = True , 224cfg = "exec" , 225), 226}, 227)