Building R-devel from Source

macOS 10.13.2


This page describes the steps involved in compiling R-devel on a Mac operating system (macOS, formerly known as OS X), with an emphasis on non-standard build configurations that can significantly improve computing performance compared to a standard build.


Preliminaries

First we need to install some libraries and tools.

  • Get Xcode from the App Store. (The page should look something like this.) The latest version of Xcode should install the command line tools by default.
    • Open a terminal window—search spotlight for 'terminal' if you've never used terminal/bash before—and run:
    • clang++ -v
    • You should see something similar to
    •  
      Apple LLVM version 9.0.0 (clang-900.0.39.2)
      Target: x86_64-apple-darwin17.3.0
      Thread model: posix
  • Download and install XQuartz. (A computer restart might be required to complete the installation process.)
  • Download and install the Java SDK. (No need for the 'demos and samples' pack.)

Most of the following items are from the R-devel optional library list.

  • Unfortunately, Xcode does not include a Fortran compiler, which we will need. The simplest way to obtain an R-compatible version of gfortran is to open a terminal window and type:
  • cd ~/Desktop
    curl -O http://r.research.att.com/libs/gfortran-4.8.2-darwin13.tar.bz2
    sudo tar fvxz gfortran-4.8.2-darwin13.tar.bz2 -C /

    The 'sudo' command will require your system password because we are placing files into areas that are password restricted (/usr/local).

    Note: that you are free to choose a more recent distribution of gfortran, which may be obtained via MacPorts or Homebrew.

  • We also need pkg-config, xz, and pcre:
  • cd ~/Desktop
    curl -O http://r.research.att.com/libs/pkgconfig-0.28-darwin.15-x86_64.tar.gz
    sudo tar fvxz pkgconfig-0.28-darwin.15-x86_64.tar.gz -C /
    curl -O http://r.research.att.com/libs/xz-5.2.3-darwin.15-x86_64.tar.gz
    sudo tar fvxz xz-5.2.3-darwin.15-x86_64.tar.gz -C /
    curl -O http://r.research.att.com/libs/pcre-8.40-darwin.15-x86_64.tar.gz
    sudo tar fvxz pcre-8.40-darwin.15-x86_64.tar.gz -C /
  • Finally, for creating images, we need JPEG, PNG, and TIFF libraries:
  • cd ~/Desktop
    curl -O http://r.research.att.com/libs/jpeg-9-darwin.15-x86_64.tar.gz
    sudo tar fvxz jpeg-9-darwin.15-x86_64.tar.gz -C /
    curl -O http://r.research.att.com/libs/libpng-1.6.28-darwin.15-x86_64.tar.gz
    sudo tar fvxz libpng-1.6.28-darwin.15-x86_64.tar.gz -C /
    curl -O http://r.research.att.com/libs/tiff-4.0.7-darwin.15-x86_64.tar.gz
    sudo tar fvxz tiff-4.0.7-darwin.15-x86_64.tar.gz -C /

Download and Unpack

Create a folder to work in. For example,

cd ~/Desktop
mkdir R

Then download and unpack the latest development version of R:

cd ~/Desktop/R
curl -O ftp://ftp.stat.math.ethz.ch/Software/R/R-devel.tar.gz
tar -zxvf R-devel.tar.gz

Alternatively, to obtain R via SVN:

svn co https://svn.r-project.org/R/trunk/ ~/Desktop/R/trunk
cp -rf ~/Desktop/R/trunk ~/Desktop/R/R-devel # copy to build folder

Update when needed using

cd ~/Desktop/R/trunk
svn up

Basic Configure

Open the 'config.site' file in '~/Desktop/R/R-devel' and change the contents to the following (but see the next section for alternative configurations):

#! /bin/sh

CC=clang
CXX=clang++
F77=gfortran-4.8
FC=$F77
OBJC=clang
CFLAGS="-Wall -mtune=core2 -g -O2"
CXXFLAGS="-Wall -mtune=core2 -g -O2"
OBJCFLAGS="-Wall -mtune=core2 -g -O2"
F77FLAGS="-Wall -g -O2"
FCFLAGS=$F77FLAGS

PKG_CONFIG_PATH=/opt/X11/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

JAVA_HOME="$(/usr/libexec/java_home)"
JAVA_CPPFLAGS="-I/${JAVA_HOME}/include -I/${JAVA_HOME}/include/darwin"
JAVA_LD_LIBRARY_PATH="${JAVA_HOME}/jre/lib/server"
JAVA_LIBS="-L/${JAVA_HOME}/jre/lib/server -ljvm"

Open a terminal window and switch to the R-devel folder:

cd ~/Desktop/R/R-devel

Set the pkg-config path as follows:

export PKG_CONFIG_PATH=/opt/X11/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

Alternative Configure: Optimization and OpenMP

It is possible to use an updated version of gfortran while also enabling architecture-specific optimizations such as Intel's AVX instructions.

If you've installed gcc7 using MacPorts, then create a symbolic link to the LLVM assembler in the default install directory:

    cd /opt/local/bin
    sudo ln -s /usr/bin/clang

Then add -Wa,-q to the Fortran flags to use LLVM's (Clang integrated) assembler:

#! /bin/sh
    
CC=clang
CXX=clang++
F77=gfortran-mp-7
FC=$F77
OBJC=clang
CFLAGS="-Wall -march=native -g -O3 -ffp-contract=fast"
CXXFLAGS="-Wall -march=native -g -O3 -ffp-contract=fast"
OBJCFLAGS="-Wall -march=native -g -O3"
F77FLAGS="-Wall -march=native -g -O3 -Wa,-q"
FCFLAGS=$F77FLAGS
FFLAGS=$F77FLAGS
    
PKG_CONFIG_PATH=/opt/X11/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig
    
JAVA_HOME="$(/usr/libexec/java_home)"
JAVA_CPPFLAGS="-I/${JAVA_HOME}/include -I/${JAVA_HOME}/include/darwin"
JAVA_LD_LIBRARY_PATH="${JAVA_HOME}/jre/lib/server"
JAVA_LIBS="-L/${JAVA_HOME}/jre/lib/server -ljvm"

Note: Without the -Wa,-q option, -march=native instructions will result in an unknown symbol error.

To utilise OpenMP features we need to switch from Apple's version of LLVM. Install Clang v6.0 via MacPorts:

sudo port install clang-6.0

and modify the config.site file:

#! /bin/sh

CC=clang-mp-6.0
CXX=clang++-mp-6.0
F77=gfortran-mp-7
FC=$F77
OBJC=clang-mp-6.0
CFLAGS="-Wall -march=native -g -O3 -ffp-contract=fast"
CXXFLAGS="-Wall -march=native -g -O3 -ffp-contract=fast"
OBJCFLAGS="-Wall -march=native -g -O3"
F77FLAGS="-Wall -march=native -g -O3 -Wa,-q"
FCFLAGS=$F77FLAGS
FFLAGS=$F77FLAGS

PKG_CONFIG_PATH=/opt/X11/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

JAVA_HOME="$(/usr/libexec/java_home)"
JAVA_CPPFLAGS="-I/${JAVA_HOME}/include -I/${JAVA_HOME}/include/darwin"
JAVA_LD_LIBRARY_PATH="${JAVA_HOME}/jre/lib/server"
JAVA_LIBS="-L/${JAVA_HOME}/jre/lib/server -ljvm"

Note: If you receive a 'library not found for -lomp' error, add a symbolic link to libomp.dylib under /usr/local/lib:

sudo ln -s /opt/local/lib/libomp/libomp.dylib /usr/local/lib/libomp.dylib

Select a BLAS and Lapack Distribution

R includes a single-threaded BLAS and Lapack distribution, but we are free to choose an external, multithreaded library.

To link against the Accelerate framework (pre-installed with macOS) run

./configure --prefix=/Users/${USER}/Desktop/R-devel --without-x --without-internal-tzcode \
	--with-blas="-framework Accelerate" --with-lapack \
		--enable-R-shlib --enable-memory-profiling

If you want to link against the OpenBLAS library run

./configure --prefix=/Users/${USER}/Desktop/R-devel --without-x --without-internal-tzcode \
	--with-blas="-lopenblas" --with-lapack \
		--enable-R-shlib --enable-memory-profiling

Build and Install

To build R-devel, run make:

cd ~/Desktop/R/R-devel
make

The build checks are run using:

make check

Finally, we need to choose an install location for R-devel; I use /Users/${USER}/Desktop/R-devel; I recommend installing to somewhere that does not require sudo privileges.

make prefix=/Users/${USER}/Desktop/R-devel install

Calling R-devel

For easy access to R-devel from a terminal window, add the following lines of code to your bash profile:

alias Rdevel="/where/I/installed/to/bin/R"

For example,

alias Rdevel="/Users/${USER}/Desktop/R-devel/bin/R"

To run R-devel with sudo privileges, you need to add

alias sudo='sudo '

To run R-devel from RStudio, use

export RSTUDIO_WHICH_R=/where/I/installed/to/bin/R
open -n -a RStudio