-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build native images using GraalVM (#1165)
- Loading branch information
1 parent
2aba0d6
commit cf8f013
Showing
8 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
13 changes: 13 additions & 0 deletions
13
src/main/scala/com/typesafe/sbt/packager/graalvm-native-image/GraalVMNativeImageKeys.scala
This file contains 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,13 @@ | ||
package com.typesafe.sbt | ||
package packager | ||
package graalvmnativeimage | ||
|
||
import sbt._ | ||
|
||
/** | ||
* GraalVM settings | ||
*/ | ||
trait GraalVMNativeImageKeys { | ||
val graalVMNativeImageOptions = | ||
SettingKey[Seq[String]]("graalvm-native-image-options", "GraalVM native-image options") | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/scala/com/typesafe/sbt/packager/graalvm-native-image/GraalVMNativeImagePlugin.scala
This file contains 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,56 @@ | ||
package com.typesafe.sbt.packager.graalvmnativeimage | ||
|
||
import sbt._ | ||
import sbt.Keys._ | ||
import java.nio.charset.Charset | ||
|
||
import com.typesafe.sbt.packager.SettingsHelper | ||
import com.typesafe.sbt.packager.Keys._ | ||
import com.typesafe.sbt.packager.linux._ | ||
import com.typesafe.sbt.packager.Compat._ | ||
import com.typesafe.sbt.packager.validation._ | ||
|
||
/** | ||
* Plugin to compile ahead-of-time native executables. | ||
* | ||
* @example Enable the plugin in the `build.sbt` | ||
* {{{ | ||
* enablePlugins(GraalVMNativeImagePlugin) | ||
* }}} | ||
*/ | ||
object GraalVMNativeImagePlugin extends AutoPlugin { | ||
|
||
object autoImport extends GraalVMNativeImageKeys { | ||
val GraalVMNativeImage: Configuration = config("graalvm-native-image") | ||
} | ||
|
||
private val GraalVMNativeImageCommand = "native-image" | ||
|
||
import autoImport._ | ||
|
||
override def projectConfigurations: Seq[Configuration] = Seq(GraalVMNativeImage) | ||
|
||
override lazy val projectSettings = Seq( | ||
target in GraalVMNativeImage := target.value / "graalvm-native-image", | ||
graalVMNativeImageOptions := Seq.empty, | ||
packageBin in GraalVMNativeImage := { | ||
val targetDirectory = (target in GraalVMNativeImage).value | ||
targetDirectory.mkdirs() | ||
val binaryName = name.value | ||
val command = { | ||
val nativeImageArguments = { | ||
val className = (mainClass in Compile).value.getOrElse(sys.error("Could not find a main class.")) | ||
val classpathJars = Seq((packageBin in Compile).value) ++ (dependencyClasspath in Compile).value.map(_.data) | ||
val classpath = classpathJars.mkString(":") | ||
val extraOptions = graalVMNativeImageOptions.value | ||
Seq("--class-path", classpath, s"-H:Name=$binaryName") ++ extraOptions ++ Seq(className) | ||
} | ||
Seq(GraalVMNativeImageCommand) ++ nativeImageArguments | ||
} | ||
sys.process.Process(command, targetDirectory) ! streams.value.log match { | ||
case 0 => targetDirectory / binaryName | ||
case x => sys.error(s"Failed to run $GraalVMNativeImageCommand, exit status: " + x) | ||
} | ||
} | ||
) | ||
} |
5 changes: 5 additions & 0 deletions
5
src/sbt-test/graalvm-native-image/simple-native-image/build.sbt
This file contains 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,5 @@ | ||
enablePlugins(GraalVMNativeImagePlugin) | ||
|
||
name := "simple-test" | ||
|
||
version := "0.1.0" |
1 change: 1 addition & 0 deletions
1
src/sbt-test/graalvm-native-image/simple-native-image/project/plugins.sbt
This file contains 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 @@ | ||
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version")) |
5 changes: 5 additions & 0 deletions
5
src/sbt-test/graalvm-native-image/simple-native-image/src/main/scala/Main.scala
This file contains 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,5 @@ | ||
object Main { | ||
def main(args: Array[String]): Unit = { | ||
println("Hello Graal") | ||
} | ||
} |
This file contains 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,3 @@ | ||
# Generate the GraalVM native image | ||
> show graalvm-native-image:packageBin | ||
$ exec bash -c 'target/graalvm-native-image/simple-test | grep -q "Hello Graal"' |
This file contains 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,45 @@ | ||
.. _graalvm-native-image-plugin: | ||
|
||
GraalVM Native Image Plugin | ||
============= | ||
|
||
GraalVM's ``native-image`` compiles Java programs AOT (ahead-of-time) into native binaries. | ||
|
||
https://www.graalvm.org/docs/reference-manual/aot-compilation/ documents the AOT compilation of GraalVM. | ||
|
||
Requirements | ||
------------ | ||
|
||
You must have ``native-image`` of GraalVM in your ``PATH``. | ||
|
||
Build | ||
----- | ||
|
||
.. code-block:: bash | ||
sbt 'show graalvm-native-image:packageBin' | ||
Required Settings | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
.. code-block:: scala | ||
enablePlugins(GraalVMNativeImagePlugin) | ||
Settings | ||
-------- | ||
|
||
Publishing Settings | ||
~~~~~~~~~~~~~~~~~~~ | ||
|
||
``graalVMNativeImageOptions`` | ||
Extra options that will be passed to the ``native-image`` command. By default, this includes the name of the main class. | ||
|
||
Tasks | ||
----- | ||
The GraalVM Native Image plugin provides the following commands: | ||
|
||
``graalvm-native-image:packageBin`` | ||
Generates a native image using GraalVM. |