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