Add support to sdk/module_exports to specify required traits
Currently, every sdk member of a specific module type has to be treated in the same way as every other sdk member of that type. e.g. it is not possible for an sdk member to use different variants to other members of the same type. Adding a new member type for each different way to treat the members is not scalable as if there were N different ways treat a member then it would require 2^N types for all the possible combinations. This adds a new traits mechanism that allows the behavior of member types to be customized per sdk member. Each member type can specify a list of supported traits and customize its behavior based on which traits are required for each member. A trait can be supported by multiple different member types. Bug: 195754365 Test: m nothing Change-Id: I165ac80d208c0402d2a9ffa8085bba29562c19b7
This commit is contained in:
parent
66213a64b2
commit
d19f894512
6 changed files with 707 additions and 3 deletions
191
android/sdk.go
191
android/sdk.go
|
@ -15,6 +15,7 @@
|
|||
package android
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
|
@ -376,6 +377,175 @@ func (b BpPrintableBase) bpPrintable() {
|
|||
|
||||
var _ BpPrintable = BpPrintableBase{}
|
||||
|
||||
// SdkMemberTrait represents a trait that members of an sdk module can contribute to the sdk
|
||||
// snapshot.
|
||||
//
|
||||
// A trait is simply a characteristic of sdk member that is not required by default which may be
|
||||
// required for some members but not others. Traits can cause additional information to be output
|
||||
// to the sdk snapshot or replace the default information exported for a member with something else.
|
||||
// e.g.
|
||||
// * By default cc libraries only export the default image variants to the SDK. However, for some
|
||||
// members it may be necessary to export specific image variants, e.g. vendor, or recovery.
|
||||
// * By default cc libraries export all the configured architecture variants except for the native
|
||||
// bridge architecture variants. However, for some members it may be necessary to export the
|
||||
// native bridge architecture variants as well.
|
||||
// * By default cc libraries export the platform variant (i.e. sdk:). However, for some members it
|
||||
// may be necessary to export the sdk variant (i.e. sdk:sdk).
|
||||
//
|
||||
// A sdk can request a module to provide no traits, one trait or a collection of traits. The exact
|
||||
// behavior of a trait is determined by how SdkMemberType implementations handle the traits. A trait
|
||||
// could be specific to one SdkMemberType or many. Some trait combinations could be incompatible.
|
||||
//
|
||||
// The sdk module type will create a special traits structure that contains a property for each
|
||||
// trait registered with RegisterSdkMemberTrait(). The property names are those returned from
|
||||
// SdkPropertyName(). Each property contains a list of modules that are required to have that trait.
|
||||
// e.g. something like this:
|
||||
//
|
||||
// sdk {
|
||||
// name: "sdk",
|
||||
// ...
|
||||
// traits: {
|
||||
// recovery_image: ["module1", "module4", "module5"],
|
||||
// native_bridge: ["module1", "module2"],
|
||||
// native_sdk: ["module1", "module3"],
|
||||
// ...
|
||||
// },
|
||||
// ...
|
||||
// }
|
||||
type SdkMemberTrait interface {
|
||||
// SdkPropertyName returns the name of the traits property on an sdk module.
|
||||
SdkPropertyName() string
|
||||
}
|
||||
|
||||
// SdkMemberTraitBase is the base struct that must be embedded within any type that implements
|
||||
// SdkMemberTrait.
|
||||
type SdkMemberTraitBase struct {
|
||||
// PropertyName is the name of the property
|
||||
PropertyName string
|
||||
}
|
||||
|
||||
func (b *SdkMemberTraitBase) SdkPropertyName() string {
|
||||
return b.PropertyName
|
||||
}
|
||||
|
||||
// SdkMemberTraitSet is a set of SdkMemberTrait instances.
|
||||
type SdkMemberTraitSet interface {
|
||||
// Empty returns true if this set is empty.
|
||||
Empty() bool
|
||||
|
||||
// Contains returns true if this set contains the specified trait.
|
||||
Contains(trait SdkMemberTrait) bool
|
||||
|
||||
// Subtract returns a new set containing all elements of this set except for those in the
|
||||
// other set.
|
||||
Subtract(other SdkMemberTraitSet) SdkMemberTraitSet
|
||||
|
||||
// String returns a string representation of the set and its contents.
|
||||
String() string
|
||||
}
|
||||
|
||||
func NewSdkMemberTraitSet(traits []SdkMemberTrait) SdkMemberTraitSet {
|
||||
if len(traits) == 0 {
|
||||
return EmptySdkMemberTraitSet()
|
||||
}
|
||||
|
||||
m := sdkMemberTraitSet{}
|
||||
for _, trait := range traits {
|
||||
m[trait] = true
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func EmptySdkMemberTraitSet() SdkMemberTraitSet {
|
||||
return (sdkMemberTraitSet)(nil)
|
||||
}
|
||||
|
||||
type sdkMemberTraitSet map[SdkMemberTrait]bool
|
||||
|
||||
var _ SdkMemberTraitSet = (sdkMemberTraitSet{})
|
||||
|
||||
func (s sdkMemberTraitSet) Empty() bool {
|
||||
return len(s) == 0
|
||||
}
|
||||
|
||||
func (s sdkMemberTraitSet) Contains(trait SdkMemberTrait) bool {
|
||||
return s[trait]
|
||||
}
|
||||
|
||||
func (s sdkMemberTraitSet) Subtract(other SdkMemberTraitSet) SdkMemberTraitSet {
|
||||
if other.Empty() {
|
||||
return s
|
||||
}
|
||||
|
||||
var remainder []SdkMemberTrait
|
||||
for trait, _ := range s {
|
||||
if !other.Contains(trait) {
|
||||
remainder = append(remainder, trait)
|
||||
}
|
||||
}
|
||||
|
||||
return NewSdkMemberTraitSet(remainder)
|
||||
}
|
||||
|
||||
func (s sdkMemberTraitSet) String() string {
|
||||
list := []string{}
|
||||
for trait, _ := range s {
|
||||
list = append(list, trait.SdkPropertyName())
|
||||
}
|
||||
sort.Strings(list)
|
||||
return fmt.Sprintf("[%s]", strings.Join(list, ","))
|
||||
}
|
||||
|
||||
// SdkMemberTraitsRegistry is a registry of SdkMemberTrait instances.
|
||||
type SdkMemberTraitsRegistry struct {
|
||||
// The list of traits sorted by property name.
|
||||
list []SdkMemberTrait
|
||||
}
|
||||
|
||||
// copyAndAppend creates a new SdkMemberTraitsRegistry that includes all the traits registered in
|
||||
// this registry plus the supplied trait.
|
||||
func (r *SdkMemberTraitsRegistry) copyAndAppend(trait SdkMemberTrait) *SdkMemberTraitsRegistry {
|
||||
oldList := r.list
|
||||
|
||||
// Copy the slice just in case this is being read while being modified, e.g. when testing.
|
||||
list := make([]SdkMemberTrait, 0, len(oldList)+1)
|
||||
list = append(list, oldList...)
|
||||
list = append(list, trait)
|
||||
|
||||
// Sort the member types by their property name to ensure that registry order has no effect
|
||||
// on behavior.
|
||||
sort.Slice(list, func(i1, i2 int) bool {
|
||||
t1 := list[i1]
|
||||
t2 := list[i2]
|
||||
|
||||
return t1.SdkPropertyName() < t2.SdkPropertyName()
|
||||
})
|
||||
|
||||
// Create a new registry so the pointer uniquely identifies the set of registered types.
|
||||
return &SdkMemberTraitsRegistry{
|
||||
list: list,
|
||||
}
|
||||
}
|
||||
|
||||
// RegisteredTraits returns the list of registered SdkMemberTrait instances.
|
||||
func (r *SdkMemberTraitsRegistry) RegisteredTraits() []SdkMemberTrait {
|
||||
return r.list
|
||||
}
|
||||
|
||||
// UniqueOnceKey returns a key to use with Config.Once that uniquely identifies this instance.
|
||||
func (r *SdkMemberTraitsRegistry) UniqueOnceKey() OnceKey {
|
||||
// Use the pointer to the registry as the unique key.
|
||||
return NewCustomOnceKey(r)
|
||||
}
|
||||
|
||||
var RegisteredSdkMemberTraits = &SdkMemberTraitsRegistry{}
|
||||
|
||||
// RegisterSdkMemberTrait registers an SdkMemberTrait object to allow them to be used in the
|
||||
// module_exports, module_exports_snapshot, sdk and sdk_snapshot module types.
|
||||
func RegisterSdkMemberTrait(trait SdkMemberTrait) {
|
||||
RegisteredSdkMemberTraits = RegisteredSdkMemberTraits.copyAndAppend(trait)
|
||||
}
|
||||
|
||||
// SdkMember is an individual member of the SDK.
|
||||
//
|
||||
// It includes all of the variants that the SDK depends upon.
|
||||
|
@ -541,12 +711,23 @@ type SdkMemberType interface {
|
|||
// CreateVariantPropertiesStruct creates a structure into which variant specific properties can be
|
||||
// added.
|
||||
CreateVariantPropertiesStruct() SdkMemberProperties
|
||||
|
||||
// SupportedTraits returns the set of traits supported by this member type.
|
||||
SupportedTraits() SdkMemberTraitSet
|
||||
}
|
||||
|
||||
// SdkDependencyContext provides access to information needed by the SdkMemberType.AddDependencies()
|
||||
// implementations.
|
||||
type SdkDependencyContext interface {
|
||||
BottomUpMutatorContext
|
||||
|
||||
// RequiredTraits returns the set of SdkMemberTrait instances that the sdk requires the named
|
||||
// member to provide.
|
||||
RequiredTraits(name string) SdkMemberTraitSet
|
||||
|
||||
// RequiresTrait returns true if the sdk requires the member with the supplied name to provide the
|
||||
// supplied trait.
|
||||
RequiresTrait(name string, trait SdkMemberTrait) bool
|
||||
}
|
||||
|
||||
// SdkMemberTypeBase is the base type for SdkMemberType implementations and must be embedded in any
|
||||
|
@ -565,6 +746,9 @@ type SdkMemberTypeBase struct {
|
|||
// module type in its SdkMemberType.AddPrebuiltModule() method. That prevents the sdk snapshot
|
||||
// code from automatically adding a prefer: true flag.
|
||||
UseSourceModuleTypeInSnapshot bool
|
||||
|
||||
// The list of supported traits.
|
||||
Traits []SdkMemberTrait
|
||||
}
|
||||
|
||||
func (b *SdkMemberTypeBase) SdkPropertyName() string {
|
||||
|
@ -587,6 +771,10 @@ func (b *SdkMemberTypeBase) UsesSourceModuleTypeInSnapshot() bool {
|
|||
return b.UseSourceModuleTypeInSnapshot
|
||||
}
|
||||
|
||||
func (b *SdkMemberTypeBase) SupportedTraits() SdkMemberTraitSet {
|
||||
return NewSdkMemberTraitSet(b.Traits)
|
||||
}
|
||||
|
||||
// SdkMemberTypesRegistry encapsulates the information about registered SdkMemberTypes.
|
||||
type SdkMemberTypesRegistry struct {
|
||||
// The list of types sorted by property name.
|
||||
|
@ -733,6 +921,9 @@ type SdkMemberContext interface {
|
|||
// Provided for use by sdk members to create a member specific location within the snapshot
|
||||
// into which to copy the prebuilt files.
|
||||
Name() string
|
||||
|
||||
// RequiresTrait returns true if this member is expected to provide the specified trait.
|
||||
RequiresTrait(trait SdkMemberTrait) bool
|
||||
}
|
||||
|
||||
// ExportedComponentsInfo contains information about the components that this module exports to an
|
||||
|
|
|
@ -16,6 +16,7 @@ bootstrap_go_package {
|
|||
srcs: [
|
||||
"bp.go",
|
||||
"exports.go",
|
||||
"member_trait.go",
|
||||
"member_type.go",
|
||||
"sdk.go",
|
||||
"update.go",
|
||||
|
@ -28,6 +29,7 @@ bootstrap_go_package {
|
|||
"exports_test.go",
|
||||
"java_sdk_test.go",
|
||||
"license_sdk_test.go",
|
||||
"member_trait_test.go",
|
||||
"sdk_test.go",
|
||||
"testing.go",
|
||||
],
|
||||
|
|
133
sdk/member_trait.go
Normal file
133
sdk/member_trait.go
Normal file
|
@ -0,0 +1,133 @@
|
|||
// Copyright (C) 2021 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 (
|
||||
"reflect"
|
||||
|
||||
"android/soong/android"
|
||||
"github.com/google/blueprint/proptools"
|
||||
)
|
||||
|
||||
// Contains information about the sdk properties that list sdk members by trait, e.g.
|
||||
// native_bridge.
|
||||
type sdkMemberTraitListProperty struct {
|
||||
// getter for the list of member names
|
||||
getter func(properties interface{}) []string
|
||||
|
||||
// the trait of member referenced in the list
|
||||
memberTrait android.SdkMemberTrait
|
||||
}
|
||||
|
||||
// Cache of dynamically generated dynamicSdkMemberTraits objects. The key is the pointer
|
||||
// to a slice of SdkMemberTrait instances held in android.RegisteredSdkMemberTraits.
|
||||
var dynamicSdkMemberTraitsMap android.OncePer
|
||||
|
||||
// A dynamically generated set of member list properties and associated structure type.
|
||||
//
|
||||
// Instances of this are created by createDynamicSdkMemberTraits.
|
||||
type dynamicSdkMemberTraits struct {
|
||||
// The dynamically generated structure type.
|
||||
//
|
||||
// Contains one []string exported field for each android.RegisteredSdkMemberTraits. The name of
|
||||
// the field is the exported form of the value returned by SdkMemberTrait.SdkPropertyName().
|
||||
propertiesStructType reflect.Type
|
||||
|
||||
// Information about each of the member trait specific list properties.
|
||||
memberTraitListProperties []*sdkMemberTraitListProperty
|
||||
}
|
||||
|
||||
func (d *dynamicSdkMemberTraits) createMemberTraitListProperties() interface{} {
|
||||
return reflect.New(d.propertiesStructType).Interface()
|
||||
}
|
||||
|
||||
func getDynamicSdkMemberTraits(registry *android.SdkMemberTraitsRegistry) *dynamicSdkMemberTraits {
|
||||
|
||||
// Get a key that uniquely identifies the registry contents.
|
||||
key := registry.UniqueOnceKey()
|
||||
|
||||
// Get the registered traits.
|
||||
registeredTraits := registry.RegisteredTraits()
|
||||
|
||||
// Get the cached value, creating new instance if necessary.
|
||||
return dynamicSdkMemberTraitsMap.Once(key, func() interface{} {
|
||||
return createDynamicSdkMemberTraits(registeredTraits)
|
||||
}).(*dynamicSdkMemberTraits)
|
||||
}
|
||||
|
||||
// Create the dynamicSdkMemberTraits from the list of registered member traits.
|
||||
//
|
||||
// A struct is created which contains one exported field per member trait corresponding to
|
||||
// the SdkMemberTrait.SdkPropertyName() value.
|
||||
//
|
||||
// A list of sdkMemberTraitListProperty instances is created, one per member trait that provides:
|
||||
// * a reference to the member trait.
|
||||
// * a getter for the corresponding field in the properties struct.
|
||||
//
|
||||
func createDynamicSdkMemberTraits(sdkMemberTraits []android.SdkMemberTrait) *dynamicSdkMemberTraits {
|
||||
|
||||
var listProperties []*sdkMemberTraitListProperty
|
||||
memberTraitToProperty := map[android.SdkMemberTrait]*sdkMemberTraitListProperty{}
|
||||
var fields []reflect.StructField
|
||||
|
||||
// Iterate over the member traits creating StructField and sdkMemberTraitListProperty objects.
|
||||
nextFieldIndex := 0
|
||||
for _, memberTrait := range sdkMemberTraits {
|
||||
|
||||
p := memberTrait.SdkPropertyName()
|
||||
|
||||
var getter func(properties interface{}) []string
|
||||
|
||||
// Create a dynamic exported field for the member trait's property.
|
||||
fields = append(fields, reflect.StructField{
|
||||
Name: proptools.FieldNameForProperty(p),
|
||||
Type: reflect.TypeOf([]string{}),
|
||||
})
|
||||
|
||||
// Copy the field index for use in the getter func as using the loop variable directly will
|
||||
// cause all funcs to use the last value.
|
||||
fieldIndex := nextFieldIndex
|
||||
nextFieldIndex += 1
|
||||
|
||||
getter = func(properties interface{}) []string {
|
||||
// The properties is expected to be of the following form (where
|
||||
// <Module_traits> is the name of an SdkMemberTrait.SdkPropertyName().
|
||||
// properties *struct {<Module_traits> []string, ....}
|
||||
//
|
||||
// Although it accesses the field by index the following reflection code is equivalent to:
|
||||
// *properties.<Module_traits>
|
||||
//
|
||||
list := reflect.ValueOf(properties).Elem().Field(fieldIndex).Interface().([]string)
|
||||
return list
|
||||
}
|
||||
|
||||
// Create an sdkMemberTraitListProperty for the member trait.
|
||||
memberListProperty := &sdkMemberTraitListProperty{
|
||||
getter: getter,
|
||||
memberTrait: memberTrait,
|
||||
}
|
||||
|
||||
memberTraitToProperty[memberTrait] = memberListProperty
|
||||
listProperties = append(listProperties, memberListProperty)
|
||||
}
|
||||
|
||||
// Create a dynamic struct from the collated fields.
|
||||
propertiesStructType := reflect.StructOf(fields)
|
||||
|
||||
return &dynamicSdkMemberTraits{
|
||||
memberTraitListProperties: listProperties,
|
||||
propertiesStructType: propertiesStructType,
|
||||
}
|
||||
}
|
287
sdk/member_trait_test.go
Normal file
287
sdk/member_trait_test.go
Normal file
|
@ -0,0 +1,287 @@
|
|||
// Copyright (C) 2021 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 (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"android/soong/android"
|
||||
"android/soong/java"
|
||||
"github.com/google/blueprint"
|
||||
)
|
||||
|
||||
type fakeMemberTrait struct {
|
||||
android.SdkMemberTraitBase
|
||||
}
|
||||
|
||||
type fakeMemberType struct {
|
||||
android.SdkMemberTypeBase
|
||||
}
|
||||
|
||||
func (t *fakeMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
|
||||
for _, name := range names {
|
||||
ctx.AddVariationDependencies(nil, dependencyTag, name)
|
||||
|
||||
if ctx.RequiresTrait(name, extraTrait) {
|
||||
ctx.AddVariationDependencies(nil, dependencyTag, name+"_extra")
|
||||
}
|
||||
if ctx.RequiresTrait(name, specialTrait) {
|
||||
ctx.AddVariationDependencies(nil, dependencyTag, name+"_special")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (t *fakeMemberType) IsInstance(module android.Module) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (t *fakeMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
|
||||
moduleType := "java_import"
|
||||
if ctx.RequiresTrait(extraTrait) {
|
||||
moduleType = "java_test_import"
|
||||
}
|
||||
return ctx.SnapshotBuilder().AddPrebuiltModule(member, moduleType)
|
||||
}
|
||||
|
||||
func (t *fakeMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
|
||||
return &fakeMemberTypeProperties{}
|
||||
}
|
||||
|
||||
type fakeMemberTypeProperties struct {
|
||||
android.SdkMemberPropertiesBase
|
||||
|
||||
path android.Path
|
||||
}
|
||||
|
||||
func (t *fakeMemberTypeProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
|
||||
headerJars := variant.(java.ApexDependency).HeaderJars()
|
||||
if len(headerJars) != 1 {
|
||||
panic(fmt.Errorf("there must be only one header jar from %q", variant.Name()))
|
||||
}
|
||||
|
||||
t.path = headerJars[0]
|
||||
}
|
||||
|
||||
func (t *fakeMemberTypeProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
|
||||
if t.path != nil {
|
||||
relative := filepath.Join("javalibs", t.path.Base())
|
||||
ctx.SnapshotBuilder().CopyToSnapshot(t.path, relative)
|
||||
propertySet.AddProperty("jars", []string{relative})
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
extraTrait = &fakeMemberTrait{
|
||||
SdkMemberTraitBase: android.SdkMemberTraitBase{
|
||||
PropertyName: "extra",
|
||||
},
|
||||
}
|
||||
|
||||
specialTrait = &fakeMemberTrait{
|
||||
SdkMemberTraitBase: android.SdkMemberTraitBase{
|
||||
PropertyName: "special",
|
||||
},
|
||||
}
|
||||
|
||||
fakeType = &fakeMemberType{
|
||||
SdkMemberTypeBase: android.SdkMemberTypeBase{
|
||||
PropertyName: "fake_members",
|
||||
SupportsSdk: true,
|
||||
Traits: []android.SdkMemberTrait{
|
||||
extraTrait,
|
||||
specialTrait,
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
android.RegisterSdkMemberTrait(extraTrait)
|
||||
android.RegisterSdkMemberTrait(specialTrait)
|
||||
android.RegisterSdkMemberType(fakeType)
|
||||
}
|
||||
|
||||
func TestBasicTrait_WithoutTrait(t *testing.T) {
|
||||
result := android.GroupFixturePreparers(
|
||||
prepareForSdkTestWithJava,
|
||||
android.FixtureWithRootAndroidBp(`
|
||||
sdk {
|
||||
name: "mysdk",
|
||||
fake_members: ["myjavalib"],
|
||||
}
|
||||
|
||||
java_library {
|
||||
name: "myjavalib",
|
||||
srcs: ["Test.java"],
|
||||
system_modules: "none",
|
||||
sdk_version: "none",
|
||||
}
|
||||
`),
|
||||
).RunTest(t)
|
||||
|
||||
CheckSnapshot(t, result, "mysdk", "",
|
||||
checkUnversionedAndroidBpContents(`
|
||||
// This is auto-generated. DO NOT EDIT.
|
||||
|
||||
java_import {
|
||||
name: "myjavalib",
|
||||
prefer: false,
|
||||
visibility: ["//visibility:public"],
|
||||
apex_available: ["//apex_available:platform"],
|
||||
jars: ["javalibs/myjavalib.jar"],
|
||||
}
|
||||
`),
|
||||
checkVersionedAndroidBpContents(`
|
||||
// This is auto-generated. DO NOT EDIT.
|
||||
|
||||
java_import {
|
||||
name: "mysdk_myjavalib@current",
|
||||
sdk_member_name: "myjavalib",
|
||||
visibility: ["//visibility:public"],
|
||||
apex_available: ["//apex_available:platform"],
|
||||
jars: ["javalibs/myjavalib.jar"],
|
||||
}
|
||||
|
||||
sdk_snapshot {
|
||||
name: "mysdk@current",
|
||||
visibility: ["//visibility:public"],
|
||||
fake_members: ["mysdk_myjavalib@current"],
|
||||
}
|
||||
`),
|
||||
)
|
||||
}
|
||||
|
||||
func TestBasicTrait_MultipleTraits(t *testing.T) {
|
||||
result := android.GroupFixturePreparers(
|
||||
prepareForSdkTestWithJava,
|
||||
android.FixtureWithRootAndroidBp(`
|
||||
sdk {
|
||||
name: "mysdk",
|
||||
fake_members: ["myjavalib", "anotherjavalib"],
|
||||
traits: {
|
||||
extra: ["myjavalib"],
|
||||
special: ["myjavalib", "anotherjavalib"],
|
||||
},
|
||||
}
|
||||
|
||||
java_library {
|
||||
name: "myjavalib",
|
||||
srcs: ["Test.java"],
|
||||
system_modules: "none",
|
||||
sdk_version: "none",
|
||||
}
|
||||
|
||||
java_library {
|
||||
name: "myjavalib_extra",
|
||||
srcs: ["Test.java"],
|
||||
system_modules: "none",
|
||||
sdk_version: "none",
|
||||
}
|
||||
|
||||
java_library {
|
||||
name: "myjavalib_special",
|
||||
srcs: ["Test.java"],
|
||||
system_modules: "none",
|
||||
sdk_version: "none",
|
||||
}
|
||||
|
||||
java_library {
|
||||
name: "anotherjavalib",
|
||||
srcs: ["Test.java"],
|
||||
system_modules: "none",
|
||||
sdk_version: "none",
|
||||
}
|
||||
|
||||
java_library {
|
||||
name: "anotherjavalib_special",
|
||||
srcs: ["Test.java"],
|
||||
system_modules: "none",
|
||||
sdk_version: "none",
|
||||
}
|
||||
`),
|
||||
).RunTest(t)
|
||||
|
||||
CheckSnapshot(t, result, "mysdk", "",
|
||||
checkUnversionedAndroidBpContents(`
|
||||
// This is auto-generated. DO NOT EDIT.
|
||||
|
||||
java_test_import {
|
||||
name: "myjavalib",
|
||||
prefer: false,
|
||||
visibility: ["//visibility:public"],
|
||||
apex_available: ["//apex_available:platform"],
|
||||
jars: ["javalibs/myjavalib.jar"],
|
||||
}
|
||||
|
||||
java_import {
|
||||
name: "myjavalib_extra",
|
||||
prefer: false,
|
||||
visibility: ["//visibility:public"],
|
||||
apex_available: ["//apex_available:platform"],
|
||||
jars: ["javalibs/myjavalib_extra.jar"],
|
||||
}
|
||||
|
||||
java_import {
|
||||
name: "myjavalib_special",
|
||||
prefer: false,
|
||||
visibility: ["//visibility:public"],
|
||||
apex_available: ["//apex_available:platform"],
|
||||
jars: ["javalibs/myjavalib_special.jar"],
|
||||
}
|
||||
|
||||
java_import {
|
||||
name: "anotherjavalib",
|
||||
prefer: false,
|
||||
visibility: ["//visibility:public"],
|
||||
apex_available: ["//apex_available:platform"],
|
||||
jars: ["javalibs/anotherjavalib.jar"],
|
||||
}
|
||||
|
||||
java_import {
|
||||
name: "anotherjavalib_special",
|
||||
prefer: false,
|
||||
visibility: ["//visibility:public"],
|
||||
apex_available: ["//apex_available:platform"],
|
||||
jars: ["javalibs/anotherjavalib_special.jar"],
|
||||
}
|
||||
`),
|
||||
)
|
||||
}
|
||||
|
||||
func TestTraitUnsupportedByMemberType(t *testing.T) {
|
||||
android.GroupFixturePreparers(
|
||||
prepareForSdkTestWithJava,
|
||||
android.FixtureWithRootAndroidBp(`
|
||||
sdk {
|
||||
name: "mysdk",
|
||||
java_header_libs: ["myjavalib"],
|
||||
traits: {
|
||||
extra: ["myjavalib"],
|
||||
},
|
||||
}
|
||||
|
||||
java_library {
|
||||
name: "myjavalib",
|
||||
srcs: ["Test.java"],
|
||||
system_modules: "none",
|
||||
sdk_version: "none",
|
||||
}
|
||||
`),
|
||||
).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
|
||||
`\Qsdk member "myjavalib" has traits [extra] that are unsupported by its member type "java_header_libs"\E`)).
|
||||
RunTest(t)
|
||||
}
|
80
sdk/sdk.go
80
sdk/sdk.go
|
@ -53,6 +53,13 @@ type sdk struct {
|
|||
// list properties, e.g. java_libs.
|
||||
dynamicMemberTypeListProperties interface{}
|
||||
|
||||
// The dynamically generated information about the registered SdkMemberTrait
|
||||
dynamicSdkMemberTraits *dynamicSdkMemberTraits
|
||||
|
||||
// The dynamically created instance of the properties struct containing the sdk member trait
|
||||
// list properties.
|
||||
dynamicMemberTraitListProperties interface{}
|
||||
|
||||
// Information about the OsType specific member variants depended upon by this variant.
|
||||
//
|
||||
// Set by OsType specific variants in the collectMembers() method and used by the
|
||||
|
@ -114,7 +121,20 @@ func newSdkModule(moduleExports bool) *sdk {
|
|||
// Create an instance of the dynamically created struct that contains all the
|
||||
// properties for the member type specific list properties.
|
||||
s.dynamicMemberTypeListProperties = s.dynamicSdkMemberTypes.createMemberTypeListProperties()
|
||||
s.AddProperties(&s.properties, s.dynamicMemberTypeListProperties)
|
||||
|
||||
traitRegistry := android.RegisteredSdkMemberTraits
|
||||
s.dynamicSdkMemberTraits = getDynamicSdkMemberTraits(traitRegistry)
|
||||
// Create an instance of the dynamically created struct that contains all the properties for the
|
||||
// member trait specific list properties.
|
||||
s.dynamicMemberTraitListProperties = s.dynamicSdkMemberTraits.createMemberTraitListProperties()
|
||||
|
||||
// Create a wrapper around the dynamic trait specific properties so that they have to be
|
||||
// specified within a traits:{} section in the .bp file.
|
||||
traitsWrapper := struct {
|
||||
Traits interface{}
|
||||
}{s.dynamicMemberTraitListProperties}
|
||||
|
||||
s.AddProperties(&s.properties, s.dynamicMemberTypeListProperties, &traitsWrapper)
|
||||
|
||||
// Make sure that the prebuilt visibility property is verified for errors.
|
||||
android.AddVisibilityProperty(s, "prebuilt_visibility", &s.properties.Prebuilt_visibility)
|
||||
|
@ -145,6 +165,11 @@ func (s *sdk) memberTypeListProperty(memberType android.SdkMemberType) *sdkMembe
|
|||
return s.dynamicSdkMemberTypes.memberTypeToProperty[memberType]
|
||||
}
|
||||
|
||||
// memberTraitListProperties returns the list of *sdkMemberTraitListProperty instances for this sdk.
|
||||
func (s *sdk) memberTraitListProperties() []*sdkMemberTraitListProperty {
|
||||
return s.dynamicSdkMemberTraits.memberTraitListProperties
|
||||
}
|
||||
|
||||
func (s *sdk) snapshot() bool {
|
||||
return s.properties.Snapshot
|
||||
}
|
||||
|
@ -198,15 +223,53 @@ func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries {
|
|||
}}
|
||||
}
|
||||
|
||||
// gatherTraits gathers the traits from the dynamically generated trait specific properties.
|
||||
//
|
||||
// Returns a map from member name to the set of required traits.
|
||||
func (s *sdk) gatherTraits() map[string]android.SdkMemberTraitSet {
|
||||
traitListByMember := map[string][]android.SdkMemberTrait{}
|
||||
for _, memberListProperty := range s.memberTraitListProperties() {
|
||||
names := memberListProperty.getter(s.dynamicMemberTraitListProperties)
|
||||
for _, name := range names {
|
||||
traitListByMember[name] = append(traitListByMember[name], memberListProperty.memberTrait)
|
||||
}
|
||||
}
|
||||
|
||||
traitSetByMember := map[string]android.SdkMemberTraitSet{}
|
||||
for name, list := range traitListByMember {
|
||||
traitSetByMember[name] = android.NewSdkMemberTraitSet(list)
|
||||
}
|
||||
|
||||
return traitSetByMember
|
||||
}
|
||||
|
||||
// newDependencyContext creates a new SdkDependencyContext for this sdk.
|
||||
func (s *sdk) newDependencyContext(mctx android.BottomUpMutatorContext) android.SdkDependencyContext {
|
||||
traits := s.gatherTraits()
|
||||
|
||||
return &dependencyContext{
|
||||
BottomUpMutatorContext: mctx,
|
||||
requiredTraits: traits,
|
||||
}
|
||||
}
|
||||
|
||||
type dependencyContext struct {
|
||||
android.BottomUpMutatorContext
|
||||
|
||||
// Map from member name to the set of traits that the sdk requires the member provides.
|
||||
requiredTraits map[string]android.SdkMemberTraitSet
|
||||
}
|
||||
|
||||
func (d *dependencyContext) RequiredTraits(name string) android.SdkMemberTraitSet {
|
||||
if s, ok := d.requiredTraits[name]; ok {
|
||||
return s
|
||||
} else {
|
||||
return android.EmptySdkMemberTraitSet()
|
||||
}
|
||||
}
|
||||
|
||||
func (d *dependencyContext) RequiresTrait(name string, trait android.SdkMemberTrait) bool {
|
||||
return d.RequiredTraits(name).Contains(trait)
|
||||
}
|
||||
|
||||
var _ android.SdkDependencyContext = (*dependencyContext)(nil)
|
||||
|
@ -287,8 +350,21 @@ func memberMutator(mctx android.BottomUpMutatorContext) {
|
|||
}
|
||||
names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
|
||||
if len(names) > 0 {
|
||||
memberType := memberListProperty.memberType
|
||||
|
||||
// Verify that the member type supports the specified traits.
|
||||
supportedTraits := memberType.SupportedTraits()
|
||||
for _, name := range names {
|
||||
requiredTraits := ctx.RequiredTraits(name)
|
||||
unsupportedTraits := requiredTraits.Subtract(supportedTraits)
|
||||
if !unsupportedTraits.Empty() {
|
||||
ctx.ModuleErrorf("sdk member %q has traits %s that are unsupported by its member type %q", name, unsupportedTraits, memberType.SdkPropertyName())
|
||||
}
|
||||
}
|
||||
|
||||
// Add dependencies using the appropriate tag.
|
||||
tag := memberListProperty.dependencyTag
|
||||
memberListProperty.memberType.AddDependencies(ctx, tag, names)
|
||||
memberType.AddDependencies(ctx, tag, names)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -393,10 +393,18 @@ be unnecessary as every module in the sdk already has its own licenses property.
|
|||
members := s.groupMemberVariantsByMemberThenType(ctx, memberVariantDeps)
|
||||
|
||||
// Create the prebuilt modules for each of the member modules.
|
||||
traits := s.gatherTraits()
|
||||
for _, member := range members {
|
||||
memberType := member.memberType
|
||||
|
||||
memberCtx := &memberContext{ctx, builder, memberType, member.name}
|
||||
name := member.name
|
||||
requiredTraits := traits[name]
|
||||
if requiredTraits == nil {
|
||||
requiredTraits = android.EmptySdkMemberTraitSet()
|
||||
}
|
||||
|
||||
// Create the snapshot for the member.
|
||||
memberCtx := &memberContext{ctx, builder, memberType, name, requiredTraits}
|
||||
|
||||
prebuiltModule := memberType.AddPrebuiltModule(memberCtx, member)
|
||||
s.createMemberSnapshot(memberCtx, member, prebuiltModule.(*bpModule))
|
||||
|
@ -1651,6 +1659,9 @@ type memberContext struct {
|
|||
builder *snapshotBuilder
|
||||
memberType android.SdkMemberType
|
||||
name string
|
||||
|
||||
// The set of traits required of this member.
|
||||
requiredTraits android.SdkMemberTraitSet
|
||||
}
|
||||
|
||||
func (m *memberContext) SdkModuleContext() android.ModuleContext {
|
||||
|
@ -1669,6 +1680,10 @@ func (m *memberContext) Name() string {
|
|||
return m.name
|
||||
}
|
||||
|
||||
func (m *memberContext) RequiresTrait(trait android.SdkMemberTrait) bool {
|
||||
return m.requiredTraits.Contains(trait)
|
||||
}
|
||||
|
||||
func (s *sdk) createMemberSnapshot(ctx *memberContext, member *sdkMember, bpModule *bpModule) {
|
||||
|
||||
memberType := member.memberType
|
||||
|
|
Loading…
Reference in a new issue