use of com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory in project bazel by bazelbuild.
the class TransitiveTargetFunction method computeSkyValue.
@Override
public SkyValue computeSkyValue(TargetAndErrorIfAny targetAndErrorIfAny, TransitiveTargetValueBuilder builder) {
Target target = targetAndErrorIfAny.getTarget();
NoSuchTargetException errorLoadingTarget = targetAndErrorIfAny.getErrorLoadingTarget();
// Get configuration fragments directly required by this rule.
if (target instanceof Rule) {
Rule rule = (Rule) target;
// Declared by the rule class:
ConfigurationFragmentPolicy configurationFragmentPolicy = rule.getRuleClassObject().getConfigurationFragmentPolicy();
for (ConfigurationFragmentFactory factory : ruleClassProvider.getConfigurationFragments()) {
Class<? extends Fragment> fragment = factory.creates();
// (named) fragments.
if (configurationFragmentPolicy.isLegalConfigurationFragment(fragment)) {
addFragmentIfNew(builder, fragment.asSubclass(BuildConfiguration.Fragment.class));
}
}
// Declared by late-bound attributes:
for (Attribute attr : rule.getAttributes()) {
if (attr.isLateBound()) {
addFragmentsIfNew(builder, attr.getLateBoundDefault().getRequiredConfigurationFragments());
}
}
// corresponding fragments in their configurations to properly resolve:
if (rule.getRuleClass().equals(ConfigSettingRule.RULE_NAME)) {
addFragmentsIfNew(builder, ConfigSettingRule.requiresConfigurationFragments(rule, optionsToFragmentMap));
}
// Fragments to unconditionally include:
addFragmentIfNew(builder, ruleClassProvider.getUniversalFragment().asSubclass(BuildConfiguration.Fragment.class));
}
return builder.build(errorLoadingTarget);
}
use of com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory in project bazel by bazelbuild.
the class ConfiguredRuleClassProvider method getAllFragments.
/**
* Returns all registered {@link BuildConfiguration.Fragment} classes.
*/
public Set<Class<? extends BuildConfiguration.Fragment>> getAllFragments() {
ImmutableSet.Builder<Class<? extends BuildConfiguration.Fragment>> fragmentsBuilder = ImmutableSet.builder();
for (ConfigurationFragmentFactory factory : getConfigurationFragments()) {
fragmentsBuilder.add(factory.creates());
}
fragmentsBuilder.add(getUniversalFragment());
return fragmentsBuilder.build();
}
use of com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory in project bazel by bazelbuild.
the class TransitiveTargetFunction method computeOptionsToFragmentMap.
/**
* Computes the option name --> config fragments map. Note that this mapping is technically
* one-to-many: a single option may be required by multiple fragments (e.g. Java options are
* used by both JavaConfiguration and Jvm). In such cases, we arbitrarily choose one fragment
* since that's all that's needed to satisfy the config_setting.
*/
private static Map<String, Class<? extends Fragment>> computeOptionsToFragmentMap(ConfiguredRuleClassProvider ruleClassProvider) {
Map<String, Class<? extends Fragment>> result = new LinkedHashMap<>();
Map<Class<? extends FragmentOptions>, Integer> visitedOptionsClasses = new HashMap<>();
for (ConfigurationFragmentFactory factory : ruleClassProvider.getConfigurationFragments()) {
Set<Class<? extends FragmentOptions>> requiredOpts = factory.requiredOptions();
for (Class<? extends FragmentOptions> optionsClass : requiredOpts) {
Integer previousBest = visitedOptionsClasses.get(optionsClass);
if (previousBest != null && previousBest <= requiredOpts.size()) {
// on as few options classes as possible (not necessarily unique).
continue;
}
visitedOptionsClasses.put(optionsClass, requiredOpts.size());
for (Field field : optionsClass.getFields()) {
if (field.isAnnotationPresent(Option.class)) {
result.put(field.getAnnotation(Option.class).name(), factory.creates());
}
}
}
}
return result;
}
use of com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory in project bazel by bazelbuild.
the class ConfigurationFragmentFunction method compute.
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws InterruptedException, ConfigurationFragmentFunctionException {
ConfigurationFragmentKey configurationFragmentKey = (ConfigurationFragmentKey) skyKey.argument();
BuildOptions buildOptions = configurationFragmentKey.getBuildOptions();
ConfigurationFragmentFactory factory = getFactory(configurationFragmentKey.getFragmentType());
try {
PackageProviderForConfigurations packageProvider = new SkyframePackageLoaderWithValueEnvironment(env, ruleClassProvider);
ConfigurationEnvironment confEnv = new ConfigurationBuilderEnvironment(packageProvider);
Fragment fragment = factory.create(confEnv, buildOptions);
if (env.valuesMissing()) {
return null;
}
return new ConfigurationFragmentValue(fragment);
} catch (InvalidConfigurationException e) {
// exception with missing Skyframe dependencies.
if (env.valuesMissing()) {
return null;
}
throw new ConfigurationFragmentFunctionException(e);
}
}
use of com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory in project bazel by bazelbuild.
the class BazelRuleClassProviderTest method checkConfigConsistency.
private void checkConfigConsistency(ConfiguredRuleClassProvider provider) {
// Check that every fragment required by a rule is present.
Set<Class<? extends BuildConfiguration.Fragment>> configurationFragments = provider.getAllFragments();
for (RuleClass ruleClass : provider.getRuleClassMap().values()) {
for (Class<?> fragment : ruleClass.getConfigurationFragmentPolicy().getRequiredConfigurationFragments()) {
assertWithMessage(ruleClass.toString()).that(configurationFragments).contains(fragment);
}
}
List<Class<? extends FragmentOptions>> configOptions = provider.getConfigurationOptions();
for (ConfigurationFragmentFactory fragmentFactory : provider.getConfigurationFragments()) {
// Check that every created fragment is present.
assertThat(configurationFragments).contains(fragmentFactory.creates());
// Check that every options class required for fragment creation is provided.
for (Class<? extends FragmentOptions> options : fragmentFactory.requiredOptions()) {
assertThat(configOptions).contains(options);
}
}
}
Aggregations