-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: stop autoregistering toolchains
- Loading branch information
Showing
8 changed files
with
231 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"Public API" | ||
|
||
load("//lib/private:extension_utils.bzl", _extension_utils = "extension_utils") | ||
|
||
extension_utils = _extension_utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
"""Utility functions for bzlmod extensions""" | ||
|
||
def _toolchain_repos(mctx, get_tag, toolchain_name, repos_fn, default_repository = None, get_name = None, get_version = None): | ||
"""Create toolchain repositories from bzlmod extension tags. | ||
For uses of the tag class where the name isn't overridden (the `default_repository` name | ||
is used), creates a single toolchain repository for the invocation of the tag closest to | ||
the root module. Otherwise, resolution follows bzlmod's depth-first search through the | ||
module graph. | ||
Only a root module may declare a toolchain with a name other than the default repository name. | ||
Args: | ||
mctx: The module context | ||
get_tag: A function that takes in `module.tags` and returns the desired tag. For example, | ||
`tag: lambda tags: tags.my_tag`. This is required because `my_tag` cannot be accessed | ||
as a simple string key from `module.tags`. | ||
toolchain_name: Name of the toolchain to use in error messages | ||
repos_fn: A function that takes (name, version) and creates a toolchain repository | ||
default_repository: Default name of the toolchain repository to pass to the repos_fn. | ||
By default, uses `toolchain_name`. | ||
get_name: A function that extracts the module name from the a tag's attributes. Defaults | ||
to grabbing the `name` attribute. | ||
get_version: A function that extracts the module name from the a tag's attributes. Defaults | ||
to grabbing the `version` attribute. Override this to a lambda that returns `None` if | ||
version isn't used as an attribute. | ||
""" | ||
if default_repository == None: | ||
default_repository = toolchain_name | ||
|
||
if get_name == None: | ||
get_name = lambda attr: attr.name | ||
if get_version == None: | ||
get_version = lambda attr: attr.version | ||
|
||
registrations = {} | ||
for mod in mctx.modules: | ||
for attr in get_tag(mod.tags): | ||
name = get_name(attr) | ||
version = get_version(attr) | ||
if name != default_repository and not mod.is_root: | ||
fail("Only the root module may provide a name for the {}} toolchain.".format(toolchain_name)) | ||
|
||
if name in registrations.keys(): | ||
if name == default_repository: | ||
# Prioritize the root-most registration of the default toolchain version and | ||
# ignore any further registrations (modules are processed breadth-first) | ||
continue | ||
if version == registrations[name]: | ||
# No problem to register a matching toolchain twice | ||
continue | ||
fail("Multiple conflicting {} toolchains declared for name {} ({} and {})".format( | ||
toolchain_name, | ||
name, | ||
version, | ||
registrations[name], | ||
)) | ||
else: | ||
registrations[name] = version | ||
|
||
for name, version in registrations.items(): | ||
repos_fn( | ||
name = name, | ||
version = version, | ||
) | ||
|
||
extension_utils = struct( | ||
toolchain_repos = _toolchain_repos, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters