forked from sourcegraph/scip-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sbt
595 lines (557 loc) · 18.9 KB
/
build.sbt
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
import sbtdocker.DockerfileBase
import scala.xml.{Node => XmlNode, NodeSeq => XmlNodeSeq, _}
import scala.xml.transform.{RewriteRule, RuleTransformer}
import java.io.File
import java.nio.file.Files
import java.util.Properties
import scala.collection.mutable.ListBuffer
import scala.util.control.NoStackTrace
lazy val V =
new {
val protobuf = "3.15.6"
val protoc =
"3.17.3" // the oldest protoc version with Apple M1 support, see https://github.com/scalapb/ScalaPB/issues/1024#issuecomment-860126568
val coursier = "2.1.5"
val scalaXml = "2.1.0"
val bsp = "2.0.0-M13"
val moped = "0.1.11"
val gradle = "7.0"
val scala213 = "2.13.10"
val scala212 = "2.12.17"
val scala211 = "2.11.12"
val scala3 = "3.2.2"
val metals = "0.11.11"
val scalameta = "4.8.1"
val semanticdbKotlinc = "0.3.2"
val testcontainers = "0.39.3"
val requests = "0.6.5"
val minimalMillVersion = "0.10.0"
val millScipVersion = "0.3.6"
val kotlinGradlePlugin = "1.5.31"
}
inThisBuild(
List(
scalaVersion := V.scala213,
crossScalaVersions := List(V.scala213),
scalafixDependencies +=
"com.github.liancheng" %% "organize-imports" % "0.6.0",
scalafixCaching := true,
scalacOptions ++= List("-Wunused:imports"),
semanticdbEnabled := true,
semanticdbVersion := V.scalameta,
organization := "com.sourcegraph",
homepage := Some(url("https://github.com/sourcegraph/scip-java")),
dynverSeparator := "-",
PB.protocVersion := V.protoc,
licenses :=
List("Apache-2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0")),
developers :=
List(
Developer(
"olafurpg",
"Ólafur Páll Geirsson",
url("https://github.com/olafurpg")
),
Developer(
"Strum355",
"Noah Santschi-Cooney",
url("https://github.com/Strum355")
)
)
)
)
name := "root"
(publish / skip) := true
commands +=
Command.command("fixAll") { s =>
"scalafixAll" :: "scalafmtAll" :: "scalafmtSbt" :: "javafmtAll" :: s
}
commands +=
Command.command("checkAll") { s =>
"javafmtCheckAll" :: "scalafmtCheckAll" :: "scalafmtSbtCheck" ::
"scalafixAll --check" :: "publishLocal" :: "docs/docusaurusCreateSite" ::
s
}
lazy val semanticdb = project
.in(file("semanticdb-java"))
.settings(
moduleName := "semanticdb-java",
javaOnlySettings,
(Compile / PB.targets) :=
Seq(PB.gens.java(V.protobuf) -> (Compile / sourceManaged).value)
)
lazy val agent = project
.in(file("semanticdb-agent"))
.settings(
fatjarPackageSettings,
javaOnlySettings,
javaToolchainVersion := "8",
moduleName := "semanticdb-agent",
libraryDependencies ++=
List(
"net.bytebuddy" % "byte-buddy" % "1.11.9",
"net.bytebuddy" % "byte-buddy-agent" % "1.11.21"
),
Compile / packageBin / packageOptions +=
Package.ManifestAttributes(
"Agent-Class" -> "com.sourcegraph.semanticdb_javac.SemanticdbAgent",
"Can-Redefine-Classes" -> "true",
"Can-Retransform-Classes" -> "true",
"Premain-Class" -> "com.sourcegraph.semanticdb_javac.SemanticdbAgent"
)
)
lazy val gradlePlugin = project
.in(file("semanticdb-gradle-plugin"))
.settings(
name := "semanticdb-gradle",
scalaVersion := V.scala213,
buildInfoPackage := "com.sourcegraph.scip_java",
publish / skip := true,
javaToolchainVersion := "8",
scalacOptions ++= Seq("-target:8", "-release", "8"),
libraryDependencies ++=
List(
"dev.gradleplugins" % "gradle-api" % V.gradle % Provided,
"dev.gradleplugins" % "gradle-test-kit" % V.gradle % Provided,
"org.jetbrains.kotlin" % "kotlin-gradle-plugin" % V.kotlinGradlePlugin %
Provided
),
buildInfoKeys :=
Seq[BuildInfoKey](
version,
sbtVersion,
scalaVersion,
"javacModuleOptions" -> javacModuleOptions,
"semanticdbScalacVersions" ->
com
.sourcegraph
.sbtsourcegraph
.Versions
.cachedSemanticdbVersionsByScalaVersion,
"sbtSourcegraphVersion" ->
com.sourcegraph.sbtsourcegraph.BuildInfo.version,
"semanticdbVersion" -> V.scalameta,
"semanticdbKotlincVersion" -> V.semanticdbKotlinc,
"mtagsVersion" -> V.metals,
"scala211" -> V.scala211,
"scala212" -> V.scala212,
"scala213" -> V.scala213,
"scala3" -> V.scala3,
"bspVersion" -> V.bsp,
"minimalMillVersion" -> V.minimalMillVersion,
"millScipVersion" -> V.millScipVersion
)
)
.enablePlugins(BuildInfoPlugin)
lazy val javacPlugin = project
.in(file("semanticdb-javac"))
.settings(
fatjarPackageSettings,
javaOnlySettings,
moduleName := "semanticdb-javac",
javaToolchainVersion := "8",
javacOptions += "-g",
(assembly / assemblyShadeRules) :=
Seq(
ShadeRule
.rename(
// Don't rename SemanticdbPlugin since the fully-qualified name is
// referenced from META-INF/services/com.sun.source.util.Plugin
"com.sourcegraph.semanticdb_javac.SemanticdbPlugin" ->
"com.sourcegraph.semanticdb_javac.SemanticdbPlugin",
// Don't rename PrintJavaVersion because we load it via FQN to
// detect the Java of a JVM installation.
"com.sourcegraph.semanticdb_javac.PrintJavaVersion" ->
"com.sourcegraph.semanticdb_javac.PrintJavaVersion",
// Don't rename InjectSemanticdbOptions because we load it via FQN to
// process a list of Java compiler options.
"com.sourcegraph.semanticdb_javac.InjectSemanticdbOptions" ->
"com.sourcegraph.semanticdb_javac.InjectSemanticdbOptions",
"com.google.**" -> "com.sourcegraph.shaded.com.google.@1",
// Shade everything else in the semanticdb-javac compiler plugin in
// order to be able to index the plugin code itself. Without this step,
// we can't add the plugin to the classpath while compiling the source
// code of the plugin itself because it results in cryptic compile errors.
"com.sourcegraph.**" -> "com.sourcegraph.shaded.com.sourcegraph.@1",
"google.**" -> "com.sourcegraph.shaded.google.@1",
"org.relaxng.**" -> "com.sourcegraph.shaded.relaxng.@1"
)
.inAll
)
)
.dependsOn(semanticdb)
lazy val scipProto = project
.in(file("scip-java-proto"))
.settings(
moduleName := "scip-java-proto",
javaToolchainVersion := "8",
javaOnlySettings,
libraryDependencies +=
"com.google.protobuf" % "protobuf-java-util" % V.protobuf,
(Compile / PB.targets) :=
Seq(PB.gens.java(V.protobuf) -> (Compile / sourceManaged).value),
Compile / PB.protocOptions := Seq("--experimental_allow_proto3_optional")
)
lazy val scip = project
.in(file("scip-semanticdb"))
.settings(
moduleName := "scip-semanticdb",
javaToolchainVersion := "8",
javaOnlySettings,
(Compile / PB.targets) :=
Seq(PB.gens.java(V.protobuf) -> (Compile / sourceManaged).value),
Compile / PB.protocOptions := Seq("--experimental_allow_proto3_optional")
)
.dependsOn(semanticdb, scipProto)
lazy val cli = project
.in(file("scip-java"))
.settings(
moduleName := "scip-java",
(Compile / mainClass) := Some("com.sourcegraph.scip_java.ScipJava"),
(run / baseDirectory) := (ThisBuild / baseDirectory).value,
buildInfoKeys :=
Seq[BuildInfoKey](
version,
sbtVersion,
scalaVersion,
"javacModuleOptions" -> javacModuleOptions,
"semanticdbScalacVersions" ->
com
.sourcegraph
.sbtsourcegraph
.Versions
.cachedSemanticdbVersionsByScalaVersion,
"sbtSourcegraphVersion" ->
com.sourcegraph.sbtsourcegraph.BuildInfo.version,
"semanticdbVersion" -> V.scalameta,
"semanticdbKotlincVersion" -> V.semanticdbKotlinc,
"mtagsVersion" -> V.metals,
"scala211" -> V.scala211,
"scala212" -> V.scala212,
"scala213" -> V.scala213,
"scala3" -> V.scala3,
"bspVersion" -> V.bsp,
"minimalMillVersion" -> V.minimalMillVersion,
"millScipVersion" -> V.millScipVersion
),
buildInfoPackage := "com.sourcegraph.scip_java",
libraryDependencies ++=
List(
"io.get-coursier" %% "coursier" % V.coursier,
"io.get-coursier" %% "coursier-jvm" % V.coursier,
"org.scalameta" % "mtags-interfaces" % V.metals,
"org.scala-lang.modules" %% "scala-xml" % V.scalaXml,
"com.lihaoyi" %% "requests" % V.requests,
"org.scalameta" %% "moped" % V.moped,
"org.scalameta" %% "ascii-graphs" % "0.1.2",
"org.jetbrains.kotlin" % "kotlin-compiler-embeddable" % "1.8.21"
),
(Compile / resourceGenerators) +=
Def
.task[Seq[File]] {
val outs = ListBuffer.empty[(File, File)]
val out = (Compile / resourceManaged).value.toPath
IO.delete(out.toFile)
def addJar(jar: File, filename: String): Unit = {
outs += jar -> out.resolve(filename).toFile
}
addJar(
(javacPlugin / Compile / Keys.`package`).value,
"semanticdb-plugin.jar"
)
addJar(
(agent / Compile / Keys.`package`).value,
"semanticdb-agent.jar"
)
addJar((gradlePlugin / Compile / assembly).value, "gradle-plugin.jar")
IO.copy(
outs,
overwrite = true,
preserveLastModified = false,
preserveExecutable = true
)
val props = new Properties()
val propsFile = out.resolve("scip-java.properties").toFile
val copiedJars = outs.collect { case (_, out) =>
out
}
val names = copiedJars.map(_.getName).mkString(";")
props.put("jarNames", names)
IO.write(props, "scip-java", propsFile)
propsFile :: copiedJars.toList
}
.taskValue,
docker / imageNames := {
val latest = {
val label =
if (isSnapshot.value)
"latest-snapshot"
else
"latest"
List(ImageName(s"sourcegraph/scip-java:$label"))
}
// Don't publish a separately tagged image for snapshots -
// only latest-snapshot
val versioned =
if (isSnapshot.value)
Nil
else
List(ImageName(s"sourcegraph/scip-java:${version.value}"))
latest ++ versioned
},
docker / dockerfile :=
NativeDockerfile((ThisBuild / baseDirectory).value / "Dockerfile")
)
.enablePlugins(PackPlugin, DockerPlugin, BuildInfoPlugin)
.dependsOn(scip)
def commitAll(): Unit = {
import scala.sys.process._
"git add .".!!
"git commit --allow-empty -m WIP".!!
}
commands +=
Command.command("commitall") { s =>
commitAll()
s
}
commands +=
Command.command("nativeImageProfiled") { s =>
val targetroot =
file("tests/minimized/.j11/target/scala-2.13/meta").absolutePath
val output = Files.createTempFile("scip-java", "index.scip")
"minimized/compile" ::
s"""nativeImageRunAgent " index-semanticdb --output=$output $targetroot"""" ::
"nativeImage" :: s
}
def minimizedSourceDirectory =
file("tests/minimized/src/main/java").getAbsoluteFile
lazy val minimizedSettings = List[Def.Setting[_]](
autoScalaLibrary := false,
(publish / skip) := true,
(publishLocal / skip) := true,
(run / fork) := true,
(Compile / unmanagedSourceDirectories) += minimizedSourceDirectory,
libraryDependencies ++= List("org.projectlombok" % "lombok" % "1.18.22"),
javacOptions +=
List(
s"-Xplugin:semanticdb",
s"-build-tool:sbt",
s"-text:on",
s"-verbose",
s"-sourceroot:${(ThisBuild / baseDirectory).value}",
s"-targetroot:${(Compile / semanticdbTargetRoot).value}",
s"-randomtimestamp=${System.nanoTime()}"
).mkString(" ")
)
lazy val minimized = project
.in(file("tests/minimized/.j11"))
.settings(minimizedSettings, javaOnlySettings)
.dependsOn(agent, javacPlugin)
.disablePlugins(JavaFormatterPlugin)
lazy val minimized8 = project
.in(file("tests/minimized/.j8"))
.settings(minimizedSettings, javaToolchainVersion := "8", javaOnlySettings)
.dependsOn(agent, javacPlugin)
.disablePlugins(JavaFormatterPlugin)
def javacModuleOptions =
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"
)
lazy val minimized17 = project
.in(file("tests/minimized/.j17"))
.settings(
javaOnlySettings,
minimizedSettings,
javaToolchainVersion := "17",
javacOptions ++= javacModuleOptions
)
.dependsOn(agent, javacPlugin)
.disablePlugins(JavaFormatterPlugin)
lazy val minimizedScala = project
.in(file("tests/minimized-scala"))
.settings(
(publish / skip) := true,
semanticdbOptions ++=
List("-P:semanticdb:text:on", "-P:semanticdb:synthetics:on")
)
.dependsOn(minimized)
lazy val unit = project
.in(file("tests/unit"))
.settings(
testSettings,
//javaOptions ++= Seq( "-Xdebug", "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"),
buildInfoKeys :=
Seq[BuildInfoKey](
version,
scalaVersion,
"temporaryDirectory" -> target.value / "tmpdir",
"sourceroot" -> (ThisBuild / baseDirectory).value,
"minimizedJavaSourceDirectory" -> minimizedSourceDirectory,
"minimizedJavaTargetroot" ->
(minimized / Compile / semanticdbTargetRoot).value,
"minimizedScalaSourceDirectory" ->
(minimizedScala / Compile / sourceDirectory).value / "scala",
"minimizedScalaTargetroot" ->
(minimizedScala / Compile / semanticdbTargetRoot).value
),
buildInfoPackage := "tests"
)
.dependsOn(javacPlugin, cli)
.enablePlugins(BuildInfoPlugin)
lazy val buildTools = project
.in(file("tests/buildTools"))
.settings(
testSettings,
(Test / javaOptions) ++=
List(
s"-javaagent:${(agent / Compile / Keys.`package`).value}",
s"-Dsemanticdb.pluginpath=${(javacPlugin / Compile / Keys.`package`).value}",
s"-Dsemanticdb.sourceroot=${(ThisBuild / baseDirectory).value}",
s"-Dsemanticdb.targetroot=${(agent / Compile / target).value / "semanticdb-targetroot"}"
),
Test / envVars ++=
Map(
"SCIP_JAVA_CLI" -> ((cli / pack).value / "bin" / "scip-java").toString
),
Test / fork := true,
// Our CI set up is a couple of measly vCPUs so parallelising tests there makes
// everything worse
Test / testForkedParallel := !sys.env.contains("CI")
)
.dependsOn(agent, unit)
lazy val snapshots = project
.in(file("tests/snapshots"))
.settings(
testSettings,
buildInfoKeys :=
Seq[BuildInfoKey](
"snapshotDirectory" -> (Compile / sourceDirectory).value / "generated"
),
buildInfoPackage := "tests.snapshots"
)
.dependsOn(minimizedScala, unit)
.enablePlugins(BuildInfoPlugin)
lazy val bench = project
.in(file("tests/benchmarks"))
.settings(
moduleName := "scip-java-bench",
Jmh / bspEnabled := false,
(run / fork) := true,
(publish / skip) := true
)
.dependsOn(unit)
.enablePlugins(JmhPlugin)
lazy val docs = project
.in(file("scip-java-docs"))
.settings(
publishLocal / skip := true,
publish / skip := true,
moduleName := "scip-java-docs",
mdocOut :=
(ThisBuild / baseDirectory).value / "website" / "target" / "docs",
fork := false,
mdocVariables :=
Map[String, String](
"VERSION" -> version.value,
"SCALA_VERSION" -> scalaVersion.value,
"STABLE_VERSION" -> version.value.replaceFirst("\\-.*", "")
)
)
.dependsOn(unit)
.enablePlugins(DocusaurusPlugin)
lazy val javaOnlySettings = List[Def.Setting[_]](
autoScalaLibrary := false,
incOptions ~= { old =>
old.withEnabled(false).withApiDebug(true)
},
crossPaths := false
)
val testSettings = List(
(publish / skip) := true,
autoScalaLibrary := true,
testFrameworks := List(TestFrameworks.MUnit),
testOptions ++= {
if (!(Test / testForkedParallel).value)
List(Tests.Argument(TestFrameworks.MUnit, "-b"))
else
Nil
},
libraryDependencies ++=
List(
"org.scalameta" %% "munit" % "0.7.29",
"org.scalameta" %% "mtags" % V.metals cross CrossVersion.full,
"com.dimafeng" %% "testcontainers-scala-munit" % V.testcontainers,
"com.dimafeng" %% "testcontainers-scala-postgresql" % V.testcontainers,
"org.scalameta" %% "moped-testkit" % V.moped,
"org.scalameta" %% "scalameta" % V.scalameta,
"io.get-coursier" %% "coursier" % V.coursier,
"com.lihaoyi" %% "pprint" % "0.6.6"
)
)
lazy val fatjarPackageSettings = List[Def.Setting[_]](
(assembly / assemblyMergeStrategy) := {
case PathList("javax", _ @_*) =>
MergeStrategy.discard
case PathList("com", "sun", _ @_*) =>
MergeStrategy.discard
case PathList("sun", _ @_*) =>
MergeStrategy.discard
case PathList("META-INF", "versions", "9", "module-info.class") =>
MergeStrategy.discard
case x =>
val oldStrategy = (assembly / assemblyMergeStrategy).value
oldStrategy(x)
},
(Compile / Keys.`package`) := {
val slimJar = (Compile / Keys.`package`).value
val fatJar = crossTarget.value / (assembly / assemblyJarName).value
val _ = assembly.value
IO.copyFile(fatJar, slimJar, CopyOptions().withOverwrite(true))
slimJar
},
(Compile / packageBin / packagedArtifact) := {
val (art, slimJar) = (Compile / packageBin / packagedArtifact).value
val fatJar =
new File(crossTarget.value + "/" + (assembly / assemblyJarName).value)
val _ = assembly.value
IO.copy(List(fatJar -> slimJar), CopyOptions().withOverwrite(true))
(art, slimJar)
},
pomPostProcess := { node =>
new RuleTransformer(
new RewriteRule {
private def isAbsorbedDependency(node: XmlNode): Boolean = {
node.label == "dependency" &&
node.child.exists(child => child.label == "artifactId")
}
override def transform(node: XmlNode): XmlNodeSeq =
node match {
case e: Elem if isAbsorbedDependency(node) =>
Comment(
"the dependency that was here has been absorbed via sbt-assembly"
)
case _ =>
node
}
}
).transform(node).head
}
)
lazy val dumpScipJavaVersion = taskKey[Unit](
"Dump the version of scip-java tool to a VERSION file"
)
dumpScipJavaVersion := {
val versionValue = (cli / version).value
IO.write((ThisBuild / baseDirectory).value / "VERSION", versionValue)
}