From c6093cbcd4f2eb8acd44f3b9d4e7146448de172f Mon Sep 17 00:00:00 2001 From: Chick Markley Date: Fri, 12 Nov 2021 12:29:48 -0800 Subject: Let firrtl based applications run despite loading unknown annotations (#2387) An application like barstools may contain a main that loads an annotations file containing annotation classes that are not on it's classpath. This change allows unknown annotations to be preserved by wrapping them in a UnrecognizedAnnotation. If annotations are then output to a file, they will be unwrapped during serialization This feature can be enabled via an AllowUnrecognizedAnnotations annotation Co-authored-by: chick Co-authored-by: Jack Koenig --- src/main/scala/firrtl/options/phases/GetIncludes.scala | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src/main/scala/firrtl/options/phases/GetIncludes.scala') diff --git a/src/main/scala/firrtl/options/phases/GetIncludes.scala b/src/main/scala/firrtl/options/phases/GetIncludes.scala index 15692e79..d50b2c6f 100644 --- a/src/main/scala/firrtl/options/phases/GetIncludes.scala +++ b/src/main/scala/firrtl/options/phases/GetIncludes.scala @@ -6,9 +6,9 @@ import firrtl.AnnotationSeq import firrtl.annotations.{AnnotationFileNotFoundException, JsonProtocol} import firrtl.options.{InputAnnotationFileAnnotation, Phase, StageUtils} import firrtl.FileUtils +import firrtl.stage.AllowUnrecognizedAnnotations import java.io.File - import scala.collection.mutable import scala.util.{Failure, Try} @@ -25,10 +25,13 @@ class GetIncludes extends Phase { * @param filename a JSON or YAML file of [[annotations.Annotation]] * @throws annotations.AnnotationFileNotFoundException if the file does not exist */ - private def readAnnotationsFromFile(filename: String): AnnotationSeq = { + private def readAnnotationsFromFile( + filename: String, + allowUnrecognizedAnnotations: Boolean = false + ): AnnotationSeq = { val file = new File(filename).getCanonicalFile if (!file.exists) { throw new AnnotationFileNotFoundException(file) } - JsonProtocol.deserialize(file) + JsonProtocol.deserialize(file, allowUnrecognizedAnnotations) } /** Recursively read all [[Annotation]]s from any [[InputAnnotationFileAnnotation]]s while making sure that each file is @@ -38,6 +41,7 @@ class GetIncludes extends Phase { * @return the original annotation sequence with any discovered annotations added */ private def getIncludes(includeGuard: mutable.Set[String] = mutable.Set())(annos: AnnotationSeq): AnnotationSeq = { + val allowUnrecognizedAnnotations = annos.contains(AllowUnrecognizedAnnotations) annos.flatMap { case a @ InputAnnotationFileAnnotation(value) => if (includeGuard.contains(value)) { @@ -45,7 +49,7 @@ class GetIncludes extends Phase { None } else { includeGuard += value - getIncludes(includeGuard)(readAnnotationsFromFile(value)) + getIncludes(includeGuard)(readAnnotationsFromFile(value, allowUnrecognizedAnnotations)) } case x => Seq(x) } -- cgit v1.2.3