Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compacting Shared Libraries in R package

Tags:

r

rcpp

r-package

My package .so file is above 3 MB (up to 10 MB) depending of the compiler and the system. This generates a NOTE with R CMD check in my package for years. My package does not contain so much code so I eventually searched to reduce the size and I found this excellent post by Dirk Eddelbuettel.

Following the advice I added SHLIB_CXX11LDFLAGS = -Wl,-S -shared in my .R/Makevars and my library size dropped from 10.4 MB to 580 KB!! For the first time I have 0 ERRORS, 0 WARNINGS and 0 NOTES. Yeah!

However this is only a local solution. At the end of the post the following is suggested for src/Makevars

strippedLib: $(SHLIB)
        if test -e "/usr/bin/strip"; then /usr/bin/strip --strip-debug $(SHLIB); fi

.phony: strippedLib

But it is mentioned that:

And this scheme may even pass muster with CRAN, but I have not yet tried.

My questions are the following:

  • The post is from Aug 2017. Does somebody knows if it passes CRAN check?
  • This is a GNU/Linux (maybe macOS) solution. Is there a cross-platform option?
like image 785
JRR Avatar asked Dec 19 '19 19:12

JRR


1 Answers

AFAIK you cannot put it in src/Makevars. I just had to revert this myself in a package where the powers-that-be noticed it.

But then, confusingly, we also have

edd@rob:~$ grep -i strip /etc/R/Makeconf     # convenience softlink on Debian/Ubuntu
STRIP_STATIC_LIB = strip --strip-debug
STRIP_SHARED_LIB = strip --strip-unneeded
edd@rob:~$ 

but I haven't yet had time to search if/where these are used. So I still do

edd@rob:~$ grep -i strip ~/.R/Makevars 
STRIP=-Wl,-S
SHLIB_CXXLDFLAGS = $(STRIP) -shared
SHLIB_CXX11LDFLAGS = $(STRIP) -shared
SHLIB_CXX14LDFLAGS = $(STRIP) -shared
SHLIB_FCLDFLAGS = $(STRIP) -shared
SHLIB_LDFLAGS = $(STRIP) -shared
edd@rob:~$ 

which is local-only.

Edit: Something I keep forgetting is the recently added --strip option for the installer:

edd@rob:~$ R CMD INSTALL --help | grep strip
      --strip           strip shared object(s)
edd@rob:~$ 

which can also be enable by setting the environment variable _R_SHLIB_STRIP_ to a true value -- see a recent NEWS file for R.

like image 111
Dirk Eddelbuettel Avatar answered Oct 18 '22 03:10

Dirk Eddelbuettel