Skip to content

Commit 6eeb672

Browse files
committed
Link directly to a JAR
1 parent cd2d79c commit 6eeb672

File tree

6 files changed

+38
-21
lines changed

6 files changed

+38
-21
lines changed

compiler/src/dotty/tools/dotc/CompilationUnit.scala

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ package dotc
33

44
import dotty.tools.dotc.core.Types.Type // Do not remove me #3383
55
import util.SourceFile
6-
import ast.{tpd, untpd}
7-
import dotty.tools.dotc.ast.tpd.{ Tree, TreeTraverser }
6+
import dotty.tools.dotc.ast.{ tpd, untpd }
87
import dotty.tools.dotc.core.Contexts.Context
98
import dotty.tools.dotc.core.SymDenotations.ClassDenotation
109
import dotty.tools.dotc.core.Symbols._
@@ -26,17 +25,17 @@ class CompilationUnit(val source: SourceFile) {
2625
object CompilationUnit {
2726

2827
/** Make a compilation unit for top class `clsd` with the contends of the `unpickled` */
29-
def mkCompilationUnit(clsd: ClassDenotation, unpickled: Tree, forceTrees: Boolean)(implicit ctx: Context): CompilationUnit = {
28+
def mkCompilationUnit(clsd: ClassDenotation, unpickled: tpd.Tree, forceTrees: Boolean)(implicit ctx: Context): CompilationUnit = {
3029
assert(!unpickled.isEmpty, unpickled)
31-
val unit1 = new CompilationUnit(new SourceFile(clsd.symbol.sourceFile, Seq()))
30+
val unit1 = new CompilationUnit(new SourceFile(clsd.symbol.associatedFile, Seq()))
3231
unit1.tpdTree = unpickled
3332
if (forceTrees)
3433
force.traverse(unit1.tpdTree)
3534
unit1
3635
}
3736

3837
/** Force the tree to be loaded */
39-
private object force extends TreeTraverser {
40-
def traverse(tree: Tree)(implicit ctx: Context): Unit = traverseChildren(tree)
38+
private object force extends tpd.TreeTraverser {
39+
def traverse(tree: tpd.Tree)(implicit ctx: Context): Unit = traverseChildren(tree)
4140
}
4241
}

compiler/src/dotty/tools/dotc/transform/LinkAll.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class LinkAll extends Phase {
3434
}
3535
}
3636

37-
if (ctx.settings.Xlink.value) allUnits(Set.empty, units.toSet, Set.empty)
37+
if (doLink) allUnits(Set.empty, units.toSet, Set.empty)
3838
else units
3939
}
4040

@@ -62,6 +62,16 @@ class LinkAll extends Phase {
6262
else acc + topClass
6363
}
6464
}
65+
66+
private def doLink(implicit ctx: Context): Boolean = {
67+
val link = ctx.settings.Xlink
68+
if (link.value) {
69+
val out = ctx.settings.outputDir
70+
if (out.value.endsWith(".jar"))
71+
ctx.error("With " + link.name + " the output directory " + out.name + " should be a .jar\n" + out.value)
72+
true
73+
} else false
74+
}
6575
}
6676

6777
object LinkAll {

compiler/test/dotty/tools/dotc/LinkTests.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dotty
22
package tools
33
package dotc
44

5+
import dotty.tools.io.Jar
6+
57
import java.io.{File => JFile}
68
import java.nio.file.{Files, Path, Paths}
79

@@ -61,11 +63,11 @@ class LinkTests extends ParallelTesting {
6163
for (check <- new JFile(sourceDir).listFiles().filter(_.toString.endsWith(checkExt))) {
6264
val outDir = {
6365
def path(str: String) = str.substring(linkDir.length, str.length - checkExt.length)
64-
defaultOutputDir + testName + path(check.toString) + "/"
66+
defaultOutputDir + testName + path(check.toString) + "/linked.jar"
6567
}
6668
val expectedClasses = scala.io.Source.fromFile(check).getLines().toSet
67-
val actualClasses = Files.walk(Paths.get(outDir)).iterator().asScala.collect {
68-
case f if f.toString.endsWith(".class") => f.toString.substring(outDir.length, f.toString.length - ".class".length)
69+
val actualClasses = new Jar(outDir).iterator.collect {
70+
case f if f.toString.endsWith(".class") => f.toString.substring(0, f.toString.length - ".class".length)
6971
}.toSet
7072
assertEquals(check.toString, expectedClasses, actualClasses)
7173
}

compiler/test/dotty/tools/dotc/MissingCoreLibTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class MissingCoreLibTests {
1212
@Test def missingDottyLib: Unit = {
1313
val classPath = mkClassPath(Jars.dottyCompiler :: Jars.dottyInterfaces :: Jars.dottyExtras) // missing Jars.dottyLib
1414
val source = "../tests/neg/nolib/Foo.scala"
15-
val options = Array("-classpath", classPath, source)
15+
val options = Array("-classpath", classPath, "-d", "../out/", source)
1616
val reporter = Main.process(options)
1717
assertEquals(1, reporter.errorCount)
1818
val errorMessage = reporter.allErrors.head.message

compiler/test/dotty/tools/vulpix/ParallelTesting.scala

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,18 +1027,22 @@ trait ParallelTesting extends RunnerOrchestration { self =>
10271027
}
10281028

10291029
/** Create out directory for directory `d` */
1030-
private def createOutputDirsForDir(d: JFile, sourceDir: JFile, outDir: String): JFile = {
1030+
private def createOutputDirsForDir(d: JFile, sourceDir: JFile, outDir: String, flags: TestFlags): JFile = {
10311031
val targetDir = new JFile(outDir + s"${sourceDir.getName}/${d.getName}")
10321032
targetDir.mkdirs()
1033-
targetDir
1033+
jaredIfNeeded(targetDir, flags)
10341034
}
10351035

10361036
/** Create out directory for `file` */
1037-
private def createOutputDirsForFile(file: JFile, sourceDir: JFile, outDir: String): JFile = {
1037+
private def createOutputDirsForFile(file: JFile, sourceDir: JFile, outDir: String, flags: TestFlags): JFile = {
10381038
val uniqueSubdir = file.getName.substring(0, file.getName.lastIndexOf('.'))
10391039
val targetDir = new JFile(outDir + s"${sourceDir.getName}/$uniqueSubdir")
10401040
targetDir.mkdirs()
1041-
targetDir
1041+
jaredIfNeeded(targetDir, flags)
1042+
}
1043+
1044+
private def jaredIfNeeded(targetDir: JFile, flags: TestFlags): JFile = {
1045+
new JFile(targetDir + (if (flags.isLinkTest) "/linked.jar" else ""))
10421046
}
10431047

10441048
/** Make sure that directory string is as expected */
@@ -1073,7 +1077,7 @@ trait ParallelTesting extends RunnerOrchestration { self =>
10731077
testGroup.name,
10741078
Array(sourceFile),
10751079
flags,
1076-
createOutputDirsForFile(sourceFile, parent, outDir)
1080+
createOutputDirsForFile(sourceFile, parent, outDir, flags)
10771081
)
10781082
new CompilationTest(target)
10791083
}
@@ -1095,7 +1099,7 @@ trait ParallelTesting extends RunnerOrchestration { self =>
10951099
(parent ne null) && parent.exists && parent.isDirectory,
10961100
s"Source file: $f, didn't exist"
10971101
)
1098-
val tastySource = createOutputDirsForFile(sourceFile, parent, outDir)
1102+
val tastySource = createOutputDirsForFile(sourceFile, parent, outDir, flags)
10991103
val target = JointCompilationSource(
11001104
testGroup.name,
11011105
Array(sourceFile),
@@ -1180,8 +1184,8 @@ trait ParallelTesting extends RunnerOrchestration { self =>
11801184
val (dirs, files) = compilationTargets(sourceDir)
11811185

11821186
val targets =
1183-
files.map(f => JointCompilationSource(testGroup.name, Array(f), flags, createOutputDirsForFile(f, sourceDir, outDir))) ++
1184-
dirs.map(dir => SeparateCompilationSource(testGroup.name, dir, flags, createOutputDirsForDir(dir, sourceDir, outDir)))
1187+
files.map(f => JointCompilationSource(testGroup.name, Array(f), flags, createOutputDirsForFile(f, sourceDir, outDir, flags))) ++
1188+
dirs.map(dir => SeparateCompilationSource(testGroup.name, dir, flags, createOutputDirsForDir(dir, sourceDir, outDir, flags)))
11851189

11861190
// Create a CompilationTest and let the user decide whether to execute a pos or a neg test
11871191
new CompilationTest(targets)
@@ -1217,7 +1221,7 @@ trait ParallelTesting extends RunnerOrchestration { self =>
12171221

12181222
val targets =
12191223
files.map { f =>
1220-
val classpath = createOutputDirsForFile(f, sourceDir, outDir)
1224+
val classpath = createOutputDirsForFile(f, sourceDir, outDir, flags)
12211225
JointCompilationSource(testGroup.name, Array(f), flags.withClasspath(classpath.getPath), classpath, fromTasty = true)
12221226
}
12231227
// TODO add SeparateCompilationSource from tasty?
@@ -1240,7 +1244,7 @@ trait ParallelTesting extends RunnerOrchestration { self =>
12401244
val (_, files) = compilationTargets(sourceDir)
12411245

12421246
val targets = files.map { file =>
1243-
JointCompilationSource(testGroup.name, Array(file), flags, createOutputDirsForFile(file, sourceDir, outDir))
1247+
JointCompilationSource(testGroup.name, Array(file), flags, createOutputDirsForFile(file, sourceDir, outDir, flags))
12441248
}
12451249

12461250
// Create a CompilationTest and let the user decide whether to execute a pos or a neg test

compiler/test/dotty/tools/vulpix/TestFlags.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ final case class TestFlags(
1515
TestFlags(s"$defaultClassPath:$classPath", runClassPath, options)
1616

1717
def all: Array[String] = Array("-classpath", defaultClassPath) ++ options
18+
19+
def isLinkTest: Boolean = options.contains("-Xlink")
1820
}
1921

2022
object TestFlags {

0 commit comments

Comments
 (0)