-
Notifications
You must be signed in to change notification settings - Fork 650
Expand file tree
/
Copy pathbuild.mill
More file actions
417 lines (359 loc) · 15.4 KB
/
build.mill
File metadata and controls
417 lines (359 loc) · 15.4 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
//| mill-version: 1.1.1-6-480a23
//| mvnDeps:
//| - com.lihaoyi::mill-contrib-jmh:$MILL_VERSION
//| - com.47deg::github4s:0.33.3
//| - com.lumidion::sonatype-central-client-requests:0.6.0
//| - com.github.lolgab::mill-mima::0.2.0
package build
import mill._
import mill.api.{BuildCtx, Result}
import mill.scalalib._
import mill.scalalib.scalafmt._
import mainargs.arg
object v extends Module {
// Add support for later Scala 2.13 by bumping this.
val scala213MinorVersion: Int = 18
// Bump Scala 3 version by bumping this.
val scala3MinorVersion: String = "3.7"
val javaVersion = {
val rawVersion = sys.props("java.specification.version")
// Older versions of Java started with 1., e.g. 1.8 == 8
rawVersion.stripPrefix("1.").toInt
}
// Use `moduleDir` to locate build file paths,
// avoiding `workspaceRoot` which may cause path errors when Chisel is used as nested build files.
val circtJson = BuildCtx.watchValue(
os.read(moduleDir / os.up / "etc" / "circt.json")
)
val firtoolVersion = {
val j = _root_.upickle.default.read[Map[String, String]](circtJson)
j("version").stripPrefix("firtool-")
}
val pluginScalaCrossVersions = {
val scala213: Seq[String] = 9.to(18).map("2.13." + _)
val scala33: Seq[String] = 7.to(7).map("3.3." + _)
val scala38: Seq[String] = 0.to(1).map("3.8." + _)
scala213 ++ scala33 ++ scala38
}
val scalaCrossVersions = Seq("2.13", "3")
def scalaCrossToVersion(major: String): String = major match {
case "2.13" => s"2.13.$scala213MinorVersion"
case "3" => s"3.$scala3MinorVersion"
}
def scalaVersionToCross(version: String): String =
if (version.startsWith("2.13")) "2.13"
else if (version.startsWith("3.")) "3"
else throw new Exception(s"Unsupported version $version")
def isScala3(ver: String): Boolean = ver.startsWith("3")
def getScala3MinorVersion(ver: String): Int = ver.split('.') match {
case Array("3", minor, _) => minor.toInt
case _ => require(false, s"$ver is not a valid Scala 3 version"); ???
}
def buildUnits(): Seq[ScalaModule] = {
scalaCrossVersions.flatMap { ver =>
Seq(
chisel(ver),
stdlib.cross(ver),
chisel(ver).test,
firrtl.cross(ver).test,
svsim.cross(ver).test,
unipublish("2.13")
)
} ++ scalaCrossVersions.filterNot(isScala3(_)).flatMap { ver2 =>
Seq(
`integration-tests`.cross(ver2).test,
lit.utility.cross(ver2)
)
}
}
def defaultCrossVersion = scalaCrossVersions.head
def defaultScalaVersion = scalaCrossToVersion(defaultCrossVersion)
val jmhVersion = "1.37"
val osLib = mvn"com.lihaoyi::os-lib:0.10.7" // 0.11 requires Java 11
val upickle = mvn"com.lihaoyi::upickle:3.3.1" // upickle 4.0 requires Scala 3.4 (we target Scala 3.3 LTS)
val firtoolResolver = mvn"org.chipsalliance::firtool-resolver:2.0.1"
val scalatest = mvn"org.scalatest::scalatest:3.2.19"
val scalacheck = mvn"org.scalatestplus::scalacheck-1-18:3.2.19.0"
val json4s = mvn"io.github.json4s::json4s-native:4.1.0"
val dataclass = mvn"io.github.alexarchambault::data-class:0.2.7"
val commonText = mvn"org.apache.commons:commons-text:1.15.0"
val scopt = mvn"com.github.scopt::scopt:4.1.0"
val mdoc = mvn"org.scalameta::mdoc:2.8.2"
def scalaReflect(scalaVersion: String) = mvn"org.scala-lang:scala-reflect:$scalaVersion"
def scala2Compiler(scalaVersion: String) = mvn"org.scala-lang:scala-compiler:$scalaVersion"
def scala3Compiler(scalaVersion: String) = mvn"org.scala-lang::scala3-compiler:$scalaVersion"
def scalaLibrary(scalaVersion: String) = mvn"org.scala-lang:scala-library:$scalaVersion"
def circt(version: String, os: String, platform: String) =
s"https://github.com/llvm/circt/releases/download/firtool-${version}/circt-full-shared-${os}-${platform}.tar.gz"
val scala2WarnConf = Seq(
"msg=APIs in chisel3.internal:s",
"msg=All APIs in package firrtl:s",
"msg=migration to the MLIR:s",
"msg=method hasDefiniteSize in trait IterableOnceOps is deprecated:s", // replacement `knownSize` is not in 2.12
"msg=object JavaConverters in package collection is deprecated:s",
"msg=undefined in comment for method cf in class PrintableHelper:s",
// This is deprecated for external users but not internal use
"cat=deprecation&origin=firrtl\\.options\\.internal\\.WriteableCircuitAnnotation:s",
"cat=deprecation&origin=chisel3\\.util\\.experimental\\.BoringUtils.*:s",
"cat=deprecation&origin=chisel3\\.experimental\\.IntrinsicModule:s",
"cat=deprecation&origin=chisel3\\.ltl.*:s",
"cat=deprecation&origin=chisel3\\.InstanceId:s",
// Deprecated for external users, will eventually be removed.
"cat=deprecation&msg=Looking up Modules is deprecated:s",
// Only for testing of deprecated APIs
"cat=deprecation&msg=Use of @instantiable on user-defined types is deprecated:s",
// FirtoolResolver uses package shading which causes issues with metadata for inlining.
// We don't want to inline from it anyway so just suppress the warning.
"msg=reading InlineInfoAttribute from firtoolresolver:s",
// Scala 2.13.17 adds warning for inferred structural types but this is not
// a problem because in Scala 3 we use Selectable.
"msg=will no longer have a structural type:s"
)
// ScalacOptions
def scala2CommonOptions(extraWarnConf: Seq[String]) = Seq(
"-deprecation",
"-feature",
"-unchecked",
"-Werror",
"-Ymacro-annotations",
"-release:8",
"-explaintypes",
"-Xcheckinit",
"-Xlint:infer-any",
"-Xlint:missing-interpolator",
"-language:reflectiveCalls",
// It is crucial to only ever inline from within the same artifact.
// Despite having several comilation units, we publish them together as a
// single artifact so it is safe to inline anything in the chisel3 package.
// See documentation: https://docs.scala-lang.org/overviews/compiler-options/optimizer.html
"-opt:l:inline",
"-opt-inline-from:chisel3.**",
s"-Wconf:${(scala2WarnConf ++ extraWarnConf).mkString(",")}"
)
def rootDir = BuildCtx.workspaceRoot
/** The Chisel version, used by publishVersion and BuildInfo */
def version = Task.Input {
val previousTagOpt = {
val proc = os.call(("git", "describe", "--tags", "--abbrev=0"), cwd = rootDir, stderr = os.Pipe, check = false)
// non-zero exit means not a git repo, e.g. CI
Option.when(proc.exitCode == 0) {
proc.out.trim()
}
}
previousTagOpt match {
case None => "unknown"
case Some(previousTag) =>
val previousTagNoV = previousTag.stripPrefix("v")
val commitsSincePreviousTag = {
val proc =
os.call(("git", "rev-list", "--count", "HEAD", "--not", previousTag), cwd = rootDir, stderr = os.Pipe)
proc.out.trim().toInt
}
if (commitsSincePreviousTag == 0) {
previousTagNoV // really the current tag
} else {
val currentCommit = {
val proc = os.call(("git", "rev-parse", "HEAD"), cwd = rootDir, stderr = os.Pipe)
proc.out.trim().take(8) // include first 8 characters of commit hash
}
s"$previousTagNoV+$commitsSincePreviousTag-$currentCommit-SNAPSHOT"
}
}
}
/** Is this a SNAPSHOT release? */
def isSnapshot = Task { version().endsWith("-SNAPSHOT") }
}
/** Alias for compiling everything */
def compileAll() = Task.Command {
Task.traverse(v.buildUnits())(_.compile)()
}
/** Alias command for running mdoc */
def mdoc() = Task.Command {
docs.mdoc()
}
/** The latest stable version */
def latestStableVersion() = Task.Command {
docs.latestStableVersion()
}
/** Our base trait that helps with our simplified cross versioning (2.13 instead of 2.13.18)
*
* Keep this lean, it's mixed in to firrtl and svsim as well.
*/
trait ChiselCrossModule extends CrossSbtModule {
protected def _scalaVersion = v.scalaCrossToVersion(crossScalaVersion)
def scalaVersion = _scalaVersion
}
trait HasScala2MacroAnno extends CrossModuleBase {
override def scalacOptions = Task {
if (!v.isScala3(crossScalaVersion)) {
super.scalacOptions() ++ Seq("-Ymacro-annotations")
} else super.scalacOptions()
}
}
trait HasScalaPlugin extends ChiselCrossModule {
def pluginModule = plugin.cross(_scalaVersion)
override def scalacOptions = Task {
super.scalacOptions() ++ Seq(s"-Xplugin:${pluginModule.jar().path}")
}
override def scalacPluginClasspath = Task {
super.scalacPluginClasspath() ++ Seq(pluginModule.jar())
}
}
trait HasCommonOptions extends CrossModuleBase {
def xsource3: Boolean = true
/** Extra warning configuration to include in scalacOptions */
def extraWarnConf: Seq[String] = Nil
override def scalacOptions = Task {
if (!v.isScala3(crossScalaVersion)) {
super.scalacOptions() ++ v.scala2CommonOptions(extraWarnConf) ++ Option.when(xsource3)("-Xsource:3")
} else super.scalacOptions()
}
}
// Build rules for managing circt artifacts
object circt extends Module {
val architecture = System.getProperty("os.arch")
val operationSystem = System.getProperty("os.name")
val mac = operationSystem.toLowerCase.startsWith("mac")
val linux = operationSystem.toLowerCase.startsWith("linux")
val windows = operationSystem.toLowerCase.startsWith("win")
val amd64 = architecture.matches("^(x8664|amd64|ia32e|em64t|x64|x86_64)$")
val aarch64 = architecture.equals("aarch64") | architecture.startsWith("armv8")
def circt(version: String, os: String, platform: String) =
s"https://github.com/llvm/circt/releases/download/firtool-${version}/circt-full-shared-${os}-${platform}.tar.gz"
// Copy-pasted from Mill 0.12.14 and updated to Mill 1.0.0 (MIT License)
def download(url: String, dest: os.RelPath)(implicit ctx: mill.api.TaskCtx.Dest): PathRef = {
val out = ctx.dest / dest
val website = new java.net.URI(url).toURL
val websiteInputStream = website.openStream
try {
java.nio.file.Files.copy(websiteInputStream, out.toNIO)
PathRef(out)
} finally {
websiteInputStream.close()
}
}
// use Task(persistent = true) to avoid download repeatedly
def installDir: T[os.Path] = Task(persistent = true) {
Task.ctx().env.get("CIRCT_INSTALL_PATH") match {
case Some(dir) => os.Path(dir)
case None =>
Task.ctx().log.info("Use CIRCT_INSTALL_PATH to vendor circt")
val tarPath = Task.dest / "circt.tar.gz"
if (!os.exists(tarPath)) {
val url = circt(
v.firtoolVersion,
if (linux) "linux" else if (mac) "macos" else throw new Exception("unsupported os"),
// circt does not yet publish for macos-aarch64, use x64 for now
if (amd64 || mac) "x64" else throw new Exception("unsupported arch")
)
Task.ctx().log.info(s"Downloading circt from ${url}")
download(url, os.rel / "circt.tar.gz")
Task.ctx().log.info(s"Download Successfully")
}
os.proc("tar", "xvf", tarPath, "--strip-components=1").call(Task.dest)
Task.dest
}
}
def binDir = Task(installDir() / "bin")
}
// TODO: move chisel src to subfolder once we have dropped sbt flow
object chisel extends Cross[Chisel](v.scalaCrossVersions)
trait Chisel extends CrossSbtModule with HasScala2MacroAnno with HasScalaPlugin with ScalafmtModule {
override def moduleDir = super.moduleDir / os.up
def svsimModule = svsim.cross(crossScalaVersion)
def coreModule = core.cross(crossScalaVersion)
override def scalacOptions = Task {
if (v.isScala3(crossScalaVersion)) {
super.scalacOptions()
} else {
super.scalacOptions() ++ v.scala2CommonOptions(Nil)
}
}
override def moduleDeps = super.moduleDeps ++ Seq(coreModule, svsimModule)
def compileMvnDeps = Seq(v.scalatest)
object test extends CrossSbtTests with TestModule.ScalaTest with ScalafmtModule {
def mvnDeps = Seq(v.scalatest, v.scalacheck)
// TODO: enable sandbox and run tests in parallel
override def testSandboxWorkingDir = false
// Suppress Scala 3 behavior requiring explicit types on implicit definitions
// Note this must come before the -Wconf is warningSuppression
override def scalacOptions = Task { super.scalacOptions() :+ "-Wconf:cat=other-implicit-type:s" }
override def testForkGrouping = discoveredTestClasses().grouped(8).toSeq
// Compile shared libraries for the simulator link library tests.
def sharedTestLibs: T[Seq[os.Path]] = Task(persistent = true) {
val srcDir = this.moduleDir / "src" / "test" / "resources" / "chisel3" / "simulator"
val cc = sys.env.getOrElse("CC", "cc")
val shared = if (scala.util.Properties.isMac) "-dynamiclib" else "-shared"
val srcFiles = Seq("linkLibA.c", "linkLibB.c", "linkLibC.c")
val libFiles = srcFiles.map { src =>
val lib = Task.dest / (src.stripSuffix(".c") + ".so")
os.proc(cc, shared, "-fPIC", srcDir / src, "-o", lib).call()
lib
}
libFiles
}
// Pass some of the pre-compiled shared libraries to the simulator via
// environment variables.
override def forkEnv = Task {
val libs = sharedTestLibs()
super.forkEnv() ++ Map(
// CHISELSIM_LIBS lets ChiselSim resolve libraries by name:
"CHISELSIM_LIBS" -> s"linkLibA=${libs(0)}",
// Also pass in a full path to test the `libraryPaths` setting:
"LINKLIBC_FULL_PATH" -> libs(2).toString
)
}
// Pass one of the pre-compiled shared libraries to the simulator via a JVM
// property.
override def forkArgs = Task {
val libs = sharedTestLibs()
super.forkArgs() :+ s"-Dchiselsim.libraries=linkLibB=${libs(1)}"
}
}
def unitTest(
@arg(short = 'G', noDefaultName = true, doc = "Pass argument to chisel3.UnitTests main")
chiselArgs: Seq[String] = Seq.empty,
@arg(short = 'C', noDefaultName = true, doc = "Pass argument to firtool")
firtoolArgs: Seq[String] = Seq.empty,
@arg(short = 'T', noDefaultName = true, doc = "Pass argument to circt-test")
circtTestArgs: Seq[String] = Seq.empty
): Task.Command[Unit] = Task.Command {
val firFile = Task.dest / "unit_tests.fir"
val mlirFile = Task.dest / "unit_tests.mlir"
// Run the `chisel3.UnitTests` main to generate a FIR file containing all
// tests. Use test.runner() to include test classes in the classpath. Use
// `-R` arguments to limit unit test discovery to Chisel sources, but none
// of its dependencies.
val runpathArgs = (localClasspath() ++ test.localClasspath()).flatMap(p => Seq("-R", p.path.toString))
test
.runner()
.run(
args = Seq("-o", firFile.toString) ++ runpathArgs ++ chiselArgs,
mainClass = "chisel3.UnitTests",
workingDir = Task.dest
)
// Compile the FIR file to MLIR using firtool.
os.proc(
Seq(
(circt.binDir() / "firtool").toString,
"--ir-hw",
firFile.toString,
"-o",
mlirFile.toString,
"--default-layer-specialization=enable"
) ++ firtoolArgs
).call(cwd = Task.dest, stdout = os.Inherit, stderr = os.Inherit)
// Run all tests in the MLIR file using circt-test.
os.proc(
Seq(
(circt.binDir() / "circt-test").toString,
mlirFile.toString
) ++ circtTestArgs
).call(cwd = Task.dest, stdout = os.Inherit, stderr = os.Inherit)
}
}
object unipublish extends Cross[Unipublish](v.scalaCrossVersions) {
def publishableVersions = Task {
v.scalaCrossVersions.filter(v.isSnapshot() || !v.isScala3(_))
}
}