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