From ec181bebfe7d1ad765e389ddb6a42643d7924914 Mon Sep 17 00:00:00 2001
From: Zbynek Slajchrt <zbynek.slajchrt@oracle.com>
Date: Wed, 21 Mar 2018 14:58:58 +0100
Subject: [PATCH] FastR installs MRAN packages by default

---
 .../oracle/truffle/r/library/utils/R/utils.R  | 50 +++++++++++++++++++
 com.oracle.truffle.r.native/Makefile          |  1 +
 com.oracle.truffle.r.native/run/Makefile      |  5 +-
 .../oracle/truffle/r/runtime/REnvVars.java    | 19 +++++++
 .../oracle/truffle/r/runtime/ROptions.java    |  5 ++
 .../r/install.packages.R                      |  8 ++-
 6 files changed, 86 insertions(+), 2 deletions(-)
 create mode 100644 com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/utils/R/utils.R

diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/utils/R/utils.R b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/utils/R/utils.R
new file mode 100644
index 0000000000..d911e336fa
--- /dev/null
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/utils/R/utils.R
@@ -0,0 +1,50 @@
+# Copyright (c) 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.
+
+eval(expression({
+	excludedPkgs <- c("rJava")
+	excludedPkgsMsgs <- c("CRAN rJava is not supported on FastR, but you can download and install rJava compatible replacement package from https://github.com/oracle/fastr/master/com.oracle.truffle.r.pkgs/rJava")
+
+	fastRPkgFilter <- function (av) {
+		# The following statement will assign the url of the FastR clone of rJava, when ready (possibly on GitHub).
+		#av["rJava","Repository"] <- "https://github.com/oracle/fastr/master/com.oracle.truffle.r.pkgs/rJava"
+		found <- rownames(av) %in% excludedPkgs
+		if (any(found)) {
+			av <- av[-which(found),]
+		}
+		av
+	}
+	options(available_packages_filters = list(add = TRUE, fastRPkgFilter))
+	
+	getDependencies.original <- getDependencies
+	getDependencies <- function(pkgs, dependencies = NA, available = NULL, lib = .libPaths()[1L], binary = FALSE) {
+		res <- getDependencies.original(pkgs, dependencies, available, lib, binary)	
+
+		found <- excludedPkgs %in% pkgs
+		if (any(found)) {
+			foundPkgMsgs <- excludedPkgsMsgs[which(found)]
+			warning(paste(foundPkgMsgs, collapse="\n"), call. = FALSE)
+		}
+		
+		res
+	}
+	
+}), asNamespace("utils"))
\ No newline at end of file
diff --git a/com.oracle.truffle.r.native/Makefile b/com.oracle.truffle.r.native/Makefile
index 159c2720d3..3f7f4f4f7f 100644
--- a/com.oracle.truffle.r.native/Makefile
+++ b/com.oracle.truffle.r.native/Makefile
@@ -28,6 +28,7 @@ export FASTR_R_HOME=$(abspath $(TOPDIR)/..)
 export FASTR_LIB_DIR=$(FASTR_R_HOME)/lib
 export FASTR_NATIVE_DIR = $(TOPDIR)
 export R_VERSION = 3.4.0
+export DEFAULT_CRAN_MIRROR = "https://mran.microsoft.com/snapshot/2018-03-20"
 export GNUR_HOME = $(TOPDIR)/gnur/patch-build
 
 ifeq ($(FASTR_RFFI),llvm)
diff --git a/com.oracle.truffle.r.native/run/Makefile b/com.oracle.truffle.r.native/run/Makefile
index 4f6aa77dec..13f2a596fb 100644
--- a/com.oracle.truffle.r.native/run/Makefile
+++ b/com.oracle.truffle.r.native/run/Makefile
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2015, 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
@@ -87,6 +87,9 @@ $(FASTR_BIN_DIR)/R: Makefile R.sh Rscript.sh Rscript_exec.sh Rclasspath.sh
 	cp $(GNUR_HOME_BINARY)/etc/repositories $(FASTR_ETC_DIR)/repositories
 	cp $(GNUR_HOME_BINARY)/etc/ldpaths $(FASTR_ETC_DIR)/ldpaths
 	ed Makeconf.etc < edMakeconf.etc
+	
+	echo $(DEFAULT_CRAN_MIRROR) > $(FASTR_ETC_DIR)/DEFAULT_CRAN_MIRROR
+	
 ifeq ($(FASTR_RFFI),llvm)
 	ed Makeconf.etc < edMakeconf.etc.llvm
 	cp $(LLVM_TOOLS) $(FASTR_BIN_DIR)
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 c9103b3d8c..918258922b 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
@@ -303,6 +303,25 @@ public final class REnvVars implements RContext.ContextState {
         return TimeZone.getDefault();
     }
 
+    public static String getCRANMirror() {
+        String cranMirror = System.getenv("CRAN_MIRROR");
+        if (cranMirror == null) {
+            Path defCranMirrorPath = Paths.get(REnvVars.rHome()).resolve("etc").resolve("DEFAULT_CRAN_MIRROR");
+            if (!Files.exists(defCranMirrorPath)) {
+                throw RSuicide.rSuicide("Missing etc/DEFAULT_CRAN_MIRROR file");
+            }
+            List<String> cranMirrors;
+            try {
+                cranMirrors = Files.readAllLines(defCranMirrorPath);
+            } catch (IOException e) {
+                throw RSuicide.rSuicide("Invalid etc/DEFAULT_CRAN_MIRROR file");
+            }
+            assert !cranMirrors.isEmpty();
+            cranMirror = cranMirrors.get(0);
+        }
+        return cranMirror;
+    }
+
     private String expandParameters(String value) {
         StringBuilder result = new StringBuilder();
         int x = 0;
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ROptions.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ROptions.java
index ba250c7d6b..7bce52aded 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ROptions.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ROptions.java
@@ -175,6 +175,11 @@ public class ROptions {
         map.put("browserNLdisabled", RDataFactory.createSharedLogicalVectorFromScalar(false));
         boolean cBoundsCheck = optionFromEnvVar("R_C_BOUNDS_CHECK", envVars);
         map.put("CBoundsCheck", RDataFactory.createSharedLogicalVectorFromScalar(cBoundsCheck));
+
+        String cranMirror = REnvVars.getCRANMirror();
+        if (cranMirror != null) {
+            map.put("repos", cranMirror);
+        }
     }
 
     private static boolean optionFromEnvVar(String envVar, REnvVars envVars) {
diff --git a/com.oracle.truffle.r.test.packages/r/install.packages.R b/com.oracle.truffle.r.test.packages/r/install.packages.R
index acb13821f8..7dac0bf7bd 100644
--- a/com.oracle.truffle.r.test.packages/r/install.packages.R
+++ b/com.oracle.truffle.r.test.packages/r/install.packages.R
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2015, 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
@@ -304,6 +304,12 @@ set.repos <- function() {
 		} else if (name == "FASTR") {
 			# set the FastR internal repo
 			repos[["FASTR"]] <- paste0("file://", normalizePath("com.oracle.truffle.r.test.native/packages/repo"))
+		} else if (name == "SNAPSHOT") {
+			con <- file("etc/DEFAULT_CRAN_MIRROR", "r"); 
+			tryCatch({
+				cran.mirror <<- readLines(con)[[1]]
+			}, finally=function() close(con))
+			repos[["CRAN"]] <- cran.mirror
 		} else {
 			# User defined
 			repos[[name]] <- uri
-- 
GitLab