From 7c4eec97d4a302cc79c9a24d156766a9878a9ea1 Mon Sep 17 00:00:00 2001
From: Adam Welc <adam.welc@oracle.com>
Date: Wed, 8 Apr 2015 17:41:15 -0700
Subject: [PATCH] Added support for running R and Rscript commands from an
 arbitrary directory.

---
 com.oracle.truffle.r.native/run/R.sh               |  7 ++++++-
 com.oracle.truffle.r.native/run/Rscript.sh         |  7 ++++++-
 .../src/com/oracle/truffle/r/runtime/REnvVars.java | 14 ++++++++++----
 mx.fastr/mx_fastr.py                               |  1 +
 4 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/com.oracle.truffle.r.native/run/R.sh b/com.oracle.truffle.r.native/run/R.sh
index af88a96c74..dfc18472c4 100644
--- a/com.oracle.truffle.r.native/run/R.sh
+++ b/com.oracle.truffle.r.native/run/R.sh
@@ -1,3 +1,4 @@
+#!/bin/bash
 #
 # This material is distributed under the GNU General Public License
 # Version 2. You may review the terms of this license at
@@ -15,9 +16,13 @@
 #
 GNUR=`which R`
 
+source="${BASH_SOURCE[0]}"
+while [ -h "$source" ] ; do source="$(readlink "$source")"; done
+PRIMARY_PATH="$( cd -P "$( dirname "$source" )" && pwd )"/..
+
 case ${1} in
     CMD)
       exec $GNUR "$@";;
     *)
-      exec mx R "$@";;
+      exec mx --primary-suite-path $PRIMARY_PATH R "$@";;
  esac
diff --git a/com.oracle.truffle.r.native/run/Rscript.sh b/com.oracle.truffle.r.native/run/Rscript.sh
index 22968bece2..4ce11bef4e 100755
--- a/com.oracle.truffle.r.native/run/Rscript.sh
+++ b/com.oracle.truffle.r.native/run/Rscript.sh
@@ -1,3 +1,4 @@
+#!/bin/bash
 #
 # This material is distributed under the GNU General Public License
 # Version 2. You may review the terms of this license at
@@ -12,4 +13,8 @@
 
 # Fledgling Rscript command to startup FastR
 
-mx Rscript "$@"
+source="${BASH_SOURCE[0]}"
+while [ -h "$source" ] ; do source="$(readlink "$source")"; done
+PRIMARY_PATH="$( cd -P "$( dirname "$source" )" && pwd )"/..
+
+mx --primary-suite-path $PRIMARY_PATH Rscript "$@"
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/REnvVars.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/REnvVars.java
index a90120dbf7..4f9e63e514 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/REnvVars.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/REnvVars.java
@@ -124,11 +124,17 @@ public class REnvVars {
     public static String rHome() {
         // This can be called before initialize, "R RHOME"
         if (rHomePath == null) {
-            File file = new File(System.getProperty("user.dir"));
-            if (file.getName().endsWith("r.test")) {
-                file = file.getParentFile();
+
+            String path = System.getProperty("rhome.path");
+            if (path != null) {
+                rHomePath = path;
+            } else {
+                File file = new File(System.getProperty("user.dir"));
+                if (file.getName().endsWith("r.test")) {
+                    file = file.getParentFile();
+                }
+                rHomePath = file.getAbsolutePath();
             }
-            rHomePath = file.getAbsolutePath();
             // Check any external setting is consistent
             String envRHomePath = getEnvVars().get("R_HOME");
             if (envRHomePath != null) {
diff --git a/mx.fastr/mx_fastr.py b/mx.fastr/mx_fastr.py
index 1ef05cee14..7de0f239e0 100644
--- a/mx.fastr/mx_fastr.py
+++ b/mx.fastr/mx_fastr.py
@@ -36,6 +36,7 @@ def runR(args, className, nonZeroIsFatal=True, extraVmArgs=None, runBench=False)
     setREnvironment()
     project = className.rpartition(".")[0]
     vmArgs = ['-cp', mx.classpath(project)]
+    vmArgs = vmArgs + ["-Drhome.path=" + _fastr_suite.dir]
 
     if runBench == False:
         vmArgs = vmArgs + ['-ea', '-esa']
-- 
GitLab