blob: a0fb81850e478b4ab33521a1c01fa81a787178ac [file] [log] [blame]
Laszlo Csomor81ed3ad2018-04-16 05:11:38 -07001# 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 the shell toolchain on the local machine."""
15
16def _is_windows(repository_ctx):
vladmos20a042f2018-06-01 04:51:21 -070017 """Returns true if the host OS is Windows."""
18 return repository_ctx.os.name.startswith("windows")
Laszlo Csomor81ed3ad2018-04-16 05:11:38 -070019
20def _sh_config_impl(repository_ctx):
vladmos20a042f2018-06-01 04:51:21 -070021 """sh_config rule implementation.
Laszlo Csomor81ed3ad2018-04-16 05:11:38 -070022
vladmos20a042f2018-06-01 04:51:21 -070023 Detects the path of the shell interpreter on the local machine and
24 stores it in a sh_toolchain rule.
Laszlo Csomor81ed3ad2018-04-16 05:11:38 -070025
vladmos20a042f2018-06-01 04:51:21 -070026 Args:
27 repository_ctx: the repository rule context object
28 """
29 sh_path = repository_ctx.os.environ.get("BAZEL_SH")
30 if not sh_path:
31 if _is_windows(repository_ctx):
32 sh_path = repository_ctx.which("bash.exe")
33 if sh_path:
Laszlo Csomora0ed8c62018-10-25 02:59:42 -070034 # repository_ctx.which returns a path object, convert that to
35 # string so we can call string.startswith on it.
36 sh_path = str(sh_path)
37
vladmos20a042f2018-06-01 04:51:21 -070038 # When the Windows Subsystem for Linux is installed there's a
39 # bash.exe under %WINDIR%\system32\bash.exe that launches Ubuntu
40 # Bash which cannot run native Windows programs so it's not what
41 # we want.
42 windir = repository_ctx.os.environ.get("WINDIR")
43 if windir and sh_path.startswith(windir):
44 sh_path = None
45 else:
46 sh_path = repository_ctx.which("bash")
47 if not sh_path:
48 sh_path = repository_ctx.which("sh")
Laszlo Csomor81ed3ad2018-04-16 05:11:38 -070049
vladmos20a042f2018-06-01 04:51:21 -070050 if not sh_path:
51 sh_path = ""
Laszlo Csomor81ed3ad2018-04-16 05:11:38 -070052
vladmos20a042f2018-06-01 04:51:21 -070053 if sh_path and _is_windows(repository_ctx):
54 sh_path = sh_path.replace("\\", "/")
Laszlo Csomor81ed3ad2018-04-16 05:11:38 -070055
vladmos20a042f2018-06-01 04:51:21 -070056 repository_ctx.file("BUILD", """
Laszlo Csomor81ed3ad2018-04-16 05:11:38 -070057load("@bazel_tools//tools/sh:sh_toolchain.bzl", "sh_toolchain")
58
59sh_toolchain(
60 name = "local_sh",
laszlocsomor2cbcde32018-05-30 02:08:15 -070061 path = "{sh_path}",
Laszlo Csomor81ed3ad2018-04-16 05:11:38 -070062 visibility = ["//visibility:public"],
63)
64
65toolchain(
66 name = "local_sh_toolchain",
Laszlo Csomor81ed3ad2018-04-16 05:11:38 -070067 toolchain = ":local_sh",
68 toolchain_type = "@bazel_tools//tools/sh:toolchain_type",
69)
laszlocsomor2cbcde32018-05-30 02:08:15 -070070""".format(sh_path = sh_path))
Laszlo Csomor81ed3ad2018-04-16 05:11:38 -070071
72sh_config = repository_rule(
73 environ = [
74 "WINDIR",
75 "PATH",
76 ],
77 local = True,
78 implementation = _sh_config_impl,
79)
80
81def sh_configure():
vladmos20a042f2018-06-01 04:51:21 -070082 """Detect the local shell interpreter and register its toolchain."""
83 sh_config(name = "local_config_sh")
84 native.register_toolchains("@local_config_sh//:local_sh_toolchain")
wyv156d9c02022-05-11 04:45:59 -070085
86sh_configure_extension = module_extension(
87 implementation = lambda ctx: sh_config(name = "local_config_sh"),
88)