Skip to content

Commit

Permalink
Rename default_applicable_licenses to default_package_metadata.
Browse files Browse the repository at this point in the history
Leave default_applicable_licenses as an alias.
Don't allow both to be set.

Step 1 of https://docs.google.com/document/d/1uyJjkKbE8kV8EinakaR9q-Un25zCukhoH_dRBkWHSKQ/edit#

PiperOrigin-RevId: 485705150
Change-Id: I5e0012e37e5bca55ed43f83dd9f26a26f78b543d
  • Loading branch information
aiuto authored and Copybara-Service committed Nov 2, 2022
1 parent 9b420b5 commit ace99b5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.License.DistributionType;
import com.google.devtools.build.lib.server.FailureDetails.PackageLoading.Code;
import java.util.List;
import java.util.Set;
import net.starlark.java.eval.EvalException;
Expand All @@ -30,15 +31,16 @@ private DefaultPackageArguments() {}
/** Returns the default set of {@link PackageArgument}s. */
static ImmutableList<PackageArgument<?>> get() {
return ImmutableList.of(
new DefaultDeprecation(),
new DefaultDistribs(),
new DefaultApplicableLicenses(),
new DefaultLicenses(),
new DefaultTestOnly(),
new DefaultVisibility(),
new Features(),
new DefaultCompatibleWith(),
new DefaultRestrictedTo());
new DefaultDeprecation(),
new DefaultDistribs(),
new DefaultApplicableLicenses(),
new DefaultPackageMetadata(),
new DefaultLicenses(),
new DefaultTestOnly(),
new DefaultVisibility(),
new Features(),
new DefaultCompatibleWith(),
new DefaultRestrictedTo());
}

private static class DefaultVisibility extends PackageArgument<List<Label>> {
Expand Down Expand Up @@ -95,17 +97,48 @@ protected void process(Package.Builder pkgBuilder, Location location,
* specified.
*/
private static class DefaultApplicableLicenses extends PackageArgument<List<Label>> {
private static final String DEFAULT_APPLICABLE_LICENSES_ATTRIBUTE =
"default_applicable_licenses";

private DefaultApplicableLicenses() {
super(DEFAULT_APPLICABLE_LICENSES_ATTRIBUTE, BuildType.LABEL_LIST);
super("default_applicable_licenses", BuildType.LABEL_LIST);
}

@Override
protected void process(Package.Builder pkgBuilder, Location location, List<Label> value) {
if (!pkgBuilder.getDefaultPackageMetadata().isEmpty()) {
pkgBuilder.addEvent(
Package.error(
location,
"Can not set both default_package_metadata and default_applicable_licenses."
+ " Move all declarations to default_package_metadata.",
Code.INVALID_PACKAGE_SPECIFICATION));
}

pkgBuilder.setDefaultPackageMetadata(value, "default_package_metadata", location);
}
}

/**
* Declares the package() attribute specifying the default value for {@link
* com.google.devtools.build.lib.packages.RuleClass#APPLICABLE_LICENSES_ATTR} when not explicitly
* specified.
*/
private static class DefaultPackageMetadata extends PackageArgument<List<Label>> {
private static final String DEFAULT_PACKAGE_METADATA_ATTRIBUTE = "default_package_metadata";

private DefaultPackageMetadata() {
super(DEFAULT_PACKAGE_METADATA_ATTRIBUTE, BuildType.LABEL_LIST);
}

@Override
protected void process(Package.Builder pkgBuilder, Location location, List<Label> value) {
pkgBuilder.setDefaultApplicableLicenses(
value, DEFAULT_APPLICABLE_LICENSES_ATTRIBUTE, location);
if (!pkgBuilder.getDefaultPackageMetadata().isEmpty()) {
pkgBuilder.addEvent(
Package.error(
location,
"Can not set both default_package_metadata and default_applicable_licenses."
+ " Move all declarations to default_package_metadata.",
Code.INVALID_PACKAGE_SPECIFICATION));
}
pkgBuilder.setDefaultPackageMetadata(value, DEFAULT_PACKAGE_METADATA_ATTRIBUTE, location);
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/main/java/com/google/devtools/build/lib/packages/Package.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ public enum ConfigSettingVisibilityPolicy {
/** The list of transitive closure of the Starlark file dependencies. */
private ImmutableList<Label> starlarkFileDependencies;

/** The package's default "applicable_licenses" attribute. */
private Set<Label> defaultApplicableLicenses = ImmutableSet.of();
/** The package's default "package_metadata" attribute. */
private ImmutableSet<Label> defaultPackageMetadata = ImmutableSet.of();

/**
* The package's default "licenses" and "distribs" attributes, as specified
Expand Down Expand Up @@ -480,7 +480,7 @@ private void finishInit(Builder builder) {
this.starlarkFileDependencies = builder.starlarkFileDependencies;
this.defaultLicense = builder.defaultLicense;
this.defaultDistributionSet = builder.defaultDistributionSet;
this.defaultApplicableLicenses = ImmutableSortedSet.copyOf(builder.defaultApplicableLicenses);
this.defaultPackageMetadata = ImmutableSortedSet.copyOf(builder.defaultPackageMetadata);
this.features = ImmutableSortedSet.copyOf(builder.features);
this.registeredExecutionPlatforms = ImmutableList.copyOf(builder.registeredExecutionPlatforms);
this.registeredToolchains = ImmutableList.copyOf(builder.registeredToolchains);
Expand Down Expand Up @@ -799,9 +799,9 @@ public boolean isDefaultVisibilitySet() {
return defaultVisibilitySet;
}

/** Gets the licenses list for the default applicable_licenses declared by this package. */
public Set<Label> getDefaultApplicableLicenses() {
return defaultApplicableLicenses;
/** Gets the package metadata list for the default metadata declared by this package. */
public Set<Label> getDefaultPackageMetadata() {
return defaultPackageMetadata;
}

/** Gets the parsed license object for the default license declared by this package. */
Expand Down Expand Up @@ -1024,7 +1024,7 @@ public boolean recordLoadedModules() {
// serialize events emitted during its construction/evaluation.
@Nullable private FailureDetail failureDetailOverride = null;

private ImmutableList<Label> defaultApplicableLicenses = ImmutableList.of();
private ImmutableList<Label> defaultPackageMetadata = ImmutableList.of();
private License defaultLicense = License.NO_LICENSE;
private Set<License.DistributionType> defaultDistributionSet = License.DEFAULT_DISTRIB;

Expand Down Expand Up @@ -1431,16 +1431,16 @@ public Builder setStarlarkFileDependencies(ImmutableList<Label> starlarkFileDepe
* attribute when not explicitly specified by the rule. Records a package error if any labels
* are duplicated.
*/
void setDefaultApplicableLicenses(List<Label> licenses, String attrName, Location location) {
void setDefaultPackageMetadata(List<Label> licenses, String attrName, Location location) {
if (hasDuplicateLabels(
licenses, "package " + pkg.getName(), attrName, location, this::addEvent)) {
setContainsErrors();
}
this.defaultApplicableLicenses = ImmutableList.copyOf(licenses);
this.defaultPackageMetadata = ImmutableList.copyOf(licenses);
}

ImmutableList<Label> getDefaultApplicableLicenses() {
return defaultApplicableLicenses;
ImmutableList<Label> getDefaultPackageMetadata() {
return defaultPackageMetadata;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2262,8 +2262,7 @@ private void populateDefaultRuleAttributeValues(
if (rule.getRuleClassObject().isPackageMetadataRule()) {
// Do nothing
} else {
rule.setAttributeValue(
attr, pkgBuilder.getDefaultApplicableLicenses(), /*explicit=*/ false);
rule.setAttributeValue(attr, pkgBuilder.getDefaultPackageMetadata(), /*explicit=*/ false);
}

} else if (attr.getName().equals("licenses") && attr.getType() == BuildType.LICENSE) {
Expand Down

0 comments on commit ace99b5

Please sign in to comment.