From 316298182ebf508240c95ddf9943556b2fb546e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20J=2E=20Saraiva?= Date: Mon, 3 Jun 2024 21:19:21 +0100 Subject: [PATCH] Output condition for globals. --- src/bindgen/language_backend/clike.rs | 5 +++++ src/bindgen/language_backend/cython.rs | 5 +++++ tests/expectations/cfg.c | 8 ++++++++ tests/expectations/cfg.compat.c | 8 ++++++++ tests/expectations/cfg.cpp | 8 ++++++++ tests/expectations/cfg.pyx | 6 ++++++ tests/expectations/cfg_both.c | 8 ++++++++ tests/expectations/cfg_both.compat.c | 8 ++++++++ tests/expectations/cfg_tag.c | 8 ++++++++ tests/expectations/cfg_tag.compat.c | 8 ++++++++ tests/expectations/cfg_tag.pyx | 6 ++++++ tests/rust/cfg.rs | 7 +++++++ 12 files changed, 85 insertions(+) diff --git a/src/bindgen/language_backend/clike.rs b/src/bindgen/language_backend/clike.rs index 1e3b76196..5da079c3d 100644 --- a/src/bindgen/language_backend/clike.rs +++ b/src/bindgen/language_backend/clike.rs @@ -770,6 +770,9 @@ impl LanguageBackend for CLikeLanguageBackend<'_> { } fn write_static(&mut self, out: &mut SourceWriter, 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 { @@ -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(&mut self, out: &mut SourceWriter, t: &Type) { diff --git a/src/bindgen/language_backend/cython.rs b/src/bindgen/language_backend/cython.rs index d768c596d..5c85b2d02 100644 --- a/src/bindgen/language_backend/cython.rs +++ b/src/bindgen/language_backend/cython.rs @@ -315,6 +315,9 @@ impl LanguageBackend for CythonLanguageBackend<'_> { } fn write_static(&mut self, out: &mut SourceWriter, 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 { @@ -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(&mut self, out: &mut SourceWriter, t: &Type) { diff --git a/tests/expectations/cfg.c b/tests/expectations/cfg.c index 5f4eb9d6a..9285f300e 100644 --- a/tests/expectations/cfg.c +++ b/tests/expectations/cfg.c @@ -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 diff --git a/tests/expectations/cfg.compat.c b/tests/expectations/cfg.compat.c index e98f3ad4d..053c4052f 100644 --- a/tests/expectations/cfg.compat.c +++ b/tests/expectations/cfg.compat.c @@ -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 diff --git a/tests/expectations/cfg.cpp b/tests/expectations/cfg.cpp index 6e7c33cfb..d7b6250dd 100644 --- a/tests/expectations/cfg.cpp +++ b/tests/expectations/cfg.cpp @@ -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 diff --git a/tests/expectations/cfg.pyx b/tests/expectations/cfg.pyx index b3280da25..e23fc7033 100644 --- a/tests/expectations/cfg.pyx +++ b/tests/expectations/cfg.pyx @@ -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); diff --git a/tests/expectations/cfg_both.c b/tests/expectations/cfg_both.c index fb0553f14..510e34e8d 100644 --- a/tests/expectations/cfg_both.c +++ b/tests/expectations/cfg_both.c @@ -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 diff --git a/tests/expectations/cfg_both.compat.c b/tests/expectations/cfg_both.compat.c index 671d1c714..2377824e1 100644 --- a/tests/expectations/cfg_both.compat.c +++ b/tests/expectations/cfg_both.compat.c @@ -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 diff --git a/tests/expectations/cfg_tag.c b/tests/expectations/cfg_tag.c index a2bdc24d8..8cc885383 100644 --- a/tests/expectations/cfg_tag.c +++ b/tests/expectations/cfg_tag.c @@ -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 diff --git a/tests/expectations/cfg_tag.compat.c b/tests/expectations/cfg_tag.compat.c index a40fc703d..ec9763536 100644 --- a/tests/expectations/cfg_tag.compat.c +++ b/tests/expectations/cfg_tag.compat.c @@ -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 diff --git a/tests/expectations/cfg_tag.pyx b/tests/expectations/cfg_tag.pyx index a258c64f5..e508bb30a 100644 --- a/tests/expectations/cfg_tag.pyx +++ b/tests/expectations/cfg_tag.pyx @@ -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); diff --git a/tests/rust/cfg.rs b/tests/rust/cfg.rs index 1737e37e6..70893609f 100644 --- a/tests/rust/cfg.rs +++ b/tests/rust/cfg.rs @@ -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];