From d002daa6b7cc0272a1f89d1e94f2f73adc043d11 Mon Sep 17 00:00:00 2001 From: KYovchevski Date: Wed, 30 Oct 2024 16:14:58 +0100 Subject: [PATCH] Allow for the build to automatically strip debug symbols from .so files --- xbuild/src/command/build.rs | 22 ++++++++++++++++++++++ xbuild/src/lib.rs | 12 +++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/xbuild/src/command/build.rs b/xbuild/src/command/build.rs index 98abe00..f164cd9 100644 --- a/xbuild/src/command/build.rs +++ b/xbuild/src/command/build.rs @@ -86,6 +86,28 @@ pub fn build(env: &BuildEnv) -> Result<()> { let cargo_dir = arch_dir.join("cargo"); let lib = env.cargo_artefact(&cargo_dir, &target, CrateType::Cdylib)?; + if env.strip_debug_symbols { + let filename = lib.file_name().unwrap(); + let unstripped_lib = lib.parent().unwrap().join("pre-strip").join(filename); + + if !dbg!(&unstripped_lib).exists() { + std::fs::create_dir_all(unstripped_lib.parent().unwrap()).unwrap(); + std::fs::File::create(&unstripped_lib).unwrap(); + } + std::fs::copy(dbg!(&lib), unstripped_lib) + .expect("Could not copy lib before stripping its debug symbols"); + + std::process::Command::new("strip") + // I'm told this should always be valid for Android, so use this as the target + .arg("--target=elf64-little") + .arg("--strip-all") + .arg(&lib) + .spawn() + .expect("Could not strip debug symbols from lib") + .wait() + .expect("Stripping of debug symbols from lib failed"); + } + let ndk = env.android_ndk(); let deps_dir = { diff --git a/xbuild/src/lib.rs b/xbuild/src/lib.rs index 0059f2d..3181bfc 100644 --- a/xbuild/src/lib.rs +++ b/xbuild/src/lib.rs @@ -442,6 +442,12 @@ pub struct BuildTargetArgs { /// Path to an api key. #[clap(long)] api_key: Option, + /// Enables stripping of debug symbols from the built .so files for Android. + /// The pre-strip versions are copied to a separate directory. + /// ## NOTE: + /// Only implemented in the Android path. + #[clap(long)] + strip_debug_symbols: bool, } impl BuildTargetArgs { @@ -548,6 +554,7 @@ impl BuildTargetArgs { provisioning_profile, api_key, android_gradle, + strip_debug_symbols: self.strip_debug_symbols, }) } } @@ -564,6 +571,7 @@ pub struct BuildTarget { provisioning_profile: Option>, api_key: Option, android_gradle: bool, + strip_debug_symbols: bool, } impl BuildTarget { @@ -627,6 +635,7 @@ pub struct BuildEnv { config: Config, verbose: bool, offline: bool, + strip_debug_symbols: bool, } impl BuildEnv { @@ -646,7 +655,6 @@ impl BuildEnv { .map(|icon| cargo.package_root().join(icon)); Ok(Self { name: package.name.clone(), - build_target, icon, cargo, config, @@ -654,6 +662,8 @@ impl BuildEnv { cache_dir, verbose, offline, + strip_debug_symbols: build_target.strip_debug_symbols, + build_target, }) }