Laszlo Csomor | 81ed3ad | 2018-04-16 05:11:38 -0700 | [diff] [blame] | 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 the shell toolchain on the local machine.""" |
| 15 | |
| 16 | def _is_windows(repository_ctx): |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 17 | """Returns true if the host OS is Windows.""" |
| 18 | return repository_ctx.os.name.startswith("windows") |
Laszlo Csomor | 81ed3ad | 2018-04-16 05:11:38 -0700 | [diff] [blame] | 19 | |
| 20 | def _sh_config_impl(repository_ctx): |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 21 | """sh_config rule implementation. |
Laszlo Csomor | 81ed3ad | 2018-04-16 05:11:38 -0700 | [diff] [blame] | 22 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 23 | Detects the path of the shell interpreter on the local machine and |
| 24 | stores it in a sh_toolchain rule. |
Laszlo Csomor | 81ed3ad | 2018-04-16 05:11:38 -0700 | [diff] [blame] | 25 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 26 | 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 Csomor | a0ed8c6 | 2018-10-25 02:59:42 -0700 | [diff] [blame] | 34 | # 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 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 38 | # 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 Csomor | 81ed3ad | 2018-04-16 05:11:38 -0700 | [diff] [blame] | 49 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 50 | if not sh_path: |
| 51 | sh_path = "" |
Laszlo Csomor | 81ed3ad | 2018-04-16 05:11:38 -0700 | [diff] [blame] | 52 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 53 | if sh_path and _is_windows(repository_ctx): |
| 54 | sh_path = sh_path.replace("\\", "/") |
Laszlo Csomor | 81ed3ad | 2018-04-16 05:11:38 -0700 | [diff] [blame] | 55 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 56 | repository_ctx.file("BUILD", """ |
Laszlo Csomor | 81ed3ad | 2018-04-16 05:11:38 -0700 | [diff] [blame] | 57 | load("@bazel_tools//tools/sh:sh_toolchain.bzl", "sh_toolchain") |
| 58 | |
| 59 | sh_toolchain( |
| 60 | name = "local_sh", |
laszlocsomor | 2cbcde3 | 2018-05-30 02:08:15 -0700 | [diff] [blame] | 61 | path = "{sh_path}", |
Laszlo Csomor | 81ed3ad | 2018-04-16 05:11:38 -0700 | [diff] [blame] | 62 | visibility = ["//visibility:public"], |
| 63 | ) |
| 64 | |
| 65 | toolchain( |
| 66 | name = "local_sh_toolchain", |
Laszlo Csomor | 81ed3ad | 2018-04-16 05:11:38 -0700 | [diff] [blame] | 67 | toolchain = ":local_sh", |
| 68 | toolchain_type = "@bazel_tools//tools/sh:toolchain_type", |
| 69 | ) |
laszlocsomor | 2cbcde3 | 2018-05-30 02:08:15 -0700 | [diff] [blame] | 70 | """.format(sh_path = sh_path)) |
Laszlo Csomor | 81ed3ad | 2018-04-16 05:11:38 -0700 | [diff] [blame] | 71 | |
| 72 | sh_config = repository_rule( |
| 73 | environ = [ |
| 74 | "WINDIR", |
| 75 | "PATH", |
| 76 | ], |
| 77 | local = True, |
| 78 | implementation = _sh_config_impl, |
| 79 | ) |
| 80 | |
| 81 | def sh_configure(): |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 82 | """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") |
wyv | 156d9c0 | 2022-05-11 04:45:59 -0700 | [diff] [blame] | 85 | |
| 86 | sh_configure_extension = module_extension( |
| 87 | implementation = lambda ctx: sh_config(name = "local_config_sh"), |
| 88 | ) |