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