forked from scalacenter/bloop
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCompilerCache.scala
405 lines (367 loc) · 14.9 KB
/
CompilerCache.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package bloop
import java.io.File
import java.io.IOException
import java.io.PrintWriter
import java.lang.Iterable
import java.util.concurrent.ConcurrentHashMap
import javax.tools.FileObject
import javax.tools.ForwardingJavaFileManager
import javax.tools.JavaFileManager
import javax.tools.JavaFileManager.Location
import javax.tools.JavaFileObject
import javax.tools.JavaFileObject.Kind
import javax.tools.{JavaCompiler => JavaxCompiler}
import scala.collection.mutable.HashSet
import bloop.CompilerCache.JavacKey
import bloop.io.AbsolutePath
import bloop.io.Paths
import bloop.logging.Logger
import bloop.util.JavaRuntime
import sbt.internal.inc.AnalyzingCompiler
import sbt.internal.inc.BloopComponentCompiler
import sbt.internal.inc.BloopZincLibraryManagement
import sbt.internal.inc.CompilerArguments
import sbt.internal.inc.PlainVirtualFileConverter
import sbt.internal.inc.ZincUtil
import sbt.internal.inc.javac.DiagnosticsReporter
import sbt.internal.inc.javac.JavaTools
import sbt.internal.inc.javac.Javadoc
import sbt.internal.inc.javac.WriteReportingJavaFileObject
import sbt.internal.util.LoggerWriter
import xsbti.ComponentProvider
import xsbti.VirtualFile
import xsbti.compile.ClassFileManager
import xsbti.compile.ClasspathOptions
import xsbti.compile.Compilers
import xsbti.compile.JavaCompiler
import xsbti.compile.Output
import xsbti.{Logger => XLogger}
import xsbti.{Reporter => XReporter}
object CompilerCache {
final case class JavacKey(javacBin: Option[AbsolutePath], allowLocal: Boolean)
}
final class CompilerCache(
componentProvider: ComponentProvider,
logger: Logger
) {
private val scalaInstanceCache = new ConcurrentHashMap[ScalaInstance, ScalaInstance]()
private val javaCompilerCache = new ConcurrentHashMap[JavacKey, JavaCompiler]()
def get(
scalaInstance: ScalaInstance,
classpathOptions: ClasspathOptions,
javacBin: Option[AbsolutePath],
javacOptions: List[String]
): Compilers = {
val scalaInstanceFromCache = scalaInstanceCache.computeIfAbsent(
scalaInstance,
_ => scalaInstance
)
val scalaCompiler =
getScalaCompiler(scalaInstanceFromCache, classpathOptions, componentProvider)
val allowLocal = !hasRuntimeJavacOptions(javacOptions)
val javaCompiler =
javaCompilerCache.computeIfAbsent(
JavacKey(javacBin, allowLocal),
key => getJavaCompiler(logger, key.javacBin, javacOptions)
)
val javaDoc = Javadoc.local.getOrElse(Javadoc.fork())
val javaTools = JavaTools(javaCompiler, javaDoc)
ZincUtil.compilers(javaTools, scalaCompiler)
}
private[bloop] def withLogger(logger: Logger): CompilerCache = {
new CompilerCache(
componentProvider,
logger
)
}
def getJavaCompiler(
logger: Logger,
javacBin: Option[AbsolutePath],
javacOptions: List[String]
): JavaCompiler = {
val allowLocal = !hasRuntimeJavacOptions(javacOptions)
javacBin match {
case Some(bin) if JavaRuntime.javac.exists(isSameCompiler(logger, _, bin)) =>
// Same bin as the one derived from this VM? Prefer built-in compiler if JDK
JavaRuntime.javaCompiler match {
case Some(compiler) if allowLocal =>
new BloopMaybeForkedJavaCompiler(compiler, Some(bin.toFile))
case _ => new BloopForkedJavaCompiler(Some(bin.toFile))
}
case Some(bin) => new BloopForkedJavaCompiler(Some(bin.toFile))
case None =>
JavaRuntime.javaCompiler match {
case Some(compiler) if allowLocal => new BloopMaybeForkedJavaCompiler(compiler, None)
case _ => new BloopForkedJavaCompiler(None)
}
}
}
def getScalaCompiler(
scalaInstance: ScalaInstance,
classpathOptions: ClasspathOptions,
componentProvider: ComponentProvider
): AnalyzingCompiler = {
val bridgeSources = BloopComponentCompiler.getModuleForBridgeSources(scalaInstance)
val bridgeId = BloopComponentCompiler.getBridgeComponentId(bridgeSources, scalaInstance)
componentProvider.component(bridgeId) match {
case Array(jar) => ZincUtil.scalaCompiler(scalaInstance, jar, classpathOptions)
case _ =>
BloopZincLibraryManagement.scalaCompiler(
scalaInstance,
BloopComponentsLock,
componentProvider,
Some(Paths.getCacheDirectory("bridge-cache").toFile),
bridgeSources,
classpathOptions,
logger
)
}
}
/**
* A Java compiler that will invalidate class files from a forked java
* compiler by adding a new classes directory to the beginning of the
* `-classpath` compiler option where we will write an empty class file for
* every invalidated class file. This empty class file will force the javac
* compiler to output an error similar to:
*
* ```
* /path/to/File.java:1
* error: cannot access Bar
*
* bad class file: ./B.class
* class file contains wrong class: java.lang.Object
* Please remove or make sure it appears in the correct subdirectory of the classpath.
* ```
*
* Fortunately, the last bit is not parsed by Zinc's `JavacParser` and
* doesn't show up as a reported Zinc problem. The first bit does show up and
* it's a good enough error to intuit that the symbol might not exist, so we
* leave it there. We cannot replace that error with a custom one because it
* would be confusing if the accessibility error is real and not caused by
* our invalidation trick.
*/
final class BloopForkedJavaCompiler(javaHome: Option[File]) extends JavaCompiler {
import xsbti.compile.IncToolOptions
private val converter = PlainVirtualFileConverter.converter
def run(
sources: Array[VirtualFile],
options: Array[String],
output: Output,
topts: IncToolOptions,
reporter: XReporter,
log: XLogger
): Boolean = {
val classpathIndex = options.indexOf("-classpath")
if (classpathIndex == -1) {
logger.error("Missing classpath option for forked Java compiler")
false
} else {
import sbt.util.InterfaceUtil
InterfaceUtil.toOption(topts.classFileManager()) match {
case None => logger.error("Missing class file manager for forked Java compiler"); false
case Some(classFileManager: BloopClassFileManager) =>
import java.nio.file.Files
val newInvalidatedEntry = AbsolutePath(
Files.createTempDirectory("invalidated-forked-javac")
)
val invalidatedPaths =
classFileManager.invalidatedClassFiles().map(_.getAbsolutePath())
val classpathValueIndex = classpathIndex + 1
val classpathValue = options.apply(classpathValueIndex)
val classpathEntries = classpathValue.split(File.pathSeparatorChar)
invalidatedPaths.foreach { invalidatedPath =>
val relativePath = classpathEntries.collectFirst {
case classpathEntry if invalidatedPath.startsWith(classpathEntry) =>
invalidatedPath.stripPrefix(classpathEntry)
}
relativePath.foreach { relative =>
val relativeParts = relative.split(File.separatorChar)
val invalidatedClassFile = relativeParts.foldLeft(newInvalidatedEntry) {
case (path, relativePart) => path.resolve(relativePart)
}
val invalidatedClassFilePath = invalidatedClassFile.underlying
Files.createDirectories(invalidatedClassFilePath.getParent)
Files.write(invalidatedClassFilePath, "".getBytes())
}
}
val newClasspathValue = newInvalidatedEntry.toFile + File.pathSeparator + classpathValue
options(classpathValueIndex) = newClasspathValue
output.getSingleOutputAsPath.ifPresent { p =>
java.nio.file.Files.createDirectories(p)
()
}
val outputOption = CompilerArguments.outputOption(output)
try {
import sbt.internal.inc.javac.BloopForkedJavaUtils
BloopForkedJavaUtils.launch(
javaHome,
"javac",
sources.map(converter.toPath(_)),
options ++ outputOption,
log,
reporter
)
} finally {
Paths.delete(newInvalidatedEntry)
}
case _ => logger.error("Missing Bloop class file manager for forked Java compiler"); false
}
}
}
}
/**
* A Java compiler that will invalidate class files using the local JDK Java
* compiler API. The invalidated class files are invalidated programmatically
* because they cannot be deleted from the classes directories because they
* are read-only.
*/
final class BloopJavaCompiler(compiler: JavaxCompiler) extends JavaCompiler {
import java.io.File
import xsbti.compile.IncToolOptions
import xsbti.Reporter
private val converter = PlainVirtualFileConverter.converter
override def run(
sources: Array[VirtualFile],
options: Array[String],
output: Output,
incToolOptions: IncToolOptions,
reporter: Reporter,
log0: xsbti.Logger
): Boolean = {
val log: sbt.util.Logger = log0
import collection.JavaConverters._
val logger = new LoggerWriter(log)
val logWriter = new PrintWriter(logger)
log.debug("Attempting to call " + compiler + " directly...")
val diagnostics = new DiagnosticsReporter(reporter)
val fileManager0 = compiler.getStandardFileManager(diagnostics, null, null)
/* Local Java compiler doesn't accept `-J<flag>` options, strip them. */
val (invalidOptions, cleanedOptions) = options partition (_ startsWith "-J")
if (invalidOptions.nonEmpty) {
log.warn("Javac is running in 'local' mode. These flags have been removed:")
log.warn(invalidOptions.mkString("\t", ", ", ""))
}
var compileSuccess = false
val zincFileManager = incToolOptions.classFileManager().get()
val fileManager = new BloopInvalidatingFileManager(fileManager0, zincFileManager)
val sourceFiles: Array[File] = sources.map(converter.toPath(_).toFile())
val jfiles = fileManager0.getJavaFileObjectsFromFiles(sourceFiles.toList.asJava)
try {
// Create directories of java args that trigger error if they don't exist
def processJavaDirArgument(idx: Int): Unit = {
if (idx == -1) ()
else {
try {
val dir = AbsolutePath(cleanedOptions(idx + 1))
if (dir.exists) ()
else java.nio.file.Files.createDirectories(dir.underlying)
()
} catch {
case _: IOException => () // Ignore any error parsing path
}
}
}
processJavaDirArgument(cleanedOptions.indexOf("-d"))
processJavaDirArgument(cleanedOptions.indexOf("-s"))
processJavaDirArgument(cleanedOptions.indexOf("-h"))
output.getSingleOutputAsPath.ifPresent { p =>
java.nio.file.Files.createDirectories(p)
()
}
val outputOption = CompilerArguments.outputOption(output)
val newJavacOptions = (cleanedOptions.toList ++ outputOption).asJava
log.debug(s"Invoking javac with ${newJavacOptions.asScala.mkString(" ")}")
val success = compiler
.getTask(logWriter, fileManager, diagnostics, newJavacOptions, null, jfiles)
.call()
/* Double check success variables for the Java compiler.
* The local compiler may report successful compilations even though
* there have been errors (e.g. encoding problems in sources). To stick
* to javac's behaviour, we report fail compilation from diagnostics. */
compileSuccess = success && !diagnostics.hasErrors
} finally {
import sbt.util.Level
logger.flushLines(if (compileSuccess) Level.Warn else Level.Error)
}
compileSuccess
}
final class BloopInvalidatingFileManager(
fileManager: JavaFileManager,
zincManager: ClassFileManager
) extends ForwardingJavaFileManager[JavaFileManager](fileManager) {
import scala.collection.JavaConverters._
override def getJavaFileForOutput(
location: Location,
className: String,
kind: Kind,
sibling: FileObject
): JavaFileObject = {
val output = super.getJavaFileForOutput(location, className, kind, sibling)
import sbt.internal.inc.javac.WriteReportingJavaFileObject
new WriteReportingJavaFileObject(output, zincManager)
}
import java.{util => ju}
override def list(
location: Location,
packageName: String,
kinds: ju.Set[Kind],
recurse: Boolean
): Iterable[JavaFileObject] = {
val invalidated = {
zincManager match {
case m: bloop.BloopClassFileManager => m.invalidatedClassFilesSet
// Bloop uses it's own classfile manager so this should not happen
case _ =>
logger.warn("Could not find BloopClassfileManager that is needed for invaldiation.")
new HashSet[File]()
}
}
val ls = super.list(location, packageName, kinds, recurse)
ls.asScala.filter(o => !invalidated.contains(new File(o.getName))).asJava
}
// Needed because JavaFileManager doesn't expect WriteReportingJavaFileObjects in isSameFile, fixes #956
override def isSameFile(a: FileObject, b: FileObject): Boolean = {
def unwrap(fo: FileObject): FileObject = fo match {
case wrfo: WriteReportingJavaFileObject => wrfo.javaFileObject
case other => other
}
super.isSameFile(unwrap(a), unwrap(b))
}
}
}
final class BloopMaybeForkedJavaCompiler(compiler: JavaxCompiler, javaHome: Option[File])
extends JavaCompiler {
import xsbti.compile.IncToolOptions
override def run(
sources: Array[VirtualFile],
options: Array[String],
output: Output,
incToolOptions: IncToolOptions,
reporter: xsbti.Reporter,
log0: xsbti.Logger
): Boolean = {
val hasJavaOpts = options.exists(_.startsWith("-J"))
val compiler0 =
if (hasJavaOpts) new BloopForkedJavaCompiler(javaHome)
else new BloopJavaCompiler(compiler)
compiler0.run(sources, options, output, incToolOptions, reporter, log0)
}
}
/**
* Equivalent to `path1.isSameFile(path2)`, but will log and return false if an exception is thrown.
*/
private def isSameCompiler(logger: Logger, path1: AbsolutePath, path2: AbsolutePath): Boolean = {
try path1.isSameFile(path2)
catch {
case ex: IOException =>
logger.warn(
s"Couldn't compare `$path1` and `$path2`; assuming they're different Java compilers."
)
logger.trace(ex)
false
}
}
private def hasRuntimeJavacOptions(javacOptions: List[String]): Boolean = {
javacOptions.exists(_.startsWith("-J"))
}
}