Skip to content

Commit

Permalink
Add auto-generation of Mun ABI for Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed Sep 22, 2019
1 parent b26fe64 commit 39883d7
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "crates/mun_abi/c"]
path = crates/mun_abi/c
url = ../abi-c
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [
"crates\\mun_abi",
"crates\\mun_runtime",
"crates\\mun_symbols",
]
Expand Down
8 changes: 8 additions & 0 deletions crates/mun_abi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "mun_abi"
version = "0.1.0"
authors = ["Wodann <[email protected]>"]
edition = "2018"

[build-dependencies]
bindgen = "0.51"
48 changes: 48 additions & 0 deletions crates/mun_abi/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::env;
use std::path::PathBuf;

use bindgen::{self, callbacks::EnumVariantValue, callbacks::ParseCallbacks};

#[derive(Debug)]
struct RemoveVendorName;

impl ParseCallbacks for RemoveVendorName {
fn enum_variant_name(
&self,
enum_name: Option<&str>,
original_variant_name: &str,
_variant_value: EnumVariantValue,
) -> Option<String> {
Some(
original_variant_name
.trim_start_matches(enum_name.unwrap_or(""))
.to_string(),
)
}

fn item_name(&self, original_item_name: &str) -> Option<String> {
if original_item_name == "MunPrivacy_t" {
Some("Privacy".to_string())
} else {
Some(original_item_name.trim_start_matches("Mun").to_string())
}
}
}

fn main() {
let bindings = bindgen::Builder::default()
.header("c/include/mun_abi.h")
.whitelist_type("Mun.*")
.blacklist_type("MunPrivacy.*")
.parse_callbacks(Box::new(RemoveVendorName))
.generate()
.expect("Unable to generate bindings for 'mun_abi.h'");

let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect(&format!(
"Couldn't write bindings to '{}'",
out_path.as_path().to_string_lossy()
));
}
1 change: 1 addition & 0 deletions crates/mun_abi/c
Submodule c added at 97e94f
7 changes: 7 additions & 0 deletions crates/mun_abi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

#[repr(u8)]
pub enum Privacy {
Public = 0,
Private = 1,
}

0 comments on commit 39883d7

Please sign in to comment.