1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "link/ResourceExcluder.h"
18
19 #include <algorithm>
20
21 #include "DominatorTree.h"
22 #include "ResourceTable.h"
23 #include "trace/TraceBuffer.h"
24
25 using android::ConfigDescription;
26
27 namespace aapt {
28
29 namespace {
30
RemoveIfExcluded(std::set<std::pair<ConfigDescription,int>> & excluded_configs_,IAaptContext * context,ResourceEntry * entry,ResourceConfigValue * value)31 void RemoveIfExcluded(std::set<std::pair<ConfigDescription, int>>& excluded_configs_,
32 IAaptContext* context,
33 ResourceEntry* entry,
34 ResourceConfigValue* value) {
35 const ConfigDescription& config = value->config;
36
37 // If this entry is a default, ignore
38 if (config == ConfigDescription::DefaultConfig()) {
39 return;
40 }
41
42 for (auto& excluded_pair : excluded_configs_) {
43
44 const ConfigDescription& excluded_config = excluded_pair.first;
45 const int& excluded_diff = excluded_pair.second;
46
47 // Check whether config contains all flags in excluded config
48 int node_diff = config.diff(excluded_config);
49 int masked_diff = excluded_diff & node_diff;
50
51 if (masked_diff == 0) {
52 if (context->IsVerbose()) {
53 context->GetDiagnostics()->Note(android::DiagMessage(value->value->GetSource())
54 << "excluded resource \"" << entry->name
55 << "\" with config " << config.toString());
56 }
57 value->value = {};
58 return;
59 }
60 }
61 }
62
63 } // namespace
64
Consume(IAaptContext * context,ResourceTable * table)65 bool ResourceExcluder::Consume(IAaptContext* context, ResourceTable* table) {
66 TRACE_NAME("ResourceExcluder::Consume");
67 for (auto& package : table->packages) {
68 for (auto& type : package->types) {
69 for (auto& entry : type->entries) {
70 for (auto& value : entry->values) {
71 RemoveIfExcluded(excluded_configs_, context, entry.get(), value.get());
72 }
73
74 // Erase the values that were removed.
75 entry->values.erase(
76 std::remove_if(
77 entry->values.begin(), entry->values.end(),
78 [](const std::unique_ptr<ResourceConfigValue>& val) -> bool {
79 return val == nullptr || val->value == nullptr;
80 }),
81 entry->values.end());
82 }
83 }
84 }
85 return true;
86 }
87
88 } // namespace aapt
89