Native aspects can opt-in to apply to files.

Only works for top-level targets.

RELNOTES: None.
PiperOrigin-RevId: 154176914
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/BuildView.java b/src/main/java/com/google/devtools/build/lib/analysis/BuildView.java
index 5d56cf3..5a45dc6 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/BuildView.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/BuildView.java
@@ -526,9 +526,12 @@
       } else {
         final NativeAspectClass aspectFactoryClass =
             ruleClassProvider.getNativeAspectClassMap().get(aspect);
+
         if (aspectFactoryClass != null) {
+          AspectParameters aspectParameters = AspectParameters.EMPTY;
+          boolean applyToFiles = aspectFactoryClass.getDefinition(aspectParameters).applyToFiles();
           for (TargetAndConfiguration targetSpec : topLevelTargetsWithConfigs) {
-            if (!(targetSpec.getTarget() instanceof Rule)) {
+            if (!applyToFiles && !(targetSpec.getTarget() instanceof Rule)) {
               continue;
             }
             // For invoking top-level aspects, use the top-level configuration for both the
@@ -538,7 +541,7 @@
                 AspectValue.createAspectKey(
                     targetSpec.getLabel(),
                     configuration,
-                    new AspectDescriptor(aspectFactoryClass, AspectParameters.EMPTY),
+                    new AspectDescriptor(aspectFactoryClass, aspectParameters),
                     configuration
                 ));
           }
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredAspect.java b/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredAspect.java
index f649728..6b7c031 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredAspect.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredAspect.java
@@ -103,6 +103,11 @@
     return new ConfiguredAspect(descriptor, TransitiveInfoProviderMap.of());
   }
 
+  public static Builder builder(
+      AspectClass aspectClass, AspectParameters parameters, RuleContext ruleContext) {
+    return new Builder(aspectClass, parameters, ruleContext);
+  }
+
   /**
    * Builder for {@link ConfiguredAspect}.
    */
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredTarget.java b/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredTarget.java
index e341991..5605f95 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredTarget.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredTarget.java
@@ -36,11 +36,6 @@
   String LABEL_FIELD = "label";
 
   /**
-   *  All <code>ConfiguredTarget</code>s have a "aspect_ids" field.
-   */
-  String ASPECTS_FIELD = "aspect_ids";
-
-  /**
    *  All <code>ConfiguredTarget</code>s have a "files" field.
    */
   String FILES_FIELD = "files";
diff --git a/src/main/java/com/google/devtools/build/lib/packages/AspectDefinition.java b/src/main/java/com/google/devtools/build/lib/packages/AspectDefinition.java
index 118b2be..1dbb131 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/AspectDefinition.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/AspectDefinition.java
@@ -68,6 +68,7 @@
    */
   @Nullable private final ImmutableSet<String> restrictToAttributes;
   @Nullable private final ConfigurationFragmentPolicy configurationFragmentPolicy;
+  private final boolean applyToFiles;
 
   public AdvertisedProviderSet getAdvertisedProviders() {
     return advertisedProviders;
@@ -81,7 +82,8 @@
       RequiredProviders requiredAspectProviders,
       ImmutableMap<String, Attribute> attributes,
       @Nullable ImmutableSet<String> restrictToAttributes,
-      @Nullable ConfigurationFragmentPolicy configurationFragmentPolicy) {
+      @Nullable ConfigurationFragmentPolicy configurationFragmentPolicy,
+      boolean applyToFiles) {
     this.aspectClass = aspectClass;
     this.advertisedProviders = advertisedProviders;
     this.requiredProviders = requiredProviders;
@@ -90,6 +92,7 @@
     this.attributes = attributes;
     this.restrictToAttributes = restrictToAttributes;
     this.configurationFragmentPolicy = configurationFragmentPolicy;
+    this.applyToFiles = applyToFiles;
   }
 
   public String getName() {
@@ -147,6 +150,15 @@
   }
 
   /**
+   * Returns whether this aspect applies to files.
+   *
+   * Currently only supported for top-level aspects and targets.
+   */
+  public boolean applyToFiles() {
+    return applyToFiles;
+  }
+
+  /**
    * Returns the attribute -&gt; set of labels that are provided by aspects of attribute.
    */
   public static ImmutableMultimap<Attribute, Label> visitAspectsIfRequired(
@@ -243,6 +255,7 @@
     private LinkedHashSet<String> propagateAlongAttributes = new LinkedHashSet<>();
     private final ConfigurationFragmentPolicy.Builder configurationFragmentPolicy =
         new ConfigurationFragmentPolicy.Builder();
+    private boolean applyToFiles = false;
 
     public Builder(AspectClass aspectClass) {
       this.aspectClass = aspectClass;
@@ -432,6 +445,17 @@
       return this;
     }
 
+    /**
+     * Sets whether this aspect should apply to files.
+     *
+     * Default is <code>false</code>.
+     * Currently only supported for top-level aspects and targets.
+     */
+    public Builder applyToFiles(boolean propagateOverGeneratedFiles) {
+      this.applyToFiles = propagateOverGeneratedFiles;
+      return this;
+    }
+
 
     /**
      * Builds the aspect definition.
@@ -447,7 +471,8 @@
           propagateAlongAttributes == null
               ? null
               : ImmutableSet.copyOf(propagateAlongAttributes),
-          configurationFragmentPolicy.build());
+          configurationFragmentPolicy.build(),
+          applyToFiles);
     }
   }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/AspectFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/AspectFunction.java
index d00c48e..5a2f964 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/AspectFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/AspectFunction.java
@@ -41,7 +41,6 @@
 import com.google.devtools.build.lib.packages.Attribute;
 import com.google.devtools.build.lib.packages.BuildFileContainsErrorsException;
 import com.google.devtools.build.lib.packages.NativeAspectClass;
-import com.google.devtools.build.lib.packages.NoSuchTargetException;
 import com.google.devtools.build.lib.packages.NoSuchThingException;
 import com.google.devtools.build.lib.packages.Package;
 import com.google.devtools.build.lib.packages.Rule;
@@ -180,21 +179,6 @@
           new BuildFileContainsErrorsException(key.getLabel().getPackageIdentifier()));
     }
 
-    Target target;
-    try {
-      target = pkg.getTarget(key.getLabel().getName());
-    } catch (NoSuchTargetException e) {
-      throw new AspectFunctionException(e);
-    }
-
-    if (!(target instanceof Rule)) {
-      env.getListener().handle(Event.error(
-          target.getLocation(),
-          String.format("%s is attached to %s %s but aspects must be attached to rules",
-              aspect.getAspectClass().getName(), target.getTargetKind(), target.getName())));
-      throw new AspectFunctionException(new AspectCreationException(
-          "aspects must be attached to rules"));
-    }
 
     ConfiguredTargetValue configuredTargetValue;
     try {
@@ -218,13 +202,24 @@
       return null;
     }
 
+    ConfiguredTarget associatedTarget = configuredTargetValue.getConfiguredTarget();
+
+    Target target = associatedTarget.getTarget();
+    if (!aspect.getDefinition().applyToFiles() && !(target instanceof Rule)) {
+      env.getListener().handle(Event.error(
+          target.getLocation(),
+          String.format("%s is attached to %s %s but aspects must be attached to rules",
+              aspect.getAspectClass().getName(), target.getTargetKind(), target.getName())));
+      throw new AspectFunctionException(new AspectCreationException(
+          "aspects must be attached to rules"));
+    }
+
+
     if (configuredTargetValue.getConfiguredTarget().getProvider(AliasProvider.class) != null) {
       return createAliasAspect(env, target, aspect, key,
           configuredTargetValue.getConfiguredTarget());
     }
 
-    ConfiguredTarget associatedTarget =
-      configuredTargetValue.getConfiguredTarget();
 
     ImmutableList.Builder<Aspect> aspectPathBuilder = ImmutableList.builder();