Search code examples
rrscriptinstall.packages

Override Specific Compiler Flags When Installing R Packages


I have a R script that tries to install many packages (omitted all but showing just one):

install.packages("zoo")

Then I run

Rscript my_r.r

Then, I noticed it will try to compile some C code:

gcc -m64 -std=gnu99 -I"/usr/include/R" -DNDEBUG -I../inst/include -I"/latest/rsg_comm/r_packages/zoo/include" -I/usr/local/include  -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c any.c -o any.o

Is there a way to ask all packages to be compiled locally with -O3 and -mtune=native?

I noticed that there is a similar post that suggests using ~/R/.Makevars. But it seems like

  1. it will override all compiler flags instead of just those 2 I specify. Is there a way to specify?
  2. I have to download the source package of each source, which is not as convenient as just using install.packages("package_name"), which will figure the latest version, and go through a mirror, etc. Or there is a convenient way?

Solution

  • You can edit your .R/Makevars file and append the desired flags using the += operator, e.g.

    CFLAGS+= -O3 -Wall -mtune=native -march=native
    

    The latter flag is used if there is a conflict, as you said in your comment below. In terms of compiling from source, you can do this via install.packages(), e.g.

    install.packages("package_name", type = "source")