6

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?
JRR
  • 3,024
  • 2
  • 13
  • 37

1 Answers1

5

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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • At least I will recompile all my packages with this option. Thanks. – JRR Dec 19 '19 at 20:37
  • For absolute clarity, what you are saying is, we can put `_R_SHLIB_STRIP_ = 1` in our `src/Makevars` file? – Joseph Wood Dec 27 '19 at 14:21
  • 1
    It is an environment variable so you would but it in `.Renviron` and alike -- see `help(Startup)` as always. I also put it into `install.r` and friends from the littler package. More details in the NEWS file: https://github.com/wch/r-source/blob/trunk/doc/NEWS.Rd#L1120-L1123 (though the line numbers in the link will get stale/wrong over time). – Dirk Eddelbuettel Dec 27 '19 at 14:25