-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.sbt
More file actions
263 lines (234 loc) · 9.43 KB
/
build.sbt
File metadata and controls
263 lines (234 loc) · 9.43 KB
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
import java.io.FileInputStream
import java.nio.file.{Files, Path}
import scala.jdk.CollectionConverters.*
val PROTOBUF_HOME = sys.env.getOrElse("PROTOBUF_HOME", s"${sys.props("user.home")}/protobuf")
val protobufVersion = "4.29.0-RC2"
val javaVersion = sys.props("java.specification.version").toInt
val release = if(javaVersion >= 22) 22 else javaVersion
val runtimeOpts = Seq(
"--add-modules", "jdk.incubator.vector",
"--add-opens", "java.base/jdk.internal.misc=ALL-UNNAMED",
"--add-opens", "java.base/java.lang=ALL-UNNAMED",
) ++ (if(release >= 22) Nil else Seq(
"--enable-preview",
))
val compileOpts = Seq(
"--add-modules", "jdk.incubator.vector",
) ++ (if(release >= 22) Seq(
"--release", release.toString,
) else Seq(
"--release", javaVersion.toString,
"--enable-preview",
))
// External jar to use instead of `core` for tests and benchmarks
val overrideTestJar = sys.env.get("OVERRIDE_TEST_JAR")
javaOptions in Global ++= runtimeOpts
javacOptions in Global ++= compileOpts
scalacOptions in Global ++= Seq("-java-output-version", release.toString)
// javaOptions in Global += "-Djmh.blackhole.autoDetect=false"
scalacOptions ++= Seq("-feature")
val baseVersion = "0.1.0"
val releaseVersion: String = {
import scala.sys.process._
lazy val hash = "git rev-parse HEAD".!!.substring(0, 10)
sys.env.get("GITHUB_REF") match {
case Some(ref) if ref.startsWith("refs/tags/v") && ref.length >= 14 => ref.substring(11)
case Some(_) => s"$baseVersion-$hash"
case None => s"$baseVersion-$hash-SNAPSHOT"
}
}
ThisBuild / publishTo := sonatypePublishToBundle.value
ThisBuild / sonatypeCredentialHost := xerial.sbt.Sonatype.sonatypeCentralHost
ThisBuild / credentials +=
Credentials("Sonatype Nexus Repository Manager", (ThisBuild / sonatypeCredentialHost).value,
sys.env.getOrElse("SONATYPE_USER", ""), sys.env.getOrElse("SONATYPE_PASSWORD", ""))
ThisBuild / versionScheme := Some("semver-spec")
ThisBuild / version := releaseVersion
ThisBuild / organization := "com.novocode"
ThisBuild / licenses += ("Apache-2.0", url("https://www.apache.org/licenses/LICENSE-2.0.html"))
ThisBuild / homepage := Some(url("http://github.com/szeiger/perfio/"))
ThisBuild / scmInfo := Some(ScmInfo(url("https://github.com/szeiger/perfio"), "scm:git@github.com:szeiger/perfio.git"))
ThisBuild / developers := List(Developer("szeiger", "Stefan Zeiger", "szeiger@novocode.com", url("http://szeiger.de")))
ThisBuild / Test / fork := true
ThisBuild / run / fork := true
ThisBuild / run / connectInput := true
Global / scalaVersion := "3.3.6"
val hedgehogVersion = "0.10.1"
lazy val root = (project in file("."))
.aggregate(LocalProject("core"), LocalProject("benchUtil"), LocalProject("bench"), LocalProject("test"), LocalProject("scalaApi"))
.settings(
crossPaths := false,
autoScalaLibrary := false,
name := "perfio-root",
publish / skip := true,
)
lazy val core = (project in file("core"))
.settings(
crossPaths := false,
autoScalaLibrary := false,
name := "perfio",
// When building the Java 21 version, add multi-release overrides for preview API classes
// by copying them from a previously built Java 22 output directory
Compile / packageBin / mappings := {
val orig = (Compile / packageBin / mappings).value
sys.env.get("OVERRIDE_CORE_J22") match {
case Some(j22classes) =>
println("Building multi-release jar")
val overrides = orig.collect {
case (f, s) if s.endsWith(".class") && isPreview(f) =>
val nf = new File(j22classes, s)
assert(nf.exists())
val ns = "META-INF/versions/22/" + s
println(" Adding override "+ns)
(nf, ns)
}
orig ++ overrides
case None => orig
}
},
(Compile / packageBin) / packageOptions +=
Package.ManifestAttributes("Multi-Release" -> "true"),
)
lazy val scalaApi = (project in file("scalaapi"))
.dependsOn(
(overrideTestJar match {
case Some(_) => Nil
case None => Seq(core: ClasspathDep[ProjectReference])
}): _*
)
.settings(
name := "perfio-scala",
publish / skip := true,
(Compile / unmanagedJars) ++= {
overrideTestJar match {
case Some(j) =>
println("Using override test jar "+j)
Seq(file(j))
case None => Nil
}
},
)
lazy val benchUtil = (project in file("bench-util"))
.dependsOn(scalaApi)
.enablePlugins(JmhPlugin)
.settings(
libraryDependencies ++= Seq(
"com.google.guava" % "guava" % "33.3.0-jre",
"com.esotericsoftware" % "kryo" % "5.6.2",
),
name := "perfio-bench-util",
publish / skip := true,
)
lazy val bench = (project in file("bench"))
.dependsOn(benchUtil)
.enablePlugins(JmhPlugin)
.settings(
name := "perfio-bench",
publish / skip := true,
)
lazy val test_ = Project("test", file("test"))
.dependsOn(scalaApi)
.settings(
libraryDependencies += "com.github.sbt" % "junit-interface" % "0.13.2" % "test",
testOptions += Tests.Argument(TestFrameworks.JUnit, "-a", "-v"),
libraryDependencies ++= Seq(
"qa.hedgehog" %% "hedgehog-core" % hedgehogVersion % "test",
"qa.hedgehog" %% "hedgehog-runner" % hedgehogVersion % "test",
"qa.hedgehog" %% "hedgehog-sbt" % hedgehogVersion % "test",
),
testOptions += Tests.Argument(TestFrameworks.JUnit, "-a", "-v"),
testFrameworks += TestFramework("hedgehog.sbt.Framework"),
name := "perfio-test",
publish / skip := true,
javaOptions += "-ea",
)
lazy val protoRuntime = (project in file("proto-runtime"))
.dependsOn(core)
.settings(
name := "perfio-proto-runtime",
publish / skip := true,
)
lazy val proto = (project in file("proto"))
.dependsOn(protoRuntime, scalaApi)
.settings(
libraryDependencies += "com.google.protobuf" % "protobuf-java" % "4.29.0-RC3" % "test",
libraryDependencies += "com.github.sbt" % "junit-interface" % "0.13.2" % "test",
testOptions += Tests.Argument(TestFrameworks.JUnit, "-a", "-v"),
name := "perfio-proto",
Test / sourceGenerators += Def.task {
val srcDir = new File((Test / sourceDirectory).value, "proto")
val srcs = srcDir.listFiles(_.getName.endsWith(".proto")).toSet
val out = (Test / sourceManaged).value
val cp = (Compile / fullClasspathAsJars).value
val cachedCompile = FileFunction.cached(streams.value.cacheDirectory / "proto") { (in: Set[File]) =>
val pin = in.intersect(srcs)
val outs = runProtoc(cp, pin.map(_.getPath), out.toPath, Seq("java", "perfio"), Some("com.example.perfio"), Some(srcDir.getPath), true).toSet
println("Generated "+outs.mkString(", "))
outs
}
val outs = cachedCompile(srcs ++ cp.iterator.map(_.data).toSet).toSeq.sorted
outs
}.taskValue,
publish / skip := true,
javaOptions += "-ea",
)
lazy val protoBench = (project in file("proto-bench"))
.dependsOn(proto)
.enablePlugins(JmhPlugin)
.settings(
scalacOptions ++= Seq("-feature", "-opt:l:inline"),
libraryDependencies += "com.google.protobuf" % "protobuf-java" % protobufVersion,
name := "perfio-proto-bench",
publish / skip := true,
)
lazy val bootstrapProto = taskKey[Unit]("Boostrap the perfIO-generated protobuf API")
bootstrapProto := {
val cp = (proto / Compile / fullClasspath).value
val srcs = Seq(
s"$PROTOBUF_HOME/include/google/protobuf/compiler/plugin.proto",
s"$PROTOBUF_HOME/include/google/protobuf/descriptor.proto",
)
val target = Path.of("proto/src/main/java")
runProtoc(cp, srcs, target, Seq("perfio"), Some("perfio.protoapi"), None, false)
}
def runProtoc(cp: Classpath, srcs: Iterable[String], target: Path, modes: Seq[String], perfioPackage: Option[String], protoPath: Option[String], isTest: Boolean): Seq[File] = {
val gen = new File("proto/protoc-gen-perfio").getAbsolutePath
val protoc = s"$PROTOBUF_HOME/bin/protoc"
if(target.toFile.exists()) IO.delete(target.toFile.listFiles())
else Files.createDirectories(target)
val temp = Files.createTempDirectory("protoc-out")
try {
modes.foreach { mode =>
val pb = new ProcessBuilder(Seq(protoc, s"--plugin=protoc-gen-perfio=$gen", s"--${mode}_out=$temp") ++ protoPath.map(s => s"--proto_path=$s") ++ srcs: _*).inheritIO()
pb.environment().put("PERFIO_CLASSPATH", cp.iterator.map(_.data).mkString(sys.props.getOrElse("path.separator", ":")))
perfioPackage.foreach { p => pb.environment().put("PERFIO_PACKAGE", p) }
if(isTest) pb.environment().put("PERFIO_TEST", "true")
val ret = pb.start().waitFor()
if(ret != 0) throw new RuntimeException(s"protoc failed with exit code $ret")
}
val tempFiles = Files.walk(temp).iterator().asScala.toVector
val tempOuts = tempFiles.filter(p => p.toFile.getName.endsWith(".java"))
val outs = tempOuts.map { t =>
val o = target.resolve(temp.relativize(t))
Files.createDirectories(o.getParent)
Files.move(t, o)
o.toAbsolutePath.toFile
}
outs
} finally IO.delete(temp.toFile)
}
val javap = inputKey[Unit]("Run javap")
test_ / Test / javap := {
import complete.DefaultParsers._
val args: Seq[String] = spaceDelimited("<arg>").parsed
val cp = (test_ / Test / fullClasspath).value
val pb = new ProcessBuilder(Seq("javap", "-cp", cp.iterator.map(_.data).mkString(sys.props.getOrElse("path.separator", ":"))) ++ args: _*).inheritIO()
pb.start().waitFor()
}
def isPreview(classFile: File): Boolean = {
val in = new FileInputStream(classFile)
try {
val a = in.readNBytes(5)
a(4) != 0
} finally in.close()
}