Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditionally compiled extern "C" { blocks do not get #if defined( guards #807

Closed
NoraCodes opened this issue Jan 20, 2023 · 0 comments · Fixed by #943
Closed

Conditionally compiled extern "C" { blocks do not get #if defined( guards #807

NoraCodes opened this issue Jan 20, 2023 · 0 comments · Fixed by #943

Comments

@NoraCodes
Copy link

While conditional compilation works fine within extern "C" blocks, annotating an entire extern "C" block with #[cfg()] is valid in Rust, but doesn't work in cbindgen.

For example, for the following Rust project:

# Cargo.toml
[package]
name = "extern_block_conditional"
version = "0.1.0"
authors = ["cbindgen"]

[lib]
name = "extern_block_conditional"
crate-type = ["lib", "dylib"]
features = [ "myfeature" ]
# cbindgen.toml
[defines]
"feature = myfeature" = "DEFINED"
// src/lib.rs
#[repr(C)]
struct Normal {
    x: i32,
    y: f32,
}

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

    fn bar(a: Normal);
}

I would expect the following C output:

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

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

#if defined(DEFINED)
extern int32_t foo(void);

extern void bar(Normal a);
#endif

But in fact, writing this as a test case in this project, the output I see is:

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

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

extern int32_t foo(void);

extern void bar(Normal a);

cc @katef

emilio added a commit that referenced this issue Apr 15, 2024
emilio added a commit that referenced this issue Apr 15, 2024
github-merge-queue bot pushed a commit that referenced this issue Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant