aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/android/java/com/google/devtools/build/android/AndroidDataMerger.java
blob: 3b39205ad8b15da5334bd3cf3dce3ae4eb203a54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Copyright 2016 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.android;

import com.google.common.base.Joiner;
import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.android.ParsedAndroidData.ParsedAndroidDataBuildingPathWalker;

import com.android.ide.common.res2.MergingException;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributeView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

/**
 * Handles the Merging of ParsedAndroidData.
 */
public class AndroidDataMerger {

  private static final Logger logger = Logger.getLogger(AndroidDataMerger.class.getCanonicalName());

  /** Interface for comparing paths. */
  interface SourceChecker {
    boolean checkEquality(Path one, Path two) throws IOException;
  }

  /** Compares two paths by the contents of the files. */
  static class ContentComparingChecker implements SourceChecker {

    static SourceChecker create() {
      return new ContentComparingChecker();
    }

    @Override
    public boolean checkEquality(Path one, Path two) throws IOException {
      // TODO(corysmith): Is there a filesystem hash we can use?
      if (getFileSize(one) != getFileSize(two)) {
        return false;
      }
      try (final InputStream oneStream = new BufferedInputStream(Files.newInputStream(one));
          final InputStream twoStream = new BufferedInputStream(Files.newInputStream(two))) {
        int bytesRead = 0;
        while (true) {
          int oneByte = oneStream.read();
          int twoByte = twoStream.read();
          bytesRead++;
          if (oneByte == -1 || twoByte == -1) {
            if (oneByte == twoByte) {
              return true;
            } else {
              // getFileSize did not return correct size.
              logger.severe(
                  String.format(
                      "Filesystem size of %s (%s) or %s (%s) is inconsistant with bytes read %s.",
                      one,
                      getFileSize(one),
                      two,
                      getFileSize(two),
                      bytesRead));
              return false;
            }
          }
          if (oneByte != twoByte) {
            return false;
          }
        }
      }
    }

    private long getFileSize(Path path) throws IOException {
      return Files.getFileAttributeView(path, BasicFileAttributeView.class).readAttributes().size();
    }
  }

  static class NoopSourceChecker implements SourceChecker {
    static SourceChecker create() {
      return new NoopSourceChecker();
    }

    @Override
    public boolean checkEquality(Path one, Path two) {
      return false;
    }
  }

  private final SourceChecker deDuplicator;

  /** Creates a merger with no path deduplication. */
  public static AndroidDataMerger create() {
    return new AndroidDataMerger(NoopSourceChecker.create());
  }

  /** Creates a merger with a custom deduplicator. */
  public static AndroidDataMerger create(SourceChecker deDuplicator) {
    return new AndroidDataMerger(deDuplicator);
  }

  /** Creates a merger with a file contents hashing deduplicator. */
  public static AndroidDataMerger createWithPathDeduplictor() {
    return create(ContentComparingChecker.create());
  }

  private AndroidDataMerger(SourceChecker deDuplicator) {
    this.deDuplicator = deDuplicator;
  }

  /**
   * Merges a list of {@link DependencyAndroidData} with a {@link UnvalidatedAndroidData}.
   *
   * @see AndroidDataMerger#merge(ParsedAndroidData, ParsedAndroidData, UnvalidatedAndroidData,
   * boolean) for details.
   */
  UnwrittenMergedAndroidData merge(
      List<DependencyAndroidData> transitive,
      List<DependencyAndroidData> direct,
      UnvalidatedAndroidData primary,
      boolean allowPrimaryOverrideAll)
      throws IOException, MergingException {
    Stopwatch timer = Stopwatch.createStarted();
    try {
      final ParsedAndroidData.Builder directBuilder = ParsedAndroidData.Builder.newBuilder();
      final ParsedAndroidData.Builder transitiveBuilder = ParsedAndroidData.Builder.newBuilder();
      final AndroidDataSerializer serializer = AndroidDataSerializer.create();
      for (final DependencyAndroidData dependency : direct) {
        parseDependencyData(directBuilder, serializer, dependency);
      }
      for (final DependencyAndroidData dependency : transitive) {
        parseDependencyData(transitiveBuilder, serializer, dependency);
      }
      logger.fine(
          String.format("Merged dependencies read in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
      timer.reset().start();
      return merge(
          transitiveBuilder.build(), directBuilder.build(), primary, allowPrimaryOverrideAll);
    } finally {
      logger.fine(String.format("Resources merged in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
    }
  }

  private void parseDependencyData(
      final ParsedAndroidData.Builder parsedDataBuilder,
      final AndroidDataSerializer serializer,
      final DependencyAndroidData dependency)
      throws IOException, MergingException {
    try {
      dependency.deserialize(serializer, parsedDataBuilder.consumers());
    } catch (DeserializationException e) {
      if (!e.isLegacy()) {
        throw new MergingException(e);
      }
      //TODO(corysmith): List the offending a target here.
      logger.warning(
          String.format(
              "\u001B[31mDEPRECATION:\u001B[0m Legacy resources used for %s",
              dependency.getManifest()));
      // Legacy android resources -- treat them as direct dependencies.
      dependency.walk(ParsedAndroidDataBuildingPathWalker.create(parsedDataBuilder));
    }
  }

  /**
   * Merges DataResources into an UnwrittenMergedAndroidData.
   * <p>
   * This method has two basic states, library and binary. These are distinguished by
   * allowPrimaryOverrideAll, which allows the primary data to overwrite any value in the closure, a
   * trait associated with binaries, as a binary is a leaf node. The other semantics are slightly
   * more complicated: a given resource can be overwritten only if it resides in the direct
   * dependencies of primary data. This forces an explicit simple priority for each resource,
   * instead of the more subtle semantics of multiple layers of libraries with potential overwrites.
   * <p>
   * The UnwrittenMergedAndroidData contains only one of each DataKey in both the direct and
   * transitive closure.
   *
   * The merge semantics are as follows: <pre>
   *   Key:
   *     A(): package A
   *     A(foo): package A with resource symbol foo
   *     A() -> B(): a dependency relationship of B.deps = [:A]
   *     A(),B() -> C(): a dependency relationship of C.deps = [:A,:B]
   *
   *   For android library (allowPrimaryOverrideAll = False)
   *
   *     A() -> B(foo) -> C(foo) == Valid
   *     A() -> B() -> C(foo) == Valid
   *     A() -> B() -> C(foo),D(foo) == Conflict
   *     A(foo) -> B(foo) -> C() == Conflict
   *     A(foo) -> B() -> C(foo) == Conflict
   *     A(foo),B(foo) -> C() -> D() == Conflict
   *     A() -> B(foo),C(foo) -> D() == Conflict
   *     A(foo),B(foo) -> C() -> D(foo) == Conflict
   *     A() -> B(foo),C(foo) -> D(foo) == Conflict
   *
   *   For android binary (allowPrimaryOverrideAll = True)
   *
   *     A() -> B(foo) -> C(foo) == Valid
   *     A() -> B() -> C(foo) == Valid
   *     A() -> B() -> C(foo),D(foo) == Conflict
   *     A(foo) -> B(foo) -> C() == Conflict
   *     A(foo) -> B() -> C(foo) == Valid
   *     A(foo),B(foo) -> C() -> D() == Conflict
   *     A() -> B(foo),C(foo) -> D() == Conflict
   *     A(foo),B(foo) -> C() -> D(foo) == Valid
   *     A() -> B(foo),C(foo) -> D(foo) == Valid
   * </pre>
   *
   * @param transitive The transitive dependencies to merge.
   * @param direct The direct dependencies to merge.
   * @param primaryData The primary data to merge against.
   * @param allowPrimaryOverrideAll Boolean that indicates if the primary data will be considered
   *    the ultimate source of truth, provided it doesn't conflict with itself.
   * @return An UnwrittenMergedAndroidData, containing DataResource objects that can be written to
   *    disk for aapt processing or serialized for future merge passes.
   * @throws MergingException if there are merge conflicts or issues with parsing resources from
   *    Primary.
   */
  UnwrittenMergedAndroidData merge(
      ParsedAndroidData transitive,
      ParsedAndroidData direct,
      UnvalidatedAndroidData primaryData,
      boolean allowPrimaryOverrideAll)
      throws MergingException {

    try {
      // Extract the primary resources.
      ParsedAndroidData primary = ParsedAndroidData.from(primaryData);

      Map<DataKey, DataResource> overwritableDeps = new HashMap<>();
      Map<DataKey, DataAsset> assets = new HashMap<>();

      Set<MergeConflict> conflicts = new HashSet<>();
      conflicts.addAll(primary.conflicts());
      for (MergeConflict conflict : Iterables.concat(direct.conflicts(), transitive.conflicts())) {
        if (allowPrimaryOverrideAll
            && (primary.containsOverwritable(conflict.dataKey())
                || primary.containsAsset(conflict.dataKey()))) {
          continue;
        }
        conflicts.add(conflict);
      }

      // resources
      for (Map.Entry<DataKey, DataResource> entry : direct.iterateOverwritableEntries()) {
        // Direct dependencies are simply overwritten, no conflict.
        if (!primary.containsOverwritable(entry.getKey())) {
          overwritableDeps.put(entry.getKey(), entry.getValue());
        }
      }
      for (Map.Entry<DataKey, DataResource> entry : transitive.iterateOverwritableEntries()) {
        // If the primary is considered to be intentional (usually at the binary level),
        // skip.
        if (primary.containsOverwritable(entry.getKey()) && allowPrimaryOverrideAll) {
          continue;
        }
        // If a transitive value is in the direct map report a conflict, as it is commonly
        // unintentional.
        if (direct.containsOverwritable(entry.getKey())) {
          conflicts.add(direct.foundResourceConflict(entry.getKey(), entry.getValue()));
        } else if (primary.containsOverwritable(entry.getKey())) {
          // If overwriting a transitive value with a primary map, assume it's an unintentional
          // override, unless allowPrimaryOverrideAll is set. At which point, this code path
          // should not be reached.
          conflicts.add(primary.foundResourceConflict(entry.getKey(), entry.getValue()));
        } else {
          // If it's in none of the of sources, add it.
          overwritableDeps.put(entry.getKey(), entry.getValue());
        }
      }

      // assets
      for (Map.Entry<DataKey, DataAsset> entry : direct.iterateAssetEntries()) {
        // Direct dependencies are simply overwritten, no conflict.
        if (!primary.containsAsset(entry.getKey())) {
          assets.put(entry.getKey(), entry.getValue());
        }
      }
      for (Map.Entry<DataKey, DataAsset> entry : transitive.iterateAssetEntries()) {
        // If the primary is considered to be intentional (usually at the binary level),
        // skip.
        if (primary.containsAsset(entry.getKey()) && allowPrimaryOverrideAll) {
          continue;
        }
        // If a transitive value is in the direct map report a conflict, as it is commonly
        // unintentional.
        if (direct.containsAsset(entry.getKey())) {
          conflicts.add(direct.foundAssetConflict(entry.getKey(), entry.getValue()));
        } else if (primary.containsAsset(entry.getKey())) {
          // If overwriting a transitive value with a primary map, assume it's an unintentional
          // override, unless allowPrimaryOverrideAll is set. At which point, this code path
          // should not be reached.
          conflicts.add(primary.foundAssetConflict(entry.getKey(), entry.getValue()));
        } else {
          // If it's in none of the of sources, add it.
          assets.put(entry.getKey(), entry.getValue());
        }
      }

      if (!conflicts.isEmpty()) {
        List<String> messages = new ArrayList<>();
        for (MergeConflict conflict : conflicts) {
          if (!conflict.first().equals(conflict.second())
              && !deDuplicator.checkEquality(
                  conflict.first().source(), conflict.second().source())) {
            messages.add(conflict.toConflictMessage());
          }
        }
        if (!messages.isEmpty()) {
          // TODO(corysmith): Turn these into errors.
          logger.warning(Joiner.on("").join(messages));
        }
      }

      return UnwrittenMergedAndroidData.of(
          primaryData.getManifest(),
          primary,
          ParsedAndroidData.of(
              ImmutableSet.<MergeConflict>of(),
              ImmutableMap.copyOf(overwritableDeps),
              direct.mergeNonOverwritable(transitive),
              ImmutableMap.copyOf(assets)));
    } catch (IOException e) {
      throw new MergingException(e);
    }
  }
}