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

Allow generating a C header representing the exported symbols of a lib #10530

Closed
huonw opened this issue Nov 17, 2013 · 12 comments
Closed

Allow generating a C header representing the exported symbols of a lib #10530

huonw opened this issue Nov 17, 2013 · 12 comments
Labels
A-ffi Area: Foreign Function Interface (FFI) C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@huonw
Copy link
Member

huonw commented Nov 17, 2013

For example,

#[crate_type="lib"];

pub struct Foo { x: i16 }

#[no_mangle]
pub extern "C" fn bar(x: u64) -> Foo { ... }

pub fn ignored() { ... } // wrong ABI

would correspond to

#ifndef FILENAME_H
#define FILENAME_H

#include<stdint.h>

struct Foo {
    int16_t x;
}

struct Foo bar(uint64_t);

#endif

so that one can call into Rust from C more easily.

(This is the inverse operation of the proposed #[header="stdio.h"] mod stdio; functionality for using libclang to read C headers into a form that rustc understands: #2124)

@alexcrichton
Copy link
Member

Along the lines of this, I've heard that having a rust.h would be nice to have for things like:

typedef struct {
  void *data;
  uintptr_t len;
} RustSlice;

typedef struct {
  uintptr_t len;
  uintptr_t alloc;
  uint8_t data[0];
} RustString;

And other useful compiler definitions.

One tough part about this would be the C representation of FFI types, it would be really nice to able to interact with them from C, but our optimizations will nullable pointers and discriminant size may make them difficult.

@wycats
Copy link
Contributor

wycats commented Jan 13, 2014

👍

@alexcrichton
Copy link
Member

I started a version of this at https://gist.github.com/alexcrichton/4ea33d9a8b19ed15a65a, but it's only the bare bones infrastructure for getting something like this up and running.

@zkamsler
Copy link
Contributor

@alexcrichton That is pretty cool. You might want to stick to using the stdint.h typedefs for printing the integer types, since the widths can vary and the signed-ness of char is implementation defined.

@thehydroimpulse
Copy link
Contributor

/cc me

@steveklabnik steveklabnik added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Apr 21, 2015
@steveklabnik
Copy link
Member

bindgen does some of this, but rust.h would be nice for FFIing into Rust for sure

EDIT: whoops, I thought bindgen did both, but it does not, thanks @huonw

@ArtemGr
Copy link
Contributor

ArtemGr commented Apr 21, 2015

(Offtopic, kind of: Bindgen wasn't even a proper Rust crate, last time I checked, and building it on Debian was enough of a pain that I had to choose another route for my FFI needs. The wish to have bindgen-like functionality in Rust is, I think, primarily a wish to have it available everywhere. It could be satisfied with bindgen working as a proper Cargo dev-dependencies package able to generate the Rust stubs from a C header entirely from build.rs). Ups. That was a rant. : }

@tomjakubowski
Copy link
Contributor

I have some WIP stuff here. https://github.com/tomjakubowski/custard/tree/dev I think last I left it, it may have been in the middle of a refactor.

It follows the same idea as rustdoc: walk the typed AST, filter out things the tool cares about, clean up the types a bit, and emit something (a header file). In all honesty, it might make sense to make this a rustdoc 'backend' or something (if you squint, a header file is just another kind of documentation).

@luser
Copy link
Contributor

luser commented Apr 11, 2016

I'm sure everyone here is already aware, but the state of the art here appears to be rusty-cheddar: https://github.com/Sean1708/rusty-cheddar

@king6cong
Copy link
Contributor

rusty-cheddar and rusty-binder aren't very active those days. Should this be a feature important enough for Rust to be maintained by rust-lang-nursery or such? @alexcrichton

@aturon
Copy link
Member

aturon commented Apr 15, 2017

Closing; this should be tackled by an external tool.

@aturon aturon closed this as completed Apr 15, 2017
@pannous
Copy link

pannous commented Nov 18, 2022

State of the art is now https://github.com/eqrion/cbindgen/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ffi Area: Foreign Function Interface (FFI) C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests