Skip to content

Commit 6409819

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

File tree

4 files changed

+213
-78
lines changed

4 files changed

+213
-78
lines changed

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

Lines changed: 64 additions & 70 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,
98-
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],
93+
versions.use_repository(
94+
http_repository,
95+
name = "kotlinx_serialization_json_jvm",
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: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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 = ["{file_name}"],
30+
)
31+
"""
32+
33+
_HTTP_JAR_BUILD = """\
34+
load("@rules_java//java:java_import.bzl", "java_import")
35+
36+
%s
37+
38+
java_import(
39+
name = 'jar',
40+
jars = [":file"],
41+
)
42+
""" % _HTTP_FILE_BUILD
43+
44+
_HTTP_ARCHIVE_BUILD = """\
45+
package(default_visibility = ["//visibility:public"])
46+
47+
filegroup(
48+
name = "files",
49+
srcs = glob(["**/*", "*"]),
50+
)
51+
"""
52+
53+
def get_download_and_template(type, r_ctx):
54+
if type == "file":
55+
def download(*args, **kwargs):
56+
return r_ctx.download_and_extract(
57+
executable = ctx.attr.executable,
58+
*args,
59+
**kwargs
60+
)
61+
62+
return (_HTTP_FILE_BUILD, "file", download)
63+
if type == "jar":
64+
return (_HTTP_JAR_BUILD, "jar", r_ctx.download)
65+
66+
def download_and_extract(*args, **kwargs):
67+
return r_ctx.download_and_extract(
68+
strip_prefix = ctx.attr.strip_prefix,
69+
*args,
70+
**kwargs
71+
)
72+
73+
return (_HTTP_ARCHIVE_BUILD, "", download_and_extract)
74+
75+
def _http_repository(r_ctx):
76+
(download, default_path, default_template) = get_download_and_template(r_ctx.attr.type, r_ctx)
77+
download_info = download(
78+
r_ctx.attrs.urls,
79+
output = r_ctx.attr.path or default_path,
80+
sha256 = r_ctx.attr.sha256,
81+
canonical_id = " ".join(urls) + r_ctx.sha256,
82+
)
83+
if not r_ctx.path("WORKSPACE").exists():
84+
r_ctx.file("WORKSPACE", "workspace(name = \"{name}\")".format(name = r_ctx.name))
85+
if not r_ctx.path("BUILD").exists():
86+
r_ctx.file("BUILD", r_ctx.attr.build_file_contents or default_template.format(file_name = r_ctx.attr.path))
87+
88+
return r_ctx.repo_metadata(
89+
reproducible = ctx.attr.sha256 != "",
90+
attrs_for_reproducibility = {
91+
"integrity": download_info.integrity,
92+
} if ctx.attr.sha256 == "" else {},
93+
)
94+
95+
http_repository = repository_rule(
96+
implementation = _http_repository,
97+
attrs = {
98+
"type": attr.string(
99+
doc = "Type of remote dependency: file, archive, jar",
100+
values = ["file", "archive", "jar"],
101+
mandatory = True,
102+
),
103+
"path": attr.string(
104+
doc = "Path in the repository to place the file or uncompress the archive.",
105+
),
106+
"executable": attr.bool(
107+
doc = "Whether the file will be executable. Only applies to type=file.",
108+
default = False,
109+
),
110+
"strip_prefix": attr.string(doc = "Prefix to remove. Only applies to type=archive."),
111+
"build_file_contents": attr.string(
112+
doc = "Build file contents.",
113+
),
114+
"sha256": attr.string(
115+
doc = """The expected SHA-256 of the file downloaded.
116+
117+
This must match the SHA-256 of the file downloaded. _It is a security risk
118+
to omit the SHA-256 as remote files can change._ At best omitting this
119+
field will make your build non-hermetic. It is optional to make development
120+
easier but should be set before shipping.""",
121+
),
122+
"urls": attr.string_list(
123+
doc = "List of urls to download from.",
124+
mandatory = True,
125+
),
126+
},
127+
)

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ version = provider(
1111
)
1212

1313
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]
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+
urls = [
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+
urls = ["https://github.com/bazelbuild/rules_cc/releases/download/{version}/rules_cc-{version}.tar.gz"],
152+
sha256 = "bbf1ae2f83305b7053b11e4467d317a7ba3517a12cef608543c1b1c5bf48a4df",
153+
strip_prefix = "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)