aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/firrtl/options/phases/Checks.scala
blob: 64d81fb478fa0712619b2bc55a66b15490a2a665 (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
46
47
48
49
50
51
52
53
// SPDX-License-Identifier: Apache-2.0

package firrtl.options.phases

import firrtl.AnnotationSeq
import firrtl.annotations.Annotation
import firrtl.options.{OptionsException, OutputAnnotationFileAnnotation, Phase, TargetDirAnnotation}
import firrtl.options.Dependency

/** [[firrtl.options.Phase Phase]] that validates an [[AnnotationSeq]]. If successful, views of this [[AnnotationSeq]]
  * as [[StageOptions]] are guaranteed to succeed.
  */
class Checks extends Phase {

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

  override def optionalPrerequisiteOf = Seq.empty

  override def invalidates(a: Phase) = false

  /** Validate an [[AnnotationSeq]] for [[StageOptions]]
    * @throws OptionsException if annotations are invalid
    */
  def transform(annotations: AnnotationSeq): AnnotationSeq = {

    val td, outA = collection.mutable.ListBuffer[Annotation]()
    annotations.foreach {
      case a: TargetDirAnnotation            => td += a
      case a: OutputAnnotationFileAnnotation => outA += a
      case _ =>
    }

    if (td.size != 1) {
      val d = td.map { case TargetDirAnnotation(x) => x }
      throw new OptionsException(
        s"""|Exactly one target directory must be specified, but found `${d.mkString(", ")}` specified via:
            |    - explicit target directory: -td, --target-dir, TargetDirAnnotation
            |    - fallback default value""".stripMargin
      )
    }

    if (outA.size > 1) {
      val x = outA.map { case OutputAnnotationFileAnnotation(x) => x }
      throw new OptionsException(
        s"""|At most one output annotation file can be specified, but found '${x.mkString(", ")}' specified via:
            |    - an option or annotation: -foaf, --output-annotation-file, OutputAnnotationFileAnnotation""".stripMargin
      )
    }

    annotations
  }

}