Search code examples
c++rrcpp

Quickly Build Package Repeatedly in R


I have a package I'm writing in R that has a boatload of complicated C++ code which takes a while to compile.

When I change the code, I'd like to quickly rebuild the package so I can test it. However, R CMD build seems to start from scratch each time, rather than using my code's makefiles to do only what is needed.

Is there a way to quickly do repeated builds of a package in R for testing?


Solution

  • Dirk's answer above pointed me in the right direction, but was insufficient. Since he has requested that I not append the final steps I required, I do so here. My question could not have been answered without the following.

    Dirk's answer did not work off the bat for me. If compilation still seems slow to you, try running:

    ccache -s
    

    The result will look a little like this

    cache directory                     /home/myuser/.ccache
    primary config                      /home/myuser/.ccache/ccache.conf
    secondary config      (readonly)    /etc/ccache.conf
    cache hit (direct)                     0
    cache hit (preprocessed)               0
    cache miss                           989
    cache hit rate                         0 %
    called for link                       12
    preprocessor error                    12
    cleanups performed                    16
    files in cache                       177
    cache size                          31.1 MB
    max cache size                       5.0 GB
    Note that cache isn't getting hit, which means ccache isn't doing anything.
    

    You can use:

    export CCACHE_LOGFILE=ccache.log
    R CMD build .
    

    to do some debugging, though this wasn't helpful for me.

    What fixed things was to run:

    export CCACHE_NOHASHDIR=true
    R CMD build .
    

    As it turns out, ccache sometimes takes the location of a file into account. R CMD build . appears to build in a temporary directory, so the location of the files was changing each time.