Skip to content

Commit

Permalink
Create hub repo for shell framework toolchains (#1066)
Browse files Browse the repository at this point in the history
This simplifies the registration of the shell framework toolchains in
bzlmod.
  • Loading branch information
jsharpe authored Jun 28, 2023
1 parent 95419b7 commit 26c7700
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
10 changes: 2 additions & 8 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
49 changes: 34 additions & 15 deletions foreign_cc/private/framework/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """\
Expand Down Expand Up @@ -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(
Expand All @@ -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.",
),
},
)
Expand All @@ -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")
4 changes: 1 addition & 3 deletions foreign_cc/private/framework/toolchains/mappings.bzl
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand Down

0 comments on commit 26c7700

Please sign in to comment.