Skip to content

Commit

Permalink
cargo-clippy: also expose as part of the clippy tool archive
Browse files Browse the repository at this point in the history
  • Loading branch information
kolloch committed Jun 18, 2024
1 parent d143d66 commit 030f781
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
22 changes: 14 additions & 8 deletions rust/private/repository_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,6 @@ def BUILD_for_rustfmt(target_triple):
binary_ext = system_to_binary_ext(target_triple.system),
)

_build_file_for_clippy_template = """\
filegroup(
name = "clippy_driver_bin",
srcs = ["bin/clippy-driver{binary_ext}"],
visibility = ["//visibility:public"],
)
"""

_build_file_for_rust_analyzer_proc_macro_srv = """\
filegroup(
name = "rust_analyzer_proc_macro_srv",
Expand All @@ -150,6 +142,19 @@ def BUILD_for_rust_analyzer_proc_macro_srv(exec_triple):
binary_ext = system_to_binary_ext(exec_triple.system),
)

_build_file_for_clippy_template = """\
filegroup(
name = "clippy_driver_bin",
srcs = ["bin/clippy-driver{binary_ext}"],
visibility = ["//visibility:public"],
)
filegroup(
name = "cargo_clippy_bin",
srcs = ["bin/cargo-clippy{binary_ext}"],
visibility = ["//visibility:public"],
)
"""

def BUILD_for_clippy(target_triple):
"""Emits a BUILD file the clippy archive.
Expand Down Expand Up @@ -244,6 +249,7 @@ rust_toolchain(
rustfmt = {rustfmt_label},
cargo = "//:cargo",
clippy_driver = "//:clippy_driver_bin",
cargo_clippy = "//:cargo_clippy_bin",
llvm_cov = {llvm_cov_label},
llvm_profdata = {llvm_profdata_label},
rustc_lib = "//:rustc_lib",
Expand Down
11 changes: 11 additions & 0 deletions rust/private/toolchain_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ def _toolchain_files_impl(ctx):
],
transitive_files = toolchain.rustc_lib,
)
elif ctx.attr.tool == "cargo-clippy":
files = depset([toolchain.cargo_clippy])
runfiles = ctx.runfiles(
files = [
toolchain.cargo_clippy,
toolchain.clippy_driver,
toolchain.rustc,
],
transitive_files = toolchain.rustc_lib,
)
elif ctx.attr.tool == "clippy":
files = depset([toolchain.clippy_driver])
runfiles = ctx.runfiles(
Expand Down Expand Up @@ -60,6 +70,7 @@ toolchain_files = rule(
doc = "The desired tool to get form the current rust_toolchain",
values = [
"cargo",
"cargo-clippy",
"clippy",
"rust_lib",
"rust_std",
Expand Down
16 changes: 16 additions & 0 deletions rust/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ def _generate_sysroot(
rustc_lib,
cargo = None,
clippy = None,
cargo_clippy = None,
llvm_tools = None,
rust_std = None,
rustfmt = None):
Expand Down Expand Up @@ -428,6 +429,12 @@ def _generate_sysroot(
sysroot_cargo = _symlink_sysroot_bin(ctx, name, "bin", cargo)
direct_files.extend([sysroot_cargo])

# Cargo-clippy
sysroot_cargo_clippy = None
if cargo_clippy:
sysroot_cargo_clippy = _symlink_sysroot_bin(ctx, name, "bin", cargo_clippy)
direct_files.extend([sysroot_cargo_clippy])

# Rustfmt
sysroot_rustfmt = None
if rustfmt:
Expand All @@ -453,6 +460,7 @@ def _generate_sysroot(
content = "\n".join([
"cargo: {}".format(cargo),
"clippy: {}".format(clippy),
"cargo-clippy: {}".format(cargo_clippy),
"llvm_tools: {}".format(llvm_tools),
"rust_std: {}".format(rust_std),
"rustc_lib: {}".format(rustc_lib),
Expand All @@ -469,6 +477,7 @@ def _generate_sysroot(
all_files = all_files,
cargo = sysroot_cargo,
clippy = sysroot_clippy,
cargo_clippy = sysroot_cargo_clippy,
rust_std = sysroot_rust_std,
rustc = sysroot_rustc,
rustc_lib = sysroot_rustc_lib,
Expand Down Expand Up @@ -529,6 +538,7 @@ def _rust_toolchain_impl(ctx):
rustfmt = ctx.file.rustfmt,
clippy = ctx.file.clippy_driver,
cargo = ctx.file.cargo,
cargo_clippy = ctx.file.cargo_clippy,
llvm_tools = ctx.attr.llvm_tools,
)

Expand Down Expand Up @@ -631,6 +641,7 @@ def _rust_toolchain_impl(ctx):
binary_ext = ctx.attr.binary_ext,
cargo = sysroot.cargo,
clippy_driver = sysroot.clippy,
cargo_clippy = sysroot.cargo_clippy,
compilation_mode_opts = compilation_mode_opts,
crosstool_files = ctx.files._cc_toolchain,
default_edition = ctx.attr.default_edition,
Expand Down Expand Up @@ -707,6 +718,11 @@ rust_toolchain = rule(
allow_single_file = True,
cfg = "exec",
),
"cargo_clippy": attr.label(
doc = "The location of the `cargo_clippy` binary. Can be a direct source or a filegroup cont