-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.sbt
526 lines (476 loc) · 14.8 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
import scala.scalanative.build.SourceLevelDebuggingConfig
import scala.scalanative.build.OptimizerConfig
import bindgen.plugin.BindgenMode
import com.indoorvivants.detective.Platform.OS.*
import com.indoorvivants.detective.Platform
import bindgen.interface.Binding
import bindgen.interface.LogLevel
import java.nio.file.Paths
Global / onChangedBuildSource := ReloadOnSourceChanges
ThisBuild / resolvers ++= Resolver.sonatypeOssRepos("snapshots")
lazy val Versions = new {
val Scala = "3.3.4"
}
lazy val root = project
.in(file("."))
.aggregate(
`tree-sitter`,
cjson,
cmark,
curl,
git,
duckdb,
git,
libuv,
lua,
openssl,
postgres,
mysql,
redis,
rocksdb,
sqlite,
s2n
)
.settings(
run := {}
)
// Example of Tree Sitter binding usage:
// https://tree-sitter.github.io/tree-sitter/using-parsers#the-basic-objects
lazy val `tree-sitter` = project
.in(file("example-tree-sitter"))
.enablePlugins(ScalaNativePlugin, BindgenPlugin)
.settings(
scalaVersion := Versions.Scala,
// Generate bindings to Tree Sitter's main API
bindgenBindings += {
Binding(
baseDirectory.value / "tree-sitter" / "lib" / "include" / "tree_sitter" / "api.h",
"treesitter"
)
.withCImports(
List("tree_sitter/api.h")
)
},
// Copy generated Scala parser
Compile / resourceGenerators += Def.task {
val scalaParserLocation =
baseDirectory.value / "tree-sitter-scala" / "src"
val resourcesFolder = (Compile / resourceManaged).value / "scala-native"
val fileNames = List("parser.c", "scanner.c", "stack.h")
fileNames.foreach { fileName =>
IO.copyFile(scalaParserLocation / fileName, resourcesFolder / fileName)
}
fileNames.map(fileName => resourcesFolder / fileName)
},
nativeConfig := {
val base = baseDirectory.value / "tree-sitter"
val conf = nativeConfig.value
val staticLib = base / "libtree-sitter.a"
conf
.withLinkingOptions(
conf.linkingOptions ++ List(
staticLib.toString
)
)
.withCompileOptions(
List(s"-I${base / "lib" / "include"}") ++ conf.compileOptions
)
}
)
.settings(bindgenSettings)
lazy val cjson = project
.in(file("example-cjson"))
.enablePlugins(ScalaNativePlugin, BindgenPlugin, VcpkgNativePlugin)
.settings(
scalaVersion := Versions.Scala,
vcpkgDependencies := VcpkgDependencies("cjson"),
vcpkgNativeConfig ~= { _.addRenamedLibrary("cjson", "libcjson") },
bindgenBindings += {
Binding(
vcpkgConfigurator.value.includes("cjson") / "cjson" / "cJSON.h",
"cjson"
)
.withCImports(
List("cJSON.h")
)
}
)
.settings(bindgenSettings)
.settings(configurePlatform())
lazy val curl = project
.in(file("example-curl"))
.enablePlugins(ScalaNativePlugin, BindgenPlugin, VcpkgNativePlugin)
.settings(
scalaVersion := Versions.Scala,
vcpkgDependencies := VcpkgDependencies("curl"),
vcpkgNativeConfig ~= { _.addRenamedLibrary("curl", "libcurl") },
bindgenBindings += {
Binding(
vcpkgConfigurator.value.includes("curl") / "curl" / "curl.h",
"curl"
)
.withCImports(
List("curl/curl.h")
)
}
)
.settings(bindgenSettings)
.settings(configurePlatform())
lazy val git = project
.in(file("example-git"))
.enablePlugins(ScalaNativePlugin, BindgenPlugin, VcpkgNativePlugin)
.settings(
scalaVersion := Versions.Scala,
vcpkgDependencies := VcpkgDependencies("libgit2"),
bindgenBindings += {
Binding(vcpkgConfigurator.value.includes("libgit2") / "git2.h", "libgit")
.withLinkName("git2")
.withCImports(List("git2.h"))
.withLogLevel(bindgen.interface.LogLevel.Info)
.withClangFlags(
vcpkgConfigurator.value.pkgConfig
.updateCompilationFlags(List("-fsigned-char"), "libgit2")
.toList
)
}
)
.settings(bindgenSettings)
.settings(configurePlatform())
lazy val postgres =
project
.in(file("example-postgres"))
.enablePlugins(ScalaNativePlugin, BindgenPlugin, VcpkgNativePlugin)
.settings(
scalaVersion := Versions.Scala,
vcpkgDependencies := VcpkgDependencies("libpq"),
bindgenBindings += {
Binding(
vcpkgConfigurator.value.includes("libpq") / "libpq-fe.h",
"libpq"
)
.withLinkName("pq")
.withCImports(List("libpq-fe.h"))
.withClangFlags(
vcpkgConfigurator.value.pkgConfig
.updateCompilationFlags(List("-std=gnu99"), "libpq")
.toList
)
}
)
.settings(bindgenSettings)
.settings(configurePlatform())
lazy val mysql =
project
.in(file("example-mysql"))
.enablePlugins(ScalaNativePlugin, BindgenPlugin, VcpkgNativePlugin)
.settings(
scalaVersion := Versions.Scala,
vcpkgDependencies := VcpkgDependencies("libmysql", "openssl", "zlib"),
// Mysql package in vcpkg is absolutely messed up
vcpkgNativeConfig ~= { _.addRenamedLibrary("libmysql", "mysqlclient") },
nativeConfig := {
val config = nativeConfig.value
config.withLinkingOptions(config.linkingOptions.flatMap {
case "-lresolv-lresolv" => Some("-lresolv")
case "-lm-lresolv" => None
case other => Some(other)
})
},
bindgenBindings += {
val actualIncludeFolder = new File(
vcpkgConfigurator.value.pkgConfig
.compilationFlags("mysqlclient")
.toList
.filter(_.contains("include/mysql"))
.head
.stripPrefix("-I")
)
Binding(actualIncludeFolder / "mysql.h", "libmysql")
.withLinkName("mysqlclient")
.withCImports(List("mysql/mysql.h"))
.withClangFlags(
vcpkgConfigurator.value.pkgConfig
.updateCompilationFlags(List("-std=gnu99"), "mysqlclient")
.toList
)
}
)
.settings(bindgenSettings)
.settings(configurePlatform())
lazy val sqlite =
project
.in(file("example-sqlite"))
.enablePlugins(ScalaNativePlugin, BindgenPlugin, VcpkgNativePlugin)
.settings(
scalaVersion := Versions.Scala,
vcpkgDependencies := VcpkgDependencies("sqlite3"),
bindgenBindings += {
Binding(
vcpkgConfigurator.value.includes("sqlite3") / "sqlite3.h",
"libsqlite"
)
.withCImports(List("sqlite.h"))
.withClangFlags(List("-fsigned-char"))
}
)
.settings(bindgenSettings)
.settings(configurePlatform())
lazy val redis =
project
.in(file("example-redis"))
.enablePlugins(ScalaNativePlugin, BindgenPlugin, VcpkgNativePlugin)
.settings(
scalaVersion := Versions.Scala,
vcpkgDependencies := VcpkgDependencies("hiredis"),
bindgenBindings += {
Binding(
vcpkgConfigurator.value.includes("hiredis") / "hiredis" / "hiredis.h",
"libredis"
)
.withCImports(List("hiredis.h"))
.withClangFlags(List("-fsigned-char"))
}
)
.settings(bindgenSettings)
.settings(configurePlatform())
lazy val cmark = project
.in(file("example-cmark"))
.enablePlugins(ScalaNativePlugin, BindgenPlugin, VcpkgNativePlugin)
.settings(
scalaVersion := Versions.Scala,
vcpkgDependencies := VcpkgDependencies("cmark"),
vcpkgNativeConfig ~= { _.addRenamedLibrary("cmark", "libcmark") },
bindgenBindings += {
Binding(vcpkgConfigurator.value.includes("cmark") / "cmark.h", "cmark")
.withCImports(List("cmark.h"))
.withClangFlags(
vcpkgConfigurator.value.pkgConfig
.updateCompilationFlags(List("-DCMARK_STATIC_DEFINE="), "libcmark")
.toList
)
}
)
.settings(bindgenSettings)
.settings(configurePlatform())
lazy val rocksdb = project
.in(file("example-rocksdb"))
.enablePlugins(ScalaNativePlugin, BindgenPlugin, VcpkgNativePlugin)
.settings(
scalaVersion := Versions.Scala,
vcpkgDependencies := VcpkgDependencies("rocksdb", "zlib"),
bindgenBindings += {
Binding(
vcpkgConfigurator.value.includes("rocksdb") / "rocksdb" / "c.h",
"rocksdb"
)
.withCImports(List("rocksdb/c.h"))
.withClangFlags(
List("-I" + vcpkgConfigurator.value.includes("rocksdb"))
)
}
)
.settings(bindgenSettings)
.settings(configurePlatform())
lazy val s2n = project
.in(file("example-s2n"))
.enablePlugins(ScalaNativePlugin, BindgenPlugin, VcpkgNativePlugin)
.settings(
scalaVersion := Versions.Scala,
vcpkgDependencies := VcpkgDependencies("s2n", "openssl"),
Compile / run / envVars := Map("S2N_DONT_MLOCK" -> "1"),
bindgenBindings += {
Binding(vcpkgConfigurator.value.includes("s2n") / "s2n.h", "s2n")
.withCImports(List("s2n.h"))
.withClangFlags(List("-I" + vcpkgConfigurator.value.includes("s2n")))
}
)
.settings(bindgenSettings)
.settings(configurePlatform())
val bindgenSettings = Seq(
bindgenMode := BindgenMode.Manual(
scalaDir = (Compile / sourceDirectory).value / "scala" / "generated",
cDir = (Compile / resourceDirectory).value / "scala-native" / "generated"
),
bindgenBindings := {
bindgenBindings.value.map(_.withNoLocation(true))
}
)
def configurePlatform(rename: String => String = identity) = Seq(
nativeConfig := {
val conf = nativeConfig.value
val arch64 =
if (
Platform.arch == Platform.Arch.Arm && Platform.bits == Platform.Bits.x64
)
List("-arch", "arm64")
else Nil
conf
.withLinkingOptions(
conf.linkingOptions ++ arch64
)
.withCompileOptions(
conf.compileOptions ++ arch64
)
}
)
lazy val duckdb = project
.in(file("example-duckdb"))
.enablePlugins(ScalaNativePlugin, BindgenPlugin)
.settings(
scalaVersion := Versions.Scala,
Compile / run / envVars := Map(
"LD_LIBRARY_PATH" -> (baseDirectory.value / "duckdb" / "build" / "debug" / "src").toString,
"DYLD_LIBRARY_PATH" -> (baseDirectory.value / "duckdb" / "build" / "debug" / "src").toString
),
// Generate bindings to Tree Sitter's main API
bindgenBindings += {
val bd = (baseDirectory.value / "duckdb" / "src" / "include")
Binding(
baseDirectory.value / "duckdb" / "src" / "include" / "duckdb.h",
"duckdb"
)
.withLinkName("duckdb")
.withCImports(List("duckdb.h"))
.withClangFlags(List(s"-I$bd", "-fsigned-char"))
},
nativeConfig := {
val base = baseDirectory.value / "duckdb"
val libFolder = base / "build" / "debug" / "src"
val headersFolder = base / "src" / "include"
val conf = nativeConfig.value
conf
.withLinkingOptions(
conf.linkingOptions ++ List(
"-lduckdb",
s"-L$libFolder"
)
)
.withCompileOptions(
conf.compileOptions ++ List(s"-I$headersFolder")
)
}
)
.settings(bindgenSettings)
lazy val libuv = project
.in(file("example-libuv"))
.enablePlugins(ScalaNativePlugin, BindgenPlugin, VcpkgNativePlugin)
.settings(
scalaVersion := Versions.Scala,
vcpkgDependencies := VcpkgDependencies("libuv"),
bindgenBindings := {
Seq(
Binding(vcpkgConfigurator.value.includes("libuv") / "uv.h", "libuv")
.withCImports(List("uv.h"))
.withClangFlags(
List(
"-I" + vcpkgConfigurator.value.includes("libuv").toString
)
)
)
}
)
.settings(bindgenSettings)
.settings(configurePlatform())
lazy val lua = project
.in(file("example-lua"))
.enablePlugins(ScalaNativePlugin, BindgenPlugin, VcpkgNativePlugin)
.settings(
vcpkgDependencies := VcpkgDependencies("lua"),
scalaVersion := Versions.Scala,
bindgenBindings := {
Seq(
Binding((Compile / baseDirectory).value / "lua-amalgam.h", "lua")
.withCImports(List("lua.h", "lauxlib.h", "lualib.h"))
.withClangFlags(
List(
"-I" + vcpkgConfigurator.value.includes("lua").toString
)
)
)
}
)
.settings(bindgenSettings)
.settings(configurePlatform())
lazy val openssl = project
.in(file("example-openssl"))
.enablePlugins(ScalaNativePlugin, BindgenPlugin, VcpkgNativePlugin)
.settings(
vcpkgDependencies := VcpkgDependencies("openssl"),
scalaVersion := Versions.Scala,
nativeConfig ~= {
(_).withSourceLevelDebuggingConfig(SourceLevelDebuggingConfig.enabled)
},
bindgenBindings := {
Seq(
Binding(
(Compile / baseDirectory).value / "openssl-amalgam.h",
"openssl"
)
.withCImports(
List("openssl/sha.h", "openssl/evp.h")
)
.withClangFlags(
List("-I" + vcpkgConfigurator.value.includes("openssl"))
)
)
}
)
.settings(bindgenSettings)
.settings(configurePlatform())
def getProjects(s: State): Seq[String] = {
val extracted = Project.extract(s)
val currentBuildUri = extracted.currentRef.build
val buildStructure = extracted.structure
val buildUnitsMap = buildStructure.units
val currentBuildUnit = buildUnitsMap(currentBuildUri)
val projectsMap = currentBuildUnit.defined
projectsMap.values.map(_.id).toVector
}
def balancedPartitioner[A](strings: Vector[A], jobs: Int): Int => Vector[A] = {
import scala.collection.mutable.ListBuffer
val parts = Vector.fill(jobs)(ListBuffer.empty[A])
var curIdx = 0
val maxSize = strings.size / jobs
strings.zipWithIndex.foreach { case (el, idx) =>
if (parts(curIdx).size >= maxSize) {
curIdx += 1
}
if (curIdx >= jobs) curIdx = 0
parts(curIdx).+=(el)
}
jobId => parts.map(_.toVector).apply(jobId - 1)
}
import complete.DefaultParsers.*
def projectCommands(st: State) = {
val exceptions: Set[String] =
if (sys.env.contains("CI")) {
val platformSpecific = Platform.os match {
// these require docker containers so we don't run them on CI
case MacOS => Set("mysql", "postgres", "redis")
case _ => Set.empty
}
platformSpecific ++ Set("duckdb", "rocksdb")
} else Set.empty
getProjects(st).sorted.reverse
.filterNot(exceptions.contains)
.flatMap { projectName =>
List(s"$projectName/run", s"show $projectName/nativeConfig")
}
}
ThisBuild / commands += Command.arb { s =>
token(
literal("runBatchedExamples") <~ Space
) ~ (IntBasic <~ Space) ~ (IntBasic)
} { case (st, t) =>
val ((_, jobId), totalJobs) = t
val commands = projectCommands(st)
val partition = balancedPartitioner(commands.toVector, totalJobs)(jobId)
partition.foldLeft(st) { case (s, n) =>
n :: s
}
}
ThisBuild / commands += Command.command("runExamples") { st =>
projectCommands(st).foldLeft(st) { case (s, n) =>
n :: s
}
}