Configuring compilers on Mac M1 (Big Sur, Monterey) for Rcpp and other tools
Background
Currently (2022-04-24), CRAN builds R 4.2 binaries for Apple silicon using Apple clang
from Command Line Tools for Xcode 13.1 and using an experimental fork of GNU Fortran 12.
If you obtain R from CRAN (i.e., here), then you need to replicate CRAN's compiler setup on your system before building R packages that contain C/C++/Fortran code from their sources (and before using Rcpp
, etc.). This requirement ensures that your package builds are compatible with R itself.
A further complication is the fact that Apple clang
doesn't support OpenMP, so you need to do even more work to compile programs that make use of multithreading. You could circumvent the issue by building R itself and all R packages from sources with LLVM clang
, which does support OpenMP, but that approach is onerous and "for experts only".
There is another approach that has been tested by a few people, including Simon Urbanek, the maintainer of R for macOS. It is experimental and also "for experts only", but it works on my machine and is much simpler than learning to build R yourself.
Instructions for obtaining a working toolchain
Warning: These come with no warranty and could break at any time. Some level of familiarity with C/C++/Fortran program compilation, Makefile syntax, and Unix shells is assumed. Everyone is encouraged to consult official documentation, which is more likely to be maintained than answers on SO. As usual, sudo
at your own risk.
I will try to address compilers and OpenMP support at the same time. I am going to assume that you are starting from nothing. Feel free to skip steps you've already taken, though you might find a fresh start helpful.
I've tested these instructions on a machine running Big Sur, and at least one person has tested them on a machine running Monterey. I would be glad to hear from others.
Download an R 4.2 binary from CRAN here and install. Be sure to select the binary built for Apple silicon.
Run
$ sudo xcode-select --install
in Terminal to install the latest release version of Apple's Command Line Tools for Xcode, which includes Apple
clang
. You can obtain earlier versions from your browser here. However, the version that you install should not be older than the one that CRAN used to build your R binary.Download the
gfortran
binary recommended here and install by unpacking to root:$ curl -LO https://mac.r-project.org/tools/gfortran-12.0.1-20220312-is-darwin20-arm64.tar.xz
$ sudo tar xvf gfortran-12.0.1-20220312-is-darwin20-arm64.tar.xz -C /
$ sudo ln -sfn $(xcrun --show-sdk-path) /opt/R/arm64/gfortran/SDKThe last command updates a symlink inside of the
gfortran
installation so that it points to the SDK inside of your Command Line Tools installation.Download an OpenMP runtime suitable for your Apple
clang
version here and install by unpacking to root. You can query your Appleclang
version withclang --version
. For example, I have version 1300.0.29.3, so I did:$ curl -LO https://mac.r-project.org/openmp/openmp-12.0.1-darwin20-Release.tar.gz
$ sudo tar xvf openmp-12.0.1-darwin20-Release.tar.gz -C /After unpacking, you should find these files on your system:
/usr/local/lib/libomp.dylib
/usr/local/include/ompt.h
/usr/local/include/omp.h
/usr/local/include/omp-tools.hAdd the following lines to
$(HOME)/.R/Makevars
, creating the file if necessary.CPPFLAGS+=-I/usr/local/include -Xclang -fopenmp
LDFLAGS+=-L/usr/local/lib -lomp
FC=/opt/R/arm64/gfortran/bin/gfortran -mtune=native
FLIBS=-L/opt/R/arm64/gfortran/lib/gcc/aarch64-apple-darwin20.6.0/12.0.1 -L/opt/R/arm64/gfortran/lib -lgfortran -lemutls_w -lmRun R and test that you can compile a program with OpenMP support. For example:
if (!requireNamespace("RcppArmadillo", quietly = TRUE)) {
install.packages("RcppArmadillo")
}
Rcpp::sourceCpp(code = '
#include <RcppArmadillo.h>
#ifdef _OPENMP
# include <omp.h>
#endif
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
void omp_test()
{
#ifdef _OPENMP
Rprintf("OpenMP threads available: %d\\n", omp_get_max_threads());
#else
Rprintf("OpenMP not supported\\n");
#endif
}
')
omp_test()OpenMP threads available: 8
If the C++ code fails to compile, or if it compiles without error but you get linker warnings or you find that OpenMP is not supported, then one of us has probably made a mistake. Please report any issues.
References
Everything is a bit scattered:
- R Installation and Administration manual [link]
- R for macOS Developers page [link]
Installing R on OSX Big Sur (EDIT: and Apple M1) for use with Rcpp and openMP
Eventually, I found a process that works on a M1 mac with Big Sur.
- Head over to https://mac.r-project.org/, it contains most things you will need
- Download and install R via
R-4.1-branch.pkg
. The CRAN version might also work, but I used the installer from mac.r-project.org, which required opening the osx security settings to allow the installation. - Install RStudio, start it, and let it install the developer tools. Alternatively, run
sudo xcode-select --install
in Terminal. - Head to https://mac.r-project.org/openmp/. Download
openmp-11.0.1-darwin20-Release.tar.gz
and install it (see Terminal commands below).
curl -O https://mac.r-project.org/openmp/openmp-11.0.1-darwin20-Release.tar.gz
sudo tar fvx openmp-11.0.1-darwin20-Release.tar.gz -C /
- Now we need to add compiler flags so that clan uses openMP. In Terminal, create the
Makevars
file.
cd ~
mkdir .R
nano .R/Makevars
in nano, paste these additional compiler flags into the Makevars
file:
CPPFLAGS += -Xclang -fopenmp
LDFLAGS += -lomp
Hit Control+O, Control+X to save and close
- Head over to the gfortran page: https://github.com/fxcoudert/gfortran-for-macOS/releases
- Use the installer
gfortran-ARM-11.0-BigSur.pkg
to install gfortran. - For some reason it appears to install in
/usr/local/gfortran
, but R expects it in/opt
. The mac-R team likes to separate arm64 and intel related files. We could go and fix paths, or simply also install gfortran under/opt
. Download the tar filegfortran-ARM-11.0-BigSur.tar.xz
. You can use curl, or just download it and point tar in the command line to it.
cd /opt/R/arm64/
sudo mkdir gfortran
sudo tar -xzyf gfortran-ARM-11.0-BigSur.tar.xz -C /opt/R/arm64/
(replace gfortran-ARM-11.0-BigSur.tar.xz
with /users/YOURUSERNAME/downloads/gfortran-ARM-11.0-BigSur.tar.xz
)
Now it should work.
Not an expert in OSX, but doing this so others can figure out how to use my R package. I'd like to streamline the process some more, but wiping the mac, reinstalling osx and testing it takes so much time.
re-install xcode12.2 commandline tools on Monterey
After discussion, it appears there is no way to do this. We will just get our code base all up to run on Xcode13
Related Topics
How to Generate Random Numbers in C++
Using C Function from Other Package in Rcpp
Write and Read String to Binary File C++
Why Void_T Doesnt Work in Sfinae But Enable_If Does
How Performing Multiple Matrix Multiplications in Cuda
How to Serialize Boost::Dynamic_Bitset
What Is Linux's Native Gui API
Default Parameters with C++ Constructors
Throw New Std::Exception VS Throw Std::Exception
Why Does Wide File-Stream in C++ Narrow Written Data by Default
If Temporaries Are Implicitly Non-Modifiable, How Does This Work
How to Use C++11 Enum Class for Flags
Detecting Simulated Keyboard/Mouse Input
Creating a New C++ Project in Eclipse Cdt with the Same Settings as Another Project
C++ Visual Studio "Non-Standard Syntax; Use '&' to Create a Pointer to Member"