Search in sources :

Example 1 with FileTypeSet

use of com.google.devtools.build.lib.util.FileTypeSet in project bazel by bazelbuild.

the class SkylarkRuleConfiguredTargetBuilder method parseProviderKeys.

private static void parseProviderKeys(SkylarkClassObject provider, Boolean isDefaultProvider, RuleContext ruleContext, Location loc, Artifact executable, Map<String, Class<? extends TransitiveInfoProvider>> registeredProviderTypes, RuleConfiguredTargetBuilder builder) throws EvalException {
    Runfiles statelessRunfiles = null;
    Runfiles dataRunfiles = null;
    Runfiles defaultRunfiles = null;
    for (String key : provider.getKeys()) {
        if (key.equals("files")) {
            // If we specify files_to_build we don't have the executable in it by default.
            builder.setFilesToBuild(cast("files", provider, SkylarkNestedSet.class, Artifact.class, loc).getSet(Artifact.class));
        } else if (key.equals("runfiles")) {
            statelessRunfiles = cast("runfiles", provider, Runfiles.class, loc);
        } else if (key.equals("data_runfiles")) {
            dataRunfiles = cast("data_runfiles", provider, Runfiles.class, loc);
        } else if (key.equals("default_runfiles")) {
            defaultRunfiles = cast("default_runfiles", provider, Runfiles.class, loc);
        } else if (key.equals("output_groups")) {
            addOutputGroups(provider.getValue(key), loc, builder);
        } else if (key.equals("instrumented_files")) {
            SkylarkClassObject insStruct = cast("instrumented_files", provider, SkylarkClassObject.class, loc);
            Location insLoc = insStruct.getCreationLoc();
            FileTypeSet fileTypeSet = FileTypeSet.ANY_FILE;
            if (insStruct.getKeys().contains("extensions")) {
                @SuppressWarnings("unchecked") List<String> exts = cast("extensions", insStruct, SkylarkList.class, String.class, insLoc);
                if (exts.isEmpty()) {
                    fileTypeSet = FileTypeSet.NO_FILE;
                } else {
                    FileType[] fileTypes = new FileType[exts.size()];
                    for (int i = 0; i < fileTypes.length; i++) {
                        fileTypes[i] = FileType.of(exts.get(i));
                    }
                    fileTypeSet = FileTypeSet.of(fileTypes);
                }
            }
            List<String> dependencyAttributes = Collections.emptyList();
            if (insStruct.getKeys().contains("dependency_attributes")) {
                dependencyAttributes = cast("dependency_attributes", insStruct, SkylarkList.class, String.class, insLoc);
            }
            List<String> sourceAttributes = Collections.emptyList();
            if (insStruct.getKeys().contains("source_attributes")) {
                sourceAttributes = cast("source_attributes", insStruct, SkylarkList.class, String.class, insLoc);
            }
            InstrumentationSpec instrumentationSpec = new InstrumentationSpec(fileTypeSet).withSourceAttributes(sourceAttributes.toArray(new String[0])).withDependencyAttributes(dependencyAttributes.toArray(new String[0]));
            InstrumentedFilesProvider instrumentedFilesProvider = InstrumentedFilesCollector.collect(ruleContext, instrumentationSpec, InstrumentedFilesCollector.NO_METADATA_COLLECTOR, Collections.<Artifact>emptySet());
            builder.addProvider(InstrumentedFilesProvider.class, instrumentedFilesProvider);
        } else if (registeredProviderTypes.containsKey(key)) {
            Class<? extends TransitiveInfoProvider> providerType = registeredProviderTypes.get(key);
            TransitiveInfoProvider providerField = cast(key, provider, providerType, loc);
            builder.addProvider(providerType, providerField);
        } else if (isDefaultProvider) {
            // Custom keys are not allowed for default providers
            throw new EvalException(loc, "Invalid key for default provider: " + key);
        } else if (key.equals("providers")) {
            Iterable iterable = cast(key, provider, Iterable.class, loc);
            for (Object o : iterable) {
                SkylarkClassObject declaredProvider = SkylarkType.cast(o, SkylarkClassObject.class, loc, "The value of 'providers' should be a sequence of declared providers");
                builder.addSkylarkDeclaredProvider(declaredProvider, loc);
            }
        } else if (!key.equals("executable")) {
            // We handled executable already.
            builder.addSkylarkTransitiveInfo(key, provider.getValue(key), loc);
        }
    }
    addSimpleProviders(builder, ruleContext, loc, executable, statelessRunfiles, dataRunfiles, defaultRunfiles, (isDefaultProvider ? provider : null));
}
Also used : SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) InstrumentationSpec(com.google.devtools.build.lib.rules.test.InstrumentedFilesCollector.InstrumentationSpec) InstrumentedFilesProvider(com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider) EvalException(com.google.devtools.build.lib.syntax.EvalException) Artifact(com.google.devtools.build.lib.actions.Artifact) Runfiles(com.google.devtools.build.lib.analysis.Runfiles) FileTypeSet(com.google.devtools.build.lib.util.FileTypeSet) SkylarkList(com.google.devtools.build.lib.syntax.SkylarkList) SkylarkList(com.google.devtools.build.lib.syntax.SkylarkList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) ClassObject(com.google.devtools.build.lib.syntax.ClassObject) TransitiveInfoProvider(com.google.devtools.build.lib.analysis.TransitiveInfoProvider) Location(com.google.devtools.build.lib.events.Location)

Example 2 with FileTypeSet

use of com.google.devtools.build.lib.util.FileTypeSet in project bazel by bazelbuild.

the class BuildRuleWithDefaultsBuilder method populateLabelAttribute.

/**
   * Populates the label type attribute with generated values. Populates with a file if possible, or
   * generates an appropriate rule. Note, that the rules are always generated in the same package.
   */
public BuildRuleWithDefaultsBuilder populateLabelAttribute(String rulePkg, String filePkg, Attribute attribute) {
    Type<?> attrType = attribute.getType();
    String label = null;
    if (attribute.getAllowedFileTypesPredicate() != FileTypeSet.NO_FILE) {
        // Try to populate with files first
        String extension = null;
        if (attribute.getAllowedFileTypesPredicate() == FileTypeSet.ANY_FILE) {
            extension = ".txt";
        } else {
            FileTypeSet fileTypes = attribute.getAllowedFileTypesPredicate();
            // This argument should always hold, if not that means a Blaze design/implementation error
            Preconditions.checkArgument(!fileTypes.getExtensions().isEmpty());
            extension = fileTypes.getExtensions().get(0);
        }
        label = getDummyFileLabel(rulePkg, filePkg, extension, attrType);
    } else {
        Predicate<RuleClass> allowedRuleClasses = attribute.getAllowedRuleClassesPredicate();
        if (allowedRuleClasses != Predicates.<RuleClass>alwaysFalse()) {
            // See if there is an applicable rule among the already enqueued rules
            BuildRuleBuilder referencedRuleBuilder = getFirstApplicableRule(allowedRuleClasses);
            if (referencedRuleBuilder != null) {
                label = ":" + referencedRuleBuilder.ruleName;
            } else {
                RuleClass referencedRuleClass = getFirstApplicableRuleClass(allowedRuleClasses);
                if (referencedRuleClass != null) {
                    // Generate a rule with the appropriate ruleClass and a label for it in
                    // the original rule
                    label = ":" + getDummyRuleLabel(rulePkg, referencedRuleClass);
                }
            }
        }
    }
    if (label != null) {
        if (attrType instanceof ListType<?>) {
            addMultiValueAttributes(attribute.getName(), label);
        } else {
            setSingleValueAttribute(attribute.getName(), label);
        }
    }
    return this;
}
Also used : FileTypeSet(com.google.devtools.build.lib.util.FileTypeSet) ListType(com.google.devtools.build.lib.syntax.Type.ListType) RuleClass(com.google.devtools.build.lib.packages.RuleClass)

Example 3 with FileTypeSet

use of com.google.devtools.build.lib.util.FileTypeSet in project bazel by bazelbuild.

the class AttributeTest method testCloneBuilder.

@Test
public void testCloneBuilder() {
    FileTypeSet txtFiles = FileTypeSet.of(FileType.of("txt"));
    RuleClass.Builder.RuleClassNamePredicate ruleClasses = new RuleClass.Builder.RuleClassNamePredicate("mock_rule");
    Attribute parentAttr = attr("x", LABEL_LIST).allowedFileTypes(txtFiles).mandatory().aspect(TestAspects.SIMPLE_ASPECT).build();
    {
        Attribute childAttr1 = parentAttr.cloneBuilder().build();
        assertEquals("x", childAttr1.getName());
        assertEquals(txtFiles, childAttr1.getAllowedFileTypesPredicate());
        assertEquals(Predicates.alwaysTrue(), childAttr1.getAllowedRuleClassesPredicate());
        assertTrue(childAttr1.isMandatory());
        assertFalse(childAttr1.isNonEmpty());
        assertThat(childAttr1.getAspects(null)).hasSize(1);
    }
    {
        Attribute childAttr2 = parentAttr.cloneBuilder().nonEmpty().allowedRuleClasses(ruleClasses).aspect(TestAspects.ERROR_ASPECT).build();
        assertEquals("x", childAttr2.getName());
        assertEquals(txtFiles, childAttr2.getAllowedFileTypesPredicate());
        assertEquals(ruleClasses, childAttr2.getAllowedRuleClassesPredicate());
        assertTrue(childAttr2.isMandatory());
        assertTrue(childAttr2.isNonEmpty());
        assertThat(childAttr2.getAspects(null)).hasSize(2);
    }
    //Check if the parent attribute is unchanged
    assertFalse(parentAttr.isNonEmpty());
    assertEquals(Predicates.alwaysTrue(), parentAttr.getAllowedRuleClassesPredicate());
}
Also used : FileTypeSet(com.google.devtools.build.lib.util.FileTypeSet) Test(org.junit.Test)

Aggregations

FileTypeSet (com.google.devtools.build.lib.util.FileTypeSet)3 ImmutableList (com.google.common.collect.ImmutableList)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 Runfiles (com.google.devtools.build.lib.analysis.Runfiles)1 TransitiveInfoProvider (com.google.devtools.build.lib.analysis.TransitiveInfoProvider)1 Location (com.google.devtools.build.lib.events.Location)1 RuleClass (com.google.devtools.build.lib.packages.RuleClass)1 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)1 InstrumentationSpec (com.google.devtools.build.lib.rules.test.InstrumentedFilesCollector.InstrumentationSpec)1 InstrumentedFilesProvider (com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider)1 ClassObject (com.google.devtools.build.lib.syntax.ClassObject)1 EvalException (com.google.devtools.build.lib.syntax.EvalException)1 SkylarkList (com.google.devtools.build.lib.syntax.SkylarkList)1 ListType (com.google.devtools.build.lib.syntax.Type.ListType)1 List (java.util.List)1 Test (org.junit.Test)1