From ef743af119f9f244d47d3857c33c92ed2e217020 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=9D=B0=E5=8F=8B=20Jieyou=20Xu=20=28Joe=29?= <39484203+jieyouxu@users.noreply.github.com> Date: Fri, 18 Oct 2024 17:34:11 +0800 Subject: [PATCH] run-make-support: add `llvm-dis` and `llvm-objcopy` --- .../src/external_deps/llvm.rs | 66 +++++++++++++++++++ src/tools/run-make-support/src/lib.rs | 11 ++-- 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/src/tools/run-make-support/src/external_deps/llvm.rs b/src/tools/run-make-support/src/external_deps/llvm.rs index 38a9ac923b4..9a6e35da3fe 100644 --- a/src/tools/run-make-support/src/external_deps/llvm.rs +++ b/src/tools/run-make-support/src/external_deps/llvm.rs @@ -60,6 +60,18 @@ pub fn llvm_pdbutil() -> LlvmPdbutil { LlvmPdbutil::new() } +/// Construct a new `llvm-dis` invocation. This assumes that `llvm-dis` is available +/// at `$LLVM_BIN_DIR/llvm-dis`. +pub fn llvm_dis() -> LlvmDis { + LlvmDis::new() +} + +/// Construct a new `llvm-objcopy` invocation. This assumes that `llvm-objcopy` is available +/// at `$LLVM_BIN_DIR/llvm-objcopy`. +pub fn llvm_objcopy() -> LlvmObjcopy { + LlvmObjcopy::new() +} + /// A `llvm-readobj` invocation builder. #[derive(Debug)] #[must_use] @@ -123,6 +135,20 @@ pub struct LlvmPdbutil { cmd: Command, } +/// A `llvm-dis` invocation builder. +#[derive(Debug)] +#[must_use] +pub struct LlvmDis { + cmd: Command, +} + +/// A `llvm-objcopy` invocation builder. +#[derive(Debug)] +#[must_use] +pub struct LlvmObjcopy { + cmd: Command, +} + crate::macros::impl_common_helpers!(LlvmReadobj); crate::macros::impl_common_helpers!(LlvmProfdata); crate::macros::impl_common_helpers!(LlvmFilecheck); @@ -132,6 +158,8 @@ pub struct LlvmPdbutil { crate::macros::impl_common_helpers!(LlvmBcanalyzer); crate::macros::impl_common_helpers!(LlvmDwarfdump); crate::macros::impl_common_helpers!(LlvmPdbutil); +crate::macros::impl_common_helpers!(LlvmDis); +crate::macros::impl_common_helpers!(LlvmObjcopy); /// Generate the path to the bin directory of LLVM. #[must_use] @@ -390,3 +418,41 @@ pub fn input>(&mut self, path: P) -> &mut Self { self } } + +impl LlvmObjcopy { + /// Construct a new `llvm-objcopy` invocation. This assumes that `llvm-objcopy` is available + /// at `$LLVM_BIN_DIR/llvm-objcopy`. + pub fn new() -> Self { + let llvm_objcopy = llvm_bin_dir().join("llvm-objcopy"); + let cmd = Command::new(llvm_objcopy); + Self { cmd } + } + + /// Dump the contents of `section` into the file at `path`. + #[track_caller] + pub fn dump_section, P: AsRef>( + &mut self, + section_name: S, + path: P, + ) -> &mut Self { + self.cmd.arg("--dump-section"); + self.cmd.arg(format!("{}={}", section_name.as_ref(), path.as_ref().to_str().unwrap())); + self + } +} + +impl LlvmDis { + /// Construct a new `llvm-dis` invocation. This assumes that `llvm-dis` is available + /// at `$LLVM_BIN_DIR/llvm-dis`. + pub fn new() -> Self { + let llvm_dis = llvm_bin_dir().join("llvm-dis"); + let cmd = Command::new(llvm_dis); + Self { cmd } + } + + /// Provide an input file. + pub fn input>(&mut self, path: P) -> &mut Self { + self.cmd.arg(path.as_ref()); + self + } +} diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index 15d813ccf53..368b98c9f0d 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -49,14 +49,17 @@ pub mod rfs { // These rely on external dependencies. pub use cc::{cc, cxx, extra_c_flags, extra_cxx_flags, Cc}; -pub use c_build::{build_native_dynamic_lib, build_native_static_lib, build_native_static_lib_optimized, build_native_static_lib_cxx}; +pub use c_build::{ + build_native_dynamic_lib, build_native_static_lib, build_native_static_lib_cxx, + build_native_static_lib_optimized, +}; pub use cargo::cargo; pub use clang::{clang, Clang}; pub use htmldocck::htmldocck; pub use llvm::{ - llvm_ar, llvm_bcanalyzer, llvm_dwarfdump, llvm_filecheck, llvm_nm, llvm_objdump, llvm_profdata, - llvm_readobj, LlvmAr, LlvmBcanalyzer, LlvmDwarfdump, LlvmFilecheck, LlvmNm, LlvmObjdump, - LlvmProfdata, LlvmReadobj, + llvm_ar, llvm_bcanalyzer, llvm_dis, llvm_dwarfdump, llvm_filecheck, llvm_nm, llvm_objcopy, + llvm_objdump, llvm_profdata, llvm_readobj, LlvmAr, LlvmBcanalyzer, LlvmDis, LlvmDwarfdump, + LlvmFilecheck, LlvmNm, LlvmObjcopy, LlvmObjdump, LlvmProfdata, LlvmReadobj, }; pub use python::python_command; pub use rustc::{aux_build, bare_rustc, rustc, rustc_path, Rustc};