fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
2bad96d9
cleanup
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 264rust_analyzer_info = write_rust_analyzer_spec_file ( ctx , ctx . rule . attr , ctx . label , RustAnalyzerInfo ( 265aliases = {}, 266crate = dep_variant_info . crate_info , 267cfgs = cfgs , 268env = dep_variant_info . crate_info . rustc_env , 269deps = rust_analyzer_deps , 270crate_specs = depset ( transitive = [ dep . crate_specs for dep in rust_analyzer_deps ]), 271proc_macro_dylib_path = None , 272build_info = dep_variant_info . build_info , 273)) 274 275return [ 276ProstProtoInfo ( 277dep_variant_info = dep_variant_info , 278transitive_dep_infos = depset ( transitive = transitive_deps ), 279package_info = package_info_file , 280), 281rust_analyzer_info , 282OutputGroupInfo ( rust_generated_srcs = [ lib_rs ]), 283] 284 285rust_prost_aspect = aspect ( 286doc = "An aspect used to generate and compile proto files with Prost." , 287implementation = _rust_prost_aspect_impl , 288attr_aspects = [ "deps" ], 289attrs = { 290"_collect_cc_coverage" : attr . label ( 291default = Label ( "@rules_rust//util:collect_coverage" ), 292executable = True , 293cfg = "exec" , 294), 295"_grep_includes" : attr . label ( 296allow_single_file = True , 297default = Label ( "@bazel_tools//tools/cpp:grep-includes" ), 298cfg = "exec" , 299), 300"_prost_process_wrapper" : attr . label ( 301doc = "The wrapper script for the Prost protoc plugin." , 302cfg = "exec" , 303executable = True , 304default = Label ( "//private:protoc_wrapper" ), 305), 306} | RUSTC_ATTRS , 307fragments = [ "cpp" ], 308toolchains = [ 309TOOLCHAIN_TYPE , 310"@bazel_tools//tools/cpp:toolchain_type" , 311"@rules_rust//rust:toolchain_type" , 312"@rules_rust//rust/rustfmt:toolchain_type" , 313], 314) 315 316def _rust_prost_library_impl ( ctx ): 317proto_dep = ctx . attr . proto 318rust_proto_info = proto_dep [ ProstProtoInfo ] 319dep_variant_info = rust_proto_info . dep_variant_info 320 321prost_toolchain = ctx . toolchains [ TOOLCHAIN_TYPE ] 322 323transitive = [] 324if prost_toolchain . include_transitive_deps : 325transitive = [ rust_proto_info . transitive_dep_infos ] 326 327return [ 328DefaultInfo ( files = depset ([ dep_variant_info . crate_info . output ])), 329rust_common . crate_group_info ( 330dep_variant_infos = depset ( 331[ dep_variant_info ], 332transitive = transitive , 333), 334), 335RustAnalyzerGroupInfo ( deps = [ proto_dep [ RustAnalyzerInfo ]]), 336] 337 338rust_prost_library = rule ( 339doc = "A rule for generating a Rust library using Prost." , 340implementation = _rust_prost_library_impl , 341attrs = { 342"proto" : attr . label ( 343doc = "A `proto_library` target for which to generate Rust gencode." , 344providers = [ ProtoInfo ], 345aspects = [ rust_prost_aspect ], 346mandatory = True , 347), 348"_collect_cc_coverage" : attr . label ( 349default = Label ( "@rules_rust//util:collect_coverage" ), 350executable = True , 351cfg = "exec" , 352), 353}, 354toolchains = [ 355TOOLCHAIN_TYPE , 356], 357) 358 359def _rust_prost_toolchain_impl ( ctx ): 360tonic_attrs = [ ctx . attr . tonic_plugin_flag , ctx . attr . tonic_plugin , ctx . attr . tonic_runtime ] 361if any ( tonic_attrs ) and not all ( tonic_attrs ): 362fail ( "When one tonic attribute is added, all must be added" ) 363 364proto_toolchain = proto_toolchains . find_toolchain ( 365ctx , 366legacy_attr = "_legacy_proto_toolchain" , 367toolchain_type = "@rules_proto//proto:toolchain_type" , 368) 369 370if ctx . attr . proto_compiler : 371# buildifier: disable=print 372( "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" ) 373proto_compiler = ctx . attr . proto_compiler [ DefaultInfo ]. files_to_run 374else : 375proto_compiler = proto_toolchain . proto_compiler 376 377return [ platform_common . ToolchainInfo ( 378prost_opts = ctx . attr . prost_opts , 379prost_plugin = ctx . attr . prost_plugin , 380prost_plugin_flag = ctx . attr . prost_plugin_flag , 381prost_runtime = ctx . attr . prost_runtime , 382prost_types = ctx . attr . prost_types , 383proto_compiler = proto_compiler , 384protoc_opts = ctx . fragments . proto . experimental_protoc_opts , 385tonic_opts = ctx . attr . tonic_opts , 386tonic_plugin = ctx . attr . tonic_plugin , 387tonic_plugin_flag = ctx . attr . tonic_plugin_flag , 388tonic_runtime = ctx . attr . tonic_runtime , 389include_transitive_deps = ctx . attr . include_transitive_deps , 390)] 391 392rust_prost_toolchain = rule ( 393implementation = _rust_prost_toolchain_impl , 394doc = "Rust Prost toolchain rule." , 395fragments = [ "proto" ], 396attrs = dict ({ 397"include_transitive_deps" : attr . bool ( 398doc = "Whether to include transitive dependencies. If set to True, all transitive dependencies will directly accessible by the dependent crate." , 399default = False , 400), 401"prost_opts" : attr . string_list ( 402doc = "Additional options to add to Prost." , 403), 404"prost_plugin" : attr . label ( 405doc = "Additional plugins to add to Prost." , 406cfg = "exec" , 407executable = True , 408mandatory = True , 409), 410"prost_plugin_flag" : attr . string ( 411doc = "Prost plugin flag format. (e.g. `--plugin=protoc-gen-prost=%s`)" , 412default = "--plugin=protoc-gen-prost=%s" , 413), 414"prost_runtime" : attr . label ( 415doc = "The Prost runtime crates to use." , 416providers = [[ rust_common . crate_info ], [ rust_common . crate_group_info ]], 417mandatory = True , 418), 419"prost_types" : attr . label ( 420doc = "The Prost types crates to use." , 421providers = [[ rust_common . crate_info ], [ rust_common . crate_group_info ]], 422mandatory = True , 423), 424"proto_compiler" : attr . label ( 425doc = "The protoc compiler to use. Note that this attribute is deprecated - prefer to use --incompatible_enable_proto_toolchain_resolution." , 426cfg = "exec" , 427executable = True , 428), 429"tonic_opts" : attr . string_list ( 430doc = "Additional options to add to Tonic." , 431), 432"tonic_plugin" : attr . label ( 433doc = "Additional plugins to add to Tonic." , 434cfg = "exec" , 435executable = True , 436), 437"tonic_plugin_flag" : attr . string ( 438doc = "Tonic plugin flag format. (e.g. `--plugin=protoc-gen-tonic=%s`))" , 439default = "--plugin=protoc-gen-tonic=%s" , 440), 441"tonic_runtime" : attr . label ( 442doc = "The Tonic runtime crates to use." , 443providers = [[ rust_common . crate_info ], [ rust_common . crate_group_info ]], 444), 445}, ** proto_toolchains . if_legacy_toolchain ({ 446"_legacy_proto_toolchain" : attr . label ( 447default = Label ( "//private:legacy_proto_toolchain" ), 448), 449})), 450toolchains = proto_toolchains . use_toolchain ( "@rules_proto//proto:toolchain_type" ), 451) 452 453def _current_prost_runtime_impl ( ctx ): 454toolchain = ctx . toolchains [ TOOLCHAIN_TYPE ] 455 456runtime_deps = [] 457 458for target in [ toolchain . prost_runtime , toolchain . prost_types ]: 459if rust_common . crate_group_info in target : 460crate_group_info = target [ rust_common . crate_group_info ] 461runtime_deps . extend ( crate_group_info . dep_variant_infos . to_list ()) 462else : 463runtime_deps . append ( rust_common . dep_variant_info ( 464crate_info = target [ rust_common . crate_info ] if rust_common . crate_info in target else None , 465dep_info = target [ rust_common . dep_info ] if rust_common . dep_info in target else None , 466cc_info = target [ CcInfo ] if CcInfo in target else None , 467build_info = None , 468)) 469 470return [ rust_common . crate_group_info ( 471dep_variant_infos = depset ( runtime_deps ), 472)] 473 474current_prost_runtime = rule ( 475doc = "A rule for accessing the current Prost toolchain components needed by the process wrapper" , 476provides = [ rust_common . crate_group_info ], 477implementation = _current_prost_runtime_impl , 478toolchains = [ TOOLCHAIN_TYPE ], 479)