Search in sources :

Example 21 with SkylarkCallable

use of com.google.devtools.build.lib.skylarkinterface.SkylarkCallable in project bazel by bazelbuild.

the class JavaSkylarkCommon method getDefaultJavacOpts.

@SkylarkCallable(name = "default_javac_opts", // This function is experimental for now.
documented = false, // There's only one mandatory positional,the Skylark context
mandatoryPositionals = 1, parameters = { @Param(name = "java_toolchain_attr", positional = false, named = true, type = String.class) })
public static List<String> getDefaultJavacOpts(SkylarkRuleContext skylarkRuleContext, String javaToolchainAttr) {
    RuleContext ruleContext = skylarkRuleContext.getRuleContext();
    ConfiguredTarget javaToolchainConfigTarget = (ConfiguredTarget) checkNotNull(skylarkRuleContext.getAttr().getValue(javaToolchainAttr));
    JavaToolchainProvider toolchain = checkNotNull(javaToolchainConfigTarget.getProvider(JavaToolchainProvider.class));
    return ImmutableList.copyOf(Iterables.concat(toolchain.getJavacOptions(), ruleContext.getTokenizedStringListAttr("javacopts")));
}
Also used : RuleContext(com.google.devtools.build.lib.analysis.RuleContext) SkylarkRuleContext(com.google.devtools.build.lib.rules.SkylarkRuleContext) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) SkylarkCallable(com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)

Example 22 with SkylarkCallable

use of com.google.devtools.build.lib.skylarkinterface.SkylarkCallable in project bazel by bazelbuild.

the class JavaProtoSkylarkCommon method getJavacOpts.

@SkylarkCallable(name = "javac_opts", // This function is experimental for now.
documented = false, // There's only one mandatory positional,the Skylark context
mandatoryPositionals = 1, parameters = { @Param(name = "java_toolchain_attr", positional = false, named = true, type = String.class) })
public static // TODO(elenairina): Consider a nicer way of returning this, taking in a JavaToolchainProvider.
List<String> getJavacOpts(SkylarkRuleContext skylarkRuleContext, String javaToolchainAttr) {
    ConfiguredTarget javaToolchainConfigTarget = (ConfiguredTarget) checkNotNull(skylarkRuleContext.getAttr().getValue(javaToolchainAttr));
    JavaToolchainProvider toolchain = checkNotNull(javaToolchainConfigTarget.getProvider(JavaToolchainProvider.class));
    return ImmutableList.<String>builder().addAll(toolchain.getJavacOptions()).addAll(toolchain.getCompatibleJavacOptions(JavaSemantics.PROTO_JAVACOPTS_KEY)).build();
}
Also used : JavaToolchainProvider(com.google.devtools.build.lib.rules.java.JavaToolchainProvider) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) SkylarkCallable(com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)

Example 23 with SkylarkCallable

use of com.google.devtools.build.lib.skylarkinterface.SkylarkCallable in project bazel by bazelbuild.

the class JavaProtoSkylarkCommon method createProtoCompileAction.

@SkylarkCallable(name = "create_java_lite_proto_compile_action", // This function is experimental for now.
documented = false, // There's 2 mandatory positional arguments, the Skylark context and the ConfiguredTarget.
mandatoryPositionals = 2, parameters = { @Param(name = "src_jar", positional = false, named = true, type = Artifact.class), @Param(name = "proto_toolchain_attr", positional = false, named = true, type = String.class), @Param(name = "flavour", positional = false, named = true, type = String.class, defaultValue = "java") })
public static void createProtoCompileAction(SkylarkRuleContext skylarkRuleContext, ConfiguredTarget target, Artifact sourceJar, String protoToolchainAttr, String flavour) {
    SupportData supportData = checkNotNull(target.getProvider(ProtoSupportDataProvider.class).getSupportData());
    ProtoCompileActionBuilder.registerActions(skylarkRuleContext.getRuleContext(), ImmutableList.of(new ProtoCompileActionBuilder.ToolchainInvocation(flavour, getProtoToolchainProvider(skylarkRuleContext, protoToolchainAttr), sourceJar.getExecPathString())), supportData.getDirectProtoSources(), supportData.getTransitiveImports(), supportData.getProtosInDirectDeps(), skylarkRuleContext.getLabel().getCanonicalForm(), ImmutableList.of(sourceJar), "JavaLite", true);
}
Also used : SupportData(com.google.devtools.build.lib.rules.proto.SupportData) SkylarkCallable(com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)

Example 24 with SkylarkCallable

use of com.google.devtools.build.lib.skylarkinterface.SkylarkCallable in project bazel by bazelbuild.

the class SkylarkMethodDoc method getParameterString.

private String getParameterString(Method method) {
    SkylarkCallable annotation = SkylarkInterfaceUtils.getSkylarkCallable(method);
    int nbPositional = annotation.mandatoryPositionals();
    if (annotation.parameters().length > 0 && nbPositional < 0) {
        nbPositional = 0;
    }
    List<String> argList = new ArrayList<>();
    for (int i = 0; i < nbPositional; i++) {
        argList.add("arg" + i + ":" + getTypeAnchor(method.getParameterTypes()[i]));
    }
    boolean named = false;
    for (Param param : annotation.parameters()) {
        if (param.named() && !param.positional() && !named) {
            named = true;
            if (!argList.isEmpty()) {
                argList.add("*");
            }
        }
        argList.add(formatParameter(param));
    }
    return Joiner.on(", ").join(argList);
}
Also used : SkylarkCallable(com.google.devtools.build.lib.skylarkinterface.SkylarkCallable) ArrayList(java.util.ArrayList) Param(com.google.devtools.build.lib.skylarkinterface.Param)

Example 25 with SkylarkCallable

use of com.google.devtools.build.lib.skylarkinterface.SkylarkCallable in project bazel by bazelbuild.

the class SkylarkInterfaceUtilsTest method testGetSkylarkCallableSubclass.

@Test
public void testGetSkylarkCallableSubclass() throws Exception {
    // Subclass's annotation is used.
    Method method = MockClassC.class.getMethod("foo");
    SkylarkCallable ann = SkylarkInterfaceUtils.getSkylarkCallable(method);
    assertThat(ann).isNotNull();
    assertThat(ann.doc()).isEqualTo("MockClassC#foo");
}
Also used : SkylarkCallable(com.google.devtools.build.lib.skylarkinterface.SkylarkCallable) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

SkylarkCallable (com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)27 Method (java.lang.reflect.Method)8 Test (org.junit.Test)7 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)6 ImmutableList (com.google.common.collect.ImmutableList)5 RepositoryFunctionException (com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)5 IOException (java.io.IOException)5 ImmutableMap (com.google.common.collect.ImmutableMap)2 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)2 Param (com.google.devtools.build.lib.skylarkinterface.Param)2 OutputStream (java.io.OutputStream)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 SkylarkJavaMethodDoc (com.google.devtools.build.docgen.skylark.SkylarkJavaMethodDoc)1 SkylarkModuleDoc (com.google.devtools.build.docgen.skylark.SkylarkModuleDoc)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 Root (com.google.devtools.build.lib.actions.Root)1 MiddlemanProvider (com.google.devtools.build.lib.analysis.MiddlemanProvider)1