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
|
package mill.scalalib.worker
import mill.scalalib.worker._
import java.io.File
import java.util.Optional
import mill.api.Loose.Agg
import mill.api.PathRef
import xsbti.compile.{CompilerCache => _, FileAnalysisStore => _, ScalaInstance => _, _}
import mill.scalalib.api.Util.{isDotty, grepJar, scalaBinaryVersion}
import sbt.internal.inc._
import sbt.internal.util.{ConsoleOut, MainAppender}
import sbt.util.LogExchange
import mill.scalalib.api.CompilationResult
case class MockedLookup(am: File => Optional[CompileAnalysis]) extends PerClasspathEntryLookup {
override def analysis(classpathEntry: File): Optional[CompileAnalysis] =
am(classpathEntry)
override def definesClass(classpathEntry: File): DefinesClass =
Locate.definesClass(classpathEntry)
}
trait KeyedCache[T]{
def withCachedValue[V](key: Long)(f: => T)(f2: T => V): V
}
object KeyedCache{
class RandomBoundedCache[T](hotParallelism: Int, coldCacheSize: Int) extends KeyedCache[T]{
private[this] val random = new scala.util.Random(313373)
val available = new java.util.concurrent.Semaphore(hotParallelism)
// Awful asymptotic complexity, but our caches are tiny n < 10 so it doesn't matter
var cache = Array.fill[Option[(Long, T)]](coldCacheSize)(None)
def withCachedValue[V](key: Long)(f: => T)(f2: T => V): V = {
available.acquire()
val pickedValue = synchronized{
cache.indexWhere(_.exists(_._1 == key)) match {
case -1 => f
case i =>
val (k, v) = cache(i).get
cache(i) = None
v
}
}
val result = f2(pickedValue)
synchronized{
cache.indexWhere(_.isEmpty) match{
// Random eviction #YOLO
case -1 => cache(random.nextInt(cache.length)) = Some((key, pickedValue))
case i => cache(i) = Some((key, pickedValue))
}
}
available.release()
result
}
}
}
object ZincWorkerImpl{
type Ctx = mill.api.Ctx.Dest with mill.api.Ctx.Log with mill.api.Ctx.Home
}
class ZincWorkerImpl(compilerBridge: Either[(ZincWorkerImpl.Ctx, Array[os.Path], String => os.Path), String => os.Path],
libraryJarNameGrep: (Agg[os.Path], String) => os.Path = grepJar(_, "scala-library", _),
compilerJarNameGrep: (Agg[os.Path], String) => os.Path = grepJar(_, "scala-compiler", _),
compilerCache: KeyedCache[Compilers] = new KeyedCache.RandomBoundedCache(1, 1)) {
private val ic = new sbt.internal.inc.IncrementalCompilerImpl()
lazy val javaOnlyCompilers = {
// Keep the classpath as written by the user
val classpathOptions = ClasspathOptions.of(false, false, false, false, false)
val dummyFile = new java.io.File("")
// Zinc does not have an entry point for Java-only compilation, so we need
// to make up a dummy ScalaCompiler instance.
val scalac = ZincUtil.scalaCompiler(
new ScalaInstance("", null, null, dummyFile, dummyFile, new Array(0), Some("")), null,
classpathOptions // this is used for javac too
)
ic.compilers(
instance = null,
classpathOptions,
None,
scalac
)
}
def docJar(scalaVersion: String,
compilerClasspath: Agg[os.Path],
scalacPluginClasspath: Agg[os.Path],
args: Seq[String])
(implicit ctx: ZincWorkerImpl.Ctx): Boolean = {
withCompilers(
scalaVersion,
compilerClasspath,
scalacPluginClasspath,
) { compilers: Compilers =>
val scaladocClass = compilers.scalac().scalaInstance().loader().loadClass("scala.tools.nsc.ScalaDoc")
val scaladocMethod = scaladocClass.getMethod("process", classOf[Array[String]])
scaladocMethod.invoke(scaladocClass.newInstance(), args.toArray).asInstanceOf[Boolean]
}
}
/** Compile the bridge if it doesn't exist yet and return the output directory.
* TODO: Proper invalidation, see #389
*/
def compileZincBridgeIfNeeded(scalaVersion: String, compilerJars: Array[File]): os.Path = {
compilerBridge match{
case Right(compiled) => compiled(scalaVersion)
case Left((ctx0, compilerBridgeClasspath, srcJars)) =>
val workingDir = ctx0.dest / scalaVersion
val compiledDest = workingDir / 'compiled
if (!os.exists(workingDir)) {
ctx0.log.info("Compiling compiler interface...")
os.makeDir.all(workingDir)
os.makeDir.all(compiledDest)
val sourceFolder = mill.api.IO.unpackZip(srcJars(scalaVersion))(workingDir)
val classloader = mill.api.ClassLoader.create(compilerJars.map(_.toURI.toURL), null)(ctx0)
val compilerMain = classloader.loadClass(
if (isDotty(scalaVersion)) "dotty.tools.dotc.Main"
else "scala.tools.nsc.Main"
)
val argsArray = Array[String](
"-d", compiledDest.toString,
"-Ylog-classpath",
"-classpath", (compilerJars ++ compilerBridgeClasspath).mkString(File.pathSeparator)
) ++ os.walk(sourceFolder.path).filter(_.ext == "scala").map(_.toString)
compilerMain.getMethod("process", classOf[Array[String]])
.invoke(null, argsArray)
}
compiledDest
}
}
def discoverMainClasses(compilationResult: CompilationResult)
(implicit ctx: ZincWorkerImpl.Ctx): Seq[String] = {
def toScala[A](o: Optional[A]): Option[A] = if (o.isPresent) Some(o.get) else None
toScala(FileAnalysisStore.binary(compilationResult.analysisFile.toIO).get())
.map(_.getAnalysis)
.flatMap{
case analysis: Analysis =>
Some(analysis.infos.allInfos.values.map(_.getMainClasses).flatten.toSeq.sorted)
case _ =>
None
}
.getOrElse(Seq.empty[String])
}
def compileJava(upstreamCompileOutput: Seq[CompilationResult],
sources: Agg[os.Path],
compileClasspath: Agg[os.Path],
javacOptions: Seq[String])
(implicit ctx: ZincWorkerImpl.Ctx): mill.api.Result[CompilationResult] = {
compileInternal(
upstreamCompileOutput,
sources,
compileClasspath,
javacOptions,
scalacOptions = Nil,
javaOnlyCompilers
)
}
def compileMixed(upstreamCompileOutput: Seq[CompilationResult],
sources: Agg[os.Path],
compileClasspath: Agg[os.Path],
javacOptions: Seq[String],
scalaVersion: String,
scalacOptions: Seq[String],
compilerClasspath: Agg[os.Path],
scalacPluginClasspath: Agg[os.Path])
(implicit ctx: ZincWorkerImpl.Ctx): mill.api.Result[CompilationResult] = {
withCompilers(
scalaVersion,
compilerClasspath,
scalacPluginClasspath,
) {compilers: Compilers =>
compileInternal(
upstreamCompileOutput,
sources,
compileClasspath,
javacOptions,
scalacOptions = scalacPluginClasspath.map(jar => s"-Xplugin:$jar").toSeq ++ scalacOptions,
compilers
)
}
}
private def withCompilers[T](scalaVersion: String,
compilerClasspath: Agg[os.Path],
scalacPluginClasspath: Agg[os.Path])
(f: Compilers => T)
(implicit ctx: ZincWorkerImpl.Ctx)= {
val combinedCompilerClasspath = compilerClasspath ++ scalacPluginClasspath
val combinedCompilerJars = combinedCompilerClasspath.toArray.map(_.toIO)
val compiledCompilerBridge =
compileZincBridgeIfNeeded(scalaVersion, compilerClasspath.toArray.map(_.toIO))
val compilerBridgeSig = os.mtime(compiledCompilerBridge)
val compilersSig =
compilerBridgeSig +
combinedCompilerClasspath.map(p => p.toString().hashCode + os.mtime(p)).sum
compilerCache.withCachedValue(compilersSig){
val compilerJar =
if (isDotty(scalaVersion))
grepJar(compilerClasspath, s"dotty-compiler_${scalaBinaryVersion(scalaVersion)}", scalaVersion)
else
compilerJarNameGrep(compilerClasspath, scalaVersion)
val scalaInstance = new ScalaInstance(
version = scalaVersion,
loader = mill.api.ClassLoader.create(combinedCompilerJars.map(_.toURI.toURL), null),
libraryJar = libraryJarNameGrep(compilerClasspath, scalaVersion).toIO,
compilerJar = compilerJar.toIO,
allJars = combinedCompilerJars,
explicitActual = None
)
ic.compilers(
scalaInstance,
ClasspathOptionsUtil.boot,
None,
ZincUtil.scalaCompiler(scalaInstance, compiledCompilerBridge.toIO)
)
}(f)
}
private def compileInternal(upstreamCompileOutput: Seq[CompilationResult],
sources: Agg[os.Path],
compileClasspath: Agg[os.Path],
javacOptions: Seq[String],
scalacOptions: Seq[String],
compilers: Compilers)
(implicit ctx: ZincWorkerImpl.Ctx): mill.api.Result[CompilationResult] = {
os.makeDir.all(ctx.dest)
println("compileClasspath " + compileClasspath)
val logger = {
val consoleAppender = MainAppender.defaultScreen(ConsoleOut.printStreamOut(
ctx.log.outputStream
))
val l = LogExchange.logger("Hello")
LogExchange.unbindLoggerAppenders("Hello")
LogExchange.bindLoggerAppenders("Hello", (consoleAppender -> sbt.util.Level.Info) :: Nil)
l
}
def analysisMap(f: File): Optional[CompileAnalysis] = {
if (f.isFile) {
Optional.empty[CompileAnalysis]
} else {
upstreamCompileOutput.collectFirst {
case CompilationResult(zincPath, classFiles) if classFiles.path.toNIO == f.toPath =>
FileAnalysisStore.binary(zincPath.toIO).get().map[CompileAnalysis](_.getAnalysis)
}.getOrElse(Optional.empty[CompileAnalysis])
}
}
val lookup = MockedLookup(analysisMap)
val zincFile = ctx.dest / 'zinc
val classesDir = ctx.dest / 'classes
val zincIOFile = zincFile.toIO
val classesIODir = classesDir.toIO
val store = FileAnalysisStore.binary(zincIOFile)
val inputs = ic.inputs(
classpath = classesIODir +: compileClasspath.map(_.toIO).toArray,
sources = sources.toArray.map(_.toIO),
classesDirectory = classesIODir,
scalacOptions = scalacOptions.toArray,
javacOptions = javacOptions.toArray,
maxErrors = 10,
sourcePositionMappers = Array(),
order = CompileOrder.Mixed,
compilers = compilers,
setup = ic.setup(
lookup,
skip = false,
zincIOFile,
new FreshCompilerCache,
IncOptions.of(),
new ManagedLoggedReporter(10, logger),
None,
Array()
),
pr = {
val prev = store.get()
PreviousResult.of(prev.map(_.getAnalysis), prev.map(_.getMiniSetup))
}
)
try {
val newResult = ic.compile(
in = inputs,
logger = logger
)
store.set(
AnalysisContents.create(
newResult.analysis(),
newResult.setup()
)
)
mill.api.Result.Success(CompilationResult(zincFile, PathRef(classesDir)))
}catch{case e: CompileFailed => mill.api.Result.Failure(e.toString)}
}
}
|