Skip to content

Commit

Permalink
Account for cfg items in foreign mods.
Browse files Browse the repository at this point in the history
Fixes #807
  • Loading branch information
emilio committed Apr 15, 2024
1 parent 49a387e commit f071790
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/bindgen/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ impl Parse {
return;
}

let mod_cfg = Cfg::append(mod_cfg, Cfg::load(&item.attrs));
for foreign_item in &item.items {
if let syn::ForeignItem::Fn(ref function) = *foreign_item {
if !config
Expand All @@ -618,7 +619,7 @@ impl Parse {
return;
}
let path = Path::new(function.sig.ident.unraw().to_string());
match Function::load(path, None, &function.sig, true, &function.attrs, mod_cfg) {
match Function::load(path, None, &function.sig, true, &function.attrs, mod_cfg.as_ref()) {
Ok(func) => {
info!("Take {}::{}.", crate_name, &function.sig.ident);

Expand Down
13 changes: 13 additions & 0 deletions tests/expectations/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ typedef struct {
;
} ConditionalField;

typedef struct {
int32_t x;
float y;
} Normal;

#if (defined(PLATFORM_UNIX) && defined(X11))
void root(FooHandle a, C c);
#endif
Expand All @@ -87,3 +92,11 @@ void root(BarHandle a, C c);
#endif

void cond(ConditionalField a);

#if defined(PLATFORM_WIN)
extern int32_t foo(void);
#endif

#if defined(PLATFORM_WIN)
extern void bar(Normal a);
#endif
13 changes: 13 additions & 0 deletions tests/expectations/cfg.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ typedef struct {
;
} ConditionalField;

typedef struct {
int32_t x;
float y;
} Normal;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
Expand All @@ -110,6 +115,14 @@ void root(BarHandle a, C c);

void cond(ConditionalField a);

#if defined(PLATFORM_WIN)
extern int32_t foo(void);
#endif

#if defined(PLATFORM_WIN)
extern void bar(Normal a);
#endif

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
22 changes: 22 additions & 0 deletions tests/expectations/cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,20 @@ struct ConditionalField {
;
};

struct Normal {
int32_t x;
float y;

bool operator==(const Normal& other) const {
return x == other.x &&
y == other.y;
}
bool operator!=(const Normal& other) const {
return x != other.x ||
y != other.y;
}
};

extern "C" {

#if (defined(PLATFORM_UNIX) && defined(X11))
Expand All @@ -214,4 +228,12 @@ void root(BarHandle a, C c);

void cond(ConditionalField a);

#if defined(PLATFORM_WIN)
extern int32_t foo();
#endif

#if defined(PLATFORM_WIN)
extern void bar(Normal a);
#endif

} // extern "C"
10 changes: 10 additions & 0 deletions tests/expectations/cfg.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,20 @@ cdef extern from *:
ctypedef struct ConditionalField:
int32_t field;

ctypedef struct Normal:
int32_t x;
float y;

IF (PLATFORM_UNIX and X11):
void root(FooHandle a, C c);

IF (PLATFORM_WIN or M_32):
void root(BarHandle a, C c);

void cond(ConditionalField a);

IF PLATFORM_WIN:
extern int32_t foo();

IF PLATFORM_WIN:
extern void bar(Normal a);
13 changes: 13 additions & 0 deletions tests/expectations/cfg_both.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ typedef struct ConditionalField {
;
} ConditionalField;

typedef struct Normal {
int32_t x;
float y;
} Normal;

#if (defined(PLATFORM_UNIX) && defined(X11))
void root(struct FooHandle a, union C c);
#endif
Expand All @@ -87,3 +92,11 @@ void root(struct BarHandle a, union C c);
#endif

void cond(struct ConditionalField a);

#if defined(PLATFORM_WIN)
extern int32_t foo(void);
#endif

#if defined(PLATFORM_WIN)
extern void bar(struct Normal a);
#endif
13 changes: 13 additions & 0 deletions tests/expectations/cfg_both.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ typedef struct ConditionalField {
;
} ConditionalField;

typedef struct Normal {
int32_t x;
float y;
} Normal;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
Expand All @@ -110,6 +115,14 @@ void root(struct BarHandle a, union C c);

void cond(struct ConditionalField a);

#if defined(PLATFORM_WIN)
extern int32_t foo(void);
#endif

#if defined(PLATFORM_WIN)
extern void bar(struct Normal a);
#endif

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
13 changes: 13 additions & 0 deletions tests/expectations/cfg_tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ struct ConditionalField {
;
};

struct Normal {
int32_t x;
float y;
};

#if (defined(PLATFORM_UNIX) && defined(X11))
void root(struct FooHandle a, union C c);
#endif
Expand All @@ -87,3 +92,11 @@ void root(struct BarHandle a, union C c);
#endif

void cond(struct ConditionalField a);

#if defined(PLATFORM_WIN)
extern int32_t foo(void);
#endif

#if defined(PLATFORM_WIN)
extern void bar(struct Normal a);
#endif
13 changes: 13 additions & 0 deletions tests/expectations/cfg_tag.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ struct ConditionalField {
;
};

struct Normal {
int32_t x;
float y;
};

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
Expand All @@ -110,6 +115,14 @@ void root(struct BarHandle a, union C c);

void cond(struct ConditionalField a);

#if defined(PLATFORM_WIN)
extern int32_t foo(void);
#endif

#if defined(PLATFORM_WIN)
extern void bar(struct Normal a);
#endif

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
10 changes: 10 additions & 0 deletions tests/expectations/cfg_tag.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,20 @@ cdef extern from *:
cdef struct ConditionalField:
int32_t field;

cdef struct Normal:
int32_t x;
float y;

IF (PLATFORM_UNIX and X11):
void root(FooHandle a, C c);

IF (PLATFORM_WIN or M_32):
void root(BarHandle a, C c);

void cond(ConditionalField a);

IF PLATFORM_WIN:
extern int32_t foo();

IF PLATFORM_WIN:
extern void bar(Normal a);
14 changes: 14 additions & 0 deletions tests/rust/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,17 @@ pub extern "C" fn root(a: BarHandle, c: C)
#[no_mangle]
pub extern "C" fn cond(a: ConditionalField)
{ }

// src/lib.rs
#[repr(C)]
struct Normal {
x: i32,
y: f32,
}

#[cfg(windows)]
extern "C" {
fn foo() -> i32;

fn bar(a: Normal);
}

0 comments on commit f071790

Please sign in to comment.