From 8ee9660a4dc95b6c2b4c4563e0d1e407d8ebd100 Mon Sep 17 00:00:00 2001
From: Mick Jordan <mick.jordan@oracle.com>
Date: Fri, 27 Nov 2015 20:12:47 -0800
Subject: [PATCH] remove static capture of RHOME path; handle RHOME in mx (cf
 bin/R)

---
 .../oracle/truffle/r/runtime/RCmdOptions.java | 13 +------
 .../oracle/truffle/r/runtime/REnvVars.java    | 38 +++++++++----------
 mx.fastr/mx_fastr.py                          | 12 +++++-
 3 files changed, 29 insertions(+), 34 deletions(-)

diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RCmdOptions.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RCmdOptions.java
index fd43291f77..7dede92046 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RCmdOptions.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RCmdOptions.java
@@ -68,7 +68,6 @@ public final class RCmdOptions {
         HELP(RCmdOptionType.BOOLEAN, true, "h", "help", false, "Print short help message and exit"),
         VERSION(RCmdOptionType.BOOLEAN, true, "version", false, "Print version info and exit"),
         ENCODING(RCmdOptionType.STRING, false, "encoding=ENC", null, "Specify encoding to be used for stdin"),
-        RHOME(RCmdOptionType.BOOLEAN, true, "RHOME", false, "Print path to R home directory and exit"),
         SAVE(RCmdOptionType.BOOLEAN, true, "save", false, "Do save workspace at the end of the session"),
         NO_SAVE(RCmdOptionType.BOOLEAN, true, "no-save", false, "Don't save it"),
         NO_ENVIRON(RCmdOptionType.BOOLEAN, false, "no-environ", false, "Don't read the site and user environment files"),
@@ -129,7 +128,7 @@ public final class RCmdOptions {
                 this.name = null;
                 this.suffix = null;
             } else {
-                int eqx = noPrefix(name) ? -1 : name.indexOf('=');
+                int eqx = name.indexOf('=');
                 this.name = "--" + (eqx > 0 ? name.substring(0, eqx) : name);
                 this.suffix = eqx > 0 ? name.substring(eqx) : null;
             }
@@ -145,10 +144,6 @@ public final class RCmdOptions {
             this(type, Client.EITHER, implemented, null, name, defaultValue, help);
         }
 
-        private static boolean noPrefix(String arg) {
-            return arg.equals("RHOME") || arg.equals("CMD");
-        }
-
         private boolean matches(String arg) {
             if (shortName != null && arg.equals(shortName)) {
                 return true;
@@ -354,8 +349,6 @@ public final class RCmdOptions {
             printHelpAndExit(RCmdOptions.Client.R);
         } else if (getBoolean(VERSION)) {
             printVersionAndExit();
-        } else if (getBoolean(RHOME)) {
-            printRHomeAndExit();
         }
     }
 
@@ -380,8 +373,4 @@ public final class RCmdOptions {
         System.exit(0);
     }
 
-    private static void printRHomeAndExit() {
-        System.out.println(REnvVars.rHome());
-        System.exit(0);
-    }
 }
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 639567f9d1..671ed65ee0 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
@@ -48,7 +48,7 @@ public final class REnvVars implements RContext.ContextState {
         String envRHomePath = envVars.get("R_HOME");
         if (envRHomePath != null) {
             new File(envRHomePath).getAbsolutePath();
-            if (!envRHomePath.equals(rHomePath)) {
+            if (!envRHomePath.equals(rHome)) {
                 Utils.fail("R_HOME set to unexpected value in the environment");
             }
         }
@@ -117,29 +117,25 @@ public final class REnvVars implements RContext.ContextState {
         return val != null ? val : envVars.get(var.toUpperCase());
     }
 
-    private static String rHomePath;
-
     public static String rHome() {
-        // This can be called before initialize, "R RHOME"
-        if (rHomePath == null) {
-            String path = System.getProperty("rhome.path");
-            if (path != null) {
-                rHomePath = path;
-            } else {
-                File file = new File(System.getProperty("user.dir"));
-                do {
-                    File binR = new File(new File(file, "bin"), "R");
-                    if (binR.exists()) {
-                        break;
-                    } else {
-                        file = file.getParentFile();
-                    }
-                } while (file != null);
-                if (file != null) {
-                    rHomePath = file.getAbsolutePath();
+        String rHomePath = null;
+        String path = System.getProperty("rhome.path");
+        if (path != null) {
+            rHomePath = path;
+        } else {
+            File file = new File(System.getProperty("user.dir"));
+            do {
+                File binR = new File(new File(file, "bin"), "R");
+                if (binR.exists()) {
+                    break;
                 } else {
-                    Utils.fail("cannot find a valid R_HOME");
+                    file = file.getParentFile();
                 }
+            } while (file != null);
+            if (file != null) {
+                rHomePath = file.getAbsolutePath();
+            } else {
+                Utils.fail("cannot find a valid R_HOME");
             }
         }
         return rHomePath;
diff --git a/mx.fastr/mx_fastr.py b/mx.fastr/mx_fastr.py
index ee212a8b9c..01597640bb 100644
--- a/mx.fastr/mx_fastr.py
+++ b/mx.fastr/mx_fastr.py
@@ -20,7 +20,7 @@
 # or visit www.oracle.com if you need additional information or have any
 # questions.
 #
-import tempfile, platform, subprocess
+import tempfile, platform, subprocess, sys
 from os.path import join, sep, dirname, abspath
 from argparse import ArgumentParser
 import mx
@@ -188,6 +188,16 @@ def run_r(args, command, parser=None, extraVmArgs=None, jdk=None, nonZeroIsFatal
 
     if not jdk and ns.jdk:
         jdk = mx.get_jdk(tag=ns.jdk)
+
+    # special cases normally handled in shell script startup
+    if command == 'r' and len(rargs) > 0:
+        if rargs[0] == 'RHOME':
+            print _fastr_suite.dir
+            sys.exit(0)
+        elif rargs[0] == 'CMD':
+            print 'CMD not implemented via mx, use: bin/R CMD ...'
+            sys.exit(1)
+
     return do_run_r(rargs, command, extraVmArgs=extraVmArgs, jdk=jdk, nonZeroIsFatal=nonZeroIsFatal)
 
 def rshell(args):
-- 
GitLab