Skip to content

Commit 558a2bd

Browse files
committed
moved getProjectRootOrCwd to Simulation object and used it in TestingDirectory
+ added CHISEL_PROJECT_ROOT as an alternative for detectig project root + fixed tests to work with with both relative and absolute HasTestingDirectory.getDirectory path
1 parent 78d8900 commit 558a2bd

File tree

5 files changed

+24
-15
lines changed

5 files changed

+24
-15
lines changed

src/main/scala/chisel3/testing/FileCheck.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ trait FileCheck {
8989
// Filecheck needs to have the thing to check in a file.
9090
//
9191
// TODO: This could be made ephemeral or use a named pipe?
92-
// os.makeDir(os.RelPath(testingDirectory.getDirectory))
93-
val dir = os.pwd / os.RelPath(testingDirectory.getDirectory)
92+
val testingDir = os.FilePath(testingDirectory.getDirectory)
93+
val dir = testingDir.resolveFrom(os.pwd)
9494
os.makeDir.all(dir)
9595
val tempDir = os.temp.dir(dir = dir, deleteOnExit = false)
9696
val checkFile = tempDir / "check"

src/main/scala/chisel3/testing/scalatest/WithTestingDirectory.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package chisel3.testing.scalatest
44

55
import chisel3.testing.HasTestingDirectory
6+
import svsim.Simulation
67
import java.nio.file.{FileSystems, Path, Paths}
78
import org.scalatest.{TestSuite, TestSuiteMixin}
89
import scala.util.DynamicVariable
@@ -32,7 +33,7 @@ trait TestingDirectory extends TestSuiteMixin { self: TestSuite =>
3233
*
3334
* For different behavior, please override this in your test suite.
3435
*/
35-
def buildDir: Path = Paths.get("build", "chiselsim")
36+
def buildDir: Path = Simulation.getProjectRootOrCwd.resolve("build").resolve("chiselsim")
3637

3738
// Assemble all the directories that should be created for this test. This is
3839
// done by examining the test (via a fixture) and then setting a dynamic

src/test/scala-2/chiselTests/experimental/TraceSpec.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class TraceSpec extends AnyFlatSpec with Matchers with TestingDirectory {
2626
def compile(testName: String, gen: () => Module)(
2727
implicit testingDirectory: HasTestingDirectory
2828
): (os.Path, AnnotationSeq) = {
29-
val testDir = os.pwd / os.RelPath(testingDirectory.getDirectory)
29+
val testDir = os.FilePath(testingDirectory.getDirectory).resolveFrom(os.pwd)
3030
val annos = (new ChiselStage).execute(
3131
Array("--target-dir", s"$testDir", "--target", "systemverilog", "--split-verilog"),
3232
Seq(

src/test/scala-2/chiselTests/stage/WarningConfigurationSpec.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class WarningConfigurationSpec extends AnyFunSpec with Matchers with chiselTests
9292
}
9393

9494
private def makeFile(name: String)(contents: String)(implicit testingDirectory: HasTestingDirectory): java.io.File = {
95-
val dir = os.pwd / os.RelPath(testingDirectory.getDirectory)
95+
val dir = os.FilePath(testingDirectory.getDirectory).resolveFrom(os.pwd)
9696
os.makeDir.all(dir)
9797
val file = dir / name
9898
os.write.over(file, contents)

svsim/src/main/scala/Simulation.scala

+18-10
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,12 @@ final class Simulation private[svsim] (
1515
) {
1616
private val executionScriptPath = s"$workingDirectory/execution-script.txt"
1717

18-
protected final def getProjectRootOrCwd: Path =
19-
sys.props
20-
.get("chisel.project.root")
21-
.orElse(sys.env.get("MILL_WORKSPACE_ROOT"))
22-
.orElse(sys.env.get("PWD"))
23-
.map(Paths.get("").toAbsolutePath.resolve(_))
24-
.getOrElse(Paths.get(""))
25-
.toAbsolutePath
26-
2718
def workingDirectory: Path = {
2819
val path = Paths.get(workingDirectoryPath)
2920
if (path.isAbsolute) {
3021
path
3122
} else {
32-
getProjectRootOrCwd.resolve(path)
23+
Simulation.getProjectRootOrCwd.resolve(path)
3324
}
3425
}
3526

@@ -460,4 +451,21 @@ object Simulation {
460451
}
461452
}
462453
}
454+
455+
/**
456+
* Tries to detect the project root (workspace) directory based on "chisel.project.root" Java system property
457+
* as well as CHISEL_PROJECT_ROOT, MILL_WORKSPACE_ROOT (set by mill), and PWD environment variables (tried in that order).
458+
* If none of those work, returns the current working directory.
459+
*
460+
* @return the absolute path to the project root or current working directory
461+
*/
462+
def getProjectRootOrCwd: Path =
463+
sys.props
464+
.get("chisel.project.root")
465+
.orElse(sys.env.get("CHISEL_PROJECT_ROOT"))
466+
.orElse(sys.env.get("MILL_WORKSPACE_ROOT"))
467+
.orElse(sys.env.get("PWD"))
468+
.map(Paths.get("").toAbsolutePath.resolve(_))
469+
.getOrElse(Paths.get(""))
470+
.toAbsolutePath
463471
}

0 commit comments

Comments
 (0)