Restrict SdkMemberTypes that can be used with sdk/sdk_snapshot

By default SdkMemberTypes are only supported on module_exports module
type. Support for sdk module type has to be explicitly specified.

The java_header_libs, native_shared_libs and stubs_sources are
supported on sdk. The latter is required to provide the stubs source
for an API specified in java_header_libs as they should be kept in
sync.

Bug: 146341462
Test: m nothing
Change-Id: I19b9e60792780a797458d4a9e489506602b13144
This commit is contained in:
Paul Duffin 2019-12-16 17:43:48 +00:00
parent 28aa544884
commit e602918294
8 changed files with 101 additions and 70 deletions

View file

@ -237,6 +237,9 @@ type SdkMemberType interface {
// The name of the member type property on an sdk module. // The name of the member type property on an sdk module.
SdkPropertyName() string SdkPropertyName() string
// True if the member type supports the sdk/sdk_snapshot, false otherwise.
UsableWithSdkAndSdkSnapshot() bool
// Add dependencies from the SDK module to all the variants the member // Add dependencies from the SDK module to all the variants the member
// contributes to the SDK. The exact set of variants required is determined // 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 // by the SDK and its properties. The dependencies must be added with the
@ -262,14 +265,20 @@ type SdkMemberType interface {
BuildSnapshot(sdkModuleContext ModuleContext, builder SnapshotBuilder, member SdkMember) BuildSnapshot(sdkModuleContext ModuleContext, builder SnapshotBuilder, member SdkMember)
} }
// Base type for SdkMemberType implementations.
type SdkMemberTypeBase struct { type SdkMemberTypeBase struct {
PropertyName string PropertyName string
SupportsSdk bool
} }
func (b *SdkMemberTypeBase) SdkPropertyName() string { func (b *SdkMemberTypeBase) SdkPropertyName() string {
return b.PropertyName return b.PropertyName
} }
func (b *SdkMemberTypeBase) UsableWithSdkAndSdkSnapshot() bool {
return b.SupportsSdk
}
// Encapsulates the information about registered SdkMemberTypes. // Encapsulates the information about registered SdkMemberTypes.
type SdkMemberTypesRegistry struct { type SdkMemberTypesRegistry struct {
// The list of types sorted by property name. // The list of types sorted by property name.
@ -279,22 +288,8 @@ type SdkMemberTypesRegistry struct {
key OnceKey key OnceKey
} }
func (r *SdkMemberTypesRegistry) RegisteredTypes() []SdkMemberType { func (r *SdkMemberTypesRegistry) copyAndAppend(memberType SdkMemberType) *SdkMemberTypesRegistry {
return r.list oldList := r.list
}
func (r *SdkMemberTypesRegistry) UniqueOnceKey() OnceKey {
// Use the pointer to the registry as the unique key.
return NewCustomOnceKey(r)
}
// The set of registered SdkMemberTypes.
var SdkMemberTypes = &SdkMemberTypesRegistry{}
// Register an SdkMemberType object to allow them to be used in the sdk and sdk_snapshot module
// types.
func RegisterSdkMemberType(memberType SdkMemberType) {
oldList := SdkMemberTypes.list
// Copy the slice just in case this is being read while being modified, e.g. when testing. // Copy the slice just in case this is being read while being modified, e.g. when testing.
list := make([]SdkMemberType, 0, len(oldList)+1) list := make([]SdkMemberType, 0, len(oldList)+1)
@ -319,8 +314,33 @@ func RegisterSdkMemberType(memberType SdkMemberType) {
key := NewOnceKey(strings.Join(properties, "|")) key := NewOnceKey(strings.Join(properties, "|"))
// Create a new registry so the pointer uniquely identifies the set of registered types. // Create a new registry so the pointer uniquely identifies the set of registered types.
SdkMemberTypes = &SdkMemberTypesRegistry{ return &SdkMemberTypesRegistry{
list: list, list: list,
key: key, key: key,
} }
} }
func (r *SdkMemberTypesRegistry) RegisteredTypes() []SdkMemberType {
return r.list
}
func (r *SdkMemberTypesRegistry) UniqueOnceKey() OnceKey {
// Use the pointer to the registry as the unique key.
return NewCustomOnceKey(r)
}
// The set of registered SdkMemberTypes, one for sdk module and one for module_exports.
var ModuleExportsMemberTypes = &SdkMemberTypesRegistry{}
var SdkMemberTypes = &SdkMemberTypesRegistry{}
// Register an SdkMemberType object to allow them to be used in the sdk and sdk_snapshot module
// types.
func RegisterSdkMemberType(memberType SdkMemberType) {
// All member types are usable with module_exports.
ModuleExportsMemberTypes = ModuleExportsMemberTypes.copyAndAppend(memberType)
// Only those that explicitly indicate it are usable with sdk.
if memberType.UsableWithSdkAndSdkSnapshot() {
SdkMemberTypes = SdkMemberTypes.copyAndAppend(memberType)
}
}

View file

@ -27,6 +27,7 @@ import (
var sharedLibrarySdkMemberType = &librarySdkMemberType{ var sharedLibrarySdkMemberType = &librarySdkMemberType{
SdkMemberTypeBase: android.SdkMemberTypeBase{ SdkMemberTypeBase: android.SdkMemberTypeBase{
PropertyName: "native_shared_libs", PropertyName: "native_shared_libs",
SupportsSdk: true,
}, },
prebuiltModuleType: "cc_prebuilt_library_shared", prebuiltModuleType: "cc_prebuilt_library_shared",
linkTypes: []string{"shared"}, linkTypes: []string{"shared"},
@ -35,6 +36,7 @@ var sharedLibrarySdkMemberType = &librarySdkMemberType{
var staticLibrarySdkMemberType = &librarySdkMemberType{ var staticLibrarySdkMemberType = &librarySdkMemberType{
SdkMemberTypeBase: android.SdkMemberTypeBase{ SdkMemberTypeBase: android.SdkMemberTypeBase{
PropertyName: "native_static_libs", PropertyName: "native_static_libs",
SupportsSdk: true,
}, },
prebuiltModuleType: "cc_prebuilt_library_static", prebuiltModuleType: "cc_prebuilt_library_static",
linkTypes: []string{"static"}, linkTypes: []string{"static"},

View file

@ -34,6 +34,9 @@ func init() {
android.RegisterSdkMemberType(&droidStubsSdkMemberType{ android.RegisterSdkMemberType(&droidStubsSdkMemberType{
SdkMemberTypeBase: android.SdkMemberTypeBase{ SdkMemberTypeBase: android.SdkMemberTypeBase{
PropertyName: "stubs_sources", PropertyName: "stubs_sources",
// stubs_sources can be used with sdk to provide the source stubs for APIs provided by
// the APEX.
SupportsSdk: true,
}, },
}) })
} }

View file

@ -41,6 +41,7 @@ func init() {
librarySdkMemberType{ librarySdkMemberType{
android.SdkMemberTypeBase{ android.SdkMemberTypeBase{
PropertyName: "java_header_libs", PropertyName: "java_header_libs",
SupportsSdk: true,
}, },
}, },
}) })

View file

@ -500,8 +500,8 @@ include/Test.h -> include/include/Test.h
func TestSnapshotWithCcStaticLibrary(t *testing.T) { func TestSnapshotWithCcStaticLibrary(t *testing.T) {
result := testSdkWithCc(t, ` result := testSdkWithCc(t, `
sdk { module_exports {
name: "mysdk", name: "myexports",
native_static_libs: ["mynativelib"], native_static_libs: ["mynativelib"],
} }
@ -520,12 +520,12 @@ func TestSnapshotWithCcStaticLibrary(t *testing.T) {
} }
`) `)
result.CheckSnapshot("mysdk", "android_common", "", result.CheckSnapshot("myexports", "android_common", "",
checkAndroidBpContents(` checkAndroidBpContents(`
// This is auto-generated. DO NOT EDIT. // This is auto-generated. DO NOT EDIT.
cc_prebuilt_library_static { cc_prebuilt_library_static {
name: "mysdk_mynativelib@current", name: "myexports_mynativelib@current",
sdk_member_name: "mynativelib", sdk_member_name: "mynativelib",
export_include_dirs: ["include/include"], export_include_dirs: ["include/include"],
arch: { arch: {
@ -560,9 +560,9 @@ cc_prebuilt_library_static {
system_shared_libs: [], system_shared_libs: [],
} }
sdk_snapshot { module_exports_snapshot {
name: "mysdk@current", name: "myexports@current",
native_static_libs: ["mysdk_mynativelib@current"], native_static_libs: ["myexports_mynativelib@current"],
} }
`), `),
checkAllCopyRules(` checkAllCopyRules(`
@ -584,8 +584,8 @@ func TestHostSnapshotWithCcStaticLibrary(t *testing.T) {
SkipIfNotLinux(t) SkipIfNotLinux(t)
result := testSdkWithCc(t, ` result := testSdkWithCc(t, `
sdk { module_exports {
name: "mysdk", name: "myexports",
device_supported: false, device_supported: false,
host_supported: true, host_supported: true,
native_static_libs: ["mynativelib"], native_static_libs: ["mynativelib"],
@ -608,12 +608,12 @@ func TestHostSnapshotWithCcStaticLibrary(t *testing.T) {
} }
`) `)
result.CheckSnapshot("mysdk", "linux_glibc_common", "", result.CheckSnapshot("myexports", "linux_glibc_common", "",
checkAndroidBpContents(` checkAndroidBpContents(`
// This is auto-generated. DO NOT EDIT. // This is auto-generated. DO NOT EDIT.
cc_prebuilt_library_static { cc_prebuilt_library_static {
name: "mysdk_mynativelib@current", name: "myexports_mynativelib@current",
sdk_member_name: "mynativelib", sdk_member_name: "mynativelib",
device_supported: false, device_supported: false,
host_supported: true, host_supported: true,
@ -652,11 +652,11 @@ cc_prebuilt_library_static {
system_shared_libs: [], system_shared_libs: [],
} }
sdk_snapshot { module_exports_snapshot {
name: "mysdk@current", name: "myexports@current",
device_supported: false, device_supported: false,
host_supported: true, host_supported: true,
native_static_libs: ["mysdk_mynativelib@current"], native_static_libs: ["myexports_mynativelib@current"],
} }
`), `),
checkAllCopyRules(` checkAllCopyRules(`

View file

@ -24,16 +24,13 @@ func init() {
// module_exports defines the exports of a mainline module. The exports are Soong modules // module_exports defines the exports of a mainline module. The exports are Soong modules
// which are required by Soong modules that are not part of the mainline module. // which are required by Soong modules that are not part of the mainline module.
func ModuleExportsFactory() android.Module { func ModuleExportsFactory() android.Module {
s := newSdkModule() return newSdkModule(true)
s.properties.Module_exports = true
return s
} }
// module_exports_snapshot is a versioned snapshot of prebuilt versions of all the exports // module_exports_snapshot is a versioned snapshot of prebuilt versions of all the exports
// of a mainline module. // of a mainline module.
func ModuleExportsSnapshotsFactory() android.Module { func ModuleExportsSnapshotsFactory() android.Module {
s := newSdkModule() s := newSdkModule(true)
s.properties.Snapshot = true s.properties.Snapshot = true
s.properties.Module_exports = true
return s return s
} }

View file

@ -214,8 +214,8 @@ aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl
func TestSnapshotWithJavaImplLibrary(t *testing.T) { func TestSnapshotWithJavaImplLibrary(t *testing.T) {
result := testSdkWithJava(t, ` result := testSdkWithJava(t, `
sdk { module_exports {
name: "mysdk", name: "myexports",
java_libs: ["myjavalib"], java_libs: ["myjavalib"],
} }
@ -232,12 +232,12 @@ func TestSnapshotWithJavaImplLibrary(t *testing.T) {
} }
`) `)
result.CheckSnapshot("mysdk", "android_common", "", result.CheckSnapshot("myexports", "android_common", "",
checkAndroidBpContents(` checkAndroidBpContents(`
// This is auto-generated. DO NOT EDIT. // This is auto-generated. DO NOT EDIT.
java_import { java_import {
name: "mysdk_myjavalib@current", name: "myexports_myjavalib@current",
sdk_member_name: "myjavalib", sdk_member_name: "myjavalib",
jars: ["java/myjavalib.jar"], jars: ["java/myjavalib.jar"],
} }
@ -248,9 +248,9 @@ java_import {
jars: ["java/myjavalib.jar"], jars: ["java/myjavalib.jar"],
} }
sdk_snapshot { module_exports_snapshot {
name: "mysdk@current", name: "myexports@current",
java_libs: ["mysdk_myjavalib@current"], java_libs: ["myexports_myjavalib@current"],
} }
`), `),
@ -266,8 +266,8 @@ func TestHostSnapshotWithJavaImplLibrary(t *testing.T) {
SkipIfNotLinux(t) SkipIfNotLinux(t)
result := testSdkWithJava(t, ` result := testSdkWithJava(t, `
sdk { module_exports {
name: "mysdk", name: "myexports",
device_supported: false, device_supported: false,
host_supported: true, host_supported: true,
java_libs: ["myjavalib"], java_libs: ["myjavalib"],
@ -287,12 +287,12 @@ func TestHostSnapshotWithJavaImplLibrary(t *testing.T) {
} }
`) `)
result.CheckSnapshot("mysdk", "linux_glibc_common", "", result.CheckSnapshot("myexports", "linux_glibc_common", "",
checkAndroidBpContents(` checkAndroidBpContents(`
// This is auto-generated. DO NOT EDIT. // This is auto-generated. DO NOT EDIT.
java_import { java_import {
name: "mysdk_myjavalib@current", name: "myexports_myjavalib@current",
sdk_member_name: "myjavalib", sdk_member_name: "myjavalib",
device_supported: false, device_supported: false,
host_supported: true, host_supported: true,
@ -307,11 +307,11 @@ java_import {
jars: ["java/myjavalib.jar"], jars: ["java/myjavalib.jar"],
} }
sdk_snapshot { module_exports_snapshot {
name: "mysdk@current", name: "myexports@current",
device_supported: false, device_supported: false,
host_supported: true, host_supported: true,
java_libs: ["mysdk_myjavalib@current"], java_libs: ["myexports_myjavalib@current"],
} }
`), `),
checkAllCopyRules(` checkAllCopyRules(`
@ -366,8 +366,8 @@ func TestBasicSdkWithDroidstubs(t *testing.T) {
func TestSnapshotWithDroidstubs(t *testing.T) { func TestSnapshotWithDroidstubs(t *testing.T) {
result := testSdkWithDroidstubs(t, ` result := testSdkWithDroidstubs(t, `
sdk { module_exports {
name: "mysdk", name: "myexports",
stubs_sources: ["myjavaapistubs"], stubs_sources: ["myjavaapistubs"],
} }
@ -379,12 +379,12 @@ func TestSnapshotWithDroidstubs(t *testing.T) {
} }
`) `)
result.CheckSnapshot("mysdk", "android_common", "", result.CheckSnapshot("myexports", "android_common", "",
checkAndroidBpContents(` checkAndroidBpContents(`
// This is auto-generated. DO NOT EDIT. // This is auto-generated. DO NOT EDIT.
prebuilt_stubs_sources { prebuilt_stubs_sources {
name: "mysdk_myjavaapistubs@current", name: "myexports_myjavaapistubs@current",
sdk_member_name: "myjavaapistubs", sdk_member_name: "myjavaapistubs",
srcs: ["java/myjavaapistubs_stubs_sources"], srcs: ["java/myjavaapistubs_stubs_sources"],
} }
@ -395,14 +395,14 @@ prebuilt_stubs_sources {
srcs: ["java/myjavaapistubs_stubs_sources"], srcs: ["java/myjavaapistubs_stubs_sources"],
} }
sdk_snapshot { module_exports_snapshot {
name: "mysdk@current", name: "myexports@current",
stubs_sources: ["mysdk_myjavaapistubs@current"], stubs_sources: ["myexports_myjavaapistubs@current"],
} }
`), `),
checkAllCopyRules(""), checkAllCopyRules(""),
checkMergeZip(".intermediates/mysdk/android_common/tmp/java/myjavaapistubs_stubs_sources.zip"), checkMergeZip(".intermediates/myexports/android_common/tmp/java/myjavaapistubs_stubs_sources.zip"),
) )
} }
@ -411,8 +411,8 @@ func TestHostSnapshotWithDroidstubs(t *testing.T) {
SkipIfNotLinux(t) SkipIfNotLinux(t)
result := testSdkWithDroidstubs(t, ` result := testSdkWithDroidstubs(t, `
sdk { module_exports {
name: "mysdk", name: "myexports",
device_supported: false, device_supported: false,
host_supported: true, host_supported: true,
stubs_sources: ["myjavaapistubs"], stubs_sources: ["myjavaapistubs"],
@ -428,12 +428,12 @@ func TestHostSnapshotWithDroidstubs(t *testing.T) {
} }
`) `)
result.CheckSnapshot("mysdk", "linux_glibc_common", "", result.CheckSnapshot("myexports", "linux_glibc_common", "",
checkAndroidBpContents(` checkAndroidBpContents(`
// This is auto-generated. DO NOT EDIT. // This is auto-generated. DO NOT EDIT.
prebuilt_stubs_sources { prebuilt_stubs_sources {
name: "mysdk_myjavaapistubs@current", name: "myexports_myjavaapistubs@current",
sdk_member_name: "myjavaapistubs", sdk_member_name: "myjavaapistubs",
device_supported: false, device_supported: false,
host_supported: true, host_supported: true,
@ -448,14 +448,14 @@ prebuilt_stubs_sources {
srcs: ["java/myjavaapistubs_stubs_sources"], srcs: ["java/myjavaapistubs_stubs_sources"],
} }
sdk_snapshot { module_exports_snapshot {
name: "mysdk@current", name: "myexports@current",
device_supported: false, device_supported: false,
host_supported: true, host_supported: true,
stubs_sources: ["mysdk_myjavaapistubs@current"], stubs_sources: ["myexports_myjavaapistubs@current"],
} }
`), `),
checkAllCopyRules(""), checkAllCopyRules(""),
checkMergeZip(".intermediates/mysdk/linux_glibc_common/tmp/java/myjavaapistubs_stubs_sources.zip"), checkMergeZip(".intermediates/myexports/linux_glibc_common/tmp/java/myjavaapistubs_stubs_sources.zip"),
) )
} }

View file

@ -133,6 +133,7 @@ func getDynamicSdkMemberTypes(registry *android.SdkMemberTypesRegistry) *dynamic
// * a dependency tag that identifies the member type of a resolved dependency. // * a dependency tag that identifies the member type of a resolved dependency.
// //
func createDynamicSdkMemberTypes(sdkMemberTypes []android.SdkMemberType) *dynamicSdkMemberTypes { func createDynamicSdkMemberTypes(sdkMemberTypes []android.SdkMemberType) *dynamicSdkMemberTypes {
var listProperties []*sdkMemberListProperty var listProperties []*sdkMemberListProperty
var fields []reflect.StructField var fields []reflect.StructField
@ -186,13 +187,20 @@ func createDynamicSdkMemberTypes(sdkMemberTypes []android.SdkMemberType) *dynami
// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.) // 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. // which Mainline modules like APEX can choose to build with.
func SdkModuleFactory() android.Module { func SdkModuleFactory() android.Module {
return newSdkModule() return newSdkModule(false)
} }
func newSdkModule() *sdk { func newSdkModule(moduleExports bool) *sdk {
s := &sdk{} s := &sdk{}
s.properties.Module_exports = moduleExports
// Get the dynamic sdk member type data for the currently registered sdk member types. // Get the dynamic sdk member type data for the currently registered sdk member types.
s.dynamicSdkMemberTypes = getDynamicSdkMemberTypes(android.SdkMemberTypes) var registry *android.SdkMemberTypesRegistry
if moduleExports {
registry = android.ModuleExportsMemberTypes
} else {
registry = android.SdkMemberTypes
}
s.dynamicSdkMemberTypes = getDynamicSdkMemberTypes(registry)
// Create an instance of the dynamically created struct that contains all the // Create an instance of the dynamically created struct that contains all the
// properties for the member type specific list properties. // properties for the member type specific list properties.
s.dynamicMemberTypeListProperties = s.dynamicSdkMemberTypes.createMemberListProperties() s.dynamicMemberTypeListProperties = s.dynamicSdkMemberTypes.createMemberListProperties()
@ -211,7 +219,7 @@ func newSdkModule() *sdk {
// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module. // sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
func SnapshotModuleFactory() android.Module { func SnapshotModuleFactory() android.Module {
s := newSdkModule() s := newSdkModule(false)
s.properties.Snapshot = true s.properties.Snapshot = true
return s return s
} }