Skip to content

Commit a115a4c

Browse files
committed
Do not use coursier cache
Switch scala-cli to use TempCache and create DirCache for our build Move some classes from BytecodeProcessor to dedicated files
1 parent c40c47e commit a115a4c

File tree

6 files changed

+66
-51
lines changed

6 files changed

+66
-51
lines changed

build.sc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -584,14 +584,13 @@ trait Cli3 extends Cli {
584584
override def myScalaVersion = Scala.scala3
585585

586586
override def nativeImageClassPath = T {
587-
// TODO - results are cached - need to add inputs or caching method
588-
val classpath = super.nativeImageClassPath().map(_.path).mkString(File.pathSeparator)
589-
val coursierCache = coursier.cache.FileCache[coursier.util.Task]().location.toString
587+
val classpath = super.nativeImageClassPath().map(_.path).mkString(File.pathSeparator)
588+
val cache = T.dest / "native-cp"
590589
// `scala3-graal-processor`.run() do not give me output and I cannot pass dynamically computed values like classpath
591590
val res = mill.modules.Jvm.callSubprocess(
592591
mainClass = `scala3-graal-processor`.finalMainClass(),
593592
classPath = `scala3-graal-processor`.runClasspath().map(_.path),
594-
mainArgs = Seq(coursierCache, classpath),
593+
mainArgs = Seq(cache.toNIO.toString, classpath),
595594
workingDir = os.pwd
596595
)
597596
val cp = res.out.text

modules/cli/src/main/scala/scala/cli/packaging/NativeImage.scala

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package scala.cli.packaging
22

3-
import coursier.cache.FileCache
4-
53
import java.io.{File, OutputStream}
64

75
import scala.annotation.tailrec
86
import scala.build.internal.{NativeBuilderHelper, Runner}
97
import scala.build.{Build, Logger}
108
import scala.cli.errors.GraalVMNativeImageError
11-
import scala.cli.graal.{BytecodeProcessor, CoursierCache}
9+
import scala.cli.graal.{BytecodeProcessor, TempCache}
1210
import scala.util.Properties
1311

1412
object NativeImage {
@@ -239,11 +237,9 @@ object NativeImage {
239237
if (!build.scalaParams.scalaBinaryVersion.startsWith("3"))
240238
(processedClassPath, Seq[os.Path](), Seq[String]())
241239
else {
242-
val coursierCacheLocation = os.Path(FileCache().location.toPath())
243-
val cache = CoursierCache(coursierCacheLocation)
244-
val cpString = processedClassPath.mkString(File.pathSeparator)
245-
val processed = BytecodeProcessor.processClassPath(cpString, cache).toSeq
246-
val nativeConfigFile = os.temp(suffix = ".json")
240+
val cpString = processedClassPath.mkString(File.pathSeparator)
241+
val processed = BytecodeProcessor.processClassPath(cpString, TempCache).toSeq
242+
val nativeConfigFile = os.temp(suffix = ".json")
247243
os.write.over(
248244
nativeConfigFile,
249245
"""[

modules/scala3-graal-processor/src/scala/cli/graal/CoursierCacheProcessor.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package scala.cli.graal
22

33
import java.io.File
4+
import java.nio.channels.FileChannel
45

56
object CoursierCacheProcessor {
67
def main(args: Array[String]) = {
7-
val List(coursierCache, classpath) = args.toList
8-
val cache = CoursierCache(BytecodeProcessor.toPath(coursierCache))
8+
val List(cacheDir, classpath) = args.toList
9+
val cache = DirCache(os.Path(cacheDir, os.pwd))
910

10-
val newCp = BytecodeProcessor.processClasspath(classpath, cache).map(_.nioPath)
11+
val newCp = BytecodeProcessor.processClassPath(classpath, cache).map(_.nioPath)
1112

1213
println(newCp.mkString(File.pathSeparator))
1314
}

modules/scala3-graal/src/main/scala/scala/cli/graal/BytecodeProcessor.scala

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,11 @@ package scala.cli.graal
33
import org.objectweb.asm._
44

55
import java.io.{File, InputStream}
6-
import java.nio.file.{Files, Path, StandardOpenOption}
6+
import java.nio.file.{Files, StandardOpenOption}
77
import java.util.jar.{Attributes, JarEntry, JarFile, JarOutputStream, Manifest}
88

99
import scala.jdk.CollectionConverters._
1010

11-
trait JarCache {
12-
def cache(path: os.Path)(processPath: os.Path => ClassPathEntry): ClassPathEntry
13-
def put(entry: os.RelPath, bytes: Array[Byte]): ClassPathEntry
14-
}
15-
16-
sealed trait ClassPathEntry {
17-
def nioPath: Path = path.toNIO
18-
def path: os.Path
19-
def modified = true
20-
}
21-
case class Unmodified(path: os.Path) extends ClassPathEntry {
22-
override def modified: Boolean = false
23-
}
24-
case class Processed(path: os.Path, original: os.Path, cache: JarCache) extends ClassPathEntry
25-
case class CreatedEntry(path: os.Path) extends ClassPathEntry
26-
27-
case class PathingJar(jar: ClassPathEntry, entries: Seq[ClassPathEntry]) extends ClassPathEntry {
28-
override def path: os.Path = jar.path
29-
}
30-
31-
object TempCache extends JarCache {
32-
override def cache(path: os.Path)(processPath: os.Path => ClassPathEntry): ClassPathEntry =
33-
processPath(
34-
if (os.isDir(path)) os.temp.dir(prefix = path.last)
35-
else os.temp(prefix = path.baseName, suffix = path.ext)
36-
)
37-
38-
override def put(entry: os.RelPath, content: Array[Byte]): ClassPathEntry = {
39-
val path = os.temp(prefix = entry.baseName, suffix = entry.ext)
40-
os.write(path, content, createFolders = true)
41-
CreatedEntry(path)
42-
}
43-
44-
}
45-
4611
object BytecodeProcessor {
4712

4813
def toClean(classpath: Seq[ClassPathEntry]): Seq[os.Path] = classpath.flatMap {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package scala.cli.graal
2+
3+
import java.nio.file.Path
4+
5+
sealed trait ClassPathEntry {
6+
def nioPath: Path = path.toNIO
7+
def path: os.Path
8+
def modified = true
9+
}
10+
11+
case class Unmodified(path: os.Path) extends ClassPathEntry {
12+
override def modified: Boolean = false
13+
}
14+
case class Processed(path: os.Path, original: os.Path, cache: JarCache) extends ClassPathEntry
15+
case class CreatedEntry(path: os.Path) extends ClassPathEntry
16+
17+
case class PathingJar(jar: ClassPathEntry, entries: Seq[ClassPathEntry]) extends ClassPathEntry {
18+
override def path: os.Path = jar.path
19+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package scala.cli.graal
2+
3+
trait JarCache {
4+
def cache(path: os.Path)(processPath: os.Path => ClassPathEntry): ClassPathEntry
5+
def put(entry: os.RelPath, bytes: Array[Byte]): ClassPathEntry
6+
}
7+
8+
case class DirCache(dir: os.Path) extends JarCache {
9+
private def dest(original: os.Path) =
10+
dir / s"${original.toNIO.toString.hashCode()}-${original.last}"
11+
override def cache(path: os.Path)(processPath: os.Path => ClassPathEntry): ClassPathEntry =
12+
processPath(dest(path))
13+
14+
override def put(entry: os.RelPath, content: Array[Byte]): ClassPathEntry = {
15+
val path = dir / entry
16+
os.write.over(path, content, createFolders = true)
17+
CreatedEntry(path)
18+
}
19+
}
20+
21+
object TempCache extends JarCache {
22+
23+
override def cache(path: os.Path)(processPath: os.Path => ClassPathEntry): ClassPathEntry =
24+
processPath(
25+
if (os.isDir(path)) os.temp.dir(prefix = path.last)
26+
else os.temp(prefix = path.baseName, suffix = "." + path.ext)
27+
)
28+
29+
override def put(entry: os.RelPath, content: Array[Byte]): ClassPathEntry = {
30+
val path = os.temp(prefix = entry.baseName, suffix = entry.ext)
31+
os.write.over(path, content, createFolders = true)
32+
CreatedEntry(path)
33+
}
34+
35+
}

0 commit comments

Comments
 (0)