blob: 38fa39a3507518916170ffed84637d65acee323c [file] [log] [blame]
Yun Pengc1f2aff2023-11-07 09:51:02 -08001# Copyright 2020 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://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,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""WORKSPACE default repository definitions."""
16
17WORKSPACE_REPOS = {
18 # Used in src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/cc_configure.WORKSPACE.
19 # Used in src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
20 # Used in src/test/java/com/google/devtools/build/lib/blackbox/framework/blackbox.WORKSAPCE
21 "rules_cc": {
Yun Peng1c4e78a2024-10-07 12:43:55 -070022 "archive": "rules_cc-0.0.12.tar.gz",
23 "sha256": "b26168b9a13f094794982b832975eaf53cefc5dced5b3be7df6b8b794dc2744b",
24 "urls": ["https://github.com/bazelbuild/rules_cc/releases/download/0.0.12/rules_cc-0.0.12.tar.gz"],
25 "strip_prefix": "rules_cc-0.0.12",
Yun Pengc1f2aff2023-11-07 09:51:02 -080026 },
Googler678b01a2024-10-05 23:16:32 -070027 "protobuf": {
28 "archive": "v27.0.zip",
29 "sha256": "a7e735f510520b41962d07459f6f5b99dd594c7ed4690bf1191b9924bec094a2",
30 "urls": ["https://github.com/protocolbuffers/protobuf/archive/refs/tags/v27.0.zip"],
31 "strip_prefix": "protobuf-27.0",
32 },
Yun Pengc1f2aff2023-11-07 09:51:02 -080033 "rules_java": {
Googler678b01a2024-10-05 23:16:32 -070034 "archive": "rules_java-8.0.0-rc2.tar.gz",
35 "sha256": "7bfdbb51bb362ecdca5d989efa3c77683ce4d431e82e2d2d7aadca33a8c35ca7",
36 "urls": ["https://github.com/bazelbuild/rules_java/releases/download/8.0.0-rc2/rules_java-8.0.0-rc2.tar.gz"],
Yun Pengc1f2aff2023-11-07 09:51:02 -080037 },
Yun Pengc1f2aff2023-11-07 09:51:02 -080038 "bazel_skylib": {
hvd8f55a572024-05-16 23:47:23 -070039 "archive": "bazel-skylib-1.6.1.tar.gz",
40 "sha256": "9f38886a40548c6e96c106b752f242130ee11aaa068a56ba7e56f4511f33e4f2",
41 "urls": ["https://github.com/bazelbuild/bazel-skylib/releases/download/1.6.1/bazel-skylib-1.6.1.tar.gz"],
Yun Pengc1f2aff2023-11-07 09:51:02 -080042 },
43 "rules_license": {
44 "archive": "rules_license-0.0.7.tar.gz",
45 "sha256": "4531deccb913639c30e5c7512a054d5d875698daeb75d8cf90f284375fe7c360",
46 "urls": ["https://github.com/bazelbuild/rules_license/releases/download/0.0.7/rules_license-0.0.7.tar.gz"],
47 },
48 "rules_python": {
Googler228c78d2024-10-03 09:09:48 -070049 "archive": "rules_python-0.36.0.tar.gz",
50 "sha256": "ca77768989a7f311186a29747e3e95c936a41dffac779aff6b443db22290d913",
51 "strip_prefix": "rules_python-0.36.0",
52 "urls": ["https://github.com/bazelbuild/rules_python/releases/download/0.36.0/rules_python-0.36.0.tar.gz"],
Yun Pengc1f2aff2023-11-07 09:51:02 -080053 },
54 "rules_pkg": {
55 "archive": "rules_pkg-0.9.1.tar.gz",
56 "sha256": "8f9ee2dc10c1ae514ee599a8b42ed99fa262b757058f65ad3c384289ff70c4b8",
57 "urls": ["https://github.com/bazelbuild/rules_pkg/releases/download/0.9.1/rules_pkg-0.9.1.tar.gz"],
58 },
Googlerb89ec432024-10-07 10:05:35 -070059 "rules_shell": {
60 "archive": "rules_shell-v0.1.1.tar.gz",
61 "sha256": "0d0c56d01c3c40420bf7bf14d73113f8a92fbd9f5cd13205a3b89f72078f0321",
62 "strip_prefix": "rules_shell-0.1.1",
63 "urls": ["https://github.com/bazelbuild/rules_shell/releases/download/v0.1.1/rules_shell-v0.1.1.tar.gz"],
64 },
Yun Pengc1f2aff2023-11-07 09:51:02 -080065 "rules_testing": {
Fabian Meumertzheim8ee8f792024-04-19 03:39:43 -070066 "archive": "rules_testing-v0.6.0.tar.gz",
67 "sha256": "02c62574631876a4e3b02a1820cb51167bb9cdcdea2381b2fa9d9b8b11c407c4",
68 "strip_prefix": "rules_testing-0.6.0",
69 "urls": ["https://github.com/bazelbuild/rules_testing/releases/download/v0.6.0/rules_testing-v0.6.0.tar.gz"],
Yun Pengc1f2aff2023-11-07 09:51:02 -080070 },
71 "remote_coverage_tools": {
72 "archive": "coverage_output_generator-v2.6.zip",
73 "sha256": "7006375f6756819b7013ca875eab70a541cf7d89142d9c511ed78ea4fefa38af",
74 "urls": ["https://mirror.bazel.build/bazel_coverage_output_generator/releases/coverage_output_generator-v2.6.zip"],
75 },
76}
77
78def _gen_workspace_stanza_impl(ctx):
79 if ctx.attr.template and (ctx.attr.preamble or ctx.attr.postamble):
80 fail("Can not use template with either preamble or postamble")
81 if ctx.attr.use_maybe and ctx.attr.repo_clause:
82 fail("Can not use use_maybe with repo_clause")
83
84 if ctx.attr.use_maybe:
85 repo_clause = """
86maybe(
87 http_archive,
88 name = "{repo}",
89 sha256 = "{sha256}",
90 strip_prefix = {strip_prefix},
91 urls = {urls},
92)
93"""
94 elif ctx.attr.repo_clause:
95 repo_clause = ctx.attr.repo_clause
96 else:
97 repo_clause = """
98http_archive(
99 name = "{repo}",
100 sha256 = "{sha256}",
101 strip_prefix = {strip_prefix},
102 urls = {urls},
103)
104"""
105
106 repo_stanzas = {}
107 for repo in ctx.attr.repos:
108 info = WORKSPACE_REPOS[repo]
109 strip_prefix = info.get("strip_prefix")
110 if strip_prefix:
111 strip_prefix = "\"%s\"" % strip_prefix
112 else:
113 strip_prefix = "None"
114
115 repo_stanzas["{%s}" % repo] = repo_clause.format(
116 repo = repo,
117 sha256 = str(info["sha256"]),
118 strip_prefix = strip_prefix,
119 urls = info["urls"],
120 )
121
122 if ctx.attr.template:
123 ctx.actions.expand_template(
124 output = ctx.outputs.out,
125 template = ctx.file.template,
126 substitutions = repo_stanzas,
127 )
128 else:
129 content = "\n".join([p.strip() for p in ctx.attr.preamble.strip().split("\n")])
130 content += "\n"
131 content += "".join(repo_stanzas.values())
132 content += "\n"
133 content += "\n".join([p.strip() for p in ctx.attr.postamble.strip().split("\n")])
134 content += "\n"
135 ctx.actions.write(ctx.outputs.out, content)
136
137 return [DefaultInfo(files = depset([ctx.outputs.out]))]
138
139gen_workspace_stanza = rule(
140 attrs = {
141 "repos": attr.string_list(doc = "Set of repos to include."),
142 "out": attr.output(mandatory = True),
143 "preamble": attr.string(doc = "Preamble."),
144 "postamble": attr.string(doc = "Set of rules to follow repos."),
145 "template": attr.label(
146 doc = "Template WORKSPACE file. May not be used with preamble or postamble." +
147 "Repo stanzas can be included using the syntax '{repo name}'.",
148 allow_single_file = True,
149 mandatory = False,
150 ),
151 "use_maybe": attr.bool(doc = "Use maybe() invocation instead of http_archive."),
152 "repo_clause": attr.string(doc = "Use a custom clause for each repository."),
153 },
154 doc = "Use specifications from WORKSPACE_REPOS to generate WORKSPACE http_archive stanzas or to" +
155 "drop them into a template.",
156 implementation = _gen_workspace_stanza_impl,
157)