Skip to content

Commit

Permalink
upstream_wrapper: Generalize upstream_wrapper for rustfmt
Browse files Browse the repository at this point in the history
...so we can reuse it for more upstream binaries
  • Loading branch information
kolloch committed Jun 18, 2024
1 parent 78c68d6 commit d143d66
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
18 changes: 4 additions & 14 deletions tools/rustfmt/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,10 @@ rust_clippy(
],
)

# This is a small wrapper around the raw upstream rustfmt binary which can be `bazel run`.
rust_binary(
# Deprecated but present for compatibility.
alias(
name = "upstream_rustfmt",
srcs = [
"src/upstream_rustfmt_wrapper.rs",
],
data = ["//rust/toolchain:current_rustfmt_toolchain_for_target"],
edition = "2018",
rustc_env = {
"RUSTFMT": "$(rlocationpath //rust/toolchain:current_rustfmt_toolchain_for_target)",
},
toolchains = ["@rules_rust//rust/toolchain:current_rust_toolchain"],
actual = "//tools/upstream_wrapper:upstream_rustfmt",
deprecation = "Prefer //tools/upstream_wrapper:upstream_rustfmt",
visibility = ["//visibility:public"],
deps = [
"//tools/runfiles",
],
)
29 changes: 29 additions & 0 deletions tools/upstream_wrapper/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("//rust:defs.bzl", "rust_binary", "rust_clippy", "rust_library")
load("//tools:tool_utils.bzl", "aspect_repository")

tools = {
"rustfmt": "//rust/toolchain:current_rustfmt_toolchain_for_target",
}

# This is a small wrapper around the raw upstream binary which can be `bazel run`.

[
rust_binary(
name = "upstream_{}".format(tool_name),
srcs = [
"src/main.rs",
],
data = [target],
edition = "2018",
rustc_env = {
"WRAPPED_TOOL_NAME": tool_name,
"WRAPPED_TOOL_TARGET": "$(rlocationpath {})".format(target),
},
toolchains = ["@rules_rust//rust/toolchain:current_rust_toolchain"],
visibility = ["//visibility:public"],
deps = [
"//tools/runfiles",
],
)
for (tool_name, target) in tools.items()
]
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
use std::path::PathBuf;
use std::process::{exit, Command};

const WRAPPED_TOOL_NAME: &str = env!("WRAPPED_TOOL_NAME");
const WRAPPED_TOOL_TARGET: &str = env!("WRAPPED_TOOL_TARGET");

fn main() {
let runfiles = runfiles::Runfiles::create().unwrap();

let rustfmt = runfiles::rlocation!(runfiles, env!("RUSTFMT"));
if !rustfmt.exists() {
panic!("rustfmt does not exist at: {}", rustfmt.display());
let wrapped_tool_path = runfiles::rlocation!(runfiles, WRAPPED_TOOL_TARGET);
if !wrapped_tool_path.exists() {
panic!(
"{WRAPPED_TOOL_NAME} does not exist at: {}",
wrapped_tool_path.display()
);
}

let working_directory = std::env::var_os("BUILD_WORKING_DIRECTORY")
.map(PathBuf::from)
.unwrap_or_else(|| std::env::current_dir().expect("Failed to get working directory"));

let status = Command::new(rustfmt)
let status = Command::new(wrapped_tool_path)
.current_dir(&working_directory)
.args(std::env::args_os().skip(1))
.status()
.expect("Failed to run rustfmt");
.unwrap_or_else(|e| panic!("Failed to run {WRAPPED_TOOL_NAME} {:#}", e));
if let Some(exit_code) = status.code() {
exit(exit_code);
}
Expand Down

0 comments on commit d143d66

Please sign in to comment.