Brian Silverman | 7d89e28 | 2021-11-17 17:36:54 -0800 | [diff] [blame^] | 1 | # Copyright 2018 The Bazel Authors. |
| 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 | |
| 15 | load( |
| 16 | "//toolchain/internal:common.bzl", |
| 17 | _os_arch_pair = "os_arch_pair", |
| 18 | _pkg_path_from_label = "pkg_path_from_label", |
| 19 | ) |
| 20 | |
| 21 | def _darwin_sdk_path(rctx): |
| 22 | exec_result = rctx.execute(["/usr/bin/xcrun", "--show-sdk-path", "--sdk", "macosx"]) |
| 23 | if exec_result.return_code: |
| 24 | fail("Failed to detect OSX SDK path: \n%s\n%s" % (exec_result.stdout, exec_result.stderr)) |
| 25 | if exec_result.stderr: |
| 26 | print(exec_result.stderr) |
| 27 | return exec_result.stdout.strip() |
| 28 | |
| 29 | # Default sysroot path can be used when the user has not provided an explicit |
| 30 | # sysroot for the target, and when host platform is the same as target |
| 31 | # platform. |
| 32 | def default_sysroot_path(rctx, os): |
| 33 | if os == "darwin": |
| 34 | return _darwin_sdk_path(rctx) |
| 35 | else: |
| 36 | return "" |
| 37 | |
| 38 | # Return the sysroot path and the label to the files, if sysroot is not a system path. |
| 39 | def sysroot_path(sysroot_dict, os, arch): |
| 40 | sysroot = sysroot_dict.get(_os_arch_pair(os, arch)) |
| 41 | if not sysroot: |
| 42 | return (None, None) |
| 43 | |
| 44 | # If the sysroot is an absolute path, use it as-is. Check for things that |
| 45 | # start with "/" and not "//" to identify absolute paths, but also support |
| 46 | # passing the sysroot as "/" to indicate the root directory. |
| 47 | if sysroot[0] == "/" and (len(sysroot) == 1 or sysroot[1] != "/"): |
| 48 | return (sysroot, None) |
| 49 | |
| 50 | sysroot_path = _pkg_path_from_label(Label(sysroot)) |
| 51 | return (sysroot_path, sysroot) |