From 720e15efee88dfe115eb2c3b184225e2c0d42ff0 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Mon, 23 Dec 2024 14:13:45 -0800 Subject: [PATCH] Forward `@bazel_tools//tools/sh:toolchain_type` to `rules_shell` This is required for compatibility with modules that rely on the auto-registered `sh_toolchain` to obtain a shebang, e.g. `rules_js`. Also remove implementations and toolchain registrations in favor of forwarders. Closes #24748. PiperOrigin-RevId: 709149200 Change-Id: I758cf403c24f6b5e6f00bbf5b4e0313a30773f78 --- scripts/bootstrap/compile.sh | 1 - src/MODULE.tools | 4 - .../lib/analysis/mock/BazelAnalysisMock.java | 6 -- .../packages/BazelPackageLoaderTest.java | 4 - tools/sh/BUILD | 1 - tools/sh/BUILD.tools | 4 +- tools/sh/sh_configure.bzl | 90 ------------------- tools/sh/sh_toolchain.bzl | 14 +-- 8 files changed, 5 insertions(+), 119 deletions(-) delete mode 100644 tools/sh/sh_configure.bzl diff --git a/scripts/bootstrap/compile.sh b/scripts/bootstrap/compile.sh index 05a6428fae65fb..6e519498762d67 100755 --- a/scripts/bootstrap/compile.sh +++ b/scripts/bootstrap/compile.sh @@ -277,7 +277,6 @@ EOF # Create @bazel_tools//tools/sh mkdir -p ${BAZEL_TOOLS_REPO}/tools/sh - link_file "${PWD}/tools/sh/sh_configure.bzl" "${BAZEL_TOOLS_REPO}/tools/sh/sh_configure.bzl" link_file "${PWD}/tools/sh/sh_toolchain.bzl" "${BAZEL_TOOLS_REPO}/tools/sh/sh_toolchain.bzl" link_file "${PWD}/tools/sh/BUILD.tools" "${BAZEL_TOOLS_REPO}/tools/sh/BUILD" diff --git a/src/MODULE.tools b/src/MODULE.tools index 13b85df75cc98b..2b5669c7e95826 100644 --- a/src/MODULE.tools +++ b/src/MODULE.tools @@ -28,10 +28,6 @@ use_repo( "remote_java_tools_darwin_arm64", ) -sh_configure_extension = use_extension("//tools/sh:sh_configure.bzl", "sh_configure_extension") -use_repo(sh_configure_extension, "local_config_sh") -register_toolchains("@local_config_sh//:local_sh_toolchain") - remote_coverage_tools_extension = use_extension("//tools/test:extensions.bzl", "remote_coverage_tools_extension") use_repo(remote_coverage_tools_extension, "remote_coverage_tools") diff --git a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java index 70fea59b7bb993..9ee004e2bdf8b3 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java @@ -807,12 +807,6 @@ def cc_configure(**kwargs): """); config.create("embedded_tools/tools/sh/BUILD"); - config.create( - "embedded_tools/tools/sh/sh_configure.bzl", - """ - def sh_configure(**kwargs): - pass - """); config.create("embedded_tools/tools/osx/BUILD"); config.create( "embedded_tools/tools/osx/xcode_configure.bzl", diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/packages/BazelPackageLoaderTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/packages/BazelPackageLoaderTest.java index 882271d130d25b..6ea58115942832 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/packages/BazelPackageLoaderTest.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/packages/BazelPackageLoaderTest.java @@ -79,10 +79,6 @@ private static void mockEmbeddedTools(Path embeddedBinaries) throws IOException "def xcode_configure(*args, **kwargs):", " pass"); FileSystemUtils.writeIsoLatin1(tools.getRelative("tools/sh/BUILD"), ""); - FileSystemUtils.writeIsoLatin1( - tools.getRelative("tools/sh/sh_configure.bzl"), - "def sh_configure(*args, **kwargs):", - " pass"); FileSystemUtils.writeIsoLatin1(tools.getRelative("tools/build_defs/repo/BUILD")); FileSystemUtils.writeIsoLatin1( tools.getRelative("tools/build_defs/repo/http.bzl"), diff --git a/tools/sh/BUILD b/tools/sh/BUILD index 66f710a92630ae..07a6f0dcc3e0f5 100644 --- a/tools/sh/BUILD +++ b/tools/sh/BUILD @@ -16,7 +16,6 @@ filegroup( name = "embedded_tools", srcs = [ "BUILD.tools", - "sh_configure.bzl", "sh_toolchain.bzl", ], visibility = ["//tools:__pkg__"], diff --git a/tools/sh/BUILD.tools b/tools/sh/BUILD.tools index b7022ecf5a6a3e..0f09db6e9d6d87 100644 --- a/tools/sh/BUILD.tools +++ b/tools/sh/BUILD.tools @@ -1,10 +1,10 @@ exports_files([ "sh_toolchain.bzl", - "sh_configure.bzl", ]) -toolchain_type( +alias( name = "toolchain_type", + actual = "@rules_shell//shell:toolchain_type", visibility = ["//visibility:public"], ) diff --git a/tools/sh/sh_configure.bzl b/tools/sh/sh_configure.bzl deleted file mode 100644 index 28831ba669d3a4..00000000000000 --- a/tools/sh/sh_configure.bzl +++ /dev/null @@ -1,90 +0,0 @@ -# Copyright 2018 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""Configure the shell toolchain on the local machine.""" - -def _is_windows(repository_ctx): - """Returns true if the host OS is Windows.""" - return repository_ctx.os.name.startswith("windows") - -def _sh_config_impl(repository_ctx): - """sh_config rule implementation. - - Detects the path of the shell interpreter on the local machine and - stores it in a sh_toolchain rule. - - Args: - repository_ctx: the repository rule context object - """ - sh_path = repository_ctx.os.environ.get("BAZEL_SH") - if not sh_path: - if _is_windows(repository_ctx): - sh_path = repository_ctx.which("bash.exe") - if sh_path: - # repository_ctx.which returns a path object, convert that to - # string so we can call string.startswith on it. - sh_path = str(sh_path) - - # When the Windows Subsystem for Linux is installed there's a - # bash.exe under %WINDIR%\system32\bash.exe that launches Ubuntu - # Bash which cannot run native Windows programs so it's not what - # we want. - windir = repository_ctx.os.environ.get("WINDIR") - if windir and sh_path.startswith(windir): - sh_path = None - else: - sh_path = repository_ctx.which("bash") - if not sh_path: - sh_path = repository_ctx.which("sh") - - if not sh_path: - sh_path = "" - - if sh_path and _is_windows(repository_ctx): - sh_path = sh_path.replace("\\", "/") - - repository_ctx.file("BUILD", """ -load("@bazel_tools//tools/sh:sh_toolchain.bzl", "sh_toolchain") - -sh_toolchain( - name = "local_sh", - path = "{sh_path}", - visibility = ["//visibility:public"], -) - -toolchain( - name = "local_sh_toolchain", - toolchain = ":local_sh", - toolchain_type = "@bazel_tools//tools/sh:toolchain_type", -) -""".format(sh_path = sh_path)) - -sh_config = repository_rule( - environ = [ - "WINDIR", - "PATH", - ], - local = True, - implementation = _sh_config_impl, -) - -def sh_configure(): - """Detect the local shell interpreter and register its toolchain.""" - sh_config(name = "local_config_sh") - native.register_toolchains("@local_config_sh//:local_sh_toolchain") - -def _sh_configure_extension_impl(module_ctx): - sh_config(name = "local_config_sh") - return module_ctx.extension_metadata(reproducible = True) - -sh_configure_extension = module_extension(implementation = _sh_configure_extension_impl) diff --git a/tools/sh/sh_toolchain.bzl b/tools/sh/sh_toolchain.bzl index 2a3370469c2fbf..b32ea90caf8da9 100644 --- a/tools/sh/sh_toolchain.bzl +++ b/tools/sh/sh_toolchain.bzl @@ -11,16 +11,8 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -"""Define a toolchain rule for the shell.""" +"""Deprecated forwarders to rules_shell.""" -def _sh_toolchain_impl(ctx): - """sh_toolchain rule implementation.""" - return [platform_common.ToolchainInfo(path = ctx.attr.path)] +load("@rules_shell//shell:sh_toolchain.bzl", _sh_toolchain = "sh_toolchain") -sh_toolchain = rule( - attrs = { - # Absolute path to the shell interpreter. - "path": attr.string(), - }, - implementation = _sh_toolchain_impl, -) +sh_toolchain = _sh_toolchain