Fork me on GitHub
#graalvm
<
2022-05-14
>
dominicm14:05:09

I'm looking at adding native-image support to pack. I know there is already clj.native-image, but it doesn't have a clojure tools (-T) usage, nor a tools.build-like API. Are there any complications/shortcuts/clever hacks that pack could help with? @borkdude I know you do a lot of scripting voodoo with native-image compilation, and it'd be great to port those to be defaults for the whole community using pack if possible.

dominicm14:05:46

Additionally, on the off chance anyone knows, I'd love to know the state of using svm-driver to build native-image without graalvm/native-image being installed. I've asked on the graal slack, but I'm not expecting an answer. The maven plugin briefly had support for it, but it was removed.

dominicm18:05:43

Seemingly it's not supported right now, due to certain patches in graalvm not being upstreamed yet. That means it's unreliable between versions. That's unfortunate.

borkdude18:05:33

Why would you want to add the svm driver?

dominicm18:05:06

Then pack could compile a native image without needing the user to use graalvm.

👍 1
dominicm18:05:21

It would run on, eg openjdk

borkdude18:05:51

Is that even possible? Where is that documented?

borkdude18:05:25

My experience with including the svm driver in a project is not good. It makes it incompatible over different version of GraalVM

dominicm18:05:53

It is. The maven plugin used to do it. It's not documented. The maven plugin removed the functionality because it doesn't consistently work for all JDKs and svm versions because of patch upstreaming.

borkdude18:05:08

It is not that hard to just download GraalVM to a temp folder and use it. The convention that is used is GRAALVM_HOME- I use that in all my compile scripts

dominicm18:05:13

I am considering doing that, yeah. I prefer the version of graal being pinned to the repo, rather than whatever version the user chooses, as it's got a higher reproducibility.

borkdude18:05:54

I didn't mean that the tool should download graalvm, I don't like that

borkdude18:05:20

I mean, for the user it is not hard at all to grab a version of graalvm and use that for compilation

borkdude18:05:53

Because I want to use a specific version of graalvm and not be dependent on some tool doing that for me, I'm capable of doing that myself

dominicm19:05:12

But if you rock up at a repo, which version is that?

borkdude19:05:24

rock up at a repo, what does that mean?

dominicm19:05:48

If you are contributing to a new repo, which graal version does the maintainer want to make releases with?

borkdude19:05:32

usually the newest, but older ones work too. it's the same with Clojure, you don't usually specify the JVM version in your repo, and clj doesn't magically grab a 1GB download from the internets

dominicm19:05:52

I don't think that comparison is analogous, it's more like specifying which version of the JDK you want your docker image to run with. Releases are a little different from development in terms of increasing consistency. Native image also seems more unstable than the standard JDK. It is also far more user-dependent due to a lack of distribution packaging because of the licensing. My perspective on instability could be inaccurate because I'm watching from afar.

borkdude19:05:33

It's been pretty stable since 21.3

borkdude19:05:41

or 21.0 or so

dominicm19:05:33

Didn't 22 break how initialization worked and break many clojure programs?

borkdude19:05:08

It deprecated an option, but it didn't actually break anything

borkdude19:05:23

and clj-easy/graal-build-time is what fixes this

borkdude19:05:30

so we're ready for deprecation by removal

dominicm19:05:00

Oh, so it isn't going yet. Good to know. I misunderstood. Still, people might get stuck on older versions.

borkdude19:05:43

I usually specify the graalvm version in documentation and people can also check in CI or a Dockerfile. If something is not working, I tell them to upgrade.

borkdude19:05:18

but I might not be the right audience for your tool, I don't think I would ever use a tool again which wraps native-image

borkdude19:05:37

as that has bitten me in the past. just use the vanilla tool is what I recommend

borkdude19:05:32

There's also nuances like musl, etc which pack is probably not going to solve

borkdude19:05:06

My opinion is that having an uberjar tool is great, but from there on, just write a script to build with native image

borkdude19:05:57

Perhaps @UE21H2HHD has a different opinion :)

lread19:05:55

My humble opinions? simple_smile So for the topic of svm… if I’ve understood… I recommend going with the flow and using Graal tooling. Graal is complicated enough without introducing unsupported or uncommonly used configurations.

lread19:05:45

And… are you also talking about a build wrapper? I thinking rolling your own makes sense. It is not that complicated and also educates you, at least a bit, on the working of Graal. There are lots of levers and dials to fiddle with for Graal native-image. I personally want to be able to twiddle all that myself. https://github.com/clj-commons/rewrite-clj/blob/5ca373690efd8eb348f9a06eda7c4d32caeece3d/script/helper/graal.clj. I might reuse this on other projects, but only as a template that I would copy and paste.

👍 1
dominicm20:05:09

It seems inefficient and exclusionary to have scripts which are copied around. At the least, a tool could handle locating the native image binary and perhaps give you a few of the boring arguments (eg -cp) to pass to native image. I'm certain there's a solution which both exposes the dials AND reduces the maintenance effort across everyone. Clojure always optimizes for power users first, but it still becomes easier over time.

dominicm20:05:38

For example, that version assertion is dope. The perfect function to expose in a library. You could even stick a simple range on it.

lread20:05:02

Yeah, I felt the temptation at one point too @dominicm! But Graal native-image is so early days and fiddly that I felt that it was not practical to generalize. That’s just me, though! Not at all saying you are wrong and I am right, just sharing.

borkdude21:05:08

> At the least, a tool could handle locating the native image binary This is what the GRAALVM_HOME environment variable convention is for. The Github Action https://github.com/graalvm/setup-graalvm/ helps you with setting this up in CI, but doing it manually is trivial too (I've done it manually so far).

borkdude21:05:15

I also don't want to call native-image from within a JVM process since graalvm is very memory hungry and wasting extra memory on calling it from a JVM isn't something I want. Just a shell script will do.

borkdude21:05:11

If you leave more memory to native-image it will likely also be faster as it spends less time in GC.

dominicm21:05:17

Yeah, that's why I wanted to use the svm-driver so there's only one VM.

dominicm15:05:22

That's a great tip, exactly the kind of thing I'm looking for. I know native image will always be highly configured, but I'd love to make the basic hello world fairly seamless.

borkdude15:05:11

I think it makes sense to always compile to an uberjar first and then use -jar foo.jar

borkdude15:05:24

And always use "--no-fallback"

borkdude15:05:46

that should be about it for Clojure I think

dominicm15:05:50

Why an uberjar and not a classpath?

borkdude15:05:57

It doesn't matter much, but I find it easier to reason about an uberjar. e.g you can test if java -jar foo.jar works, if that doesn't work, then native image will fail as well

borkdude15:05:13

but classpath should work as well

borkdude15:05:37

I've had some problems with long classpaths on Windows where an uberjar did work correctly

borkdude15:05:48

but perhaps tools.build will fix that correctly now

dominicm15:05:02

I wonder if that applies when using process command?

dominicm15:05:34

I also wonder if native image can handle classpaths inside a jar manifest

borkdude15:05:04

I also find myself wanting to publish an uberjar as an alternative to the native image anyways, so I would need both

borkdude15:05:37

And a good default is to allow arbitrary native image arguments on top of the defaults as every projects requires its own settings