summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorJack Koenig2020-09-09 13:01:35 -0700
committerGitHub2020-09-09 20:01:35 +0000
commita2138cb11675d4eb17067c757c04b53590313c5d (patch)
tree8825b207a054971e41646125bcd599ba7c8b9b13 /src/test
parent88265eec586046e6ec96b4615e5516be0f3d9e2c (diff)
Add new annotation for Chisel Circuit serialization (#1580)
ChiselCircuitAnnotation no longer extends CustomFileEmission, rather it is Unserializable. Also the --chisel-output-file is added to the ChiselCli. New phase AddSerializationAnnotations constructs a CircuitSerializationAnnotation from ChiselCircuitAnnotation and ChiselOutputFileAnnotation. Both .fir and .pb file formats are supported. Default format is .fir unless a --chisel-output-file is specified with a .pb extension.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/scala/chiselTests/stage/ChiselMainSpec.scala36
-rw-r--r--src/test/scala/chiselTests/stage/phases/AddSerializationAnnotationsSpec.scala61
2 files changed, 95 insertions, 2 deletions
diff --git a/src/test/scala/chiselTests/stage/ChiselMainSpec.scala b/src/test/scala/chiselTests/stage/ChiselMainSpec.scala
index 036e15b1..2e9d928f 100644
--- a/src/test/scala/chiselTests/stage/ChiselMainSpec.scala
+++ b/src/test/scala/chiselTests/stage/ChiselMainSpec.scala
@@ -11,6 +11,9 @@ import org.scalatest.GivenWhenThen
import org.scalatest.featurespec.AnyFeatureSpec
import org.scalatest.matchers.should.Matchers
+import scala.io.Source
+import firrtl.Parser
+
object ChiselMainSpec {
/** A module that connects two different types together resulting in an elaboration error */
@@ -52,7 +55,7 @@ class ChiselMainSpec extends AnyFeatureSpec with GivenWhenThen with Matchers wit
}
class TargetDirectoryFixture(dirName: String) {
- val dir = new File(s"test_run_dir/FirrtlStageSpec/$dirName")
+ val dir = new File(s"test_run_dir/ChiselStageSpec/$dirName")
val buildDir = new File(dir + "/build")
dir.mkdirs()
}
@@ -63,7 +66,8 @@ class ChiselMainSpec extends AnyFeatureSpec with GivenWhenThen with Matchers wit
files: Seq[String] = Seq.empty,
stdout: Option[String] = None,
stderr: Option[String] = None,
- result: Int = 0) {
+ result: Int = 0,
+ fileChecks: Map[String, File => Unit] = Map.empty) {
def testName: String = "args" + args.mkString("_")
def argsString: String = args.mkString(" ")
}
@@ -117,6 +121,7 @@ class ChiselMainSpec extends AnyFeatureSpec with GivenWhenThen with Matchers wit
And(s"file '$f' should be emitted in the target directory")
val out = new File(td.buildDir + s"/$f")
out should (exist)
+ p.fileChecks.get(f).map(_(out))
}
}
}
@@ -148,6 +153,33 @@ class ChiselMainSpec extends AnyFeatureSpec with GivenWhenThen with Matchers wit
).foreach(runStageExpectFiles)
}
+ Feature("Specifying a custom output file") {
+ runStageExpectFiles(ChiselMainTest(
+ args = Array("--chisel-output-file", "Foo", "--no-run-firrtl"),
+ generator = Some(classOf[SameTypesModule]),
+ stdout = Some(""),
+ files = Seq("Foo.fir"),
+ fileChecks = Map(
+ "Foo.fir" -> { file =>
+ And("It should be valid FIRRTL")
+ Parser.parse(Source.fromFile(file).mkString)
+ }
+ )
+ ))
+ runStageExpectFiles(ChiselMainTest(
+ args = Array("--chisel-output-file", "Foo.pb", "--no-run-firrtl"),
+ generator = Some(classOf[SameTypesModule]),
+ stdout = Some(""),
+ files = Seq("Foo.pb"),
+ fileChecks = Map(
+ "Foo.pb" -> { file =>
+ And("It should be valid ProtoBuf")
+ firrtl.proto.FromProto.fromFile(file.toString)
+ }
+ )
+ ))
+ }
+
info("As an aspect writer")
info("I write an aspect")
Feature("Running aspects via the command line") {
diff --git a/src/test/scala/chiselTests/stage/phases/AddSerializationAnnotationsSpec.scala b/src/test/scala/chiselTests/stage/phases/AddSerializationAnnotationsSpec.scala
new file mode 100644
index 00000000..c248da6a
--- /dev/null
+++ b/src/test/scala/chiselTests/stage/phases/AddSerializationAnnotationsSpec.scala
@@ -0,0 +1,61 @@
+// See LICENSE for license details.
+
+package chiselTests.stage.phases
+
+
+import chisel3.RawModule
+import chisel3.stage.{ChiselGeneratorAnnotation, ChiselOutputFileAnnotation, CircuitSerializationAnnotation}
+import chisel3.stage.CircuitSerializationAnnotation._
+import chisel3.stage.phases.{AddSerializationAnnotations, AddImplicitOutputFile, Elaborate}
+
+import firrtl.AnnotationSeq
+import firrtl.options.{Phase, PhaseManager, Dependency, TargetDirAnnotation}
+import firrtl.options.Viewer.view
+import org.scalatest.flatspec.AnyFlatSpec
+import org.scalatest.matchers.should.Matchers
+
+class AddSerializationAnnotationsSpec extends AnyFlatSpec with Matchers {
+
+ class Foo extends RawModule { override val desiredName = "Foo" }
+
+ class Fixture {
+ val phase: Phase = new AddSerializationAnnotations
+ val manager = new PhaseManager(Dependency[AddSerializationAnnotations] :: Nil)
+ }
+
+ behavior of classOf[AddSerializationAnnotations].toString
+
+ it should "default to FirrtlFileFormat" in new Fixture {
+ val annotations: AnnotationSeq = Seq(
+ ChiselGeneratorAnnotation(() => new Foo),
+ ChiselOutputFileAnnotation("Bar") )
+
+ manager
+ .transform(annotations)
+ .collect { case CircuitSerializationAnnotation(_, filename, format) => (filename, format) }
+ .toSeq should be (Seq(("Bar", FirrtlFileFormat)))
+ }
+
+ it should "support ProtoBufFileFormat" in new Fixture {
+ val annotations: AnnotationSeq = Seq(
+ ChiselGeneratorAnnotation(() => new Foo),
+ ChiselOutputFileAnnotation("Bar.pb") )
+
+ manager
+ .transform(annotations)
+ .collect { case CircuitSerializationAnnotation(_, filename, format) => (filename, format) }
+ .toSeq should be (Seq(("Bar", ProtoBufFileFormat)))
+ }
+
+ it should "support explicitly asking for FirrtlFileFormat" in new Fixture {
+ val annotations: AnnotationSeq = Seq(
+ ChiselGeneratorAnnotation(() => new Foo),
+ ChiselOutputFileAnnotation("Bar.pb.fir") )
+
+ manager
+ .transform(annotations)
+ .collect { case CircuitSerializationAnnotation(_, filename, format) => (filename, format) }
+ .toSeq should be (Seq(("Bar.pb", FirrtlFileFormat)))
+ }
+
+}