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