Skip to content

Commit 63fbedb

Browse files
Add extra_rustc_flags_for_crate_types. (bazelbuild#2431)
This allows extra rustc flags that will only apply to library targets to be set by the toolchain. Cross language LTO needs '-C linker-plugin-lto' on libraries, how passing this flag to rust_binary generated bloat. Adding this attribute resolves this issue. Change-Id: Iba725fab1b1941e9586ff97cd71ec3bc1dfc1523 --------- Co-authored-by: UebelAndre <[email protected]>
1 parent 0731206 commit 63fbedb

File tree

6 files changed

+83
-13
lines changed

6 files changed

+83
-13
lines changed

docs/flatten.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1178,9 +1178,9 @@ Run the test with `bazel test //hello_lib:greeting_test`.
11781178
rust_toolchain(<a href="#rust_toolchain-name">name</a>, <a href="#rust_toolchain-allocator_library">allocator_library</a>, <a href="#rust_toolchain-binary_ext">binary_ext</a>, <a href="#rust_toolchain-cargo">cargo</a>, <a href="#rust_toolchain-clippy_driver">clippy_driver</a>, <a href="#rust_toolchain-debug_info">debug_info</a>,
11791179
<a href="#rust_toolchain-default_edition">default_edition</a>, <a href="#rust_toolchain-dylib_ext">dylib_ext</a>, <a href="#rust_toolchain-env">env</a>, <a href="#rust_toolchain-exec_triple">exec_triple</a>, <a href="#rust_toolchain-experimental_link_std_dylib">experimental_link_std_dylib</a>,
11801180
<a href="#rust_toolchain-experimental_use_cc_common_link">experimental_use_cc_common_link</a>, <a href="#rust_toolchain-extra_exec_rustc_flags">extra_exec_rustc_flags</a>, <a href="#rust_toolchain-extra_rustc_flags">extra_rustc_flags</a>,
1181-
<a href="#rust_toolchain-global_allocator_library">global_allocator_library</a>, <a href="#rust_toolchain-llvm_cov">llvm_cov</a>, <a href="#rust_toolchain-llvm_profdata">llvm_profdata</a>, <a href="#rust_toolchain-llvm_tools">llvm_tools</a>, <a href="#rust_toolchain-opt_level">opt_level</a>,
1182-
<a href="#rust_toolchain-per_crate_rustc_flags">per_crate_rustc_flags</a>, <a href="#rust_toolchain-rust_doc">rust_doc</a>, <a href="#rust_toolchain-rust_std">rust_std</a>, <a href="#rust_toolchain-rustc">rustc</a>, <a href="#rust_toolchain-rustc_lib">rustc_lib</a>, <a href="#rust_toolchain-rustfmt">rustfmt</a>, <a href="#rust_toolchain-staticlib_ext">staticlib_ext</a>,
1183-
<a href="#rust_toolchain-stdlib_linkflags">stdlib_linkflags</a>, <a href="#rust_toolchain-target_json">target_json</a>, <a href="#rust_toolchain-target_triple">target_triple</a>)
1181+
<a href="#rust_toolchain-extra_rustc_flags_for_crate_types">extra_rustc_flags_for_crate_types</a>, <a href="#rust_toolchain-global_allocator_library">global_allocator_library</a>, <a href="#rust_toolchain-llvm_cov">llvm_cov</a>, <a href="#rust_toolchain-llvm_profdata">llvm_profdata</a>,
1182+
<a href="#rust_toolchain-llvm_tools">llvm_tools</a>, <a href="#rust_toolchain-opt_level">opt_level</a>, <a href="#rust_toolchain-per_crate_rustc_flags">per_crate_rustc_flags</a>, <a href="#rust_toolchain-rust_doc">rust_doc</a>, <a href="#rust_toolchain-rust_std">rust_std</a>, <a href="#rust_toolchain-rustc">rustc</a>, <a href="#rust_toolchain-rustc_lib">rustc_lib</a>,
1183+
<a href="#rust_toolchain-rustfmt">rustfmt</a>, <a href="#rust_toolchain-staticlib_ext">staticlib_ext</a>, <a href="#rust_toolchain-stdlib_linkflags">stdlib_linkflags</a>, <a href="#rust_toolchain-target_json">target_json</a>, <a href="#rust_toolchain-target_triple">target_triple</a>)
11841184
</pre>
11851185

11861186
Declares a Rust toolchain for use.
@@ -1246,6 +1246,7 @@ See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpu
12461246
| <a id="rust_toolchain-experimental_use_cc_common_link"></a>experimental_use_cc_common_link | Label to a boolean build setting that controls whether cc_common.link is used to link rust binaries. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>//rust/settings:experimental_use_cc_common_link</code> |
12471247
| <a id="rust_toolchain-extra_exec_rustc_flags"></a>extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration | List of strings | optional | <code>[]</code> |
12481248
| <a id="rust_toolchain-extra_rustc_flags"></a>extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | <code>[]</code> |
1249+
| <a id="rust_toolchain-extra_rustc_flags_for_crate_types"></a>extra_rustc_flags_for_crate_types | Extra flags to pass to rustc based on crate type | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> List of strings</a> | optional | <code>{}</code> |
12491250
| <a id="rust_toolchain-global_allocator_library"></a>global_allocator_library | Target that provides allocator functions for when a global allocator is present. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>@rules_rust//ffi/cc/global_allocator_library</code> |
12501251
| <a id="rust_toolchain-llvm_cov"></a>llvm_cov | The location of the <code>llvm-cov</code> binary. Can be a direct source or a filegroup containing one item. If None, rust code is not instrumented for coverage. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
12511252
| <a id="rust_toolchain-llvm_profdata"></a>llvm_profdata | The location of the <code>llvm-profdata</code> binary. Can be a direct source or a filegroup containing one item. If <code>llvm_cov</code> is None, this can be None as well and rust code is not instrumented for coverage. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |

docs/rust_repositories.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ A dedicated filegroup-like rule for Rust stdlib artifacts.
3939
rust_toolchain(<a href="#rust_toolchain-name">name</a>, <a href="#rust_toolchain-allocator_library">allocator_library</a>, <a href="#rust_toolchain-binary_ext">binary_ext</a>, <a href="#rust_toolchain-cargo">cargo</a>, <a href="#rust_toolchain-clippy_driver">clippy_driver</a>, <a href="#rust_toolchain-debug_info">debug_info</a>,
4040
<a href="#rust_toolchain-default_edition">default_edition</a>, <a href="#rust_toolchain-dylib_ext">dylib_ext</a>, <a href="#rust_toolchain-env">env</a>, <a href="#rust_toolchain-exec_triple">exec_triple</a>, <a href="#rust_toolchain-experimental_link_std_dylib">experimental_link_std_dylib</a>,
4141
<a href="#rust_toolchain-experimental_use_cc_common_link">experimental_use_cc_common_link</a>, <a href="#rust_toolchain-extra_exec_rustc_flags">extra_exec_rustc_flags</a>, <a href="#rust_toolchain-extra_rustc_flags">extra_rustc_flags</a>,
42-
<a href="#rust_toolchain-global_allocator_library">global_allocator_library</a>, <a href="#rust_toolchain-llvm_cov">llvm_cov</a>, <a href="#rust_toolchain-llvm_profdata">llvm_profdata</a>, <a href="#rust_toolchain-llvm_tools">llvm_tools</a>, <a href="#rust_toolchain-opt_level">opt_level</a>,
43-
<a href="#rust_toolchain-per_crate_rustc_flags">per_crate_rustc_flags</a>, <a href="#rust_toolchain-rust_doc">rust_doc</a>, <a href="#rust_toolchain-rust_std">rust_std</a>, <a href="#rust_toolchain-rustc">rustc</a>, <a href="#rust_toolchain-rustc_lib">rustc_lib</a>, <a href="#rust_toolchain-rustfmt">rustfmt</a>, <a href="#rust_toolchain-staticlib_ext">staticlib_ext</a>,
44-
<a href="#rust_toolchain-stdlib_linkflags">stdlib_linkflags</a>, <a href="#rust_toolchain-target_json">target_json</a>, <a href="#rust_toolchain-target_triple">target_triple</a>)
42+
<a href="#rust_toolchain-extra_rustc_flags_for_crate_types">extra_rustc_flags_for_crate_types</a>, <a href="#rust_toolchain-global_allocator_library">global_allocator_library</a>, <a href="#rust_toolchain-llvm_cov">llvm_cov</a>, <a href="#rust_toolchain-llvm_profdata">llvm_profdata</a>,
43+
<a href="#rust_toolchain-llvm_tools">llvm_tools</a>, <a href="#rust_toolchain-opt_level">opt_level</a>, <a href="#rust_toolchain-per_crate_rustc_flags">per_crate_rustc_flags</a>, <a href="#rust_toolchain-rust_doc">rust_doc</a>, <a href="#rust_toolchain-rust_std">rust_std</a>, <a href="#rust_toolchain-rustc">rustc</a>, <a href="#rust_toolchain-rustc_lib">rustc_lib</a>,
44+
<a href="#rust_toolchain-rustfmt">rustfmt</a>, <a href="#rust_toolchain-staticlib_ext">staticlib_ext</a>, <a href="#rust_toolchain-stdlib_linkflags">stdlib_linkflags</a>, <a href="#rust_toolchain-target_json">target_json</a>, <a href="#rust_toolchain-target_triple">target_triple</a>)
4545
</pre>
4646

4747
Declares a Rust toolchain for use.
@@ -107,6 +107,7 @@ See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpu
107107
| <a id="rust_toolchain-experimental_use_cc_common_link"></a>experimental_use_cc_common_link | Label to a boolean build setting that controls whether cc_common.link is used to link rust binaries. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>//rust/settings:experimental_use_cc_common_link</code> |
108108
| <a id="rust_toolchain-extra_exec_rustc_flags"></a>extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration | List of strings | optional | <code>[]</code> |
109109
| <a id="rust_toolchain-extra_rustc_flags"></a>extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | <code>[]</code> |
110+
| <a id="rust_toolchain-extra_rustc_flags_for_crate_types"></a>extra_rustc_flags_for_crate_types | Extra flags to pass to rustc based on crate type | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> List of strings</a> | optional | <code>{}</code> |
110111
| <a id="rust_toolchain-global_allocator_library"></a>global_allocator_library | Target that provides allocator functions for when a global allocator is present. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>@rules_rust//ffi/cc/global_allocator_library</code> |
111112
| <a id="rust_toolchain-llvm_cov"></a>llvm_cov | The location of the <code>llvm-cov</code> binary. Can be a direct source or a filegroup containing one item. If None, rust code is not instrumented for coverage. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
112113
| <a id="rust_toolchain-llvm_profdata"></a>llvm_profdata | The location of the <code>llvm-profdata</code> binary. Can be a direct source or a filegroup containing one item. If <code>llvm_cov</code> is None, this can be None as well and rust code is not instrumented for coverage. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |

rust/private/rustc.bzl

+3
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,9 @@ def construct_arguments(
10701070
if toolchain._rename_first_party_crates:
10711071
env["RULES_RUST_THIRD_PARTY_DIR"] = toolchain._third_party_dir
10721072

1073+
if crate_info.type in toolchain.extra_rustc_flags_for_crate_types.keys():
1074+
rustc_flags.add_all(toolchain.extra_rustc_flags_for_crate_types[crate_info.type])
1075+
10731076
if is_exec_configuration(ctx):
10741077
rustc_flags.add_all(toolchain.extra_exec_rustc_flags)
10751078
else:

rust/toolchain.bzl

+4
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ def _rust_toolchain_impl(ctx):
650650
staticlib_ext = ctx.attr.staticlib_ext,
651651
stdlib_linkflags = stdlib_linkflags_cc_info,
652652
extra_rustc_flags = ctx.attr.extra_rustc_flags,
653+
extra_rustc_flags_for_crate_types = ctx.attr.extra_rustc_flags_for_crate_types,
653654
extra_exec_rustc_flags = ctx.attr.extra_exec_rustc_flags,
654655
per_crate_rustc_flags = ctx.attr.per_crate_rustc_flags,
655656
sysroot = sysroot_path,
@@ -747,6 +748,9 @@ rust_toolchain = rule(
747748
"extra_rustc_flags": attr.string_list(
748749
doc = "Extra flags to pass to rustc in non-exec configuration",
749750
),
751+
"extra_rustc_flags_for_crate_types": attr.string_list_dict(
752+
doc = "Extra flags to pass to rustc based on crate type",
753+
),
750754
"global_allocator_library": attr.label(
751755
doc = "Target that provides allocator functions for when a global allocator is present.",
752756
default = "@rules_rust//ffi/cc/global_allocator_library",

test/toolchain/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}

test/toolchain/toolchain_test.bzl

+67-7
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
44
load("@bazel_skylib//rules:write_file.bzl", "write_file")
5-
load("//rust:defs.bzl", "rust_library")
5+
load("//rust:defs.bzl", "rust_library", "rust_shared_library")
66
load("//rust:toolchain.bzl", "rust_stdlib_filegroup", "rust_toolchain")
77

88
EXEC_TOOLCHAIN_FLAG = "missing"
99
TOOLCHAIN_FLAG = "before"
1010
CONFIG_FLAG = "after"
11+
CRATE_FLAGS = {"cdylib": ["cdylib_flag"], "rlib": ["rlib_flag"]}
1112

12-
def _toolchain_adds_rustc_flags_impl(ctx):
13+
def _toolchain_adds_rustc_flags_impl(ctx, crate_type):
1314
""" Tests adding extra_rustc_flags on the toolchain, asserts that:
1415
1516
- extra_rustc_flags added by the toolchain are applied BEFORE flags added by a config on the commandline
1617
- The exec flags from the toolchain don't go on the commandline for a non-exec target
18+
- crate type rustc flags are added
1719
"""
1820
env = analysistest.begin(ctx)
1921
target = analysistest.target_under_test(env)
@@ -30,6 +32,27 @@ def _toolchain_adds_rustc_flags_impl(ctx):
3032
),
3133
)
3234

35+
asserts.true(
36+
env,
37+
action.argv[-3] == CRATE_FLAGS[crate_type][0],
38+
"Unexpected rustc flags: {}\nShould have contained: {}".format(
39+
action.argv,
40+
CRATE_FLAGS["rlib"],
41+
),
42+
)
43+
44+
for type in CRATE_FLAGS.keys():
45+
if type == crate_type:
46+
continue
47+
asserts.false(
48+
env,
49+
CRATE_FLAGS[type][0] in action.argv,
50+
"Unexpected rustc flags: {}\nShould not contain: {}".format(
51+
action.argv,
52+
CRATE_FLAGS[type],
53+
),
54+
)
55+
3356
asserts.true(
3457
env,
3558
EXEC_TOOLCHAIN_FLAG not in action.argv,
@@ -48,8 +71,22 @@ def _toolchain_adds_rustc_flags_impl(ctx):
4871

4972
return analysistest.end(env)
5073

51-
toolchain_adds_rustc_flags_test = analysistest.make(
52-
_toolchain_adds_rustc_flags_impl,
74+
def _toolchain_adds_rustc_flags_lib_impl(ctx):
75+
return _toolchain_adds_rustc_flags_impl(ctx, "rlib")
76+
77+
def _toolchain_adds_rustc_flags_shared_lib_impl(ctx):
78+
return _toolchain_adds_rustc_flags_impl(ctx, "cdylib")
79+
80+
toolchain_adds_rustc_flags_lib_test = analysistest.make(
81+
_toolchain_adds_rustc_flags_lib_impl,
82+
config_settings = {
83+
str(Label("//:extra_rustc_flags")): [CONFIG_FLAG],
84+
str(Label("//rust/settings:experimental_toolchain_generated_sysroot")): True,
85+
},
86+
)
87+
88+
toolchain_adds_rustc_flags_shared_lib_test = analysistest.make(
89+
_toolchain_adds_rustc_flags_shared_lib_impl,
5390
config_settings = {
5491
str(Label("//:extra_rustc_flags")): [CONFIG_FLAG],
5592
str(Label("//rust/settings:experimental_toolchain_generated_sysroot")): True,
@@ -105,6 +142,12 @@ def _define_targets():
105142
edition = "2021",
106143
)
107144

145+
rust_shared_library(
146+
name = "shared_lib",
147+
srcs = ["lib.rs"],
148+
edition = "2021",
149+
)
150+
108151
native.filegroup(
109152
name = "stdlib_srcs",
110153
srcs = ["config.txt"],
@@ -139,6 +182,7 @@ def _define_targets():
139182
stdlib_linkflags = [],
140183
extra_rustc_flags = [TOOLCHAIN_FLAG],
141184
extra_exec_rustc_flags = [EXEC_TOOLCHAIN_FLAG],
185+
extra_rustc_flags_for_crate_types = CRATE_FLAGS,
142186
visibility = ["//visibility:public"],
143187
)
144188

@@ -153,6 +197,11 @@ def _define_targets():
153197
dep = ":lib",
154198
)
155199

200+
extra_toolchain_wrapper(
201+
name = "shared_lib_with_extra_toolchain",
202+
dep = ":shared_lib",
203+
)
204+
156205
def _rust_stdlib_filegroup_provides_runfiles_test_impl(ctx):
157206
env = analysistest.begin(ctx)
158207
target = analysistest.target_under_test(env)
@@ -166,13 +215,23 @@ rust_stdlib_filegroup_provides_runfiles_test = analysistest.make(
166215
)
167216

168217
def toolchain_test_suite(name):
218+
""" Instantiates tests for rust toolchains.
219+
220+
Args:
221+
name: a name for the test suite
222+
"""
169223
_define_targets()
170224

171-
toolchain_adds_rustc_flags_test(
172-
name = "toolchain_adds_rustc_flags_test",
225+
toolchain_adds_rustc_flags_lib_test(
226+
name = "toolchain_adds_rustc_flags_lib_test",
173227
target_under_test = ":lib_with_extra_toolchain",
174228
)
175229

230+
toolchain_adds_rustc_flags_shared_lib_test(
231+
name = "toolchain_adds_rustc_flags_shared_lib_test",
232+
target_under_test = ":shared_lib_with_extra_toolchain",
233+
)
234+
176235
rust_stdlib_filegroup_provides_runfiles_test(
177236
name = "rust_stdlib_filegroup_provides_runfiles_test",
178237
target_under_test = ":std_libs",
@@ -181,7 +240,8 @@ def toolchain_test_suite(name):
181240
native.test_suite(
182241
name = name,
183242
tests = [
184-
":toolchain_adds_rustc_flags_test",
243+
":toolchain_adds_rustc_flags_lib_test",
244+
":toolchain_adds_rustc_flags_shared_lib_test",
185245
":rust_stdlib_filegroup_provides_runfiles_test",
186246
],
187247
)

0 commit comments

Comments
 (0)