-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sbt
571 lines (525 loc) · 20.2 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
import Util._
import Dependencies._
import Scripted._
//import com.typesafe.tools.mima.core._, ProblemFilters._
def baseVersion = "1.1.0-SNAPSHOT"
def internalPath = file("internal")
lazy val compilerBridgeScalaVersions = List(scala212, scala211, scala210)
def mimaSettings: Seq[Setting[_]] = Seq(
mimaPreviousArtifacts := Set(
organization.value % moduleName.value % "1.0.0"
cross (if (crossPaths.value) CrossVersion.binary else CrossVersion.disabled)
)
)
def commonSettings: Seq[Setting[_]] = Seq(
scalaVersion := scala212,
// publishArtifact in packageDoc := false,
resolvers += Resolver.typesafeIvyRepo("releases"),
resolvers += Resolver.sonatypeRepo("snapshots"),
resolvers += "bintray-sbt-maven-releases" at "https://dl.bintray.com/sbt/maven-releases/",
resolvers += Resolver.url(
"bintray-sbt-ivy-snapshots",
new URL("https://dl.bintray.com/sbt/ivy-snapshots/"))(Resolver.ivyStylePatterns),
// concurrentRestrictions in Global += Util.testExclusiveRestriction,
testOptions += Tests.Argument(TestFrameworks.ScalaCheck, "-w", "1"),
javacOptions in compile ++= Seq("-Xlint", "-Xlint:-serial"),
crossScalaVersions := Seq(scala211, scala212),
publishArtifact in Test := false,
commands ++= Seq(publishBridgesAndTest, publishBridgesAndSet, crossTestBridges),
scalacOptions += "-YdisableFlatCpCaching"
)
def relaxNon212: Seq[Setting[_]] = Seq(
scalacOptions := {
val old = scalacOptions.value
scalaBinaryVersion.value match {
case "2.12" => old
case _ =>
old filterNot Set(
"-Xfatal-warnings",
"-deprecation",
"-Ywarn-unused",
"-Ywarn-unused-import",
"-YdisableFlatCpCaching"
)
}
}
)
def minimalSettings: Seq[Setting[_]] = commonSettings
// def minimalSettings: Seq[Setting[_]] =
// commonSettings ++ customCommands ++
// publishPomSettings ++ Release.javaVersionCheckSettings
def baseSettings: Seq[Setting[_]] =
minimalSettings
// minimalSettings ++ baseScalacOptions ++ Licensed.settings ++ Formatting.settings
def addBaseSettingsAndTestDeps(p: Project): Project =
p.settings(baseSettings).configure(addTestDependencies)
val altLocalRepoName = "alternative-local"
val altLocalRepoPath = sys.props("user.home") + "/.ivy2/sbt-alternative"
lazy val altLocalResolver = Resolver.file(
altLocalRepoName,
file(sys.props("user.home") + "/.ivy2/sbt-alternative"))(Resolver.ivyStylePatterns)
lazy val altLocalPublish =
TaskKey[Unit]("alt-local-publish", "Publishes an artifact locally to an alternative location.")
def altPublishSettings: Seq[Setting[_]] =
Seq(
resolvers += altLocalResolver,
altLocalPublish := {
import sbt.librarymanagement._
import sbt.internal.librarymanagement._
val config = (Keys.publishLocalConfiguration).value
val moduleSettings = (Keys.moduleSettings).value
val ivy = new IvySbt((ivyConfiguration.value))
val module = new ivy.Module(moduleSettings)
val newConfig = config.withResolverName(altLocalRepoName).withOverwrite(false)
streams.value.log.info(s"Publishing $module to local repo: $altLocalRepoName")
IvyActions.publish(module, newConfig, streams.value.log)
}
)
val noPublish: Seq[Setting[_]] = List(
publish := {},
publishLocal := {},
publishArtifact in Compile := false,
publishArtifact in Test := false,
publishArtifact := false,
skip in publish := true,
)
lazy val zincRoot: Project = (project in file("."))
// configs(Sxr.sxrConf).
.aggregate(
zinc,
zincTesting,
zincPersist,
zincCore,
zincIvyIntegration,
zincCompile,
zincCompileCore,
compilerInterface,
compilerBridge,
zincBenchmarks,
zincApiInfo,
zincClasspath,
zincClassfile,
zincScripted
)
.settings(
inThisBuild(
Seq(
git.baseVersion := baseVersion,
// https://github.com/sbt/sbt-git/issues/109
// Workaround from https://github.com/sbt/sbt-git/issues/92#issuecomment-161853239
git.gitUncommittedChanges := {
val statusCommands = Seq(
Seq("diff-index", "--cached", "HEAD"),
Seq("diff-index", "HEAD"),
Seq("diff-files"),
Seq("ls-files", "--exclude-standard", "--others")
)
// can't use git.runner.value because it's a task
val runner = com.typesafe.sbt.git.ConsoleGitRunner
val dir = baseDirectory.value
// sbt/zinc#334 Seemingly "git status" resets some stale metadata.
runner("status")(dir, com.typesafe.sbt.git.NullLogger)
val uncommittedChanges = statusCommands flatMap { c =>
val res = runner(c: _*)(dir, com.typesafe.sbt.git.NullLogger)
if (res.isEmpty) Nil else Seq(c -> res)
}
val un = uncommittedChanges.nonEmpty
if (un) {
uncommittedChanges foreach {
case (cmd, res) =>
sLog.value debug s"""Uncommitted changes found via "${cmd mkString " "}":\n${res}"""
}
}
un
},
version := {
val v = version.value
if (v contains "SNAPSHOT") git.baseVersion.value
else v
},
bintrayPackage := "zinc",
scmInfo := Some(ScmInfo(url("https://github.com/sbt/zinc"), "[email protected]:sbt/zinc.git")),
description := "Incremental compiler of Scala",
homepage := Some(url("https://github.com/sbt/zinc")),
developers +=
Developer("jvican", "Jorge Vicente Cantero", "@jvican", url("https://github.com/jvican")),
)),
minimalSettings,
otherRootSettings,
noPublish,
name := "zinc Root",
customCommands
)
lazy val zinc = (project in file("zinc"))
.dependsOn(
zincCore,
zincPersist,
zincCompileCore,
zincClassfile,
zincIvyIntegration % "compile->compile;test->test",
zincTesting % Test
)
.configure(addBaseSettingsAndTestDeps)
.settings(
name := "zinc",
mimaSettings,
)
lazy val zincTesting = (project in internalPath / "zinc-testing")
.settings(
minimalSettings,
noPublish,
name := "zinc Testing",
libraryDependencies ++= Seq(scalaCheck, scalatest, junit, sjsonnewScalaJson.value)
)
.configure(addSbtLmCore, addSbtLmIvy)
lazy val zincCompile = (project in file("zinc-compile"))
.dependsOn(zincCompileCore, zincCompileCore % "test->test")
.configure(addBaseSettingsAndTestDeps)
.settings(
name := "zinc Compile",
mimaSettings,
)
.configure(addSbtUtilTracking)
// Persists the incremental data structures using Protobuf
lazy val zincPersist = (project in internalPath / "zinc-persist")
.dependsOn(zincCore, zincCore % "test->test")
.configure(addBaseSettingsAndTestDeps)
.settings(
name := "zinc Persist",
libraryDependencies += sbinary,
compileOrder := sbt.CompileOrder.Mixed,
PB.targets in Compile := List(scalapb.gen() -> (sourceManaged in Compile).value),
mimaSettings,
)
// Implements the core functionality of detecting and propagating changes incrementally.
// Defines the data structures for representing file fingerprints and relationships and the overall source analysis
lazy val zincCore = (project in internalPath / "zinc-core")
.dependsOn(
zincApiInfo,
zincClasspath,
compilerInterface,
compilerBridge % Test,
zincTesting % Test
)
.configure(addBaseSettingsAndTestDeps)
.settings(
// we need to fork because in unit tests we set usejavacp = true which means
// we are expecting all of our dependencies to be on classpath so Scala compiler
// can use them while constructing its own classpath for compilation
fork in Test := true,
// needed because we fork tests and tests are ran in parallel so we have multiple Scala
// compiler instances that are memory hungry
javaOptions in Test += "-Xmx1G",
name := "zinc Core",
compileOrder := sbt.CompileOrder.Mixed,
mimaSettings,
)
.configure(addSbtIO, addSbtUtilLogging, addSbtUtilRelation)
lazy val zincBenchmarks = (project in internalPath / "zinc-benchmarks")
.dependsOn(compilerInterface % "compile->compile;compile->test")
.dependsOn(compilerBridge, zincCore, zincTesting % Test)
.enablePlugins(JmhPlugin)
.settings(
noPublish,
name := "Benchmarks of Zinc and the compiler bridge",
libraryDependencies ++= Seq(
"org.eclipse.jgit" % "org.eclipse.jgit" % "4.6.0.201612231935-r",
"net.openhft" % "affinity" % "3.0.6"
),
scalaVersion := scala212,
crossScalaVersions := Seq(scala211, scala212),
javaOptions in Test += "-Xmx600M -Xms600M",
)
lazy val zincIvyIntegration = (project in internalPath / "zinc-ivy-integration")
.dependsOn(zincCompileCore, zincTesting % Test)
.settings(
baseSettings,
name := "zinc Ivy Integration",
compileOrder := sbt.CompileOrder.ScalaThenJava,
mimaSettings,
)
.configure(addSbtLmCore, addSbtLmIvyTest)
// sbt-side interface to compiler. Calls compiler-side interface reflectively
lazy val zincCompileCore = (project in internalPath / "zinc-compile-core")
.enablePlugins(ContrabandPlugin)
.dependsOn(
compilerInterface % "compile;test->test",
zincClasspath,
zincApiInfo,
zincClassfile,
zincTesting % Test
)
.configure(addBaseSettingsAndTestDeps)
.settings(
name := "zinc Compile Core",
libraryDependencies ++= Seq(scalaCompiler.value % Test, launcherInterface, parserCombinator),
unmanagedJars in Test := Seq(packageSrc in compilerBridge in Compile value).classpath,
managedSourceDirectories in Compile +=
baseDirectory.value / "src" / "main" / "contraband-java",
sourceManaged in (Compile, generateContrabands) := baseDirectory.value / "src" / "main" / "contraband-java",
mimaSettings,
)
.configure(addSbtUtilLogging, addSbtIO, addSbtUtilControl)
// defines Java structures used across Scala versions, such as the API structures and relationships extracted by
// the analysis compiler phases and passed back to sbt. The API structures are defined in a simple
// format from which Java sources are generated by the sbt-contraband plugin.
lazy val compilerInterface = (project in internalPath / "compiler-interface")
.enablePlugins(ContrabandPlugin)
.settings(
minimalSettings,
// javaOnlySettings,
name := "Compiler Interface",
// Use the smallest Scala version in the compilerBridgeScalaVersions
// Technically the scalaVersion shouldn't have any effect since scala library is not included,
// but given that Scala 2.10 compiler cannot parse Java 8 source, it's probably good to keep this.
crossScalaVersions := Seq(scala210),
scalaVersion := scala210,
relaxNon212,
libraryDependencies ++= Seq(scalaLibrary.value % Test),
exportJars := true,
resourceGenerators in Compile += Def
.task(
generateVersionFile(
version.value,
resourceManaged.value,
streams.value,
compile in Compile value
)
)
.taskValue,
managedSourceDirectories in Compile +=
baseDirectory.value / "src" / "main" / "contraband-java",
sourceManaged in (Compile, generateContrabands) := baseDirectory.value / "src" / "main" / "contraband-java",
crossPaths := false,
autoScalaLibrary := false,
altPublishSettings,
mimaSettings,
)
.configure(addSbtUtilInterface)
val cleanSbtBridge = taskKey[Unit]("Cleans the sbt bridge.")
def wrapIn(color: String, content: String): String = {
import sbt.internal.util.ConsoleAppender
if (!ConsoleAppender.formatEnabledInEnv) content
else color + content + scala.Console.RESET
}
// Compiler-side interface to compiler that is compiled against the compiler being used either in advance or on the fly.
// Includes API and Analyzer phases that extract source API and relationships.
lazy val compilerBridge: Project = (project in internalPath / "compiler-bridge")
.dependsOn(compilerInterface % "compile;test->test", zincApiInfo % "test->test")
.settings(
baseSettings,
crossScalaVersions := compilerBridgeScalaVersions,
relaxNon212,
libraryDependencies += scalaCompiler.value % "provided",
autoScalaLibrary := false,
// precompiledSettings,
name := "Compiler Bridge",
exportJars := true,
// we need to fork because in unit tests we set usejavacp = true which means
// we are expecting all of our dependencies to be on classpath so Scala compiler
// can use them while constructing its own classpath for compilation
fork in Test := true,
// needed because we fork tests and tests are ran in parallel so we have multiple Scala
// compiler instances that are memory hungry
javaOptions in Test += "-Xmx1G",
inBoth(unmanagedSourceDirectories ++= scalaPartialVersion.value.collect {
case (2, y) if y == 10 => new File(scalaSource.value.getPath + "_2.10")
case (2, y) if y >= 11 => new File(scalaSource.value.getPath + "_2.11+")
}.toList),
cleanSbtBridge := {
val sbtV = sbtVersion.value
val sbtOrg = "org.scala-sbt"
val sbtScalaVersion = "2.10.6"
val bridgeVersion = version.value
val scalaV = scalaVersion.value
// Assumes that JDK version is the same than the one that publishes the bridge
val classVersion = System.getProperty("java.class.version")
val home = System.getProperty("user.home")
val org = organization.value
val artifact = moduleName.value
val artifactName =
s"$org-$artifact-$bridgeVersion-bin_${scalaV}__$classVersion"
val targetsToDelete = List(
// We cannot use the target key, it's not scoped in `ThisBuild` nor `Global`.
(baseDirectory in ThisBuild).value / "target" / "zinc-components",
file(home) / ".ivy2/cache" / sbtOrg / artifactName,
file(home) / ".sbt/boot" / s"scala-$sbtScalaVersion" / sbtOrg / "sbt" / sbtV / artifactName
)
val logger = streams.value.log
logger.info(wrapIn(scala.Console.BOLD, "Cleaning stale compiler bridges:"))
targetsToDelete.foreach { target =>
IO.delete(target)
logger.info(s"${wrapIn(scala.Console.GREEN, " ✓ ")}${target.getAbsolutePath}")
}
},
publishLocal := publishLocal.dependsOn(cleanSbtBridge).value,
altPublishSettings,
mimaSettings,
)
val scalaPartialVersion = Def setting (CrossVersion partialVersion scalaVersion.value)
def inBoth(ss: Setting[_]*): Seq[Setting[_]] = Seq(Compile, Test) flatMap (inConfig(_)(ss))
// defines operations on the API of a source, including determining whether it has changed and converting it to a string
// and discovery of Projclasses and annotations
lazy val zincApiInfo = (project in internalPath / "zinc-apiinfo")
.dependsOn(compilerInterface, zincClassfile % "compile;test->test")
.configure(addBaseSettingsAndTestDeps)
.settings(
name := "zinc ApiInfo",
crossScalaVersions := compilerBridgeScalaVersions,
relaxNon212,
mimaSettings,
)
// Utilities related to reflection, managing Scala versions, and custom class loaders
lazy val zincClasspath = (project in internalPath / "zinc-classpath")
.dependsOn(compilerInterface)
.configure(addBaseSettingsAndTestDeps)
.settings(
name := "zinc Classpath",
crossScalaVersions := compilerBridgeScalaVersions,
relaxNon212,
libraryDependencies ++= Seq(scalaCompiler.value, launcherInterface),
mimaSettings,
)
.configure(addSbtIO)
// class file reader and analyzer
lazy val zincClassfile = (project in internalPath / "zinc-classfile")
.dependsOn(compilerInterface % "compile;test->test")
.configure(addBaseSettingsAndTestDeps)
.settings(
name := "zinc Classfile",
crossScalaVersions := compilerBridgeScalaVersions,
relaxNon212,
mimaSettings,
)
.configure(addSbtIO, addSbtUtilLogging)
// re-implementation of scripted engine
lazy val zincScripted = (project in internalPath / "zinc-scripted")
.dependsOn(zinc, zincIvyIntegration % "test->test")
.settings(
minimalSettings,
noPublish,
name := "zinc Scripted",
)
.configure(addSbtUtilScripted)
lazy val crossTestBridges = {
Command.command("crossTestBridges") { state =>
(compilerBridgeScalaVersions.flatMap { (bridgeVersion: String) =>
// Note the ! here. You need this so compilerInterface gets forced to the scalaVersion
s"++ $bridgeVersion!" ::
s"${compilerBridge.id}/test" ::
Nil
}) :::
(s"++ $scala212!" ::
state)
}
}
lazy val publishBridgesAndSet = {
Command.args("publishBridgesAndSet", "<version>") { (state, args) =>
require(args.nonEmpty, "Missing Scala version argument.")
val userScalaVersion = args.mkString("")
s"${compilerInterface.id}/publishLocal" ::
compilerBridgeScalaVersions.flatMap { (bridgeVersion: String) =>
s"++ $bridgeVersion!" ::
s"${zincApiInfo.id}/publishLocal" ::
s"${compilerBridge.id}/publishLocal" :: Nil
} :::
s"++ $userScalaVersion!" ::
state
}
}
lazy val publishBridgesAndTest = Command.args("publishBridgesAndTest", "<version>") {
(state, args) =>
require(args.nonEmpty,
"Missing arguments to publishBridgesAndTest. Maybe quotes are missing around command?")
val version = args mkString ""
s"${compilerInterface.id}/publishLocal" ::
(compilerBridgeScalaVersions.flatMap { (bridgeVersion: String) =>
s"++ $bridgeVersion" ::
s"${zincApiInfo.id}/publishLocal" ::
s"${compilerBridge.id}/publishLocal" :: Nil
}) :::
s"++ $version" ::
s"zincRoot/scalaVersion" ::
s"zincRoot/test" ::
s"zincRoot/scripted" ::
state
}
val dir = IO.createTemporaryDirectory
val dirPath = dir.getAbsolutePath
lazy val tearDownBenchmarkResources = taskKey[Unit]("Remove benchmark resources.")
tearDownBenchmarkResources in ThisBuild := { IO.delete(dir) }
addCommandAlias(
"runBenchmarks",
s""";zincBenchmarks/run $dirPath
|;zincBenchmarks/jmh:run -p _tempDir=$dirPath -prof gc
|;tearDownBenchmarkResources
""".stripMargin
)
lazy val otherRootSettings = Seq(
Scripted.scriptedBufferLog := true,
Scripted.scriptedPrescripted := { addSbtAlternateResolver _ },
Scripted.scripted := scriptedTask.evaluated,
Scripted.scriptedUnpublished := scriptedUnpublishedTask.evaluated,
Scripted.scriptedSource := (sourceDirectory in zinc).value / "sbt-test",
publishAll := {
val _ = (publishLocal).all(ScopeFilter(inAnyProject)).value
}
)
def scriptedTask: Def.Initialize[InputTask[Unit]] = Def.inputTask {
val result = scriptedSource(dir => (s: State) => scriptedParser(dir)).parsed
publishAll.value
// These two projects need to be visible in a repo even if the default
// local repository is hidden, so we publish them to an alternate location and add
// that alternate repo to the running scripted test (in Scripted.scriptedpreScripted).
(altLocalPublish in compilerInterface).value
(altLocalPublish in compilerBridge).value
doScripted(
(fullClasspath in zincScripted in Test).value,
(scalaInstance in zincScripted).value,
scriptedSource.value,
result,
scriptedBufferLog.value,
scriptedPrescripted.value
)
}
def addSbtAlternateResolver(scriptedRoot: File) = {
val resolver = scriptedRoot / "project" / "AddResolverPlugin.scala"
if (!resolver.exists) {
IO.write(
resolver,
s"""import sbt._
|import Keys._
|
|object AddResolverPlugin extends AutoPlugin {
| override def requires = sbt.plugins.JvmPlugin
| override def trigger = allRequirements
|
| override lazy val projectSettings = Seq(resolvers += alternativeLocalResolver)
| lazy val alternativeLocalResolver = Resolver.file("$altLocalRepoName", file("$altLocalRepoPath"))(Resolver.ivyStylePatterns)
|}
|""".stripMargin
)
}
}
def scriptedUnpublishedTask: Def.Initialize[InputTask[Unit]] = Def.inputTask {
val result = scriptedSource(dir => (s: State) => scriptedParser(dir)).parsed
doScripted(
(fullClasspath in zincScripted in Test).value,
(scalaInstance in zincScripted).value,
scriptedSource.value,
result,
scriptedBufferLog.value,
scriptedPrescripted.value
)
}
lazy val publishAll = TaskKey[Unit]("publish-all")
lazy val publishLauncher = TaskKey[Unit]("publish-launcher")
def customCommands: Seq[Setting[_]] = Seq(
commands += Command.command("release") { state =>
"clean" :: // This is required since version number is generated in properties file.
"+compile" ::
"+publishSigned" ::
"reload" ::
state
}
)