aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/firrtl/options/phases/WriteOutputAnnotations.scala
blob: 79769a81b5cce632320cf5ad8879ed3f058f2011 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// See LICENSE for license details.

package firrtl.options.phases

import firrtl.AnnotationSeq
import firrtl.annotations.{DeletedAnnotation, JsonProtocol}
import firrtl.options.{Phase, PreservesAll, StageOptions, Unserializable, Viewer}
import firrtl.options.Dependency

import java.io.PrintWriter

/** [[firrtl.options.Phase Phase]] that writes an [[AnnotationSeq]] to a file. A file is written if and only if a
  * [[StageOptions]] view has a non-empty [[StageOptions.annotationFileOut annotationFileOut]].
  */
class WriteOutputAnnotations extends Phase with PreservesAll[Phase] {

  override def prerequisites =
    Seq( Dependency[GetIncludes],
         Dependency[ConvertLegacyAnnotations],
         Dependency[AddDefaults],
         Dependency[Checks] )

  override def dependents = Seq.empty

  /** Write the input [[AnnotationSeq]] to a fie. */
  def transform(annotations: AnnotationSeq): AnnotationSeq = {
    val sopts = Viewer[StageOptions].view(annotations)
    val serializable = annotations.filter{
      case _: Unserializable    => false
      case _: DeletedAnnotation => sopts.writeDeleted
      case _                    => true
    }

    sopts.annotationFileOut match {
      case None =>
      case Some(file) =>
        val pw = new PrintWriter(sopts.getBuildFileName(file, Some(".anno.json")))
        pw.write(JsonProtocol.serialize(serializable))
        pw.close()
    }

    annotations
  }

}