Skip to content

Instantly share code, notes, and snippets.

@m-sp
Last active April 24, 2018 00:46
Show Gist options
  • Save m-sp/3f2d9c08bfcd077d6987d35ba0190f23 to your computer and use it in GitHub Desktop.
Save m-sp/3f2d9c08bfcd077d6987d35ba0190f23 to your computer and use it in GitHub Desktop.
Compiling scalafmt to a native binary with graal

Graal contains an AOT compiler which makes it really straight forward to compile jars to native binaries. I've currently only tried the community edition on linux. So your mileage may vary.

Requirements

Create native binary

Graal ships with substrate vm which can be used to compile any java byte code to a native image. We need to pass the correct classpath and main to the native-image tool. The simplest way for is to fetch scalafmt and all it's dependecies via coursier. We are immediately piping it to paste to convert the newline to : so that we can directly use it for the classpath argument.

  • CLASSPATH=$(coursier fetch com.geirsson:scalafmt-cli_2.12:1.4.0 -r bintray:scalameta/maven | paste -s -d: -)

To actually create the binary execute

  • /path/to/graalvm/bin/native-image -cp $CLASSPATH org.scalafmt.cli.Cli -H:+ReportUnsupportedElementsAtRuntime

we unfortunately need to include the -H:+ReportUnsupportedElementsAtRuntime because scalafmt uses code which isn't yet supported by the AOT compiler.

Known issues

  • Relative paths don't work. They are always resolved to the session folder in which the native image was generated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment