Search in sources :

Example 6 with OptionDescriptor

use of org.graalvm.compiler.options.OptionDescriptor in project graal by oracle.

the class RuntimeOptionFeature method collectOptionKeys.

private Object collectOptionKeys(Object obj) {
    if (obj instanceof OptionKey) {
        OptionKey<?> optionKey = (OptionKey<?>) obj;
        if (!(optionKey instanceof HostedOptionKey)) {
            OptionDescriptor optionDescriptor = optionKey.getDescriptor();
            if (optionDescriptor == null) {
                throw VMError.shouldNotReachHere("No OptionDescriptor registered for an OptionKey. Often that is the result of an incomplete build. " + "The registration code is generated by an annotation processor at build time, so a clean and full rebuild often helps to solve this problem");
            }
            reachableOptions.put(optionDescriptor, true);
        }
    }
    return obj;
}
Also used : HostedOptionKey(com.oracle.svm.core.option.HostedOptionKey) OptionDescriptor(org.graalvm.compiler.options.OptionDescriptor) OptionKey(org.graalvm.compiler.options.OptionKey) HostedOptionKey(com.oracle.svm.core.option.HostedOptionKey)

Example 7 with OptionDescriptor

use of org.graalvm.compiler.options.OptionDescriptor in project graal by oracle.

the class HostedOptionParser method parse.

public String[] parse(String[] args) {
    List<String> remainingArgs = new ArrayList<>();
    Set<String> errors = new HashSet<>();
    InterruptImageBuilding interrupt = null;
    for (String arg : args) {
        boolean isImageBuildOption = false;
        try {
            isImageBuildOption |= SubstrateOptionsParser.parseHostedOption(SubstrateOptionsParser.HOSTED_OPTION_PREFIX, allHostedOptions, hostedValues, PLUS_MINUS, errors, arg, System.out);
        } catch (InterruptImageBuilding e) {
            interrupt = e;
        }
        try {
            isImageBuildOption |= SubstrateOptionsParser.parseHostedOption(SubstrateOptionsParser.RUNTIME_OPTION_PREFIX, allRuntimeOptions, runtimeValues, PLUS_MINUS, errors, arg, System.out);
        } catch (InterruptImageBuilding e) {
            interrupt = e;
        }
        if (!isImageBuildOption) {
            remainingArgs.add(arg);
        }
    }
    if (interrupt != null) {
        throw interrupt;
    }
    if (!errors.isEmpty()) {
        throw UserError.abort(errors);
    }
    /*
         * We cannot prevent that runtime-only options are accessed during native image generation.
         * However, we set these options to null here, so that at least they do not have a sensible
         * value.
         */
    for (OptionDescriptor descriptor : allRuntimeOptions.values()) {
        if (!allHostedOptions.containsValue(descriptor)) {
            hostedValues.put(descriptor.getOptionKey(), null);
        }
    }
    return remainingArgs.toArray(new String[remainingArgs.size()]);
}
Also used : OptionDescriptor(org.graalvm.compiler.options.OptionDescriptor) ArrayList(java.util.ArrayList) InterruptImageBuilding(com.oracle.svm.core.util.InterruptImageBuilding) HashSet(java.util.HashSet)

Example 8 with OptionDescriptor

use of org.graalvm.compiler.options.OptionDescriptor in project graal by oracle.

the class RuntimeOptionParser method updateRuntimeOptions.

@Platforms(Platform.HOSTED_ONLY.class)
public boolean updateRuntimeOptions(Set<OptionDescriptor> newRuntimeOptions) {
    boolean result = false;
    for (OptionDescriptor descriptor : newRuntimeOptions) {
        String name = descriptor.getName();
        if (!sortedOptions.containsKey(name)) {
            sortedOptions.put(name, descriptor);
            result = true;
        } else {
            assert descriptor == sortedOptions.get(name);
        }
    }
    return result;
}
Also used : OptionDescriptor(org.graalvm.compiler.options.OptionDescriptor) Platforms(org.graalvm.nativeimage.Platforms)

Example 9 with OptionDescriptor

use of org.graalvm.compiler.options.OptionDescriptor in project graal by oracle.

the class SubstrateOptionsParser method parseOption.

static OptionParseResult parseOption(SortedMap<String, OptionDescriptor> options, String option, EconomicMap<OptionKey<?>, Object> valuesMap, String optionPrefix, BooleanOptionFormat booleanOptionFormat) {
    if (option.length() == 0) {
        return OptionParseResult.error("Option name must be specified");
    }
    String optionName;
    Object value = null;
    String valueString = null;
    char first = option.charAt(0);
    int eqIndex = option.indexOf('=');
    if (first == '+' || first == '-') {
        if (eqIndex != -1) {
            return OptionParseResult.error("Cannot mix +/- with <name>=<value> format: '" + optionPrefix + option + "'");
        }
        optionName = option.substring(1, eqIndex == -1 ? option.length() : eqIndex);
        if (booleanOptionFormat == BooleanOptionFormat.NAME_VALUE) {
            return OptionParseResult.error("Option '" + optionName + "' must use <name>=<value> format, not +/- prefix");
        }
        value = (first == '+');
    } else {
        if (eqIndex == -1) {
            optionName = option;
            valueString = null;
        } else {
            optionName = option.substring(0, eqIndex);
            valueString = option.substring(eqIndex + 1);
        }
    }
    OptionDescriptor desc = options.get(optionName);
    if (desc == null && value != null) {
        if (eqIndex != -1) {
            optionName = option.substring(1, eqIndex);
            desc = options.get(optionName);
        }
    }
    if (desc == null) {
        List<OptionDescriptor> matches = new ArrayList<>();
        OptionsParser.collectFuzzyMatches(options.values(), optionName, matches);
        StringBuilder msg = new StringBuilder("Could not find option '").append(optionName).append('\'');
        if (!matches.isEmpty()) {
            msg.append(". Did you mean one of these:");
            for (OptionDescriptor match : matches) {
                msg.append(' ').append(match.getName());
            }
        }
        msg.append(". Use " + optionPrefix + SubstrateOptions.PrintFlags.getName() + "= to list all available options.");
        return OptionParseResult.error(msg.toString());
    }
    Class<?> optionType = desc.getOptionValueType();
    if (value == null) {
        if (optionType == Boolean.class && booleanOptionFormat == BooleanOptionFormat.PLUS_MINUS) {
            return OptionParseResult.error("Boolean option '" + optionName + "' must have +/- prefix");
        }
        if (valueString == null) {
            return OptionParseResult.error("Missing value for option '" + optionName + "'");
        }
        try {
            if (optionType == Integer.class) {
                long longValue = parseLong(valueString);
                if ((int) longValue != longValue) {
                    return OptionParseResult.error("Wrong value for option '" + optionName + "': '" + valueString + "' is not a valid number");
                }
                value = (int) longValue;
            } else if (optionType == Long.class) {
                value = parseLong(valueString);
            } else if (optionType == String.class) {
                value = valueString;
            } else if (optionType == Double.class) {
                value = Double.parseDouble(valueString);
            } else if (optionType == Boolean.class) {
                if (valueString.equals("true")) {
                    value = true;
                } else if (valueString.equals("false")) {
                    value = false;
                } else {
                    return OptionParseResult.error("Boolean option '" + optionName + "' must have value 'true' or 'false'");
                }
            } else if (optionType == CompilationWrapper.ExceptionAction.class) {
                value = CompilationWrapper.ExceptionAction.valueOf(valueString);
            } else {
                throw VMError.shouldNotReachHere("Unsupported option value class: " + optionType.getSimpleName());
            }
        } catch (NumberFormatException ex) {
            return OptionParseResult.error("Invalid value for option '" + optionName + "': '" + valueString + "' is not a valid number");
        }
    } else {
        if (optionType != Boolean.class) {
            return OptionParseResult.error("Non-boolean option '" + optionName + "' can not use +/- prefix. Use '" + optionName + "=<value>' format");
        }
    }
    desc.getOptionKey().update(valuesMap, value);
    if (SubstrateOptions.PrintFlags.getName().equals(optionName)) {
        String optionValue = (String) value;
        EnumSet<OptionType> selectedOptionTypes;
        if (optionValue.isEmpty()) {
            selectedOptionTypes = EnumSet.allOf(OptionType.class);
        } else {
            selectedOptionTypes = EnumSet.noneOf(OptionType.class);
            String enumString = null;
            try {
                String[] enumStrings = optionValue.split(",");
                for (int i = 0; i < enumStrings.length; i++) {
                    enumString = enumStrings[i];
                    selectedOptionTypes.add(OptionType.valueOf(enumString));
                }
            } catch (IllegalArgumentException e) {
                String possibleValues = Arrays.stream(OptionType.values()).map(OptionType::name).collect(Collectors.joining(", ", "", ""));
                return OptionParseResult.error("Invalid value for option '" + optionName + ". " + enumString + "' is not one of: " + possibleValues);
            }
        }
        return OptionParseResult.printFlags(selectedOptionTypes);
    }
    return OptionParseResult.correct();
}
Also used : OptionDescriptor(org.graalvm.compiler.options.OptionDescriptor) ArrayList(java.util.ArrayList) OptionType(org.graalvm.compiler.options.OptionType)

Example 10 with OptionDescriptor

use of org.graalvm.compiler.options.OptionDescriptor in project graal by oracle.

the class SubstrateOptionsParser method printFlags.

static void printFlags(Predicate<OptionDescriptor> filter, SortedMap<String, OptionDescriptor> sortedOptions, String prefix, PrintStream out) {
    for (Entry<String, OptionDescriptor> entry : sortedOptions.entrySet()) {
        OptionDescriptor descriptor = entry.getValue();
        if (!filter.test(descriptor)) {
            continue;
        }
        String helpMsg = descriptor.getHelp();
        int helpLen = helpMsg.length();
        if (helpLen > 0 && helpMsg.charAt(helpLen - 1) != '.') {
            helpMsg += '.';
        }
        if (descriptor.getOptionValueType() == Boolean.class) {
            Boolean val = (Boolean) descriptor.getOptionKey().getDefaultValue();
            if (helpLen != 0) {
                helpMsg += ' ';
            }
            if (val == null || !((boolean) val)) {
                helpMsg += "Default: - (disabled).";
            } else {
                helpMsg += "Default: + (enabled).";
            }
            printOption(out, prefix + "\u00b1" + entry.getKey(), helpMsg);
        } else {
            Object def = descriptor.getOptionKey().getDefaultValue();
            if (def instanceof String) {
                def = '"' + String.valueOf(def) + '"';
            }
            printOption(out, prefix + entry.getKey() + "=" + def, helpMsg);
        }
    }
}
Also used : OptionDescriptor(org.graalvm.compiler.options.OptionDescriptor)

Aggregations

OptionDescriptor (org.graalvm.compiler.options.OptionDescriptor)10 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)2 HostedOptionKey (com.oracle.svm.core.option.HostedOptionKey)1 InterruptImageBuilding (com.oracle.svm.core.util.InterruptImageBuilding)1 OptionDescriptors (org.graalvm.compiler.options.OptionDescriptors)1 OptionKey (org.graalvm.compiler.options.OptionKey)1 OptionType (org.graalvm.compiler.options.OptionType)1 Platforms (org.graalvm.nativeimage.Platforms)1