Search in sources :

Example 1 with ProtoSourcesProvider

use of com.google.devtools.build.lib.rules.proto.ProtoSourcesProvider in project bazel by bazelbuild.

the class ProtobufSupport method getInputsToOutputsMap.

private ImmutableSetMultimap<ImmutableSet<Artifact>, Artifact> getInputsToOutputsMap() {
    Iterable<ObjcProtoProvider> objcProtoProviders = getObjcProtoProviders();
    Iterable<ProtoSourcesProvider> protoProviders = getProtoSourcesProviders();
    ImmutableList.Builder<NestedSet<Artifact>> protoSets = new ImmutableList.Builder<NestedSet<Artifact>>();
    // all the transitive groups of proto.
    for (ObjcProtoProvider objcProtoProvider : objcProtoProviders) {
        protoSets.addAll(objcProtoProvider.getProtoGroups());
    }
    for (ProtoSourcesProvider protoProvider : protoProviders) {
        protoSets.add(protoProvider.getTransitiveProtoSources());
    }
    HashMap<Artifact, Set<Artifact>> protoToGroupMap = new HashMap<>();
    // group will be considered the smallest input group with which the proto can be generated.
    for (NestedSet<Artifact> nestedProtoSet : protoSets.build()) {
        ImmutableSet<Artifact> protoSet = ImmutableSet.copyOf(nestedProtoSet.toSet());
        for (Artifact proto : protoSet) {
            // generated with the runtime library.
            if (attributes.isProtoWellKnown(proto)) {
                continue;
            }
            if (!protoToGroupMap.containsKey(proto)) {
                protoToGroupMap.put(proto, protoSet);
            } else {
                protoToGroupMap.put(proto, Sets.intersection(protoSet, protoToGroupMap.get(proto)));
            }
        }
    }
    // Now that we have the smallest proto inputs groups for each proto to be generated, we reverse
    // that map into a multimap to take advantage of the fact that multiple protos can be generated
    // with the same inputs, to avoid having multiple generation actions with the same inputs and
    // different ouputs. This only applies for the generation actions, as the compilation actions
    // compile one generated file at a time.
    // It's OK to use ImmutableSet<Artifact> as the key, since Artifact caches it's hashCode, and
    // ImmutableSet calculates it's hashCode in O(n).
    ImmutableSetMultimap.Builder<ImmutableSet<Artifact>, Artifact> inputsToOutputsMapBuilder = ImmutableSetMultimap.builder();
    for (Artifact proto : protoToGroupMap.keySet()) {
        inputsToOutputsMapBuilder.put(ImmutableSet.copyOf(protoToGroupMap.get(proto)), proto);
    }
    return inputsToOutputsMapBuilder.build();
}
Also used : NestedSet(com.google.devtools.build.lib.collect.nestedset.NestedSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashMap(java.util.HashMap) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) ImmutableList(com.google.common.collect.ImmutableList) NestedSet(com.google.devtools.build.lib.collect.nestedset.NestedSet) NestedSetBuilder(com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder) Artifact(com.google.devtools.build.lib.actions.Artifact) ImmutableSet(com.google.common.collect.ImmutableSet) ProtoSourcesProvider(com.google.devtools.build.lib.rules.proto.ProtoSourcesProvider)

Example 2 with ProtoSourcesProvider

use of com.google.devtools.build.lib.rules.proto.ProtoSourcesProvider in project bazel by bazelbuild.

the class ObjcProtoAspect method create.

@Override
public ConfiguredAspect create(ConfiguredTarget base, RuleContext ruleContext, AspectParameters parameters) throws InterruptedException {
    ConfiguredAspect.Builder aspectBuilder = new ConfiguredAspect.Builder(this, parameters, ruleContext);
    ObjcProtoProvider.Builder aspectObjcProtoProvider = new ObjcProtoProvider.Builder();
    if (ruleContext.attributes().has("deps", BuildType.LABEL_LIST)) {
        Iterable<ObjcProtoProvider> depObjcProtoProviders = ruleContext.getPrerequisites("deps", Mode.TARGET, ObjcProtoProvider.class);
        aspectObjcProtoProvider.addTransitive(depObjcProtoProviders);
    }
    // Validation for the correct target attributes is done in ProtoSupport.java.
    if (ruleContext.attributes().isAttributeValueExplicitlySpecified(ObjcProtoLibraryRule.PORTABLE_PROTO_FILTERS_ATTR)) {
        aspectObjcProtoProvider.addPortableProtoFilters(PrerequisiteArtifacts.nestedSet(ruleContext, ObjcProtoLibraryRule.PORTABLE_PROTO_FILTERS_ATTR, Mode.HOST));
        // Gather up all the dependency protos depended by this target.
        Iterable<ProtoSourcesProvider> protoProviders = ruleContext.getPrerequisites("deps", Mode.TARGET, ProtoSourcesProvider.class);
        for (ProtoSourcesProvider protoProvider : protoProviders) {
            aspectObjcProtoProvider.addProtoGroup(protoProvider.getTransitiveProtoSources());
        }
        // Propagate protobuf's headers and search paths so the BinaryLinkingTargetFactory subclasses
        // (i.e. objc_binary) don't have to depend on it.
        ObjcProvider protobufObjcProvider = ruleContext.getPrerequisite(ObjcRuleClasses.PROTO_LIB_ATTR, Mode.TARGET, ObjcProvider.class);
        aspectObjcProtoProvider.addProtobufHeaders(protobufObjcProvider.get(ObjcProvider.HEADER));
        aspectObjcProtoProvider.addProtobufHeaderSearchPaths(protobufObjcProvider.get(ObjcProvider.INCLUDE));
    }
    // Only add the provider if it has any values, otherwise skip it.
    if (!aspectObjcProtoProvider.isEmpty()) {
        aspectBuilder.addProvider(aspectObjcProtoProvider.build());
    }
    return aspectBuilder.build();
}
Also used : ConfiguredAspect(com.google.devtools.build.lib.analysis.ConfiguredAspect) ProtoSourcesProvider(com.google.devtools.build.lib.rules.proto.ProtoSourcesProvider)

Example 3 with ProtoSourcesProvider

use of com.google.devtools.build.lib.rules.proto.ProtoSourcesProvider in project bazel by bazelbuild.

the class ProtoAttributes method getProtoDepsSources.

/** Returns the sets of proto files that were added using proto_library dependencies. */
private NestedSet<Artifact> getProtoDepsSources() {
    NestedSetBuilder<Artifact> artifacts = NestedSetBuilder.stableOrder();
    Iterable<ProtoSourcesProvider> providers = ruleContext.getPrerequisites("deps", Mode.TARGET, ProtoSourcesProvider.class);
    for (ProtoSourcesProvider provider : providers) {
        artifacts.addTransitive(provider.getTransitiveProtoSources());
    }
    return artifacts.build();
}
Also used : ProtoSourcesProvider(com.google.devtools.build.lib.rules.proto.ProtoSourcesProvider) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 4 with ProtoSourcesProvider

use of com.google.devtools.build.lib.rules.proto.ProtoSourcesProvider in project bazel by bazelbuild.

the class J2ObjcAspect method proto.

private ConfiguredAspect proto(ConfiguredTarget base, RuleContext ruleContext, AspectParameters parameters) throws InterruptedException {
    ProtoSourcesProvider protoSourcesProvider = base.getProvider(ProtoSourcesProvider.class);
    ImmutableList<Artifact> protoSources = protoSourcesProvider.getDirectProtoSources();
    // Avoid pulling in any generated files from blacklisted protos.
    ProtoSourceFileBlacklist protoBlacklist = new ProtoSourceFileBlacklist(ruleContext, ruleContext.getPrerequisiteArtifacts(PROTO_SOURCE_FILE_BLACKLIST_ATTR, Mode.HOST).list());
    ImmutableList<Artifact> filteredProtoSources = ImmutableList.copyOf(protoBlacklist.filter(protoSources));
    J2ObjcSource j2ObjcSource = protoJ2ObjcSource(ruleContext, filteredProtoSources);
    J2ObjcMappingFileProvider directJ2ObjcMappingFileProvider;
    if (Iterables.isEmpty(j2ObjcSource.getObjcSrcs())) {
        directJ2ObjcMappingFileProvider = new J2ObjcMappingFileProvider.Builder().build();
    } else {
        directJ2ObjcMappingFileProvider = createJ2ObjcProtoCompileActions(base, ruleContext, filteredProtoSources, j2ObjcSource);
    }
    return buildAspect(base, ruleContext, parameters, j2ObjcSource, directJ2ObjcMappingFileProvider, PROTO_DEPENDENT_ATTRIBUTES);
}
Also used : NestedSetBuilder(com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder) ProtoCompileActionBuilder(com.google.devtools.build.lib.rules.proto.ProtoCompileActionBuilder) ProtoSourceFileBlacklist(com.google.devtools.build.lib.rules.proto.ProtoSourceFileBlacklist) ProtoSourcesProvider(com.google.devtools.build.lib.rules.proto.ProtoSourcesProvider) Artifact(com.google.devtools.build.lib.actions.Artifact)

Aggregations

ProtoSourcesProvider (com.google.devtools.build.lib.rules.proto.ProtoSourcesProvider)4 Artifact (com.google.devtools.build.lib.actions.Artifact)3 NestedSetBuilder (com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSetMultimap (com.google.common.collect.ImmutableSetMultimap)1 ConfiguredAspect (com.google.devtools.build.lib.analysis.ConfiguredAspect)1 NestedSet (com.google.devtools.build.lib.collect.nestedset.NestedSet)1 ProtoCompileActionBuilder (com.google.devtools.build.lib.rules.proto.ProtoCompileActionBuilder)1 ProtoSourceFileBlacklist (com.google.devtools.build.lib.rules.proto.ProtoSourceFileBlacklist)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1