fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
6a663880
cleanup
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 ( 66dev_components = toolchain . dev_components , 67edition = toolchain . edition , 68extra_rustc_flags = extra_rustc_flags , 69extra_exec_rustc_flags = toolchain . extra_exec_rustc_flags , 70allocator_library = toolchain . allocator_library , 71rustfmt_version = toolchain . rustfmt_version , 72rust_analyzer_version = toolchain . rust_analyzer_version , 73sha256s = toolchain . sha256s , 74extra_target_triples = toolchain . extra_target_triples , 75urls = toolchain . urls , 76versions = toolchain . versions , 77register_toolchains = False , 78aliases = toolchain . aliases , 79) 80metadata_kwargs = {} 81if bazel_features . external_deps . extension_metadata_has_reproducible : 82metadata_kwargs [ "reproducible" ] = True 83return module_ctx . extension_metadata ( ** metadata_kwargs ) 84 85_COMMON_TAG_KWARGS = dict ( 86allocator_library = attr . string ( 87doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary." , 88default = "@rules_rust//ffi/cc/allocator_library" , 89), 90dev_components = attr . bool ( 91doc = "Whether to download the rustc-dev components (defaults to False). Requires version to be \"nightly\"." , 92default = False , 93), 94edition = attr . string ( 95doc = ( 96"The rust edition to be used by default (2015, 2018, or 2021). " + 97"If absent, every rule is required to specify its `edition` attribute." 98), 99), 100rustfmt_version = attr . string ( 101doc = "The version of the tool among \"nightly\", \"beta\", or an exact version." , 102default = DEFAULT_NIGHTLY_VERSION , 103), 104sha256s = attr . string_dict ( 105doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details." , 106), 107urls = attr . string_list ( 108doc = "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)." , 109default = DEFAULT_STATIC_RUST_URL_TEMPLATES , 110), 111) 112 113_RUST_TOOLCHAIN_TAG = tag_class ( 114attrs = dict ( 115extra_target_triples = attr . string_list ( 116default = DEFAULT_EXTRA_TARGET_TRIPLES , 117), 118extra_exec_rustc_flags = attr . string_list ( 119doc = "Extra flags to pass to rustc in exec configuration" , 120), 121extra_rustc_flags = attr . string_list ( 122doc = "Extra flags to pass to rustc in non-exec configuration" , 123), 124extra_rustc_flags_triples = attr . string_list_dict ( 125doc = "Extra flags to pass to rustc in non-exec configuration. Key is the triple, value is the flag." , 126), 127rust_analyzer_version = attr . string ( 128doc = "The version of Rustc to pair with rust-analyzer." , 129), 130versions = attr . string_list ( 131doc = ( 132"A list of toolchain versions to download. This parameter only accepts one version " + 133 "per channel. E.g. `[\"1.65.0\", \"nightly/2022-11-02\", \"beta/2020-12-30\"]`. " + 134"May be set to an empty list (`[]`) to inhibit `rules_rust` from registering toolchains." 135), 136default = _RUST_TOOLCHAIN_VERSIONS , 137), 138aliases = attr . string_dict ( 139doc = ( 140"Map of full toolchain repository name to an alias. If any repository is created by this " + 141"extension matches a key in this dictionary, the name of the created repository will be " + 142"remapped to the value instead. This may be required to work around path length limits " + 143"on Windows." 144), 145default = {}, 146), 147** _COMMON_TAG_KWARGS 148), 149) 150 151_RUST_HOST_TOOLS_TAG = tag_class ( 152attrs = dict ( 153version = 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) 160 161rust = module_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 ( 211implementation = _rust_host_tools_impl , 212tag_classes = { 213"host_tools" : _RUST_HOST_TOOLS_TAG , 214}, 215** _conditional_rust_host_tools_args 216)