Skip to content

Commit 5b7d871

Browse files
committed
[GR-14379] Use GraalVM SDK launchers.
PullRequest: fastr/2322
2 parents 3f7ffa6 + 1ac374d commit 5b7d871

File tree

11 files changed

+122
-406
lines changed

11 files changed

+122
-406
lines changed

com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/RMain.java

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -70,14 +70,6 @@ private static int runROrRScript(String command, String[] args, InputStream inSt
7070
}
7171
}
7272

73-
/**
74-
* Tells this R launcher to not process the {@code --jvm} and {@code --jvm.help}. Normally such
75-
* arguments are processed by the native launcher, but if there is no native launcher, we need
76-
* to explicitly process them in this class since the Truffle launcher does not count on this
77-
* eventuality.
78-
*/
79-
private static final boolean ignoreJvmArguments = "true".equals(System.getProperty("fastr.internal.ignorejvmargs"));
80-
8173
protected final InputStream inStream;
8274
protected final OutputStream outStream;
8375
protected final OutputStream errStream;
@@ -95,7 +87,6 @@ private static int runROrRScript(String command, String[] args, InputStream inSt
9587
private RCmdOptions options;
9688
private ConsoleHandler consoleHandler;
9789
private String[] rArguments;
98-
private boolean useJVM;
9990
private Context preparedContext; // to transfer between launch and execute when !launcherMode
10091

10192
private RMain(boolean launcherMode, InputStream inStream, OutputStream outStream, OutputStream errStream, int timeoutSecs) {
@@ -135,15 +126,6 @@ protected List<String> preprocessArguments(List<String> arguments, Map<String, S
135126

136127
List<String> unrecognizedArgs = new ArrayList<>();
137128
for (int i = 0; i < arguments.size(); i++) {
138-
if (!ignoreJvmArguments && ("--jvm.help".equals(arguments.get(i)) || "--vm.help".equals(arguments.get(i)))) {
139-
// This condition should be removed when FastR always ships with native launcher
140-
// that handles this option for us
141-
printJvmHelp();
142-
throw exit();
143-
} else if (!ignoreJvmArguments && "--jvm".equals(arguments.get(i))) {
144-
useJVM = true;
145-
} else
146-
147129
if (!recognizedArgsIndices[i]) {
148130
unrecognizedArgs.add(arguments.get(i));
149131
}
@@ -176,9 +158,6 @@ protected void launch(Builder contextBuilderIn) {
176158
}
177159
this.consoleHandler = ConsoleHandler.createConsoleHandler(options, null, inStream, outStream);
178160
Builder contextBuilder = contextBuilderIn;
179-
if (!ignoreJvmArguments && !useJVM) {
180-
contextBuilder.allowHostClassLookup(null);
181-
}
182161

183162
boolean isLLVMBackEnd = false;
184163
boolean debugLLVMLibs = false;
@@ -331,67 +310,4 @@ public synchronized Throwable fillInStackTrace() {
331310
return this;
332311
}
333312
}
334-
335-
// The following code is copied from org.graalvm.launcher.Launcher and it should be removed
336-
// when the R launcher always ships native version that handles --vm.help for us.
337-
338-
private static void printJvmHelp() {
339-
System.out.println("JVM options:");
340-
printOption("--vm.classpath <...>", "A " + File.pathSeparator + " separated list of classpath entries that will be added to the JVM's classpath");
341-
printOption("--vm.D<name>=<value>", "Set a system property");
342-
printOption("--vm.esa", "Enable system assertions");
343-
printOption("--vm.ea[:<packagename>...|:<classname>]", "Enable assertions with specified granularity");
344-
printOption("--vm.agentlib:<libname>[=<options>]", "Load native agent library <libname>");
345-
printOption("--vm.agentpath:<pathname>[=<options>]", "Load native agent library by full pathname");
346-
printOption("--vm.javaagent:<jarpath>[=<options>]", "Load Java programming language agent");
347-
printOption("--vm.Xbootclasspath/a:<...>", "A " + File.pathSeparator + " separated list of classpath entries that will be added to the JVM's boot classpath");
348-
printOption("--vm.Xmx<size>", "Set maximum Java heap size");
349-
printOption("--vm.Xms<size>", "Set initial Java heap size");
350-
printOption("--vm.Xss<size>", "Set java thread stack size");
351-
}
352-
353-
private static void printOption(String option, String description, int indentation) {
354-
String indent = spaces(indentation);
355-
String desc = wrap(description != null ? description : "");
356-
String nl = System.lineSeparator();
357-
String[] descLines = desc.split(nl);
358-
int optionWidth = 45;
359-
if (option.length() >= optionWidth && description != null) {
360-
System.out.println(indent + option + nl + indent + spaces(optionWidth) + descLines[0]);
361-
} else {
362-
System.out.println(indent + option + spaces(optionWidth - option.length()) + descLines[0]);
363-
}
364-
for (int i = 1; i < descLines.length; i++) {
365-
System.out.println(indent + spaces(optionWidth) + descLines[i]);
366-
}
367-
}
368-
369-
static void printOption(String option, String description) {
370-
printOption(option, description, 2);
371-
}
372-
373-
private static String spaces(int length) {
374-
return new String(new char[length]).replace('\0', ' ');
375-
}
376-
377-
private static String wrap(String s) {
378-
final int width = 120;
379-
StringBuilder sb = new StringBuilder(s);
380-
int cursor = 0;
381-
while (cursor + width < sb.length()) {
382-
int i = sb.lastIndexOf(" ", cursor + width);
383-
if (i == -1 || i < cursor) {
384-
i = sb.indexOf(" ", cursor + width);
385-
}
386-
if (i != -1) {
387-
sb.replace(i, i + 1, System.lineSeparator());
388-
cursor = i;
389-
} else {
390-
break;
391-
}
392-
}
393-
return sb.toString();
394-
}
395-
396-
// End of copied code
397313
}

com.oracle.truffle.r.native/run/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ rcmds: $(FASTR_BIN_DIR)/R
7575
$(FASTR_BIN_DIR)/R: Makefile R.sh Rscript.sh Rscript_exec.sh Rclasspath.sh
7676
cp -r $(BIN_FILES) $(FASTR_BIN_DIR)
7777
# Note: in the GraalVM release support distribution we override exec/R and Rscript
78-
# with scripts R_legacy and Rscript_legacy from com.oracle.truffle.r.release
78+
# with scripts R_launcher and Rscript_launcher from com.oracle.truffle.r.release
7979
# overide bin/exec/R
8080
cp R.sh $(FASTR_BIN_DIR)/exec/R
8181
# override bin/Rscript

com.oracle.truffle.r.native/run/Rscript.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# Rscript is a script in the bin directory that simply invokes bin/execRextras/Rscript
2828
# N.B. This can't be in bin/exec as then it is treated as a sub-architecture
2929
# NOTE: this is used only in the development build,
30-
# com.oracle.truffle.r.release/src/Rscript_legacy is used in the release
30+
# com.oracle.truffle.r.release/src/Rscript_launcher is used in the release
3131

3232
source="${BASH_SOURCE[0]}"
3333
while [ -h "$source" ] ; do source="$(readlink "$source")"; done

com.oracle.truffle.r.native/run/Rscript_exec.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
#
3-
# Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
3+
# Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
44
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
#
66
# This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,7 @@
2525
# Startup FastR (Rscript) using the mx tool (development)
2626
# This is exec'ed by the (generic) Rscript script in the parent directory.
2727
# NOTE: this is used only in the development build,
28-
# com.oracle.truffle.r.release/src/Rscript_legacy is used in the release
28+
# com.oracle.truffle.r.release/src/Rscript_launcher is used in the release
2929

3030
source="${BASH_SOURCE[0]}"
3131
while [ -h "$source" ] ; do source="$(readlink "$source")"; done
Lines changed: 7 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22
#
3-
# Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
3+
# Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
44
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
#
66
# This code is free software; you can redistribute it and/or modify it
@@ -39,19 +39,15 @@ location="$( cd -P "$( dirname "$source" )" && pwd )"
3939
fastr_home="$( dirname "$location" )"
4040

4141
silent=0
42-
uninstall=0
4342
verbose=0
4443
for arg in "$@"; do
4544
if [[ $arg == "--silent" ]]; then
4645
silent=1
4746
elif [[ $arg == "--verbose" ]]; then
4847
verbose=1
49-
elif [[ $arg == "uninstall" ]]; then
50-
uninstall=1
5148
elif [[ $arg == "--help" ]]; then
52-
echo "Usage: install_r_native_image [uninstall] [--silent]"
53-
echo "When 'uninstall' argument is not present: builds and installs native image of the R runtime."
54-
echo "When 'uninstall' argument is present: uninstalls previously installed native image of the R runtime."
49+
echo "Usage: install_r_native_image [--silent]"
50+
echo "Builds and installs native image of the R runtime."
5551
echo "Use the --silent option to turn off the confirmation when installing."
5652
echo "Use the --verbose option to turn on detailed logging."
5753
exit 0
@@ -64,20 +60,10 @@ function log {
6460
fi
6561
}
6662

67-
if [[ $uninstall -eq 1 ]]; then
68-
echo "Uninstalling native image of R runtime..."
69-
log "current working directory: ${PWD}"
70-
log "relative FastR home: ${fastr_home}"
71-
mv "$fastr_home/bin/exec_R.backup" "$fastr_home/bin/exec/R"
72-
mv "$fastr_home/bin/Rscript.backup" "$fastr_home/bin/Rscript"
73-
rm -f "$fastr_home/bin/RorRscriptDispatcher"
74-
echo "Native image of R runtime uninstalled"
75-
exit 0
76-
fi
77-
63+
echo "Note: this script is deprecated use: \$GRAALVM_HOME/bin/gu rebuild-images R"
7864
if [[ $silent -eq 0 ]]; then
79-
echo "This script is going to build a native image of the R runtime and update the R launchers to use that image as the default, i.e., when '--jvm' option is not used. You can uninstall the native image by running this script with the 'uninstall' argument. Run this script with '--help' for more details."
80-
echo "The build takes several minutes and needs a minimum of 6GB of RAM and 150MB of free disk space. The computer may lag during the build."
65+
echo "This script is going to build a native image of the R runtime and update the R launchers to use that image as the default, i.e., when '--jvm' option is not used."
66+
echo "The build takes several minutes and needs a minimum of 8GB of RAM and 200MB of free disk space. The computer may lag during the build."
8167
read -p "Are you sure you want to build and install the native image of the R runtime? (Yy/Nn) " -n 1 -r
8268
echo
8369
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
@@ -91,10 +77,8 @@ cd "$fastr_home/bin"
9177

9278
graalvm_home_bin="../../../../bin"
9379
native_image=$graalvm_home_bin/native-image
94-
launcher_relative_home="jre/languages/R/bin/RMain"
9580
if [ ! -f "$native_image" ]; then
9681
graalvm_home_bin="../../../bin"
97-
launcher_relative_home="languages/R/bin/RMain"
9882
native_image=$graalvm_home_bin/native-image
9983
fi
10084
if [ ! -f "$native_image" ]; then
@@ -104,50 +88,6 @@ fi
10488
graalvm_home="$( dirname "${graalvm_home_bin}" )"
10589
log "current working directory: ${PWD}"
10690
log "relative graalvm_home: ${graalvm_home}"
107-
log "launcher relative home: ${launcher_relative_home}"
10891
log "relative FastR home: ${fastr_home}"
10992

110-
fastr_launcher_ni_args=(
111-
--no-fallback
112-
--initialize-at-build-time
113-
-H:+ReportExceptionStackTraces
114-
-cp "${fastr_home}/fastr-launcher.jar:${fastr_home}/../../lib/graalvm/launcher-common.jar"
115-
--language:R
116-
--language:llvm
117-
--tool:all
118-
-Dorg.graalvm.version=19.0.3
119-
-H:-ParseRuntimeOptions
120-
-Dorg.graalvm.launcher.relative.language.home=bin/RMain
121-
-Dorg.graalvm.launcher.classpath=${fastr_home}/../../lib/graalvm/launcher-common.jar:${fastr_home}/fastr-launcher.jar
122-
-Dorg.graalvm.launcher.relative.home=${launcher_relative_home}
123-
-Dfastr.awt.support=false
124-
-H:Class=com.oracle.truffle.r.launcher.RMain
125-
)
126-
if [[ $verbose -eq 1 ]]; then
127-
fastr_launcher_ni_args+=('--verbose')
128-
fi
129-
130-
log "Running:" $native_image "${fastr_launcher_ni_args[@]}" -H:Name=RMain
131-
$native_image "${fastr_launcher_ni_args[@]}" -H:Name=RMain
132-
133-
log "Creating backup of the R and Rscript launchers"
134-
cp "exec/R" "exec_R.backup"
135-
cp "Rscript" "Rscript.backup"
136-
137-
log "Patching the R and Rscript launchers to dispatch to the generated native image"
138-
sed -e '/^## REMOVE FOR NATIVE IMAGE: BEGIN/,/^## REMOVE FOR NATIVE IMAGE: END/d;' "exec_R.backup" | \
139-
sed -e 's|^exec "${fastr_home}/../../bin/java" .*|exec "$R_HOME/bin/RMain" R ${FASTR_INTERNAL_ARGS[@]} "$@"|' > "exec/R"
140-
sed -e '/^## REMOVE FOR NATIVE IMAGE: BEGIN/,/^## REMOVE FOR NATIVE IMAGE: END/d;' "Rscript.backup" | \
141-
sed -e 's|^exec "${fastr_home}/../../bin/java" .*|exec "$R_HOME/bin/RMain" Rscript ${FASTR_INTERNAL_ARGS[@]} "$@"|' > "Rscript"
142-
143-
if [ "$R_INSTALL_NATIVE_IMAGE_VERY_VERBOSE" = "true" ]; then
144-
echo "${PWD}exec/R contents:"
145-
echo "*****************\n"
146-
cat exec/R
147-
echo "*****************\n"
148-
echo "${PWD}/Rscript contents:"
149-
cat Rscript
150-
echo "*****************\n"
151-
echo "ls -la ${PWD}"
152-
ls -la ${PWD}
153-
fi
93+
"$native_image" --macro:RMain-launcher
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
4+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5+
#
6+
# This code is free software; you can redistribute it and/or modify it
7+
# under the terms of the GNU General Public License version 3 only, as
8+
# published by the Free Software Foundation.
9+
#
10+
# This code is distributed in the hope that it will be useful, but WITHOUT
11+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
# version 3 for more details (a copy is included in the LICENSE file that
14+
# accompanied this code).
15+
#
16+
# You should have received a copy of the GNU General Public License version
17+
# 3 along with this work; if not, write to the Free Software Foundation,
18+
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19+
#
20+
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21+
# or visit www.oracle.com if you need additional information or have any
22+
# questions.
23+
#
24+
25+
# This script is deployed as
26+
# jre/languages/R/bin/exec/R in JDK8
27+
# languages/R/bin/exec/R in JDK11
28+
# It forwards to the RMain launcher, which is a regular Graal SDK launcher
29+
30+
source="${BASH_SOURCE[0]}"
31+
while [ -h "$source" ] ; do
32+
prev_source="$source"
33+
source="$(readlink "$source")";
34+
if [[ "$source" != /* ]]; then
35+
# if the link was relative, it was relative to where it came from
36+
dir="$( cd -P "$( dirname "$prev_source" )" && pwd )"
37+
source="$dir/$source"
38+
fi
39+
done
40+
location="$( cd -P "$( dirname "$source" )" && pwd )"
41+
fastr_home="$( dirname "$( dirname "${location}" )" )"
42+
43+
exec "${fastr_home}/bin/RMain" R ${FASTR_INTERNAL_ARGS[@]} "$@"

0 commit comments

Comments
 (0)