Skip to content

Commit

Permalink
Fix selection of output libs to copy
Browse files Browse the repository at this point in the history
Change the way output binaries are selected, as the current
implementation relies on CLI args that might change easily and don't
cover all possible scenarios.

Instead, the already used `cargo_metadata` can parse the diagnostic
output from Cargo which reports any artifacts produced, giving a
reliable list of any produced file of interest.
  • Loading branch information
dnaka91 authored and bbqsrc committed Aug 21, 2024
1 parent b422f28 commit 9dc638c
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 124 deletions.
103 changes: 79 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ maintenance = { status = "actively-developed" }
anyhow = "1.0.75"
cargo_metadata = "0.18.1"
dunce = "1.0.4"
filetime = "0.2.24"
gumdrop = "0.8.1"
is-terminal = "0.4.9"
libc = "0.2.147"
Expand Down
35 changes: 31 additions & 4 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ use std::{
collections::BTreeMap,
env,
ffi::OsString,
io::BufReader,
path::{Path, PathBuf},
process::Command,
process::{Command, Stdio},
};

use cargo_metadata::{camino::Utf8PathBuf, semver::Version};
use anyhow::{Context, Result};
use cargo_metadata::{camino::Utf8PathBuf, semver::Version, Artifact, Message};

use crate::shell::Shell;

Expand Down Expand Up @@ -203,7 +205,7 @@ pub(crate) fn run(
cargo_manifest: &Path,
bindgen: bool,
#[allow(unused_variables)] out_dir: &Utf8PathBuf,
) -> std::process::ExitStatus {
) -> Result<(std::process::ExitStatus, Vec<Artifact>)> {
if version.major < 23 {
shell.error("NDK versions less than r23 are not supported. Install an up-to-date version of the NDK.").unwrap();
std::process::exit(1);
Expand Down Expand Up @@ -259,7 +261,32 @@ pub(crate) fn run(
cargo_args.insert(arg_insertion_position, triple.into());
cargo_args.insert(arg_insertion_position, "--target".into());

cargo_cmd.args(cargo_args).status().expect("cargo crashed")
cargo_args.insert(arg_insertion_position, "json-render-diagnostics".into());
cargo_args.insert(arg_insertion_position, "--message-format".into());

let mut child = cargo_cmd
.args(cargo_args)
.stdin(Stdio::inherit())
.stderr(Stdio::inherit())
.stdout(Stdio::piped())
.spawn()
.context("failed spawning cargo process")?;

let reader = BufReader::new(child.stdout.take().context("no stdout available")?);
let mut artifacts = Vec::new();

for msg in Message::parse_stream(reader) {
match msg? {
Message::CompilerArtifact(artifact) => artifacts.push(artifact),
Message::CompilerMessage(msg) => println!("{msg}"),
Message::TextLine(line) => println!("{line}"),
_ => {}
}
}

let status = child.wait().context("cargo crashed")?;

Ok((status, artifacts))
}

pub(crate) fn strip(ndk_home: &Path, bin_path: &Path) -> std::process::ExitStatus {
Expand Down
Loading

0 comments on commit 9dc638c

Please sign in to comment.