Migrate cachedCompileIncrementalTask to dir caching

This commit is contained in:
Eugene Yokota 2024-08-15 00:03:15 -04:00
parent c9e5924b09
commit e0f0550276
5 changed files with 44 additions and 53 deletions

View File

@ -923,9 +923,9 @@ object Defaults extends BuildCommon {
compileOutputs := {
import scala.jdk.CollectionConverters.*
val c = fileConverter.value
val (_, jarFile) = compileIncremental.value
val (_, vfDir, _) = compileIncremental.value
val classFiles = compile.value.readStamps.getAllProductStamps.keySet.asScala
classFiles.toSeq.map(c.toPath) :+ compileAnalysisFile.value.toPath :+ c.toPath(jarFile)
classFiles.toSeq.map(c.toPath) :+ compileAnalysisFile.value.toPath :+ c.toPath(vfDir)
},
compileOutputs := compileOutputs.triggeredBy(compile).value,
tastyFiles := Def.taskIf {
@ -2530,10 +2530,6 @@ object Defaults extends BuildCommon {
val dir = c.toPath(backendOutput.value).toFile
result match
case Result.Value(res) =>
val rawJarPath = c.toPath(res._2)
IO.delete(dir)
IO.unzip(rawJarPath.toFile, dir)
IO.delete(dir / "META-INF" / "MANIFEST.MF")
val analysis = store.unsafeGet().getAnalysis()
reporter.sendSuccessReport(analysis)
bspTask.notifySuccess(analysis)
@ -2544,13 +2540,6 @@ object Defaults extends BuildCommon {
bspTask.notifyFailure(compileFailed)
throw cause
},
packagedArtifact := {
val (hasModified, out) = compileIncremental.value
artifact.value -> out
},
artifact := artifactSetting.value,
artifactClassifier := Some("noresources"),
artifactPath := artifactPathSetting(artifact).value,
)
)
@ -2571,21 +2560,11 @@ object Defaults extends BuildCommon {
val contents = AnalysisContents.create(analysisResult.analysis(), analysisResult.setup())
store.set(contents)
Def.declareOutput(analysisOut)
val dir = ci.options.classesDirectory.toFile()
val mappings = Path
.allSubpaths(dir)
.filter(_._1.isFile())
.map { case (p, path) =>
val vf = c.toVirtualFile(p.toPath())
(vf: HashedVirtualFileRef) -> path
}
.toSeq
// inlined to avoid caching mappings
val pkgConfig = Pkg.Configuration(mappings, artifactPath.value, packageOptions.value)
val out = Pkg(pkgConfig, c, s.log, Pkg.timeFromConfiguration(pkgConfig))
s.log.debug(s"wrote $out")
Def.declareOutput(out)
analysisResult.hasModified() -> (out: HashedVirtualFileRef)
val dir = ci.options.classesDirectory
val vfDir = c.toVirtualFile(dir)
val packedDir = Def.declareOutputDirectory(vfDir)
s.log.debug(s"wrote $vfDir")
(analysisResult.hasModified(), vfDir: VirtualFileRef, packedDir: HashedVirtualFileRef)
}
.tag(Tags.Compile, Tags.CPU)
@ -2732,6 +2711,7 @@ object Defaults extends BuildCommon {
inputs.options.sources.toVector,
scalacOptions.value.toVector,
javacOptions.value.toVector,
outputPath.value + prefix(configuration.value.name),
)
},
bspCompileTask :=
@ -4425,17 +4405,19 @@ object Classpaths {
def makeProducts: Initialize[Task[Seq[File]]] = Def.task {
val c = fileConverter.value
val resources = copyResources.value.map(_._2).toSet
val dir = classDirectory.value
val rawJar = compileIncremental.value._2
val rawJarPath = c.toPath(rawJar)
val classDir = classDirectory.value
val vfBackendDir = compileIncremental.value._2
val backendDir = c.toPath(vfBackendDir)
// delete outdated files
Path
.allSubpaths(dir)
.allSubpaths(classDir)
.collect { case (f, _) if f.isFile() && !resources.contains(f) => f }
.foreach(IO.delete)
IO.unzip(rawJarPath.toFile, dir)
IO.delete(dir / "META-INF" / "MANIFEST.MF")
dir :: Nil
IO.copyDirectory(
source = backendDir.toFile(),
target = classDir,
)
classDir :: Nil
}
private[sbt] def makePickleProducts: Initialize[Task[Seq[VirtualFile]]] = Def.task {

View File

@ -250,7 +250,7 @@ object Keys {
val consoleProject = taskKey[Unit]("Starts the Scala interpreter with the sbt and the build definition on the classpath and useful imports.").withRank(AMinusTask)
val compile = taskKey[CompileAnalysis]("Compiles sources.").withRank(APlusTask)
val manipulateBytecode = taskKey[CompileResult]("Manipulates generated bytecode").withRank(BTask)
val compileIncremental = taskKey[(Boolean, HashedVirtualFileRef)]("Actually runs the incremental compilation").withRank(DTask)
val compileIncremental = taskKey[(Boolean, VirtualFileRef, HashedVirtualFileRef)]("Actually runs the incremental compilation").withRank(DTask)
val previousCompile = taskKey[PreviousResult]("Read the incremental compiler analysis from disk").withRank(DTask)
val tastyFiles = taskKey[Seq[File]]("Returns the TASTy files produced by compilation").withRank(DTask)
private[sbt] val compileScalaBackend = taskKey[CompileResult]("Compiles only Scala sources if pipelining is enabled. Compiles both Scala and Java sources otherwise").withRank(Invisible)

View File

@ -10,6 +10,7 @@ case class CompileInputs2(
sources: Vector[HashedVirtualFileRef],
scalacOptions: Vector[String],
javacOptions: Vector[String],
relativeOutputPath: String,
)
object CompileInputs2:
@ -18,7 +19,7 @@ object CompileInputs2:
given IsoLList.Aux[
CompileInputs2,
Vector[HashedVirtualFileRef] :*: Vector[HashedVirtualFileRef] :*: Vector[String] :*:
Vector[String] :*: LNil
Vector[String] :*: String :*: LNil
] =
LList.iso(
{ (v: CompileInputs2) =>
@ -26,12 +27,19 @@ object CompileInputs2:
("sources", v.sources) :*:
("scalacOptions", v.scalacOptions) :*:
("javacOptions", v.javacOptions) :*:
("relativeOutputPath", v.relativeOutputPath) :*:
LNil
},
{
(in: Vector[HashedVirtualFileRef] :*: Vector[HashedVirtualFileRef] :*: Vector[String] :*:
Vector[String] :*: LNil) =>
CompileInputs2(in.head, in.tail.head, in.tail.tail.head, in.tail.tail.tail.head)
Vector[String] :*: String :*: LNil) =>
CompileInputs2(
in.head,
in.tail.head,
in.tail.tail.head,
in.tail.tail.tail.head,
in.tail.tail.tail.tail.head
)
}
)
end CompileInputs2

View File

@ -1,4 +1,5 @@
scalaVersion := "2.12.19"
name := "root"
lazy val core = project
.settings(

View File

@ -1,19 +1,19 @@
> ++3.0.2 compile
> ++3.0.2 packageBin
$ exists target/out/jvm/scala-3.0.2/core/core_3-0.1.0-SNAPSHOT-noresources.jar
-$ exists target/out/jvm/scala-3.1.2/core/core_3-0.1.0-SNAPSHOT-noresources.jar
-$ exists target/out/jvm/scala-3.0.2/subproj/subproj_3-0.1.0-SNAPSHOT-noresources.jar
-$ exists target/out/jvm/scala-3.1.2/subproj/subproj_3-0.1.0-SNAPSHOT-noresources.jar
$ exists target/out/jvm/scala-3.0.2/core/core_3-0.1.0-SNAPSHOT.jar
-$ exists target/out/jvm/scala-3.1.2/core/core_3-0.1.0-SNAPSHOT.jar
-$ exists target/out/jvm/scala-3.0.2/subproj/subproj_3-0.1.0-SNAPSHOT.jar
-$ exists target/out/jvm/scala-3.1.2/subproj/subproj_3-0.1.0-SNAPSHOT.jar
> clean
-$ exists target/out/jvm/scala-3.0.2/core/core_3-0.1.0-SNAPSHOT-noresources.jar
-$ exists target/out/jvm/scala-3.1.2/core/core_3-0.1.0-SNAPSHOT-noresources.jar
-$ exists target/out/jvm/scala-3.0.2/subproj/subproj_3-0.1.0-SNAPSHOT-noresources.jar
-$ exists target/out/jvm/scala-3.1.2/subproj/subproj_3-0.1.0-SNAPSHOT-noresources.jar
-$ exists target/out/jvm/scala-3.0.2/core/core_3-0.1.0-SNAPSHOT.jar
-$ exists target/out/jvm/scala-3.1.2/core/core_3-0.1.0-SNAPSHOT.jar
-$ exists target/out/jvm/scala-3.0.2/subproj/subproj_3-0.1.0-SNAPSHOT.jar
-$ exists target/out/jvm/scala-3.1.2/subproj/subproj_3-0.1.0-SNAPSHOT.jar
> ++3.1.2 compile
> ++3.1.2 packageBin
-$ exists target/out/jvm/scala-3.0.2/core/core_3-0.1.0-SNAPSHOT-noresources.jar
$ exists target/out/jvm/scala-3.1.2/core/core_3-0.1.0-SNAPSHOT-noresources.jar
-$ exists target/out/jvm/scala-3.0.2/subproj/subproj_3-0.1.0-SNAPSHOT-noresources.jar
$ exists target/out/jvm/scala-3.1.2/subproj/subproj_3-0.1.0-SNAPSHOT-noresources.jar
-$ exists target/out/jvm/scala-3.0.2/core/core_3-0.1.0-SNAPSHOT.jar
$ exists target/out/jvm/scala-3.1.2/core/core_3-0.1.0-SNAPSHOT.jar
-$ exists target/out/jvm/scala-3.0.2/subproj/subproj_3-0.1.0-SNAPSHOT.jar
$ exists target/out/jvm/scala-3.1.2/subproj/subproj_3-0.1.0-SNAPSHOT.jar