From a7d30fee8f2f04db86780f17b562d31446661b91 Mon Sep 17 00:00:00 2001 From: Martin Vassor Date: Tue, 6 Nov 2018 21:36:44 +0100 Subject: [PATCH] Take cargo features into account through USE flags. --- src/ebuild.template | 11 ++++++++++- src/lib.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/ebuild.template b/src/ebuild.template index 2ac9447..7a244fe 100644 --- a/src/ebuild.template +++ b/src/ebuild.template @@ -17,7 +17,16 @@ RESTRICT="mirror" LICENSE="{license}" # Update to proper Gentoo format SLOT="0" KEYWORDS="~amd64" -IUSE="" +IUSE="{iuse}" DEPEND="" RDEPEND="" + +src_compile() {{ + cargo build --no-default-features \ + --features {cargo_features}\ + -j $(makeopts_jobs)\ + $(usex debug "" --release) \ + --verbose \ + || die "Cargo build failed" +}} diff --git a/src/lib.rs b/src/lib.rs index 41f9bcc..56f6da1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,7 @@ use cargo::{CliResult, Config}; use std::fs::OpenOptions; use std::io::Write; use std::path::PathBuf; +use std::collections::HashMap; /// Finds the root Cargo.toml of the workspace fn workspace(config: &Config, manifest_path: Option) -> CargoResult { @@ -59,6 +60,46 @@ fn resolve<'a>( Ok((packages, resolve)) } +fn all_features(package: &Package) -> &HashMap> { + package.manifest() + .summary() + .features() +} + +fn features_no_default(package: &Package) -> Vec<&String> { + all_features(package).keys() + .filter(|k| **k != "default".to_string()) + .collect() +} + +fn default_features(package: &Package) -> Vec { + all_features(package) + .get("default") + .unwrap_or(&vec!["".to_string()]) + .to_vec() +} + +fn feature_to_iuse(package: &Package) -> String { + let mut s = "".to_string(); + let defaults = default_features(package); + for feature in features_no_default(package) { + if defaults.contains(feature) { + s += "+"; + } + s += feature; + s += " "; + } + s +} + +fn feature_to_usex(package: &Package) -> String { + let mut s = "".to_string(); + for feature in features_no_default(package) { + s += &format!("$(usex {} \"{},\" \"\")", feature, feature); + } + s +} + pub fn run(verbose: u32, quiet: bool) -> CliResult { // create a default Cargo config let config = Config::default()?; @@ -138,6 +179,8 @@ pub fn run(verbose: u32, quiet: bool) -> CliResult { crates = crates.join(""), cargo_ebuild_ver = env!("CARGO_PKG_VERSION"), this_year = 1900 + time::now().tm_year, + iuse = feature_to_iuse(package), + cargo_features = feature_to_usex(package), ).chain_err(|| "unable to write ebuild to disk")?; println!("Wrote: {}", ebuild_path.display());