Search in sources :

Example 16 with Location

use of com.google.devtools.build.lib.events.Location in project bazel by bazelbuild.

the class SliceExpression method doEval.

@Override
Object doEval(Environment env) throws EvalException, InterruptedException {
    Object objValue = obj.eval(env);
    Object startValue = start.eval(env);
    Object endValue = end.eval(env);
    Object stepValue = step.eval(env);
    Location loc = getLocation();
    if (objValue instanceof SkylarkList) {
        SkylarkList<Object> list = (SkylarkList<Object>) objValue;
        Object slice = list.getSlice(startValue, endValue, stepValue, loc);
        return SkylarkType.convertToSkylark(slice, env);
    } else if (objValue instanceof String) {
        String string = (String) objValue;
        List<Integer> indices = EvalUtils.getSliceIndices(startValue, endValue, stepValue, string.length(), loc);
        char[] result = new char[indices.size()];
        char[] original = ((String) objValue).toCharArray();
        int resultIndex = 0;
        for (int originalIndex : indices) {
            result[resultIndex] = original[originalIndex];
            ++resultIndex;
        }
        return new String(result);
    }
    throw new EvalException(loc, Printer.format("type '%s' has no operator [:](%s, %s, %s)", EvalUtils.getDataTypeName(objValue), EvalUtils.getDataTypeName(startValue), EvalUtils.getDataTypeName(endValue), EvalUtils.getDataTypeName(stepValue)));
}
Also used : List(java.util.List) Location(com.google.devtools.build.lib.events.Location)

Example 17 with Location

use of com.google.devtools.build.lib.events.Location in project bazel by bazelbuild.

the class SkylarkRuleConfiguredTargetBuilder method addStructFieldsAndBuild.

private static ConfiguredTarget addStructFieldsAndBuild(RuleContext ruleContext, RuleConfiguredTargetBuilder builder, Object target, Artifact executable, Map<String, Class<? extends TransitiveInfoProvider>> registeredProviderTypes) throws EvalException {
    Location loc = null;
    Boolean isParsed = false;
    if (target instanceof SkylarkClassObject) {
        SkylarkClassObject struct = (SkylarkClassObject) target;
        loc = struct.getCreationLoc();
        parseProviderKeys(struct, false, ruleContext, loc, executable, registeredProviderTypes, builder);
        isParsed = true;
    } else if (target instanceof Iterable) {
        loc = ruleContext.getRule().getRuleClassObject().getConfiguredTargetFunction().getLocation();
        for (Object o : (Iterable) target) {
            SkylarkClassObject declaredProvider = SkylarkType.cast(o, SkylarkClassObject.class, loc, "A return value of a rule implementation function should be " + "a sequence of declared providers");
            if (declaredProvider.getConstructor().getKey().equals(SkylarkRuleContext.getDefaultProvider().getKey())) {
                parseProviderKeys(declaredProvider, true, ruleContext, loc, executable, registeredProviderTypes, builder);
                isParsed = true;
            } else {
                Location creationLoc = declaredProvider.getCreationLocOrNull();
                builder.addSkylarkDeclaredProvider(declaredProvider, creationLoc != null ? creationLoc : loc);
            }
        }
    }
    if (!isParsed) {
        addSimpleProviders(builder, ruleContext, loc, executable, null, null, null, null);
    }
    try {
        return builder.build();
    } catch (IllegalArgumentException e) {
        throw new EvalException(loc, e.getMessage());
    }
}
Also used : SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) ClassObject(com.google.devtools.build.lib.syntax.ClassObject) EvalException(com.google.devtools.build.lib.syntax.EvalException) Location(com.google.devtools.build.lib.events.Location)

Example 18 with Location

use of com.google.devtools.build.lib.events.Location 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 19 with Location

use of com.google.devtools.build.lib.events.Location in project bazel by bazelbuild.

the class AttributeContainerTest method testPackedState.

@Test
public void testPackedState() throws Exception {
    Random rng = new Random();
    // The state packing machinery has special behavior at multiples of 8,
    // so set enough explicit values and locations to exercise that.
    final int N = 17;
    Attribute[] attributes = new Attribute[N];
    for (int i = 0; i < N; ++i) {
        attributes[i] = ruleClass.getAttribute(i);
    }
    Object someValue = new Object();
    Location[] locations = new Location[N];
    for (int i = 0; i < N; ++i) {
        locations[i] = newLocation();
    }
    // test relies on checking reference inequality
    assertTrue(locations[0] != locations[1]);
    for (int explicitCount = 0; explicitCount <= N; ++explicitCount) {
        for (int locationCount = 0; locationCount <= N; ++locationCount) {
            AttributeContainer container = new AttributeContainer(ruleClass);
            // Shuffle the attributes each time through, to exercise
            // different stored indices and orderings.
            Collections.shuffle(Arrays.asList(attributes));
            // Also randomly interleave calls to the two setters.
            int valuePassKey = rng.nextInt(1 << N);
            int locationPassKey = rng.nextInt(1 << N);
            for (int pass = 0; pass <= 1; ++pass) {
                for (int i = 0; i < explicitCount; ++i) {
                    if (pass == ((valuePassKey >> i) & 1)) {
                        container.setAttributeValue(attributes[i], someValue, true);
                    }
                }
                for (int i = 0; i < locationCount; ++i) {
                    if (pass == ((locationPassKey >> i) & 1)) {
                        container.setAttributeLocation(attributes[i], locations[i]);
                    }
                }
            }
            for (int i = 0; i < N; ++i) {
                boolean expected = i < explicitCount;
                assertEquals(expected, container.isAttributeValueExplicitlySpecified(attributes[i]));
            }
            for (int i = 0; i < N; ++i) {
                Location expected = i < locationCount ? locations[i] : null;
                assertSame(expected, container.getAttributeLocation(attributes[i].getName()));
            }
        }
    }
}
Also used : Random(java.util.Random) Location(com.google.devtools.build.lib.events.Location) Test(org.junit.Test)

Aggregations

Location (com.google.devtools.build.lib.events.Location)19 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)4 Test (org.junit.Test)4 ClassObject (com.google.devtools.build.lib.syntax.ClassObject)3 EvalException (com.google.devtools.build.lib.syntax.EvalException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 List (java.util.List)2 ImmutableList (com.google.common.collect.ImmutableList)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 ConfiguredAspect (com.google.devtools.build.lib.analysis.ConfiguredAspect)1 Runfiles (com.google.devtools.build.lib.analysis.Runfiles)1 TransitiveInfoProvider (com.google.devtools.build.lib.analysis.TransitiveInfoProvider)1 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)1 RepositoryName (com.google.devtools.build.lib.cmdline.RepositoryName)1 Event (com.google.devtools.build.lib.events.Event)1 AttributeMap (com.google.devtools.build.lib.packages.AttributeMap)1 Target (com.google.devtools.build.lib.packages.Target)1 SkylarkRuleConfiguredTargetBuilder (com.google.devtools.build.lib.rules.SkylarkRuleConfiguredTargetBuilder)1 InstrumentationSpec (com.google.devtools.build.lib.rules.test.InstrumentedFilesCollector.InstrumentationSpec)1 InstrumentedFilesProvider (com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider)1