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
Support cargo:rustc-cfg in build.rs #4296
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,4 +53,13 @@ impl CfgOptions { | |
pub fn insert_features(&mut self, iter: impl IntoIterator<Item = SmolStr>) { | ||
iter.into_iter().for_each(|feat| self.insert_key_value("feature".into(), feat)); | ||
} | ||
|
||
/// Shortcut to set cfgs | ||
pub fn insert_cfgs(&mut self, iter: impl IntoIterator<Item = SmolStr>) { | ||
iter.into_iter().for_each(|cfg| match cfg.find('=') { | ||
Some(split) => self | ||
.insert_key_value(cfg[0..split].into(), cfg[split + 1..].trim_matches('"').into()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In theory, we should unescape the RHS here, but in practice, I don't think it is relevant here, right? |
||
None => self.insert_atom(cfg), | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,7 @@ pub struct PackageData { | |
pub dependencies: Vec<PackageDependency>, | ||
pub edition: Edition, | ||
pub features: Vec<String>, | ||
pub cfgs: Vec<PathBuf>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL But why ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might well be a copy-paste error in
which should well be covered by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sound reasonable ! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oli-obk/cargo_metadata#112 has been merged and I changed the code to minimize the needed changes once that hits |
||
pub out_dir: Option<PathBuf>, | ||
pub proc_macro_dylib_path: Option<PathBuf>, | ||
} | ||
|
@@ -165,10 +166,12 @@ impl CargoWorkspace { | |
})?; | ||
|
||
let mut out_dir_by_id = FxHashMap::default(); | ||
let mut cfgs = FxHashMap::default(); | ||
let mut proc_macro_dylib_paths = FxHashMap::default(); | ||
if cargo_features.load_out_dirs_from_check { | ||
let resources = load_extern_resources(cargo_toml, cargo_features)?; | ||
out_dir_by_id = resources.out_dirs; | ||
cfgs = resources.cfgs; | ||
proc_macro_dylib_paths = resources.proc_dylib_paths; | ||
} | ||
|
||
|
@@ -194,6 +197,7 @@ impl CargoWorkspace { | |
edition, | ||
dependencies: Vec::new(), | ||
features: Vec::new(), | ||
cfgs: cfgs.get(&id).cloned().unwrap_or_default(), | ||
out_dir: out_dir_by_id.get(&id).cloned(), | ||
proc_macro_dylib_path: proc_macro_dylib_paths.get(&id).cloned(), | ||
}); | ||
|
@@ -275,6 +279,7 @@ impl CargoWorkspace { | |
pub struct ExternResources { | ||
out_dirs: FxHashMap<PackageId, PathBuf>, | ||
proc_dylib_paths: FxHashMap<PackageId, PathBuf>, | ||
cfgs: FxHashMap<PackageId, Vec<PathBuf>>, | ||
} | ||
|
||
pub fn load_extern_resources( | ||
|
@@ -300,8 +305,9 @@ pub fn load_extern_resources( | |
for message in cargo_metadata::parse_messages(output.stdout.as_slice()) { | ||
if let Ok(message) = message { | ||
match message { | ||
Message::BuildScriptExecuted(BuildScript { package_id, out_dir, .. }) => { | ||
res.out_dirs.insert(package_id, out_dir); | ||
Message::BuildScriptExecuted(BuildScript { package_id, out_dir, cfgs, .. }) => { | ||
res.out_dirs.insert(package_id.clone(), out_dir); | ||
res.cfgs.insert(package_id, cfgs); | ||
} | ||
|
||
Message::CompilerArtifact(message) => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this does not belong here (and the above
inseret_features
probably as well) --CfgOptions
should a be pretty abstract thing, exactly whatrustc
sees.Lowering various command-line args to this data structure should happen where we get the args.