From 2a383a597375fac1bb7809d78106b70732bb7ff7 Mon Sep 17 00:00:00 2001 From: All8Up Date: Mon, 8 Apr 2024 16:44:50 -0400 Subject: [PATCH] Add additional_includes to C generator. --- backends/c/src/config.rs | 3 +++ backends/c/src/writer.rs | 5 +++++ examples/complex/bindings/c/example_complex.h | 1 + examples/complex/tests/bindings.rs | 2 ++ 4 files changed, 11 insertions(+) diff --git a/backends/c/src/config.rs b/backends/c/src/config.rs index 07e179b7..1b399510 100644 --- a/backends/c/src/config.rs +++ b/backends/c/src/config.rs @@ -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 ``` or `"item.h"`. + pub additional_includes: Vec, /// The `_X` in `#ifndef _X` to be used. pub ifndef: String, /// Multiline string with custom `#define` values. @@ -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(), diff --git a/backends/c/src/writer.rs b/backends/c/src/writer.rs index 153b7e67..737fbf3f 100644 --- a/backends/c/src/writer.rs +++ b/backends/c/src/writer.rs @@ -34,6 +34,11 @@ pub trait CWriter { indented!(w, r#"#include "#)?; indented!(w, r#"#include "#)?; + // Write any user supplied includes into the file. + for include in &self.config().additional_includes { + indented!(w, "#include {}", include)?; + } + Ok(()) } diff --git a/examples/complex/bindings/c/example_complex.h b/examples/complex/bindings/c/example_complex.h index 85bc5ec0..d1b6acd5 100644 --- a/examples/complex/bindings/c/example_complex.h +++ b/examples/complex/bindings/c/example_complex.h @@ -9,6 +9,7 @@ extern "C" { #include #include +#include // Custom attribute. diff --git a/examples/complex/tests/bindings.rs b/examples/complex/tests/bindings.rs index 0495a867..82f0b522 100644 --- a/examples/complex/tests/bindings.rs +++ b/examples/complex/tests/bindings.rs @@ -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!["".into()], function_attribute: "__FUNCTION_ATTR ".to_string(), custom_defines, documentation: CDocumentationStyle::Inline,