Skip to content

Commit 8140be4

Browse files
committed
Simpler approach to get helpers included in binary
1 parent d5679e6 commit 8140be4

File tree

3 files changed

+22
-51
lines changed

3 files changed

+22
-51
lines changed

crates/ark/src/debug.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@ use harp::utils::r_str_to_owned_utf8_unchecked;
88
use crate::interface::RMain;
99
use crate::interface::CAPTURE_CONSOLE_OUTPUT;
1010

11+
// Ensure the compiler includes the C entry points in `debug.c` in the binary.
12+
// We store function pointers in global variables that are declared "used". The
13+
// compiler is able to follow the chain of dependency from these variables to
14+
// the C functions and ultimately their Rust implementations defined below.
15+
16+
extern "C" {
17+
fn ark_print(x: libr::SEXP) -> *const ffi::c_char;
18+
fn ark_inspect(x: libr::SEXP) -> *const ffi::c_char;
19+
fn ark_trace_back() -> *const ffi::c_char;
20+
fn ark_display_value(x: libr::SEXP) -> *const ffi::c_char;
21+
}
22+
23+
#[used]
24+
static _ARK_PRINT: unsafe extern "C" fn(x: libr::SEXP) -> *const ffi::c_char = ark_print;
25+
#[used]
26+
static _ARK_INSPECT: unsafe extern "C" fn(x: libr::SEXP) -> *const ffi::c_char = ark_inspect;
27+
#[used]
28+
static _ARK_TRACE_BACK: unsafe extern "C" fn() -> *const ffi::c_char = ark_trace_back;
29+
#[used]
30+
static _ARK_DISPLAY_VALUE: unsafe extern "C" fn(x: libr::SEXP) -> *const ffi::c_char =
31+
ark_display_value;
32+
1133
// Implementations for entry points in `debug.c`.
1234

1335
#[no_mangle]

crates/harp/harp-macros/src/lib.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use quote::format_ident;
1010
use quote::quote;
1111
use quote::ToTokens;
1212
use syn::parse_macro_input;
13-
use syn::ItemFn;
1413
use syn::ItemStruct;
1514

1615
extern crate proc_macro;
@@ -214,32 +213,3 @@ pub fn register(_attr: TokenStream, item: TokenStream) -> TokenStream {
214213
let all = quote! { #function #registration };
215214
all.into()
216215
}
217-
218-
#[proc_macro_attribute]
219-
pub fn ensure_used(_attr: TokenStream, item: TokenStream) -> TokenStream {
220-
let input = parse_macro_input!(item as ItemFn);
221-
222-
let fn_name = &input.sig.ident;
223-
let static_name = format_ident!("_{fn_name}");
224-
225-
let inputs = &input.sig.inputs;
226-
let output = &input.sig.output;
227-
let abi = &input.sig.abi;
228-
let fn_type = quote! {
229-
#abi fn(#inputs) #output
230-
};
231-
232-
let out = quote! {
233-
#[no_mangle]
234-
#input
235-
236-
// Generate a static variable that hold the function pointer to force the
237-
// function to be compiled in
238-
#[used]
239-
#[no_mangle]
240-
#[doc(hidden)]
241-
static #static_name: #fn_type = #fn_name;
242-
};
243-
244-
TokenStream::from(out)
245-
}

crates/harp/src/utils.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
//
66
//
77

8-
use std::ffi;
98
use std::ffi::CStr;
109
use std::ffi::CString;
1110

12-
use harp_macros::ensure_used;
1311
use itertools::Itertools;
1412
use libr::*;
1513
use once_cell::sync::Lazy;
@@ -763,25 +761,6 @@ pub fn r_subset_vec(x: SEXP, indices: Vec<i64>) -> Result<SEXP> {
763761
Ok(out.sexp)
764762
}
765763

766-
// Ensure the compiler includes the C entry points in `debug.c` in the binary.
767-
// This must be in a module that is used by other parts of the program, i.e. not
768-
// in debug.rs. Otherwise the compiler will still omit the whole thing.
769-
770-
extern "C" {
771-
fn ark_print(x: libr::SEXP) -> *const ffi::c_char;
772-
fn ark_inspect(x: libr::SEXP) -> *const ffi::c_char;
773-
fn ark_trace_back() -> *const ffi::c_char;
774-
fn ark_display_value(x: libr::SEXP) -> *const ffi::c_char;
775-
}
776-
777-
#[ensure_used]
778-
pub extern "C" fn _placeholder() {
779-
unsafe { ark_print(libr::R_NilValue) };
780-
unsafe { ark_inspect(libr::R_NilValue) };
781-
unsafe { ark_trace_back() };
782-
unsafe { ark_display_value(libr::R_NilValue) };
783-
}
784-
785764
#[cfg(test)]
786765
mod tests {
787766
use libr::STRING_ELT;

0 commit comments

Comments
 (0)