Skip to content

Commit

Permalink
Build native images using GraalVM.
Browse files Browse the repository at this point in the history
Closes #1123.
  • Loading branch information
ScalaWilliam committed Oct 17, 2018
1 parent fdcf581 commit 1dfe71e
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 0 deletions.
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 src/main/scala/com/typesafe/sbt/packager/graalvm/Keys.scala
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")
}
5 changes: 5 additions & 0 deletions src/sbt-test/graalvm/simple-native-image/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enablePlugins(GraalVMPlugin)

name := "simple-test"

version := "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.typesafe.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 world")
}
}
3 changes: 3 additions & 0 deletions src/sbt-test/graalvm/simple-native-image/test
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
47 changes: 47 additions & 0 deletions src/sphinx/formats/graalvm.rst
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.

0 comments on commit 1dfe71e

Please sign in to comment.