Search in sources :

Example 1 with OptionValueDescription

use of com.google.devtools.common.options.OptionsParser.OptionValueDescription in project bazel by bazelbuild.

the class InvocationPolicyEnforcer method applyUseDefaultOperation.

private static void applyUseDefaultOperation(OptionsParser parser, String policyType, String flagName) throws OptionsParsingException {
    Map<String, OptionValueDescription> clearedValues = parser.clearValue(flagName);
    for (Entry<String, OptionValueDescription> clearedValue : clearedValues.entrySet()) {
        OptionValueDescription clearedValueDescription = clearedValue.getValue();
        String clearedFlagName = clearedValue.getKey();
        String originalValue = clearedValueDescription.getValue().toString();
        String source = clearedValueDescription.getSource();
        Object clearedFlagDefaultValue = parser.getOptionDescription(clearedFlagName).getDefaultValue();
        log.info(String.format("Using default value '%s' for flag '%s' as " + "specified by %s invocation policy, overriding original value '%s' from '%s'", clearedFlagDefaultValue, clearedFlagName, policyType, originalValue, source));
    }
}
Also used : OptionValueDescription(com.google.devtools.common.options.OptionsParser.OptionValueDescription)

Example 2 with OptionValueDescription

use of com.google.devtools.common.options.OptionsParser.OptionValueDescription in project bazel by bazelbuild.

the class OptionsParserImpl method clearValue.

private void clearValue(Field field, Option option, Map<String, OptionValueDescription> clearedValues) throws OptionsParsingException {
    OptionValueDescription removed = parsedValues.remove(field);
    if (removed != null) {
        clearedValues.put(option.name(), removed);
    }
    canonicalizeValues.removeAll(field);
    // originally parsed.
    for (String[] args : new String[][] { option.implicitRequirements(), option.expansion() }) {
        Iterator<String> argsIterator = Iterators.forArray(args);
        while (argsIterator.hasNext()) {
            String arg = argsIterator.next();
            ParseOptionResult parseOptionResult = parseOption(arg, argsIterator);
            clearValue(parseOptionResult.field, parseOptionResult.option, clearedValues);
        }
    }
}
Also used : UnparsedOptionValueDescription(com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription) OptionValueDescription(com.google.devtools.common.options.OptionsParser.OptionValueDescription)

Example 3 with OptionValueDescription

use of com.google.devtools.common.options.OptionsParser.OptionValueDescription in project bazel by bazelbuild.

the class OptionsParserImpl method asListOfEffectiveOptions.

/**
   * Implements {@link OptionsParser#asListOfEffectiveOptions()}.
   */
List<OptionValueDescription> asListOfEffectiveOptions() {
    List<OptionValueDescription> result = Lists.newArrayList();
    for (Map.Entry<String, Field> mapEntry : optionsData.getAllNamedFields()) {
        String fieldName = mapEntry.getKey();
        Field field = mapEntry.getValue();
        OptionValueDescription entry = parsedValues.get(field);
        if (entry == null) {
            Object value = optionsData.getDefaultValue(field);
            result.add(new OptionValueDescription(fieldName, value, OptionPriority.DEFAULT, null, null, null, false));
        } else {
            result.add(entry);
        }
    }
    return result;
}
Also used : UnparsedOptionValueDescription(com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription) OptionValueDescription(com.google.devtools.common.options.OptionsParser.OptionValueDescription) Field(java.lang.reflect.Field) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 4 with OptionValueDescription

use of com.google.devtools.common.options.OptionsParser.OptionValueDescription in project bazel by bazelbuild.

the class OptionsParserImpl method setValue.

// Warnings should not end with a '.' because the internal reporter adds one automatically.
private void setValue(Field field, String name, Object value, OptionPriority priority, String source, String implicitDependant, String expandedFrom) {
    OptionValueDescription entry = parsedValues.get(field);
    if (entry != null) {
        // Override existing option if the new value has higher or equal priority.
        if (priority.compareTo(entry.getPriority()) >= 0) {
            // Output warnings:
            if ((implicitDependant != null) && (entry.getImplicitDependant() != null)) {
                if (!implicitDependant.equals(entry.getImplicitDependant())) {
                    warnings.add("Option '" + name + "' is implicitly defined by both option '" + entry.getImplicitDependant() + "' and option '" + implicitDependant + "'");
                }
            } else if ((implicitDependant != null) && priority.equals(entry.getPriority())) {
                warnings.add("Option '" + name + "' is implicitly defined by option '" + implicitDependant + "'; the implicitly set value overrides the previous one");
            } else if (entry.getImplicitDependant() != null) {
                warnings.add("A new value for option '" + name + "' overrides a previous implicit setting of that option by option '" + entry.getImplicitDependant() + "'");
            } else if ((priority == entry.getPriority()) && ((entry.getExpansionParent() == null) && (expandedFrom != null))) {
                // Create a warning if an expansion option overrides an explicit option:
                warnings.add("The option '" + expandedFrom + "' was expanded and now overrides a " + "previous explicitly specified option '" + name + "'");
            }
            // Record the new value:
            parsedValues.put(field, new OptionValueDescription(name, value, priority, source, implicitDependant, expandedFrom, false));
        }
    } else {
        parsedValues.put(field, new OptionValueDescription(name, value, priority, source, implicitDependant, expandedFrom, false));
        maybeAddDeprecationWarning(field);
    }
}
Also used : UnparsedOptionValueDescription(com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription) OptionValueDescription(com.google.devtools.common.options.OptionsParser.OptionValueDescription)

Example 5 with OptionValueDescription

use of com.google.devtools.common.options.OptionsParser.OptionValueDescription in project bazel by bazelbuild.

the class OptionsParserTest method asListOfEffectiveOptions.

@Test
public void asListOfEffectiveOptions() throws Exception {
    OptionsParser parser = OptionsParser.newOptionsParser(IntrospectionExample.class);
    parser.parse(OptionPriority.COMMAND_LINE, "source", Arrays.asList("--alpha=one", "--gamma=two"));
    List<OptionValueDescription> result = parser.asListOfEffectiveOptions();
    assertNotNull(result);
    assertEquals(5, result.size());
    HashMap<String, OptionValueDescription> map = new HashMap<String, OptionValueDescription>();
    for (OptionValueDescription description : result) {
        map.put(description.getName(), description);
    }
    assertOptionValue("alpha", "one", OptionPriority.COMMAND_LINE, "source", map.get("alpha"));
    assertOptionValue("beta", "beta", OptionPriority.DEFAULT, null, map.get("beta"));
    assertOptionValue("gamma", "two", OptionPriority.COMMAND_LINE, "source", map.get("gamma"));
    assertOptionValue("delta", "delta", OptionPriority.DEFAULT, null, map.get("delta"));
    assertOptionValue("echo", "echo", OptionPriority.DEFAULT, null, map.get("echo"));
}
Also used : UnparsedOptionValueDescription(com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription) OptionValueDescription(com.google.devtools.common.options.OptionsParser.OptionValueDescription) HashMap(java.util.HashMap) OptionsParser.newOptionsParser(com.google.devtools.common.options.OptionsParser.newOptionsParser) Test(org.junit.Test)

Aggregations

OptionValueDescription (com.google.devtools.common.options.OptionsParser.OptionValueDescription)9 UnparsedOptionValueDescription (com.google.devtools.common.options.OptionsParser.UnparsedOptionValueDescription)7 OptionsParser.newOptionsParser (com.google.devtools.common.options.OptionsParser.newOptionsParser)2 Field (java.lang.reflect.Field)2 Test (org.junit.Test)2 AllowValues (com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.AllowValues)1 DisallowValues (com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.DisallowValues)1 FlagPolicy (com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.FlagPolicy)1 OptionDescription (com.google.devtools.common.options.OptionsParser.OptionDescription)1 OptionsParsingException (com.google.devtools.common.options.OptionsParsingException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1