1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "gn/inherited_libraries.h"
6
7 #include "gn/target.h"
8
9 InheritedLibraries::InheritedLibraries() = default;
10
11 InheritedLibraries::~InheritedLibraries() = default;
12
GetOrdered() const13 std::vector<const Target*> InheritedLibraries::GetOrdered() const {
14 std::vector<const Target*> result;
15 result.resize(map_.size());
16
17 // The indices in the map should be from 0 to the number of items in the
18 // map, so insert directly into the result (with some sanity checks).
19 for (const auto& pair : map_) {
20 size_t index = pair.second.index;
21 DCHECK(index < result.size());
22 DCHECK(!result[index]);
23 result[index] = pair.first;
24 }
25
26 return result;
27 }
28
29 std::vector<std::pair<const Target*, bool>>
GetOrderedAndPublicFlag() const30 InheritedLibraries::GetOrderedAndPublicFlag() const {
31 std::vector<std::pair<const Target*, bool>> result;
32 result.resize(map_.size());
33
34 for (const auto& pair : map_) {
35 size_t index = pair.second.index;
36 DCHECK(index < result.size());
37 DCHECK(!result[index].first);
38 result[index] = std::make_pair(pair.first, pair.second.is_public);
39 }
40
41 return result;
42 }
43
Append(const Target * target,bool is_public)44 void InheritedLibraries::Append(const Target* target, bool is_public) {
45 // Try to insert a new node.
46 auto insert_result =
47 map_.insert(std::make_pair(target, Node(map_.size(), is_public)));
48
49 if (!insert_result.second) {
50 // Element already present, insert failed and insert_result indicates the
51 // old one. The old one may need to have its public flag updated.
52 if (is_public) {
53 Node& existing_node = insert_result.first->second;
54 existing_node.is_public = true;
55 }
56 }
57 }
58
AppendInherited(const InheritedLibraries & other,bool is_public)59 void InheritedLibraries::AppendInherited(const InheritedLibraries& other,
60 bool is_public) {
61 // Append all items in order, mark them public only if the're already public
62 // and we're adding them publically.
63 for (const auto& cur : other.GetOrderedAndPublicFlag())
64 Append(cur.first, is_public && cur.second);
65 }
66
AppendPublicSharedLibraries(const InheritedLibraries & other,bool is_public)67 void InheritedLibraries::AppendPublicSharedLibraries(
68 const InheritedLibraries& other,
69 bool is_public) {
70 for (const auto& cur : other.GetOrderedAndPublicFlag()) {
71 if (cur.first->output_type() == Target::SHARED_LIBRARY && cur.second)
72 Append(cur.first, is_public);
73 }
74 }
75