fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
9803d303
rust-analyzer
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"""Rust Protobuf Rules 16 17These build rules are used for building [protobufs][protobuf]/[gRPC][grpc] in [Rust][rust] with Bazel. 18 19[rust]: http://www.rust-lang.org/ 20[protobuf]: https://developers.google.com/protocol-buffers/ 21[grpc]: https://grpc.io 22 23### Setup 24 25To use the Rust proto rules, add the following to your `WORKSPACE` file to add the 26external repositories for the Rust proto toolchain (in addition to the [rust rules setup](..)): 27 28```python 29load("//proto:repositories.bzl", "rust_proto_repositories") 30 31rust_proto_repositories() 32``` 33""" 34 35load ( "@rules_proto//proto:defs.bzl" , "ProtoInfo" ) 36load ( 37"//proto:toolchain.bzl" , 38_generate_proto = "rust_generate_proto" , 39_generated_file_stem = "generated_file_stem" , 40) 41load ( "//rust:defs.bzl" , "rust_common" ) 42 43# buildifier: disable=bzl-visibility 44load ( "//rust/private:rustc.bzl" , "rustc_compile_action" ) 45 46# buildifier: disable=bzl-visibility 47load ( "//rust/private:utils.bzl" , "can_build_metadata" , "compute_crate_name" , "determine_output_hash" , "find_toolchain" , "transform_deps" ) 48 49RustProtoInfo = provider ( 50doc = "Rust protobuf provider info" , 51fields = { 52"proto_sources" : "List[string]: list of source paths of protos" , 53"transitive_proto_sources" : "depset[string]" , 54}, 55) 56 57def _compute_proto_source_path ( file , source_root_attr ): 58"""Take the short path of file and make it suitable for protoc. 59 60Args: 61file (File): The target source file. 62source_root_attr (str): The directory relative to which the `.proto` \ 63files defined in the proto_library are defined. 64 65Returns: 66str: The protoc suitible path of `file` 67""" 68 69# Bazel creates symlinks to the .proto files under a directory called 70# "_virtual_imports/<rule name>" if we do any sort of munging of import 71# paths (e.g. using strip_import_prefix / import_prefix attributes) 72virtual_imports = "/_virtual_imports/" 73if virtual_imports in file . path : 74return file . path . split ( virtual_imports )[ 1 ]. split ( "/" , 1 )[ 1 ] 75 76# For proto, they need to be requested with their absolute name to be 77# compatible with the descriptor_set passed by proto_library. 78# I.e. if you compile a protobuf at @repo1//package:file.proto, the proto 79# compiler would generate a file descriptor with the path 80# `package/file.proto`. Since we compile from the proto descriptor, we need 81# to pass the list of descriptors and the list of path to compile. 82# For the precedent example, the file (noted `f`) would have 83# `f.short_path` returns `external/repo1/package/file.proto`. 84# In addition, proto_library can provide a proto_source_path to change the base 85# path, which should a be a prefix. 86path = file . short_path 87 88# Strip external prefix. 89path = path . split ( "/" , 2 )[ 2 ] if path . startswith ( "../" ) else path 90 91# Strip source_root. 92if path . startswith ( source_root_attr ): 93return path [ len ( source_root_attr ):] 94else : 95return path 96 97def _rust_proto_aspect_impl ( target , ctx ): 98"""The implementation of the `rust_proto_aspect` aspect 99 100Args: 101target (Target): The target to which the aspect is applied 102ctx (ctx): The rule context which the targetis created from 103 104Returns: 105list: A list containg a `RustProtoInfo` provider 106""" 107if ProtoInfo not in target : 108return None 109 110if hasattr ( ctx . rule . attr , "proto_source_root" ): 111source_root = ctx . rule . attr . proto_source_root 112else : 113source_root = "" 114 115if source_root and source_root [ - 1 ] != "/" : 116source_root += "/" 117 118sources = [ 119_compute_proto_source_path ( f , source_root ) 120for f in target [ ProtoInfo ]. direct_sources 121] 122transitive_sources = [ 123f [ RustProtoInfo ]. transitive_proto_sources 124for f in ctx . rule . attr . deps 125if RustProtoInfo in f 126] 127return [ RustProtoInfo ( 128proto_sources = sources , 129transitive_proto_sources = depset ( transitive = transitive_sources , direct = sources ), 130)] 131 132_rust_proto_aspect = aspect ( 133doc = "An aspect that gathers rust proto direct and transitive sources" , 134implementation = _rust_proto_aspect_impl , 135attr_aspects = [ "deps" ], 136) 137 138def _gen_lib ( ctx , grpc , srcs , lib ): 139"""Generate a lib.rs file for the crates. 140 141Args: 142ctx (ctx): The current rule's context object 143grpc (bool): True if the current rule is a `gRPC` rule. 144srcs (list): A list of protoc suitible file paths (str). 145lib (File): The File object where the rust source file should be written 146""" 147content = [ "extern crate protobuf;" ] 148if grpc : 149content . append ( "extern crate grpc;" ) 150content . append ( "extern crate tls_api;" ) 151for f in srcs . to_list (): 152content . append ( "pub mod %s;" % _generated_file_stem ( f )) 153content . append ( "pub use %s::*;" % _generated_file_stem ( f )) 154if grpc : 155content . append ( "pub mod %s_grpc;" % _generated_file_stem ( f )) 156content . append ( "pub use %s_grpc::*;" % _generated_file_stem ( f )) 157ctx . actions . write ( lib , "\n ". join ( content )) 158 159def _expand_provider ( lst , provider ): 160"""Gathers a list of a specific provider from a list of targets. 161 162Args: 163lst (list): A list of Targets 164provider (Provider): The target provider type to extract `lst` 165 166Returns: 167list: A list of providers of the type from `provider`. 168""" 169return [ el [ provider ] for el in lst if provider in el ] 170 171def _rust_proto_compile ( protos , descriptor_sets , imports , crate_name , ctx , is_grpc , compile_deps , toolchain ): 172"""Create and run a rustc compile action based on the current rule's attributes 173 174Args: 175protos (depset): Paths of protos to compile. 176descriptor_sets (depset): A set of transitive protobuf `FileDescriptorSet`s 177imports (depset): A set of transitive protobuf Imports. 178crate_name (str): The name of the Crate for the current target 179ctx (ctx): The current rule's context object 180is_grpc (bool): True if the current rule is a `gRPC` rule. 181compile_deps (list): A list of Rust dependencies (`List[Target]`) 182toolchain (rust_toolchain): the current `rust_toolchain`. 183 184Returns: 185list: A list of providers, see `rustc_compile_action` 186""" 187 188# Create all the source in a specific folder 189proto_toolchain = ctx . toolchains [ Label ( "//proto:toolchain_type" )] 190output_dir = "%s.%s.rust" % ( crate_name , "grpc" if is_grpc else "proto" ) 191 192# Generate the proto stubs 193srcs = _generate_proto ( 194ctx , 195descriptor_sets , 196protos = protos , 197imports = imports , 198output_dir = output_dir , 199proto_toolchain = proto_toolchain , 200is_grpc = is_grpc , 201) 202 203# and lib.rs 204lib_rs = ctx . actions . declare_file ( "%s/lib.rs" % output_dir ) 205_gen_lib ( ctx , is_grpc , protos , lib_rs ) 206srcs . append ( lib_rs ) 207 208# And simulate rust_library behavior 209output_hash = determine_output_hash ( lib_rs , ctx . label ) 210rust_lib = ctx . actions . declare_file ( "%s/lib%s-%s.rlib" % ( 211output_dir , 212crate_name , 213output_hash , 214)) 215rust_metadata = None 216if can_build_metadata ( toolchain , ctx , "rlib" ): 217rust_metadata = ctx . actions . declare_file ( "%s/lib%s-%s.rmeta" % ( 218output_dir , 219crate_name , 220output_hash , 221)) 222 223# Gather all dependencies for compilation 224compile_action_deps = depset ( 225transform_deps ( 226compile_deps + 227proto_toolchain . grpc_compile_deps if is_grpc else proto_toolchain . proto_compile_deps , 228), 229) 230 231return rustc_compile_action ( 232ctx = ctx , 233attr = ctx . attr , 234toolchain = toolchain , 235crate_info = rust_common . create_crate_info ( 236name = crate_name , 237type = "rlib" , 238root = lib_rs , 239srcs = depset ( srcs ), 240deps = compile_action_deps , 241proc_macro_deps = depset ([]), 242aliases = {}, 243output = rust_lib , 244metadata = rust_metadata , 245edition = proto_toolchain . edition , 246rustc_env = {}, 247is_test = False , 248compile_data = depset ([ target . files for target in getattr ( ctx . attr , "compile_data" , [])]), 249compile_data_targets = depset ( getattr ( ctx . attr , "compile_data" , [])), 250wrapped_crate_type = None , 251owner = ctx . label , 252), 253output_hash = output_hash , 254) 255 256def _rust_protogrpc_library_impl ( ctx , is_grpc ): 257"""Implementation of the rust_(proto|grpc)_library. 258 259Args: 260ctx (ctx): The current rule's context object 261is_grpc (bool): True if the current rule is a `gRPC` rule. 262 263Returns: 264list: A list of providers, see `_rust_proto_compile` 265""" 266proto = _expand_provider ( ctx . attr . deps , ProtoInfo ) 267transitive_sources = [ 268f [ RustProtoInfo ]. transitive_proto_sources 269for f in ctx . attr . deps 270if RustProtoInfo in f 271] 272 273toolchain = find_toolchain ( ctx ) 274crate_name = compute_crate_name ( ctx . workspace_name , ctx . label , toolchain ) 275 276return _rust_proto_compile ( 277protos = depset ( transitive = transitive_sources ), 278descriptor_sets = depset ( transitive = [ p . transitive_descriptor_sets for p in proto ]), 279imports = depset ( transitive = [ p . transitive_imports for p in proto ]), 280crate_name = crate_name , 281ctx = ctx , 282is_grpc = is_grpc , 283compile_deps = ctx . attr . rust_deps , 284toolchain = toolchain , 285) 286 287def _rust_proto_library_impl ( ctx ): 288"""The implementation of the `rust_proto_library` rule 289 290Args: 291ctx (ctx): The rule's context object. 292 293Returns: 294list: A list of providers, see `_rust_protogrpc_library_impl` 295""" 296return _rust_protogrpc_library_impl ( ctx , False ) 297 298rust_proto_library = rule ( 299implementation = _rust_proto_library_impl , 300attrs = { 301"deps" : attr . label_list ( 302doc = ( 303"List of proto_library dependencies that will be built. " + 304"One crate for each proto_library will be created with the corresponding stubs." 305), 306mandatory = True , 307providers = [ ProtoInfo ], 308aspects = [ _rust_proto_aspect ], 309), 310"rust_deps" : attr . label_list ( 311doc = "The crates the generated library depends on." , 312), 313"_cc_toolchain" : attr . label ( 314default = Label ( "@bazel_tools//tools/cpp:current_cc_toolchain" ), 315), 316"_optional_output_wrapper" : attr . label ( 317executable = True , 318cfg = "exec" , 319default = Label ( "//proto:optional_output_wrapper" ), 320), 321"_process_wrapper" : attr . label ( 322default = Label ( "//util/process_wrapper" ), 323executable = True , 324allow_single_file = True , 325cfg = "exec" , 326), 327}, 328fragments = [ "cpp" ], 329host_fragments = [ "cpp" ], 330toolchains = [ 331str ( Label ( "//proto:toolchain_type" )), 332str ( Label ( "//rust:toolchain_type" )), 333"@bazel_tools//tools/cpp:toolchain_type" , 334], 335# TODO: Remove once (bazelbuild/bazel#11584) is closed and the rules use 336# the version of Bazel that issue was closed on as the min supported version 337incompatible_use_toolchain_transition = True , 338doc = """\ 339Builds a Rust library crate from a set of `proto_library`s. 340 341Example: 342 343```python 344load("@rules_rust//proto:proto.bzl", "rust_proto_library") 345 346proto_library( 347name = "my_proto", 348srcs = ["my.proto"] 349) 350 351rust_proto_library( 352name = "rust", 353deps = [":my_proto"], 354) 355 356rust_binary( 357name = "my_proto_binary", 358srcs = ["my_proto_binary.rs"], 359deps = [":rust"], 360) 361``` 362""" , 363) 364 365def _rust_grpc_library_impl ( ctx ): 366"""The implementation of the `rust_grpc_library` rule 367 368Args: 369ctx (ctx): The rule's context object 370 371Returns: 372list: A list of providers. See `_rust_protogrpc_library_impl` 373""" 374return _rust_protogrpc_library_impl ( ctx , True ) 375 376rust_grpc_library = rule ( 377implementation = _rust_grpc_library_impl , 378attrs = { 379"deps" : attr . label_list ( 380doc = ( 381"List of proto_library dependencies that will be built. " + 382"One crate for each proto_library will be created with the corresponding gRPC stubs." 383), 384mandatory = True , 385providers = [ ProtoInfo ], 386aspects = [ _rust_proto_aspect ], 387), 388"rust_deps" : attr . label_list ( 389doc = "The crates the generated library depends on." , 390), 391"_cc_toolchain" : attr . label ( 392default = "@bazel_tools//tools/cpp:current_cc_toolchain" , 393), 394"_optional_output_wrapper" : attr . label ( 395executable = True , 396cfg = "exec" , 397default = Label ( "//proto:optional_output_wrapper" ), 398), 399"_process_wrapper" : attr . label ( 400default = Label ( "//util/process_wrapper" ), 401executable = True , 402allow_single_file = True , 403cfg = "exec" , 404), 405}, 406fragments = [ "cpp" ], 407host_fragments = [ "cpp" ], 408toolchains = [ 409str ( Label ( "//proto:toolchain_type" )), 410str ( Label ( "//rust:toolchain_type" )), 411"@bazel_tools//tools/cpp:toolchain_type" , 412], 413# TODO: Remove once (bazelbuild/bazel#11584) is closed and the rules use 414# the version of Bazel that issue was closed on as the min supported version 415incompatible_use_toolchain_transition = True , 416doc = """\ 417Builds a Rust library crate from a set of `proto_library`s suitable for gRPC. 418 419Example: 420 421```python 422load("//proto:proto.bzl", "rust_grpc_library") 423 424proto_library( 425name = "my_proto", 426srcs = ["my.proto"] 427) 428 429rust_grpc_library( 430name = "rust", 431deps = [":my_proto"], 432) 433 434rust_binary( 435name = "my_service", 436srcs = ["my_service.rs"], 437deps = [":rust"], 438) 439``` 440""" , 441)