Search in sources :

Example 1 with Label

use of com.google.devtools.build.lib.cmdline.Label in project bazel by bazelbuild.

the class Package method makeNoSuchTargetException.

protected NoSuchTargetException makeNoSuchTargetException(String targetName, String suffix) {
    Label label;
    try {
        label = createLabel(targetName);
    } catch (LabelSyntaxException e) {
        throw new IllegalArgumentException(targetName);
    }
    String msg = String.format("target '%s' not declared in package '%s'%s defined by %s", targetName, name, suffix, filename);
    return new NoSuchTargetException(label, msg);
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) Label(com.google.devtools.build.lib.cmdline.Label)

Example 2 with Label

use of com.google.devtools.build.lib.cmdline.Label in project bazel by bazelbuild.

the class PackageFactory method skylarkifyValue.

/**
   * Converts back to type that will work in BUILD and skylark,
   * such as string instead of label, SkylarkList instead of List,
   * Returns null if we don't want to export the value.
   *
   * <p>All of the types returned are immutable. If we want, we can change this to
   * immutable in the future, but this is the safe choice for now.
   */
@Nullable
private static Object skylarkifyValue(Object val, Package pkg) throws NotRepresentableException {
    // from Java native types to Skylark types should be part of the Type class hierarchy,
    if (val == null) {
        return null;
    }
    if (val instanceof Boolean) {
        return val;
    }
    if (val instanceof Integer) {
        return val;
    }
    if (val instanceof String) {
        return val;
    }
    if (val instanceof TriState) {
        switch((TriState) val) {
            case AUTO:
                return Integer.valueOf(-1);
            case YES:
                return Integer.valueOf(1);
            case NO:
                return Integer.valueOf(0);
        }
    }
    if (val instanceof Label) {
        Label l = (Label) val;
        if (l.getPackageName().equals(pkg.getName())) {
            return ":" + l.getName();
        }
        return l.getCanonicalForm();
    }
    if (val instanceof List) {
        List<Object> l = new ArrayList<>();
        for (Object o : (List) val) {
            Object elt = skylarkifyValue(o, pkg);
            if (elt == null) {
                continue;
            }
            l.add(elt);
        }
        return SkylarkList.Tuple.copyOf(l);
    }
    if (val instanceof Map) {
        Map<Object, Object> m = new TreeMap<>();
        for (Map.Entry<?, ?> e : ((Map<?, ?>) val).entrySet()) {
            Object key = skylarkifyValue(e.getKey(), pkg);
            Object mapVal = skylarkifyValue(e.getValue(), pkg);
            if (key == null || mapVal == null) {
                continue;
            }
            m.put(key, mapVal);
        }
        return m;
    }
    if (val.getClass().isAnonymousClass()) {
        // Filter them until we invent something more clever.
        return null;
    }
    if (val instanceof SkylarkValue) {
        return val;
    }
    if (val instanceof License) {
        // TODO(bazel-team): convert License.getLicenseTypes() to a list of strings.
        return null;
    }
    if (val instanceof BuildType.SelectorList) {
        return val;
    }
    // if we add more types that we can represent.
    throw new NotRepresentableException(String.format("cannot represent %s (%s) in skylark", val.toString(), val.getClass().toString()));
}
Also used : Label(com.google.devtools.build.lib.cmdline.Label) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) GlobList(com.google.devtools.build.lib.syntax.GlobList) MutableList(com.google.devtools.build.lib.syntax.SkylarkList.MutableList) List(java.util.List) SkylarkList(com.google.devtools.build.lib.syntax.SkylarkList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ClassObject(com.google.devtools.build.lib.syntax.ClassObject) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) BuildLangTypedAttributeValuesMap(com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap) TreeMap(java.util.TreeMap) SkylarkValue(com.google.devtools.build.lib.skylarkinterface.SkylarkValue) Nullable(javax.annotation.Nullable)

Example 3 with Label

use of com.google.devtools.build.lib.cmdline.Label in project bazel by bazelbuild.

the class BlazeQueryEnvironment method getBuildFiles.

// TODO(bazel-team): rename this to getDependentFiles when all implementations
// of QueryEnvironment is fixed.
@Override
public Set<Target> getBuildFiles(final QueryExpression caller, Set<Target> nodes, boolean buildFiles, boolean subincludes, boolean loads) throws QueryException {
    Set<Target> dependentFiles = new LinkedHashSet<>();
    Set<Package> seenPackages = new HashSet<>();
    // Keep track of seen labels, to avoid adding a fake subinclude label that also exists as a
    // real target.
    Set<Label> seenLabels = new HashSet<>();
    // extensions) for package "pkg", to "buildfiles".
    for (Target x : nodes) {
        Package pkg = x.getPackage();
        if (seenPackages.add(pkg)) {
            if (buildFiles) {
                addIfUniqueLabel(getNode(pkg.getBuildFile()), seenLabels, dependentFiles);
            }
            List<Label> extensions = new ArrayList<>();
            if (subincludes) {
                extensions.addAll(pkg.getSubincludeLabels());
            }
            if (loads) {
                extensions.addAll(pkg.getSkylarkFileDependencies());
            }
            for (Label subinclude : extensions) {
                addIfUniqueLabel(getSubincludeTarget(subinclude, pkg), seenLabels, dependentFiles);
                // Also add the BUILD file of the subinclude.
                if (buildFiles) {
                    try {
                        addIfUniqueLabel(getSubincludeTarget(subinclude.getLocalTargetLabel("BUILD"), pkg), seenLabels, dependentFiles);
                    } catch (LabelSyntaxException e) {
                        throw new AssertionError("BUILD should always parse as a target name", e);
                    }
                }
            }
        }
    }
    return dependentFiles;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Target(com.google.devtools.build.lib.packages.Target) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) Label(com.google.devtools.build.lib.cmdline.Label) ArrayList(java.util.ArrayList) Package(com.google.devtools.build.lib.packages.Package) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with Label

use of com.google.devtools.build.lib.cmdline.Label in project bazel by bazelbuild.

the class SkyQueryEnvironment method makePackageKeyToTargetKeyMap.

@ThreadSafe
Multimap<SkyKey, SkyKey> makePackageKeyToTargetKeyMap(Iterable<SkyKey> keys) {
    Multimap<SkyKey, SkyKey> packageKeyToTargetKeyMap = ArrayListMultimap.create();
    for (SkyKey key : keys) {
        Label label = SKYKEY_TO_LABEL.apply(key);
        if (label == null) {
            continue;
        }
        packageKeyToTargetKeyMap.put(PackageValue.key(label.getPackageIdentifier()), key);
    }
    return packageKeyToTargetKeyMap;
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) Label(com.google.devtools.build.lib.cmdline.Label) ThreadSafe(com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)

Example 5 with Label

use of com.google.devtools.build.lib.cmdline.Label in project bazel by bazelbuild.

the class CompileOneDependencyTransformer method getInputLabels.

/** Returns all labels that are contained in direct compile time inputs of {@code rule}. */
private static Set<Label> getInputLabels(Rule rule) {
    RawAttributeMapper attributeMapper = RawAttributeMapper.of(rule);
    Set<Label> labels = new TreeSet<>();
    for (String attrName : attributeMapper.getAttributeNames()) {
        if (!attributeMapper.getAttributeDefinition(attrName).isDirectCompileTimeInput()) {
            continue;
        }
        // attribute xcode_config, which leads to test errors in Bazel tests.
        if (rule.isAttrDefined(attrName, BuildType.LABEL_LIST)) {
            labels.addAll(attributeMapper.getMergedValues(attrName, BuildType.LABEL_LIST));
        }
    }
    return labels;
}
Also used : RawAttributeMapper(com.google.devtools.build.lib.packages.RawAttributeMapper) TreeSet(java.util.TreeSet) Label(com.google.devtools.build.lib.cmdline.Label)

Aggregations

Label (com.google.devtools.build.lib.cmdline.Label)190 Test (org.junit.Test)47 Target (com.google.devtools.build.lib.packages.Target)37 SkyKey (com.google.devtools.build.skyframe.SkyKey)35 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)28 Rule (com.google.devtools.build.lib.packages.Rule)26 Artifact (com.google.devtools.build.lib.actions.Artifact)21 Attribute (com.google.devtools.build.lib.packages.Attribute)21 ImmutableMap (com.google.common.collect.ImmutableMap)20 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)19 Nullable (javax.annotation.Nullable)19 ImmutableList (com.google.common.collect.ImmutableList)18 ArrayList (java.util.ArrayList)18 LinkedHashSet (java.util.LinkedHashSet)18 Map (java.util.Map)18 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)17 NoSuchTargetException (com.google.devtools.build.lib.packages.NoSuchTargetException)13 Package (com.google.devtools.build.lib.packages.Package)13 HashMap (java.util.HashMap)13 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)12