Search in sources :

Example 11 with SkylarkClassObject

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

the class PyCommon method getTransitivePythonSourcesFromSkylarkProvider.

private NestedSet<Artifact> getTransitivePythonSourcesFromSkylarkProvider(TransitiveInfoCollection dep) {
    SkylarkClassObject pythonSkylarkProvider = null;
    SkylarkProviders skylarkProviders = dep.getProvider(SkylarkProviders.class);
    try {
        if (skylarkProviders != null) {
            pythonSkylarkProvider = skylarkProviders.getValue(PYTHON_SKYLARK_PROVIDER_NAME, SkylarkClassObject.class);
        }
        if (pythonSkylarkProvider != null) {
            Object sourceFiles = pythonSkylarkProvider.getValue(TRANSITIVE_PYTHON_SRCS);
            String errorType;
            if (sourceFiles == null) {
                errorType = "null";
            } else {
                errorType = EvalUtils.getDataTypeNameFromClass(sourceFiles.getClass());
            }
            String errorMsg = "Illegal Argument: attribute '%s' in provider '%s' is " + "of unexpected type. Should be a set, but got a '%s'";
            NestedSet<Artifact> pythonSourceFiles = SkylarkType.cast(sourceFiles, SkylarkNestedSet.class, Artifact.class, null, errorMsg, TRANSITIVE_PYTHON_SRCS, PYTHON_SKYLARK_PROVIDER_NAME, errorType).getSet(Artifact.class);
            return pythonSourceFiles;
        }
    } catch (EvalException e) {
        ruleContext.ruleError(e.getMessage());
    }
    return null;
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) EvalException(com.google.devtools.build.lib.syntax.EvalException) SkylarkNestedSet(com.google.devtools.build.lib.syntax.SkylarkNestedSet) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 12 with SkylarkClassObject

use of com.google.devtools.build.lib.packages.SkylarkClassObject 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;
        }
    }
}
Also used : SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) Mutability(com.google.devtools.build.lib.syntax.Mutability) AspectDescriptor(com.google.devtools.build.lib.packages.AspectDescriptor) Environment(com.google.devtools.build.lib.syntax.Environment) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) EvalException(com.google.devtools.build.lib.syntax.EvalException) SkylarkRuleContext(com.google.devtools.build.lib.rules.SkylarkRuleContext)

Example 13 with SkylarkClassObject

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

the class ActionsProvider method create.

/**
   * Factory method for creating instances of the Actions provider.
   */
public static SkylarkClassObject create(Iterable<ActionAnalysisMetadata> actions) {
    Map<Artifact, ActionAnalysisMetadata> map = new HashMap<>();
    for (ActionAnalysisMetadata action : actions) {
        for (Artifact artifact : action.getOutputs()) {
            // ought to be equal anyway.
            if (!map.containsKey(artifact)) {
                map.put(artifact, action);
            }
        }
    }
    ImmutableMap<String, Object> fields = ImmutableMap.<String, Object>of("by_file", map);
    return new SkylarkClassObject(SKYLARK_CONSTRUCTOR, fields);
}
Also used : HashMap(java.util.HashMap) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) ActionAnalysisMetadata(com.google.devtools.build.lib.actions.ActionAnalysisMetadata) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 14 with SkylarkClassObject

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

the class JavaSkylarkApiTest method javaProviderExposedOnJavaLibrary.

@Test
public void javaProviderExposedOnJavaLibrary() throws Exception {
    scratch.file("foo/extension.bzl", "my_provider = provider()", "def _impl(ctx):", "  dep_params = ctx.attr.dep[java_common.provider]", "  return [my_provider(p = dep_params)]", "my_rule = rule(_impl, attrs = { 'dep' : attr.label() })");
    scratch.file("foo/BUILD", "load(':extension.bzl', 'my_rule')", "java_library(name = 'jl', srcs = ['java/A.java'])", "my_rule(name = 'r', dep = ':jl')");
    ConfiguredTarget myRuleTarget = getConfiguredTarget("//foo:r");
    ConfiguredTarget javaLibraryTarget = getConfiguredTarget("//foo:jl");
    SkylarkKey myProviderKey = new SkylarkKey(Label.parseAbsolute("//foo:extension.bzl"), "my_provider");
    SkylarkClassObject declaredProvider = myRuleTarget.getProvider(SkylarkProviders.class).getDeclaredProvider(myProviderKey);
    Object javaProvider = declaredProvider.getValue("p");
    assertThat(javaProvider).isInstanceOf(JavaProvider.class);
    assertThat(javaLibraryTarget.getProvider(JavaProvider.class)).isEqualTo(javaProvider);
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) SkylarkKey(com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) Test(org.junit.Test)

Example 15 with SkylarkClassObject

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

the class JavaSkylarkApiTest method testJavaPlugin.

@Test
public void testJavaPlugin() throws Exception {
    scratch.file("java/test/extension.bzl", "result = provider()", "def impl(ctx):", "   depj = ctx.attr.dep.java", "   return [result(", "             processor_classpath = depj.annotation_processing.processor_classpath,", "             processor_classnames = depj.annotation_processing.processor_classnames,", "          )]", "my_rule = rule(impl, attrs = { 'dep' : attr.label() })");
    scratch.file("java/test/BUILD", "load(':extension.bzl', 'my_rule')", "java_library(name = 'plugin_dep',", "    srcs = [ 'ProcessorDep.java'])", "java_plugin(name = 'plugin',", "    srcs = ['AnnotationProcessor.java'],", "    processor_class = 'com.google.process.stuff',", "    deps = [ ':plugin_dep' ])", "java_library(name = 'to_be_processed',", "    plugins = [':plugin'],", "    srcs = ['ToBeProcessed.java'])", "my_rule(name = 'my', dep = ':to_be_processed')");
    ConfiguredTarget configuredTarget = getConfiguredTarget("//java/test:my");
    SkylarkProviders provider = configuredTarget.getProvider(SkylarkProviders.class);
    SkylarkClassObject skylarkClassObject = provider.getDeclaredProvider(new SkylarkKey(Label.parseAbsolute("//java/test:extension.bzl"), "result"));
    assertThat(Iterables.transform((List<?>) skylarkClassObject.getValue("processor_classpath"), new Function<Object, String>() {

        @Nullable
        @Override
        public String apply(@Nullable Object o) {
            return ((Artifact) o).getFilename();
        }
    })).containsExactly("libplugin.jar", "libplugin_dep.jar");
    assertThat((List<?>) skylarkClassObject.getValue("processor_classnames")).containsExactly("com.google.process.stuff");
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) SkylarkKey(com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) List(java.util.List) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

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