Merge "Allow extractCommonProperties to return an error" am: bd7e3295dd
Change-Id: Iab5fbb31db1ce9bdb7d4a4558e051f2b6df7d830
This commit is contained in:
commit
01d23134f2
3 changed files with 83 additions and 11 deletions
|
@ -231,6 +231,7 @@ type EmbeddedPropertiesStruct struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type testPropertiesStruct struct {
|
type testPropertiesStruct struct {
|
||||||
|
name string
|
||||||
private string
|
private string
|
||||||
Public_Kept string `sdk:"keep"`
|
Public_Kept string `sdk:"keep"`
|
||||||
S_Common string
|
S_Common string
|
||||||
|
@ -246,10 +247,17 @@ func (p *testPropertiesStruct) optimizableProperties() interface{} {
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *testPropertiesStruct) String() string {
|
||||||
|
return p.name
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ propertiesContainer = (*testPropertiesStruct)(nil)
|
||||||
|
|
||||||
func TestCommonValueOptimization(t *testing.T) {
|
func TestCommonValueOptimization(t *testing.T) {
|
||||||
common := &testPropertiesStruct{}
|
common := &testPropertiesStruct{name: "common"}
|
||||||
structs := []propertiesContainer{
|
structs := []propertiesContainer{
|
||||||
&testPropertiesStruct{
|
&testPropertiesStruct{
|
||||||
|
name: "struct-0",
|
||||||
private: "common",
|
private: "common",
|
||||||
Public_Kept: "common",
|
Public_Kept: "common",
|
||||||
S_Common: "common",
|
S_Common: "common",
|
||||||
|
@ -264,6 +272,7 @@ func TestCommonValueOptimization(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
&testPropertiesStruct{
|
&testPropertiesStruct{
|
||||||
|
name: "struct-1",
|
||||||
private: "common",
|
private: "common",
|
||||||
Public_Kept: "common",
|
Public_Kept: "common",
|
||||||
S_Common: "common",
|
S_Common: "common",
|
||||||
|
@ -285,6 +294,7 @@ func TestCommonValueOptimization(t *testing.T) {
|
||||||
h := TestHelper{t}
|
h := TestHelper{t}
|
||||||
h.AssertDeepEquals("common properties not correct",
|
h.AssertDeepEquals("common properties not correct",
|
||||||
&testPropertiesStruct{
|
&testPropertiesStruct{
|
||||||
|
name: "common",
|
||||||
private: "",
|
private: "",
|
||||||
Public_Kept: "",
|
Public_Kept: "",
|
||||||
S_Common: "common",
|
S_Common: "common",
|
||||||
|
@ -302,6 +312,7 @@ func TestCommonValueOptimization(t *testing.T) {
|
||||||
|
|
||||||
h.AssertDeepEquals("updated properties[0] not correct",
|
h.AssertDeepEquals("updated properties[0] not correct",
|
||||||
&testPropertiesStruct{
|
&testPropertiesStruct{
|
||||||
|
name: "struct-0",
|
||||||
private: "common",
|
private: "common",
|
||||||
Public_Kept: "common",
|
Public_Kept: "common",
|
||||||
S_Common: "",
|
S_Common: "",
|
||||||
|
@ -319,6 +330,7 @@ func TestCommonValueOptimization(t *testing.T) {
|
||||||
|
|
||||||
h.AssertDeepEquals("updated properties[1] not correct",
|
h.AssertDeepEquals("updated properties[1] not correct",
|
||||||
&testPropertiesStruct{
|
&testPropertiesStruct{
|
||||||
|
name: "struct-1",
|
||||||
private: "common",
|
private: "common",
|
||||||
Public_Kept: "common",
|
Public_Kept: "common",
|
||||||
S_Common: "",
|
S_Common: "",
|
||||||
|
|
|
@ -173,6 +173,15 @@ func (h *TestHelper) AssertStringEquals(message string, expected string, actual
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *TestHelper) AssertErrorMessageEquals(message string, expected string, actual error) {
|
||||||
|
h.t.Helper()
|
||||||
|
if actual == nil {
|
||||||
|
h.t.Errorf("Expected error but was nil")
|
||||||
|
} else if actual.Error() != expected {
|
||||||
|
h.t.Errorf("%s: expected %s, actual %s", message, expected, actual.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (h *TestHelper) AssertTrimmedStringEquals(message string, expected string, actual string) {
|
func (h *TestHelper) AssertTrimmedStringEquals(message string, expected string, actual string) {
|
||||||
h.t.Helper()
|
h.t.Helper()
|
||||||
h.AssertStringEquals(message, strings.TrimSpace(expected), strings.TrimSpace(actual))
|
h.AssertStringEquals(message, strings.TrimSpace(expected), strings.TrimSpace(actual))
|
||||||
|
|
|
@ -306,13 +306,13 @@ func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) andro
|
||||||
for _, sdkVariant := range sdkVariants {
|
for _, sdkVariant := range sdkVariants {
|
||||||
properties := sdkVariant.dynamicMemberTypeListProperties
|
properties := sdkVariant.dynamicMemberTypeListProperties
|
||||||
osTypeToMemberProperties[sdkVariant.Target().Os] = sdkVariant
|
osTypeToMemberProperties[sdkVariant.Target().Os] = sdkVariant
|
||||||
dynamicMemberPropertiesContainers = append(dynamicMemberPropertiesContainers, &dynamicMemberPropertiesContainer{properties})
|
dynamicMemberPropertiesContainers = append(dynamicMemberPropertiesContainers, &dynamicMemberPropertiesContainer{sdkVariant, properties})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract the common lists of members into a separate struct.
|
// Extract the common lists of members into a separate struct.
|
||||||
commonDynamicMemberProperties := s.dynamicSdkMemberTypes.createMemberListProperties()
|
commonDynamicMemberProperties := s.dynamicSdkMemberTypes.createMemberListProperties()
|
||||||
extractor := newCommonValueExtractor(commonDynamicMemberProperties)
|
extractor := newCommonValueExtractor(commonDynamicMemberProperties)
|
||||||
extractor.extractCommonProperties(commonDynamicMemberProperties, dynamicMemberPropertiesContainers)
|
extractCommonProperties(ctx, extractor, commonDynamicMemberProperties, dynamicMemberPropertiesContainers)
|
||||||
|
|
||||||
// Add properties common to all os types.
|
// Add properties common to all os types.
|
||||||
s.addMemberPropertiesToPropertySet(builder, snapshotModule, commonDynamicMemberProperties)
|
s.addMemberPropertiesToPropertySet(builder, snapshotModule, commonDynamicMemberProperties)
|
||||||
|
@ -389,6 +389,13 @@ func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) andro
|
||||||
return outputZipFile
|
return outputZipFile
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func extractCommonProperties(ctx android.ModuleContext, extractor *commonValueExtractor, commonProperties interface{}, inputPropertiesSlice interface{}) {
|
||||||
|
err := extractor.extractCommonProperties(commonProperties, inputPropertiesSlice)
|
||||||
|
if err != nil {
|
||||||
|
ctx.ModuleErrorf("error extracting common properties: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *sdk) addMemberPropertiesToPropertySet(builder *snapshotBuilder, propertySet android.BpPropertySet, dynamicMemberTypeListProperties interface{}) {
|
func (s *sdk) addMemberPropertiesToPropertySet(builder *snapshotBuilder, propertySet android.BpPropertySet, dynamicMemberTypeListProperties interface{}) {
|
||||||
for _, memberListProperty := range s.memberListProperties() {
|
for _, memberListProperty := range s.memberListProperties() {
|
||||||
names := memberListProperty.getter(dynamicMemberTypeListProperties)
|
names := memberListProperty.getter(dynamicMemberTypeListProperties)
|
||||||
|
@ -829,6 +836,8 @@ type osTypeSpecificInfo struct {
|
||||||
archInfos []*archTypeSpecificInfo
|
archInfos []*archTypeSpecificInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ propertiesContainer = (*osTypeSpecificInfo)(nil)
|
||||||
|
|
||||||
type variantPropertiesFactoryFunc func() android.SdkMemberProperties
|
type variantPropertiesFactoryFunc func() android.SdkMemberProperties
|
||||||
|
|
||||||
// Create a new osTypeSpecificInfo for the specified os type and its properties
|
// Create a new osTypeSpecificInfo for the specified os type and its properties
|
||||||
|
@ -886,7 +895,7 @@ func newOsTypeSpecificInfo(ctx android.SdkMemberContext, osType android.OsType,
|
||||||
|
|
||||||
// Optimize the properties by extracting common properties from arch type specific
|
// Optimize the properties by extracting common properties from arch type specific
|
||||||
// properties into os type specific properties.
|
// properties into os type specific properties.
|
||||||
func (osInfo *osTypeSpecificInfo) optimizeProperties(commonValueExtractor *commonValueExtractor) {
|
func (osInfo *osTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
|
||||||
// Nothing to do if there is only a single common architecture.
|
// Nothing to do if there is only a single common architecture.
|
||||||
if len(osInfo.archInfos) == 0 {
|
if len(osInfo.archInfos) == 0 {
|
||||||
return
|
return
|
||||||
|
@ -897,10 +906,10 @@ func (osInfo *osTypeSpecificInfo) optimizeProperties(commonValueExtractor *commo
|
||||||
multilib = multilib.addArchType(archInfo.archType)
|
multilib = multilib.addArchType(archInfo.archType)
|
||||||
|
|
||||||
// Optimize the arch properties first.
|
// Optimize the arch properties first.
|
||||||
archInfo.optimizeProperties(commonValueExtractor)
|
archInfo.optimizeProperties(ctx, commonValueExtractor)
|
||||||
}
|
}
|
||||||
|
|
||||||
commonValueExtractor.extractCommonProperties(osInfo.Properties, osInfo.archInfos)
|
extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, osInfo.Properties, osInfo.archInfos)
|
||||||
|
|
||||||
// Choose setting for compile_multilib that is appropriate for the arch variants supplied.
|
// Choose setting for compile_multilib that is appropriate for the arch variants supplied.
|
||||||
osInfo.Properties.Base().Compile_multilib = multilib.String()
|
osInfo.Properties.Base().Compile_multilib = multilib.String()
|
||||||
|
@ -973,6 +982,10 @@ func (osInfo *osTypeSpecificInfo) addToPropertySet(ctx *memberContext, bpModule
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (osInfo *osTypeSpecificInfo) String() string {
|
||||||
|
return fmt.Sprintf("OsType{%s}", osInfo.osType)
|
||||||
|
}
|
||||||
|
|
||||||
type archTypeSpecificInfo struct {
|
type archTypeSpecificInfo struct {
|
||||||
baseInfo
|
baseInfo
|
||||||
|
|
||||||
|
@ -981,6 +994,8 @@ type archTypeSpecificInfo struct {
|
||||||
linkInfos []*linkTypeSpecificInfo
|
linkInfos []*linkTypeSpecificInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ propertiesContainer = (*archTypeSpecificInfo)(nil)
|
||||||
|
|
||||||
// Create a new archTypeSpecificInfo for the specified arch type and its properties
|
// Create a new archTypeSpecificInfo for the specified arch type and its properties
|
||||||
// structures populated with information from the variants.
|
// structures populated with information from the variants.
|
||||||
func newArchSpecificInfo(ctx android.SdkMemberContext, archType android.ArchType, variantPropertiesFactory variantPropertiesFactoryFunc, archVariants []android.Module) *archTypeSpecificInfo {
|
func newArchSpecificInfo(ctx android.SdkMemberContext, archType android.ArchType, variantPropertiesFactory variantPropertiesFactoryFunc, archVariants []android.Module) *archTypeSpecificInfo {
|
||||||
|
@ -1038,12 +1053,12 @@ func getLinkType(variant android.Module) string {
|
||||||
|
|
||||||
// Optimize the properties by extracting common properties from link type specific
|
// Optimize the properties by extracting common properties from link type specific
|
||||||
// properties into arch type specific properties.
|
// properties into arch type specific properties.
|
||||||
func (archInfo *archTypeSpecificInfo) optimizeProperties(commonValueExtractor *commonValueExtractor) {
|
func (archInfo *archTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
|
||||||
if len(archInfo.linkInfos) == 0 {
|
if len(archInfo.linkInfos) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
commonValueExtractor.extractCommonProperties(archInfo.Properties, archInfo.linkInfos)
|
extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, archInfo.Properties, archInfo.linkInfos)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the properties for an arch type to a property set.
|
// Add the properties for an arch type to a property set.
|
||||||
|
@ -1058,12 +1073,18 @@ func (archInfo *archTypeSpecificInfo) addToPropertySet(ctx *memberContext, archP
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (archInfo *archTypeSpecificInfo) String() string {
|
||||||
|
return fmt.Sprintf("ArchType{%s}", archInfo.archType)
|
||||||
|
}
|
||||||
|
|
||||||
type linkTypeSpecificInfo struct {
|
type linkTypeSpecificInfo struct {
|
||||||
baseInfo
|
baseInfo
|
||||||
|
|
||||||
linkType string
|
linkType string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ propertiesContainer = (*linkTypeSpecificInfo)(nil)
|
||||||
|
|
||||||
// Create a new linkTypeSpecificInfo for the specified link type and its properties
|
// Create a new linkTypeSpecificInfo for the specified link type and its properties
|
||||||
// structures populated with information from the variant.
|
// structures populated with information from the variant.
|
||||||
func newLinkSpecificInfo(ctx android.SdkMemberContext, linkType string, variantPropertiesFactory variantPropertiesFactoryFunc, linkVariant android.Module) *linkTypeSpecificInfo {
|
func newLinkSpecificInfo(ctx android.SdkMemberContext, linkType string, variantPropertiesFactory variantPropertiesFactoryFunc, linkVariant android.Module) *linkTypeSpecificInfo {
|
||||||
|
@ -1079,6 +1100,10 @@ func newLinkSpecificInfo(ctx android.SdkMemberContext, linkType string, variantP
|
||||||
return linkInfo
|
return linkInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *linkTypeSpecificInfo) String() string {
|
||||||
|
return fmt.Sprintf("LinkType{%s}", l.linkType)
|
||||||
|
}
|
||||||
|
|
||||||
type memberContext struct {
|
type memberContext struct {
|
||||||
sdkMemberContext android.ModuleContext
|
sdkMemberContext android.ModuleContext
|
||||||
builder *snapshotBuilder
|
builder *snapshotBuilder
|
||||||
|
@ -1143,11 +1168,11 @@ func (s *sdk) createMemberSnapshot(ctx *memberContext, member *sdkMember, bpModu
|
||||||
osSpecificPropertiesContainers = append(osSpecificPropertiesContainers, osInfo)
|
osSpecificPropertiesContainers = append(osSpecificPropertiesContainers, osInfo)
|
||||||
|
|
||||||
// Optimize the properties across all the variants for a specific os type.
|
// Optimize the properties across all the variants for a specific os type.
|
||||||
osInfo.optimizeProperties(commonValueExtractor)
|
osInfo.optimizeProperties(ctx, commonValueExtractor)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract properties which are common across all architectures and os types.
|
// Extract properties which are common across all architectures and os types.
|
||||||
commonValueExtractor.extractCommonProperties(commonProperties, osSpecificPropertiesContainers)
|
extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, commonProperties, osSpecificPropertiesContainers)
|
||||||
|
|
||||||
// Add the common properties to the module.
|
// Add the common properties to the module.
|
||||||
commonProperties.AddToPropertySet(ctx, bpModule)
|
commonProperties.AddToPropertySet(ctx, bpModule)
|
||||||
|
@ -1192,6 +1217,9 @@ type fieldAccessorFunc func(structValue reflect.Value) reflect.Value
|
||||||
|
|
||||||
// A property that can be optimized by the commonValueExtractor.
|
// A property that can be optimized by the commonValueExtractor.
|
||||||
type extractorProperty struct {
|
type extractorProperty struct {
|
||||||
|
// The name of the field for this property.
|
||||||
|
name string
|
||||||
|
|
||||||
// Retrieves the value on which common value optimization will be performed.
|
// Retrieves the value on which common value optimization will be performed.
|
||||||
getter fieldAccessorFunc
|
getter fieldAccessorFunc
|
||||||
|
|
||||||
|
@ -1199,6 +1227,10 @@ type extractorProperty struct {
|
||||||
emptyValue reflect.Value
|
emptyValue reflect.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p extractorProperty) String() string {
|
||||||
|
return p.name
|
||||||
|
}
|
||||||
|
|
||||||
// Supports extracting common values from a number of instances of a properties
|
// Supports extracting common values from a number of instances of a properties
|
||||||
// structure into a separate common set of properties.
|
// structure into a separate common set of properties.
|
||||||
type commonValueExtractor struct {
|
type commonValueExtractor struct {
|
||||||
|
@ -1240,6 +1272,9 @@ func (e *commonValueExtractor) gatherFields(structType reflect.Type, containingS
|
||||||
|
|
||||||
// Save a copy of the field index for use in the function.
|
// Save a copy of the field index for use in the function.
|
||||||
fieldIndex := f
|
fieldIndex := f
|
||||||
|
|
||||||
|
name := field.Name
|
||||||
|
|
||||||
fieldGetter := func(value reflect.Value) reflect.Value {
|
fieldGetter := func(value reflect.Value) reflect.Value {
|
||||||
if containingStructAccessor != nil {
|
if containingStructAccessor != nil {
|
||||||
// This is an embedded structure so first access the field for the embedded
|
// This is an embedded structure so first access the field for the embedded
|
||||||
|
@ -1250,6 +1285,12 @@ func (e *commonValueExtractor) gatherFields(structType reflect.Type, containingS
|
||||||
// Skip through interface and pointer values to find the structure.
|
// Skip through interface and pointer values to find the structure.
|
||||||
value = getStructValue(value)
|
value = getStructValue(value)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
panic(fmt.Errorf("%s for fieldIndex %d of field %s of value %#v", r, fieldIndex, name, value.Interface()))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// Return the field.
|
// Return the field.
|
||||||
return value.Field(fieldIndex)
|
return value.Field(fieldIndex)
|
||||||
}
|
}
|
||||||
|
@ -1259,6 +1300,7 @@ func (e *commonValueExtractor) gatherFields(structType reflect.Type, containingS
|
||||||
e.gatherFields(field.Type, fieldGetter)
|
e.gatherFields(field.Type, fieldGetter)
|
||||||
} else {
|
} else {
|
||||||
property := extractorProperty{
|
property := extractorProperty{
|
||||||
|
name,
|
||||||
fieldGetter,
|
fieldGetter,
|
||||||
reflect.Zero(field.Type),
|
reflect.Zero(field.Type),
|
||||||
}
|
}
|
||||||
|
@ -1288,12 +1330,15 @@ foundStruct:
|
||||||
// Allows additional information to be associated with the properties, e.g. for
|
// Allows additional information to be associated with the properties, e.g. for
|
||||||
// filtering.
|
// filtering.
|
||||||
type propertiesContainer interface {
|
type propertiesContainer interface {
|
||||||
|
fmt.Stringer
|
||||||
|
|
||||||
// Get the properties that need optimizing.
|
// Get the properties that need optimizing.
|
||||||
optimizableProperties() interface{}
|
optimizableProperties() interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// A wrapper for dynamic member properties to allow them to be optimized.
|
// A wrapper for dynamic member properties to allow them to be optimized.
|
||||||
type dynamicMemberPropertiesContainer struct {
|
type dynamicMemberPropertiesContainer struct {
|
||||||
|
sdkVariant *sdk
|
||||||
dynamicMemberProperties interface{}
|
dynamicMemberProperties interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1301,6 +1346,10 @@ func (c dynamicMemberPropertiesContainer) optimizableProperties() interface{} {
|
||||||
return c.dynamicMemberProperties
|
return c.dynamicMemberProperties
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c dynamicMemberPropertiesContainer) String() string {
|
||||||
|
return c.sdkVariant.String()
|
||||||
|
}
|
||||||
|
|
||||||
// Extract common properties from a slice of property structures of the same type.
|
// Extract common properties from a slice of property structures of the same type.
|
||||||
//
|
//
|
||||||
// All the property structures must be of the same type.
|
// All the property structures must be of the same type.
|
||||||
|
@ -1311,7 +1360,7 @@ func (c dynamicMemberPropertiesContainer) optimizableProperties() interface{} {
|
||||||
// have the same value (using DeepEquals) across all the input properties. If it does not then no
|
// have the same value (using DeepEquals) across all the input properties. If it does not then no
|
||||||
// change is made. Otherwise, the common value is stored in the field in the commonProperties
|
// change is made. Otherwise, the common value is stored in the field in the commonProperties
|
||||||
// and the field in each of the input properties structure is set to its default value.
|
// and the field in each of the input properties structure is set to its default value.
|
||||||
func (e *commonValueExtractor) extractCommonProperties(commonProperties interface{}, inputPropertiesSlice interface{}) {
|
func (e *commonValueExtractor) extractCommonProperties(commonProperties interface{}, inputPropertiesSlice interface{}) error {
|
||||||
commonPropertiesValue := reflect.ValueOf(commonProperties)
|
commonPropertiesValue := reflect.ValueOf(commonProperties)
|
||||||
commonStructValue := commonPropertiesValue.Elem()
|
commonStructValue := commonPropertiesValue.Elem()
|
||||||
|
|
||||||
|
@ -1356,4 +1405,6 @@ func (e *commonValueExtractor) extractCommonProperties(commonProperties interfac
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue