Search in sources :

Example 6 with License

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;
}
Also used : HashMap(java.util.HashMap) Label(com.google.devtools.build.lib.cmdline.Label) License(com.google.devtools.build.lib.packages.License) TargetLicense(com.google.devtools.build.lib.analysis.LicensesProvider.TargetLicense)

Example 7 with License

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());
}
Also used : License(com.google.devtools.build.lib.packages.License) TargetLicense(com.google.devtools.build.lib.analysis.LicensesProvider.TargetLicense) Test(org.junit.Test)

Example 8 with License

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");
                }
            }
        }
    }
}
Also used : ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) Target(com.google.devtools.build.lib.packages.Target) ViewCreationFailedException(com.google.devtools.build.lib.analysis.ViewCreationFailedException) TargetLicense(com.google.devtools.build.lib.analysis.LicensesProvider.TargetLicense) TargetLicense(com.google.devtools.build.lib.analysis.LicensesProvider.TargetLicense) License(com.google.devtools.build.lib.packages.License) LicensesProvider(com.google.devtools.build.lib.analysis.LicensesProvider) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) StaticallyLinkedMarkerProvider(com.google.devtools.build.lib.analysis.StaticallyLinkedMarkerProvider) DistributionType(com.google.devtools.build.lib.packages.License.DistributionType) InputFile(com.google.devtools.build.lib.packages.InputFile)

Example 9 with License

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);
}
Also used : BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) AttributeMap(com.google.devtools.build.lib.packages.AttributeMap) Attribute(com.google.devtools.build.lib.packages.Attribute) License(com.google.devtools.build.lib.packages.License) Rule(com.google.devtools.build.lib.packages.Rule)

Example 10 with License

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);
}
Also used : Label(com.google.devtools.build.lib.cmdline.Label) License(com.google.devtools.build.lib.packages.License) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) Test(org.junit.Test)

Aggregations

License (com.google.devtools.build.lib.packages.License)15 TargetLicense (com.google.devtools.build.lib.analysis.LicensesProvider.TargetLicense)12 Label (com.google.devtools.build.lib.cmdline.Label)9 Test (org.junit.Test)9 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)2 LicensesProvider (com.google.devtools.build.lib.analysis.LicensesProvider)2 Predicate (com.google.common.base.Predicate)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 EqualsTester (com.google.common.testing.EqualsTester)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 FileProvider (com.google.devtools.build.lib.analysis.FileProvider)1 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)1 StaticallyLinkedMarkerProvider (com.google.devtools.build.lib.analysis.StaticallyLinkedMarkerProvider)1 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)1 ViewCreationFailedException (com.google.devtools.build.lib.analysis.ViewCreationFailedException)1 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)1 NestedSetBuilder (com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder)1 Attribute (com.google.devtools.build.lib.packages.Attribute)1 AttributeMap (com.google.devtools.build.lib.packages.AttributeMap)1 BuildType (com.google.devtools.build.lib.packages.BuildType)1