| 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 | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 183 | } | 
|  | 184 |  | 
| Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame^] | 185 | type BpPropertyTag interface{} | 
|  | 186 |  | 
| Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 187 | // A set of properties for use in a .bp file. | 
|  | 188 | type BpPropertySet interface { | 
|  | 189 | // Add a property, the value can be one of the following types: | 
|  | 190 | // * string | 
|  | 191 | // * array of the above | 
|  | 192 | // * bool | 
|  | 193 | // * BpPropertySet | 
|  | 194 | // | 
| Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame^] | 195 | // 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] | 196 | AddProperty(name string, value interface{}) | 
|  | 197 |  | 
| Paul Duffin | 5b511a2 | 2020-01-15 14:23:52 +0000 | [diff] [blame^] | 198 | // Add a property with an associated tag | 
|  | 199 | AddPropertyWithTag(name string, value interface{}, tag BpPropertyTag) | 
|  | 200 |  | 
| Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 201 | // Add a property set with the specified name and return so that additional | 
|  | 202 | // properties can be added. | 
|  | 203 | AddPropertySet(name string) BpPropertySet | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | // A .bp module definition. | 
|  | 207 | type BpModule interface { | 
|  | 208 | BpPropertySet | 
| Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 209 | } | 
| Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 210 |  | 
|  | 211 | // An individual member of the SDK, includes all of the variants that the SDK | 
|  | 212 | // requires. | 
|  | 213 | type SdkMember interface { | 
|  | 214 | // The name of the member. | 
|  | 215 | Name() string | 
|  | 216 |  | 
|  | 217 | // All the variants required by the SDK. | 
|  | 218 | Variants() []SdkAware | 
|  | 219 | } | 
|  | 220 |  | 
|  | 221 | // Interface that must be implemented for every type that can be a member of an | 
|  | 222 | // sdk. | 
|  | 223 | // | 
|  | 224 | // The basic implementation should look something like this, where ModuleType is | 
|  | 225 | // the name of the module type being supported. | 
|  | 226 | // | 
| Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 227 | //    type moduleTypeSdkMemberType struct { | 
|  | 228 | //        android.SdkMemberTypeBase | 
| Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 229 | //    } | 
|  | 230 | // | 
| Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 231 | //    func init() { | 
|  | 232 | //        android.RegisterSdkMemberType(&moduleTypeSdkMemberType{ | 
|  | 233 | //            SdkMemberTypeBase: android.SdkMemberTypeBase{ | 
|  | 234 | //                PropertyName: "module_types", | 
|  | 235 | //            }, | 
|  | 236 | //        } | 
| Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 237 | //    } | 
|  | 238 | // | 
|  | 239 | //    ...methods... | 
|  | 240 | // | 
|  | 241 | type SdkMemberType interface { | 
| Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 242 | // The name of the member type property on an sdk module. | 
|  | 243 | SdkPropertyName() string | 
|  | 244 |  | 
| Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 245 | // True if the member type supports the sdk/sdk_snapshot, false otherwise. | 
|  | 246 | UsableWithSdkAndSdkSnapshot() bool | 
|  | 247 |  | 
| Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 248 | // Add dependencies from the SDK module to all the variants the member | 
|  | 249 | // contributes to the SDK. The exact set of variants required is determined | 
|  | 250 | // by the SDK and its properties. The dependencies must be added with the | 
|  | 251 | // supplied tag. | 
|  | 252 | // | 
|  | 253 | // The BottomUpMutatorContext provided is for the SDK module. | 
|  | 254 | AddDependencies(mctx BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) | 
|  | 255 |  | 
|  | 256 | // Return true if the supplied module is an instance of this member type. | 
|  | 257 | // | 
|  | 258 | // This is used to check the type of each variant before added to the | 
|  | 259 | // SdkMember. Returning false will cause an error to be logged expaining that | 
|  | 260 | // the module is not allowed in whichever sdk property it was added. | 
|  | 261 | IsInstance(module Module) bool | 
|  | 262 |  | 
|  | 263 | // Build the snapshot for the SDK member | 
|  | 264 | // | 
|  | 265 | // The ModuleContext provided is for the SDK module, so information for | 
|  | 266 | // variants in the supplied member can be accessed using the Other... methods. | 
|  | 267 | // | 
|  | 268 | // The SdkMember is guaranteed to contain variants for which the | 
|  | 269 | // IsInstance(Module) method returned true. | 
|  | 270 | BuildSnapshot(sdkModuleContext ModuleContext, builder SnapshotBuilder, member SdkMember) | 
|  | 271 | } | 
| Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 272 |  | 
| Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 273 | // Base type for SdkMemberType implementations. | 
| Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 274 | type SdkMemberTypeBase struct { | 
|  | 275 | PropertyName string | 
| Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 276 | SupportsSdk  bool | 
| Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 277 | } | 
|  | 278 |  | 
|  | 279 | func (b *SdkMemberTypeBase) SdkPropertyName() string { | 
|  | 280 | return b.PropertyName | 
|  | 281 | } | 
|  | 282 |  | 
| Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 283 | func (b *SdkMemberTypeBase) UsableWithSdkAndSdkSnapshot() bool { | 
|  | 284 | return b.SupportsSdk | 
|  | 285 | } | 
|  | 286 |  | 
| Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 287 | // Encapsulates the information about registered SdkMemberTypes. | 
|  | 288 | type SdkMemberTypesRegistry struct { | 
|  | 289 | // The list of types sorted by property name. | 
|  | 290 | list []SdkMemberType | 
|  | 291 |  | 
|  | 292 | // The key that uniquely identifies this registry instance. | 
|  | 293 | key OnceKey | 
|  | 294 | } | 
|  | 295 |  | 
| Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 296 | func (r *SdkMemberTypesRegistry) copyAndAppend(memberType SdkMemberType) *SdkMemberTypesRegistry { | 
|  | 297 | oldList := r.list | 
| Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 298 |  | 
|  | 299 | // Copy the slice just in case this is being read while being modified, e.g. when testing. | 
|  | 300 | list := make([]SdkMemberType, 0, len(oldList)+1) | 
|  | 301 | list = append(list, oldList...) | 
|  | 302 | list = append(list, memberType) | 
|  | 303 |  | 
|  | 304 | // Sort the member types by their property name to ensure that registry order has no effect | 
|  | 305 | // on behavior. | 
|  | 306 | sort.Slice(list, func(i1, i2 int) bool { | 
|  | 307 | t1 := list[i1] | 
|  | 308 | t2 := list[i2] | 
|  | 309 |  | 
|  | 310 | return t1.SdkPropertyName() < t2.SdkPropertyName() | 
|  | 311 | }) | 
|  | 312 |  | 
|  | 313 | // Generate a key that identifies the slice of SdkMemberTypes by joining the property names | 
|  | 314 | // from all the SdkMemberType . | 
|  | 315 | var properties []string | 
|  | 316 | for _, t := range list { | 
|  | 317 | properties = append(properties, t.SdkPropertyName()) | 
|  | 318 | } | 
|  | 319 | key := NewOnceKey(strings.Join(properties, "|")) | 
|  | 320 |  | 
|  | 321 | // 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] | 322 | return &SdkMemberTypesRegistry{ | 
| Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 323 | list: list, | 
|  | 324 | key:  key, | 
|  | 325 | } | 
|  | 326 | } | 
| Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 327 |  | 
|  | 328 | func (r *SdkMemberTypesRegistry) RegisteredTypes() []SdkMemberType { | 
|  | 329 | return r.list | 
|  | 330 | } | 
|  | 331 |  | 
|  | 332 | func (r *SdkMemberTypesRegistry) UniqueOnceKey() OnceKey { | 
|  | 333 | // Use the pointer to the registry as the unique key. | 
|  | 334 | return NewCustomOnceKey(r) | 
|  | 335 | } | 
|  | 336 |  | 
|  | 337 | // The set of registered SdkMemberTypes, one for sdk module and one for module_exports. | 
|  | 338 | var ModuleExportsMemberTypes = &SdkMemberTypesRegistry{} | 
|  | 339 | var SdkMemberTypes = &SdkMemberTypesRegistry{} | 
|  | 340 |  | 
|  | 341 | // Register an SdkMemberType object to allow them to be used in the sdk and sdk_snapshot module | 
|  | 342 | // types. | 
|  | 343 | func RegisterSdkMemberType(memberType SdkMemberType) { | 
|  | 344 | // All member types are usable with module_exports. | 
|  | 345 | ModuleExportsMemberTypes = ModuleExportsMemberTypes.copyAndAppend(memberType) | 
|  | 346 |  | 
|  | 347 | // Only those that explicitly indicate it are usable with sdk. | 
|  | 348 | if memberType.UsableWithSdkAndSdkSnapshot() { | 
|  | 349 | SdkMemberTypes = SdkMemberTypes.copyAndAppend(memberType) | 
|  | 350 | } | 
|  | 351 | } |