Skip to content

Commit d67732c

Browse files
committed
Stdlib source bootstrap
1 parent 067da48 commit d67732c

31 files changed

Lines changed: 1622 additions & 301 deletions

rs/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ bzl_library(
3636
visibility = ["//visibility:public"],
3737
deps = [
3838
"//rs/private:annotations",
39+
"//rs/private:archive_cargo_workspace_repository",
3940
"//rs/private:cargo_credentials",
41+
"//rs/private:cargo_workspace_crate",
4042
"//rs/private:cfg_parser",
4143
"//rs/private:crate_repository",
4244
"//rs/private:downloader",
@@ -53,7 +55,6 @@ bzl_library(
5355
"@aspect_tools_telemetry_report//:defs.bzl",
5456
"@bazel_lib//lib:repo_utils",
5557
"@bazel_skylib//lib:paths",
56-
"@rs_rust_host_tools//:defs.bzl",
5758
],
5859
)
5960

rs/extensions.bzl

Lines changed: 573 additions & 153 deletions
Large diffs are not rendered by default.

rs/platforms/config/declare_config_settings.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""Config settings for Rust target triples."""
22

3-
load("//rs/platforms:triples.bzl", "SUPPORTED_TARGET_TRIPLES", "triple_to_constraint_set")
3+
load("//rs/platforms:triples.bzl", "ALL_TARGET_TRIPLES", "triple_to_constraint_set")
44

5-
def declare_config_settings(targets = SUPPORTED_TARGET_TRIPLES):
5+
def declare_config_settings(targets = ALL_TARGET_TRIPLES):
66
for target_triple in targets:
77
native.config_setting(
88
name = target_triple,

rs/platforms/declare_platforms.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""Platform definitions for Rust target triples."""
22

3-
load("//rs/platforms:triples.bzl", "SUPPORTED_TARGET_TRIPLES", "triple_to_constraint_set")
3+
load("//rs/platforms:triples.bzl", "ALL_TARGET_TRIPLES", "triple_to_constraint_set")
44

5-
def declare_platforms(targets = SUPPORTED_TARGET_TRIPLES):
5+
def declare_platforms(targets = ALL_TARGET_TRIPLES):
66
for target_triple in targets:
77
native.platform(
88
name = target_triple,

rs/platforms/triples.bzl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,13 @@ SUPPORTED_TARGET_TRIPLES = [
143143
#"x86_64-unknown-redox", # ✓ Redox OS
144144
"x86_64-unknown-uefi", # ? 64-bit UEFI
145145

146-
# No Tier3 support.
146+
# Additional Tier 3 targets must opt into source stdlib builds.
147147
]
148+
149+
SOURCE_STDLIB_TARGET_TRIPLES = [
150+
"bpfeb-unknown-none",
151+
"bpfel-unknown-none",
152+
"wasm64-unknown-unknown",
153+
]
154+
155+
ALL_TARGET_TRIPLES = SUPPORTED_TARGET_TRIPLES + SOURCE_STDLIB_TARGET_TRIPLES

rs/private/BUILD.bazel

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
load("@bazel_lib//:bzl_library.bzl", "bzl_library")
22
load(":all_crate_deps_test.bzl", "all_crate_deps_tests")
3+
load(":build_settings.bzl", "bool_setting")
34
load(":cfg_parser_test.bzl", "cfg_parser_tests")
5+
load(":empty_proc_macro_deps.bzl", "declare_empty_proc_macro_deps")
46
load(":lint_flags_test.bzl", "lint_flags_tests")
57
load(":semver_test.bzl", "semver_select_tests")
68

9+
bool_setting(
10+
name = "source_stdlib_building",
11+
build_setting_default = False,
12+
visibility = ["//visibility:public"],
13+
)
14+
715
all_crate_deps_tests()
816

917
cfg_parser_tests()
@@ -12,6 +20,8 @@ lint_flags_tests()
1220

1321
semver_select_tests()
1422

23+
declare_empty_proc_macro_deps()
24+
1525
bzl_library(
1626
name = "all_crate_deps",
1727
srcs = ["all_crate_deps.bzl"],
@@ -29,6 +39,22 @@ bzl_library(
2939
],
3040
)
3141

42+
bzl_library(
43+
name = "build_settings",
44+
srcs = ["build_settings.bzl"],
45+
visibility = ["//rs:__subpackages__"],
46+
)
47+
48+
bzl_library(
49+
name = "cargo_workspace_crate",
50+
srcs = ["cargo_workspace_crate.bzl"],
51+
visibility = ["//rs:__subpackages__"],
52+
deps = [
53+
":all_crate_deps",
54+
"//rs:rust_crate",
55+
],
56+
)
57+
3258
bzl_library(
3359
name = "git_crate_metadata_repository",
3460
srcs = ["git_crate_metadata_repository.bzl"],
@@ -38,6 +64,12 @@ bzl_library(
3864
],
3965
)
4066

67+
bzl_library(
68+
name = "empty_proc_macro_deps",
69+
srcs = ["empty_proc_macro_deps.bzl"],
70+
visibility = ["//rs:__subpackages__"],
71+
)
72+
4173
bzl_library(
4274
name = "crate_repository",
4375
srcs = ["crate_repository.bzl"],
@@ -96,12 +128,52 @@ bzl_library(
96128
deps = ["@bazel_lib//lib:repo_utils"],
97129
)
98130

131+
bzl_library(
132+
name = "cargo_workspace_repository_utils",
133+
srcs = ["cargo_workspace_repository_utils.bzl"],
134+
visibility = ["//rs:__subpackages__"],
135+
deps = [
136+
":repository_utils",
137+
":toml2json",
138+
],
139+
)
140+
141+
bzl_library(
142+
name = "archive_cargo_workspace_repository",
143+
srcs = ["archive_cargo_workspace_repository.bzl"],
144+
visibility = ["//rs:__subpackages__"],
145+
deps = [
146+
":cargo_workspace_repository_utils",
147+
":toml2json",
148+
"@bazel_tools//tools/build_defs/repo:utils.bzl",
149+
],
150+
)
151+
152+
bzl_library(
153+
name = "source_stdlib",
154+
srcs = ["source_stdlib.bzl"],
155+
visibility = ["//rs:__subpackages__"],
156+
deps = ["@rules_rust//rust:defs.bzl"],
157+
)
158+
159+
bzl_library(
160+
name = "source_stdlib_repository",
161+
srcs = ["source_stdlib_repository.bzl"],
162+
visibility = ["//rs:__subpackages__"],
163+
)
164+
165+
bzl_library(
166+
name = "source_stdlib_config_repository",
167+
srcs = ["source_stdlib_config_repository.bzl"],
168+
visibility = ["//rs:__subpackages__"],
169+
)
170+
99171
bzl_library(
100172
name = "git_cargo_workspace_repository",
101173
srcs = ["git_cargo_workspace_repository.bzl"],
102174
visibility = ["//rs:__subpackages__"],
103175
deps = [
104-
":repository_utils",
176+
":cargo_workspace_repository_utils",
105177
":toml2json",
106178
"@bazel_tools//tools/build_defs/repo:git_worker.bzl",
107179
"@bazel_tools//tools/build_defs/repo:utils.bzl",
@@ -113,6 +185,7 @@ bzl_library(
113185
srcs = ["cargo_repository.bzl"],
114186
visibility = ["//rs:__subpackages__"],
115187
deps = [
188+
"//rs/private:rust_repository_utils",
116189
"@rules_rust//rust/platform:bzl_lib",
117190
"@rules_rust//rust/private:bzl_lib",
118191
],
@@ -203,16 +276,6 @@ bzl_library(
203276
],
204277
)
205278

206-
bzl_library(
207-
name = "symlink_utils",
208-
srcs = ["symlink_utils.bzl"],
209-
visibility = ["//rs:__subpackages__"],
210-
deps = [
211-
"@bazel_features//:features",
212-
"@bazel_lib//lib:paths",
213-
],
214-
)
215-
216279
bzl_library(
217280
name = "cargo_lints",
218281
srcs = ["cargo_lints.bzl"],

rs/private/annotations.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def _crate_annotation(
1212
build_script_tags = [],
1313
data = [],
1414
deps = [],
15+
extra_compile_data = [],
1516
tags = [],
1617
crate_features = [],
1718
crate_features_select = {},
@@ -38,6 +39,7 @@ def _crate_annotation(
3839
build_script_tags = build_script_tags,
3940
data = data,
4041
deps = deps,
42+
extra_compile_data = extra_compile_data,
4143
tags = tags,
4244
crate_features = crate_features,
4345
crate_features_select = crate_features_select,
@@ -54,6 +56,9 @@ def _crate_annotation(
5456

5557
_DEFAULT_CRATE_ANNOTATION = _crate_annotation()
5658

59+
def crate_annotation(**kwargs):
60+
return _crate_annotation(**kwargs)
61+
5762
_WINDOWS_GNULLVM_IMPLICIT_ANNOTATION = _crate_annotation(
5863
additive_build_file_content = """
5964
load("@rules_cc//cc:defs.bzl", "cc_import")
@@ -78,9 +83,12 @@ _IMPLICIT_ANNOTATIONS_BY_CRATE = {
7883
def annotation_for(annotations_by_crate, crate_name, version):
7984
"""Return the annotation matching crate/version, falling back to '*' or default."""
8085
version_map = annotations_by_crate.get(crate_name, {})
86+
wildcard_map = annotations_by_crate.get("*", {})
8187
return (
8288
version_map.get(version) or
8389
version_map.get("*") or
90+
wildcard_map.get(version) or
91+
wildcard_map.get("*") or
8492
_IMPLICIT_ANNOTATIONS_BY_CRATE.get(crate_name, _DEFAULT_CRATE_ANNOTATION)
8593
)
8694

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "get_auth")
2+
load(":cargo_workspace_repository_utils.bzl", "render_cargo_workspace_build_file")
3+
load(":toml2json.bzl", "run_toml2json")
4+
5+
_PACKAGE_SRCS_FILEGROUP = "_cargo_workspace_srcs"
6+
7+
def _render_package_srcs_filegroup():
8+
return """\
9+
10+
filegroup(
11+
name = "{name}",
12+
srcs = glob(
13+
["**"],
14+
exclude = [
15+
"BUILD",
16+
"BUILD.bazel",
17+
"REPO.bazel",
18+
"WORKSPACE",
19+
"WORKSPACE.bazel",
20+
],
21+
),
22+
visibility = ["//visibility:public"],
23+
)
24+
""".format(name = _PACKAGE_SRCS_FILEGROUP)
25+
26+
def _root_label(label):
27+
return label if label.startswith("//") else "//" + label
28+
29+
def _build_file_dest(package):
30+
return package + "/BUILD.bazel" if package else "BUILD.bazel"
31+
32+
def _filegroup_package_and_name(filegroup):
33+
parts = filegroup.split(":")
34+
if len(parts) != 2 or not parts[1]:
35+
fail("workspace filegroup must be formatted as 'package:name', got '{}'".format(filegroup))
36+
return parts
37+
38+
def _render_workspace_filegroups(filegroups):
39+
content = []
40+
for label, include in sorted(filegroups.items()):
41+
package, name = _filegroup_package_and_name(label)
42+
content.append((_build_file_dest(package), """\
43+
filegroup(
44+
name = {name},
45+
srcs = glob(
46+
[{include}],
47+
exclude = [
48+
"BUILD",
49+
"BUILD.bazel",
50+
],
51+
),
52+
visibility = ["//visibility:public"],
53+
)
54+
""".format(
55+
name = repr(name),
56+
include = repr(include),
57+
)))
58+
return content
59+
60+
def _render_root_filegroups(filegroups):
61+
content = []
62+
for name, label in sorted(filegroups.items()):
63+
content.append("""\
64+
filegroup(
65+
name = {name},
66+
srcs = [{label}],
67+
visibility = ["//visibility:public"],
68+
)
69+
""".format(
70+
name = repr(name),
71+
label = repr(_root_label(label)),
72+
))
73+
return "\n".join(content)
74+
75+
def _archive_cargo_workspace_repository_impl(rctx):
76+
rctx.download_and_extract(
77+
rctx.attr.urls,
78+
sha256 = rctx.attr.sha256,
79+
auth = get_auth(rctx, rctx.attr.urls),
80+
strip_prefix = rctx.attr.strip_prefix,
81+
)
82+
83+
workspace_filegroups = {}
84+
for dest, content in _render_workspace_filegroups(rctx.attr.filegroups):
85+
workspace_filegroups.setdefault(dest, "")
86+
workspace_filegroups[dest] += content
87+
root_filegroups = _render_root_filegroups(rctx.attr.root_filegroups)
88+
if root_filegroups:
89+
workspace_filegroups.setdefault("BUILD.bazel", "")
90+
workspace_filegroups["BUILD.bazel"] += root_filegroups
91+
92+
workspace_cargo_toml = run_toml2json(rctx, rctx.attr.workspace_cargo_toml)
93+
for dest, additive_build_file_content in rctx.attr.build_files.items():
94+
additive_build_file_content += workspace_filegroups.pop(dest, "")
95+
additive_build_file_content += _render_package_srcs_filegroup()
96+
render_cargo_workspace_build_file(
97+
rctx,
98+
dest,
99+
additive_build_file_content,
100+
rctx.attr.gen_binaries.get(dest, []),
101+
workspace_cargo_toml,
102+
crate_key = rctx.attr.crate_keys.get(dest),
103+
)
104+
105+
for dest, content in workspace_filegroups.items():
106+
rctx.file(dest, content)
107+
108+
return rctx.repo_metadata(reproducible = True)
109+
110+
archive_cargo_workspace_repository = repository_rule(
111+
implementation = _archive_cargo_workspace_repository_impl,
112+
attrs = {
113+
"urls": attr.string_list(mandatory = True),
114+
"sha256": attr.string(mandatory = True),
115+
"strip_prefix": attr.string(),
116+
"hub_name": attr.string(mandatory = True),
117+
"build_files": attr.string_dict(mandatory = True),
118+
"crate_keys": attr.string_dict(default = {}),
119+
"gen_binaries": attr.string_list_dict(default = {}),
120+
"filegroups": attr.string_dict(default = {}),
121+
"root_filegroups": attr.string_dict(default = {}),
122+
"workspace_cargo_toml": attr.string(default = "Cargo.toml"),
123+
},
124+
)

rs/private/build_settings.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def _bool_setting_impl(_ctx):
2+
return []
3+
4+
bool_setting = rule(
5+
implementation = _bool_setting_impl,
6+
build_setting = config.bool(flag = False),
7+
)

0 commit comments

Comments
 (0)