Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust version on crates.io #741

Open
ghenry opened this issue Dec 26, 2024 · 4 comments
Open

Rust version on crates.io #741

ghenry opened this issue Dec 26, 2024 · 4 comments

Comments

@ghenry
Copy link
Collaborator

ghenry commented Dec 26, 2024

Merry Christmas all!

I'm in the process of moving SentryPeer to Rust (and writing about it for pragprog.com) and am now looking at my usage of OpenDHT.

I see you have Rust bindings, so I'll play with those and look at how rustls handles bringing in boringssl automatically. I think you expect opendht c libs to be installed before building? I can't see a build.rs anyway.

Would you mind if I got this on to crates.io? I did the packing for Alpine and Homebrew if you remember?

Thanks,
Gavin.

@ghenry
Copy link
Collaborator Author

ghenry commented Dec 26, 2024

Nevermind, I see the way you're doing it in ffi.rs:

#[link(name = "opendht-c")]

@aberaud
Copy link
Member

aberaud commented Dec 26, 2024

Nice, tell us if you have any issue with the Rust binding :)

Merry Christmas

@ghenry
Copy link
Collaborator Author

ghenry commented Dec 26, 2024

Will do! Just playing with automating keeping it up to date using opendht-c and bindgen :-)

@ghenry
Copy link
Collaborator Author

ghenry commented Dec 28, 2024

I've created a build.rs to generate a bindings.rs file in the build dir and imported it into ffi.rs. However, it tramples over all your handrolled -sys extern style and unsafe calls as func sigs have changed, so I'll stop there.

I presume you're happy with just adding some new functions etc. by hand like you did already the next time you create one?

If not, add this to the top of ffi.rs:

// Ignore Rust's style conventions for our C FFI bindings
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

// Use the include! macro to dump our generated bindings right into our crate's main entry point,
// src/lib.rs:
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

and create a build.rs file:

/*
 *  Copyright (C) 2024 Gavin Henry
 *  Author: Gavin Henry <[email protected]>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

use std::env;
use std::path::PathBuf;

fn main() {
    // The bindgen::Builder is the main entry point
    // to bindgen, and lets you build up options for
    // the resulting bindings.
    let bindings = bindgen::Builder::default()
        // Where do look for things in wrapper.h
        .clang_arg("-I../include")
        // The input header we would like to generate
        // bindings for.
        .header("../c/opendht_c.h")
        // Pick the things we want to generate bindings for
        // .allowlist_function("sentrypeer_config_new|sentrypeer_config_destroy")
        // .allowlist_function("sip_message_event_new|sip_message_event_destroy")
        // .allowlist_function("sip_log_event")
        // Grab everything prefixed with dht_
        .allowlist_item("dht_.*")
        // Set whether string constants should be generated as &CStr instead of &[u8].
        .generate_cstr(true)
        // Tell cargo to invalidate the built crate whenever any of the
        // included header files changed.
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
        // Finish the builder and generate the bindings.
        .generate()
        // Unwrap the Result and panic on failure.
        .expect("Unable to generate bindings");

    // Write the bindings to the $OUT_DIR/bindings.rs file.
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings.rs, so can't use the OpenDHT C lib!");
}

Cargo.toml would need an update for bindgen as a build-dep and removing some other things from ffi.rs.

My goal was to get an official opendht crate and opendht-sys crate published.

Oh, and we could do with a cargo fmt on everything :-)

Thanks,
Gavin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants