Skip to content

Commit

Permalink
Keep timestamps when copying from java shadows
Browse files Browse the repository at this point in the history
so that it won't break build system's incremental mechanism.

(cherry picked from commit b360931)
  • Loading branch information
ting-yuan authored and KSP Auto Pick committed Jul 21, 2023
1 parent e5588fd commit 2eb5dac
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package com.google.devtools.ksp

import com.google.devtools.ksp.symbol.*
import java.io.File
import java.nio.file.Files
import java.nio.file.StandardCopyOption

abstract class KSVirtualFile(val baseDir: File, val name: String) : KSFile {
override val annotations: Sequence<KSAnnotation>
Expand Down Expand Up @@ -62,3 +64,22 @@ class NoSourceFile(baseDir: File, val fqn: String) : KSVirtualFile(baseDir, "NoS
override val fileName: String
get() = "<NoSourceFile for $fqn is a virtual file; DO NOT USE.>"
}

// Copy recursively, including last-modified-time of file and its parent dirs.
//
// `java.nio.file.Files.copy(path1, path2, options...)` keeps last-modified-time (if supported) according to
// https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html
fun copyWithTimestamp(src: File, dst: File, overwrite: Boolean) {
if (!dst.parentFile.exists())
copyWithTimestamp(src.parentFile, dst.parentFile, false)
if (overwrite) {
Files.copy(
src.toPath(),
dst.toPath(),
StandardCopyOption.COPY_ATTRIBUTES,
StandardCopyOption.REPLACE_EXISTING
)
} else {
Files.copy(src.toPath(), dst.toPath(), StandardCopyOption.COPY_ATTRIBUTES)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ import org.jetbrains.kotlin.types.typeUtil.supertypes
import java.io.DataInput
import java.io.DataOutput
import java.io.File
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import java.util.*

abstract class PersistentMap<K : Comparable<K>, V>(
Expand Down Expand Up @@ -456,25 +454,6 @@ class IncrementalContext(
fun File.abs() = File(baseDir, path)
fun File.bak() = File(bakRoot, abs().toRelativeString(outRoot))

// Copy recursively, including last-modified-time of file and its parent dirs.
//
// `java.nio.file.Files.copy(path1, path2, options...)` keeps last-modified-time (if supported) according to
// https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html
fun copy(src: File, dst: File, overwrite: Boolean) {
if (!dst.parentFile.exists())
copy(src.parentFile, dst.parentFile, false)
if (overwrite) {
Files.copy(
src.toPath(),
dst.toPath(),
StandardCopyOption.COPY_ATTRIBUTES,
StandardCopyOption.REPLACE_EXISTING
)
} else {
Files.copy(src.toPath(), dst.toPath(), StandardCopyOption.COPY_ATTRIBUTES)
}
}

// Backing up outputs is necessary for two reasons:
//
// 1. Currently, outputs are always cleaned up in gradle plugin before compiler is called.
Expand All @@ -488,13 +467,13 @@ class IncrementalContext(

// Backup
outputs.forEach { generated ->
copy(generated.abs(), generated.bak(), true)
copyWithTimestamp(generated.abs(), generated.bak(), true)
}

// Restore non-dirty outputs
cleanOutputs.forEach { dst ->
if (dst !in outputs) {
copy(dst.bak(), dst.abs(), false)
copyWithTimestamp(dst.bak(), dst.abs(), false)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ abstract class AbstractKotlinSymbolProcessingExtension(
roundDir.walkTopDown().forEach {
val dst = File(options.javaOutputDir, File(it.path).toRelativeString(roundDir))
if (dst.isFile || !dst.exists()) {
it.copyTo(dst)
copyWithTimestamp(it, dst, false)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,35 @@ class OutputDepsIt {

val src2Output = mapOf(
"workload/src/main/java/p1/J1.java" to setOf(
"java/p1/J1Generated.java",
"java/p1/K1Generated.java",
"java/p1/K2Generated.java",
"kotlin/p1/J1Generated.kt",
"kotlin/p1/K1Generated.kt",
"kotlin/p1/K2Generated.kt",
"resources/p1.Anno1.log",
"resources/p1.Anno2.log",
),
"workload/src/main/java/p1/J2.java" to setOf(
"java/p1/J2Generated.java",
"kotlin/p1/J2Generated.kt",
"resources/p1.Anno1.log",
"resources/p1.Anno2.log",
),
"workload/src/main/kotlin/p1/K1.kt" to setOf(
"java/p1/J1Generated.java",
"java/p1/K1Generated.java",
"java/p1/K2Generated.java",
"kotlin/p1/J1Generated.kt",
"kotlin/p1/K1Generated.kt",
"kotlin/p1/K2Generated.kt",
"resources/p1.Anno1.log",
"resources/p1.Anno2.log",
),
"workload/src/main/kotlin/p1/K2.kt" to setOf(
"java/p1/J1Generated.java",
"java/p1/K1Generated.java",
"java/p1/K2Generated.java",
"kotlin/p1/J1Generated.kt",
"kotlin/p1/K1Generated.kt",
"kotlin/p1/K2Generated.kt",
Expand All @@ -66,6 +76,11 @@ class OutputDepsIt {

val deletedSrc2Output = listOf(
"workload/src/main/java/p1/J1.java" to listOf(
"java/p1/Anno1Generated.java",
"java/p1/Anno2Generated.java",
"java/p1/J2Generated.java",
"java/p1/K1Generated.java",
"java/p1/K2Generated.java",
"kotlin/p1/Anno1Generated.kt",
"kotlin/p1/Anno2Generated.kt",
"kotlin/p1/J2Generated.kt",
Expand All @@ -75,6 +90,10 @@ class OutputDepsIt {
"resources/p1.Anno2.log",
),
"workload/src/main/java/p1/J2.java" to listOf(
"java/p1/Anno1Generated.java",
"java/p1/Anno2Generated.java",
"java/p1/K1Generated.java",
"java/p1/K2Generated.java",
"kotlin/p1/Anno1Generated.kt",
"kotlin/p1/Anno2Generated.kt",
"kotlin/p1/K1Generated.kt",
Expand All @@ -83,13 +102,18 @@ class OutputDepsIt {
"resources/p1.Anno2.log",
),
"workload/src/main/kotlin/p1/K1.kt" to listOf(
"java/p1/Anno1Generated.java",
"java/p1/Anno2Generated.java",
"java/p1/K2Generated.java",
"kotlin/p1/Anno1Generated.kt",
"kotlin/p1/Anno2Generated.kt",
"kotlin/p1/K2Generated.kt",
"resources/p1.Anno1.log",
"resources/p1.Anno2.log",
),
"workload/src/main/kotlin/p1/K2.kt" to listOf(
"java/p1/Anno1Generated.java",
"java/p1/Anno2Generated.java",
"kotlin/p1/Anno1Generated.kt",
"kotlin/p1/Anno2Generated.kt",
"resources/p1.Anno1.log",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class TestProcessor : SymbolProcessor {
writer.write("private val unused = \"unused\"")
}
}
codeGenerator.createNewFile(Dependencies(false, file), file.packageName.asString(), outputBaseFN, "java")
.use { output ->
OutputStreamWriter(output).use { writer ->
writer.write("class $outputBaseFN {}")
}
}
}
processed = true
return emptyList()
Expand Down

0 comments on commit 2eb5dac

Please sign in to comment.