Skip to content

Commit

Permalink
Add --profile build argument
Browse files Browse the repository at this point in the history
Fixes: #190
  • Loading branch information
Johennes committed Dec 19, 2024
1 parent 0ea522e commit dd20e43
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 28 deletions.
10 changes: 5 additions & 5 deletions crates/ubrn_cli/src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,13 @@ impl AndroidArgs {
.arg(target.to_string())
.arg("--platform")
.arg(format!("{}", api_level));
if !self.common_args.release {
if self.common_args.profile() != "release" {
cmd.arg("--no-strip");
}
cmd.arg("--").arg("build");
if self.common_args.release {
cmd.arg("--release");
}
cmd.arg("--")
.arg("build")
.arg("--profile")
.arg(self.common_args.profile());
cmd.args(cargo_extras.clone());
run_cmd(cmd.current_dir(rust_dir))?;
Ok(target.clone())
Expand Down
10 changes: 8 additions & 2 deletions crates/ubrn_cli/src/building.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ pub(crate) struct CommonBuildArgs {
#[clap(long, short, default_value = "false")]
pub(crate) release: bool,

/// Use a specific build profile
///
/// This overrides the -r / --release flag if both are specified.
#[clap(long, short)]
pub(crate) profile: Option<String>,

/// If the Rust library has been built for at least one target, then
/// don't re-run cargo build.
///
Expand All @@ -101,8 +107,8 @@ pub(crate) struct CommonBuildArgs {
}

impl CommonBuildArgs {
pub(crate) fn profile<'a>(&self) -> &'a str {
CrateMetadata::profile(self.release)
pub(crate) fn profile(&self) -> &str {
CrateMetadata::profile(self.profile.as_deref(), self.release)
}
}

Expand Down
7 changes: 3 additions & 4 deletions crates/ubrn_cli/src/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,9 @@ impl IOsArgs {
.arg("--manifest-path")
.arg(manifest_path)
.arg("--target")
.arg(&target.triple);
if self.common_args.release {
cmd.arg("--release");
}
.arg(&target.triple)
.arg("--profile")
.arg(self.common_args.profile());
cmd.args(cargo_extras.clone());
run_cmd(cmd.current_dir(rust_dir))?;
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions crates/ubrn_common/src/rust_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ pub struct CrateMetadata {
}

impl CrateMetadata {
pub fn profile<'a>(release: bool) -> &'a str {
if release {
pub fn profile(profile: Option<&str>, release: bool) -> &str {
profile.unwrap_or(if release {
"release"
} else {
"debug"
}
})
}

pub fn library_path(&self, target: Option<&str>, profile: &str) -> Utf8PathBuf {
Expand Down
20 changes: 15 additions & 5 deletions docs/src/reference/commandline.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ This takes care of the work of compiling the Rust, ready for generating bindings
- `--and-generate` this runs the `generate all` command immediately after building.
- `--targets` a comma separated list of targets, specific to each platform. This overrides the values in the config file.
- `--release` builds a release version of the library.
- `--profile` uses a specific build profile, overriding --release if necessary

## `build android`

Expand All @@ -77,13 +78,18 @@ Options:
-t, --targets <TARGETS>...
Comma separated list of targets, that override the values in the `config.yaml` file.
Android: aarch64-linux-android, armv7-linux-androideabi, x86_64-linux-android i686-linux-android,
Android: aarch64-linux-android,armv7-linux-androideabi,x86_64-linux-android,i686-linux-android,
Synonyms for: arm64-v8a, armeabi-v7a, x86_64, x86
Synonyms for: arm64-v8a,armeabi-v7a,x86_64,x86
-r, --release
Build a release build
-p, --profile <PROFILE>
Use a specific build profile
This overrides the -r / --release flag if both are specified.
--no-cargo
If the Rust library has been built for at least one target, then don't re-run cargo build.
Expand Down Expand Up @@ -132,9 +138,8 @@ You can find the version you need in your react-native `android/build.gradle` fi
## `build ios`

Build the crate for use on an iOS device or simulator.
```
Build the crate for use on an iOS device or simulator

```
Usage: uniffi-bindgen-react-native build ios [OPTIONS] --config <CONFIG>
Options:
Expand All @@ -155,11 +160,16 @@ Options:
-t, --targets <TARGETS>...
Comma separated list of targets, that override the values in the `config.yaml` file.
iOS: aarch64-apple-ios, aarch64-apple-ios-sim, x86_64-apple-ios
iOS: aarch64-apple-ios,aarch64-apple-ios-sim,x86_64-apple-ios
-r, --release
Build a release build
-p, --profile <PROFILE>
Use a specific build profile
This overrides the -r / --release flag if both are specified.
--no-cargo
If the Rust library has been built for at least one target, then don't re-run cargo build.
Expand Down
22 changes: 13 additions & 9 deletions xtask/src/run/rust_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ pub(crate) struct CrateArg {
#[clap(long, requires = "crate_dir", default_value = "false")]
pub(crate) release: bool,

/// Use a specific build profile
///
/// This overrides the release flag if both are specified.
#[clap(long, requires = "crate_dir")]
pub(crate) profile: Option<String>,

/// Do not invoke cargo build.
///
/// This is useful when invoking from within a test.
Expand All @@ -31,29 +37,27 @@ pub(crate) struct CrateArg {
impl CrateArg {
pub(crate) fn cargo_build(&self, clean: bool) -> Result<CrateMetadata> {
let metadata = CrateMetadata::try_from(self.crate_dir.clone().expect("crate has no path"))?;
let profile = CrateMetadata::profile(self.release);
let lib_path = metadata.library_path(None, profile);
let lib_path = metadata.library_path(None, self.profile());
if lib_path.exists() && clean {
metadata.cargo_clean()?;
}
if !lib_path.exists() || !self.no_cargo {
cargo_build(&metadata, self.release)?;
cargo_build(&metadata, self.profile())?;
}
Ok(metadata)
}

pub(crate) fn profile(&self) -> &str {
CrateMetadata::profile(self.release)
CrateMetadata::profile(self.profile.as_deref(), self.release)
}
}

fn cargo_build(metadata: &CrateMetadata, release: bool) -> Result<()> {
fn cargo_build(metadata: &CrateMetadata, profile: &str) -> Result<()> {
let mut cmd = Command::new("cargo");
cmd.current_dir(metadata.crate_dir());
cmd.arg("build");
if release {
cmd.arg("--release");
}
cmd.arg("build")
.arg("--profile")
.arg(profile);
run_cmd_quietly(&mut cmd)?;
Ok(())
}

0 comments on commit dd20e43

Please sign in to comment.