Skip to content

Commit

Permalink
add a simple scala-cli based filecheck framework
Browse files Browse the repository at this point in the history
  • Loading branch information
sequencer committed Oct 22, 2023
1 parent e951966 commit a7ddc41
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 1 deletion.
29 changes: 29 additions & 0 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,32 @@ trait CIRCTPanamaBinderModuleTest
Seq(PathRef(millSourcePath / "src" / "test"))
}
}

object filecheckutility extends Cross[FileCheckUtility](v.scalaCrossVersions)

trait FileCheckUtility
extends tests.FileCheckUtilityModule
with CrossModuleBase
with ScalafmtModule {
def millSourcePath = super.millSourcePath / os.up / "filecheck" / "utility"
def circtPanamaBinderModule = circtpanamabinder(crossScalaVersion)
}

val checkTuples = v.scalaCrossVersions.flatMap(sv =>
os.walk(
os.pwd / "filecheck" / "tests",
// avoid issue when including chisel/build.sc
includeTarget = true
)
.filter(os.isFile)
.filter(f => f.ext == "scala" || f.ext == "sc" )
.map(_.last).map(test =>
(sv, test)
)
)
object filecheck extends Cross[FileCheckTestModule](checkTuples)

trait FileCheckTestModule
extends tests.FileCheckTestModule {
def fileCheckUtilityModule = filecheckutility("2.13.12")
}
15 changes: 15 additions & 0 deletions filecheck/tests/ChiselCIRCTBinding.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//> RUN: scala-cli $THIS
//> using options -Xplugin:$PLUGINJAR
//> using javaHome $JAVAHOME
//> using javaOpt --enable-native-access=ALL-UNNAMED --enable-preview $JAVACNATIVELIBPATH
import chisel3._
import utility.binding._

class FooBundle extends Bundle {
val foo = Input(UInt(3.W))
}
class FooModule extends Module {
val io = IO(new FooBundle)
}
firrtlString(new FooModule)
verilogString(new FooModule)
5 changes: 5 additions & 0 deletions filecheck/tests/EmptyModule.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//> RUN: scala-cli $THIS | firtool -format=fir
import chisel3._
import circt.stage.ChiselStage
class EmptyModule extends Module
println(ChiselStage.emitCHIRRTL(new EmptyModule))
11 changes: 11 additions & 0 deletions filecheck/tests/WithCompilerPlugin.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//> RUN: scala-cli $THIS
//> using options -Xplugin:$PLUGINJAR
import chisel3._
import circt.stage.ChiselStage
class FooBundle extends Bundle {
val foo = Input(UInt(3.W))
}
class FooModule extends Module {
val io = IO(new FooBundle)
}
println(ChiselStage.emitCHIRRTL(new FooModule))
10 changes: 10 additions & 0 deletions filecheck/tests/WithoutCompilerPlugin.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//> RUN: scala-cli $THIS
import chisel3._
import circt.stage.ChiselStage
class FooBundle extends Bundle {
val foo = Input(UInt(3.W))
}
class FooModule extends Module {
val io = IO(new FooBundle)
}
println(ChiselStage.emitCHIRRTL(new FooModule))
23 changes: 23 additions & 0 deletions filecheck/utility/src/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import chisel3._

package object utility {
object binding {
def streamString(module: => RawModule, stream: chisel3.internal.CIRCTConverter => geny.Writable): String = Seq(
new chisel3.stage.phases.Elaborate,
chisel3.internal.panama.Convert
).foldLeft(
firrtl.AnnotationSeq(Seq(chisel3.stage.ChiselGeneratorAnnotation(() => module)))
) { case (annos, phase) => phase.transform(annos) }
.collectFirst {
case chisel3.internal.panama.circt.PanamaCIRCTConverterAnnotation(converter) =>
val string = new java.io.ByteArrayOutputStream
stream(converter).writeBytesTo(string)
new String(string.toByteArray)
}
.get

def firrtlString(module: => RawModule): String = streamString(module, _.firrtlStream)

def verilogString(module: => RawModule): String = streamString(module, _.verilogStream)
}
}
53 changes: 52 additions & 1 deletion tests.sc
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import mill._
import mill.api.Result
import mill.scalalib._
import $file.common
import mill.scalalib.api.CompilationResult
import mill.util.Jvm

import java.util
import scala.jdk.StreamConverters.StreamHasToScala

trait SvsimUnitTestModule
extends TestModule
Expand Down Expand Up @@ -77,3 +82,49 @@ trait CIRCTPanamaBinderModuleTestModule
scalacheckIvy
)
}

trait FileCheckUtilityModule
extends ScalaModule
with common.HasCIRCTPanamaBinderModule
with common.HasMacroAnnotations

trait FileCheckTestModule
extends Cross.Module2[String, String]
with TaskModule {
def crossScalaVersion = crossValue
def dutPath = crossValue2
def fileCheckUtilityModule: FileCheckUtilityModule

def file = T.input(PathRef(millSourcePath / "tests" / dutPath))
def source = T(os.read(file().path))
def testLines = T(source().lines().toScala(LazyList).filter(s => s.matches("^//>\\s+RUN:.*")))
def tests = T(testLines().map(_
.replaceAll("^//>\\s+RUN:\\s+", "")
.replaceAll("\\$THIS", processedFile().path.toString)
))
def processedFilePath = T(T.dest/dutPath)
def processed = T(
s"""//> using scala "$crossScalaVersion"
|//> using jars ${fileCheckUtilityModule.runClasspath().map(_.path).mkString(" ")}
|""".stripMargin +
source()
.replaceAll("^//>\\s+RUN:.*", "")
.replaceAll("\\$THIS", processedFilePath().toString)
.replaceAll("\\$PLUGINJAR", fileCheckUtilityModule.pluginModule.jar().path.toString)
.replaceAll("\\$JAVAHOME", sys.props("java.home"))
.replaceAll("\\$JAVACNATIVELIBPATH", fileCheckUtilityModule.circtPanamaBinderModule.libraryPaths().map(p => s"-Djava.library.path=${p.path}").mkString(" "))
)
def processedFile = T {
os.write.over(processedFilePath(), processed())
PathRef(processedFilePath())
}
def run = T {
processedFile()
tests().map{c =>
T.log.info(s"run $c")
os.proc("bash", "-c", c).call()
}
}

override def defaultCommandName(): String = "test"
}

0 comments on commit a7ddc41

Please sign in to comment.