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