Search in sources :

Example 1 with FilterConfigurationParser

use of com.oracle.svm.configure.filters.FilterConfigurationParser in project graal by oracle.

the class ConfigurationTool method generate.

@SuppressWarnings("fallthrough")
private static void generate(Iterator<String> argsIter, boolean acceptTraceFileArgs) throws IOException {
    List<URI> traceInputs = new ArrayList<>();
    boolean builtinCallerFilter = true;
    boolean builtinHeuristicFilter = true;
    List<URI> callerFilters = new ArrayList<>();
    ConfigurationSet omittedInputSet = new ConfigurationSet();
    ConfigurationSet inputSet = new ConfigurationSet();
    ConfigurationSet outputSet = new ConfigurationSet();
    while (argsIter.hasNext()) {
        String[] parts = argsIter.next().split("=", 2);
        String current = parts[0];
        String value = (parts.length > 1) ? parts[1] : null;
        ConfigurationSet set = outputSet;
        switch(current) {
            case "--input-dir":
                inputSet.addDirectory(requirePath(current, value));
                break;
            case "--output-dir":
                Path directory = requirePath(current, value);
                if (!Files.exists(directory)) {
                    Files.createDirectory(directory);
                } else if (!Files.isDirectory(directory)) {
                    throw new NoSuchFileException(value);
                }
                outputSet.addDirectory(directory);
                break;
            case "--omit-from-input-dir":
                omittedInputSet.addDirectory(requirePath(current, value));
                break;
            case "--reflect-input":
                // fall through
                set = inputSet;
            case "--reflect-output":
                set.getReflectConfigPaths().add(requirePathUri(current, value));
                break;
            case "--jni-input":
                // fall through
                set = inputSet;
            case "--jni-output":
                set.getJniConfigPaths().add(requirePathUri(current, value));
                break;
            case "--proxy-input":
                // fall through
                set = inputSet;
            case "--proxy-output":
                set.getProxyConfigPaths().add(requirePathUri(current, value));
                break;
            case "--resource-input":
                // fall through
                set = inputSet;
            case "--resource-output":
                set.getResourceConfigPaths().add(requirePathUri(current, value));
                break;
            case "--serialization-input":
                // fall through
                set = inputSet;
            case "--serialization-output":
                set.getSerializationConfigPaths().add(requirePathUri(current, value));
                break;
            case "--predefined-classes-input":
                // fall through
                set = inputSet;
            case "--predefined-classes-output":
                set.getPredefinedClassesConfigPaths().add(requirePathUri(current, value));
                break;
            case "--trace-input":
                traceInputs.add(requirePathUri(current, value));
                break;
            case // legacy
            "--no-filter":
                builtinCallerFilter = false;
                builtinHeuristicFilter = false;
                break;
            case "--no-builtin-caller-filter":
                builtinCallerFilter = false;
                break;
            case "--no-builtin-heuristic-filter":
                builtinHeuristicFilter = false;
                break;
            case "--caller-filter-file":
                callerFilters.add(requirePathUri(current, value));
                break;
            case "--":
                if (acceptTraceFileArgs) {
                    argsIter.forEachRemaining(arg -> traceInputs.add(Paths.get(arg).toUri()));
                } else {
                    throw new UsageException("Unknown argument: " + current);
                }
                break;
            default:
                if (!acceptTraceFileArgs || current.startsWith("-")) {
                    throw new UsageException("Unknown argument: " + current);
                }
                traceInputs.add(Paths.get(current).toUri());
                break;
        }
    }
    failIfAgentLockFilesPresent(inputSet, omittedInputSet, outputSet);
    RuleNode callersFilter = null;
    if (!builtinCallerFilter) {
        callersFilter = RuleNode.createRoot();
        callersFilter.addOrGetChildren("**", RuleNode.Inclusion.Include);
    }
    if (!callerFilters.isEmpty()) {
        if (callersFilter == null) {
            callersFilter = AccessAdvisor.copyBuiltinCallerFilterTree();
        }
        for (URI uri : callerFilters) {
            try {
                FilterConfigurationParser parser = new FilterConfigurationParser(callersFilter);
                parser.parseAndRegister(uri);
            } catch (Exception e) {
                throw new UsageException("Cannot parse filter file " + uri + ": " + e);
            }
        }
        callersFilter.removeRedundantNodes();
    }
    AccessAdvisor advisor = new AccessAdvisor();
    advisor.setHeuristicsEnabled(builtinHeuristicFilter);
    if (callersFilter != null) {
        advisor.setCallerFilterTree(callersFilter);
    }
    TraceProcessor p;
    TraceProcessor omittedInputTraceProcessor;
    try {
        omittedInputTraceProcessor = new TraceProcessor(advisor, omittedInputSet.loadJniConfig(ConfigurationSet.FAIL_ON_EXCEPTION), omittedInputSet.loadReflectConfig(ConfigurationSet.FAIL_ON_EXCEPTION), omittedInputSet.loadProxyConfig(ConfigurationSet.FAIL_ON_EXCEPTION), omittedInputSet.loadResourceConfig(ConfigurationSet.FAIL_ON_EXCEPTION), omittedInputSet.loadSerializationConfig(ConfigurationSet.FAIL_ON_EXCEPTION), omittedInputSet.loadPredefinedClassesConfig(null, null, ConfigurationSet.FAIL_ON_EXCEPTION), null);
        List<Path> predefinedClassDestDirs = new ArrayList<>();
        for (URI pathUri : outputSet.getPredefinedClassesConfigPaths()) {
            Path subdir = Files.createDirectories(Paths.get(pathUri).getParent().resolve(ConfigurationFile.PREDEFINED_CLASSES_AGENT_EXTRACTED_SUBDIR));
            subdir = Files.createDirectories(subdir);
            predefinedClassDestDirs.add(subdir);
        }
        Predicate<String> shouldExcludeClassesWithHash = omittedInputTraceProcessor.getPredefinedClassesConfiguration()::containsClassWithHash;
        p = new TraceProcessor(advisor, inputSet.loadJniConfig(ConfigurationSet.FAIL_ON_EXCEPTION), inputSet.loadReflectConfig(ConfigurationSet.FAIL_ON_EXCEPTION), inputSet.loadProxyConfig(ConfigurationSet.FAIL_ON_EXCEPTION), inputSet.loadResourceConfig(ConfigurationSet.FAIL_ON_EXCEPTION), inputSet.loadSerializationConfig(ConfigurationSet.FAIL_ON_EXCEPTION), inputSet.loadPredefinedClassesConfig(predefinedClassDestDirs.toArray(new Path[0]), shouldExcludeClassesWithHash, ConfigurationSet.FAIL_ON_EXCEPTION), omittedInputTraceProcessor);
    } catch (IOException e) {
        throw e;
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
    if (traceInputs.isEmpty() && inputSet.isEmpty()) {
        throw new UsageException("No inputs specified.");
    }
    for (URI uri : traceInputs) {
        try (Reader reader = Files.newBufferedReader(Paths.get(uri))) {
            p.process(reader);
        }
    }
    if (outputSet.isEmpty()) {
        System.err.println("Warning: no outputs specified, validating inputs only.");
    }
    for (URI uri : outputSet.getReflectConfigPaths()) {
        try (JsonWriter writer = new JsonWriter(Paths.get(uri))) {
            p.getReflectionConfiguration().printJson(writer);
        }
    }
    for (URI uri : outputSet.getJniConfigPaths()) {
        try (JsonWriter writer = new JsonWriter(Paths.get(uri))) {
            p.getJniConfiguration().printJson(writer);
        }
    }
    for (URI uri : outputSet.getProxyConfigPaths()) {
        try (JsonWriter writer = new JsonWriter(Paths.get(uri))) {
            p.getProxyConfiguration().printJson(writer);
        }
    }
    for (URI uri : outputSet.getResourceConfigPaths()) {
        try (JsonWriter writer = new JsonWriter(Paths.get(uri))) {
            p.getResourceConfiguration().printJson(writer);
        }
    }
    for (URI uri : outputSet.getSerializationConfigPaths()) {
        try (JsonWriter writer = new JsonWriter(Paths.get(uri))) {
            p.getSerializationConfiguration().printJson(writer);
        }
    }
    for (URI uri : outputSet.getPredefinedClassesConfigPaths()) {
        try (JsonWriter writer = new JsonWriter(Paths.get(uri))) {
            p.getPredefinedClassesConfiguration().printJson(writer);
        }
    }
}
Also used : Path(java.nio.file.Path) RuleNode(com.oracle.svm.configure.filters.RuleNode) TraceProcessor(com.oracle.svm.configure.trace.TraceProcessor) ArrayList(java.util.ArrayList) NoSuchFileException(java.nio.file.NoSuchFileException) FilterConfigurationParser(com.oracle.svm.configure.filters.FilterConfigurationParser) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) URI(java.net.URI) AccessAdvisor(com.oracle.svm.configure.trace.AccessAdvisor) JsonWriter(com.oracle.svm.configure.json.JsonWriter) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) ConfigurationSet(com.oracle.svm.configure.config.ConfigurationSet)

Example 2 with FilterConfigurationParser

use of com.oracle.svm.configure.filters.FilterConfigurationParser in project graal by oracle.

the class ConfigurationTool method generateFilterRules.

private static void generateFilterRules(Iterator<String> argsIter) throws IOException {
    Path outputPath = null;
    boolean reduce = false;
    List<String> args = new ArrayList<>();
    while (argsIter.hasNext()) {
        String arg = argsIter.next();
        if (arg.startsWith("--reduce")) {
            String[] parts = arg.split("=", 2);
            reduce = (parts.length < 2) || Boolean.parseBoolean(parts[1]);
        } else {
            args.add(arg);
        }
    }
    RuleNode rootNode = null;
    for (String arg : args) {
        String[] parts = arg.split("=", 2);
        String current = parts[0];
        String value = (parts.length > 1) ? parts[1] : null;
        switch(current) {
            case "--include-packages-from-modules":
            case "--exclude-packages-from-modules":
            case "--exclude-unexported-packages-from-modules":
                if (!ImageInfo.inImageCode()) {
                    if (rootNode != null) {
                        throw new UsageException(current + " must be specified before other rule-creating arguments");
                    }
                    String[] moduleNames = (value != null) ? value.split(",") : new String[0];
                    RuleNode.Inclusion exportedInclusion = current.startsWith("--include") ? RuleNode.Inclusion.Include : RuleNode.Inclusion.Exclude;
                    RuleNode.Inclusion unexportedInclusion = exportedInclusion;
                    RuleNode.Inclusion rootInclusion = exportedInclusion.invert();
                    if (current.equals("--exclude-unexported-packages-from-modules")) {
                        rootInclusion = RuleNode.Inclusion.Include;
                        exportedInclusion = RuleNode.Inclusion.Include;
                        unexportedInclusion = RuleNode.Inclusion.Exclude;
                    }
                    rootNode = ModuleFilterTools.generateFromModules(moduleNames, rootInclusion, exportedInclusion, unexportedInclusion, reduce);
                } else {
                    throw new UsageException(current + " is currently not supported in the native-image build of this tool.");
                }
                break;
            case "--input-file":
                rootNode = maybeCreateRootNode(rootNode);
                new FilterConfigurationParser(rootNode).parseAndRegister(requirePathUri(current, value));
                break;
            case "--output-file":
                outputPath = requirePath(current, value);
                break;
            case "--include-classes":
                rootNode = addSingleRule(rootNode, current, value, RuleNode.Inclusion.Include);
                break;
            case "--exclude-classes":
                rootNode = addSingleRule(rootNode, current, value, RuleNode.Inclusion.Exclude);
                break;
            default:
                throw new UsageException("Unknown argument: " + current);
        }
    }
    // in case of no inputs
    rootNode = maybeCreateRootNode(rootNode);
    rootNode.removeRedundantNodes();
    if (outputPath != null) {
        try (FileOutputStream os = new FileOutputStream(outputPath.toFile())) {
            rootNode.printJsonTree(os);
        }
    } else {
        rootNode.printJsonTree(System.out);
    }
}
Also used : Path(java.nio.file.Path) RuleNode(com.oracle.svm.configure.filters.RuleNode) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) FilterConfigurationParser(com.oracle.svm.configure.filters.FilterConfigurationParser)

Aggregations

FilterConfigurationParser (com.oracle.svm.configure.filters.FilterConfigurationParser)2 RuleNode (com.oracle.svm.configure.filters.RuleNode)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 ConfigurationSet (com.oracle.svm.configure.config.ConfigurationSet)1 JsonWriter (com.oracle.svm.configure.json.JsonWriter)1 AccessAdvisor (com.oracle.svm.configure.trace.AccessAdvisor)1 TraceProcessor (com.oracle.svm.configure.trace.TraceProcessor)1 BufferedReader (java.io.BufferedReader)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 URI (java.net.URI)1 NoSuchFileException (java.nio.file.NoSuchFileException)1