Skip to content

Commit

Permalink
Add clippy and rustfmt check aspects
Browse files Browse the repository at this point in the history
This enables clippy and rustfmt checks on all rust targets in the
projecs.

They will be run by default in CI.
They won't be run by default locally.

To run locally, include the --config=rustchecks flag

Change-Id: Ie5a093020391bc444f5530dc14b36d068f7eec3c
  • Loading branch information
jblebrun committed Jun 4, 2024
1 parent 63ecbb4 commit 0b5230d
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 5 deletions.
13 changes: 12 additions & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
# Bazel build defaults.
# incompatible_enable_cc_toolchain_resolution:
# Should not be needed after Bazel 7.0 is released.
# cxxopt='-std=c++17':
# cxxopt='-std=c++17':
# Build C++ targets using C++17.
# <https://stackoverflow.com/questions/40260242/how-to-set-c-standard-version-when-build-with-bazel>
# --host_cxxopt='-std=c++17'
# Fix Abseil "C++ versions less than C++14 are not supported".
# <https://github.com/protocolbuffers/protobuf/issues/12393#issuecomment-1504349780>
build --incompatible_enable_cc_toolchain_resolution --cxxopt='-std=c++17' --host_cxxopt='-std=c++17'

# Setup clippy aspect to run clippy automatically on all rust targets
# See: https://bazelbuild.github.io/rules_rust/rust_clippy.html
build:clippy --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect
build:clippy --output_groups=+clippy_checks

# Setup rustfmt aspect to run automatically on all rust targets
# https://bazelbuild.github.io/rules_rust/rust_fmt.html
build:rustfmt --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect
build:rustfmt --output_groups=+rustfmt_checks
build:rustfmt --@rules_rust//:rustfmt.toml=//:.rustfmt.toml

# Required for bazel_clang_tidy to operate as expected
build:clang-tidy --aspects @bazel_clang_tidy//clang_tidy:clang_tidy.bzl%clang_tidy_aspect
build:clang-tidy --output_groups=report
Expand Down
5 changes: 4 additions & 1 deletion BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ package(
)

# Export LICENSE file for projects that reference Oak in Bazel as an external dependency.
exports_files(["LICENSE"])
exports_files([
"LICENSE",
".rustfmt.toml",
])

constraint_value(
name = "os_oak",
Expand Down
12 changes: 12 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ bazel-ci:
# Some crates also need to be built for x86_64-unknown-none.
bazel build --config=unsafe-fast-presubmit --platforms=//:x86_64-unknown-none -- {{bare_metal_crates}}

bazel-clippy:
bazel build --config=clippy --config=unsafe-fast-presubmit //...:all -- -third_party/...

bazel-rustfmt:
bazel build --config=rustfmt --config=unsafe-fast-presubmit //...:all -- -third_party/...

xtask job:
./scripts/xtask {{job}}

clippy-ci: (xtask "run-cargo-clippy") bazel-clippy
check-format-ci: (xtask "check-format") bazel-rustfmt

# Temporary target to help debugging Bazel remote cache with more detailed logs.
# It should be deleted when debugging is completed.
# TODO: b/337266665 - Remove bazel-cache-test logic once we are satisfied with remote cache hits.
Expand Down
2 changes: 1 addition & 1 deletion kokoro/check_format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
source "$(dirname "$0")/common.sh"

./scripts/docker_pull
./scripts/docker_run nix develop .#ci --command ./scripts/xtask check-format
./scripts/docker_run nix develop .#ci --command just check-format-ci
2 changes: 1 addition & 1 deletion kokoro/run_clippy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
source "$(dirname "$0")/common.sh"

./scripts/docker_pull
./scripts/docker_run nix develop .#ci --command ./scripts/xtask run-cargo-clippy
./scripts/docker_run nix develop .#ci --command just clippy-ci
2 changes: 1 addition & 1 deletion oak_proto_build_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn fix_prost_derives() -> Result<(), Box<dyn std::error::Error>> {
// let out_dir_path = std::path::PathBuf::from(std::env::var("OUT_DIR"))?;

let out_dir = std::env::var("OUT_DIR")?;
for entry in fs::read_dir(&out_dir)? {
for entry in fs::read_dir(out_dir)? {
let file_path = entry?.path();
let contents = fs::read_to_string(&file_path)?;

Expand Down
6 changes: 6 additions & 0 deletions oak_restricted_kernel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ rust_library(
rust_test(
name = "oak_restricted_kernel_test",
crate = ":oak_restricted_kernel",
crate_features = [
"simple_io_channel",
],
deps = [
"@oak_crates_index//:assertables",
],
Expand All @@ -91,6 +94,9 @@ rust_test(
rust_test(
name = "oak_restricted_kernel_sinmple_io_channel_test",
crate = ":oak_restricted_kernel_simple_io_channel",
crate_features = [
"simple_io_channel",
],
deps = [
"@oak_crates_index//:assertables",
],
Expand Down

0 comments on commit 0b5230d

Please sign in to comment.