Skip to content

Commit

Permalink
Embed quarkus version in native executable as a global string symbol
Browse files Browse the repository at this point in the history
Enables getting the quarkus version without running the native
executable:

```
❯ strings target/quarkus-999-SNAPSHOT-runner | grep -F 'quarkus.version'
quarkus.version=999-SNAPSHOT
```

Closes quarkusio#43020
  • Loading branch information
zakkak committed Sep 5, 2024
1 parent fcb4958 commit a688cd1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkus.deployment;

import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveFieldBuildItem;
import io.quarkus.deployment.steps.MainClassBuildStep;
import io.quarkus.runtime.Application;

public class QuarkusVersionProcessor {
@BuildStep
ReflectiveFieldBuildItem setupVersionField() {
return new ReflectiveFieldBuildItem(
"Ensure it's included in the executable to be able to grep the quarkus version",
Application.APP_CLASS_NAME, MainClassBuildStep.GREPABLE_QUARKUS_VERSION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ public final class ReflectiveFieldBuildItem extends MultiBuildItem {
final String reason;

public ReflectiveFieldBuildItem(String reason, FieldInfo field) {
this.reason = reason;
this.name = field.name();
this.declaringClass = field.declaringClass().name().toString();
this(reason, field.declaringClass().name().toString(), field.name());
}

public ReflectiveFieldBuildItem(FieldInfo field) {
Expand All @@ -27,9 +25,13 @@ public ReflectiveFieldBuildItem(Field field) {
}

public ReflectiveFieldBuildItem(String reason, Field field) {
this(reason, field.getDeclaringClass().getName(), field.getName());
}

public ReflectiveFieldBuildItem(String reason, String declaringClass, String fieldName) {
this.reason = reason;
this.name = field.getName();
this.declaringClass = field.getDeclaringClass().getName();
this.name = fieldName;
this.declaringClass = declaringClass;
}

public String getDeclaringClass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public class MainClassBuildStep {
static final String STARTUP_CONTEXT = "STARTUP_CONTEXT";
static final String LOG = "LOG";
static final String JAVA_LIBRARY_PATH = "java.library.path";
// This is declared as a constant so that it can be grepped for in the native-image binary using `strings`, e.g.:
// strings ./target/quarkus-runner | grep "quarkus.version="
public static final String GREPABLE_QUARKUS_VERSION = "GREPABLE_QUARKUS_VERSION";

public static final String GENERATE_APP_CDS_SYSTEM_PROPERTY = "quarkus.appcds.generate";

Expand Down Expand Up @@ -155,6 +158,9 @@ void build(List<StaticBytecodeRecorderBuildItem> staticInitTasks,
FieldCreator scField = file.getFieldCreator(STARTUP_CONTEXT_FIELD);
scField.setModifiers(Modifier.PUBLIC | Modifier.STATIC);

FieldCreator quarkusVersionField = file.getFieldCreator(GREPABLE_QUARKUS_VERSION, String.class)
.setModifiers(Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL);

MethodCreator ctor = file.getMethodCreator("<init>", void.class);
ctor.invokeSpecialMethod(ofMethod(Application.class, "<init>", void.class, boolean.class),
ctor.getThis(), ctor.load(launchMode.isAuxiliaryApplication()));
Expand Down Expand Up @@ -192,6 +198,9 @@ void build(List<StaticBytecodeRecorderBuildItem> staticInitTasks,
mv.writeStaticField(logField.getFieldDescriptor(), mv.invokeStaticMethod(
ofMethod(Logger.class, "getLogger", Logger.class, String.class), mv.load("io.quarkus.application")));

// Init the GREPABLE_QUARKUS_VERSION field
mv.writeStaticField(quarkusVersionField.getFieldDescriptor(), mv.load("quarkus.version=" + Version.getVersion()));

ResultHandle startupContext = mv.newInstance(ofConstructor(StartupContext.class));
mv.writeStaticField(scField.getFieldDescriptor(), startupContext);
TryBlock tryBlock = mv.tryBlock();
Expand Down

0 comments on commit a688cd1

Please sign in to comment.