Fix #381
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:
parent
6fce1ccc4b
commit
ea6ea24740
@ -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> {
|
||||
|
4
tests/expectations/both/literal_target.c
Normal file
4
tests/expectations/both/literal_target.c
Normal file
@ -0,0 +1,4 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
4
tests/expectations/both/literal_target.compat.c
Normal file
4
tests/expectations/both/literal_target.compat.c
Normal file
@ -0,0 +1,4 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
4
tests/expectations/literal_target.c
Normal file
4
tests/expectations/literal_target.c
Normal file
@ -0,0 +1,4 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
4
tests/expectations/literal_target.compat.c
Normal file
4
tests/expectations/literal_target.compat.c
Normal file
@ -0,0 +1,4 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
4
tests/expectations/literal_target.cpp
Normal file
4
tests/expectations/literal_target.cpp
Normal file
@ -0,0 +1,4 @@
|
||||
#include <cstdarg>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <new>
|
4
tests/expectations/tag/literal_target.c
Normal file
4
tests/expectations/tag/literal_target.c
Normal file
@ -0,0 +1,4 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
4
tests/expectations/tag/literal_target.compat.c
Normal file
4
tests/expectations/tag/literal_target.compat.c
Normal 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
12
tests/rust/literal_target/Cargo.lock
generated
Normal 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",
|
||||
]
|
7
tests/rust/literal_target/Cargo.toml
Normal file
7
tests/rust/literal_target/Cargo.toml
Normal 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" }
|
0
tests/rust/literal_target/src/lib.rs
Normal file
0
tests/rust/literal_target/src/lib.rs
Normal file
Loading…
x
Reference in New Issue
Block a user