From 639e109691f4e66cf897736e1295bb816c5c5a75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Tue, 26 Mar 2024 15:16:19 +0100 Subject: [PATCH] refactor(xtask): move binutil in separate module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- xtask/src/archive.rs | 29 ++++------------------------- xtask/src/binutil.rs | 22 ++++++++++++++++++++++ xtask/src/main.rs | 2 ++ 3 files changed, 28 insertions(+), 25 deletions(-) create mode 100644 xtask/src/binutil.rs diff --git a/xtask/src/archive.rs b/xtask/src/archive.rs index aed6283dfc..9c11b6537c 100644 --- a/xtask/src/archive.rs +++ b/xtask/src/archive.rs @@ -1,12 +1,10 @@ use std::collections::HashSet; -use std::env; use std::fmt::Write; use std::path::{Path, PathBuf}; -use anyhow::{anyhow, Result}; +use anyhow::Result; use goblin::archive::Archive as GoblinArchive; use goblin::elf64::header; -use llvm_tools::LlvmTools; use xshell::cmd; pub struct Archive(PathBuf); @@ -51,7 +49,7 @@ impl Archive { }; let all_symbols = { - let nm = binutil("nm")?; + let nm = crate::binutil("nm")?; let stdout = cmd!(sh, "{nm} --export-symbols {archive}").output()?.stdout; String::from_utf8(stdout)? }; @@ -75,7 +73,7 @@ impl Archive { let rename_path = archive.with_extension("redefine-syms"); sh.write_file(&rename_path, symbol_renames)?; - let objcopy = binutil("objcopy")?; + let objcopy = crate::binutil("objcopy")?; cmd!(sh, "{objcopy} --redefine-syms={rename_path} {archive}").run()?; sh.remove_path(&rename_path)?; @@ -88,7 +86,7 @@ impl Archive { let archive = self.as_ref(); let file = file.as_ref(); - let ar = binutil("ar")?; + let ar = crate::binutil("ar")?; cmd!(sh, "{ar} qL {archive} {file}").run()?; Ok(()) @@ -115,22 +113,3 @@ impl Archive { Ok(()) } } - -fn binutil(name: &str) -> Result { - let exe_suffix = env::consts::EXE_SUFFIX; - let exe = format!("llvm-{name}{exe_suffix}"); - - let path = LlvmTools::new() - .map_err(|err| match err { - llvm_tools::Error::NotFound => anyhow!( - "Could not find llvm-tools component\n\ - \n\ - Maybe the rustup component `llvm-tools` is missing? Install it through: `rustup component add llvm-tools`" - ), - err => anyhow!("{err:?}"), - })? - .tool(&exe) - .ok_or_else(|| anyhow!("could not find {exe}"))?; - - Ok(path) -} diff --git a/xtask/src/binutil.rs b/xtask/src/binutil.rs new file mode 100644 index 0000000000..e4ca73a886 --- /dev/null +++ b/xtask/src/binutil.rs @@ -0,0 +1,22 @@ +use std::path::PathBuf; + +use anyhow::{anyhow, Result}; + +pub fn binutil(name: &str) -> Result { + let exe_suffix = std::env::consts::EXE_SUFFIX; + let exe = format!("llvm-{name}{exe_suffix}"); + + let path = llvm_tools::LlvmTools::new() + .map_err(|err| match err { + llvm_tools::Error::NotFound => anyhow!( + "Could not find llvm-tools component\n\ + \n\ + Maybe the rustup component `llvm-tools` is missing? Install it through: `rustup component add llvm-tools`" + ), + err => anyhow!("{err:?}"), + })? + .tool(&exe) + .ok_or_else(|| anyhow!("could not find {exe}"))?; + + Ok(path) +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 133db08239..af71cf868c 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -3,6 +3,7 @@ mod arch; mod archive; mod artifact; +mod binutil; mod build; mod cargo_build; mod ci; @@ -11,6 +12,7 @@ mod clippy; use std::path::Path; use anyhow::Result; +pub(crate) use binutil::binutil; use clap::Parser; use xshell::Shell;