fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
83a03ab0
0.11.0
1# Copyright 2018 The Bazel Authors. All rights reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""Toolchain for compiling rust stubs from protobuf and gRPC.""" 16 17# buildifier: disable=bzl-visibility 18load ( "//rust/private:utils.bzl" , "name_to_crate_name" ) 19 20def generated_file_stem ( file_path ): 21"""Returns the basename of a file without any extensions. 22 23Example: 24```python 25content.append("pub mod %s;" % _generated_file_stem(f)) 26``` 27 28Args: 29file_path (string): A path to a file 30 31Returns: 32string: The file stem of the filename 33""" 34basename = file_path . rsplit ( "/" , 2 )[ - 1 ] 35basename = name_to_crate_name ( basename ) 36return basename . rsplit ( "." , 2 )[ 0 ] 37 38def rust_generate_proto ( 39ctx , 40transitive_descriptor_sets , 41protos , 42imports , 43output_dir , 44proto_toolchain , 45is_grpc = False ): 46"""Generate a proto compilation action. 47 48Args: 49ctx (ctx): rule context. 50transitive_descriptor_sets (depset): descriptor generated by previous protobuf libraries. 51protos (list): list of paths of protos to compile. 52imports (depset): directory, relative to the package, to output the list of stubs. 53output_dir (str): The basename of the output directory for for the output generated stubs 54proto_toolchain (ToolchainInfo): The toolchain for rust-proto compilation. See `rust_proto_toolchain` 55is_grpc (bool, optional): generate gRPC stubs. Defaults to False. 56 57Returns: 58list: the list of generate stubs (File) 59""" 60 61tools = [ 62proto_toolchain . protoc , 63proto_toolchain . proto_plugin , 64] 65executable = proto_toolchain . protoc 66args = ctx . actions . args () 67 68if not protos : 69fail ( "Protobuf compilation requested without inputs!" ) 70paths = [ "%s/%s" % ( output_dir , generated_file_stem ( i )) for i in protos . to_list ()] 71outs = [ ctx . actions . declare_file ( path + ".rs" ) for path in paths ] 72output_directory = outs [ 0 ]. dirname 73 74if is_grpc : 75# Add grpc stubs to the list of outputs 76grpc_files = [ ctx . actions . declare_file ( path + "_grpc.rs" ) for path in paths ] 77outs . extend ( grpc_files ) 78 79# gRPC stubs is generated only if a service is defined in the proto, 80# so we create an empty grpc module in the other case. 81tools . append ( proto_toolchain . grpc_plugin ) 82tools . append ( ctx . executable . _optional_output_wrapper ) 83args . add_all ([ f . path for f in grpc_files ]) 84args . add_all ([ 85"--" , 86proto_toolchain . protoc . path , 87"--plugin=protoc-gen-grpc-rust=" + proto_toolchain . grpc_plugin . path , 88"--grpc-rust_out=" + output_directory , 89]) 90executable = ctx . executable . _optional_output_wrapper 91 92args . add_all ([ 93"--plugin=protoc-gen-rust=" + proto_toolchain . proto_plugin . path , 94"--rust_out=" + output_directory , 95]) 96 97args . add_joined ( 98transitive_descriptor_sets , 99join_with = ":" , 100format_joined = "--descriptor_set_in=%s" , 101) 102 103args . add_all ( protos ) 104ctx . actions . run ( 105inputs = depset ( 106transitive = [ 107transitive_descriptor_sets , 108imports , 109], 110), 111outputs = outs , 112tools = tools , 113progress_message = "Generating Rust protobuf stubs" , 114mnemonic = "RustProtocGen" , 115executable = executable , 116arguments = [ args ], 117) 118return outs 119 120def _rust_proto_toolchain_impl ( ctx ): 121return platform_common . ToolchainInfo ( 122edition = ctx . attr . edition , 123grpc_compile_deps = ctx . attr . grpc_compile_deps , 124grpc_plugin = ctx . file . grpc_plugin , 125proto_compile_deps = ctx . attr . proto_compile_deps , 126proto_plugin = ctx . file . proto_plugin , 127protoc = ctx . executable . protoc , 128) 129 130# Default dependencies needed to compile protobuf stubs. 131PROTO_COMPILE_DEPS = [ 132Label ( "//proto/3rdparty/crates:protobuf" ), 133] 134 135# Default dependencies needed to compile gRPC stubs. 136GRPC_COMPILE_DEPS = PROTO_COMPILE_DEPS + [ 137Label ( "//proto/3rdparty/crates:grpc" ), 138Label ( "//proto/3rdparty/crates:tls-api" ), 139Label ( "//proto/3rdparty/crates:tls-api-stub" ), 140] 141 142rust_proto_toolchain = rule ( 143implementation = _rust_proto_toolchain_impl , 144attrs = { 145"edition" : attr . string ( 146doc = "The edition used by the generated rust source." , 147), 148"grpc_compile_deps" : attr . label_list ( 149doc = "The crates the generated grpc libraries depends on." , 150cfg = "target" , 151default = GRPC_COMPILE_DEPS , 152), 153"grpc_plugin" : attr . label ( 154doc = "The location of the Rust protobuf compiler plugin to generate rust gRPC stubs." , 155allow_single_file = True , 156cfg = "exec" , 157default = Label ( "//proto:protoc_gen_rust_grpc" ), 158), 159"proto_compile_deps" : attr . label_list ( 160doc = "The crates the generated protobuf libraries depends on." , 161cfg = "target" , 162default = PROTO_COMPILE_DEPS , 163), 164"proto_plugin" : attr . label ( 165doc = "The location of the Rust protobuf compiler plugin used to generate rust sources." , 166allow_single_file = True , 167cfg = "exec" , 168default = Label ( "//proto:protoc_gen_rust" ), 169), 170"protoc" : attr . label ( 171doc = "The location of the `protoc` binary. It should be an executable target." , 172executable = True , 173cfg = "exec" , 174default = Label ( "@com_google_protobuf//:protoc" ), 175), 176}, 177doc = """\ 178Declares a Rust Proto toolchain for use. 179 180This is used to configure proto compilation and can be used to set different \ 181protobuf compiler plugin. 182 183Example: 184 185Suppose a new nicer gRPC plugin has came out. The new plugin can be \ 186used in Bazel by defining a new toolchain definition and declaration: 187 188```python 189load('@rules_rust//proto:toolchain.bzl', 'rust_proto_toolchain') 190 191rust_proto_toolchain( 192name="rust_proto_impl", 193grpc_plugin="@rust_grpc//:grpc_plugin", 194grpc_compile_deps=["@rust_grpc//:grpc_deps"], 195) 196 197toolchain( 198name="rust_proto", 199exec_compatible_with = [ 200"@platforms//cpu:cpuX", 201], 202target_compatible_with = [ 203"@platforms//cpu:cpuX", 204], 205toolchain = ":rust_proto_impl", 206) 207``` 208 209Then, either add the label of the toolchain rule to register_toolchains in the WORKSPACE, or pass \ 210it to the `--extra_toolchains` flag for Bazel, and it will be used. 211 212See @rules_rust//proto:BUILD for examples of defining the toolchain. 213""" , 214)