Skip to content

Commit

Permalink
Add flags to pass through features to cargo
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelStruckWO committed Apr 15, 2024
1 parent ae162f7 commit 79a2ba6
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 15 deletions.
22 changes: 17 additions & 5 deletions src/commands/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,21 @@ impl From<LibTypeArg> for Option<LibType> {
}
}

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

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 +80,7 @@ pub fn run(
&config,
mode,
lib_type_arg,
features,
skip_toolchains_check,
);
} else if package_name.is_some() {
Expand All @@ -90,6 +99,7 @@ pub fn run(
&config,
mode,
lib_type_arg.clone(),
features.clone(),
skip_toolchains_check,
)
})
Expand All @@ -107,6 +117,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 Down Expand Up @@ -157,7 +168,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 @@ -349,7 +360,7 @@ fn generate_bindings_with_output(
lib_name: &str,
mode: Mode,
lib_type: LibType,
config: &Config,
config: &Config
) -> Result<()> {
run_step(config, "Generating Swift bindings...", || {
let lib_file = library_file_name(lib_name, lib_type);
Expand All @@ -366,14 +377,15 @@ fn generate_bindings_with_output(
})
}

fn build_with_output(
fn build_with_output(
target: &Target,
lib_name: &str,
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
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 79a2ba6

Please sign in to comment.