Create hub repo for shell framework toolchains (#1066)

This simplifies the registration of the shell framework toolchains in
bzlmod.
This commit is contained in:
James Sharpe 2023-06-28 10:20:01 +01:00 committed by GitHub
parent 95419b72d3
commit 26c7700830
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 26 deletions

View File

@ -20,17 +20,11 @@ use_repo(
"ninja_1.11.1_toolchains",
"ninja_build_src",
"pkgconfig_src",
"rules_foreign_cc_framework_toolchain_freebsd",
"rules_foreign_cc_framework_toolchain_linux",
"rules_foreign_cc_framework_toolchain_macos",
"rules_foreign_cc_framework_toolchain_windows",
"rules_foreign_cc_framework_toolchains",
)
register_toolchains(
"@rules_foreign_cc_framework_toolchain_freebsd//:toolchain",
"@rules_foreign_cc_framework_toolchain_linux//:toolchain",
"@rules_foreign_cc_framework_toolchain_macos//:toolchain",
"@rules_foreign_cc_framework_toolchain_windows//:toolchain",
"@rules_foreign_cc_framework_toolchains//:all",
"@rules_foreign_cc//toolchains:built_make_toolchain",
"@rules_foreign_cc//toolchains:built_meson_toolchain",
"@rules_foreign_cc//toolchains:built_pkgconfig_toolchain",

View File

@ -12,14 +12,6 @@ exports_files(["defs.bzl"])
foreign_cc_framework_toolchain(
name = "commands"
)
toolchain(
name = "toolchain",
toolchain_type = "@rules_foreign_cc//foreign_cc/private/framework:shell_toolchain",
toolchain = ":commands",
exec_compatible_with = {exec_compat},
target_compatible_with = {target_compat},
)
"""
_DEFS_BZL_CONTENT = """\
@ -52,7 +44,6 @@ def _framework_toolchain_repository_impl(repository_ctx):
repository_ctx.file("BUILD.bazel", _BUILD_FILE_CONTENT.format(
exec_compat = repository_ctx.attr.exec_compatible_with,
target_compat = repository_ctx.attr.target_compatible_with,
))
framework_toolchain_repository = repository_rule(
@ -65,8 +56,38 @@ framework_toolchain_repository = repository_rule(
"exec_compatible_with": attr.string_list(
doc = "A list of constraint_values that must be present in the execution platform for this target.",
),
"target_compatible_with": attr.string_list(
doc = "A list of constraint_values that must be present in the target platform for this target to be considered compatible.",
},
)
_HUB_BUILD_FILE_CONTENT = """\
toolchain(
name = "{item}",
toolchain_type = "@rules_foreign_cc//foreign_cc/private/framework:shell_toolchain",
toolchain = "@{item}//:commands",
exec_compatible_with = {exec_compat},
)
"""
def _framework_toolchain_repository_hub_impl(repository_ctx):
"""The implementation of `framework_toolchain_repository_hub`
Args:
repository_ctx (repository_ctx): The rule's context object
"""
hub_build_content = ""
for item in TOOLCHAIN_MAPPINGS:
hub_build_content += _HUB_BUILD_FILE_CONTENT.format(item = item.repo_name, exec_compat = item.exec_compatible_with)
repository_ctx.file("BUILD.bazel", hub_build_content)
framework_toolchain_repository_hub = repository_rule(
doc = "A repository rule which defines a `@rules_foreign_cc//foreign_cc/private/framework:shell_toolchain` hub which holds all the shell toolchains generated by `@rules_foreign_cc`.",
implementation = _framework_toolchain_repository_hub_impl,
attrs = {
"exec_compatible_with": attr.string_list(
doc = "A list of constraint_values that must be present in the execution platform for this target.",
),
},
)
@ -78,17 +99,15 @@ def register_framework_toolchains(register_toolchains = True):
Args:
register_toolchains: Whether to call native.register_toolchains or not
"""
toolchains = []
for item in TOOLCHAIN_MAPPINGS:
framework_toolchain_repository(
name = item.repo_name,
commands_src = item.file,
exec_compatible_with = item.exec_compatible_with,
target_compatible_with = item.target_compatible_with,
)
toolchains.append("@{}//:toolchain".format(item.repo_name))
framework_toolchain_repository_hub(name = "rules_foreign_cc_framework_toolchains")
if register_toolchains:
native.register_toolchains(*toolchains)
native.register_toolchains("@rules_foreign_cc_framework_toolchains//:all")

View File

@ -1,13 +1,12 @@
"""A module defining default toolchain info for the foreign_cc framework"""
def _toolchain_mapping(file, repo_name, exec_compatible_with = [], target_compatible_with = []):
def _toolchain_mapping(file, repo_name, exec_compatible_with = []):
"""Mapping of toolchain definition files to platform constraints
Args:
file (str): Toolchain definition file
repo_name (str): name of repository to create for this toolchain
exec_compatible_with (list): A list of compatible execution platform constraints.
target_compatible_with (list): Compatible target platform constraints
Returns:
struct: A collection of toolchain data
@ -16,7 +15,6 @@ def _toolchain_mapping(file, repo_name, exec_compatible_with = [], target_compat
file = file,
repo_name = repo_name,
exec_compatible_with = exec_compatible_with,
target_compatible_with = target_compatible_with,
)
# This list is the single entrypoint for all foreign_cc framework toolchains.