Skip to content

Commit 6a75f11

Browse files
committed
add option -Werror to fail when a warning occurs
1 parent 9795ca5 commit 6a75f11

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,4 +1715,15 @@ public static boolean canEnableFallbackCompilation() {
17151715
@Option(help = "Passes the numbers of warnings that occurred in the driver phase to the builder.", type = OptionType.Debug, stability = OptionStability.STABLE) //
17161716
public static final HostedOptionKey<Integer> DriverWarningsCount = new HostedOptionKey<>(0);
17171717

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

substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1493,7 +1493,7 @@ private int completeImageBuild() {
14931493
var enableNativeAccessModules = getModulesFromPath(imageBuilderModulePath).keySet();
14941494
imageBuilderJavaArgs.add("--enable-native-access=" + String.join(",", enableNativeAccessModules));
14951495
// pass the number of warnings to the builder process
1496-
imageBuilderArgs.add(oH(SubstrateOptions.DriverWarningsCount)+LogUtils.getWarningsCount());
1496+
imageBuilderArgs.add(oH(SubstrateOptions.DriverWarningsCount) + LogUtils.getWarningsCount());
14971497

14981498
boolean useColorfulOutput = configureBuildOutput();
14991499

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ProgressReporter.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import java.util.stream.Stream;
5555

5656
import com.oracle.svm.core.RuntimeAssertionsSupport;
57+
import com.oracle.svm.core.util.UserError;
5758
import com.oracle.svm.util.LogUtils;
5859
import org.graalvm.nativeimage.ImageSingletons;
5960
import org.graalvm.nativeimage.hosted.Feature;
@@ -796,8 +797,9 @@ public void printEpilog(Optional<String> optionalImageName, Optional<NativeImage
796797
l().a(outcomePrefix(buildOutcome)).a(" generating '").bold().a(imageName).reset().a("' ")
797798
.a(buildOutcome.successful() ? "in" : "after").a(" ").a(timeStats).a(".").println();
798799

799-
printWarningMessages();
800+
printWarningsCount();
800801
printErrorMessage(optionalUnhandledThrowable, parsedHostedOptions);
802+
checkTreatWarningsAsError();
801803
}
802804

803805
private static String outcomePrefix(NativeImageGeneratorRunner.BuildOutcome buildOutcome) {
@@ -808,7 +810,7 @@ private static String outcomePrefix(NativeImageGeneratorRunner.BuildOutcome buil
808810
};
809811
}
810812

811-
private void printWarningMessages() {
813+
private void printWarningsCount() {
812814
int warningsCount = LogUtils.getWarningsCount() + SubstrateOptions.DriverWarningsCount.getValue();
813815
if (warningsCount == 0) {
814816
return;
@@ -819,6 +821,35 @@ private void printWarningMessages() {
819821
l().println();
820822
}
821823

824+
private void checkTreatWarningsAsError() {
825+
if (SubstrateOptions.TreatWarningsAsError.getValue().contains("all")) {
826+
deleteBuiltArtifacts();
827+
throw UserError.abort("Build failed: Warnings are treated as errors because the -Werror flag is set.");
828+
}
829+
}
830+
831+
/**
832+
* Delete built artifacts. This is done e.g. in the -Werror case to ensure we don't "fail on
833+
* error" but still have built - but potentially broken - artifacts created.
834+
*/
835+
private void deleteBuiltArtifacts() {
836+
BuildArtifacts.singleton().forEach((artifactType, paths) -> {
837+
if (artifactType != ArtifactType.BUILD_INFO) {
838+
for (Path path : paths) {
839+
try {
840+
if (path.startsWith(SubstrateOptions.getImagePath())) {
841+
java.nio.file.Files.delete(path);
842+
} else {
843+
LogUtils.warning("Cleaning up due to -Werror failed: Cannot delete artifacts not in " + SubstrateOptions.getImagePath() + ". Invalid: " + path);
844+
}
845+
} catch (IOException ex) {
846+
LogUtils.warning("Cleaning up due to -Werror failed: cannot delete " + path + ": " + ex.getMessage());
847+
}
848+
}
849+
}
850+
});
851+
}
852+
822853
private void printErrorMessage(Optional<Throwable> optionalUnhandledThrowable, OptionValues parsedHostedOptions) {
823854
if (optionalUnhandledThrowable.isEmpty()) {
824855
return;

substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/LogUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
// Checkstyle: Allow raw info or warning printing - begin
3131
public class LogUtils {
3232
/**
33-
* Number of warnings seen during image build. Note this is limited to the current process, i.e. there is a split
34-
* between Driver and Builder.
33+
* Number of warnings seen during image build. Note this is limited to the current process, i.e.
34+
* there is a split between Driver and Builder.
3535
*/
3636
private static int warningsCount = 0;
3737

0 commit comments

Comments
 (0)