fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
6409c9fb
patching
1"""Rules for building protos in Rust with Prost and Tonic.""" 2 3load ( "@rules_proto//proto:defs.bzl" , "ProtoInfo" , "proto_common" ) 4load ( "@rules_proto//proto:proto_common.bzl" , proto_toolchains = "toolchains" ) 5load ( "//proto/prost:providers.bzl" , "ProstProtoInfo" ) 6load ( "//rust:defs.bzl" , "rust_common" ) 7 8# buildifier: disable=bzl-visibility 9load ( "//rust/private:providers.bzl" , "RustAnalyzerGroupInfo" , "RustAnalyzerInfo" ) 10 11# buildifier: disable=bzl-visibility 12load ( "//rust/private:rust.bzl" , "RUSTC_ATTRS" ) 13 14# buildifier: disable=bzl-visibility 15load ( "//rust/private:rust_analyzer.bzl" , "write_rust_analyzer_spec_file" ) 16 17# buildifier: disable=bzl-visibility 18load ( "//rust/private:rustc.bzl" , "rustc_compile_action" ) 19 20# buildifier: disable=bzl-visibility 21load ( "//rust/private:utils.bzl" , "can_build_metadata" ) 22 23RUST_EDITION = "2021" 24 25TOOLCHAIN_TYPE = "@rules_rust//proto/prost:toolchain_type" 26 27def _create_proto_lang_toolchain ( ctx , prost_toolchain ): 28proto_lang_toolchain = proto_common . ProtoLangToolchainInfo ( 29out_replacement_format_flag = "--prost_out=%s" , 30plugin_format_flag = prost_toolchain . prost_plugin_flag , 31plugin = prost_toolchain . prost_plugin [ DefaultInfo ]. files_to_run , 32runtime = prost_toolchain . prost_runtime , 33provided_proto_sources = depset (), 34proto_compiler = ctx . attr . _prost_process_wrapper [ DefaultInfo ]. files_to_run , 35protoc_opts = prost_toolchain . protoc_opts , 36progress_message = "ProstGenProto %{label}" , 37mnemonic = "ProstGenProto" , 38) 39 40return proto_lang_toolchain 41 42def _compile_proto ( ctx , crate_name , proto_info , deps , prost_toolchain , rustfmt_toolchain = None ): 43deps_info_file = ctx . actions . declare_file ( ctx . label . name + ".prost_deps_info" ) 44dep_package_infos = [ dep [ ProstProtoInfo ]. package_info for dep in deps ] 45ctx . actions . write ( 46output = deps_info_file , 47content = "\n ". join ([ file . path for file in dep_package_infos ]), 48) 49 50package_info_file = ctx . actions . declare_file ( ctx . label . name + ".prost_package_info" ) 51lib_rs = ctx . actions . declare_file ( "{}.lib.rs" . format ( ctx . label . name )) 52 53proto_compiler = prost_toolchain . proto_compiler 54tools = depset ([ proto_compiler . executable ]) 55 56additional_args = ctx . actions . args () 57 58# Prost process wrapper specific args 59additional_args . add ( "--protoc={}" . format ( proto_compiler . executable . path )) 60additional_args . add ( "--label={}" . format ( ctx . label )) 61additional_args . add ( "--out_librs={}" . format ( lib_rs . path )) 62additional_args . add ( "--package_info_output={}" . format ( "{}={}" . format ( crate_name , package_info_file . path ))) 63additional_args . add ( "--deps_info={}" . format ( deps_info_file . path )) 64additional_args . add ( "--prost_opt=compile_well_known_types" ) 65additional_args . add ( "--descriptor_set={}" . format ( proto_info . direct_descriptor_set . path )) 66additional_args . add_all ( prost_toolchain . prost_opts , format_each = "--prost_opt=%s" ) 67 68if prost_toolchain . tonic_plugin : 69tonic_plugin = prost_toolchain . tonic_plugin [ DefaultInfo ]. files_to_run 70additional_args . add ( prost_toolchain . tonic_plugin_flag % tonic_plugin . executable . path ) 71additional_args . add ( "--tonic_opt=no_include" ) 72additional_args . add ( "--tonic_opt=compile_well_known_types" ) 73additional_args . add ( "--is_tonic" ) 74additional_args . add_all ( prost_toolchain . tonic_opts , format_each = "--tonic_opt=%s" ) 75tools = depset ([ tonic_plugin . executable ], transitive = [ tools ]) 76 77if rustfmt_toolchain : 78additional_args . add ( "--rustfmt={}" . format ( rustfmt_toolchain . rustfmt . path )) 79tools = depset ( transitive = [ tools , rustfmt_toolchain . all_files ]) 80 81additional_inputs = depset ([ deps_info_file , proto_info . direct_descriptor_set ] + [ dep [ ProstProtoInfo ]. package_info for dep in deps ]) 82 83proto_common . compile ( 84actions = ctx . actions , 85proto_info = proto_info , 86additional_tools = tools . to_list (), 87additional_inputs = additional_inputs , 88additional_args = additional_args , 89generated_files = [ lib_rs , package_info_file ], 90proto_lang_toolchain_info = _create_proto_lang_toolchain ( ctx , prost_toolchain ), 91plugin_output = ctx . bin_dir . path , 92) 93 94return lib_rs , package_info_file 95 96def _get_crate_info ( providers ): 97"""Finds the CrateInfo provider in the list of providers.""" 98for provider in providers : 99if hasattr ( provider , "name" ): 100return provider 101fail ( "Couldn't find a CrateInfo in the list of providers" ) 102 103def _get_dep_info ( providers ): 104"""Finds the DepInfo provider in the list of providers.""" 105for provider in providers : 106if hasattr ( provider , "direct_crates" ): 107return provider 108fail ( "Couldn't find a DepInfo in the list of providers" ) 109 110def _get_cc_info ( providers ): 111"""Finds the CcInfo provider in the list of providers.""" 112for provider in providers : 113if hasattr ( provider , "linking_context" ): 114return provider 115fail ( "Couldn't find a CcInfo in the list of providers" ) 116 117def _compile_rust ( ctx , attr , crate_name , src , deps , edition ): 118"""Compiles a Rust source file. 119 120Args: 121ctx (RuleContext): The rule context. 122attr (Attrs): The current rule's attributes (`ctx.attr` for rules, `ctx.rule.attr` for aspects) 123crate_name (str): The crate module name to use. 124src (File): The crate root source file to be compiled. 125deps (List of DepVariantInfo): A list of dependencies needed. 126edition (str): The Rust edition to use. 127 128Returns: 129A DepVariantInfo provider. 130""" 131toolchain = ctx . toolchains [ "@rules_rust//rust:toolchain_type" ] 132output_hash = repr ( hash ( src . path + ".prost" )) 133 134lib_name = "{prefix}{name}-{lib_hash}{extension}" . format ( 135prefix = "lib" , 136name = crate_name , 137lib_hash = output_hash , 138extension = ".rlib" , 139) 140 141rmeta_name = "{prefix}{name}-{lib_hash}{extension}" . format ( 142prefix = "lib" , 143name = crate_name , 144lib_hash = output_hash , 145extension = ".rmeta" , 146) 147 148lib = ctx . actions . declare_file ( lib_name ) 149rmeta = None 150 151if can_build_metadata ( toolchain , ctx , "rlib" ): 152rmeta_name = "{prefix}{name}-{lib_hash}{extension}" . format ( 153prefix = "lib" , 154name = crate_name , 155lib_hash = output_hash , 156extension = ".rmeta" , 157) 158rmeta = ctx . actions . declare_file ( rmeta_name ) 159 160providers = rustc_compile_action ( 161ctx = ctx , 162attr = attr , 163toolchain = toolchain , 164crate_info_dict = dict ( 165name = crate_name , 166type = "rlib" , 167root = src , 168srcs = depset ([ src ]), 169deps = depset ( deps ), 170proc_macro_deps = depset ([]), 171aliases = {}, 172output = lib , 173metadata = rmeta , 174edition = edition , 175is_test = False , 176rustc_env = {}, 177compile_data = depset ([]), 178compile_data_targets = depset ([]), 179owner = ctx . label , 180), 181output_hash = output_hash , 182) 183 184crate_info = _get_crate_info ( providers ) 185dep_info = _get_dep_info ( providers ) 186cc_info = _get_cc_info ( providers ) 187 188return rust_common . dep_variant_info ( 189crate_info = crate_info , 190dep_info = dep_info , 191cc_info = cc_info , 192build_info = None , 193) 194 195def _rust_prost_aspect_impl ( target , ctx ): 196if ProstProtoInfo in target : 197return [] 198 199runtime_deps = [] 200 201rustfmt_toolchain = ctx . toolchains [ "@rules_rust//rust/rustfmt:toolchain_type" ] 202prost_toolchain = ctx . toolchains [ "@rules_rust//proto/prost:toolchain_type" ] 203for prost_runtime in [ prost_toolchain . prost_runtime , prost_toolchain . tonic_runtime ]: 204if not prost_runtime : 205 continue 206if rust_common . crate_group_info in prost_runtime : 207crate_group_info = prost_runtime [ rust_common . crate_group_info ] 208runtime_deps . extend ( crate_group_info . dep_variant_infos . to_list ()) 209else : 210runtime_deps . append ( rust_common . dep_variant_info ( 211crate_info = prost_runtime [ rust_common . crate_info ] if rust_common . crate_info in prost_runtime else None , 212dep_info = prost_runtime [ rust_common . dep_info ] if rust_common . dep_info in prost_runtime else None , 213cc_info = prost_runtime [ CcInfo ] if CcInfo in prost_runtime else None , 214build_info = None , 215)) 216 217proto_deps = getattr ( ctx . rule . attr , "deps" , []) 218 219direct_deps = [] 220transitive_deps = [ depset ( runtime_deps )] 221rust_analyzer_deps = [] 222for proto_dep in proto_deps : 223proto_info = proto_dep [ ProstProtoInfo ] 224 225direct_deps . append ( proto_info . dep_variant_info ) 226transitive_deps . append ( depset ( 227[ proto_info . dep_variant_info ], 228transitive = [ proto_info . transitive_dep_infos ], 229)) 230 231if RustAnalyzerInfo in proto_dep : 232rust_analyzer_deps . append ( proto_dep [ RustAnalyzerInfo ]) 233 234deps = runtime_deps + direct_deps 235 236crate_name = ctx . label . name . replace ( "-" , "_" ). replace ( "/" , "_" ) 237 238proto_info = target [ ProtoInfo ] 239 240lib_rs , package_info_file = _compile_proto ( 241ctx = ctx , 242crate_name = crate_name , 243proto_info = proto_info , 244deps = proto_deps , 245prost_toolchain = prost_toolchain , 246rustfmt_toolchain = rustfmt_toolchain , 247) 248 249dep_variant_info = _compile_rust ( 250ctx = ctx , 251attr = ctx . rule . attr , 252crate_name = crate_name , 253src = lib_rs , 254deps = deps , 255edition = RUST_EDITION , 256) 257 258# Always add `test` & `debug_assertions`. See rust-analyzer source code: 259# https://github.com/rust-analyzer/rust-analyzer/blob/2021-11-15/crates/project_model/src/workspace.rs#L529-L531 260cfgs = [ "test" , "debug_assertions" ] 261 262rust_analyzer_info = write_rust_analyzer_spec_file ( ctx , ctx . rule . attr , ctx . label , RustAnalyzerInfo ( 263aliases = {}, 264crate = dep_variant_info . crate_info , 265cfgs = cfgs , 266env = dep_variant_info . crate_info . rustc_env , 267deps = rust_analyzer_deps , 268crate_specs = depset ( transitive = [ dep . crate_specs for dep in rust_analyzer_deps ]), 269proc_macro_dylib_path = None , 270build_info = dep_variant_info . build_info , 271)) 272 273return [ 274ProstProtoInfo ( 275dep_variant_info = dep_variant_info , 276transitive_dep_infos = depset ( transitive = transitive_deps ), 277package_info = package_info_file , 278), 279rust_analyzer_info , 280OutputGroupInfo ( rust_generated_srcs = [ lib_rs ]), 281] 282 283rust_prost_aspect = aspect ( 284doc = "An aspect used to generate and compile proto files with Prost." , 285implementation = _rust_prost_aspect_impl , 286attr_aspects = [ "deps" ], 287attrs = { 288"_collect_cc_coverage" : attr . label ( 289default = Label ( "//util:collect_coverage" ), 290executable = True , 291cfg = "exec" , 292), 293"_grep_includes" : attr . label ( 294allow_single_file = True , 295default = Label ( "@bazel_tools//tools/cpp:grep-includes" ), 296cfg = "exec" , 297), 298"_prost_process_wrapper" : attr . label ( 299doc = "The wrapper script for the Prost protoc plugin." , 300cfg = "exec" , 301executable = True , 302default = Label ( "//proto/prost/private:protoc_wrapper" ), 303), 304} | RUSTC_ATTRS , 305fragments = [ "cpp" ], 306toolchains = [ 307TOOLCHAIN_TYPE , 308"@bazel_tools//tools/cpp:toolchain_type" , 309"@rules_rust//rust:toolchain_type" , 310"@rules_rust//rust/rustfmt:toolchain_type" , 311], 312) 313 314def _rust_prost_library_impl ( ctx ): 315proto_dep = ctx . attr . proto 316rust_proto_info = proto_dep [ ProstProtoInfo ] 317dep_variant_info = rust_proto_info . dep_variant_info 318 319return [ 320DefaultInfo ( files = depset ([ dep_variant_info . crate_info . output ])), 321rust_common . crate_group_info ( 322dep_variant_infos = depset ( 323[ dep_variant_info ], 324transitive = [ rust_proto_info . transitive_dep_infos ], 325), 326), 327RustAnalyzerGroupInfo ( deps = [ proto_dep [ RustAnalyzerInfo ]]), 328] 329 330rust_prost_library = rule ( 331doc = "A rule for generating a Rust library using Prost." , 332implementation = _rust_prost_library_impl , 333attrs = { 334"proto" : attr . label ( 335doc = "A `proto_library` target for which to generate Rust gencode." , 336providers = [ ProtoInfo ], 337aspects = [ rust_prost_aspect ], 338mandatory = True , 339), 340"_collect_cc_coverage" : attr . label ( 341default = Label ( "@rules_rust//util:collect_coverage" ), 342executable = True , 343cfg = "exec" , 344), 345}, 346) 347 348def _rust_prost_toolchain_impl ( ctx ): 349tonic_attrs = [ ctx . attr . tonic_plugin_flag , ctx . attr . tonic_plugin , ctx . attr . tonic_runtime ] 350if any ( tonic_attrs ) and not all ( tonic_attrs ): 351fail ( "When one tonic attribute is added, all must be added" ) 352 353proto_toolchain = proto_toolchains . find_toolchain ( 354ctx , 355legacy_attr = "_legacy_proto_toolchain" , 356toolchain_type = "@rules_proto//proto:toolchain_type" , 357) 358 359if ctx . attr . proto_compiler : 360# buildifier: disable=print 361( "WARN: rust_prost_toolchain's proto_compiler attribute is deprecated. Make sure your rules_proto dependency is at least version 6.0.0 and stop setting proto_compiler" ) 362proto_compiler = ctx . attr . proto_compiler [ DefaultInfo ]. files_to_run 363else : 364proto_compiler = proto_toolchain . proto_compiler 365 366return [ platform_common . ToolchainInfo ( 367prost_opts = ctx . attr . prost_opts , 368prost_plugin = ctx . attr . prost_plugin , 369prost_plugin_flag = ctx . attr . prost_plugin_flag , 370prost_runtime = ctx . attr . prost_runtime , 371prost_types = ctx . attr . prost_types , 372proto_compiler = proto_compiler , 373protoc_opts = ctx . fragments . proto . experimental_protoc_opts , 374tonic_opts = ctx . attr . tonic_opts , 375tonic_plugin = ctx . attr . tonic_plugin , 376tonic_plugin_flag = ctx . attr . tonic_plugin_flag , 377tonic_runtime = ctx . attr . tonic_runtime , 378)] 379 380rust_prost_toolchain = rule ( 381implementation = _rust_prost_toolchain_impl , 382doc = "Rust Prost toolchain rule." , 383fragments = [ "proto" ], 384attrs = dict ({ 385"prost_opts" : attr . string_list ( 386doc = "Additional options to add to Prost." , 387), 388"prost_plugin" : attr . label ( 389doc = "Additional plugins to add to Prost." , 390cfg = "exec" , 391executable = True , 392mandatory = True , 393), 394"prost_plugin_flag" : attr . string ( 395doc = "Prost plugin flag format. (e.g. `--plugin=protoc-gen-prost=%s`)" , 396default = "--plugin=protoc-gen-prost=%s" , 397), 398"prost_runtime" : attr . label ( 399doc = "The Prost runtime crates to use." , 400providers = [[ rust_common . crate_info ], [ rust_common . crate_group_info ]], 401mandatory = True , 402), 403"prost_types" : attr . label ( 404doc = "The Prost types crates to use." , 405providers = [[ rust_common . crate_info ], [ rust_common . crate_group_info ]], 406mandatory = True , 407), 408"proto_compiler" : attr . label ( 409doc = "The protoc compiler to use. Note that this attribute is deprecated - prefer to use --incompatible_enable_proto_toolchain_resolution." , 410cfg = "exec" , 411executable = True , 412), 413"tonic_opts" : attr . string_list ( 414doc = "Additional options to add to Tonic." , 415), 416"tonic_plugin" : attr . label ( 417doc = "Additional plugins to add to Tonic." , 418cfg = "exec" , 419executable = True , 420), 421"tonic_plugin_flag" : attr . string ( 422doc = "Tonic plugin flag format. (e.g. `--plugin=protoc-gen-tonic=%s`))" , 423default = "--plugin=protoc-gen-tonic=%s" , 424), 425"tonic_runtime" : attr . label ( 426doc = "The Tonic runtime crates to use." , 427providers = [[ rust_common . crate_info ], [ rust_common . crate_group_info ]], 428), 429}, ** proto_toolchains . if_legacy_toolchain ({ 430"_legacy_proto_toolchain" : attr . label ( 431default = "//proto/protobuf:legacy_proto_toolchain" , 432), 433})), 434toolchains = proto_toolchains . use_toolchain ( "@rules_proto//proto:toolchain_type" ), 435) 436 437def _current_prost_runtime_impl ( ctx ): 438toolchain = ctx . toolchains [ TOOLCHAIN_TYPE ] 439 440runtime_deps = [] 441 442for target in [ toolchain . prost_runtime , toolchain . prost_types ]: 443if rust_common . crate_group_info in target : 444crate_group_info = target [ rust_common . crate_group_info ] 445runtime_deps . extend ( crate_group_info . dep_variant_infos . to_list ()) 446else : 447runtime_deps . append ( rust_common . dep_variant_info ( 448crate_info = target [ rust_common . crate_info ] if rust_common . crate_info in target else None , 449dep_info = target [ rust_common . dep_info ] if rust_common . dep_info in target else None , 450cc_info = target [ CcInfo ] if CcInfo in target else None , 451build_info = None , 452)) 453 454return [ rust_common . crate_group_info ( 455dep_variant_infos = depset ( runtime_deps ), 456)] 457 458current_prost_runtime = rule ( 459doc = "A rule for accessing the current Prost toolchain components needed by the process wrapper" , 460provides = [ rust_common . crate_group_info ], 461implementation = _current_prost_runtime_impl , 462toolchains = [ TOOLCHAIN_TYPE ], 463)