Search in sources :

Example 1 with BuildLangTypedAttributeValuesMap

use of com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap in project bazel by bazelbuild.

the class PackageFactory method addRule.

// Helper function for createRuleFunction.
private static void addRule(RuleFactory ruleFactory, String ruleClassName, PackageContext context, Map<String, Object> kwargs, FuncallExpression ast, Environment env) throws RuleFactory.InvalidRuleException, Package.NameConflictException, InterruptedException {
    RuleClass ruleClass = getBuiltInRuleClass(ruleClassName, ruleFactory);
    BuildLangTypedAttributeValuesMap attributeValues = new BuildLangTypedAttributeValuesMap(kwargs);
    AttributeContainer attributeContainer = ruleFactory.getAttributeContainer(ruleClass);
    RuleFactory.createAndAddRule(context, ruleClass, attributeValues, ast, env, attributeContainer);
}
Also used : BuildLangTypedAttributeValuesMap(com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap)

Example 2 with BuildLangTypedAttributeValuesMap

use of com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap in project bazel by bazelbuild.

the class RuleClassTest method createRule.

private Rule createRule(RuleClass ruleClass, String name, Map<String, Object> attributeValues, Location location) throws LabelSyntaxException, InterruptedException, CannotPrecomputeDefaultsException {
    Package.Builder pkgBuilder = createDummyPackageBuilder();
    Label ruleLabel;
    try {
        ruleLabel = pkgBuilder.createLabel(name);
    } catch (LabelSyntaxException e) {
        throw new IllegalArgumentException("Rule has illegal label");
    }
    return ruleClass.createRule(pkgBuilder, ruleLabel, new BuildLangTypedAttributeValuesMap(attributeValues), reporter, /*ast=*/
    null, location, new AttributeContainer(ruleClass));
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) Label(com.google.devtools.build.lib.cmdline.Label) BuildLangTypedAttributeValuesMap(com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap)

Example 3 with BuildLangTypedAttributeValuesMap

use of com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap in project bazel by bazelbuild.

the class RuleFactoryTest method testWorkspaceRuleFailsInBuildFile.

@Test
public void testWorkspaceRuleFailsInBuildFile() throws Exception {
    Path myPkgPath = scratch.resolve("/foo/workspace/mypkg/BUILD");
    Package.Builder pkgBuilder = packageFactory.newPackageBuilder(PackageIdentifier.createInMainRepo("mypkg"), "TESTING").setFilename(myPkgPath).setMakeEnv(new MakeEnvironment.Builder());
    Map<String, Object> attributeValues = new HashMap<>();
    attributeValues.put("name", "foo");
    attributeValues.put("actual", "//bar:baz");
    RuleClass ruleClass = provider.getRuleClassMap().get("bind");
    try {
        RuleFactory.createAndAddRule(pkgBuilder, ruleClass, new BuildLangTypedAttributeValuesMap(attributeValues), new Reporter(new EventBus()), /*ast=*/
        null, LOCATION_42, /*env=*/
        null, new AttributeContainer(ruleClass));
        fail();
    } catch (RuleFactory.InvalidRuleException e) {
        assertThat(e.getMessage()).contains("must be in the WORKSPACE file");
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) HashMap(java.util.HashMap) Reporter(com.google.devtools.build.lib.events.Reporter) EventBus(com.google.common.eventbus.EventBus) BuildLangTypedAttributeValuesMap(com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap) Test(org.junit.Test)

Example 4 with BuildLangTypedAttributeValuesMap

use of com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap in project bazel by bazelbuild.

the class ExternalPackageBuilder method createAndAddRepositoryRule.

public Rule createAndAddRepositoryRule(Package.Builder pkg, RuleClass ruleClass, RuleClass bindRuleClass, Map<String, Object> kwargs, FuncallExpression ast) throws RuleFactory.InvalidRuleException, Package.NameConflictException, LabelSyntaxException, InterruptedException {
    StoredEventHandler eventHandler = new StoredEventHandler();
    BuildLangTypedAttributeValuesMap attributeValues = new BuildLangTypedAttributeValuesMap(kwargs);
    Rule rule = RuleFactory.createRule(pkg, ruleClass, attributeValues, eventHandler, ast, ast.getLocation(), /*env=*/
    null, new AttributeContainer(ruleClass));
    pkg.addEvents(eventHandler.getEvents());
    overwriteRule(pkg, rule);
    for (Map.Entry<String, Label> entry : ruleClass.getExternalBindingsFunction().apply(rule).entrySet()) {
        Label nameLabel = Label.parseAbsolute("//external:" + entry.getKey());
        addBindRule(pkg, bindRuleClass, nameLabel, entry.getValue(), rule.getLocation(), new AttributeContainer(bindRuleClass));
    }
    return rule;
}
Also used : StoredEventHandler(com.google.devtools.build.lib.events.StoredEventHandler) Label(com.google.devtools.build.lib.cmdline.Label) BuildLangTypedAttributeValuesMap(com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap) Map(java.util.Map) BuildLangTypedAttributeValuesMap(com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap)

Example 5 with BuildLangTypedAttributeValuesMap

use of com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap in project bazel by bazelbuild.

the class ExternalPackageBuilder method addBindRule.

static void addBindRule(Builder pkg, RuleClass bindRuleClass, Label virtual, Label actual, Location location, AttributeContainer attributeContainer) throws RuleFactory.InvalidRuleException, Package.NameConflictException, InterruptedException {
    Map<String, Object> attributes = Maps.newHashMap();
    // Bound rules don't have a name field, but this works because we don't want more than one
    // with the same virtual name.
    attributes.put("name", virtual.getName());
    if (actual != null) {
        attributes.put("actual", actual);
    }
    StoredEventHandler handler = new StoredEventHandler();
    BuildLangTypedAttributeValuesMap attributeValues = new BuildLangTypedAttributeValuesMap(attributes);
    Rule rule = RuleFactory.createRule(pkg, bindRuleClass, attributeValues, handler, /*ast=*/
    null, location, /*env=*/
    null, attributeContainer);
    overwriteRule(pkg, rule);
    rule.setVisibility(ConstantRuleVisibility.PUBLIC);
}
Also used : StoredEventHandler(com.google.devtools.build.lib.events.StoredEventHandler) BuildLangTypedAttributeValuesMap(com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap)

Aggregations

BuildLangTypedAttributeValuesMap (com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap)9 EventBus (com.google.common.eventbus.EventBus)5 Reporter (com.google.devtools.build.lib.events.Reporter)5 Path (com.google.devtools.build.lib.vfs.Path)5 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 Label (com.google.devtools.build.lib.cmdline.Label)2 StoredEventHandler (com.google.devtools.build.lib.events.StoredEventHandler)2 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)1 Map (java.util.Map)1