Skip to content

Commit

Permalink
Merge pull request #55 from MarcelStruckWO/feature-flags
Browse files Browse the repository at this point in the history
Add flags to pass through features to cargo
  • Loading branch information
antoniusnaumann authored Apr 15, 2024
2 parents ae162f7 + 3290825 commit 5b143ec
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 41 deletions.
59 changes: 33 additions & 26 deletions Cargo.lock

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

21 changes: 17 additions & 4 deletions src/commands/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ use execute::{command, Execute};
use indicatif::MultiProgress;

use crate::bindings::generate_bindings;
use crate::console::*;
use crate::console::{run_step, run_step_with_commands};
use crate::lib_type::LibType;
use crate::metadata::{metadata, MetadataExt};
use crate::swiftpackage::{create_swiftpackage, recreate_output_dir};
use crate::targets::*;
use crate::xcframework::create_xcframework;
use crate::console::*;

#[derive(ValueEnum, Debug, Clone)]
#[value()]
Expand Down Expand Up @@ -48,13 +48,22 @@ impl From<LibTypeArg> for Option<LibType> {
}
}

#[derive(Debug, Clone)]
pub struct FeatureOptions {
pub features: Option<Vec<String>>,
pub all_features: bool,
pub no_default_features: bool,
}

#[allow(clippy::too_many_arguments)]
pub fn run(
platforms: Option<Vec<Platform>>,
package_name: Option<String>,
disable_warnings: bool,
config: Config,
mode: Mode,
lib_type_arg: LibTypeArg,
features: FeatureOptions,
skip_toolchains_check: bool,
) -> Result<()> {
// TODO: Allow path as optional argument to take other directories than current directory
Expand All @@ -72,6 +81,7 @@ pub fn run(
&config,
mode,
lib_type_arg,
features,
skip_toolchains_check,
);
} else if package_name.is_some() {
Expand All @@ -90,6 +100,7 @@ pub fn run(
&config,
mode,
lib_type_arg.clone(),
features.clone(),
skip_toolchains_check,
)
})
Expand All @@ -107,6 +118,7 @@ fn run_for_crate(
config: &Config,
mode: Mode,
lib_type_arg: LibTypeArg,
features: FeatureOptions,
skip_toolchains_check: bool,
) -> Result<()> {
let lib = current_crate
Expand All @@ -123,7 +135,7 @@ fn run_for_crate(

if lib_type == LibType::Dynamic {
warning!(
&config,
&config,
"Building as dynamic library is discouraged. It might prevent apps that use this library from publishing to the App Store."
);
}
Expand Down Expand Up @@ -157,7 +169,7 @@ fn run_for_crate(

let crate_name = lib.name.replace('-', "_");
for target in &targets {
build_with_output(target, &crate_name, mode, lib_type, config)?;
build_with_output(target, &crate_name, mode, lib_type, config, &features)?;
}

generate_bindings_with_output(&targets, &crate_name, mode, lib_type, config)?;
Expand Down Expand Up @@ -372,8 +384,9 @@ fn build_with_output(
mode: Mode,
lib_type: LibType,
config: &Config,
features: &FeatureOptions,
) -> Result<()> {
let mut commands = target.commands(lib_name, mode, lib_type);
let mut commands = target.commands(lib_name, mode, lib_type, features);
for command in &mut commands {
command.env("CARGO_TERM_COLOR", "always");
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ mod targets;
mod templating;
mod xcframework;

pub use commands::*;
pub use crate::console::error::Result;
pub use crate::console::Config;
pub use commands::*;
pub use lib_type::LibType;
pub use targets::*;

Expand Down
23 changes: 22 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::process::ExitCode;

use cargo_swift::{init, package, Config, LibType, Mode};
use cargo_swift::{
init,
package::{self, FeatureOptions},
Config, LibType, Mode,
};
use clap::{Parser, Subcommand};

#[derive(Parser)]
Expand Down Expand Up @@ -88,6 +92,15 @@ enum Action {
#[arg(long)]
/// Disable toolchains check
skip_toolchains_check: bool,

#[arg(short = 'F', long, trailing_var_arg = true)]
features: Option<Vec<String>>,

#[arg(long)]
all_features: bool,

#[arg(long)]
no_default_features: bool,
},
}

Expand All @@ -111,13 +124,21 @@ fn main() -> ExitCode {
release,
lib_type,
skip_toolchains_check,
features,
all_features,
no_default_features,
} => package::run(
platforms,
package_name,
suppress_warnings,
config,
if release { Mode::Release } else { Mode::Debug },
lib_type,
FeatureOptions {
features,
all_features,
no_default_features,
},
skip_toolchains_check,
),
};
Expand Down
42 changes: 33 additions & 9 deletions src/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use nonempty::{nonempty, NonEmpty};

use crate::lib_type::LibType;
use crate::metadata::{metadata, MetadataExt};
use crate::package::FeatureOptions;

pub trait TargetInfo {
fn target(&self) -> Target;
Expand Down Expand Up @@ -41,15 +42,32 @@ impl Display for Mode {
}

impl Target {
fn cargo_build_commands(&self, mode: Mode) -> Vec<Command> {
let flag = match mode {
Mode::Debug => "",
Mode::Release => "--release",
};

fn cargo_build_commands(&self, mode: Mode, features: &FeatureOptions) -> Vec<Command> {
self.architectures()
.into_iter()
.map(|arch| command(format!("cargo build --target {arch} {flag}")))
.map(|arch| {
let mut cmd = command("cargo build");
cmd.arg("--target").arg(arch);

match mode {
Mode::Debug => {}
Mode::Release => {
cmd.arg("--release");
}
}

if let Some(features) = &features.features {
cmd.arg("--features").arg(features.join(","));
}
if features.all_features {
cmd.arg("--all-features");
}
if features.no_default_features {
cmd.arg("--no-default-features");
}

cmd
})
.collect()
}

Expand Down Expand Up @@ -96,8 +114,14 @@ impl Target {
///
/// This function returns a list of commands that should be executed in their given
/// order to build this target (and bundle architecture targets with lipo if it is a universal target).
pub fn commands(&self, lib_name: &str, mode: Mode, lib_type: LibType) -> Vec<Command> {
self.cargo_build_commands(mode)
pub fn commands(
&self,
lib_name: &str,
mode: Mode,
lib_type: LibType,
features: &FeatureOptions,
) -> Vec<Command> {
self.cargo_build_commands(mode, features)
.into_iter()
.chain(self.lipo_commands(lib_name, mode, lib_type))
.chain(self.rpath_install_id_commands(lib_name, mode, lib_type))
Expand Down

0 comments on commit 5b143ec

Please sign in to comment.