Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 1 | // Copyright 2020 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 19 | "iter" |
| 20 | "slices" |
Yu Liu | 467d7c5 | 2024-09-18 21:54:44 +0000 | [diff] [blame] | 21 | |
| 22 | "github.com/google/blueprint" |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 23 | ) |
| 24 | |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 25 | // DepSet is designed to be conceptually compatible with Bazel's depsets: |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 26 | // https://docs.bazel.build/versions/master/skylark/depsets.html |
| 27 | |
| 28 | type DepSetOrder int |
| 29 | |
| 30 | const ( |
| 31 | PREORDER DepSetOrder = iota |
| 32 | POSTORDER |
| 33 | TOPOLOGICAL |
| 34 | ) |
| 35 | |
| 36 | func (o DepSetOrder) String() string { |
| 37 | switch o { |
| 38 | case PREORDER: |
| 39 | return "PREORDER" |
| 40 | case POSTORDER: |
| 41 | return "POSTORDER" |
| 42 | case TOPOLOGICAL: |
| 43 | return "TOPOLOGICAL" |
| 44 | default: |
| 45 | panic(fmt.Errorf("Invalid DepSetOrder %d", o)) |
| 46 | } |
| 47 | } |
| 48 | |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 49 | type depSettableType comparable |
| 50 | |
| 51 | // A DepSet efficiently stores a slice of an arbitrary type from transitive dependencies without |
| 52 | // copying. It is stored as a DAG of DepSet nodes, each of which has some direct contents and a list |
| 53 | // of dependency DepSet nodes. |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 54 | // |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 55 | // A DepSet has an order that will be used to walk the DAG when ToList() is called. The order |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 56 | // can be POSTORDER, PREORDER, or TOPOLOGICAL. POSTORDER and PREORDER orders return a postordered |
| 57 | // or preordered left to right flattened list. TOPOLOGICAL returns a list that guarantees that |
| 58 | // elements of children are listed after all of their parents (unless there are duplicate direct |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 59 | // elements in the DepSet or any of its transitive dependencies, in which case the ordering of the |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 60 | // duplicated element is not guaranteed). |
| 61 | // |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 62 | // A DepSet is created by NewDepSet or NewDepSetBuilder.Build from the slice for direct contents |
| 63 | // and the *DepSets of dependencies. A DepSet is immutable once created. |
| 64 | type DepSet[T depSettableType] struct { |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 65 | handle *depSet[T] |
| 66 | } |
| 67 | |
| 68 | type depSet[T depSettableType] struct { |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 69 | preorder bool |
| 70 | reverse bool |
| 71 | order DepSetOrder |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 72 | direct []T |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 73 | transitive []DepSet[T] |
| 74 | } |
| 75 | |
| 76 | func (d DepSet[T]) impl() *depSet[T] { |
| 77 | return d.handle |
| 78 | } |
| 79 | |
| 80 | func (d DepSet[T]) order() DepSetOrder { |
| 81 | impl := d.impl() |
| 82 | return impl.order |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Yu Liu | 467d7c5 | 2024-09-18 21:54:44 +0000 | [diff] [blame] | 85 | type depSetGob[T depSettableType] struct { |
| 86 | Preorder bool |
| 87 | Reverse bool |
| 88 | Order DepSetOrder |
| 89 | Direct []T |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 90 | Transitive []DepSet[T] |
Yu Liu | 467d7c5 | 2024-09-18 21:54:44 +0000 | [diff] [blame] | 91 | } |
Yu Liu | 26a716d | 2024-08-30 23:40:32 +0000 | [diff] [blame] | 92 | |
Yu Liu | 467d7c5 | 2024-09-18 21:54:44 +0000 | [diff] [blame] | 93 | func (d *DepSet[T]) ToGob() *depSetGob[T] { |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 94 | impl := d.impl() |
Yu Liu | 467d7c5 | 2024-09-18 21:54:44 +0000 | [diff] [blame] | 95 | return &depSetGob[T]{ |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 96 | Preorder: impl.preorder, |
| 97 | Reverse: impl.reverse, |
| 98 | Order: impl.order, |
| 99 | Direct: impl.direct, |
| 100 | Transitive: impl.transitive, |
Yu Liu | 467d7c5 | 2024-09-18 21:54:44 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | func (d *DepSet[T]) FromGob(data *depSetGob[T]) { |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 105 | d.handle = &depSet[T]{ |
| 106 | preorder: data.Preorder, |
| 107 | reverse: data.Reverse, |
| 108 | order: data.Order, |
| 109 | direct: data.Direct, |
| 110 | transitive: data.Transitive, |
| 111 | } |
Yu Liu | 467d7c5 | 2024-09-18 21:54:44 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | func (d *DepSet[T]) GobEncode() ([]byte, error) { |
| 115 | return blueprint.CustomGobEncode[depSetGob[T]](d) |
Yu Liu | 26a716d | 2024-08-30 23:40:32 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | func (d *DepSet[T]) GobDecode(data []byte) error { |
Yu Liu | 467d7c5 | 2024-09-18 21:54:44 +0000 | [diff] [blame] | 119 | return blueprint.CustomGobDecode[depSetGob[T]](data, d) |
Yu Liu | 26a716d | 2024-08-30 23:40:32 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 122 | // NewDepSet returns an immutable DepSet with the given order, direct and transitive contents. |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 123 | func NewDepSet[T depSettableType](order DepSetOrder, direct []T, transitive []DepSet[T]) DepSet[T] { |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 124 | var directCopy []T |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 125 | var transitiveCopy []DepSet[T] |
| 126 | nonEmptyTransitiveCount := 0 |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 127 | for _, t := range transitive { |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 128 | if t.handle != nil { |
| 129 | if t.order() != order { |
| 130 | panic(fmt.Errorf("incompatible order, new DepSet is %s but transitive DepSet is %s", |
| 131 | order, t.order())) |
| 132 | } |
| 133 | nonEmptyTransitiveCount++ |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 134 | } |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 137 | if nonEmptyTransitiveCount > 0 { |
| 138 | transitiveCopy = make([]DepSet[T], 0, nonEmptyTransitiveCount) |
| 139 | } |
| 140 | var transitiveIter iter.Seq2[int, DepSet[T]] |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 141 | if order == TOPOLOGICAL { |
| 142 | // TOPOLOGICAL is implemented as a postorder traversal followed by reversing the output. |
| 143 | // Pre-reverse the inputs here so their order is maintained in the output. |
Colin Cross | b5e3f7d | 2023-07-06 15:37:53 -0700 | [diff] [blame] | 144 | directCopy = ReverseSlice(direct) |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 145 | transitiveIter = slices.Backward(transitive) |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 146 | } else { |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 147 | directCopy = slices.Clone(direct) |
| 148 | transitiveIter = slices.All(transitive) |
| 149 | } |
| 150 | for _, t := range transitiveIter { |
| 151 | if t.handle != nil { |
| 152 | transitiveCopy = append(transitiveCopy, t) |
| 153 | } |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 156 | if len(directCopy) == 0 && len(transitive) == 0 { |
| 157 | return DepSet[T]{nil} |
| 158 | } |
| 159 | |
| 160 | depSet := &depSet[T]{ |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 161 | preorder: order == PREORDER, |
| 162 | reverse: order == TOPOLOGICAL, |
| 163 | order: order, |
| 164 | direct: directCopy, |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 165 | transitive: transitiveCopy, |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 166 | } |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 167 | |
| 168 | return DepSet[T]{depSet} |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 171 | // DepSetBuilder is used to create an immutable DepSet. |
| 172 | type DepSetBuilder[T depSettableType] struct { |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 173 | order DepSetOrder |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 174 | direct []T |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 175 | transitive []DepSet[T] |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 178 | // NewDepSetBuilder returns a DepSetBuilder to create an immutable DepSet with the given order and |
| 179 | // type, represented by a slice of type that will be in the DepSet. |
| 180 | func NewDepSetBuilder[T depSettableType](order DepSetOrder) *DepSetBuilder[T] { |
| 181 | return &DepSetBuilder[T]{ |
| 182 | order: order, |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 186 | // DirectSlice adds direct contents to the DepSet being built by a DepSetBuilder. Newly added direct |
| 187 | // contents are to the right of any existing direct contents. |
| 188 | func (b *DepSetBuilder[T]) DirectSlice(direct []T) *DepSetBuilder[T] { |
| 189 | b.direct = append(b.direct, direct...) |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 190 | return b |
| 191 | } |
| 192 | |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 193 | // Direct adds direct contents to the DepSet being built by a DepSetBuilder. Newly added direct |
| 194 | // contents are to the right of any existing direct contents. |
| 195 | func (b *DepSetBuilder[T]) Direct(direct ...T) *DepSetBuilder[T] { |
| 196 | b.direct = append(b.direct, direct...) |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 197 | return b |
| 198 | } |
| 199 | |
| 200 | // Transitive adds transitive contents to the DepSet being built by a DepSetBuilder. Newly added |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 201 | // transitive contents are to the right of any existing transitive contents. |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 202 | func (b *DepSetBuilder[T]) Transitive(transitive ...DepSet[T]) *DepSetBuilder[T] { |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 203 | for _, t := range transitive { |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 204 | if t.handle != nil && t.order() != b.order { |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 205 | panic(fmt.Errorf("incompatible order, new DepSet is %s but transitive DepSet is %s", |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 206 | b.order, t.order())) |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | b.transitive = append(b.transitive, transitive...) |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 210 | return b |
| 211 | } |
| 212 | |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 213 | // Returns the DepSet being built by this DepSetBuilder. The DepSetBuilder retains its contents |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 214 | // for creating more depSets. |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 215 | func (b *DepSetBuilder[T]) Build() DepSet[T] { |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 216 | return NewDepSet(b.order, b.direct, b.transitive) |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // walk calls the visit method in depth-first order on a DepSet, preordered if d.preorder is set, |
| 220 | // otherwise postordered. |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 221 | func (d DepSet[T]) walk(visit func([]T)) { |
| 222 | visited := make(map[DepSet[T]]bool) |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 223 | |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 224 | var dfs func(d DepSet[T]) |
| 225 | dfs = func(d DepSet[T]) { |
| 226 | impl := d.impl() |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 227 | visited[d] = true |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 228 | if impl.preorder { |
| 229 | visit(impl.direct) |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 230 | } |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 231 | for _, dep := range impl.transitive { |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 232 | if !visited[dep] { |
| 233 | dfs(dep) |
| 234 | } |
| 235 | } |
| 236 | |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 237 | if !impl.preorder { |
| 238 | visit(impl.direct) |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | dfs(d) |
| 243 | } |
| 244 | |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 245 | // ToList returns the DepSet flattened to a list. The order in the list is based on the order |
| 246 | // of the DepSet. POSTORDER and PREORDER orders return a postordered or preordered left to right |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 247 | // flattened list. TOPOLOGICAL returns a list that guarantees that elements of children are listed |
| 248 | // after all of their parents (unless there are duplicate direct elements in the DepSet or any of |
| 249 | // its transitive dependencies, in which case the ordering of the duplicated element is not |
| 250 | // guaranteed). |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 251 | func (d DepSet[T]) ToList() []T { |
| 252 | if d.handle == nil { |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 253 | return nil |
| 254 | } |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 255 | impl := d.impl() |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 256 | var list []T |
| 257 | d.walk(func(paths []T) { |
| 258 | list = append(list, paths...) |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 259 | }) |
Colin Cross | 48016d5 | 2023-06-27 09:45:26 -0700 | [diff] [blame] | 260 | list = firstUniqueInPlace(list) |
Colin Cross | e98d345 | 2024-10-23 13:04:01 -0700 | [diff] [blame^] | 261 | if impl.reverse { |
Colin Cross | b5e3f7d | 2023-07-06 15:37:53 -0700 | [diff] [blame] | 262 | ReverseSliceInPlace(list) |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 263 | } |
| 264 | return list |
| 265 | } |