-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathbuild.mill
More file actions
471 lines (425 loc) · 15.5 KB
/
build.mill
File metadata and controls
471 lines (425 loc) · 15.5 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
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
//| mill-version: 1.1.2
//| mill-jvm-version: zulu:21
//| mvnDeps:
//| - com.lihaoyi::mill-contrib-jmh:$MILL_VERSION
//| - com.lihaoyi::mill-contrib-versionfile:$MILL_VERSION
package build
import mill._
import scalalib._
import publish._
import scalajslib._
import scalanativelib._
import scalanativelib.api._
import scalajslib.api._
import scalafmt._
import javalib.api.JvmWorkerUtil
import javalib.NativeImageModule
import contrib.versionfile.VersionFileModule
import contrib.jmh.JmhModule
import mill.util.Jvm
import mill.api.BuildCtx
import java.util.Base64
val scalaVersions: Seq[String] = Seq("3.3.7", "2.13.18")
val jvmScalaVersions: Seq[String] = scalaVersions ++ Seq("2.12.21")
val stackSize = "100m"
val stackSizekBytes = 100 * 1024
trait SjsonnetPublishModule extends PublishModule {
def artifactName = "sjsonnet"
def publishVersion = sjsonnet.currentVersion().toString
def pomSettings = PomSettings(
description = artifactName(),
organization = "com.databricks",
url = "https://github.com/databricks/sjsonnet",
licenses = Seq(License.Common.Apache2),
versionControl = VersionControl.github("databricks", "sjsonnet"),
developers = Seq(
Developer(
name = "Databricks Jsonnet Team",
organization = Some("Databricks"),
organizationUrl = Some("https://www.databricks.com")
)
)
)
}
trait SjsonnetCrossModule extends CrossScalaModule with ScalafmtModule {
def crossValue: String
def mvnDeps = Seq(
mvn"com.lihaoyi::fastparse::3.1.1",
mvn"com.lihaoyi::pprint::0.9.6",
mvn"com.lihaoyi::ujson::4.4.2",
mvn"com.lihaoyi::scalatags::0.13.1",
mvn"org.scala-lang.modules::scala-collection-compat::2.14.0"
)
def manifest = super
.manifest()
.add(
"Enable-Native-Access" -> "ALL-UNNAMED"
)
def generatedSources = Task {
os.write(
Task.ctx().dest / "Version.scala",
s"""package sjsonnet
|object Version{
| val version = ${pprint.Util.literalize(sjsonnet.currentVersion().toString)}
|}
|""".stripMargin
)
Seq(PathRef(Task.ctx().dest / "Version.scala"))
}
def scalacOptions = Seq("-deprecation", "-Werror") ++ (
if (!JvmWorkerUtil.isScala3(scalaVersion()))
Seq(
"-opt:l:inline",
"-feature",
"-opt-inline-from:sjsonnet.*,sjsonnet.**",
"-Xsource:3",
"-Xlint:_"
) ++ (if (scalaVersion().startsWith("2.13"))
Seq("-Wopt", "-Wconf:origin=scala.collection.compat.*:s")
else Seq("-Xfatal-warnings", "-Ywarn-unused:-nowarn"))
else Seq[String]("-Wconf:origin=scala.collection.compat.*:s", "-Xlint:all")
)
trait CrossTests extends ScalaModule with TestModule.Utest {
def mvnDeps = Seq(mvn"com.lihaoyi::utest::0.9.5")
def testSandboxWorkingDir = false
}
}
trait SjsonnetJvmNative extends ScalaModule {
def mvnDeps = super.mvnDeps() ++ Seq(
mvn"com.lihaoyi::os-lib::0.11.8",
mvn"com.lihaoyi::mainargs::0.7.8"
)
}
object bench extends ScalaModule with JmhModule with ScalafmtModule {
def forkArgs = Seq("-Xss" + stackSize, "--enable-native-access=ALL-UNNAMED")
def scalaVersion = scalaVersions.head
def moduleDeps = Seq(sjsonnet.jvm.crossModules.head)
def jmhCoreVersion = "1.37"
def scalacOptions = sjsonnet.jvm.crossModules.head.scalacOptions
def resources = Task.Sources(
this.moduleDir / "resources" / "bug_suite",
this.moduleDir / "resources" / "cpp_suite",
this.moduleDir / "resources" / "go_suite",
this.moduleDir / "resources" / "sjsonnet_suite"
)
def listRegressions = Task {
resources()
.map(_.path)
.flatMap(
os.walk(_)
.filter(os.isFile)
.filter(_.ext == "jsonnet")
)
.sortBy(_.toString)
.map(_.relativeTo(BuildCtx.workspaceRoot).toString)
}
def runRegressions(args: String*) = Task.Command {
val (_, resources) = generateBenchmarkSources()
val regressionCases = if args.isEmpty then listRegressions() else args.toSeq
val forkedArgs = forkArgs().toSeq
Jvm.callProcess(
mainClass = "org.openjdk.jmh.Main",
classPath = (runClasspath() ++ generatorDeps()).map(_.path) ++
Seq(jmhGeneratedSources().path, resources.path),
mainArgs = Array(
"sjsonnet.bench.RegressionBenchmark.main",
s"-p=path=${regressionCases.mkString(",")}"
),
cwd = Task.ctx().dest,
javaHome = javaHome().map(_.path),
jvmArgs = forkedArgs,
stdin = os.Inherit,
stdout = os.Inherit
)
()
}
}
object sjsonnet extends VersionFileModule {
trait SjsonnetScalaJSModule
extends SjsonnetCrossModule
with ScalaJSModule
with SjsonnetPublishModule {
def moduleDir = super.moduleDir / os.up
def scalaJSVersion = "1.20.2"
val sourceDirs = Seq(
"src",
"src-js",
"src-jvm-js"
)
def sources = Task.Sources(sourceDirs.map(d => this.moduleDir / d)*)
def mvnDeps = super.mvnDeps() ++
Seq(
mvn"org.virtuslab::scala-yaml::0.3.1"
)
def nodeJsArgs: List[String] = List("--stack-size=" + stackSizekBytes)
trait SjsonnetScalaJSTests extends ScalaJSTests with CrossTests {
def jsEnvConfig = JsEnvConfig.NodeJs(args = nodeJsArgs)
def resources = Task.Sources(
this.moduleDir / "resources" / "test_suite",
this.moduleDir / "resources" / "go_test_suite",
this.moduleDir / "resources" / "new_test_suite"
)
def sources = Task.Sources(sourceDirs.map(d => this.moduleDir / d)*)
def generatedSources = Task {
resources().map(_.path).flatMap { testSuite =>
val fileNames = os
.walk(testSuite)
.filter(os.isFile)
.map { k =>
val name = k
.relativeTo(testSuite)
.toString
.replaceAll("/", "_")
.replaceAll("\\.", "_")
.replaceAll("-", "_")
val values = Base64.getEncoder.encodeToString(os.read.bytes(k)).grouped(65535).toSeq
val fName = Task.ctx().dest / s"${testSuite.last}_$name.scala"
os.write.over(
fName,
s"""package sjsonnet
|
|import java.util.Base64
|
|object ${testSuite.last}_$name {
| def contentArr = Seq(
| ${values.map("\"" + _ + "\"").mkString(",\n ")}
| )
| def content() = Base64.getDecoder().decode(contentArr.mkString)
|}
|""".stripMargin
)
k -> fName
}
.toMap
val testSuiteClassPath = Task.ctx().dest / s"TestResources_${testSuite.last}.scala"
os.write.over(
testSuiteClassPath,
s"""package sjsonnet
|
|object TestResources_${testSuite.last}{
| val files: Map[String, () => Array[Byte]] = Map(
|""".stripMargin
)
for ((k, fName) <- fileNames) {
os.write.append(
testSuiteClassPath,
s""" "${k.relativeTo(testSuite)}" -> ${fName.baseName}.content _,
|""".stripMargin
)
}
os.write.append(
testSuiteClassPath,
s""" )
|}
|""".stripMargin
)
Seq(PathRef(testSuiteClassPath)) ++ fileNames.values.map(k => PathRef(k))
}
}
}
}
object js extends Cross[SjsonnetJsModule](scalaVersions)
trait SjsonnetJsModule extends SjsonnetScalaJSModule {
def moduleKind = Task { ModuleKind.CommonJSModule }
object test extends SjsonnetScalaJSTests
}
object wasm extends Cross[SjsonnetWasmModule](scalaVersions)
trait SjsonnetWasmModule extends SjsonnetScalaJSModule {
def moduleKind = Task { ModuleKind.ESModule }
def moduleSplitStyle = ModuleSplitStyle.FewestModules
def scalaJSExperimentalUseWebAssembly = true
override def nodeJsArgs = super.nodeJsArgs ++ List("--experimental-wasm-exnref")
object test extends SjsonnetScalaJSTests
}
object native extends Cross[SjsonnetNativeModule](scalaVersions)
trait SjsonnetNativeModule
extends SjsonnetCrossModule
with ScalaNativeModule
with SjsonnetPublishModule
with SjsonnetJvmNative {
def moduleDir = super.moduleDir / os.up
def scalaNativeVersion = "0.5.10"
val sourceDirs = Seq(
"src",
"src-native",
"src-jvm-native"
)
def sources = Task.Sources(sourceDirs.map(d => this.moduleDir / d)*)
def mvnDeps = super.mvnDeps() ++
Seq(
mvn"com.github.lolgab::scala-native-crypto::0.3.0",
mvn"org.virtuslab::scala-yaml::0.3.1"
)
def releaseMode = ReleaseMode.ReleaseFull
def nativeLTO = LTO.Full
def nativeMultithreading = None
object test extends ScalaNativeTests with CrossTests {
def releaseMode = ReleaseMode.Debug
def nativeMultithreading = None
def sources = Task.Sources(sourceDirs.map(d => this.moduleDir / d)*)
def forkEnv = super.forkEnv() ++ Map(
"SCALANATIVE_THREAD_STACK_SIZE" -> stackSize
)
def nativeLTO = LTO.None
}
}
object jvm extends Cross[SjsonnnetJvmModule](jvmScalaVersions)
trait SjsonnnetJvmModule
extends SjsonnetCrossModule
with ScalaModule
with SjsonnetPublishModule
with SjsonnetJvmNative {
def moduleDir = super.moduleDir / os.up
def mainClass = Some("sjsonnet.SjsonnetMain")
val sourceDirs = Seq(
"src",
"src-jvm",
"src-jvm-native"
)
def sources = Task.Sources(sourceDirs.map(d => this.moduleDir / d)*)
def forkArgs =
Seq("-Xss" + stackSize, "--enable-native-access=ALL-UNNAMED")
def mvnDeps = super.mvnDeps() ++ Seq(
mvn"org.tukaani:xz::1.11",
mvn"at.yawk.lz4:lz4-java::1.10.3",
mvn"org.yaml:snakeyaml::2.6",
mvn"com.google.re2j:re2j:1.8"
)
object test extends ScalaTests with CrossTests {
def forkArgs = Seq("-Xss" + stackSize, "--enable-native-access=ALL-UNNAMED")
def sources = Task.Sources(sourceDirs.map(d => this.moduleDir / d)*)
}
object client extends JavaModule with SjsonnetPublishModule {
def artifactName = "sjsonnet-server"
def mvnDeps = Seq(
mvn"org.scala-sbt.ipcsocket:ipcsocket:1.6.3".exclude(
"net.java.dev.jna" -> "jna",
"net.java.dev.jna" -> "jna-platform"
)
)
object test extends JavaTests with TestModule.Junit4
}
object server extends ScalaModule with SjsonnetPublishModule {
def artifactName = "sjsonnet-server"
def scalaVersion = SjsonnnetJvmModule.this.crossValue
def moduleDeps = Seq(SjsonnnetJvmModule.this, client)
def mvnDeps = Seq(
mvn"org.scala-sbt.ipcsocket:ipcsocket:1.6.3".exclude(
"net.java.dev.jna" -> "jna",
"net.java.dev.jna" -> "jna-platform"
),
mvn"net.java.dev.jna:jna:5.17.0",
mvn"net.java.dev.jna:jna-platform:5.17.0"
)
override def prependShellScript = mill.util.Jvm.universalScript(
shellCommands = {
def java(mainClass: String) =
s"""exec java -DSJSONNET_EXECUTABLE=$$0 -DSJSONNET_VERSION=${sjsonnet
.currentVersion()
.toString} $$JAVA_OPTS -cp $$0 $mainClass "$$@""""
s"""case "$$1" in
| -i | --interactive )
| ${java("sjsonnet.SjsonnetMain")}
| ;;
| *)
| ${java("sjsonnet.client.SjsonnetClientMain")}
| ;;
|esac""".stripMargin
},
cmdCommands = {
def java(mainClass: String) =
s"""java -DSJSONNET_EXECUTABLE=%~dpnx0 -DSJSONNET_VERSION=${sjsonnet
.currentVersion()
.toString} %JAVA_OPTS% -cp %~dpnx0 $mainClass %*"""
s"""if "%1" == "-i" set _I_=true
|if "%1" == "--interactive" set _I_=true
|if defined _I_ (
| ${java("sjsonnet.SjsonnetMain")}
|) else (
| ${java("sjsonnet.client.SjsonnetClientMain")}
|)""".stripMargin
}
)
object test extends ScalaTests with TestModule.Junit4
}
}
object graal extends NativeImageModule {
def finalMainClass = jvm(scalaVersions(0)).mainClass().get
def runClasspath = Seq()
def jvmId = "graalvm-community:25.0.1"
def jvmIndexVersion = "0.0.4-128-8f0ad3"
def localBinName = "sjsonnet-native"
def nativeImageClasspath = Seq(jvm(scalaVersions(0)).assembly())
// Override to skip metadata repository configuration which fails on JDK 25
override def nativeMetadataConfigurations = Task {
Set.empty[mill.javalib.graalvm.MetadataResult]
}
def nativeImageOptions = super.nativeImageOptions() ++ Seq(
"--no-fallback",
"-O3",
"--static-nolibc",
"-R:MaxHeapSize=2G",
"--initialize-at-build-time",
"--initialize-at-run-time=os.package$",
"-H:+ReportExceptionStackTraces"
)
}
}
object playground extends Module {
def playgroundHtml = Task.Source(BuildCtx.workspaceRoot / "playground" / "index.html")
/**
* Compile ScalaJS and inline the resulting JS bundle into a single HTML file. Note: the HTML
* template (playground/index.html) may still reference external assets (e.g. CSS/JS from CDNs),
* so the output is not guaranteed to be fully self-contained or offline-capable.
*/
def bundle = Task {
val jsOutput = sjsonnet.js(scalaVersions.head).fullLinkJS()
val mainJs = jsOutput.dest.path / "main.js"
val dest = Task.ctx().dest
// Wrap CommonJS module for browser usage
val source = os.read(mainJs)
val wrappedJs =
s"""// sjsonnet browser bundle - auto-generated
|(function(global) {
| var exports = {};
| var module = { exports: exports };
|
| $source
|
| global.SjsonnetMain = exports.SjsonnetMain || module.exports.SjsonnetMain;
|})(typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : Function('return this')());""".stripMargin
// Read HTML template and inline the JS bundle
val htmlTemplate = os.read(playgroundHtml().path)
val placeholder = "/* SJSONNET_BUNDLE_PLACEHOLDER */"
val occurrences =
java.util.regex.Pattern.quote(placeholder).r.findAllMatchIn(htmlTemplate).length
require(
occurrences == 1,
s"Expected exactly 1 occurrence of '$placeholder' in playground/index.html, found $occurrences"
)
val inlinedHtml = htmlTemplate.replace(placeholder, wrappedJs)
os.write(dest / "index.html", inlinedHtml)
PathRef(dest)
}
/**
* Build and open the playground in the default browser.
*
* Since the HTML is self-contained (JS inlined), no HTTP server is needed. The file is opened
* directly in the default browser.
*/
def start() = Task.Command {
val htmlFile = bundle().path / "index.html"
val fileUri = htmlFile.toNIO.toUri.toString
println(s"Opening Sjsonnet Playground: $fileUri")
val osName = System.getProperty("os.name", "").toLowerCase
val openCmd =
if (osName.contains("mac") || osName.contains("darwin")) Seq("open", fileUri)
else if (osName.contains("win")) Seq("cmd", "/c", "start", "", fileUri)
else Seq("xdg-open", fileUri) // Linux / BSD
val opened = scala.util.Try(os.proc(openCmd).call(stdout = os.Inherit, stderr = os.Inherit))
if (opened.isFailure) {
println(s"Could not open browser automatically. Please open this file manually:")
println(s" $htmlFile")
}
}
}