From c69f74172e11b68dbb83f8545a749115a0bf3602 Mon Sep 17 00:00:00 2001 From: Christian Meusel Date: Tue, 10 May 2022 16:27:55 +0200 Subject: [PATCH 1/2] Add support for specifying Cargo features This adds the command line options to enable and disable Cargo features when generating recipes for https://github.com/meta-rust/meta-rust/pull/382. --- src/bitbake.template | 2 ++ src/main.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/bitbake.template b/src/bitbake.template index 86e3c2d..fa5a70a 100644 --- a/src/bitbake.template +++ b/src/bitbake.template @@ -13,6 +13,8 @@ S = "${{WORKDIR}}/git" CARGO_SRC_DIR = "{project_rel_dir}" {git_srcpv} +{features} + # please note if you have entries that do not begin with crate:// # you must change them to how that package can be fetched SRC_URI += " \ diff --git a/src/main.rs b/src/main.rs index 7f298ff..dace5c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -140,6 +140,16 @@ struct Args { /// Legacy Overrides: Use legacy override syntax #[structopt(short = "l", long = "--legacy-overrides")] legacy_overrides: bool, + + /// Space or comma separated list of Cargo features to activate + #[structopt(long = "--features")] + features: Option, + /// Activate all available Cargo features + #[structopt(long = "--all-features")] + all_features: bool, + /// Do not activate the Cargo `default` features + #[structopt(long = "--no-default-features")] + no_default_features: bool, } #[derive(StructOpt, Debug)] @@ -401,6 +411,7 @@ fn real_main(options: Args, config: &mut Config) -> CliResult { project_src_uri = project_repo.uri, project_src_rev = project_repo.rev, git_srcpv = git_srcpv, + features = feature_variable_assignments(&options).join("\n"), cargo_bitbake_ver = env!("CARGO_PKG_VERSION"), ) .map_err(|e| anyhow!("Unable to write to bitbake recipe file with: {}", e))?; @@ -409,3 +420,29 @@ fn real_main(options: Args, config: &mut Config) -> CliResult { Ok(()) } + +/// Returns a vector of variable assignments for the feature selection given +/// through `options`. This vector may come in handy for not generating many +/// empty lines in the actual output in case that no feature selection is made +/// at all. +/// +/// # Arguments +/// +/// * `options` - The program arguments with the current feature selection +fn feature_variable_assignments(options: &Args) -> Vec { + let mut output = Vec::new(); + + if let Some(features) = &options.features { + output.push(format!("CARGO_FEATURES = \"{}\"", features)); + } + + if options.all_features { + output.push(String::from("CARGO_ALL_FEATURES = \"1\"")); + } + + if options.no_default_features { + output.push(String::from("CARGO_NO_DEFAULT_FEATURES = \"1\"")); + } + + output +} From 368a63d6343df5203fba0c5b989a513922064ea5 Mon Sep 17 00:00:00 2001 From: Christian Meusel Date: Tue, 10 May 2022 16:44:10 +0200 Subject: [PATCH 2/2] Fix two small issues pointed out by Clippy --- src/git.rs | 2 +- src/main.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/git.rs b/src/git.rs index eade79d..fa6699f 100644 --- a/src/git.rs +++ b/src/git.rs @@ -56,7 +56,7 @@ pub fn git_to_yocto_git_url(url: &str, name: Option<&str>, prefix: GitPrefix) -> // check if its a git@github.com:cardoe/cargo-bitbake.git style URL // and fix it up if it is let fixed_url = if SSH_STYLE_REMOTE.is_match(url) { - format!("ssh://{}", url.replace(":", "/")) + format!("ssh://{}", url.replace(':', "/")) } else { url.to_string() }; diff --git a/src/main.rs b/src/main.rs index dace5c1..1d92e4c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -309,7 +309,7 @@ fn real_main(options: Args, config: &mut Config) -> CliResult { println!("No package.description set in your Cargo.toml, using package.name"); package.name() }, - |s| cargo::util::interning::InternedString::new(&s.trim().replace("\n", " \\\n")), + |s| cargo::util::interning::InternedString::new(&s.trim().replace('\n', " \\\n")), ); // package homepage (or source code location)