Skip to content

Commit 0d941d4

Browse files
authored
Merge branch 'main' into cdylib-bin-link-args
2 parents 83887c4 + 3a95857 commit 0d941d4

463 files changed

Lines changed: 18826 additions & 18751 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cargo/3rdparty/crates/crates.bzl

Lines changed: 1148 additions & 13 deletions
Large diffs are not rendered by default.

cargo/3rdparty/crates/defs.bzl

Lines changed: 16 additions & 1151 deletions
Large diffs are not rendered by default.

cargo/private/cargo_build_script.bzl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,16 @@ def _cargo_build_script_impl(ctx):
608608
if out_dir_volatile_basenames:
609609
env["RULES_RUST_OUT_DIR_VOLATILE_BASENAMES"] = ":".join(out_dir_volatile_basenames)
610610

611+
emit_warnings_setting = ctx.attr._emit_build_script_warnings[BuildSettingInfo].value
612+
if emit_warnings_setting == "on":
613+
emit_warnings = True
614+
elif emit_warnings_setting == "off":
615+
emit_warnings = False
616+
else:
617+
emit_warnings = ctx.attr.emit_warnings
618+
if not emit_warnings:
619+
env["RULES_RUST_SUPPRESS_BUILD_SCRIPT_WARNINGS"] = "1"
620+
611621
ctx.actions.run(
612622
executable = ctx.executable._cargo_build_script_runner,
613623
arguments = [args, runfiles_args],
@@ -702,6 +712,16 @@ cargo_build_script = rule(
702712
providers = [[DepInfo], [CrateGroupInfo]],
703713
cfg = "exec",
704714
),
715+
"emit_warnings": attr.bool(
716+
doc = dedent("""\
717+
Whether to forward `cargo::warning=` lines from the build script to stderr.
718+
719+
Honored only when `--@rules_rust//cargo/settings:emit_build_script_warnings`
720+
is `auto` (the default). Setting the flag to `on` or `off` overrides
721+
this attribute for every target.
722+
"""),
723+
default = True,
724+
),
705725
"link_deps": attr.label_list(
706726
doc = dedent("""\
707727
The subset of the Rust (normal) dependencies of the crate that
@@ -777,6 +797,9 @@ cargo_build_script = rule(
777797
"_default_use_default_shell_env": attr.label(
778798
default = Label("//cargo/settings:use_default_shell_env"),
779799
),
800+
"_emit_build_script_warnings": attr.label(
801+
default = Label("//cargo/settings:emit_build_script_warnings"),
802+
),
780803
"_experimental_symlink_execroot": attr.label(
781804
default = Label("//cargo/settings:experimental_symlink_execroot"),
782805
),

cargo/private/cargo_build_script_runner/bin.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::path::{Path, PathBuf};
2222
use std::process::Command;
2323

2424
use cargo_build_script_runner::cargo_manifest_dir::{remove_symlink, symlink, RunfilesMaker};
25-
use cargo_build_script_runner::{BuildScriptOutput, CompileAndLinkFlags};
25+
use cargo_build_script_runner::{BuildScriptOutput, CompileAndLinkFlags, SUPPRESS_WARNINGS_ENV};
2626

2727
fn run_buildrs() -> Result<(), String> {
2828
// We use exec_root.join rather than std::fs::canonicalize, to avoid resolving symlinks, as
@@ -170,21 +170,25 @@ fn run_buildrs() -> Result<(), String> {
170170
);
171171
}
172172

173-
let (buildrs_outputs, process_output) = BuildScriptOutput::outputs_from_command(&mut command)
174-
.map_err(|process_output| {
175-
format!(
176-
"Build script process failed{}\n--stdout:\n{}\n--stderr:\n{}",
177-
if let Some(exit_code) = process_output.status.code() {
178-
format!(" with exit code {exit_code}")
179-
} else {
180-
String::new()
173+
let emit_warnings = env::var_os(SUPPRESS_WARNINGS_ENV).is_none_or(|v| v != "1");
174+
175+
let (buildrs_outputs, process_output) =
176+
BuildScriptOutput::outputs_from_command(&mut command, emit_warnings).map_err(
177+
|process_output| {
178+
format!(
179+
"Build script process failed{}\n--stdout:\n{}\n--stderr:\n{}",
180+
if let Some(exit_code) = process_output.status.code() {
181+
format!(" with exit code {exit_code}")
182+
} else {
183+
String::new()
184+
},
185+
String::from_utf8(process_output.stdout)
186+
.expect("Failed to parse stdout of child process"),
187+
String::from_utf8(process_output.stderr)
188+
.expect("Failed to parse stdout of child process"),
189+
)
181190
},
182-
String::from_utf8(process_output.stdout)
183-
.expect("Failed to parse stdout of child process"),
184-
String::from_utf8(process_output.stderr)
185-
.expect("Failed to parse stdout of child process"),
186-
)
187-
})?;
191+
)?;
188192

189193
write(
190194
&env_file,

cargo/private/cargo_build_script_runner/lib.rs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use std::io::{BufRead, BufReader, Read};
1818
use std::process::{Command, Output};
1919

20+
pub const SUPPRESS_WARNINGS_ENV: &str = "RULES_RUST_SUPPRESS_BUILD_SCRIPT_WARNINGS";
21+
2022
pub mod cargo_manifest_dir;
2123

2224
#[derive(Debug, PartialEq, Eq)]
@@ -57,13 +59,7 @@ pub enum BuildScriptOutput {
5759
}
5860

5961
impl BuildScriptOutput {
60-
/// Converts a line into a [BuildScriptOutput] enum.
61-
///
62-
/// Examples
63-
/// ```rust
64-
/// assert_eq!(BuildScriptOutput::new("cargo::rustc-link-lib=lib"), Some(BuildScriptOutput::LinkLib("lib".to_owned())));
65-
/// ```
66-
fn new(line: &str) -> Option<BuildScriptOutput> {
62+
fn new(line: &str, emit_warnings: bool) -> Option<BuildScriptOutput> {
6763
let split = line.splitn(2, '=').collect::<Vec<_>>();
6864
if split.len() <= 1 {
6965
// Not a cargo directive.
@@ -94,7 +90,9 @@ impl BuildScriptOutput {
9490
None
9591
}
9692
"warning" => {
97-
eprint!("Build Script Warning: {}", split[1]);
93+
if emit_warnings {
94+
eprint!("Build Script Warning: {}", split[1]);
95+
}
9896
None
9997
}
10098
"metadata" => {
@@ -135,7 +133,10 @@ impl BuildScriptOutput {
135133
}
136134

137135
/// Converts a [BufReader] into a vector of [BuildScriptOutput] enums.
138-
fn outputs_from_reader<T: Read>(mut reader: BufReader<T>) -> Vec<BuildScriptOutput> {
136+
fn outputs_from_reader<T: Read>(
137+
mut reader: BufReader<T>,
138+
emit_warnings: bool,
139+
) -> Vec<BuildScriptOutput> {
139140
let mut result = Vec::<BuildScriptOutput>::new();
140141
let mut buf = Vec::new();
141142
while reader
@@ -145,7 +146,7 @@ impl BuildScriptOutput {
145146
{
146147
// like cargo, ignore any lines that are not valid utf8
147148
if let Ok(line) = String::from_utf8(buf.clone()) {
148-
if let Some(bso) = BuildScriptOutput::new(&line) {
149+
if let Some(bso) = BuildScriptOutput::new(&line, emit_warnings) {
149150
result.push(bso);
150151
}
151152
}
@@ -157,13 +158,14 @@ impl BuildScriptOutput {
157158
/// Take a [Command], execute it and converts its input into a vector of [BuildScriptOutput]
158159
pub fn outputs_from_command(
159160
cmd: &mut Command,
161+
emit_warnings: bool,
160162
) -> Result<(Vec<BuildScriptOutput>, Output), Output> {
161163
let child_output = cmd
162164
.output()
163165
.unwrap_or_else(|e| panic!("Unable to start command:\n{:#?}\n{:?}", cmd, e));
164166
if child_output.status.success() {
165167
let reader = BufReader::new(child_output.stdout.as_slice());
166-
let output = Self::outputs_from_reader(reader);
168+
let output = Self::outputs_from_reader(reader, emit_warnings);
167169
Ok((output, child_output))
168170
} else {
169171
Err(child_output)
@@ -308,7 +310,7 @@ mod tests {
308310

309311
fn from_read_buffer_to_env_and_flags_test_impl(buff: Cursor<&str>) {
310312
let reader = BufReader::new(buff);
311-
let result = BuildScriptOutput::outputs_from_reader(reader);
313+
let result = BuildScriptOutput::outputs_from_reader(reader, true);
312314
assert_eq!(result.len(), 13);
313315
assert_eq!(result[0], BuildScriptOutput::LinkLib("sdfsdf".to_owned()));
314316
assert_eq!(result[1], BuildScriptOutput::Env("FOO=BAR".to_owned()));
@@ -453,7 +455,7 @@ cargo::rustc-env=valid2=2
453455
",
454456
);
455457
let reader = BufReader::new(buff);
456-
let result = BuildScriptOutput::outputs_from_reader(reader);
458+
let result = BuildScriptOutput::outputs_from_reader(reader, true);
457459
assert_eq!(result.len(), 2);
458460
assert_eq!(
459461
&BuildScriptOutput::outputs_to_env(&result, "/some/absolute/path", ""),
@@ -472,18 +474,30 @@ cargo:rustc-env=valid2=2
472474
",
473475
);
474476
let reader = BufReader::new(buff);
475-
let result = BuildScriptOutput::outputs_from_reader(reader);
477+
let result = BuildScriptOutput::outputs_from_reader(reader, true);
476478
assert_eq!(result.len(), 2);
477479
assert_eq!(
478480
&BuildScriptOutput::outputs_to_env(&result, "/some/absolute/path", ""),
479481
"valid1=1\nvalid2=2"
480482
);
481483
}
482484

485+
#[test]
486+
fn warning_lines_never_appear_in_outputs() {
487+
let lines = "cargo::warning=hello\ncargo::rustc-env=A=1\n";
488+
for emit_warnings in [true, false] {
489+
let result = BuildScriptOutput::outputs_from_reader(
490+
BufReader::new(Cursor::new(lines)),
491+
emit_warnings,
492+
);
493+
assert_eq!(result, vec![BuildScriptOutput::Env("A=1".to_owned())]);
494+
}
495+
}
496+
483497
#[test]
484498
fn metadata_directive_maps_to_dep_env_key_value() {
485499
let reader = BufReader::new(Cursor::new("cargo::metadata=version_1_10_0=1\n"));
486-
let result = BuildScriptOutput::outputs_from_reader(reader);
500+
let result = BuildScriptOutput::outputs_from_reader(reader, true);
487501
assert_eq!(
488502
result,
489503
vec![BuildScriptOutput::DepEnv("VERSION_1_10_0=1".to_owned())]
@@ -507,7 +521,7 @@ cargo::rustc-env=BAR=/abs/exec_root/elsewhere/file.rs
507521
",
508522
);
509523
let reader = BufReader::new(buff);
510-
let result = BuildScriptOutput::outputs_from_reader(reader);
524+
let result = BuildScriptOutput::outputs_from_reader(reader, true);
511525
assert_eq!(
512526
BuildScriptOutput::outputs_to_env(
513527
&result,
@@ -531,7 +545,7 @@ cargo::rustc-link-search=/abs/exec_root/other/path
531545
",
532546
);
533547
let reader = BufReader::new(buff);
534-
let result = BuildScriptOutput::outputs_from_reader(reader);
548+
let result = BuildScriptOutput::outputs_from_reader(reader, true);
535549
assert_eq!(
536550
BuildScriptOutput::outputs_to_flags(
537551
&result,

cargo/private/cargo_build_script_wrapper.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def cargo_build_script(
2323
proc_macro_deps = [],
2424
build_script_env = {},
2525
build_script_env_files = [],
26+
emit_warnings = True,
2627
use_default_shell_env = None,
2728
data = [],
2829
compile_data = [],
@@ -112,6 +113,10 @@ def cargo_build_script(
112113
build_script_env (dict, optional): Environment variables for build scripts.
113114
build_script_env_files (list of label, optional): Files containing additional environment variables to set
114115
when running the build script.
116+
emit_warnings (bool, optional): Whether to forward `cargo::warning=` lines from the build script
117+
to stderr. Honored only when
118+
`@rules_rust//cargo/settings:emit_build_script_warnings` is `auto` (the
119+
default); set the flag to `on` or `off` to override every target.
115120
use_default_shell_env (bool, optional): Whether or not to include the default shell environment for the build script action. If unset the global
116121
setting `@rules_rust//cargo/settings:use_default_shell_env` will be used to determine this value.
117122
data (list, optional): Files needed by the build script.
@@ -219,6 +224,7 @@ def cargo_build_script(
219224
version = version,
220225
build_script_env = build_script_env,
221226
build_script_env_files = build_script_env_files,
227+
emit_warnings = emit_warnings,
222228
use_default_shell_env = sanitized_use_default_shell_env,
223229
links = links,
224230
deps = deps,

cargo/settings/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ load(
33
":settings.bzl",
44
"cargo_manifest_dir_filename_suffixes_to_retain",
55
"debug_std_streams_output_group",
6+
"emit_build_script_warnings",
67
"experimental_symlink_execroot",
78
"out_dir_volatile_file_basenames",
89
"use_default_shell_env",
@@ -30,3 +31,5 @@ experimental_symlink_execroot()
3031
use_default_shell_env()
3132

3233
out_dir_volatile_file_basenames()
34+
35+
emit_build_script_warnings()

cargo/settings/settings.bzl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Definitions for all `@rules_rust//cargo` settings
44
"""
55

6-
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_list_flag")
6+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag", "string_list_flag")
77

88
def experimental_symlink_execroot():
99
"""A flag for which causes `cargo_build_script` to symlink the execroot of the action to \
@@ -43,6 +43,25 @@ def use_default_shell_env():
4343
build_setting_default = True,
4444
)
4545

46+
def emit_build_script_warnings():
47+
"""A flag which controls whether `cargo_build_script` warnings \
48+
(`cargo::warning=`) are printed to stderr.
49+
50+
Supported values:
51+
52+
- `on`: emit warnings for every `cargo_build_script` target, overriding any
53+
per-target `emit_warnings = False`.
54+
- `auto` (default): respect the per-target `emit_warnings` attribute.
55+
`crate_universe`-generated targets set it to `False`, so registry/git
56+
crates stay quiet (matching Cargo); first-party targets emit by default.
57+
- `off`: silence warnings build-wide.
58+
"""
59+
string_flag(
60+
name = "emit_build_script_warnings",
61+
build_setting_default = "auto",
62+
values = ["on", "auto", "off"],
63+
)
64+
4665
def out_dir_volatile_file_basenames():
4766
"""A flag which determines what file basenames are removed from `OUT_DIR` by `cargo_build_script` actions to make the `_bs.out_dir` TreeArtifact deterministic.
4867
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
load("//cargo:defs.bzl", "cargo_build_script")
2+
load("//rust:defs.bzl", "rust_library", "rust_test")
3+
4+
cargo_build_script(
5+
name = "build_quiet",
6+
srcs = ["build.rs"],
7+
edition = "2018",
8+
emit_warnings = False,
9+
)
10+
11+
rust_library(
12+
name = "quiet_lib",
13+
srcs = ["test_lib.rs"],
14+
edition = "2018",
15+
deps = [":build_quiet"],
16+
)
17+
18+
rust_test(
19+
name = "quiet_test",
20+
crate = ":quiet_lib",
21+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
println!("cargo::warning=this should be suppressed when emit_warnings=False");
3+
println!("cargo::rustc-env=FROM_BUILD_SCRIPT=ok");
4+
}

0 commit comments

Comments
 (0)