-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleBuilder.scala
58 lines (52 loc) · 2.34 KB
/
ModuleBuilder.scala
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
package chisel.packaging
import chisel3._
import scala.sys.process._
import java.nio.file._
import scala.language.postfixOps
/** Module definition.
* @param config Optional, arbitrary configuration object, passed to post build actions.
* @param constr Module constructor function.
* @param core Core definition.
**/
final case class ModuleDef(config: Option[Any], constr: () => Module, core: CoreDefinition)
/**
* Abstract IP-XACT builder class:
* Objects can inherit from ModuleBuilder to automate the building
* and packaging process. Provides main method that can be run
* automatically via sbt run, takes arguments which cores to build.
* @param packagingDir Base directory of packaging submodule
* (default: ./packaging)
**/
abstract class ModuleBuilder(packagingDir: String = "packaging") {
val chiselArgs = Array[String]()
/** List of modules to build. */
val modules: Seq[ModuleDef]
private def extractScript(name: String): Path = {
val p = Paths.get(java.io.File.createTempFile("chisel-packaging-", "", null).getAbsolutePath.toString).resolveSibling(name)
val ps = new java.io.FileOutputStream(p.toFile)
val in = Option(getClass().getClassLoader().getResourceAsStream(name))
if (in.isEmpty) throw new Exception(s"$name not found in resources!")
in map { is =>
Iterator continually (is.read) takeWhile (-1 !=) foreach (ps.write)
ps.flush()
ps.close()
p.toFile.deleteOnExit()
p.toFile.setExecutable(true)
Paths.get(p.toString)
} get
}
def main(args: Array[String]) {
assert ((modules map (_.core.name.toLowerCase)).toSet.size == modules.length, "module names must be unique")
val fm = modules filter (m => args.length == 0 || args.map(_.toLowerCase).contains(m.core.name.toLowerCase))
assert (fm.length > 0, "no matching cores found for: " + args.mkString(", "))
val (packaging, axi) = (extractScript("package.py"), extractScript("axi4.py"))
System.err.println(s"packaging script in: ${packaging.toString}")
fm foreach { m =>
Driver.execute(chiselArgs ++ Array("--target-dir", m.core.root, "--top-name", m.core.name), m.constr)
m.core.postBuildActions map (fn => fn.apply(m.config))
val json = "%s/%s.json".format(m.core.root, m.core.name)
m.core.write(json)
s"${packaging.toString} %s".format(json).!
}
}
}