Last active
August 18, 2020 07:16
-
-
Save carlganz/a829d6e2254ffb5461dccf80b73b27cf to your computer and use it in GitHub Desktop.
R bindgen
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
extern crate bindgen; | |
use std::env; | |
use std::path::PathBuf; | |
fn main() { | |
println!("cargo:rustc-link-lib=R"); | |
println!("cargo:rustc-link-search=native={}", "/usr/lib/R/lib"); | |
// The bindgen::Builder is the main entry point | |
// to bindgen, and lets you build up options for | |
// the resulting bindings. | |
let bindings = bindgen::Builder::default() | |
// The input header we would like to generate | |
// bindings for. | |
.trust_clang_mangling(false) | |
.header("wrapper.h") | |
.clang_arg("-I/usr/share/R/include") | |
.clang_arg("-L/usr/lib/R/lib") | |
.clang_arg("-lR") | |
.blacklist_type("FP_NAN") | |
.blacklist_type("FP_INFINITE") | |
.blacklist_type("FP_ZERO") | |
.blacklist_type("FP_SUBNORMAL") | |
.blacklist_type("FP_NORMAL") | |
.blacklist_type("max_align_t") | |
// Finish the builder and generate the bindings. | |
.generate() | |
// Unwrap the Result and panic on failure. | |
.expect("Unable to generate bindings"); | |
// Write the bindings to the $OUT_DIR/bindings.rs file. | |
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); | |
bindings | |
.write_to_file(out_path.join("bindings.rs")) | |
.expect("Couldn't write bindings!"); | |
} |
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
#![allow(non_upper_case_globals)] | |
#![allow(non_camel_case_types)] | |
#![allow(non_snake_case)] | |
include!(concat!(env!("OUT_DIR"), "/bindings.rs")); | |
#[cfg(test)] | |
mod tests { | |
use std; | |
//use std::ffi::CString; | |
#[test] | |
fn should_work() { | |
unsafe{ | |
let p = 5.0 as f64; | |
println!("{}", p); | |
let r = super::Rf_ScalarReal(p); | |
super::Rf_protect(r); | |
let c = super::REAL(r); | |
println!("{}", *c); | |
assert_eq!(p, *c); | |
super::Rf_unprotect(1); | |
} | |
} | |
} |
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
#include <R.h> | |
#include <Rmath.h> | |
#include <Rinternals.h> |
Yes you are correct. Still segfaults though.
Did you ever get this to work?
Nope :(
Alright.. I'd still wish to interface R and Rust someday :D Some folks are working on it. Contact me if this is still a priority for you.
Not a lot of resources out there but there are
Actually, there is one super promising organisation here: ExtendR.
I heard about it from @andy-thomason.
We'll be very pleased to hear from you to help drive ExtendR development.
I've been tinkering with FFI ever since our last small correspondence. I full intend to contribute. I just started reading some of the code, and immediately got flushed. 💯
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In
lib.rs
: Shouldn't you callRf_protect
after getting the value ofr
?