xref: /aosp_15_r20/external/skia/bazel/cipd_install.bzl (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1"""This module defines the cipd_install repository rule.
2
3The cipd_install rule is a wrapper around http_archive to download the CIPD
4package at the specified version over HTTPS. This does not require depot_tools nor a cipd binary
5on the host machine.
6"""
7
8load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
9
10def cipd_download_urls(cipd_package, sha256, tag):
11    """Construct download URLs for the given CIPD package.
12
13    Args:
14        cipd_package: The full name of the CIPD package. This is a "path" from the root of CIPD.
15            This should be a publicly accessibly package, as authentication is not supported.
16        sha256: The sha256 hash of the zip archive downloaded from CIPD. This should match the
17            official CIPD website.
18        tag: Represnts the version of the CIPD package to download, e.g. "git_package:abc123...".
19    Returns:
20        A string list containing download URLs for the given CIPD package.
21    """
22    cipd_url = "https://chrome-infra-packages.appspot.com/dl/"
23    cipd_url += cipd_package
24    cipd_url += "/+/"
25
26    # When Bazel downloads the CIPD package, the local file name is "<tag>.zip". On Windows, colon
27    # is not a valid character for file names, so this causes Bazel to crash. URL-escaping the tag
28    # results in a valid file name.
29    cipd_url += tag.replace(":", "%3A")
30
31    mirror_url = "https://storage.googleapis.com/skia-world-readable/bazel/"
32    mirror_url += sha256
33    mirror_url += ".zip"
34
35    return [cipd_url, mirror_url]
36
37def cipd_install(
38        name,
39        cipd_package,
40        sha256,
41        tag,
42        build_file = None,
43        build_file_content = None,
44        postinstall_cmds_posix = None,
45        postinstall_cmds_win = None):
46    """Download and extract the zipped archive from CIPD, making it available for Bazel rules.
47
48    Args:
49        name: The name of the Bazel "repository" created. For example, if name is "alpha_beta",
50            the full Bazel label will start with "@alpha_beta//".
51        cipd_package: The full name of the CIPD package. This is a "path" from the root of CIPD.
52            This should be a publicly accessible package, as authentication is not
53            supported.
54        sha256: The sha256 hash of the zip archive downloaded from CIPD. This should match the
55            official CIPD website.
56        tag: Represents the version of the CIPD package to download, e.g. "git_package:abc123...".
57        build_file: The file to use as the BUILD.bazel file for this repository. Such build files
58            typically contain "exports_files" and/or "filegroup" rules. Since CIPD packages do not
59            include BUILD.bazel files, we must provide our own. Either build_file or
60            build_file_content can be specified, but not both.
61        build_file_content: The content for the BUILD file for this repository. Either build_file
62            or build_file_content can be specified, but not both.
63        postinstall_cmds_posix: Optional Bash commands to run on Mac/Linux after download.
64        postinstall_cmds_win: Optional Powershell commands to run on Windows after download.
65    """
66    urls = cipd_download_urls(cipd_package, sha256, tag)
67
68    http_archive(
69        name = name,
70        sha256 = sha256,
71        urls = urls,
72        type = "zip",
73        build_file = build_file,
74        build_file_content = build_file_content,
75        patch_cmds = postinstall_cmds_posix,
76        patch_cmds_win = postinstall_cmds_win,
77    )
78