0
votes

I want to make a release build for an R package that uses Rcpp, but when I look at the arguments to g++, I see that even though I have a -O3 flag in my Makevars file, Rcpp is overriding this with a -O0 flag at the end, for example I'm seeing this as output

g++ -std=gnu++11 -I"/usr/share/R/include" -DNDEBUG -I"/home/dz5937/R/x86_64-pc-linux-gnu-library/3.6/Rcpp/include" -I"/home/dz5937/R/x86_64-pc-linux-gnu-library/3.6/RcppEigen/include" -O3 -std=c++14 -fpic -g -O2 -fdebug-prefix-map=/build/r-base-jbaK_j/r-base-3.6.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -UNDEBUG -Wall -pedantic -g -O0 -c btsatr.cpp -o btsatr.o

How can I make Rcpp do a release build, i.e. higher optimization level than -O0?

This is what I have in my Makevars file

PKG_CXXFLAGS = -O3 -std=c++14
1

1 Answers

3
votes

That to has come up before but you "can't" short of locally and one-off editing your Makevars for R.

So the short answer (and here I am going to use the path on my Debian / Ubuntu system which has a handy shortcut of /etc/R as a softlink into R's own etc/ directory):

edd@rob:~$ grep -- '-g' /etc/R/Makeconf | grep -v "^#"
CFLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-8T8CYO/r-base-4.0.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g $(LTO)
CXXFLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-8T8CYO/r-base-4.0.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g $(LTO)
CXX11FLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-8T8CYO/r-base-4.0.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g $(LTO)
CXX14FLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-8T8CYO/r-base-4.0.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g $(LTO)
CXX17FLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-8T8CYO/r-base-4.0.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g $(LTO)
CXX20FLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-8T8CYO/r-base-4.0.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g $(LTO)
FCFLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-8T8CYO/r-base-4.0.3=. -fstack-protector-strong $(LTO_FC)
FFLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-8T8CYO/r-base-4.0.3=. -fstack-protector-strong $(LTO_FC)
SAFE_FFLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-8T8CYO/r-base-4.0.3=. -fstack-protector-strong -msse2 -mfpmath=sse
TCLTK_LIBS = -L/usr/lib/x86_64-linux-gnu -ltcl8.6 -L/usr/lib/x86_64-linux-gnu -ltk8.6 -lX11 -lXss -lXext
edd@rob:~$ 

This is (in essence) a combination of three sources:

  • things that R itself hardwires
  • things that the distribution hardwires for gcc and friends
  • things that package maintainer (me, actually) specifies for the distro

All this sets your CXXFLAGS (and same for CXX11... CXX14... ...). You can then set

  • your user-local values via ~/.R/Makevars
  • your package-local values via src/Makevars

The really weird thing is that even after the almost 25 years that we have no mechanism to edit and override this and selectively undo. I think I recall that for gcc et al the rightmost value wins...

So to sum up, to be "sure" you'd have to edit out the -O0 value in your machine-local config file.