fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
b5ecaea4
patching
1"""Bazel rules for [wasm-bindgen](https://crates.io/crates/wasm-bindgen)""" 2 3load ( "//rust:defs.bzl" , "rust_common" ) 4load ( "//wasm_bindgen:providers.bzl" , "RustWasmBindgenInfo" ) 5load ( "//wasm_bindgen/private: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 ], 81) 82 83return RustWasmBindgenInfo ( 84wasm = bindgen_wasm_module , 85js = depset ( js_out ), 86ts = depset ( ts_out ), 87) 88 89def _rust_wasm_bindgen_impl ( ctx ): 90toolchain = ctx . toolchains [ Label ( "//wasm_bindgen:toolchain_type" )] 91 92info = rust_wasm_bindgen_action ( 93ctx = ctx , 94toolchain = toolchain , 95wasm_file = ctx . attr . wasm_file , 96target_output = ctx . attr . target , 97bindgen_flags = ctx . attr . bindgen_flags , 98) 99 100return [ 101DefaultInfo ( 102files = depset ([ info . wasm ], transitive = [ info . js , info . ts ]), 103), 104info , 105] 106 107WASM_BINDGEN_ATTR = { 108"bindgen_flags" : attr . string_list ( 109doc = "Flags to pass directly to the bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details." , 110), 111"target" : attr . string ( 112doc = "The type of output to generate. See https://rustwasm.github.io/wasm-bindgen/reference/deployment.html for details." , 113default = "bundler" , 114values = [ "web" , "bundler" , "nodejs" , "no-modules" , "deno" ], 115), 116"wasm_file" : attr . label ( 117doc = "The `.wasm` file or crate to generate bindings for." , 118allow_single_file = True , 119cfg = wasm_bindgen_transition , 120mandatory = True , 121), 122"_allowlist_function_transition" : attr . label ( 123default = Label ( "//tools/allowlists/function_transition_allowlist" ), 124), 125} 126 127rust_wasm_bindgen = rule ( 128implementation = _rust_wasm_bindgen_impl , 129doc = """\ 130Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws]. 131 132[ws]: https://rustwasm.github.io/docs/wasm-bindgen/ 133 134An example of this rule in use can be seen at [@rules_rust//examples/wasm](../examples/wasm) 135""" , 136attrs = { 137"bindgen_flags" : attr . string_list ( 138doc = "Flags to pass directly to the bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details." , 139), 140"target" : attr . string ( 141doc = "The type of output to generate. See https://rustwasm.github.io/wasm-bindgen/reference/deployment.html for details." , 142default = "bundler" , 143values = [ "web" , "bundler" , "nodejs" , "no-modules" , "deno" ], 144), 145"wasm_file" : attr . label ( 146doc = "The `.wasm` file or crate to generate bindings for." , 147allow_single_file = True , 148cfg = wasm_bindgen_transition , 149mandatory = True , 150), 151"_allowlist_function_transition" : attr . label ( 152default = Label ( "//tools/allowlists/function_transition_allowlist" ), 153), 154}, 155toolchains = [ 156str ( Label ( "//wasm_bindgen:toolchain_type" )), 157], 158) 159 160def _rust_wasm_bindgen_toolchain_impl ( ctx ): 161return platform_common . ToolchainInfo ( 162bindgen = ctx . executable . bindgen , 163) 164 165rust_wasm_bindgen_toolchain = rule ( 166implementation = _rust_wasm_bindgen_toolchain_impl , 167doc = """\ 168The tools required for the `rust_wasm_bindgen` rule. 169 170In cases where users want to control or change the version of `wasm-bindgen` used by [rust_wasm_bindgen](#rust_wasm_bindgen), 171a unique toolchain can be created as in the example below: 172 173```python 174load("@rules_rust//bindgen:bindgen.bzl", "rust_bindgen_toolchain") 175 176rust_bindgen_toolchain( 177bindgen = "//3rdparty/crates:wasm_bindgen_cli__bin", 178) 179 180toolchain( 181name = "wasm_bindgen_toolchain", 182toolchain = "wasm_bindgen_toolchain_impl", 183toolchain_type = "@rules_rust//wasm_bindgen:toolchain_type", 184) 185``` 186 187Now that you have your own toolchain, you need to register it by 188inserting the following statement in your `WORKSPACE` file: 189 190```python 191register_toolchains("//my/toolchains:wasm_bindgen_toolchain") 192``` 193 194For additional information, see the [Bazel toolchains documentation][toolchains]. 195 196[toolchains]: https://docs.bazel.build/versions/master/toolchains.html 197""" , 198attrs = { 199"bindgen" : attr . label ( 200doc = "The label of a `wasm-bindgen-cli` executable." , 201executable = True , 202cfg = "exec" , 203), 204}, 205)