Skip to content

Commit de726a1

Browse files
David FreeseUebelAndre
David Freese
andauthored
Create internal rust_binary rule instead of using transitions (bazelbuild#1187)
* Create internal rust_binary rule instead of using transitions This seems like a cleaner way to break the dependency that rust_binary has on the process wrapper. The common args are consumed and modified to set the process wrapper to None, which is detected and performs the appropriate switch. The transitions and settings can then be removed. * buildifier * Update BUILD.bazel * Update rust.bzl * Update rust/private/rustc.bzl Co-authored-by: UebelAndre <[email protected]>
1 parent 5e6ad9f commit de726a1

File tree

4 files changed

+33
-82
lines changed

4 files changed

+33
-82
lines changed

rust/private/rust.bzl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,33 @@ rust_binary = rule(
928928
"""),
929929
)
930930

931+
def _fake_out_process_wrapper(attrs):
932+
new_attr = dict(attrs.items())
933+
new_attr["_process_wrapper"] = attr.label(
934+
default = None,
935+
executable = True,
936+
allow_single_file = True,
937+
cfg = "exec",
938+
)
939+
return new_attr
940+
941+
# Provides an internal rust_binary to use that we can use to build the process
942+
# wrapper, this breaks the dependency of rust_binary on the process wrapper by
943+
# setting it to None, which the functions in rustc detect and build accordingly.
944+
rust_binary_without_process_wrapper = rule(
945+
implementation = _rust_binary_impl,
946+
provides = _common_providers,
947+
attrs = dict(_fake_out_process_wrapper(_common_attrs).items() + _rust_binary_attrs.items()),
948+
executable = True,
949+
fragments = ["cpp"],
950+
host_fragments = ["cpp"],
951+
toolchains = [
952+
str(Label("//rust:toolchain")),
953+
"@bazel_tools//tools/cpp:toolchain_type",
954+
],
955+
incompatible_use_toolchain_transition = True,
956+
)
957+
931958
rust_test = rule(
932959
implementation = _rust_test_impl,
933960
provides = _common_providers,

rust/private/rustc.bzl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,8 +922,7 @@ def rustc_compile_action(
922922
dsym_folder = ctx.actions.declare_directory(crate_info.output.basename + ".dSYM")
923923
action_outputs.append(dsym_folder)
924924

925-
# This uses startswith as on windows the basename will be process_wrapper_fake.exe.
926-
if not ctx.executable._process_wrapper.basename.startswith("process_wrapper_fake"):
925+
if ctx.executable._process_wrapper:
927926
# Run as normal
928927
ctx.actions.run(
929928
executable = ctx.executable._process_wrapper,

rust/private/transitions.bzl

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -64,48 +64,3 @@ with_import_macro_bootstrapping_mode = rule(
6464
),
6565
},
6666
)
67-
68-
def _without_process_wrapper_transition_impl(_settings, _attr):
69-
"""This transition allows rust_* rules to invoke rustc without process_wrapper."""
70-
return {
71-
"//rust/settings:use_process_wrapper": False,
72-
}
73-
74-
without_process_wrapper_transition = transition(
75-
implementation = _without_process_wrapper_transition_impl,
76-
inputs = [],
77-
outputs = ["//rust/settings:use_process_wrapper"],
78-
)
79-
80-
def _without_process_wrapper_impl(ctx):
81-
executable = ctx.executable.target
82-
link_name = ctx.label.name
83-
84-
# Append .exe if on windows
85-
if executable.extension:
86-
link_name = link_name + "." + executable.extension
87-
link = ctx.actions.declare_file(link_name)
88-
ctx.actions.symlink(
89-
output = link,
90-
target_file = executable,
91-
)
92-
return [
93-
DefaultInfo(
94-
executable = link,
95-
),
96-
]
97-
98-
without_process_wrapper = rule(
99-
implementation = _without_process_wrapper_impl,
100-
attrs = {
101-
"target": attr.label(
102-
cfg = without_process_wrapper_transition,
103-
allow_single_file = True,
104-
mandatory = True,
105-
executable = True,
106-
),
107-
"_allowlist_function_transition": attr.label(
108-
default = Label("//tools/allowlists/function_transition_allowlist"),
109-
),
110-
},
111-
)

util/process_wrapper/BUILD.bazel

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,15 @@
1-
load("@rules_cc//cc:defs.bzl", "cc_binary")
2-
load("//rust:defs.bzl", "rust_binary", "rust_test")
1+
load("//rust:defs.bzl", "rust_test")
32

43
# buildifier: disable=bzl-visibility
5-
load("//rust/private:transitions.bzl", "without_process_wrapper")
4+
load("//rust/private:rust.bzl", "rust_binary_without_process_wrapper")
65

7-
alias(
6+
rust_binary_without_process_wrapper(
87
name = "process_wrapper",
9-
actual = select({
10-
# This will never get used, it's only here to break the circular dependency to allow building process_wrapper
11-
":use_fake_process_wrapper": ":process_wrapper_fake",
12-
"//conditions:default": ":process_wrapper_impl",
13-
}),
14-
visibility = ["//visibility:public"],
15-
)
16-
17-
cc_binary(
18-
name = "process_wrapper_fake",
19-
srcs = ["fake.cc"],
20-
)
21-
22-
config_setting(
23-
name = "use_fake_process_wrapper",
24-
flag_values = {
25-
"//rust/settings:use_process_wrapper": "False",
26-
},
27-
)
28-
29-
# Changing the name of this rule requires a corresponding
30-
# change in //rust/private/rustc.bzl:925
31-
without_process_wrapper(
32-
name = "process_wrapper_impl",
33-
target = ":process_wrapper_bin",
34-
visibility = ["//visibility:public"],
35-
)
36-
37-
rust_binary(
38-
name = "process_wrapper_bin",
398
srcs = glob(["*.rs"]),
9+
visibility = ["//visibility:public"],
4010
)
4111

4212
rust_test(
4313
name = "process_wrapper_test",
44-
crate = ":process_wrapper_bin",
14+
crate = ":process_wrapper",
4515
)

0 commit comments

Comments
 (0)