Skip to content
This repository has been archived by the owner on Jan 8, 2022. It is now read-only.

Take cargo features into account through USE flags. #19

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/ebuild.template
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}}
43 changes: 43 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) -> CargoResult<Workspace> {
Expand Down Expand Up @@ -59,6 +60,46 @@ fn resolve<'a>(
Ok((packages, resolve))
}

fn all_features(package: &Package) -> &HashMap<String, Vec<String>> {
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<String> {
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()?;
Expand Down Expand Up @@ -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());
Expand Down