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