enso/project/StdBits.scala
Pavel Marek 5a7ad6bfe4
Upgrade enso to GraalVM for jdk 21 (#7991)
Upgrade to GraalVM JDK 21.
```
> java -version
openjdk version "21" 2023-09-19
OpenJDK Runtime Environment GraalVM CE 21+35.1 (build 21+35-jvmci-23.1-b15)
OpenJDK 64-Bit Server VM GraalVM CE 21+35.1 (build 21+35-jvmci-23.1-b15, mixed mode, sharing)
```

With SDKMan, download with `sdk install java 21-graalce`.

# Important Notes
- After this PR, one can theoretically run enso with any JRE with version at least 21.
- Removed `sbt bootstrap` hack and all the other build time related hacks related to the handling of GraalVM distribution.
- `project-manager` remains backward compatible - it can open older engines with runtimes. New engines now do no longer require a separate runtime to be downloaded.
- sbt does not support compilation of `module-info.java` files in mixed projects - https://github.com/sbt/sbt/issues/3368
- Which means that we can have `module-info.java` files only for Java-only projects.
- Anyway, we need just a single `module-info.class` in the resulting `runtime.jar` fat jar.
- `runtime.jar` is assembled in `runtime-with-instruments` with a custom merge strategy (`sbt-assembly` plugin). Caching is disabled for custom merge strategies, which means that re-assembly of `runtime.jar` will be more frequent.
- Engine distribution contains multiple JAR archives (modules) in `component` directory, along with `runner/runner.jar` that is hidden inside a nested directory.
- The new entry point to the engine runner is [EngineRunnerBootLoader](https://github.com/enso-org/enso/pull/7991/files#diff-9ab172d0566c18456472aeb95c4345f47e2db3965e77e29c11694d3a9333a2aa) that contains a custom ClassLoader - to make sure that everything that does not have to be loaded from a module is loaded from `runner.jar`, which is not a module.
- The new command line for launching the engine runner is in [distribution/bin/enso](https://github.com/enso-org/enso/pull/7991/files#diff-0b66983403b2c329febc7381cd23d45871d4d555ce98dd040d4d1e879c8f3725)
- [Newest version of Frgaal](https://repo1.maven.org/maven2/org/frgaal/compiler/20.0.1/) (20.0.1) does not recognize `--source 21` option, only `--source 20`.
2023-11-17 18:02:36 +00:00

131 lines
4.7 KiB
Scala

import sbt.Keys._
import sbt._
import sbt.internal.util.ManagedLogger
import sbt.io.IO
import sbt.librarymanagement.{ConfigurationFilter, DependencyFilter}
import java.io.File
object StdBits {
/** Discovers dependencies of a project and copies them into the destination
* directory.
*
* @param destination location where to put the dependencies
* @param providedJarNames name of JARs generated by the local projects;
* unexpected (old) files are removed, so this task
* needs to know these files' names to avoid removing
* them
* @param ignoreScalaLibrary whether to ignore Scala dependencies that are
* added by default be SBT and are not relevant in
* pure-Java projects
*/
def copyDependencies(
destination: File,
providedJarNames: Seq[String],
ignoreScalaLibrary: Boolean
): Def.Initialize[Task[Unit]] =
Def.task {
val libraryUpdates = (Compile / update).value
val log = streams.value.log
val baseFilter: NameFilter = new ExactFilter(Configurations.Runtime.name)
val validConfig =
if (ignoreScalaLibrary)
baseFilter - new ExactFilter(Configurations.ScalaTool.name)
else baseFilter
val configFilter: ConfigurationFilter =
DependencyFilter.configurationFilter(name = validConfig)
val graalVmOrgs = GraalVM.modules.map(_.organization).distinct
// All graal related modules must be filtered away - they will be provided in
// module-path, and so, they must not be included in std-bits polyglot directories.
val graalModuleFilter = DependencyFilter.moduleFilter(
organization = new SimpleFilter(orgName => {
!graalVmOrgs.contains(orgName)
})
)
val relevantFiles =
libraryUpdates
.select(
configuration = configFilter,
module = graalModuleFilter,
artifact = DependencyFilter.artifactFilter()
)
val dependencyStore =
streams.value.cacheStoreFactory.make("std-bits-dependencies")
Tracked.diffInputs(dependencyStore, FileInfo.hash)(relevantFiles.toSet) {
report =>
val expectedFileNames =
report.checked.map(file => file.getName) ++ providedJarNames
for (existing <- IO.listFiles(destination)) {
if (!expectedFileNames.contains(existing.getName)) {
log.info(
s"Removing outdated std-bits dependency ${existing.getName}."
)
IO.delete(existing)
}
}
for (changed <- report.modified -- report.removed) {
log.info(
s"Updating changed std-bits dependency ${changed.getName}."
)
updateDependency(changed, destination, log)
}
for (file <- report.unmodified) {
val dest = destination / file.getName
if (!dest.exists()) {
log.info(s"Adding missing std-bits dependency ${file.getName}.")
updateDependency(file, destination, log)
}
}
}
}
private def updateDependency(
jar: File,
destinationDir: File,
logger: ManagedLogger
): Unit = {
val destination = destinationDir / jar.getName
IO.copyFile(jar, destination)
}
/** Builds a single standard library package `name`. Should only be used
* in tasks used in local development.
*
* @param name name of the package, see `stdBitsProjects` in build.sbt
* @param root top directory where distribution is being built
* @param cache used for persisting the cached information
* @param log logger used in the task
* @param defaultDevEnsoVersion default `dev` version
*/
def buildStdLibPackage(
name: String,
root: File,
cacheFactory: sbt.util.CacheStoreFactory,
log: sbt.Logger,
defaultDevEnsoVersion: String
) = {
log.info(s"Building standard library package for '$name'")
val prefix = "Standard"
val targetPkgRoot = root / "lib" / prefix / name / defaultDevEnsoVersion
val sourceDir = file(
s"distribution/lib/$prefix/$name/$defaultDevEnsoVersion"
)
if (!sourceDir.exists) {
throw new RuntimeException("Invalid standard library package " + name)
}
val result = DistributionPackage.copyDirectoryIncremental(
source = file(s"distribution/lib/$prefix/$name/$defaultDevEnsoVersion"),
destination = targetPkgRoot,
cache = cacheFactory.sub("engine-libraries").make(s"$prefix.$name")
)
if (result) {
log.info(s"Package '$name' has been updated")
} else {
log.info(s"No changes detected for '$name' package")
}
}
}