1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sdk
import (
"fmt"
"reflect"
"strings"
"android/soong/android"
)
type bpPropertySet struct {
properties map[string]interface{}
tags map[string]android.BpPropertyTag
order []string
}
var _ android.BpPropertySet = (*bpPropertySet)(nil)
func (s *bpPropertySet) init() {
s.properties = make(map[string]interface{})
s.tags = make(map[string]android.BpPropertyTag)
}
// Converts the given value, which is assumed to be a struct, to a
// bpPropertySet.
func convertToPropertySet(value reflect.Value) *bpPropertySet {
res := newPropertySet()
structType := value.Type()
for i := 0; i < structType.NumField(); i++ {
field := structType.Field(i)
fieldVal := value.Field(i)
switch fieldVal.Type().Kind() {
case reflect.Ptr:
if fieldVal.IsNil() {
continue // nil pointer means the property isn't set.
}
fieldVal = fieldVal.Elem()
case reflect.Slice:
if fieldVal.IsNil() {
continue // Ignore a nil slice (but not one with length zero).
}
}
if fieldVal.Type().Kind() == reflect.Struct {
fieldVal = fieldVal.Addr() // Avoid struct copy below.
}
res.AddProperty(strings.ToLower(field.Name), fieldVal.Interface())
}
return res
}
// Converts the given value to something that can be set in a property.
func coercePropertyValue(value interface{}) interface{} {
val := reflect.ValueOf(value)
switch val.Kind() {
case reflect.Struct:
// convertToPropertySet requires an addressable struct, and this is probably
// a mistake.
panic(fmt.Sprintf("Value is a struct, not a pointer to one: %v", value))
case reflect.Ptr:
if _, ok := value.(*bpPropertySet); !ok {
derefValue := reflect.Indirect(val)
if derefValue.Kind() != reflect.Struct {
panic(fmt.Sprintf("A pointer must be to a struct, got: %v", value))
}
return convertToPropertySet(derefValue)
}
}
return value
}
// Merges the fields of the given property set into s.
func (s *bpPropertySet) mergePropertySet(propSet *bpPropertySet) {
for _, name := range propSet.order {
if tag, ok := propSet.tags[name]; ok {
s.AddPropertyWithTag(name, propSet.properties[name], tag)
} else {
s.AddProperty(name, propSet.properties[name])
}
}
}
func (s *bpPropertySet) AddProperty(name string, value interface{}) {
value = coercePropertyValue(value)
if propSetValue, ok := value.(*bpPropertySet); ok {
if curValue, ok := s.properties[name]; ok {
if curSet, ok := curValue.(*bpPropertySet); ok {
curSet.mergePropertySet(propSetValue)
return
}
// If the current value isn't a property set we got conflicting types.
// Continue down to the check below to complain about it.
}
}
if s.properties[name] != nil {
panic(fmt.Sprintf("Property %q already exists in property set", name))
}
s.properties[name] = value
s.order = append(s.order, name)
}
func (s *bpPropertySet) AddPropertyWithTag(name string, value interface{}, tag android.BpPropertyTag) {
s.AddProperty(name, value)
s.tags[name] = tag
}
func (s *bpPropertySet) AddPropertySet(name string) android.BpPropertySet {
s.AddProperty(name, newPropertySet())
return s.properties[name].(android.BpPropertySet)
}
func (s *bpPropertySet) getValue(name string) interface{} {
return s.properties[name]
}
func (s *bpPropertySet) getTag(name string) interface{} {
return s.tags[name]
}
func (s *bpPropertySet) transformContents(transformer bpPropertyTransformer) {
var newOrder []string
for _, name := range s.order {
value := s.properties[name]
tag := s.tags[name]
var newValue interface{}
var newTag android.BpPropertyTag
if propertySet, ok := value.(*bpPropertySet); ok {
var newPropertySet *bpPropertySet
newPropertySet, newTag = transformPropertySet(transformer, name, propertySet, tag)
if newPropertySet == nil {
newValue = nil
} else {
newValue = newPropertySet
}
} else {
newValue, newTag = transformer.transformProperty(name, value, tag)
}
if newValue == nil {
// Delete the property from the map and exclude it from the new order.
delete(s.properties, name)
} else {
// Update the property in the map and add the name to the new order list.
s.properties[name] = newValue
s.tags[name] = newTag
newOrder = append(newOrder, name)
}
}
s.order = newOrder
}
func transformPropertySet(transformer bpPropertyTransformer, name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
newPropertySet, newTag := transformer.transformPropertySetBeforeContents(name, propertySet, tag)
if newPropertySet != nil {
newPropertySet.transformContents(transformer)
newPropertySet, newTag = transformer.transformPropertySetAfterContents(name, newPropertySet, newTag)
}
return newPropertySet, newTag
}
func (s *bpPropertySet) setProperty(name string, value interface{}) {
if s.properties[name] == nil {
s.AddProperty(name, value)
} else {
s.properties[name] = value
s.tags[name] = nil
}
}
func (s *bpPropertySet) insertAfter(position string, name string, value interface{}) {
if s.properties[name] != nil {
panic("Property %q already exists in property set")
}
// Add the name to the end of the order, to ensure it has necessary capacity
// and to handle the case when the position does not exist.
s.order = append(s.order, name)
// Search through the order for the item that matches supplied position. If
// found then insert the name of the new property after it.
for i, v := range s.order {
if v == position {
// Copy the items after the one where the new property should be inserted.
copy(s.order[i+2:], s.order[i+1:])
// Insert the item in the list.
s.order[i+1] = name
}
}
s.properties[name] = value
}
type bpModule struct {
*bpPropertySet
moduleType string
}
var _ android.BpModule = (*bpModule)(nil)
type bpPropertyTransformer interface {
// Transform the property set, returning the new property set/tag to insert back into the
// parent property set (or module if this is the top level property set).
//
// This will be called before transforming the properties in the supplied set.
//
// The name will be "" for the top level property set.
//
// Returning (nil, ...) will cause the property set to be removed.
transformPropertySetBeforeContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag)
// Transform the property set, returning the new property set/tag to insert back into the
// parent property set (or module if this is the top level property set).
//
// This will be called after transforming the properties in the supplied set.
//
// The name will be "" for the top level property set.
//
// Returning (nil, ...) will cause the property set to be removed.
transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag)
// Transform a property, return the new value/tag to insert back into the property set.
//
// Returning (nil, ...) will cause the property to be removed.
transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag)
}
// Interface for transforming bpModule objects.
type bpTransformer interface {
// Transform the module, returning the result.
//
// The method can either create a new module and return that, or modify the supplied module
// in place and return that.
//
// After this returns the transformer is applied to the contents of the returned module.
transformModule(module *bpModule) *bpModule
bpPropertyTransformer
}
type identityTransformation struct{}
var _ bpTransformer = (*identityTransformation)(nil)
func (t identityTransformation) transformModule(module *bpModule) *bpModule {
return module
}
func (t identityTransformation) transformPropertySetBeforeContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
return propertySet, tag
}
func (t identityTransformation) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
return propertySet, tag
}
func (t identityTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
return value, tag
}
func (m *bpModule) deepCopy() *bpModule {
return m.transform(deepCopyTransformer)
}
func (m *bpModule) transform(transformer bpTransformer) *bpModule {
transformedModule := transformer.transformModule(m)
// Copy the contents of the returned property set into the module and then transform that.
transformedModule.bpPropertySet, _ = transformPropertySet(transformer, "", transformedModule.bpPropertySet, nil)
return transformedModule
}
type deepCopyTransformation struct {
identityTransformation
}
func (t deepCopyTransformation) transformModule(module *bpModule) *bpModule {
// Take a shallow copy of the module. Any mutable property values will be copied by the
// transformer.
moduleCopy := *module
return &moduleCopy
}
func (t deepCopyTransformation) transformPropertySetBeforeContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
// Create a shallow copy of the properties map. Any mutable property values will be copied by the
// transformer.
propertiesCopy := make(map[string]interface{})
for propertyName, value := range propertySet.properties {
propertiesCopy[propertyName] = value
}
// Ditto for tags map.
tagsCopy := make(map[string]android.BpPropertyTag)
for propertyName, propertyTag := range propertySet.tags {
tagsCopy[propertyName] = propertyTag
}
// Create a new property set.
return &bpPropertySet{
properties: propertiesCopy,
tags: tagsCopy,
order: append([]string(nil), propertySet.order...),
}, tag
}
func (t deepCopyTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
// Copy string slice, otherwise return value.
if values, ok := value.([]string); ok {
valuesCopy := make([]string, len(values))
copy(valuesCopy, values)
return valuesCopy, tag
}
return value, tag
}
var deepCopyTransformer bpTransformer = deepCopyTransformation{}
// A .bp file
type bpFile struct {
modules map[string]*bpModule
order []*bpModule
}
// Add a module.
//
// The module must have had its "name" property set to a string value that
// is unique within this file.
func (f *bpFile) AddModule(module android.BpModule) {
m := module.(*bpModule)
if name, ok := m.getValue("name").(string); ok {
if f.modules[name] != nil {
panic(fmt.Sprintf("Module %q already exists in bp file", name))
}
f.modules[name] = m
f.order = append(f.order, m)
} else {
panic("Module does not have a name property, or it is not a string")
}
}
func (f *bpFile) newModule(moduleType string) *bpModule {
return newModule(moduleType)
}
func newModule(moduleType string) *bpModule {
module := &bpModule{
moduleType: moduleType,
bpPropertySet: newPropertySet(),
}
return module
}
func newPropertySet() *bpPropertySet {
set := &bpPropertySet{}
set.init()
return set
}
|