Skip to content

Commit

Permalink
Output condition for globals.
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviojs authored and emilio committed Jun 4, 2024
1 parent e469e44 commit 3162981
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/bindgen/language_backend/clike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,9 @@ impl LanguageBackend for CLikeLanguageBackend<'_> {
}

fn write_static<W: Write>(&mut self, out: &mut SourceWriter<W>, s: &Static) {
let condition = s.cfg.to_condition(self.config);
condition.write_before(self.config, out);

self.write_documentation(out, &s.documentation);
out.write("extern ");
if let Type::Ptr { is_const: true, .. } = s.ty {
Expand All @@ -778,6 +781,8 @@ impl LanguageBackend for CLikeLanguageBackend<'_> {
}
cdecl::write_field(self, out, &s.ty, &s.export_name, self.config);
out.write(";");

condition.write_after(self.config, out);
}

fn write_type<W: Write>(&mut self, out: &mut SourceWriter<W>, t: &Type) {
Expand Down
5 changes: 5 additions & 0 deletions src/bindgen/language_backend/cython.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ impl LanguageBackend for CythonLanguageBackend<'_> {
}

fn write_static<W: Write>(&mut self, out: &mut SourceWriter<W>, s: &Static) {
let condition = s.cfg.to_condition(self.config);
condition.write_before(self.config, out);

self.write_documentation(out, &s.documentation);
out.write("extern ");
if let Type::Ptr { is_const: true, .. } = s.ty {
Expand All @@ -323,6 +326,8 @@ impl LanguageBackend for CythonLanguageBackend<'_> {
}
cdecl::write_field(self, out, &s.ty, &s.export_name, self.config);
out.write(";");

condition.write_after(self.config, out);
}

fn write_type<W: Write>(&mut self, out: &mut SourceWriter<W>, t: &Type) {
Expand Down
8 changes: 8 additions & 0 deletions tests/expectations/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ typedef struct {
float y;
} Normal;

#if defined(PLATFORM_WIN)
extern int32_t global_array_with_different_sizes[2];
#endif

#if defined(PLATFORM_UNIX)
extern int32_t global_array_with_different_sizes[1];
#endif

#if (defined(PLATFORM_UNIX) && defined(X11))
void root(FooHandle a, C c);
#endif
Expand Down
8 changes: 8 additions & 0 deletions tests/expectations/cfg.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ typedef struct {
extern "C" {
#endif // __cplusplus

#if defined(PLATFORM_WIN)
extern int32_t global_array_with_different_sizes[2];
#endif

#if defined(PLATFORM_UNIX)
extern int32_t global_array_with_different_sizes[1];
#endif

#if (defined(PLATFORM_UNIX) && defined(X11))
void root(FooHandle a, C c);
#endif
Expand Down
8 changes: 8 additions & 0 deletions tests/expectations/cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ struct Normal {

extern "C" {

#if defined(PLATFORM_WIN)
extern int32_t global_array_with_different_sizes[2];
#endif

#if defined(PLATFORM_UNIX)
extern int32_t global_array_with_different_sizes[1];
#endif

#if (defined(PLATFORM_UNIX) && defined(X11))
void root(FooHandle a, C c);
#endif
Expand Down
6 changes: 6 additions & 0 deletions tests/expectations/cfg.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ cdef extern from *:
int32_t x;
float y;

IF PLATFORM_WIN:
extern int32_t global_array_with_different_sizes[2];

IF PLATFORM_UNIX:
extern int32_t global_array_with_different_sizes[1];

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

Expand Down
8 changes: 8 additions & 0 deletions tests/expectations/cfg_both.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ typedef struct Normal {
float y;
} Normal;

#if defined(PLATFORM_WIN)
extern int32_t global_array_with_different_sizes[2];
#endif

#if defined(PLATFORM_UNIX)
extern int32_t global_array_with_different_sizes[1];
#endif

#if (defined(PLATFORM_UNIX) && defined(X11))
void root(struct FooHandle a, union C c);
#endif
Expand Down
8 changes: 8 additions & 0 deletions tests/expectations/cfg_both.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ typedef struct Normal {
extern "C" {
#endif // __cplusplus

#if defined(PLATFORM_WIN)
extern int32_t global_array_with_different_sizes[2];
#endif

#if defined(PLATFORM_UNIX)
extern int32_t global_array_with_different_sizes[1];
#endif

#if (defined(PLATFORM_UNIX) && defined(X11))
void root(struct FooHandle a, union C c);
#endif
Expand Down
8 changes: 8 additions & 0 deletions tests/expectations/cfg_tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ struct Normal {
float y;
};

#if defined(PLATFORM_WIN)
extern int32_t global_array_with_different_sizes[2];
#endif

#if defined(PLATFORM_UNIX)
extern int32_t global_array_with_different_sizes[1];
#endif

#if (defined(PLATFORM_UNIX) && defined(X11))
void root(struct FooHandle a, union C c);
#endif
Expand Down
8 changes: 8 additions & 0 deletions tests/expectations/cfg_tag.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ struct Normal {
extern "C" {
#endif // __cplusplus

#if defined(PLATFORM_WIN)
extern int32_t global_array_with_different_sizes[2];
#endif

#if defined(PLATFORM_UNIX)
extern int32_t global_array_with_different_sizes[1];
#endif

#if (defined(PLATFORM_UNIX) && defined(X11))
void root(struct FooHandle a, union C c);
#endif
Expand Down
6 changes: 6 additions & 0 deletions tests/expectations/cfg_tag.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ cdef extern from *:
int32_t x;
float y;

IF PLATFORM_WIN:
extern int32_t global_array_with_different_sizes[2];

IF PLATFORM_UNIX:
extern int32_t global_array_with_different_sizes[1];

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

Expand Down
7 changes: 7 additions & 0 deletions tests/rust/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,10 @@ extern "C" {

fn bar(a: Normal);
}

#[cfg(windows)]
#[no_mangle]
pub static mut global_array_with_different_sizes: [i32; 2] = [123, 456];
#[cfg(unix)]
#[no_mangle]
pub static mut global_array_with_different_sizes: [i32; 1] = [7890];

0 comments on commit 3162981

Please sign in to comment.