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)));
}
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());
}
}
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"));
}
}
}
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()));
}
}
}
}
Aggregations