Skip to content
Open
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
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rust-version = "1.74"
exclude = [
"tests/profile.rs", # Test relies in a sub-crate, see https://github.com/rust-lang/cargo/issues/9017
]
default-run = "cbindgen"

[dependencies]
clap = { version = "4.3", optional = true }
Expand All @@ -42,13 +43,20 @@ pretty_assertions = "1.4.0"
[features]
default = ["clap"]
unstable_ir = []
gobject = []

[[bin]]
name = "cbindgen"
path = "src/main.rs"
doc = false
required-features = ["clap"]

[[bin]]
name = "gbindgen"
path = "src/gmain.rs"
doc = false
required-features = ["clap", "gobject"]

[lib]
name = "cbindgen"
path = "src/lib.rs"
Expand Down
6 changes: 5 additions & 1 deletion src/bindgen/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use std::rc::Rc;

use crate::bindgen::config::{Config, Language};
use crate::bindgen::ir::{
Constant, Function, ItemContainer, ItemMap, Path as BindgenPath, Static, Struct, Type, Typedef,
Constant, Function, GObject, ItemContainer, ItemMap, Path as BindgenPath, Static, Struct, Type,
Typedef,
};
use crate::bindgen::language_backend::{
CLikeLanguageBackend, CythonLanguageBackend, LanguageBackend,
Expand All @@ -32,6 +33,7 @@ pub struct Bindings {
pub constants: Vec<Constant>,
pub items: Vec<ItemContainer>,
pub functions: Vec<Function>,
pub gobjects: Vec<GObject>,
source_files: Vec<path::PathBuf>,
/// Bindings are generated by a recursive call to cbindgen
/// and shouldn't do anything when written anywhere.
Expand All @@ -52,6 +54,7 @@ impl Bindings {
source_files: Vec<path::PathBuf>,
noop: bool,
package_version: String,
gobjects: Vec<GObject>,
) -> Bindings {
Bindings {
config,
Expand All @@ -65,6 +68,7 @@ impl Bindings {
source_files,
noop,
package_version,
gobjects,
}
}

Expand Down
15 changes: 13 additions & 2 deletions src/bindgen/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::path;

use crate::bindgen::bindings::Bindings;
use crate::bindgen::cargo::Cargo;
use crate::bindgen::config::{Braces, Config, Language, LineEndingStyle, Profile, Style};
use crate::bindgen::config::{Braces, Config, Language, LineEndingStyle, Profile, RenameRule, Style};
use crate::bindgen::error::Error;
use crate::bindgen::library::Library;
use crate::bindgen::parser::{self, Parse};
Expand Down Expand Up @@ -155,6 +155,15 @@ impl Builder {
self
}

#[allow(unused)]
pub fn with_gobject(mut self, gobject: bool) -> Builder {
self.config.gobject = gobject;
self.config.cpp_compat = true;
self.config.enumeration.rename_variants = RenameRule::QualifiedScreamingSnakeCase;
self.config.language = Language::C;
self
}

#[allow(unused)]
pub fn with_style(mut self, style: Style) -> Builder {
self.config.style = style;
Expand Down Expand Up @@ -367,13 +376,14 @@ impl Builder {
Default::default(),
true,
String::new(),
Vec::new(),
));
}

let mut result = Parse::new();

if self.std_types {
result.add_std_types();
result.add_std_types(&self.config);
}

for x in &self.srcs {
Expand Down Expand Up @@ -412,6 +422,7 @@ impl Builder {
result.functions,
result.source_files,
result.package_version,
result.gobjects,
)
.generate()
}
Expand Down
2 changes: 1 addition & 1 deletion src/bindgen/cdecl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl CDecl {
write!(out, "{} ", self.type_qualifers);
}

if config.language != Language::Cython {
if config.language != Language::Cython && !config.gobject {
if let Some(ref ctype) = self.type_ctype {
write!(out, "{} ", ctype.to_str());
}
Expand Down
5 changes: 5 additions & 0 deletions src/bindgen/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,8 @@ pub struct Config {
/// and creating a new InternalConfig struct would require more breaking
/// changes to our public API.
pub config_path: Option<StdPathBuf>,
/// Enable GObject generation
pub gobject: bool,
}

impl Default for Config {
Expand Down Expand Up @@ -1076,6 +1078,7 @@ impl Default for Config {
only_target_dependencies: false,
cython: CythonConfig::default(),
config_path: None,
gobject: false,
}
}
}
Expand Down Expand Up @@ -1109,6 +1112,7 @@ impl Config {
}
}

#[allow(unused)]
pub fn from_file<P: AsRef<StdPath>>(file_name: P) -> Result<Config, String> {
let config_text = fs::read_to_string(file_name.as_ref()).map_err(|_| {
format!(
Expand All @@ -1123,6 +1127,7 @@ impl Config {
Ok(config)
}

#[allow(unused)]
pub fn from_root_or_default<P: AsRef<StdPath>>(root: P) -> Config {
let c = root.as_ref().join("cbindgen.toml");

Expand Down
Loading