com.google.devtools.build.lib.query2.proto.proto2api.Build Java Examples

The following examples show how to use com.google.devtools.build.lib.query2.proto.proto2api.Build. 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: BuildElementValidation.java    From intellij with Apache License 2.0 6 votes vote down vote up
/** Returns false iff we know with certainty that the element cannot resolve to the given type. */
public static boolean possiblyValidType(PsiElement element, Build.Attribute.Discriminator type) {
  if (!HANDLED_TYPES.contains(type)) {
    return true;
  }
  if (element instanceof ListLiteral || element instanceof GlobExpression) {
    return LIST_TYPES.contains(type);
  }
  if (element instanceof StringLiteral) {
    return STRING_TYPES.contains(type);
  }
  if (element instanceof DictionaryLiteral) {
    return DICT_TYPES.contains(type);
  }
  if (element instanceof IntegerLiteral) {
    return INTEGER_TYPES.contains(type);
  }
  return true;
}
 
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: ProtoOutputFormatterCallback.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Override
public void close(boolean failFast) throws IOException {
  if (!failFast && printStream != null) {
    if (options.protoIncludeConfigurations) {
      writeData(protoResult.build());
    } else {
      // Documentation promises that setting this flag to false means we convert directly
      // to the build.proto format. This is hard to test in integration testing due to the way
      // proto output is turned readable (codex). So change the following code with caution.
      QueryResult.Builder queryResult = Build.QueryResult.newBuilder();
      protoResult.getResultsList().forEach(ct -> queryResult.addTarget(ct.getTarget()));
      writeData(queryResult.build());
    }
    printStream.flush();
  }
}
 
Example #4
Source File: RuleDefinition.java    From intellij with Apache License 2.0 6 votes vote down vote up
public static RuleDefinition fromProto(Build.RuleDefinition proto) {
  Map<String, AttributeDefinition> attributes = new LinkedHashMap<>();
  for (Build.AttributeDefinition attr : proto.getAttributeList()) {
    attributes.put(attr.getName(), AttributeDefinition.fromProto(attr));
  }
  AttributeDefinition nameDef = attributes.get("name");
  if (nameDef != null && !nameDef.isMandatory()) {
    // blaze isn't correctly marking the 'name' attribute as mandatory
    attributes.put(
        "name",
        AttributeDefinition.fromProto(nameDef.toProto().toBuilder().setMandatory(true).build()));
  }
  attributes =
      attributes.entrySet().stream()
          .sorted(Entry.comparingByValue())
          .collect(ImmutableMap.toImmutableMap(Entry::getKey, Entry::getValue));
  return new RuleDefinition(
      proto.getName(),
      ImmutableMap.copyOf(attributes),
      proto.hasDocumentation() ? proto.getDocumentation() : null);
}
 
Example #5
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 #6
Source File: QueryIntegrationTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
private static List<String> getTargetNames(QueryResult result) {
  List<String> results = new ArrayList<>();
  for (Build.Target target : result.getTargetList()) {
    results.add(target.getRule().getName());
  }
  return results;
}
 
Example #7
Source File: Bazel.java    From bazel-tools with Apache License 2.0 5 votes vote down vote up
public static ImmutableGraph<String> dependencyGraph(final Path workspace, final Rule rule)
    throws IOException {
  final Process process =
      new ProcessBuilder("bazel", "query", "--output=proto", "deps(" + rule + ") + " + rule)
          .redirectError(ProcessBuilder.Redirect.INHERIT)
          .redirectOutput(ProcessBuilder.Redirect.PIPE)
          .directory(workspace.toFile())
          .start();

  try {
    final Build.QueryResult queryResult = Build.QueryResult.parseFrom(process.getInputStream());

    final MutableGraph<String> graph =
        GraphBuilder.directed().expectedNodeCount(queryResult.getTargetCount()).build();

    for (final Build.Target protoTarget : queryResult.getTargetList()) {
      final String name = protoTarget.getRule().getName();
      graph.addNode(name);

      for (final String dependency : protoTarget.getRule().getRuleInputList()) {
        graph.putEdge(name, dependency);
      }
    }

    return ImmutableGraph.copyOf(graph);
  } finally {
    process.destroyForcibly();
  }
}
 
Example #8
Source File: LazyStringListCodecTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Test
public void testCodec() throws Exception {
  new SerializationTester(
          // Tests empty list as well, since license has two repeated string fields.
          Build.License.newBuilder().addLicenseType("first").addLicenseType("second").build())
      .runTests();
}
 
Example #9
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 #10
Source File: AttributeFormatter.java    From bazel with Apache License 2.0 5 votes vote down vote up
private static Build.FilesetEntry.SymlinkBehavior symlinkBehaviorToPb(
    FilesetEntry.SymlinkBehavior symlinkBehavior) {
  switch (symlinkBehavior) {
    case COPY:
      return Build.FilesetEntry.SymlinkBehavior.COPY;
    case DEREFERENCE:
      return Build.FilesetEntry.SymlinkBehavior.DEREFERENCE;
    default:
      throw new AssertionError("Unhandled FilesetEntry.SymlinkBehavior");
  }
}
 
Example #11
Source File: AttributeFormatter.java    From bazel with Apache License 2.0 5 votes vote down vote up
private static void writeSelectorListToBuilder(
    Build.Attribute.Builder attrPb,
    Type<?> type,
    SelectorList<?> selectorList) {
  Build.Attribute.SelectorList.Builder selectorListBuilder =
      Build.Attribute.SelectorList.newBuilder();
  selectorListBuilder.setType(ProtoUtils.getDiscriminatorFromType(type));
  for (Selector<?> selector : selectorList.getSelectors()) {
    Build.Attribute.Selector.Builder selectorBuilder = Build.Attribute.Selector.newBuilder()
        .setNoMatchError(selector.getNoMatchError())
        .setHasDefaultValue(selector.hasDefault());

    // Note that the order of entries returned by selector.getEntries is stable. The map's
    // entries' order is preserved from the fact that Starlark dictionary entry order is stable
    // (it's determined by insertion order).
    for (Map.Entry<Label, ?> entry : selector.getEntries().entrySet()) {
      Label condition = entry.getKey();
      SelectorEntry.Builder selectorEntryBuilder =
          SelectorEntry.newBuilder()
              .setLabel(condition.toString())
              .setIsDefaultValue(!selector.isValueSet(condition));

      Object conditionValue = entry.getValue();
      if (conditionValue != null) {
        writeAttributeValueToBuilder(
            new SelectorEntryBuilderAdapter(selectorEntryBuilder),
            type,
            conditionValue);
      }
      selectorBuilder.addEntries(selectorEntryBuilder);
    }
    selectorListBuilder.addElements(selectorBuilder);
  }
  attrPb.setSelectorList(selectorListBuilder);
}
 
Example #12
Source File: AttributeFormatter.java    From bazel with Apache License 2.0 5 votes vote down vote up
private static void maybeSetNoDep(Type<?> type, Build.Attribute.Builder attrPb) {
  if (depTypes.contains(type)) {
    attrPb.setNodep(false);
  } else if (noDepTypes.contains(type)) {
    attrPb.setNodep(true);
  }
}
 
Example #13
Source File: BuiltInFunctionAttributeCompletionContributorTest.java    From intellij with Apache License 2.0 5 votes vote down vote up
private void setRuleAndAttributes(String ruleName, String... attributes) {
  ImmutableMap.Builder<String, AttributeDefinition> map = ImmutableMap.builder();
  for (String attr : attributes) {
    map.put(
        attr,
        new AttributeDefinition(attr, Build.Attribute.Discriminator.UNKNOWN, false, null, null));
  }
  RuleDefinition rule = new RuleDefinition(ruleName, map.build(), null);
  specProvider.setRules(ImmutableMap.of(ruleName, rule));
}
 
Example #14
Source File: AttributeDefinition.java    From intellij with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
public AttributeDefinition(
    String name,
    Build.Attribute.Discriminator type,
    boolean mandatory,
    @Nullable String documentation,
    @Nullable ImmutableList<String> allowedRuleClasses) {
  this.name = name;
  this.type = type;
  this.mandatory = mandatory;
  this.documentation = documentation;
  this.allowedRuleClasses = allowedRuleClasses;
}
 
Example #15
Source File: AttributeDefinition.java    From intellij with Apache License 2.0 5 votes vote down vote up
static AttributeDefinition fromProto(Build.AttributeDefinition proto) {
  return new AttributeDefinition(
      proto.getName(),
      proto.getType(),
      proto.getMandatory(),
      proto.hasDocumentation() ? proto.getDocumentation() : null,
      proto.hasAllowedRuleClasses()
          ? ImmutableList.copyOf(proto.getAllowedRuleClasses().getAllowedRuleClassList())
          : null);
}
 
Example #16
Source File: AttributeDefinition.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Override
public Build.AttributeDefinition toProto() {
  Build.AttributeDefinition.Builder builder =
      Build.AttributeDefinition.newBuilder().setName(name).setType(type).setMandatory(mandatory);
  ProtoWrapper.setIfNotNull(builder::setDocumentation, documentation);
  if (allowedRuleClasses != null) {
    builder.setAllowedRuleClasses(
        Build.AllowedRuleClassInfo.newBuilder()
            .addAllAllowedRuleClass(allowedRuleClasses)
            .setPolicy(
                Build.AllowedRuleClassInfo.AllowedRuleClasses.ANY)); // unnecessary, but mandatory
  }
  return builder.build();
}
 
Example #17
Source File: RuleDefinition.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Override
public Build.RuleDefinition toProto() {
  Build.RuleDefinition.Builder builder =
      Build.RuleDefinition.newBuilder()
          .setName(name)
          .addAllAttribute(ProtoWrapper.mapToProtos(attributes.values()));
  ProtoWrapper.setIfNotNull(builder::setDocumentation, documentation);
  return builder.build();
}
 
Example #18
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 #19
Source File: AttributeFormatter.java    From bazel with Apache License 2.0 4 votes vote down vote up
private AttributeBuilderAdapter(
    Build.Attribute.Builder attributeBuilder,
    boolean encodeBooleanAndTriStateAsIntegerAndString) {
  this.attributeBuilder = Preconditions.checkNotNull(attributeBuilder);
  this.encodeBooleanAndTriStateAsIntegerAndString = encodeBooleanAndTriStateAsIntegerAndString;
}
 
Example #20
Source File: AttributeFormatter.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public void addFilesetListValue(Build.FilesetEntry.Builder builder) {
  attributeBuilder.addFilesetListValue(builder);
}
 
Example #21
Source File: AttributeFormatter.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public void setLicense(Build.License.Builder builder) {
  attributeBuilder.setLicense(builder);
}
 
Example #22
Source File: AttributeFormatter.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public void addFilesetListValue(Build.FilesetEntry.Builder builder) {
  selectorEntryBuilder.addFilesetListValue(builder);
}
 
Example #23
Source File: AttributeFormatter.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public void setLicense(Build.License.Builder builder) {
  selectorEntryBuilder.setLicense(builder);
}
 
Example #24
Source File: RuleFormatter.java    From bazel with Apache License 2.0 4 votes vote down vote up
public static Build.Rule.Builder serializeRule(Rule rule) {
  Build.Rule.Builder builder = Build.Rule.newBuilder();
  builder.setName(rule.getLabel().getName());
  builder.setRuleClass(rule.getRuleClass());
  builder.setPublicByDefault(rule.getRuleClassObject().isPublicByDefault());

  RawAttributeMapper rawAttributeMapper = RawAttributeMapper.of(rule);
  boolean isStarlark = rule.getRuleClassObject().isStarlark();

  if (isStarlark) {
    builder.setSkylarkEnvironmentHashCode(
        // hexify
        BaseEncoding.base16()
            .lowerCase()
            .encode(
                Preconditions.checkNotNull(
                    rule.getRuleClassObject().getRuleDefinitionEnvironmentDigest(), rule)));
  }
  for (Attribute attr : rule.getAttributes()) {
    Object rawAttributeValue = rawAttributeMapper.getRawAttributeValue(rule, attr);
    boolean isExplicit = rule.isAttributeValueExplicitlySpecified(attr);

    if (!isStarlark && !isExplicit) {
      // If the rule class is native (i.e. not Starlark-defined), then we can skip serialization
      // of implicit attribute values. The native rule class can provide the same default value
      // for the attribute after deserialization.
      continue;
    }

    Object valueToSerialize;
    if (isExplicit) {
      valueToSerialize = rawAttributeValue;
    } else if (rawAttributeValue instanceof ComputedDefault) {
      // If the rule class is Starlark-defined (i.e. rule.getRuleClassObject().isStarlark() is
      // true), and the attribute has a ComputedDefault value, then we must serialize what it
      // evaluates to. The Starlark-defined ComputedDefault function won't be available after
      // deserialization due to Starlark's non-serializability.
      valueToSerialize = evaluateStarlarkComputedDefault(rawAttributeMapper, attr);
      if (valueToSerialize == null) {
        continue;
      }
    } else {
      // If the rule class is Starlark-defined and the attribute value is implicit, then we
      // must serialize it. The Starlark-defined rule class won't be available after
      // deserialization due to Starlark's non-serializability.
      valueToSerialize = rawAttributeValue;
    }

    builder.addAttribute(
        AttributeFormatter.getAttributeProto(
            attr,
            valueToSerialize,
            isExplicit,
            /*encodeBooleanAndTriStateAsIntegerAndString=*/ false));
  }
  return builder;
}
 
Example #25
Source File: ProtoOutputFormatter.java    From bazel with Apache License 2.0 4 votes vote down vote up
/** Converts a logical {@link Target} object into a {@link Build.Target} protobuffer. */
public Build.Target toTargetProtoBuffer(Target target) throws InterruptedException {
  return toTargetProtoBuffer(target, /*extraDataForAttrHash=*/ "");
}
 
Example #26
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));
  }
}
 
Example #27
Source File: BuildLanguageSpec.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Override
public BuildLanguage toProto() {
  return Build.BuildLanguage.newBuilder()
      .addAllRule(ProtoWrapper.mapToProtos(rules.values()))
      .build();
}
 
Example #28
Source File: AttributeDefinition.java    From intellij with Apache License 2.0 4 votes vote down vote up
public Build.Attribute.Discriminator getType() {
  return type;
}
 
Example #29
Source File: AttributeFormatter.java    From bazel with Apache License 2.0 votes vote down vote up
void setLicense(Build.License.Builder builder); 
Example #30
Source File: AttributeFormatter.java    From bazel with Apache License 2.0 votes vote down vote up
void addFilesetListValue(Build.FilesetEntry.Builder builder);