|
| 1 | +# Copyright 2018 The Bazel Authors. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Configure sh_toolchains based on the local machine.""" |
| 15 | + |
| 16 | +visibility(["//shell", "//shell/private/extensions"]) |
| 17 | + |
| 18 | +_DEFAULT_SHELL_PATHS = { |
| 19 | + "windows": "c:/msys64/usr/bin/bash.exe", |
| 20 | + "linux": "/bin/bash", |
| 21 | + "osx": "/bin/bash", |
| 22 | + "freebsd": "/usr/local/bin/bash", |
| 23 | + "openbsd": "/usr/local/bin/bash", |
| 24 | +} |
| 25 | + |
| 26 | +_UNIX_SH_TOOLCHAIN_TEMPLATE = """ |
| 27 | +sh_toolchain( |
| 28 | + name = "{os}_sh", |
| 29 | + path = {sh_path}, |
| 30 | +) |
| 31 | +""" |
| 32 | + |
| 33 | +_WINDOWS_SH_TOOLCHAIN_TEMPLATE = """ |
| 34 | +sh_toolchain( |
| 35 | + name = "{os}_sh", |
| 36 | + path = {sh_path}, |
| 37 | + launcher = "@bazel_tools//tools/launcher", |
| 38 | + launcher_maker = "@bazel_tools//tools/launcher:launcher_maker", |
| 39 | +) |
| 40 | +""" |
| 41 | + |
| 42 | +_TOOLCHAIN_TEMPLATE = """ |
| 43 | +toolchain( |
| 44 | + name = "{os}_sh_toolchain", |
| 45 | + toolchain = ":{os}_sh", |
| 46 | + toolchain_type = "@rules_shell//shell:toolchain_type", |
| 47 | + target_compatible_with = [ |
| 48 | + "@platforms//os:{os}", |
| 49 | + ], |
| 50 | +) |
| 51 | +""" |
| 52 | + |
| 53 | +def _sh_config_impl(repository_ctx): |
| 54 | + """sh_config rule implementation. |
| 55 | +
|
| 56 | + Creates sh_toolchains for commonly supported target platforms. |
| 57 | + For the target platform matching the local machine, it detects the path of |
| 58 | + the shell interpreter instead of using the default path. |
| 59 | +
|
| 60 | + Args: |
| 61 | + repository_ctx: the repository rule context object |
| 62 | + """ |
| 63 | + toolchains = [] |
| 64 | + for os, default_shell_path in _DEFAULT_SHELL_PATHS.items(): |
| 65 | + is_host = repository_ctx.os.name.startswith(os) |
| 66 | + if is_host: |
| 67 | + # This toolchain was first added before optional toolchains were |
| 68 | + # available, so instead of not registering a toolchain if we |
| 69 | + # couldn't find the shell, we register a toolchain with an empty |
| 70 | + # path. |
| 71 | + sh_path = _detect_local_shell_path(repository_ctx) or "" |
| 72 | + else: |
| 73 | + sh_path = default_shell_path |
| 74 | + |
| 75 | + sh_toolchain_template = _WINDOWS_SH_TOOLCHAIN_TEMPLATE if os == "windows" else _UNIX_SH_TOOLCHAIN_TEMPLATE |
| 76 | + toolchains.append(sh_toolchain_template.format( |
| 77 | + os = os, |
| 78 | + sh_path = repr(sh_path), |
| 79 | + )) |
| 80 | + toolchains.append(_TOOLCHAIN_TEMPLATE.format( |
| 81 | + os = os, |
| 82 | + )) |
| 83 | + |
| 84 | + repository_ctx.file("BUILD", """ |
| 85 | +load("@rules_shell//shell/toolchains:sh_toolchain.bzl", "sh_toolchain") |
| 86 | +""" + "\n".join(toolchains)) |
| 87 | + |
| 88 | +sh_config = repository_rule( |
| 89 | + environ = [ |
| 90 | + "WINDIR", |
| 91 | + "PATH", |
| 92 | + ], |
| 93 | + # TODO: Replace this with configure = True and add BAZEL_SH to the |
| 94 | + # environ list above for consistency with CC and other repo rules. |
| 95 | + # This would make discovery differ from --shell_executable. |
| 96 | + local = True, |
| 97 | + implementation = _sh_config_impl, |
| 98 | +) |
| 99 | + |
| 100 | +def _detect_local_shell_path(repository_ctx): |
| 101 | + if repository_ctx.os.name.startswith("windows"): |
| 102 | + return _detect_local_shell_path_windows(repository_ctx) |
| 103 | + else: |
| 104 | + return _detect_local_shell_path_unix(repository_ctx) |
| 105 | + |
| 106 | +def _detect_local_shell_path_windows(repository_ctx): |
| 107 | + sh_path = repository_ctx.os.environ.get("BAZEL_SH") |
| 108 | + if sh_path: |
| 109 | + return sh_path.replace("\\", "/") |
| 110 | + |
| 111 | + sh_path_obj = repository_ctx.which("bash.exe") |
| 112 | + if sh_path_obj: |
| 113 | + # repository_ctx.which returns a path object, convert that to |
| 114 | + # string so we can call string.startswith on it. |
| 115 | + sh_path = str(sh_path_obj) |
| 116 | + |
| 117 | + # When the Windows Subsystem for Linux is installed there's a |
| 118 | + # bash.exe under %WINDIR%\system32\bash.exe that launches Ubuntu |
| 119 | + # Bash which cannot run native Windows programs so it's not what |
| 120 | + # we want. |
| 121 | + windir = repository_ctx.os.environ.get("WINDIR") |
| 122 | + if not windir or not sh_path.startswith(windir): |
| 123 | + return sh_path.replace("\\", "/") |
| 124 | + |
| 125 | + return None |
| 126 | + |
| 127 | +def _detect_local_shell_path_unix(repository_ctx): |
| 128 | + sh_path = repository_ctx.os.environ.get("BAZEL_SH") |
| 129 | + if sh_path: |
| 130 | + return sh_path |
| 131 | + |
| 132 | + sh_path_obj = repository_ctx.which("bash") |
| 133 | + if sh_path_obj: |
| 134 | + return str(sh_path_obj) |
| 135 | + |
| 136 | + sh_path_obj = repository_ctx.which("sh") |
| 137 | + if sh_path_obj: |
| 138 | + return str(sh_path_obj) |
| 139 | + |
| 140 | + return None |
0 commit comments