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