use of com.google.devtools.build.lib.packages.License in project bazel by bazelbuild.
the class XmlOutputFormatter method createValueElement.
/**
* Creates and returns a new DOM tree for the specified attribute values.
* For non-configurable attributes, this is a single value. For configurable
* attributes, this contains one value for each configuration.
* (Only toplevel values are named attributes; list elements are unnamed.)
*
* <p>In the case of configurable attributes, multi-value attributes (e.g. lists)
* merge all configured lists into an aggregate flattened list. Single-value attributes
* simply refrain to set a value and annotate the DOM element as configurable.
*
* <P>(The ungainly qualified class name is required to avoid ambiguity with
* OutputFormatter.OutputType.)
*/
private static Element createValueElement(Document doc, Type<?> type, Iterable<Object> values) {
// "Import static" with method scope:
Type<?> FILESET_ENTRY = BuildType.FILESET_ENTRY;
Type<?> LABEL_LIST = BuildType.LABEL_LIST;
Type<?> LICENSE = BuildType.LICENSE;
Type<?> STRING_LIST = Type.STRING_LIST;
final Element elem;
final boolean hasMultipleValues = Iterables.size(values) > 1;
Type<?> elemType = type.getListElementType();
if (elemType != null) {
// it's a list (includes "distribs")
elem = doc.createElement("list");
for (Object value : values) {
for (Object elemValue : (Collection<?>) value) {
elem.appendChild(createValueElement(doc, elemType, elemValue));
}
}
} else if (type instanceof Type.DictType) {
Set<Object> visitedValues = new HashSet<>();
elem = doc.createElement("dict");
Type.DictType<?, ?> dictType = (Type.DictType<?, ?>) type;
for (Object value : values) {
for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
if (visitedValues.add(entry.getKey())) {
Element pairElem = doc.createElement("pair");
elem.appendChild(pairElem);
pairElem.appendChild(createValueElement(doc, dictType.getKeyType(), entry.getKey()));
pairElem.appendChild(createValueElement(doc, dictType.getValueType(), entry.getValue()));
}
}
}
} else if (type == LICENSE) {
elem = createSingleValueElement(doc, "license", hasMultipleValues);
if (!hasMultipleValues) {
License license = (License) Iterables.getOnlyElement(values);
Element exceptions = createValueElement(doc, LABEL_LIST, license.getExceptions());
exceptions.setAttribute("name", "exceptions");
elem.appendChild(exceptions);
Element licenseTypes = createValueElement(doc, STRING_LIST, license.getLicenseTypes());
licenseTypes.setAttribute("name", "license-types");
elem.appendChild(licenseTypes);
}
} else if (type == FILESET_ENTRY) {
// Fileset entries: not configurable.
FilesetEntry filesetEntry = (FilesetEntry) Iterables.getOnlyElement(values);
elem = doc.createElement("fileset-entry");
elem.setAttribute("srcdir", filesetEntry.getSrcLabel().toString());
elem.setAttribute("destdir", filesetEntry.getDestDir().toString());
elem.setAttribute("symlinks", filesetEntry.getSymlinkBehavior().toString());
elem.setAttribute("strip_prefix", filesetEntry.getStripPrefix());
if (filesetEntry.getExcludes() != null) {
Element excludes = createValueElement(doc, LABEL_LIST, filesetEntry.getExcludes());
excludes.setAttribute("name", "excludes");
elem.appendChild(excludes);
}
if (filesetEntry.getFiles() != null) {
Element files = createValueElement(doc, LABEL_LIST, filesetEntry.getFiles());
files.setAttribute("name", "files");
elem.appendChild(files);
}
} else {
// INTEGER STRING LABEL DISTRIBUTION OUTPUT
elem = createSingleValueElement(doc, type.toString(), hasMultipleValues);
if (!hasMultipleValues && !Iterables.isEmpty(values)) {
Object value = Iterables.getOnlyElement(values);
// Values such as those of attribute "linkstamp" may be null.
if (value != null) {
try {
elem.setAttribute("value", value.toString());
} catch (DOMException e) {
elem.setAttribute("value", "[[[ERROR: could not be encoded as XML]]]");
}
}
}
}
return elem;
}
use of com.google.devtools.build.lib.packages.License in project bazel by bazelbuild.
the class LicensingTests method getTransitiveLicenses.
/**
* Gets the licenses of all targets that are in the transitive closure of a
* target. The result includes this target itself.
*
* @return a map from target labels to licenses
*/
protected static Map<Label, License> getTransitiveLicenses(ConfiguredTarget target) {
final ImmutableMap.Builder<Label, License> result = ImmutableMap.builder();
LicensesProvider provider = target.getProvider(LicensesProvider.class);
if (provider != null) {
for (TargetLicense targetLicense : provider.getTransitiveLicenses()) {
result.put(targetLicense.getLabel(), targetLicense.getLicense());
}
}
return result.build();
}
use of com.google.devtools.build.lib.packages.License in project bazel by bazelbuild.
the class LicensingTests method testDualLicensedUsesLeastRestrictive.
@Test
public void testDualLicensedUsesLeastRestrictive() throws Exception {
scratch.file("user/BUILD", "sh_binary(name = 'user',", " srcs = ['user.sh'],", " deps = ['//used'])");
scratch.file("used/BUILD", "licenses(['restricted', 'permissive'])", "sh_library(name = 'used',", " srcs=['used.sh'])");
ConfiguredTarget used = getConfiguredTarget("//used");
Map<Label, License> usedActual = Maps.filterKeys(getTransitiveLicenses(used), AnalysisMock.get().ccSupport().labelFilter());
Label usedLabel = Label.parseAbsolute("//used");
License license = usedActual.get(usedLabel);
license.checkCompatibility(EnumSet.of(DistributionType.CLIENT), getTarget("//user"), usedLabel, reporter, false);
assertNoEvents();
}
use of com.google.devtools.build.lib.packages.License in project bazel by bazelbuild.
the class LicensingTests method testCollectLicenses.
@Test
public void testCollectLicenses() throws Exception {
Predicate<Label> SOURCE_FILTER = new Predicate<Label>() {
@Override
public boolean apply(Label label) {
// Remove source files from the results.
return AnalysisMock.get().ccSupport().labelFilter().apply(label) && !label.toString().endsWith(".cc");
}
};
scratch.file("a/BUILD", "licenses(['restricted', 'reciprocal'])", "cc_library(name = 'alib', srcs=['a.cc'], deps=[':blib'])", "cc_library(name = 'blib', srcs=['b.cc'], distribs=['web'], " + "deps=['//kaboom:boom', '//kablam:blam'])", "exports_files(['c'])");
scratch.file("kaboom/BUILD", "licenses(['notice'])", "cc_library(name = 'boom', srcs=['bang.cc'])");
scratch.file("kablam/BUILD", "licenses(['restricted','exception=//a:alib'])", "cc_library(name='blam', srcs=['blam.cc'])");
ConfiguredTarget aTarget = getConfiguredTarget("//a:alib");
ConfiguredTarget bTarget = getConfiguredTarget("//a:blib");
ConfiguredTarget cTarget = getConfiguredTarget("//a:c");
ConfiguredTarget kaboomTarget = getConfiguredTarget("//kaboom:boom");
ConfiguredTarget kablamTarget = getConfiguredTarget("//kablam:blam");
Map<Label, License> aMap = Maps.filterKeys(getTransitiveLicenses(aTarget), SOURCE_FILTER);
Map<Label, License> aExpected = licenses("//a:alib", "restricted,reciprocal", "//a:blib", "restricted,reciprocal", "//kablam:blam", "restricted,exception=//a:alib", "//kaboom:boom", "notice");
assertSameMapEntries(aExpected, aMap);
Map<Label, License> bMap = Maps.filterKeys(getTransitiveLicenses(bTarget), SOURCE_FILTER);
Map<Label, License> bExpected = licenses("//a:blib", "restricted,reciprocal", "//kablam:blam", "restricted,exception=//a:alib", "//kaboom:boom", "notice");
assertSameMapEntries(bExpected, bMap);
Map<Label, License> kaboomMap = Maps.filterKeys(getTransitiveLicenses(kaboomTarget), SOURCE_FILTER);
assertSameMapEntries(licenses("//kaboom:boom", "notice"), kaboomMap);
Map<Label, License> kablamMap = Maps.filterKeys(getTransitiveLicenses(kablamTarget), SOURCE_FILTER);
assertSameMapEntries(licenses("//kablam:blam", "restricted,exception=//a:alib"), kablamMap);
License cLicense = getTarget(cTarget.getLabel()).getLicense();
assertEquals(License.parseLicense(Arrays.asList("restricted", "reciprocal")), cLicense);
}
use of com.google.devtools.build.lib.packages.License in project bazel by bazelbuild.
the class LicensingTests method testJavaToolchainOutputLicense.
@Test
public void testJavaToolchainOutputLicense() throws Exception {
scratch.file("java/a/BUILD", "java_toolchain(", " name = 'toolchain',", " licenses = ['restricted'],", " output_licenses = ['unencumbered'],", " encoding = 'UTF-8',", " source_version = '8',", " target_version = '8',", " bootclasspath = [':bcp'],", " extclasspath = [':ecp'],", " javac = [':langtools'],", " javabuilder = ['javabuilder'],", " header_compiler = ['header_compiler'],", " singlejar = ['singlejar'],", " genclass = ['genclass'],", " ijar = ['ijar'])", "java_binary(", " name = 'a',", " srcs = ['a.java'])");
useConfiguration("--java_toolchain=//java/a:toolchain", "--check_licenses");
ConfiguredTarget bin = getConfiguredTarget("//java/a:a");
Map<Label, License> licenses = getTransitiveLicenses(bin);
License toolchainLicense = licenses.get(Label.parseAbsoluteUnchecked("//java/a:toolchain"));
assertThat(toolchainLicense).isNotEqualTo(License.parseLicense(ImmutableList.of("restricted")));
assertThat(toolchainLicense).isEqualTo(License.parseLicense(ImmutableList.of("unencumbered")));
}
Aggregations