Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow controlling the Cargo profile used for macro expansion #607

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
83 changes: 83 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Expand Up @@ -31,6 +31,9 @@ version = "1.0.3"
default-features = false
features = ["clone-impls", "extra-traits", "full", "parsing", "printing"]

[dev-dependencies]
serial_test = "0.5.0"

[features]
default = ["clap"]

Expand Down
8 changes: 7 additions & 1 deletion src/bindgen/builder.rs
Expand Up @@ -6,7 +6,7 @@ use std::path;

use crate::bindgen::bindings::Bindings;
use crate::bindgen::cargo::Cargo;
use crate::bindgen::config::{Braces, Config, Language, Style};
use crate::bindgen::config::{Braces, Config, Language, Profile, Style};
use crate::bindgen::error::Error;
use crate::bindgen::library::Library;
use crate::bindgen::parser::{self, Parse};
Expand Down Expand Up @@ -225,6 +225,12 @@ impl Builder {
self
}

#[allow(unused)]
pub fn with_parse_expand_profile(mut self, profile: Profile) -> Builder {
self.config.parse.expand.profile = profile;
self
}

#[allow(unused)]
pub fn with_documentation(mut self, documentation: bool) -> Builder {
self.config.documentation = documentation;
Expand Down
3 changes: 3 additions & 0 deletions src/bindgen/cargo/cargo.rs
Expand Up @@ -11,6 +11,7 @@ use crate::bindgen::cargo::cargo_metadata::{self, Metadata};
use crate::bindgen::cargo::cargo_toml;
use crate::bindgen::error::Error;
use crate::bindgen::ir::Cfg;
pub(crate) use cargo_expand::Profile;

/// Parse a dependency string used in Cargo.lock
fn parse_dep_string(dep_string: &str) -> (&str, Option<&str>) {
Expand Down Expand Up @@ -233,6 +234,7 @@ impl Cargo {
expand_all_features: bool,
expand_default_features: bool,
expand_features: &Option<Vec<String>>,
profile: Profile,
) -> Result<String, cargo_expand::Error> {
cargo_expand::expand(
&self.manifest_path,
Expand All @@ -242,6 +244,7 @@ impl Cargo {
expand_all_features,
expand_default_features,
expand_features,
profile,
)
}
}
15 changes: 15 additions & 0 deletions src/bindgen/cargo/cargo_expand.rs
Expand Up @@ -24,6 +24,14 @@ pub enum Error {
Compile(String),
}

/// Which Cargo profile (group) to use when expanding macros.
pub enum Profile {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to have this enum and the config.rs enum duplicated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't really sure how to do the dependencies. If the canonical version is in cargo_expand, then cargo_expand is exporting public types now; if the canonical version is in config, then cargo_expand needs to import config. Preference?

/// Do not pass `--release` when expanding macros
Debug,
/// Pass `--release` when expanding macros
Release,
}

impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::Io(err)
Expand Down Expand Up @@ -65,6 +73,7 @@ pub fn expand(
expand_all_features: bool,
expand_default_features: bool,
expand_features: &Option<Vec<String>>,
profile: Profile,
) -> Result<String, Error> {
let cargo = env::var("CARGO").unwrap_or_else(|_| String::from("cargo"));
let mut cmd = Command::new(cargo);
Expand Down Expand Up @@ -108,6 +117,12 @@ pub fn expand(
if !expand_default_features {
cmd.arg("--no-default-features");
}
match profile {
Profile::Debug => {}
Profile::Release => {
cmd.arg("--release");
}
}
cmd.arg("-p");
let mut package = crate_name.to_owned();
if let Some(version) = version {
Expand Down
25 changes: 25 additions & 0 deletions src/bindgen/config.rs
Expand Up @@ -668,6 +668,27 @@ pub struct MacroExpansionConfig {
pub bitflags: bool,
}

/// Controls which Cargo profile is used for macro expansion.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Profile {
Debug,
Release,
}

impl FromStr for Profile {
type Err = String;

fn from_str(s: &str) -> Result<Profile, Self::Err> {
match s {
"debug" | "Debug" => Ok(Profile::Debug),
"release" | "Release" => Ok(Profile::Release),
_ => Err(format!("Unrecognized Profile: '{}'.", s)),
}
}
}

deserialize_enum_str!(Profile);

/// Settings to apply when running `rustc --pretty=expanded`
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "snake_case")]
Expand All @@ -683,6 +704,8 @@ pub struct ParseExpandConfig {
/// List of features to use when expanding. Combines with `default_features` like in
/// `Cargo.toml`.
pub features: Option<Vec<String>>,
/// Controls whether or not to pass `--release` when expanding.
pub profile: Profile,
}

impl Default for ParseExpandConfig {
Expand All @@ -692,6 +715,7 @@ impl Default for ParseExpandConfig {
all_features: false,
default_features: true,
features: None,
profile: Profile::Debug,
}
}
}
Expand Down Expand Up @@ -723,6 +747,7 @@ fn retrocomp_parse_expand_config_deserialize<'de, D: Deserializer<'de>>(
all_features: true,
default_features: true,
features: None,
profile: Profile::Debug,
})
}

Expand Down
1 change: 1 addition & 0 deletions src/bindgen/mod.rs
Expand Up @@ -60,5 +60,6 @@ pub(crate) use self::cargo::*;

pub use self::bindings::Bindings;
pub use self::builder::Builder;
pub use self::config::Profile; // disambiguate with cargo::Profile
pub use self::config::*;
pub use self::error::Error;
7 changes: 6 additions & 1 deletion src/bindgen/parser.rs
Expand Up @@ -9,8 +9,9 @@ use std::io::Read;
use std::path::{Path as FilePath, PathBuf as FilePathBuf};

use crate::bindgen::bitflags;
use crate::bindgen::cargo;
use crate::bindgen::cargo::{Cargo, PackageRef};
use crate::bindgen::config::{Config, ParseConfig};
use crate::bindgen::config::{Config, ParseConfig, Profile};
use crate::bindgen::error::Error;
use crate::bindgen::ir::{
AnnotationSet, Cfg, Constant, Documentation, Enum, Function, GenericParams, ItemMap,
Expand Down Expand Up @@ -191,6 +192,10 @@ impl<'a> Parser<'a> {
self.config.parse.expand.all_features,
self.config.parse.expand.default_features,
&self.config.parse.expand.features,
match self.config.parse.expand.profile {
Profile::Debug => cargo::Profile::Debug,
Profile::Release => cargo::Profile::Release,
},
)
.map_err(|x| Error::CargoExpand(pkg.name.clone(), x))?;
let i = syn::parse_file(&s).map_err(|x| Error::ParseSyntaxError {
Expand Down
23 changes: 22 additions & 1 deletion src/main.rs
Expand Up @@ -5,6 +5,7 @@
use std::env;
use std::io;
use std::path::{Path, PathBuf};
use std::str::FromStr;

extern crate clap;
#[macro_use]
Expand All @@ -24,7 +25,7 @@ use clap::{App, Arg, ArgMatches};
mod bindgen;
mod logging;

use crate::bindgen::{Bindings, Builder, Cargo, Config, Error, Language, Style};
use crate::bindgen::{Bindings, Builder, Cargo, Config, Error, Language, Profile, Style};

fn apply_config_overrides<'a>(config: &mut Config, matches: &ArgMatches<'a>) {
// We allow specifying a language to override the config default. This is
Expand Down Expand Up @@ -61,6 +62,16 @@ fn apply_config_overrides<'a>(config: &mut Config, matches: &ArgMatches<'a>) {
}
}

if let Some(profile) = matches.value_of("profile") {
config.parse.expand.profile = match Profile::from_str(profile) {
Ok(p) => p,
Err(e) => {
error!("{}", e);
return;
}
}
}

if matches.is_present("d") {
config.parse.parse_deps = true;
}
Expand Down Expand Up @@ -226,6 +237,16 @@ fn main() {
)
.required(false),
)
.arg(
Arg::with_name("profile")
.long("profile")
.value_name("PROFILE")
.help(
"Specify the profile to use when expanding macros. \
Has no effect otherwise."
)
.possible_values(&["Debug", "debug", "Release", "release"]),
)
.arg(
Arg::with_name("quiet")
.short("q")
Expand Down