-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add auto-generation of Mun ABI for Rust
- Loading branch information
Showing
6 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |