crosvm/tools/fmt
Dennis Kempin 7f03a70790 Parallelize tools/fmt
cargo fmt will only format those files that are referenced by
crosvm. To really catch all files in our source tree, we need to
run rustfmt directly.

This will check files multiple times, since rustfmt will follow
included modules. But is still faster that cargo fmt.

To run these in parallel, this change adds some tools to common.py
for parallel execution of commands.

This reduces the fmt check time from ~9s to 1.5s.

I verified that we are actually catching all files now by adding a
newline to all .rs files in our sources tree. The formatter reverted
them all.

BUG=b:218559722
TEST=./tools/fmt

Change-Id: If721d837ebc9eee3ce28fa7f439ab0bcc0b993cf
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3474926
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
2022-03-07 22:50:12 +00:00

49 lines
1.3 KiB
Python
Executable file

#!/usr/bin/env python3
# Copyright 2022 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Run `rustfmt` on all Rust code contained in the crosvm workspace, including
# all commmon/* crates as well.
#
# Usage:
#
# $ bin/fmt
#
# To print a diff and exit 1 if code is not formatted, but without changing any
# files, use:
#
# $ bin/fmt --check
#
from impl.common import CROSVM_ROOT, parallel, run_main, cmd, chdir
from pathlib import Path
mdformat = cmd("mdformat")
rustfmt = cmd(cmd("rustup which rustfmt"))
# How many files to check at once in each thread.
BATCH_SIZE = 8
def find_sources(extension: str):
for file in Path(".").glob(f"**/{extension}"):
if file.is_relative_to("third_party"):
continue
if "target" in file.parts:
continue
yield str(file)
def main(check: bool = False):
chdir(CROSVM_ROOT)
check_arg = "--check" if check else None
print(f"{'Checking' if check else 'Formatting'}: Rust, Markdown")
parallel(
*rustfmt(check_arg).foreach(find_sources("*.rs"), batch_size=BATCH_SIZE),
*mdformat("--wrap 100", check_arg).foreach(find_sources("*.md"), batch_size=BATCH_SIZE),
).fg()
run_main(main)