Skip to content

Commit

Permalink
feat(runtime_capi): add automatic generation of C bindings for the ru…
Browse files Browse the repository at this point in the history
…ntime
  • Loading branch information
Wodann committed Oct 25, 2019
1 parent 433ffa9 commit b49b336
Show file tree
Hide file tree
Showing 9 changed files with 481 additions and 4 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,3 @@ Cargo.lock
# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Generated Rust bindings
crates/mun_abi/src/autogen.rs
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "crates/mun_abi/c"]
path = crates/mun_abi/c
url = ../../mun-lang/abi-c
[submodule "crates/mun_runtime_capi/c"]
path = crates/mun_runtime_capi/c
url = ../../mun-lang/runtime-c.git
342 changes: 342 additions & 0 deletions crates/mun_abi/src/autogen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,342 @@
/* automatically generated by rust-bindgen */

#![allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]
use crate::Privacy;

#[doc = " <div rustbindgen derive=\"PartialEq\">"]
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Guid {
pub b: [u8; 16usize],
}
#[test]
fn bindgen_test_layout_Guid() {
assert_eq!(
::std::mem::size_of::<Guid>(),
16usize,
concat!("Size of: ", stringify!(Guid))
);
assert_eq!(
::std::mem::align_of::<Guid>(),
1usize,
concat!("Alignment of ", stringify!(Guid))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<Guid>())).b as *const _ as usize },
0usize,
concat!("Offset of field: ", stringify!(Guid), "::", stringify!(b))
);
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct TypeInfo {
pub guid: Guid,
pub name: *const ::std::os::raw::c_char,
}
#[test]
fn bindgen_test_layout_TypeInfo() {
assert_eq!(
::std::mem::size_of::<TypeInfo>(),
24usize,
concat!("Size of: ", stringify!(TypeInfo))
);
assert_eq!(
::std::mem::align_of::<TypeInfo>(),
8usize,
concat!("Alignment of ", stringify!(TypeInfo))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<TypeInfo>())).guid as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(TypeInfo),
"::",
stringify!(guid)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<TypeInfo>())).name as *const _ as usize },
16usize,
concat!(
"Offset of field: ",
stringify!(TypeInfo),
"::",
stringify!(name)
)
);
}
#[doc = " <div rustbindgen derive=\"Clone\">"]
#[repr(C)]
#[derive(Clone)]
pub struct FunctionSignature {
pub name: *const ::std::os::raw::c_char,
pub arg_types: *const TypeInfo,
pub return_type: *const TypeInfo,
pub num_arg_types: u16,
pub privacy: Privacy,
}
#[test]
fn bindgen_test_layout_FunctionSignature() {
assert_eq!(
::std::mem::size_of::<FunctionSignature>(),
32usize,
concat!("Size of: ", stringify!(FunctionSignature))
);
assert_eq!(
::std::mem::align_of::<FunctionSignature>(),
8usize,
concat!("Alignment of ", stringify!(FunctionSignature))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<FunctionSignature>())).name as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(FunctionSignature),
"::",
stringify!(name)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<FunctionSignature>())).arg_types as *const _ as usize },
8usize,
concat!(
"Offset of field: ",
stringify!(FunctionSignature),
"::",
stringify!(arg_types)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<FunctionSignature>())).return_type as *const _ as usize },
16usize,
concat!(
"Offset of field: ",
stringify!(FunctionSignature),
"::",
stringify!(return_type)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<FunctionSignature>())).num_arg_types as *const _ as usize },
24usize,
concat!(
"Offset of field: ",
stringify!(FunctionSignature),
"::",
stringify!(num_arg_types)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<FunctionSignature>())).privacy as *const _ as usize },
26usize,
concat!(
"Offset of field: ",
stringify!(FunctionSignature),
"::",
stringify!(privacy)
)
);
}
#[doc = " <div rustbindgen derive=\"Clone\">"]
#[repr(C)]
#[derive(Clone)]
pub struct FunctionInfo {
pub signature: FunctionSignature,
pub fn_ptr: *const ::std::os::raw::c_void,
}
#[test]
fn bindgen_test_layout_FunctionInfo() {
assert_eq!(
::std::mem::size_of::<FunctionInfo>(),
40usize,
concat!("Size of: ", stringify!(FunctionInfo))
);
assert_eq!(
::std::mem::align_of::<FunctionInfo>(),
8usize,
concat!("Alignment of ", stringify!(FunctionInfo))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<FunctionInfo>())).signature as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(FunctionInfo),
"::",
stringify!(signature)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<FunctionInfo>())).fn_ptr as *const _ as usize },
32usize,
concat!(
"Offset of field: ",
stringify!(FunctionInfo),
"::",
stringify!(fn_ptr)
)
);
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ModuleInfo {
pub path: *const ::std::os::raw::c_char,
pub functions: *const FunctionInfo,
pub num_functions: u32,
}
#[test]
fn bindgen_test_layout_ModuleInfo() {
assert_eq!(
::std::mem::size_of::<ModuleInfo>(),
24usize,
concat!("Size of: ", stringify!(ModuleInfo))
);
assert_eq!(
::std::mem::align_of::<ModuleInfo>(),
8usize,
concat!("Alignment of ", stringify!(ModuleInfo))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<ModuleInfo>())).path as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(ModuleInfo),
"::",
stringify!(path)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<ModuleInfo>())).functions as *const _ as usize },
8usize,
concat!(
"Offset of field: ",
stringify!(ModuleInfo),
"::",
stringify!(functions)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<ModuleInfo>())).num_functions as *const _ as usize },
16usize,
concat!(
"Offset of field: ",
stringify!(ModuleInfo),
"::",
stringify!(num_functions)
)
);
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct DispatchTable {
pub signatures: *const FunctionSignature,
pub fn_ptrs: *mut *const ::std::os::raw::c_void,
pub num_entries: u32,
}
#[test]
fn bindgen_test_layout_DispatchTable() {
assert_eq!(
::std::mem::size_of::<DispatchTable>(),
24usize,
concat!("Size of: ", stringify!(DispatchTable))
);
assert_eq!(
::std::mem::align_of::<DispatchTable>(),
8usize,
concat!("Alignment of ", stringify!(DispatchTable))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<DispatchTable>())).signatures as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(DispatchTable),
"::",
stringify!(signatures)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<DispatchTable>())).fn_ptrs as *const _ as usize },
8usize,
concat!(
"Offset of field: ",
stringify!(DispatchTable),
"::",
stringify!(fn_ptrs)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<DispatchTable>())).num_entries as *const _ as usize },
16usize,
concat!(
"Offset of field: ",
stringify!(DispatchTable),
"::",
stringify!(num_entries)
)
);
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct AssemblyInfo {
pub symbols: ModuleInfo,
pub dispatch_table: DispatchTable,
pub dependencies: *const *const ::std::os::raw::c_char,
pub num_dependencies: u32,
}
#[test]
fn bindgen_test_layout_AssemblyInfo() {
assert_eq!(
::std::mem::size_of::<AssemblyInfo>(),
64usize,
concat!("Size of: ", stringify!(AssemblyInfo))
);
assert_eq!(
::std::mem::align_of::<AssemblyInfo>(),
8usize,
concat!("Alignment of ", stringify!(AssemblyInfo))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<AssemblyInfo>())).symbols as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(AssemblyInfo),
"::",
stringify!(symbols)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<AssemblyInfo>())).dispatch_table as *const _ as usize },
24usize,
concat!(
"Offset of field: ",
stringify!(AssemblyInfo),
"::",
stringify!(dispatch_table)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<AssemblyInfo>())).dependencies as *const _ as usize },
48usize,
concat!(
"Offset of field: ",
stringify!(AssemblyInfo),
"::",
stringify!(dependencies)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<AssemblyInfo>())).num_dependencies as *const _ as usize },
56usize,
concat!(
"Offset of field: ",
stringify!(AssemblyInfo),
"::",
stringify!(num_dependencies)
)
);
}
2 changes: 1 addition & 1 deletion crates/mun_abi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Bindings are automatically generated from C
// Bindings are automatically generated from C on `cargo build`
mod autogen;

mod autogen_impl;
Expand Down
12 changes: 12 additions & 0 deletions crates/mun_runtime_capi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "mun_runtime_capi"
version = "0.1.0"
authors = ["Wodann <wodannson@gmail.com>"]
edition = "2018"

[dependencies]
mun_abi = { path = "../mun_abi" }
mun_runtime = { path = "../mun_runtime" }

[build-dependencies]
cbindgen = "0.9.1"
11 changes: 11 additions & 0 deletions crates/mun_runtime_capi/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::env;

use cbindgen;

fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();

cbindgen::generate(crate_dir)
.expect("Unable to generate Mun Runtime bindings.")
.write_to_file("c/include/mun_runtime.h");
}
1 change: 1 addition & 0 deletions crates/mun_runtime_capi/c
Submodule c added at f9d3d6
Loading

0 comments on commit b49b336

Please sign in to comment.