aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/firrtl/options/StageOptions.scala
blob: 3899fee19540e90091d77d6db2a9083ac0bfc3a4 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// SPDX-License-Identifier: Apache-2.0

package firrtl.options

import java.io.File

/** Options that every stage shares
  * @param targetDirName a target (build) directory
  * @param an input annotation file
  * @param programArgs explicit program arguments
  * @param outputAnnotationFileName an output annotation filename
  */
class StageOptions private[firrtl] (
  val targetDir:         String = TargetDirAnnotation().directory,
  val annotationFilesIn: Seq[String] = Seq.empty,
  val annotationFileOut: Option[String] = None,
  val programArgs:       Seq[String] = Seq.empty,
  val writeDeleted:      Boolean = false) {

  private[options] def copy(
    targetDir:         String = targetDir,
    annotationFilesIn: Seq[String] = annotationFilesIn,
    annotationFileOut: Option[String] = annotationFileOut,
    programArgs:       Seq[String] = programArgs,
    writeDeleted:      Boolean = writeDeleted
  ): StageOptions = {

    new StageOptions(
      targetDir = targetDir,
      annotationFilesIn = annotationFilesIn,
      annotationFileOut = annotationFileOut,
      programArgs = programArgs,
      writeDeleted = writeDeleted
    )

  }

  /** Generate a filename (with an optional suffix) and create any parent directories. Suffix is only added if it is not
    * already there.
    * @param filename the name of the file
    * @param suffix an optional suffix that the file must end in
    * @return the name of the file
    * @note the filename may include a path
    */
  def getBuildFileName(filename: String, suffix: Option[String] = None): String = {
    require(filename.nonEmpty, "requested filename must not be empty")
    require(suffix.isEmpty || suffix.get.startsWith("."), s"suffix must start with '.', but got ${suffix.get}")

    /* Mangle the file in the following ways:
     *   1. Ensure that the file ends in the requested suffix
     *   2. Prepend the target directory if this is not an absolute path
     */
    val file = {
      val f = if (suffix.nonEmpty && !filename.endsWith(suffix.get)) {
        new File(filename + suffix.get)
      } else {
        new File(filename)
      }
      if (f.isAbsolute) {
        f
      } else {
        new File(targetDir + "/" + f)
      }
    }.toPath.normalize.toFile

    file.getParentFile match {
      case null                       =>
      case parent if (!parent.exists) => parent.mkdirs()
      case _                          =>
    }

    file.toString
  }

}