-
Notifications
You must be signed in to change notification settings - Fork 501
Implement Clippy Aspect & Build Rule #339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
43c66f7
Install clippy, bump default rust version to 1.44.0
smklein ad348f1
Add missing paren
smklein 58aa347
Update rust_toolchain to include clippy driver
smklein 1c1ec3c
Implement Clippy aspect, Build Rule
smklein e1fa891
Merge branch 'master' into bump-revs
smklein 4ed8aa9
Merge branch 'bump-revs' into clippy-aspect
smklein 4cd90bc
cleanup
smklein 97372a8
Merge branch 'master' into bump-revs
smklein a5460ef
Merge branch 'bump-revs' into clippy-aspect
smklein 7c129fe
Condensing srcs aggregation
smklein f39be4d
Review feedback
smklein b1ff4c5
Merge branch 'master' into bump-revs
smklein 6e80097
Merge branch 'bump-revs' into clippy-aspect
smklein 114a995
Fix depsets
smklein ed03cfc
Patch clippy such that it can run on examples directory
smklein 2260b4b
Run clippy on examples
smklein 3710c86
Improve documentation
smklein 238943f
Add clippy tests to presubmit
smklein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
# Copyright 2020 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
load( | ||
"@io_bazel_rules_rust//rust:private/rustc.bzl", | ||
"CrateInfo", | ||
"collect_deps", | ||
"collect_inputs", | ||
"construct_arguments", | ||
"construct_compile_command", | ||
) | ||
load( | ||
"@io_bazel_rules_rust//rust:private/rust.bzl", | ||
"crate_root_src", | ||
"get_edition" | ||
) | ||
load("@io_bazel_rules_rust//rust:private/utils.bzl", "find_toolchain") | ||
|
||
_rust_extensions = [ | ||
"rs", | ||
] | ||
|
||
def _is_rust_target(srcs): | ||
return any([src.extension in _rust_extensions for src in srcs]) | ||
|
||
def _all_sources(target, rule): | ||
srcs = [] | ||
if "srcs" in dir(rule.attr): | ||
srcs += [f for src in rule.attr.srcs for f in src.files.to_list()] | ||
if "hdrs" in dir(rule.attr): | ||
srcs += [f for src in rule.attr.hdrs for f in src.files.to_list()] | ||
return srcs | ||
|
||
def _rust_sources(target, rule): | ||
srcs = _all_sources(target, rule) | ||
smklein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
srcs = [src for src in srcs if src.extension in _rust_extensions] | ||
return srcs | ||
|
||
def _clippy_aspect_impl(target, ctx): | ||
if CrateInfo not in target: | ||
return [] | ||
rust_srcs = _rust_sources(target, ctx.rule) | ||
if rust_srcs == []: | ||
return [] | ||
|
||
toolchain = find_toolchain(ctx) | ||
crate_name = ctx.label.name.replace("-", "_") | ||
crate_type = getattr(ctx.rule.attr, "crate_type") if hasattr(ctx.rule.attr, "crate_type") else "lib" | ||
crate_info = CrateInfo( | ||
name = crate_name, | ||
type = crate_type, | ||
root = crate_root_src(ctx, srcs = rust_srcs), | ||
srcs = rust_srcs, | ||
deps = ctx.rule.attr.deps, | ||
proc_macro_deps = ctx.rule.attr.proc_macro_deps, | ||
aliases = ctx.rule.attr.aliases, | ||
output = None, | ||
edition = get_edition(ctx.rule.attr, toolchain), | ||
rustc_env = ctx.rule.attr.rustc_env, | ||
) | ||
|
||
dep_info, build_info = collect_deps( | ||
ctx.label, | ||
crate_info.deps, | ||
crate_info.proc_macro_deps, | ||
crate_info.aliases, | ||
toolchain, | ||
) | ||
|
||
compile_inputs, out_dir = collect_inputs( | ||
ctx, | ||
toolchain, | ||
crate_info, | ||
dep_info, | ||
build_info | ||
) | ||
|
||
args, env = construct_arguments( | ||
ctx, | ||
toolchain, | ||
crate_info, | ||
dep_info, | ||
output_hash = None, | ||
rust_flags = []) | ||
|
||
# A marker file indicating clang tidy has executed successfully. | ||
# This file is necessary because "ctx.actions.run" mandates an output. | ||
clippy_marker = ctx.actions.declare_file(ctx.label.name + "_clippy.ok") | ||
|
||
command = construct_compile_command( | ||
ctx, | ||
toolchain.clippy_driver.path, | ||
toolchain, | ||
crate_info, | ||
build_info, | ||
out_dir, | ||
) + (" && touch %s" % clippy_marker.path) | ||
|
||
# Deny the default-on clippy warning levels. | ||
# | ||
# If these are left as warnings, then Bazel will consider the execution | ||
# result of the aspect to be "success", and Clippy won't be re-triggered | ||
# unless the source file is modified. | ||
args.add("-Dclippy::style") | ||
args.add("-Dclippy::correctness"); | ||
args.add("-Dclippy::complexity"); | ||
args.add("-Dclippy::perf"); | ||
|
||
ctx.actions.run_shell( | ||
command = command, | ||
inputs = compile_inputs, | ||
outputs = [clippy_marker], | ||
env = env, | ||
tools = [toolchain.clippy_driver], | ||
arguments = [args], | ||
mnemonic = "Clippy", | ||
) | ||
|
||
return [ | ||
OutputGroupInfo(clippy_checks = depset([clippy_marker])), | ||
] | ||
|
||
# Executes a "compile command" on all specified targets. | ||
# | ||
# Example: Run the clang-tidy checker on all targets in the codebase. | ||
# bazel build --aspects=@rules_rust//rust:clippy.bzl%rust_clippy_aspect \ | ||
# --output_groups=clippy_checks \ | ||
# //... | ||
rust_clippy_aspect = aspect( | ||
attr_aspects = ["deps"], | ||
fragments = ["cpp"], | ||
host_fragments = ["cpp"], | ||
attrs = { | ||
"_cc_toolchain": attr.label( | ||
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"), | ||
), | ||
}, | ||
toolchains = [ | ||
"@io_bazel_rules_rust//rust:toolchain", | ||
"@bazel_tools//tools/cpp:toolchain_type" | ||
], | ||
implementation = _clippy_aspect_impl, | ||
) | ||
smklein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def _rust_clippy_rule_impl(ctx): | ||
checks = depset([]) | ||
for dep in ctx.attr.deps: | ||
checks = depset(dep[OutputGroupInfo].clippy_checks.to_list(), transitive = checks.to_list()) | ||
smklein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return [DefaultInfo(files = checks)] | ||
|
||
rust_clippy = rule( | ||
damienmg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
implementation = _rust_clippy_rule_impl, | ||
attrs = { | ||
'deps': attr.label_list(aspects = [rust_clippy_aspect]), | ||
}, | ||
smklein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.