| 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 | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 18 | "sort" | 
| Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 19 | "strings" | 
|  | 20 |  | 
| Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 21 | "github.com/google/blueprint" | 
| Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" | 
|  | 23 | ) | 
|  | 24 |  | 
| Paul Duffin | 923e8a5 | 2020-03-30 15:33:32 +0100 | [diff] [blame] | 25 | // Extracted from SdkAware to make it easier to define custom subsets of the | 
|  | 26 | // SdkAware interface and improve code navigation within the IDE. | 
|  | 27 | // | 
|  | 28 | // In addition to its use in SdkAware this interface must also be implemented by | 
|  | 29 | // APEX to specify the SDKs required by that module and its contents. e.g. APEX | 
|  | 30 | // is expected to implement RequiredSdks() by reading its own properties like | 
|  | 31 | // `uses_sdks`. | 
|  | 32 | type RequiredSdks interface { | 
|  | 33 | // The set of SDKs required by an APEX and its contents. | 
|  | 34 | RequiredSdks() SdkRefs | 
|  | 35 | } | 
|  | 36 |  | 
| Paul Duffin | 50f0da4 | 2020-07-22 13:52:01 +0100 | [diff] [blame] | 37 | // Provided to improve code navigation with the IDE. | 
|  | 38 | type sdkAwareWithoutModule interface { | 
| Paul Duffin | 923e8a5 | 2020-03-30 15:33:32 +0100 | [diff] [blame] | 39 | RequiredSdks | 
|  | 40 |  | 
| Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 41 | sdkBase() *SdkBase | 
|  | 42 | MakeMemberOf(sdk SdkRef) | 
|  | 43 | IsInAnySdk() bool | 
|  | 44 | ContainingSdk() SdkRef | 
|  | 45 | MemberName() string | 
|  | 46 | BuildWithSdks(sdks SdkRefs) | 
| Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 47 | } | 
|  | 48 |  | 
| Paul Duffin | 50f0da4 | 2020-07-22 13:52:01 +0100 | [diff] [blame] | 49 | // SdkAware is the interface that must be supported by any module to become a member of SDK or to be | 
|  | 50 | // built with SDK | 
|  | 51 | type SdkAware interface { | 
|  | 52 | Module | 
|  | 53 | sdkAwareWithoutModule | 
|  | 54 | } | 
|  | 55 |  | 
| Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 56 | // SdkRef refers to a version of an SDK | 
|  | 57 | type SdkRef struct { | 
|  | 58 | Name    string | 
|  | 59 | Version string | 
|  | 60 | } | 
|  | 61 |  | 
| Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 62 | // Unversioned determines if the SdkRef is referencing to the unversioned SDK module | 
|  | 63 | func (s SdkRef) Unversioned() bool { | 
|  | 64 | return s.Version == "" | 
| Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 65 | } | 
|  | 66 |  | 
| Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 67 | // String returns string representation of this SdkRef for debugging purpose | 
|  | 68 | func (s SdkRef) String() string { | 
|  | 69 | if s.Name == "" { | 
|  | 70 | return "(No Sdk)" | 
|  | 71 | } | 
|  | 72 | if s.Unversioned() { | 
|  | 73 | return s.Name | 
|  | 74 | } | 
|  | 75 | return s.Name + string(SdkVersionSeparator) + s.Version | 
|  | 76 | } | 
|  | 77 |  | 
| Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 78 | // SdkVersionSeparator is a character used to separate an sdk name and its version | 
|  | 79 | const SdkVersionSeparator = '@' | 
| Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 80 |  | 
| Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 81 | // ParseSdkRef parses a `name@version` style string into a corresponding SdkRef struct | 
| Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 82 | func ParseSdkRef(ctx BaseModuleContext, str string, property string) SdkRef { | 
| Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 83 | tokens := strings.Split(str, string(SdkVersionSeparator)) | 
| Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 84 | if len(tokens) < 1 || len(tokens) > 2 { | 
|  | 85 | ctx.PropertyErrorf(property, "%q does not follow name#version syntax", str) | 
|  | 86 | return SdkRef{Name: "invalid sdk name", Version: "invalid sdk version"} | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | name := tokens[0] | 
|  | 90 |  | 
| Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 91 | var version string | 
| Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 92 | if len(tokens) == 2 { | 
|  | 93 | version = tokens[1] | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | return SdkRef{Name: name, Version: version} | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | type SdkRefs []SdkRef | 
|  | 100 |  | 
| Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 101 | // Contains tells if the given SdkRef is in this list of SdkRef's | 
| Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 102 | func (refs SdkRefs) Contains(s SdkRef) bool { | 
|  | 103 | for _, r := range refs { | 
|  | 104 | if r == s { | 
|  | 105 | return true | 
|  | 106 | } | 
|  | 107 | } | 
|  | 108 | return false | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | type sdkProperties struct { | 
|  | 112 | // The SDK that this module is a member of. nil if it is not a member of any SDK | 
|  | 113 | ContainingSdk *SdkRef `blueprint:"mutated"` | 
|  | 114 |  | 
|  | 115 | // The list of SDK names and versions that are used to build this module | 
|  | 116 | RequiredSdks SdkRefs `blueprint:"mutated"` | 
|  | 117 |  | 
|  | 118 | // Name of the module that this sdk member is representing | 
|  | 119 | Sdk_member_name *string | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | // SdkBase is a struct that is expected to be included in module types to implement the SdkAware | 
|  | 123 | // interface. InitSdkAwareModule should be called to initialize this struct. | 
|  | 124 | type SdkBase struct { | 
|  | 125 | properties sdkProperties | 
| Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 126 | module     SdkAware | 
| Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 127 | } | 
|  | 128 |  | 
|  | 129 | func (s *SdkBase) sdkBase() *SdkBase { | 
|  | 130 | return s | 
|  | 131 | } | 
|  | 132 |  | 
| Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 133 | // MakeMemberOf sets this module to be a member of a specific SDK | 
| Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 134 | func (s *SdkBase) MakeMemberOf(sdk SdkRef) { | 
|  | 135 | s.properties.ContainingSdk = &sdk | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | // IsInAnySdk returns true if this module is a member of any SDK | 
|  | 139 | func (s *SdkBase) IsInAnySdk() bool { | 
|  | 140 | return s.properties.ContainingSdk != nil | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | // ContainingSdk returns the SDK that this module is a member of | 
|  | 144 | func (s *SdkBase) ContainingSdk() SdkRef { | 
|  | 145 | if s.properties.ContainingSdk != nil { | 
|  | 146 | return *s.properties.ContainingSdk | 
|  | 147 | } | 
| Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 148 | return SdkRef{Name: "", Version: ""} | 
| Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 149 | } | 
|  | 150 |  | 
| Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 151 | // MemberName returns the name of the module that this SDK member is overriding | 
| Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 152 | func (s *SdkBase) MemberName() string { | 
|  | 153 | return proptools.String(s.properties.Sdk_member_name) | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | // BuildWithSdks is used to mark that this module has to be built with the given SDK(s). | 
|  | 157 | func (s *SdkBase) BuildWithSdks(sdks SdkRefs) { | 
|  | 158 | s.properties.RequiredSdks = sdks | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | // RequiredSdks returns the SDK(s) that this module has to be built with | 
|  | 162 | func (s *SdkBase) RequiredSdks() SdkRefs { | 
|  | 163 | return s.properties.RequiredSdks | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | // InitSdkAwareModule initializes the SdkBase struct. This must be called by all modules including | 
|  | 167 | // SdkBase. | 
|  | 168 | func InitSdkAwareModule(m SdkAware) { | 
|  | 169 | base := m.sdkBase() | 
| Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 170 | base.module = m | 
| Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 171 | m.AddProperties(&base.properties) | 
|  | 172 | } | 
| Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 173 |  | 
|  | 174 | // Provide support for generating the build rules which will build the snapshot. | 
|  | 175 | type SnapshotBuilder interface { | 
|  | 176 | // Copy src to the dest (which is a snapshot relative path) and add the dest | 
|  | 177 | // to the zip | 
|  | 178 | CopyToSnapshot(src Path, dest string) | 
|  | 179 |  | 
| Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 180 | // Return the path to an empty file. | 
|  | 181 | // | 
|  | 182 | // This can be used by sdk member types that need to create an empty file in the snapshot, simply | 
|  | 183 | // pass the value returned from this to the CopyToSnapshot() method. | 
|  | 184 | EmptyFile() Path | 
|  | 185 |  | 
| Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 186 | // Unzip the supplied zip into the snapshot relative directory destDir. | 
|  | 187 | UnzipToSnapshot(zipPath Path, destDir string) | 
|  | 188 |  | 
| Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 189 | // Add a new prebuilt module to the snapshot. The returned module | 
|  | 190 | // must be populated with the module type specific properties. The following | 
|  | 191 | // properties will be automatically populated. | 
|  | 192 | // | 
|  | 193 | // * name | 
|  | 194 | // * sdk_member_name | 
|  | 195 | // * prefer | 
|  | 196 | // | 
|  | 197 | // This will result in two Soong modules being generated in the Android. One | 
|  | 198 | // that is versioned, coupled to the snapshot version and marked as | 
|  | 199 | // prefer=true. And one that is not versioned, not marked as prefer=true and | 
|  | 200 | // will only be used if the equivalently named non-prebuilt module is not | 
|  | 201 | // present. | 
| Paul Duffin | 9d8d609 | 2019-12-05 18:19:29 +0000 | [diff] [blame] | 202 | AddPrebuiltModule(member SdkMember, moduleType string) BpModule | 
| Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 203 |  | 
|  | 204 | // The property tag to use when adding a property to a BpModule that contains | 
|  | 205 | // references to other sdk members. Using this will ensure that the reference | 
|  | 206 | // is correctly output for both versioned and unversioned prebuilts in the | 
|  | 207 | // snapshot. | 
|  | 208 | // | 
| Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 209 | // "required: true" means that the property must only contain references | 
|  | 210 | // to other members of the sdk. Passing a reference to a module that is not a | 
|  | 211 | // member of the sdk will result in a build error. | 
|  | 212 | // | 
|  | 213 | // "required: false" means that the property can contain references to modules | 
|  | 214 | // that are either members or not members of the sdk. If a reference is to a | 
|  | 215 | // module that is a non member then the reference is left unchanged, i.e. it | 
|  | 216 | // is not transformed as references to members are. | 
|  | 217 | // | 
|  | 218 | // The handling of the member names is dependent on whether it is an internal or | 
|  | 219 | // exported member. An exported member is one whose name is specified in one of | 
|  | 220 | // the member type specific properties. An internal member is one that is added | 
|  | 221 | // due to being a part of an exported (or other internal) member and is not itself | 
|  | 222 | // an exported member. | 
|  | 223 | // | 
|  | 224 | // Member names are handled as follows: | 
|  | 225 | // * When creating the unversioned form of the module the name is left unchecked | 
|  | 226 | //   unless the member is internal in which case it is transformed into an sdk | 
|  | 227 | //   specific name, i.e. by prefixing with the sdk name. | 
|  | 228 | // | 
|  | 229 | // * When creating the versioned form of the module the name is transformed into | 
|  | 230 | //   a versioned sdk specific name, i.e. by prefixing with the sdk name and | 
|  | 231 | //   suffixing with the version. | 
|  | 232 | // | 
| Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 233 | // e.g. | 
| Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 234 | // bpPropertySet.AddPropertyWithTag("libs", []string{"member1", "member2"}, builder.SdkMemberReferencePropertyTag(true)) | 
|  | 235 | SdkMemberReferencePropertyTag(required bool) BpPropertyTag | 
| Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 236 | } | 
|  | 237 |  | 
| Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 238 | type BpPropertyTag interface{} | 
|  | 239 |  | 
| Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 240 | // A set of properties for use in a .bp file. | 
|  | 241 | type BpPropertySet interface { | 
|  | 242 | // Add a property, the value can be one of the following types: | 
|  | 243 | // * string | 
|  | 244 | // * array of the above | 
|  | 245 | // * bool | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 246 | // For these types it is an error if multiple properties with the same name | 
|  | 247 | // are added. | 
|  | 248 | // | 
|  | 249 | // * pointer to a struct | 
| Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 250 | // * BpPropertySet | 
|  | 251 | // | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 252 | // A pointer to a Blueprint-style property struct is first converted into a | 
|  | 253 | // BpPropertySet by traversing the fields and adding their values as | 
|  | 254 | // properties in a BpPropertySet. A field with a struct value is itself | 
|  | 255 | // converted into a BpPropertySet before adding. | 
|  | 256 | // | 
|  | 257 | // Adding a BpPropertySet is done as follows: | 
|  | 258 | // * If no property with the name exists then the BpPropertySet is added | 
|  | 259 | //   directly to this property. Care must be taken to ensure that it does not | 
|  | 260 | //   introduce a cycle. | 
|  | 261 | // * If a property exists with the name and the current value is a | 
|  | 262 | //   BpPropertySet then every property of the new BpPropertySet is added to | 
|  | 263 | //   the existing BpPropertySet. | 
|  | 264 | // * 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] | 265 | AddProperty(name string, value interface{}) | 
|  | 266 |  | 
| Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame] | 267 | // Add a property with an associated tag | 
|  | 268 | AddPropertyWithTag(name string, value interface{}, tag BpPropertyTag) | 
|  | 269 |  | 
| Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 270 | // Add a property set with the specified name and return so that additional | 
|  | 271 | // properties can be added. | 
|  | 272 | AddPropertySet(name string) BpPropertySet | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | // A .bp module definition. | 
|  | 276 | type BpModule interface { | 
|  | 277 | BpPropertySet | 
| Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 278 | } | 
| Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 279 |  | 
|  | 280 | // An individual member of the SDK, includes all of the variants that the SDK | 
|  | 281 | // requires. | 
|  | 282 | type SdkMember interface { | 
|  | 283 | // The name of the member. | 
|  | 284 | Name() string | 
|  | 285 |  | 
|  | 286 | // All the variants required by the SDK. | 
|  | 287 | Variants() []SdkAware | 
|  | 288 | } | 
|  | 289 |  | 
| Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 290 | type SdkMemberTypeDependencyTag interface { | 
|  | 291 | blueprint.DependencyTag | 
|  | 292 |  | 
|  | 293 | SdkMemberType() SdkMemberType | 
|  | 294 | } | 
|  | 295 |  | 
| Paul Duffin | cee7e66 | 2020-07-09 17:32:57 +0100 | [diff] [blame] | 296 | var _ SdkMemberTypeDependencyTag = (*sdkMemberDependencyTag)(nil) | 
|  | 297 | var _ ReplaceSourceWithPrebuilt = (*sdkMemberDependencyTag)(nil) | 
|  | 298 |  | 
| Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 299 | type sdkMemberDependencyTag struct { | 
|  | 300 | blueprint.BaseDependencyTag | 
|  | 301 | memberType SdkMemberType | 
|  | 302 | } | 
|  | 303 |  | 
|  | 304 | func (t *sdkMemberDependencyTag) SdkMemberType() SdkMemberType { | 
|  | 305 | return t.memberType | 
|  | 306 | } | 
|  | 307 |  | 
| Paul Duffin | cee7e66 | 2020-07-09 17:32:57 +0100 | [diff] [blame] | 308 | // Prevent dependencies from the sdk/module_exports onto their members from being | 
|  | 309 | // replaced with a preferred prebuilt. | 
|  | 310 | func (t *sdkMemberDependencyTag) ReplaceSourceWithPrebuilt() bool { | 
|  | 311 | return false | 
|  | 312 | } | 
|  | 313 |  | 
| Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 314 | func DependencyTagForSdkMemberType(memberType SdkMemberType) SdkMemberTypeDependencyTag { | 
|  | 315 | return &sdkMemberDependencyTag{memberType: memberType} | 
|  | 316 | } | 
|  | 317 |  | 
| Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 318 | // Interface that must be implemented for every type that can be a member of an | 
|  | 319 | // sdk. | 
|  | 320 | // | 
|  | 321 | // The basic implementation should look something like this, where ModuleType is | 
|  | 322 | // the name of the module type being supported. | 
|  | 323 | // | 
| Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 324 | //    type moduleTypeSdkMemberType struct { | 
|  | 325 | //        android.SdkMemberTypeBase | 
| Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 326 | //    } | 
|  | 327 | // | 
| Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 328 | //    func init() { | 
|  | 329 | //        android.RegisterSdkMemberType(&moduleTypeSdkMemberType{ | 
|  | 330 | //            SdkMemberTypeBase: android.SdkMemberTypeBase{ | 
|  | 331 | //                PropertyName: "module_types", | 
|  | 332 | //            }, | 
|  | 333 | //        } | 
| Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 334 | //    } | 
|  | 335 | // | 
|  | 336 | //    ...methods... | 
|  | 337 | // | 
|  | 338 | type SdkMemberType interface { | 
| Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 339 | // The name of the member type property on an sdk module. | 
|  | 340 | SdkPropertyName() string | 
|  | 341 |  | 
| Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 342 | // True if the member type supports the sdk/sdk_snapshot, false otherwise. | 
|  | 343 | UsableWithSdkAndSdkSnapshot() bool | 
|  | 344 |  | 
| Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 345 | // Return true if modules of this type can have dependencies which should be | 
|  | 346 | // treated as if they are sdk members. | 
|  | 347 | // | 
|  | 348 | // Any dependency that is to be treated as a member of the sdk needs to implement | 
|  | 349 | // SdkAware and be added with an SdkMemberTypeDependencyTag tag. | 
|  | 350 | HasTransitiveSdkMembers() bool | 
|  | 351 |  | 
| Martin Stjernholm | caa47d7 | 2020-07-11 04:52:24 +0100 | [diff] [blame] | 352 | // Return true if prebuilt host artifacts may be specific to the host OS. Only | 
|  | 353 | // applicable to modules where HostSupported() is true. If this is true, | 
|  | 354 | // snapshots will list each host OS variant explicitly and disable all other | 
|  | 355 | // host OS'es. | 
|  | 356 | IsHostOsDependent() bool | 
|  | 357 |  | 
| Martin Stjernholm | cd07bce | 2020-03-10 22:37:59 +0000 | [diff] [blame] | 358 | // Add dependencies from the SDK module to all the module variants the member | 
|  | 359 | // type contributes to the SDK. `names` is the list of module names given in | 
|  | 360 | // the member type property (as returned by SdkPropertyName()) in the SDK | 
|  | 361 | // module. The exact set of variants required is determined by the SDK and its | 
|  | 362 | // properties. The dependencies must be added with the supplied tag. | 
| Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 363 | // | 
|  | 364 | // The BottomUpMutatorContext provided is for the SDK module. | 
|  | 365 | AddDependencies(mctx BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) | 
|  | 366 |  | 
|  | 367 | // Return true if the supplied module is an instance of this member type. | 
|  | 368 | // | 
|  | 369 | // This is used to check the type of each variant before added to the | 
|  | 370 | // SdkMember. Returning false will cause an error to be logged expaining that | 
|  | 371 | // the module is not allowed in whichever sdk property it was added. | 
|  | 372 | IsInstance(module Module) bool | 
|  | 373 |  | 
| Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 374 | // Add a prebuilt module that the sdk will populate. | 
|  | 375 | // | 
| Paul Duffin | 425b0ea | 2020-05-06 12:41:39 +0100 | [diff] [blame] | 376 | // The sdk module code generates the snapshot as follows: | 
| Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 377 | // | 
|  | 378 | // * A properties struct of type SdkMemberProperties is created for each variant and | 
|  | 379 | //   populated with information from the variant by calling PopulateFromVariant(SdkAware) | 
|  | 380 | //   on the struct. | 
|  | 381 | // | 
|  | 382 | // * An additional properties struct is created into which the common properties will be | 
|  | 383 | //   added. | 
|  | 384 | // | 
|  | 385 | // * The variant property structs are analysed to find exported (capitalized) fields which | 
|  | 386 | //   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] | 387 | //   properties. | 
|  | 388 | // | 
|  | 389 | //   A field annotated with a tag of `sdk:"keep"` will be treated as if it | 
| Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 390 | //   was not capitalized, i.e. not optimized for common values. | 
| Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 391 | // | 
| Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 392 | //   A field annotated with a tag of `android:"arch_variant"` will be allowed to have | 
|  | 393 | //   values that differ by arch, fields not tagged as such must have common values across | 
|  | 394 | //   all variants. | 
|  | 395 | // | 
| Paul Duffin | c459f89 | 2020-04-30 18:08:29 +0100 | [diff] [blame] | 396 | // * Additional field tags can be specified on a field that will ignore certain values | 
|  | 397 | //   for the purpose of common value optimization. A value that is ignored must have the | 
|  | 398 | //   default value for the property type. This is to ensure that significant value are not | 
|  | 399 | //   ignored by accident. The purpose of this is to allow the snapshot generation to reflect | 
|  | 400 | //   the behavior of the runtime. e.g. if a property is ignored on the host then a property | 
|  | 401 | //   that is common for android can be treated as if it was common for android and host as | 
|  | 402 | //   the setting for host is ignored anyway. | 
|  | 403 | //   * `sdk:"ignored-on-host" - this indicates the property is ignored on the host variant. | 
|  | 404 | // | 
| Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 405 | // * The sdk module type populates the BpModule structure, creating the arch specific | 
|  | 406 | //   structure and calls AddToPropertySet(...) on the properties struct to add the member | 
|  | 407 | //   specific properties in the correct place in the structure. | 
|  | 408 | // | 
| Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 409 | AddPrebuiltModule(ctx SdkMemberContext, member SdkMember) BpModule | 
| Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 410 |  | 
| Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 411 | // Create a structure into which variant specific properties can be added. | 
|  | 412 | CreateVariantPropertiesStruct() SdkMemberProperties | 
| Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 413 | } | 
| Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 414 |  | 
| Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 415 | // Base type for SdkMemberType implementations. | 
| Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 416 | type SdkMemberTypeBase struct { | 
| Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 417 | PropertyName         string | 
|  | 418 | SupportsSdk          bool | 
|  | 419 | TransitiveSdkMembers bool | 
| Martin Stjernholm | caa47d7 | 2020-07-11 04:52:24 +0100 | [diff] [blame] | 420 | HostOsDependent      bool | 
| Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 421 | } | 
|  | 422 |  | 
|  | 423 | func (b *SdkMemberTypeBase) SdkPropertyName() string { | 
|  | 424 | return b.PropertyName | 
|  | 425 | } | 
|  | 426 |  | 
| Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 427 | func (b *SdkMemberTypeBase) UsableWithSdkAndSdkSnapshot() bool { | 
|  | 428 | return b.SupportsSdk | 
|  | 429 | } | 
|  | 430 |  | 
| Paul Duffin | f4ae4f1 | 2020-01-13 20:58:25 +0000 | [diff] [blame] | 431 | func (b *SdkMemberTypeBase) HasTransitiveSdkMembers() bool { | 
|  | 432 | return b.TransitiveSdkMembers | 
|  | 433 | } | 
|  | 434 |  | 
| Martin Stjernholm | caa47d7 | 2020-07-11 04:52:24 +0100 | [diff] [blame] | 435 | func (b *SdkMemberTypeBase) IsHostOsDependent() bool { | 
|  | 436 | return b.HostOsDependent | 
|  | 437 | } | 
|  | 438 |  | 
| Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 439 | // Encapsulates the information about registered SdkMemberTypes. | 
|  | 440 | type SdkMemberTypesRegistry struct { | 
|  | 441 | // The list of types sorted by property name. | 
|  | 442 | list []SdkMemberType | 
|  | 443 |  | 
|  | 444 | // The key that uniquely identifies this registry instance. | 
|  | 445 | key OnceKey | 
|  | 446 | } | 
|  | 447 |  | 
| Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 448 | func (r *SdkMemberTypesRegistry) copyAndAppend(memberType SdkMemberType) *SdkMemberTypesRegistry { | 
|  | 449 | oldList := r.list | 
| Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 450 |  | 
|  | 451 | // Copy the slice just in case this is being read while being modified, e.g. when testing. | 
|  | 452 | list := make([]SdkMemberType, 0, len(oldList)+1) | 
|  | 453 | list = append(list, oldList...) | 
|  | 454 | list = append(list, memberType) | 
|  | 455 |  | 
|  | 456 | // Sort the member types by their property name to ensure that registry order has no effect | 
|  | 457 | // on behavior. | 
|  | 458 | sort.Slice(list, func(i1, i2 int) bool { | 
|  | 459 | t1 := list[i1] | 
|  | 460 | t2 := list[i2] | 
|  | 461 |  | 
|  | 462 | return t1.SdkPropertyName() < t2.SdkPropertyName() | 
|  | 463 | }) | 
|  | 464 |  | 
|  | 465 | // Generate a key that identifies the slice of SdkMemberTypes by joining the property names | 
|  | 466 | // from all the SdkMemberType . | 
|  | 467 | var properties []string | 
|  | 468 | for _, t := range list { | 
|  | 469 | properties = append(properties, t.SdkPropertyName()) | 
|  | 470 | } | 
|  | 471 | key := NewOnceKey(strings.Join(properties, "|")) | 
|  | 472 |  | 
|  | 473 | // Create a new registry so the pointer uniquely identifies the set of registered types. | 
| Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 474 | return &SdkMemberTypesRegistry{ | 
| Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 475 | list: list, | 
|  | 476 | key:  key, | 
|  | 477 | } | 
|  | 478 | } | 
| Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 479 |  | 
|  | 480 | func (r *SdkMemberTypesRegistry) RegisteredTypes() []SdkMemberType { | 
|  | 481 | return r.list | 
|  | 482 | } | 
|  | 483 |  | 
|  | 484 | func (r *SdkMemberTypesRegistry) UniqueOnceKey() OnceKey { | 
|  | 485 | // Use the pointer to the registry as the unique key. | 
|  | 486 | return NewCustomOnceKey(r) | 
|  | 487 | } | 
|  | 488 |  | 
|  | 489 | // The set of registered SdkMemberTypes, one for sdk module and one for module_exports. | 
|  | 490 | var ModuleExportsMemberTypes = &SdkMemberTypesRegistry{} | 
|  | 491 | var SdkMemberTypes = &SdkMemberTypesRegistry{} | 
|  | 492 |  | 
|  | 493 | // Register an SdkMemberType object to allow them to be used in the sdk and sdk_snapshot module | 
|  | 494 | // types. | 
|  | 495 | func RegisterSdkMemberType(memberType SdkMemberType) { | 
|  | 496 | // All member types are usable with module_exports. | 
|  | 497 | ModuleExportsMemberTypes = ModuleExportsMemberTypes.copyAndAppend(memberType) | 
|  | 498 |  | 
|  | 499 | // Only those that explicitly indicate it are usable with sdk. | 
|  | 500 | if memberType.UsableWithSdkAndSdkSnapshot() { | 
|  | 501 | SdkMemberTypes = SdkMemberTypes.copyAndAppend(memberType) | 
|  | 502 | } | 
|  | 503 | } | 
| Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 504 |  | 
|  | 505 | // Base structure for all implementations of SdkMemberProperties. | 
|  | 506 | // | 
| Martin Stjernholm | 89238f4 | 2020-07-10 00:14:03 +0100 | [diff] [blame] | 507 | // Contains common properties that apply across many different member types. | 
| Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 508 | type SdkMemberPropertiesBase struct { | 
| Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 509 | // The number of unique os types supported by the member variants. | 
| Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 510 | // | 
|  | 511 | // If a member has a variant with more than one os type then it will need to differentiate | 
|  | 512 | // the locations of any of their prebuilt files in the snapshot by os type to prevent them | 
|  | 513 | // from colliding. See OsPrefix(). | 
|  | 514 | // | 
|  | 515 | // This property is the same for all variants of a member and so would be optimized away | 
|  | 516 | // if it was not explicitly kept. | 
| Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 517 | Os_count int `sdk:"keep"` | 
| Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 518 |  | 
|  | 519 | // The os type for which these properties refer. | 
| Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 520 | // | 
|  | 521 | // Provided to allow a member to differentiate between os types in the locations of their | 
|  | 522 | // prebuilt files when it supports more than one os type. | 
|  | 523 | // | 
|  | 524 | // This property is the same for all os type specific variants of a member and so would be | 
|  | 525 | // optimized away if it was not explicitly kept. | 
| Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 526 | Os OsType `sdk:"keep"` | 
| Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 527 |  | 
|  | 528 | // The setting to use for the compile_multilib property. | 
| Martin Stjernholm | 89238f4 | 2020-07-10 00:14:03 +0100 | [diff] [blame] | 529 | Compile_multilib string `android:"arch_variant"` | 
| Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 530 | } | 
|  | 531 |  | 
|  | 532 | // The os prefix to use for any file paths in the sdk. | 
|  | 533 | // | 
|  | 534 | // Is an empty string if the member only provides variants for a single os type, otherwise | 
|  | 535 | // is the OsType.Name. | 
|  | 536 | func (b *SdkMemberPropertiesBase) OsPrefix() string { | 
|  | 537 | if b.Os_count == 1 { | 
|  | 538 | return "" | 
|  | 539 | } else { | 
|  | 540 | return b.Os.Name | 
|  | 541 | } | 
| Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 542 | } | 
|  | 543 |  | 
|  | 544 | func (b *SdkMemberPropertiesBase) Base() *SdkMemberPropertiesBase { | 
|  | 545 | return b | 
|  | 546 | } | 
|  | 547 |  | 
|  | 548 | // Interface to be implemented on top of a structure that contains variant specific | 
|  | 549 | // information. | 
|  | 550 | // | 
|  | 551 | // Struct fields that are capitalized are examined for common values to extract. Fields | 
|  | 552 | // that are not capitalized are assumed to be arch specific. | 
|  | 553 | type SdkMemberProperties interface { | 
|  | 554 | // Access the base structure. | 
|  | 555 | Base() *SdkMemberPropertiesBase | 
|  | 556 |  | 
| Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 557 | // Populate this structure with information from the variant. | 
|  | 558 | PopulateFromVariant(ctx SdkMemberContext, variant Module) | 
| Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 559 |  | 
| Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 560 | // Add the information from this structure to the property set. | 
|  | 561 | AddToPropertySet(ctx SdkMemberContext, propertySet BpPropertySet) | 
|  | 562 | } | 
|  | 563 |  | 
|  | 564 | // Provides access to information common to a specific member. | 
|  | 565 | type SdkMemberContext interface { | 
|  | 566 |  | 
|  | 567 | // The module context of the sdk common os variant which is creating the snapshot. | 
|  | 568 | SdkModuleContext() ModuleContext | 
|  | 569 |  | 
|  | 570 | // The builder of the snapshot. | 
|  | 571 | SnapshotBuilder() SnapshotBuilder | 
| Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 572 |  | 
|  | 573 | // The type of the member. | 
|  | 574 | MemberType() SdkMemberType | 
|  | 575 |  | 
|  | 576 | // The name of the member. | 
|  | 577 | // | 
|  | 578 | // Provided for use by sdk members to create a member specific location within the snapshot | 
|  | 579 | // into which to copy the prebuilt files. | 
|  | 580 | Name() string | 
| Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 581 | } |