Merge "Refactor java_library/java_test snapshot processing"

This commit is contained in:
Treehugger Robot
2020-03-09 13:14:32 +00:00
committed by Gerrit Code Review
2 changed files with 113 additions and 70 deletions

View File

@@ -1879,12 +1879,12 @@ const (
) )
// path to the jar file of a java library. Relative to <sdk_root>/<api_dir> // path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
func sdkSnapshotFilePathForJar(member android.SdkMember) string { func sdkSnapshotFilePathForJar(name string) string {
return sdkSnapshotFilePathForMember(member, jarFileSuffix) return sdkSnapshotFilePathForMember(name, jarFileSuffix)
} }
func sdkSnapshotFilePathForMember(member android.SdkMember, suffix string) string { func sdkSnapshotFilePathForMember(name string, suffix string) string {
return filepath.Join(javaDir, member.Name()+suffix) return filepath.Join(javaDir, name+suffix)
} }
type librarySdkMemberType struct { type librarySdkMemberType struct {
@@ -1904,32 +1904,46 @@ func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
return ok return ok
} }
func (mt *librarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) { func (mt *librarySdkMemberType) AddPrebuiltModule(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) android.BpModule {
return builder.AddPrebuiltModule(member, "java_import")
}
variants := member.Variants() func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
if len(variants) != 1 { return &librarySdkMemberProperties{memberType: mt}
sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name()) }
for _, variant := range variants {
sdkModuleContext.ModuleErrorf(" %q", variant) type librarySdkMemberProperties struct {
} android.SdkMemberPropertiesBase
}
variant := variants[0] memberType *librarySdkMemberType
library *Library
jarToExport android.Path
}
func (p *librarySdkMemberProperties) PopulateFromVariant(variant android.SdkAware) {
j := variant.(*Library) j := variant.(*Library)
exportedJar := mt.jarToExportGetter(j) p.library = j
snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member) p.jarToExport = p.memberType.jarToExportGetter(j)
builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath) }
for _, dir := range j.AidlIncludeDirs() { func (p *librarySdkMemberProperties) AddToPropertySet(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, propertySet android.BpPropertySet) {
// TODO(jiyong): copy parcelable declarations only if p.jarToExport != nil {
aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil) exportedJar := p.jarToExport
for _, file := range aidlFiles { snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.library.Name())
builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file)) builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
for _, dir := range p.library.AidlIncludeDirs() {
// TODO(jiyong): copy parcelable declarations only
aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
for _, file := range aidlFiles {
builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
}
} }
}
module := builder.AddPrebuiltModule(member, "java_import") propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
module.AddProperty("jars", []string{snapshotRelativeJavaLibPath}) }
} }
var javaHeaderLibsSdkMemberType android.SdkMemberType = &librarySdkMemberType{ var javaHeaderLibsSdkMemberType android.SdkMemberType = &librarySdkMemberType{
@@ -2094,31 +2108,44 @@ func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
return ok return ok
} }
func (mt *testSdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) { func (mt *testSdkMemberType) AddPrebuiltModule(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) android.BpModule {
variants := member.Variants() return builder.AddPrebuiltModule(member, "java_test_import")
if len(variants) != 1 { }
sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
for _, variant := range variants {
sdkModuleContext.ModuleErrorf(" %q", variant)
}
}
variant := variants[0]
j := variant.(*Test)
implementationJars := j.ImplementationJars() func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
return &testSdkMemberProperties{}
}
type testSdkMemberProperties struct {
android.SdkMemberPropertiesBase
test *Test
jarToExport android.Path
}
func (p *testSdkMemberProperties) PopulateFromVariant(variant android.SdkAware) {
test := variant.(*Test)
implementationJars := test.ImplementationJars()
if len(implementationJars) != 1 { if len(implementationJars) != 1 {
panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name())) panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
} }
snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member) p.test = test
builder.CopyToSnapshot(implementationJars[0], snapshotRelativeJavaLibPath) p.jarToExport = implementationJars[0]
}
snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(member, testConfigSuffix) func (p *testSdkMemberProperties) AddToPropertySet(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, propertySet android.BpPropertySet) {
builder.CopyToSnapshot(j.testConfig, snapshotRelativeTestConfigPath) if p.jarToExport != nil {
snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.test.Name())
builder.CopyToSnapshot(p.jarToExport, snapshotRelativeJavaLibPath)
module := builder.AddPrebuiltModule(member, "java_test_import") snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.test.Name(), testConfigSuffix)
module.AddProperty("jars", []string{snapshotRelativeJavaLibPath}) builder.CopyToSnapshot(p.test.testConfig, snapshotRelativeTestConfigPath)
module.AddProperty("test_config", snapshotRelativeTestConfigPath)
propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
}
} }
// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and // java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and

View File

@@ -676,50 +676,66 @@ func (s *sdk) createMemberSnapshot(sdkModuleContext android.ModuleContext, build
osInfo := &osTypeSpecificInfo{} osInfo := &osTypeSpecificInfo{}
osInfo.Properties = memberType.CreateVariantPropertiesStruct() osInfo.Properties = memberType.CreateVariantPropertiesStruct()
variants := member.Variants() variants := member.Variants()
commonArch := false
for _, variant := range variants { for _, variant := range variants {
var properties android.SdkMemberProperties var properties android.SdkMemberProperties
// Get the info associated with the arch type inside the os info. // Get the info associated with the arch type inside the os info.
archType := variant.Target().Arch.ArchType archType := variant.Target().Arch.ArchType
archInfo := &archTypeSpecificInfo{archType: archType} if archType.Name == "common" {
properties = memberType.CreateVariantPropertiesStruct() // The arch type is common so populate the common properties directly.
archInfo.Properties = properties properties = osInfo.Properties
osInfo.archTypes = append(osInfo.archTypes, archInfo) commonArch = true
} else {
archInfo := &archTypeSpecificInfo{archType: archType}
properties = memberType.CreateVariantPropertiesStruct()
archInfo.Properties = properties
osInfo.archTypes = append(osInfo.archTypes, archInfo)
}
properties.PopulateFromVariant(variant) properties.PopulateFromVariant(variant)
} }
var archProperties []android.SdkMemberProperties if commonArch {
for _, archInfo := range osInfo.archTypes { if len(variants) != 1 {
archProperties = append(archProperties, archInfo.Properties) panic("Expected to only have 1 variant when arch type is common but found " + string(len(variants)))
} }
} else {
extractCommonProperties(osInfo.Properties, archProperties) var archProperties []android.SdkMemberProperties
for _, archInfo := range osInfo.archTypes {
// Choose setting for compile_multilib that is appropriate for the arch variants supplied. archProperties = append(archProperties, archInfo.Properties)
var multilib string
archVariantCount := len(osInfo.archTypes)
if archVariantCount == 2 {
multilib = "both"
} else if archVariantCount == 1 {
if strings.HasSuffix(osInfo.archTypes[0].archType.Name, "64") {
multilib = "64"
} else {
multilib = "32"
} }
}
osInfo.Properties.Base().Compile_multilib = multilib extractCommonProperties(osInfo.Properties, archProperties)
// Choose setting for compile_multilib that is appropriate for the arch variants supplied.
var multilib string
archVariantCount := len(osInfo.archTypes)
if archVariantCount == 2 {
multilib = "both"
} else if archVariantCount == 1 {
if strings.HasSuffix(osInfo.archTypes[0].archType.Name, "64") {
multilib = "64"
} else {
multilib = "32"
}
}
osInfo.Properties.Base().Compile_multilib = multilib
}
osInfo.Properties.AddToPropertySet(sdkModuleContext, builder, bpModule) osInfo.Properties.AddToPropertySet(sdkModuleContext, builder, bpModule)
archPropertySet := bpModule.AddPropertySet("arch") if !commonArch {
for _, av := range osInfo.archTypes { archPropertySet := bpModule.AddPropertySet("arch")
archTypePropertySet := archPropertySet.AddPropertySet(av.archType.Name) for _, av := range osInfo.archTypes {
archTypePropertySet := archPropertySet.AddPropertySet(av.archType.Name)
av.Properties.AddToPropertySet(sdkModuleContext, builder, archTypePropertySet) av.Properties.AddToPropertySet(sdkModuleContext, builder, archTypePropertySet)
}
} }
memberType.FinalizeModule(sdkModuleContext, builder, member, bpModule) memberType.FinalizeModule(sdkModuleContext, builder, member, bpModule)