Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Native image

A Goldberry application can be built as a GraalVM native image: no class loading, no reflection on the binding path, and a start-up measured against the process rather than against a JVM. The binding schema is designed for that.

The Showcase packages the desktop library, web-view shim, media codecs and SVG renderer on Linux x64, macOS arm64 and Windows x64. It carries fonts, icons, stylesheets and the widget catalogues it uses. The modules an application includes determine the executable’s size.

Metadata and resources

Before anything else: the C toolchain

native-image links with the system gcc, so it needs a C toolchain and the development package for zlib — not merely the runtime one, which is what a desktop already has:

sudo apt install build-essential zlib1g-dev     # Debian / Ubuntu
sudo dnf install gcc glibc-devel zlib-devel libstdc++-static

Without it the build runs to completion, spends a minute on analysis, and fails at the last step with cannot find -lz. zlib1g alone is not enough: the linker resolves -lz through the libz.so symlink that zlib1g-dev installs.

The two commands

./gradlew :example:nativeImageMetadata -Pgraalvm.home=/path/to/graalvm
./gradlew :example:nativeImage         -Pgraalvm.home=/path/to/graalvm

GRAALVM_HOME works instead of the property. Either way it must be a GraalVM and not a stock JDK — native-image and the tracing agent ship only with the former, and the task says so if you point it at the wrong thing.

The result is one executable in example/build/native/goldberry-showcase-<target>. Goldberry embeds the shared libraries from the classifier jars as resources and extracts each library to a temporary file when first loaded.

Showcase CI embeds the core and web-view libraries, media codecs and SVG renderer. To include the codecs and SVG renderer in a local image, first run ./gradlew :media:nativesBuild :vector:nativesBuild. For builds from another runner, pass their install directories with -Pgoldberry.media.libdir and -Pgoldberry.vector.libdir. Run the resulting executable with --verify-native-libraries to check that the web-view, media and SVG libraries load. Web views and system decoders use the operating system’s installed runtimes.

That makes a writable temp directory a requirement, and -Dgoldberry.native.library is the way out of one that is read-only or noexec.

Why there are two commands

A closed world has to know every foreign function the program will call before it runs, and Goldberry’s are not knowable from the source: a binding class takes a SymbolLookup obtained at run time and builds its handles from it, which is what lets an application choose which libgoldberry it loads. libgoldberry exports 246 symbols, plus six upcalls from five owners: the event watch, the file dialogs, the tray, the layout engine’s measure callback and the clipboard. The list and the bindings agree in both directions: an exported symbol nothing binds is dead weight in every artifact, and a bound symbol nothing exports is a link error at the first call.

So the first command runs the showcase under GraalVM’s tracing agent and records what it saw — the foreign descriptors, the resources, the reflection Logback does — into

example/src/main/resources/META-INF/native-image/dev.goldberry/goldberry-example/

That path is under src, not build: the metadata is source. It is reviewed in a diff, it changes when the application does, and being inside the jar is what lets a downstream image build find it without being told.

The trace is only as good as the run, and that is why resources are not traced. :core, :widgets and :example each ship a META-INF/native-image/…/reachability-metadata.json declaring their own files by glob, because that set is finite and a directory listing cannot be one screen short. A stylesheet or a font the far side of a toggle the run never flipped is not in a trace.

Because those declarations travel in the jars, an application building its own image gets the toolkit’s resources without knowing it needs them.

What is traced — the reflection, the services, the upcall stubs — really does depend on what the code did, and the warning applies to it unchanged: a screen the run never reaches contributes nothing. Re-run the metadata task after adding one, and read the diff.

The FFM descriptors are not traced. A holder links its handle in its class initializer, but a …Calls record binds when its wrapper is first used, so a run that opened no Markdown would initialise no MarkdownCalls and a trace would record none of its five functions. :natives:foreignMetadata initialises every holder and every upcall owner and writes the foreign section itself, into the goldberry-natives jar under META-INF/native-image/, where native-image reads it for any application.

The image is woven, the jar is not

nativeImage depends on weaveModels, and the build orders it before jar. That is not a detail: an image built from classes the weaver has not run over would bind its models by reflection, which is the one thing an image must not do. nativeImage arranges it for you. Ahead-of-time binding covers an image built without this task.

What the flags are for

FlagWhy
--module-path / --moduleThe showcase runs modular, as it does everywhere else
--enable-native-access=…nativesJEP 472, naming the one module that touches native code
--no-fallbackNot passed. A fallback image is a JVM in a trench coat. GraalVM 25.3 builds none, and the flag only warns that it has no effect
-H:+ReportExceptionStackTracesNames the class that could not be reached, rather than a stack in the builder

Nothing about class initialization is passed here. :natives ships its own META-INF/native-image/dev.goldberry/goldberry-natives/native-image.properties naming the two classes that have an opinion, and they are opposite opinions:

ClassWhenWhy
NativeLibraryrun timeIt dlopens in its initializer, which must not happen in the builder
Downcallsbuild timeIt holds the shared Linker, and every holder’s initializer calls Downcalls.link
the …calls packagesbuild timeA downcall handle is only a call if it is a compile-time constant, and only a build-time initializer makes it one

They are packages and they have to be. A holder is a nested class, and naming its enclosing class does not reach it. The call then costs 4538 ns against 8, silently, because the image builds and runs. Naming a hundred and thirty-four nested classes in a flag is not a list anyone can maintain, and naming the binding packages instead would build-time initialize the binding classes themselves, whose holder idiom dlopens the library in the builder. So the holders live in packages that contain nothing else.

All of them travel in the jar, so an application building its own image gets them without knowing they exist, as it gets the resource declarations.

The one flag the frame rate depends on

GraalVM’s FFM downcalls are not optimized — oracle/graal#8113 lists it as open work, and it costs a factor of 450 on the call itself. Goldberry takes the workaround: a holder’s FD_<symbol> handle is unbound (it takes the address to call as an argument), so it can be linked while the image is being built, which is what turns it into a constant the compiler can lower into a direct call.

Sixty frames of the showcase, headless (--resize=WxH walks the window a pixel a frame while it runs, and --late-budget=N fails the run past N missed refreshes):

./example/build/native/goldberry-showcase-linux-x64     -Dgoldberry.backend.videoDriver=dummy --frames=60
60 framesper frame
without the --initialize-at-build-time lines2.55 s42.5 ms
with it0.061 s1.0 ms

The same rule applies one level down, and it is the trap to know before editing a binding: a downcall handle has to be read by the method that calls it. Passing one into a helper as an argument costs 810 ns a call in an image against 8.9 ns when the helper names the constant itself — the JVM inlines and folds it, and native-image does not. That is why a holder’s call names its own FD_<symbol> field rather than taking a handle, and why the handle is static final on the holder rather than a component of it: an instance field is a value read from an object, not a constant read from a class, and measures 4540 ns/call.

And the list of packages is its own hazard. native-image.properties is the one file in :natives that nothing compiles, runs or reads — it is a hand-typed package list consumed by a tool that is not part of this build — so a package added to the module and not to the file is invisible in every way but the measurement above. The shipped resource is held to ForeignSurface.holderClassNames(), with one deliberate exception: desktop.calls stays out, because PortalSettings binds a dozen desktop-bus functions in a static initialiser. Build-time initialising it either fails the image with a MemorySegment in the image heap or bakes the build machine’s session bus into it.

Nothing fails when it is missing. The image builds, runs, paints correctly and is forty times slower. It is silent because the generated metadata registers the descriptors anyway. A descriptor registered nowhere raises MissingForeignRegistrationError and names itself. To see the difference, move the properties file aside and rebuild passing the run-time half by hand:

./gradlew :example:nativeImage -Pgraalvm.home=… \
    -Pgraalvm.args="--initialize-at-run-time=dev.goldberry.natives.NativeLibrary"

Two metadata directories: traced, and written

The agent’s output goes to META-INF/native-image/dev.goldberry/goldberry-example, and nothing hand-written goes in there — the next trace overwrites it. Anything a human has to add lives in the sibling …/goldberry-example-manual. native-image reads every META-INF/native-image/** it finds, so the two are merged for the tool and kept apart for the diff.

It holds one entry:

{ "module": "dev.goldberry.example", "glob": "logback.xml" }

Logback asks a ClassLoader for logback.xml, so the agent records it as a classpath resource. The image runs on the module path, where that file is at the root of a named module and has to be registered against that module or it is not there at all. The symptom is the worst kind: with no configuration found, logback ends with no appenders and prints nothing — not even its own status — so an image that is working perfectly looks like an image that is doing nothing.

The general shape of that trap is worth remembering: the agent records how a lookup was made, not where the file will be. A resource fetched through a ClassLoader by a library that knows nothing of modules is recorded without one. An application’s own logback.xml is the same file with the same trap, so it belongs in that application’s hand-written directory too.

In a native image, Logback’s search for duplicate configuration files can trigger DNS lookups when a module-resource URL is hashed. To give Logback the file directly, register a Configurator that loads the module’s logback.xml with JoranConfigurator and returns DO_NOT_INVOKE_NEXT_IF_ANY. The Showcase uses this approach.

What the trace does not record

The trace records no widget class, neither the toolkit’s nor an application’s. The element tree asks the two widgets for their restyle answer and compares them, so no widget class is reflected on, and every class in dev.goldberry.widget is in a closed list, as a model bound ahead of time is.

The natives jar is a module

Each classifier jar names itself: dev.goldberry.natives.linux_x64, dev.goldberry.natives.linux_aarch64, dev.goldberry.natives.macos_aarch64, dev.goldberry.natives.windows_x64. A modular build keeps it on the module path instead of splitting it onto the class path.

Nothing requires a platform’s module, so the JVM does not resolve it, and NativeLibrary reads the library from the module path anyway: this module, the system class loader, then the jar on --module-path by its name. The library’s directory is not a package name, so nothing has to be opened.

native-image is the exception. An image takes only resolved modules, so the natives jar either stays on -cp, which is what the showcase does, or goes on the module path with --add-modules dev.goldberry.natives.<target>.

Why the library is carried rather than linked

Statically linking the archives into the image is the obvious answer and it does not work. The linking part is fine — -Wl,-u,<symbol> pulls the code in, given -lstdc++ and -lm which native-image does not pass. What fails is that Goldberry resolves every native function by name at run time, so the symbols have to be in the executable’s dynamic symbol table, and native-image links with its own --version-script that makes everything it does not list local. --export-dynamic-symbol does not beat it, and a second version script is refused outright — “anonymous version tag cannot be combined with other version tags”.

What is not built

No image task in build. Neither task is wired into build, because a local build has no GraalVM to count on. A release of the showcase carries an image for each of the three platforms on its GitHub Release.

No image of the toolkit on its own. :core and :widgets are libraries; an image is a property of an application, and :example is the application here.