From 3a18dc6fd54ebf6840f9b6b275d0eeaa764235df Mon Sep 17 00:00:00 2001
From: Danilo Ansaloni <danilo.ansaloni@oracle.com>
Date: Sat, 7 Apr 2018 07:46:35 +0200
Subject: [PATCH] Register FastR component.

---
 .../truffle/r/launcher/LegacyLauncher.java    | 46 ++++++++++++++++++
 com.oracle.truffle.r.release/src/R_legacy     | 47 +++++++++++++++++++
 .../src/Rscript_legacy                        | 47 +++++++++++++++++++
 mx.fastr/mx_fastr_dists.py                    | 17 +++++++
 mx.fastr/suite.py                             | 18 ++++---
 5 files changed, 168 insertions(+), 7 deletions(-)
 create mode 100644 com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/LegacyLauncher.java
 create mode 100755 com.oracle.truffle.r.release/src/R_legacy
 create mode 100755 com.oracle.truffle.r.release/src/Rscript_legacy

diff --git a/com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/LegacyLauncher.java b/com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/LegacyLauncher.java
new file mode 100644
index 0000000000..2faf5416d6
--- /dev/null
+++ b/com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/LegacyLauncher.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.r.launcher;
+
+import java.lang.reflect.Method;
+import java.util.Arrays;
+
+import org.graalvm.polyglot.Engine;
+
+public final class LegacyLauncher {
+
+    public static void main(String[] args) throws NoSuchMethodException, SecurityException {
+        String className = args[0];
+        Method loadClassMethod = Engine.class.getDeclaredMethod("loadLanguageClass", String.class);
+        try {
+            loadClassMethod.setAccessible(true);
+            Class<?> result = (Class<?>) loadClassMethod.invoke(null, className);
+            result.getMethod("main", String[].class).invoke(null, (Object) Arrays.copyOfRange(args, 1, args.length));
+        } catch (NoSuchMethodException | SecurityException e) {
+            e.printStackTrace();
+        } catch (Exception e) {
+            e.printStackTrace();
+            System.exit(1);
+        }
+    }
+}
diff --git a/com.oracle.truffle.r.release/src/R_legacy b/com.oracle.truffle.r.release/src/R_legacy
new file mode 100755
index 0000000000..0022bda131
--- /dev/null
+++ b/com.oracle.truffle.r.release/src/R_legacy
@@ -0,0 +1,47 @@
+#!/usr/bin/env bash
+
+source="${BASH_SOURCE[0]}"
+while [ -h "$source" ] ; do
+  prev_source="$source"
+  source="$(readlink "$source")";
+  if [[ "$source" != /* ]]; then
+    # if the link was relative, it was relative to where it came from
+    dir="$( cd -P "$( dirname "$prev_source" )" && pwd )"
+    source="$dir/$source"
+  fi
+done
+EXEC="$( cd -P "$( dirname "$source" )" && pwd )"
+# This script is deployed as jre/languages/R/bin/exec/R
+exec_location=jre/languages/R/bin/exec
+slashes=${exec_location//[^\/]/}
+dir_count=${#slashes}
+JRE="$EXEC"
+for i in $(seq 1 ${dir_count}); do
+  JRE="$( dirname "$JRE" )"
+done
+
+JAVA_ARGS=()
+PROGRAM_ARGS=()
+for opt in "${@:1}"
+do
+    case $opt in
+        -J:*)
+            opt=${opt:3}
+            JAVA_ARGS+=("$opt") ;;
+        *)
+            PROGRAM_ARGS+=("$opt") ;;
+    esac
+done
+
+JAVA_HOME="$( dirname "$JRE" )"
+
+GRAALVMCP=""
+TMPCP="%%GRAALVMCP"
+for cp in ${TMPCP//:/ }
+do
+    GRAALVMCP="$GRAALVMCP$JAVA_HOME/$cp:"
+done
+
+# we can invoke FastR directly, but we do have to set R_HOME
+export R_HOME="$JRE/languages/R"
+exec "$JAVA_HOME/bin/java" -cp "$GRAALVMCP" -noverify -Dgraal.TruffleCompilationThreshold=10000 -Dgraal.TruffleCompilerThreads=2 -Xmx8g "${JAVA_ARGS[@]}" com.oracle.graalvm.launcher.LegacyLauncher com.oracle.truffle.r.launcher.RCommand "${PROGRAM_ARGS[@]}"
diff --git a/com.oracle.truffle.r.release/src/Rscript_legacy b/com.oracle.truffle.r.release/src/Rscript_legacy
new file mode 100755
index 0000000000..bf3213e478
--- /dev/null
+++ b/com.oracle.truffle.r.release/src/Rscript_legacy
@@ -0,0 +1,47 @@
+#!/usr/bin/env bash
+
+source="${BASH_SOURCE[0]}"
+while [ -h "$source" ] ; do
+  prev_source="$source"
+  source="$(readlink "$source")";
+  if [[ "$source" != /* ]]; then
+    # if the link was relative, it was relative to where it came from
+    dir="$( cd -P "$( dirname "$prev_source" )" && pwd )"
+    source="$dir/$source"
+  fi
+done
+BIN="$( cd -P "$( dirname "$source" )" && pwd )"
+# This script is deployed as jre/languages/R/bin/Rscript
+bin_location=jre/languages/R/bin
+slashes=${bin_location//[^\/]/}
+dir_count=${#slashes}
+JRE="$BIN"
+for i in $(seq 1 ${dir_count}); do
+  JRE="$( dirname "$JRE" )"
+done
+
+JAVA_ARGS=()
+PROGRAM_ARGS=()
+for opt in "${@:1}"
+do
+    case $opt in
+        -J:*)
+            opt=${opt:3}
+            JAVA_ARGS+=("$opt") ;;
+        *)
+            PROGRAM_ARGS+=("$opt") ;;
+    esac
+done
+
+JAVA_HOME="$( dirname "$JRE" )"
+
+GRAALVMCP=""
+TMPCP="%%GRAALVMCP"
+for cp in ${TMPCP//:/ }
+do
+    GRAALVMCP="$GRAALVMCP$JAVA_HOME/$cp:"
+done
+
+# we can invoke FastR directly, but we do have to set R_HOME
+export R_HOME="$JRE/languages/R"
+exec "$JAVA_HOME/bin/java" -cp "$GRAALVMCP" -noverify -Dgraal.TruffleCompilationThreshold=10000 -Dgraal.TruffleCompilerThreads=2 -Xmx4g "${JAVA_ARGS[@]}" com.oracle.graalvm.launcher.LegacyLauncher com.oracle.truffle.r.launcher.RscriptCommand "${PROGRAM_ARGS[@]}"
diff --git a/mx.fastr/mx_fastr_dists.py b/mx.fastr/mx_fastr_dists.py
index 835c2cc9de..22b48e852c 100644
--- a/mx.fastr/mx_fastr_dists.py
+++ b/mx.fastr/mx_fastr_dists.py
@@ -21,6 +21,7 @@
 # questions.
 #
 import mx
+import mx_sdk
 import mx_fastr
 import os, string, shutil
 from os.path import join, basename, isfile
@@ -208,3 +209,19 @@ def mx_post_parse_cmd_line(opts):
         if isinstance(dist, mx.JARDistribution):
             dist.set_archiveparticipant(FastRArchiveParticipant(dist))
 
+mx_sdk.register_component(mx_sdk.GraalVmLanguage(
+    name='FastR',
+    id='R',
+    documentation_files=['extracted-dependency:fastr:FASTR_GRAALVM_SUPPORT<rffi>/README_FASTR'],
+    license_files=[
+        'link:<support>/COPYRIGHT_FASTR',
+        'link:<support>/LICENSE_FASTR',
+    ],
+    third_party_license_files=[],
+    truffle_jars=['dependency:fastr:FASTR'],
+    support_distributions=['extracted-dependency:fastr:FASTR_GRAALVM_SUPPORT<rffi>'],
+    provided_executables=[
+        'link:<support>/bin/Rscript',
+        'link:<support>/bin/exec/R',
+    ]
+))
diff --git a/mx.fastr/suite.py b/mx.fastr/suite.py
index 65b520b2da..8c0c108de8 100644
--- a/mx.fastr/suite.py
+++ b/mx.fastr/suite.py
@@ -449,20 +449,22 @@ suite = {
       "layout" : {
         "./" : [
           {
-            "source_type": "extracted-dependency",
-            "dependency": "FASTR_RELEASE<rffi>",
-            "path": "*",
-            "exclude": [
+            "source_type" : "extracted-dependency",
+            "dependency" : "FASTR_RELEASE<rffi>",
+            "path" : "*",
+            "exclude" : [
               "COPYRIGHT",
               "LICENSE",
               "README.md",
+              "bin/Rscript",
               "bin/fastr_jars",
+              "bin/exec/R",
             ],
           },
           {
-            "source_type": "extracted-dependency",
-            "dependency": "FASTR_RELEASE<rffi>",
-            "path": "bin/fastr_jars/*",
+            "source_type" : "extracted-dependency",
+            "dependency" : "FASTR_RELEASE<rffi>",
+            "path" : "bin/fastr_jars/*",
             "exclude" : [
               "bin/fastr_jars/truffle*",
               "bin/fastr_jars/graal-sdk*",
@@ -473,6 +475,8 @@ suite = {
         "COPYRIGHT_FASTR" : "extracted-dependency:fastr:FASTR_RELEASE<rffi>/COPYRIGHT",
         "LICENSE_FASTR" : "extracted-dependency:fastr:FASTR_RELEASE<rffi>/LICENSE",
         "README_FASTR" : "extracted-dependency:fastr:FASTR_RELEASE<rffi>/README.md",
+        "bin/Rscript" : "file:com.oracle.truffle.r.release/src/Rscript_legacy",
+        "bin/exec/R" : "file:com.oracle.truffle.r.release/src/R_legacy",
       },
     }
   },
-- 
GitLab