xref: /XiangShan/build.sc (revision 8cfc24b28454f1915c339ce79485711f8e438f59)
1/***************************************************************************************
2* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC)
3* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences
4* Copyright (c) 2020-2021 Peng Cheng Laboratory
5*
6* XiangShan is licensed under Mulan PSL v2.
7* You can use this software according to the terms and conditions of the Mulan PSL v2.
8* You may obtain a copy of Mulan PSL v2 at:
9*          http://license.coscl.org.cn/MulanPSL2
10*
11* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
12* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
13* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
14*
15* See the Mulan PSL v2 for more details.
16***************************************************************************************/
17
18import mill._
19import scalalib._
20import scalafmt._
21import $packages._
22import $file.`rocket-chip`.common
23import $file.`rocket-chip`.cde.common
24import $file.`rocket-chip`.hardfloat.common
25import $file.huancun.common
26import $file.coupledL2.common
27import $file.openLLC.common
28
29/* for publishVersion */
30import $ivy.`de.tototec::de.tobiasroeser.mill.vcs.version::0.4.0`
31import de.tobiasroeser.mill.vcs.version.VcsVersion
32import java.io.{BufferedReader, InputStreamReader}
33import java.time.LocalDateTime
34import java.time.format.DateTimeFormatter
35import java.util.Locale
36import scala.util.matching.Regex
37
38val defaultScalaVersion = "2.13.15"
39val pwd = os.Path(sys.env("MILL_WORKSPACE_ROOT"))
40
41def defaultVersions = Map(
42  "chisel"        -> ivy"org.chipsalliance::chisel:6.6.0",
43  "chisel-plugin" -> ivy"org.chipsalliance:::chisel-plugin:6.6.0",
44  "chiseltest"    -> ivy"edu.berkeley.cs::chiseltest:6.0.0"
45)
46
47trait HasChisel extends SbtModule {
48  def chiselModule: Option[ScalaModule] = None
49
50  def chiselPluginJar: T[Option[PathRef]] = None
51
52  def chiselIvy: Option[Dep] = Some(defaultVersions("chisel"))
53
54  def chiselPluginIvy: Option[Dep] = Some(defaultVersions("chisel-plugin"))
55
56  override def scalaVersion = defaultScalaVersion
57
58  override def scalacOptions = super.scalacOptions() ++
59    Agg("-language:reflectiveCalls", "-Ymacro-annotations", "-Ytasty-reader")
60
61  override def ivyDeps = super.ivyDeps() ++ Agg(chiselIvy.get)
62
63  override def scalacPluginIvyDeps = super.scalacPluginIvyDeps() ++ Agg(chiselPluginIvy.get)
64}
65
66object rocketchip
67  extends $file.`rocket-chip`.common.RocketChipModule
68    with HasChisel {
69  def scalaVersion: T[String] = T(defaultScalaVersion)
70
71  override def millSourcePath = pwd / "rocket-chip"
72
73  def macrosModule = macros
74
75  def hardfloatModule = hardfloat
76
77  def cdeModule = cde
78
79  def mainargsIvy = ivy"com.lihaoyi::mainargs:0.7.0"
80
81  def json4sJacksonIvy = ivy"org.json4s::json4s-jackson:4.0.7"
82
83  object macros extends Macros
84
85  trait Macros
86    extends $file.`rocket-chip`.common.MacrosModule
87      with SbtModule {
88
89    def scalaVersion: T[String] = T(defaultScalaVersion)
90
91    def scalaReflectIvy = ivy"org.scala-lang:scala-reflect:${defaultScalaVersion}"
92  }
93
94  object hardfloat
95    extends $file.`rocket-chip`.hardfloat.common.HardfloatModule with HasChisel {
96
97    def scalaVersion: T[String] = T(defaultScalaVersion)
98
99    override def millSourcePath = pwd / "rocket-chip" / "hardfloat" / "hardfloat"
100
101  }
102
103  object cde
104    extends $file.`rocket-chip`.cde.common.CDEModule with ScalaModule {
105
106    def scalaVersion: T[String] = T(defaultScalaVersion)
107
108    override def millSourcePath = pwd / "rocket-chip" / "cde" / "cde"
109  }
110}
111
112object utility extends HasChisel {
113
114  override def millSourcePath = pwd / "utility"
115
116  override def moduleDeps = super.moduleDeps ++ Seq(
117    rocketchip
118  )
119
120  override def ivyDeps = super.ivyDeps() ++ Agg(
121    ivy"com.lihaoyi::sourcecode:0.4.2",
122  )
123
124}
125
126object yunsuan extends HasChisel {
127
128  override def millSourcePath = pwd / "yunsuan"
129
130}
131
132object huancun extends $file.huancun.common.HuanCunModule with HasChisel {
133
134  override def millSourcePath = pwd / "huancun"
135
136  def rocketModule: ScalaModule = rocketchip
137
138  def utilityModule: ScalaModule = utility
139
140}
141
142object coupledL2 extends $file.coupledL2.common.CoupledL2Module with HasChisel {
143
144  override def millSourcePath = pwd / "coupledL2"
145
146  def rocketModule: ScalaModule = rocketchip
147
148  def utilityModule: ScalaModule = utility
149
150  def huancunModule: ScalaModule = huancun
151
152}
153
154object openNCB extends SbtModule with HasChisel {
155
156  override def millSourcePath = pwd / "openLLC" / "openNCB"
157
158  override def moduleDeps = super.moduleDeps ++ Seq(
159    rocketchip
160  )
161
162}
163
164object openLLC extends $file.openLLC.common.OpenLLCModule with HasChisel {
165
166  override def millSourcePath = pwd / "openLLC"
167
168  def coupledL2Module: ScalaModule = coupledL2
169
170  def rocketModule: ScalaModule = rocketchip
171
172  def utilityModule: ScalaModule = utility
173
174  def openNCBModule: ScalaModule = openNCB
175
176}
177
178object difftest extends HasChisel {
179
180  override def millSourcePath = pwd / "difftest"
181
182  object test extends SbtTests with TestModule.ScalaTest {
183    override def sources = T.sources {
184      super.sources() ++ Seq(PathRef(this.millSourcePath / "src" / "generator" / "chisel"))
185    }
186  }
187
188}
189
190object fudian extends HasChisel {
191
192  override def millSourcePath = pwd / "fudian"
193
194}
195
196object chiselAIA extends HasChisel {
197  override def millSourcePath = pwd / "ChiselAIA"
198
199  override def moduleDeps = super.moduleDeps ++ Seq(
200    rocketchip,
201    utility
202  )
203}
204
205object macros extends ScalaModule {
206
207  override def millSourcePath = pwd / "macros"
208
209  override def scalaVersion: T[String] = T(defaultScalaVersion)
210
211  override def ivyDeps = super.ivyDeps() ++ Agg(ivy"org.scala-lang:scala-reflect:${defaultScalaVersion}")
212
213  def scalaReflectIvy = ivy"org.scala-lang:scala-reflect:${defaultScalaVersion}"
214}
215
216// extends this trait to use XiangShan in other projects
217trait XiangShanModule extends ScalaModule {
218
219  def rocketModule: ScalaModule
220
221  def difftestModule: ScalaModule
222
223  def huancunModule: ScalaModule
224
225  def coupledL2Module: ScalaModule
226
227  def openLLCModule: ScalaModule
228
229  def fudianModule: ScalaModule
230
231  def utilityModule: ScalaModule
232
233  def yunsuanModule: ScalaModule
234
235  def chiselAIAModule: ScalaModule
236
237  def macrosModule: ScalaModule
238
239  override def moduleDeps = super.moduleDeps ++ Seq(
240    rocketModule,
241    difftestModule,
242    huancunModule,
243    coupledL2Module,
244    openLLCModule,
245    yunsuanModule,
246    fudianModule,
247    utilityModule,
248    chiselAIAModule,
249    macrosModule,
250  )
251
252  val resourcesPATH = pwd.toString() + "/src/main/resources"
253  val envPATH = sys.env("PATH") + ":" + resourcesPATH
254
255  override def forkEnv = Map("PATH" -> envPATH)
256}
257
258object xiangshan extends XiangShanModule with HasChisel with ScalafmtModule {
259
260  override def millSourcePath = pwd
261
262  def rocketModule = rocketchip
263
264  def difftestModule = difftest
265
266  def huancunModule = huancun
267
268  def coupledL2Module = coupledL2
269
270  def openLLCModule = openLLC
271
272  def fudianModule = fudian
273
274  def utilityModule = utility
275
276  def yunsuanModule = yunsuan
277
278  def chiselAIAModule = chiselAIA
279
280  def macrosModule = macros
281
282  // properties may be changed by user. Use `Task.Input` here.
283  def forkArgsTask = Task.Input {
284    Seq(s"-Xmx${sys.props.getOrElse("jvm-xmx", "40G")}", s"-Xss${sys.props.getOrElse("jvm-xss", "256m")}")
285  }
286
287  override def forkArgs = forkArgsTask()
288
289  override def ivyDeps = super.ivyDeps() ++ Agg(
290    defaultVersions("chiseltest"),
291    ivy"io.circe::circe-yaml:1.15.0",
292    ivy"io.circe::circe-generic-extras:0.14.4"
293  )
294
295  override def scalacOptions = super.scalacOptions() ++ Agg("-deprecation", "-feature")
296
297  def publishVersion: T[String] = VcsVersion.vcsState().format(
298    revHashDigits = 8,
299    dirtyHashDigits = 0,
300    commitCountPad = -1,
301    countSep = "",
302    tagModifier = (tag: String) => "[Rr]elease.*".r.findFirstMatchIn(tag) match {
303      case Some(_) => "Kunminghu-Release-" + LocalDateTime.now().format(
304                                 DateTimeFormatter.ofPattern("MMM-dd-yyyy").withLocale(new Locale("en")))
305      case None => "Kunminghu-dev"
306    },
307    /* add "username, buildhost, buildtime" for non-release version */
308    untaggedSuffix = " (%s@%s) # %s".format(
309      System.getProperty("user.name"),
310      java.net.InetAddress.getLocalHost().getHostName(),
311      LocalDateTime.now().format(DateTimeFormatter.ofPattern("MMM dd hh:mm:ss yyyy").withLocale(new Locale("en")))),
312  )
313
314  // gitStatus changes frequently and unpredictably. Use `Task.Input` here.
315  def gitStatus: T[String] = Task.Input {
316    val gitRevParseBuilder = new ProcessBuilder("git", "rev-parse", "HEAD")
317    val gitRevParseProcess = gitRevParseBuilder.start()
318    val shaReader = new BufferedReader(new InputStreamReader(gitRevParseProcess.getInputStream))
319    val sha = shaReader.readLine()
320
321    val gitStatusBuilder = new ProcessBuilder("git", "status", "-uno", "--porcelain")
322    val gitStatusProcess = gitStatusBuilder.start()
323    val gitStatusReader = new BufferedReader(new InputStreamReader(gitStatusProcess.getInputStream))
324    val status = gitStatusReader.readLine()
325    val gitDirty = if (status == null) 0 else 1
326
327    val str =
328      s"""|SHA=$sha
329          |dirty=$gitDirty
330          |""".stripMargin
331    str
332  }
333
334  def packDifftestResources(destDir: os.Path): Unit = {
335    // package difftest source as resources, only git tracked files were collected
336    val difftest_srcs = os.proc("git", "ls-files").call(cwd = pwd / "difftest").out
337                          .text().split("\n").filter(_.nonEmpty).toSeq
338                          .map(os.RelPath(_))
339    difftest_srcs.foreach { f =>
340      os.copy(pwd / "difftest" / f, destDir / "difftest-src" / f, createFolders = true)
341    }
342
343    // package ready-to-run binary as resources
344    val ready_to_run = Seq("riscv64-nemu-interpreter-dual-so",
345                           "riscv64-nemu-interpreter-so",
346                           "riscv64-spike-so")
347    ready_to_run.foreach { f =>
348      os.copy(pwd / "ready-to-run" / f, destDir / "ready-to-run" / f, createFolders = true)
349    }
350  }
351
352  override def resources = T.sources {
353    os.write(T.dest / "publishVersion", publishVersion())
354    os.write(T.dest / "gitStatus", gitStatus())
355    os.write(T.dest / "gitModules", os.proc("git", "submodule", "status").call().out.text())
356    packDifftestResources(T.dest)
357    super.resources() ++ Seq(PathRef(T.dest))
358  }
359
360  object test extends SbtTests with TestModule.ScalaTest {
361    override def moduleDeps = super.moduleDeps ++ Seq(
362      difftestModule.test
363    )
364
365    override def forkArgs = forkArgsTask()
366
367    override def scalacOptions = super.scalacOptions() ++ Agg("-deprecation", "-feature")
368
369    val resourcesPATH = pwd.toString() + "/src/main/resources"
370    val envPATH = sys.env("PATH") + ":" + resourcesPATH
371
372    override def forkEnv = Map("PATH" -> envPATH)
373  }
374}
375