Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 1 | // Copyright (C) 2019 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
Paul Duffin | d19f894 | 2021-07-14 12:08:37 +0100 | [diff] [blame] | 18 | "fmt" |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 19 | "sort" |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 20 | "strings" |
| 21 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 22 | "github.com/google/blueprint" |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
| 24 | ) |
| 25 | |
Paul Duffin | 1938dba | 2022-07-26 23:53:00 +0000 | [diff] [blame] | 26 | // minApiLevelForSdkSnapshot provides access to the min_sdk_version for MinApiLevelForSdkSnapshot |
| 27 | type minApiLevelForSdkSnapshot interface { |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 28 | MinSdkVersion(ctx EarlyModuleContext) ApiLevel |
Paul Duffin | 1938dba | 2022-07-26 23:53:00 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | // MinApiLevelForSdkSnapshot returns the ApiLevel of the min_sdk_version of the supplied module. |
| 32 | // |
| 33 | // If the module does not provide a min_sdk_version then it defaults to 1. |
| 34 | func MinApiLevelForSdkSnapshot(ctx EarlyModuleContext, module Module) ApiLevel { |
| 35 | minApiLevel := NoneApiLevel |
| 36 | if m, ok := module.(minApiLevelForSdkSnapshot); ok { |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 37 | minApiLevel = m.MinSdkVersion(ctx) |
Paul Duffin | 1938dba | 2022-07-26 23:53:00 +0000 | [diff] [blame] | 38 | } |
| 39 | if minApiLevel == NoneApiLevel { |
| 40 | // The default min API level is 1. |
| 41 | minApiLevel = uncheckedFinalApiLevel(1) |
| 42 | } |
| 43 | return minApiLevel |
| 44 | } |
| 45 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 46 | // SnapshotBuilder provides support for generating the build rules which will build the snapshot. |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 47 | type SnapshotBuilder interface { |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 48 | // CopyToSnapshot generates a rule that will copy the src to the dest (which is a snapshot |
| 49 | // relative path) and add the dest to the zip. |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 50 | CopyToSnapshot(src Path, dest string) |
| 51 | |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 52 | // EmptyFile returns the path to an empty file. |
| 53 | // |
| 54 | // This can be used by sdk member types that need to create an empty file in the snapshot, simply |
| 55 | // pass the value returned from this to the CopyToSnapshot() method. |
| 56 | EmptyFile() Path |
| 57 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 58 | // UnzipToSnapshot generates a rule that will unzip the supplied zip into the snapshot relative |
| 59 | // directory destDir. |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 60 | UnzipToSnapshot(zipPath Path, destDir string) |
| 61 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 62 | // AddPrebuiltModule adds a new prebuilt module to the snapshot. |
| 63 | // |
| 64 | // It is intended to be called from SdkMemberType.AddPrebuiltModule which can add module type |
| 65 | // specific properties that are not variant specific. The following properties will be |
| 66 | // automatically populated before returning. |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 67 | // |
| 68 | // * name |
| 69 | // * sdk_member_name |
| 70 | // * prefer |
| 71 | // |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 72 | // Properties that are variant specific will be handled by SdkMemberProperties structure. |
| 73 | // |
| 74 | // Each module created by this method can be output to the generated Android.bp file in two |
| 75 | // different forms, depending on the setting of the SOONG_SDK_SNAPSHOT_VERSION build property. |
| 76 | // The two forms are: |
| 77 | // 1. A versioned Soong module that is referenced from a corresponding similarly versioned |
| 78 | // snapshot module. |
| 79 | // 2. An unversioned Soong module that. |
| 80 | // |
| 81 | // See sdk/update.go for more information. |
Paul Duffin | 9d8d609 | 2019-12-05 18:19:29 +0000 | [diff] [blame] | 82 | AddPrebuiltModule(member SdkMember, moduleType string) BpModule |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 83 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 84 | // SdkMemberReferencePropertyTag returns a property tag to use when adding a property to a |
| 85 | // BpModule that contains references to other sdk members. |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 86 | // |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 87 | // Using this will ensure that the reference is correctly output for both versioned and |
| 88 | // unversioned prebuilts in the snapshot. |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 89 | // |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 90 | // "required: true" means that the property must only contain references to other members of the |
| 91 | // sdk. Passing a reference to a module that is not a member of the sdk will result in a build |
| 92 | // error. |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 93 | // |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 94 | // "required: false" means that the property can contain references to modules that are either |
| 95 | // members or not members of the sdk. If a reference is to a module that is a non member then the |
| 96 | // reference is left unchanged, i.e. it is not transformed as references to members are. |
| 97 | // |
| 98 | // The handling of the member names is dependent on whether it is an internal or exported member. |
| 99 | // An exported member is one whose name is specified in one of the member type specific |
| 100 | // properties. An internal member is one that is added due to being a part of an exported (or |
| 101 | // other internal) member and is not itself an exported member. |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 102 | // |
| 103 | // Member names are handled as follows: |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 104 | // * When creating the unversioned form of the module the name is left unchecked unless the member |
| 105 | // is internal in which case it is transformed into an sdk specific name, i.e. by prefixing with |
| 106 | // the sdk name. |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 107 | // |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 108 | // * When creating the versioned form of the module the name is transformed into a versioned sdk |
| 109 | // specific name, i.e. by prefixing with the sdk name and suffixing with the version. |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 110 | // |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 111 | // e.g. |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 112 | // bpPropertySet.AddPropertyWithTag("libs", []string{"member1", "member2"}, builder.SdkMemberReferencePropertyTag(true)) |
| 113 | SdkMemberReferencePropertyTag(required bool) BpPropertyTag |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 116 | // BpPropertyTag is a marker interface that can be associated with properties in a BpPropertySet to |
| 117 | // provide additional information which can be used to customize their behavior. |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 118 | type BpPropertyTag interface{} |
| 119 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 120 | // BpPropertySet is a set of properties for use in a .bp file. |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 121 | type BpPropertySet interface { |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 122 | // AddProperty adds a property. |
| 123 | // |
| 124 | // The value can be one of the following types: |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 125 | // * string |
| 126 | // * array of the above |
| 127 | // * bool |
Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 128 | // For these types it is an error if multiple properties with the same name |
| 129 | // are added. |
| 130 | // |
| 131 | // * pointer to a struct |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 132 | // * BpPropertySet |
| 133 | // |
Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 134 | // A pointer to a Blueprint-style property struct is first converted into a |
| 135 | // BpPropertySet by traversing the fields and adding their values as |
| 136 | // properties in a BpPropertySet. A field with a struct value is itself |
| 137 | // converted into a BpPropertySet before adding. |
| 138 | // |
| 139 | // Adding a BpPropertySet is done as follows: |
| 140 | // * If no property with the name exists then the BpPropertySet is added |
| 141 | // directly to this property. Care must be taken to ensure that it does not |
| 142 | // introduce a cycle. |
| 143 | // * If a property exists with the name and the current value is a |
| 144 | // BpPropertySet then every property of the new BpPropertySet is added to |
| 145 | // the existing BpPropertySet. |
| 146 | // * Otherwise, if a property exists with the name then it is an error. |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 147 | AddProperty(name string, value interface{}) |
| 148 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 149 | // AddPropertyWithTag adds a property with an associated property tag. |
Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 150 | AddPropertyWithTag(name string, value interface{}, tag BpPropertyTag) |
| 151 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 152 | // AddPropertySet adds a property set with the specified name and returns it so that additional |
| 153 | // properties can be added to it. |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 154 | AddPropertySet(name string) BpPropertySet |
Paul Duffin | 0df4968 | 2021-05-07 01:10:01 +0100 | [diff] [blame] | 155 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 156 | // AddCommentForProperty adds a comment for the named property (or property set). |
Paul Duffin | 0df4968 | 2021-05-07 01:10:01 +0100 | [diff] [blame] | 157 | AddCommentForProperty(name, text string) |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 160 | // BpModule represents a module definition in a .bp file. |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 161 | type BpModule interface { |
| 162 | BpPropertySet |
Paul Duffin | 0df4968 | 2021-05-07 01:10:01 +0100 | [diff] [blame] | 163 | |
| 164 | // ModuleType returns the module type of the module |
| 165 | ModuleType() string |
| 166 | |
| 167 | // Name returns the name of the module or "" if no name has been specified. |
| 168 | Name() string |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 169 | } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 170 | |
Paul Duffin | 51227d8 | 2021-05-18 12:54:27 +0100 | [diff] [blame] | 171 | // BpPrintable is a marker interface that must be implemented by any struct that is added as a |
| 172 | // property value. |
| 173 | type BpPrintable interface { |
| 174 | bpPrintable() |
| 175 | } |
| 176 | |
| 177 | // BpPrintableBase must be embedded within any struct that is added as a |
| 178 | // property value. |
| 179 | type BpPrintableBase struct { |
| 180 | } |
| 181 | |
| 182 | func (b BpPrintableBase) bpPrintable() { |
| 183 | } |
| 184 | |
| 185 | var _ BpPrintable = BpPrintableBase{} |
| 186 | |
Paul Duffin | f04033b | 2021-09-22 11:51:09 +0100 | [diff] [blame] | 187 | // sdkRegisterable defines the interface that must be implemented by objects that can be registered |
| 188 | // in an sdkRegistry. |
| 189 | type sdkRegisterable interface { |
| 190 | // SdkPropertyName returns the name of the corresponding property on an sdk module. |
| 191 | SdkPropertyName() string |
| 192 | } |
| 193 | |
| 194 | // sdkRegistry provides support for registering and retrieving objects that define properties for |
| 195 | // use by sdk and module_exports module types. |
| 196 | type sdkRegistry struct { |
| 197 | // The list of registered objects sorted by property name. |
| 198 | list []sdkRegisterable |
| 199 | } |
| 200 | |
| 201 | // copyAndAppend creates a new sdkRegistry that includes all the traits registered in |
| 202 | // this registry plus the supplied trait. |
| 203 | func (r *sdkRegistry) copyAndAppend(registerable sdkRegisterable) *sdkRegistry { |
| 204 | oldList := r.list |
| 205 | |
Paul Duffin | 581f2e5 | 2021-09-22 13:25:23 +0100 | [diff] [blame] | 206 | // Make sure that list does not already contain the property. Uses a simple linear search instead |
| 207 | // of a binary search even though the list is sorted. That is because the number of items in the |
| 208 | // list is small and so not worth the overhead of a binary search. |
| 209 | found := false |
| 210 | newPropertyName := registerable.SdkPropertyName() |
| 211 | for _, r := range oldList { |
| 212 | if r.SdkPropertyName() == newPropertyName { |
| 213 | found = true |
| 214 | break |
| 215 | } |
| 216 | } |
| 217 | if found { |
| 218 | names := []string{} |
| 219 | for _, r := range oldList { |
| 220 | names = append(names, r.SdkPropertyName()) |
| 221 | } |
| 222 | panic(fmt.Errorf("duplicate properties found, %q already exists in %q", newPropertyName, names)) |
| 223 | } |
| 224 | |
Paul Duffin | f04033b | 2021-09-22 11:51:09 +0100 | [diff] [blame] | 225 | // Copy the slice just in case this is being read while being modified, e.g. when testing. |
| 226 | list := make([]sdkRegisterable, 0, len(oldList)+1) |
| 227 | list = append(list, oldList...) |
| 228 | list = append(list, registerable) |
| 229 | |
| 230 | // Sort the registered objects by their property name to ensure that registry order has no effect |
| 231 | // on behavior. |
| 232 | sort.Slice(list, func(i1, i2 int) bool { |
| 233 | t1 := list[i1] |
| 234 | t2 := list[i2] |
| 235 | |
| 236 | return t1.SdkPropertyName() < t2.SdkPropertyName() |
| 237 | }) |
| 238 | |
| 239 | // Create a new registry so the pointer uniquely identifies the set of registered types. |
| 240 | return &sdkRegistry{ |
| 241 | list: list, |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // registeredObjects returns the list of registered instances. |
| 246 | func (r *sdkRegistry) registeredObjects() []sdkRegisterable { |
| 247 | return r.list |
| 248 | } |
| 249 | |
| 250 | // uniqueOnceKey returns a key that uniquely identifies this instance and can be used with |
| 251 | // OncePer.Once |
| 252 | func (r *sdkRegistry) uniqueOnceKey() OnceKey { |
| 253 | // Use the pointer to the registry as the unique key. The pointer is used because it is guaranteed |
| 254 | // to uniquely identify the contained list. The list itself cannot be used as slices are not |
| 255 | // comparable. Using the pointer does mean that two separate registries with identical lists would |
| 256 | // have different keys and so cause whatever information is cached to be created multiple times. |
| 257 | // However, that is not an issue in practice as it should not occur outside tests. Constructing a |
| 258 | // string representation of the list to use instead would avoid that but is an unnecessary |
| 259 | // complication that provides no significant benefit. |
| 260 | return NewCustomOnceKey(r) |
| 261 | } |
| 262 | |
Paul Duffin | d19f894 | 2021-07-14 12:08:37 +0100 | [diff] [blame] | 263 | // SdkMemberTrait represents a trait that members of an sdk module can contribute to the sdk |
| 264 | // snapshot. |
| 265 | // |
| 266 | // A trait is simply a characteristic of sdk member that is not required by default which may be |
| 267 | // required for some members but not others. Traits can cause additional information to be output |
| 268 | // to the sdk snapshot or replace the default information exported for a member with something else. |
| 269 | // e.g. |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 270 | // - By default cc libraries only export the default image variants to the SDK. However, for some |
| 271 | // members it may be necessary to export specific image variants, e.g. vendor, or recovery. |
| 272 | // - By default cc libraries export all the configured architecture variants except for the native |
| 273 | // bridge architecture variants. However, for some members it may be necessary to export the |
| 274 | // native bridge architecture variants as well. |
| 275 | // - By default cc libraries export the platform variant (i.e. sdk:). However, for some members it |
| 276 | // may be necessary to export the sdk variant (i.e. sdk:sdk). |
Paul Duffin | d19f894 | 2021-07-14 12:08:37 +0100 | [diff] [blame] | 277 | // |
| 278 | // A sdk can request a module to provide no traits, one trait or a collection of traits. The exact |
| 279 | // behavior of a trait is determined by how SdkMemberType implementations handle the traits. A trait |
| 280 | // could be specific to one SdkMemberType or many. Some trait combinations could be incompatible. |
| 281 | // |
| 282 | // The sdk module type will create a special traits structure that contains a property for each |
| 283 | // trait registered with RegisterSdkMemberTrait(). The property names are those returned from |
| 284 | // SdkPropertyName(). Each property contains a list of modules that are required to have that trait. |
| 285 | // e.g. something like this: |
| 286 | // |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 287 | // sdk { |
| 288 | // name: "sdk", |
| 289 | // ... |
| 290 | // traits: { |
| 291 | // recovery_image: ["module1", "module4", "module5"], |
| 292 | // native_bridge: ["module1", "module2"], |
| 293 | // native_sdk: ["module1", "module3"], |
| 294 | // ... |
| 295 | // }, |
| 296 | // ... |
| 297 | // } |
Paul Duffin | d19f894 | 2021-07-14 12:08:37 +0100 | [diff] [blame] | 298 | type SdkMemberTrait interface { |
| 299 | // SdkPropertyName returns the name of the traits property on an sdk module. |
| 300 | SdkPropertyName() string |
| 301 | } |
| 302 | |
Paul Duffin | f04033b | 2021-09-22 11:51:09 +0100 | [diff] [blame] | 303 | var _ sdkRegisterable = (SdkMemberTrait)(nil) |
| 304 | |
Paul Duffin | d19f894 | 2021-07-14 12:08:37 +0100 | [diff] [blame] | 305 | // SdkMemberTraitBase is the base struct that must be embedded within any type that implements |
| 306 | // SdkMemberTrait. |
| 307 | type SdkMemberTraitBase struct { |
| 308 | // PropertyName is the name of the property |
| 309 | PropertyName string |
| 310 | } |
| 311 | |
| 312 | func (b *SdkMemberTraitBase) SdkPropertyName() string { |
| 313 | return b.PropertyName |
| 314 | } |
| 315 | |
| 316 | // SdkMemberTraitSet is a set of SdkMemberTrait instances. |
| 317 | type SdkMemberTraitSet interface { |
| 318 | // Empty returns true if this set is empty. |
| 319 | Empty() bool |
| 320 | |
| 321 | // Contains returns true if this set contains the specified trait. |
| 322 | Contains(trait SdkMemberTrait) bool |
| 323 | |
| 324 | // Subtract returns a new set containing all elements of this set except for those in the |
| 325 | // other set. |
| 326 | Subtract(other SdkMemberTraitSet) SdkMemberTraitSet |
| 327 | |
| 328 | // String returns a string representation of the set and its contents. |
| 329 | String() string |
| 330 | } |
| 331 | |
| 332 | func NewSdkMemberTraitSet(traits []SdkMemberTrait) SdkMemberTraitSet { |
| 333 | if len(traits) == 0 { |
| 334 | return EmptySdkMemberTraitSet() |
| 335 | } |
| 336 | |
| 337 | m := sdkMemberTraitSet{} |
| 338 | for _, trait := range traits { |
| 339 | m[trait] = true |
| 340 | } |
| 341 | return m |
| 342 | } |
| 343 | |
| 344 | func EmptySdkMemberTraitSet() SdkMemberTraitSet { |
| 345 | return (sdkMemberTraitSet)(nil) |
| 346 | } |
| 347 | |
| 348 | type sdkMemberTraitSet map[SdkMemberTrait]bool |
| 349 | |
| 350 | var _ SdkMemberTraitSet = (sdkMemberTraitSet{}) |
| 351 | |
| 352 | func (s sdkMemberTraitSet) Empty() bool { |
| 353 | return len(s) == 0 |
| 354 | } |
| 355 | |
| 356 | func (s sdkMemberTraitSet) Contains(trait SdkMemberTrait) bool { |
| 357 | return s[trait] |
| 358 | } |
| 359 | |
| 360 | func (s sdkMemberTraitSet) Subtract(other SdkMemberTraitSet) SdkMemberTraitSet { |
| 361 | if other.Empty() { |
| 362 | return s |
| 363 | } |
| 364 | |
| 365 | var remainder []SdkMemberTrait |
| 366 | for trait, _ := range s { |
| 367 | if !other.Contains(trait) { |
| 368 | remainder = append(remainder, trait) |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | return NewSdkMemberTraitSet(remainder) |
| 373 | } |
| 374 | |
| 375 | func (s sdkMemberTraitSet) String() string { |
| 376 | list := []string{} |
| 377 | for trait, _ := range s { |
| 378 | list = append(list, trait.SdkPropertyName()) |
| 379 | } |
| 380 | sort.Strings(list) |
| 381 | return fmt.Sprintf("[%s]", strings.Join(list, ",")) |
| 382 | } |
| 383 | |
Paul Duffin | f04033b | 2021-09-22 11:51:09 +0100 | [diff] [blame] | 384 | var registeredSdkMemberTraits = &sdkRegistry{} |
Paul Duffin | 30c830b | 2021-09-22 11:49:47 +0100 | [diff] [blame] | 385 | |
| 386 | // RegisteredSdkMemberTraits returns a OnceKey and a sorted list of registered traits. |
| 387 | // |
| 388 | // The key uniquely identifies the array of traits and can be used with OncePer.Once() to cache |
| 389 | // information derived from the array of traits. |
| 390 | func RegisteredSdkMemberTraits() (OnceKey, []SdkMemberTrait) { |
Paul Duffin | f04033b | 2021-09-22 11:51:09 +0100 | [diff] [blame] | 391 | registerables := registeredSdkMemberTraits.registeredObjects() |
| 392 | traits := make([]SdkMemberTrait, len(registerables)) |
| 393 | for i, registerable := range registerables { |
| 394 | traits[i] = registerable.(SdkMemberTrait) |
| 395 | } |
| 396 | return registeredSdkMemberTraits.uniqueOnceKey(), traits |
Paul Duffin | 30c830b | 2021-09-22 11:49:47 +0100 | [diff] [blame] | 397 | } |
Paul Duffin | d19f894 | 2021-07-14 12:08:37 +0100 | [diff] [blame] | 398 | |
| 399 | // RegisterSdkMemberTrait registers an SdkMemberTrait object to allow them to be used in the |
| 400 | // module_exports, module_exports_snapshot, sdk and sdk_snapshot module types. |
| 401 | func RegisterSdkMemberTrait(trait SdkMemberTrait) { |
Paul Duffin | 30c830b | 2021-09-22 11:49:47 +0100 | [diff] [blame] | 402 | registeredSdkMemberTraits = registeredSdkMemberTraits.copyAndAppend(trait) |
Paul Duffin | d19f894 | 2021-07-14 12:08:37 +0100 | [diff] [blame] | 403 | } |
| 404 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 405 | // SdkMember is an individual member of the SDK. |
| 406 | // |
| 407 | // It includes all of the variants that the SDK depends upon. |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 408 | type SdkMember interface { |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 409 | // Name returns the name of the member. |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 410 | Name() string |
| 411 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 412 | // Variants returns all the variants of this module depended upon by the SDK. |
Paul Duffin | 5e71e68 | 2022-11-23 18:09:54 +0000 | [diff] [blame] | 413 | Variants() []Module |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Paul Duffin | f7b3d0d | 2021-09-02 14:29:21 +0100 | [diff] [blame] | 416 | // SdkMemberDependencyTag is the interface that a tag must implement in order to allow the |
Paul Duffin | 2d3da31 | 2021-05-06 12:02:27 +0100 | [diff] [blame] | 417 | // dependent module to be automatically added to the sdk. |
Paul Duffin | f7b3d0d | 2021-09-02 14:29:21 +0100 | [diff] [blame] | 418 | type SdkMemberDependencyTag interface { |
Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 419 | blueprint.DependencyTag |
| 420 | |
Paul Duffin | a720811 | 2021-04-23 21:20:20 +0100 | [diff] [blame] | 421 | // SdkMemberType returns the SdkMemberType that will be used to automatically add the child module |
| 422 | // to the sdk. |
Paul Duffin | 5cca7c4 | 2021-05-26 10:16:01 +0100 | [diff] [blame] | 423 | // |
| 424 | // Returning nil will prevent the module being added to the sdk. |
Paul Duffin | eee466e | 2021-04-27 23:17:56 +0100 | [diff] [blame] | 425 | SdkMemberType(child Module) SdkMemberType |
Paul Duffin | a720811 | 2021-04-23 21:20:20 +0100 | [diff] [blame] | 426 | |
| 427 | // ExportMember determines whether a module added to the sdk through this tag will be exported |
| 428 | // from the sdk or not. |
| 429 | // |
| 430 | // An exported member is added to the sdk using its own name, e.g. if "foo" was exported from sdk |
| 431 | // "bar" then its prebuilt would be simply called "foo". A member can be added to the sdk via |
| 432 | // multiple tags and if any of those tags returns true from this method then the membe will be |
| 433 | // exported. Every module added directly to the sdk via one of the member type specific |
| 434 | // properties, e.g. java_libs, will automatically be exported. |
| 435 | // |
| 436 | // If a member is not exported then it is treated as an internal implementation detail of the |
| 437 | // sdk and so will be added with an sdk specific name. e.g. if "foo" was an internal member of sdk |
| 438 | // "bar" then its prebuilt would be called "bar_foo". Additionally its visibility will be set to |
| 439 | // "//visibility:private" so it will not be accessible from outside its Android.bp file. |
| 440 | ExportMember() bool |
Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Paul Duffin | f7b3d0d | 2021-09-02 14:29:21 +0100 | [diff] [blame] | 443 | var _ SdkMemberDependencyTag = (*sdkMemberDependencyTag)(nil) |
| 444 | var _ ReplaceSourceWithPrebuilt = (*sdkMemberDependencyTag)(nil) |
Paul Duffin | cee7e66 | 2020-07-09 17:32:57 +0100 | [diff] [blame] | 445 | |
Paul Duffin | f7b3d0d | 2021-09-02 14:29:21 +0100 | [diff] [blame] | 446 | type sdkMemberDependencyTag struct { |
Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 447 | blueprint.BaseDependencyTag |
| 448 | memberType SdkMemberType |
Paul Duffin | a720811 | 2021-04-23 21:20:20 +0100 | [diff] [blame] | 449 | export bool |
Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Paul Duffin | f7b3d0d | 2021-09-02 14:29:21 +0100 | [diff] [blame] | 452 | func (t *sdkMemberDependencyTag) SdkMemberType(_ Module) SdkMemberType { |
Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 453 | return t.memberType |
| 454 | } |
| 455 | |
Paul Duffin | f7b3d0d | 2021-09-02 14:29:21 +0100 | [diff] [blame] | 456 | func (t *sdkMemberDependencyTag) ExportMember() bool { |
Paul Duffin | a720811 | 2021-04-23 21:20:20 +0100 | [diff] [blame] | 457 | return t.export |
| 458 | } |
| 459 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 460 | // ReplaceSourceWithPrebuilt prevents dependencies from the sdk/module_exports onto their members |
| 461 | // from being replaced with a preferred prebuilt. |
Paul Duffin | f7b3d0d | 2021-09-02 14:29:21 +0100 | [diff] [blame] | 462 | func (t *sdkMemberDependencyTag) ReplaceSourceWithPrebuilt() bool { |
Paul Duffin | cee7e66 | 2020-07-09 17:32:57 +0100 | [diff] [blame] | 463 | return false |
| 464 | } |
| 465 | |
Paul Duffin | f7b3d0d | 2021-09-02 14:29:21 +0100 | [diff] [blame] | 466 | // DependencyTagForSdkMemberType creates an SdkMemberDependencyTag that will cause any |
Paul Duffin | a720811 | 2021-04-23 21:20:20 +0100 | [diff] [blame] | 467 | // dependencies added by the tag to be added to the sdk as the specified SdkMemberType and exported |
| 468 | // (or not) as specified by the export parameter. |
Paul Duffin | f7b3d0d | 2021-09-02 14:29:21 +0100 | [diff] [blame] | 469 | func DependencyTagForSdkMemberType(memberType SdkMemberType, export bool) SdkMemberDependencyTag { |
| 470 | return &sdkMemberDependencyTag{memberType: memberType, export: export} |
Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 473 | // SdkMemberType is the interface that must be implemented for every type that can be a member of an |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 474 | // sdk. |
| 475 | // |
| 476 | // The basic implementation should look something like this, where ModuleType is |
| 477 | // the name of the module type being supported. |
| 478 | // |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 479 | // type moduleTypeSdkMemberType struct { |
| 480 | // android.SdkMemberTypeBase |
| 481 | // } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 482 | // |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 483 | // func init() { |
| 484 | // android.RegisterSdkMemberType(&moduleTypeSdkMemberType{ |
| 485 | // SdkMemberTypeBase: android.SdkMemberTypeBase{ |
| 486 | // PropertyName: "module_types", |
| 487 | // }, |
| 488 | // } |
| 489 | // } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 490 | // |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 491 | // ...methods... |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 492 | type SdkMemberType interface { |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 493 | // SdkPropertyName returns the name of the member type property on an sdk module. |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 494 | SdkPropertyName() string |
| 495 | |
Paul Duffin | 1308205 | 2021-05-11 00:31:38 +0100 | [diff] [blame] | 496 | // RequiresBpProperty returns true if this member type requires its property to be usable within |
| 497 | // an Android.bp file. |
| 498 | RequiresBpProperty() bool |
| 499 | |
Paul Duffin | f861df7 | 2022-07-01 15:56:06 +0000 | [diff] [blame] | 500 | // SupportedBuildReleases returns the string representation of a set of target build releases that |
| 501 | // support this member type. |
| 502 | SupportedBuildReleases() string |
| 503 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 504 | // UsableWithSdkAndSdkSnapshot returns true if the member type supports the sdk/sdk_snapshot, |
| 505 | // false otherwise. |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 506 | UsableWithSdkAndSdkSnapshot() bool |
| 507 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 508 | // IsHostOsDependent returns true if prebuilt host artifacts may be specific to the host OS. Only |
| 509 | // applicable to modules where HostSupported() is true. If this is true, snapshots will list each |
| 510 | // host OS variant explicitly and disable all other host OS'es. |
Martin Stjernholm | caa47d7 | 2020-07-11 04:52:24 +0100 | [diff] [blame] | 511 | IsHostOsDependent() bool |
| 512 | |
Liz Kammer | 96320df | 2022-05-12 20:40:00 -0400 | [diff] [blame] | 513 | // SupportedLinkages returns the names of the linkage variants supported by this module. |
| 514 | SupportedLinkages() []string |
| 515 | |
Paul Duffin | 4e7d1c4 | 2022-05-13 13:12:19 +0000 | [diff] [blame] | 516 | // ArePrebuiltsRequired returns true if prebuilts are required in the sdk snapshot, false |
| 517 | // otherwise. |
| 518 | ArePrebuiltsRequired() bool |
| 519 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 520 | // AddDependencies adds dependencies from the SDK module to all the module variants the member |
| 521 | // type contributes to the SDK. `names` is the list of module names given in the member type |
| 522 | // property (as returned by SdkPropertyName()) in the SDK module. The exact set of variants |
| 523 | // required is determined by the SDK and its properties. The dependencies must be added with the |
| 524 | // supplied tag. |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 525 | // |
| 526 | // The BottomUpMutatorContext provided is for the SDK module. |
Paul Duffin | 296701e | 2021-07-14 10:29:36 +0100 | [diff] [blame] | 527 | AddDependencies(ctx SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 528 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 529 | // IsInstance returns true if the supplied module is an instance of this member type. |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 530 | // |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 531 | // This is used to check the type of each variant before added to the SdkMember. Returning false |
| 532 | // will cause an error to be logged explaining that the module is not allowed in whichever sdk |
| 533 | // property it was added. |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 534 | IsInstance(module Module) bool |
| 535 | |
Paul Duffin | 0d4ed0a | 2021-05-10 23:58:40 +0100 | [diff] [blame] | 536 | // UsesSourceModuleTypeInSnapshot returns true when the AddPrebuiltModule() method returns a |
| 537 | // source module type. |
| 538 | UsesSourceModuleTypeInSnapshot() bool |
| 539 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 540 | // AddPrebuiltModule is called to add a prebuilt module that the sdk will populate. |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 541 | // |
Paul Duffin | 425b0ea | 2020-05-06 12:41:39 +0100 | [diff] [blame] | 542 | // The sdk module code generates the snapshot as follows: |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 543 | // |
| 544 | // * A properties struct of type SdkMemberProperties is created for each variant and |
Paul Duffin | 5e71e68 | 2022-11-23 18:09:54 +0000 | [diff] [blame] | 545 | // populated with information from the variant by calling PopulateFromVariant(Module) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 546 | // on the struct. |
| 547 | // |
| 548 | // * An additional properties struct is created into which the common properties will be |
| 549 | // added. |
| 550 | // |
| 551 | // * The variant property structs are analysed to find exported (capitalized) fields which |
| 552 | // have common values. Those fields are cleared and the common value added to the common |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 553 | // properties. |
| 554 | // |
Paul Duffin | 02e25c8 | 2022-09-22 15:30:58 +0100 | [diff] [blame] | 555 | // A field annotated with a tag of `sdk:"ignore"` will be treated as if it |
| 556 | // was not capitalized, i.e. ignored and not optimized for common values. |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 557 | // |
Paul Duffin | bfdca96 | 2022-09-22 16:21:54 +0100 | [diff] [blame] | 558 | // A field annotated with a tag of `sdk:"keep"` will not be cleared even if the value is common |
| 559 | // across multiple structs. Common values will still be copied into the common property struct. |
| 560 | // So, if the same value is placed in all structs populated from variants that value would be |
| 561 | // copied into all common property structs and so be available in every instance. |
| 562 | // |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 563 | // A field annotated with a tag of `android:"arch_variant"` will be allowed to have |
| 564 | // values that differ by arch, fields not tagged as such must have common values across |
| 565 | // all variants. |
| 566 | // |
Paul Duffin | c459f89 | 2020-04-30 18:08:29 +0100 | [diff] [blame] | 567 | // * Additional field tags can be specified on a field that will ignore certain values |
| 568 | // for the purpose of common value optimization. A value that is ignored must have the |
| 569 | // default value for the property type. This is to ensure that significant value are not |
| 570 | // ignored by accident. The purpose of this is to allow the snapshot generation to reflect |
| 571 | // the behavior of the runtime. e.g. if a property is ignored on the host then a property |
| 572 | // that is common for android can be treated as if it was common for android and host as |
| 573 | // the setting for host is ignored anyway. |
| 574 | // * `sdk:"ignored-on-host" - this indicates the property is ignored on the host variant. |
| 575 | // |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 576 | // * The sdk module type populates the BpModule structure, creating the arch specific |
| 577 | // structure and calls AddToPropertySet(...) on the properties struct to add the member |
| 578 | // specific properties in the correct place in the structure. |
| 579 | // |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 580 | AddPrebuiltModule(ctx SdkMemberContext, member SdkMember) BpModule |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 581 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 582 | // CreateVariantPropertiesStruct creates a structure into which variant specific properties can be |
| 583 | // added. |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 584 | CreateVariantPropertiesStruct() SdkMemberProperties |
Paul Duffin | d19f894 | 2021-07-14 12:08:37 +0100 | [diff] [blame] | 585 | |
| 586 | // SupportedTraits returns the set of traits supported by this member type. |
| 587 | SupportedTraits() SdkMemberTraitSet |
Liz Kammer | 96320df | 2022-05-12 20:40:00 -0400 | [diff] [blame] | 588 | |
| 589 | // Overrides returns whether type overrides other SdkMemberType |
| 590 | Overrides(SdkMemberType) bool |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 591 | } |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 592 | |
Paul Duffin | f04033b | 2021-09-22 11:51:09 +0100 | [diff] [blame] | 593 | var _ sdkRegisterable = (SdkMemberType)(nil) |
| 594 | |
Paul Duffin | 296701e | 2021-07-14 10:29:36 +0100 | [diff] [blame] | 595 | // SdkDependencyContext provides access to information needed by the SdkMemberType.AddDependencies() |
| 596 | // implementations. |
| 597 | type SdkDependencyContext interface { |
| 598 | BottomUpMutatorContext |
Paul Duffin | d19f894 | 2021-07-14 12:08:37 +0100 | [diff] [blame] | 599 | |
| 600 | // RequiredTraits returns the set of SdkMemberTrait instances that the sdk requires the named |
| 601 | // member to provide. |
| 602 | RequiredTraits(name string) SdkMemberTraitSet |
| 603 | |
| 604 | // RequiresTrait returns true if the sdk requires the member with the supplied name to provide the |
| 605 | // supplied trait. |
| 606 | RequiresTrait(name string, trait SdkMemberTrait) bool |
Paul Duffin | 296701e | 2021-07-14 10:29:36 +0100 | [diff] [blame] | 607 | } |
| 608 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 609 | // SdkMemberTypeBase is the base type for SdkMemberType implementations and must be embedded in any |
| 610 | // struct that implements SdkMemberType. |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 611 | type SdkMemberTypeBase struct { |
Paul Duffin | 1308205 | 2021-05-11 00:31:38 +0100 | [diff] [blame] | 612 | PropertyName string |
| 613 | |
Liz Kammer | 96320df | 2022-05-12 20:40:00 -0400 | [diff] [blame] | 614 | // Property names that this SdkMemberTypeBase can override, this is useful when a module type is a |
| 615 | // superset of another module type. |
| 616 | OverridesPropertyNames map[string]bool |
| 617 | |
| 618 | // The names of linkage variants supported by this module. |
| 619 | SupportedLinkageNames []string |
| 620 | |
Paul Duffin | 1308205 | 2021-05-11 00:31:38 +0100 | [diff] [blame] | 621 | // When set to true BpPropertyNotRequired indicates that the member type does not require the |
| 622 | // property to be specifiable in an Android.bp file. |
| 623 | BpPropertyNotRequired bool |
| 624 | |
Paul Duffin | f861df7 | 2022-07-01 15:56:06 +0000 | [diff] [blame] | 625 | // The name of the first targeted build release. |
| 626 | // |
| 627 | // If not specified then it is assumed to be available on all targeted build releases. |
| 628 | SupportedBuildReleaseSpecification string |
| 629 | |
Paul Duffin | 4e7d1c4 | 2022-05-13 13:12:19 +0000 | [diff] [blame] | 630 | // Set to true if this must be usable with the sdk/sdk_snapshot module types. Otherwise, it will |
| 631 | // only be usable with module_exports/module_exports_snapshots module types. |
| 632 | SupportsSdk bool |
| 633 | |
| 634 | // Set to true if prebuilt host artifacts of this member may be specific to the host OS. Only |
| 635 | // applicable to modules where HostSupported() is true. |
Paul Duffin | 2d3da31 | 2021-05-06 12:02:27 +0100 | [diff] [blame] | 636 | HostOsDependent bool |
Paul Duffin | 0d4ed0a | 2021-05-10 23:58:40 +0100 | [diff] [blame] | 637 | |
| 638 | // When set to true UseSourceModuleTypeInSnapshot indicates that the member type creates a source |
| 639 | // module type in its SdkMemberType.AddPrebuiltModule() method. That prevents the sdk snapshot |
| 640 | // code from automatically adding a prefer: true flag. |
| 641 | UseSourceModuleTypeInSnapshot bool |
Paul Duffin | d19f894 | 2021-07-14 12:08:37 +0100 | [diff] [blame] | 642 | |
Paul Duffin | 4e7d1c4 | 2022-05-13 13:12:19 +0000 | [diff] [blame] | 643 | // Set to proptools.BoolPtr(false) if this member does not generate prebuilts but is only provided |
| 644 | // to allow the sdk to gather members from this member's dependencies. If not specified then |
| 645 | // defaults to true. |
| 646 | PrebuiltsRequired *bool |
| 647 | |
Paul Duffin | d19f894 | 2021-07-14 12:08:37 +0100 | [diff] [blame] | 648 | // The list of supported traits. |
| 649 | Traits []SdkMemberTrait |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | func (b *SdkMemberTypeBase) SdkPropertyName() string { |
| 653 | return b.PropertyName |
| 654 | } |
| 655 | |
Paul Duffin | 1308205 | 2021-05-11 00:31:38 +0100 | [diff] [blame] | 656 | func (b *SdkMemberTypeBase) RequiresBpProperty() bool { |
| 657 | return !b.BpPropertyNotRequired |
| 658 | } |
| 659 | |
Paul Duffin | f861df7 | 2022-07-01 15:56:06 +0000 | [diff] [blame] | 660 | func (b *SdkMemberTypeBase) SupportedBuildReleases() string { |
| 661 | return b.SupportedBuildReleaseSpecification |
| 662 | } |
| 663 | |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 664 | func (b *SdkMemberTypeBase) UsableWithSdkAndSdkSnapshot() bool { |
| 665 | return b.SupportsSdk |
| 666 | } |
| 667 | |
Martin Stjernholm | caa47d7 | 2020-07-11 04:52:24 +0100 | [diff] [blame] | 668 | func (b *SdkMemberTypeBase) IsHostOsDependent() bool { |
| 669 | return b.HostOsDependent |
| 670 | } |
| 671 | |
Paul Duffin | 4e7d1c4 | 2022-05-13 13:12:19 +0000 | [diff] [blame] | 672 | func (b *SdkMemberTypeBase) ArePrebuiltsRequired() bool { |
| 673 | return proptools.BoolDefault(b.PrebuiltsRequired, true) |
| 674 | } |
| 675 | |
Paul Duffin | 0d4ed0a | 2021-05-10 23:58:40 +0100 | [diff] [blame] | 676 | func (b *SdkMemberTypeBase) UsesSourceModuleTypeInSnapshot() bool { |
| 677 | return b.UseSourceModuleTypeInSnapshot |
| 678 | } |
| 679 | |
Paul Duffin | d19f894 | 2021-07-14 12:08:37 +0100 | [diff] [blame] | 680 | func (b *SdkMemberTypeBase) SupportedTraits() SdkMemberTraitSet { |
| 681 | return NewSdkMemberTraitSet(b.Traits) |
| 682 | } |
| 683 | |
Liz Kammer | 96320df | 2022-05-12 20:40:00 -0400 | [diff] [blame] | 684 | func (b *SdkMemberTypeBase) Overrides(other SdkMemberType) bool { |
| 685 | return b.OverridesPropertyNames[other.SdkPropertyName()] |
| 686 | } |
| 687 | |
| 688 | func (b *SdkMemberTypeBase) SupportedLinkages() []string { |
| 689 | return b.SupportedLinkageNames |
| 690 | } |
| 691 | |
Paul Duffin | 30c830b | 2021-09-22 11:49:47 +0100 | [diff] [blame] | 692 | // registeredModuleExportsMemberTypes is the set of registered SdkMemberTypes for module_exports |
| 693 | // modules. |
Paul Duffin | f04033b | 2021-09-22 11:51:09 +0100 | [diff] [blame] | 694 | var registeredModuleExportsMemberTypes = &sdkRegistry{} |
Paul Duffin | 62782de | 2021-07-14 12:05:16 +0100 | [diff] [blame] | 695 | |
Paul Duffin | f04033b | 2021-09-22 11:51:09 +0100 | [diff] [blame] | 696 | // registeredSdkMemberTypes is the set of registered registeredSdkMemberTypes for sdk modules. |
| 697 | var registeredSdkMemberTypes = &sdkRegistry{} |
Paul Duffin | 30c830b | 2021-09-22 11:49:47 +0100 | [diff] [blame] | 698 | |
| 699 | // RegisteredSdkMemberTypes returns a OnceKey and a sorted list of registered types. |
| 700 | // |
| 701 | // If moduleExports is true then the slice of types includes all registered types that can be used |
| 702 | // with the module_exports and module_exports_snapshot module types. Otherwise, the slice of types |
| 703 | // only includes those registered types that can be used with the sdk and sdk_snapshot module |
| 704 | // types. |
| 705 | // |
| 706 | // The key uniquely identifies the array of types and can be used with OncePer.Once() to cache |
| 707 | // information derived from the array of types. |
| 708 | func RegisteredSdkMemberTypes(moduleExports bool) (OnceKey, []SdkMemberType) { |
Paul Duffin | f04033b | 2021-09-22 11:51:09 +0100 | [diff] [blame] | 709 | var registry *sdkRegistry |
Paul Duffin | 30c830b | 2021-09-22 11:49:47 +0100 | [diff] [blame] | 710 | if moduleExports { |
| 711 | registry = registeredModuleExportsMemberTypes |
| 712 | } else { |
| 713 | registry = registeredSdkMemberTypes |
| 714 | } |
| 715 | |
Paul Duffin | f04033b | 2021-09-22 11:51:09 +0100 | [diff] [blame] | 716 | registerables := registry.registeredObjects() |
| 717 | types := make([]SdkMemberType, len(registerables)) |
| 718 | for i, registerable := range registerables { |
| 719 | types[i] = registerable.(SdkMemberType) |
| 720 | } |
| 721 | return registry.uniqueOnceKey(), types |
Paul Duffin | 30c830b | 2021-09-22 11:49:47 +0100 | [diff] [blame] | 722 | } |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 723 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 724 | // RegisterSdkMemberType registers an SdkMemberType object to allow them to be used in the |
| 725 | // module_exports, module_exports_snapshot and (depending on the value returned from |
| 726 | // SdkMemberType.UsableWithSdkAndSdkSnapshot) the sdk and sdk_snapshot module types. |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 727 | func RegisterSdkMemberType(memberType SdkMemberType) { |
| 728 | // All member types are usable with module_exports. |
Paul Duffin | 30c830b | 2021-09-22 11:49:47 +0100 | [diff] [blame] | 729 | registeredModuleExportsMemberTypes = registeredModuleExportsMemberTypes.copyAndAppend(memberType) |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 730 | |
| 731 | // Only those that explicitly indicate it are usable with sdk. |
| 732 | if memberType.UsableWithSdkAndSdkSnapshot() { |
Paul Duffin | 30c830b | 2021-09-22 11:49:47 +0100 | [diff] [blame] | 733 | registeredSdkMemberTypes = registeredSdkMemberTypes.copyAndAppend(memberType) |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 736 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 737 | // SdkMemberPropertiesBase is the base structure for all implementations of SdkMemberProperties and |
| 738 | // must be embedded in any struct that implements SdkMemberProperties. |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 739 | // |
Martin Stjernholm | 89238f4 | 2020-07-10 00:14:03 +0100 | [diff] [blame] | 740 | // Contains common properties that apply across many different member types. |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 741 | type SdkMemberPropertiesBase struct { |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 742 | // The number of unique os types supported by the member variants. |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 743 | // |
| 744 | // If a member has a variant with more than one os type then it will need to differentiate |
| 745 | // the locations of any of their prebuilt files in the snapshot by os type to prevent them |
| 746 | // from colliding. See OsPrefix(). |
| 747 | // |
Paul Duffin | 02e25c8 | 2022-09-22 15:30:58 +0100 | [diff] [blame] | 748 | // Ignore this property during optimization. This is needed because this property is the same for |
| 749 | // all variants of a member and so would be optimized away if it was not ignored. |
| 750 | Os_count int `sdk:"ignore"` |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 751 | |
| 752 | // The os type for which these properties refer. |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 753 | // |
| 754 | // Provided to allow a member to differentiate between os types in the locations of their |
| 755 | // prebuilt files when it supports more than one os type. |
| 756 | // |
Paul Duffin | 02e25c8 | 2022-09-22 15:30:58 +0100 | [diff] [blame] | 757 | // Ignore this property during optimization. This is needed because this property is the same for |
| 758 | // all variants of a member and so would be optimized away if it was not ignored. |
| 759 | Os OsType `sdk:"ignore"` |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 760 | |
| 761 | // The setting to use for the compile_multilib property. |
Martin Stjernholm | 89238f4 | 2020-07-10 00:14:03 +0100 | [diff] [blame] | 762 | Compile_multilib string `android:"arch_variant"` |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 765 | // OsPrefix returns the os prefix to use for any file paths in the sdk. |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 766 | // |
| 767 | // Is an empty string if the member only provides variants for a single os type, otherwise |
| 768 | // is the OsType.Name. |
| 769 | func (b *SdkMemberPropertiesBase) OsPrefix() string { |
| 770 | if b.Os_count == 1 { |
| 771 | return "" |
| 772 | } else { |
| 773 | return b.Os.Name |
| 774 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | func (b *SdkMemberPropertiesBase) Base() *SdkMemberPropertiesBase { |
| 778 | return b |
| 779 | } |
| 780 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 781 | // SdkMemberProperties is the interface to be implemented on top of a structure that contains |
| 782 | // variant specific information. |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 783 | // |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 784 | // Struct fields that are capitalized are examined for common values to extract. Fields that are not |
| 785 | // capitalized are assumed to be arch specific. |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 786 | type SdkMemberProperties interface { |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 787 | // Base returns the base structure. |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 788 | Base() *SdkMemberPropertiesBase |
| 789 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 790 | // PopulateFromVariant populates this structure with information from a module variant. |
| 791 | // |
| 792 | // It will typically be called once for each variant of a member module that the SDK depends upon. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 793 | PopulateFromVariant(ctx SdkMemberContext, variant Module) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 794 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 795 | // AddToPropertySet adds the information from this structure to the property set. |
| 796 | // |
| 797 | // This will be called for each instance of this structure on which the PopulateFromVariant method |
| 798 | // was called and also on a number of different instances of this structure into which properties |
| 799 | // common to one or more variants have been copied. Therefore, implementations of this must handle |
| 800 | // the case when this structure is only partially populated. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 801 | AddToPropertySet(ctx SdkMemberContext, propertySet BpPropertySet) |
| 802 | } |
| 803 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 804 | // SdkMemberContext provides access to information common to a specific member. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 805 | type SdkMemberContext interface { |
| 806 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 807 | // SdkModuleContext returns the module context of the sdk common os variant which is creating the |
| 808 | // snapshot. |
| 809 | // |
| 810 | // This is common to all members of the sdk and is not specific to the member being processed. |
| 811 | // If information about the member being processed needs to be obtained from this ModuleContext it |
| 812 | // must be obtained using one of the OtherModule... methods not the Module... methods. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 813 | SdkModuleContext() ModuleContext |
| 814 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 815 | // SnapshotBuilder the builder of the snapshot. |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 816 | SnapshotBuilder() SnapshotBuilder |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 817 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 818 | // MemberType returns the type of the member currently being processed. |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 819 | MemberType() SdkMemberType |
| 820 | |
Paul Duffin | 9428970 | 2021-09-09 15:38:32 +0100 | [diff] [blame] | 821 | // Name returns the name of the member currently being processed. |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 822 | // |
| 823 | // Provided for use by sdk members to create a member specific location within the snapshot |
| 824 | // into which to copy the prebuilt files. |
| 825 | Name() string |
Paul Duffin | d19f894 | 2021-07-14 12:08:37 +0100 | [diff] [blame] | 826 | |
| 827 | // RequiresTrait returns true if this member is expected to provide the specified trait. |
| 828 | RequiresTrait(trait SdkMemberTrait) bool |
Paul Duffin | 1364891 | 2022-07-15 13:12:35 +0000 | [diff] [blame] | 829 | |
| 830 | // IsTargetBuildBeforeTiramisu return true if the target build release for which this snapshot is |
| 831 | // being generated is before Tiramisu, i.e. S. |
| 832 | IsTargetBuildBeforeTiramisu() bool |
Cole Faust | 3486740 | 2023-04-28 12:32:27 -0700 | [diff] [blame] | 833 | |
| 834 | // ModuleErrorf reports an error at the line number of the module type in the module definition. |
| 835 | ModuleErrorf(fmt string, args ...interface{}) |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 836 | } |
Paul Duffin | b97b157 | 2021-04-29 21:50:40 +0100 | [diff] [blame] | 837 | |
| 838 | // ExportedComponentsInfo contains information about the components that this module exports to an |
| 839 | // sdk snapshot. |
| 840 | // |
| 841 | // A component of a module is a child module that the module creates and which forms an integral |
| 842 | // part of the functionality that the creating module provides. A component module is essentially |
| 843 | // owned by its creator and is tightly coupled to the creator and other components. |
| 844 | // |
| 845 | // e.g. the child modules created by prebuilt_apis are not components because they are not tightly |
| 846 | // coupled to the prebuilt_apis module. Once they are created the prebuilt_apis ignores them. The |
| 847 | // child impl and stub library created by java_sdk_library (and corresponding import) are components |
| 848 | // because the creating module depends upon them in order to provide some of its own functionality. |
| 849 | // |
| 850 | // A component is exported if it is part of an sdk snapshot. e.g. The xml and impl child modules are |
| 851 | // components but they are not exported as they are not part of an sdk snapshot. |
| 852 | // |
| 853 | // This information is used by the sdk snapshot generation code to ensure that it does not create |
| 854 | // an sdk snapshot that contains a declaration of the component module and the module that creates |
| 855 | // it as that would result in duplicate modules when attempting to use the snapshot. e.g. a snapshot |
| 856 | // that included the java_sdk_library_import "foo" and also a java_import "foo.stubs" would fail |
| 857 | // as there would be two modules called "foo.stubs". |
| 858 | type ExportedComponentsInfo struct { |
| 859 | // The names of the exported components. |
| 860 | Components []string |
| 861 | } |
| 862 | |
| 863 | var ExportedComponentsInfoProvider = blueprint.NewProvider(ExportedComponentsInfo{}) |
Paul Duffin | 958806b | 2022-05-16 13:10:47 +0000 | [diff] [blame] | 864 | |
| 865 | // AdditionalSdkInfo contains additional properties to add to the generated SDK info file. |
| 866 | type AdditionalSdkInfo struct { |
| 867 | Properties map[string]interface{} |
| 868 | } |
| 869 | |
| 870 | var AdditionalSdkInfoProvider = blueprint.NewProvider(AdditionalSdkInfo{}) |