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