Skip to content

Commit 1162046

Browse files
committed
provide API to use absolute path for SourceInfo
- bug fix that chisel.project.root doesn't take effect - if CHISEL_PROJECT_ROOT_ABSOLUTE is defined, use absolute path in chisel SourceInfo - if CHISEL_PROJECT_ROOT_REPLACE is defined, replace CHISEL_PROJECT_ROOT or user.dir with it in chisel chisel SourceInfo
1 parent 96cc1b7 commit 1162046

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

core/src/main/scala/chisel3/internal/SourceInfoFileResolver.scala

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,28 @@ import scala.reflect.macros.blackbox.Context
1414
/** Scala compile-time function for determine the String used to represent a source file path in SourceInfos */
1515
private[internal] object SourceInfoFileResolver {
1616
def resolve(source: scala.reflect.internal.util.SourceFile): String = {
17-
val userDir = sys.props.get("user.dir") // Figure out what to do if not provided
18-
val projectRoot = sys.props.get("chisel.project.root")
17+
val userDir = sys.props.get("user.dir")
18+
val projectRoot = sys.env.get("CHISEL_PROJECT_ROOT")
19+
val isAbs = sys.env.contains("CHISEL_PROJECT_ROOT_ABSOLUTE")
20+
val projectRootReplace = sys.env.get("CHISEL_PROJECT_ROOT_REPLACE")
1921
val root = projectRoot.orElse(userDir)
2022

2123
val path = root.map(r => source.file.canonicalPath.stripPrefix(r)).getOrElse(source.file.name)
2224
val pathNoStartingSlash = if (path(0) == '/') path.tail else path
23-
pathNoStartingSlash
25+
26+
(root, isAbs, projectRootReplace) match {
27+
case (None, _, _) =>
28+
throw new Exception(s"Neither user.dir nor chisel.project.root is not defined.")
29+
case (Some(_), true, None) =>
30+
source.file.canonicalPath
31+
case (Some(_), true, Some(projectRootReplace)) =>
32+
s"$projectRootReplace/${pathNoStartingSlash}"
33+
case (Some(_), false, Some(projectRootReplace)) =>
34+
throw new Exception(
35+
s"chisel.project.root.replace defined ${projectRootReplace}, but chisel.project.root.absolute is not defined"
36+
)
37+
case (Some(_), false, None) =>
38+
pathNoStartingSlash
39+
}
2440
}
2541
}

0 commit comments

Comments
 (0)