Skip to content
This repository has been archived by the owner on Nov 23, 2017. It is now read-only.
/ moz-cheddar Public archive
forked from Sean1708/rusty-cheddar

UNMAINTAINED: A Rust crate for automatically generating C header files from Rust source file. Fork of sean1708's rusty-cheddar.

Notifications You must be signed in to change notification settings

mozilla/moz-cheddar

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This is a fork of rusty-cheddar until such time as the rusty-binder version is available.

moz-cheddar

Build Status crates.io MIT licensed

moz-cheddar is a library for converting Rust source files into C header files.

A note on versioning: While moz-cheddar is still pre-v1.0.0 it will likely go through numerous breaking changes. We attempt to follow semver and bump the minor version any time a new feature is added or output behavior is changed.

moz-cheddar targets C99 or later (for single line comments, and use of stdint.h and stdbool.h).

The most useful way to use moz-cheddar is in a build script. To do this add the following build-dependencies section to your Cargo.toml (to use it as a normal library simply replace build-dependencies with dependencies):

# Cargo.toml

[build-dependencies]
moz-cheddar = "0.4.0"

Then create the following build.rs:

// build.rs

extern crate cheddar;

fn main() {
    cheddar::Cheddar::new().expect("could not read manifest")
        .run_build("include/my_header.h");
}

This should work as is providing you've set up your project correctly. Don't forget to add a build = ... to your [package] section, see the cargo docs for more info.

moz-cheddar will then create a my_header.h file in include/. Note that moz-cheddar emits very few warnings, it is up to the programmer to write a library which can be correctly called from C.

API In a Module

You can also place your API in a module to help keep your source code neat. To do this you must supply the name of the module to Cheddar, then ensure that the items are available in the top-level scope:

// build.rs

extern crate cheddar;

fn main() {
    cheddar::Cheddar::new().expect("could not read manifest")
        .module("c_api").expect("malformed module path")
        .run_build("target/include/rusty.h");
}
// src/lib.rs

pub use c_api::*;

mod c_api {
    // api goes here ...
}

There are also .compile() and .compile_code() methods for finer control.

Conversions

In the examples below, boilerplate has been omitted from the header.

Typedefs

moz-cheddar converts pub type A = B into typedef B A;. Types containing generics are ignored.

Rust:

type UInt32 = u32;
pub type UInt64 = u64;
pub type MyOption<T> = Option<T>

Header:

// Some boilerplate omitted.
typedef uint64_t UInt64;
// Some more boilerplate omitted.

Enums

moz-cheddar will convert public enums which are marked #[repr(C)]. If the enum is generic or contains tuple or struct variants then cheddar will fail. moz-cheddar should correctly handle explicit discriminants.

Rust:

#[repr(C)]
pub enum Colours {
    Red = -6,
    Blue,
    Green = 7,
    Yellow,
}

// This would fail if it was #[repr(C)].
pub enum Tastes<T> {
    Savoury(T),
    Sweet,
}

// This would fail if it was public.
#[repr(C)]
enum Units {
    Kg(f64),
    M(f64),
    S(f64),
    A(f64),
    K(f64),
    Mol(f64),
    Cd(f64),
}

Header:

// Some boilerplate omitted.
typedef enum Colours {
        Colours_Red = -6,
        Colours_Blue,
        Colours_Green = 7,
        Colours_Yellow,
} Colours;
// Some more boilerplate omitted.

Structs

Structs are handled very similarly to enums, they must be public, marked #[repr(C)], and they must not contain generics. This currently only checked at the struct-level. Generic fields are not checked.

Rust:

#[repr(C)]
pub struct Person {
    age: i32,
    height: f64,
    weight: f64,
}

Header:

// Some boilerplate omitted.
typedef struct Person {
        int32_t age;
        double height;
        double weight;
} Person;
// Some more boilerplate omitted.

Opaque Structs

One common C idiom is to hide the implementation of a struct using an opaque struct, which can only be used behind a pointer. This is especially useful in Rust-C interfaces as it allows you to use any arbitrary Rust struct in C.

To define an opaque struct you must define a public newtype which is marked as #[repr(C)].

Rust:

struct Foo<T> {
    bar: i32,
    baz: Option<T>,
}

#[repr(C)]
pub struct MyCrate_Foo(Foo<PathBuf>);

Header:

// Some boilerplate omitted.
typedef struct MyCrate_Foo MyCrate_Foo;
// Some boilerplate omitted.

Note that the newtype must not be generic but the type that it wraps can be arbitrary.

Functions

For moz-cheddar to pick up on a function declaration it must be public, marked #[no_mangle] and have one of the following ABIs:

  • C
  • Cdecl
  • Stdcall
  • Fastcall
  • System

If you believe one of these has been included in error, or if one has been omitted, then please open an issue at the repo.

moz-cheddar will fail on functions which are marked as diverging (-> !).

Rust:

use std::ops::Add;

#[no_mangle]
pub extern fn hello() {
    println!("Hello!");
}

fn add<O, R, L: Add<R, Output=O>>(l: L, r: R) -> O {
    l + r
}

#[no_mangle]
#[allow(non_snake_case)]
pub extern fn MyAdd_add_u8(l: u8, r: u8) -> u8 {
    add(l, r)
}

#[no_mangle]
#[allow(non_snake_case)]
pub extern fn MyAdd_add_u16(l: u16, r: u16) -> u16 {
    add(l, r)
}

Header:

// Some boilerplate omitted.
void hello();

uint8_t MyAdd_add_u8(uint8_t l, uint8_t r);

uint16_t MyAdd_add_u16(uint16_t l, uint16_t r);
// Some more boilerplate omitted.

Paths

You must not put types defined in other modules in an exported type signature without hiding it behind an opaque struct. This is because the C compiler must know the layout of the type and moz-cheddar can not yet search other modules.

The very important exception to this rule are the C ABI types defined in the libc crate and std::os::raw. Types from these two modules must be fully qualified (e.g. libc::c_void or std::os::raw::c_longlong) so that they can be converted properly. Importing them with a use statement will not work.

Contributing

Contributions to moz-cheddar are more than welcome.

Bugs

If you find a bug or have a feature request please open an issue.

If you find the source code unclear in any way then I consider that a bug. I try to make my source code as clear as possible, so any help in that regard is appreciated.

Pull requests are also welcome, of course.

Tests

The tests require you to have a version (> v2.7.2) of CppHeaderParser installed for the version of Python which is installed as python (usually Python 2). Furthermore, due to the fact that the tests are a massive pile of wanky hacks, you must be in the same directory as moz-cheddar's Cargo.toml to successfully run them.

If you don't have this, try

pip install CppHeaderParser

About

UNMAINTAINED: A Rust crate for automatically generating C header files from Rust source file. Fork of sean1708's rusty-cheddar.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust 94.1%
  • Python 5.8%
  • Shell 0.1%