From 7b71093c467eac187c90f2637d194d094e87c5ec Mon Sep 17 00:00:00 2001 From: James Sharpe Date: Tue, 27 Jun 2023 21:55:57 +0000 Subject: [PATCH 1/2] Create hub repo for shell framework toolchains --- MODULE.bazel | 10 +--- foreign_cc/private/framework/toolchain.bzl | 49 +++++++++++++------ .../private/framework/toolchains/mappings.bzl | 4 +- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/MODULE.bazel b/MODULE.bazel index cbe635e0c..da990d8ac 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 81efcc593..5f9e5b9bd 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 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 ac1a4ea45..d614102b1 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. From 06508ecac1bc6159dcfe73d25101f7a193e2b1f8 Mon Sep 17 00:00:00 2001 From: James Sharpe Date: Wed, 28 Jun 2023 08:34:51 +0100 Subject: [PATCH 2/2] Update foreign_cc/private/framework/toolchain.bzl --- foreign_cc/private/framework/toolchain.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/foreign_cc/private/framework/toolchain.bzl b/foreign_cc/private/framework/toolchain.bzl index 5f9e5b9bd..8198faa70 100644 --- a/foreign_cc/private/framework/toolchain.bzl +++ b/foreign_cc/private/framework/toolchain.bzl @@ -83,7 +83,7 @@ def _framework_toolchain_repository_hub_impl(repository_ctx): 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 toolchains generated by `@rules_foreign_cc`.", + 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(