2019-11-27 18:43:54 +01:00
|
|
|
// 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"
|
2020-09-10 01:40:37 +02:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2019-11-27 18:43:54 +01:00
|
|
|
|
|
|
|
"android/soong/android"
|
|
|
|
)
|
|
|
|
|
|
|
|
type bpPropertySet struct {
|
|
|
|
properties map[string]interface{}
|
2020-01-15 15:23:52 +01:00
|
|
|
tags map[string]android.BpPropertyTag
|
2021-05-07 02:10:01 +02:00
|
|
|
comments map[string]string
|
2019-11-27 18:43:54 +01:00
|
|
|
order []string
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ android.BpPropertySet = (*bpPropertySet)(nil)
|
|
|
|
|
|
|
|
func (s *bpPropertySet) init() {
|
|
|
|
s.properties = make(map[string]interface{})
|
2020-01-15 15:23:52 +01:00
|
|
|
s.tags = make(map[string]android.BpPropertyTag)
|
2019-11-27 18:43:54 +01:00
|
|
|
}
|
|
|
|
|
2020-09-10 01:40:37 +02:00
|
|
|
// 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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:43:54 +01:00
|
|
|
func (s *bpPropertySet) AddProperty(name string, value interface{}) {
|
2020-09-10 01:40:37 +02:00
|
|
|
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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:43:54 +01:00
|
|
|
if s.properties[name] != nil {
|
2020-03-02 17:29:11 +01:00
|
|
|
panic(fmt.Sprintf("Property %q already exists in property set", name))
|
2019-11-27 18:43:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
s.properties[name] = value
|
|
|
|
s.order = append(s.order, name)
|
|
|
|
}
|
|
|
|
|
2020-01-15 15:23:52 +01:00
|
|
|
func (s *bpPropertySet) AddPropertyWithTag(name string, value interface{}, tag android.BpPropertyTag) {
|
|
|
|
s.AddProperty(name, value)
|
|
|
|
s.tags[name] = tag
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:43:54 +01:00
|
|
|
func (s *bpPropertySet) AddPropertySet(name string) android.BpPropertySet {
|
2020-09-10 01:40:37 +02:00
|
|
|
s.AddProperty(name, newPropertySet())
|
|
|
|
return s.properties[name].(android.BpPropertySet)
|
2019-11-27 18:43:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *bpPropertySet) getValue(name string) interface{} {
|
|
|
|
return s.properties[name]
|
|
|
|
}
|
|
|
|
|
2021-05-07 02:10:01 +02:00
|
|
|
func (s *bpPropertySet) getOptionalValue(name string) (interface{}, bool) {
|
|
|
|
value, ok := s.properties[name]
|
|
|
|
return value, ok
|
|
|
|
}
|
|
|
|
|
2020-01-15 15:23:52 +01:00
|
|
|
func (s *bpPropertySet) getTag(name string) interface{} {
|
|
|
|
return s.tags[name]
|
|
|
|
}
|
|
|
|
|
2021-05-07 02:10:01 +02:00
|
|
|
func (s *bpPropertySet) AddCommentForProperty(name, text string) {
|
|
|
|
if s.comments == nil {
|
|
|
|
s.comments = map[string]string{}
|
|
|
|
}
|
|
|
|
s.comments[name] = strings.TrimSpace(text)
|
|
|
|
}
|
|
|
|
|
2020-02-21 17:06:25 +01:00
|
|
|
func (s *bpPropertySet) transformContents(transformer bpPropertyTransformer) {
|
2020-01-15 12:52:11 +01:00
|
|
|
var newOrder []string
|
|
|
|
for _, name := range s.order {
|
|
|
|
value := s.properties[name]
|
2020-01-15 15:23:52 +01:00
|
|
|
tag := s.tags[name]
|
2020-01-15 12:52:11 +01:00
|
|
|
var newValue interface{}
|
2020-01-15 15:23:52 +01:00
|
|
|
var newTag android.BpPropertyTag
|
2020-01-15 12:52:11 +01:00
|
|
|
if propertySet, ok := value.(*bpPropertySet); ok {
|
2020-02-21 17:06:25 +01:00
|
|
|
var newPropertySet *bpPropertySet
|
|
|
|
newPropertySet, newTag = transformPropertySet(transformer, name, propertySet, tag)
|
|
|
|
if newPropertySet == nil {
|
|
|
|
newValue = nil
|
|
|
|
} else {
|
|
|
|
newValue = newPropertySet
|
|
|
|
}
|
2020-01-14 16:53:11 +01:00
|
|
|
} else {
|
2020-01-15 15:23:52 +01:00
|
|
|
newValue, newTag = transformer.transformProperty(name, value, tag)
|
2020-01-14 16:53:11 +01:00
|
|
|
}
|
2019-11-27 18:43:54 +01:00
|
|
|
|
2020-01-15 12:52:11 +01:00
|
|
|
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
|
2020-01-15 15:23:52 +01:00
|
|
|
s.tags[name] = newTag
|
2020-01-15 12:52:11 +01:00
|
|
|
newOrder = append(newOrder, name)
|
|
|
|
}
|
2019-11-27 18:43:54 +01:00
|
|
|
}
|
2020-01-15 12:52:11 +01:00
|
|
|
s.order = newOrder
|
2019-11-27 18:43:54 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 17:06:25 +01:00
|
|
|
func transformPropertySet(transformer bpPropertyTransformer, name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
|
2020-02-21 17:06:25 +01:00
|
|
|
newPropertySet, newTag := transformer.transformPropertySetBeforeContents(name, propertySet, tag)
|
2020-02-21 17:06:25 +01:00
|
|
|
if newPropertySet != nil {
|
|
|
|
newPropertySet.transformContents(transformer)
|
2020-02-21 17:06:25 +01:00
|
|
|
|
|
|
|
newPropertySet, newTag = transformer.transformPropertySetAfterContents(name, newPropertySet, newTag)
|
2020-02-21 17:06:25 +01:00
|
|
|
}
|
|
|
|
return newPropertySet, newTag
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:43:54 +01:00
|
|
|
func (s *bpPropertySet) setProperty(name string, value interface{}) {
|
|
|
|
if s.properties[name] == nil {
|
|
|
|
s.AddProperty(name, value)
|
|
|
|
} else {
|
|
|
|
s.properties[name] = value
|
2020-01-15 15:23:52 +01:00
|
|
|
s.tags[name] = nil
|
2019-11-27 18:43:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-11 00:49:04 +02:00
|
|
|
func (s *bpPropertySet) removeProperty(name string) {
|
|
|
|
delete(s.properties, name)
|
|
|
|
delete(s.tags, name)
|
|
|
|
_, s.order = android.RemoveFromList(name, s.order)
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:43:54 +01:00
|
|
|
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 {
|
2020-01-14 16:53:11 +01:00
|
|
|
*bpPropertySet
|
2019-11-27 18:43:54 +01:00
|
|
|
moduleType string
|
|
|
|
}
|
|
|
|
|
2021-05-07 02:10:01 +02:00
|
|
|
func (m *bpModule) ModuleType() string {
|
|
|
|
return m.moduleType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *bpModule) Name() string {
|
|
|
|
name, hasName := m.getOptionalValue("name")
|
|
|
|
if hasName {
|
|
|
|
return name.(string)
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:43:54 +01:00
|
|
|
var _ android.BpModule = (*bpModule)(nil)
|
|
|
|
|
2020-01-15 12:52:11 +01:00
|
|
|
type bpPropertyTransformer interface {
|
2020-01-15 15:23:52 +01:00
|
|
|
// Transform the property set, returning the new property set/tag to insert back into the
|
2020-01-15 12:52:11 +01:00
|
|
|
// 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.
|
|
|
|
//
|
2020-01-15 15:23:52 +01:00
|
|
|
// Returning (nil, ...) will cause the property set to be removed.
|
2020-02-21 17:06:25 +01:00
|
|
|
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)
|
2020-01-15 12:52:11 +01:00
|
|
|
|
2020-01-15 15:23:52 +01:00
|
|
|
// Transform a property, return the new value/tag to insert back into the property set.
|
2020-01-15 12:52:11 +01:00
|
|
|
//
|
2020-01-15 15:23:52 +01:00
|
|
|
// Returning (nil, ...) will cause the property to be removed.
|
|
|
|
transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag)
|
2020-01-15 12:52:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-05-24 22:10:05 +02:00
|
|
|
func (t identityTransformation) transformPropertySetBeforeContents(_ string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
|
2020-02-21 17:06:25 +01:00
|
|
|
return propertySet, tag
|
|
|
|
}
|
|
|
|
|
2022-05-24 22:10:05 +02:00
|
|
|
func (t identityTransformation) transformPropertySetAfterContents(_ string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
|
2020-01-15 15:23:52 +01:00
|
|
|
return propertySet, tag
|
2020-01-15 12:52:11 +01:00
|
|
|
}
|
|
|
|
|
2022-05-24 22:10:05 +02:00
|
|
|
func (t identityTransformation) transformProperty(_ string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
|
2020-01-15 15:23:52 +01:00
|
|
|
return value, tag
|
2020-01-15 12:52:11 +01:00
|
|
|
}
|
|
|
|
|
2020-01-14 16:53:11 +01:00
|
|
|
func (m *bpModule) deepCopy() *bpModule {
|
2020-01-15 12:52:11 +01:00
|
|
|
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.
|
2020-02-21 17:06:25 +01:00
|
|
|
transformedModule.bpPropertySet, _ = transformPropertySet(transformer, "", transformedModule.bpPropertySet, nil)
|
2020-01-15 12:52:11 +01:00
|
|
|
return transformedModule
|
|
|
|
}
|
|
|
|
|
2020-02-21 17:06:25 +01:00
|
|
|
type deepCopyTransformation struct {
|
|
|
|
identityTransformation
|
|
|
|
}
|
2020-01-15 12:52:11 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-05-24 22:10:05 +02:00
|
|
|
func (t deepCopyTransformation) transformPropertySetBeforeContents(_ string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
|
2020-01-15 12:52:11 +01:00
|
|
|
// Create a shallow copy of the properties map. Any mutable property values will be copied by the
|
|
|
|
// transformer.
|
|
|
|
propertiesCopy := make(map[string]interface{})
|
2020-01-15 15:23:52 +01:00
|
|
|
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
|
2020-01-15 12:52:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new property set.
|
|
|
|
return &bpPropertySet{
|
|
|
|
properties: propertiesCopy,
|
2020-01-15 15:23:52 +01:00
|
|
|
tags: tagsCopy,
|
2020-01-15 12:52:11 +01:00
|
|
|
order: append([]string(nil), propertySet.order...),
|
2020-01-15 15:23:52 +01:00
|
|
|
}, tag
|
2019-11-27 18:43:54 +01:00
|
|
|
}
|
|
|
|
|
2022-05-24 22:10:05 +02:00
|
|
|
func (t deepCopyTransformation) transformProperty(_ string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
|
2020-01-15 12:52:11 +01:00
|
|
|
// Copy string slice, otherwise return value.
|
|
|
|
if values, ok := value.([]string); ok {
|
|
|
|
valuesCopy := make([]string, len(values))
|
|
|
|
copy(valuesCopy, values)
|
2020-01-15 15:23:52 +01:00
|
|
|
return valuesCopy, tag
|
2020-01-15 12:52:11 +01:00
|
|
|
}
|
2020-01-15 15:23:52 +01:00
|
|
|
return value, tag
|
2020-01-15 12:52:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var deepCopyTransformer bpTransformer = deepCopyTransformation{}
|
|
|
|
|
2019-11-27 18:43:54 +01:00
|
|
|
// A .bp file
|
|
|
|
type bpFile struct {
|
|
|
|
modules map[string]*bpModule
|
|
|
|
order []*bpModule
|
|
|
|
}
|
|
|
|
|
2022-05-24 22:10:05 +02:00
|
|
|
// AddModule adds a module to this.
|
2019-11-27 18:43:54 +01:00
|
|
|
//
|
|
|
|
// 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)
|
2021-05-07 02:10:01 +02:00
|
|
|
moduleType := module.ModuleType()
|
|
|
|
name := m.Name()
|
|
|
|
hasName := true
|
|
|
|
if name == "" {
|
|
|
|
// Use a prefixed module type as the name instead just in case this is something like a package
|
|
|
|
// of namespace module which does not require a name.
|
|
|
|
name = "#" + moduleType
|
|
|
|
hasName = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.modules[name] != nil {
|
|
|
|
if hasName {
|
2019-11-27 18:43:54 +01:00
|
|
|
panic(fmt.Sprintf("Module %q already exists in bp file", name))
|
2021-05-07 02:10:01 +02:00
|
|
|
} else {
|
|
|
|
panic(fmt.Sprintf("Unnamed module type %q already exists in bp file", moduleType))
|
2019-11-27 18:43:54 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-07 02:10:01 +02:00
|
|
|
|
|
|
|
f.modules[name] = m
|
|
|
|
f.order = append(f.order, m)
|
2019-11-27 18:43:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *bpFile) newModule(moduleType string) *bpModule {
|
2020-02-21 17:06:25 +01:00
|
|
|
return newModule(moduleType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newModule(moduleType string) *bpModule {
|
2019-11-27 18:43:54 +01:00
|
|
|
module := &bpModule{
|
2020-01-14 16:53:11 +01:00
|
|
|
moduleType: moduleType,
|
2020-02-21 17:06:25 +01:00
|
|
|
bpPropertySet: newPropertySet(),
|
2019-11-27 18:43:54 +01:00
|
|
|
}
|
|
|
|
return module
|
|
|
|
}
|
2020-02-21 17:06:25 +01:00
|
|
|
|
|
|
|
func newPropertySet() *bpPropertySet {
|
|
|
|
set := &bpPropertySet{}
|
|
|
|
set.init()
|
|
|
|
return set
|
|
|
|
}
|