Skip to content

Commit 779dc5b

Browse files
committed
Stop using bazel'http_* rules
1 parent e388a10 commit 779dc5b

File tree

5 files changed

+223
-78
lines changed

5 files changed

+223
-78
lines changed

src/main/starlark/core/repositories/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ release_archive(
2323
"bzlmod_impl.bzl",
2424
"compiler.bzl",
2525
"ksp.bzl",
26+
"utils.bzl",
2627
"versions.bzl",
2728
],
2829
src_map = {

src/main/starlark/core/repositories/initialize.release.bzl

Lines changed: 63 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,17 @@
1414
"""This file contains the Kotlin compiler repository definitions. It should not be loaded directly by client workspaces.
1515
"""
1616

17-
load(
18-
"@bazel_tools//tools/build_defs/repo:http.bzl",
19-
"http_archive",
20-
"http_file",
21-
"http_jar",
22-
)
23-
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
2417
load(
2518
"//kotlin/internal:defs.bzl",
2619
_KSP_COMPILER_PLUGIN_REPO = "KSP_COMPILER_PLUGIN_REPO",
2720
_KT_COMPILER_REPO = "KT_COMPILER_REPO",
2821
)
2922
load(":compiler.bzl", "kotlin_compiler_repository")
3023
load(":ksp.bzl", "ksp_compiler_plugin_repository")
24+
load(
25+
":utils.bzl",
26+
"http_repository",
27+
)
3128
load(":versions.bzl", "version", _versions = "versions")
3229

3330
versions = _versions
@@ -64,107 +61,104 @@ def kotlin_repositories(
6461
strip_version = ksp_compiler_release.version,
6562
)
6663

67-
maybe(
68-
http_file,
64+
versions.use_repository(
65+
http_repository,
6966
name = "com_github_pinterest_ktlint",
70-
sha256 = versions.PINTEREST_KTLINT.sha256,
71-
urls = [url.format(version = versions.PINTEREST_KTLINT.version) for url in versions.PINTEREST_KTLINT.url_templates],
67+
version = versions.PINTEREST_KTLINT,
68+
type = "file",
7269
executable = True,
7370
)
7471

75-
maybe(
76-
http_jar,
72+
versions.use_repository(
73+
http_repository,
7774
name = "kotlinx_serialization_core_jvm",
78-
sha256 = versions.KOTLINX_SERIALIZATION_CORE_JVM.sha256,
79-
urls = [url.format(version = versions.KOTLINX_SERIALIZATION_CORE_JVM.version) for url in versions.KOTLINX_SERIALIZATION_CORE_JVM.url_templates],
75+
version = versions.KOTLINX_SERIALIZATION_CORE_JVM,
76+
type = "jar",
8077
)
8178

82-
maybe(
83-
http_jar,
79+
versions.use_repository(
80+
http_repository,
8481
name = "kotlinx_serialization_json",
85-
sha256 = versions.KOTLINX_SERIALIZATION_JSON.sha256,
86-
urls = [url.format(version = versions.KOTLINX_SERIALIZATION_JSON.version) for url in versions.KOTLINX_SERIALIZATION_JSON.url_templates],
82+
version = versions.KOTLINX_SERIALIZATION_JSON,
83+
type = "jar",
8784
)
8885

89-
maybe(
90-
http_jar,
86+
versions.use_repository(
87+
http_repository,
9188
name = "kotlinx_serialization_json_jvm",
92-
sha256 = versions.KOTLINX_SERIALIZATION_JSON_JVM.sha256,
93-
urls = [url.format(version = versions.KOTLINX_SERIALIZATION_JSON_JVM.version) for url in versions.KOTLINX_SERIALIZATION_JSON_JVM.url_templates],
89+
version = versions.KOTLINX_SERIALIZATION_JSON_JVM,
90+
type = "jar",
9491
)
9592

96-
maybe(
97-
http_jar,
93+
versions.use_repository(
94+
http_repository,
9895
name = "kotlin_build_tools_impl",
99-
sha256 = versions.KOTLIN_BUILD_TOOLS_IMPL.sha256,
100-
urls = [url.format(version = versions.KOTLIN_BUILD_TOOLS_IMPL.version) for url in versions.KOTLIN_BUILD_TOOLS_IMPL.url_templates],
96+
version = versions.KOTLIN_BUILD_TOOLS_IMPL,
97+
type = "jar",
10198
)
10299

103100
if is_bzlmod:
104101
return
105102

106-
maybe(
107-
http_archive,
103+
versions.use_repository(
104+
http_repository,
108105
name = "py_absl",
109-
sha256 = "8a3d0830e4eb4f66c4fa907c06edf6ce1c719ced811a12e26d9d3162f8471758",
110-
urls = [
111-
"https://github.com/abseil/abseil-py/archive/refs/tags/v2.1.0.tar.gz",
112-
],
113-
strip_prefix = "abseil-py-2.1.0",
106+
type = "archive",
107+
version = versions.PY_ABSL,
114108
)
115109

116-
maybe(
117-
http_archive,
118-
name = "rules_cc",
119-
urls = ["https://github.com/bazelbuild/rules_cc/releases/download/0.0.16/rules_cc-0.0.16.tar.gz"],
120-
sha256 = "bbf1ae2f83305b7053b11e4467d317a7ba3517a12cef608543c1b1c5bf48a4df",
121-
strip_prefix = "rules_cc-0.0.16",
110+
versions.use_repository(
111+
http_repository,
112+
name = "py_absl",
113+
type = "archive",
114+
version = versions.PY_ABSL,
122115
)
123116

124-
maybe(
125-
http_archive,
117+
versions.use_repository(
118+
http_repository,
119+
name = "rules_cc",
120+
type = "archive",
121+
version = versions.RULES_CC,
122+
)
123+
versions.use_repository(
124+
http_repository,
126125
name = "rules_license",
127-
sha256 = versions.RULES_LICENSE.sha256,
128-
urls = [url.format(version = versions.RULES_LICENSE.version) for url in versions.RULES_LICENSE.url_templates],
126+
type = "archive",
127+
version = versions.RULES_LICENSE,
129128
)
130-
131-
maybe(
132-
http_archive,
129+
versions.use_repository(
130+
http_repository,
133131
name = "rules_android",
134-
sha256 = versions.RULES_ANDROID.sha256,
135-
strip_prefix = versions.RULES_ANDROID.strip_prefix_template.format(version = versions.RULES_ANDROID.version),
136-
urls = [url.format(version = versions.RULES_ANDROID.version) for url in versions.RULES_ANDROID.url_templates],
132+
type = "archive",
133+
version = versions.RULES_ANDROID,
137134
)
138135

139-
maybe(
140-
http_archive,
136+
versions.use_repository(
137+
http_repository,
141138
name = "rules_java",
142-
sha256 = versions.RULES_JAVA.sha256,
143-
urls = [url.format(version = versions.RULES_JAVA.version) for url in versions.RULES_JAVA.url_templates],
139+
type = "archive",
140+
version = versions.RULES_JAVA,
144141
)
145142

146143
# See note in versions.bzl before updating bazel_skylib
147-
maybe(
148-
http_archive,
144+
versions.use_repository(
145+
http_repository,
149146
name = "bazel_skylib",
150-
sha256 = versions.BAZEL_SKYLIB.sha256,
151-
urls = [url.format(version = versions.BAZEL_SKYLIB.version) for url in versions.BAZEL_SKYLIB.url_templates],
147+
type = "archive",
148+
version = versions.BAZEL_SKYLIB,
152149
)
153150

154-
maybe(
155-
http_archive,
151+
versions.use_repository(
152+
http_repository,
156153
name = "com_google_protobuf",
157-
sha256 = versions.COM_GOOGLE_PROTOBUF.sha256,
158-
strip_prefix = versions.COM_GOOGLE_PROTOBUF.strip_prefix_template.format(version = versions.COM_GOOGLE_PROTOBUF.version),
159-
urls = [url.format(version = versions.COM_GOOGLE_PROTOBUF.version) for url in versions.COM_GOOGLE_PROTOBUF.url_templates],
154+
type = "archive",
155+
version = versions.COM_GOOGLE_PROTOBUF,
160156
)
161-
162-
maybe(
163-
http_archive,
157+
versions.use_repository(
158+
http_repository,
164159
name = "rules_proto",
165-
sha256 = versions.RULES_PROTO.sha256,
166-
strip_prefix = versions.RULES_PROTO.strip_prefix_template.format(version = versions.RULES_PROTO.version),
167-
urls = [url.format(version = versions.RULES_PROTO.version) for url in versions.RULES_PROTO.url_templates],
160+
type = "archive",
161+
version = versions.RULES_PROTO,
168162
)
169163

170164
def kotlinc_version(release, sha256):
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
"""Local replacement for @bazel_tools//tools/build_defs/repo
2+
3+
Those tools have been deprecated in favor of language specific implementations. Given the current state of
4+
the documentation, it's easier to re-implement here.
5+
"""
6+
7+
def maybe(repo_rule, name, **kwargs):
8+
"""Utility function for only adding a repository if it's not already present.
9+
10+
This is to implement safe repositories.bzl macro documented in
11+
https://bazel.build/rules/deploying#dependencies.
12+
13+
Args:
14+
repo_rule: repository rule function.
15+
name: name of the repository to create.
16+
**kwargs: remaining arguments that are passed to the repo_rule function.
17+
18+
Returns:
19+
Nothing, defines the repository when needed as a side-effect.
20+
"""
21+
if not native.existing_rule(name):
22+
repo_rule(name = name, **kwargs)
23+
24+
_HTTP_FILE_BUILD = """\
25+
package(default_visibility = ["//visibility:public"])
26+
27+
filegroup(
28+
name = "file",
29+
srcs = glob(["{file_name}"]),
30+
)
31+
"""
32+
33+
_HTTP_JAR_BUILD = """\
34+
load("@rules_java//java:java_import.bzl", "java_import")
35+
%s
36+
java_import(
37+
name = "jar",
38+
jars = [":file"],
39+
)
40+
""" % _HTTP_FILE_BUILD
41+
42+
_HTTP_ARCHIVE_BUILD = """\
43+
package(default_visibility = ["//visibility:public"])
44+
45+
filegroup(
46+
name = "files",
47+
srcs = glob(["**/*", "*"]),
48+
)
49+
"""
50+
51+
def _resolve_type(type, r_ctx):
52+
if type == "archive":
53+
def download_and_extract(*args, **kwargs):
54+
return r_ctx.download_and_extract(
55+
strip_prefix = r_ctx.attr.strip_prefix,
56+
*args,
57+
**kwargs
58+
)
59+
60+
return (_HTTP_ARCHIVE_BUILD, r_ctx.attr.path or "", download_and_extract)
61+
62+
if type == "jar":
63+
template = _HTTP_JAR_BUILD
64+
path = "%s/%s" % (type, r_ctx.attr.path or "downloaded.jar")
65+
else:
66+
template = _HTTP_FILE_BUILD
67+
path = "%s/%s" % (type, r_ctx.attr.path or "downloaded")
68+
69+
def download(*args, **kwargs):
70+
if r_ctx.attr.executable:
71+
kwargs["executable"] = r_ctx.attr.executable
72+
return r_ctx.download(*args, **kwargs)
73+
74+
return (template, path, download)
75+
76+
def _http_repository(r_ctx):
77+
(default_template, path, download) = _resolve_type(r_ctx.attr.type, r_ctx)
78+
download_info = download(
79+
r_ctx.attr.urls,
80+
output = path,
81+
sha256 = r_ctx.attr.sha256,
82+
canonical_id = " ".join(r_ctx.attr.urls) + r_ctx.attr.sha256,
83+
)
84+
85+
def write_if_absent(file, contents):
86+
file_path = r_ctx.path(path).dirname.get_child(file)
87+
if file_path.exists:
88+
return
89+
r_ctx.file(file_path, contents)
90+
91+
write_if_absent("WORKSPACE", "workspace(name = \"{name}\")".format(name = r_ctx.name))
92+
write_if_absent("BUILD", default_template.format(file_name = r_ctx.path(path).basename))
93+
94+
return r_ctx.repo_metadata(
95+
reproducible = r_ctx.attr.sha256 != "",
96+
attrs_for_reproducibility = {"integrity": download_info.integrity} if r_ctx.attr.sha256 == "" else {},
97+
) if hasattr(r_ctx, "repo_metadata") else {
98+
"name": r_ctx.attr.name,
99+
"type": r_ctx.attr.type,
100+
"urls": r_ctx.attr.urls,
101+
"sha256": download_info.sha256,
102+
}
103+
104+
http_repository = repository_rule(
105+
implementation = _http_repository,
106+
attrs = {
107+
"type": attr.string(
108+
doc = "Type of remote dependency: file, archive, jar",
109+
values = ["file", "archive", "jar"],
110+
mandatory = True,
111+
),
112+
"path": attr.string(
113+
doc = "Path in the repository to place the file or uncompress the archive.",
114+
),
115+
"executable": attr.bool(
116+
doc = "Whether the file will be executable. Only applies to type=file.",
117+
default = False,
118+
),
119+
"strip_prefix": attr.string(doc = "Prefix to remove. Only applies to type=archive."),
120+
"build_file_contents": attr.string(
121+
doc = "Build file contents.",
122+
),
123+
"sha256": attr.string(
124+
doc = """The expected SHA-256 of the file downloaded.
125+
126+
This must match the SHA-256 of the file downloaded. _It is a security risk
127+
to omit the SHA-256 as remote files can change._ At best omitting this
128+
field will make your build non-hermetic. It is optional to make development
129+
easier but should be set before shipping.""",
130+
),
131+
"urls": attr.string_list(
132+
doc = "List of urls to download from.",
133+
mandatory = True,
134+
),
135+
},
136+
)

src/main/starlark/core/repositories/versions.bzl

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ version = provider(
1010
},
1111
)
1212

13-
def _use_repository(name, version, rule, **kwargs):
14-
http_archive_arguments = dict(kwargs)
15-
http_archive_arguments["sha256"] = version.sha256
16-
http_archive_arguments["urls"] = [u.format(version = version.version) for u in version.url_templates]
13+
def _use_repository(rule, name, version, **kwargs):
14+
rule_arguments = dict(kwargs)
15+
rule_arguments["sha256"] = version.sha256
16+
rule_arguments["urls"] = [u.format(version = version.version) for u in version.url_templates]
1717
if (hasattr(version, "strip_prefix_template")):
18-
http_archive_arguments["strip_prefix"] = version.strip_prefix_template.format(version = version.version)
18+
rule_arguments["strip_prefix"] = version.strip_prefix_template.format(version = version.version)
1919

20-
maybe(rule, name = name, **http_archive_arguments)
20+
maybe(rule, name = name, **rule_arguments)
2121

2222
versions = struct(
2323
# IMPORTANT! rules_kotlin does not use the bazel_skylib unittest in production
@@ -138,5 +138,19 @@ versions = struct(
138138
],
139139
sha256 = "8769e5647557e3700919c32d508f5c5dad53c5d8234cd10846354fbcff14aa24",
140140
),
141+
PY_ABSL = version(
142+
version = "2.1.0",
143+
sha256 = "8a3d0830e4eb4f66c4fa907c06edf6ce1c719ced811a12e26d9d3162f8471758",
144+
url_templates = [
145+
"https://github.com/abseil/abseil-py/archive/refs/tags/v{version}.tar.gz",
146+
],
147+
strip_prefix_template = "abseil-py-{version}",
148+
),
149+
RULES_CC = version(
150+
version = "0.0.16",
151+
url_templates = ["https://github.com/bazelbuild/rules_cc/releases/download/{version}/rules_cc-{version}.tar.gz"],
152+
sha256 = "bbf1ae2f83305b7053b11e4467d317a7ba3517a12cef608543c1b1c5bf48a4df",
153+
strip_prefix_template = "rules_cc-{version}",
154+
),
141155
use_repository = _use_repository,
142156
)

src/main/starlark/release_archive/repository.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2-
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
1+
load("//src/main/starlark/core/repositories:utils.bzl", "http_repository", "maybe")
32

43
versions = struct(
54
RULES_KOTLIN = struct(
@@ -189,7 +188,8 @@ def archive_repository(
189188
source_repository_name = source_repository_name or ("%s_head" % name)
190189
if remote_source_archive:
191190
maybe(
192-
http_archive,
191+
http_repository,
192+
type = "archive",
193193
name = source_repository_name,
194194
sha256 = remote_source_archive.sha256,
195195
strip_prefix = remote_source_archive.prefix,

0 commit comments

Comments
 (0)