fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
69ddb09d
main
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 _empty_repository_impl ( repository_ctx ): 35repository_ctx . file ( "WORKSPACE.bazel" , """workspace(name = "{}")""" . format ( 36repository_ctx . name , 37)) 38repository_ctx . file ( "BUILD.bazel" , "" ) 39 40_empty_repository = repository_rule ( 41doc = "Declare an empty repository." , 42implementation = _empty_repository_impl , 43) 44 45def _rust_impl ( module_ctx ): 46# Toolchain configuration is only allowed in the root module, or in 47# rules_rust. 48# See https://github.com/bazelbuild/bazel/discussions/22024 for discussion. 49root , rules_rust = _find_modules ( module_ctx ) 50 51toolchains = root . tags . toolchain or rules_rust . tags . toolchain 52 53for toolchain in toolchains : 54if toolchain . extra_rustc_flags and toolchain . extra_rustc_flags_triples : 55fail ( "Cannot define both extra_rustc_flags and extra_rustc_flags_triples" ) 56if len ( toolchain . versions ) == 0 : 57# If the root module has asked for rules_rust to not register default 58# toolchains, an empty repository named `rust_toolchains` is created 59# so that the `register_toolchains()` in MODULES.bazel is still 60# valid. 61_empty_repository ( name = "rust_toolchains" ) 62else : 63extra_rustc_flags = toolchain . extra_rustc_flags if toolchain . extra_rustc_flags else toolchain . extra_rustc_flags_triples 64 65rust_register_toolchains ( 66hub_name = "rust_toolchains" , 67dev_components = toolchain . dev_components , 68edition = toolchain . edition , 69extra_rustc_flags = extra_rustc_flags , 70extra_exec_rustc_flags = toolchain . extra_exec_rustc_flags , 71allocator_library = toolchain . allocator_library , 72rustfmt_version = toolchain . rustfmt_version , 73rust_analyzer_version = toolchain . rust_analyzer_version , 74sha256s = toolchain . sha256s , 75extra_target_triples = toolchain . extra_target_triples , 76urls = toolchain . urls , 77versions = toolchain . versions , 78register_toolchains = False , 79aliases = toolchain . aliases , 80) 81metadata_kwargs = {} 82if bazel_features . external_deps . extension_metadata_has_reproducible : 83metadata_kwargs [ "reproducible" ] = True 84return module_ctx . extension_metadata ( ** metadata_kwargs ) 85 86_COMMON_TAG_KWARGS = { 87"allocator_library" : attr . string ( 88doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary." , 89default = "@rules_rust//ffi/cc/allocator_library" , 90), 91"dev_components" : attr . bool ( 92doc = "Whether to download the rustc-dev components (defaults to False). Requires version to be \"nightly\"." , 93default = False , 94), 95"edition" : attr . string ( 96doc = ( 97"The rust edition to be used by default (2015, 2018, or 2021). " + 98"If absent, every rule is required to specify its `edition` attribute." 99), 100), 101"rustfmt_version" : attr . string ( 102doc = "The version of the tool among \"nightly\", \"beta\", or an exact version." , 103default = DEFAULT_NIGHTLY_VERSION , 104), 105"sha256s" : attr . string_dict ( 106doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details." , 107), 108"urls" : attr . string_list ( 109doc = "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)." , 110default = DEFAULT_STATIC_RUST_URL_TEMPLATES , 111), 112} 113 114_RUST_TOOLCHAIN_TAG = tag_class ( 115attrs = { 116"aliases" : attr . string_dict ( 117doc = ( 118"Map of full toolchain repository name to an alias. If any repository is created by this " + 119"extension matches a key in this dictionary, the name of the created repository will be " + 120"remapped to the value instead. This may be required to work around path length limits " + 121"on Windows." 122), 123default = {}, 124), 125"extra_exec_rustc_flags" : attr . string_list ( 126doc = "Extra flags to pass to rustc in exec configuration" , 127), 128"extra_rustc_flags" : attr . string_list ( 129doc = "Extra flags to pass to rustc in non-exec configuration" , 130), 131"extra_rustc_flags_triples" : attr . string_list_dict ( 132doc = "Extra flags to pass to rustc in non-exec configuration. Key is the triple, value is the flag." , 133), 134"extra_target_triples" : attr . string_list ( 135default = DEFAULT_EXTRA_TARGET_TRIPLES , 136), 137"rust_analyzer_version" : attr . string ( 138doc = "The version of Rustc to pair with rust-analyzer." , 139), 140"versions" : attr . string_list ( 141doc = ( 142"A list of toolchain versions to download. This parameter only accepts one version " + 143 "per channel. E.g. `[\"1.65.0\", \"nightly/2022-11-02\", \"beta/2020-12-30\"]`. " + 144"May be set to an empty list (`[]`) to inhibit `rules_rust` from registering toolchains." 145), 146default = _RUST_TOOLCHAIN_VERSIONS , 147), 148} | _COMMON_TAG_KWARGS , 149) 150 151_RUST_HOST_TOOLS_TAG = tag_class ( 152attrs = { 153"version" : attr . string ( 154default = rust_common . default_version , 155doc = "The version of Rust to use for tools executed on the Bazel host." , 156), 157} | _COMMON_TAG_KWARGS , 158) 159 160rust = module_extension ( 161doc = "Rust toolchain extension." , 162implementation = _rust_impl , 163tag_classes = { 164"toolchain" : _RUST_TOOLCHAIN_TAG , 165}, 166) 167 168# This is a separate module extension so that only the host tools are 169# marked as reproducible and os and arch dependent 170def _rust_host_tools_impl ( module_ctx ): 171root , _ = _find_modules ( module_ctx ) 172 173if len ( root . tags . host_tools ) == 1 : 174attrs = root . tags . host_tools [ 0 ] 175 176host_tools = { 177"allocator_library" : attrs . allocator_library , 178"dev_components" : attrs . dev_components , 179"edition" : attrs . edition , 180"rustfmt_version" : attrs . rustfmt_version , 181"sha256s" : attrs . sha256s , 182"urls" : attrs . urls , 183"version" : attrs . version , 184} 185elif not root . tags . host_tools : 186host_tools = { 187"version" : rust_common . default_version , 188} 189else : 190fail ( "Multiple host_tools were defined in your root MODULE.bazel" ) 191 192host_triple = get_host_triple ( module_ctx ) 193rust_toolchain_tools_repository ( 194name = "rust_host_tools" , 195exec_triple = host_triple . str , 196target_triple = host_triple . str , 197** host_tools 198) 199 200metadata_kwargs = {} 201if bazel_features . external_deps . extension_metadata_has_reproducible : 202metadata_kwargs [ "reproducible" ] = True 203return module_ctx . extension_metadata ( ** metadata_kwargs ) 204 205_conditional_rust_host_tools_args = { 206"arch_dependent" : True , 207"os_dependent" : True , 208} if bazel_features . external_deps . module_extension_has_os_arch_dependent else {} 209 210rust_host_tools = module_extension ( 211doc = "An extension which exposes Rust tools compatible with the current host platform." , 212implementation = _rust_host_tools_impl , 213tag_classes = { 214"host_tools" : _RUST_HOST_TOOLS_TAG , 215}, 216** _conditional_rust_host_tools_args 217)