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

Commit

Permalink
generate compile_commands.json. why not.
Browse files Browse the repository at this point in the history
  • Loading branch information
kivikakk committed May 15, 2024
1 parent 1a0f98c commit 4ad98ed
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ lazy val root = (project in file("."))
"org.scalatest" %% "scalatest" % "3.2.18" % "test",
"edu.berkeley.cs" %% "chiseltest" % "6.0.0",
"com.github.scopt" %% "scopt" % "4.1.0",
"com.lihaoyi" %% "upickle" % "3.1.0",
),
scalacOptions ++= Seq(
"-language:reflectiveCalls", "-deprecation", "-feature", "-Xcheckinit",
Expand Down
9 changes: 5 additions & 4 deletions src/main/scala/ee/hrzn/chryse/tasks/BaseTask.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ abstract class BaseTask {
}

case class CompilationUnit(
val inPaths: Seq[String],
val primaryInPath: Option[String],
val otherInPaths: Seq[String],
val outPath: String,
val cmd: Seq[String],
) {
val digestPath = s"$outPath.dig"
private val sortedInPaths = inPaths.sorted
private val sortedInPaths = otherInPaths.appendedAll(primaryInPath).sorted

private def addIntToDigest(n: Int)(implicit digest: MessageDigest): Unit =
digest.update(String.format("%08x", n).getBytes("UTF-8"))
Expand All @@ -85,8 +86,8 @@ abstract class BaseTask {

private def digestInsWithCmd(): String = {
implicit val digest = MessageDigest.getInstance("SHA-256")
addIntToDigest(inPaths.length)
for { inPath <- inPaths.sorted } {
addIntToDigest(sortedInPaths.length)
for { inPath <- sortedInPaths } {
addStringToDigest(inPath)
addBytesToDigest(Files.readAllBytes(Paths.get(inPath)))
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/scala/ee/hrzn/chryse/tasks/BuildTask.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ object BuildTask extends BaseTask {
)

val yosysCu = CompilationUnit(
Seq(verilogPath, yosysScriptPath),
Some(verilogPath),
Seq(yosysScriptPath),
jsonPath,
Seq(
"yosys",
Expand All @@ -57,7 +58,8 @@ object BuildTask extends BaseTask {

val ascPath = s"$buildDir/$name-${platform.id}.asc"
val ascCu = CompilationUnit(
Seq(jsonPath, pcfPath),
Some(jsonPath),
Seq(pcfPath),
ascPath,
Seq(
platform.nextpnrBinary,
Expand All @@ -76,7 +78,8 @@ object BuildTask extends BaseTask {

val binPath = s"$buildDir/$name-${platform.id}.bin"
val binCu = CompilationUnit(
Seq(ascPath),
Some(ascPath),
Seq(),
binPath,
Seq(platform.packBinary, ascPath, binPath),
)
Expand Down
18 changes: 17 additions & 1 deletion src/main/scala/ee/hrzn/chryse/tasks/CxxsimTask.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ object CxxsimTask extends BaseTask {
)

val yosysCu = CompilationUnit(
None,
Seq(blackboxIlPath, verilogPath, yosysScriptPath),
ccPath,
Seq(
Expand Down Expand Up @@ -143,12 +144,21 @@ object CxxsimTask extends BaseTask {
cc <- ccs
obj = buildPathForCc(cc)
cmd = compileCmdForCc(cc, obj)
} yield CompilationUnit(Seq(cc) ++ headers, obj, cmd)
} yield CompilationUnit(Some(cc), headers, obj, cmd)

runCus("compilation", cus)

val cwd = System.getProperty("user.dir")
writePath(s"$buildDir/compile_commands.json") { wr =>
upickle.default.writeTo(
cus.map(cu => ClangdEntry(cwd, cu.primaryInPath.get, cu.cmd)),
wr,
)
}

val binPath = s"$buildDir/$name"
val linkCu = CompilationUnit(
None,
cus.map(_.outPath),
binPath,
Seq("c++", "-o", binPath) ++ cxxOpts ++ cus.map(_.outPath),
Expand Down Expand Up @@ -177,3 +187,9 @@ object CxxsimTask extends BaseTask {
.map(_.toString)
.filter(_.endsWith(ext))
}

private case class ClangdEntry(directory: String, file: String, arguments: Seq[String])
private object ClangdEntry {
implicit val rw: upickle.default.ReadWriter[ClangdEntry] =
upickle.default.macroRW
}

0 comments on commit 4ad98ed

Please sign in to comment.