Search in sources :

Example 1 with ConfigurationFragmentFactory

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);
}
Also used : Target(com.google.devtools.build.lib.packages.Target) ConfigurationFragmentPolicy(com.google.devtools.build.lib.packages.ConfigurationFragmentPolicy) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) Attribute(com.google.devtools.build.lib.packages.Attribute) ConfigSettingRule(com.google.devtools.build.lib.analysis.config.ConfigRuleClasses.ConfigSettingRule) Rule(com.google.devtools.build.lib.packages.Rule) ConfigurationFragmentFactory(com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory) Fragment(com.google.devtools.build.lib.analysis.config.BuildConfiguration.Fragment)

Example 2 with ConfigurationFragmentFactory

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();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) NativeAspectClass(com.google.devtools.build.lib.packages.NativeAspectClass) RuleClass(com.google.devtools.build.lib.packages.RuleClass) ConfigurationFragmentFactory(com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory)

Example 3 with ConfigurationFragmentFactory

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;
}
Also used : FragmentOptions(com.google.devtools.build.lib.analysis.config.FragmentOptions) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConfigurationFragmentFactory(com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory) Fragment(com.google.devtools.build.lib.analysis.config.BuildConfiguration.Fragment) LinkedHashMap(java.util.LinkedHashMap) Field(java.lang.reflect.Field)

Example 4 with ConfigurationFragmentFactory

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);
    }
}
Also used : ConfigurationFragmentKey(com.google.devtools.build.lib.skyframe.ConfigurationFragmentValue.ConfigurationFragmentKey) PackageProviderForConfigurations(com.google.devtools.build.lib.analysis.config.PackageProviderForConfigurations) ConfigurationEnvironment(com.google.devtools.build.lib.analysis.config.ConfigurationEnvironment) BuildOptions(com.google.devtools.build.lib.analysis.config.BuildOptions) ConfigurationFragmentFactory(com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory) Fragment(com.google.devtools.build.lib.analysis.config.BuildConfiguration.Fragment) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)

Example 5 with ConfigurationFragmentFactory

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);
        }
    }
}
Also used : FragmentOptions(com.google.devtools.build.lib.analysis.config.FragmentOptions) RuleClass(com.google.devtools.build.lib.packages.RuleClass) ConfigurationFragmentFactory(com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory) RuleClass(com.google.devtools.build.lib.packages.RuleClass)

Aggregations

ConfigurationFragmentFactory (com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory)5 Fragment (com.google.devtools.build.lib.analysis.config.BuildConfiguration.Fragment)3 FragmentOptions (com.google.devtools.build.lib.analysis.config.FragmentOptions)2 RuleClass (com.google.devtools.build.lib.packages.RuleClass)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 BuildOptions (com.google.devtools.build.lib.analysis.config.BuildOptions)1 ConfigSettingRule (com.google.devtools.build.lib.analysis.config.ConfigRuleClasses.ConfigSettingRule)1 ConfigurationEnvironment (com.google.devtools.build.lib.analysis.config.ConfigurationEnvironment)1 InvalidConfigurationException (com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)1 PackageProviderForConfigurations (com.google.devtools.build.lib.analysis.config.PackageProviderForConfigurations)1 Attribute (com.google.devtools.build.lib.packages.Attribute)1 ConfigurationFragmentPolicy (com.google.devtools.build.lib.packages.ConfigurationFragmentPolicy)1 NativeAspectClass (com.google.devtools.build.lib.packages.NativeAspectClass)1 NoSuchTargetException (com.google.devtools.build.lib.packages.NoSuchTargetException)1 Rule (com.google.devtools.build.lib.packages.Rule)1 Target (com.google.devtools.build.lib.packages.Target)1 ConfigurationFragmentKey (com.google.devtools.build.lib.skyframe.ConfigurationFragmentValue.ConfigurationFragmentKey)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1