use of com.google.devtools.build.lib.packages.License in project bazel by bazelbuild.
the class LicensingTests method licenses.
/**
* Takes a list of strings, which alternate labels with license lists, and
* returns a map of labels to licenses. The license strings are comma-separated
* strings.
*/
protected static Map<Label, License> licenses(String... strings) throws LicenseParsingException, LabelSyntaxException {
Map<Label, License> result = new HashMap<>();
for (int i = 0; i < strings.length; i += 2) {
String labelStr = strings[i];
String licStr = strings[i + 1];
Label label = Label.parseAbsolute(labelStr);
List<String> splitLicenses = licStr.isEmpty() ? Arrays.<String>asList() : Arrays.asList(licStr.split(","));
License license = License.parseLicense(splitLicenses);
result.put(label, license);
}
return result;
}
use of com.google.devtools.build.lib.packages.License in project bazel by bazelbuild.
the class LicensingTests method testLicenseParsing.
@Test
public void testLicenseParsing() throws Exception {
License l1 = License.parseLicense(Arrays.asList("restricted", "exception=//a:a"));
License l2 = License.parseLicense(Arrays.asList("restricted", "reciprocal", "exception=//a:a"));
License l3 = License.parseLicense(Arrays.asList("by_exception_only", "reciprocal", "exception=//a:a", "exception=//b:b"));
assertEquals("[restricted] with exceptions [//a:a]", l1.toString());
assertEquals("[restricted, reciprocal] with exceptions [//a:a]", l2.toString());
assertEquals("[by_exception_only, reciprocal] with exceptions [//a:a, //b:b]", l3.toString());
}
use of com.google.devtools.build.lib.packages.License in project bazel by bazelbuild.
the class BuildTool method validateLicensingForTargets.
/**
* Takes a set of configured targets, and checks if the distribution methods
* declared for the targets are compatible with the constraints imposed by
* their prerequisites' licenses.
*
* @param configuredTargets the targets to check
* @param keepGoing if false, and a licensing error is encountered, both
* generates an error message on the reporter, <em>and</em> throws an
* exception. If true, then just generates a message on the reporter.
* @throws ViewCreationFailedException if the license checking failed (and not
* --keep_going)
*/
private void validateLicensingForTargets(Iterable<ConfiguredTarget> configuredTargets, boolean keepGoing) throws ViewCreationFailedException {
for (ConfiguredTarget configuredTarget : configuredTargets) {
final Target target = configuredTarget.getTarget();
if (TargetUtils.isTestRule(target)) {
// Tests are exempt from license checking
continue;
}
final Set<DistributionType> distribs = target.getDistributions();
StaticallyLinkedMarkerProvider markerProvider = configuredTarget.getProvider(StaticallyLinkedMarkerProvider.class);
boolean staticallyLinked = markerProvider != null && markerProvider.isLinkedStatically();
LicensesProvider provider = configuredTarget.getProvider(LicensesProvider.class);
if (provider != null) {
NestedSet<TargetLicense> licenses = provider.getTransitiveLicenses();
for (TargetLicense targetLicense : licenses) {
if (!targetLicense.getLicense().checkCompatibility(distribs, target, targetLicense.getLabel(), getReporter(), staticallyLinked)) {
if (!keepGoing) {
throw new ViewCreationFailedException("Build aborted due to licensing error");
}
}
}
} else if (configuredTarget.getTarget() instanceof InputFile) {
// Input file targets do not provide licenses because they do not
// depend on the rule where their license is taken from. This is usually
// not a problem, because the transitive collection of licenses always
// hits the rule they come from, except when the input file is a
// top-level target. Thus, we need to handle that case specially here.
//
// See FileTarget#getLicense for more information about the handling of
// license issues with File targets.
License license = configuredTarget.getTarget().getLicense();
if (!license.checkCompatibility(distribs, target, configuredTarget.getLabel(), getReporter(), staticallyLinked)) {
if (!keepGoing) {
throw new ViewCreationFailedException("Build aborted due to licensing error");
}
}
}
}
}
use of com.google.devtools.build.lib.packages.License in project bazel by bazelbuild.
the class LicensesProviderImpl method of.
/**
* Create the appropriate {@link LicensesProvider} for a rule based on its {@code RuleContext}
*/
public static LicensesProvider of(RuleContext ruleContext) {
if (!ruleContext.getConfiguration().checkLicenses()) {
return EMPTY;
}
NestedSetBuilder<TargetLicense> builder = NestedSetBuilder.linkOrder();
BuildConfiguration configuration = ruleContext.getConfiguration();
Rule rule = ruleContext.getRule();
AttributeMap attributes = ruleContext.attributes();
License toolOutputLicense = rule.getToolOutputLicense(attributes);
TargetLicense outputLicenses = toolOutputLicense == null ? null : new TargetLicense(rule.getLabel(), toolOutputLicense);
if (configuration.isHostConfiguration() && toolOutputLicense != null) {
if (toolOutputLicense != License.NO_LICENSE) {
builder.add(outputLicenses);
}
} else {
if (rule.getLicense() != License.NO_LICENSE) {
builder.add(new TargetLicense(rule.getLabel(), rule.getLicense()));
}
ListMultimap<String, ? extends TransitiveInfoCollection> configuredMap = ruleContext.getConfiguredTargetMap();
for (String depAttrName : attributes.getAttributeNames()) {
// Only add the transitive licenses for the attributes that do not have the output_licenses.
Attribute attribute = attributes.getAttributeDefinition(depAttrName);
for (TransitiveInfoCollection dep : configuredMap.get(depAttrName)) {
LicensesProvider provider = dep.getProvider(LicensesProvider.class);
if (provider == null) {
continue;
}
if (useOutputLicenses(attribute, configuration) && provider.hasOutputLicenses()) {
builder.add(provider.getOutputLicenses());
} else {
builder.addTransitive(provider.getTransitiveLicenses());
}
}
}
}
return new LicensesProviderImpl(builder.build(), outputLicenses);
}
use of com.google.devtools.build.lib.packages.License in project bazel by bazelbuild.
the class BazelLicensingTests method testJavaPluginAllowsOutputLicenseDeclaration.
@Test
public void testJavaPluginAllowsOutputLicenseDeclaration() throws Exception {
scratch.file("ise/BUILD", "licenses(['restricted'])", "java_library(name = 'dependency',", " srcs = ['dependency.java'])", "java_plugin(name = 'plugin',", " deps = [':dependency'],", " srcs = ['plugin.java'],", " output_licenses = ['unencumbered'])");
scratch.file("gsa/BUILD", "licenses(['unencumbered'])", "java_library(name = 'library',", " srcs = ['library.java'],", " plugins = ['//ise:plugin'])");
ConfiguredTarget library = getConfiguredTarget("//gsa:library");
Map<Label, License> actual = Maps.filterKeys(getTransitiveLicenses(library), CC_OR_JAVA_OR_SH_LABEL_FILTER);
Map<Label, License> expected = licenses("//ise:plugin", "unencumbered", "//gsa:library", "unencumbered");
assertSameMapEntries(expected, actual);
}
Aggregations