Skip to content

Commit fc3251a

Browse files
authored
Rollup merge of #154616 - erickt:quiet, r=jieyouxu
Add `--quiet` flag to x.py and bootstrap to suppress output This adds a `--quiet` flag to x.py and bootstrap to suppress some of the output when compiling Rust. It conflicts with `--verbose`, matching the behavior of `cargo` which does not allow `--verbose` and `--quiet`. It works by passing quiet flags down to the underlying cargo, or LLVM build processes. Note that for LLVM, we only can suppress logs when we explicitly configure it with ninja. Otherwise we won't know what flag to pass along to whichever build system cmake decides to use. This can be helpful with AI workloads in the Rust codebase to help shrink down the output to reduce token usage, which can help prevent context pollution and lower costs. This patch was partially generated with Gemini, but I've reviewed the changes it made.
2 parents 2beaa60 + f52f125 commit fc3251a

14 files changed

Lines changed: 484 additions & 182 deletions

File tree

src/bootstrap/bootstrap.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def eprint(*args, **kwargs):
4949
print(*args, **kwargs)
5050

5151

52-
def get(base, url, path, checksums, verbose=False):
52+
def get(base, url, path, checksums, verbose=0):
5353
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
5454
temp_path = temp_file.name
5555

@@ -66,11 +66,11 @@ def get(base, url, path, checksums, verbose=False):
6666
sha256 = checksums[url]
6767
if os.path.exists(path):
6868
if verify(path, sha256, False):
69-
if verbose:
69+
if verbose > 0:
7070
eprint("using already-download file", path)
7171
return
7272
else:
73-
if verbose:
73+
if verbose > 0:
7474
eprint(
7575
"ignoring already-download file",
7676
path,
@@ -80,12 +80,12 @@ def get(base, url, path, checksums, verbose=False):
8080
download(temp_path, "{}/{}".format(base, url), True, verbose)
8181
if not verify(temp_path, sha256, verbose):
8282
raise RuntimeError("failed verification")
83-
if verbose:
83+
if verbose > 0:
8484
eprint("moving {} to {}".format(temp_path, path))
8585
shutil.move(temp_path, path)
8686
finally:
8787
if os.path.isfile(temp_path):
88-
if verbose:
88+
if verbose > 0:
8989
eprint("removing", temp_path)
9090
os.unlink(temp_path)
9191

@@ -113,11 +113,11 @@ def _download(path, url, probably_big, verbose, exception):
113113
# If an error occurs:
114114
# - If we are on win32 fallback to powershell
115115
# - Otherwise raise the error if appropriate
116-
if probably_big or verbose:
116+
if probably_big or verbose > 0:
117117
eprint("downloading {}".format(url))
118118

119119
try:
120-
if (probably_big or verbose) and "GITHUB_ACTIONS" not in os.environ:
120+
if (probably_big or verbose > 0) and "GITHUB_ACTIONS" not in os.environ:
121121
option = "--progress-bar"
122122
else:
123123
option = "--silent"
@@ -180,7 +180,7 @@ def _download(path, url, probably_big, verbose, exception):
180180

181181
def verify(path, expected, verbose):
182182
"""Check if the sha256 sum of the given path is valid"""
183-
if verbose:
183+
if verbose > 0:
184184
eprint("verifying", path)
185185
with open(path, "rb") as source:
186186
found = hashlib.sha256(source.read()).hexdigest()
@@ -194,7 +194,7 @@ def verify(path, expected, verbose):
194194
return verified
195195

196196

197-
def unpack(tarball, tarball_suffix, dst, verbose=False, match=None):
197+
def unpack(tarball, tarball_suffix, dst, verbose=0, match=None):
198198
"""Unpack the given tarball file"""
199199
eprint("extracting", tarball)
200200
fname = os.path.basename(tarball).replace(tarball_suffix, "")
@@ -208,7 +208,7 @@ def unpack(tarball, tarball_suffix, dst, verbose=False, match=None):
208208
name = name[len(match) + 1 :]
209209

210210
dst_path = os.path.join(dst, name)
211-
if verbose:
211+
if verbose > 0:
212212
eprint(" extracting", member)
213213
tar.extract(member, dst)
214214
src_path = os.path.join(dst, member)
@@ -218,9 +218,9 @@ def unpack(tarball, tarball_suffix, dst, verbose=False, match=None):
218218
shutil.rmtree(os.path.join(dst, fname))
219219

220220

221-
def run(args, verbose=False, exception=False, is_bootstrap=False, **kwargs):
221+
def run(args, verbose=0, exception=False, is_bootstrap=False, **kwargs):
222222
"""Run a child program in a new process"""
223-
if verbose:
223+
if verbose > 0:
224224
eprint("running: " + " ".join(args))
225225
sys.stdout.flush()
226226
# Ensure that the .exe is used on Windows just in case a Linux ELF has been
@@ -233,7 +233,7 @@ def run(args, verbose=False, exception=False, is_bootstrap=False, **kwargs):
233233
code = ret.wait()
234234
if code != 0:
235235
err = "failed to run: " + " ".join(args)
236-
if verbose or exception:
236+
if verbose > 0 or exception:
237237
raise RuntimeError(err)
238238
# For most failures, we definitely do want to print this error, or the user will have no
239239
# idea what went wrong. But when we've successfully built bootstrap and it failed, it will
@@ -293,13 +293,13 @@ def default_build_triple(verbose):
293293
version = version.decode(default_encoding)
294294
host = next(x for x in version.split("\n") if x.startswith("host: "))
295295
triple = host.split("host: ")[1]
296-
if verbose:
296+
if verbose > 0:
297297
eprint(
298298
"detected default triple {} from pre-installed rustc".format(triple)
299299
)
300300
return triple
301301
except Exception as e:
302-
if verbose:
302+
if verbose > 0:
303303
eprint("pre-installed rustc not detected: {}".format(e))
304304
eprint("falling back to auto-detect")
305305

@@ -699,7 +699,7 @@ def download_toolchain(self):
699699
# Unpack the tarballs in parallel.
700700
# In Python 2.7, Pool cannot be used as a context manager.
701701
pool_size = min(len(tarballs_download_info), get_cpus())
702-
if self.verbose:
702+
if self.verbose > 0:
703703
print(
704704
"Choosing a pool size of",
705705
pool_size,
@@ -1126,6 +1126,8 @@ def build_bootstrap_cmd(self, env):
11261126
]
11271127
# verbose cargo output is very noisy, so only enable it with -vv
11281128
args.extend("--verbose" for _ in range(self.verbose - 1))
1129+
if self.verbose < 0:
1130+
args.append("--quiet")
11291131

11301132
target_features = []
11311133
if self.get_toml("crt-static", build_section) == "true":
@@ -1275,7 +1277,12 @@ def parse_args(args):
12751277
parser.add_argument(
12761278
"--warnings", choices=["deny", "warn", "default"], default="default"
12771279
)
1278-
parser.add_argument("-v", "--verbose", action="count", default=0)
1280+
group = parser.add_mutually_exclusive_group()
1281+
group.add_argument("-v", "--verbose", action="count", default=0)
1282+
# Note that we're storing the `--quiet` value in `verbose`. That way we don't need to thread
1283+
# `self.quiet` throughout the code. That could be error prone, which could let some output
1284+
# through that should have been suppressed.
1285+
group.add_argument("-q", "--quiet", action="store_const", const=-1, dest="verbose")
12791286

12801287
return parser.parse_known_args(args)[0]
12811288

src/bootstrap/src/core/build_steps/llvm.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,18 @@ fn configure_cmake(
651651
// LLVM and LLD builds can produce a lot of those and hit CI limits on log size.
652652
cfg.define("CMAKE_INSTALL_MESSAGE", "LAZY");
653653

654+
if builder.config.quiet {
655+
// Only log errors and warnings from `cmake`.
656+
cfg.define("CMAKE_MESSAGE_LOG_LEVEL", "WARNING");
657+
658+
// If we're configuring llvm to build with `ninja`, we can suppress output from it with
659+
// `--quiet`. Otherwise don't add anything since we don't know which build system is going
660+
// to use.
661+
if builder.ninja() {
662+
cfg.build_arg("--quiet");
663+
}
664+
}
665+
654666
// Do not allow the user's value of DESTDIR to influence where
655667
// LLVM will install itself. LLVM must always be installed in our
656668
// own build directories.

src/bootstrap/src/core/builder/cargo.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,11 @@ impl Builder<'_> {
509509
}
510510
};
511511

512+
// Optionally suppress cargo output.
513+
if self.config.quiet {
514+
cargo.arg("--quiet");
515+
}
516+
512517
// Run cargo from the source root so it can find .cargo/config.
513518
// This matters when using vendoring and the working directory is outside the repository.
514519
cargo.current_dir(&self.src);

src/bootstrap/src/core/config/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ pub struct Config {
140140
pub config: Option<PathBuf>,
141141
pub jobs: Option<u32>,
142142
pub cmd: Subcommand,
143+
pub quiet: bool,
143144
pub incremental: bool,
144145
pub dump_bootstrap_shims: bool,
145146
/// Arguments appearing after `--` to be forwarded to tools,
@@ -369,6 +370,7 @@ impl Config {
369370
let Flags {
370371
cmd: flags_cmd,
371372
verbose: flags_verbose,
373+
quiet: flags_quiet,
372374
incremental: flags_incremental,
373375
config: flags_config,
374376
build_dir: flags_build_dir,
@@ -1433,6 +1435,7 @@ impl Config {
14331435
print_step_timings: build_print_step_timings.unwrap_or(false),
14341436
profiler: build_profiler.unwrap_or(false),
14351437
python: build_python.map(PathBuf::from),
1438+
quiet: flags_quiet,
14361439
reproducible_artifacts: flags_reproducible_artifact,
14371440
reuse: build_reuse.map(PathBuf::from),
14381441
rust_analyzer_info,

src/bootstrap/src/core/config/flags.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ pub struct Flags {
4646
#[command(subcommand)]
4747
pub cmd: Subcommand,
4848

49-
#[arg(global = true, short, long, action = clap::ArgAction::Count)]
49+
#[arg(global = true, short, long, action = clap::ArgAction::Count, conflicts_with = "quiet")]
5050
/// use verbose output (-vv for very verbose)
5151
pub verbose: u8, // each extra -v after the first is passed to Cargo
52+
#[arg(global = true, short, long, conflicts_with = "verbose")]
53+
/// use quiet output
54+
pub quiet: bool,
5255
#[arg(global = true, short, long)]
5356
/// use incremental compilation
5457
pub incremental: bool,

0 commit comments

Comments
 (0)