From 4aa243d4db416e9e57d9c1e0c815b76d09d8745d Mon Sep 17 00:00:00 2001 From: James Sharpe Date: Thu, 7 Apr 2022 16:44:38 +0100 Subject: [PATCH] Fixups to use of Labels to support bzlmod (#872) --- foreign_cc/built_tools/make_build.bzl | 2 +- foreign_cc/built_tools/ninja_build.bzl | 2 +- foreign_cc/private/framework/toolchain.bzl | 31 ++++------------ .../private/framework/toolchains/access.bzl | 2 +- .../framework/toolchains/freebsd_commands.bzl | 35 +++++++++++++++++++ .../framework/toolchains/linux_commands.bzl | 35 +++++++++++++++++++ .../framework/toolchains/macos_commands.bzl | 35 +++++++++++++++++++ .../private/framework/toolchains/mappings.bzl | 18 ++++++---- .../framework/toolchains/windows_commands.bzl | 35 +++++++++++++++++++ foreign_cc/repositories.bzl | 8 ++--- toolchains/built_toolchains.bzl | 2 +- toolchains/native_tools/tool_access.bzl | 16 ++++----- toolchains/toolchains.bzl | 6 ++-- 13 files changed, 178 insertions(+), 49 deletions(-) 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 5d55da301..81efcc593 100644 --- a/foreign_cc/private/framework/toolchain.bzl +++ b/foreign_cc/private/framework/toolchain.bzl @@ -1,6 +1,5 @@ """A module containing the implementation of the foreign_cc framework's toolchains""" -load("//foreign_cc/private/framework/toolchains:commands.bzl", "PLATFORM_COMMANDS") load("//foreign_cc/private/framework/toolchains:mappings.bzl", "TOOLCHAIN_MAPPINGS") _BUILD_FILE_CONTENT = """\ @@ -17,7 +16,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}, ) @@ -26,11 +25,7 @@ toolchain( _DEFS_BZL_CONTENT = """\ load( "{commands_src}", - {symbols} -) - -commands = struct( - {commands} + "commands" ) def _foreign_cc_framework_toolchain_impl(ctx): @@ -51,16 +46,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, - 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()]), + commands_src = repository_ctx.attr.commands_src, )) repository_ctx.file("BUILD.bazel", _BUILD_FILE_CONTENT.format( @@ -72,9 +59,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 +81,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/freebsd_commands.bzl b/foreign_cc/private/framework/toolchains/freebsd_commands.bzl index 4585cebd3..ad0a61de9 100644 --- a/foreign_cc/private/framework/toolchains/freebsd_commands.bzl +++ b/foreign_cc/private/framework/toolchains/freebsd_commands.bzl @@ -261,3 +261,38 @@ if [[ -L "{file}" ]]; then rm "{file}" && cp -a "${{target}}" "{file}" fi """.format(file = file) + +commands = struct( + assert_script_errors = assert_script_errors, + cat = cat, + children_to_path = children_to_path, + cleanup_function = cleanup_function, + copy_dir_contents_to_dir = copy_dir_contents_to_dir, + define_absolute_paths = define_absolute_paths, + define_function = define_function, + define_sandbox_paths = define_sandbox_paths, + disable_tracing = disable_tracing, + echo = echo, + enable_tracing = enable_tracing, + env = env, + export_var = export_var, + if_else = if_else, + increment_pkg_config_path = increment_pkg_config_path, + local_var = local_var, + mkdirs = mkdirs, + path = path, + pwd = pwd, + redirect_out_err = redirect_out_err, + replace_absolute_paths = replace_absolute_paths, + replace_in_files = replace_in_files, + replace_sandbox_paths = replace_sandbox_paths, + replace_symlink = replace_symlink, + rm_rf = rm_rf, + script_extension = script_extension, + script_prelude = script_prelude, + shebang = shebang, + symlink_contents_to_dir = symlink_contents_to_dir, + symlink_to_dir = symlink_to_dir, + touch = touch, + use_var = use_var, +) diff --git a/foreign_cc/private/framework/toolchains/linux_commands.bzl b/foreign_cc/private/framework/toolchains/linux_commands.bzl index 6c09484e6..575d3aca0 100644 --- a/foreign_cc/private/framework/toolchains/linux_commands.bzl +++ b/foreign_cc/private/framework/toolchains/linux_commands.bzl @@ -243,3 +243,38 @@ if [[ -L "{file}" ]]; then rm "{file}" && cp -a "${{target}}" "{file}" fi """.format(file = file) + +commands = struct( + assert_script_errors = assert_script_errors, + cat = cat, + children_to_path = children_to_path, + cleanup_function = cleanup_function, + copy_dir_contents_to_dir = copy_dir_contents_to_dir, + define_absolute_paths = define_absolute_paths, + define_function = define_function, + define_sandbox_paths = define_sandbox_paths, + disable_tracing = disable_tracing, + echo = echo, + enable_tracing = enable_tracing, + env = env, + export_var = export_var, + if_else = if_else, + increment_pkg_config_path = increment_pkg_config_path, + local_var = local_var, + mkdirs = mkdirs, + path = path, + pwd = pwd, + redirect_out_err = redirect_out_err, + replace_absolute_paths = replace_absolute_paths, + replace_in_files = replace_in_files, + replace_sandbox_paths = replace_sandbox_paths, + replace_symlink = replace_symlink, + rm_rf = rm_rf, + script_extension = script_extension, + script_prelude = script_prelude, + shebang = shebang, + symlink_contents_to_dir = symlink_contents_to_dir, + symlink_to_dir = symlink_to_dir, + touch = touch, + use_var = use_var, +) diff --git a/foreign_cc/private/framework/toolchains/macos_commands.bzl b/foreign_cc/private/framework/toolchains/macos_commands.bzl index 68d486ed1..d5e8e42ba 100644 --- a/foreign_cc/private/framework/toolchains/macos_commands.bzl +++ b/foreign_cc/private/framework/toolchains/macos_commands.bzl @@ -256,3 +256,38 @@ if [[ -L "{file}" ]]; then rm "{file}" && cp -a "${{target}}" "{file}" fi """.format(file = file) + +commands = struct( + assert_script_errors = assert_script_errors, + cat = cat, + children_to_path = children_to_path, + cleanup_function = cleanup_function, + copy_dir_contents_to_dir = copy_dir_contents_to_dir, + define_absolute_paths = define_absolute_paths, + define_function = define_function, + define_sandbox_paths = define_sandbox_paths, + disable_tracing = disable_tracing, + echo = echo, + enable_tracing = enable_tracing, + env = env, + export_var = export_var, + if_else = if_else, + increment_pkg_config_path = increment_pkg_config_path, + local_var = local_var, + mkdirs = mkdirs, + path = path, + pwd = pwd, + redirect_out_err = redirect_out_err, + replace_absolute_paths = replace_absolute_paths, + replace_in_files = replace_in_files, + replace_sandbox_paths = replace_sandbox_paths, + replace_symlink = replace_symlink, + rm_rf = rm_rf, + script_extension = script_extension, + script_prelude = script_prelude, + shebang = shebang, + symlink_contents_to_dir = symlink_contents_to_dir, + symlink_to_dir = symlink_to_dir, + touch = touch, + use_var = use_var, +) diff --git a/foreign_cc/private/framework/toolchains/mappings.bzl b/foreign_cc/private/framework/toolchains/mappings.bzl index ff7849b91..ac1a4ea45 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/private/framework/toolchains/windows_commands.bzl b/foreign_cc/private/framework/toolchains/windows_commands.bzl index 547eb2323..223be6ff7 100644 --- a/foreign_cc/private/framework/toolchains/windows_commands.bzl +++ b/foreign_cc/private/framework/toolchains/windows_commands.bzl @@ -257,3 +257,38 @@ if [[ -L "{file}" ]]; then rm "{file}" && cp -a "${{target}}" "{file}" fi """.format(file = file) + +commands = struct( + assert_script_errors = assert_script_errors, + cat = cat, + children_to_path = children_to_path, + cleanup_function = cleanup_function, + copy_dir_contents_to_dir = copy_dir_contents_to_dir, + define_absolute_paths = define_absolute_paths, + define_function = define_function, + define_sandbox_paths = define_sandbox_paths, + disable_tracing = disable_tracing, + echo = echo, + enable_tracing = enable_tracing, + env = env, + export_var = export_var, + if_else = if_else, + increment_pkg_config_path = increment_pkg_config_path, + local_var = local_var, + mkdirs = mkdirs, + path = path, + pwd = pwd, + redirect_out_err = redirect_out_err, + replace_absolute_paths = replace_absolute_paths, + replace_in_files = replace_in_files, + replace_sandbox_paths = replace_sandbox_paths, + replace_symlink = replace_symlink, + rm_rf = rm_rf, + script_extension = script_extension, + script_prelude = script_prelude, + shebang = shebang, + symlink_contents_to_dir = symlink_contents_to_dir, + symlink_to_dir = symlink_to_dir, + touch = touch, + use_var = use_var, +) diff --git a/foreign_cc/repositories.bzl b/foreign_cc/repositories.bzl index b8ccac878..a87a04082 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 3c84202d5..5a94454fb 100644 --- a/toolchains/built_toolchains.bzl +++ b/toolchains/built_toolchains.bzl @@ -417,7 +417,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 193f07c12..045c498ed 100644 --- a/toolchains/native_tools/tool_access.bzl +++ b/toolchains/native_tools/tool_access.bzl @@ -6,7 +6,7 @@ def access_tool(toolchain_type_, ctx): """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 Returns: @@ -18,25 +18,25 @@ def access_tool(toolchain_type_, ctx): fail("No toolchain found for " + toolchain_type_) def get_autoconf_data(ctx): - return _access_and_expect_label_copied(str(Label("//toolchains:autoconf_toolchain")), ctx) + return _access_and_expect_label_copied(Label("//toolchains:autoconf_toolchain"), ctx) def get_automake_data(ctx): - return _access_and_expect_label_copied(str(Label("//toolchains:automake_toolchain")), ctx) + return _access_and_expect_label_copied(Label("//toolchains:automake_toolchain"), ctx) def get_cmake_data(ctx): - return _access_and_expect_label_copied(str(Label("//toolchains:cmake_toolchain")), ctx) + return _access_and_expect_label_copied(Label("//toolchains:cmake_toolchain"), ctx) def get_m4_data(ctx): - return _access_and_expect_label_copied(str(Label("//toolchains:m4_toolchain")), ctx) + return _access_and_expect_label_copied(Label("//toolchains:m4_toolchain"), ctx) def get_make_data(ctx): - return _access_and_expect_label_copied(str(Label("//toolchains:make_toolchain")), ctx) + return _access_and_expect_label_copied(Label("//toolchains:make_toolchain"), ctx) def get_ninja_data(ctx): - return _access_and_expect_label_copied(str(Label("//toolchains:ninja_toolchain")), ctx) + return _access_and_expect_label_copied(Label("//toolchains:ninja_toolchain"), ctx) def get_pkgconfig_data(ctx): - return _access_and_expect_label_copied(str(Label("//toolchains:pkgconfig_toolchain")), ctx) + return _access_and_expect_label_copied(Label("//toolchains:pkgconfig_toolchain"), ctx) def _access_and_expect_label_copied(toolchain_type_, ctx): tool_data = access_tool(toolchain_type_, ctx) diff --git a/toolchains/toolchains.bzl b/toolchains/toolchains.bzl index 1d0b0d16e..64f880e48 100644 --- a/toolchains/toolchains.bzl +++ b/toolchains/toolchains.bzl @@ -13,9 +13,9 @@ 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", ) def _current_toolchain_impl(ctx):