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