Skip to content
This repository has been archived by the owner on Jun 16, 2024. It is now read-only.

Commit

Permalink
CxxrtlTask: build with Zig when asked.
Browse files Browse the repository at this point in the history
  • Loading branch information
kivikakk committed Jun 7, 2024
1 parent 4652a02 commit 990fc10
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@
package ee.hrzn.chryse.platform.cxxrtl

import chisel3._
import ee.hrzn.chryse.build.filesInDirWithExt
import ee.hrzn.chryse.platform.ElaboratablePlatform

abstract case class CxxrtlPlatform(id: String) extends ElaboratablePlatform {
abstract case class CxxrtlPlatform(id: String, useZig: Boolean = false)
extends ElaboratablePlatform {
type TopPlatform[Top <: Module] = Top

var cxxOpts: Seq[String] = Seq("-std=c++17", "-g", "-pedantic", "-Wall",
"-Wextra", "-Wno-zero-length-array", "-Wno-unused-parameter")

def ccs(simDir: String, cxxrtlCcPath: String): Seq[String] =
Seq(cxxrtlCcPath) ++ filesInDirWithExt(simDir, ".cc")

// XXX: just depend on what look like headers for now.
def depsFor(simDir: String, ccPath: String): Seq[String] =
filesInDirWithExt(simDir, ".h").toSeq

override def apply[Top <: Module](genTop: => Top) =
genTop
}
8 changes: 0 additions & 8 deletions src/main/scala/ee/hrzn/chryse/tasks/BaseTask.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,6 @@ trait BaseTask {
run.foreach(_.markUpToDate())
}

protected def filesInDirWithExt(dir: String, ext: String): Iterator[String] =
Files
.walk(Paths.get(dir), 1)
.iterator
.asScala
.map(_.toString)
.filter(_.endsWith(ext))

protected def logFileBetween(
path: String,
start: Regex,
Expand Down
46 changes: 26 additions & 20 deletions src/main/scala/ee/hrzn/chryse/tasks/CxxrtlTask.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import java.nio.file.Files
import java.nio.file.Paths
import scala.collection.mutable
import scala.sys.process._
import scala.util.matching.Regex

private[chryse] object CxxrtlTask extends BaseTask {
private val simDir = "cxxrtl"
Expand Down Expand Up @@ -104,8 +105,7 @@ private[chryse] object CxxrtlTask extends BaseTask {
)
runCu(CmdStepSynthesise, yosysCu)

val ccs = Seq(ccPath) ++ filesInDirWithExt(simDir, ".cc")
val headers = filesInDirWithExt(simDir, ".h").toSeq
val ccs = platform.ccs(simDir, ccPath)

val yosysDatDir = Seq("yosys-config", "--datdir").!!.trim()
val cxxOpts = new mutable.ArrayBuffer[String]
Expand All @@ -114,27 +114,29 @@ private[chryse] object CxxrtlTask extends BaseTask {
if (runOptions.debug) cxxOpts.append("-g")
if (runOptions.optimize) cxxOpts.append("-O3")

def buildPathForCc(cc: String) =
cc.replace(s"$simDir/", s"$buildDir/${platform.id}/")
.replace(".cc", ".o")

def compileCmdForCc(cc: String, obj: String): Seq[String] = Seq(
"c++",
s"-I$buildDir/${platform.id}",
s"-I$buildDir", // XXX: other artefacts the user might generate
s"-I$yosysDatDir/include/backends/cxxrtl/runtime",
"-c",
cc,
"-o",
obj,
) ++ cxxOpts ++ appOptions.allCxxFlags

// XXX: depend on what look like headers for now.
def buildPathForCc(cc: String) = {
val inBuildDir = ("^" + Regex.quote(s"$simDir/")).r
.replaceFirstIn(cc, s"$buildDir/${platform.id}/")
"\\.cc$".r.replaceFirstIn(inBuildDir, ".o")
}

def compileCmdForCc(cc: String, obj: String): Seq[String] =
(if (platform.useZig) Seq("zig") else Seq()) ++ Seq(
"c++",
s"-I$buildDir/${platform.id}",
s"-I$buildDir", // XXX: other artefacts the user might generate
s"-I$yosysDatDir/include/backends/cxxrtl/runtime",
"-c",
cc,
"-o",
obj,
) ++ cxxOpts ++ appOptions.allCxxFlags

val cus = for {
cc <- ccs
obj = buildPathForCc(cc)
cmd = compileCmdForCc(cc, obj)
} yield CompilationUnit(Some(cc), headers, obj, cmd)
} yield CompilationUnit(Some(cc), platform.depsFor(simDir, cc), obj, cmd)

// clangd won't look deeper than $buildDir, so just overwrite.
writePath(s"$buildDir/compile_commands.json") { wr =>
Expand All @@ -148,7 +150,11 @@ private[chryse] object CxxrtlTask extends BaseTask {
None,
cus.map(_.outPath),
binPath,
Seq("c++", "-o", binPath) ++ cxxOpts ++ cus.map(
(if (platform.useZig) Seq("zig") else Seq()) ++ Seq(
"c++",
"-o",
binPath,
) ++ cxxOpts ++ cus.map(
_.outPath,
) ++ appOptions.allLdFlags,
)
Expand Down

0 comments on commit 990fc10

Please sign in to comment.