-
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.
Showing
7 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/main/scala/com/typesafe/sbt/packager/graalvm/GraalVMPlugin.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,48 @@ | ||
package com.typesafe.sbt.packager.graalvm | ||
|
||
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(GraalVMPlugin) | ||
* }}} | ||
*/ | ||
object GraalVMPlugin extends AutoPlugin { | ||
|
||
object autoImport extends GraalVMKeys { | ||
val GraalVM: Configuration = config("graalvm") | ||
} | ||
|
||
import autoImport._ | ||
|
||
override def projectConfigurations: Seq[Configuration] = Seq(GraalVM) | ||
|
||
override lazy val projectSettings = Seq( | ||
target in GraalVM := target.value / "graalvm", | ||
sourceDirectory in GraalVM := sourceDirectory.value, | ||
name in GraalVM := name.value, | ||
nativeImageOptions in GraalVM := Seq(s"-H:Name=${(name in GraalVM).value}"), | ||
packageBin in GraalVM := { | ||
(target in GraalVM).value.mkdirs() | ||
val className = (mainClass in Compile).value.getOrElse(sys.error("Meh...")) | ||
val classPath = Seq((packageBin in Compile).value) ++ (dependencyClasspath in Compile).value.map(_.data) | ||
val arguments = Seq("--class-path", classPath.mkString(":")) ++ (nativeImageOptions in GraalVM).value ++ Seq(className) | ||
val command = Seq("native-image") ++ arguments | ||
sys.process.Process(command, (target in GraalVM).value) ! streams.value.log match { | ||
case 0 => file("K") | ||
case x => sys.error("Failed to run native-image, exit status: " + x) | ||
} | ||
} | ||
) | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/scala/com/typesafe/sbt/packager/graalvm/Keys.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,12 @@ | ||
package com.typesafe.sbt | ||
package packager | ||
package graalvm | ||
|
||
import sbt._ | ||
|
||
/** | ||
* GraalVM settings | ||
*/ | ||
trait GraalVMKeys { | ||
val nativeImageOptions = SettingKey[Seq[String]]("native-image-options", "native-image options") | ||
} |
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(GraalVMPlugin) | ||
|
||
name := "simple-test" | ||
|
||
version := "0.1.0" |
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/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 world") | ||
} | ||
} |
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:packageBin | ||
$ exists target/graalvm/simple-test |
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,47 @@ | ||
.. _graalvm-plugin: | ||
|
||
GraalVM Plugin | ||
============= | ||
|
||
GraalVM's ``native-image`` compiles Java programs AOT (ahead-of-time). | ||
|
||
https://www.graalvm.org/docs/reference-manual/aot-compilation/ documents the AOT compilation of GraalVM. | ||
|
||
.. note:: The GraalVM plugin depends on the :ref:`universal-plugin`. | ||
|
||
Requirements | ||
------------ | ||
|
||
You must have ``native-image`` of GraalVM in your classpath. | ||
|
||
Build | ||
----- | ||
|
||
.. code-block:: bash | ||
sbt 'show graalvm:packageBin' | ||
Required Settings | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
.. code-block:: scala | ||
enablePlugins(GraalVMPlugin) | ||
Settings | ||
-------- | ||
|
||
Publishing Settings | ||
~~~~~~~~~~~~~~~~~~~ | ||
|
||
``nativeImageOptions`` | ||
Extra options that will be passed to the ``native-image`` command. By default, this includes the name of the main class. | ||
|
||
Tasks | ||
----- | ||
The GraalVM plugin provides the following commands: | ||
|
||
``graalvm:packageBin`` | ||
Generates a native image using GraalVM. |