From da9c8475df117b61f91bd87c5ba9ee22e7346c74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Galder=20Zamarren=CC=83o?= Date: Wed, 27 Jan 2021 11:01:22 +0100 Subject: [PATCH] Add exclude-config option #2535 --- .../svm/driver/DefaultOptionHandler.java | 13 +++++++++++++ .../src/com/oracle/svm/driver/NativeImage.java | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/DefaultOptionHandler.java b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/DefaultOptionHandler.java index 5ed68d86c2efd..cd9e4cabd8a69 100644 --- a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/DefaultOptionHandler.java +++ b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/DefaultOptionHandler.java @@ -30,6 +30,7 @@ import java.nio.file.Paths; import java.util.List; import java.util.Queue; +import java.util.regex.Pattern; import org.graalvm.compiler.options.OptionType; @@ -151,6 +152,18 @@ public boolean consume(Queue args) { args.poll(); NativeImage.showWarning("Ignoring server-mode native-image argument " + headArg + "."); return true; + case "--exclude-config": + args.poll(); + String excludeConfig = args.poll(); + if (excludeConfig == null) { + NativeImage.showError(headArg + " requires a jar and resource regular expression, comma separated"); + } + final String[] split = excludeConfig.split(","); + if (split.length < 2) { + NativeImage.showError(headArg + " value '" + excludeConfig + " not correctly formatted. It requires a jar and resource regular expression, comma separated"); + } + nativeImage.addExcludeConfig(Paths.get(split[0]), Pattern.compile(split[1])); + return true; } String debugAttach = "--debug-attach"; diff --git a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java index 9d2c53963fa40..642b2168e5360 100644 --- a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java +++ b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java @@ -90,6 +90,7 @@ import java.util.jar.Attributes; import java.util.jar.JarFile; import java.util.jar.Manifest; +import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -226,6 +227,8 @@ private static String oR(OptionKey option) { final Registry optionRegistry; private LinkedHashSet enabledLanguages; + private final Map excludedConfigs = new HashMap<>(); + public static final String nativeImagePropertiesFilename = "native-image.properties"; public static final String nativeImageMetaInf = "META-INF/native-image"; @@ -934,6 +937,11 @@ private void processNativeImageMetaInf(Path classpathEntry, Path nativeImageMeta .filter(p -> p.endsWith(fileType.fileName)) .collect(Collectors.toList()); for (Path nativeImageMetaInfFile : nativeImageMetaInfFiles) { + boolean excluded = isExcluded(nativeImageMetaInfFile, classpathEntry); + if (excluded) { + continue; + } + Path resourceRoot = nativeImageMetaInfBase.getParent().getParent(); Function resolver = str -> { Path componentDirectory = resourceRoot.relativize(nativeImageMetaInfFile).getParent(); @@ -956,6 +964,16 @@ private void processNativeImageMetaInf(Path classpathEntry, Path nativeImageMeta } } + public void addExcludeConfig(Path jarPath, Pattern resourceRegex) { + excludedConfigs.put(jarPath, resourceRegex); + } + + private boolean isExcluded(Path resourcePath, Path classpathEntry) { + return excludedConfigs.entrySet().stream() + .filter(e -> classpathEntry.endsWith(e.getKey())) + .anyMatch(e -> e.getValue().matcher(resourcePath.toString()).find()); + } + private void processNativeImageMetaInf(@SuppressWarnings("unused") Path classpathEntry, Path resourceRoot, Path resourcePath, MetaInfFileType resourceType, Function resolver) throws IOException {