Generate GraalVM native images with Clojure Deps and CLI tools
Switch branches/tags
Nothing to show
Clone or download
Latest commit 305d69a Oct 30, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/clj Fix class name munging issue Oct 30, 2018
.gitignore Initial commit May 25, 2018
LICENSE Create LICENSE May 25, 2018
README.md Update SHA Oct 30, 2018
deps.edn Make classpath with tools.deps.alpha Sep 21, 2018

README.md

clj.native-image

Build GraalVM native images using Clojure Deps and CLI tools.

This should be useful for creating lightweight, native CLI executables using Clojure and deps.edn. See clj.native-cli for a starter project template.

This project depends on tools.deps.alpha and should be considered alpha itself.

Prerequisites

Usage

Assuming a project structure like this (or see example):

.
├── deps.edn
└── src
    └── core.clj

In your deps.edn specify an alias with a dependency on clj.native-image:

{:aliases {:native-image
           {:main-opts ["-m clj.native-image core"
                        "-H:Name=json2edn"]
            :extra-deps
            {clj.native-image
             {:git/url "https://github.com/taylorwood/clj.native-image.git"
              :sha "d97f25aa153e0f94139f5d03e60a345151815d4d"}}}}
 :jvm-opts ["-Dclojure.compiler.direct-linking=true"]}

Where core.clj is a class with -main entrypoint, for example:

(ns core
  (:gen-class))
(defn -main [& args]
  (println "Hello, World!"))

From your project directory, invoke clojure with the native-image alias, specifying the main namespace (core in example above):

➜ clojure -A:native-image
Loading core
Compiling core
Building native image 'core' with classpath 'classes:src:etc.'

   classlist:   1,944.26 ms
   8<----------------------
     [total]:  38,970.37 ms

Note: Either GRAALVM_HOME environment variable must be set, or GraalVM's native-image path must be passed as an argument, and any additional arguments will be passed to native-image e.g.:

➜ clojure -A:native-image --verbose

You can now execute the native image:

➜ ./core
Hello, World!

See this Gist for another example.

Notes

The --no-server flag is passed to native-image by default, to avoid creating orphaned build servers.

References

GraalVM Native Image AOT Compilation

This project was inspired by depstar.

Contributing

You'll need Clojure CLI tooling and GraalVM installed to test locally. Just change the source of the clj.native-image dependency to a :local/root instead of :git/url.

Issues, PRs, and suggestions are welcome!

License

Copyright © 2018 Taylor Wood.

Distributed under the MIT License.