1
1
package rprocessing ;
2
2
3
+ import java .io .File ;
4
+ import java .io .FileFilter ;
5
+ import java .io .FilenameFilter ;
6
+ import java .util .HashSet ;
7
+ import java .util .List ;
8
+ import java .util .Set ;
9
+
3
10
import javax .script .ScriptException ;
4
11
5
12
import org .renjin .eval .EvalException ;
6
13
14
+ import processing .core .PApplet ;
15
+ import processing .core .PConstants ;
7
16
import rprocessing .exception .NotFoundException ;
8
17
import rprocessing .exception .REvalException ;
9
18
import rprocessing .lancher .StandaloneSketch ;
19
+ import rprocessing .mode .library .LibraryImporter ;
10
20
import rprocessing .util .Printer ;
11
21
import rprocessing .util .StreamPrinter ;
12
22
@@ -21,6 +31,22 @@ public class Runner {
21
31
22
32
private static final boolean VERBOSE = Boolean .parseBoolean (System .getenv ("VERBOSE_RLANG_MODE" ));
23
33
34
+ private static final String ARCH ;
35
+
36
+ static {
37
+ log ("Getting the architecture." );
38
+ final int archBits = Integer .parseInt (System .getProperty ("sun.arch.data.model" ));
39
+ if (PApplet .platform == PConstants .MACOSX ) {
40
+ ARCH = "macosx" + archBits ;
41
+ } else if (PApplet .platform == PConstants .WINDOWS ) {
42
+ ARCH = "macosx" + archBits ;
43
+ } else if (PApplet .platform == PConstants .LINUX ) {
44
+ ARCH = "linux" + archBits ;
45
+ } else {
46
+ ARCH = "unknown" + archBits ;
47
+ }
48
+ }
49
+
24
50
private static void log (final Object ... objs ) {
25
51
if (!VERBOSE ) {
26
52
return ;
@@ -36,14 +62,15 @@ public static void main(final String[] args) throws Exception {
36
62
throw new NotFoundException ("The path of your R script is needed as an argument." );
37
63
}
38
64
try {
65
+ log ("Run the runner in Main." );
39
66
sketch = new StandaloneSketch (args );
40
67
runSketchBlocking (sketch , new StreamPrinter (System .out ), new StreamPrinter (System .err ));
41
68
42
69
// See https://github.com/gaocegege/Processing.R/issues/89
43
70
// It can't be reproduced, so comment the statement.
44
71
// System.exit(0);
45
72
} catch (final Throwable t ) {
46
- System .err .println (t );
73
+ System .err .println ("Runner raises an exception: " + t );
47
74
System .exit (-1 );
48
75
}
49
76
}
@@ -66,6 +93,16 @@ public static synchronized void runSketchBlocking(final RunnableSketch sketch,
66
93
rp .addPAppletToRContext ();
67
94
rp .evaluateCoreCode ();
68
95
96
+ final List <File > libDirs = sketch .getLibraryDirectories ();
97
+ if (libDirs != null ) {
98
+ LibraryImporter libraryImporter = new LibraryImporter (libDirs , rp .getRenjinEngine ());
99
+ final Set <String > libs = new HashSet <>();
100
+ for (final File dir : libDirs ) {
101
+ searchForExtraStuff (dir , libs );
102
+ }
103
+ libraryImporter .loadLibraries (libs );
104
+ }
105
+
69
106
try {
70
107
// Run Sketch.
71
108
rp .runBlock (args );
@@ -74,4 +111,62 @@ public static synchronized void runSketchBlocking(final RunnableSketch sketch,
74
111
throw new REvalException (ee .getMessage ());
75
112
}
76
113
}
114
+
115
+ /**
116
+ * Recursively search the given directory for jar files and directories containing dynamic
117
+ * libraries, adding them to the classpath and the library path respectively.
118
+ */
119
+ private static void searchForExtraStuff (final File dir , final Set <String > entries ) {
120
+ if (dir == null ) {
121
+ throw new IllegalArgumentException ("null dir" );
122
+ }
123
+
124
+ final String dirName = dir .getName ();
125
+ if (!dirName .equals (ARCH ) && dirName .matches ("^(macosx|windows|linux)(32|64)$" )) {
126
+ log ("Ignoring wrong architecture " + dir );
127
+ return ;
128
+ }
129
+
130
+ log ("Searching: " , dir );
131
+
132
+ final File [] dlls = dir .listFiles (new FilenameFilter () {
133
+ @ Override
134
+ public boolean accept (final File dir , final String name ) {
135
+ return name .matches ("^.+\\ .(so|dll|jnilib|dylib)$" );
136
+ }
137
+ });
138
+ if (dlls != null && dlls .length > 0 ) {
139
+ entries .add (dir .getAbsolutePath ());
140
+ } else {
141
+ log ("No DLLs in " , dir );
142
+ }
143
+
144
+ final File [] jars = dir .listFiles (new FilenameFilter () {
145
+ @ Override
146
+ public boolean accept (final File dir , final String name ) {
147
+ return name .matches ("^.+\\ .jar$" );
148
+ }
149
+ });
150
+ if (!(jars == null || jars .length == 0 )) {
151
+ for (final File jar : jars ) {
152
+ entries .add (jar .getAbsolutePath ());
153
+ }
154
+ } else {
155
+ log ("No JARs in " , dir );
156
+ }
157
+
158
+ final File [] dirs = dir .listFiles (new FileFilter () {
159
+ @ Override
160
+ public boolean accept (final File f ) {
161
+ return f .isDirectory () && f .getName ().charAt (0 ) != '.' ;
162
+ }
163
+ });
164
+ if (!(dirs == null || dirs .length == 0 )) {
165
+ for (final File d : dirs ) {
166
+ searchForExtraStuff (d , entries );
167
+ }
168
+ } else {
169
+ log ("No dirs in " , dir );
170
+ }
171
+ }
77
172
}
0 commit comments