Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1711,4 +1711,19 @@ private static void validateEnableFallbackCompilation(HostedOptionKey<Boolean> o
public static boolean canEnableFallbackCompilation() {
return !EnableFallbackCompilation.getValue() && !useEconomyCompilerConfig();
}

@Option(help = "Passes the numbers of warnings that occurred in the driver phase to the builder.", type = OptionType.Debug, stability = OptionStability.STABLE) //
public static final HostedOptionKey<Integer> DriverWarningsCount = new HostedOptionKey<>(0);

@APIOption(name = "-Werror", defaultValue = "all")//
@Option(help = "Treat warnings as errors and terminate build.")//
public static final HostedOptionKey<AccumulatingLocatableMultiOptionValue.Strings> TreatWarningsAsError = new HostedOptionKey<>(AccumulatingLocatableMultiOptionValue.Strings.build(), key -> {
key.getValue().getValuesWithOrigins().forEach(option -> {
if (!option.origin().commandLineLike()) {
throw UserError.abort("Option '%s' provided by %s can only be used on the command line.",
SubstrateOptionsParser.commandArgument(key, option.value()), option.origin());
}
});
});

}
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,8 @@ private int completeImageBuild() {
// allow native access for all modules on the image builder module path
var enableNativeAccessModules = getModulesFromPath(imageBuilderModulePath).keySet();
imageBuilderJavaArgs.add("--enable-native-access=" + String.join(",", enableNativeAccessModules));
// pass the number of warnings to the builder process
imageBuilderArgs.add(oH(SubstrateOptions.DriverWarningsCount) + LogUtils.getWarningsCount());

boolean useColorfulOutput = configureBuildOutput();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
import java.util.stream.Stream;

import com.oracle.svm.core.RuntimeAssertionsSupport;
import com.oracle.svm.core.util.UserError;
import com.oracle.svm.util.LogUtils;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.impl.ImageSingletonsSupport;
Expand Down Expand Up @@ -795,7 +797,9 @@ public void printEpilog(Optional<String> optionalImageName, Optional<NativeImage
l().a(outcomePrefix(buildOutcome)).a(" generating '").bold().a(imageName).reset().a("' ")
.a(buildOutcome.successful() ? "in" : "after").a(" ").a(timeStats).a(".").println();

printWarningsCount();
printErrorMessage(optionalUnhandledThrowable, parsedHostedOptions);
checkTreatWarningsAsError();
}

private static String outcomePrefix(NativeImageGeneratorRunner.BuildOutcome buildOutcome) {
Expand All @@ -806,6 +810,46 @@ private static String outcomePrefix(NativeImageGeneratorRunner.BuildOutcome buil
};
}

private void printWarningsCount() {
int warningsCount = LogUtils.getWarningsCount() + SubstrateOptions.DriverWarningsCount.getValue();
if (warningsCount == 0) {
return;
}

l().println();
l().yellowBold().a("The build process encountered ").a(warningsCount).a(warningsCount == 1 ? " warning." : " warnings.").reset().println();
l().println();
}

private void checkTreatWarningsAsError() {
if (SubstrateOptions.TreatWarningsAsError.getValue().contains("all")) {
deleteBuiltArtifacts();
throw UserError.abort("Build failed: Warnings are treated as errors because the -Werror flag is set.");
}
}

/**
* Delete built artifacts. This is done e.g. in the -Werror case to ensure we don't "fail on
* error" but still have built - but potentially broken - artifacts created.
*/
private void deleteBuiltArtifacts() {
BuildArtifacts.singleton().forEach((artifactType, paths) -> {
if (artifactType != ArtifactType.BUILD_INFO) {
for (Path path : paths) {
try {
if (path.startsWith(SubstrateOptions.getImagePath())) {
java.nio.file.Files.delete(path);
} else {
LogUtils.warning("Cleaning up due to -Werror failed: Cannot delete artifacts not in " + SubstrateOptions.getImagePath() + ". Invalid: " + path);
}
} catch (IOException ex) {
LogUtils.warning("Cleaning up due to -Werror failed: cannot delete " + path + ": " + ex.getMessage());
}
}
}
});
}

private void printErrorMessage(Optional<Throwable> optionalUnhandledThrowable, OptionValues parsedHostedOptions) {
if (optionalUnhandledThrowable.isEmpty()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@

// Checkstyle: Allow raw info or warning printing - begin
public class LogUtils {
/**
* Number of warnings seen during image build. Note this is limited to the current process, i.e.
* there is a split between Driver and Builder.
*/
private static int warningsCount = 0;

/**
* Print an info message.
*/
Expand Down Expand Up @@ -66,6 +72,7 @@ public static void prefixInfo(String prefix, String format, Object... args) {
*/
public static void warning(String message) {
System.out.println("Warning: " + message);
warningsCount++;
}

/**
Expand All @@ -87,4 +94,8 @@ public static void warning(String format, Object... args) {
public static void warningDeprecatedEnvironmentVariable(String environmentVariableName) {
warning("The " + environmentVariableName + " environment variable is deprecated and might be removed in a future release. Please refer to the GraalVM release notes.");
}

public static int getWarningsCount() {
return warningsCount;
}
}