Search in sources :

Example 6 with AttributeMap

use of com.google.devtools.build.lib.packages.AttributeMap in project bazel by bazelbuild.

the class DependencyResolver method resolveRuleLabels.

/**
   * Converts the given multimap of attributes to labels into a multi map of attributes to {@link
   * Dependency} objects using the proper configuration transition for each attribute.
   *
   * @throws IllegalArgumentException if the {@code node} does not refer to a {@link Rule} instance
   */
public final Collection<Dependency> resolveRuleLabels(TargetAndConfiguration node, OrderedSetMultimap<Attribute, Label> depLabels, NestedSetBuilder<Label> rootCauses) throws InterruptedException, InconsistentAspectOrderException {
    Preconditions.checkArgument(node.getTarget() instanceof Rule);
    Rule rule = (Rule) node.getTarget();
    OrderedSetMultimap<Attribute, Dependency> outgoingEdges = OrderedSetMultimap.create();
    RuleResolver depResolver = new RuleResolver(rule, node.getConfiguration(), ImmutableList.<Aspect>of(), /*attributeMap=*/
    null, rootCauses, outgoingEdges);
    Map<Attribute, Collection<Label>> m = depLabels.asMap();
    for (Map.Entry<Attribute, Collection<Label>> entry : depLabels.asMap().entrySet()) {
        for (Label depLabel : entry.getValue()) {
            depResolver.resolveDep(new AttributeAndOwner(entry.getKey()), depLabel);
        }
    }
    return outgoingEdges.values();
}
Also used : Attribute(com.google.devtools.build.lib.packages.Attribute) Label(com.google.devtools.build.lib.cmdline.Label) Collection(java.util.Collection) Rule(com.google.devtools.build.lib.packages.Rule) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) AttributeMap(com.google.devtools.build.lib.packages.AttributeMap)

Example 7 with AttributeMap

use of com.google.devtools.build.lib.packages.AttributeMap in project bazel by bazelbuild.

the class SdkMavenRepositoryTest method testGeneratedAarImport.

@Test
public void testGeneratedAarImport() throws Exception {
    sdkMavenRepository.writeBuildFiles(workspaceDir);
    Rule aarImport = getConfiguredTarget("//com.google.android:bar-1.0.0").getTarget().getAssociatedRule();
    assertThat(aarImport.getRuleClass()).isEqualTo("aar_import");
    AttributeMap attributes = RawAttributeMapper.of(aarImport);
    assertThat(attributes.get("aar", BuildType.LABEL)).isEqualTo(Label.parseAbsoluteUnchecked("//:repo/com/google/android/bar/1.0.0/bar.aar"));
    assertThat(attributes.get("exports", BuildType.LABEL_LIST)).containsExactly(Label.parseAbsoluteUnchecked("//com.google.android:foo-1.0.0"));
}
Also used : AttributeMap(com.google.devtools.build.lib.packages.AttributeMap) Rule(com.google.devtools.build.lib.packages.Rule) Test(org.junit.Test)

Example 8 with AttributeMap

use of com.google.devtools.build.lib.packages.AttributeMap in project bazel by bazelbuild.

the class SdkMavenRepositoryTest method testGeneratedJavaImport.

@Test
public void testGeneratedJavaImport() throws Exception {
    sdkMavenRepository.writeBuildFiles(workspaceDir);
    Rule javaImport = getConfiguredTarget("//com.google.android:foo-1.0.0").getTarget().getAssociatedRule();
    assertThat(javaImport.getRuleClass()).isEqualTo("java_import");
    AttributeMap attributes = RawAttributeMapper.of(javaImport);
    assertThat(attributes.get("jars", BuildType.LABEL_LIST)).containsExactly(Label.parseAbsoluteUnchecked("//:repo/com/google/android/foo/1.0.0/foo.jar"));
    assertThat(attributes.get("exports", BuildType.LABEL_LIST)).isEmpty();
}
Also used : AttributeMap(com.google.devtools.build.lib.packages.AttributeMap) Rule(com.google.devtools.build.lib.packages.Rule) Test(org.junit.Test)

Example 9 with AttributeMap

use of com.google.devtools.build.lib.packages.AttributeMap in project bazel by bazelbuild.

the class WorkspaceResolver method resolveTransitiveDependencies.

/**
   * Calculates transitive dependencies of the given //external package.
   */
public void resolveTransitiveDependencies(Package externalPackage) {
    Location location = Location.fromFile(externalPackage.getFilename());
    for (Target target : externalPackage.getTargets()) {
        // Targets are //external:foo.
        if (target.getTargetKind().startsWith("maven_jar ")) {
            RepositoryName repositoryName;
            try {
                repositoryName = RepositoryName.create("@" + target.getName());
            } catch (LabelSyntaxException e) {
                handler.handle(Event.error(location, "Invalid repository name for " + target + ": " + e.getMessage()));
                return;
            }
            com.google.devtools.build.lib.packages.Rule workspaceRule = externalPackage.getRule(repositoryName.strippedName());
            DefaultModelResolver modelResolver = resolver.getModelResolver();
            AttributeMap attributeMap = AggregatingAttributeMapper.of(workspaceRule);
            Rule rule;
            try {
                rule = new Rule(Resolver.getArtifact(attributeMap.get("artifact", Type.STRING)));
            } catch (InvalidArtifactCoordinateException e) {
                handler.handle(Event.error(location, "Couldn't get attribute: " + e.getMessage()));
                return;
            }
            if (attributeMap.isAttributeValueExplicitlySpecified("repository")) {
                modelResolver.addUserRepository(attributeMap.get("repository", Type.STRING));
                rule.setRepository(attributeMap.get("repository", Type.STRING), handler);
            }
            if (attributeMap.isAttributeValueExplicitlySpecified("sha1")) {
                rule.setSha1(attributeMap.get("sha1", Type.STRING));
            } else {
                rule.setSha1(resolver.downloadSha1(rule));
            }
            ModelSource modelSource;
            try {
                modelSource = modelResolver.resolveModel(rule.groupId(), rule.artifactId(), rule.version());
            } catch (UnresolvableModelException e) {
                handler.handle(Event.error("Could not resolve model for " + target + ": " + e.getMessage()));
                continue;
            }
            resolver.addArtifact(rule, modelSource.getLocation());
            resolver.resolveEffectiveModel(modelSource, Sets.<String>newHashSet(), rule);
        } else if (!target.getTargetKind().startsWith("bind") && !target.getTargetKind().startsWith("source ")) {
            handler.handle(Event.warn(location, "Cannot fetch transitive dependencies for " + target + " yet, skipping"));
        }
    }
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) DefaultModelResolver(com.google.devtools.build.workspace.maven.DefaultModelResolver) RepositoryName(com.google.devtools.build.lib.cmdline.RepositoryName) ModelSource(org.apache.maven.model.building.ModelSource) InvalidArtifactCoordinateException(com.google.devtools.build.workspace.maven.Resolver.InvalidArtifactCoordinateException) Target(com.google.devtools.build.lib.packages.Target) AttributeMap(com.google.devtools.build.lib.packages.AttributeMap) UnresolvableModelException(org.apache.maven.model.resolution.UnresolvableModelException) Rule(com.google.devtools.build.workspace.maven.Rule) Location(com.google.devtools.build.lib.events.Location)

Example 10 with AttributeMap

use of com.google.devtools.build.lib.packages.AttributeMap in project bazel by bazelbuild.

the class AndroidCommon method getTransitivePrerequisites.

public static final <T extends TransitiveInfoProvider> Iterable<T> getTransitivePrerequisites(RuleContext ruleContext, Mode mode, final Class<T> classType) {
    IterablesChain.Builder<T> builder = IterablesChain.builder();
    AttributeMap attributes = ruleContext.attributes();
    for (String attr : TRANSITIVE_ATTRIBUTES) {
        if (attributes.has(attr, BuildType.LABEL_LIST)) {
            builder.add(ruleContext.getPrerequisites(attr, mode, classType));
        }
    }
    return builder.build();
}
Also used : AttributeMap(com.google.devtools.build.lib.packages.AttributeMap) IterablesChain(com.google.devtools.build.lib.collect.IterablesChain)

Aggregations

AttributeMap (com.google.devtools.build.lib.packages.AttributeMap)11 Attribute (com.google.devtools.build.lib.packages.Attribute)5 Rule (com.google.devtools.build.lib.packages.Rule)5 Test (org.junit.Test)3 Label (com.google.devtools.build.lib.cmdline.Label)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 OutputFileConfiguredTarget (com.google.devtools.build.lib.analysis.OutputFileConfiguredTarget)1 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)1 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)1 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)1 RepositoryName (com.google.devtools.build.lib.cmdline.RepositoryName)1 IterablesChain (com.google.devtools.build.lib.collect.IterablesChain)1 Location (com.google.devtools.build.lib.events.Location)1 AspectDefinition (com.google.devtools.build.lib.packages.AspectDefinition)1 LateBoundLabel (com.google.devtools.build.lib.packages.Attribute.LateBoundLabel)1 License (com.google.devtools.build.lib.packages.License)1 Target (com.google.devtools.build.lib.packages.Target)1 DefaultModelResolver (com.google.devtools.build.workspace.maven.DefaultModelResolver)1 InvalidArtifactCoordinateException (com.google.devtools.build.workspace.maven.Resolver.InvalidArtifactCoordinateException)1 Rule (com.google.devtools.build.workspace.maven.Rule)1