Skip to content

Commit b80d441

Browse files
committed
Embed the R directory in the compiled extension
If the user already informed the R directory while compiling the extention, then it is not needed to explictly set R_HOME before using the library.
1 parent 91f9f2b commit b80d441

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

ext/extconf.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,12 @@
1010
exit 1
1111
end
1212

13+
File.open("config.h", "w") do |f|
14+
f.puts("#ifndef R_CONFIG_H")
15+
f.puts("#define R_CONFIG_H")
16+
r_home = $configure_args.has_key?('--with-R-dir') ? $configure_args['--with-R-dir'].inspect : 'NULL'
17+
f.puts("#define RSRUBY_R_HOME #{r_home}")
18+
f.puts("#endif")
19+
end
20+
1321
create_makefile("rsruby_c")

ext/rsruby.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
*/
3131

3232
#include "rsruby.h"
33+
#include "config.h"
34+
#include <stdlib.h>
3335

3436
/* Global list to protect R objects from garbage collection */
3537
/* This is inspired in $R_SRC/src/main/memory.c */
@@ -146,6 +148,9 @@ void init_R(int argc, char **argv){
146148

147149
char *defaultArgv[] = {"rsruby","-q","--vanilla"};
148150

151+
if (RSRUBY_R_HOME) {
152+
setenv("R_HOME", RSRUBY_R_HOME, 0);
153+
}
149154
Rf_initEmbeddedR(sizeof(defaultArgv) / sizeof(defaultArgv[0]), defaultArgv);
150155
R_Interactive = FALSE; //Remove crash menu (and other interactive R features)
151156
}

0 commit comments

Comments
 (0)