Skip to content

Commit

Permalink
Support new graalvm container versions, backwards support for legacy …
Browse files Browse the repository at this point in the history
…versions
  • Loading branch information
kgston committed Jul 27, 2023
1 parent d352378 commit ebcdb21
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/validate-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ jobs:
- name: Setup GraalVM environment
uses: graalvm/setup-graalvm@v1
with:
java-version: 17
java-version: 17.0.8
distribution: 'graalvm'
cache: 'sbt'
- name: Validate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object GraalVMNativeImagePlugin extends AutoPlugin {

import autoImport._

private val GraalVMBaseImage = "ghcr.io/graalvm/graalvm-ce"
private val GraalVMBaseImagePath = "ghcr.io/graalvm/"

override def requires: Plugins = JavaAppPackaging

Expand All @@ -48,9 +48,20 @@ object GraalVMNativeImagePlugin extends AutoPlugin {
includeFilter := "*",
resources := resourceDirectories.value.descendantsExcept(includeFilter.value, excludeFilter.value).get,
UniversalPlugin.autoImport.containerBuildImage := Def.taskDyn {
val splitPackageVersion = "(.*):(.*)".r
graalVMNativeImageGraalVersion.value match {
case Some(tag) => generateContainerBuildImage(s"$GraalVMBaseImage:$tag", graalVMNativeImagePlatformArch.value)
case None => Def.task(None: Option[String])
case Some(splitPackageVersion(packageName, tag)) =>
packageName match {
case "graalvm-community" => Def.task(Some(s"$GraalVMBaseImagePath$packageName:$tag"): Option[String])
case _ =>
generateContainerBuildImage(
s"${GraalVMBaseImagePath}graalvm-ce:$tag",
graalVMNativeImagePlatformArch.value
)
}
case Some(tag) =>
generateContainerBuildImage(s"${GraalVMBaseImagePath}graalvm-ce:$tag", graalVMNativeImagePlatformArch.value)
case None => Def.task(None: Option[String])
}
}.value,
packageBin := {
Expand Down Expand Up @@ -140,21 +151,26 @@ object GraalVMNativeImagePlugin extends AutoPlugin {
image: String,
streams: TaskStreams
): File = {

import sys.process._
stage(targetDirectory, classpathJars, resources, streams)

val graalDestDir = "/opt/graalvm"
val stageDestDir = s"$graalDestDir/stage"
val resourcesDestDir = s"$stageDestDir/resources"
val platformFlag = platformArch.map(arch => Seq("--platform", arch)).getOrElse(Seq.empty)
val hostPlatform =
(dockerCommand ++ Seq("system", "info", "--format", "{{.OSType}}/{{.Architecture}}")).!!.trim

val command = dockerCommand ++ Seq("run") ++ platformFlag ++ Seq(
val command = dockerCommand ++ Seq(
"run",
"--workdir",
"/opt/graalvm",
"--rm",
"--platform",
platformArch.getOrElse(hostPlatform),
"-v",
s"${targetDirectory.getAbsolutePath}:$graalDestDir",
image,
"native-image",
"-cp",
(resourcesDestDir +: classpathJars.map(jar => s"$stageDestDir/" + jar._2)).mkString(":"),
s"-H:Name=$binaryName"
Expand All @@ -180,9 +196,13 @@ object GraalVMNativeImagePlugin extends AutoPlugin {
platformArch: Option[String] = None
): Def.Initialize[Task[Option[String]]] =
Def.task {
import sys.process._

val dockerCommand = (DockerPlugin.autoImport.dockerExecCommand in GraalVMNativeImage).value
val streams = Keys.streams.value
val platformValue = platformArch.getOrElse("local")
val hostPlatform =
(dockerCommand ++ Seq("system", "info", "--format", "{{.OSType}}/{{.Architecture}}")).!!.trim
val platformValue = platformArch.getOrElse(hostPlatform)

val (baseName, tag) = baseImage.split(":", 2) match {
case Array(n, t) => (n, t)
Expand All @@ -208,7 +228,7 @@ object GraalVMNativeImagePlugin extends AutoPlugin {
Cmd("WORKDIR", "/opt/graalvm"),
ExecCmd("RUN", "gu", "install", "native-image"),
ExecCmd("RUN", "sh", "-c", "ln -s /opt/graalvm-ce-*/bin/native-image /usr/local/bin/native-image"),
ExecCmd("ENTRYPOINT", "native-image")
ExecCmd("CMD", "native-image")
).makeContent

val command = dockerCommand ++ Seq(
Expand Down Expand Up @@ -246,7 +266,7 @@ object GraalVMNativeImagePlugin extends AutoPlugin {
val mappings = classpathJars ++ resources.map {
case (resource, path) => resource -> s"resources/$path"
}
Stager.stage(GraalVMBaseImage)(streams, stageDir, mappings)
Stager.stage(GraalVMBaseImagePath)(streams, stageDir, mappings)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ enablePlugins(GraalVMNativeImagePlugin)
name := "docker-test"
version := "0.1.0"
graalVMNativeImageOptions := Seq("--no-fallback")
graalVMNativeImageGraalVersion := Some("22.3.2")
graalVMNativeImageGraalVersion := Some("graalvm-community:17.0.8")
graalVMNativeImagePlatformArch := Some("arm64")
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Generate the GraalVM native image
> show graalvm-native-image:packageBin
$ exec bash -c 'docker run --platform arm64 -v .:/test -w /test ubuntu ./target/graalvm-native-image/docker-test | grep -q "Hello Graal"'
$ exec bash -c 'docker run --rm --platform arm64 -v .:/test -w /test ubuntu ./target/graalvm-native-image/docker-test | grep -q "Hello Graal"'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
enablePlugins(GraalVMNativeImagePlugin)

name := "docker-test"
version := "0.1.0"
graalVMNativeImageOptions := Seq("--no-fallback")
graalVMNativeImageGraalVersion := Some("graalvm-ce:22.3.3")
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("project.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Main {
def main(args: Array[String]): Unit = {
println("Hello Graal")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generate the GraalVM native image
> show graalvm-native-image:packageBin
$ exec bash -c 'target/graalvm-native-image/docker-test | grep -q "Hello Graal"'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
enablePlugins(GraalVMNativeImagePlugin)

name := "docker-test"
version := "0.1.0"
graalVMNativeImageOptions := Seq("--no-fallback")
graalVMNativeImageGraalVersion := Some("22.3.3")
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("project.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Main {
def main(args: Array[String]): Unit = {
println("Hello Graal")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generate the GraalVM native image
> show graalvm-native-image:packageBin
$ exec bash -c 'target/graalvm-native-image/docker-test | grep -q "Hello Graal"'
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ enablePlugins(GraalVMNativeImagePlugin)
name := "docker-test"
version := "0.1.0"
graalVMNativeImageOptions := Seq("--no-fallback")
graalVMNativeImageGraalVersion := Some("22.3.2")
graalVMNativeImageGraalVersion := Some("graalvm-community:17.0.8")
4 changes: 3 additions & 1 deletion src/sphinx/formats/graalvm-native-image.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ customized using the following settings.

.. code-block:: scala
graalVMNativeImageGraalVersion := Some("19.1.1")
graalVMNativeImageGraalVersion := Some("19.1.1") // Legacy GraalVM versions supported up to 22.3.3
graalVMNativeImageGraalVersion := Some("graalvm-ce:19.1.1") // Legacy GraalVM versions supported up to 22.3.3
graalVMNativeImageGraalVersion := Some("graalvm-community:17.0.8") // New GraalVM version scheme
``graalVMNativeImagePlatformArch``
Setting this enables building the native image on a different platform architecture. Requires ``graalVMNativeImageGraalVersion``
Expand Down

0 comments on commit ebcdb21

Please sign in to comment.