diff -uNr R-3.1.3.orig/Makeconf.in R-3.1.3/Makeconf.in
--- R-3.1.3.orig/Makeconf.in	2014-07-14 18:15:12.000000000 -0400
+++ R-3.1.3/Makeconf.in	2017-08-25 06:29:58.000000000 -0400
@@ -77,7 +77,7 @@
 READLINE_LIBS = @READLINE_LIBS@
 SED = @SED@
 SHELL = @R_SHELL@
-SHLIB_EXT = @SHLIB_EXT@
+SHLIB_EXT = .dylib
 SHLIB_CFLAGS = @SHLIB_CFLAGS@
 SHLIB_FFLAGS = @SHLIB_FFLAGS@
 SHLIB_LD = @SHLIB_LD@
diff -uNr R-3.1.3.orig/Makefile.fw R-3.1.3/Makefile.fw
--- R-3.1.3.orig/Makefile.fw	2011-10-02 18:02:39.000000000 -0400
+++ R-3.1.3/Makefile.fw	2017-08-25 06:29:58.000000000 -0400
@@ -8,7 +8,7 @@
 
 install install-strip: install-R-framework
 		@(sed 's|^LIBR =.*|LIBR = -F$(R_FRAMEWORK_DIR)/.. -framework R|' \
-		  $(top_builddir)/etc/Makeconf > "$(rhome)/etc${R_ARCH}/Makeconf")
+		  $(top_builddir)/etc/Makeconf > "$(DESTDIR)$(R_FRAMEWORK_DIR)/Resources/etc${R_ARCH}/Makeconf")
 		@(sed 's/Versions\/$(FW_VERSION)\/Resources/Resources/' \
 		  "$(DESTDIR)$(R_FRAMEWORK_DIR)/Resources/bin/R" > \
 		  "$(DESTDIR)$(R_FRAMEWORK_DIR)/Resources/bin/RR")
@@ -37,7 +37,7 @@
 	  ln -f -s -n Versions/Current/Resources Resources)
 	@## the resulting libR will point dyld to the fat libR regardless of its origin
 	@(cd "$(DESTDIR)$(R_FRAMEWORK_DIR)/Resources/lib" && \
-	  install_name_tool -id "$(R_FRAMEWORK_DIR)/Versions/$(FW_VERSION)/Resources/lib/libR.dylib" $(Rexeclibdir)/libR.dylib)
+	  install_name_tool -id "$(R_FRAMEWORK_DIR)/Versions/$(FW_VERSION)/Resources/lib/libR.dylib" $(DESTDIR)$(Rexeclibdir)/libR.dylib)
 	@( otool=otool ; if otool -D "$(DESTDIR)$(Rexeclibdir)/libR.dylib"|grep 'not an object'>/dev/null; then otool=otool64; fi; \
 	   for lib in Rlapack Rblas R; do \
 	   if test -e "$(DESTDIR)$(Rexeclibdir)/lib$${lib}.dylib"; then $(ECHO) "  lib$${lib}"; \
diff -uNr R-3.1.3.orig/configure R-3.1.3/configure
--- R-3.1.3.orig/configure	2015-03-01 18:16:28.000000000 -0500
+++ R-3.1.3/configure	2017-08-25 06:29:58.000000000 -0400
@@ -4006,8 +4006,8 @@
 
 ## As from R 3.2.0 split up -L... and -lR
 if test "${want_R_shlib}" = yes; then
-  LIBR0="-L\$(R_HOME)/lib\$(R_ARCH)"
-  LIBR1=-lR
+  LIBR0="\$(R_HOME)/lib\$(R_ARCH)/libR\$(DYLIB_EXT)"
+  LIBR1=
 else
   LIBR0=
   LIBR1=
@@ -26158,7 +26158,7 @@
 main_ld="${CC}"
 shlib_ld="${CC}"
 shlib_cxxld="${CXX}"
-SHLIB_EXT=".so"
+SHLIB_EXT=".dylib"
 SHLIB_LIBADD=
 use_exportfiles=no
 
@@ -30549,7 +30549,7 @@
 
 if test "${use_blas_shlib}" = yes; then
   ## set BLAS_LIBS to point at local version
-  BLAS_LIBS="-L\$(R_HOME)/lib\$(R_ARCH) -lRblas"
+  BLAS_LIBS="\$(R_HOME)/lib\$(R_ARCH)/libRblas\$(DYLIB_EXT)"
 fi
 
  if test "x${use_veclib_g95fix}" = xyes; then
@@ -30719,7 +30719,7 @@
 
 fi
 if test "${acx_lapack_ok}" != "yes"; then
-  LAPACK_LIBS="-L\$(R_HOME)/lib\$(R_ARCH) -lRlapack"
+  LAPACK_LIBS="\$(R_HOME)/lib\$(R_ARCH)/libRlapack\$(DYLIB_EXT)"
 fi
 
  if test "${acx_lapack_ok}" = "yes"; then
@@ -32288,15 +32288,15 @@
 use_X11="no"
 if test -z "${no_x}"; then
   ## now we look for Xt and its header: it seems Intrinsic.h is key.
-  r_save_CFLAGS="${CFLAGS}"
-  CFLAGS="${CFLAGS} ${X_CFLAGS}"
+  r_save_CPPFLAGS="${CPPFLAGS}"
+  CPPFLAGS="${CPPFLAGS} ${X_CFLAGS}"
   ac_fn_c_check_header_mongrel "$LINENO" "X11/Intrinsic.h" "ac_cv_header_X11_Intrinsic_h" "$ac_includes_default"
 if test "x$ac_cv_header_X11_Intrinsic_h" = xyes; then :
 
 fi
 
 
-  CFLAGS="${r_save_CFLAGS}"
+  CPPFLAGS="${r_save_CPPFLAGS}"
   if test "${ac_cv_header_X11_Intrinsic_h}" = yes ; then
     { $as_echo "$as_me:${as_lineno-$LINENO}: checking for XtToolkitInitialize in -lXt" >&5
 $as_echo_n "checking for XtToolkitInitialize in -lXt... " >&6; }
@@ -32377,8 +32377,8 @@
 ## check if X11 typedefs KeySym
 
 if test "${use_X11}" = yes; then
-  r_save_CFLAGS="${CFLAGS}"
-  CFLAGS="${CFLAGS} ${X_CFLAGS}"
+  r_save_CPPFLAGS="${CPPFLAGS}"
+  CPPFLAGS="${CPPFLAGS} ${X_CFLAGS}"
   ac_fn_c_check_type "$LINENO" "KeySym" "ac_cv_type_KeySym" "#include <X11/X.h>
 "
 if test "x$ac_cv_type_KeySym" = xyes; then :
@@ -32387,7 +32387,7 @@
   r_cv_type_keysym=no
 fi
 
-  CFLAGS="${r_save_CFLAGS}"
+  CPPFLAGS="${r_save_CPPFLAGS}"
   if test "${r_cv_type_keysym}" = yes; then
 
 $as_echo "#define HAVE_KEYSYM 1" >>confdefs.h
@@ -32396,15 +32396,15 @@
 fi
 ## check if Xmu is supported
 if test "${use_X11}" = yes; then
-  r_save_CFLAGS="${CFLAGS}"
-  CFLAGS="${CFLAGS} ${X_CFLAGS}"
+  r_save_CPPFLAGS="${CPPFLAGS}"
+  CPPFLAGS="${CPPFLAGS} ${X_CFLAGS}"
   ac_fn_c_check_header_mongrel "$LINENO" "X11/Xmu/Atoms.h" "ac_cv_header_X11_Xmu_Atoms_h" "$ac_includes_default"
 if test "x$ac_cv_header_X11_Xmu_Atoms_h" = xyes; then :
 
 fi
 
 
-  CFLAGS="${r_save_CFLAGS}"
+  CPPFLAGS="${r_save_CPPFLAGS}"
   if test "${ac_cv_header_X11_Xmu_Atoms_h}" = yes ; then
     { $as_echo "$as_me:${as_lineno-$LINENO}: checking for XmuInternAtom in -lXmu" >&5
 $as_echo_n "checking for XmuInternAtom in -lXmu... " >&6; }
@@ -34000,15 +34000,15 @@
 #include <string.h>
 #include <zlib.h>
 int main() {
-#ifdef ZLIB_VERSION
-/* Work around Debian bug: it uses 1.2.3.4 even though there was no such
-   version on the master site zlib.net */
-  exit(strncmp(ZLIB_VERSION, "1.2.5", 5) < 0);
+#ifdef ZLIB_VERNUM
+  if (ZLIB_VERNUM < 0x1250) {
+    exit(1);
+  }
+  exit(0);
 #else
   exit(1);
 #endif
 }
-
 _ACEOF
 if ac_fn_c_try_run "$LINENO"; then :
   r_cv_header_zlib_h=yes
diff -uNr R-3.1.3.orig/src/extra/intl/vasnprintf.c R-3.1.3/src/extra/intl/vasnprintf.c
--- R-3.1.3.orig/src/extra/intl/vasnprintf.c	2010-03-17 10:43:05.000000000 -0400
+++ R-3.1.3/src/extra/intl/vasnprintf.c	2017-08-25 06:25:34.000000000 -0400
@@ -4006,7 +4006,7 @@
 #endif
 		  *fbp = dp->conversion;
 #if USE_SNPRINTF
-# if !(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3))
+# if !defined(__APPLE__) && !(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3))
 		fbp[1] = '%';
 		fbp[2] = 'n';
 		fbp[3] = '\0';
diff -uNr R-3.1.3.orig/src/main/Makefile.in R-3.1.3/src/main/Makefile.in
--- R-3.1.3.orig/src/main/Makefile.in	2014-07-14 18:15:06.000000000 -0400
+++ R-3.1.3/src/main/Makefile.in	2017-08-25 06:29:58.000000000 -0400
@@ -104,7 +104,7 @@
 R_bin_OBJECTS = Rmain.o @WANT_R_SHLIB_FALSE@$(OBJECTS)
 @WANT_R_SHLIB_FALSE@R_bin_LDADD = $(MAIN_OBJS) $(EXTRA_STATIC_LIBS) $(EXTRA_LIBS)
 ## Linked against -lRblas becasue -lR is and otherwise ld complains.
-@WANT_R_SHLIB_TRUE@R_bin_LDADD = -lR @BLAS_SHLIB_TRUE@-lRblas
+@WANT_R_SHLIB_TRUE@R_bin_LDADD = ../../lib@R_ARCH@/libR$(DYLIB_EXT) @BLAS_SHLIB_TRUE@../../lib@R_ARCH@/libRblas$(DYLIB_EXT)
 @BUILD_LTO_FALSE@R_bin_DEPENDENCIES =@WANT_R_SHLIB_FALSE@ libR.a @USE_EXPORTFILES_TRUE@ $(top_builddir)/etc/R.exp
 
 libR_la = libR$(DYLIB_EXT)
diff -uNr R-3.1.3.orig/src/nmath/standalone/Makefile.in R-3.1.3/src/nmath/standalone/Makefile.in
--- R-3.1.3.orig/src/nmath/standalone/Makefile.in	2014-03-12 19:03:20.000000000 -0400
+++ R-3.1.3/src/nmath/standalone/Makefile.in	2017-08-25 06:29:58.000000000 -0400
@@ -130,7 +130,7 @@
 
 test: $(srcdir)/test.c
 	$(CC) -o $@ $(ALL_CPPFLAGS) $(ALL_CFLAGS) $(srcdir)/test.c \
-	  -L. -lRmath $(LIBM)
+	  libRmath$(DYLIB_EXT) $(LIBM)
 
 install: installdirs install-header @WANT_R_FRAMEWORK_FALSE@ install-pc
 @WANT_R_FRAMEWORK_FALSE@	@!(test -f $(libRmath_la)) || $(SHELL) $(top_srcdir)/tools/copy-if-change $(libRmath_la) $(DESTDIR)$(libdir)/$(libRmath_la)
