-
Notifications
You must be signed in to change notification settings - Fork 162
[WIP] Add sloth support
#4348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Gedochao
wants to merge
6
commits into
VirtusLab:main
Choose a base branch
from
Gedochao:feature/lazyvalgrade
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
[WIP] Add sloth support
#4348
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b179235
sun.misc.Unsafe tests
Gedochao 0561d03
Add lazyvalgrade support
Gedochao f32f193
Switch from `lazyvalgrade-cli` to `lazyvalgrade-core`
Gedochao 66242f6
Refactor
Gedochao 330d1d0
Migrate to `sloth`
Gedochao 1ccbaaf
Fix --sloth lazy-val patching on the GraalVM native launcher
Gedochao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
modules/build/src/main/scala/scala/build/postprocessing/SlothPatcher.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package scala.build.postprocessing | ||
|
|
||
| import os.Path | ||
| import sloth.jar.JarProcessor | ||
|
|
||
| import java.io.{ByteArrayOutputStream, PrintStream} | ||
| import java.math.BigInteger | ||
| import java.security.MessageDigest | ||
|
|
||
| import scala.build.errors.BuildException | ||
| import scala.build.internal.Constants | ||
| import scala.build.options.BuildOptions | ||
| import scala.build.{Directories, Logger} | ||
| import scala.util.control.NonFatal | ||
|
|
||
| object SlothPatcher { | ||
|
|
||
| private val cacheDir = Directories.directories.cacheDir / "sloth" | ||
|
|
||
| def transformClassPath( | ||
| classPath: Seq[Path], | ||
| options: BuildOptions, | ||
| logger: Logger | ||
| ): Either[BuildException, Seq[Path]] = | ||
| if options.notForBloopOptions.sloth then | ||
| Right { | ||
| // sloth logs to stdout/stderr via its own bundled logging; capture it so it | ||
| // doesn't pollute the user program's output, routing anything it prints to debug. | ||
| captureStdio(logger) { | ||
| classPath.map { entry => | ||
| if entry.ext == "jar" then patchJar(entry, logger) | ||
| else entry | ||
| } | ||
| } | ||
| } | ||
| else Right(classPath) | ||
|
|
||
| private def captureStdio[T](logger: Logger)(f: => T): T = { | ||
| val outBuffer = new ByteArrayOutputStream | ||
| val errBuffer = new ByteArrayOutputStream | ||
| val originalOut = System.out | ||
| val originalErr = System.err | ||
| System.setOut(new PrintStream(outBuffer, true)) | ||
| System.setErr(new PrintStream(errBuffer, true)) | ||
| try f | ||
| finally { | ||
| System.setOut(originalOut) | ||
| System.setErr(originalErr) | ||
| val captured = (outBuffer.toString ++ errBuffer.toString).trim | ||
| if captured.nonEmpty then logger.debug(captured) | ||
| } | ||
| } | ||
|
|
||
| private def patchJar(jar: Path, logger: Logger): Path = | ||
| val cachedDir = cacheDir / Constants.slothVersion / sha1(jar) | ||
| val cached = cachedDir / jar.last | ||
| if os.exists(cached) then cached | ||
| else | ||
| os.makeDir.all(cachedDir) | ||
| runJarProcessor(jar, cached) match | ||
| case Right(()) => | ||
| logger.debug(s"Patched lazy vals in $jar -> $cached") | ||
| cached | ||
| case Left(message) => | ||
| os.remove.all(cachedDir) | ||
| logger.message(s"Could not patch lazy vals in $jar, using original: $message") | ||
| jar | ||
|
|
||
| private def runJarProcessor( | ||
| input: Path, | ||
| output: Path | ||
| ): Either[String, Unit] = | ||
| try | ||
| val result = JarProcessor.process(input.toNIO, output.toNIO) | ||
| if result.failedClasses > 0 then | ||
| val details = if result.errors.nonEmpty then s": ${result.errors.mkString("; ")}" else "" | ||
| Left(s"Failed to patch lazy vals in $input ($result.failedClasses failed classes)$details") | ||
| else Right(()) | ||
| catch | ||
| case NonFatal(e) => | ||
| Left(s"Failed to patch lazy vals in $input: ${e.getMessage}") | ||
|
|
||
| private def sha1(path: Path): String = | ||
| val md = MessageDigest.getInstance("SHA-1") | ||
| md.update(os.read.bytes(path)) | ||
| String.format("%040x", new BigInteger(1, md.digest())) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
modules/directives/src/main/scala/scala/build/preprocessing/directives/Sloth.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package scala.build.preprocessing.directives | ||
|
|
||
| import scala.build.directives.* | ||
| import scala.build.errors.BuildException | ||
| import scala.build.options.{BuildOptions, PostBuildOptions} | ||
| import scala.cli.commands.SpecificationLevel | ||
|
|
||
| @DirectiveExamples("//> using sloth") | ||
| @DirectiveUsage("//> using sloth", "`//> using sloth`") | ||
| @DirectiveDescription( | ||
| "Patch Scala 3.0-3.7.x lazy val bytecode on the classpath for JDK 26+ compatibility" | ||
| ) | ||
| @DirectiveLevel(SpecificationLevel.EXPERIMENTAL) | ||
| final case class Sloth( | ||
| @DirectiveName("lazyvalgrade") | ||
|
Gedochao marked this conversation as resolved.
|
||
| @DirectiveName("lazyValPatching") | ||
| sloth: Boolean = false | ||
| ) extends HasBuildOptions { | ||
| def buildOptions: Either[BuildException, BuildOptions] = | ||
| Right(BuildOptions( | ||
| notForBloopOptions = PostBuildOptions(slothOpt = Some(true)) | ||
| )) | ||
| } | ||
|
|
||
| object Sloth { | ||
| val handler: DirectiveHandler[Sloth] = DirectiveHandler.derive | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
modules/integration/src/test/scala/scala/cli/integration/LazyValTests.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package scala.cli.integration | ||
|
|
||
| trait LazyValTests { | ||
| private val lazyValsLibOrg = "test.lazyvals" | ||
| private val lazyValsLibName = "lazyvals-lib" | ||
| private val lazyValsLibVersion = "0.1.0" | ||
| private val lazyValsLibDep = s"$lazyValsLibOrg::$lazyValsLibName:$lazyValsLibVersion" | ||
|
|
||
| protected def publishLazyValsLib( | ||
| scalaVersion: String, | ||
| workspace: os.Path | ||
| ): (String, os.Path) = { | ||
| val libDir = workspace / "lazyvals-lib" | ||
| val repoDir = workspace / "test-repo" | ||
| os.write( | ||
| libDir / "LazyValsLib.scala", | ||
| """package lazyvalslib | ||
| |object LazyValsLib { | ||
| | lazy val greeting: String = "Hello" | ||
| |} | ||
| |""".stripMargin, | ||
| createFolders = true | ||
| ) | ||
| os.proc( | ||
| TestUtil.cli, | ||
| "--power", | ||
| "publish", | ||
| libDir, | ||
| "--organization", | ||
| lazyValsLibOrg, | ||
| "--name", | ||
| lazyValsLibName, | ||
| "--project-version", | ||
| lazyValsLibVersion, | ||
| "--scala", | ||
| scalaVersion, | ||
| "--publish-repo", | ||
| repoDir.toNIO.toUri.toASCIIString | ||
| ).call(cwd = workspace, stdin = os.Inherit, stdout = os.Inherit) | ||
| os.remove.all(libDir) | ||
| (lazyValsLibDep, repoDir) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.