Skip to content

Commit

Permalink
Update subcommand.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvc94ch committed Mar 8, 2022
1 parent aa3195d commit 90e6171
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 79 deletions.
3 changes: 2 additions & 1 deletion cargo-apk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ repository = "https://github.com/rust-windowing/android-ndk-rs"

[dependencies]
anyhow = "1.0.38"
cargo-subcommand = "0.5.0"
cargo-subcommand = { git = "https://github.com/dvc94ch/cargo-subcommand", branch = "refactoring" }
clap = { version = "3.1.6", features = ["derive"] }
dunce = "1.0.1"
env_logger = "0.8.2"
log = "0.4.14"
Expand Down
23 changes: 10 additions & 13 deletions cargo-apk/src/apk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl<'a> ApkBuilder<'a> {
if self.cmd.target().is_none() {
cargo.arg("--target").arg(triple);
}
cargo.args(self.cmd.args());
self.cmd.args().apply(&mut cargo);
if !cargo.status()?.success() {
return Err(NdkError::CmdFailed(cargo).into());
}
Expand Down Expand Up @@ -145,19 +145,17 @@ impl<'a> ApkBuilder<'a> {

for target in &self.build_targets {
let triple = target.rust_triple();
let build_dir = dunce::simplified(self.cmd.target_dir())
.join(triple)
.join(self.cmd.profile());
let artifact = build_dir
.join(artifact)
.join(artifact.file_name(CrateType::Cdylib, triple));
let build_dir = self.cmd.build_dir(Some(triple));
let artifact = self
.cmd
.artifact(artifact, Some(triple), CrateType::Cdylib);

let mut cargo = cargo_ndk(&config.ndk, *target, self.min_sdk_version())?;
cargo.arg("rustc");
if self.cmd.target().is_none() {
cargo.arg("--target").arg(triple);
}
cargo.args(self.cmd.args());
self.cmd.args().apply(&mut cargo);

// Workaround for https://github.com/rust-windowing/android-ndk-rs/issues/149:
// Rust (1.56 as of writing) still requires libgcc during linking, but this does
Expand All @@ -166,9 +164,7 @@ impl<'a> ApkBuilder<'a> {
// is still required even after replacing it with libunwind in the source.
// XXX: Add an upper-bound on the Rust version whenever this is not necessary anymore.
if self.ndk.build_tag() > 7272597 {
if !self.cmd.args().contains(&"--".to_owned()) {
cargo.arg("--");
}
cargo.arg("--");
let cargo_apk_link_dir = self
.cmd
.target_dir()
Expand Down Expand Up @@ -223,11 +219,12 @@ impl<'a> ApkBuilder<'a> {
Ok(())
}

pub fn default(&self) -> Result<(), Error> {
pub fn default(&self, task: &str) -> Result<(), Error> {
let ndk = Ndk::from_env()?;
for target in &self.build_targets {
let mut cargo = cargo_ndk(&ndk, *target, self.min_sdk_version())?;
cargo.args(self.cmd.args());
cargo.arg(task);
self.cmd.args().apply(&mut cargo);
if !cargo.status()?.success() {
return Err(NdkError::CmdFailed(cargo).into());
}
Expand Down
123 changes: 58 additions & 65 deletions cargo-apk/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,82 +1,75 @@
use cargo_apk::{ApkBuilder, Error};
use cargo_subcommand::Subcommand;
use std::process::Command;
use cargo_subcommand::{Args, Subcommand};
use clap::Parser;

#[derive(Parser)]
struct Cmd {
#[clap(subcommand)]
apk: ApkCmd,
}

#[derive(clap::Subcommand)]
enum ApkCmd {
/// Helps cargo build apk's for android
Apk {
#[clap(subcommand)]
cmd: ApkSubCmd,
},
}

#[derive(clap::Subcommand)]
enum ApkSubCmd {
/// Checks that the current package builds without creating an apk
Check {
#[clap(flatten)]
args: Args,
},
/// Compiles the current package and creates an apk
Build {
#[clap(flatten)]
args: Args,
},
/// Run a binary or example of the local package
Run {
#[clap(flatten)]
args: Args,
},
/// Start a gdb session attached to an adb device with symbols loaded
Gdb {
#[clap(flatten)]
args: Args,
},
}

fn main() -> anyhow::Result<()> {
env_logger::init();
let args = std::env::args();
let cmd = Subcommand::new(args, "apk", |_, _| Ok(false)).map_err(Error::Subcommand)?;
let builder = ApkBuilder::from_subcommand(&cmd)?;

match cmd.cmd() {
"check" | "c" => builder.check()?,
"build" | "b" => {
let cmd = Cmd::parse();
let ApkCmd::Apk { cmd } = cmd.apk;
match cmd {
ApkSubCmd::Check { args } => {
let cmd = Subcommand::new(args)?;
let builder = ApkBuilder::from_subcommand(&cmd)?;
builder.check()?;
}
ApkSubCmd::Build { args } => {
let cmd = Subcommand::new(args)?;
let builder = ApkBuilder::from_subcommand(&cmd)?;
for artifact in cmd.artifacts() {
builder.build(artifact)?;
}
}
"run" | "r" => {
ApkSubCmd::Run { args } => {
let cmd = Subcommand::new(args)?;
let builder = ApkBuilder::from_subcommand(&cmd)?;
anyhow::ensure!(cmd.artifacts().len() == 1, Error::invalid_args());
builder.run(&cmd.artifacts()[0])?;
}
"--" => {
builder.default()?;
}
"gdb" => {
ApkSubCmd::Gdb { args } => {
let cmd = Subcommand::new(args)?;
let builder = ApkBuilder::from_subcommand(&cmd)?;
anyhow::ensure!(cmd.artifacts().len() == 1, Error::invalid_args());
builder.gdb(&cmd.artifacts()[0])?;
}
"help" => {
if let Some(arg) = cmd.args().get(0) {
match &**arg {
"build" | "b" | "check" | "c" | "run" | "r" | "test" | "t" | "doc" => {
run_cargo(&cmd)?
}
"gdb" => print_gdb_help(),
_ => print_help(),
}
} else {
print_help();
}
}
_ => print_help(),
}

Ok(())
}

fn run_cargo(cmd: &Subcommand) -> Result<(), Error> {
Command::new("cargo")
.arg(cmd.cmd())
.args(cmd.args())
.status()?;
Ok(())
}

fn print_help() {
println!(
r#"cargo-apk
Helps cargo build apk's for android
USAGE:
cargo apk [SUBCOMMAND]
SUBCOMMAND:
check, c Checks that the current package builds without creating an apk
build, b Compiles the current package and creates an apk
run, r Run a binary or example of the local package
gdb Start a gdb session attached to an adb device with symbols loaded
"#
);
}

fn print_gdb_help() {
println!(
r#"cargo-apk gdb
Start a gdb session attached to an adb device with symbols loaded
USAGE:
cargo apk gdb
"#
);
}

0 comments on commit 90e6171

Please sign in to comment.