|
1 | 1 | # clojure-graalvm-agent-helper
|
2 |
| -Ease native configuration generation for GraalVM NativeImage |
| 2 | +Ease native configuration generation for GraalVM NativeImage. You don't have to write configurations for native image by hand, since there is [Agent](https://medium.com/graalvm/introducing-the-tracing-agent-simplifying-graalvm-native-image-configuration-c3b56c486271) which might trace all the calls you make. |
| 3 | + |
| 4 | +This tool helps you automate the process. You can leave in-context calls and production version of the jar will not contain any code which in-context wraps. |
| 5 | + |
| 6 | +Extracted from `holy-lambda` micro-framework. |
| 7 | + |
| 8 | +## How to use it? |
| 9 | +1. Add following dependency: |
| 10 | + |
| 11 | + ``` clojure |
| 12 | + io.github.FieryCod/clojure-graalvm-agent-helper {:mvn/version "0.0.1"} |
| 13 | + ``` |
| 14 | + |
| 15 | + 2. Use `agent/in-context` in your code: |
| 16 | + |
| 17 | + ```clojure |
| 18 | + (ns example.core |
| 19 | + (:gen-class) |
| 20 | + (:require |
| 21 | + [fierycod.graalvm-agent-helper.core :as agent])) |
| 22 | + |
| 23 | + (agent/in-context |
| 24 | + (println "I can run arbitrary code in agent context. Agent will catch all the calls in context and generate native configuration out of it :)")) |
| 25 | + |
| 26 | + (agent/in-context |
| 27 | + (println "In case of error agent will catch it :)") |
| 28 | + (throw (ex-info "Ups.." {}))) |
| 29 | + |
| 30 | + (defn -main |
| 31 | + [] |
| 32 | + (agent/in-context (println "Since main is called I will print as well :)"))) |
| 33 | + ``` |
| 34 | + |
| 35 | + 3. Compile with `USE_AGENT_CONTEXT` environment set to `true`, so that `in-context` will not be removed |
| 36 | + |
| 37 | + ``` |
| 38 | + USE_AGENT_CONTEXT=true clojure -X:uberjar :aot true :jvm-opts '["-Dclojure.compiler.direct-linking=true", "-Dclojure.spec.skip-macros=true"]' :jar agent-output.jar :main-class example.core |
| 39 | + ``` |
| 40 | + |
| 41 | + 4. Run in native agent context |
| 42 | + ``` |
| 43 | + java -agentlib:native-image-agent=config-output-dir=resources/native-configuration \ |
| 44 | + -Dexecutor=native-agent \ |
| 45 | + -jar agent-output.jar |
| 46 | + ``` |
| 47 | + |
| 48 | + 5. Compile a project without `USE_IN_AGENT_CONTEXT` |
| 49 | + ``` |
| 50 | + clojure -X:uberjar :aot true :jvm-opts '["-Dclojure.compiler.direct-linking=true", "-Dclojure.spec.skip-macros=true"]' :jar output.jar :main-class example.core |
| 51 | + ``` |
| 52 | + |
| 53 | + 5. Feed `native-image` with configuration and compile |
| 54 | + ``` |
| 55 | + native-image -jar output.jar \ |
| 56 | + -H:ConfigurationFileDirectories=resources/native-configuration \ |
| 57 | + -H:+AllowIncompleteClasspath \ |
| 58 | + --report-unsupported-elements-at-runtime \ |
| 59 | + --no-fallback \ |
| 60 | + --verbose \ |
| 61 | + --enable-url-protocols=http,https \ |
| 62 | + --no-server \ |
| 63 | + --initialize-at-build-time |
| 64 | + ``` |
| 65 | + |
| 66 | + Check example folder if you still have trouble setting it up. |
0 commit comments