fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
bef8d2d4
patching
1"Module extensions for using rules_rust with bzlmod" 2 3load ( "@bazel_features//:features.bzl" , "bazel_features" ) 4load ( "//rust:defs.bzl" , "rust_common" ) 5load ( "//rust:repositories.bzl" , "rust_register_toolchains" , "rust_toolchain_tools_repository" ) 6load ( "//rust/platform:triple.bzl" , "get_host_triple" ) 7load ( 8"//rust/private:repository_utils.bzl" , 9"DEFAULT_EXTRA_TARGET_TRIPLES" , 10"DEFAULT_NIGHTLY_VERSION" , 11"DEFAULT_STATIC_RUST_URL_TEMPLATES" , 12) 13 14_RUST_TOOLCHAIN_VERSIONS = [ 15rust_common . default_version , 16DEFAULT_NIGHTLY_VERSION , 17] 18 19def _find_modules ( module_ctx ): 20root = None 21our_module = None 22for mod in module_ctx . modules : 23if mod . is_root : 24root = mod 25if mod . name == "rules_rust" : 26our_module = mod 27if root == None : 28root = our_module 29if our_module == None : 30fail ( "Unable to find rules_rust module" ) 31 32return root , our_module 33 34def _rust_impl ( module_ctx ): 35# Toolchain configuration is only allowed in the root module, or in 36# rules_rust. 37# See https://github.com/bazelbuild/bazel/discussions/22024 for discussion. 38root , rules_rust = _find_modules ( module_ctx ) 39 40toolchains = root . tags . toolchain or rules_rust . tags . toolchain 41 42for toolchain in toolchains : 43rust_register_toolchains ( 44dev_components = toolchain . dev_components , 45edition = toolchain . edition , 46allocator_library = toolchain . allocator_library , 47rustfmt_version = toolchain . rustfmt_version , 48rust_analyzer_version = toolchain . rust_analyzer_version , 49sha256s = toolchain . sha256s , 50extra_target_triples = toolchain . extra_target_triples , 51urls = toolchain . urls , 52versions = toolchain . versions , 53register_toolchains = False , 54) 55 56_COMMON_TAG_KWARGS = dict ( 57allocator_library = attr . string ( 58doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary." , 59default = "@rules_rust//ffi/cc/allocator_library" , 60), 61dev_components = attr . bool ( 62doc = "Whether to download the rustc-dev components (defaults to False). Requires version to be \"nightly\"." , 63default = False , 64), 65edition = attr . string ( 66doc = ( 67"The rust edition to be used by default (2015, 2018, or 2021). " + 68"If absent, every rule is required to specify its `edition` attribute." 69), 70), 71rustfmt_version = attr . string ( 72doc = "The version of the tool among \"nightly\", \"beta\", or an exact version." , 73default = DEFAULT_NIGHTLY_VERSION , 74), 75sha256s = attr . string_dict ( 76doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details." , 77), 78urls = attr . string_list ( 79doc = "A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format)." , 80default = DEFAULT_STATIC_RUST_URL_TEMPLATES , 81), 82) 83 84_RUST_TOOLCHAIN_TAG = tag_class ( 85attrs = dict ( 86extra_target_triples = attr . string_list ( 87default = DEFAULT_EXTRA_TARGET_TRIPLES , 88), 89rust_analyzer_version = attr . string ( 90doc = "The version of Rustc to pair with rust-analyzer." , 91), 92versions = attr . string_list ( 93doc = ( 94"A list of toolchain versions to download. This paramter only accepts one versions " + 95 "per channel. E.g. `[\"1.65.0\", \"nightly/2022-11-02\", \"beta/2020-12-30\"]`." 96), 97default = _RUST_TOOLCHAIN_VERSIONS , 98), 99** _COMMON_TAG_KWARGS 100), 101) 102 103_RUST_HOST_TOOLS_TAG = tag_class ( 104attrs = dict ( 105version = attr . string ( 106default = rust_common . default_version , 107doc = "The version of Rust to use for tools executed on the Bazel host." , 108), 109** _COMMON_TAG_KWARGS 110), 111) 112 113rust = module_extension ( 114implementation = _rust_impl , 115tag_classes = { 116"toolchain" : _RUST_TOOLCHAIN_TAG , 117}, 118) 119 120# This is a separate module extension so that only the host tools are 121# marked as reproducible and os and arch dependent 122def _rust_host_tools_impl ( module_ctx ): 123root , _ = _find_modules ( module_ctx ) 124 125if len ( root . tags . host_tools ) == 1 : 126attrs = root . tags . host_tools [ 0 ] 127 128host_tools = { 129"allocator_library" : attrs . allocator_library , 130"dev_components" : attrs . dev_components , 131"edition" : attrs . edition , 132"rustfmt_version" : attrs . rustfmt_version , 133"sha256s" : attrs . sha256s , 134"urls" : attrs . urls , 135"version" : attrs . version , 136} 137elif not root . tags . host_tools : 138host_tools = { 139"version" : rust_common . default_version , 140} 141else : 142fail ( "Multiple host_tools were defined in your root MODULE.bazel" ) 143 144host_triple = get_host_triple ( module_ctx ) 145rust_toolchain_tools_repository ( 146name = "rust_host_tools" , 147exec_triple = host_triple . str , 148target_triple = host_triple . str , 149** host_tools 150) 151 152metadata_kwargs = {} 153if bazel_features . external_deps . extension_metadata_has_reproducible : 154metadata_kwargs [ "reproducible" ] = True 155return module_ctx . extension_metadata ( ** metadata_kwargs ) 156 157_conditional_rust_host_tools_args = { 158"arch_dependent" : True , 159"os_dependent" : True , 160} if bazel_features . external_deps . module_extension_has_os_arch_dependent else {} 161 162rust_host_tools = module_extension ( 163implementation = _rust_host_tools_impl , 164tag_classes = { 165"host_tools" : _RUST_HOST_TOOLS_TAG , 166}, 167** _conditional_rust_host_tools_args 168)