Skip to content

Commit

Permalink
copy only the libraries that were built
Browse files Browse the repository at this point in the history
  • Loading branch information
mightyguava authored and bbqsrc committed May 13, 2024
1 parent c6e65ef commit 5045be6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
51 changes: 25 additions & 26 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ pub fn run(args: Vec<String>) -> anyhow::Result<()> {
.or_else(|| {
if let Some(selected_package) = cargo_args
.iter()
.position(|arg| arg == "-p")
.position(|arg| arg == "-p" || arg == "--package")
.and_then(|idx| cargo_args.get(idx + 1))
{
let selected_package = metadata
Expand Down Expand Up @@ -676,49 +676,48 @@ pub fn run(args: Vec<String>) -> anyhow::Result<()> {

let dir = out_dir.join(target.triple()).join(build_mode.to_string());

let so_files = match fs::read_dir(&dir) {
let lib_filename = format!("lib{}.so", config.lib_name);
let so_file = match fs::read_dir(&dir) {
Ok(dir) => dir
.filter_map(Result::ok)
.map(|x| x.path())
.filter(|x| x.extension() == Some(OsStr::new("so")))
.collect::<Vec<_>>(),
.find(|x| x.file_name() == Some(OsStr::new(lib_filename.as_str()))),
Err(e) => {
shell.error(format!("Could not read directory: {:?}", dir))?;
shell.error(e)?;
std::process::exit(1);
}
};

if so_files.is_empty() {
shell.error(format!("No .so files found in path {:?}", dir))?;
if so_file.is_none() {
shell.error(format!("{:?} file not found in path {:?}", lib_filename, dir))?;
shell.error("Did you set the crate-type in Cargo.toml to include 'cdylib'?")?;
shell.error("For more info, see <https://doc.rust-lang.org/cargo/reference/cargo-targets.html#library>.")?;
std::process::exit(1);
}

for so_file in so_files {
let dest = arch_output_dir.join(so_file.file_name().unwrap());
let so_file = so_file.unwrap();

let dest = arch_output_dir.join(so_file.file_name().unwrap());
shell.verbose(|shell| {
shell.status(
"Copying",
format!(
"{} -> {}",
&dunce::canonicalize(&so_file).unwrap().display(),
&dest.display()
),
)
})?;
fs::copy(so_file, &dest).unwrap();

if !args.no_strip {
shell.verbose(|shell| {
shell.status(
"Copying",
format!(
"{} -> {}",
&dunce::canonicalize(&so_file).unwrap().display(),
&dest.display()
),
"Stripping",
format!("{}", &dunce::canonicalize(&dest).unwrap().display()),
)
})?;
fs::copy(so_file, &dest).unwrap();

if !args.no_strip {
shell.verbose(|shell| {
shell.status(
"Stripping",
format!("{}", &dunce::canonicalize(&dest).unwrap().display()),
)
})?;
let _ = crate::cargo::strip(&ndk_home, &dest);
}
let _ = crate::cargo::strip(&ndk_home, &dest);
}
}
}
Expand Down
17 changes: 12 additions & 5 deletions src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ fn default_targets() -> Vec<Target> {

#[derive(Debug, Deserialize)]
struct CargoToml {
package: Option<Package>,
package: Package,
lib: Option<Lib>,
}

#[derive(Debug, Deserialize)]
struct Package {
name: String,
metadata: Option<Metadata>,
}

Expand All @@ -29,6 +31,11 @@ struct Metadata {
ndk: Option<Ndk>,
}

#[derive(Debug, Deserialize)]
struct Lib {
name: Option<String>
}

#[derive(Debug, Deserialize)]
pub(crate) struct Ndk {
#[serde(default = "default_platform")]
Expand Down Expand Up @@ -59,6 +66,7 @@ struct NdkTarget {

#[derive(Debug)]
pub struct Config {
pub lib_name: String,
pub platform: u8,
pub targets: Vec<Target>,
}
Expand All @@ -68,6 +76,7 @@ impl Default for Config {
Self {
platform: Ndk::default().platform,
targets: default_targets(),
lib_name: String::new(),
}
}
}
Expand Down Expand Up @@ -134,10 +143,7 @@ pub(crate) fn config(
let toml_string = std::fs::read_to_string(cargo_toml_path)?;
let cargo_toml: CargoToml = toml::from_str(&toml_string)?;

let package = match cargo_toml.package {
Some(v) => v,
None => return Ok(crate::meta::Config::default()),
};
let package = cargo_toml.package;

let ndk = package.metadata.and_then(|x| x.ndk).unwrap_or_default();
let base_targets = ndk.targets;
Expand All @@ -149,6 +155,7 @@ pub(crate) fn config(
};

Ok(Config {
lib_name: cargo_toml.lib.and_then(|x| x.name).unwrap_or(package.name).replace("-", "_"),
platform: ndk.platform,
targets,
})
Expand Down

0 comments on commit 5045be6

Please sign in to comment.