Search in sources :

Example 6 with SkylarkClassObject

use of com.google.devtools.build.lib.packages.SkylarkClassObject in project bazel by bazelbuild.

the class SkylarkRuleClassFunctionsTest method testConditionalStructConcatenation.

@Test
public void testConditionalStructConcatenation() throws Exception {
    // TODO(fwe): cannot be handled by current testing suite
    eval("def func():", "  x = struct(a = 1, b = 2)", "  if True:", "    x += struct(c = 1, d = 2)", "  return x", "x = func()");
    SkylarkClassObject x = (SkylarkClassObject) lookup("x");
    assertEquals(1, x.getValue("a"));
    assertEquals(2, x.getValue("b"));
    assertEquals(1, x.getValue("c"));
    assertEquals(2, x.getValue("d"));
}
Also used : SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) Test(org.junit.Test)

Example 7 with SkylarkClassObject

use of com.google.devtools.build.lib.packages.SkylarkClassObject 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 8 with SkylarkClassObject

use of com.google.devtools.build.lib.packages.SkylarkClassObject in project bazel by bazelbuild.

the class SkylarkRuleImplementationFunctionsTest method testDeclaredProvidersAliasTarget.

@Test
public void testDeclaredProvidersAliasTarget() throws Exception {
    scratch.file("test/foo.bzl", "foo_provider = provider()", "foobar_provider = provider()", "def _impl(ctx):", "    foo = foo_provider()", "    foobar = foobar_provider()", "    return [foo, foobar]", "foo_rule = rule(", "    implementation = _impl,", "    attrs = {", "       \"srcs\": attr.label_list(allow_files=True),", "    }", ")");
    scratch.file("test/bar.bzl", "load(':foo.bzl', 'foo_provider')", "def _impl(ctx):", "    dep = ctx.attr.deps[0]", // The goal is to test this object
    "    provider = dep[foo_provider]", // so we return it here
    "    return struct(proxy = provider)", "bar_rule = rule(", "    implementation = _impl,", "    attrs = {", "       'srcs': attr.label_list(allow_files=True),", "       'deps': attr.label_list(allow_files=True),", "    }", ")");
    scratch.file("test/BUILD", "load(':foo.bzl', 'foo_rule')", "load(':bar.bzl', 'bar_rule')", "foo_rule(name = 'foo_rule')", "alias(name = 'dep_rule', actual=':foo_rule')", "bar_rule(name = 'my_rule', deps = [':dep_rule'])");
    ConfiguredTarget configuredTarget = getConfiguredTarget("//test:my_rule");
    Object provider = configuredTarget.getProvider(SkylarkProviders.class).getValue("proxy");
    assertThat(provider).isInstanceOf(SkylarkClassObject.class);
    assertThat(((SkylarkClassObject) provider).getConstructor().getPrintableName()).isEqualTo("foo_provider");
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) Test(org.junit.Test)

Example 9 with SkylarkClassObject

use of com.google.devtools.build.lib.packages.SkylarkClassObject in project bazel by bazelbuild.

the class SkylarkAspectFactory method addDeclaredProviders.

private void addDeclaredProviders(ConfiguredAspect.Builder builder, Iterable aspectSkylarkObject) throws EvalException {
    for (Object o : aspectSkylarkObject) {
        Location loc = skylarkAspect.getImplementation().getLocation();
        SkylarkClassObject declaredProvider = SkylarkType.cast(o, SkylarkClassObject.class, loc, "A return value of an aspect implementation function should be " + "a sequence of declared providers");
        Location creationLoc = declaredProvider.getCreationLocOrNull();
        builder.addSkylarkDeclaredProvider(declaredProvider, creationLoc != null ? creationLoc : loc);
    }
}
Also used : SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) Location(com.google.devtools.build.lib.events.Location)

Example 10 with SkylarkClassObject

use of com.google.devtools.build.lib.packages.SkylarkClassObject in project bazel by bazelbuild.

the class SkylarkAspectFactory method createAspect.

private ConfiguredAspect createAspect(Object aspectSkylarkObject, AspectDescriptor aspectDescriptor, RuleContext ruleContext) throws EvalException {
    ConfiguredAspect.Builder builder = new ConfiguredAspect.Builder(aspectDescriptor, ruleContext);
    if (aspectSkylarkObject instanceof Iterable) {
        addDeclaredProviders(builder, (Iterable) aspectSkylarkObject);
    } else {
        SkylarkClassObject struct = (SkylarkClassObject) aspectSkylarkObject;
        Location loc = struct.getCreationLoc();
        for (String key : struct.getKeys()) {
            if (key.equals("output_groups")) {
                addOutputGroups(struct.getValue(key), loc, builder);
            } else if (key.equals("providers")) {
                Object value = struct.getValue(key);
                Iterable providers = SkylarkType.cast(value, Iterable.class, loc, "The value for \"providers\" should be a list of declared providers, " + "got %s instead", EvalUtils.getDataTypeName(value, false));
                addDeclaredProviders(builder, providers);
            } else {
                builder.addSkylarkTransitiveInfo(key, struct.getValue(key), loc);
            }
        }
    }
    ConfiguredAspect configuredAspect = builder.build();
    SkylarkProviderValidationUtil.checkOrphanArtifacts(ruleContext);
    return configuredAspect;
}
Also used : ConfiguredAspect(com.google.devtools.build.lib.analysis.ConfiguredAspect) SkylarkRuleConfiguredTargetBuilder(com.google.devtools.build.lib.rules.SkylarkRuleConfiguredTargetBuilder) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) Location(com.google.devtools.build.lib.events.Location)

Aggregations

SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)27 Test (org.junit.Test)15 SkylarkProviders (com.google.devtools.build.lib.analysis.SkylarkProviders)10 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)9 EvalException (com.google.devtools.build.lib.syntax.EvalException)6 Location (com.google.devtools.build.lib.events.Location)4 SkylarkClassObjectConstructor (com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor)4 ClassObject (com.google.devtools.build.lib.syntax.ClassObject)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Artifact (com.google.devtools.build.lib.actions.Artifact)3 FileConfiguredTarget (com.google.devtools.build.lib.analysis.FileConfiguredTarget)2 RuleConfiguredTarget (com.google.devtools.build.lib.analysis.RuleConfiguredTarget)2 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)2 Runfiles (com.google.devtools.build.lib.analysis.Runfiles)2 RunfilesProvider (com.google.devtools.build.lib.analysis.RunfilesProvider)2 ClassObjectConstructor (com.google.devtools.build.lib.packages.ClassObjectConstructor)2 SkylarkKey (com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey)2 SkylarkRuleContext (com.google.devtools.build.lib.rules.SkylarkRuleContext)2 Environment (com.google.devtools.build.lib.syntax.Environment)2 Mutability (com.google.devtools.build.lib.syntax.Mutability)2