Skip to content

Commit 0ba2414

Browse files
jrose-signalemilio
authored andcommitted
Allow controlling the Cargo profile used for macro expansion
If there's already a release build, it's better for cbindgen to reuse the build artifacts from that to expand macros rather than starting from scratch with a debug build. Controlled with --profile (debug|release) as well as parse.expand.profile in cbindgen.toml, though hardcoding a profile in a config file seems unlikely.
1 parent 398b28c commit 0ba2414

File tree

10 files changed

+264
-3
lines changed

10 files changed

+264
-3
lines changed

Cargo.lock

Lines changed: 83 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ version = "1.0.3"
3131
default-features = false
3232
features = ["clone-impls", "extra-traits", "full", "parsing", "printing"]
3333

34+
[dev-dependencies]
35+
serial_test = "0.5.0"
36+
3437
[features]
3538
default = ["clap"]
3639

src/bindgen/builder.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::path;
66

77
use crate::bindgen::bindings::Bindings;
88
use crate::bindgen::cargo::Cargo;
9-
use crate::bindgen::config::{Braces, Config, Language, Style};
9+
use crate::bindgen::config::{Braces, Config, Language, Profile, Style};
1010
use crate::bindgen::error::Error;
1111
use crate::bindgen::library::Library;
1212
use crate::bindgen::parser::{self, Parse};
@@ -225,6 +225,12 @@ impl Builder {
225225
self
226226
}
227227

228+
#[allow(unused)]
229+
pub fn with_parse_expand_profile(mut self, profile: Profile) -> Builder {
230+
self.config.parse.expand.profile = profile;
231+
self
232+
}
233+
228234
#[allow(unused)]
229235
pub fn with_documentation(mut self, documentation: bool) -> Builder {
230236
self.config.documentation = documentation;

src/bindgen/cargo/cargo.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::bindgen::cargo::cargo_metadata::{self, Metadata};
1111
use crate::bindgen::cargo::cargo_toml;
1212
use crate::bindgen::error::Error;
1313
use crate::bindgen::ir::Cfg;
14+
pub(crate) use cargo_expand::Profile;
1415

1516
/// Parse a dependency string used in Cargo.lock
1617
fn parse_dep_string(dep_string: &str) -> (&str, Option<&str>) {
@@ -233,6 +234,7 @@ impl Cargo {
233234
expand_all_features: bool,
234235
expand_default_features: bool,
235236
expand_features: &Option<Vec<String>>,
237+
profile: Profile,
236238
) -> Result<String, cargo_expand::Error> {
237239
cargo_expand::expand(
238240
&self.manifest_path,
@@ -242,6 +244,7 @@ impl Cargo {
242244
expand_all_features,
243245
expand_default_features,
244246
expand_features,
247+
profile,
245248
)
246249
}
247250
}

src/bindgen/cargo/cargo_expand.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ pub enum Error {
2424
Compile(String),
2525
}
2626

27+
/// Which Cargo profile (group) to use when expanding macros.
28+
pub enum Profile {
29+
/// Do not pass `--release` when expanding macros
30+
Debug,
31+
/// Pass `--release` when expanding macros
32+
Release,
33+
}
34+
2735
impl From<io::Error> for Error {
2836
fn from(err: io::Error) -> Self {
2937
Error::Io(err)
@@ -65,6 +73,7 @@ pub fn expand(
6573
expand_all_features: bool,
6674
expand_default_features: bool,
6775
expand_features: &Option<Vec<String>>,
76+
profile: Profile,
6877
) -> Result<String, Error> {
6978
let cargo = env::var("CARGO").unwrap_or_else(|_| String::from("cargo"));
7079
let mut cmd = Command::new(cargo);
@@ -108,6 +117,12 @@ pub fn expand(
108117
if !expand_default_features {
109118
cmd.arg("--no-default-features");
110119
}
120+
match profile {
121+
Profile::Debug => {}
122+
Profile::Release => {
123+
cmd.arg("--release");
124+
}
125+
}
111126
cmd.arg("-p");
112127
let mut package = crate_name.to_owned();
113128
if let Some(version) = version {

src/bindgen/config.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,27 @@ pub struct MacroExpansionConfig {
668668
pub bitflags: bool,
669669
}
670670

671+
/// Controls which Cargo profile is used for macro expansion.
672+
#[derive(Debug, Copy, Clone, PartialEq)]
673+
pub enum Profile {
674+
Debug,
675+
Release,
676+
}
677+
678+
impl FromStr for Profile {
679+
type Err = String;
680+
681+
fn from_str(s: &str) -> Result<Profile, Self::Err> {
682+
match s {
683+
"debug" | "Debug" => Ok(Profile::Debug),
684+
"release" | "Release" => Ok(Profile::Release),
685+
_ => Err(format!("Unrecognized Profile: '{}'.", s)),
686+
}
687+
}
688+
}
689+
690+
deserialize_enum_str!(Profile);
691+
671692
/// Settings to apply when running `rustc --pretty=expanded`
672693
#[derive(Debug, Clone, Deserialize)]
673694
#[serde(rename_all = "snake_case")]
@@ -683,6 +704,8 @@ pub struct ParseExpandConfig {
683704
/// List of features to use when expanding. Combines with `default_features` like in
684705
/// `Cargo.toml`.
685706
pub features: Option<Vec<String>>,
707+
/// Controls whether or not to pass `--release` when expanding.
708+
pub profile: Profile,
686709
}
687710

688711
impl Default for ParseExpandConfig {
@@ -692,6 +715,7 @@ impl Default for ParseExpandConfig {
692715
all_features: false,
693716
default_features: true,
694717
features: None,
718+
profile: Profile::Debug,
695719
}
696720
}
697721
}
@@ -723,6 +747,7 @@ fn retrocomp_parse_expand_config_deserialize<'de, D: Deserializer<'de>>(
723747
all_features: true,
724748
default_features: true,
725749
features: None,
750+
profile: Profile::Debug,
726751
})
727752
}
728753

src/bindgen/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,6 @@ pub(crate) use self::cargo::*;
6060

6161
pub use self::bindings::Bindings;
6262
pub use self::builder::Builder;
63+
pub use self::config::Profile; // disambiguate with cargo::Profile
6364
pub use self::config::*;
6465
pub use self::error::Error;

src/bindgen/parser.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ use std::io::Read;
99
use std::path::{Path as FilePath, PathBuf as FilePathBuf};
1010

1111
use crate::bindgen::bitflags;
12+
use crate::bindgen::cargo;
1213
use crate::bindgen::cargo::{Cargo, PackageRef};
13-
use crate::bindgen::config::{Config, ParseConfig};
14+
use crate::bindgen::config::{Config, ParseConfig, Profile};
1415
use crate::bindgen::error::Error;
1516
use crate::bindgen::ir::{
1617
AnnotationSet, Cfg, Constant, Documentation, Enum, Function, GenericParams, ItemMap,
@@ -191,6 +192,10 @@ impl<'a> Parser<'a> {
191192
self.config.parse.expand.all_features,
192193
self.config.parse.expand.default_features,
193194
&self.config.parse.expand.features,
195+
match self.config.parse.expand.profile {
196+
Profile::Debug => cargo::Profile::Debug,
197+
Profile::Release => cargo::Profile::Release,
198+
},
194199
)
195200
.map_err(|x| Error::CargoExpand(pkg.name.clone(), x))?;
196201
let i = syn::parse_file(&s).map_err(|x| Error::ParseSyntaxError {

src/main.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use std::env;
66
use std::io;
77
use std::path::{Path, PathBuf};
8+
use std::str::FromStr;
89

910
extern crate clap;
1011
#[macro_use]
@@ -24,7 +25,7 @@ use clap::{App, Arg, ArgMatches};
2425
mod bindgen;
2526
mod logging;
2627

27-
use crate::bindgen::{Bindings, Builder, Cargo, Config, Error, Language, Style};
28+
use crate::bindgen::{Bindings, Builder, Cargo, Config, Error, Language, Profile, Style};
2829

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

65+
if let Some(profile) = matches.value_of("profile") {
66+
config.parse.expand.profile = match Profile::from_str(profile) {
67+
Ok(p) => p,
68+
Err(e) => {
69+
error!("{}", e);
70+
return;
71+
}
72+
}
73+
}
74+
6475
if matches.is_present("d") {
6576
config.parse.parse_deps = true;
6677
}
@@ -226,6 +237,16 @@ fn main() {
226237
)
227238
.required(false),
228239
)
240+
.arg(
241+
Arg::with_name("profile")
242+
.long("profile")
243+
.value_name("PROFILE")
244+
.help(
245+
"Specify the profile to use when expanding macros. \
246+
Has no effect otherwise."
247+
)
248+
.possible_values(&["Debug", "debug", "Release", "release"]),
249+
)
229250
.arg(
230251
Arg::with_name("quiet")
231252
.short("q")

0 commit comments

Comments
 (0)