Add two Cython-specific options to the config

This commit is contained in:
Vadim Petrochenkov
2020-10-03 23:36:14 +03:00
committed by Emilio Cobos Álvarez
parent 1c1d4754ce
commit b0aae44c77
9 changed files with 74 additions and 1 deletions
+13
View File
@@ -979,6 +979,19 @@ features = ["cbindgen"]
# `&mut T` and `NonNull<T>` all require a valid pointer value.
non_null_attribute = "_Nonnull"
# Options specific to Cython bindings.
[cython]
# Header specified in the top level `cdef extern from header:` declaration.
#
# default: *
header = '"my_header.h"'
# `from module cimport name1, name2` declarations added in the same place
# where you'd get includes in C.
[cython.cimports]
module = ["name1", "name2"]
```
+10 -1
View File
@@ -156,6 +156,7 @@ impl Bindings {
if self.config.no_includes
&& self.config.sys_includes().is_empty()
&& self.config.includes().is_empty()
&& (self.config.cython.cimports.is_empty() || self.config.language != Language::Cython)
&& self.config.after_includes.is_none()
{
return;
@@ -232,6 +233,13 @@ impl Bindings {
out.new_line();
}
if self.config.language == Language::Cython {
for (module, names) in &self.config.cython.cimports {
write!(out, "from {} cimport {}", module, names.join(", "));
out.new_line();
}
}
if let Some(ref line) = self.config.after_includes {
write!(out, "{}", line);
out.new_line();
@@ -389,7 +397,8 @@ impl Bindings {
if self.config.language == Language::Cython {
if op == NamespaceOperation::Open {
out.new_line();
out.write("cdef extern from *");
let header = self.config.cython.header.as_deref().unwrap_or("*");
write!(out, "cdef extern from {}", header);
out.open_brace();
} else {
out.close_brace(false);
+16
View File
@@ -835,6 +835,19 @@ pub struct PtrConfig {
pub non_null_attribute: Option<String>,
}
/// Settings specific to Cython bindings.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
#[serde(default)]
pub struct CythonConfig {
/// Header specified in the top level `cdef extern from header:` declaration.
pub header: Option<String>,
/// `from module cimport name1, name2, ...` declarations added in the same place
/// where you'd get includes in C.
pub cimports: HashMap<String, Vec<String>>,
}
/// A collection of settings to customize the generated bindings.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "snake_case")]
@@ -918,6 +931,8 @@ pub struct Config {
/// Configuration options for pointers
#[serde(rename = "ptr")]
pub pointer: PtrConfig,
/// Configuration options specific to Cython.
pub cython: CythonConfig,
}
impl Default for Config {
@@ -957,6 +972,7 @@ impl Default for Config {
documentation: true,
documentation_style: DocumentationStyle::Auto,
pointer: PtrConfig::default(),
cython: CythonConfig::default(),
}
}
}
@@ -0,0 +1,4 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
+5
View File
@@ -0,0 +1,5 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>
+10
View File
@@ -0,0 +1,10 @@
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
cdef extern from *:
ctypedef bint bool
ctypedef struct va_list
from libc.stddef cimport *
from libc.stdint cimport int8_t, int16_t
cdef extern from "my_header.h":
pass
+10
View File
@@ -0,0 +1,10 @@
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
cdef extern from *:
ctypedef bint bool
ctypedef struct va_list
from libc.stdint cimport int8_t, int16_t
from libc.stddef cimport *
cdef extern from "my_header.h":
pass
View File
+6
View File
@@ -0,0 +1,6 @@
[cython]
header = '"my_header.h"'
[cython.cimports]
"libc.stdint" = ["int8_t", "int16_t"]
"libc.stddef" = ["*"]