blob: 96107b0aed4e998a1647a7623df7f8f5666b8666 [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": {
22 "archive": "rules_cc-0.0.9.tar.gz",
23 "sha256": "2037875b9a4456dce4a79d112a8ae885bbc4aad968e6587dca6e64f3a0900cdf",
24 "urls": ["https://github.com/bazelbuild/rules_cc/releases/download/0.0.9/rules_cc-0.0.9.tar.gz"],
25 "strip_prefix": "rules_cc-0.0.9",
26 },
27 "rules_java": {
iancha1992d51f9482024-02-14 00:47:49 -080028 "archive": "rules_java-7.4.0.tar.gz",
29 "sha256": "976ef08b49c929741f201790e59e3807c72ad81f428c8bc953cdbeff5fed15eb",
30 "urls": ["https://github.com/bazelbuild/rules_java/releases/download/7.4.0/rules_java-7.4.0.tar.gz"],
Yun Pengc1f2aff2023-11-07 09:51:02 -080031 },
32 # Used in src/test/java/com/google/devtools/build/lib/blackbox/framework/blackbox.WORKSAPCE
33 "rules_proto": {
34 "archive": "5.3.0-21.7.tar.gz",
35 "sha256": "dc3fb206a2cb3441b485eb1e423165b231235a1ea9b031b4433cf7bc1fa460dd",
36 "strip_prefix": "rules_proto-5.3.0-21.7",
37 "urls": ["https://github.com/bazelbuild/rules_proto/archive/refs/tags/5.3.0-21.7.tar.gz"],
38 },
39 "bazel_skylib": {
Fabian Meumertzheim5f796842023-11-14 21:07:28 -080040 "archive": "bazel-skylib-1.5.0.tar.gz",
41 "sha256": "cd55a062e763b9349921f0f5db8c3933288dc8ba4f76dd9416aac68acee3cb94",
42 "urls": ["https://github.com/bazelbuild/bazel-skylib/releases/download/1.5.0/bazel-skylib-1.5.0.tar.gz"],
Yun Pengc1f2aff2023-11-07 09:51:02 -080043 },
44 "rules_license": {
45 "archive": "rules_license-0.0.7.tar.gz",
46 "sha256": "4531deccb913639c30e5c7512a054d5d875698daeb75d8cf90f284375fe7c360",
47 "urls": ["https://github.com/bazelbuild/rules_license/releases/download/0.0.7/rules_license-0.0.7.tar.gz"],
48 },
49 "rules_python": {
50 "archive": "rules_python-0.24.0.tar.gz",
51 "sha256": "0a8003b044294d7840ac7d9d73eef05d6ceb682d7516781a4ec62eeb34702578",
52 "strip_prefix": "rules_python-0.24.0",
53 "urls": ["https://github.com/bazelbuild/rules_python/releases/download/0.24.0/rules_python-0.24.0.tar.gz"],
54 },
55 "rules_pkg": {
56 "archive": "rules_pkg-0.9.1.tar.gz",
57 "sha256": "8f9ee2dc10c1ae514ee599a8b42ed99fa262b757058f65ad3c384289ff70c4b8",
58 "urls": ["https://github.com/bazelbuild/rules_pkg/releases/download/0.9.1/rules_pkg-0.9.1.tar.gz"],
59 },
60 "rules_testing": {
61 "archive": "rules_testing-v0.0.4.tar.gz",
62 "sha256": "4e21f9aa7996944ce91431f27bca374bff56e680acfe497276074d56bc5d9af2",
63 "strip_prefix": "rules_testing-0.0.4",
64 "urls": ["https://github.com/bazelbuild/rules_testing/releases/download/v0.0.4/rules_testing-v0.0.4.tar.gz"],
65 },
66 "remote_coverage_tools": {
67 "archive": "coverage_output_generator-v2.6.zip",
68 "sha256": "7006375f6756819b7013ca875eab70a541cf7d89142d9c511ed78ea4fefa38af",
69 "urls": ["https://mirror.bazel.build/bazel_coverage_output_generator/releases/coverage_output_generator-v2.6.zip"],
70 },
71}
72
73def _gen_workspace_stanza_impl(ctx):
74 if ctx.attr.template and (ctx.attr.preamble or ctx.attr.postamble):
75 fail("Can not use template with either preamble or postamble")
76 if ctx.attr.use_maybe and ctx.attr.repo_clause:
77 fail("Can not use use_maybe with repo_clause")
78
79 if ctx.attr.use_maybe:
80 repo_clause = """
81maybe(
82 http_archive,
83 name = "{repo}",
84 sha256 = "{sha256}",
85 strip_prefix = {strip_prefix},
86 urls = {urls},
87)
88"""
89 elif ctx.attr.repo_clause:
90 repo_clause = ctx.attr.repo_clause
91 else:
92 repo_clause = """
93http_archive(
94 name = "{repo}",
95 sha256 = "{sha256}",
96 strip_prefix = {strip_prefix},
97 urls = {urls},
98)
99"""
100
101 repo_stanzas = {}
102 for repo in ctx.attr.repos:
103 info = WORKSPACE_REPOS[repo]
104 strip_prefix = info.get("strip_prefix")
105 if strip_prefix:
106 strip_prefix = "\"%s\"" % strip_prefix
107 else:
108 strip_prefix = "None"
109
110 repo_stanzas["{%s}" % repo] = repo_clause.format(
111 repo = repo,
112 sha256 = str(info["sha256"]),
113 strip_prefix = strip_prefix,
114 urls = info["urls"],
115 )
116
117 if ctx.attr.template:
118 ctx.actions.expand_template(
119 output = ctx.outputs.out,
120 template = ctx.file.template,
121 substitutions = repo_stanzas,
122 )
123 else:
124 content = "\n".join([p.strip() for p in ctx.attr.preamble.strip().split("\n")])
125 content += "\n"
126 content += "".join(repo_stanzas.values())
127 content += "\n"
128 content += "\n".join([p.strip() for p in ctx.attr.postamble.strip().split("\n")])
129 content += "\n"
130 ctx.actions.write(ctx.outputs.out, content)
131
132 return [DefaultInfo(files = depset([ctx.outputs.out]))]
133
134gen_workspace_stanza = rule(
135 attrs = {
136 "repos": attr.string_list(doc = "Set of repos to include."),
137 "out": attr.output(mandatory = True),
138 "preamble": attr.string(doc = "Preamble."),
139 "postamble": attr.string(doc = "Set of rules to follow repos."),
140 "template": attr.label(
141 doc = "Template WORKSPACE file. May not be used with preamble or postamble." +
142 "Repo stanzas can be included using the syntax '{repo name}'.",
143 allow_single_file = True,
144 mandatory = False,
145 ),
146 "use_maybe": attr.bool(doc = "Use maybe() invocation instead of http_archive."),
147 "repo_clause": attr.string(doc = "Use a custom clause for each repository."),
148 },
149 doc = "Use specifications from WORKSPACE_REPOS to generate WORKSPACE http_archive stanzas or to" +
150 "drop them into a template.",
151 implementation = _gen_workspace_stanza_impl,
152)