1/*
2 * Copyright (C) 2017 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 "AbiFilter.h"
18
19#include <memory>
20
21#include "io/Util.h"
22
23namespace aapt {
24
25std::unique_ptr<AbiFilter> AbiFilter::FromAbiList(const std::vector<configuration::Abi>& abi_list) {
26  std::unordered_set<std::string> abi_set;
27  for (auto& abi : abi_list) {
28    abi_set.insert(configuration::AbiToString(abi).to_string());
29  }
30  // Make unique by hand as the constructor is private.
31  return std::unique_ptr<AbiFilter>(new AbiFilter(abi_set));
32}
33
34bool AbiFilter::Keep(const std::string& path) {
35  // We only care about libraries.
36  if (!util::StartsWith(path, kLibPrefix)) {
37    return true;
38  }
39
40  auto abi_end = path.find('/', kLibPrefixLen);
41  if (abi_end == std::string::npos) {
42    // Ignore any files in the top level lib directory.
43    return true;
44  }
45
46  // Strip the lib/ prefix.
47  const std::string& path_abi = path.substr(kLibPrefixLen, abi_end - kLibPrefixLen);
48  return (abis_.find(path_abi) != abis_.end());
49}
50
51}  // namespace aapt
52