use of com.google.devtools.build.lib.packages.AspectDescriptor in project bazel by bazelbuild.
the class DependencyTest method withConfigurationAndAspects_BasicAccessors.
@Test
public void withConfigurationAndAspects_BasicAccessors() throws Exception {
update();
AspectDescriptor simpleAspect = new AspectDescriptor(TestAspects.SIMPLE_ASPECT);
AspectDescriptor attributeAspect = new AspectDescriptor(TestAspects.ATTRIBUTE_ASPECT);
AspectCollection twoAspects = AspectCollection.createForTests(ImmutableSet.of(simpleAspect, attributeAspect));
Dependency targetDep = Dependency.withConfigurationAndAspects(Label.parseAbsolute("//a"), getTargetConfiguration(), twoAspects);
assertThat(targetDep.getLabel()).isEqualTo(Label.parseAbsolute("//a"));
assertThat(targetDep.hasStaticConfiguration()).isTrue();
assertThat(targetDep.getConfiguration()).isEqualTo(getTargetConfiguration());
assertThat(targetDep.getAspects()).isEqualTo(twoAspects);
assertThat(targetDep.getAspectConfiguration(simpleAspect)).isEqualTo(getTargetConfiguration());
assertThat(targetDep.getAspectConfiguration(attributeAspect)).isEqualTo(getTargetConfiguration());
try {
targetDep.getTransition();
fail("withConfigurationAndAspects-created Dependencies should throw ISE on getTransition()");
} catch (IllegalStateException ex) {
// good. that's what I WANTED to happen.
}
}
use of com.google.devtools.build.lib.packages.AspectDescriptor in project bazel by bazelbuild.
the class ToplevelSkylarkAspectFunction method compute.
@Nullable
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws LoadSkylarkAspectFunctionException, InterruptedException {
SkylarkAspectLoadingKey aspectLoadingKey = (SkylarkAspectLoadingKey) skyKey.argument();
String skylarkValueName = aspectLoadingKey.getSkylarkValueName();
SkylarkImport extensionFile = aspectLoadingKey.getSkylarkImport();
// Find label corresponding to skylark file, if one exists.
ImmutableMap<String, Label> labelLookupMap;
try {
labelLookupMap = SkylarkImportLookupFunction.findLabelsForLoadStatements(ImmutableList.of(extensionFile), Label.parseAbsoluteUnchecked("//:empty"), env);
} catch (SkylarkImportFailedException e) {
env.getListener().handle(Event.error(e.getMessage()));
throw new LoadSkylarkAspectFunctionException(new AspectCreationException(e.getMessage()));
}
if (labelLookupMap == null) {
return null;
}
SkylarkAspect skylarkAspect;
Label extensionFileLabel = Iterables.getOnlyElement(labelLookupMap.values());
try {
skylarkAspect = AspectFunction.loadSkylarkAspect(env, extensionFileLabel, skylarkValueName);
if (skylarkAspect == null) {
return null;
}
if (!skylarkAspect.getParamAttributes().isEmpty()) {
throw new AspectCreationException("Cannot instantiate parameterized aspect " + skylarkAspect.getName() + " at the top level.", extensionFileLabel);
}
} catch (AspectCreationException e) {
throw new LoadSkylarkAspectFunctionException(e);
}
SkyKey aspectKey = ActionLookupValue.key(AspectValue.createAspectKey(aspectLoadingKey.getTargetLabel(), aspectLoadingKey.getTargetConfiguration(), new AspectDescriptor(skylarkAspect.getAspectClass(), AspectParameters.EMPTY), aspectLoadingKey.getAspectConfiguration()));
return env.getValue(aspectKey);
}
use of com.google.devtools.build.lib.packages.AspectDescriptor in project bazel by bazelbuild.
the class SkyframeExecutor method getConfiguredTargetMap.
/**
* Returns a map from {@link Dependency} inputs to the {@link ConfiguredTarget}s corresponding to
* those dependencies.
*
* <p>For use for legacy support and tests calling through {@code BuildView} only.
*
* <p>If a requested configured target is in error, the corresponding value is omitted from the
* returned list.
*/
@ThreadSafety.ThreadSafe
public ImmutableMultimap<Dependency, ConfiguredTarget> getConfiguredTargetMap(ExtendedEventHandler eventHandler, BuildConfiguration originalConfig, Iterable<Dependency> keys, boolean useOriginalConfig) {
checkActive();
Multimap<Dependency, BuildConfiguration> configs;
if (originalConfig != null) {
if (useOriginalConfig) {
// This flag is used because of some unfortunate complexity in the configuration machinery:
// Most callers of this method pass a <Label, Configuration> pair to directly create a
// ConfiguredTarget from, but happen to use the Dependency data structure to pass that
// info (even though the data has nothing to do with dependencies). If this configuration
// includes a split transition, a dynamic configuration created from it will *not*
// include that transition (because dynamic configurations don't embed transitions to
// other configurations. In that case, we need to preserve the original configuration.
// TODO(bazel-team); make this unnecessary once split transition logic is properly ported
// out of configurations.
configs = ArrayListMultimap.<Dependency, BuildConfiguration>create();
configs.put(Iterables.getOnlyElement(keys), originalConfig);
} else {
configs = getConfigurations(eventHandler, originalConfig.getOptions(), keys);
}
} else {
configs = ArrayListMultimap.<Dependency, BuildConfiguration>create();
for (Dependency key : keys) {
configs.put(key, null);
}
}
final List<SkyKey> skyKeys = new ArrayList<>();
for (Dependency key : keys) {
if (!configs.containsKey(key)) {
// it couldn't be loaded). Exclude it from the results.
continue;
}
for (BuildConfiguration depConfig : configs.get(key)) {
skyKeys.add(ConfiguredTargetValue.key(key.getLabel(), depConfig));
for (AspectDescriptor aspectDescriptor : key.getAspects().getAllAspects()) {
skyKeys.add(ActionLookupValue.key(AspectValue.createAspectKey(key.getLabel(), depConfig, aspectDescriptor, depConfig)));
}
}
}
EvaluationResult<SkyValue> result = evaluateSkyKeys(eventHandler, skyKeys);
for (Map.Entry<SkyKey, ErrorInfo> entry : result.errorMap().entrySet()) {
reportCycles(eventHandler, entry.getValue().getCycleInfo(), entry.getKey());
}
ImmutableMultimap.Builder<Dependency, ConfiguredTarget> cts = ImmutableMultimap.<Dependency, ConfiguredTarget>builder();
DependentNodeLoop: for (Dependency key : keys) {
if (!configs.containsKey(key)) {
// it couldn't be loaded). Exclude it from the results.
continue;
}
for (BuildConfiguration depConfig : configs.get(key)) {
SkyKey configuredTargetKey = ConfiguredTargetValue.key(key.getLabel(), depConfig);
if (result.get(configuredTargetKey) == null) {
continue;
}
ConfiguredTarget configuredTarget = ((ConfiguredTargetValue) result.get(configuredTargetKey)).getConfiguredTarget();
List<ConfiguredAspect> configuredAspects = new ArrayList<>();
for (AspectDescriptor aspectDescriptor : key.getAspects().getAllAspects()) {
SkyKey aspectKey = ActionLookupValue.key(AspectValue.createAspectKey(key.getLabel(), depConfig, aspectDescriptor, depConfig));
if (result.get(aspectKey) == null) {
continue DependentNodeLoop;
}
configuredAspects.add(((AspectValue) result.get(aspectKey)).getConfiguredAspect());
}
try {
cts.put(key, MergedConfiguredTarget.of(configuredTarget, configuredAspects));
} catch (DuplicateException e) {
throw new IllegalStateException(String.format("Error creating %s", configuredTarget.getTarget().getLabel()), e);
}
}
}
return cts.build();
}
use of com.google.devtools.build.lib.packages.AspectDescriptor in project bazel by bazelbuild.
the class SkylarkAspectFactory method create.
@Override
public ConfiguredAspect create(ConfiguredTarget base, RuleContext ruleContext, AspectParameters parameters) throws InterruptedException {
try (Mutability mutability = Mutability.create("aspect")) {
AspectDescriptor aspectDescriptor = new AspectDescriptor(skylarkAspect.getAspectClass(), parameters);
SkylarkRuleContext skylarkRuleContext;
try {
skylarkRuleContext = new SkylarkRuleContext(ruleContext, aspectDescriptor);
} catch (EvalException e) {
ruleContext.ruleError(e.getMessage());
return null;
}
Environment env = Environment.builder(mutability).setGlobals(skylarkAspect.getFuncallEnv().getGlobals()).setEventHandler(ruleContext.getAnalysisEnvironment().getEventHandler()).build();
// so we do *not* setLoadingPhase().
Object aspectSkylarkObject;
try {
aspectSkylarkObject = skylarkAspect.getImplementation().call(ImmutableList.<Object>of(base, skylarkRuleContext), ImmutableMap.<String, Object>of(), /*ast=*/
null, env);
if (ruleContext.hasErrors()) {
return null;
} else if (!(aspectSkylarkObject instanceof SkylarkClassObject) && !(aspectSkylarkObject instanceof Iterable)) {
ruleContext.ruleError(String.format("Aspect implementation should return a struct or a list, but got %s", SkylarkType.typeOf(aspectSkylarkObject)));
return null;
}
return createAspect(aspectSkylarkObject, aspectDescriptor, ruleContext);
} catch (EvalException e) {
addAspectToStackTrace(base, e);
ruleContext.ruleError("\n" + e.print());
return null;
}
}
}
use of com.google.devtools.build.lib.packages.AspectDescriptor in project bazel by bazelbuild.
the class AspectCollection method deduplicateAspects.
/**
* Deduplicate aspects in path.
*
* @throws AspectCycleOnPathException if an aspect occurs twice on the path and
* the second occurrence sees a different set of aspects.
*/
private static LinkedHashMap<AspectDescriptor, Aspect> deduplicateAspects(Iterable<Aspect> aspectPath) throws AspectCycleOnPathException {
LinkedHashMap<AspectDescriptor, Aspect> aspectMap = new LinkedHashMap<>();
ArrayList<Aspect> seenAspects = new ArrayList<>();
for (Aspect aspect : aspectPath) {
if (!aspectMap.containsKey(aspect.getDescriptor())) {
aspectMap.put(aspect.getDescriptor(), aspect);
seenAspects.add(aspect);
} else {
validateDuplicateAspect(aspect, seenAspects);
}
}
return aspectMap;
}
Aggregations