Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 57ee1774 authored by Paul Duffin's avatar Paul Duffin
Browse files

Extract the osTypeSpecificInfo code from module creation loop

Extract the functionality to create an osTypeSpecificInfo struct,
to optimize the properties, and add its properties to a property set
into methods of the *osTypeSpecificInfo struct.

This change is in preparation for adding support for link type which
is another dimension within arch type which itself sits within os type.

Bug: 153306490
Test: m nothing
Bug: 142918168
Merged-In: I025ee90e1461f7389bf4a9d056b281453068cf87
Change-Id: I025ee90e1461f7389bf4a9d056b281453068cf87
parent 4a4b2d0b
Loading
Loading
Loading
Loading
+160 −119
Original line number Diff line number Diff line
@@ -793,6 +793,8 @@ type baseInfo struct {
type osTypeSpecificInfo struct {
	baseInfo

	osType android.OsType

	// The list of arch type specific info for this os type.
	//
	// Nil if there is one variant whose arch type is common
@@ -801,86 +803,22 @@ type osTypeSpecificInfo struct {

type variantPropertiesFactoryFunc func() android.SdkMemberProperties

type archTypeSpecificInfo struct {
	baseInfo

	archType android.ArchType
}

// Create a new archTypeSpecificInfo for the specified arch type and its properties
// Create a new osTypeSpecificInfo for the specified os type and its properties
// structures populated with information from the variants.
func newArchSpecificInfo(archType android.ArchType, variantPropertiesFactory variantPropertiesFactoryFunc, archVariants []android.SdkAware) *archTypeSpecificInfo {

	if len(archVariants) != 1 {
		panic(fmt.Errorf("expected one arch specific variant but found %d", len(archVariants)))
	}

	// Create an arch specific info into which the variant properties can be copied.
	archInfo := &archTypeSpecificInfo{archType: archType}

	// Create the properties into which the arch type specific properties will be
	// added.
	archInfo.Properties = variantPropertiesFactory()
	archInfo.Properties.PopulateFromVariant(archVariants[0])

	return archInfo
}

// Add the properties for an arch type to a property set.
func (archInfo *archTypeSpecificInfo) addToPropertySet(builder *snapshotBuilder, archPropertySet android.BpPropertySet, archOsPrefix string) {
	archTypeName := archInfo.archType.Name
	archTypePropertySet := archPropertySet.AddPropertySet(archOsPrefix + archTypeName)
	archInfo.Properties.AddToPropertySet(builder.ctx, builder, archTypePropertySet)
}

func (s *sdk) createMemberSnapshot(sdkModuleContext android.ModuleContext, builder *snapshotBuilder, member *sdkMember, bpModule android.BpModule) {

	memberType := member.memberType

	// Group the variants by os type.
	variantsByOsType := make(map[android.OsType][]android.SdkAware)
	variants := member.Variants()
	for _, variant := range variants {
		osType := variant.Target().Os
		variantsByOsType[osType] = append(variantsByOsType[osType], variant)
	}

	osCount := len(variantsByOsType)
	variantPropertiesFactory := func() android.SdkMemberProperties {
		properties := memberType.CreateVariantPropertiesStruct()
		base := properties.Base()
		base.Os_count = osCount
		return properties
func newOsTypeSpecificInfo(osType android.OsType, variantPropertiesFactory variantPropertiesFactoryFunc, osTypeVariants []android.SdkAware) *osTypeSpecificInfo {
	osInfo := &osTypeSpecificInfo{
		osType: osType,
	}

	osTypeToInfo := make(map[android.OsType]*osTypeSpecificInfo)

	// The set of properties that are common across all architectures and os types.
	commonProperties := variantPropertiesFactory()
	commonProperties.Base().Os = android.CommonOS

	// Create common value extractor that can be used to optimize the properties.
	commonValueExtractor := newCommonValueExtractor(commonProperties)

	// The list of property structures which are os type specific but common across
	// architectures within that os type.
	var osSpecificPropertiesList []android.SdkMemberProperties

	for osType, osTypeVariants := range variantsByOsType {
		// Group the properties for each variant by arch type within the os.
		osInfo := &osTypeSpecificInfo{}
		osTypeToInfo[osType] = osInfo

	osSpecificVariantPropertiesFactory := func() android.SdkMemberProperties {
		properties := variantPropertiesFactory()
		properties.Base().Os = osType
		return properties
	}

		// Add the os specific properties to a list of os type specific yet architecture
		// independent properties structs.
	// Create a structure into which properties common across the architectures in
	// this os type will be stored.
	osInfo.Properties = osSpecificVariantPropertiesFactory()
		osSpecificPropertiesList = append(osSpecificPropertiesList, osInfo.Properties)

	// Group the variants by arch type.
	var variantsByArchName = make(map[string][]android.SdkAware)
@@ -915,6 +853,17 @@ func (s *sdk) createMemberSnapshot(sdkModuleContext android.ModuleContext, build
		}
	}

	return osInfo
}

// Optimize the properties by extracting common properties from arch type specific
// properties into os type specific properties.
func (osInfo *osTypeSpecificInfo) optimizeProperties(commonValueExtractor *commonValueExtractor) {
	// Nothing to do if there is only a single common architecture.
	if len(osInfo.archInfos) == 0 {
		return
	}

	var archPropertiesList []android.SdkMemberProperties
	for _, archInfo := range osInfo.archInfos {
		archPropertiesList = append(archPropertiesList, archInfo.Properties)
@@ -938,22 +887,15 @@ func (s *sdk) createMemberSnapshot(sdkModuleContext android.ModuleContext, build
	osInfo.Properties.Base().Compile_multilib = multilib
}

	// Extract properties which are common across all architectures and os types.
	commonValueExtractor.extractCommonProperties(commonProperties, osSpecificPropertiesList)

	// Add the common properties to the module.
	commonProperties.AddToPropertySet(sdkModuleContext, builder, bpModule)

	// Create a target property set into which target specific properties can be
	// added.
	targetPropertySet := bpModule.AddPropertySet("target")

	// Iterate over the os types in a fixed order.
	for _, osType := range s.getPossibleOsTypes() {
		osInfo := osTypeToInfo[osType]
		if osInfo == nil {
			continue
		}
// Add the properties for an os to a property set.
//
// Maps the properties related to the os variants through to an appropriate
// module structure that will produce equivalent set of variants when it is
// processed in a build.
func (osInfo *osTypeSpecificInfo) addToPropertySet(
	builder *snapshotBuilder,
	bpModule android.BpModule,
	targetPropertySet android.BpPropertySet) {

	var osPropertySet android.BpPropertySet
	var archPropertySet android.BpPropertySet
@@ -993,6 +935,7 @@ func (s *sdk) createMemberSnapshot(sdkModuleContext android.ModuleContext, build
		//     <arch and os specific sections, e.g. android_x86>
		//   }
		//
		osType := osInfo.osType
		osPropertySet = targetPropertySet.AddPropertySet(osType.Name)
		archPropertySet = targetPropertySet

@@ -1001,16 +944,114 @@ func (s *sdk) createMemberSnapshot(sdkModuleContext android.ModuleContext, build
		archOsPrefix = osType.Name + "_"
	}

		osInfo.Properties.AddToPropertySet(sdkModuleContext, builder, osPropertySet)
	// Add the os specific but arch independent properties to the module.
	osInfo.Properties.AddToPropertySet(builder.ctx, builder, osPropertySet)

	// Add arch (and possibly os) specific sections for each set of arch (and possibly
	// os) specific properties.
	//
	// The archInfos list will be empty if the os contains variants for the common
	// architecture.
	for _, archInfo := range osInfo.archInfos {
		archInfo.addToPropertySet(builder, archPropertySet, archOsPrefix)
	}
}

type archTypeSpecificInfo struct {
	baseInfo

	archType android.ArchType
}

// Create a new archTypeSpecificInfo for the specified arch type and its properties
// structures populated with information from the variants.
func newArchSpecificInfo(archType android.ArchType, variantPropertiesFactory variantPropertiesFactoryFunc, archVariants []android.SdkAware) *archTypeSpecificInfo {

	if len(archVariants) != 1 {
		panic(fmt.Errorf("expected one arch specific variant but found %d", len(archVariants)))
	}

	// Create an arch specific info into which the variant properties can be copied.
	archInfo := &archTypeSpecificInfo{archType: archType}

	// Create the properties into which the arch type specific properties will be
	// added.
	archInfo.Properties = variantPropertiesFactory()
	archInfo.Properties.PopulateFromVariant(archVariants[0])

	return archInfo
}

// Add the properties for an arch type to a property set.
func (archInfo *archTypeSpecificInfo) addToPropertySet(builder *snapshotBuilder, archPropertySet android.BpPropertySet, archOsPrefix string) {
	archTypeName := archInfo.archType.Name
	archTypePropertySet := archPropertySet.AddPropertySet(archOsPrefix + archTypeName)
	archInfo.Properties.AddToPropertySet(builder.ctx, builder, archTypePropertySet)
}

func (s *sdk) createMemberSnapshot(sdkModuleContext android.ModuleContext, builder *snapshotBuilder, member *sdkMember, bpModule android.BpModule) {

	memberType := member.memberType

	// Group the variants by os type.
	variantsByOsType := make(map[android.OsType][]android.SdkAware)
	variants := member.Variants()
	for _, variant := range variants {
		osType := variant.Target().Os
		variantsByOsType[osType] = append(variantsByOsType[osType], variant)
	}

	osCount := len(variantsByOsType)
	variantPropertiesFactory := func() android.SdkMemberProperties {
		properties := memberType.CreateVariantPropertiesStruct()
		base := properties.Base()
		base.Os_count = osCount
		return properties
	}

	osTypeToInfo := make(map[android.OsType]*osTypeSpecificInfo)

	// The set of properties that are common across all architectures and os types.
	commonProperties := variantPropertiesFactory()
	commonProperties.Base().Os = android.CommonOS

	// Create common value extractor that can be used to optimize the properties.
	commonValueExtractor := newCommonValueExtractor(commonProperties)

	// The list of property structures which are os type specific but common across
	// architectures within that os type.
	var osSpecificPropertiesList []android.SdkMemberProperties

	for osType, osTypeVariants := range variantsByOsType {
		osInfo := newOsTypeSpecificInfo(osType, variantPropertiesFactory, osTypeVariants)
		osTypeToInfo[osType] = osInfo
		// Add the os specific properties to a list of os type specific yet architecture
		// independent properties structs.
		osSpecificPropertiesList = append(osSpecificPropertiesList, osInfo.Properties)

		// Optimize the properties across all the variants for a specific os type.
		osInfo.optimizeProperties(commonValueExtractor)
	}

	// Extract properties which are common across all architectures and os types.
	commonValueExtractor.extractCommonProperties(commonProperties, osSpecificPropertiesList)

	// Add the common properties to the module.
	commonProperties.AddToPropertySet(sdkModuleContext, builder, bpModule)

	// Create a target property set into which target specific properties can be
	// added.
	targetPropertySet := bpModule.AddPropertySet("target")

	// Iterate over the os types in a fixed order.
	for _, osType := range s.getPossibleOsTypes() {
		osInfo := osTypeToInfo[osType]
		if osInfo == nil {
			continue
		}

		osInfo.addToPropertySet(builder, bpModule, targetPropertySet)
	}
}

// Compute the list of possible os types that this sdk could support.