Search in sources :

Example 1 with BuiltinFunction

use of com.google.devtools.build.lib.syntax.BuiltinFunction in project bazel by bazelbuild.

the class WorkspaceFactory method newRuleFunction.

/**
   * Returns a function-value implementing the build rule "ruleClass" (e.g. cc_library) in the
   * specified package context.
   */
private static BuiltinFunction newRuleFunction(final RuleFactory ruleFactory, final String ruleClassName, final boolean allowOverride) {
    return new BuiltinFunction(ruleClassName, FunctionSignature.KWARGS, BuiltinFunction.USE_AST_ENV) {

        public Object invoke(Map<String, Object> kwargs, FuncallExpression ast, Environment env) throws EvalException, InterruptedException {
            try {
                Package.Builder builder = PackageFactory.getContext(env, ast).pkgBuilder;
                if (!allowOverride && kwargs.containsKey("name") && builder.targets.containsKey(kwargs.get("name"))) {
                    throw new EvalException(ast.getLocation(), "Cannot redefine repository after any load statement in the WORKSPACE file" + " (for repository '" + kwargs.get("name") + "')");
                }
                RuleClass ruleClass = ruleFactory.getRuleClass(ruleClassName);
                RuleClass bindRuleClass = ruleFactory.getRuleClass("bind");
                Rule rule = builder.externalPackageData().createAndAddRepositoryRule(builder, ruleClass, bindRuleClass, kwargs, ast);
                if (!isLegalWorkspaceName(rule.getName())) {
                    throw new EvalException(ast.getLocation(), rule + "'s name field must be a legal workspace name");
                }
            } catch (RuleFactory.InvalidRuleException | Package.NameConflictException | LabelSyntaxException e) {
                throw new EvalException(ast.getLocation(), e.getMessage());
            }
            return NONE;
        }
    };
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) EvalException(com.google.devtools.build.lib.syntax.EvalException) NameConflictException(com.google.devtools.build.lib.packages.Package.NameConflictException) BuiltinFunction(com.google.devtools.build.lib.syntax.BuiltinFunction) Environment(com.google.devtools.build.lib.syntax.Environment) FuncallExpression(com.google.devtools.build.lib.syntax.FuncallExpression) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) InvalidRuleException(com.google.devtools.build.lib.packages.RuleFactory.InvalidRuleException)

Example 2 with BuiltinFunction

use of com.google.devtools.build.lib.syntax.BuiltinFunction in project bazel by bazelbuild.

the class SkylarkRuleImplementationFunctionsTest method testNoStackTraceOnInterrupt.

@Test
public void testNoStackTraceOnInterrupt() throws Exception {
    setupThrowFunction(new BuiltinFunction("throw") {

        @SuppressWarnings("unused")
        public Object invoke() throws Exception {
            throw new InterruptedException();
        }
    });
    try {
        eval("throw()");
        fail("Expected an InterruptedException");
    } catch (InterruptedException ex) {
    // Expected.
    }
}
Also used : BuiltinFunction(com.google.devtools.build.lib.syntax.BuiltinFunction) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) EvalException(com.google.devtools.build.lib.syntax.EvalException) ExpectedException(org.junit.rules.ExpectedException) Test(org.junit.Test)

Example 3 with BuiltinFunction

use of com.google.devtools.build.lib.syntax.BuiltinFunction in project bazel by bazelbuild.

the class SkylarkRepositoryContextTest method buildRuleClass.

protected static RuleClass buildRuleClass(Attribute... attributes) {
    RuleClass.Builder ruleClassBuilder = new RuleClass.Builder("test", RuleClassType.WORKSPACE, true);
    for (Attribute attr : attributes) {
        ruleClassBuilder.addOrOverrideAttribute(attr);
    }
    ruleClassBuilder.setWorkspaceOnly();
    ruleClassBuilder.setConfiguredTargetFunction(new BuiltinFunction("test") {
    });
    return ruleClassBuilder.build();
}
Also used : BuiltinFunction(com.google.devtools.build.lib.syntax.BuiltinFunction) Attribute(com.google.devtools.build.lib.packages.Attribute) RuleClass(com.google.devtools.build.lib.packages.RuleClass)

Example 4 with BuiltinFunction

use of com.google.devtools.build.lib.syntax.BuiltinFunction in project bazel by bazelbuild.

the class WorkspaceFactory method newBindFunction.

private static BuiltinFunction newBindFunction(final RuleFactory ruleFactory) {
    return new BuiltinFunction("bind", FunctionSignature.namedOnly(1, "name", "actual"), BuiltinFunction.USE_AST_ENV) {

        public Object invoke(String name, String actual, FuncallExpression ast, Environment env) throws EvalException, InterruptedException {
            Label nameLabel;
            try {
                nameLabel = Label.parseAbsolute("//external:" + name);
                try {
                    Package.Builder builder = PackageFactory.getContext(env, ast).pkgBuilder;
                    RuleClass ruleClass = ruleFactory.getRuleClass("bind");
                    builder.externalPackageData().addBindRule(builder, ruleClass, nameLabel, actual == null ? null : Label.parseAbsolute(actual), ast.getLocation(), ruleFactory.getAttributeContainer(ruleClass));
                } catch (RuleFactory.InvalidRuleException | Package.NameConflictException | LabelSyntaxException e) {
                    throw new EvalException(ast.getLocation(), e.getMessage());
                }
            } catch (LabelSyntaxException e) {
                throw new EvalException(ast.getLocation(), e.getMessage());
            }
            return NONE;
        }
    };
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) Label(com.google.devtools.build.lib.cmdline.Label) EvalException(com.google.devtools.build.lib.syntax.EvalException) NameConflictException(com.google.devtools.build.lib.packages.Package.NameConflictException) BuiltinFunction(com.google.devtools.build.lib.syntax.BuiltinFunction) Environment(com.google.devtools.build.lib.syntax.Environment) FuncallExpression(com.google.devtools.build.lib.syntax.FuncallExpression) InvalidRuleException(com.google.devtools.build.lib.packages.RuleFactory.InvalidRuleException)

Example 5 with BuiltinFunction

use of com.google.devtools.build.lib.syntax.BuiltinFunction in project bazel by bazelbuild.

the class SkylarkRuleImplementationFunctionsTest method testStackTraceWithoutOriginalMessage.

@Test
public void testStackTraceWithoutOriginalMessage() throws Exception {
    setupThrowFunction(new BuiltinFunction("throw") {

        @SuppressWarnings("unused")
        public Object invoke() throws Exception {
            throw new ThereIsNoMessageException();
        }
    });
    checkEvalErrorContains("There Is No Message: SkylarkRuleImplementationFunctionsTest$3.invoke() in " + "SkylarkRuleImplementationFunctionsTest.java:", // This test skips the line number since it was not consistent across local tests and TAP.
    "throw()");
}
Also used : BuiltinFunction(com.google.devtools.build.lib.syntax.BuiltinFunction) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) EvalException(com.google.devtools.build.lib.syntax.EvalException) ExpectedException(org.junit.rules.ExpectedException) Test(org.junit.Test)

Aggregations

BuiltinFunction (com.google.devtools.build.lib.syntax.BuiltinFunction)6 EvalException (com.google.devtools.build.lib.syntax.EvalException)4 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)3 Environment (com.google.devtools.build.lib.syntax.Environment)3 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)2 NameConflictException (com.google.devtools.build.lib.packages.Package.NameConflictException)2 InvalidRuleException (com.google.devtools.build.lib.packages.RuleFactory.InvalidRuleException)2 FuncallExpression (com.google.devtools.build.lib.syntax.FuncallExpression)2 Test (org.junit.Test)2 ExpectedException (org.junit.rules.ExpectedException)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Label (com.google.devtools.build.lib.cmdline.Label)1 Attribute (com.google.devtools.build.lib.packages.Attribute)1 RuleClass (com.google.devtools.build.lib.packages.RuleClass)1 SkylarkSignature (com.google.devtools.build.lib.skylarkinterface.SkylarkSignature)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1