@@ -24,6 +24,7 @@ sourceCpp <- function(file = "",
2424 embeddedR = TRUE ,
2525 rebuild = FALSE ,
2626 cacheDir = tempdir(),
27+ cleanupCacheDir = FALSE ,
2728 showOutput = verbose ,
2829 verbose = getOption(" verbose" ),
2930 dryRun = FALSE ) {
@@ -32,8 +33,8 @@ sourceCpp <- function(file = "",
3233 # (since cached dynlibs can now perist across sessions we need to be
3334 # sure to invalidate them when R or Rcpp versions change)
3435 cacheDir <- path.expand(cacheDir )
35- cacheDir <- normalizePath(cacheDir , winslash = " /" , mustWork = FALSE )
3636 cacheDir <- .sourceCppPlatformCacheDir(cacheDir )
37+ cacheDir <- normalizePath(cacheDir )
3738
3839 # resolve code into a file if necessary. also track the working
3940 # directory to source the R embedded code chunk within
@@ -197,6 +198,10 @@ sourceCpp <- function(file = "",
197198 setwd(rWorkingDir ) # will be reset by previous on.exit handler
198199 source(file = srcConn , echo = TRUE )
199200 }
201+
202+ # cleanup the cache dir if requested
203+ if (cleanupCacheDir )
204+ cleanupSourceCppCache(cacheDir , context $ cppSourcePath , context $ buildDirectory )
200205
201206 # return (invisibly) a list containing exported functions and modules
202207 invisible (list (functions = context $ exportedFunctions ,
@@ -205,6 +210,27 @@ sourceCpp <- function(file = "",
205210 buildDirectory = context $ buildDirectory ))
206211}
207212
213+
214+ # Cleanup a directory used as the cache for a sourceCpp compilation. This will
215+ # remove all files from the cache directory that aren't a result of the
216+ # compilation that yielded the passed buildDirectory.
217+ cleanupSourceCppCache <- function (cacheDir , cppSourcePath , buildDirectory ) {
218+ # normalize cpp source path and build directory
219+ cppSourcePath <- normalizePath(cppSourcePath )
220+ buildDirectory <- normalizePath(buildDirectory )
221+
222+ # determine the parent dir that was used for the compilation then collect all the
223+ # *.cpp files and subdirectories therein
224+ cacheFiles <- list.files(cacheDir , pattern = glob2rx(" *.cpp" ), recursive = FALSE , full.names = TRUE )
225+ cacheFiles <- c(cacheFiles , list.dirs(cacheDir , recursive = FALSE , full.names = TRUE ))
226+ cacheFiles <- normalizePath(cacheFiles )
227+
228+ # determine the list of tiles that were not yielded by the passed sourceCpp
229+ # result and remove them
230+ oldCacheFiles <- cacheFiles [! cacheFiles %in% c(cppSourcePath , buildDirectory )]
231+ unlink(oldCacheFiles , recursive = TRUE )
232+ }
233+
208234# Define a single C++ function
209235cppFunction <- function (code ,
210236 depends = character (),
@@ -1063,10 +1089,8 @@ sourceCppFunction <- function(func, isVoid, dll, symbol) {
10631089.sourceCppPlatformCacheDir <- function (cacheDir ) {
10641090
10651091 dir <- file.path(cacheDir ,
1066- paste(" sourceCpp " ,
1092+ paste(R.version $ platform ,
10671093 utils :: packageVersion(" Rcpp" ),
1068- R.version $ platform ,
1069- R.version $ `svn rev` ,
10701094 sep = " -" ))
10711095 if (! dir.exists(dir ))
10721096 dir.create(dir , recursive = TRUE )
0 commit comments