@@ -124,24 +124,24 @@ class FileToFilesMap(storageFile: File) : BasicMap<File, Collection<File>>(stora
124
124
}
125
125
126
126
object symbolCollector : KSDefaultVisitor<(LookupSymbol ) -> Unit, Unit>() {
127
- override fun defaultHandler (node : KSNode , collect : (LookupSymbol ) -> Unit ) = Unit
127
+ override fun defaultHandler (node : KSNode , data : (LookupSymbol ) -> Unit ) = Unit
128
128
129
- override fun visitDeclaration (declaration : KSDeclaration , collect : (LookupSymbol ) -> Unit ) {
129
+ override fun visitDeclaration (declaration : KSDeclaration , data : (LookupSymbol ) -> Unit ) {
130
130
if (declaration.isPrivate())
131
131
return
132
132
133
133
val name = declaration.simpleName.asString()
134
134
val scope = declaration.qualifiedName?.asString()?.let { it.substring(0 , Math .max(it.length - name.length - 1 , 0 ))} ? : return
135
- collect (LookupSymbol (name, scope))
135
+ data (LookupSymbol (name, scope))
136
136
}
137
137
138
- override fun visitDeclarationContainer (declarationContainer : KSDeclarationContainer , collect : (LookupSymbol ) -> Unit ) {
138
+ override fun visitDeclarationContainer (declarationContainer : KSDeclarationContainer , data : (LookupSymbol ) -> Unit ) {
139
139
// Local declarations aren't visible to other files / classes.
140
140
if (declarationContainer is KSFunctionDeclaration )
141
141
return
142
142
143
143
declarationContainer.declarations.forEach {
144
- it.accept(this , collect )
144
+ it.accept(this , data )
145
145
}
146
146
}
147
147
}
@@ -155,15 +155,20 @@ class IncrementalContext(
155
155
private val options : KspOptions ,
156
156
private val ksFiles : List <KSFile >,
157
157
private val componentProvider : ComponentProvider ,
158
- private val anyChangesWildcard : File ,
159
- private val isIncremental : Boolean
158
+ private val anyChangesWildcard : File
160
159
) {
161
160
// Symbols defined in changed files. This is used to update symbolsMap in the end.
162
161
private val updatedSymbols = MultiMap .createSet<File , LookupSymbol >()
163
162
164
- // Symbols defined in each file. This is
163
+ // Symbols defined in each file. This is saved across processing.
165
164
private val symbolsMap = FileToSymbolsMap (File (options.cachesDir, " symbols" ))
166
165
166
+ private val cachesUpToDateFile = File (options.cachesDir, " caches.uptodate" )
167
+ private val isIncremental = options.incremental
168
+ private var rebuild = ! isIncremental || ! cachesUpToDateFile.exists()
169
+ || (options.knownModified.isEmpty() && options.knownRemoved.isEmpty())
170
+ || (options.knownModified + options.knownRemoved).any { ! it.isKotlinFile(listOf (" kt" )) && ! it.isJavaFile() }
171
+
167
172
private val baseDir = options.projectBaseDir
168
173
169
174
private val logsDir = File (baseDir, " build" ).apply { mkdir() }
@@ -195,7 +200,7 @@ class IncrementalContext(
195
200
}
196
201
}
197
202
198
- fun updateLookupCache (dirtyFiles : Collection <File >) {
203
+ private fun updateLookupCache (dirtyFiles : Collection <File >) {
199
204
if (lookupTracker is LookupTrackerImpl ) {
200
205
lookupCache.update(lookupTracker, dirtyFiles, options.knownRemoved)
201
206
lookupCache.flush(false )
@@ -261,7 +266,7 @@ class IncrementalContext(
261
266
return visited
262
267
}
263
268
264
- fun logDirtyFilesByDeps (dirtyFiles : Collection <File >) {
269
+ private fun logDirtyFilesByDeps (dirtyFiles : Collection <File >) {
265
270
if (! options.incrementalLog)
266
271
return
267
272
@@ -276,7 +281,7 @@ class IncrementalContext(
276
281
logFile.appendText(" \n " )
277
282
}
278
283
279
- fun logDirtyFilesByOutputs (dirtyFiles : Collection <File >) {
284
+ private fun logDirtyFilesByOutputs (dirtyFiles : Collection <File >) {
280
285
if (! options.incrementalLog)
281
286
return
282
287
@@ -299,7 +304,7 @@ class IncrementalContext(
299
304
logFile.appendText(" \n " )
300
305
}
301
306
302
- fun logSourceToOutputs () {
307
+ private fun logSourceToOutputs () {
303
308
if (! options.incrementalLog)
304
309
return
305
310
@@ -315,7 +320,7 @@ class IncrementalContext(
315
320
logFile.appendText(" \n " )
316
321
}
317
322
318
- fun logDirtyFiles (files : List <KSFile >) {
323
+ private fun logDirtyFiles (files : List <KSFile >) {
319
324
if (! options.incrementalLog)
320
325
return
321
326
@@ -333,7 +338,12 @@ class IncrementalContext(
333
338
334
339
// Beware: no side-effects here; Caches should only be touched in updateCaches.
335
340
fun calcDirtyFiles (): Collection <KSFile > {
336
- if (isIncremental) {
341
+ if (! isIncremental) {
342
+ cleanIncrementalCache()
343
+ return ksFiles
344
+ }
345
+
346
+ if (! rebuild) {
337
347
val dirtyFilesByDeps = calcDirtySetByDeps()
338
348
339
349
logDirtyFilesByDeps(dirtyFilesByDeps)
@@ -357,7 +367,7 @@ class IncrementalContext(
357
367
}
358
368
}
359
369
360
- fun updateSourceToOutputs (dirtyFiles : Collection <File >, outputs : Set <File >, sourceToOutputs : Map <File , Set <File >>) {
370
+ private fun updateSourceToOutputs (dirtyFiles : Collection <File >, outputs : Set <File >, sourceToOutputs : Map <File , Set <File >>) {
361
371
// Prune deleted sources in source-to-outputs map.
362
372
removed.forEach {
363
373
sourceToOutputsMap.remove(it)
@@ -375,7 +385,7 @@ class IncrementalContext(
375
385
}
376
386
377
387
// TODO: Recover if processing failed.
378
- fun updateOutputs (outputs : Set <File >, cleanOutputs : Collection <File >) {
388
+ private fun updateOutputs (outputs : Set <File >, cleanOutputs : Collection <File >) {
379
389
val outRoot = options.kspOutputDir
380
390
val bakRoot = File (options.cachesDir, " backups" )
381
391
@@ -406,12 +416,12 @@ class IncrementalContext(
406
416
}
407
417
408
418
// TODO: Don't do anything if processing failed.
409
- fun updateCaches (dirtyFiles : Collection <File >, outputs : Set <File >, sourceToOutputs : Map <File , Set <File >>) {
419
+ private fun updateCaches (dirtyFiles : Collection <File >, outputs : Set <File >, sourceToOutputs : Map <File , Set <File >>) {
410
420
updateSourceToOutputs(dirtyFiles, outputs, sourceToOutputs)
411
421
updateLookupCache(dirtyFiles)
412
422
413
423
// Update symbolsMap
414
- if (isIncremental ) {
424
+ if (! rebuild ) {
415
425
// Update symbol caches from modified files.
416
426
options.knownModified.forEach {
417
427
symbolsMap.set(it, updatedSymbols[it].toSet())
@@ -432,6 +442,12 @@ class IncrementalContext(
432
442
}
433
443
434
444
fun updateCachesAndOutputs (dirtyFiles : Collection <KSFile >, outputs : Set <File >, sourceToOutputs : Map <File , Set <File >>) {
445
+ if (! isIncremental)
446
+ return
447
+
448
+ cachesUpToDateFile.delete()
449
+ assert (! cachesUpToDateFile.exists())
450
+
435
451
val cleanOutputs = mutableSetOf<File >()
436
452
val dirtyFilePaths = dirtyFiles.map { it.relativeFile }
437
453
sourceToOutputsMap.keys.forEach { source ->
@@ -442,6 +458,8 @@ class IncrementalContext(
442
458
updateCaches(dirtyFilePaths, outputs, sourceToOutputs)
443
459
updateOutputs(outputs, cleanOutputs)
444
460
461
+ cachesUpToDateFile.createNewFile()
462
+ assert (cachesUpToDateFile.exists())
445
463
}
446
464
447
465
// Insert Java file -> names lookup records.
0 commit comments