• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://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, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Bazel rules for downloading CIPD packages."""
15
16load(
17    "//pw_env_setup/bazel/cipd_setup/internal:cipd_internal.bzl",
18    _cipd_client_impl = "cipd_client_impl",
19    _cipd_deps_impl = "cipd_deps_impl",
20    _cipd_repository_impl = "cipd_repository_impl",
21)
22
23_cipd_client_repository = repository_rule(
24    _cipd_client_impl,
25    attrs = {
26        "_cipd_digest_file": attr.label(default = str(Label("//pw_env_setup:py/pw_env_setup/cipd_setup/.cipd_version.digests"))),
27        "_cipd_version_file": attr.label(default = str(Label("//pw_env_setup:py/pw_env_setup/cipd_setup/.cipd_version"))),
28    },
29    doc = """
30Fetches the cipd client.
31
32This rule should not be used directly and instead should be called via
33the cipd_client_repository macro.
34""",
35)
36
37def cipd_client_repository():
38    """Fetches the cipd client.
39
40    Fetches the cipd client to the prescribed remote repository target
41    prefix 'cipd_client'. This rule should be called before a
42    cipd_repository rule is instantiated.
43    """
44    _cipd_client_repository(
45        name = "cipd_client",
46    )
47
48cipd_repository = repository_rule(
49    _cipd_repository_impl,
50    attrs = {
51        "build_file": attr.label(
52            allow_single_file = True,
53            doc = "Override the BUILD file in the new CIPD repository.",
54        ),
55        "patch_args": attr.string_list(
56            doc = "Arguments to pass to the patch tool. List of strings.",
57            default = [],
58        ),
59        "patches": attr.label_list(
60            doc = "A list of patches to apply to the CIPD package after downloading it",
61            default = [],
62        ),
63        "path": attr.string(
64            doc = "Path within CIPD where this repository lives.",
65        ),
66        "tag": attr.string(
67            doc = "Tag specifying which version of the repository to fetch.",
68        ),
69        "tag_by_os": attr.string_dict(
70            doc = "Dict from OS name (mac, linux, windows) to tag. Incompatible with the 'tag' attribute.",
71        ),
72        "_cipd_client": attr.label(
73            default = "@cipd_client//:cipd",
74            doc = "Location of the CIPD client binary (internal).",
75        ),
76    },
77    doc = """
78Downloads a singular CIPD dependency to the root of a remote repository.
79
80Example:
81
82    load(
83        "@pigweed//pw_env_setup/bazel/cipd_setup:cipd_rules.bzl",
84        "cipd_client_repository",
85        "cipd_repository",
86    )
87
88    # Must be called before cipd_repository
89    cipd_client_repository()
90
91    cipd_repository(
92        name = "bloaty",
93        path = "pigweed/third_party/bloaty-embedded/${os=linux,mac}-${arch=amd64}",
94        tag = "git_revision:2d87d204057b419f5290f8d38b61b9c2c5b4fb52-2",
95    )
96""",
97)
98
99_pigweed_deps = repository_rule(
100    _cipd_deps_impl,
101    attrs = {
102        "_pigweed_packages_json": attr.label(
103            default = str(Label("//pw_env_setup:py/pw_env_setup/cipd_setup/pigweed.json")),
104        ),
105        "_upstream_testing_packages_json": attr.label(
106            default = str(Label("//pw_env_setup:py/pw_env_setup/cipd_setup/testing.json")),
107        ),
108    },
109)
110
111def pigweed_deps():
112    """Configures Pigweeds Bazel dependencies
113
114    Example:
115        load("@pigweed//pw_env_setup:pigweed_deps.bzl", "pigweed_deps")
116
117        pigweed_deps()
118
119        load("@cipd_deps//:cipd_init.bzl", "cipd_init")
120
121        cipd_init()
122"""
123    _pigweed_deps(
124        name = "cipd_deps",
125    )
126