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/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);
     }
   }
 }