Skip to content

Commit

Permalink
Add support for alternative system images to the compiler
Browse files Browse the repository at this point in the history
This PR adds a new `-system` / `--system` setting to scalac which mimics its counterpart in javac.

This fixes the biggest problem (at least for us) of scala/bug#13015. It is now possible to compile code against an older system image without enforcing strict module access. scalac generally does not enforce modules (scala/scala-dev#529) but it does when using `-release` (with class lookup based on `ct.sym`) and there was no way to opt out of these restrictions.

The usual opt-out in javac is `--add-exports` but it is not supported for system modules in combination with `--release` (https://bugs.openjdk.org/browse/JDK-8178152) so there is no expectation that scalac could support it. Instead the solution for javac is to replace `--release` with a combination of `-source`, `-target` and a system image for the target version via `--system`. This combination, unlike `--release`, can be used with `--add-exports`. If scalac adds full module support at a later time (with access restrictions enabled by default) it will also need to support `--add-exports` and allow its use in combination with `--system`.

I am also un-deprecating `-target` (which was deprecated in favor of `-release`) because it now has a legitimate use in combination with an alternative system image (just like in javac where it is serves the same purpose and is not deprecated, either).
  • Loading branch information
szeiger committed Jul 5, 2024
1 parent bd623d6 commit f0d9912
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/compiler/scala/tools/nsc/Global.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class Global(var currentSettings: Settings, reporter0: Reporter)
def optimizerClassPath(base: ClassPath): ClassPath =
base match {
case AggregateClassPath(entries) if entries.head.isInstanceOf[CtSymClassPath] =>
JrtClassPath(release = None, closeableRegistry)
JrtClassPath(release = None, settings.systemPathValue, closeableRegistry)
.map(jrt => AggregateClassPath(entries.drop(1).prepended(jrt)))
.getOrElse(base)
case _ => base
Expand Down
19 changes: 14 additions & 5 deletions src/compiler/scala/tools/nsc/classpath/DirectoryClassPath.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package scala.tools.nsc.classpath

import java.io.{Closeable, File}
import java.net.{URI, URL}
import java.util.Collections

import scala.reflect.io.{AbstractFile, PlainFile, PlainNioFile}
import scala.tools.nsc.util.{ClassPath, ClassRepresentation, EfficientClassPath}
Expand Down Expand Up @@ -130,9 +131,9 @@ trait JFileDirectoryLookup[FileEntryType <: ClassRepresentation] extends Directo

object JrtClassPath {
import java.nio.file._, java.net.URI
private val jrtClassPathCache = new FileBasedCache[Unit, JrtClassPath]()
private val jrtClassPathCache = new FileBasedCache[Option[String], JrtClassPath]()
private val ctSymClassPathCache = new FileBasedCache[String, CtSymClassPath]()
def apply(release: Option[String], closeableRegistry: CloseableRegistry): Option[ClassPath] = {
def apply(release: Option[String], systemPath: Option[String], closeableRegistry: CloseableRegistry): Option[ClassPath] = {
import scala.util.Properties._
if (!isJavaAtLeast("9")) None
else {
Expand All @@ -158,8 +159,13 @@ object JrtClassPath {
}
case _ =>
try {
val fs = FileSystems.getFileSystem(URI.create("jrt:/"))
val classPath = jrtClassPathCache.getOrCreate((), Nil, () => new JrtClassPath(fs), closeableRegistry, false)
val classPath = jrtClassPathCache.getOrCreate(systemPath, Nil, () => {
val fs = systemPath match {
case Some(javaHome) => FileSystems.newFileSystem(URI.create("jrt:/"), Collections.singletonMap("java.home", javaHome))
case None => FileSystems.getFileSystem(URI.create("jrt:/"))
}
new JrtClassPath(fs, systemPath.isDefined)
}, closeableRegistry, false)
Some(classPath)
} catch {
case _: ProviderNotFoundException | _: FileSystemNotFoundException => None
Expand All @@ -177,7 +183,7 @@ object JrtClassPath {
*
* The implementation assumes that no classes exist in the empty package.
*/
final class JrtClassPath(fs: java.nio.file.FileSystem) extends ClassPath with NoSourcePaths {
final class JrtClassPath(fs: java.nio.file.FileSystem, closeFS: Boolean) extends ClassPath with NoSourcePaths with Closeable {
import java.nio.file.Path, java.nio.file._
type F = Path
private val dir: Path = fs.getPath("/packages")
Expand Down Expand Up @@ -224,6 +230,9 @@ final class JrtClassPath(fs: java.nio.file.FileSystem) extends ClassPath with No
}.take(1).toList.headOption
}
}

def close(): Unit =
if(closeFS) fs.close()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ trait StandardScalaSettings { _: MutableSettings =>
val javaextdirs = PathSetting ("-javaextdirs", "Override java extdirs classpath.", Defaults.javaExtDirs) withAbbreviation "--java-extension-directories"
val sourcepath = PathSetting ("-sourcepath", "Specify location(s) of source files.", "") withAbbreviation "--source-path" // Defaults.scalaSourcePath
val rootdir = PathSetting ("-rootdir", "The absolute path of the project root directory, usually the git/scm checkout. Used by -Wconf.", "") withAbbreviation "--root-directory"
val systemPath = PathSetting ("-system", "Override location of Java system modules", "") withAbbreviation "--system"

/** Other settings.
*/
Expand Down Expand Up @@ -75,11 +76,13 @@ trait StandardScalaSettings { _: MutableSettings =>
val current = setting.value.toInt
if (!isJavaAtLeast("9") && current > 8) errorFn.apply("-release is only supported on JVM 9 and higher")
if (target.valueSetByUser.map(_.toInt > current).getOrElse(false)) errorFn("-release cannot be less than -target")
if (systemPath.isSetByUser) errorFn("-release cannot be used with -system")
//target.value = setting.value // this would trigger deprecation
}
.withAbbreviation("--release")
// .withAbbreviation("-java-output-version")
def releaseValue: Option[String] = release.valueSetByUser
def systemPathValue: Option[String] = systemPath.valueSetByUser
val target =
ChoiceSetting("-target", "target", "Target platform for object files.", AllTargetVersions, "8")
.withPreSetHook(normalizeTarget)
Expand All @@ -90,7 +93,6 @@ trait StandardScalaSettings { _: MutableSettings =>
// .withAbbreviation("--Xtarget")
// .withAbbreviation("-Xtarget")
// .withAbbreviation("-Xunchecked-java-output-version")
.withDeprecationMessage("Use -release instead to compile against the correct platform API.")
def targetValue: String = target.valueSetByUser.orElse(releaseValue).getOrElse(target.value)
val unchecked = BooleanSetting ("-unchecked", "Enable additional warnings where generated code depends on assumptions. See also -Wconf.") withAbbreviation "--unchecked" withPostSetHook { s =>
if (s.value) Wconf.tryToSet(List(s"cat=unchecked:w"))
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/scala/tools/util/PathResolver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ final class PathResolver(settings: Settings, closeableRegistry: CloseableRegistr
sourcesInPath(sourcePath) // 7. The Scala source path.
)

private def jrt: Option[ClassPath] = JrtClassPath.apply(settings.releaseValue, closeableRegistry)
private def jrt: Option[ClassPath] = JrtClassPath.apply(settings.releaseValue, settings.systemPathValue, closeableRegistry)

lazy val containers = basis.flatten.distinct

Expand Down

0 comments on commit f0d9912

Please sign in to comment.