Skip to content

Add support for jvm using directive #1539

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ case object ScalaPreprocessor extends Preprocessor {
UsingJavacOptionsDirectiveHandler,
UsingJavaOptionsDirectiveHandler,
UsingJavaPropsDirectiveHandler,
UsingJvmDirectiveHandler,
UsingMainClassDirectiveHandler,
UsingOptionDirectiveHandler,
UsingPackagingDirectiveHandler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import java.io.File

import scala.build.internal.{Constants, ExternalBinaryParams, FetchExternalBinary, Runner}
import scala.build.options.BuildOptions
import scala.build.{Build, BuildThreads, Logger}
import scala.build.{Build, BuildThreads, Logger, Positioned}
import scala.cli.CurrentParams
import scala.cli.commands.publish.ConfigUtil.*
import scala.cli.commands.util.CommonOps.SharedDirectoriesOptionsOps
Expand Down Expand Up @@ -51,7 +51,7 @@ object Metabrowse extends ScalaCommand[MetabrowseOptions] {
fetchSources = Some(true)
),
javaOptions = baseOptions.javaOptions.copy(
jvmIdOpt = baseOptions.javaOptions.jvmIdOpt.orElse(Some("8"))
jvmIdOpt = baseOptions.javaOptions.jvmIdOpt.orElse(Some(Positioned.none("8")))
)
)
}
Expand Down
2 changes: 1 addition & 1 deletion modules/cli/src/main/scala/scala/cli/commands/Run.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ object Run extends ScalaCommand[RunOptions] with BuildCommandHelpers {
jvmIdOpt = baseOptions.javaOptions.jvmIdOpt.orElse {
runMode(options) match {
case _: RunMode.Spark | RunMode.HadoopJar =>
Some("8")
Some(Positioned.none("8"))
case RunMode.Default => None
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object JvmUtils {
javaHomeOpt = javaHome.filter(_.nonEmpty).map(v =>
Positioned(Seq(Position.CommandLine("--java-home")), os.Path(v, Os.pwd))
),
jvmIdOpt = jvm.filter(_.nonEmpty),
jvmIdOpt = jvm.filter(_.nonEmpty).map(Positioned.commandLine),
jvmIndexOpt = jvmIndex.filter(_.nonEmpty),
jvmIndexOs = jvmIndexOs.map(_.trim).filter(_.nonEmpty),
jvmIndexArch = jvmIndexArch.map(_.trim).filter(_.nonEmpty),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ object LauncherCli {

val buildOptions = BuildOptions(
javaOptions = JavaOptions(
jvmIdOpt = Some(OsLibc.baseDefaultJvm(OsLibc.jvmIndexOs, "17"))
jvmIdOpt = Some(OsLibc.baseDefaultJvm(OsLibc.jvmIndexOs, "17")).map(Positioned.none)
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.io.File

import scala.annotation.tailrec
import scala.build.internal.{ManifestJar, Runner}
import scala.build.{Build, Logger}
import scala.build.{Build, Logger, Positioned}
import scala.cli.errors.GraalVMNativeImageError
import scala.cli.graal.{BytecodeProcessor, TempCache}
import scala.cli.internal.CachedBinary
Expand Down Expand Up @@ -171,7 +171,7 @@ object NativeImage {
val jvmId = build.options.notForBloopOptions.packageOptions.nativeImageOptions.jvmId
val options = build.options.copy(
javaOptions = build.options.javaOptions.copy(
jvmIdOpt = Some(jvmId)
jvmIdOpt = Some(Positioned.none(jvmId))
)
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package scala.build.preprocessing.directives

import scala.build.Logger
import scala.build.errors.BuildException
import scala.build.options.{BuildOptions, JavaOptions, MaybeScalaVersion, ScalaOptions}
import scala.build.preprocessing.directives.UsingMainClassDirectiveHandler.checkIfValuesAreExpected

case object UsingJvmDirectiveHandler extends UsingDirectiveHandler {
def name = "JVM version"

def description = "Use a specific JVM, such as `14`, `adopt:11`, or `graalvm:21`, or `system`"

def usage = "//> using jvm _value_"

override def usageMd = "`//> using jvm` _value_"

override def examples = Seq(
"//> using jvm \"11\"",
"//> using jvm \"adopt:11\"",
"//> using jvm \"graalvm:21\""
)

def keys = Seq("jvm")

override def isRestricted = false

def handleValues(
scopedDirective: ScopedDirective,
logger: Logger
): Either[BuildException, ProcessedUsingDirective] =
checkIfValuesAreExpected(scopedDirective).map { groupedValues =>
val jvmIdOpts = groupedValues.scopedStringValues.map(_.positioned)
val options = BuildOptions(
javaOptions = JavaOptions(
jvmIdOpt = jvmIdOpts.headOption
)
)
ProcessedDirective(Some(options), Seq.empty)
}

override def getValueNumberBounds(key: String) = UsingDirectiveValueNumberBounds(1, 1)

}
Original file line number Diff line number Diff line change
Expand Up @@ -1671,10 +1671,23 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])

test("Runs with JVM 8") {
val inputs =
TestInputs(os.rel / "run.scala" -> """object Main extends App { println("hello")}""")
TestInputs(
os.rel / "run.scala" -> """object Main extends App { println(System.getProperty("java.version"))}"""
)
inputs.fromRoot { root =>
val p = os.proc(TestUtil.cli, "run.scala", "--jvm", "8").call(cwd = root)
expect(p.out.trim() == "hello")
expect(p.out.trim().startsWith("1.8"))
}
}

test("Runs with JVM 8 with using directive") {
val inputs =
TestInputs(os.rel / "run.scala" ->
"""//> using jvm "8"
|object Main extends App { println(System.getProperty("java.version"))}""".stripMargin)
inputs.fromRoot { root =>
val p = os.proc(TestUtil.cli, "run.scala").call(cwd = root)
expect(p.out.trim().startsWith("1.8"))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import scala.util.control.NonFatal
*/
final case class JavaOptions(
javaHomeOpt: Option[Positioned[os.Path]] = None,
jvmIdOpt: Option[String] = None,
jvmIdOpt: Option[Positioned[String]] = None,
jvmIndexOpt: Option[String] = None,
jvmIndexOs: Option[String] = None,
jvmIndexArch: Option[String] = None,
Expand Down Expand Up @@ -74,7 +74,7 @@ final case class JavaOptions(
else None
}
.orElse {
jvmIdOpt.map { jvmId =>
jvmIdOpt.map(_.value).map { jvmId =>
implicit val ec: ExecutionContextExecutorService = cache.ec
cache.logger.use {
val enforceLiberica =
Expand Down
5 changes: 5 additions & 0 deletions website/docs/commands/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ scala-cli Hello.scala hi.sc --main-class hi_sc
scala-cli Hello.scala --jvm adopt:14
```

You can also specify custom JVM with the using directive `//> using jvm`:
```scala
//> using jvm "adopt:14"
```

JVMs are [managed by coursier](https://get-coursier.io/docs/cli-java#managed-jvms), and are read from the [coursier JVM index](https://github.com/coursier/jvm-index).
(New JVM versions are automatically checked daily, and updates for those are - manually - merged
swiftly.)
Expand Down
13 changes: 13 additions & 0 deletions website/docs/reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ Add dependencies

`//> using lib "tabby:tabby:0.2.3,url=https://github.com/bjornregnell/tabby/releases/download/v0.2.3/tabby_3-0.2.3.jar"`

### JVM version

Use a specific JVM, such as `14`, `adopt:11`, or `graalvm:21`, or `system`

`//> using jvm` _value_

#### Examples
`//> using jvm "11"`

`//> using jvm "adopt:11"`

`//> using jvm "graalvm:21"`

### Java home

Sets Java home used to run your application or tests
Expand Down
13 changes: 13 additions & 0 deletions website/docs/reference/scala-command/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ Add dependencies

`//> using lib "tabby:tabby:0.2.3,url=https://github.com/bjornregnell/tabby/releases/download/v0.2.3/tabby_3-0.2.3.jar"`

### JVM version

Use a specific JVM, such as `14`, `adopt:11`, or `graalvm:21`, or `system`

`//> using jvm` _value_

#### Examples
`//> using jvm "11"`

`//> using jvm "adopt:11"`

`//> using jvm "graalvm:21"`

### Java home

Sets Java home used to run your application or tests
Expand Down