From 8c42f89cf877f2d9bcdacdbf1ef2f3f6e3a8a94d Mon Sep 17 00:00:00 2001
From: Mick Jordan <mick.jordan@oracle.com>
Date: Wed, 30 Dec 2015 12:53:23 -0800
Subject: [PATCH] consolidate rfficall/appl libs as libR

---
 com.oracle.truffle.r.native/Makefile          |   2 -
 .../builtinlibs/Makefile                      | 102 ------------------
 .../builtinlibs/src/xerbla.c                  |  21 ----
 .../fficall/src/common/common.mk              |   6 +-
 .../fficall/src/common/print_fastr.c          |  10 ++
 .../fficall/src/jni/Makefile                  |  15 ++-
 .../library/stats/Makefile                    |   3 +-
 .../r/runtime/ffi/jnr/JNI_CallRFFI.java       |   6 +-
 .../truffle/r/runtime/ffi/jnr/JNR_RAppl.java  |   2 +-
 mx.fastr/copyrights/overrides                 |   1 -
 10 files changed, 30 insertions(+), 138 deletions(-)
 delete mode 100644 com.oracle.truffle.r.native/builtinlibs/Makefile
 delete mode 100644 com.oracle.truffle.r.native/builtinlibs/src/xerbla.c

diff --git a/com.oracle.truffle.r.native/Makefile b/com.oracle.truffle.r.native/Makefile
index 98b258d4ae..32478092ea 100644
--- a/com.oracle.truffle.r.native/Makefile
+++ b/com.oracle.truffle.r.native/Makefile
@@ -34,7 +34,6 @@ export GNUR_HOME = $(TOPDIR)/gnur/R-$(R_VERSION)
 all: 
 	$(MAKE) -C gnur
 	$(MAKE) -C include
-	$(MAKE) -C builtinlibs
 	$(MAKE) -C osextras
 	$(MAKE) -C fficall
 	$(MAKE) -C library
@@ -42,7 +41,6 @@ all:
 
 clean: 
 	$(MAKE) -C include clean
-	$(MAKE) -C builtinlibs clean
 	$(MAKE) -C osextras clean
 	$(MAKE) -C fficall clean
 	$(MAKE) -C library clean
diff --git a/com.oracle.truffle.r.native/builtinlibs/Makefile b/com.oracle.truffle.r.native/builtinlibs/Makefile
deleted file mode 100644
index f052790aaf..0000000000
--- a/com.oracle.truffle.r.native/builtinlibs/Makefile
+++ /dev/null
@@ -1,102 +0,0 @@
-#
-# Copyright (c) 2014, 2015, 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.
-#
-
-#
-# This Makefile contains the recipe for creating a modified version of 
-# GnuR's libappl (mostly just Fortran) and create it as a shared library (GnuR creates a static library
-# and it also contains code that refers to internal symnbols).
-#
-
-ifneq ($(MAKECMDGOALS),clean)
-include $(TOPDIR)/platform.mk
-endif
-
-.PHONY: all clean cleanlib cleanobj force libr
-
-LIBDIR = $(abspath $(TOPDIR)/../lib)
-LIB_APPL := $(LIBDIR)/libappl$(DYLIB_EXT)
-
-all: $(LIB_APPL) libr
-
-SRC = src
-OBJ = lib
-
-GNUR_SRC = $(GNUR_HOME)/src/appl
-GNUR_C_FILES = interv.c
-GNUR_C_OBJECTS := $(addprefix $(OBJ)/, $(GNUR_C_FILES:.c=.o))
-
-C_SOURCES := $(wildcard $(SRC)/*.c)
-C_OBJECTS := $(subst $(SRC),$(OBJ),$(C_SOURCES:.c=.o))
-
-# the Fortran sources are not recompiled
-F_OBJECTS := $(wildcard $(GNUR_HOME)/src/appl/d*.o)
-
-$(LIB_APPL): $(C_OBJECTS) $(GNUR_C_OBJECTS) $(F_OBJECTS) Makefile
-	mkdir -p $(LIBDIR)
-	$(DYLIB_LD) $(DYLIB_LDFLAGS)  -o $(LIB_APPL) $(C_OBJECTS) $(GNUR_C_OBJECTS) $(F_OBJECTS) $(LIBS)
-
-$(C_OBJECTS): | $(OBJ)
-
-$(OBJ):
-	mkdir -p $(OBJ)
-
-# On Darwin we need to create a libR.dylib in which xerbla goes 
-# as the libRlapack library has a dependency on libR and expecte xerbla to
-# be defined there. We could fix this if we cared by not using the GnuR instance of the 
-# libRlapack and rebuilding it from scratch.
-
-ifeq ($(OS_NAME), Darwin)
-LIB_R := $(LIBDIR)/libR$(DYLIB_EXT)
-
-libr: $(LIB_R)
-
-$(LIB_R): $(OBJ)/xerbla.o
-	mkdir -p $(LIBDIR)
-	$(DYLIB_LD) $(DYLIB_LDFLAGS) -o $(LIB_R) -current_version $(R_VERSION) -compatibility_version $(R_VERSION) $(OBJ)/xerbla.o
-
-cleanlibr:
-	rm -f $(LIBDIR)/libR.*
-
-else
-libr: 
-
-cleanlibr:
-
-endif
-
-FFI_INCLUDES = -I$(TOPDIR)/include -I$(TOPDIR)/include/R_ext
-
-$(OBJ)/%.o: $(SRC)/%.c
-	$(CC) $(CFLAGS) $(FFI_INCLUDES) -c $< -o $@
-
-$(OBJ)/%.o: $(GNUR_SRC)/%.c
-	$(CC) $(CFLAGS) $(FFI_INCLUDES) -c $< -o $@
-
-cleanlib:
-	rm -f $(LIBDIR)/libappl.*
-
-clean: cleanobj cleanlib cleanlibr
-
-cleanobj:
-	rm -f $(OBJ)/*.o
-
diff --git a/com.oracle.truffle.r.native/builtinlibs/src/xerbla.c b/com.oracle.truffle.r.native/builtinlibs/src/xerbla.c
deleted file mode 100644
index 06c41cb966..0000000000
--- a/com.oracle.truffle.r.native/builtinlibs/src/xerbla.c
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (c) 1995-2015, The R Core Team
- * Copyright (c) 2003, The R Foundation
- * Copyright (c) 2015, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-#include <R.h>
-
-void F77_NAME(xerbla)(const char *srname, int *info)
-{
-   /* srname is not null-terminated.  It should be 6 characters. */
-    char buf[7];
-    strncpy(buf, srname, 6);
-    buf[6] = '\0';
-    printf("BLAS/LAPACK routine '%6s' gave error code %d", buf, -(*info));
-}
diff --git a/com.oracle.truffle.r.native/fficall/src/common/common.mk b/com.oracle.truffle.r.native/fficall/src/common/common.mk
index 36f0387164..68c5e2ce28 100644
--- a/com.oracle.truffle.r.native/fficall/src/common/common.mk
+++ b/com.oracle.truffle.r.native/fficall/src/common/common.mk
@@ -25,13 +25,17 @@
 # header files that redirect to GnuR versions to be location/version independent. It is included
 # by the actual implementation Makefile, e.g.in ../jni
 
-GNUR_APPL_C_FILES = pretty.c
+GNUR_APPL_C_FILES = pretty.c interv.c
 GNUR_APPL_SRC = $(GNUR_HOME)/src/appl
+# the Fortran sources are not recompiled
+GNUR_APPL_F_OBJECTS := $(wildcard $(GNUR_APPL_SRC)/d*.o)
+
 
 GNUR_MAIN_C_FILES = colors.c devices.c engine.c graphics.c plot.c plot3d.c plotmath.c rlocale.c sort.c
 GNUR_MAIN_SRC = $(GNUR_HOME)/src/main
 
 GNUR_C_OBJECTS := $(addprefix $(OBJ)/, $(GNUR_APPL_C_FILES:.c=.o) $(GNUR_MAIN_C_FILES:.c=.o))
+GNUR_F_OBJECTS := $(GNUR_APPL_F_OBJECTS)
 
 # headers that we refer to indirectly (allows version/location independence in source)
 GNUR_GRAPHICS_H := $(GNUR_HOME)/src/include/Graphics.h
diff --git a/com.oracle.truffle.r.native/fficall/src/common/print_fastr.c b/com.oracle.truffle.r.native/fficall/src/common/print_fastr.c
index 78f5020f42..c17dca824e 100644
--- a/com.oracle.truffle.r.native/fficall/src/common/print_fastr.c
+++ b/com.oracle.truffle.r.native/fficall/src/common/print_fastr.c
@@ -79,6 +79,7 @@
 #include <rffiutils.h>
 #include <Defn.h>
 #include <Print.h>
+#include <R_ext/RS.h>
 
 
 /* Global print parameter struct: */
@@ -117,4 +118,13 @@ void PrintDefaults(void)
     R_print.cutoff = GetOptionCutoff();
 }
 
+void F77_NAME(xerbla)(const char *srname, int *info)
+{
+   /* srname is not null-terminated.  It should be 6 characters. */
+    char buf[7];
+    strncpy(buf, srname, 6);
+    buf[6] = '\0';
+    printf("BLAS/LAPACK routine '%6s' gave error code %d", buf, -(*info));
+}
+
 // FastR: the rest of the file is omitted
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/Makefile b/com.oracle.truffle.r.native/fficall/src/jni/Makefile
index 76c69734c1..7c86d2b0f4 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/Makefile
+++ b/com.oracle.truffle.r.native/fficall/src/jni/Makefile
@@ -21,7 +21,7 @@
 # questions.
 #
 
-# Master Makefile for librffi. Includes ../common/common.mk to include common files in the build
+# Master Makefile for libR. Includes ../common/common.mk to include common files in the build
 
 ifeq ($(TOPDIR),)
     TOPDIR = $(abspath ../../..)
@@ -50,8 +50,8 @@ C_LOCAL_SOURCES := $(wildcard *.c)
 $(info C_LOCAL_SOURCES=$(C_LOCAL_SOURCES))
 
 C_HDRS := $(wildcard *.h)
-C_LIBNAME := librfficall$(DYLIB_EXT)
-C_LIB := $(TOPDIR)/../lib/$(C_LIBNAME)
+C_LIBNAME := libR$(DYLIB_EXT)
+C_LIB := $(FASTR_LIB_DIR)/$(C_LIBNAME)
 
 C_SOURCES = $(C_LOCAL_SOURCES) $(C_COMMON_SOURCES)
 C_OBJECTS := $(patsubst %.c,$(OBJ)/%.o,$(C_LOCAL_SOURCES)) $(patsubst %.c,$(OBJ)/%.o,$(C_COMMON_SOURCES))
@@ -63,13 +63,18 @@ FFI_INCLUDES = -I$(TOPDIR)/include -I$(TOPDIR)/include/R_ext
 
 INCLUDES := $(LOCAL_INCLUDES) $(JNI_INCLUDES) $(FFI_INCLUDES)
 
+ifeq ($(OS_NAME), Darwin)
+VERSION_FLAGS := -current_version $(R_VERSION) -compatibility_version $(R_VERSION)
+endif
+
 # uncomment to see exactly where headers are being read from
 #CFLAGS := $(CFLAGS) -H
 
 all: Makefile $(C_LIB) 
 
-$(C_LIB): $(OBJ) $(C_OBJECTS) $(F_OBJECTS) $(GNUR_C_OBJECTS)
-	$(DYLIB_LD) $(DYLIB_LDFLAGS) -o $(C_LIB) $(C_OBJECTS) $(F_OBJECTS) $(GNUR_C_OBJECTS)
+$(C_LIB): $(OBJ) $(C_OBJECTS) $(F_OBJECTS) $(GNUR_F_OBJECTS) $(GNUR_C_OBJECTS)
+	$(DYLIB_LD) $(DYLIB_LDFLAGS) -o $(C_LIB) $(C_OBJECTS) $(F_OBJECTS) $(GNUR_C_OBJECTS) $(GNUR_F_OBJECTS) $(VERSION_FLAGS)
+	
 
 $(OBJ):
 	mkdir -p $(OBJ)
diff --git a/com.oracle.truffle.r.native/library/stats/Makefile b/com.oracle.truffle.r.native/library/stats/Makefile
index b12fe0b5bc..e43aa36a64 100644
--- a/com.oracle.truffle.r.native/library/stats/Makefile
+++ b/com.oracle.truffle.r.native/library/stats/Makefile
@@ -40,7 +40,7 @@ ifneq ($(MAKECMDGOALS),clean)
 include $(TOPDIR)/platform.mk
 endif
 
-PKG_LIBS := $(LAPACK_LIBS) $(BLAS_LIBS) -lappl -L$(FASTR_LIB_DIR) $(FLIBS)
+PKG_LIBS := $(LAPACK_LIBS) $(BLAS_LIBS) -L$(FASTR_LIB_DIR) $(FLIBS)
 
 OBJ = lib
 
@@ -75,7 +75,6 @@ nametool:
 ifeq ($(OS_NAME),Darwin)
 	install_name_tool -change libRblas.dylib $(FASTR_R_HOME)/lib/libRblas.dylib $(FASTR_LIBRARY_DIR)/$(PKG)/libs/$(PKG).so
 	install_name_tool -change libRlapack.dylib $(FASTR_R_HOME)/lib/libRlapack.dylib $(FASTR_LIBRARY_DIR)/$(PKG)/libs/$(PKG).so
-	install_name_tool -change libappl.dylib $(FASTR_R_HOME)/lib/libappl.dylib $(FASTR_LIBRARY_DIR)/$(PKG)/libs/$(PKG).so
 endif
 
 cleanpkg:
diff --git a/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/jnr/JNI_CallRFFI.java b/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/jnr/JNI_CallRFFI.java
index eeeeba8ff3..4db5a804f0 100644
--- a/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/jnr/JNI_CallRFFI.java
+++ b/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/jnr/JNI_CallRFFI.java
@@ -51,15 +51,15 @@ public class JNI_CallRFFI implements CallRFFI {
     private static final boolean ForceRTLDGlobal = false;
 
     /**
-     * Load the {@code librfficall} library. N.B. this library defines some non-JNI global symbols
-     * that are referenced by C code in R packages. Unfortunately, {@link System#load(String)} uses
+     * Load the {@code libR} library. N.B. this library defines some non-JNI global symbols that are
+     * referenced by C code in R packages. Unfortunately, {@link System#load(String)} uses
      * {@code RTLD_LOCAL} with {@code dlopen}, so we have to load the library manually and set
      * {@code RTLD_GLOBAL}. However, a {@code dlopen} does not hook the JNI functions into the JVM,
      * so we have to do an additional {@code System.load} to achieve that.
      */
     @TruffleBoundary
     private static void loadLibrary() {
-        String librffiPath = LibPaths.getBuiltinLibPath("rfficall");
+        String librffiPath = LibPaths.getBuiltinLibPath("R");
         try {
             DLL.load(librffiPath, ForceRTLDGlobal, false);
         } catch (DLLException ex) {
diff --git a/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/jnr/JNR_RAppl.java b/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/jnr/JNR_RAppl.java
index 499e11ffb3..d28b71f21b 100644
--- a/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/jnr/JNR_RAppl.java
+++ b/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/jnr/JNR_RAppl.java
@@ -45,7 +45,7 @@ public class JNR_RAppl implements RApplRFFI {
         @TruffleBoundary
         private static Linpack createAndLoadLib() {
             // need to load blas lib as Fortran functions in appl lib need it
-            return LibraryLoader.create(Linpack.class).library("Rblas").library("appl").load();
+            return LibraryLoader.create(Linpack.class).library("Rblas").library("R").load();
         }
 
         static Linpack linpack() {
diff --git a/mx.fastr/copyrights/overrides b/mx.fastr/copyrights/overrides
index 2635e17cf7..bc88d8ceee 100644
--- a/mx.fastr/copyrights/overrides
+++ b/mx.fastr/copyrights/overrides
@@ -39,7 +39,6 @@ com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/tools/ToolsText.ja
 com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/utils/CountFields.java,gnu_r.copyright
 com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/utils/Menu.java,gnu_r.copyright
 com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/utils/WriteTable.java,gnu_r.copyright
-com.oracle.truffle.r.native/builtinlibs/src/xerbla.c,gnu_r.copyright
 com.oracle.truffle.r.native/fficall/src/common/arithmetic_fastr.c,gnu_r_gentleman_ihaka.copyright
 com.oracle.truffle.r.native/fficall/src/common/coerce_fastr.c,gnu_r_gentleman_ihaka.copyright
 com.oracle.truffle.r.native/fficall/src/common/inlined_fastr.c,gnu_r_gentleman_ihaka.copyright
-- 
GitLab