// import Mill dependency
import mill._
import mill.define.Sources
import mill.modules.Util
import mill.scalalib.TestModule.ScalaTest
import scalalib._
// support BSP
import mill.bsp._

// Note: This project requires .mill-jvm-opts file containing:
//   -Dchisel.project.root=${PWD}
// This is needed because Chisel needs to know the project root directory
// to properly generate and handle test directories and output files.
// See: https://github.com/com-lihaoyi/mill/issues/3840

object macros extends ScalaModule {
    def scalaVersion = "2.13.16"
    def scalacOptions = Seq[String]()
    override def ivyDeps = Agg(
        ivy"org.chipsalliance::chisel:7.2.0"
    )
    override def scalacPluginIvyDeps = Agg(
        ivy"org.chipsalliance:::chisel-plugin:7.2.0"
    )
}

object `TPchisel` extends SbtModule { m =>
    override def millSourcePath = super.millSourcePath / os.up
    override def scalaVersion = "2.13.16"
    override def scalacOptions = Seq(
        "-language:reflectiveCalls",
        "-deprecation",
        "-feature",
        "-Xcheckinit",
        "-Ymacro-annotations",
        "-unchecked",
        "-Xfatal-warnings",
        "-Ywarn-dead-code",
        "-Ywarn-unused"
    )
    override def moduleDeps = Seq(macros)
    override def ivyDeps = Agg(
        ivy"org.chipsalliance::chisel:7.2.0"
    )
    override def scalacPluginIvyDeps = Agg(
        ivy"org.chipsalliance:::chisel-plugin:7.2.0"
    )
    object test extends SbtTests with TestModule.ScalaTest {
        override def ivyDeps = m.ivyDeps() ++ Agg(
            ivy"org.scalatest::scalatest::3.2.19"
        )

        val nproc = sys.env.get("NPROC").getOrElse("1");

        override def forkEnv = T {
            super.forkEnv() ++ Map(
                "VERILATOR_FLAGS" -> "--O3 --noassert --no-coverage --x-assign fast --x-initial fast --trace-fst -CFLAGS -O3 -CFLAGS -march=native -CFLAGS -flto -LDFLAGS -flto --threads 4",
                "VERILATOR_RECOMPILE" -> "false"
            )
        }
        override def forkArgs = super.forkArgs() ++ Seq(
            "-Dscalatest.parallel.enabled=true",
            "-Dscalatest.parallel.mode=concurrent",
            s"-Dscalatest.parallel.config.fixed.pool.size=$nproc"
        )
    }
}
