blob: 59393cbd0add4d5fc302dc642abd9f78625d8bf3 [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"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080023#include "trace/TraceBuffer.h"
Winson3c918b82019-01-25 14:25:37 -080024
25using android::ConfigDescription;
26
27namespace aapt {
28
29namespace {
30
31void 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()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000053 context->GetDiagnostics()->Note(android::DiagMessage(value->value->GetSource())
54 << "excluded resource \"" << entry->name
55 << "\" with config " << config.toString());
Winson3c918b82019-01-25 14:25:37 -080056 }
57 value->value = {};
58 return;
59 }
60 }
61}
62
63} // namespace
64
65bool ResourceExcluder::Consume(IAaptContext* context, ResourceTable* table) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -080066 TRACE_NAME("ResourceExcluder::Consume");
Winson3c918b82019-01-25 14:25:37 -080067 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