Skip to content

Add #include directives with headers for static wrappers #2447

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

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@
## Changed
* Static functions with no arguments use `void` as their single argument
instead of having no arguments when the `--wrap-static-fns` flag is used.
* The source file generated when the `--wrap-static-fns` flag is enabled now
contains `#include` directives with all the input headers and all the source
code added with the `header_contents` method.
## Removed
* The following deprecated flags were removed: `--use-msvc-mangling`,
`--rustfmt-bindings` and `--size_t-is-usize`.
Expand Down
2 changes: 0 additions & 2 deletions bindgen-integration/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,6 @@ fn setup_wrap_static_fns_test() {
.arg("-o")
.arg(&obj_path)
.arg(out_path.join("wrap_static_fns.c"))
.arg("-include")
.arg(input_header_file_path)
.output()
.expect("`clang` command error");
if !clang_output.status.success() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#include "tests/headers/wrap-static-fns.h"

// Static wrappers

int foo__extern(void) asm("foo__extern");
int foo__extern(void) { return foo(); }
int bar__extern(void) asm("bar__extern");
Expand Down
19 changes: 19 additions & 0 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4545,6 +4545,7 @@ pub(crate) mod utils {
use crate::ir::ty::TypeKind;
use crate::{args_are_cpp, file_is_cpp};
use std::borrow::Cow;
use std::io::Write;
use std::mem;
use std::path::PathBuf;
use std::str::FromStr;
Expand Down Expand Up @@ -4583,6 +4584,24 @@ pub(crate) mod utils {

let mut code = Vec::new();

if !context.options().input_headers.is_empty() {
for header in &context.options().input_headers {
writeln!(code, "#include \"{}\"", header)?;
}

writeln!(code)?;
}

if !context.options().input_header_contents.is_empty() {
for (name, contents) in &context.options().input_header_contents {
writeln!(code, "// {}\n{}", name, contents)?;
}

writeln!(code)?;
}

writeln!(code, "// Static wrappers\n")?;

for &id in &result.items_to_serialize {
let item = context.resolve_item(id);
item.serialize(context, (), &mut vec![], &mut code)?;
Expand Down