| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Copyright 2023 Google LLC. |
| 4 |
# |
| 5 |
# Use of this source code is governed by a BSD-style license that can be |
| 6 |
# found in the LICENSE file. |
| 7 |
|
| 8 |
""" |
| 9 |
This script writes the full path to the MacSDK that is being used |
| 10 |
by the clang_mac toolchain for builds within this workspace. This |
| 11 |
path is created by //toolchain/download_mac_toolchain.bzl when |
| 12 |
downloading the mac toolchain, and the MacSDK directory is populated |
| 13 |
with symlinks to XCode's MacSDK contents. |
| 14 |
""" |
| 15 |
|
| 16 |
import codecs |
| 17 |
import hashlib |
| 18 |
import os |
| 19 |
import subprocess |
| 20 |
import sys |
| 21 |
from pathlib import Path |
| 22 |
|
| 23 |
|
| 24 |
def GetWorkspaceDir() -> str: |
| 25 |
"""Return the workspace directory containing this script.""" |
| 26 |
this_script_path = Path(os.path.realpath(__file__)) |
| 27 |
return str(this_script_path.parent.parent) |
| 28 |
|
| 29 |
|
| 30 |
def GetBazelWorkspaceHash() -> str: |
| 31 |
"""Return the Bazel hash for this workspace. |
| 32 |
|
| 33 |
This is the MD5 has of the full path to the workspace. See |
| 34 |
https://bazel.build/remote/output-directories#layout-diagram for more detail.""" |
| 35 |
ws = GetWorkspaceDir().encode("utf-8") |
| 36 |
return hashlib.md5(ws).hexdigest() |
| 37 |
|
| 38 |
|
| 39 |
def GetBazelRepositoryCacheDir() -> str: |
| 40 |
"""Return the Bazel repository cache directory.""" |
| 41 |
|
| 42 |
prev_cwd = os.getcwd() |
| 43 |
os.chdir(GetWorkspaceDir()) |
| 44 |
cmd = ["bazelisk", "info", "repository_cache"] |
| 45 |
output = subprocess.check_output(cmd) |
| 46 |
decoded_output = codecs.decode(output, "utf-8") |
| 47 |
return decoded_output.strip() |
| 48 |
|
| 49 |
|
| 50 |
def GetBazelOutputDir() -> str: |
| 51 |
"""Return the Bazel output directory. |
| 52 |
|
| 53 |
This is described in https://bazel.build/remote/output-directories""" |
| 54 |
repo_cache_dir = Path(GetBazelRepositoryCacheDir()) |
| 55 |
# The repository cache is inside the output directory, so going up |
| 56 |
# three levels returns the output directory. |
| 57 |
output_dir = repo_cache_dir.parent.parent.parent |
| 58 |
return str(output_dir) |
| 59 |
|
| 60 |
|
| 61 |
def GetBazelWorkspaceCacheDir() -> str: |
| 62 |
"""Determine the output directory cache for this workspace. |
| 63 |
|
| 64 |
Note: The Bazel docs(1) are very clear that the organization of the output |
| 65 |
directory may change at any time. |
| 66 |
|
| 67 |
(1) https://bazel.build/remote/output-directories |
| 68 |
""" |
| 69 |
return os.path.join(GetBazelOutputDir(), GetBazelWorkspaceHash()) |
| 70 |
|
| 71 |
|
| 72 |
def GetMacSDKSymlinkDir() -> str: |
| 73 |
"""Determine the MacSDK symlinks directory for this workspace.""" |
| 74 |
return os.path.join(GetBazelWorkspaceCacheDir(), "external", "clang_mac", "symlinks", "xcode", "MacSDK") |
| 75 |
|
| 76 |
|
| 77 |
if "__main__" == __name__: |
| 78 |
print(GetMacSDKSymlinkDir()) |