forked from chipsalliance/firrtl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sc
277 lines (242 loc) · 8.51 KB
/
build.sc
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
// SPDX-License-Identifier: Apache-2.0
import mill._
import mill.scalalib._
import mill.scalalib.publish._
import mill.scalalib.scalafmt._
import mill.modules.Util
import $ivy.`com.lihaoyi::mill-contrib-buildinfo:$MILL_VERSION`
import mill.contrib.buildinfo.BuildInfo
import java.io.IOException
object firrtl extends mill.Cross[firrtlCrossModule]("2.12.15", "2.13.7")
class firrtlCrossModule(val crossScalaVersion: String)
extends CrossSbtModule
with ScalafmtModule
with PublishModule
with BuildInfo {
override def millSourcePath = super.millSourcePath / os.up
// 2.12.12 -> Array("2", "12", "12") -> "12" -> 12
private def majorVersion = crossScalaVersion.split('.')(1).toInt
def publishVersion = "1.6-SNAPSHOT"
override def mainClass = T {
Some("firrtl.stage.FirrtlMain")
}
private def javacCrossOptions = Seq("-source", "1.8", "-target", "1.8")
override def scalacOptions = T {
super.scalacOptions() ++ Seq(
"-deprecation",
"-unchecked",
"-Yrangepos" // required by SemanticDB compiler plugin
) ++ (if (majorVersion == 13) Seq("-Ymacro-annotations") else Nil)
}
override def javacOptions = T {
super.javacOptions() ++ javacCrossOptions
}
override def ivyDeps = T {
super.ivyDeps() ++ Agg(
ivy"${scalaOrganization()}:scala-reflect:${scalaVersion()}",
ivy"com.github.scopt::scopt:3.7.1",
ivy"net.jcazevedo::moultingyaml:0.4.2",
ivy"org.json4s::json4s-native:3.6.12",
ivy"org.apache.commons:commons-text:1.9",
ivy"io.github.alexarchambault::data-class:0.2.5",
ivy"org.antlr:antlr4-runtime:$antlr4Version",
ivy"com.google.protobuf:protobuf-java:$protocVersion",
ivy"com.lihaoyi::os-lib:0.8.1"
) ++ {
if (majorVersion == 13)
Agg(ivy"org.scala-lang.modules::scala-parallel-collections:1.0.4")
else
Agg()
}
}
override def scalacPluginIvyDeps =
if (majorVersion == 12) Agg(ivy"org.scalamacros:::paradise:2.1.1") else super.scalacPluginIvyDeps
object test extends Tests {
override def ivyDeps = T {
Agg(
ivy"org.scalatest::scalatest:3.2.10",
ivy"org.scalatestplus::scalacheck-1-15:3.2.11.0"
)
}
def testFrameworks = T {
Seq("org.scalatest.tools.Framework")
}
}
override def buildInfoPackageName = Some("firrtl")
override def buildInfoMembers: T[Map[String, String]] = T {
Map(
"buildInfoPackage" -> artifactName(),
"version" -> publishVersion(),
"scalaVersion" -> scalaVersion()
)
}
override def generatedSources = T {
generatedAntlr4Source() ++ generatedProtoSources() :+ generatedBuildInfo()._2
}
/* antlr4 */
def antlr4Version = "4.9.3"
def antlrSource = T.source {
millSourcePath / "src" / "main" / "antlr4" / "FIRRTL.g4"
}
/** override this to false will force FIRRTL using system protoc. */
val checkSystemAntlr4Version: Boolean = true
def antlr4Path = T.persistent {
// Linux distro package antlr4 as antlr4, while brew package as antlr
PathRef(Seq("antlr4", "antlr").flatMap { f =>
try {
val systemAntlr4Version = os.proc(f).call(check = false).out.lines.head.split(" ").last
if (systemAntlr4Version == antlr4Version || !checkSystemAntlr4Version)
Some(os.Path(os.proc("bash", "-c", s"command -v $f").call().out.lines.head))
else
None
} catch {
case _: IOException =>
None
}
}.headOption match {
case Some(bin) =>
println(s"Use system antlr4: $bin")
bin
case None =>
println("Download antlr4 from Internet")
if (!os.isFile(T.ctx.dest / "antlr4"))
Util.download(s"https://www.antlr.org/download/antlr-$antlr4Version-complete.jar", os.rel / "antlr4.jar")
T.ctx.dest / "antlr4.jar"
})
}
def generatedAntlr4Source = T.sources {
antlr4Path().path match {
case f if f.last == "antlr4.jar" =>
os.proc(
"java",
"-jar",
f.toString,
"-o",
T.ctx.dest.toString,
"-lib",
antlrSource().path.toString,
"-package",
"firrtl.antlr",
"-listener",
"-visitor",
antlrSource().path.toString
).call()
case _ =>
os.proc(
antlr4Path().path.toString,
"-o",
T.ctx.dest.toString,
"-lib",
antlrSource().path.toString,
"-package",
"firrtl.antlr",
"-listener",
"-visitor",
antlrSource().path.toString
).call()
}
T.ctx.dest
}
/* protoc */
def protocVersion = "3.15.6"
def protobufSource = T.source {
millSourcePath / "src" / "main" / "proto" / "firrtl.proto"
}
def architecture = T {
System.getProperty("os.arch")
}
def operationSystem = T {
System.getProperty("os.name")
}
/** override this to false will force FIRRTL using system protoc. */
val checkSystemProtocVersion: Boolean = true
// For Mac users:
// MacOS ARM 64-bit supports native binaries via brew:
// you can install protoc via `brew install protobuf`,
// If you don't use brew installed protoc
// It still supports x86_64 binaries via Rosetta 2
// install via `/usr/sbin/softwareupdate --install-rosetta --agree-to-license`.
def protocPath = T.persistent {
PathRef({
try {
val systemProtocVersion = os.proc("protoc", "--version").call(check = false).out.lines.head.split(" ").last
if (systemProtocVersion == protocVersion || !checkSystemProtocVersion)
Some(os.Path(os.proc("bash", "-c", "command -v protoc").call().out.lines.head))
else
None
} catch {
case _: IOException =>
None
}
} match {
case Some(bin) =>
println(s"Use system protoc: $bin")
bin
case None =>
println("Download protoc from Internet")
val isMac = operationSystem().toLowerCase.startsWith("mac")
val isLinux = operationSystem().toLowerCase.startsWith("linux")
val isWindows = operationSystem().toLowerCase.startsWith("win")
val aarch_64 = architecture().equals("aarch64") | architecture().startsWith("armv8")
val ppcle_64 = architecture().equals("ppc64le")
val s390x = architecture().equals("s390x")
val x86_32 = architecture().matches("^(x8632|x86|i[3-6]86|ia32|x32)$")
val x86_64 = architecture().matches("^(x8664|amd64|ia32e|em64t|x64|x86_64)$")
val protocBinary =
if (isMac)
if (aarch_64 || x86_64) "osx-x86_64"
else throw new Exception("mill cannot detect your architecture of your Mac")
else if (isLinux)
if (aarch_64) "linux-aarch_64"
else if (ppcle_64) "linux-ppcle_64"
else if (s390x) "linux-s390x"
else if (x86_32) "linux-x86_32"
else if (x86_64) "linux-x86_64"
else throw new Exception("mill cannot detect your architecture of your Linux")
else if (isWindows)
if (x86_32) "win32"
else if (x86_64) "win64"
else throw new Exception("mill cannot detect your architecture of your Windows")
else throw new Exception("mill cannot detect your operation system.")
val unpackPath = os.rel / "unpacked"
val bin =
if (isWindows)
T.ctx.dest / unpackPath / "bin" / "protoc.exe"
else
T.ctx.dest / unpackPath / "bin" / "protoc"
if (!os.exists(bin)) {
Util.downloadUnpackZip(
s"https://github.com/protocolbuffers/protobuf/releases/download/v$protocVersion/protoc-$protocVersion-$protocBinary.zip",
unpackPath
)
}
// Download Linux/Mac binary doesn't have x.
if (!isWindows) os.perms.set(bin, "rwx------")
bin
})
}
def generatedProtoSources = T.sources {
os.proc(
protocPath().path.toString,
"-I",
protobufSource().path / os.up,
s"--java_out=${T.ctx.dest.toString}",
protobufSource().path.toString()
).call()
T.ctx.dest / "firrtl"
}
def pomSettings = T {
PomSettings(
description = artifactName(),
organization = "edu.berkeley.cs",
url = "https://github.com/freechipsproject/firrtl",
licenses = Seq(License.`BSD-3-Clause`),
versionControl = VersionControl.github("freechipsproject", "firrtl"),
developers = Seq(
Developer("jackbackrack", "Jonathan Bachrach", "https://eecs.berkeley.edu/~jrb/")
)
)
}
// make mill publish sbt compatible package
override def artifactName = "firrtl"
}