Java Code Examples for com.google.devtools.build.lib.query2.proto.proto2api.Build#Attribute

The following examples show how to use com.google.devtools.build.lib.query2.proto.proto2api.Build#Attribute . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AttributeFormatter.java    From bazel with Apache License 2.0 6 votes vote down vote up
private static Build.Attribute getAttributeProto(
    String name,
    Type<?> type,
    @Nullable Object value,
    boolean explicitlySpecified,
    boolean encodeBooleanAndTriStateAsIntegerAndString) {
  Build.Attribute.Builder attrPb = Build.Attribute.newBuilder();
  attrPb.setName(name);
  attrPb.setExplicitlySpecified(explicitlySpecified);
  maybeSetNoDep(type, attrPb);

  if (value instanceof SelectorList<?>) {
    attrPb.setType(Discriminator.SELECTOR_LIST);
    writeSelectorListToBuilder(attrPb, type, (SelectorList<?>) value);
  } else {
    attrPb.setType(ProtoUtils.getDiscriminatorFromType(type));
    if (value != null) {
      AttributeBuilderAdapter adapter =
          new AttributeBuilderAdapter(attrPb, encodeBooleanAndTriStateAsIntegerAndString);
      writeAttributeValueToBuilder(adapter, type, value);
    }
  }

  return attrPb.build();
}
 
Example 2
Source File: SyntheticAttributeHashCalculatorTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test
public void testComputeSerializedAttributesUsedOverAvailable() throws Exception {
  Rule rule =
      getRule(scratch.file("pkg/BUILD", "genrule(name='x', cmd='touch $@', outs=['y'])"), "x");

  String hashBefore =
      SyntheticAttributeHashCalculator.compute(
          rule, /*serializedAttributes=*/ ImmutableMap.of(), /*extraDataForAttrHash=*/ "");

  ImmutableMap<Attribute, Build.Attribute> serializedAttributes =
      ImmutableMap.of(
          rule.getRuleClassObject().getAttributeByName("cmd"),
          Build.Attribute.newBuilder()
              .setName("dummy")
              .setType(Discriminator.STRING)
              .setStringValue("hi")
              .build());

  String hashAfter =
      SyntheticAttributeHashCalculator.compute(
          rule, serializedAttributes, /*extraDataForAttrHash*/ "");

  assertThat(hashBefore).isNotEqualTo(hashAfter);
}
 
Example 3
Source File: AttributeFormatter.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Convert attribute value to proto representation.
 *
 * <p>If {@param value} is null, only the {@code name}, {@code explicitlySpecified}, {@code
 * nodep} (if applicable), and {@code type} fields will be included in the proto message.
 *
 * <p>If {@param encodeBooleanAndTriStateAsIntegerAndString} is true then boolean and tristate
 * values are also encoded as integers and strings.
 */
public static Build.Attribute getAttributeProto(
    Attribute attr,
    @Nullable Object value,
    boolean explicitlySpecified,
    boolean encodeBooleanAndTriStateAsIntegerAndString) {
  return getAttributeProto(
      attr.getName(),
      attr.getType(),
      value,
      explicitlySpecified,
      encodeBooleanAndTriStateAsIntegerAndString);
}
 
Example 4
Source File: ProtoOutputFormatterCallback.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
protected void addAttributes(
    Build.Rule.Builder rulePb, Rule rule, Object extraDataForAttrHash) {
  // We know <code>currentTarget</code> will be one of these two types of configured targets
  // because this method is only triggered in ProtoOutputFormatter.toTargetProtoBuffer when
  // the target in currentTarget is an instanceof Rule.
  ImmutableMap<Label, ConfigMatchingProvider> configConditions;
  if (currentTarget instanceof AliasConfiguredTarget) {
    configConditions = ((AliasConfiguredTarget) currentTarget).getConfigConditions();
  } else if (currentTarget instanceof RuleConfiguredTarget) {
    configConditions = ((RuleConfiguredTarget) currentTarget).getConfigConditions();
  } else {
    // Other subclasses of ConfiguredTarget don't have attribute information.
    return;
  }
  ConfiguredAttributeMapper attributeMapper =
      ConfiguredAttributeMapper.of(rule, configConditions);
  Map<Attribute, Build.Attribute> serializedAttributes = Maps.newHashMap();
  for (Attribute attr : rule.getAttributes()) {
    if (!shouldIncludeAttribute(rule, attr)) {
      continue;
    }
    Object attributeValue = attributeMapper.get(attr.getName(), attr.getType());
    Build.Attribute serializedAttribute =
        AttributeFormatter.getAttributeProto(
            attr,
            attributeValue,
            rule.isAttributeValueExplicitlySpecified(attr),
            /*encodeBooleanAndTriStateAsIntegerAndString=*/ true);
    serializedAttributes.put(attr, serializedAttribute);
  }
  rulePb.addAllAttribute(serializedAttributes.values());
}
 
Example 5
Source File: ProtoOutputFormatter.java    From bazel with Apache License 2.0 4 votes vote down vote up
protected void addAttributes(Build.Rule.Builder rulePb, Rule rule, Object extraDataForAttrHash)
    throws InterruptedException {
  Map<Attribute, Build.Attribute> serializedAttributes = Maps.newHashMap();
  AggregatingAttributeMapper attributeMapper = AggregatingAttributeMapper.of(rule);
  for (Attribute attr : rule.getAttributes()) {
    if (!shouldIncludeAttribute(rule, attr)) {
      continue;
    }
    Object attributeValue;
    if (flattenSelects || !attributeMapper.isConfigurable(attr.getName())) {
      attributeValue =
          flattenAttributeValues(
              attr.getType(), PossibleAttributeValues.forRuleAndAttribute(rule, attr));
    } else {
      attributeValue = attributeMapper.getSelectorList(attr.getName(), attr.getType());
    }
    Build.Attribute serializedAttribute =
        AttributeFormatter.getAttributeProto(
            attr,
            attributeValue,
            rule.isAttributeValueExplicitlySpecified(attr),
            /*encodeBooleanAndTriStateAsIntegerAndString=*/ true);
    serializedAttributes.put(attr, serializedAttribute);
  }
  rulePb.addAllAttribute(
      serializedAttributes
          .values()
          .stream()
          .distinct()
          .sorted(ATTRIBUTE_NAME)
          .collect(Collectors.toList()));

  if (includeSyntheticAttributeHash) {
    rulePb.addAttribute(
        Build.Attribute.newBuilder()
            .setName("$internal_attr_hash")
            .setStringValue(
                SyntheticAttributeHashCalculator.compute(
                    rule, serializedAttributes, extraDataForAttrHash))
            .setType(Discriminator.STRING));
  }
}