As noted by @Benlitz, Cargo metadata supports directly specifying the
target name instead of using the `#[cfg]` syntax. We can support this by
manually creating the `syn::NestedMeta` node from the target name seen
as a string literal.
This commit is contained in:
Vincent Tavernier 2020-04-17 12:24:59 +02:00 committed by Emilio Cobos Álvarez
parent 6fce1ccc4b
commit ea6ea24740
11 changed files with 69 additions and 15 deletions

View File

@ -124,23 +124,30 @@ impl Cfg {
}
pub fn load_metadata(dependency: &Dependency) -> Option<Cfg> {
dependency
.target
.as_ref()
.map(|target| {
syn::parse_str::<syn::Meta>(target)
.expect("error parsing dependency's target metadata")
})
.and_then(|target| {
if let syn::Meta::List(syn::MetaList { path, nested, .. }) = target {
if !path.is_ident("cfg") || nested.len() != 1 {
return None;
match &dependency.target {
Some(target) => match syn::parse_str::<syn::Meta>(target) {
Ok(target) => {
// Parsing succeeded using the #[cfg] syntax
if let syn::Meta::List(syn::MetaList { path, nested, .. }) = target {
if !path.is_ident("cfg") || nested.len() != 1 {
return None;
}
Cfg::load_single(nested.first().unwrap())
} else {
None
}
Cfg::load_single(nested.first().unwrap())
} else {
None
}
})
Err(_) =>
// Parsing failed using #[cfg], this may be a literal target name
{
Cfg::load_single(&syn::NestedMeta::Lit(syn::Lit::Str(syn::LitStr::new(
target,
proc_macro2::Span::call_site(),
))))
}
},
None => None,
}
}
fn load_single(item: &syn::NestedMeta) -> Option<Cfg> {

View File

@ -0,0 +1,4 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

View File

@ -0,0 +1,4 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

View File

@ -0,0 +1,4 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

View File

@ -0,0 +1,4 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

View File

@ -0,0 +1,4 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <new>

View File

@ -0,0 +1,4 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

View File

@ -0,0 +1,4 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

12
tests/rust/literal_target/Cargo.lock generated Normal file
View File

@ -0,0 +1,12 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "dep"
version = "0.1.0"
[[package]]
name = "literal_target"
version = "0.1.0"
dependencies = [
"dep",
]

View File

@ -0,0 +1,7 @@
[package]
name = "literal_target"
version = "0.1.0"
authors = ["cbindgen"]
[target.x86_64-pc-windows-gnu.dependencies]
dep = { path = "../workspace/dep" }

View File