diff --git a/MODULE.bazel b/MODULE.bazel index cbe635e0..da990d8a 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -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", diff --git a/foreign_cc/private/framework/toolchain.bzl b/foreign_cc/private/framework/toolchain.bzl index 81efcc59..8198faa7 100644 --- a/foreign_cc/private/framework/toolchain.bzl +++ b/foreign_cc/private/framework/toolchain.bzl @@ -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") diff --git a/foreign_cc/private/framework/toolchains/mappings.bzl b/foreign_cc/private/framework/toolchains/mappings.bzl index ac1a4ea4..d614102b 100644 --- a/foreign_cc/private/framework/toolchains/mappings.bzl +++ b/foreign_cc/private/framework/toolchains/mappings.bzl @@ -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.