Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native sources build step output GraalVM version and docker image to use #32335

Merged
merged 2 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,28 @@ ArtifactResultBuildItem nativeSourcesResult(NativeConfig nativeConfig,
.setNativeImageFeatures(nativeImageFeatures)
.build();
List<String> command = nativeImageArgs.getArgs();
try (FileOutputStream commandFOS = new FileOutputStream(outputDir.resolve("native-image.args").toFile())) {
try (FileOutputStream commandFOS = new FileOutputStream(outputDir.resolve("native-image.args").toFile());
FileOutputStream dockerImageNameFOS = new FileOutputStream(outputDir.resolve("graalvm.version").toFile())) {
String commandStr = String.join(" ", command);
commandFOS.write(commandStr.getBytes(StandardCharsets.UTF_8));

log.info("The sources for a subsequent native-image run along with the necessary arguments can be found in "
+ outputDir);
dockerImageNameFOS.write(GraalVM.Version.CURRENT.version.toString().getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
throw new RuntimeException("Failed to build native image sources", e);
}

if (nativeConfig.isContainerBuild()) {
try (FileOutputStream dockerImageNameFOS = new FileOutputStream(
outputDir.resolve("native-builder.image").toFile())) {
dockerImageNameFOS.write(nativeConfig.getEffectiveBuilderImage().getBytes(StandardCharsets.UTF_8));
scrocquesel marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception e) {
throw new RuntimeException("Failed to build native image sources", e);
}
}
scrocquesel marked this conversation as resolved.
Show resolved Hide resolved

log.info("The sources for a subsequent native-image run along with the necessary arguments can be found in "
+ outputDir);

// drop the original output to avoid confusion
IoUtils.recursiveDelete(nativeImageSourceJarBuildItem.getPath().getParent());

Expand Down
26 changes: 17 additions & 9 deletions docs/src/main/asciidoc/building-native-image.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -746,16 +746,17 @@ After compilation has finished, you find the build artifact in `target/native-so
----
$ cd target/native-sources
$ ls
native-image.args getting-started-1.0.0-SNAPSHOT-runner.jar lib
getting-started-1.0.0-SNAPSHOT-runner.jar graalvm.version lib native-image.args
----

From the output above one can see that, in addition to the produced jar file and the associated lib directory, a text file named `native-image.args` was created.
This file holds all parameters (including the name of the JAR to compile) to pass along to GraalVM's `native-image` command.
If you have GraalVM installed, you can start the native compilation by executing:
A text file named `graalvm.version` was also created and holds the GraalVM version that should be used.
If you have GraalVM installed and it matches this version, you can start the native compilation by executing:

[source,bash]
----
$ cd target/native-source
$ cd target/native-sources
$ native-image $(cat native-image.args)
...
$ ls
Expand All @@ -769,22 +770,29 @@ The process for Gradle is analogous.

Running the build process in a container is also possible:

[source,bash]
----
$ ./mvnw clean package -Dquarkus.package.type=native-sources -Dquarkus.native.container-build=true
----

`-Dquarkus.native.container-build=true` will produce an additional text file named `native-builder.image` holding the docker image name to be used to build the native image.

[source,bash,subs=attributes+]
----
cd target/native-image
cd target/native-sources
docker run \
-it \
--rm \
--v $(pwd):/work <1>
-w /work <2>
--v $(pwd):/work \# <1>
-w /work \# <2>
--entrypoint bin/sh \
quay.io/quarkus/ubi-quarkus-graalvmce-builder-image:{graalvm-flavor} \ <3>
-c "native-image $(cat native-image.args) -J-Xmx4g" <4>
$(cat native-builder.image) \# <3>
-c "native-image $(cat native-image.args) -J-Xmx4g"# <4>
----

<1> Mount the host's directory `target/native-image` to the container's `/work`. Thus, the generated binary will also be written to this directory.
<2> Switch the working directory to `/work`, which we have mounted in <1>.
<3> Use the `quay.io/quarkus/ubi-quarkus-graalvmce-builder-image:{graalvm-flavor}` docker image introduced in <<#multistage-docker,Using a multi-stage Docker build>> to build the native image.
<3> Use the docker image from the file `native-builder.image`.
<4> Call `native-image` with the content of file `native-image.args` as arguments. We also supply an additional argument to limit the process's maximum memory to 4 Gigabytes (this may vary depending on the project being built and the machine building it).

[WARNING]
Expand Down