blob: 2555995dfc8e7aac9457498e4682ecc6d1cf1904 [file] [log] [blame]
Winson3c918b82019-01-25 14:25:37 -08001/*
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
24using android::ConfigDescription;
25
26namespace aapt {
27
28namespace {
29
30void RemoveIfExcluded(std::set<std::pair<ConfigDescription, int>>& excluded_configs_,
31 IAaptContext* context,
32 ResourceEntry* entry,
33 ResourceConfigValue* value) {
34 const ConfigDescription& config = value->config;
35
36 // If this entry is a default, ignore
37 if (config == ConfigDescription::DefaultConfig()) {
38 return;
39 }
40
41 for (auto& excluded_pair : excluded_configs_) {
42
43 const ConfigDescription& excluded_config = excluded_pair.first;
44 const int& excluded_diff = excluded_pair.second;
45
46 // Check whether config contains all flags in excluded config
47 int node_diff = config.diff(excluded_config);
48 int masked_diff = excluded_diff & node_diff;
49
50 if (masked_diff == 0) {
51 if (context->IsVerbose()) {
52 context->GetDiagnostics()->Note(
53 DiagMessage(value->value->GetSource())
54 << "excluded resource \""
55 << entry->name
56 << "\" with config "
57 << config.toString());
58 }
59 value->value = {};
60 return;
61 }
62 }
63}
64
65} // namespace
66
67bool ResourceExcluder::Consume(IAaptContext* context, ResourceTable* table) {
68 for (auto& package : table->packages) {
69 for (auto& type : package->types) {
70 for (auto& entry : type->entries) {
71 for (auto& value : entry->values) {
72 RemoveIfExcluded(excluded_configs_, context, entry.get(), value.get());
73 }
74
75 // Erase the values that were removed.
76 entry->values.erase(
77 std::remove_if(
78 entry->values.begin(), entry->values.end(),
79 [](const std::unique_ptr<ResourceConfigValue>& val) -> bool {
80 return val == nullptr || val->value == nullptr;
81 }),
82 entry->values.end());
83 }
84 }
85 }
86 return true;
87}
88
89} // namespace aapt