Makevar Settings for R from Homebrew - r

I install and use home-brew on my mac's and always run into configuration issues with a few packages. The latest being 'jpeg'.
This is primarily due to the packages not searching /usr/local/include and lib. I've bounced around the internet and bit's of R documentation, but I can't find much about the canonical way to set LDFLAGS and CFLAGS for all packages.
So the question is, what is the canonical way to set LDFLAGS and CFLAGS for all packages through Makevar?
My ~/.R/Makevars file currently looks like this:
CC=gcc-4.9
CXX=g++-4.9
R_XTRA_CFLAGS=-std=c99 -I/usr/local/include
LDFLAGS+=-L/usr/local/lib

Related

Resolve issues installing R packages after switch from Intel to apple silicon

I just migrated things over from my old mac to my my new mac (Intel to Apple Silicon), installed the latest version of R, ran brew upgrade, and began updating R packages. Many needed to be reinstalled, some from source. I bumped into several issues.
Library linkages not being found (e.g., Mac OS Big Sur R compilation error: ld: framework not found CoreFoundation)
for CRAN packages, having the CRAN mirror selection pop up in a weird X11 GUI window instead of a numeric selection in the R console (which was the previous behavior).
I didn't really want to troubleshoot each library issue. I got the sense that ~/.R/Makevars and ~/.Rprofile might need to be edited. What did I need to do to make these problems go away?
A few resources were helpful, but some seemed out of date or incomplete. Here is my attempt to give a full step-by-step recreation of what I think worked, and I will edit as needed while I get my new system up and running.
Based largely on the recommendations here, I edited the Makevars file as follows:
In the shell:
vi ~/.R/Makevars
I then hit i (for insert) and deleted Everything that was there. I then added:
LDFLAGS += -L/opt/homebrew/opt/libomp/lib
CPPFLAGS += -I/opt/homebrew/opt/libomp/include
FLIBS =-L/opt/homebrew/opt/gfortran/lib
F77 = /opt/homebrew/bin/gfortran
FC = /opt/homebrew/bin/gfortran
CFLAGS = -I/opt/homebrew/include
CPPFLAGS = -I/opt/homebrew/include
CXXFLAGS = -I/opt/homebrew/include
To save, I hit esc and then :x to save and close.
AFAIK, this resolved the issues with library finding/loading/linking, and I have installed several packages from source without error now.
To deal with the annoying pop-out GUI window to select the CRAN mirror, I edited the ~/.Rprofile (also using vi as described above) to read:
local({r <- getOption("repos")
r["CRAN"] <- "https://cloud.r-project.org"
options(repos=r)
})
options(menu.graphics = FALSE)
This was based on the best looking answers to this old but still important question

Installing GSL in R (two versions?)

Trying to figure out how to get a new version of gsl recognized by R. I added the last two lines to the Makevars file because I couldn't get the system to recognize this new version as default.
CXX14FLAGS=-O3 -march=native -mtune=native -fPIC
CXX14=g++ -std=c++14
CXX_STD=CXX14
GSL_CFLAGS=-I/usr/local/gsl/2.4/include
GSL_LIBGS=-L/usr/local/gsl/2.4/lib -lgsl -lgslcblas -lm
However, when I go to install, you can see part of it is calling 2.4 and part isn't.
Note that according to gsl's github page, the error I'm getting is consistent with a 1.X version of gsl.
Any help (1) getting centos to recognize the new gsl version or (2) just getting R to use it to compile the gsl package would be greatly appreciated!
The problem is that gsl 2.5>= is needed. The error message is not updated, see
https://github.com/RobinHankin/gsl/issues/18

How to override compilers used by R?

When I try to compile R packages from source, it uses the compilers and settings defined in etc/Makeconf within the R installation directory. How can I override these settings?
I have an ~/.R/Makevars file (suggested e.g. here), and I included the settings I want there, but these are not being used for some reason. Why not, and how can I fix this?
I could not find the official documentation on ~/.R/Makevars and Makeconf—links would be welcome.
In the past, this very same setup used to work correctly for me, but recently it doesn't. I assume something must have changed in recent R versions, but I am not sure when. Were there any recent changes that could have affected this?
Motivation and context:
I am on macOS and I want to use gfortran from MacPorts. Thus I set FC = /opt/local/bin/gfortran-mp-11 and FLIBS = -L/opt/local/lib/gcc11 -lgfortran -lquadmath -lm in ~/.R/Makevars. However, the system still wants to use a gfortran installation in /usr/local, which does not exist on my machine. It clearly takes the paths and options from etc/Makeconf. I am using the official R binaries.
It turns out that the reason why ~/.R/Makevars was ignored on my machine when trying to build a certain package was a bug in withr:
https://github.com/r-lib/withr/issues/169
Installing the development version of withr using devtools::install_github("r-lib/withr#master") fixed the problem.

How do I linking to a non-standard library when building an R package

I'm trying to build the R openssl package and link it to an openssl library that is a newer version than the one in the systemwide lib64 directory.
The Makevars file simply has:
KG_CPPFLAGS=#cflags#
PKG_LIBS=#libs#
I can specify my local library by passing a custom library path in the configure script. But when I issue R CMD INSTALL, the linking command begins with
gcc -std=gnu99 -shared -L/usr/local/lib64 -o openssl.so [...objects...] -Lmy/path/here -lssl -lcrypto
As a result, the linker finds the wrong library (the one in /usr/local/lib64) instead of mine.
How do I adjust the linking command so that it looks for the library first in my directory, instead of the system folder.
(Background: I don't have root access and this older version of CentOS (5) doesn't support openssl v1, so I built v1 myself just to compile the R package. But I'm stuck at the linking stage due to whatever magic is happening by R CMD INSTALL.)
I solved this by passing arguments to R CMD INSTALL. I.e.:
R CMD INSTALL . --configure-vars="INCLUDE_DIR=$HOME/local/include LIB_DIR=$HOME/local/lib64"
But this varies for each package. There's no general solution.
Instead, read config and src/Makefile.in.

Construction of flags in R CMD SHLIB

I am having a lot of trouble understanding the steps that construct the compilation flags when using R CMD SHLIB. I use the following:
in ~/.R/Makevars define `CPPFLAGS=-O3'
in src/Makevars.win define PKG_CPPFLAGS = -O3
include Rcpp and RcppGSL as dependencies in the DESCRIPTION file
where the second step is clearly redundant, but I do it just in case PKG_CXXFLAGS was initialized to something different.
But when I run R CMD SHLIB I find my -O3 flags but also -UNDEBUG -Wall -pedantic -g -O0. Where can those additional flags be coming from? Can Rcpp and RcppGSL affect these flags in a way I can't control through my Makevars.in?
For per-user, or personal settings, I just use ~/.R/Makevars which will affect all R CMD SHLIB or R CMD COMPILE calls, including those from Rcpp.
You can also define them in per-project Makefiles, or src/Makevars, and besides the per-user files, you can of course edit the system-wide variant (which is where the other values that confused you came from). See the file $R_HOME/etc/Makeconf for the latter.
Lastly, DESCRIPTION has nothing to do here.

Resources