Skip to content

Commit 8de46ac

Browse files
committed
some bootstrap stuff
1 parent cdaf15f commit 8de46ac

File tree

12 files changed

+140
-50
lines changed

12 files changed

+140
-50
lines changed

extensions/prost/private/prost.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def _compile_rust(
196196
extension = ".rmeta",
197197
)
198198
rmeta = ctx.actions.declare_file(rmeta_name)
199-
rustc_rmeta_output = generate_output_diagnostics(ctx, rmeta)
199+
rustc_rmeta_output = generate_output_diagnostics(ctx, toolchain, rmeta)
200200
metadata_supports_pipelining = can_use_metadata_for_pipelining(toolchain, "rlib")
201201

202202
providers = rustc_compile_action(

extensions/protobuf/proto.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, is_gr
211211
crate_name,
212212
output_hash,
213213
))
214-
rustc_rmeta_output = generate_output_diagnostics(ctx, rust_metadata)
214+
rustc_rmeta_output = generate_output_diagnostics(ctx, toolchain, rust_metadata)
215215
metadata_supports_pipelining = can_use_metadata_for_pipelining(toolchain, "rlib")
216216

217217
# Gather all dependencies for compilation

extensions/wasm_bindgen/private/wasm_bindgen_test.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def _rust_wasm_bindgen_test_impl(ctx):
123123
proc_macro_deps = depset(proc_macro_deps, transitive = [crate.proc_macro_deps]).to_list(),
124124
aliases = {},
125125
output = output,
126-
rustc_output = generate_output_diagnostics(ctx, output),
126+
rustc_output = generate_output_diagnostics(ctx, toolchain, output),
127127
edition = crate.edition,
128128
rustc_env = rustc_env,
129129
rustc_env_files = rustc_env_files,

rust/private/BUILD.bazel

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2+
load("@bazel_skylib//rules:common_settings.bzl", "bool_setting")
23
load("//rust/private:rust_analyzer.bzl", "rust_analyzer_detect_sysroot")
34
load("//rust/private:rustc.bzl", "is_proc_macro_dep", "is_proc_macro_dep_enabled")
45
load("//rust/private:stamp.bzl", "stamp_build_setting")
@@ -57,3 +58,26 @@ rust_analyzer_detect_sysroot(
5758
name = "rust_analyzer_detect_sysroot",
5859
visibility = ["//visibility:public"],
5960
)
61+
62+
# This setting lets us configure a bootstrap toolchain to build the process_wrapper
63+
# and a "full" toolchain (that uses process_wrapper) to build user code.
64+
bool_setting(
65+
name = "bootstrap_setting",
66+
build_setting_default = False,
67+
)
68+
69+
config_setting(
70+
name = "bootstrapped",
71+
flag_values = {
72+
":bootstrap_setting": "0",
73+
},
74+
visibility = ["//visibility:public"],
75+
)
76+
77+
config_setting(
78+
name = "bootstrapping",
79+
flag_values = {
80+
":bootstrap_setting": "1",
81+
},
82+
visibility = ["//visibility:public"],
83+
)

rust/private/repository_utils.bzl

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,39 @@ rust_toolchain(
348348
strip_level = {strip_level},
349349
tags = ["rust_version={version}"],
350350
)
351+
352+
rust_toolchain(
353+
name = "{toolchain_name}_bootstrap",
354+
bootstrapping = True,
355+
rust_doc = "//:rustdoc",
356+
rust_std = "//:rust_std-{target_triple}",
357+
rustc = "//:rustc",
358+
linker = {linker_label},
359+
linker_type = {linker_type},
360+
rustfmt = {rustfmt_label},
361+
cargo = "//:cargo",
362+
clippy_driver = "//:clippy_driver_bin",
363+
cargo_clippy = "//:cargo_clippy_bin",
364+
llvm_cov = {llvm_cov_label},
365+
llvm_profdata = {llvm_profdata_label},
366+
llvm_lib = {llvm_lib_label},
367+
rustc_lib = "//:rustc_lib",
368+
allocator_library = {allocator_library},
369+
global_allocator_library = {global_allocator_library},
370+
binary_ext = "{binary_ext}",
371+
staticlib_ext = "{staticlib_ext}",
372+
dylib_ext = "{dylib_ext}",
373+
stdlib_linkflags = [{stdlib_linkflags}],
374+
default_edition = "{default_edition}",
375+
exec_triple = "{exec_triple}",
376+
target_triple = "{target_triple}",
377+
visibility = ["//visibility:public"],
378+
extra_rustc_flags = {extra_rustc_flags},
379+
extra_exec_rustc_flags = {extra_exec_rustc_flags},
380+
opt_level = {opt_level},
381+
strip_level = {strip_level},
382+
tags = ["rust_version={version}"],
383+
)
351384
"""
352385

353386
def BUILD_for_rust_toolchain(
@@ -449,7 +482,20 @@ toolchain(
449482
target_compatible_with = {target_constraint_sets_serialized},
450483
toolchain = "{toolchain}",
451484
toolchain_type = "{toolchain_type}",
452-
{target_settings}
485+
target_settings = [
486+
"@rules_rust//rust/private:bootstrapped",{target_settings}
487+
],
488+
)
489+
490+
toolchain(
491+
name = "{name}_bootstrap",
492+
exec_compatible_with = {exec_constraint_sets_serialized},
493+
target_compatible_with = {target_constraint_sets_serialized},
494+
toolchain = "{toolchain}_bootstrap",
495+
toolchain_type = "{toolchain_type}",
496+
target_settings = [
497+
"@rules_rust//rust/private:bootstrapping",{target_settings}
498+
],
453499
)
454500
"""
455501

@@ -460,7 +506,7 @@ def BUILD_for_toolchain(
460506
target_settings,
461507
target_compatible_with,
462508
exec_compatible_with):
463-
target_settings_value = "target_settings = {},".format(json.encode(target_settings)) if target_settings else "# target_settings = []"
509+
target_settings_value = ",\n ".join([repr(setting) for setting in target_settings])
464510

465511
return _build_file_for_toolchain_template.format(
466512
name = name,

rust/private/rust.bzl

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def _rust_library_common(ctx, crate_type):
209209
paths.replace_extension(rust_lib_name, ".rmeta"),
210210
sibling = rust_lib,
211211
)
212-
rustc_rmeta_output = generate_output_diagnostics(ctx, rust_metadata)
212+
rustc_rmeta_output = generate_output_diagnostics(ctx, toolchain, rust_metadata)
213213
metadata_supports_pipelining = (
214214
can_use_metadata_for_pipelining(toolchain, crate_type) and
215215
not ctx.attr.disable_pipelining
@@ -232,7 +232,7 @@ def _rust_library_common(ctx, crate_type):
232232
proc_macro_deps = proc_macro_deps,
233233
aliases = ctx.attr.aliases,
234234
output = rust_lib,
235-
rustc_output = generate_output_diagnostics(ctx, rust_lib),
235+
rustc_output = generate_output_diagnostics(ctx, toolchain, rust_lib),
236236
metadata = rust_metadata,
237237
metadata_supports_pipelining = metadata_supports_pipelining,
238238
rustc_rmeta_output = rustc_rmeta_output,
@@ -282,7 +282,7 @@ def _rust_binary_impl(ctx):
282282
paths.replace_extension("lib" + crate_name, ".rmeta"),
283283
sibling = output,
284284
)
285-
rustc_rmeta_output = generate_output_diagnostics(ctx, rust_metadata)
285+
rustc_rmeta_output = generate_output_diagnostics(ctx, toolchain, rust_metadata)
286286

287287
providers = rustc_compile_action(
288288
ctx = ctx,
@@ -297,7 +297,7 @@ def _rust_binary_impl(ctx):
297297
proc_macro_deps = proc_macro_deps,
298298
aliases = ctx.attr.aliases,
299299
output = output,
300-
rustc_output = generate_output_diagnostics(ctx, output),
300+
rustc_output = generate_output_diagnostics(ctx, toolchain, output),
301301
metadata = rust_metadata,
302302
rustc_rmeta_output = rustc_rmeta_output,
303303
edition = get_edition(ctx.attr, toolchain, ctx.label),
@@ -394,7 +394,7 @@ def _rust_test_impl(ctx):
394394
paths.replace_extension("lib" + crate_name, ".rmeta"),
395395
sibling = output,
396396
)
397-
rustc_rmeta_output = generate_output_diagnostics(ctx, rust_metadata)
397+
rustc_rmeta_output = generate_output_diagnostics(ctx, toolchain, rust_metadata)
398398

399399
# Need to consider all src files together when transforming
400400
srcs = depset(ctx.files.srcs, transitive = [crate.srcs]).to_list()
@@ -429,7 +429,7 @@ def _rust_test_impl(ctx):
429429
proc_macro_deps = depset(proc_macro_deps, transitive = [crate.proc_macro_deps]).to_list(),
430430
aliases = aliases,
431431
output = output,
432-
rustc_output = generate_output_diagnostics(ctx, output),
432+
rustc_output = generate_output_diagnostics(ctx, toolchain, output),
433433
metadata = rust_metadata,
434434
rustc_rmeta_output = rustc_rmeta_output,
435435
edition = crate.edition,
@@ -473,7 +473,7 @@ def _rust_test_impl(ctx):
473473
paths.replace_extension("lib" + crate_name, ".rmeta"),
474474
sibling = output,
475475
)
476-
rustc_rmeta_output = generate_output_diagnostics(ctx, rust_metadata)
476+
rustc_rmeta_output = generate_output_diagnostics(ctx, toolchain, rust_metadata)
477477

478478
if ctx.attr.rustc_env:
479479
rustc_env = expand_dict_value_locations(
@@ -495,7 +495,7 @@ def _rust_test_impl(ctx):
495495
proc_macro_deps = proc_macro_deps,
496496
aliases = ctx.attr.aliases,
497497
output = output,
498-
rustc_output = generate_output_diagnostics(ctx, output),
498+
rustc_output = generate_output_diagnostics(ctx, toolchain, output),
499499
metadata = rust_metadata,
500500
rustc_rmeta_output = rustc_rmeta_output,
501501
edition = get_edition(ctx.attr, toolchain, ctx.label),
@@ -645,13 +645,6 @@ RUSTC_ATTRS = {
645645
"_per_crate_rustc_flag": attr.label(
646646
default = Label("//rust/settings:experimental_per_crate_rustc_flag"),
647647
),
648-
"_process_wrapper": attr.label(
649-
doc = "A process wrapper for running rustc on all platforms.",
650-
default = Label("//util/process_wrapper"),
651-
executable = True,
652-
allow_single_file = True,
653-
cfg = "exec",
654-
),
655648
"_rustc_output_diagnostics": attr.label(
656649
default = Label("//rust/settings:rustc_output_diagnostics"),
657650
),
@@ -1318,21 +1311,6 @@ rust_binary = rule(
13181311
def _common_attrs_for_binary_without_process_wrapper(attrs):
13191312
new_attr = dict(attrs)
13201313

1321-
# use a fake process wrapper
1322-
new_attr["_process_wrapper"] = attr.label(
1323-
default = None,
1324-
executable = True,
1325-
allow_single_file = True,
1326-
cfg = "exec",
1327-
)
1328-
1329-
new_attr["_bootstrap_process_wrapper"] = attr.label(
1330-
default = Label("//util/process_wrapper:bootstrap_process_wrapper"),
1331-
executable = True,
1332-
allow_single_file = True,
1333-
cfg = "exec",
1334-
)
1335-
13361314
# fix stamp = 0
13371315
new_attr["stamp"] = attr.int(
13381316
doc = dedent("""\
@@ -1350,20 +1328,31 @@ _RustBuiltWithoutProcessWrapperInfo = provider(
13501328
fields = {},
13511329
)
13521330

1331+
def _bootstrap_process_wrapper_transition_impl(_settings, _attr):
1332+
return {str(Label("//rust/private:bootstrap_setting")): True}
1333+
1334+
_bootstrap_process_wrapper_transition = transition(
1335+
implementation = _bootstrap_process_wrapper_transition_impl,
1336+
inputs = [],
1337+
outputs = [str(Label("//rust/private:bootstrap_setting"))],
1338+
)
1339+
13531340
def _rust_binary_without_process_wrapper_impl(ctx):
13541341
providers = _rust_binary_impl(ctx)
13551342
return providers + [_RustBuiltWithoutProcessWrapperInfo()]
13561343

1357-
# Provides an internal rust_{binary,library} to use that we can use to build the process
1358-
# wrapper, this breaks the dependency of rust_* on the process wrapper by
1359-
# setting it to None, which the functions in rustc detect and build accordingly.
13601344
rust_binary_without_process_wrapper = rule(
13611345
implementation = _rust_binary_without_process_wrapper_impl,
13621346
doc = "A variant of `rust_binary` that uses a minimal process wrapper for `Rustc` actions.",
13631347
provides = COMMON_PROVIDERS + [_RustBuiltWithoutProcessWrapperInfo],
1364-
attrs = _common_attrs_for_binary_without_process_wrapper(_common_attrs | _rust_binary_attrs),
1348+
attrs = _common_attrs_for_binary_without_process_wrapper(_common_attrs | _rust_binary_attrs) | {
1349+
"_allowlist_function_transition": attr.label(
1350+
default = Label("//tools/allowlists/function_transition_allowlist"),
1351+
),
1352+
},
13651353
executable = True,
13661354
fragments = ["cpp"],
1355+
cfg = _bootstrap_process_wrapper_transition,
13671356
toolchains = [
13681357
str(Label("//rust:toolchain_type")),
13691358
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),

rust/private/rustc.bzl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,10 +1502,12 @@ def rustc_compile_action(
15021502
dsym_folder = ctx.actions.declare_directory(crate_info.output.basename + ".dSYM", sibling = crate_info.output)
15031503
action_outputs.append(dsym_folder)
15041504

1505-
if ctx.executable._process_wrapper:
1505+
process_wrapper = toolchain.process_wrapper
1506+
1507+
if process_wrapper:
15061508
# Run as normal
15071509
ctx.actions.run(
1508-
executable = ctx.executable._process_wrapper,
1510+
executable = process_wrapper,
15091511
inputs = compile_inputs,
15101512
outputs = action_outputs,
15111513
env = env,
@@ -1523,7 +1525,7 @@ def rustc_compile_action(
15231525
)
15241526
if args_metadata:
15251527
ctx.actions.run(
1526-
executable = ctx.executable._process_wrapper,
1528+
executable = process_wrapper,
15271529
inputs = compile_inputs,
15281530
outputs = [build_metadata] + [x for x in [rustc_rmeta_output] if x],
15291531
env = env,
@@ -1538,12 +1540,12 @@ def rustc_compile_action(
15381540
),
15391541
toolchain = "@rules_rust//rust:toolchain_type",
15401542
)
1541-
elif hasattr(ctx.executable, "_bootstrap_process_wrapper"):
1543+
else:
15421544
# Run without process_wrapper
15431545
if build_env_files or build_flags_files or stamp or build_metadata:
15441546
fail("build_env_files, build_flags_files, stamp, build_metadata are not supported when building without process_wrapper")
15451547
ctx.actions.run(
1546-
executable = ctx.executable._bootstrap_process_wrapper,
1548+
executable = toolchain.bootstrap_process_wrapper,
15471549
inputs = compile_inputs,
15481550
outputs = action_outputs,
15491551
env = env,
@@ -1559,8 +1561,6 @@ def rustc_compile_action(
15591561
toolchain = "@rules_rust//rust:toolchain_type",
15601562
resource_set = get_rustc_resource_set(toolchain),
15611563
)
1562-
else:
1563-
fail("No process wrapper was defined for {}".format(ctx.label))
15641564

15651565
if experimental_use_cc_common_link:
15661566
# Wrap the main `.o` file into a compilation output suitable for

rust/private/unpretty.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def _rust_unpretty_aspect_impl(target, ctx):
213213
args.rustc_flags.add("-Zunpretty={}".format(mode))
214214

215215
ctx.actions.run(
216-
executable = ctx.executable._process_wrapper,
216+
executable = toolchain.process_wrapper,
217217
inputs = compile_inputs,
218218
outputs = [unpretty_out],
219219
env = env,

rust/private/utils.bzl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ def can_build_metadata(toolchain, ctx, crate_type, *, disable_pipelining = False
736736
# 2) either:
737737
# * always_enable_metadata_output_groups is set
738738
# * this target can use metadata for pipelined compilation
739-
return bool(ctx.attr._process_wrapper) and (
739+
return bool(toolchain.process_wrapper) and (
740740
ctx.attr._always_enable_metadata_output_groups[AlwaysEnableMetadataOutputGroupsInfo].always_enable_metadata_output_groups or
741741
(not disable_pipelining and
742742
can_use_metadata_for_pipelining(toolchain, crate_type))
@@ -943,11 +943,12 @@ def _symlink_for_non_generated_source(ctx, src_file, package_root):
943943
else:
944944
return src_file
945945

946-
def generate_output_diagnostics(ctx, sibling, require_process_wrapper = True):
946+
def generate_output_diagnostics(ctx, toolchain, sibling, require_process_wrapper = True):
947947
"""Generates a .rustc-output file if it's required.
948948
949949
Args:
950950
ctx: (ctx): The current rule's context object
951+
toolchain: (Rust toolchain): The current rust toolchain
951952
sibling: (File): The file to generate the diagnostics for.
952953
require_process_wrapper: (bool): Whether to require the process wrapper
953954
in order to generate the .rustc-output file.
@@ -958,7 +959,7 @@ def generate_output_diagnostics(ctx, sibling, require_process_wrapper = True):
958959
# Since this feature requires error_format=json, we usually need
959960
# process_wrapper, since it can write the json here, then convert it to the
960961
# regular error format so the user can see the error properly.
961-
if require_process_wrapper and not ctx.attr._process_wrapper:
962+
if require_process_wrapper and not toolchain.process_wrapper:
962963
return None
963964
provider = ctx.attr._rustc_output_diagnostics[RustcOutputDiagnosticsInfo]
964965
if not provider.rustc_output_diagnostics:

0 commit comments

Comments
 (0)