Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 1 | // Copyright (C) 2019 The Android Open Source Project |
| 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 sdk |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | |
| 20 | "android/soong/android" |
| 21 | ) |
| 22 | |
| 23 | type bpPropertySet struct { |
| 24 | properties map[string]interface{} |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 25 | tags map[string]android.BpPropertyTag |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 26 | order []string |
| 27 | } |
| 28 | |
| 29 | var _ android.BpPropertySet = (*bpPropertySet)(nil) |
| 30 | |
| 31 | func (s *bpPropertySet) init() { |
| 32 | s.properties = make(map[string]interface{}) |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 33 | s.tags = make(map[string]android.BpPropertyTag) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | func (s *bpPropertySet) AddProperty(name string, value interface{}) { |
| 37 | if s.properties[name] != nil { |
| 38 | panic("Property %q already exists in property set") |
| 39 | } |
| 40 | |
| 41 | s.properties[name] = value |
| 42 | s.order = append(s.order, name) |
| 43 | } |
| 44 | |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 45 | func (s *bpPropertySet) AddPropertyWithTag(name string, value interface{}, tag android.BpPropertyTag) { |
| 46 | s.AddProperty(name, value) |
| 47 | s.tags[name] = tag |
| 48 | } |
| 49 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 50 | func (s *bpPropertySet) AddPropertySet(name string) android.BpPropertySet { |
| 51 | set := &bpPropertySet{} |
| 52 | set.init() |
| 53 | s.AddProperty(name, set) |
| 54 | return set |
| 55 | } |
| 56 | |
| 57 | func (s *bpPropertySet) getValue(name string) interface{} { |
| 58 | return s.properties[name] |
| 59 | } |
| 60 | |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 61 | func (s *bpPropertySet) getTag(name string) interface{} { |
| 62 | return s.tags[name] |
| 63 | } |
| 64 | |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 65 | func (s *bpPropertySet) transform(transformer bpPropertyTransformer) { |
| 66 | var newOrder []string |
| 67 | for _, name := range s.order { |
| 68 | value := s.properties[name] |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 69 | tag := s.tags[name] |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 70 | var newValue interface{} |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 71 | var newTag android.BpPropertyTag |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 72 | if propertySet, ok := value.(*bpPropertySet); ok { |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 73 | newValue, newTag = transformer.transformPropertySet(name, propertySet, tag) |
Paul Duffin | cc72e98 | 2020-01-14 15:53:11 +0000 | [diff] [blame] | 74 | } else { |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 75 | newValue, newTag = transformer.transformProperty(name, value, tag) |
Paul Duffin | cc72e98 | 2020-01-14 15:53:11 +0000 | [diff] [blame] | 76 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 77 | |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 78 | if newValue == nil { |
| 79 | // Delete the property from the map and exclude it from the new order. |
| 80 | delete(s.properties, name) |
| 81 | } else { |
| 82 | // Update the property in the map and add the name to the new order list. |
| 83 | s.properties[name] = newValue |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 84 | s.tags[name] = newTag |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 85 | newOrder = append(newOrder, name) |
| 86 | } |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 87 | } |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 88 | s.order = newOrder |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | func (s *bpPropertySet) setProperty(name string, value interface{}) { |
| 92 | if s.properties[name] == nil { |
| 93 | s.AddProperty(name, value) |
| 94 | } else { |
| 95 | s.properties[name] = value |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 96 | s.tags[name] = nil |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | func (s *bpPropertySet) insertAfter(position string, name string, value interface{}) { |
| 101 | if s.properties[name] != nil { |
| 102 | panic("Property %q already exists in property set") |
| 103 | } |
| 104 | |
| 105 | // Add the name to the end of the order, to ensure it has necessary capacity |
| 106 | // and to handle the case when the position does not exist. |
| 107 | s.order = append(s.order, name) |
| 108 | |
| 109 | // Search through the order for the item that matches supplied position. If |
| 110 | // found then insert the name of the new property after it. |
| 111 | for i, v := range s.order { |
| 112 | if v == position { |
| 113 | // Copy the items after the one where the new property should be inserted. |
| 114 | copy(s.order[i+2:], s.order[i+1:]) |
| 115 | // Insert the item in the list. |
| 116 | s.order[i+1] = name |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | s.properties[name] = value |
| 121 | } |
| 122 | |
| 123 | type bpModule struct { |
Paul Duffin | cc72e98 | 2020-01-14 15:53:11 +0000 | [diff] [blame] | 124 | *bpPropertySet |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 125 | moduleType string |
| 126 | } |
| 127 | |
| 128 | var _ android.BpModule = (*bpModule)(nil) |
| 129 | |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 130 | type bpPropertyTransformer interface { |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 131 | // Transform the property set, returning the new property set/tag to insert back into the |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 132 | // parent property set (or module if this is the top level property set). |
| 133 | // |
| 134 | // This will be called before transforming the properties in the supplied set. |
| 135 | // |
| 136 | // The name will be "" for the top level property set. |
| 137 | // |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 138 | // Returning (nil, ...) will cause the property set to be removed. |
| 139 | transformPropertySet(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 140 | |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 141 | // Transform a property, return the new value/tag to insert back into the property set. |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 142 | // |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 143 | // Returning (nil, ...) will cause the property to be removed. |
| 144 | transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // Interface for transforming bpModule objects. |
| 148 | type bpTransformer interface { |
| 149 | // Transform the module, returning the result. |
| 150 | // |
| 151 | // The method can either create a new module and return that, or modify the supplied module |
| 152 | // in place and return that. |
| 153 | // |
| 154 | // After this returns the transformer is applied to the contents of the returned module. |
| 155 | transformModule(module *bpModule) *bpModule |
| 156 | |
| 157 | bpPropertyTransformer |
| 158 | } |
| 159 | |
| 160 | type identityTransformation struct{} |
| 161 | |
| 162 | var _ bpTransformer = (*identityTransformation)(nil) |
| 163 | |
| 164 | func (t identityTransformation) transformModule(module *bpModule) *bpModule { |
| 165 | return module |
| 166 | } |
| 167 | |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 168 | func (t identityTransformation) transformPropertySet(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) { |
| 169 | return propertySet, tag |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 172 | func (t identityTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) { |
| 173 | return value, tag |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Paul Duffin | cc72e98 | 2020-01-14 15:53:11 +0000 | [diff] [blame] | 176 | func (m *bpModule) deepCopy() *bpModule { |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 177 | return m.transform(deepCopyTransformer) |
| 178 | } |
| 179 | |
| 180 | func (m *bpModule) transform(transformer bpTransformer) *bpModule { |
| 181 | transformedModule := transformer.transformModule(m) |
| 182 | // Copy the contents of the returned property set into the module and then transform that. |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 183 | transformedModule.bpPropertySet, _ = transformer.transformPropertySet("", transformedModule.bpPropertySet, nil) |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 184 | transformedModule.bpPropertySet.transform(transformer) |
| 185 | return transformedModule |
| 186 | } |
| 187 | |
| 188 | type deepCopyTransformation struct{} |
| 189 | |
| 190 | func (t deepCopyTransformation) transformModule(module *bpModule) *bpModule { |
| 191 | // Take a shallow copy of the module. Any mutable property values will be copied by the |
| 192 | // transformer. |
| 193 | moduleCopy := *module |
| 194 | return &moduleCopy |
| 195 | } |
| 196 | |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 197 | func (t deepCopyTransformation) transformPropertySet(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) { |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 198 | // Create a shallow copy of the properties map. Any mutable property values will be copied by the |
| 199 | // transformer. |
| 200 | propertiesCopy := make(map[string]interface{}) |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 201 | for propertyName, value := range propertySet.properties { |
| 202 | propertiesCopy[propertyName] = value |
| 203 | } |
| 204 | |
| 205 | // Ditto for tags map. |
| 206 | tagsCopy := make(map[string]android.BpPropertyTag) |
| 207 | for propertyName, propertyTag := range propertySet.tags { |
| 208 | tagsCopy[propertyName] = propertyTag |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // Create a new property set. |
| 212 | return &bpPropertySet{ |
| 213 | properties: propertiesCopy, |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 214 | tags: tagsCopy, |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 215 | order: append([]string(nil), propertySet.order...), |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 216 | }, tag |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 219 | func (t deepCopyTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) { |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 220 | // Copy string slice, otherwise return value. |
| 221 | if values, ok := value.([]string); ok { |
| 222 | valuesCopy := make([]string, len(values)) |
| 223 | copy(valuesCopy, values) |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 224 | return valuesCopy, tag |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 225 | } |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 226 | return value, tag |
Paul Duffin | b4d9c1f | 2020-01-15 11:52:11 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | var deepCopyTransformer bpTransformer = deepCopyTransformation{} |
| 230 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 231 | // A .bp file |
| 232 | type bpFile struct { |
| 233 | modules map[string]*bpModule |
| 234 | order []*bpModule |
| 235 | } |
| 236 | |
| 237 | // Add a module. |
| 238 | // |
| 239 | // The module must have had its "name" property set to a string value that |
| 240 | // is unique within this file. |
| 241 | func (f *bpFile) AddModule(module android.BpModule) { |
| 242 | m := module.(*bpModule) |
| 243 | if name, ok := m.getValue("name").(string); ok { |
| 244 | if f.modules[name] != nil { |
| 245 | panic(fmt.Sprintf("Module %q already exists in bp file", name)) |
| 246 | } |
| 247 | |
| 248 | f.modules[name] = m |
| 249 | f.order = append(f.order, m) |
| 250 | } else { |
| 251 | panic("Module does not have a name property, or it is not a string") |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | func (f *bpFile) newModule(moduleType string) *bpModule { |
| 256 | module := &bpModule{ |
Paul Duffin | cc72e98 | 2020-01-14 15:53:11 +0000 | [diff] [blame] | 257 | moduleType: moduleType, |
| 258 | bpPropertySet: &bpPropertySet{}, |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 259 | } |
Paul Duffin | cc72e98 | 2020-01-14 15:53:11 +0000 | [diff] [blame] | 260 | module.bpPropertySet.init() |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 261 | return module |
| 262 | } |