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();
}
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");
}
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;
}
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;
}
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");
}
Aggregations