xref: /aosp_15_r20/external/bazelbuild-rules_rust/tools/rustfmt/BUILD.bazel (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1load("//rust:defs.bzl", "rust_binary", "rust_clippy", "rust_library")
2load("//tools:tool_utils.bzl", "aspect_repository")
3
4exports_files(
5    [
6        "rustfmt.toml",
7        "rustfmt_utils.bzl",
8    ],
9    visibility = ["//visibility:public"],
10)
11
12rust_library(
13    name = "rustfmt_lib",
14    srcs = glob(
15        include = ["src/**/*.rs"],
16        exclude = [
17            "src/**/*main.rs",
18            "src/bin/**",
19        ],
20    ),
21    data = [
22        "//:rustfmt.toml",
23        "//rust/toolchain:current_rustfmt_toolchain_for_target",
24    ],
25    edition = "2018",
26    rustc_env = {
27        "RUSTFMT": "$(rlocationpath //rust/toolchain:current_rustfmt_toolchain_for_target)",
28        "RUSTFMT_CONFIG": "$(rlocationpath //:rustfmt.toml)",
29    },
30    deps = [
31        "//tools/runfiles",
32    ],
33)
34
35# Deprecated but present for compatibility.
36alias(
37    name = "rustfmt",
38    actual = ":target_aware_rustfmt",
39    deprecation = "Prefer //tools/upstream_wrapper:rustfmt",
40    visibility = ["//visibility:public"],
41)
42
43# This is a wrapper around the upstream rustfmt binary which is aware of targets,
44# and will try to do things like set the correct edition for files when formatting them based on their owning targets.
45rust_binary(
46    name = "target_aware_rustfmt",
47    srcs = [
48        "src/main.rs",
49    ],
50    data = [
51        "//:rustfmt.toml",
52    ],
53    edition = "2018",
54    rustc_env = {
55        "ASPECT_REPOSITORY": aspect_repository(),
56        "RUST_DEFAULT_EDITION": "$(RUST_DEFAULT_EDITION)",
57    },
58    toolchains = ["@rules_rust//rust/toolchain:current_rust_toolchain"],
59    visibility = ["//visibility:public"],
60    deps = [
61        ":rustfmt_lib",
62        "//util/label",
63    ],
64)
65
66rust_binary(
67    name = "rustfmt_test",
68    srcs = [
69        "src/bin/test_main.rs",
70    ],
71    edition = "2018",
72    visibility = ["//visibility:public"],
73    deps = [
74        ":rustfmt_lib",
75        "//tools/runfiles",
76    ],
77)
78
79rust_clippy(
80    name = "rustfmt_clippy",
81    testonly = True,
82    visibility = ["//visibility:private"],
83    deps = [
84        ":target_aware_rustfmt",
85    ],
86)
87
88# Deprecated but present for compatibility.
89alias(
90    name = "upstream_rustfmt",
91    actual = "//tools/upstream_wrapper:rustfmt",
92    deprecation = "Prefer //tools/upstream_wrapper:rustfmt",
93    visibility = ["//visibility:public"],
94)
95