Skip to content

Commit

Permalink
Add additional_includes to C generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
All8Up authored and ralfbiedert committed Apr 9, 2024
1 parent c1d2048 commit 2a383a5
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions backends/c/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ pub struct Config {
pub directives: bool,
/// Whether to write `#include <>` directives.
pub imports: bool,
/// Additional `#include` lines in the form of `<item.h>`` or `"item.h"`.
pub additional_includes: Vec<String>,
/// The `_X` in `#ifndef _X` to be used.
pub ifndef: String,
/// Multiline string with custom `#define` values.
Expand Down Expand Up @@ -107,6 +109,7 @@ impl Default for Config {
Self {
directives: true,
imports: true,
additional_includes: vec![],
file_header_comment: "// Automatically generated by Interoptopus.".to_string(),
ifndef: "interoptopus_generated".to_string(),
custom_defines: "".to_string(),
Expand Down
5 changes: 5 additions & 0 deletions backends/c/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ pub trait CWriter {
indented!(w, r#"#include <stdint.h>"#)?;
indented!(w, r#"#include <stdbool.h>"#)?;

// Write any user supplied includes into the file.
for include in &self.config().additional_includes {
indented!(w, "#include {}", include)?;
}

Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions examples/complex/bindings/c/example_complex.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern "C" {

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>


// Custom attribute.
Expand Down
2 changes: 2 additions & 0 deletions examples/complex/tests/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ fn bindings_c() -> Result<(), Error> {
Generator::new(
Config {
ifndef: "example_complex".to_string(),
// Add an unneeded include for testing purposes.
additional_includes: vec!["<stdio.h>".into()],
function_attribute: "__FUNCTION_ATTR ".to_string(),
custom_defines,
documentation: CDocumentationStyle::Inline,
Expand Down

0 comments on commit 2a383a5

Please sign in to comment.