From 48e2169c534255c00736947f9fb1bcd278ff990e Mon Sep 17 00:00:00 2001
From: Tomas Stupka <tomas.stupka@oracle.com>
Date: Fri, 6 Apr 2018 02:58:19 +0200
Subject: [PATCH] propagate R arguments like e.g.--jvm through launcher scripts
 and spawned children processes

---
 com.oracle.truffle.r.native/run/Makefile            |  8 ++++++--
 com.oracle.truffle.r.native/run/R.sh                |  2 +-
 com.oracle.truffle.r.native/run/Rscript_exec.sh     |  2 +-
 .../src/com/oracle/truffle/r/runtime/REnvVars.java  | 13 +++++++++++++
 4 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/com.oracle.truffle.r.native/run/Makefile b/com.oracle.truffle.r.native/run/Makefile
index 13f2a596fb..24e74d213a 100644
--- a/com.oracle.truffle.r.native/run/Makefile
+++ b/com.oracle.truffle.r.native/run/Makefile
@@ -69,6 +69,8 @@ rcmds: $(FASTR_BIN_DIR)/R
 
 $(FASTR_BIN_DIR)/R: Makefile R.sh Rscript.sh Rscript_exec.sh Rclasspath.sh
 	cp -r $(BIN_FILES) $(FASTR_BIN_DIR)
+	# cp  $(FASTR_R_HOME)/com.oracle.truffle.r.native/gnur/patch/bin-patch/* $(FASTR_BIN_DIR)
+	
     # overide bin/R
 	cp R.sh $(FASTR_BIN_DIR)/exec/R
 	# override bin/Rscript
@@ -77,8 +79,10 @@ $(FASTR_BIN_DIR)/R: Makefile R.sh Rscript.sh Rscript_exec.sh Rclasspath.sh
 	# for embedded support
 	cp Rclasspath.sh $(FASTR_BIN_DIR)/execRextras/Rclasspath
 	chmod +x $(FASTR_BIN_DIR)/exec/R $(FASTR_BIN_DIR)/execRextras/Rscript $(FASTR_BIN_DIR)/Rscript $(FASTR_BIN_DIR)/execRextras/Rclasspath
-	# update R_HOME_DIR to FastR
-	(sed -e 's!^\(R_HOME_DIR=\)\(.*\)!\1"$(FASTR_R_HOME)"!' | sed -e 's/    -h.--help./    --r-help\)/') < $(R_SCRIPT) > $(FASTR_BIN_DIR)/R
+	# update R_HOME_DIR to FastR, --help to -r-help because --help is handled by FastR, and pass args to Rcmd (esp. --jvm)
+	(sed -e 's!^\(R_HOME_DIR=\)\(.*\)!\1"$(FASTR_R_HOME)"!' | \
+	 sed -e 's/    -h.--help./    --r-help\)/' | \
+	 sed -e 's!      exec sh "$${R_HOME}/bin/Rcmd" "$${@}" ;;!      FASTR_INTERNAL_ARGS="$${FASTR_INTERNAL_ARGS} $${args}" exec sh "$${R_HOME}/bin/Rcmd" "$${@}" ;;!') < $(R_SCRIPT) > $(FASTR_BIN_DIR)/R
 	chmod +x $(FASTR_BIN_DIR)/R 
 	touch $(FASTR_ETC_DIR)/ldpaths
 	sed -e "s|\(R_LIBS_USER=.*-'\)\(.*\)'}|\1$(FASTR_R_HOME)/library'}|" < $(GNUR_HOME_BINARY)/etc/Renviron > $(FASTR_ETC_DIR)/Renviron
diff --git a/com.oracle.truffle.r.native/run/R.sh b/com.oracle.truffle.r.native/run/R.sh
index 5f7471ef41..af7bf7df38 100644
--- a/com.oracle.truffle.r.native/run/R.sh
+++ b/com.oracle.truffle.r.native/run/R.sh
@@ -46,4 +46,4 @@ else
     mx=`which mx`
 fi
 
-exec $mx --primary-suite-path $PRIMARY_PATH $MX_R_GLOBAL_ARGS R $MX_R_CMD_ARGS "$@"
+exec $mx --primary-suite-path $PRIMARY_PATH $MX_R_GLOBAL_ARGS R $FASTR_INTERNAL_ARGS $MX_R_CMD_ARGS "$@"
diff --git a/com.oracle.truffle.r.native/run/Rscript_exec.sh b/com.oracle.truffle.r.native/run/Rscript_exec.sh
index 950435e34d..d60986df11 100644
--- a/com.oracle.truffle.r.native/run/Rscript_exec.sh
+++ b/com.oracle.truffle.r.native/run/Rscript_exec.sh
@@ -42,4 +42,4 @@ else
     mx=`which mx`
 fi
 
-exec $mx --primary-suite-path $PRIMARY_PATH $MX_R_GLOBAL_ARGS Rscript $MX_R_CMD_ARGS "$@"
+exec $mx --primary-suite-path $PRIMARY_PATH $MX_R_GLOBAL_ARGS Rscript $FASTR_INTERNAL_ARGS $MX_R_CMD_ARGS "$@"
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 918258922b..d8d95185e1 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
@@ -73,6 +73,19 @@ public final class REnvVars implements RContext.ContextState {
         // Always read the system file
         FileSystem fileSystem = FileSystems.getDefault();
         safeReadEnvironFile(fileSystem.getPath(rHome, "etc", "Renviron").toString());
+
+        String internalArgs = System.getenv("FASTR_INTERNAL_ARGS");
+        if (context.getEnv().isHostLookupAllowed()) {
+            if (internalArgs == null) {
+                internalArgs = "--jvm";
+            } else if (!internalArgs.contains("--jvm")) {
+                internalArgs += " --jvm";
+            }
+        }
+        if (internalArgs != null) {
+            envVars.put("FASTR_INTERNAL_ARGS", internalArgs);
+        }
+
         envVars.put("R_DOC_DIR", fileSystem.getPath(rHome, "doc").toString());
         envVars.put("R_INCLUDE_DIR", fileSystem.getPath(rHome, "include").toString());
         envVars.put("R_SHARE_DIR", fileSystem.getPath(rHome, "share").toString());
-- 
GitLab