/
SemanticDbJavaModule.scala
242 lines (215 loc) · 8.41 KB
/
SemanticDbJavaModule.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
package mill.scalalib
import mill.api.{PathRef, Result, experimental}
import mill.define.{ModuleRef, Target}
import mill.main.BuildInfo
import mill.scalalib.api.{CompilationResult, Versions, ZincWorkerUtil}
import mill.scalalib.bsp.BspBuildTarget
import mill.util.Version
import mill.{Agg, T}
import scala.util.Properties
@experimental
trait SemanticDbJavaModule extends CoursierModule {
def zincWorker: ModuleRef[ZincWorkerModule]
def upstreamCompileOutput: T[Seq[CompilationResult]]
def zincReportCachedProblems: T[Boolean]
def zincIncrementalCompilation: T[Boolean]
def allSourceFiles: T[Seq[PathRef]]
def compile: T[mill.scalalib.api.CompilationResult]
def bspBuildTarget: BspBuildTarget
def javacOptions: T[Seq[String]]
def compileClasspath: T[Agg[PathRef]]
def semanticDbVersion: T[String] = T.input {
val builtin = SemanticDbJavaModule.buildTimeSemanticDbVersion
val requested = T.env.getOrElse[String](
"SEMANTICDB_VERSION",
SemanticDbJavaModule.contextSemanticDbVersion.get().getOrElse(builtin)
)
Version.chooseNewest(requested, builtin)(Version.IgnoreQualifierOrdering)
}
def semanticDbJavaVersion: T[String] = T.input {
val builtin = SemanticDbJavaModule.buildTimeJavaSemanticDbVersion
val requested = T.env.getOrElse[String](
"JAVASEMANTICDB_VERSION",
SemanticDbJavaModule.contextJavaSemanticDbVersion.get().getOrElse(builtin)
)
Version.chooseNewest(requested, builtin)(Version.IgnoreQualifierOrdering)
}
def semanticDbScalaVersion: T[String] = BuildInfo.scalaVersion
protected def semanticDbPluginIvyDeps: T[Agg[Dep]] = T {
val sv = semanticDbScalaVersion()
val semDbVersion = semanticDbVersion()
if (!ZincWorkerUtil.isScala3(sv) && semDbVersion.isEmpty) {
val msg =
"""|
|With Scala 2 you must provide a semanticDbVersion
|
|def semanticDbVersion = ???
|""".stripMargin
Result.Failure(msg)
} else if (ZincWorkerUtil.isScala3(sv)) {
Result.Success(Agg.empty[Dep])
} else {
Result.Success(Agg(
ivy"org.scalameta:semanticdb-scalac_${sv}:${semDbVersion}"
))
}
}
private def semanticDbJavaPluginIvyDeps: T[Agg[Dep]] = T {
val sv = semanticDbJavaVersion()
if (sv.isEmpty) {
val msg =
"""|
|You must provide a javaSemanticDbVersion
|
|def semanticDbJavaVersion = ???
|""".stripMargin
Result.Failure(msg)
} else {
Result.Success(Agg(
ivy"com.sourcegraph:semanticdb-javac:${sv}"
))
}
}
/**
* Scalac options to activate the compiler plugins.
*/
protected def semanticDbEnablePluginScalacOptions: T[Seq[String]] = T {
val resolvedJars = resolveDeps(T.task {
val bind = bindDependency()
semanticDbPluginIvyDeps().map(_.exclude("*" -> "*")).map(bind)
})()
resolvedJars.iterator.map(jar => s"-Xplugin:${jar.path}").toSeq
}
protected def semanticDbPluginClasspath: T[Agg[PathRef]] = T {
resolveDeps(T.task {
val bind = bindDependency()
semanticDbPluginIvyDeps().map(bind)
})()
}
protected def resolvedSemanticDbJavaPluginIvyDeps: T[Agg[PathRef]] = T {
resolveDeps(T.task { semanticDbJavaPluginIvyDeps().map(bindDependency()) })()
}
def semanticDbData: T[PathRef] = T.persistent {
val javacOpts = SemanticDbJavaModule.javacOptionsTask(javacOptions(), semanticDbJavaVersion())
// we currently assume, we don't do incremental java compilation
os.remove.all(T.dest / "classes")
T.log.debug(s"effective javac options: ${javacOpts}")
zincWorker().worker()
.compileJava(
upstreamCompileOutput = upstreamCompileOutput(),
sources = allSourceFiles().map(_.path),
compileClasspath =
(compileClasspath() ++ resolvedSemanticDbJavaPluginIvyDeps()).map(_.path),
javacOptions = javacOpts,
reporter = None,
reportCachedProblems = zincReportCachedProblems(),
incrementalCompilation = zincIncrementalCompilation()
).map(r =>
SemanticDbJavaModule.copySemanticdbFiles(r.classes.path, T.workspace, T.dest / "data")
)
}
// keep in sync with bspCompiledClassesAndSemanticDbFiles
def compiledClassesAndSemanticDbFiles: Target[PathRef] = T {
val dest = T.dest
val classes = compile().classes.path
val sems = semanticDbData().path
if (os.exists(sems)) os.copy(sems, dest, mergeFolders = true)
if (os.exists(classes)) os.copy(classes, dest, mergeFolders = true, replaceExisting = true)
PathRef(dest)
}
// keep in sync with compiledClassesAndSemanticDbFiles
def bspCompiledClassesAndSemanticDbFiles: Target[UnresolvedPath] = {
if (
compiledClassesAndSemanticDbFiles.ctx.enclosing == s"${classOf[SemanticDbJavaModule].getName}#compiledClassesAndSemanticDbFiles"
) {
T {
T.log.debug(
s"compiledClassesAndSemanticDbFiles target was not overridden, assuming hard-coded classes directory for target ${compiledClassesAndSemanticDbFiles}"
)
UnresolvedPath.DestPath(
os.sub,
compiledClassesAndSemanticDbFiles.ctx.segments,
compiledClassesAndSemanticDbFiles.ctx.foreign
)
}
} else {
T {
T.log.debug(
s"compiledClassesAndSemanticDbFiles target was overridden, need to actually execute compilation to get the compiled classes directory for target ${compiledClassesAndSemanticDbFiles}"
)
UnresolvedPath.ResolvedPath(compiledClassesAndSemanticDbFiles().path)
}
}
}
}
object SemanticDbJavaModule {
val buildTimeJavaSemanticDbVersion = Versions.semanticDbJavaVersion
val buildTimeSemanticDbVersion = Versions.semanticDBVersion
private[mill] val contextSemanticDbVersion: InheritableThreadLocal[Option[String]] =
new InheritableThreadLocal[Option[String]] {
protected override def initialValue(): Option[String] = None.asInstanceOf[Option[String]]
}
private[mill] val contextJavaSemanticDbVersion: InheritableThreadLocal[Option[String]] =
new InheritableThreadLocal[Option[String]] {
protected override def initialValue(): Option[String] = None.asInstanceOf[Option[String]]
}
private[mill] def resetContext(): Unit = {
contextJavaSemanticDbVersion.set(None)
contextSemanticDbVersion.set(None)
}
def javacOptionsTask(javacOptions: Seq[String], semanticDbJavaVersion: String)(implicit
ctx: mill.api.Ctx
): Seq[String] = {
// these are only needed for Java 17+
val extracJavacExports =
if (Properties.isJavaAtLeast(17)) List(
"-J--add-exports",
"-Jjdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
"-J--add-exports",
"-Jjdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
"-J--add-exports",
"-Jjdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED",
"-J--add-exports",
"-Jjdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
"-J--add-exports",
"-Jjdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED"
)
else List.empty
val isNewEnough =
Version.isAtLeast(semanticDbJavaVersion, "0.8.10")(Version.IgnoreQualifierOrdering)
val buildTool = s" -build-tool:${if (isNewEnough) "mill" else "sbt"}"
val verbose = if (ctx.log.debugEnabled) " -verbose" else ""
javacOptions ++ Seq(
s"-Xplugin:semanticdb -sourceroot:${ctx.workspace} -targetroot:${ctx.dest / "classes"}${buildTool}${verbose}"
) ++ extracJavacExports
}
// The semanticdb-javac plugin has issues with the -sourceroot setting, so we correct this on the fly
def copySemanticdbFiles(
classesDir: os.Path,
sourceroot: os.Path,
targetDir: os.Path
): PathRef = {
assert(classesDir != targetDir)
os.remove.all(targetDir)
os.makeDir.all(targetDir)
val ups = sourceroot.segments.size
val semanticPath = os.rel / "META-INF" / "semanticdb"
val toClean = classesDir / semanticPath / sourceroot.segments.toSeq
// copy over all found semanticdb-files into the target directory
// but with corrected directory layout
os.walk(classesDir, preOrder = true)
.filter(os.isFile)
.foreach { p =>
if (p.ext == "semanticdb") {
val target =
if (ups > 0 && p.startsWith(toClean)) {
targetDir / semanticPath / p.relativeTo(toClean)
} else {
targetDir / p.relativeTo(classesDir)
}
os.copy(p, target, createFolders = true)
}
}
PathRef(targetDir)
}
}