Skip to content

Commit

Permalink
Port NativeConfig to @ConfigMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Apr 12, 2023
1 parent 715ea8b commit 0f588c8
Show file tree
Hide file tree
Showing 12 changed files with 369 additions and 178 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public abstract class NativeImageBuildContainerRunner extends NativeImageBuildRu

protected NativeImageBuildContainerRunner(NativeConfig nativeConfig) {
this.nativeConfig = nativeConfig;
containerRuntime = nativeConfig.containerRuntime.orElseGet(ContainerRuntimeUtil::detectContainerRuntime);
containerRuntime = nativeConfig.containerRuntime().orElseGet(ContainerRuntimeUtil::detectContainerRuntime);

this.baseContainerRuntimeArgs = new String[] { "--env", "LANG=C", "--rm" };

Expand Down Expand Up @@ -112,8 +112,8 @@ public void addShutdownHook(Process process) {

protected List<String> getContainerRuntimeBuildArgs(Path outputDir) {
List<String> containerRuntimeArgs = new ArrayList<>();
nativeConfig.containerRuntimeOptions.ifPresent(containerRuntimeArgs::addAll);
if (nativeConfig.debugBuildProcess && nativeConfig.publishDebugBuildProcessPort) {
nativeConfig.containerRuntimeOptions().ifPresent(containerRuntimeArgs::addAll);
if (nativeConfig.debugBuildProcess() && nativeConfig.publishDebugBuildProcessPort()) {
// publish the debug port onto the host if asked for
containerRuntimeArgs.add("--publish=" + NativeImageBuildStep.DEBUG_BUILD_PROCESS_PORT + ":"
+ NativeImageBuildStep.DEBUG_BUILD_PROCESS_PORT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private String runCommandAndReadOutput(String[] command, String errorMsg) throws
protected void postBuild(Path outputDir, String nativeImageName, String resultingExecutableName) {
copyFromContainerVolume(outputDir, resultingExecutableName,
"Failed to copy native image from container volume back to the host.");
if (nativeConfig.debug.enabled) {
if (nativeConfig.debug().enabled()) {
copyFromContainerVolume(outputDir, "sources", "Failed to copy sources from container volume back to the host.");
String symbols = String.format("%s.debug", nativeImageName);
copyFromContainerVolume(outputDir, symbols, "Failed to copy debug symbols from container volume back to the host.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, LocalesBuildTimeCon
Optional<ProcessInheritIODisabledBuildItem> processInheritIODisabledBuildItem,
List<NativeImageFeatureBuildItem> nativeImageFeatures,
NativeImageRunnerBuildItem nativeImageRunner) {
if (nativeConfig.debug.enabled) {
if (nativeConfig.debug().enabled()) {
copyJarSourcesToLib(outputTargetBuildItem, curateOutcomeBuildItem);
copySourcesToSourceCache(outputTargetBuildItem);
}
Expand All @@ -204,7 +204,7 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, LocalesBuildTimeCon
String resultingExecutableName = getResultingExecutableName(nativeImageName, nativeImageRunner.isContainerBuild());
Path generatedExecutablePath = outputDir.resolve(resultingExecutableName);
Path finalExecutablePath = outputTargetBuildItem.getOutputDirectory().resolve(resultingExecutableName);
if (nativeConfig.reuseExisting) {
if (nativeConfig.reuseExisting()) {
if (Files.exists(finalExecutablePath)) {
return new NativeImageBuildItem(finalExecutablePath,
NativeImageBuildItem.GraalVMVersion.unknown());
Expand All @@ -223,7 +223,7 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, LocalesBuildTimeCon
}

try {
if (nativeConfig.cleanupServer) {
if (nativeConfig.cleanupServer()) {
log.warn(
"Your application is setting the deprecated 'quarkus.native.cleanup-server' configuration key"
+ " to true. Please consider removing this configuration key as it is ignored"
Expand Down Expand Up @@ -257,14 +257,14 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, LocalesBuildTimeCon
NativeImageBuildRunner.Result buildNativeResult = buildRunner.build(nativeImageArgs,
nativeImageName,
resultingExecutableName, outputDir,
graalVMVersion, nativeConfig.debug.enabled,
graalVMVersion, nativeConfig.debug().enabled(),
processInheritIODisabled.isPresent() || processInheritIODisabledBuildItem.isPresent());
if (buildNativeResult.getExitCode() != 0) {
throw imageGenerationFailed(buildNativeResult.getExitCode(), isContainerBuild);
}
IoUtils.copy(generatedExecutablePath, finalExecutablePath);
Files.delete(generatedExecutablePath);
if (nativeConfig.debug.enabled) {
if (nativeConfig.debug().enabled()) {
final String symbolsName = String.format("%s.debug", nativeImageName);
Path generatedSymbols = outputDir.resolve(symbolsName);
if (generatedSymbols.toFile().exists()) {
Expand All @@ -285,7 +285,7 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, LocalesBuildTimeCon
} catch (Exception e) {
throw new RuntimeException("Failed to build native image", e);
} finally {
if (nativeConfig.debug.enabled) {
if (nativeConfig.debug().enabled()) {
removeJarSourcesFromLib(outputTargetBuildItem);
IoUtils.recursiveDelete(outputDir.resolve(Paths.get(APP_SOURCES)));
}
Expand All @@ -310,8 +310,8 @@ private String getResultingExecutableName(String nativeImageName, boolean isCont
*/
@BuildStep
public NativeImageRunnerBuildItem resolveNativeImageBuildRunner(NativeConfig nativeConfig) {
boolean isExplicitContainerBuild = nativeConfig.containerBuild
.orElse(nativeConfig.containerRuntime.isPresent() || nativeConfig.remoteContainerBuild);
boolean isExplicitContainerBuild = nativeConfig.containerBuild()
.orElse(nativeConfig.containerRuntime().isPresent() || nativeConfig.remoteContainerBuild());
if (!isExplicitContainerBuild) {
NativeImageBuildLocalRunner localRunner = getNativeImageBuildLocalRunner(nativeConfig);
if (localRunner != null) {
Expand All @@ -326,7 +326,7 @@ public NativeImageRunnerBuildItem resolveNativeImageBuildRunner(NativeConfig nat
}
log.warn(errorMessage + " Attempting to fall back to container build.");
}
if (nativeConfig.remoteContainerBuild) {
if (nativeConfig.remoteContainerBuild()) {
return new NativeImageRunnerBuildItem(new NativeImageBuildRemoteContainerRunner(nativeConfig));
}
return new NativeImageRunnerBuildItem(new NativeImageBuildLocalContainerRunner(nativeConfig));
Expand Down Expand Up @@ -439,14 +439,14 @@ private void checkGraalVMVersion(GraalVM.Version version) {

private static NativeImageBuildLocalRunner getNativeImageBuildLocalRunner(NativeConfig nativeConfig) {
String executableName = getNativeImageExecutableName();
if (nativeConfig.graalvmHome.isPresent()) {
File file = Paths.get(nativeConfig.graalvmHome.get(), "bin", executableName).toFile();
if (nativeConfig.graalvmHome().isPresent()) {
File file = Paths.get(nativeConfig.graalvmHome().get(), "bin", executableName).toFile();
if (file.exists()) {
return new NativeImageBuildLocalRunner(file.getAbsolutePath());
}
}

File javaHome = nativeConfig.javaHome;
File javaHome = nativeConfig.javaHome();
if (javaHome == null) {
// try system property first - it will be the JAVA_HOME used by the current JVM
String home = System.getProperty(JAVA_HOME_SYS);
Expand Down Expand Up @@ -639,9 +639,9 @@ public Builder setNativeImageName(String nativeImageName) {
public NativeImageInvokerInfo build() {
List<String> nativeImageArgs = new ArrayList<>();
boolean enableSslNative = false;
boolean inlineBeforeAnalysis = nativeConfig.inlineBeforeAnalysis;
boolean addAllCharsets = nativeConfig.addAllCharsets;
boolean enableHttpsUrlHandler = nativeConfig.enableHttpsUrlHandler;
boolean inlineBeforeAnalysis = nativeConfig.inlineBeforeAnalysis();
boolean addAllCharsets = nativeConfig.addAllCharsets();
boolean enableHttpsUrlHandler = nativeConfig.enableHttpsUrlHandler();
for (NativeImageSystemPropertyBuildItem prop : nativeImageProperties) {
//todo: this should be specific build items
if (prop.getKey().equals("quarkus.ssl.native") && prop.getValue() != null) {
Expand Down Expand Up @@ -682,7 +682,7 @@ public NativeImageInvokerInfo build() {
nativeImageArgs.add("-H:IncludeLocales=" + includeLocales);
}

nativeImageArgs.add("-J-Dfile.encoding=" + nativeConfig.fileEncoding);
nativeImageArgs.add("-J-Dfile.encoding=" + nativeConfig.fileEncoding());

if (enableSslNative) {
enableHttpsUrlHandler = true;
Expand Down Expand Up @@ -713,7 +713,7 @@ public NativeImageInvokerInfo build() {
nativeImageArgs.add("-H:-ParseOnce");
}

if (nativeConfig.debug.enabled && graalVMVersion.compareTo(GraalVM.Version.VERSION_23_0_0) >= 0) {
if (nativeConfig.debug().enabled() && graalVMVersion.compareTo(GraalVM.Version.VERSION_23_0_0) >= 0) {
/*
* Instruct GraalVM / Mandrel to keep more accurate information about source locations when generating
* debug info for debugging and monitoring tools. This parameter may break compatibility with Truffle.
Expand Down Expand Up @@ -743,7 +743,7 @@ public NativeImageInvokerInfo build() {
// required by camel-quarkus-xstream
nativeImageArgs.add("-J--add-opens=java.base/java.util=ALL-UNNAMED");

if (nativeConfig.enableReports) {
if (nativeConfig.enableReports()) {
nativeImageArgs.add("-H:PrintAnalysisCallTreeType=CSV");
}

Expand All @@ -768,11 +768,11 @@ public NativeImageInvokerInfo build() {

nativeImageArgs.add("-H:+AllowFoldMethods");

if (nativeConfig.headless) {
if (nativeConfig.headless()) {
nativeImageArgs.add("-J-Djava.awt.headless=true");
}

if (nativeConfig.enableFallbackImages) {
if (nativeConfig.enableFallbackImages()) {
nativeImageArgs.add("--auto-fallback");
} else {
//Default: be strict as those fallback images aren't very useful
Expand All @@ -784,17 +784,17 @@ public NativeImageInvokerInfo build() {
nativeImageArgs.add("--link-at-build-time");
}

if (nativeConfig.reportErrorsAtRuntime) {
if (nativeConfig.reportErrorsAtRuntime()) {
nativeImageArgs.add("--report-unsupported-elements-at-runtime");
}
if (nativeConfig.reportExceptionStackTraces) {
if (nativeConfig.reportExceptionStackTraces()) {
nativeImageArgs.add("-H:+ReportExceptionStackTraces");
}
if (nativeConfig.debug.enabled) {
if (nativeConfig.debug().enabled()) {
nativeImageArgs.add("-g");
nativeImageArgs.add("-H:DebugInfoSourceSearchPath=" + APP_SOURCES);
}
if (nativeConfig.debugBuildProcess) {
if (nativeConfig.debugBuildProcess()) {
String debugBuildProcessHost;
if (containerBuild) {
debugBuildProcessHost = "0.0.0.0";
Expand All @@ -805,14 +805,14 @@ public NativeImageInvokerInfo build() {
.add("-J-Xrunjdwp:transport=dt_socket,address=" + debugBuildProcessHost + ":"
+ DEBUG_BUILD_PROCESS_PORT + ",server=y,suspend=y");
}
if (nativeConfig.dumpProxies) {
if (nativeConfig.dumpProxies()) {
nativeImageArgs.add("-Dsun.misc.ProxyGenerator.saveGeneratedFiles=true");
}
if (nativeConfig.nativeImageXmx.isPresent()) {
nativeImageArgs.add("-J-Xmx" + nativeConfig.nativeImageXmx.get());
if (nativeConfig.nativeImageXmx().isPresent()) {
nativeImageArgs.add("-J-Xmx" + nativeConfig.nativeImageXmx().get());
}
List<String> protocols = new ArrayList<>(2);
if (nativeConfig.enableHttpUrlHandler) {
if (nativeConfig.enableHttpUrlHandler()) {
protocols.add("http");
}
if (enableHttpsUrlHandler) {
Expand All @@ -833,47 +833,47 @@ public NativeImageInvokerInfo build() {
nativeImageArgs.add("-H:NativeLinkerOption=" + noPIE);
}

if (!nativeConfig.enableIsolates) {
if (!nativeConfig.enableIsolates()) {
nativeImageArgs.add("-H:-SpawnIsolates");
}
if (!nativeConfig.enableJni) {
if (!nativeConfig.enableJni()) {
log.warn(
"Your application is setting the deprecated 'quarkus.native.enable-jni' configuration key to false."
+ " Please consider removing this configuration key as it is ignored (JNI is always enabled) and it"
+ " will be removed in a future Quarkus version.");
}
if (nativeConfig.enableServer) {
if (nativeConfig.enableServer()) {
log.warn(
"Your application is setting the deprecated 'quarkus.native.enable-server' configuration key to true."
+ " Please consider removing this configuration key as it is ignored"
+ " (The Native image build server is always disabled) and it"
+ " will be removed in a future Quarkus version.");
}
if (nativeConfig.enableVmInspection) {
if (nativeConfig.enableVmInspection()) {
nativeImageArgs.add("-H:+AllowVMInspection");
}

if (nativeConfig.monitoring.isPresent()) {
List<NativeConfig.MonitoringOption> monitoringOptions = nativeConfig.monitoring.get();
if (nativeConfig.monitoring().isPresent()) {
List<NativeConfig.MonitoringOption> monitoringOptions = nativeConfig.monitoring().get();
if (!monitoringOptions.isEmpty()) {
nativeImageArgs.add("--enable-monitoring=" + monitoringOptions.stream()
.map(o -> o.name().toLowerCase(Locale.ROOT)).collect(Collectors.joining(",")));
}
}
if (nativeConfig.autoServiceLoaderRegistration) {
if (nativeConfig.autoServiceLoaderRegistration()) {
nativeImageArgs.add("-H:+UseServiceLoaderFeature");
//When enabling, at least print what exactly is being added:
nativeImageArgs.add("-H:+TraceServiceLoaderFeature");
} else {
nativeImageArgs.add("-H:-UseServiceLoaderFeature");
}
if (nativeConfig.fullStackTraces) {
if (nativeConfig.fullStackTraces()) {
nativeImageArgs.add("-H:+StackTrace");
} else {
nativeImageArgs.add("-H:-StackTrace");
}

if (nativeConfig.enableDashboardDump) {
if (nativeConfig.enableDashboardDump()) {
nativeImageArgs.add("-H:DashboardDump=" + outputTargetBuildItem.getBaseName() + "_dashboard.dump");
nativeImageArgs.add("-H:+DashboardAll");
}
Expand Down Expand Up @@ -941,8 +941,8 @@ public NativeImageInvokerInfo build() {
}

private void handleAdditionalProperties(List<String> command) {
if (nativeConfig.additionalBuildArgs.isPresent()) {
List<String> strings = nativeConfig.additionalBuildArgs.get();
if (nativeConfig.additionalBuildArgs().isPresent()) {
List<String> strings = nativeConfig.additionalBuildArgs().get();
for (String buildArg : strings) {
String trimmedBuildArg = buildArg.trim();
if (trimmedBuildArg.contains(TRUST_STORE_SYSTEM_PROPERTY_MARKER) && containerBuild) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void compress(NativeConfig nativeConfig, NativeImageRunnerBuildItem nativ
NativeImageBuildItem image,
BuildProducer<UpxCompressedBuildItem> upxCompressedProducer,
BuildProducer<ArtifactResultBuildItem> artifactResultProducer) {
if (nativeConfig.compression.level.isEmpty()) {
if (nativeConfig.compression().level().isEmpty()) {
log.debug("UPX compression disabled");
return;
}
Expand All @@ -66,8 +66,8 @@ public void compress(NativeConfig nativeConfig, NativeImageRunnerBuildItem nativ
}

private boolean runUpxFromHost(File upx, File executable, NativeConfig nativeConfig) {
String level = getCompressionLevel(nativeConfig.compression.level.getAsInt());
List<String> extraArgs = nativeConfig.compression.additionalArgs.orElse(Collections.emptyList());
String level = getCompressionLevel(nativeConfig.compression().level().getAsInt());
List<String> extraArgs = nativeConfig.compression().additionalArgs().orElse(Collections.emptyList());
List<String> args = Stream.concat(
Stream.concat(Stream.of(upx.getAbsolutePath(), level), extraArgs.stream()),
Stream.of(executable.getAbsolutePath()))
Expand Down Expand Up @@ -100,11 +100,11 @@ private boolean runUpxFromHost(File upx, File executable, NativeConfig nativeCon

private boolean runUpxInContainer(NativeImageBuildItem nativeImage, NativeConfig nativeConfig,
String effectiveBuilderImage) {
String level = getCompressionLevel(nativeConfig.compression.level.getAsInt());
List<String> extraArgs = nativeConfig.compression.additionalArgs.orElse(Collections.emptyList());
String level = getCompressionLevel(nativeConfig.compression().level().getAsInt());
List<String> extraArgs = nativeConfig.compression().additionalArgs().orElse(Collections.emptyList());

List<String> commandLine = new ArrayList<>();
ContainerRuntimeUtil.ContainerRuntime containerRuntime = nativeConfig.containerRuntime
ContainerRuntimeUtil.ContainerRuntime containerRuntime = nativeConfig.containerRuntime()
.orElseGet(ContainerRuntimeUtil::detectContainerRuntime);
commandLine.add(containerRuntime.getExecutableName());

Expand Down
Loading

0 comments on commit 0f588c8

Please sign in to comment.