From 138795772757d9d19a44514f130b154dddecb4e7 Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Thu, 28 Nov 2019 14:31:38 +0000 Subject: [PATCH 1/5] Parameterize the sdk member processing Extracts the type specific functionality into the SdkMemberType interface which has to be implemented by each module type that can be added as a member of the sdk. It provides functionality to add the required dependencies for the module type, check to see if a resolved module is the correct instance and build the snapshot. The latter was previously part of SdkAware but was moved because it has to be able to process multiple SdkAware variants so delegating it to a single instance did not make sense. The custom code for handling each member type specific property, e.g. java_libs, has been replaced with common code that processes a list of sdkMemberListProperty struct which associates the property (name and getter) with the SdkMemberType and a special DependencyTag which is passed to the SdkMemberType when it has to add dependencies. The DependencyTag contains a reference to the appropriate sdkMemberListProperty which allows the resolved dependencies to be grouped by type. Previously, the dependency collection methods would ignore a module if it was an unsupported type because they did not have a way of determining which property it was initially listed in. That meant it was possible to add say a droidstubs module to the java_libs property (and because they had the same variants) it would work as if it was added to the stubs_sources property. Or alternatively, a module of an unsupported type could be added to any property and it would just be ignored. However, the DependencyTag provides information about which property a resolved module was referenced in and so it can detect when the resolved module is of the wrong type and report an error. That check identified a bug in one of the tests where the sdk referenced a java_import module (which is not allowed in an sdk) instead of a java_library module (which is allowed). That test was fixed as part of this. A list of sdkMemberListProperty structs defines the member properties supported by the sdk and are processed in order to ensure consistent behaviour. The resolved dependencies are grouped by type and each group is then processed in defined order. Within each type dependencies are grouped by name and encapsulated behind an SdkMember interface which includes the name and the list of variants. The Droidstubs and java.Library types can only support one variant and will fail if given more. The processing for the native_shared_libs property has been moved into the cc/library.go file so the sdk package code should now have no type specific information in it apart from what is if the list of sdkMemberListProperty structs. Bug: 143678475 Test: m conscrypt-module-sdk Change-Id: I10203594d33dbf53441f655aff124f9ab3538d87 --- android/sdk.go | 57 ++++++++- cc/library.go | 228 +++++++++++++++++++++++++++++++++++ java/droiddoc.go | 23 +++- java/java.go | 26 +++- sdk/sdk.go | 72 +++++++---- sdk/sdk_test.go | 2 +- sdk/update.go | 305 +++++++++-------------------------------------- 7 files changed, 438 insertions(+), 275 deletions(-) diff --git a/android/sdk.go b/android/sdk.go index 01e18ed4b..b9220ca69 100644 --- a/android/sdk.go +++ b/android/sdk.go @@ -17,6 +17,7 @@ package android import ( "strings" + "github.com/google/blueprint" "github.com/google/blueprint/proptools" ) @@ -31,9 +32,6 @@ type SdkAware interface { MemberName() string BuildWithSdks(sdks SdkRefs) RequiredSdks() SdkRefs - - // Build a snapshot of the module. - BuildSnapshot(sdkModuleContext ModuleContext, builder SnapshotBuilder) } // SdkRef refers to a version of an SDK @@ -203,3 +201,56 @@ type BpPropertySet interface { type BpModule interface { BpPropertySet } + +// An individual member of the SDK, includes all of the variants that the SDK +// requires. +type SdkMember interface { + // The name of the member. + Name() string + + // All the variants required by the SDK. + Variants() []SdkAware +} + +// Interface that must be implemented for every type that can be a member of an +// sdk. +// +// The basic implementation should look something like this, where ModuleType is +// the name of the module type being supported. +// +// var ModuleTypeSdkMemberType = newModuleTypeSdkMemberType() +// +// func newModuleTypeSdkMemberType() android.SdkMemberType { +// return &moduleTypeSdkMemberType{} +// } +// +// type moduleTypeSdkMemberType struct { +// } +// +// ...methods... +// +type SdkMemberType interface { + // Add dependencies from the SDK module to all the variants the member + // contributes to the SDK. The exact set of variants required is determined + // by the SDK and its properties. The dependencies must be added with the + // supplied tag. + // + // The BottomUpMutatorContext provided is for the SDK module. + AddDependencies(mctx BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) + + // Return true if the supplied module is an instance of this member type. + // + // This is used to check the type of each variant before added to the + // SdkMember. Returning false will cause an error to be logged expaining that + // the module is not allowed in whichever sdk property it was added. + IsInstance(module Module) bool + + // Build the snapshot for the SDK member + // + // The ModuleContext provided is for the SDK module, so information for + // variants in the supplied member can be accessed using the Other... methods. + // + // The SdkMember is guaranteed to contain variants for which the + // IsInstance(Module) method returned true. + BuildSnapshot(sdkModuleContext ModuleContext, builder SnapshotBuilder, member SdkMember) +} diff --git a/cc/library.go b/cc/library.go index 98cae3d49..60b00b193 100644 --- a/cc/library.go +++ b/cc/library.go @@ -24,6 +24,7 @@ import ( "strings" "sync" + "github.com/google/blueprint" "github.com/google/blueprint/pathtools" "android/soong/android" @@ -1403,3 +1404,230 @@ func maybeInjectBoringSSLHash(ctx android.ModuleContext, outputFile android.Modu return outputFile } + +var LibrarySdkMemberType = &librarySdkMemberType{} + +type librarySdkMemberType struct { +} + +func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) { + targets := mctx.MultiTargets() + for _, lib := range names { + for _, target := range targets { + name, version := StubsLibNameAndVersion(lib) + if version == "" { + version = LatestStubsVersionFor(mctx.Config(), name) + } + mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{ + {Mutator: "image", Variation: android.CoreVariation}, + {Mutator: "link", Variation: "shared"}, + {Mutator: "version", Variation: version}, + }...), dependencyTag, name) + } + } +} + +func (mt *librarySdkMemberType) IsInstance(module android.Module) bool { + _, ok := module.(*Module) + return ok +} + +// copy exported header files and stub *.so files +func (mt *librarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) { + info := organizeVariants(member) + buildSharedNativeLibSnapshot(sdkModuleContext, info, builder) +} + +func buildSharedNativeLibSnapshot(sdkModuleContext android.ModuleContext, info *nativeLibInfo, builder android.SnapshotBuilder) { + // a function for emitting include dirs + printExportedDirCopyCommandsForNativeLibs := func(lib archSpecificNativeLibInfo) { + includeDirs := lib.exportedIncludeDirs + includeDirs = append(includeDirs, lib.exportedSystemIncludeDirs...) + if len(includeDirs) == 0 { + return + } + for _, dir := range includeDirs { + if _, gen := dir.(android.WritablePath); gen { + // generated headers are copied via exportedDeps. See below. + continue + } + targetDir := nativeIncludeDir + if info.hasArchSpecificFlags { + targetDir = filepath.Join(lib.archType, targetDir) + } + + // TODO(jiyong) copy headers having other suffixes + headers, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.h", nil) + for _, file := range headers { + src := android.PathForSource(sdkModuleContext, file) + dest := filepath.Join(targetDir, file) + builder.CopyToSnapshot(src, dest) + } + } + + genHeaders := lib.exportedDeps + for _, file := range genHeaders { + targetDir := nativeGeneratedIncludeDir + if info.hasArchSpecificFlags { + targetDir = filepath.Join(lib.archType, targetDir) + } + dest := filepath.Join(targetDir, lib.name, file.Rel()) + builder.CopyToSnapshot(file, dest) + } + } + + if !info.hasArchSpecificFlags { + printExportedDirCopyCommandsForNativeLibs(info.archVariants[0]) + } + + // for each architecture + for _, av := range info.archVariants { + builder.CopyToSnapshot(av.outputFile, nativeStubFilePathFor(av)) + + if info.hasArchSpecificFlags { + printExportedDirCopyCommandsForNativeLibs(av) + } + } + + info.generatePrebuiltLibrary(sdkModuleContext, builder) +} + +func (info *nativeLibInfo) generatePrebuiltLibrary(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder) { + + // a function for emitting include dirs + addExportedDirsForNativeLibs := func(lib archSpecificNativeLibInfo, properties android.BpPropertySet, systemInclude bool) { + includeDirs := nativeIncludeDirPathsFor(lib, systemInclude, info.hasArchSpecificFlags) + if len(includeDirs) == 0 { + return + } + var propertyName string + if !systemInclude { + propertyName = "export_include_dirs" + } else { + propertyName = "export_system_include_dirs" + } + properties.AddProperty(propertyName, includeDirs) + } + + pbm := builder.AddPrebuiltModule(info.name, "cc_prebuilt_library_shared") + + if !info.hasArchSpecificFlags { + addExportedDirsForNativeLibs(info.archVariants[0], pbm, false /*systemInclude*/) + addExportedDirsForNativeLibs(info.archVariants[0], pbm, true /*systemInclude*/) + } + + archProperties := pbm.AddPropertySet("arch") + for _, av := range info.archVariants { + archTypeProperties := archProperties.AddPropertySet(av.archType) + archTypeProperties.AddProperty("srcs", []string{nativeStubFilePathFor(av)}) + if info.hasArchSpecificFlags { + // export_* properties are added inside the arch: {: {...}} block + addExportedDirsForNativeLibs(av, archTypeProperties, false /*systemInclude*/) + addExportedDirsForNativeLibs(av, archTypeProperties, true /*systemInclude*/) + } + } + pbm.AddProperty("stl", "none") + pbm.AddProperty("system_shared_libs", []string{}) +} + +const ( + nativeIncludeDir = "include" + nativeGeneratedIncludeDir = "include_gen" + nativeStubDir = "lib" + nativeStubFileSuffix = ".so" +) + +// path to the stub file of a native shared library. Relative to / +func nativeStubFilePathFor(lib archSpecificNativeLibInfo) string { + return filepath.Join(lib.archType, + nativeStubDir, lib.name+nativeStubFileSuffix) +} + +// paths to the include dirs of a native shared library. Relative to / +func nativeIncludeDirPathsFor(lib archSpecificNativeLibInfo, systemInclude bool, archSpecific bool) []string { + var result []string + var includeDirs []android.Path + if !systemInclude { + includeDirs = lib.exportedIncludeDirs + } else { + includeDirs = lib.exportedSystemIncludeDirs + } + for _, dir := range includeDirs { + var path string + if _, gen := dir.(android.WritablePath); gen { + path = filepath.Join(nativeGeneratedIncludeDir, lib.name) + } else { + path = filepath.Join(nativeIncludeDir, dir.String()) + } + if archSpecific { + path = filepath.Join(lib.archType, path) + } + result = append(result, path) + } + return result +} + +// archSpecificNativeLibInfo represents an arch-specific variant of a native lib +type archSpecificNativeLibInfo struct { + name string + archType string + exportedIncludeDirs android.Paths + exportedSystemIncludeDirs android.Paths + exportedFlags []string + exportedDeps android.Paths + outputFile android.Path +} + +func (lib *archSpecificNativeLibInfo) signature() string { + return fmt.Sprintf("%v %v %v %v", + lib.name, + lib.exportedIncludeDirs.Strings(), + lib.exportedSystemIncludeDirs.Strings(), + lib.exportedFlags) +} + +// nativeLibInfo represents a collection of arch-specific modules having the same name +type nativeLibInfo struct { + name string + archVariants []archSpecificNativeLibInfo + // hasArchSpecificFlags is set to true if modules for each architecture all have the same + // include dirs, flags, etc, in which case only those of the first arch is selected. + hasArchSpecificFlags bool +} + +// Organize the variants by architecture. +func organizeVariants(member android.SdkMember) *nativeLibInfo { + info := &nativeLibInfo{name: member.Name()} + + for _, variant := range member.Variants() { + ccModule := variant.(*Module) + + info.archVariants = append(info.archVariants, archSpecificNativeLibInfo{ + name: ccModule.BaseModuleName(), + archType: ccModule.Target().Arch.ArchType.String(), + exportedIncludeDirs: ccModule.ExportedIncludeDirs(), + exportedSystemIncludeDirs: ccModule.ExportedSystemIncludeDirs(), + exportedFlags: ccModule.ExportedFlags(), + exportedDeps: ccModule.ExportedDeps(), + outputFile: ccModule.OutputFile().Path(), + }) + } + + // Determine if include dirs and flags for each variant are different across arch-specific + // variants or not. And set hasArchSpecificFlags accordingly + // by default, include paths and flags are assumed to be the same across arches + info.hasArchSpecificFlags = false + oldSignature := "" + for _, av := range info.archVariants { + newSignature := av.signature() + if oldSignature == "" { + oldSignature = newSignature + } + if oldSignature != newSignature { + info.hasArchSpecificFlags = true + break + } + } + + return info +} diff --git a/java/droiddoc.go b/java/droiddoc.go index 16e6921e1..45a72e14d 100644 --- a/java/droiddoc.go +++ b/java/droiddoc.go @@ -19,6 +19,7 @@ import ( "path/filepath" "strings" + "github.com/google/blueprint" "github.com/google/blueprint/proptools" "android/soong/android" @@ -1973,7 +1974,27 @@ func PrebuiltStubsSourcesFactory() android.Module { return module } -func (d *Droidstubs) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder) { +var DroidStubsSdkMemberType = &droidStubsSdkMemberType{} + +type droidStubsSdkMemberType struct { +} + +func (mt *droidStubsSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) { + mctx.AddVariationDependencies(nil, dependencyTag, names...) +} + +func (mt *droidStubsSdkMemberType) IsInstance(module android.Module) bool { + _, ok := module.(*Droidstubs) + return ok +} + +func (mt *droidStubsSdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) { + variants := member.Variants() + if len(variants) != 1 { + sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name()) + } + variant := variants[0] + d, _ := variant.(*Droidstubs) stubsSrcJar := d.stubsSrcJar snapshotRelativeDir := filepath.Join("java", d.Name()+"_stubs_sources") diff --git a/java/java.go b/java/java.go index 9c0fcbaa0..0334ca49a 100644 --- a/java/java.go +++ b/java/java.go @@ -1721,7 +1721,31 @@ func (j *Library) javaStubFilePathFor() string { return filepath.Join(javaStubDir, j.Name()+javaStubFileSuffix) } -func (j *Library) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder) { +var LibrarySdkMemberType = &librarySdkMemberType{} + +type librarySdkMemberType struct { +} + +func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) { + mctx.AddVariationDependencies(nil, dependencyTag, names...) +} + +func (mt *librarySdkMemberType) IsInstance(module android.Module) bool { + _, ok := module.(*Library) + return ok +} + +func (mt *librarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) { + variants := member.Variants() + 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.(*Library) + headerJars := j.HeaderJars() if len(headerJars) != 1 { panic(fmt.Errorf("there must be only one header jar from %q", j.Name())) diff --git a/sdk/sdk.go b/sdk/sdk.go index 18b0040b9..80dd088c8 100644 --- a/sdk/sdk.go +++ b/sdk/sdk.go @@ -27,6 +27,7 @@ import ( // registered before mutators in this package. See RegisterPostDepsMutators for more details. _ "android/soong/apex" "android/soong/cc" + "android/soong/java" ) func init() { @@ -37,6 +38,14 @@ func init() { android.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory) android.PreDepsMutators(RegisterPreDepsMutators) android.PostDepsMutators(RegisterPostDepsMutators) + + // Populate the dependency tags for each member list property. This needs to + // be done here to break an initialization cycle. + for _, memberListProperty := range sdkMemberListProperties { + memberListProperty.dependencyTag = &sdkMemberDependencyTag{ + memberListProperty: memberListProperty, + } + } } type sdk struct { @@ -62,6 +71,45 @@ type sdkProperties struct { Snapshot bool `blueprint:"mutated"` } +type sdkMemberDependencyTag struct { + blueprint.BaseDependencyTag + memberListProperty *sdkMemberListProperty +} + +// Contains information about the sdk properties that list sdk members, e.g. +// Java_libs. +type sdkMemberListProperty struct { + // the name of the property as used in a .bp file + name string + + // getter for the list of member names + getter func(properties *sdkProperties) []string + + // the type of member referenced in the list + memberType android.SdkMemberType + + // the dependency tag used for items in this list. + dependencyTag *sdkMemberDependencyTag +} + +var sdkMemberListProperties = []*sdkMemberListProperty{ + { + name: "java_libs", + getter: func(properties *sdkProperties) []string { return properties.Java_libs }, + memberType: java.LibrarySdkMemberType, + }, + { + name: "stubs_sources", + getter: func(properties *sdkProperties) []string { return properties.Stubs_sources }, + memberType: java.DroidStubsSdkMemberType, + }, + { + name: "native_shared_libs", + getter: func(properties *sdkProperties) []string { return properties.Native_shared_libs }, + memberType: cc.LibrarySdkMemberType, + }, +} + // sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.) // which Mainline modules like APEX can choose to build with. func ModuleFactory() android.Module { @@ -145,10 +193,6 @@ type dependencyTag struct { blueprint.BaseDependencyTag } -// For dependencies from an SDK module to its members -// e.g. mysdk -> libfoo and libbar -var sdkMemberDepTag dependencyTag - // For dependencies from an in-development version of an SDK member to frozen versions of the same member // e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12 type sdkMemberVesionedDepTag struct { @@ -160,22 +204,10 @@ type sdkMemberVesionedDepTag struct { // Step 1: create dependencies from an SDK module to its members. func memberMutator(mctx android.BottomUpMutatorContext) { if m, ok := mctx.Module().(*sdk); ok { - mctx.AddVariationDependencies(nil, sdkMemberDepTag, m.properties.Java_libs...) - mctx.AddVariationDependencies(nil, sdkMemberDepTag, m.properties.Stubs_sources...) - - targets := mctx.MultiTargets() - for _, target := range targets { - for _, lib := range m.properties.Native_shared_libs { - name, version := cc.StubsLibNameAndVersion(lib) - if version == "" { - version = cc.LatestStubsVersionFor(mctx.Config(), name) - } - mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{ - {Mutator: "image", Variation: android.CoreVariation}, - {Mutator: "link", Variation: "shared"}, - {Mutator: "version", Variation: version}, - }...), sdkMemberDepTag, name) - } + for _, memberListProperty := range sdkMemberListProperties { + names := memberListProperty.getter(&m.properties) + tag := memberListProperty.dependencyTag + memberListProperty.memberType.AddDependencies(mctx, tag, names) } } } diff --git a/sdk/sdk_test.go b/sdk/sdk_test.go index 6c2065998..2f0e59858 100644 --- a/sdk/sdk_test.go +++ b/sdk/sdk_test.go @@ -158,7 +158,7 @@ func TestBasicSdkWithJava(t *testing.T) { ctx, _ := testSdk(t, ` sdk { name: "mysdk", - java_libs: ["sdkmember"], + java_libs: ["myjavalib"], } sdk_snapshot { diff --git a/sdk/update.go b/sdk/update.go index 63bb1b73b..602e0a498 100644 --- a/sdk/update.go +++ b/sdk/update.go @@ -16,7 +16,6 @@ package sdk import ( "fmt" - "path/filepath" "reflect" "strings" @@ -24,8 +23,6 @@ import ( "github.com/google/blueprint/proptools" "android/soong/android" - "android/soong/cc" - "android/soong/java" ) var pctx = android.NewPackageContext("android/soong/sdk") @@ -105,105 +102,46 @@ func (gf *generatedFile) build(pctx android.PackageContext, ctx android.BuilderC rb.Build(pctx, ctx, gf.path.Base(), "Build "+gf.path.Base()) } -func (s *sdk) javaLibs(ctx android.ModuleContext) []android.SdkAware { - result := []android.SdkAware{} +// Collect all the members. +// +// The members are first grouped by type and then grouped by name. The order of +// the types is the order they are referenced in sdkMemberListProperties. The +// names are in order in which the dependencies were added. +func collectMembers(ctx android.ModuleContext) []*sdkMember { + byType := make(map[android.SdkMemberType][]*sdkMember) + byName := make(map[string]*sdkMember) + ctx.VisitDirectDeps(func(m android.Module) { - if j, ok := m.(*java.Library); ok { - result = append(result, j) - } - }) - return result -} + tag := ctx.OtherModuleDependencyTag(m) + if memberTag, ok := tag.(*sdkMemberDependencyTag); ok { + memberListProperty := memberTag.memberListProperty + memberType := memberListProperty.memberType -func (s *sdk) stubsSources(ctx android.ModuleContext) []android.SdkAware { - result := []android.SdkAware{} - ctx.VisitDirectDeps(func(m android.Module) { - if j, ok := m.(*java.Droidstubs); ok { - result = append(result, j) - } - }) - return result -} - -// archSpecificNativeLibInfo represents an arch-specific variant of a native lib -type archSpecificNativeLibInfo struct { - name string - archType string - exportedIncludeDirs android.Paths - exportedSystemIncludeDirs android.Paths - exportedFlags []string - exportedDeps android.Paths - outputFile android.Path -} - -func (lib *archSpecificNativeLibInfo) signature() string { - return fmt.Sprintf("%v %v %v %v", - lib.name, - lib.exportedIncludeDirs.Strings(), - lib.exportedSystemIncludeDirs.Strings(), - lib.exportedFlags) -} - -// nativeLibInfo represents a collection of arch-specific modules having the same name -type nativeLibInfo struct { - name string - archVariants []archSpecificNativeLibInfo - // hasArchSpecificFlags is set to true if modules for each architecture all have the same - // include dirs, flags, etc, in which case only those of the first arch is selected. - hasArchSpecificFlags bool -} - -// nativeMemberInfos collects all cc.Modules that are member of an SDK. -func (s *sdk) nativeMemberInfos(ctx android.ModuleContext) []*nativeLibInfo { - infoMap := make(map[string]*nativeLibInfo) - - // Collect cc.Modules - ctx.VisitDirectDeps(func(m android.Module) { - ccModule, ok := m.(*cc.Module) - if !ok { - return - } - depName := ctx.OtherModuleName(m) - - if _, ok := infoMap[depName]; !ok { - infoMap[depName] = &nativeLibInfo{name: depName} - } - - info := infoMap[depName] - info.archVariants = append(info.archVariants, archSpecificNativeLibInfo{ - name: ccModule.BaseModuleName(), - archType: ccModule.Target().Arch.ArchType.String(), - exportedIncludeDirs: ccModule.ExportedIncludeDirs(), - exportedSystemIncludeDirs: ccModule.ExportedSystemIncludeDirs(), - exportedFlags: ccModule.ExportedFlags(), - exportedDeps: ccModule.ExportedDeps(), - outputFile: ccModule.OutputFile().Path(), - }) - }) - - // Determine if include dirs and flags for each module are different across arch-specific - // modules or not. And set hasArchSpecificFlags accordingly - for _, info := range infoMap { - // by default, include paths and flags are assumed to be the same across arches - info.hasArchSpecificFlags = false - oldSignature := "" - for _, av := range info.archVariants { - newSignature := av.signature() - if oldSignature == "" { - oldSignature = newSignature + // Make sure that the resolved module is allowed in the member list property. + if !memberType.IsInstance(m) { + ctx.ModuleErrorf("module %q is not valid in property %s", ctx.OtherModuleName(m), memberListProperty.name) } - if oldSignature != newSignature { - info.hasArchSpecificFlags = true - break + + name := ctx.OtherModuleName(m) + + member := byName[name] + if member == nil { + member = &sdkMember{memberType: memberType, name: name} + byName[name] = member + byType[memberType] = append(byType[memberType], member) } + + member.variants = append(member.variants, m.(android.SdkAware)) } + }) + + var members []*sdkMember + for _, memberListProperty := range sdkMemberListProperties { + membersOfType := byType[memberListProperty.memberType] + members = append(members, membersOfType...) } - var list []*nativeLibInfo - for _, v := range infoMap { - list = append(list, v) - } - return list + return members } // SDK directory structure @@ -224,44 +162,6 @@ func (s *sdk) nativeMemberInfos(ctx android.ModuleContext) []*nativeLibInfo { // /lib/ // libFoo.so : a stub library -const ( - nativeIncludeDir = "include" - nativeGeneratedIncludeDir = "include_gen" - nativeStubDir = "lib" - nativeStubFileSuffix = ".so" -) - -// path to the stub file of a native shared library. Relative to / -func nativeStubFilePathFor(lib archSpecificNativeLibInfo) string { - return filepath.Join(lib.archType, - nativeStubDir, lib.name+nativeStubFileSuffix) -} - -// paths to the include dirs of a native shared library. Relative to / -func nativeIncludeDirPathsFor(ctx android.ModuleContext, lib archSpecificNativeLibInfo, - systemInclude bool, archSpecific bool) []string { - var result []string - var includeDirs []android.Path - if !systemInclude { - includeDirs = lib.exportedIncludeDirs - } else { - includeDirs = lib.exportedSystemIncludeDirs - } - for _, dir := range includeDirs { - var path string - if _, gen := dir.(android.WritablePath); gen { - path = filepath.Join(nativeGeneratedIncludeDir, lib.name) - } else { - path = filepath.Join(nativeIncludeDir, dir.String()) - } - if archSpecific { - path = filepath.Join(lib.archType, path) - } - result = append(result, path) - } - return result -} - // A name that uniquely identifies a prebuilt SDK member for a version of SDK snapshot // This isn't visible to users, so could be changed in future. func versionedSdkMemberName(ctx android.ModuleContext, memberName string, version string) string { @@ -290,22 +190,8 @@ func (s *sdk) buildSnapshot(ctx android.ModuleContext) android.OutputPath { } s.builderForTests = builder - // copy exported AIDL files and stub jar files - javaLibs := s.javaLibs(ctx) - for _, m := range javaLibs { - m.BuildSnapshot(ctx, builder) - } - - // copy stubs sources - stubsSources := s.stubsSources(ctx) - for _, m := range stubsSources { - m.BuildSnapshot(ctx, builder) - } - - // copy exported header files and stub *.so files - nativeLibInfos := s.nativeMemberInfos(ctx) - for _, info := range nativeLibInfos { - buildSharedNativeLibSnapshot(ctx, info, builder) + for _, member := range collectMembers(ctx) { + member.memberType.BuildSnapshot(ctx, builder, member) } for _, unversioned := range builder.prebuiltOrder { @@ -326,14 +212,11 @@ func (s *sdk) buildSnapshot(ctx android.ModuleContext) android.OutputPath { snapshotModule := bpFile.newModule("sdk_snapshot") snapshotModule.AddProperty("name", snapshotName) addHostDeviceSupportedProperties(&s.ModuleBase, snapshotModule) - if len(s.properties.Java_libs) > 0 { - snapshotModule.AddProperty("java_libs", builder.versionedSdkMemberNames(s.properties.Java_libs)) - } - if len(s.properties.Stubs_sources) > 0 { - snapshotModule.AddProperty("stubs_sources", builder.versionedSdkMemberNames(s.properties.Stubs_sources)) - } - if len(s.properties.Native_shared_libs) > 0 { - snapshotModule.AddProperty("native_shared_libs", builder.versionedSdkMemberNames(s.properties.Native_shared_libs)) + for _, memberListProperty := range sdkMemberListProperties { + names := memberListProperty.getter(&s.properties) + if len(names) > 0 { + snapshotModule.AddProperty(memberListProperty.name, builder.versionedSdkMemberNames(names)) + } } bpFile.AddModule(snapshotModule) @@ -442,98 +325,6 @@ func (s *sdk) GetAndroidBpContentsForTests() string { return contents.content.String() } -func buildSharedNativeLibSnapshot(ctx android.ModuleContext, info *nativeLibInfo, builder android.SnapshotBuilder) { - // a function for emitting include dirs - printExportedDirCopyCommandsForNativeLibs := func(lib archSpecificNativeLibInfo) { - includeDirs := lib.exportedIncludeDirs - includeDirs = append(includeDirs, lib.exportedSystemIncludeDirs...) - if len(includeDirs) == 0 { - return - } - for _, dir := range includeDirs { - if _, gen := dir.(android.WritablePath); gen { - // generated headers are copied via exportedDeps. See below. - continue - } - targetDir := nativeIncludeDir - if info.hasArchSpecificFlags { - targetDir = filepath.Join(lib.archType, targetDir) - } - - // TODO(jiyong) copy headers having other suffixes - headers, _ := ctx.GlobWithDeps(dir.String()+"/**/*.h", nil) - for _, file := range headers { - src := android.PathForSource(ctx, file) - dest := filepath.Join(targetDir, file) - builder.CopyToSnapshot(src, dest) - } - } - - genHeaders := lib.exportedDeps - for _, file := range genHeaders { - targetDir := nativeGeneratedIncludeDir - if info.hasArchSpecificFlags { - targetDir = filepath.Join(lib.archType, targetDir) - } - dest := filepath.Join(targetDir, lib.name, file.Rel()) - builder.CopyToSnapshot(file, dest) - } - } - - if !info.hasArchSpecificFlags { - printExportedDirCopyCommandsForNativeLibs(info.archVariants[0]) - } - - // for each architecture - for _, av := range info.archVariants { - builder.CopyToSnapshot(av.outputFile, nativeStubFilePathFor(av)) - - if info.hasArchSpecificFlags { - printExportedDirCopyCommandsForNativeLibs(av) - } - } - - info.generatePrebuiltLibrary(ctx, builder) -} - -func (info *nativeLibInfo) generatePrebuiltLibrary(ctx android.ModuleContext, builder android.SnapshotBuilder) { - - // a function for emitting include dirs - addExportedDirsForNativeLibs := func(lib archSpecificNativeLibInfo, properties android.BpPropertySet, systemInclude bool) { - includeDirs := nativeIncludeDirPathsFor(ctx, lib, systemInclude, info.hasArchSpecificFlags) - if len(includeDirs) == 0 { - return - } - var propertyName string - if !systemInclude { - propertyName = "export_include_dirs" - } else { - propertyName = "export_system_include_dirs" - } - properties.AddProperty(propertyName, includeDirs) - } - - pbm := builder.AddPrebuiltModule(info.name, "cc_prebuilt_library_shared") - - if !info.hasArchSpecificFlags { - addExportedDirsForNativeLibs(info.archVariants[0], pbm, false /*systemInclude*/) - addExportedDirsForNativeLibs(info.archVariants[0], pbm, true /*systemInclude*/) - } - - archProperties := pbm.AddPropertySet("arch") - for _, av := range info.archVariants { - archTypeProperties := archProperties.AddPropertySet(av.archType) - archTypeProperties.AddProperty("srcs", []string{nativeStubFilePathFor(av)}) - if info.hasArchSpecificFlags { - // export_* properties are added inside the arch: {: {...}} block - addExportedDirsForNativeLibs(av, archTypeProperties, false /*systemInclude*/) - addExportedDirsForNativeLibs(av, archTypeProperties, true /*systemInclude*/) - } - } - pbm.AddProperty("stl", "none") - pbm.AddProperty("system_shared_libs", []string{}) -} - type snapshotBuilder struct { ctx android.ModuleContext sdk *sdk @@ -613,3 +404,19 @@ func (s *snapshotBuilder) versionedSdkMemberNames(members []string) []string { } return references } + +var _ android.SdkMember = (*sdkMember)(nil) + +type sdkMember struct { + memberType android.SdkMemberType + name string + variants []android.SdkAware +} + +func (m *sdkMember) Name() string { + return m.name +} + +func (m *sdkMember) Variants() []android.SdkAware { + return m.variants +} From 82d90438be3919feb9a746b0c08a23cced252e32 Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Sat, 30 Nov 2019 09:24:33 +0000 Subject: [PATCH 2/5] Separate sdk testing infrastructure from sdk tests Bug: 143678475 Test: m conscrypt-module-sdk Change-Id: Ib00870ddefc4c1dd9e42ca594e6ebe8e24c42e05 --- Android.bp | 1 + sdk/sdk_test.go | 173 +----------------------------------------- sdk/testing.go | 194 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 198 insertions(+), 170 deletions(-) create mode 100644 sdk/testing.go diff --git a/Android.bp b/Android.bp index 6910d7587..2cc69324f 100644 --- a/Android.bp +++ b/Android.bp @@ -509,6 +509,7 @@ bootstrap_go_package { ], testSrcs: [ "sdk/sdk_test.go", + "sdk/testing.go", ], pluginFor: ["soong_build"], } diff --git a/sdk/sdk_test.go b/sdk/sdk_test.go index 2f0e59858..9fea26049 100644 --- a/sdk/sdk_test.go +++ b/sdk/sdk_test.go @@ -15,143 +15,16 @@ package sdk import ( - "io/ioutil" - "os" "path/filepath" - "strings" "testing" "android/soong/android" - "android/soong/apex" "android/soong/cc" - "android/soong/java" ) -func testSdkContext(bp string) (*android.TestContext, android.Config) { - config := android.TestArchConfig(buildDir, nil) - ctx := android.NewTestArchContext() - - // from android package - ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) - ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) { - ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel() - }) - ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) { - ctx.TopDown("prebuilt_select", android.PrebuiltSelectModuleMutator).Parallel() - ctx.BottomUp("prebuilt_postdeps", android.PrebuiltPostDepsMutator).Parallel() - }) - - // from java package - ctx.RegisterModuleType("android_app_certificate", java.AndroidAppCertificateFactory) - ctx.RegisterModuleType("java_library", java.LibraryFactory) - ctx.RegisterModuleType("java_import", java.ImportFactory) - ctx.RegisterModuleType("droidstubs", java.DroidstubsFactory) - ctx.RegisterModuleType("prebuilt_stubs_sources", java.PrebuiltStubsSourcesFactory) - - // from cc package - ctx.RegisterModuleType("cc_library", cc.LibraryFactory) - ctx.RegisterModuleType("cc_library_shared", cc.LibrarySharedFactory) - ctx.RegisterModuleType("cc_object", cc.ObjectFactory) - ctx.RegisterModuleType("cc_prebuilt_library_shared", cc.PrebuiltSharedLibraryFactory) - ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory) - ctx.RegisterModuleType("llndk_library", cc.LlndkLibraryFactory) - ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory) - ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) { - ctx.BottomUp("image", android.ImageMutator).Parallel() - ctx.BottomUp("link", cc.LinkageMutator).Parallel() - ctx.BottomUp("vndk", cc.VndkMutator).Parallel() - ctx.BottomUp("test_per_src", cc.TestPerSrcMutator).Parallel() - ctx.BottomUp("version", cc.VersionMutator).Parallel() - ctx.BottomUp("begin", cc.BeginMutator).Parallel() - }) - - // from apex package - ctx.RegisterModuleType("apex", apex.BundleFactory) - ctx.RegisterModuleType("apex_key", apex.ApexKeyFactory) - ctx.PostDepsMutators(apex.RegisterPostDepsMutators) - - // from this package - ctx.RegisterModuleType("sdk", ModuleFactory) - ctx.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory) - ctx.PreDepsMutators(RegisterPreDepsMutators) - ctx.PostDepsMutators(RegisterPostDepsMutators) - - ctx.Register() - - bp = bp + ` - apex_key { - name: "myapex.key", - public_key: "myapex.avbpubkey", - private_key: "myapex.pem", - } - - android_app_certificate { - name: "myapex.cert", - certificate: "myapex", - } - ` + cc.GatherRequiredDepsForTest(android.Android) - - ctx.MockFileSystem(map[string][]byte{ - "Android.bp": []byte(bp), - "build/make/target/product/security": nil, - "apex_manifest.json": nil, - "system/sepolicy/apex/myapex-file_contexts": nil, - "system/sepolicy/apex/myapex2-file_contexts": nil, - "myapex.avbpubkey": nil, - "myapex.pem": nil, - "myapex.x509.pem": nil, - "myapex.pk8": nil, - "Test.java": nil, - "Test.cpp": nil, - "include/Test.h": nil, - "aidl/foo/bar/Test.aidl": nil, - "libfoo.so": nil, - "stubs-sources/foo/bar/Foo.java": nil, - "foo/bar/Foo.java": nil, - }) - - return ctx, config -} - -func testSdk(t *testing.T, bp string) (*android.TestContext, android.Config) { - ctx, config := testSdkContext(bp) - _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) - android.FailIfErrored(t, errs) - _, errs = ctx.PrepareBuildActions(config) - android.FailIfErrored(t, errs) - return ctx, config -} - -func testSdkError(t *testing.T, pattern, bp string) { - t.Helper() - ctx, config := testSdkContext(bp) - _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) - if len(errs) > 0 { - android.FailIfNoMatchingErrors(t, pattern, errs) - return - } - _, errs = ctx.PrepareBuildActions(config) - if len(errs) > 0 { - android.FailIfNoMatchingErrors(t, pattern, errs) - return - } - - t.Fatalf("missing expected error %q (0 errors are returned)", pattern) -} - -func ensureListContains(t *testing.T, result []string, expected string) { - t.Helper() - if !android.InList(expected, result) { - t.Errorf("%q is not found in %v", expected, result) - } -} - -func pathsToStrings(paths android.Paths) []string { - var ret []string - for _, p := range paths { - ret = append(ret, p.String()) - } - return ret +// Needed in an _test.go file in this package to ensure tests run correctly, particularly in IDE. +func TestMain(m *testing.M) { + runTestWithBuildDir(m) } func TestBasicSdkWithJava(t *testing.T) { @@ -802,43 +675,3 @@ sdk_snapshot { t.Errorf("Expected snapshot output to be %q but was %q", expectedOutputZip, outputZip) } } - -func checkSnapshotAndroidBpContents(t *testing.T, s *sdk, expectedContents string) { - t.Helper() - androidBpContents := strings.NewReplacer("\\n", "\n").Replace(s.GetAndroidBpContentsForTests()) - if androidBpContents != expectedContents { - t.Errorf("Android.bp contents do not match, expected %s, actual %s", expectedContents, androidBpContents) - } -} - -var buildDir string - -func setUp() { - var err error - buildDir, err = ioutil.TempDir("", "soong_sdk_test") - if err != nil { - panic(err) - } -} - -func tearDown() { - _ = os.RemoveAll(buildDir) -} - -func TestMain(m *testing.M) { - run := func() int { - setUp() - defer tearDown() - - return m.Run() - } - - os.Exit(run()) -} - -func SkipIfNotLinux(t *testing.T) { - t.Helper() - if android.BuildOs != android.Linux { - t.Skipf("Skipping as sdk snapshot generation is only supported on %s not %s", android.Linux, android.BuildOs) - } -} diff --git a/sdk/testing.go b/sdk/testing.go new file mode 100644 index 000000000..29815e0df --- /dev/null +++ b/sdk/testing.go @@ -0,0 +1,194 @@ +// Copyright 2019 Google Inc. All rights reserved. +// +// 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 ( + "io/ioutil" + "os" + "strings" + "testing" + + "android/soong/android" + "android/soong/apex" + "android/soong/cc" + "android/soong/java" +) + +func testSdkContext(bp string) (*android.TestContext, android.Config) { + config := android.TestArchConfig(buildDir, nil) + ctx := android.NewTestArchContext() + + // from android package + ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) + ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) { + ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel() + }) + ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) { + ctx.TopDown("prebuilt_select", android.PrebuiltSelectModuleMutator).Parallel() + ctx.BottomUp("prebuilt_postdeps", android.PrebuiltPostDepsMutator).Parallel() + }) + + // from java package + ctx.RegisterModuleType("android_app_certificate", java.AndroidAppCertificateFactory) + ctx.RegisterModuleType("java_library", java.LibraryFactory) + ctx.RegisterModuleType("java_import", java.ImportFactory) + ctx.RegisterModuleType("droidstubs", java.DroidstubsFactory) + ctx.RegisterModuleType("prebuilt_stubs_sources", java.PrebuiltStubsSourcesFactory) + + // from cc package + ctx.RegisterModuleType("cc_library", cc.LibraryFactory) + ctx.RegisterModuleType("cc_library_shared", cc.LibrarySharedFactory) + ctx.RegisterModuleType("cc_object", cc.ObjectFactory) + ctx.RegisterModuleType("cc_prebuilt_library_shared", cc.PrebuiltSharedLibraryFactory) + ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory) + ctx.RegisterModuleType("llndk_library", cc.LlndkLibraryFactory) + ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory) + ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) { + ctx.BottomUp("image", android.ImageMutator).Parallel() + ctx.BottomUp("link", cc.LinkageMutator).Parallel() + ctx.BottomUp("vndk", cc.VndkMutator).Parallel() + ctx.BottomUp("test_per_src", cc.TestPerSrcMutator).Parallel() + ctx.BottomUp("version", cc.VersionMutator).Parallel() + ctx.BottomUp("begin", cc.BeginMutator).Parallel() + }) + + // from apex package + ctx.RegisterModuleType("apex", apex.BundleFactory) + ctx.RegisterModuleType("apex_key", apex.ApexKeyFactory) + ctx.PostDepsMutators(apex.RegisterPostDepsMutators) + + // from this package + ctx.RegisterModuleType("sdk", ModuleFactory) + ctx.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory) + ctx.PreDepsMutators(RegisterPreDepsMutators) + ctx.PostDepsMutators(RegisterPostDepsMutators) + + ctx.Register() + + bp = bp + ` + apex_key { + name: "myapex.key", + public_key: "myapex.avbpubkey", + private_key: "myapex.pem", + } + + android_app_certificate { + name: "myapex.cert", + certificate: "myapex", + } + ` + cc.GatherRequiredDepsForTest(android.Android) + + ctx.MockFileSystem(map[string][]byte{ + "Android.bp": []byte(bp), + "build/make/target/product/security": nil, + "apex_manifest.json": nil, + "system/sepolicy/apex/myapex-file_contexts": nil, + "system/sepolicy/apex/myapex2-file_contexts": nil, + "myapex.avbpubkey": nil, + "myapex.pem": nil, + "myapex.x509.pem": nil, + "myapex.pk8": nil, + "Test.java": nil, + "Test.cpp": nil, + "include/Test.h": nil, + "aidl/foo/bar/Test.aidl": nil, + "libfoo.so": nil, + "stubs-sources/foo/bar/Foo.java": nil, + "foo/bar/Foo.java": nil, + }) + + return ctx, config +} + +func testSdk(t *testing.T, bp string) (*android.TestContext, android.Config) { + ctx, config := testSdkContext(bp) + _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) + android.FailIfErrored(t, errs) + _, errs = ctx.PrepareBuildActions(config) + android.FailIfErrored(t, errs) + return ctx, config +} + +func testSdkError(t *testing.T, pattern, bp string) { + t.Helper() + ctx, config := testSdkContext(bp) + _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) + if len(errs) > 0 { + android.FailIfNoMatchingErrors(t, pattern, errs) + return + } + _, errs = ctx.PrepareBuildActions(config) + if len(errs) > 0 { + android.FailIfNoMatchingErrors(t, pattern, errs) + return + } + + t.Fatalf("missing expected error %q (0 errors are returned)", pattern) +} + +func ensureListContains(t *testing.T, result []string, expected string) { + t.Helper() + if !android.InList(expected, result) { + t.Errorf("%q is not found in %v", expected, result) + } +} + +func pathsToStrings(paths android.Paths) []string { + var ret []string + for _, p := range paths { + ret = append(ret, p.String()) + } + return ret +} + +func checkSnapshotAndroidBpContents(t *testing.T, s *sdk, expectedContents string) { + t.Helper() + androidBpContents := strings.NewReplacer("\\n", "\n").Replace(s.GetAndroidBpContentsForTests()) + if androidBpContents != expectedContents { + t.Errorf("Android.bp contents do not match, expected %s, actual %s", expectedContents, androidBpContents) + } +} + +var buildDir string + +func setUp() { + var err error + buildDir, err = ioutil.TempDir("", "soong_sdk_test") + if err != nil { + panic(err) + } +} + +func tearDown() { + _ = os.RemoveAll(buildDir) +} + +func runTestWithBuildDir(m *testing.M) { + run := func() int { + setUp() + defer tearDown() + + return m.Run() + } + + os.Exit(run()) +} + +func SkipIfNotLinux(t *testing.T) { + t.Helper() + if android.BuildOs != android.Linux { + t.Skipf("Skipping as sdk snapshot generation is only supported on %s not %s", android.Linux, android.BuildOs) + } +} From c3c5d5e351b8ed863b59cf6cf68322b16061c1b8 Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Fri, 29 Nov 2019 20:45:22 +0000 Subject: [PATCH 3/5] Improve testing of sdk snapshot generation Adds TestHelper to provide general test helper functionality for use by any test. Adds testSdkResult, composed with TestHelper that encapsulates the result of processing and sdk {..} definition and provides specialized support for testing the build rules. Dedups the analysis of the sdk build rules, and improves it to extract more information, and in different forms. That is represented by the snapshotBuildInfo struct. Adds a CheckSnapshot() method which checks the snapshot for a specified sdk version. It takes a list of functions that can each perform a check on a specific facet of the supplied snapshotBuildInfo. Methods are provided for tests to use to check the following facets: * Generated Android.bp contents. * Copy rules * Merge zip inputs This approach makes it possible for each test to customize what is being checked without either duplicating functionality, causing a proliferation of specialized forms of the CheckSnapshot method for different types of tests or adding arguments for every possible check that any test would need which would lead to lots of churn to existing tests when new arguments are added. The main testing improvement is for CheckSnapshot() to actually try and load the Android.bp that is generated. In order to do that it was necessary to create a mock filesystem populated with information from the build rules, i.e. the destination files from every Cp command as well as the destination directory from every repackage zip command. That helps detect a number of sources of errors: * Failing to copy a file/directory that is mentioned in the generated Android.bp file. * Invalid properties. * Invalid format of the .bp file. * Integrity issues within the .bp file. Bug: 143678475 Test: m conscrypt-module-sdk Change-Id: I4d3fe18f86698186d18e7e8b32d2e319183f7f0c --- sdk/sdk_test.go | 184 ++++++++++++------------------------- sdk/testing.go | 239 +++++++++++++++++++++++++++++++++++++++++++++--- sdk/update.go | 1 - 3 files changed, 283 insertions(+), 141 deletions(-) diff --git a/sdk/sdk_test.go b/sdk/sdk_test.go index 9fea26049..0b8dfc9e5 100644 --- a/sdk/sdk_test.go +++ b/sdk/sdk_test.go @@ -15,10 +15,8 @@ package sdk import ( - "path/filepath" "testing" - "android/soong/android" "android/soong/cc" ) @@ -28,7 +26,7 @@ func TestMain(m *testing.M) { } func TestBasicSdkWithJava(t *testing.T) { - ctx, _ := testSdk(t, ` + result := testSdk(t, ` sdk { name: "mysdk", java_libs: ["myjavalib"], @@ -89,11 +87,11 @@ func TestBasicSdkWithJava(t *testing.T) { } `) - sdkMemberV1 := ctx.ModuleForTests("sdkmember_mysdk_1", "android_common_myapex").Rule("combineJar").Output - sdkMemberV2 := ctx.ModuleForTests("sdkmember_mysdk_2", "android_common_myapex2").Rule("combineJar").Output + sdkMemberV1 := result.ModuleForTests("sdkmember_mysdk_1", "android_common_myapex").Rule("combineJar").Output + sdkMemberV2 := result.ModuleForTests("sdkmember_mysdk_2", "android_common_myapex2").Rule("combineJar").Output - javalibForMyApex := ctx.ModuleForTests("myjavalib", "android_common_myapex") - javalibForMyApex2 := ctx.ModuleForTests("myjavalib", "android_common_myapex2") + javalibForMyApex := result.ModuleForTests("myjavalib", "android_common_myapex") + javalibForMyApex2 := result.ModuleForTests("myjavalib", "android_common_myapex2") // Depending on the uses_sdks value, different libs are linked ensureListContains(t, pathsToStrings(javalibForMyApex.Rule("javac").Implicits), sdkMemberV1.String()) @@ -101,7 +99,7 @@ func TestBasicSdkWithJava(t *testing.T) { } func TestBasicSdkWithCc(t *testing.T) { - ctx, _ := testSdk(t, ` + result := testSdk(t, ` sdk { name: "mysdk", native_shared_libs: ["sdkmember"], @@ -166,11 +164,11 @@ func TestBasicSdkWithCc(t *testing.T) { } `) - sdkMemberV1 := ctx.ModuleForTests("sdkmember_mysdk_1", "android_arm64_armv8-a_core_shared_myapex").Rule("toc").Output - sdkMemberV2 := ctx.ModuleForTests("sdkmember_mysdk_2", "android_arm64_armv8-a_core_shared_myapex2").Rule("toc").Output + sdkMemberV1 := result.ModuleForTests("sdkmember_mysdk_1", "android_arm64_armv8-a_core_shared_myapex").Rule("toc").Output + sdkMemberV2 := result.ModuleForTests("sdkmember_mysdk_2", "android_arm64_armv8-a_core_shared_myapex2").Rule("toc").Output - cpplibForMyApex := ctx.ModuleForTests("mycpplib", "android_arm64_armv8-a_core_shared_myapex") - cpplibForMyApex2 := ctx.ModuleForTests("mycpplib", "android_arm64_armv8-a_core_shared_myapex2") + cpplibForMyApex := result.ModuleForTests("mycpplib", "android_arm64_armv8-a_core_shared_myapex") + cpplibForMyApex2 := result.ModuleForTests("mycpplib", "android_arm64_armv8-a_core_shared_myapex2") // Depending on the uses_sdks value, different libs are linked ensureListContains(t, pathsToStrings(cpplibForMyApex.Rule("ld").Implicits), sdkMemberV1.String()) @@ -268,7 +266,7 @@ func TestDepNotInRequiredSdks(t *testing.T) { } func TestSdkIsCompileMultilibBoth(t *testing.T) { - ctx, _ := testSdk(t, ` + result := testSdk(t, ` sdk { name: "mysdk", native_shared_libs: ["sdkmember"], @@ -282,11 +280,11 @@ func TestSdkIsCompileMultilibBoth(t *testing.T) { } `) - armOutput := ctx.ModuleForTests("sdkmember", "android_arm_armv7-a-neon_core_shared").Module().(*cc.Module).OutputFile() - arm64Output := ctx.ModuleForTests("sdkmember", "android_arm64_armv8-a_core_shared").Module().(*cc.Module).OutputFile() + armOutput := result.Module("sdkmember", "android_arm_armv7-a-neon_core_shared").(*cc.Module).OutputFile() + arm64Output := result.Module("sdkmember", "android_arm64_armv8-a_core_shared").(*cc.Module).OutputFile() var inputs []string - buildParams := ctx.ModuleForTests("mysdk", "android_common").Module().BuildParamsForTests() + buildParams := result.Module("mysdk", "android_common").BuildParamsForTests() for _, bp := range buildParams { if bp.Input != nil { inputs = append(inputs, bp.Input.String()) @@ -299,7 +297,7 @@ func TestSdkIsCompileMultilibBoth(t *testing.T) { } func TestSnapshot(t *testing.T) { - ctx, config := testSdk(t, ` + result := testSdk(t, ` sdk { name: "mysdk", java_libs: ["myjavalib"], @@ -341,9 +339,9 @@ func TestSnapshot(t *testing.T) { } `) - sdk := ctx.ModuleForTests("mysdk", "android_common").Module().(*sdk) - - checkSnapshotAndroidBpContents(t, sdk, `// This is auto-generated. DO NOT EDIT. + result.CheckSnapshot("mysdk", "android_common", + checkAndroidBpContents(` +// This is auto-generated. DO NOT EDIT. java_import { name: "mysdk_myjavalib@current", @@ -421,65 +419,30 @@ sdk_snapshot { stubs_sources: ["mysdk_myjavaapistubs@current"], native_shared_libs: ["mysdk_mynativelib@current"], } - -`) - - var copySrcs []string - var copyDests []string - buildParams := sdk.BuildParamsForTests() - var mergeZipInputs []string - var intermediateZip string - var outputZip string - for _, bp := range buildParams { - ruleString := bp.Rule.String() - if ruleString == android.Cp.String() { - copySrcs = append(copySrcs, bp.Input.String()) - copyDests = append(copyDests, bp.Output.Rel()) // rooted at the snapshot root - } else if ruleString == zipFiles.String() { - intermediateZip = bp.Output.String() - } else if ruleString == mergeZips.String() { - input := bp.Input.String() - if intermediateZip != input { - t.Errorf("Intermediate zip %s is not an input to merge_zips, %s is used instead", intermediateZip, input) - } - mergeZipInputs = bp.Inputs.Strings() - outputZip = bp.Output.String() - } - } - - buildDir := config.BuildDir() - ensureListContains(t, copySrcs, "aidl/foo/bar/Test.aidl") - ensureListContains(t, copySrcs, "include/Test.h") - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/mynativelib/android_arm64_armv8-a_core_shared/gen/aidl/aidl/foo/bar/BnTest.h")) - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/mynativelib/android_arm64_armv8-a_core_shared/gen/aidl/aidl/foo/bar/BpTest.h")) - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/mynativelib/android_arm64_armv8-a_core_shared/gen/aidl/aidl/foo/bar/Test.h")) - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/myjavalib/android_common/turbine-combined/myjavalib.jar")) - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/mynativelib/android_arm64_armv8-a_core_shared/mynativelib.so")) - - ensureListContains(t, copyDests, "aidl/aidl/foo/bar/Test.aidl") - ensureListContains(t, copyDests, "arm64/include/include/Test.h") - ensureListContains(t, copyDests, "arm64/include_gen/mynativelib/aidl/foo/bar/BnTest.h") - ensureListContains(t, copyDests, "arm64/include_gen/mynativelib/aidl/foo/bar/BpTest.h") - ensureListContains(t, copyDests, "arm64/include_gen/mynativelib/aidl/foo/bar/Test.h") - ensureListContains(t, copyDests, "java/myjavalib.jar") - ensureListContains(t, copyDests, "arm64/lib/mynativelib.so") - - expectedOutputZip := filepath.Join(buildDir, ".intermediates/mysdk/android_common/mysdk-current.zip") - expectedRepackagedZip := filepath.Join(buildDir, ".intermediates/mysdk/android_common/tmp/java/myjavaapistubs_stubs_sources.zip") - - // Ensure that the droidstubs .srcjar as repackaged into a temporary zip file - // and then merged together with the intermediate snapshot zip. - ensureListContains(t, mergeZipInputs, expectedRepackagedZip) - if outputZip != expectedOutputZip { - t.Errorf("Expected snapshot output to be %q but was %q", expectedOutputZip, outputZip) - } +`), + checkAllCopyRules(` +.intermediates/myjavalib/android_common/turbine-combined/myjavalib.jar -> java/myjavalib.jar +aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl +.intermediates/mynativelib/android_arm64_armv8-a_core_shared/mynativelib.so -> arm64/lib/mynativelib.so +include/Test.h -> arm64/include/include/Test.h +.intermediates/mynativelib/android_arm64_armv8-a_core_shared/gen/aidl/aidl/foo/bar/Test.h -> arm64/include_gen/mynativelib/aidl/foo/bar/Test.h +.intermediates/mynativelib/android_arm64_armv8-a_core_shared/gen/aidl/aidl/foo/bar/BnTest.h -> arm64/include_gen/mynativelib/aidl/foo/bar/BnTest.h +.intermediates/mynativelib/android_arm64_armv8-a_core_shared/gen/aidl/aidl/foo/bar/BpTest.h -> arm64/include_gen/mynativelib/aidl/foo/bar/BpTest.h +.intermediates/mynativelib/android_arm_armv7-a-neon_core_shared/mynativelib.so -> arm/lib/mynativelib.so +include/Test.h -> arm/include/include/Test.h +.intermediates/mynativelib/android_arm_armv7-a-neon_core_shared/gen/aidl/aidl/foo/bar/Test.h -> arm/include_gen/mynativelib/aidl/foo/bar/Test.h +.intermediates/mynativelib/android_arm_armv7-a-neon_core_shared/gen/aidl/aidl/foo/bar/BnTest.h -> arm/include_gen/mynativelib/aidl/foo/bar/BnTest.h +.intermediates/mynativelib/android_arm_armv7-a-neon_core_shared/gen/aidl/aidl/foo/bar/BpTest.h -> arm/include_gen/mynativelib/aidl/foo/bar/BpTest.h +`), + checkMergeZip(".intermediates/mysdk/android_common/tmp/java/myjavaapistubs_stubs_sources.zip"), + ) } func TestHostSnapshot(t *testing.T) { // b/145598135 - Generating host snapshots for anything other than linux is not supported. SkipIfNotLinux(t) - ctx, config := testSdk(t, ` + result := testSdk(t, ` sdk { name: "mysdk", device_supported: false, @@ -528,9 +491,9 @@ func TestHostSnapshot(t *testing.T) { } `) - sdk := ctx.ModuleForTests("mysdk", "linux_glibc_common").Module().(*sdk) - - checkSnapshotAndroidBpContents(t, sdk, `// This is auto-generated. DO NOT EDIT. + result.CheckSnapshot("mysdk", "linux_glibc_common", + checkAndroidBpContents(` +// This is auto-generated. DO NOT EDIT. java_import { name: "mysdk_myjavalib@current", @@ -622,56 +585,21 @@ sdk_snapshot { stubs_sources: ["mysdk_myjavaapistubs@current"], native_shared_libs: ["mysdk_mynativelib@current"], } - -`) - - var copySrcs []string - var copyDests []string - buildParams := sdk.BuildParamsForTests() - var mergeZipInputs []string - var intermediateZip string - var outputZip string - for _, bp := range buildParams { - ruleString := bp.Rule.String() - if ruleString == android.Cp.String() { - copySrcs = append(copySrcs, bp.Input.String()) - copyDests = append(copyDests, bp.Output.Rel()) // rooted at the snapshot root - } else if ruleString == zipFiles.String() { - intermediateZip = bp.Output.String() - } else if ruleString == mergeZips.String() { - input := bp.Input.String() - if intermediateZip != input { - t.Errorf("Intermediate zip %s is not an input to merge_zips, %s is used instead", intermediateZip, input) - } - mergeZipInputs = bp.Inputs.Strings() - outputZip = bp.Output.String() - } - } - - buildDir := config.BuildDir() - ensureListContains(t, copySrcs, "aidl/foo/bar/Test.aidl") - ensureListContains(t, copySrcs, "include/Test.h") - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/mynativelib/linux_glibc_x86_64_shared/gen/aidl/aidl/foo/bar/BnTest.h")) - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/mynativelib/linux_glibc_x86_64_shared/gen/aidl/aidl/foo/bar/BpTest.h")) - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/mynativelib/linux_glibc_x86_64_shared/gen/aidl/aidl/foo/bar/Test.h")) - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/myjavalib/linux_glibc_common/javac/myjavalib.jar")) - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/mynativelib/linux_glibc_x86_64_shared/mynativelib.so")) - - ensureListContains(t, copyDests, "aidl/aidl/foo/bar/Test.aidl") - ensureListContains(t, copyDests, "x86_64/include/include/Test.h") - ensureListContains(t, copyDests, "x86_64/include_gen/mynativelib/aidl/foo/bar/BnTest.h") - ensureListContains(t, copyDests, "x86_64/include_gen/mynativelib/aidl/foo/bar/BpTest.h") - ensureListContains(t, copyDests, "x86_64/include_gen/mynativelib/aidl/foo/bar/Test.h") - ensureListContains(t, copyDests, "java/myjavalib.jar") - ensureListContains(t, copyDests, "x86_64/lib/mynativelib.so") - - expectedOutputZip := filepath.Join(buildDir, ".intermediates/mysdk/linux_glibc_common/mysdk-current.zip") - expectedRepackagedZip := filepath.Join(buildDir, ".intermediates/mysdk/linux_glibc_common/tmp/java/myjavaapistubs_stubs_sources.zip") - - // Ensure that the droidstubs .srcjar as repackaged into a temporary zip file - // and then merged together with the intermediate snapshot zip. - ensureListContains(t, mergeZipInputs, expectedRepackagedZip) - if outputZip != expectedOutputZip { - t.Errorf("Expected snapshot output to be %q but was %q", expectedOutputZip, outputZip) - } +`), + checkAllCopyRules(` +.intermediates/myjavalib/linux_glibc_common/javac/myjavalib.jar -> java/myjavalib.jar +aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl +.intermediates/mynativelib/linux_glibc_x86_64_shared/mynativelib.so -> x86_64/lib/mynativelib.so +include/Test.h -> x86_64/include/include/Test.h +.intermediates/mynativelib/linux_glibc_x86_64_shared/gen/aidl/aidl/foo/bar/Test.h -> x86_64/include_gen/mynativelib/aidl/foo/bar/Test.h +.intermediates/mynativelib/linux_glibc_x86_64_shared/gen/aidl/aidl/foo/bar/BnTest.h -> x86_64/include_gen/mynativelib/aidl/foo/bar/BnTest.h +.intermediates/mynativelib/linux_glibc_x86_64_shared/gen/aidl/aidl/foo/bar/BpTest.h -> x86_64/include_gen/mynativelib/aidl/foo/bar/BpTest.h +.intermediates/mynativelib/linux_glibc_x86_shared/mynativelib.so -> x86/lib/mynativelib.so +include/Test.h -> x86/include/include/Test.h +.intermediates/mynativelib/linux_glibc_x86_shared/gen/aidl/aidl/foo/bar/Test.h -> x86/include_gen/mynativelib/aidl/foo/bar/Test.h +.intermediates/mynativelib/linux_glibc_x86_shared/gen/aidl/aidl/foo/bar/BnTest.h -> x86/include_gen/mynativelib/aidl/foo/bar/BnTest.h +.intermediates/mynativelib/linux_glibc_x86_shared/gen/aidl/aidl/foo/bar/BpTest.h -> x86/include_gen/mynativelib/aidl/foo/bar/BpTest.h +`), + checkMergeZip(".intermediates/mysdk/linux_glibc_common/tmp/java/myjavaapistubs_stubs_sources.zip"), + ) } diff --git a/sdk/testing.go b/sdk/testing.go index 29815e0df..ec451edc0 100644 --- a/sdk/testing.go +++ b/sdk/testing.go @@ -15,8 +15,10 @@ package sdk import ( + "fmt" "io/ioutil" "os" + "path/filepath" "strings" "testing" @@ -26,7 +28,7 @@ import ( "android/soong/java" ) -func testSdkContext(bp string) (*android.TestContext, android.Config) { +func testSdkContext(bp string, fs map[string][]byte) (*android.TestContext, android.Config) { config := android.TestArchConfig(buildDir, nil) ctx := android.NewTestArchContext() @@ -90,7 +92,7 @@ func testSdkContext(bp string) (*android.TestContext, android.Config) { } ` + cc.GatherRequiredDepsForTest(android.Android) - ctx.MockFileSystem(map[string][]byte{ + mockFS := map[string][]byte{ "Android.bp": []byte(bp), "build/make/target/product/security": nil, "apex_manifest.json": nil, @@ -107,23 +109,39 @@ func testSdkContext(bp string) (*android.TestContext, android.Config) { "libfoo.so": nil, "stubs-sources/foo/bar/Foo.java": nil, "foo/bar/Foo.java": nil, - }) + } + + for k, v := range fs { + mockFS[k] = v + } + + ctx.MockFileSystem(mockFS) return ctx, config } -func testSdk(t *testing.T, bp string) (*android.TestContext, android.Config) { - ctx, config := testSdkContext(bp) +func testSdk(t *testing.T, bp string) *testSdkResult { + t.Helper() + return testSdkWithFs(t, bp, nil) +} + +func testSdkWithFs(t *testing.T, bp string, fs map[string][]byte) *testSdkResult { + t.Helper() + ctx, config := testSdkContext(bp, fs) _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) android.FailIfErrored(t, errs) _, errs = ctx.PrepareBuildActions(config) android.FailIfErrored(t, errs) - return ctx, config + return &testSdkResult{ + TestHelper: TestHelper{t: t}, + ctx: ctx, + config: config, + } } func testSdkError(t *testing.T, pattern, bp string) { t.Helper() - ctx, config := testSdkContext(bp) + ctx, config := testSdkContext(bp, nil) _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) if len(errs) > 0 { android.FailIfNoMatchingErrors(t, pattern, errs) @@ -153,14 +171,211 @@ func pathsToStrings(paths android.Paths) []string { return ret } -func checkSnapshotAndroidBpContents(t *testing.T, s *sdk, expectedContents string) { - t.Helper() - androidBpContents := strings.NewReplacer("\\n", "\n").Replace(s.GetAndroidBpContentsForTests()) - if androidBpContents != expectedContents { - t.Errorf("Android.bp contents do not match, expected %s, actual %s", expectedContents, androidBpContents) +// Provides general test support. +type TestHelper struct { + t *testing.T +} + +func (h *TestHelper) AssertStringEquals(message string, expected string, actual string) { + h.t.Helper() + if actual != expected { + h.t.Errorf("%s: expected %s, actual %s", message, expected, actual) } } +func (h *TestHelper) AssertTrimmedStringEquals(message string, expected string, actual string) { + h.t.Helper() + h.AssertStringEquals(message, strings.TrimSpace(expected), strings.TrimSpace(actual)) +} + +// Encapsulates result of processing an SDK definition. Provides support for +// checking the state of the build structures. +type testSdkResult struct { + TestHelper + ctx *android.TestContext + config android.Config +} + +// Analyse the sdk build rules to extract information about what it is doing. + +// e.g. find the src/dest pairs from each cp command, the various zip files +// generated, etc. +func (r *testSdkResult) getSdkSnapshotBuildInfo(sdk *sdk) *snapshotBuildInfo { + androidBpContents := strings.NewReplacer("\\n", "\n").Replace(sdk.GetAndroidBpContentsForTests()) + + info := &snapshotBuildInfo{ + r: r, + androidBpContents: androidBpContents, + } + + buildParams := sdk.BuildParamsForTests() + copyRules := &strings.Builder{} + for _, bp := range buildParams { + switch bp.Rule.String() { + case android.Cp.String(): + // Get source relative to build directory. + src := r.pathRelativeToBuildDir(bp.Input) + // Get destination relative to the snapshot root + dest := bp.Output.Rel() + _, _ = fmt.Fprintf(copyRules, "%s -> %s\n", src, dest) + info.snapshotContents = append(info.snapshotContents, dest) + + case repackageZip.String(): + // Add the destdir to the snapshot contents as that is effectively where + // the content of the repackaged zip is copied. + dest := bp.Args["destdir"] + info.snapshotContents = append(info.snapshotContents, dest) + + case zipFiles.String(): + // This could be an intermediate zip file and not the actual output zip. + // In that case this will be overridden when the rule to merge the zips + // is processed. + info.outputZip = r.pathRelativeToBuildDir(bp.Output) + + case mergeZips.String(): + // Copy the current outputZip to the intermediateZip. + info.intermediateZip = info.outputZip + mergeInput := r.pathRelativeToBuildDir(bp.Input) + if info.intermediateZip != mergeInput { + r.t.Errorf("Expected intermediate zip %s to be an input to merge zips but found %s instead", + info.intermediateZip, mergeInput) + } + + // Override output zip (which was actually the intermediate zip file) with the actual + // output zip. + info.outputZip = r.pathRelativeToBuildDir(bp.Output) + + // Save the zips to be merged into the intermediate zip. + info.mergeZips = r.pathsRelativeToBuildDir(bp.Inputs) + } + } + + info.copyRules = copyRules.String() + + return info +} + +func (r *testSdkResult) Module(name string, variant string) android.Module { + return r.ctx.ModuleForTests(name, variant).Module() +} + +func (r *testSdkResult) ModuleForTests(name string, variant string) android.TestingModule { + return r.ctx.ModuleForTests(name, variant) +} + +func (r *testSdkResult) pathRelativeToBuildDir(path android.Path) string { + buildDir := filepath.Clean(r.config.BuildDir()) + "/" + return strings.TrimPrefix(filepath.Clean(path.String()), buildDir) +} + +func (r *testSdkResult) pathsRelativeToBuildDir(paths android.Paths) []string { + var result []string + for _, path := range paths { + result = append(result, r.pathRelativeToBuildDir(path)) + } + return result +} + +// Check the snapshot build rules. +// +// Takes a list of functions which check different facets of the snapshot build rules. +// Allows each test to customize what is checked without duplicating lots of code +// or proliferating check methods of different flavors. +func (r *testSdkResult) CheckSnapshot(name string, variant string, checkers ...snapshotBuildInfoChecker) { + r.t.Helper() + + sdk := r.Module(name, variant).(*sdk) + + snapshotBuildInfo := r.getSdkSnapshotBuildInfo(sdk) + + // Check state of the snapshot build. + for _, checker := range checkers { + checker(snapshotBuildInfo) + } + + // Make sure that the generated zip file is in the correct place. + actual := snapshotBuildInfo.outputZip + r.AssertStringEquals("Snapshot zip file in wrong place", + fmt.Sprintf(".intermediates/%s/%s/%s-current.zip", name, variant, name), actual) + + // Populate a mock filesystem with the files that would have been copied by + // the rules. + fs := make(map[string][]byte) + for _, dest := range snapshotBuildInfo.snapshotContents { + fs[dest] = nil + } + + // Process the generated bp file to make sure it is valid. + testSdkWithFs(r.t, snapshotBuildInfo.androidBpContents, fs) +} + +type snapshotBuildInfoChecker func(info *snapshotBuildInfo) + +// Check that the snapshot's generated Android.bp is correct. +// +// Both the expected and actual string are both trimmed before comparing. +func checkAndroidBpContents(expected string) snapshotBuildInfoChecker { + return func(info *snapshotBuildInfo) { + info.r.t.Helper() + info.r.AssertTrimmedStringEquals("Android.bp contents do not match", expected, info.androidBpContents) + } +} + +// Check that the snapshot's copy rules are correct. +// +// The copy rules are formatted as -> , one per line and then compared +// to the supplied expected string. Both the expected and actual string are trimmed +// before comparing. +func checkAllCopyRules(expected string) snapshotBuildInfoChecker { + return func(info *snapshotBuildInfo) { + info.r.t.Helper() + info.r.AssertTrimmedStringEquals("Incorrect copy rules", expected, info.copyRules) + } +} + +// Check that the specified path is in the list of zips to merge with the intermediate zip. +func checkMergeZip(expected string) snapshotBuildInfoChecker { + return func(info *snapshotBuildInfo) { + info.r.t.Helper() + if info.intermediateZip == "" { + info.r.t.Errorf("No intermediate zip file was created") + } + ensureListContains(info.r.t, info.mergeZips, expected) + } +} + +// Encapsulates information about the snapshot build structure in order to insulate tests from +// knowing too much about internal structures. +// +// All source/input paths are relative either the build directory. All dest/output paths are +// relative to the snapshot root directory. +type snapshotBuildInfo struct { + r *testSdkResult + + // The contents of the generated Android.bp file + androidBpContents string + + // The paths, relative to the snapshot root, of all files and directories copied into the + // snapshot. + snapshotContents []string + + // A formatted representation of the src/dest pairs, one pair per line, of the format + // src -> dest + copyRules string + + // The path to the intermediate zip, which is a zip created from the source files copied + // into the snapshot directory and which will be merged with other zips to form the final output. + // Is am empty string if there is no intermediate zip because there are no zips to merge in. + intermediateZip string + + // The paths to the zips to merge into the output zip, does not include the intermediate + // zip. + mergeZips []string + + // The final output zip. + outputZip string +} + var buildDir string func setUp() { diff --git a/sdk/update.go b/sdk/update.go index 602e0a498..7731fbb3e 100644 --- a/sdk/update.go +++ b/sdk/update.go @@ -276,7 +276,6 @@ func generateBpContents(contents *generatedContents, bpFile *bpFile) { outputPropertySet(contents, &bpModule.bpPropertySet) contents.Printfln("}") } - contents.Printfln("") } func outputPropertySet(contents *generatedContents, set *bpPropertySet) { From a80fdec00dfefe9f56d5be08860e8035bdd49dce Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Tue, 3 Dec 2019 15:25:00 +0000 Subject: [PATCH 4/5] Extract the cc and java sdk related tests out into their own file Makes it easier for multiple people to work on different areas without conflicts. Bug: 143678475 Test: m conscrypt-module-sdk Change-Id: I93140450c0b049946a9d0432225bccff82f5a873 --- Android.bp | 2 + sdk/cc_sdk_test.go | 328 +++++++++++++++++++++++++++ sdk/java_sdk_test.go | 332 +++++++++++++++++++++++++++ sdk/sdk_test.go | 524 ------------------------------------------- 4 files changed, 662 insertions(+), 524 deletions(-) create mode 100644 sdk/cc_sdk_test.go create mode 100644 sdk/java_sdk_test.go diff --git a/Android.bp b/Android.bp index 2cc69324f..f72d62433 100644 --- a/Android.bp +++ b/Android.bp @@ -508,6 +508,8 @@ bootstrap_go_package { "sdk/update.go", ], testSrcs: [ + "sdk/cc_sdk_test.go", + "sdk/java_sdk_test.go", "sdk/sdk_test.go", "sdk/testing.go", ], diff --git a/sdk/cc_sdk_test.go b/sdk/cc_sdk_test.go new file mode 100644 index 000000000..96d321d58 --- /dev/null +++ b/sdk/cc_sdk_test.go @@ -0,0 +1,328 @@ +// 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 ( + "testing" + + "android/soong/cc" +) + +// Contains tests for SDK members provided by the cc package. + +func TestSdkIsCompileMultilibBoth(t *testing.T) { + result := testSdk(t, ` + sdk { + name: "mysdk", + native_shared_libs: ["sdkmember"], + } + + cc_library_shared { + name: "sdkmember", + srcs: ["Test.cpp"], + system_shared_libs: [], + stl: "none", + } + `) + + armOutput := result.Module("sdkmember", "android_arm_armv7-a-neon_core_shared").(*cc.Module).OutputFile() + arm64Output := result.Module("sdkmember", "android_arm64_armv8-a_core_shared").(*cc.Module).OutputFile() + + var inputs []string + buildParams := result.Module("mysdk", "android_common").BuildParamsForTests() + for _, bp := range buildParams { + if bp.Input != nil { + inputs = append(inputs, bp.Input.String()) + } + } + + // ensure that both 32/64 outputs are inputs of the sdk snapshot + ensureListContains(t, inputs, armOutput.String()) + ensureListContains(t, inputs, arm64Output.String()) +} + +func TestBasicSdkWithCc(t *testing.T) { + result := testSdk(t, ` + sdk { + name: "mysdk", + native_shared_libs: ["sdkmember"], + } + + sdk_snapshot { + name: "mysdk@1", + native_shared_libs: ["sdkmember_mysdk_1"], + } + + sdk_snapshot { + name: "mysdk@2", + native_shared_libs: ["sdkmember_mysdk_2"], + } + + cc_prebuilt_library_shared { + name: "sdkmember", + srcs: ["libfoo.so"], + prefer: false, + system_shared_libs: [], + stl: "none", + } + + cc_prebuilt_library_shared { + name: "sdkmember_mysdk_1", + sdk_member_name: "sdkmember", + srcs: ["libfoo.so"], + system_shared_libs: [], + stl: "none", + } + + cc_prebuilt_library_shared { + name: "sdkmember_mysdk_2", + sdk_member_name: "sdkmember", + srcs: ["libfoo.so"], + system_shared_libs: [], + stl: "none", + } + + cc_library_shared { + name: "mycpplib", + srcs: ["Test.cpp"], + shared_libs: ["sdkmember"], + system_shared_libs: [], + stl: "none", + } + + apex { + name: "myapex", + native_shared_libs: ["mycpplib"], + uses_sdks: ["mysdk@1"], + key: "myapex.key", + certificate: ":myapex.cert", + } + + apex { + name: "myapex2", + native_shared_libs: ["mycpplib"], + uses_sdks: ["mysdk@2"], + key: "myapex.key", + certificate: ":myapex.cert", + } + `) + + sdkMemberV1 := result.ModuleForTests("sdkmember_mysdk_1", "android_arm64_armv8-a_core_shared_myapex").Rule("toc").Output + sdkMemberV2 := result.ModuleForTests("sdkmember_mysdk_2", "android_arm64_armv8-a_core_shared_myapex2").Rule("toc").Output + + cpplibForMyApex := result.ModuleForTests("mycpplib", "android_arm64_armv8-a_core_shared_myapex") + cpplibForMyApex2 := result.ModuleForTests("mycpplib", "android_arm64_armv8-a_core_shared_myapex2") + + // Depending on the uses_sdks value, different libs are linked + ensureListContains(t, pathsToStrings(cpplibForMyApex.Rule("ld").Implicits), sdkMemberV1.String()) + ensureListContains(t, pathsToStrings(cpplibForMyApex2.Rule("ld").Implicits), sdkMemberV2.String()) +} + +func TestSnapshotWithCcShared(t *testing.T) { + result := testSdk(t, ` + sdk { + name: "mysdk", + native_shared_libs: ["mynativelib"], + } + + cc_library_shared { + name: "mynativelib", + srcs: [ + "Test.cpp", + "aidl/foo/bar/Test.aidl", + ], + export_include_dirs: ["include"], + aidl: { + export_aidl_headers: true, + }, + system_shared_libs: [], + stl: "none", + } + `) + + result.CheckSnapshot("mysdk", "android_common", + checkAndroidBpContents(` +// This is auto-generated. DO NOT EDIT. + +cc_prebuilt_library_shared { + name: "mysdk_mynativelib@current", + sdk_member_name: "mynativelib", + arch: { + arm64: { + srcs: ["arm64/lib/mynativelib.so"], + export_include_dirs: [ + "arm64/include/include", + "arm64/include_gen/mynativelib", + ], + }, + arm: { + srcs: ["arm/lib/mynativelib.so"], + export_include_dirs: [ + "arm/include/include", + "arm/include_gen/mynativelib", + ], + }, + }, + stl: "none", + system_shared_libs: [], +} + +cc_prebuilt_library_shared { + name: "mynativelib", + prefer: false, + arch: { + arm64: { + srcs: ["arm64/lib/mynativelib.so"], + export_include_dirs: [ + "arm64/include/include", + "arm64/include_gen/mynativelib", + ], + }, + arm: { + srcs: ["arm/lib/mynativelib.so"], + export_include_dirs: [ + "arm/include/include", + "arm/include_gen/mynativelib", + ], + }, + }, + stl: "none", + system_shared_libs: [], +} + +sdk_snapshot { + name: "mysdk@current", + native_shared_libs: ["mysdk_mynativelib@current"], +} +`), + checkAllCopyRules(` +.intermediates/mynativelib/android_arm64_armv8-a_core_shared/mynativelib.so -> arm64/lib/mynativelib.so +include/Test.h -> arm64/include/include/Test.h +.intermediates/mynativelib/android_arm64_armv8-a_core_shared/gen/aidl/aidl/foo/bar/Test.h -> arm64/include_gen/mynativelib/aidl/foo/bar/Test.h +.intermediates/mynativelib/android_arm64_armv8-a_core_shared/gen/aidl/aidl/foo/bar/BnTest.h -> arm64/include_gen/mynativelib/aidl/foo/bar/BnTest.h +.intermediates/mynativelib/android_arm64_armv8-a_core_shared/gen/aidl/aidl/foo/bar/BpTest.h -> arm64/include_gen/mynativelib/aidl/foo/bar/BpTest.h +.intermediates/mynativelib/android_arm_armv7-a-neon_core_shared/mynativelib.so -> arm/lib/mynativelib.so +include/Test.h -> arm/include/include/Test.h +.intermediates/mynativelib/android_arm_armv7-a-neon_core_shared/gen/aidl/aidl/foo/bar/Test.h -> arm/include_gen/mynativelib/aidl/foo/bar/Test.h +.intermediates/mynativelib/android_arm_armv7-a-neon_core_shared/gen/aidl/aidl/foo/bar/BnTest.h -> arm/include_gen/mynativelib/aidl/foo/bar/BnTest.h +.intermediates/mynativelib/android_arm_armv7-a-neon_core_shared/gen/aidl/aidl/foo/bar/BpTest.h -> arm/include_gen/mynativelib/aidl/foo/bar/BpTest.h +`), + ) +} + +func TestHostSnapshotWithCcShared(t *testing.T) { + // b/145598135 - Generating host snapshots for anything other than linux is not supported. + SkipIfNotLinux(t) + + result := testSdk(t, ` + sdk { + name: "mysdk", + device_supported: false, + host_supported: true, + native_shared_libs: ["mynativelib"], + } + + cc_library_shared { + name: "mynativelib", + device_supported: false, + host_supported: true, + srcs: [ + "Test.cpp", + "aidl/foo/bar/Test.aidl", + ], + export_include_dirs: ["include"], + aidl: { + export_aidl_headers: true, + }, + system_shared_libs: [], + stl: "none", + } + `) + + result.CheckSnapshot("mysdk", "linux_glibc_common", + checkAndroidBpContents(` +// This is auto-generated. DO NOT EDIT. + +cc_prebuilt_library_shared { + name: "mysdk_mynativelib@current", + sdk_member_name: "mynativelib", + device_supported: false, + host_supported: true, + arch: { + x86_64: { + srcs: ["x86_64/lib/mynativelib.so"], + export_include_dirs: [ + "x86_64/include/include", + "x86_64/include_gen/mynativelib", + ], + }, + x86: { + srcs: ["x86/lib/mynativelib.so"], + export_include_dirs: [ + "x86/include/include", + "x86/include_gen/mynativelib", + ], + }, + }, + stl: "none", + system_shared_libs: [], +} + +cc_prebuilt_library_shared { + name: "mynativelib", + prefer: false, + device_supported: false, + host_supported: true, + arch: { + x86_64: { + srcs: ["x86_64/lib/mynativelib.so"], + export_include_dirs: [ + "x86_64/include/include", + "x86_64/include_gen/mynativelib", + ], + }, + x86: { + srcs: ["x86/lib/mynativelib.so"], + export_include_dirs: [ + "x86/include/include", + "x86/include_gen/mynativelib", + ], + }, + }, + stl: "none", + system_shared_libs: [], +} + +sdk_snapshot { + name: "mysdk@current", + device_supported: false, + host_supported: true, + native_shared_libs: ["mysdk_mynativelib@current"], +} +`), + checkAllCopyRules(` +.intermediates/mynativelib/linux_glibc_x86_64_shared/mynativelib.so -> x86_64/lib/mynativelib.so +include/Test.h -> x86_64/include/include/Test.h +.intermediates/mynativelib/linux_glibc_x86_64_shared/gen/aidl/aidl/foo/bar/Test.h -> x86_64/include_gen/mynativelib/aidl/foo/bar/Test.h +.intermediates/mynativelib/linux_glibc_x86_64_shared/gen/aidl/aidl/foo/bar/BnTest.h -> x86_64/include_gen/mynativelib/aidl/foo/bar/BnTest.h +.intermediates/mynativelib/linux_glibc_x86_64_shared/gen/aidl/aidl/foo/bar/BpTest.h -> x86_64/include_gen/mynativelib/aidl/foo/bar/BpTest.h +.intermediates/mynativelib/linux_glibc_x86_shared/mynativelib.so -> x86/lib/mynativelib.so +include/Test.h -> x86/include/include/Test.h +.intermediates/mynativelib/linux_glibc_x86_shared/gen/aidl/aidl/foo/bar/Test.h -> x86/include_gen/mynativelib/aidl/foo/bar/Test.h +.intermediates/mynativelib/linux_glibc_x86_shared/gen/aidl/aidl/foo/bar/BnTest.h -> x86/include_gen/mynativelib/aidl/foo/bar/BnTest.h +.intermediates/mynativelib/linux_glibc_x86_shared/gen/aidl/aidl/foo/bar/BpTest.h -> x86/include_gen/mynativelib/aidl/foo/bar/BpTest.h +`), + ) +} diff --git a/sdk/java_sdk_test.go b/sdk/java_sdk_test.go new file mode 100644 index 000000000..e5fa47cfe --- /dev/null +++ b/sdk/java_sdk_test.go @@ -0,0 +1,332 @@ +// 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 ( + "testing" +) + +// Contains tests for SDK members provided by the java package. + +func TestBasicSdkWithJavaLibrary(t *testing.T) { + result := testSdk(t, ` + sdk { + name: "mysdk", + java_libs: ["myjavalib"], + } + + sdk_snapshot { + name: "mysdk@1", + java_libs: ["sdkmember_mysdk_1"], + } + + sdk_snapshot { + name: "mysdk@2", + java_libs: ["sdkmember_mysdk_2"], + } + + java_import { + name: "sdkmember", + prefer: false, + host_supported: true, + } + + java_import { + name: "sdkmember_mysdk_1", + sdk_member_name: "sdkmember", + host_supported: true, + } + + java_import { + name: "sdkmember_mysdk_2", + sdk_member_name: "sdkmember", + host_supported: true, + } + + java_library { + name: "myjavalib", + srcs: ["Test.java"], + libs: ["sdkmember"], + system_modules: "none", + sdk_version: "none", + compile_dex: true, + host_supported: true, + } + + apex { + name: "myapex", + java_libs: ["myjavalib"], + uses_sdks: ["mysdk@1"], + key: "myapex.key", + certificate: ":myapex.cert", + } + + apex { + name: "myapex2", + java_libs: ["myjavalib"], + uses_sdks: ["mysdk@2"], + key: "myapex.key", + certificate: ":myapex.cert", + } + `) + + sdkMemberV1 := result.ctx.ModuleForTests("sdkmember_mysdk_1", "android_common_myapex").Rule("combineJar").Output + sdkMemberV2 := result.ctx.ModuleForTests("sdkmember_mysdk_2", "android_common_myapex2").Rule("combineJar").Output + + javalibForMyApex := result.ctx.ModuleForTests("myjavalib", "android_common_myapex") + javalibForMyApex2 := result.ctx.ModuleForTests("myjavalib", "android_common_myapex2") + + // Depending on the uses_sdks value, different libs are linked + ensureListContains(t, pathsToStrings(javalibForMyApex.Rule("javac").Implicits), sdkMemberV1.String()) + ensureListContains(t, pathsToStrings(javalibForMyApex2.Rule("javac").Implicits), sdkMemberV2.String()) +} + +func TestSnapshotWithJavaLibrary(t *testing.T) { + result := testSdk(t, ` + sdk { + name: "mysdk", + java_libs: ["myjavalib"], + } + + java_library { + name: "myjavalib", + srcs: ["Test.java"], + aidl: { + export_include_dirs: ["aidl"], + }, + system_modules: "none", + sdk_version: "none", + compile_dex: true, + host_supported: true, + } + `) + + result.CheckSnapshot("mysdk", "android_common", + checkAndroidBpContents(` +// This is auto-generated. DO NOT EDIT. + +java_import { + name: "mysdk_myjavalib@current", + sdk_member_name: "myjavalib", + jars: ["java/myjavalib.jar"], +} + +java_import { + name: "myjavalib", + prefer: false, + jars: ["java/myjavalib.jar"], +} + +sdk_snapshot { + name: "mysdk@current", + java_libs: ["mysdk_myjavalib@current"], +} + +`), + checkAllCopyRules(` +.intermediates/myjavalib/android_common/turbine-combined/myjavalib.jar -> java/myjavalib.jar +aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl +`), + ) +} + +func TestHostSnapshotWithJavaLibrary(t *testing.T) { + // b/145598135 - Generating host snapshots for anything other than linux is not supported. + SkipIfNotLinux(t) + + result := testSdk(t, ` + sdk { + name: "mysdk", + device_supported: false, + host_supported: true, + java_libs: ["myjavalib"], + } + + java_library { + name: "myjavalib", + device_supported: false, + host_supported: true, + srcs: ["Test.java"], + aidl: { + export_include_dirs: ["aidl"], + }, + system_modules: "none", + sdk_version: "none", + compile_dex: true, + } + `) + + result.CheckSnapshot("mysdk", "linux_glibc_common", + checkAndroidBpContents(` +// This is auto-generated. DO NOT EDIT. + +java_import { + name: "mysdk_myjavalib@current", + sdk_member_name: "myjavalib", + device_supported: false, + host_supported: true, + jars: ["java/myjavalib.jar"], +} + +java_import { + name: "myjavalib", + prefer: false, + device_supported: false, + host_supported: true, + jars: ["java/myjavalib.jar"], +} + +sdk_snapshot { + name: "mysdk@current", + device_supported: false, + host_supported: true, + java_libs: ["mysdk_myjavalib@current"], +} +`), + checkAllCopyRules(` +.intermediates/myjavalib/linux_glibc_common/javac/myjavalib.jar -> java/myjavalib.jar +aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl +`), + ) +} + +// Note: This test does not verify that a droidstubs can be referenced, either +// directly or indirectly from an APEX as droidstubs can never be a part of an +// apex. +func TestBasicSdkWithDroidstubs(t *testing.T) { + testSdk(t, ` + sdk { + name: "mysdk", + stubs_sources: ["mystub"], + } + sdk_snapshot { + name: "mysdk@10", + stubs_sources: ["mystub_mysdk@10"], + } + prebuilt_stubs_sources { + name: "mystub_mysdk@10", + sdk_member_name: "mystub", + srcs: ["stubs-sources/foo/bar/Foo.java"], + } + droidstubs { + name: "mystub", + srcs: ["foo/bar/Foo.java"], + sdk_version: "none", + system_modules: "none", + } + java_library { + name: "myjavalib", + srcs: [":mystub"], + sdk_version: "none", + system_modules: "none", + } + `) +} + +func TestSnapshotWithDroidstubs(t *testing.T) { + result := testSdk(t, ` + sdk { + name: "mysdk", + stubs_sources: ["myjavaapistubs"], + } + + droidstubs { + name: "myjavaapistubs", + srcs: ["foo/bar/Foo.java"], + system_modules: "none", + sdk_version: "none", + } + `) + + result.CheckSnapshot("mysdk", "android_common", + checkAndroidBpContents(` +// This is auto-generated. DO NOT EDIT. + +prebuilt_stubs_sources { + name: "mysdk_myjavaapistubs@current", + sdk_member_name: "myjavaapistubs", + srcs: ["java/myjavaapistubs_stubs_sources"], +} + +prebuilt_stubs_sources { + name: "myjavaapistubs", + prefer: false, + srcs: ["java/myjavaapistubs_stubs_sources"], +} + +sdk_snapshot { + name: "mysdk@current", + stubs_sources: ["mysdk_myjavaapistubs@current"], +} + +`), + checkAllCopyRules(""), + checkMergeZip(".intermediates/mysdk/android_common/tmp/java/myjavaapistubs_stubs_sources.zip"), + ) +} + +func TestHostSnapshotWithDroidstubs(t *testing.T) { + // b/145598135 - Generating host snapshots for anything other than linux is not supported. + SkipIfNotLinux(t) + + result := testSdk(t, ` + sdk { + name: "mysdk", + device_supported: false, + host_supported: true, + stubs_sources: ["myjavaapistubs"], + } + + droidstubs { + name: "myjavaapistubs", + device_supported: false, + host_supported: true, + srcs: ["foo/bar/Foo.java"], + system_modules: "none", + sdk_version: "none", + } + `) + + result.CheckSnapshot("mysdk", "linux_glibc_common", + checkAndroidBpContents(` +// This is auto-generated. DO NOT EDIT. + +prebuilt_stubs_sources { + name: "mysdk_myjavaapistubs@current", + sdk_member_name: "myjavaapistubs", + device_supported: false, + host_supported: true, + srcs: ["java/myjavaapistubs_stubs_sources"], +} + +prebuilt_stubs_sources { + name: "myjavaapistubs", + prefer: false, + device_supported: false, + host_supported: true, + srcs: ["java/myjavaapistubs_stubs_sources"], +} + +sdk_snapshot { + name: "mysdk@current", + device_supported: false, + host_supported: true, + stubs_sources: ["mysdk_myjavaapistubs@current"], +} +`), + checkAllCopyRules(""), + checkMergeZip(".intermediates/mysdk/linux_glibc_common/tmp/java/myjavaapistubs_stubs_sources.zip"), + ) +} diff --git a/sdk/sdk_test.go b/sdk/sdk_test.go index 0b8dfc9e5..0a9dfb10c 100644 --- a/sdk/sdk_test.go +++ b/sdk/sdk_test.go @@ -16,8 +16,6 @@ package sdk import ( "testing" - - "android/soong/cc" ) // Needed in an _test.go file in this package to ensure tests run correctly, particularly in IDE. @@ -25,189 +23,6 @@ func TestMain(m *testing.M) { runTestWithBuildDir(m) } -func TestBasicSdkWithJava(t *testing.T) { - result := testSdk(t, ` - sdk { - name: "mysdk", - java_libs: ["myjavalib"], - } - - sdk_snapshot { - name: "mysdk@1", - java_libs: ["sdkmember_mysdk_1"], - } - - sdk_snapshot { - name: "mysdk@2", - java_libs: ["sdkmember_mysdk_2"], - } - - java_import { - name: "sdkmember", - prefer: false, - host_supported: true, - } - - java_import { - name: "sdkmember_mysdk_1", - sdk_member_name: "sdkmember", - host_supported: true, - } - - java_import { - name: "sdkmember_mysdk_2", - sdk_member_name: "sdkmember", - host_supported: true, - } - - java_library { - name: "myjavalib", - srcs: ["Test.java"], - libs: ["sdkmember"], - system_modules: "none", - sdk_version: "none", - compile_dex: true, - host_supported: true, - } - - apex { - name: "myapex", - java_libs: ["myjavalib"], - uses_sdks: ["mysdk@1"], - key: "myapex.key", - certificate: ":myapex.cert", - } - - apex { - name: "myapex2", - java_libs: ["myjavalib"], - uses_sdks: ["mysdk@2"], - key: "myapex.key", - certificate: ":myapex.cert", - } - `) - - sdkMemberV1 := result.ModuleForTests("sdkmember_mysdk_1", "android_common_myapex").Rule("combineJar").Output - sdkMemberV2 := result.ModuleForTests("sdkmember_mysdk_2", "android_common_myapex2").Rule("combineJar").Output - - javalibForMyApex := result.ModuleForTests("myjavalib", "android_common_myapex") - javalibForMyApex2 := result.ModuleForTests("myjavalib", "android_common_myapex2") - - // Depending on the uses_sdks value, different libs are linked - ensureListContains(t, pathsToStrings(javalibForMyApex.Rule("javac").Implicits), sdkMemberV1.String()) - ensureListContains(t, pathsToStrings(javalibForMyApex2.Rule("javac").Implicits), sdkMemberV2.String()) -} - -func TestBasicSdkWithCc(t *testing.T) { - result := testSdk(t, ` - sdk { - name: "mysdk", - native_shared_libs: ["sdkmember"], - } - - sdk_snapshot { - name: "mysdk@1", - native_shared_libs: ["sdkmember_mysdk_1"], - } - - sdk_snapshot { - name: "mysdk@2", - native_shared_libs: ["sdkmember_mysdk_2"], - } - - cc_prebuilt_library_shared { - name: "sdkmember", - srcs: ["libfoo.so"], - prefer: false, - system_shared_libs: [], - stl: "none", - } - - cc_prebuilt_library_shared { - name: "sdkmember_mysdk_1", - sdk_member_name: "sdkmember", - srcs: ["libfoo.so"], - system_shared_libs: [], - stl: "none", - } - - cc_prebuilt_library_shared { - name: "sdkmember_mysdk_2", - sdk_member_name: "sdkmember", - srcs: ["libfoo.so"], - system_shared_libs: [], - stl: "none", - } - - cc_library_shared { - name: "mycpplib", - srcs: ["Test.cpp"], - shared_libs: ["sdkmember"], - system_shared_libs: [], - stl: "none", - } - - apex { - name: "myapex", - native_shared_libs: ["mycpplib"], - uses_sdks: ["mysdk@1"], - key: "myapex.key", - certificate: ":myapex.cert", - } - - apex { - name: "myapex2", - native_shared_libs: ["mycpplib"], - uses_sdks: ["mysdk@2"], - key: "myapex.key", - certificate: ":myapex.cert", - } - `) - - sdkMemberV1 := result.ModuleForTests("sdkmember_mysdk_1", "android_arm64_armv8-a_core_shared_myapex").Rule("toc").Output - sdkMemberV2 := result.ModuleForTests("sdkmember_mysdk_2", "android_arm64_armv8-a_core_shared_myapex2").Rule("toc").Output - - cpplibForMyApex := result.ModuleForTests("mycpplib", "android_arm64_armv8-a_core_shared_myapex") - cpplibForMyApex2 := result.ModuleForTests("mycpplib", "android_arm64_armv8-a_core_shared_myapex2") - - // Depending on the uses_sdks value, different libs are linked - ensureListContains(t, pathsToStrings(cpplibForMyApex.Rule("ld").Implicits), sdkMemberV1.String()) - ensureListContains(t, pathsToStrings(cpplibForMyApex2.Rule("ld").Implicits), sdkMemberV2.String()) -} - -// Note: This test does not verify that a droidstubs can be referenced, either -// directly or indirectly from an APEX as droidstubs can never be a part of an -// apex. -func TestBasicSdkWithDroidstubs(t *testing.T) { - testSdk(t, ` - sdk { - name: "mysdk", - stubs_sources: ["mystub"], - } - sdk_snapshot { - name: "mysdk@10", - stubs_sources: ["mystub_mysdk@10"], - } - prebuilt_stubs_sources { - name: "mystub_mysdk@10", - sdk_member_name: "mystub", - srcs: ["stubs-sources/foo/bar/Foo.java"], - } - droidstubs { - name: "mystub", - srcs: ["foo/bar/Foo.java"], - sdk_version: "none", - system_modules: "none", - } - java_library { - name: "myjavalib", - srcs: [":mystub"], - sdk_version: "none", - system_modules: "none", - } - `) -} - func TestDepNotInRequiredSdks(t *testing.T) { testSdkError(t, `module "myjavalib".*depends on "otherlib".*that isn't part of the required SDKs:.*`, ` sdk { @@ -264,342 +79,3 @@ func TestDepNotInRequiredSdks(t *testing.T) { } `) } - -func TestSdkIsCompileMultilibBoth(t *testing.T) { - result := testSdk(t, ` - sdk { - name: "mysdk", - native_shared_libs: ["sdkmember"], - } - - cc_library_shared { - name: "sdkmember", - srcs: ["Test.cpp"], - system_shared_libs: [], - stl: "none", - } - `) - - armOutput := result.Module("sdkmember", "android_arm_armv7-a-neon_core_shared").(*cc.Module).OutputFile() - arm64Output := result.Module("sdkmember", "android_arm64_armv8-a_core_shared").(*cc.Module).OutputFile() - - var inputs []string - buildParams := result.Module("mysdk", "android_common").BuildParamsForTests() - for _, bp := range buildParams { - if bp.Input != nil { - inputs = append(inputs, bp.Input.String()) - } - } - - // ensure that both 32/64 outputs are inputs of the sdk snapshot - ensureListContains(t, inputs, armOutput.String()) - ensureListContains(t, inputs, arm64Output.String()) -} - -func TestSnapshot(t *testing.T) { - result := testSdk(t, ` - sdk { - name: "mysdk", - java_libs: ["myjavalib"], - native_shared_libs: ["mynativelib"], - stubs_sources: ["myjavaapistubs"], - } - - java_library { - name: "myjavalib", - srcs: ["Test.java"], - aidl: { - export_include_dirs: ["aidl"], - }, - system_modules: "none", - sdk_version: "none", - compile_dex: true, - host_supported: true, - } - - cc_library_shared { - name: "mynativelib", - srcs: [ - "Test.cpp", - "aidl/foo/bar/Test.aidl", - ], - export_include_dirs: ["include"], - aidl: { - export_aidl_headers: true, - }, - system_shared_libs: [], - stl: "none", - } - - droidstubs { - name: "myjavaapistubs", - srcs: ["foo/bar/Foo.java"], - system_modules: "none", - sdk_version: "none", - } - `) - - result.CheckSnapshot("mysdk", "android_common", - checkAndroidBpContents(` -// This is auto-generated. DO NOT EDIT. - -java_import { - name: "mysdk_myjavalib@current", - sdk_member_name: "myjavalib", - jars: ["java/myjavalib.jar"], -} - -java_import { - name: "myjavalib", - prefer: false, - jars: ["java/myjavalib.jar"], -} - -prebuilt_stubs_sources { - name: "mysdk_myjavaapistubs@current", - sdk_member_name: "myjavaapistubs", - srcs: ["java/myjavaapistubs_stubs_sources"], -} - -prebuilt_stubs_sources { - name: "myjavaapistubs", - prefer: false, - srcs: ["java/myjavaapistubs_stubs_sources"], -} - -cc_prebuilt_library_shared { - name: "mysdk_mynativelib@current", - sdk_member_name: "mynativelib", - arch: { - arm64: { - srcs: ["arm64/lib/mynativelib.so"], - export_include_dirs: [ - "arm64/include/include", - "arm64/include_gen/mynativelib", - ], - }, - arm: { - srcs: ["arm/lib/mynativelib.so"], - export_include_dirs: [ - "arm/include/include", - "arm/include_gen/mynativelib", - ], - }, - }, - stl: "none", - system_shared_libs: [], -} - -cc_prebuilt_library_shared { - name: "mynativelib", - prefer: false, - arch: { - arm64: { - srcs: ["arm64/lib/mynativelib.so"], - export_include_dirs: [ - "arm64/include/include", - "arm64/include_gen/mynativelib", - ], - }, - arm: { - srcs: ["arm/lib/mynativelib.so"], - export_include_dirs: [ - "arm/include/include", - "arm/include_gen/mynativelib", - ], - }, - }, - stl: "none", - system_shared_libs: [], -} - -sdk_snapshot { - name: "mysdk@current", - java_libs: ["mysdk_myjavalib@current"], - stubs_sources: ["mysdk_myjavaapistubs@current"], - native_shared_libs: ["mysdk_mynativelib@current"], -} -`), - checkAllCopyRules(` -.intermediates/myjavalib/android_common/turbine-combined/myjavalib.jar -> java/myjavalib.jar -aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl -.intermediates/mynativelib/android_arm64_armv8-a_core_shared/mynativelib.so -> arm64/lib/mynativelib.so -include/Test.h -> arm64/include/include/Test.h -.intermediates/mynativelib/android_arm64_armv8-a_core_shared/gen/aidl/aidl/foo/bar/Test.h -> arm64/include_gen/mynativelib/aidl/foo/bar/Test.h -.intermediates/mynativelib/android_arm64_armv8-a_core_shared/gen/aidl/aidl/foo/bar/BnTest.h -> arm64/include_gen/mynativelib/aidl/foo/bar/BnTest.h -.intermediates/mynativelib/android_arm64_armv8-a_core_shared/gen/aidl/aidl/foo/bar/BpTest.h -> arm64/include_gen/mynativelib/aidl/foo/bar/BpTest.h -.intermediates/mynativelib/android_arm_armv7-a-neon_core_shared/mynativelib.so -> arm/lib/mynativelib.so -include/Test.h -> arm/include/include/Test.h -.intermediates/mynativelib/android_arm_armv7-a-neon_core_shared/gen/aidl/aidl/foo/bar/Test.h -> arm/include_gen/mynativelib/aidl/foo/bar/Test.h -.intermediates/mynativelib/android_arm_armv7-a-neon_core_shared/gen/aidl/aidl/foo/bar/BnTest.h -> arm/include_gen/mynativelib/aidl/foo/bar/BnTest.h -.intermediates/mynativelib/android_arm_armv7-a-neon_core_shared/gen/aidl/aidl/foo/bar/BpTest.h -> arm/include_gen/mynativelib/aidl/foo/bar/BpTest.h -`), - checkMergeZip(".intermediates/mysdk/android_common/tmp/java/myjavaapistubs_stubs_sources.zip"), - ) -} - -func TestHostSnapshot(t *testing.T) { - // b/145598135 - Generating host snapshots for anything other than linux is not supported. - SkipIfNotLinux(t) - - result := testSdk(t, ` - sdk { - name: "mysdk", - device_supported: false, - host_supported: true, - java_libs: ["myjavalib"], - native_shared_libs: ["mynativelib"], - stubs_sources: ["myjavaapistubs"], - } - - java_library { - name: "myjavalib", - device_supported: false, - host_supported: true, - srcs: ["Test.java"], - aidl: { - export_include_dirs: ["aidl"], - }, - system_modules: "none", - sdk_version: "none", - compile_dex: true, - } - - cc_library_shared { - name: "mynativelib", - device_supported: false, - host_supported: true, - srcs: [ - "Test.cpp", - "aidl/foo/bar/Test.aidl", - ], - export_include_dirs: ["include"], - aidl: { - export_aidl_headers: true, - }, - system_shared_libs: [], - stl: "none", - } - - droidstubs { - name: "myjavaapistubs", - device_supported: false, - host_supported: true, - srcs: ["foo/bar/Foo.java"], - system_modules: "none", - sdk_version: "none", - } - `) - - result.CheckSnapshot("mysdk", "linux_glibc_common", - checkAndroidBpContents(` -// This is auto-generated. DO NOT EDIT. - -java_import { - name: "mysdk_myjavalib@current", - sdk_member_name: "myjavalib", - device_supported: false, - host_supported: true, - jars: ["java/myjavalib.jar"], -} - -java_import { - name: "myjavalib", - prefer: false, - device_supported: false, - host_supported: true, - jars: ["java/myjavalib.jar"], -} - -prebuilt_stubs_sources { - name: "mysdk_myjavaapistubs@current", - sdk_member_name: "myjavaapistubs", - device_supported: false, - host_supported: true, - srcs: ["java/myjavaapistubs_stubs_sources"], -} - -prebuilt_stubs_sources { - name: "myjavaapistubs", - prefer: false, - device_supported: false, - host_supported: true, - srcs: ["java/myjavaapistubs_stubs_sources"], -} - -cc_prebuilt_library_shared { - name: "mysdk_mynativelib@current", - sdk_member_name: "mynativelib", - device_supported: false, - host_supported: true, - arch: { - x86_64: { - srcs: ["x86_64/lib/mynativelib.so"], - export_include_dirs: [ - "x86_64/include/include", - "x86_64/include_gen/mynativelib", - ], - }, - x86: { - srcs: ["x86/lib/mynativelib.so"], - export_include_dirs: [ - "x86/include/include", - "x86/include_gen/mynativelib", - ], - }, - }, - stl: "none", - system_shared_libs: [], -} - -cc_prebuilt_library_shared { - name: "mynativelib", - prefer: false, - device_supported: false, - host_supported: true, - arch: { - x86_64: { - srcs: ["x86_64/lib/mynativelib.so"], - export_include_dirs: [ - "x86_64/include/include", - "x86_64/include_gen/mynativelib", - ], - }, - x86: { - srcs: ["x86/lib/mynativelib.so"], - export_include_dirs: [ - "x86/include/include", - "x86/include_gen/mynativelib", - ], - }, - }, - stl: "none", - system_shared_libs: [], -} - -sdk_snapshot { - name: "mysdk@current", - device_supported: false, - host_supported: true, - java_libs: ["mysdk_myjavalib@current"], - stubs_sources: ["mysdk_myjavaapistubs@current"], - native_shared_libs: ["mysdk_mynativelib@current"], -} -`), - checkAllCopyRules(` -.intermediates/myjavalib/linux_glibc_common/javac/myjavalib.jar -> java/myjavalib.jar -aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl -.intermediates/mynativelib/linux_glibc_x86_64_shared/mynativelib.so -> x86_64/lib/mynativelib.so -include/Test.h -> x86_64/include/include/Test.h -.intermediates/mynativelib/linux_glibc_x86_64_shared/gen/aidl/aidl/foo/bar/Test.h -> x86_64/include_gen/mynativelib/aidl/foo/bar/Test.h -.intermediates/mynativelib/linux_glibc_x86_64_shared/gen/aidl/aidl/foo/bar/BnTest.h -> x86_64/include_gen/mynativelib/aidl/foo/bar/BnTest.h -.intermediates/mynativelib/linux_glibc_x86_64_shared/gen/aidl/aidl/foo/bar/BpTest.h -> x86_64/include_gen/mynativelib/aidl/foo/bar/BpTest.h -.intermediates/mynativelib/linux_glibc_x86_shared/mynativelib.so -> x86/lib/mynativelib.so -include/Test.h -> x86/include/include/Test.h -.intermediates/mynativelib/linux_glibc_x86_shared/gen/aidl/aidl/foo/bar/Test.h -> x86/include_gen/mynativelib/aidl/foo/bar/Test.h -.intermediates/mynativelib/linux_glibc_x86_shared/gen/aidl/aidl/foo/bar/BnTest.h -> x86/include_gen/mynativelib/aidl/foo/bar/BnTest.h -.intermediates/mynativelib/linux_glibc_x86_shared/gen/aidl/aidl/foo/bar/BpTest.h -> x86/include_gen/mynativelib/aidl/foo/bar/BpTest.h -`), - checkMergeZip(".intermediates/mysdk/linux_glibc_common/tmp/java/myjavaapistubs_stubs_sources.zip"), - ) -} From a6e737b078a9e372d3293caa401d6f6c4bd7943b Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Fri, 29 Nov 2019 11:55:51 +0000 Subject: [PATCH 5/5] Organize sdk member properties Grouping in alphabetical order by package and then by name within the package should minimize conflicts when making changes. Bug: 143678475 Test: m conscrypt-module-sdk Change-Id: Ia7dbcd41ce8b8dd8675a90b1b6868fcaeaf72ee4 --- sdk/sdk.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/sdk/sdk.go b/sdk/sdk.go index 80dd088c8..75c85852c 100644 --- a/sdk/sdk.go +++ b/sdk/sdk.go @@ -92,7 +92,19 @@ type sdkMemberListProperty struct { dependencyTag *sdkMemberDependencyTag } +// Information about how to handle each member list property. +// +// It is organized first by package and then by name within the package. +// Packages are in alphabetical order and properties are in alphabetical order +// within each package. var sdkMemberListProperties = []*sdkMemberListProperty{ + // Members from cc package. + { + name: "native_shared_libs", + getter: func(properties *sdkProperties) []string { return properties.Native_shared_libs }, + memberType: cc.LibrarySdkMemberType, + }, + // Members from java package. { name: "java_libs", getter: func(properties *sdkProperties) []string { return properties.Java_libs }, @@ -103,11 +115,6 @@ var sdkMemberListProperties = []*sdkMemberListProperty{ getter: func(properties *sdkProperties) []string { return properties.Stubs_sources }, memberType: java.DroidStubsSdkMemberType, }, - { - name: "native_shared_libs", - getter: func(properties *sdkProperties) []string { return properties.Native_shared_libs }, - memberType: cc.LibrarySdkMemberType, - }, } // sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)