Make new module creation API more flexible

Previously passing additional information to the implementations of
AddPrebuiltModule() or the SdkMemberProperties interface would have
required making changes to the API. This change added an
SdkMemberContext object into which additional information can easily
be added without requiring changes to existing implementations.

The BuildSnapshot() method was not modified because it is deprecated
and will be removed in a follow up change.

It also switches the API from passing variants as android.SdkAware to
android.Module. That is for a couple of reasons:
1) SdkAware is designed for managing the relationship between the
   module and the SDK, not for generating the output snapshot. As such
   there is nothing in SdkAware that is needed for generating the
   output snapshot.
2) Accepting android.Module instead makes it easier to use the
   underlying code for generating the snapshot module as well as the
   individual member modules.

This is in preparation for a number of improvements and bug fixes in
both the snapshot creation code and implementations to address found
while trying to built the platform against ART prebuilts.

Bug: 151937654
Bug: 153306490
Test: m nothing
Merged-In: Iac10f1200c0f283aa35402167eec8f9aeb65a38e
Change-Id: Iac10f1200c0f283aa35402167eec8f9aeb65a38e
This commit is contained in:
Paul Duffin 2020-03-19 16:11:18 +00:00
parent 5e2e0fd191
commit 17ab883cb0
5 changed files with 77 additions and 49 deletions

View File

@ -356,7 +356,7 @@ type SdkMemberType interface {
// structure and calls AddToPropertySet(...) on the properties struct to add the member
// specific properties in the correct place in the structure.
//
AddPrebuiltModule(sdkModuleContext ModuleContext, builder SnapshotBuilder, member SdkMember) BpModule
AddPrebuiltModule(ctx SdkMemberContext, member SdkMember) BpModule
// Create a structure into which variant specific properties can be added.
CreateVariantPropertiesStruct() SdkMemberProperties
@ -385,7 +385,7 @@ func (b *SdkMemberTypeBase) BuildSnapshot(sdkModuleContext ModuleContext, builde
panic("override AddPrebuiltModule")
}
func (b *SdkMemberTypeBase) AddPrebuiltModule(sdkModuleContext ModuleContext, builder SnapshotBuilder, member SdkMember) BpModule {
func (b *SdkMemberTypeBase) AddPrebuiltModule(ctx SdkMemberContext, member SdkMember) BpModule {
// Returning nil causes the legacy BuildSnapshot method to be used.
return nil
}
@ -500,9 +500,19 @@ type SdkMemberProperties interface {
// Access the base structure.
Base() *SdkMemberPropertiesBase
// Populate the structure with information from the variant.
PopulateFromVariant(variant SdkAware)
// Populate this structure with information from the variant.
PopulateFromVariant(ctx SdkMemberContext, variant Module)
// Add the information from the structure to the property set.
AddToPropertySet(sdkModuleContext ModuleContext, builder SnapshotBuilder, propertySet BpPropertySet)
// Add the information from this structure to the property set.
AddToPropertySet(ctx SdkMemberContext, propertySet BpPropertySet)
}
// Provides access to information common to a specific member.
type SdkMemberContext interface {
// The module context of the sdk common os variant which is creating the snapshot.
SdkModuleContext() ModuleContext
// The builder of the snapshot.
SnapshotBuilder() SnapshotBuilder
}

View File

@ -63,9 +63,8 @@ func (mt *binarySdkMemberType) IsInstance(module android.Module) bool {
return false
}
func (mt *binarySdkMemberType) AddPrebuiltModule(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) android.BpModule {
pbm := builder.AddPrebuiltModule(member, "cc_prebuilt_binary")
return pbm
func (mt *binarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
return ctx.SnapshotBuilder().AddPrebuiltModule(member, "cc_prebuilt_binary")
}
func (mt *binarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
@ -107,7 +106,7 @@ type nativeBinaryInfoProperties struct {
SystemSharedLibs []string
}
func (p *nativeBinaryInfoProperties) PopulateFromVariant(variant android.SdkAware) {
func (p *nativeBinaryInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
ccModule := variant.(*Module)
p.archType = ccModule.Target().Arch.ArchType.String()
@ -122,11 +121,12 @@ func (p *nativeBinaryInfoProperties) PopulateFromVariant(variant android.SdkAwar
}
}
func (p *nativeBinaryInfoProperties) AddToPropertySet(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, propertySet android.BpPropertySet) {
func (p *nativeBinaryInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
if p.Compile_multilib != "" {
propertySet.AddProperty("compile_multilib", p.Compile_multilib)
}
builder := ctx.SnapshotBuilder()
if p.outputFile != nil {
propertySet.AddProperty("srcs", []string{nativeBinaryPathFor(*p)})

View File

@ -106,8 +106,8 @@ func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
return false
}
func (mt *librarySdkMemberType) AddPrebuiltModule(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) android.BpModule {
pbm := builder.AddPrebuiltModule(member, mt.prebuiltModuleType)
func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
pbm := ctx.SnapshotBuilder().AddPrebuiltModule(member, mt.prebuiltModuleType)
ccModule := member.Variants()[0].(*Module)
@ -332,7 +332,7 @@ type nativeLibInfoProperties struct {
outputFile android.Path
}
func (p *nativeLibInfoProperties) PopulateFromVariant(variant android.SdkAware) {
func (p *nativeLibInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
ccModule := variant.(*Module)
// If the library has some link types then it produces an output binary file, otherwise it
@ -365,6 +365,6 @@ func (p *nativeLibInfoProperties) PopulateFromVariant(variant android.SdkAware)
p.exportedGeneratedHeaders = ccModule.ExportedGeneratedHeaders()
}
func (p *nativeLibInfoProperties) AddToPropertySet(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, propertySet android.BpPropertySet) {
addPossiblyArchSpecificProperties(sdkModuleContext, builder, p, propertySet)
func (p *nativeLibInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
addPossiblyArchSpecificProperties(ctx.SdkModuleContext(), ctx.SnapshotBuilder(), p, propertySet)
}

View File

@ -1891,8 +1891,8 @@ func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
return ok
}
func (mt *librarySdkMemberType) AddPrebuiltModule(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) android.BpModule {
return builder.AddPrebuiltModule(member, "java_import")
func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
}
func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
@ -1908,15 +1908,18 @@ type librarySdkMemberProperties struct {
jarToExport android.Path
}
func (p *librarySdkMemberProperties) PopulateFromVariant(variant android.SdkAware) {
func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
j := variant.(*Library)
p.library = j
p.jarToExport = p.memberType.jarToExportGetter(j)
}
func (p *librarySdkMemberProperties) AddToPropertySet(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, propertySet android.BpPropertySet) {
func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
if p.jarToExport != nil {
sdkModuleContext := ctx.SdkModuleContext()
builder := ctx.SnapshotBuilder()
exportedJar := p.jarToExport
snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), p.library.Name())
builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
@ -2096,8 +2099,8 @@ func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
return ok
}
func (mt *testSdkMemberType) AddPrebuiltModule(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) android.BpModule {
return builder.AddPrebuiltModule(member, "java_test_import")
func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
}
func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
@ -2111,7 +2114,7 @@ type testSdkMemberProperties struct {
jarToExport android.Path
}
func (p *testSdkMemberProperties) PopulateFromVariant(variant android.SdkAware) {
func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
test := variant.(*Test)
implementationJars := test.ImplementationJars()
@ -2123,8 +2126,10 @@ func (p *testSdkMemberProperties) PopulateFromVariant(variant android.SdkAware)
p.jarToExport = implementationJars[0]
}
func (p *testSdkMemberProperties) AddToPropertySet(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, propertySet android.BpPropertySet) {
func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
if p.jarToExport != nil {
builder := ctx.SnapshotBuilder()
snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), p.test.Name())
builder.CopyToSnapshot(p.jarToExport, snapshotRelativeJavaLibPath)

View File

@ -254,12 +254,15 @@ func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) andro
members, multilib := s.organizeMembers(ctx, memberRefs)
for _, member := range members {
memberType := member.memberType
prebuiltModule := memberType.AddPrebuiltModule(ctx, builder, member)
memberCtx := &memberContext{ctx, builder}
prebuiltModule := memberType.AddPrebuiltModule(memberCtx, member)
if prebuiltModule == nil {
// Fall back to legacy method of building a snapshot
memberType.BuildSnapshot(ctx, builder, member)
} else {
s.createMemberSnapshot(ctx, builder, member, prebuiltModule)
s.createMemberSnapshot(memberCtx, member, prebuiltModule)
}
}
@ -831,7 +834,7 @@ type variantPropertiesFactoryFunc func() android.SdkMemberProperties
// Create a new osTypeSpecificInfo for the specified os type and its properties
// structures populated with information from the variants.
func newOsTypeSpecificInfo(osType android.OsType, variantPropertiesFactory variantPropertiesFactoryFunc, osTypeVariants []android.SdkAware) *osTypeSpecificInfo {
func newOsTypeSpecificInfo(ctx android.SdkMemberContext, osType android.OsType, variantPropertiesFactory variantPropertiesFactoryFunc, osTypeVariants []android.Module) *osTypeSpecificInfo {
osInfo := &osTypeSpecificInfo{
osType: osType,
}
@ -847,7 +850,7 @@ func newOsTypeSpecificInfo(osType android.OsType, variantPropertiesFactory varia
osInfo.Properties = osSpecificVariantPropertiesFactory()
// Group the variants by arch type.
var variantsByArchName = make(map[string][]android.SdkAware)
var variantsByArchName = make(map[string][]android.Module)
var archTypes []android.ArchType
for _, variant := range osTypeVariants {
archType := variant.Target().Arch.ArchType
@ -866,14 +869,14 @@ func newOsTypeSpecificInfo(osType android.OsType, variantPropertiesFactory varia
// A common arch type only has one variant and its properties should be treated
// as common to the os type.
osInfo.Properties.PopulateFromVariant(commonVariants[0])
osInfo.Properties.PopulateFromVariant(ctx, commonVariants[0])
} else {
// Create an arch specific info for each supported architecture type.
for _, archType := range archTypes {
archTypeName := archType.Name
archVariants := variantsByArchName[archTypeName]
archInfo := newArchSpecificInfo(archType, osSpecificVariantPropertiesFactory, archVariants)
archInfo := newArchSpecificInfo(ctx, archType, osSpecificVariantPropertiesFactory, archVariants)
osInfo.archInfos = append(osInfo.archInfos, archInfo)
}
@ -912,10 +915,7 @@ func (osInfo *osTypeSpecificInfo) optimizeProperties(commonValueExtractor *commo
// 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) {
func (osInfo *osTypeSpecificInfo) addToPropertySet(ctx *memberContext, bpModule android.BpModule, targetPropertySet android.BpPropertySet) {
var osPropertySet android.BpPropertySet
var archPropertySet android.BpPropertySet
@ -965,7 +965,7 @@ func (osInfo *osTypeSpecificInfo) addToPropertySet(
}
// Add the os specific but arch independent properties to the module.
osInfo.Properties.AddToPropertySet(builder.ctx, builder, osPropertySet)
osInfo.Properties.AddToPropertySet(ctx, osPropertySet)
// Add arch (and possibly os) specific sections for each set of arch (and possibly
// os) specific properties.
@ -973,7 +973,7 @@ func (osInfo *osTypeSpecificInfo) addToPropertySet(
// 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)
archInfo.addToPropertySet(ctx, archPropertySet, archOsPrefix)
}
}
@ -987,7 +987,7 @@ type archTypeSpecificInfo struct {
// 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 {
func newArchSpecificInfo(ctx android.SdkMemberContext, archType android.ArchType, variantPropertiesFactory variantPropertiesFactoryFunc, archVariants []android.Module) *archTypeSpecificInfo {
// Create an arch specific info into which the variant properties can be copied.
archInfo := &archTypeSpecificInfo{archType: archType}
@ -997,7 +997,7 @@ func newArchSpecificInfo(archType android.ArchType, variantPropertiesFactory var
archInfo.Properties = variantPropertiesFactory()
if len(archVariants) == 1 {
archInfo.Properties.PopulateFromVariant(archVariants[0])
archInfo.Properties.PopulateFromVariant(ctx, archVariants[0])
} else {
// There is more than one variant for this arch type which must be differentiated
// by link type.
@ -1006,7 +1006,7 @@ func newArchSpecificInfo(archType android.ArchType, variantPropertiesFactory var
if linkType == "" {
panic(fmt.Errorf("expected one arch specific variant as it is not identified by link type but found %d", len(archVariants)))
} else {
linkInfo := newLinkSpecificInfo(linkType, variantPropertiesFactory, linkVariant)
linkInfo := newLinkSpecificInfo(ctx, linkType, variantPropertiesFactory, linkVariant)
archInfo.linkInfos = append(archInfo.linkInfos, linkInfo)
}
@ -1052,14 +1052,14 @@ func (archInfo *archTypeSpecificInfo) optimizeProperties(commonValueExtractor *c
}
// Add the properties for an arch type to a property set.
func (archInfo *archTypeSpecificInfo) addToPropertySet(builder *snapshotBuilder, archPropertySet android.BpPropertySet, archOsPrefix string) {
func (archInfo *archTypeSpecificInfo) addToPropertySet(ctx *memberContext, archPropertySet android.BpPropertySet, archOsPrefix string) {
archTypeName := archInfo.archType.Name
archTypePropertySet := archPropertySet.AddPropertySet(archOsPrefix + archTypeName)
archInfo.Properties.AddToPropertySet(builder.ctx, builder, archTypePropertySet)
archInfo.Properties.AddToPropertySet(ctx, archTypePropertySet)
for _, linkInfo := range archInfo.linkInfos {
linkPropertySet := archTypePropertySet.AddPropertySet(linkInfo.linkType)
linkInfo.Properties.AddToPropertySet(builder.ctx, builder, linkPropertySet)
linkInfo.Properties.AddToPropertySet(ctx, linkPropertySet)
}
}
@ -1071,7 +1071,7 @@ type linkTypeSpecificInfo struct {
// Create a new linkTypeSpecificInfo for the specified link type and its properties
// structures populated with information from the variant.
func newLinkSpecificInfo(linkType string, variantPropertiesFactory variantPropertiesFactoryFunc, linkVariant android.SdkAware) *linkTypeSpecificInfo {
func newLinkSpecificInfo(ctx android.SdkMemberContext, linkType string, variantPropertiesFactory variantPropertiesFactoryFunc, linkVariant android.Module) *linkTypeSpecificInfo {
linkInfo := &linkTypeSpecificInfo{
baseInfo: baseInfo{
// Create the properties into which the link type specific properties will be
@ -1080,16 +1080,29 @@ func newLinkSpecificInfo(linkType string, variantPropertiesFactory variantProper
},
linkType: linkType,
}
linkInfo.Properties.PopulateFromVariant(linkVariant)
linkInfo.Properties.PopulateFromVariant(ctx, linkVariant)
return linkInfo
}
func (s *sdk) createMemberSnapshot(sdkModuleContext android.ModuleContext, builder *snapshotBuilder, member *sdkMember, bpModule android.BpModule) {
type memberContext struct {
sdkMemberContext android.ModuleContext
builder *snapshotBuilder
}
func (m *memberContext) SdkModuleContext() android.ModuleContext {
return m.sdkMemberContext
}
func (m *memberContext) SnapshotBuilder() android.SnapshotBuilder {
return m.builder
}
func (s *sdk) createMemberSnapshot(ctx *memberContext, member *sdkMember, bpModule android.BpModule) {
memberType := member.memberType
// Group the variants by os type.
variantsByOsType := make(map[android.OsType][]android.SdkAware)
variantsByOsType := make(map[android.OsType][]android.Module)
variants := member.Variants()
for _, variant := range variants {
osType := variant.Target().Os
@ -1118,7 +1131,7 @@ func (s *sdk) createMemberSnapshot(sdkModuleContext android.ModuleContext, build
var osSpecificPropertiesList []android.SdkMemberProperties
for osType, osTypeVariants := range variantsByOsType {
osInfo := newOsTypeSpecificInfo(osType, variantPropertiesFactory, osTypeVariants)
osInfo := newOsTypeSpecificInfo(ctx, osType, variantPropertiesFactory, osTypeVariants)
osTypeToInfo[osType] = osInfo
// Add the os specific properties to a list of os type specific yet architecture
// independent properties structs.
@ -1132,7 +1145,7 @@ func (s *sdk) createMemberSnapshot(sdkModuleContext android.ModuleContext, build
commonValueExtractor.extractCommonProperties(commonProperties, osSpecificPropertiesList)
// Add the common properties to the module.
commonProperties.AddToPropertySet(sdkModuleContext, builder, bpModule)
commonProperties.AddToPropertySet(ctx, bpModule)
// Create a target property set into which target specific properties can be
// added.
@ -1145,7 +1158,7 @@ func (s *sdk) createMemberSnapshot(sdkModuleContext android.ModuleContext, build
continue
}
osInfo.addToPropertySet(builder, bpModule, targetPropertySet)
osInfo.addToPropertySet(ctx, bpModule, targetPropertySet)
}
}