2020-06-08 14:56:03 +03:00
|
|
|
import sbt.Keys._
|
|
|
|
import sbt._
|
|
|
|
|
|
|
|
object FixInstrumentsGeneration {
|
|
|
|
|
2020-10-22 17:12:28 +03:00
|
|
|
/** This task detects any changes in source files of Instruments and forces
|
2020-06-08 14:56:03 +03:00
|
|
|
* recompilation of all instruments on any change. This is to ensure that the
|
|
|
|
* Annotation Processor registers all of the instruments.
|
|
|
|
*
|
|
|
|
* Without that fix, incremental compilation would not register unchanged
|
|
|
|
* instruments, leading to runtime errors.
|
2020-06-10 11:37:12 +03:00
|
|
|
*
|
2020-07-01 14:21:13 +03:00
|
|
|
* It should be added as a dependency of Compile / compile / compileInputs.
|
2020-06-08 14:56:03 +03:00
|
|
|
*/
|
2020-06-10 11:37:12 +03:00
|
|
|
lazy val preCompileTask = Def.task {
|
2020-07-01 14:21:13 +03:00
|
|
|
val log = streams.value.log
|
2020-06-10 11:37:12 +03:00
|
|
|
val root = baseDirectory.value
|
2020-06-08 14:56:03 +03:00
|
|
|
val classFilesDirectory = (Compile / classDirectory).value
|
2020-06-10 11:37:12 +03:00
|
|
|
val FragileFiles(fragileSources, fragileClassFiles) =
|
|
|
|
getFragileFiles(root, classFilesDirectory)
|
2020-06-08 14:56:03 +03:00
|
|
|
|
2020-06-10 11:37:12 +03:00
|
|
|
val fragileSourcesStore =
|
2020-06-08 14:56:03 +03:00
|
|
|
streams.value.cacheStoreFactory.make("instruments_fixer")
|
|
|
|
|
2020-07-01 14:21:13 +03:00
|
|
|
Tracked.diffInputs(fragileSourcesStore, FileInfo.hash)(
|
|
|
|
fragileSources.toSet
|
|
|
|
) { sourcesDiff: ChangeReport[File] =>
|
|
|
|
if (sourcesDiff.modified.nonEmpty && sourcesDiff.unmodified.nonEmpty) {
|
|
|
|
val others =
|
|
|
|
if (sourcesDiff.modified.size >= 2)
|
|
|
|
s" and ${sourcesDiff.modified.size - 1} others"
|
|
|
|
else ""
|
|
|
|
val firstInstrument = sourcesDiff.modified.head
|
|
|
|
val sourcesMessage = firstInstrument.toString + others
|
|
|
|
log.warn(
|
|
|
|
s"Instruments sources ($sourcesMessage) have been changed.\n" +
|
|
|
|
s"Forcing recompilation of all instruments to maintain " +
|
|
|
|
s"consistency of generated services files."
|
|
|
|
)
|
2020-06-08 14:56:03 +03:00
|
|
|
|
2020-07-01 14:21:13 +03:00
|
|
|
fragileClassFiles.foreach(_.delete())
|
|
|
|
}
|
2020-06-08 14:56:03 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-10 11:37:12 +03:00
|
|
|
|
2020-10-22 17:12:28 +03:00
|
|
|
/** This task detects if just a subset of the Instruments has been recompiled
|
2020-06-10 11:37:12 +03:00
|
|
|
* (right now we did not find a way of detecting this before compilation). If
|
|
|
|
* the Instrumentation state is detected to be inconsistent, current
|
|
|
|
* compilation is aborted and classfiles are deleted to ensure that when
|
|
|
|
* re-run Instrumentation will be brought back to a consistent state.
|
|
|
|
*
|
|
|
|
* Without that fix, incremental compilation would not register unchanged
|
|
|
|
* instruments, leading to runtime errors.
|
|
|
|
*
|
|
|
|
* It should replace the default `Compile / compile` task in a project.
|
|
|
|
*/
|
|
|
|
lazy val patchedCompile = Def.task {
|
|
|
|
val compilationResult = (Compile / compile).value
|
|
|
|
|
2020-07-01 14:21:13 +03:00
|
|
|
val log = streams.value.log
|
2020-06-10 11:37:12 +03:00
|
|
|
val root = baseDirectory.value
|
|
|
|
val classFilesDirectory = (Compile / classDirectory).value
|
|
|
|
val FragileFiles(_, fragileClassFiles) =
|
|
|
|
getFragileFiles(root, classFilesDirectory)
|
|
|
|
|
|
|
|
val fragileClassFilesStore =
|
|
|
|
streams.value.cacheStoreFactory.make("instruments_classfiles")
|
|
|
|
|
|
|
|
Tracked.diffInputs(fragileClassFilesStore, FileInfo.lastModified)(
|
|
|
|
fragileClassFiles.toSet
|
|
|
|
) { sourcesDiff: ChangeReport[File] =>
|
|
|
|
if (sourcesDiff.modified.nonEmpty && sourcesDiff.unmodified.nonEmpty) {
|
|
|
|
fragileClassFiles.foreach(_.delete())
|
|
|
|
|
|
|
|
val projectName = name.value
|
2020-07-01 14:21:13 +03:00
|
|
|
log.error(
|
2020-06-10 11:37:12 +03:00
|
|
|
"Truffle Instrumentation is not up to date, " +
|
|
|
|
"which will lead to runtime errors\n" +
|
|
|
|
"Fixes have been applied to ensure consistent Instrumentation state, " +
|
|
|
|
"but compilation has to be triggered again.\n" +
|
|
|
|
"Please re-run the previous command.\n" +
|
|
|
|
"(If this for some reason fails, " +
|
|
|
|
s"please do a clean build of the $projectName project)"
|
|
|
|
)
|
|
|
|
|
|
|
|
throw new RuntimeException("Please re-run last command")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
compilationResult
|
|
|
|
}
|
|
|
|
|
2021-02-22 16:32:55 +03:00
|
|
|
/** Deletes the compiled instrumentation class files, forcing all of them to
|
|
|
|
* be recompiled.
|
|
|
|
*
|
|
|
|
* Since all instruments are recompiled at once, the service state should be
|
|
|
|
* consistent as all of them will be re-registered.
|
|
|
|
*/
|
|
|
|
def cleanInstruments = Def.task {
|
|
|
|
val log = streams.value.log
|
|
|
|
val root = baseDirectory.value
|
|
|
|
val classFilesDirectory = (Compile / classDirectory).value
|
|
|
|
val FragileFiles(_, fragileClassFiles) =
|
|
|
|
getFragileFiles(root, classFilesDirectory)
|
|
|
|
fragileClassFiles.foreach { file =>
|
|
|
|
if (file.exists()) {
|
|
|
|
log.info(s"[clean-instruments] Removing $file")
|
|
|
|
file.delete()
|
|
|
|
} else {
|
|
|
|
log.info(s"[clean-instruments] $file was already missing")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.info(
|
|
|
|
"All fragile class files have been deleted. The next compilation " +
|
|
|
|
"should be forced to recompile all of them, preserving instrumentation " +
|
|
|
|
"configuration consistency."
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-10 11:37:12 +03:00
|
|
|
private case class FragileFiles(sources: Seq[File], classFiles: Seq[File])
|
|
|
|
|
|
|
|
private def getFragileFiles(
|
|
|
|
root: File,
|
|
|
|
classFilesDirectory: File
|
|
|
|
): FragileFiles = {
|
|
|
|
val fragileSources =
|
|
|
|
(file(s"$root/src/main/java/") ** "*Instrument.java").get ++
|
2021-02-08 20:14:54 +03:00
|
|
|
Seq(
|
|
|
|
file(s"$root/src/main/java/org/enso/interpreter/Language.java"),
|
|
|
|
file(s"$root/src/main/java/org/enso/interpreter/epb/EpbLanguage.java")
|
|
|
|
)
|
2020-06-10 11:37:12 +03:00
|
|
|
val fragileClassFiles =
|
|
|
|
(classFilesDirectory ** "*Instrument.class").get ++
|
2021-02-08 20:14:54 +03:00
|
|
|
Seq(
|
|
|
|
file(s"$classFilesDirectory/org/enso/interpreter/Language.class"),
|
|
|
|
file(s"$classFilesDirectory/org/enso/interpreter/epb/EpbLanguage.class")
|
|
|
|
)
|
2020-06-10 11:37:12 +03:00
|
|
|
FragileFiles(fragileSources, fragileClassFiles)
|
|
|
|
}
|
2020-06-08 14:56:03 +03:00
|
|
|
}
|