@@ -24,6 +24,7 @@ sourceCpp <- function(file = "",
24
24
embeddedR = TRUE ,
25
25
rebuild = FALSE ,
26
26
cacheDir = tempdir(),
27
+ cleanupCacheDir = FALSE ,
27
28
showOutput = verbose ,
28
29
verbose = getOption(" verbose" ),
29
30
dryRun = FALSE ) {
@@ -32,8 +33,8 @@ sourceCpp <- function(file = "",
32
33
# (since cached dynlibs can now perist across sessions we need to be
33
34
# sure to invalidate them when R or Rcpp versions change)
34
35
cacheDir <- path.expand(cacheDir )
35
- cacheDir <- normalizePath(cacheDir , winslash = " /" , mustWork = FALSE )
36
36
cacheDir <- .sourceCppPlatformCacheDir(cacheDir )
37
+ cacheDir <- normalizePath(cacheDir )
37
38
38
39
# resolve code into a file if necessary. also track the working
39
40
# directory to source the R embedded code chunk within
@@ -197,6 +198,10 @@ sourceCpp <- function(file = "",
197
198
setwd(rWorkingDir ) # will be reset by previous on.exit handler
198
199
source(file = srcConn , echo = TRUE )
199
200
}
201
+
202
+ # cleanup the cache dir if requested
203
+ if (cleanupCacheDir )
204
+ cleanupSourceCppCache(cacheDir , context $ cppSourcePath , context $ buildDirectory )
200
205
201
206
# return (invisibly) a list containing exported functions and modules
202
207
invisible (list (functions = context $ exportedFunctions ,
@@ -205,6 +210,27 @@ sourceCpp <- function(file = "",
205
210
buildDirectory = context $ buildDirectory ))
206
211
}
207
212
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
+
208
234
# Define a single C++ function
209
235
cppFunction <- function (code ,
210
236
depends = character (),
@@ -1063,10 +1089,8 @@ sourceCppFunction <- function(func, isVoid, dll, symbol) {
1063
1089
.sourceCppPlatformCacheDir <- function (cacheDir ) {
1064
1090
1065
1091
dir <- file.path(cacheDir ,
1066
- paste(" sourceCpp " ,
1092
+ paste(R.version $ platform ,
1067
1093
utils :: packageVersion(" Rcpp" ),
1068
- R.version $ platform ,
1069
- R.version $ `svn rev` ,
1070
1094
sep = " -" ))
1071
1095
if (! dir.exists(dir ))
1072
1096
dir.create(dir , recursive = TRUE )
0 commit comments