Search in sources :

Example 6 with SkylarkProviders

use of com.google.devtools.build.lib.analysis.SkylarkProviders in project bazel by bazelbuild.

the class JavaCommon method collectCompileTimeDependencyArtifacts.

/**
   * Collects Java dependency artifacts for this target.
   *
   * @param outDeps output (compile-time) dependency artifact of this target
   */
public NestedSet<Artifact> collectCompileTimeDependencyArtifacts(@Nullable Artifact outDeps) {
    NestedSetBuilder<Artifact> builder = NestedSetBuilder.stableOrder();
    Set<JavaCompilationArgsProvider> addedProviders = new LinkedHashSet<>();
    if (outDeps != null) {
        builder.add(outDeps);
    }
    for (JavaCompilationArgsProvider provider : AnalysisUtils.getProviders(getExports(ruleContext), JavaCompilationArgsProvider.class)) {
        builder.addTransitive(provider.getCompileTimeJavaDependencyArtifacts());
        addedProviders.add(provider);
    }
    // information is passed from a custom Skylark Java rule).
    for (SkylarkProviders skylarkProviders : AnalysisUtils.getProviders(getExports(ruleContext), SkylarkProviders.class)) {
        JavaProvider javaProvider = (JavaProvider) skylarkProviders.getDeclaredProvider(JavaProvider.JAVA_PROVIDER.getKey());
        if (javaProvider != null) {
            JavaCompilationArgsProvider compilationArgsProvider = javaProvider.getProvider(JavaCompilationArgsProvider.class);
            if (!addedProviders.contains(compilationArgsProvider)) {
                builder.addTransitive((javaProvider.getProvider(JavaCompilationArgsProvider.class)).getCompileTimeJavaDependencyArtifacts());
            }
        }
    }
    return builder.build();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 7 with SkylarkProviders

use of com.google.devtools.build.lib.analysis.SkylarkProviders in project bazel by bazelbuild.

the class BuildConfigurationSkylarkTest method testSkylarkWithTestEnvOptions.

@Test
public void testSkylarkWithTestEnvOptions() throws Exception {
    useConfiguration("--test_env=TEST_ENV_VAR=my_value");
    scratch.file("examples/rule/BUILD");
    scratch.file("examples/rule/config_test.bzl", "def _test_rule_impl(ctx):", "   return struct(test_env = ctx.configuration.test_env)", "test_rule = rule(implementation = _test_rule_impl,", "   attrs = {},", ")");
    scratch.file("examples/config_skylark/BUILD", "package(default_visibility = ['//visibility:public'])", "load('/examples/rule/config_test', 'test_rule')", "test_rule(", "    name = 'my_target',", ")");
    ConfiguredTarget skylarkTarget = getConfiguredTarget("//examples/config_skylark:my_target");
    SkylarkProviders skylarkProviders = skylarkTarget.getProvider(SkylarkProviders.class);
    assertThat(skylarkProviders.getValue("test_env", SkylarkDict.class).get("TEST_ENV_VAR")).isEqualTo("my_value");
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) SkylarkDict(com.google.devtools.build.lib.syntax.SkylarkDict) Test(org.junit.Test)

Example 8 with SkylarkProviders

use of com.google.devtools.build.lib.analysis.SkylarkProviders 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 9 with SkylarkProviders

use of com.google.devtools.build.lib.analysis.SkylarkProviders in project bazel by bazelbuild.

the class PyCommon method checkForSharedLibraries.

/**
   * Returns true if this target has an .so file in its transitive dependency closure.
   */
public static boolean checkForSharedLibraries(Iterable<TransitiveInfoCollection> deps) throws EvalException {
    for (TransitiveInfoCollection dep : deps) {
        SkylarkProviders providers = dep.getProvider(SkylarkProviders.class);
        SkylarkClassObject provider = null;
        if (providers != null) {
            provider = providers.getValue(PYTHON_SKYLARK_PROVIDER_NAME, SkylarkClassObject.class);
        }
        if (provider != null) {
            Boolean isUsingSharedLibrary = provider.getValue(IS_USING_SHARED_LIBRARY, Boolean.class);
            if (Boolean.TRUE.equals(isUsingSharedLibrary)) {
                return true;
            }
        } else if (FileType.contains(dep.getProvider(FileProvider.class).getFilesToBuild(), CppFileTypes.SHARED_LIBRARY)) {
            return true;
        }
    }
    return false;
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) FileProvider(com.google.devtools.build.lib.analysis.FileProvider) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection)

Example 10 with SkylarkProviders

use of com.google.devtools.build.lib.analysis.SkylarkProviders in project bazel by bazelbuild.

the class SkylarkAspectsTest method aspectPropagating.

@Test
public void aspectPropagating() throws Exception {
    scratch.file("test/aspect.bzl", "def _impl(target, ctx):", "   s = depset([target.label])", "   c = depset([ctx.rule.kind])", "   for i in ctx.rule.attr.deps:", "       s += i.target_labels", "       c += i.rule_kinds", "   return struct(target_labels = s, rule_kinds = c)", "", "MyAspect = aspect(", "   implementation=_impl,", "   attr_aspects=['deps'],", ")");
    scratch.file("test/BUILD", "java_library(", "     name = 'yyy',", ")", "java_library(", "     name = 'xxx',", "     srcs = ['A.java'],", "     deps = [':yyy'],", ")");
    AnalysisResult analysisResult = update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:xxx");
    assertThat(getLabelsToBuild(analysisResult)).containsExactly("//test:xxx");
    AspectValue aspectValue = analysisResult.getAspects().iterator().next();
    SkylarkProviders skylarkProviders = aspectValue.getConfiguredAspect().getProvider(SkylarkProviders.class);
    assertThat(skylarkProviders).isNotNull();
    Object names = skylarkProviders.getValue("target_labels");
    assertThat(names).isInstanceOf(SkylarkNestedSet.class);
    assertThat(transform(((SkylarkNestedSet) names).toCollection(), new Function<Object, String>() {

        @Nullable
        @Override
        public String apply(Object o) {
            assertThat(o).isInstanceOf(Label.class);
            return o.toString();
        }
    })).containsExactly("//test:xxx", "//test:yyy");
    Object ruleKinds = skylarkProviders.getValue("rule_kinds");
    assertThat(ruleKinds).isInstanceOf(SkylarkNestedSet.class);
    assertThat(((SkylarkNestedSet) ruleKinds).toCollection()).containsExactly("java_library");
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) AspectValue(com.google.devtools.build.lib.skyframe.AspectValue) Label(com.google.devtools.build.lib.cmdline.Label) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Nullable(javax.annotation.Nullable) SkylarkNestedSet(com.google.devtools.build.lib.syntax.SkylarkNestedSet) Test(org.junit.Test)

Aggregations

SkylarkProviders (com.google.devtools.build.lib.analysis.SkylarkProviders)17 Test (org.junit.Test)12 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)9 AnalysisResult (com.google.devtools.build.lib.analysis.BuildView.AnalysisResult)6 Nullable (javax.annotation.Nullable)6 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)5 Artifact (com.google.devtools.build.lib.actions.Artifact)4 FileConfiguredTarget (com.google.devtools.build.lib.analysis.FileConfiguredTarget)4 RuleConfiguredTarget (com.google.devtools.build.lib.analysis.RuleConfiguredTarget)4 Label (com.google.devtools.build.lib.cmdline.Label)4 SkylarkNestedSet (com.google.devtools.build.lib.syntax.SkylarkNestedSet)4 AspectValue (com.google.devtools.build.lib.skyframe.AspectValue)3 OutputGroupProvider (com.google.devtools.build.lib.analysis.OutputGroupProvider)2 ClassObjectConstructor (com.google.devtools.build.lib.packages.ClassObjectConstructor)2 SkylarkClassObjectConstructor (com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor)2 FileProvider (com.google.devtools.build.lib.analysis.FileProvider)1 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)1 SkylarkKey (com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey)1 JavaProvider (com.google.devtools.build.lib.rules.java.JavaProvider)1 EvalException (com.google.devtools.build.lib.syntax.EvalException)1