xref: /aosp_15_r20/external/skia/toolchain/utils.bzl (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1"""This module provides the gcs_mirror_url macro."""
2
3# Set to True to force the macro to only return the mirror URL.
4_TEST_GCS_MIRROR = False
5
6# Must be kept in sync with the suffixes supported by gcs_mirror (e.g.
7# https://skia.googlesource.com/skia/+/8ad66c2340713234df6b249e793415233337a103/bazel/gcs_mirror/gcs_mirror.go#140).
8_SUPPORTED_SUFFIXES = [".tar.gz", ".tgz", ".tar.xz", ".deb", ".zip"]
9
10_GCS_MIRROR_PREFIX = "https://storage.googleapis.com/skia-world-readable/bazel"
11
12def gcs_mirror_url(url, sha256):
13    """Takes the URL of an external resource and computes its GCS mirror URL.
14
15    We store backup copies of external resources in the skia-world-readable GCS bucket. This macro
16    returns a list with two elements: the original URL, and the mirrored URL.
17
18    Files are expected to be in the mirror location named after their sha256 hash. The files should
19    still have their file extension, as some of the Starlark functions sniff the file extension
20    (e.g. download_and_extract). See //bazel/gcs_mirror for an automated way to update this mirror.
21
22    To mirror a new URL, please use the `gcs_mirror` utility found at
23    https://skia.googlesource.com/skia/+/8ad66c2340713234df6b249e793415233337a103/bazel/gcs_mirror/gcs_mirror.go.
24
25    Args:
26        url: URL of the mirrored resource.
27        sha256: SHA256 hash of the mirrored resource.
28    Returns:
29        A list of the form [original URL, mirror URL].
30    """
31    extension = ""
32    for suffix in _SUPPORTED_SUFFIXES:
33        if url.endswith(suffix):
34            extension = suffix
35            break
36    if extension == "":
37        fail("URL %s has an unsupported suffix." % url)
38
39    mirror_url = "%s/%s%s" % (_GCS_MIRROR_PREFIX, sha256, extension)
40    return [mirror_url] if _TEST_GCS_MIRROR else [url, mirror_url]
41
42def gcs_mirror_only(sha256, suffix):
43    if suffix not in _SUPPORTED_SUFFIXES:
44        fail("unsupported suffix %s" % suffix)
45    return "%s/%s%s" % (_GCS_MIRROR_PREFIX, sha256, suffix)
46