diff --git a/foreign_cc/built_tools/make_build.bzl b/foreign_cc/built_tools/make_build.bzl index 1d0ad29c9..cc15a55a6 100644 --- a/foreign_cc/built_tools/make_build.bzl +++ b/foreign_cc/built_tools/make_build.bzl @@ -103,7 +103,7 @@ make_tool = rule( output_to_genfiles = True, implementation = _make_tool_impl, toolchains = [ - str(Label("//foreign_cc/private/framework:shell_toolchain")), + "@rules_foreign_cc//foreign_cc/private/framework:shell_toolchain", "@bazel_tools//tools/cpp:toolchain_type", ], ) diff --git a/foreign_cc/built_tools/ninja_build.bzl b/foreign_cc/built_tools/ninja_build.bzl index 7127fc157..f21aea941 100644 --- a/foreign_cc/built_tools/ninja_build.bzl +++ b/foreign_cc/built_tools/ninja_build.bzl @@ -33,7 +33,7 @@ ninja_tool = rule( output_to_genfiles = True, implementation = _ninja_tool_impl, toolchains = [ - str(Label("//foreign_cc/private/framework:shell_toolchain")), + "@rules_foreign_cc//foreign_cc/private/framework:shell_toolchain", "@bazel_tools//tools/cpp:toolchain_type", ], ) diff --git a/foreign_cc/private/framework/toolchain.bzl b/foreign_cc/private/framework/toolchain.bzl index 01778d3a4..8d50e6f7b 100644 --- a/foreign_cc/private/framework/toolchain.bzl +++ b/foreign_cc/private/framework/toolchain.bzl @@ -17,7 +17,7 @@ foreign_cc_framework_toolchain( toolchain( name = "toolchain", toolchain_type = "@rules_foreign_cc//foreign_cc/private/framework:shell_toolchain", - toolchain = "//:commands", + toolchain = ":commands", exec_compatible_with = {exec_compat}, target_compatible_with = {target_compat}, ) @@ -51,14 +51,8 @@ def _framework_toolchain_repository_impl(repository_ctx): repository_ctx (repository_ctx): The rule's context object """ - # Ensure we always have an absolute label. This may not be the case - # when building within the `@rules_foreign_cc` workspace. - absolute_label = str(repository_ctx.attr.commands_src) - if not absolute_label.startswith("@"): - absolute_label = "@rules_foreign_cc" + absolute_label - repository_ctx.file("defs.bzl", _DEFS_BZL_CONTENT.format( - commands_src = absolute_label, + commands_src = repository_ctx.attr.commands_src, symbols = "\n ".join(["\"{}\",".format(symbol) for symbol in PLATFORM_COMMANDS.keys()]), commands = "\n ".join(["{cmd} = {cmd},".format(cmd = symbol) for symbol in PLATFORM_COMMANDS.keys()]), )) @@ -72,9 +66,8 @@ framework_toolchain_repository = repository_rule( doc = "A repository rule which defines a `@rules_foreign_cc//foreign_cc/private/framework:shell_toolchain` toolchain.", implementation = _framework_toolchain_repository_impl, attrs = { - "commands_src": attr.label( - doc = "The label of a `.bzl` source which defines toolchain commands", - allow_files = [".bzl"], + "commands_src": attr.string( + doc = "The string of a `.bzl` source which defines toolchain commands", ), "exec_compatible_with": attr.string_list( doc = "A list of constraint_values that must be present in the execution platform for this target.", @@ -95,17 +88,14 @@ def register_framework_toolchains(register_toolchains = True): toolchains = [] for item in TOOLCHAIN_MAPPINGS: - # Generate a toolchain name without the `.bzl` suffix - toolchain_name = "rules_foreign_cc_framework_toolchain_" + item.file.name[:-len(".bzl")] - framework_toolchain_repository( - name = toolchain_name, + 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(toolchain_name)) + toolchains.append("@{}//:toolchain".format(item.repo_name)) if (register_toolchains): native.register_toolchains(*toolchains) diff --git a/foreign_cc/private/framework/toolchains/access.bzl b/foreign_cc/private/framework/toolchains/access.bzl index 16d05d1bc..53bb1cb43 100644 --- a/foreign_cc/private/framework/toolchains/access.bzl +++ b/foreign_cc/private/framework/toolchains/access.bzl @@ -16,7 +16,7 @@ def create_context(ctx): - prelude (dict): A cache for rendered functions """ return struct( - shell = ctx.toolchains[str(Label("//foreign_cc/private/framework:shell_toolchain"))].commands, + shell = ctx.toolchains[Label("//foreign_cc/private/framework:shell_toolchain")].commands, prelude = {}, ) diff --git a/foreign_cc/private/framework/toolchains/mappings.bzl b/foreign_cc/private/framework/toolchains/mappings.bzl index ff7849b91..47f6d4c56 100644 --- a/foreign_cc/private/framework/toolchains/mappings.bzl +++ b/foreign_cc/private/framework/toolchains/mappings.bzl @@ -1,10 +1,11 @@ """A module defining default toolchain info for the foreign_cc framework""" -def _toolchain_mapping(file, exec_compatible_with = [], target_compatible_with = []): +def _toolchain_mapping(*, file, repo_name, exec_compatible_with = [], target_compatible_with = []): """Mapping of toolchain definition files to platform constraints Args: - file (Label): Toolchain definition file + 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 @@ -13,6 +14,7 @@ def _toolchain_mapping(file, exec_compatible_with = [], target_compatible_with = """ return struct( file = file, + repo_name = repo_name, exec_compatible_with = exec_compatible_with, target_compatible_with = target_compatible_with, ) @@ -20,27 +22,31 @@ def _toolchain_mapping(file, exec_compatible_with = [], target_compatible_with = # This list is the single entrypoint for all foreign_cc framework toolchains. TOOLCHAIN_MAPPINGS = [ _toolchain_mapping( + repo_name = "rules_foreign_cc_framework_toolchain_linux", exec_compatible_with = [ "@platforms//os:linux", ], - file = Label("@rules_foreign_cc//foreign_cc/private/framework/toolchains:linux_commands.bzl"), + file = "@rules_foreign_cc//foreign_cc/private/framework/toolchains:linux_commands.bzl", ), _toolchain_mapping( + repo_name = "rules_foreign_cc_framework_toolchain_freebsd", exec_compatible_with = [ "@platforms//os:freebsd", ], - file = Label("@rules_foreign_cc//foreign_cc/private/framework/toolchains:freebsd_commands.bzl"), + file = "@rules_foreign_cc//foreign_cc/private/framework/toolchains:freebsd_commands.bzl", ), _toolchain_mapping( + repo_name = "rules_foreign_cc_framework_toolchain_windows", exec_compatible_with = [ "@platforms//os:windows", ], - file = Label("@rules_foreign_cc//foreign_cc/private/framework/toolchains:windows_commands.bzl"), + file = "@rules_foreign_cc//foreign_cc/private/framework/toolchains:windows_commands.bzl", ), _toolchain_mapping( + repo_name = "rules_foreign_cc_framework_toolchain_macos", exec_compatible_with = [ "@platforms//os:macos", ], - file = Label("@rules_foreign_cc//foreign_cc/private/framework/toolchains:macos_commands.bzl"), + file = "@rules_foreign_cc//foreign_cc/private/framework/toolchains:macos_commands.bzl", ), ] diff --git a/foreign_cc/repositories.bzl b/foreign_cc/repositories.bzl index e257d2a61..01cb57797 100644 --- a/foreign_cc/repositories.bzl +++ b/foreign_cc/repositories.bzl @@ -52,10 +52,10 @@ def rules_foreign_cc_dependencies( native.register_toolchains(*native_tools_toolchains) native.register_toolchains( - str(Label("//toolchains:preinstalled_autoconf_toolchain")), - str(Label("//toolchains:preinstalled_automake_toolchain")), - str(Label("//toolchains:preinstalled_m4_toolchain")), - str(Label("//toolchains:preinstalled_pkgconfig_toolchain")), + "@rules_foreign_cc//toolchains:preinstalled_autoconf_toolchain", + "@rules_foreign_cc//toolchains:preinstalled_automake_toolchain", + "@rules_foreign_cc//toolchains:preinstalled_m4_toolchain", + "@rules_foreign_cc//toolchains:preinstalled_pkgconfig_toolchain", ) if register_default_tools: diff --git a/toolchains/built_toolchains.bzl b/toolchains/built_toolchains.bzl index 9fa783d74..f8aa9869b 100644 --- a/toolchains/built_toolchains.bzl +++ b/toolchains/built_toolchains.bzl @@ -415,7 +415,7 @@ def _make_toolchain(version, register_toolchains): http_archive, name = "gnumake_src", build_file_content = _ALL_CONTENT, - patches = [str(Label("//toolchains:make-reproducible-bootstrap.patch"))], + patches = [Label("//toolchains:make-reproducible-bootstrap.patch")], sha256 = "e05fdde47c5f7ca45cb697e973894ff4f5d79e13b750ed57d7b66d8defc78e19", strip_prefix = "make-4.3", urls = [ diff --git a/toolchains/native_tools/tool_access.bzl b/toolchains/native_tools/tool_access.bzl index 5dc5404b6..0e611f1b6 100644 --- a/toolchains/native_tools/tool_access.bzl +++ b/toolchains/native_tools/tool_access.bzl @@ -8,7 +8,7 @@ def access_tool(toolchain_type_, ctx, tool_name): """A helper macro for getting the path to a build tool's executable Args: - toolchain_type_ (str): The name of the toolchain type + toolchain_type_ (Label): The name of the toolchain type ctx (ctx): The rule's context object tool_name (str): The name of the tool to query @@ -24,25 +24,25 @@ def access_tool(toolchain_type_, ctx, tool_name): ) def get_autoconf_data(ctx): - return _access_and_expect_label_copied(str(Label("//toolchains:autoconf_toolchain")), ctx, "autoconf") + return _access_and_expect_label_copied(Label("//toolchains:autoconf_toolchain"), ctx, "autoconf") def get_automake_data(ctx): - return _access_and_expect_label_copied(str(Label("//toolchains:automake_toolchain")), ctx, "automake") + return _access_and_expect_label_copied(Label("//toolchains:automake_toolchain"), ctx, "automake") def get_cmake_data(ctx): - return _access_and_expect_label_copied(str(Label("//toolchains:cmake_toolchain")), ctx, "cmake") + return _access_and_expect_label_copied(Label("//toolchains:cmake_toolchain"), ctx, "cmake") def get_m4_data(ctx): - return _access_and_expect_label_copied(str(Label("//toolchains:m4_toolchain")), ctx, "m4") + return _access_and_expect_label_copied(Label("//toolchains:m4_toolchain"), ctx, "m4") def get_make_data(ctx): - return _access_and_expect_label_copied(str(Label("//toolchains:make_toolchain")), ctx, "make") + return _access_and_expect_label_copied(Label("//toolchains:make_toolchain"), ctx, "make") def get_ninja_data(ctx): - return _access_and_expect_label_copied(str(Label("//toolchains:ninja_toolchain")), ctx, "ninja") + return _access_and_expect_label_copied(Label("//toolchains:ninja_toolchain"), ctx, "ninja") def get_pkgconfig_data(ctx): - return _access_and_expect_label_copied(str(Label("//toolchains:pkgconfig_toolchain")), ctx, "pkg-config") + return _access_and_expect_label_copied(Label("//toolchains:pkgconfig_toolchain"), ctx, "pkg-config") def _access_and_expect_label_copied(toolchain_type_, ctx, tool_name): tool_data = access_tool(toolchain_type_, ctx, tool_name) diff --git a/toolchains/toolchains.bzl b/toolchains/toolchains.bzl index b561682fa..938ab026e 100644 --- a/toolchains/toolchains.bzl +++ b/toolchains/toolchains.bzl @@ -13,7 +13,7 @@ prebuilt_toolchains = _prebuilt_toolchains def preinstalled_toolchains(): """Register toolchains for various build tools expected to be installed on the exec host""" native.register_toolchains( - str(Label("//toolchains:preinstalled_cmake_toolchain")), - str(Label("//toolchains:preinstalled_make_toolchain")), - str(Label("//toolchains:preinstalled_ninja_toolchain")), + "@rules_foreign_cc//toolchains:preinstalled_cmake_toolchain", + "@rules_foreign_cc//toolchains:preinstalled_make_toolchain", + "@rules_foreign_cc//toolchains:preinstalled_ninja_toolchain", )