From d143d66110de0dcc53826c227a6e26ef092ae041 Mon Sep 17 00:00:00 2001 From: Peter Kolloch Date: Tue, 18 Jun 2024 19:02:54 +0200 Subject: [PATCH] upstream_wrapper: Generalize upstream_wrapper for rustfmt ...so we can reuse it for more upstream binaries --- tools/rustfmt/BUILD.bazel | 18 +++--------- tools/upstream_wrapper/BUILD.bazel | 29 +++++++++++++++++++ .../src/main.rs} | 16 ++++++---- 3 files changed, 44 insertions(+), 19 deletions(-) create mode 100644 tools/upstream_wrapper/BUILD.bazel rename tools/{rustfmt/src/upstream_rustfmt_wrapper.rs => upstream_wrapper/src/main.rs} (65%) diff --git a/tools/rustfmt/BUILD.bazel b/tools/rustfmt/BUILD.bazel index c8aeb81a64..f1c05e6871 100644 --- a/tools/rustfmt/BUILD.bazel +++ b/tools/rustfmt/BUILD.bazel @@ -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", - ], ) diff --git a/tools/upstream_wrapper/BUILD.bazel b/tools/upstream_wrapper/BUILD.bazel new file mode 100644 index 0000000000..3468f19581 --- /dev/null +++ b/tools/upstream_wrapper/BUILD.bazel @@ -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() +] \ No newline at end of file diff --git a/tools/rustfmt/src/upstream_rustfmt_wrapper.rs b/tools/upstream_wrapper/src/main.rs similarity index 65% rename from tools/rustfmt/src/upstream_rustfmt_wrapper.rs rename to tools/upstream_wrapper/src/main.rs index 33c091aa13..538794124f 100644 --- a/tools/rustfmt/src/upstream_rustfmt_wrapper.rs +++ b/tools/upstream_wrapper/src/main.rs @@ -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); }