Skip to content

Commit

Permalink
add support for cargo --features
Browse files Browse the repository at this point in the history
  • Loading branch information
gnzlbg committed Apr 3, 2018
1 parent fad3c9c commit dd67164
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cargo-asm-test/lib_crate/Cargo.toml
Expand Up @@ -3,4 +3,6 @@ name = "lib_crate"
version = "0.1.0"
authors = ["gnzlbg <gonzalobg88@gmail.com>"]

[dependencies]
[features]
tiger = []
cat = []
9 changes: 9 additions & 0 deletions cargo-asm-test/lib_crate/src/bar.rs
Expand Up @@ -43,3 +43,12 @@ pub fn double_n(mut x: usize, mut n: usize) -> usize {
x
}

#[cfg(feature = "tiger")]
pub fn tiger_add(x: usize, y: usize) -> usize {
x + y
}

#[cfg(all(feature = "tiger", feature = "cat"))]
pub fn cat_tiger_add(x: usize, y: usize) -> usize {
x + y
}
6 changes: 6 additions & 0 deletions src/build.rs
Expand Up @@ -54,6 +54,12 @@ pub fn project() -> Vec<::std::path::PathBuf> {
};
cargo_build.arg("--verbose");

if !opts.features().is_empty() {
cargo_build.arg(
&format!("--features={}", opts.features().join(","))
);
}

if let Some(triple) = opts.TRIPLE() {
cargo_build.arg(&format!("--target={}", triple));
}
Expand Down
12 changes: 12 additions & 0 deletions src/options.rs
Expand Up @@ -25,6 +25,8 @@ pub struct AsmOptions {
pub asm_style: Style,
#[structopt(long = "build-type", help = "Build type: debug, release.", default_value = "release")]
pub build_type: Type,
#[structopt(long = "features", help = "cargo --features")]
pub features: Vec<String>,
#[structopt(long = "rust", help = "Print interleaved Rust code.")]
pub rust: bool,
#[structopt(long = "comments", help = "Print assembly comments.")]
Expand All @@ -48,6 +50,8 @@ pub struct LlvmIrOptions {
pub path: String,
#[structopt(long = "target", help = "Build for the target triple.")]
pub TRIPLE: Option<String>,
#[structopt(long = "features", help = "cargo --features")]
pub features: Vec<String>,
#[structopt(long = "no-color", help = "Disable colored output.")]
pub no_color: bool,
#[structopt(long = "build-type", help = "Build type: debug, release.", default_value = "release")]
Expand Down Expand Up @@ -76,6 +80,7 @@ pub trait OptionsExt {
fn print_comments(&self) -> bool;
fn print_directives(&self) -> bool;
fn set_rust(&self, value: bool);
fn features(&self) -> Vec<String>;
}

impl OptionsExt for ::std::sync::RwLock<Options> {
Expand Down Expand Up @@ -169,6 +174,13 @@ impl OptionsExt for ::std::sync::RwLock<Options> {
Options::LlvmIr(ref mut o) => o.rust = value,
}
}
fn features(&self) -> Vec<String> {
match *self.read().unwrap() {
Options::Asm(ref o) => o.features.clone(),
Options::LlvmIr(ref o) => o.features.clone(),
}
}

}

#[derive(StructOpt, Debug)]
Expand Down
23 changes: 23 additions & 0 deletions tests/cli.rs
Expand Up @@ -922,3 +922,26 @@ fn completions() {
.fails()
.unwrap();
}

#[test]
fn cargo_features() {
lib_test(&["lib_crate::bar::tiger_add"])
.stderr().contains("could not find function at path \"lib_crate::bar::tiger_add\" in the generated assembly.")
.fails()
.unwrap();

lib_test(&["lib_crate::bar::tiger_add", "--features=tiger"])
.stdout().contains("lib_crate::bar::tiger_add")
.succeeds()
.unwrap();

lib_test(&["lib_crate::bar::cat_tiger_add", "--features=tiger"])
.stderr().contains("could not find function at path \"lib_crate::bar::cat_tiger_add\" in the generated assembly.")
.fails()
.unwrap();

lib_test(&["lib_crate::bar::cat_tiger_add", "--features=tiger,cat"])
.stdout().contains("lib_crate::bar::cat_tiger_add")
.succeeds()
.unwrap();
}

0 comments on commit dd67164

Please sign in to comment.