From 4ad98ed8e2b3412c862f9fbc7471110c70df9988 Mon Sep 17 00:00:00 2001 From: Asherah Connor Date: Wed, 15 May 2024 18:25:48 +0300 Subject: [PATCH] generate compile_commands.json. why not. --- build.sbt | 1 + .../scala/ee/hrzn/chryse/tasks/BaseTask.scala | 9 +++++---- .../scala/ee/hrzn/chryse/tasks/BuildTask.scala | 9 ++++++--- .../ee/hrzn/chryse/tasks/CxxsimTask.scala | 18 +++++++++++++++++- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/build.sbt b/build.sbt index 559a73e..3bb0269 100644 --- a/build.sbt +++ b/build.sbt @@ -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", diff --git a/src/main/scala/ee/hrzn/chryse/tasks/BaseTask.scala b/src/main/scala/ee/hrzn/chryse/tasks/BaseTask.scala index fa1871c..8335fe1 100644 --- a/src/main/scala/ee/hrzn/chryse/tasks/BaseTask.scala +++ b/src/main/scala/ee/hrzn/chryse/tasks/BaseTask.scala @@ -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")) @@ -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))) } diff --git a/src/main/scala/ee/hrzn/chryse/tasks/BuildTask.scala b/src/main/scala/ee/hrzn/chryse/tasks/BuildTask.scala index 20a3abd..7ea0265 100644 --- a/src/main/scala/ee/hrzn/chryse/tasks/BuildTask.scala +++ b/src/main/scala/ee/hrzn/chryse/tasks/BuildTask.scala @@ -38,7 +38,8 @@ object BuildTask extends BaseTask { ) val yosysCu = CompilationUnit( - Seq(verilogPath, yosysScriptPath), + Some(verilogPath), + Seq(yosysScriptPath), jsonPath, Seq( "yosys", @@ -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, @@ -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), ) diff --git a/src/main/scala/ee/hrzn/chryse/tasks/CxxsimTask.scala b/src/main/scala/ee/hrzn/chryse/tasks/CxxsimTask.scala index d0b7662..57218b1 100644 --- a/src/main/scala/ee/hrzn/chryse/tasks/CxxsimTask.scala +++ b/src/main/scala/ee/hrzn/chryse/tasks/CxxsimTask.scala @@ -92,6 +92,7 @@ object CxxsimTask extends BaseTask { ) val yosysCu = CompilationUnit( + None, Seq(blackboxIlPath, verilogPath, yosysScriptPath), ccPath, Seq( @@ -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), @@ -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 +}