aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/firrtl/Driver.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/scala/firrtl/Driver.scala')
-rw-r--r--src/main/scala/firrtl/Driver.scala130
1 files changed, 33 insertions, 97 deletions
diff --git a/src/main/scala/firrtl/Driver.scala b/src/main/scala/firrtl/Driver.scala
index e96c3c5b..f23be6f5 100644
--- a/src/main/scala/firrtl/Driver.scala
+++ b/src/main/scala/firrtl/Driver.scala
@@ -4,11 +4,10 @@ package firrtl
import scala.collection._
import scala.io.Source
-import scala.sys.process.{BasicIO, ProcessLogger, stringSeqToProcess}
import scala.util.{Failure, Success, Try}
import scala.util.control.ControlThrowable
import java.io.{File, FileNotFoundException}
-
+import scala.sys.process.{BasicIO, ProcessLogger, stringSeqToProcess}
import net.jcazevedo.moultingyaml._
import logger.Logger
import Parser.{IgnoreInfo, InfoMode}
@@ -17,6 +16,10 @@ import firrtl.annotations.AnnotationYamlProtocol._
import firrtl.passes.{PassException, PassExceptions}
import firrtl.transforms._
import firrtl.Utils.throwInternalError
+import firrtl.stage.{FirrtlExecutionResultView, FirrtlStage}
+import firrtl.stage.phases.DriverCompatibility
+import firrtl.options.{StageUtils, Phase}
+import firrtl.options.Viewer.view
/**
@@ -40,30 +43,22 @@ import firrtl.Utils.throwInternalError
* @see firrtlTests/DriverSpec.scala in the test directory for a lot more examples
* @see [[CompilerUtils.mergeTransforms]] to see how customTransformations are inserted
*/
-
+@deprecated("Use firrtl.stage.FirrtlStage", "1.2")
object Driver {
/** Print a warning message
*
* @param message error message
*/
- //scalastyle:off regex
- def dramaticWarning(message: String): Unit = {
- println(Console.YELLOW + "-"*78)
- println(s"Warning: $message")
- println("-"*78 + Console.RESET)
- }
+ @deprecated("Use firrtl.options.StageUtils.dramaticWarning", "1.2")
+ def dramaticWarning(message: String): Unit = StageUtils.dramaticWarning(message)
/**
* print the message in red
*
* @param message error message
*/
- //scalastyle:off regex
- def dramaticError(message: String): Unit = {
- println(Console.RED + "-"*78)
- println(s"Error: $message")
- println("-"*78 + Console.RESET)
- }
+ @deprecated("Use firrtl.options.StageUtils.dramaticWarning", "1.2")
+ def dramaticError(message: String): Unit = StageUtils.dramaticError(message)
/** Load annotation file based on options
* @param optionsManager use optionsManager config to load annotation file if it exists
@@ -107,7 +102,7 @@ object Driver {
if (firrtlConfig.annotationFileNameOverride.nonEmpty) {
val msg = "annotationFileNameOverride is deprecated! " +
"Use annotationFileNames"
- Driver.dramaticWarning(msg)
+ dramaticWarning(msg)
} else if (usingImplicitAnnoFile) {
val msg = "Implicit .anno file from top-name is deprecated!\n" +
(" "*9) + "Use explicit -faf option or annotationFileNames"
@@ -159,7 +154,7 @@ object Driver {
}
// Useful for handling erros in the options
- case class OptionsException(msg: String) extends Exception(msg)
+ case class OptionsException(message: String) extends Exception(message)
/** Get the Circuit from the compile options
*
@@ -216,77 +211,25 @@ object Driver {
* @return a FirrtlExecutionResult indicating success or failure, provide access to emitted data on success
* for downstream tools as desired
*/
- //scalastyle:off cyclomatic.complexity method.length
def execute(optionsManager: ExecutionOptionsManager with HasFirrtlOptions): FirrtlExecutionResult = {
- def firrtlConfig = optionsManager.firrtlOptions
-
- Logger.makeScope(optionsManager) {
- // Wrap compilation in a try/catch to present Scala MatchErrors in a more user-friendly format.
- val finalState = try {
- val circuit = getCircuit(optionsManager) match {
- case Success(c) => c
- case Failure(OptionsException(msg)) =>
- dramaticError(msg)
- return FirrtlExecutionFailure(msg)
- case Failure(e) => throw e
- }
+ StageUtils.dramaticWarning("firrtl.Driver is deprecated since 1.2!\nPlease switch to firrtl.stage.FirrtlStage")
- val annos = getAnnotations(optionsManager)
+ val annos = optionsManager.firrtlOptions.toAnnotations ++ optionsManager.commonOptions.toAnnotations
- // Does this need to be before calling compiler?
- optionsManager.makeTargetDir()
+ val phases: Seq[Phase] = Seq(
+ new DriverCompatibility.AddImplicitAnnotationFile,
+ new DriverCompatibility.AddImplicitFirrtlFile,
+ new DriverCompatibility.AddImplicitOutputFile,
+ new DriverCompatibility.AddImplicitEmitter,
+ new FirrtlStage )
- firrtlConfig.compiler.compile(
- CircuitState(circuit, ChirrtlForm, annos),
- firrtlConfig.customTransforms
- )
- }
- catch {
- // Rethrow the exceptions which are expected or due to the runtime environment (out of memory, stack overflow)
- case p: ControlThrowable => throw p
- case p: PassException => throw p
- case p: PassExceptions => throw p
- case p: FIRRTLException => throw p
- // Propagate exceptions from custom transforms
- case CustomTransformException(cause) => throw cause
- // Treat remaining exceptions as internal errors.
- case e: Exception => throwInternalError(exception = Some(e))
- }
-
- // Do emission
- // Note: Single emission target assumption is baked in here
- // Note: FirrtlExecutionSuccess emitted is only used if we're emitting the whole Circuit
- val emittedRes = firrtlConfig.getOutputConfig(optionsManager) match {
- case SingleFile(filename) =>
- val emitted = finalState.getEmittedCircuit
- val outputFile = new java.io.PrintWriter(filename)
- outputFile.write(emitted.value)
- outputFile.close()
- emitted.value
- case OneFilePerModule(dirName) =>
- val emittedModules = finalState.emittedComponents collect { case x: EmittedModule => x }
- if (emittedModules.isEmpty) throwInternalError() // There should be something
- emittedModules.foreach { module =>
- val filename = optionsManager.getBuildFileName(firrtlConfig.outputSuffix, s"$dirName/${module.name}")
- val outputFile = new java.io.PrintWriter(filename)
- outputFile.write(module.value)
- outputFile.close()
- }
- "" // Should we return something different here?
- }
-
- // If set, emit final annotations to a file
- optionsManager.firrtlOptions.outputAnnotationFileName match {
- case "" =>
- case file =>
- val filename = optionsManager.getBuildFileName("anno.json", file)
- val outputFile = new java.io.PrintWriter(filename)
- outputFile.write(JsonProtocol.serialize(finalState.annotations))
- outputFile.close()
- }
-
- FirrtlExecutionSuccess(firrtlConfig.compilerName, emittedRes, finalState)
+ val annosx = try {
+ phases.foldLeft(annos)( (a, p) => p.runTransform(a) )
+ } catch {
+ case e: firrtl.options.OptionsException => return FirrtlExecutionFailure(e.message)
}
+
+ view[FirrtlExecutionResult](annosx)
}
/**
@@ -321,23 +264,16 @@ object Driver {
}
object FileUtils {
- /**
- * recursive create directory and all parents
- *
+
+ /** Create a directory if it doesn't exist
* @param directoryName a directory string with one or more levels
- * @return
+ * @return true if the directory exists or if it was successfully created
*/
def makeDirectory(directoryName: String): Boolean = {
- val dirFile = new java.io.File(directoryName)
+ val dirFile = new File(directoryName)
if(dirFile.exists()) {
- if(dirFile.isDirectory) {
- true
- }
- else {
- false
- }
- }
- else {
+ dirFile.isDirectory
+ } else {
dirFile.mkdirs()
}
}
@@ -361,7 +297,7 @@ object FileUtils {
if(file.getPath.split("/").last.isEmpty ||
file.getAbsolutePath == "/" ||
file.getPath.startsWith("/")) {
- Driver.dramaticError(s"delete directory ${file.getPath} will not delete absolute paths")
+ StageUtils.dramaticError(s"delete directory ${file.getPath} will not delete absolute paths")
false
}
else {