Skip to content

Commit

Permalink
Allow for the build to automatically strip debug symbols from .so files
Browse files Browse the repository at this point in the history
  • Loading branch information
KYovchevski committed Oct 30, 2024
1 parent 7353c59 commit d002daa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
22 changes: 22 additions & 0 deletions xbuild/src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
12 changes: 11 additions & 1 deletion xbuild/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,12 @@ pub struct BuildTargetArgs {
/// Path to an api key.
#[clap(long)]
api_key: Option<PathBuf>,
/// 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 {
Expand Down Expand Up @@ -548,6 +554,7 @@ impl BuildTargetArgs {
provisioning_profile,
api_key,
android_gradle,
strip_debug_symbols: self.strip_debug_symbols,
})
}
}
Expand All @@ -564,6 +571,7 @@ pub struct BuildTarget {
provisioning_profile: Option<Vec<u8>>,
api_key: Option<PathBuf>,
android_gradle: bool,
strip_debug_symbols: bool,
}

impl BuildTarget {
Expand Down Expand Up @@ -627,6 +635,7 @@ pub struct BuildEnv {
config: Config,
verbose: bool,
offline: bool,
strip_debug_symbols: bool,
}

impl BuildEnv {
Expand All @@ -646,14 +655,15 @@ impl BuildEnv {
.map(|icon| cargo.package_root().join(icon));
Ok(Self {
name: package.name.clone(),
build_target,
icon,
cargo,
config,
build_dir,
cache_dir,
verbose,
offline,
strip_debug_symbols: build_target.strip_debug_symbols,
build_target,
})
}

Expand Down

0 comments on commit d002daa

Please sign in to comment.