Skip to content

Commit

Permalink
Rewrite scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Patel committed May 4, 2024
1 parent 7c2d54d commit 96b6bc4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 58 deletions.
20 changes: 6 additions & 14 deletions rust/r2api/Makefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
R2LIB=r_core
BINDGEN=bindgen
CARGO=cargo

CFLAGS+=$(shell pkg-config --cflags $(R2LIB))
R2INC=$(shell pkg-config --variable=includedir $(R2LIB))

BINDGEN_FLAGS+=--allowlist-function='r_.*'
BINDGEN_FLAGS+=--allowlist-function='sdb_.*'
BINDGEN_FLAGS+=--blocklist-item=IPPORT_RESERVED
BINDGEN_FLAGS+=--with-derive-default

# BINDGEN_FLAGS+= --opaque-type 'std::.*'
.PHONY: all test_static pub

all:
cargo fmt
$(BINDGEN) $(BINDGEN_FLAGS) $(R2INC)/libr/$(R2LIB).h -- $(CFLAGS) > src/lib.rs
$(CARGO) fmt
$(CARGO) build
$(CARGO) run

test_static:
$(CARGO) run --features=static

pub:
$(CARGO) publish --allow-dirty
$(CARGO) publish
66 changes: 37 additions & 29 deletions rust/r2api/build.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
extern crate bindgen;

use std::env;
use std::path::PathBuf;
// use pkgconfig module
use std::process::Command;

fn main() {
// Tell cargo to statically link to the radare2-build static lib
let mut pkgconf_args = vec!["--cflags", "--libs"];

#[cfg(feature = "static")]
{
// TODO: this ../radare2-bild path should be dynamically constructed
// println!("cargo:rustc-link-search=../radare2-build/radare2/libr/");
// println!("cargo:rustc-link-lib=r");
println!("cargo:rustc-link-arg=../radare2-build/radare2/libr/libr.a");
}
#[cfg(not(feature = "static"))]
{
println!("cargo:rustc-link-lib=r_io");
println!("cargo:rustc-link-lib=r_asm");
println!("cargo:rustc-link-lib=r_arch");
println!("cargo:rustc-link-lib=r_esil");
println!("cargo:rustc-link-lib=r_anal");
println!("cargo:rustc-link-lib=r_search");
println!("cargo:rustc-link-lib=r_util");
println!("cargo:rustc-link-lib=r_reg");
println!("cargo:rustc-link-lib=r_debug");
println!("cargo:rustc-link-lib=r_lang");
println!("cargo:rustc-link-lib=r_bin");
println!("cargo:rustc-link-lib=r_syscall");
println!("cargo:rustc-link-lib=r_core");
println!("cargo:rustc-link-lib=r_socket");
println!("cargo:rustc-link-lib=r_fs");
println!("cargo:rustc-link-lib=r_cons");
pkgconf_args.push("--static");

pkgconf_args.push("r_core");

let cfg = Command::new("pkg-config")
.args(pkgconf_args)
.output()
.expect("Can't find r_core")
.stdout;
let cfg_str = String::from_utf8(cfg).unwrap();

let mut flags: Vec<String> = Vec::new();
for f in cfg_str.split(' ') {
if let Some(include) = f.strip_prefix("-I") {
flags.push(String::from(f));
println!("cargo:include={include}");
} else if let Some(path) = f.strip_prefix("-L") {
println!("cargo:rustc-link-search=native={path}");
} else if let Some(lib) = f.strip_prefix("-l") {
// Work around bug where pkg-config provides dylibs even if --static is passed
let skip = cfg!(feature = "static") && lib.starts_with("r_");
if !skip {
println!("cargo:rustc-link-lib=dylib={lib}");
}
} else if f.ends_with(".a") {
println!("cargo:rustc-link-arg={f}")
} else {
println!("cargo:warning=Unknown flag from pkg-config: {f}");
}
}

// Tell cargo to invalidate the built crate whenever the wrapper changes
Expand All @@ -45,7 +49,11 @@ fn main() {
.header("wrapper.h")
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.allowlist_function("r_.*")
.allowlist_function("sdb_.*")
.blocklist_item("IPPORT_RESERVED")
.clang_args(flags)
.derive_default(true)
// Finish the builder and generate the bindings.
.generate()
Expand Down
21 changes: 7 additions & 14 deletions rust/r2api/src/bin/example.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
use r2api::{r_cons_flush, r_core_cmd0, r_core_free, r_core_new, r_io_new};
use std::ffi::CString;
use std::ffi::CStr;

macro_rules! c_str {
($lit:expr) => {
std::ffi::CStr::from_ptr(concat!($lit, "\0").as_ptr() as *const i8).as_ptr()
macro_rules! static_cstr {
($str: tt) => {
CStr::from_bytes_with_nul_unchecked(concat!($str, "\x00").as_bytes()).as_ptr()
};
}

fn main() {
/*
let c = r_core::new();
c.cmd("?e hello world");
c.free();
*/

unsafe {
let c = r_core_new();
let pangram = "?E Hello World\x00".as_ptr() as *const i8;
let _ = r_io_new();
r_core_cmd0(c, pangram);
r_core_cmd0(c, CString::new("b").unwrap().as_ptr());
r_core_cmd0(c, c_str!("?e woot"));
r_core_cmd0(c, static_cstr!("?E Hello World"));
r_core_cmd0(c, static_cstr!("b"));
r_core_cmd0(c, static_cstr!("?e woot"));
r_cons_flush();
r_core_free(c);
}
Expand Down
2 changes: 1 addition & 1 deletion rust/r2api/wrapper.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
// #include <r_core.h>
#include <r_core.h>

0 comments on commit 96b6bc4

Please sign in to comment.