GithubHelp home page GithubHelp logo

Comments (3)

emilio avatar emilio commented on June 26, 2024

Could you create a reduced test-case? I suspect something weird is going on, because bindgen padding looks like pub __bindgen_padding_0: ....

from rust-bindgen.

emilio avatar emilio commented on June 26, 2024

Or like, something that I can run locally at least :)

from rust-bindgen.

ClementNerma avatar ClementNerma commented on June 26, 2024

Sure, here is an example:

# Cargo.toml
[package]
name = "buggy-crate"
version = "0.1.0"
edition = "2021"

[build-dependencies]
bindgen = "0.69.4"
pkg-config = "0.3.30"
// build.rs
use std::{env, iter, path::PathBuf};

pub static FUSE_VERSION: u32 = 316;
pub static FUSE_LIBNAME: &str = "fuse3";

fn fuse_binding_filter(builder: bindgen::Builder) -> bindgen::Builder {
    let mut builder = builder
        // Whitelist "fuse_*" symbols and blocklist everything else
        .allowlist_recursively(false)
        .allowlist_type("[fF][uU][sS][eE].*")
        .allowlist_function("[fF][uU][sS][eE].*")
        .allowlist_var("[fF][uU][sS][eE].*")
        .blocklist_type("fuse_log_func_t")
        .blocklist_function("fuse_set_log_func");

    // TODO: properly bind fuse_log_func_t and allowlist fuse_set_log_func again

    if cfg!(target_os = "macos") {
        // osxfuse needs this type
        builder = builder.allowlist_type("setattr_x");
    }

    builder
}

fn cuse_binding_filter(builder: bindgen::Builder) -> bindgen::Builder {
    builder
        // Whitelist "cuse_*" symbols and blocklist everything else
        .allowlist_recursively(false)
        .allowlist_type("[cC][uU][sS][eE].*")
        .allowlist_function("[cC][uU][sS][eE].*")
        .allowlist_var("[cC][uU][sS][eE].*")
}

fn generate_fuse_bindings(
    header: &str,
    fuse_lib: &pkg_config::Library,
    binding_filter: fn(bindgen::Builder) -> bindgen::Builder,
) {
    // Find header file
    let mut header_path: Option<PathBuf> = None;

    for include_path in fuse_lib.include_paths.iter() {
        let test_path = include_path.join(header);

        if test_path.exists() {
            header_path = Some(test_path);
            break;
        }
    }

    let header_path = header_path
        .unwrap_or_else(|| panic!("Cannot find {}", header))
        .to_str()
        .unwrap_or_else(|| panic!("Path to {} contains invalid unicode characters", header))
        .to_string();

    // Gather fuse defines
    let defines = fuse_lib.defines.iter().map(|(key, val)| match val {
        Some(val) => format!("-D{}={}", key, val),
        None => format!("-D{}", key),
    });

    // Gather include paths
    let includes = fuse_lib
        .include_paths
        .iter()
        .map(|dir| format!("-I{}", dir.display()));

    // API version definition
    let api_define = iter::once(format!("-DFUSE_USE_VERSION={FUSE_VERSION}"));

    // Chain compile flags
    let compile_flags = defines.chain(includes).chain(api_define);

    // Create bindgen builder
    let mut builder = bindgen::builder()
        // Add clang flags
        .clang_args(compile_flags)
        // Derive Debug, Copy and Default
        .derive_default(true)
        .derive_copy(true)
        .derive_debug(true)
        // Add CargoCallbacks so build.rs is rerun on header changes
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()));

    builder = binding_filter(builder);

    // Generate bindings
    let bindings = builder
        .header(header_path)
        .generate()
        .unwrap_or_else(|_| panic!("Failed to generate {} bindings", header));

    // Write bindings to file
    let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
    let bindings_path = out_dir.join(header.replace(".h", ".rs"));

    bindings
        .write_to_file(&bindings_path)
        .unwrap_or_else(|_| panic!("Failed to write {}", bindings_path.display()));
}

fn main() {
    let mut pkgcfg = pkg_config::Config::new();
    pkgcfg.cargo_metadata(false);

    // Find libfuse

    let fuse_lib = pkgcfg
        .cargo_metadata(true)
        .probe(FUSE_LIBNAME)
        .unwrap_or_else(|err| panic!("Failed to find pkg-config module {FUSE_LIBNAME} ({err})"));

    // Generate highlevel bindings
    generate_fuse_bindings("fuse.h", &fuse_lib, fuse_binding_filter);

    // Generate lowlevel bindings
    generate_fuse_bindings("fuse_lowlevel.h", &fuse_lib, fuse_binding_filter);

    // Generate lowlevel cuse bindings
    generate_fuse_bindings("cuse_lowlevel.h", &fuse_lib, cuse_binding_filter);
}

Note: you need to have both pkg-config and libfuse-dev installed.

Run this, and it will generate three bindings files in target. There you can find many structs with the problem I mentioned.

from rust-bindgen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.