1
0

feat: add cbindgen support

This commit is contained in:
2026-05-15 17:02:29 +08:00
parent 684a24fcf8
commit 8d7f96d499
4 changed files with 188 additions and 3 deletions

View File

@@ -3,3 +3,5 @@
For how to utilize this library, please see our example located in `example/ppic`. It is a Qt project built with CMake and demonstrate basically all usage of exposed functions in this dynamic library.
All crucial spots are written in this example as the code comment so I don't write them in there again.
Due to the limitation of Rust compiler, you may need to enable `RUSTC_BOOTSTRAP=1` environment when creating binding (it is not necessary for just building this dynamic library). See [this GitHub issue](https://github.com/mozilla/cbindgen/issues/1015) for more info.

23
wfassoc-cdylib/build.rs Normal file
View File

@@ -0,0 +1,23 @@
extern crate cbindgen;
use std::{env, path::Path};
fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let crate_dir_path = Path::new(&crate_dir);
let cbindgen_toml_path = crate_dir_path.join("cbindgen.toml");
let cbindgen_config = cbindgen::Config::from_file(cbindgen_toml_path)
.expect("Unable to load cbindgen configuration file.");
let out_dir = env::var("OUT_DIR").unwrap();
let out_dir_path = Path::new(&out_dir);
let wfassoc_h_path = out_dir_path.join("wfassoc.h");
cbindgen::Builder::new()
.with_crate(crate_dir)
.with_config(cbindgen_config)
.generate()
.expect("Unable to generate bindings")
.write_to_file(wfassoc_h_path);
}

View File

@@ -0,0 +1,160 @@
# This is a template cbindgen.toml file with all of the default values.
# Some values are commented out because their absence is the real default.
#
# See https://github.com/mozilla/cbindgen/blob/main/docs.md#cbindgentoml
# for detailed documentation of every option here.
language = "C"
############## Options for Wrapping the Contents of the Header #################
# header = "/* Text to put at the beginning of the generated file. Probably a license. */"
# trailer = "/* Text to put at the end of the generated file */"
# include_guard = "my_bindings_h"
pragma_once = true
# autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */"
include_version = false
# namespace = "my_namespace"
namespaces = []
using_namespaces = []
sys_includes = []
includes = []
no_includes = false
cpp_compat = true
after_includes = ""
############################ Code Style Options ################################
braces = "SameLine"
line_length = 100
tab_width = 4
documentation = true
documentation_style = "auto"
documentation_length = "full"
line_endings = "Native" # also "LF", "CR", "CRLF", "Native"
############################# Codegen Options ##################################
style = "both"
sort_by = "Name" # default for `fn.sort_by` and `const.sort_by`
usize_is_size_t = true
[defines]
# "target_os = freebsd" = "DEFINE_FREEBSD"
# "feature = serde" = "DEFINE_SERDE"
[export]
include = []
exclude = []
# prefix = "CAPI_"
item_types = []
renaming_overrides_prefixing = false
[export.rename]
[export.body]
[export.mangle]
[fn]
rename_args = "None"
# must_use = "MUST_USE_FUNC"
# deprecated = "DEPRECATED_FUNC"
# deprecated_with_note = "DEPRECATED_FUNC_WITH_NOTE"
# no_return = "NO_RETURN"
# prefix = "START_FUNC"
# postfix = "END_FUNC"
args = "auto"
sort_by = "None"
[struct]
rename_fields = "None"
# must_use = "MUST_USE_STRUCT"
# deprecated = "DEPRECATED_STRUCT"
# deprecated_with_note = "DEPRECATED_STRUCT_WITH_NOTE"
rename_associated_constant = "None"
derive_constructor = false
derive_eq = false
derive_neq = false
derive_lt = false
derive_lte = false
derive_gt = false
derive_gte = false
[enum]
rename_variants = "UpperCase"
# must_use = "MUST_USE_ENUM"
# deprecated = "DEPRECATED_ENUM"
# deprecated_with_note = "DEPRECATED_ENUM_WITH_NOTE"
add_sentinel = false
prefix_with_name = true
derive_helper_methods = false
derive_const_casts = false
derive_mut_casts = false
# cast_assert_name = "ASSERT"
derive_tagged_enum_destructor = false
derive_tagged_enum_copy_constructor = false
enum_class = true
private_default_tagged_enum_constructor = false
[const]
allow_static_const = true
allow_constexpr = false
sort_by = "Name"
[macro_expansion]
bitflags = false
############## Options for How Your Rust library Should Be Parsed ##############
[parse]
parse_deps = false
# include = []
exclude = []
clean = false
extra_bindings = []
[parse.expand]
crates = ["wfassoc-cdylib"]
all_features = false
default_features = true
features = []

View File

@@ -214,9 +214,9 @@ impl From<Scope> for wfassoc::Scope {
#[repr(u32)]
#[derive(Debug, Copy, Clone, TryFromPrimitive)]
pub enum View {
User,
System,
Hybrid,
User = 0,
System = 1,
Hybrid = 2,
}
impl From<View> for wfassoc::View {