blob: 0adfd89c3c4998e2b9c509e24c573504faeefefe [file] [log] [blame]
Jiyong Parkd1063c12019-07-17 20:08:41 +09001// 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
15package android
16
17import (
Paul Duffin255f18e2019-12-13 11:22:16 +000018 "sort"
Jiyong Parkd1063c12019-07-17 20:08:41 +090019 "strings"
20
Paul Duffin13879572019-11-28 14:31:38 +000021 "github.com/google/blueprint"
Jiyong Parkd1063c12019-07-17 20:08:41 +090022 "github.com/google/blueprint/proptools"
23)
24
Paul Duffin923e8a52020-03-30 15:33:32 +010025// 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`.
32type RequiredSdks interface {
33 // The set of SDKs required by an APEX and its contents.
34 RequiredSdks() SdkRefs
35}
36
Paul Duffin50f0da42020-07-22 13:52:01 +010037// Provided to improve code navigation with the IDE.
38type sdkAwareWithoutModule interface {
Paul Duffin923e8a52020-03-30 15:33:32 +010039 RequiredSdks
40
Jiyong Parkd1063c12019-07-17 20:08:41 +090041 sdkBase() *SdkBase
42 MakeMemberOf(sdk SdkRef)
43 IsInAnySdk() bool
Paul Duffinb9e7a3c2021-05-06 15:53:19 +010044
45 // IsVersioned determines whether the module is versioned, i.e. has a name of the form
46 // <name>@<version>
47 IsVersioned() bool
48
Jiyong Parkd1063c12019-07-17 20:08:41 +090049 ContainingSdk() SdkRef
50 MemberName() string
51 BuildWithSdks(sdks SdkRefs)
Jiyong Parkd1063c12019-07-17 20:08:41 +090052}
53
Paul Duffin50f0da42020-07-22 13:52:01 +010054// SdkAware is the interface that must be supported by any module to become a member of SDK or to be
55// built with SDK
56type SdkAware interface {
57 Module
58 sdkAwareWithoutModule
59}
60
Jiyong Parkd1063c12019-07-17 20:08:41 +090061// SdkRef refers to a version of an SDK
62type SdkRef struct {
63 Name string
64 Version string
65}
66
Jiyong Park9b409bc2019-10-11 14:59:13 +090067// Unversioned determines if the SdkRef is referencing to the unversioned SDK module
68func (s SdkRef) Unversioned() bool {
69 return s.Version == ""
Jiyong Parkd1063c12019-07-17 20:08:41 +090070}
71
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090072// String returns string representation of this SdkRef for debugging purpose
73func (s SdkRef) String() string {
74 if s.Name == "" {
75 return "(No Sdk)"
76 }
77 if s.Unversioned() {
78 return s.Name
79 }
80 return s.Name + string(SdkVersionSeparator) + s.Version
81}
82
Jiyong Park9b409bc2019-10-11 14:59:13 +090083// SdkVersionSeparator is a character used to separate an sdk name and its version
84const SdkVersionSeparator = '@'
Jiyong Parkd1063c12019-07-17 20:08:41 +090085
Jiyong Park9b409bc2019-10-11 14:59:13 +090086// ParseSdkRef parses a `name@version` style string into a corresponding SdkRef struct
Jiyong Parkd1063c12019-07-17 20:08:41 +090087func ParseSdkRef(ctx BaseModuleContext, str string, property string) SdkRef {
Jiyong Park9b409bc2019-10-11 14:59:13 +090088 tokens := strings.Split(str, string(SdkVersionSeparator))
Jiyong Parkd1063c12019-07-17 20:08:41 +090089 if len(tokens) < 1 || len(tokens) > 2 {
Paul Duffin525a5902021-05-06 16:33:52 +010090 ctx.PropertyErrorf(property, "%q does not follow name@version syntax", str)
Jiyong Parkd1063c12019-07-17 20:08:41 +090091 return SdkRef{Name: "invalid sdk name", Version: "invalid sdk version"}
92 }
93
94 name := tokens[0]
95
Jiyong Park9b409bc2019-10-11 14:59:13 +090096 var version string
Jiyong Parkd1063c12019-07-17 20:08:41 +090097 if len(tokens) == 2 {
98 version = tokens[1]
99 }
100
101 return SdkRef{Name: name, Version: version}
102}
103
104type SdkRefs []SdkRef
105
Jiyong Park9b409bc2019-10-11 14:59:13 +0900106// Contains tells if the given SdkRef is in this list of SdkRef's
Jiyong Parkd1063c12019-07-17 20:08:41 +0900107func (refs SdkRefs) Contains(s SdkRef) bool {
108 for _, r := range refs {
109 if r == s {
110 return true
111 }
112 }
113 return false
114}
115
116type sdkProperties struct {
117 // The SDK that this module is a member of. nil if it is not a member of any SDK
118 ContainingSdk *SdkRef `blueprint:"mutated"`
119
120 // The list of SDK names and versions that are used to build this module
121 RequiredSdks SdkRefs `blueprint:"mutated"`
122
123 // Name of the module that this sdk member is representing
124 Sdk_member_name *string
125}
126
127// SdkBase is a struct that is expected to be included in module types to implement the SdkAware
128// interface. InitSdkAwareModule should be called to initialize this struct.
129type SdkBase struct {
130 properties sdkProperties
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000131 module SdkAware
Jiyong Parkd1063c12019-07-17 20:08:41 +0900132}
133
134func (s *SdkBase) sdkBase() *SdkBase {
135 return s
136}
137
Jiyong Park9b409bc2019-10-11 14:59:13 +0900138// MakeMemberOf sets this module to be a member of a specific SDK
Jiyong Parkd1063c12019-07-17 20:08:41 +0900139func (s *SdkBase) MakeMemberOf(sdk SdkRef) {
140 s.properties.ContainingSdk = &sdk
141}
142
143// IsInAnySdk returns true if this module is a member of any SDK
144func (s *SdkBase) IsInAnySdk() bool {
145 return s.properties.ContainingSdk != nil
146}
147
Paul Duffinb9e7a3c2021-05-06 15:53:19 +0100148// IsVersioned returns true if this module is versioned.
149func (s *SdkBase) IsVersioned() bool {
150 return strings.Contains(s.module.Name(), "@")
151}
152
Jiyong Parkd1063c12019-07-17 20:08:41 +0900153// ContainingSdk returns the SDK that this module is a member of
154func (s *SdkBase) ContainingSdk() SdkRef {
155 if s.properties.ContainingSdk != nil {
156 return *s.properties.ContainingSdk
157 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900158 return SdkRef{Name: "", Version: ""}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900159}
160
Jiyong Park9b409bc2019-10-11 14:59:13 +0900161// MemberName returns the name of the module that this SDK member is overriding
Jiyong Parkd1063c12019-07-17 20:08:41 +0900162func (s *SdkBase) MemberName() string {
163 return proptools.String(s.properties.Sdk_member_name)
164}
165
166// BuildWithSdks is used to mark that this module has to be built with the given SDK(s).
167func (s *SdkBase) BuildWithSdks(sdks SdkRefs) {
168 s.properties.RequiredSdks = sdks
169}
170
171// RequiredSdks returns the SDK(s) that this module has to be built with
172func (s *SdkBase) RequiredSdks() SdkRefs {
173 return s.properties.RequiredSdks
174}
175
176// InitSdkAwareModule initializes the SdkBase struct. This must be called by all modules including
177// SdkBase.
178func InitSdkAwareModule(m SdkAware) {
179 base := m.sdkBase()
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000180 base.module = m
Jiyong Parkd1063c12019-07-17 20:08:41 +0900181 m.AddProperties(&base.properties)
182}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000183
Paul Duffin0c2e0832021-04-28 00:39:52 +0100184// IsModuleInVersionedSdk returns true if the module is an versioned sdk.
185func IsModuleInVersionedSdk(module Module) bool {
186 if s, ok := module.(SdkAware); ok {
187 if !s.ContainingSdk().Unversioned() {
188 return true
189 }
190 }
191 return false
192}
193
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000194// Provide support for generating the build rules which will build the snapshot.
195type SnapshotBuilder interface {
196 // Copy src to the dest (which is a snapshot relative path) and add the dest
197 // to the zip
198 CopyToSnapshot(src Path, dest string)
199
Paul Duffin91547182019-11-12 19:39:36 +0000200 // Unzip the supplied zip into the snapshot relative directory destDir.
201 UnzipToSnapshot(zipPath Path, destDir string)
202
Paul Duffinb645ec82019-11-27 17:43:54 +0000203 // Add a new prebuilt module to the snapshot. The returned module
204 // must be populated with the module type specific properties. The following
205 // properties will be automatically populated.
206 //
207 // * name
208 // * sdk_member_name
209 // * prefer
210 //
211 // This will result in two Soong modules being generated in the Android. One
212 // that is versioned, coupled to the snapshot version and marked as
213 // prefer=true. And one that is not versioned, not marked as prefer=true and
214 // will only be used if the equivalently named non-prebuilt module is not
215 // present.
Paul Duffin9d8d6092019-12-05 18:19:29 +0000216 AddPrebuiltModule(member SdkMember, moduleType string) BpModule
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000217
218 // The property tag to use when adding a property to a BpModule that contains
219 // references to other sdk members. Using this will ensure that the reference
220 // is correctly output for both versioned and unversioned prebuilts in the
221 // snapshot.
222 //
Paul Duffin13f02712020-03-06 12:30:43 +0000223 // "required: true" means that the property must only contain references
224 // to other members of the sdk. Passing a reference to a module that is not a
225 // member of the sdk will result in a build error.
226 //
227 // "required: false" means that the property can contain references to modules
228 // that are either members or not members of the sdk. If a reference is to a
229 // module that is a non member then the reference is left unchanged, i.e. it
230 // is not transformed as references to members are.
231 //
232 // The handling of the member names is dependent on whether it is an internal or
233 // exported member. An exported member is one whose name is specified in one of
234 // the member type specific properties. An internal member is one that is added
235 // due to being a part of an exported (or other internal) member and is not itself
236 // an exported member.
237 //
238 // Member names are handled as follows:
239 // * When creating the unversioned form of the module the name is left unchecked
240 // unless the member is internal in which case it is transformed into an sdk
241 // specific name, i.e. by prefixing with the sdk name.
242 //
243 // * When creating the versioned form of the module the name is transformed into
244 // a versioned sdk specific name, i.e. by prefixing with the sdk name and
245 // suffixing with the version.
246 //
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000247 // e.g.
Paul Duffin13f02712020-03-06 12:30:43 +0000248 // bpPropertySet.AddPropertyWithTag("libs", []string{"member1", "member2"}, builder.SdkMemberReferencePropertyTag(true))
249 SdkMemberReferencePropertyTag(required bool) BpPropertyTag
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000250}
251
Paul Duffin5b511a22020-01-15 14:23:52 +0000252type BpPropertyTag interface{}
253
Paul Duffinb645ec82019-11-27 17:43:54 +0000254// A set of properties for use in a .bp file.
255type BpPropertySet interface {
256 // Add a property, the value can be one of the following types:
257 // * string
258 // * array of the above
259 // * bool
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100260 // For these types it is an error if multiple properties with the same name
261 // are added.
262 //
263 // * pointer to a struct
Paul Duffinb645ec82019-11-27 17:43:54 +0000264 // * BpPropertySet
265 //
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100266 // A pointer to a Blueprint-style property struct is first converted into a
267 // BpPropertySet by traversing the fields and adding their values as
268 // properties in a BpPropertySet. A field with a struct value is itself
269 // converted into a BpPropertySet before adding.
270 //
271 // Adding a BpPropertySet is done as follows:
272 // * If no property with the name exists then the BpPropertySet is added
273 // directly to this property. Care must be taken to ensure that it does not
274 // introduce a cycle.
275 // * If a property exists with the name and the current value is a
276 // BpPropertySet then every property of the new BpPropertySet is added to
277 // the existing BpPropertySet.
278 // * Otherwise, if a property exists with the name then it is an error.
Paul Duffinb645ec82019-11-27 17:43:54 +0000279 AddProperty(name string, value interface{})
280
Paul Duffin5b511a22020-01-15 14:23:52 +0000281 // Add a property with an associated tag
282 AddPropertyWithTag(name string, value interface{}, tag BpPropertyTag)
283
Paul Duffinb645ec82019-11-27 17:43:54 +0000284 // Add a property set with the specified name and return so that additional
285 // properties can be added.
286 AddPropertySet(name string) BpPropertySet
287}
288
289// A .bp module definition.
290type BpModule interface {
291 BpPropertySet
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000292}
Paul Duffin13879572019-11-28 14:31:38 +0000293
294// An individual member of the SDK, includes all of the variants that the SDK
295// requires.
296type SdkMember interface {
297 // The name of the member.
298 Name() string
299
300 // All the variants required by the SDK.
301 Variants() []SdkAware
302}
303
Paul Duffina7208112021-04-23 21:20:20 +0100304// SdkMemberTypeDependencyTag is the interface that a tag must implement in order to allow the
Paul Duffin2d3da312021-05-06 12:02:27 +0100305// dependent module to be automatically added to the sdk.
Paul Duffinf8539922019-11-19 19:44:10 +0000306type SdkMemberTypeDependencyTag interface {
307 blueprint.DependencyTag
308
Paul Duffina7208112021-04-23 21:20:20 +0100309 // SdkMemberType returns the SdkMemberType that will be used to automatically add the child module
310 // to the sdk.
Paul Duffineee466e2021-04-27 23:17:56 +0100311 SdkMemberType(child Module) SdkMemberType
Paul Duffina7208112021-04-23 21:20:20 +0100312
313 // ExportMember determines whether a module added to the sdk through this tag will be exported
314 // from the sdk or not.
315 //
316 // An exported member is added to the sdk using its own name, e.g. if "foo" was exported from sdk
317 // "bar" then its prebuilt would be simply called "foo". A member can be added to the sdk via
318 // multiple tags and if any of those tags returns true from this method then the membe will be
319 // exported. Every module added directly to the sdk via one of the member type specific
320 // properties, e.g. java_libs, will automatically be exported.
321 //
322 // If a member is not exported then it is treated as an internal implementation detail of the
323 // sdk and so will be added with an sdk specific name. e.g. if "foo" was an internal member of sdk
324 // "bar" then its prebuilt would be called "bar_foo". Additionally its visibility will be set to
325 // "//visibility:private" so it will not be accessible from outside its Android.bp file.
326 ExportMember() bool
Paul Duffinf8539922019-11-19 19:44:10 +0000327}
328
Paul Duffincee7e662020-07-09 17:32:57 +0100329var _ SdkMemberTypeDependencyTag = (*sdkMemberDependencyTag)(nil)
330var _ ReplaceSourceWithPrebuilt = (*sdkMemberDependencyTag)(nil)
331
Paul Duffinf8539922019-11-19 19:44:10 +0000332type sdkMemberDependencyTag struct {
333 blueprint.BaseDependencyTag
334 memberType SdkMemberType
Paul Duffina7208112021-04-23 21:20:20 +0100335 export bool
Paul Duffinf8539922019-11-19 19:44:10 +0000336}
337
Paul Duffineee466e2021-04-27 23:17:56 +0100338func (t *sdkMemberDependencyTag) SdkMemberType(_ Module) SdkMemberType {
Paul Duffinf8539922019-11-19 19:44:10 +0000339 return t.memberType
340}
341
Paul Duffina7208112021-04-23 21:20:20 +0100342func (t *sdkMemberDependencyTag) ExportMember() bool {
343 return t.export
344}
345
Paul Duffincee7e662020-07-09 17:32:57 +0100346// Prevent dependencies from the sdk/module_exports onto their members from being
347// replaced with a preferred prebuilt.
348func (t *sdkMemberDependencyTag) ReplaceSourceWithPrebuilt() bool {
349 return false
350}
351
Paul Duffina7208112021-04-23 21:20:20 +0100352// DependencyTagForSdkMemberType creates an SdkMemberTypeDependencyTag that will cause any
353// dependencies added by the tag to be added to the sdk as the specified SdkMemberType and exported
354// (or not) as specified by the export parameter.
355func DependencyTagForSdkMemberType(memberType SdkMemberType, export bool) SdkMemberTypeDependencyTag {
356 return &sdkMemberDependencyTag{memberType: memberType, export: export}
Paul Duffinf8539922019-11-19 19:44:10 +0000357}
358
Paul Duffin13879572019-11-28 14:31:38 +0000359// Interface that must be implemented for every type that can be a member of an
360// sdk.
361//
362// The basic implementation should look something like this, where ModuleType is
363// the name of the module type being supported.
364//
Paul Duffin255f18e2019-12-13 11:22:16 +0000365// type moduleTypeSdkMemberType struct {
366// android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +0000367// }
368//
Paul Duffin255f18e2019-12-13 11:22:16 +0000369// func init() {
370// android.RegisterSdkMemberType(&moduleTypeSdkMemberType{
371// SdkMemberTypeBase: android.SdkMemberTypeBase{
372// PropertyName: "module_types",
373// },
374// }
Paul Duffin13879572019-11-28 14:31:38 +0000375// }
376//
377// ...methods...
378//
379type SdkMemberType interface {
Paul Duffin255f18e2019-12-13 11:22:16 +0000380 // The name of the member type property on an sdk module.
381 SdkPropertyName() string
382
Paul Duffine6029182019-12-16 17:43:48 +0000383 // True if the member type supports the sdk/sdk_snapshot, false otherwise.
384 UsableWithSdkAndSdkSnapshot() bool
385
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100386 // Return true if prebuilt host artifacts may be specific to the host OS. Only
387 // applicable to modules where HostSupported() is true. If this is true,
388 // snapshots will list each host OS variant explicitly and disable all other
389 // host OS'es.
390 IsHostOsDependent() bool
391
Martin Stjernholmcd07bce2020-03-10 22:37:59 +0000392 // Add dependencies from the SDK module to all the module variants the member
393 // type contributes to the SDK. `names` is the list of module names given in
394 // the member type property (as returned by SdkPropertyName()) in the SDK
395 // module. The exact set of variants required is determined by the SDK and its
396 // properties. The dependencies must be added with the supplied tag.
Paul Duffin13879572019-11-28 14:31:38 +0000397 //
398 // The BottomUpMutatorContext provided is for the SDK module.
399 AddDependencies(mctx BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string)
400
401 // Return true if the supplied module is an instance of this member type.
402 //
403 // This is used to check the type of each variant before added to the
404 // SdkMember. Returning false will cause an error to be logged expaining that
405 // the module is not allowed in whichever sdk property it was added.
406 IsInstance(module Module) bool
407
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000408 // Add a prebuilt module that the sdk will populate.
409 //
Paul Duffin425b0ea2020-05-06 12:41:39 +0100410 // The sdk module code generates the snapshot as follows:
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000411 //
412 // * A properties struct of type SdkMemberProperties is created for each variant and
413 // populated with information from the variant by calling PopulateFromVariant(SdkAware)
414 // on the struct.
415 //
416 // * An additional properties struct is created into which the common properties will be
417 // added.
418 //
419 // * The variant property structs are analysed to find exported (capitalized) fields which
420 // have common values. Those fields are cleared and the common value added to the common
Paul Duffin864e1b42020-05-06 10:23:19 +0100421 // properties.
422 //
423 // A field annotated with a tag of `sdk:"keep"` will be treated as if it
Paul Duffinb07fa512020-03-10 22:17:04 +0000424 // was not capitalized, i.e. not optimized for common values.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000425 //
Paul Duffin864e1b42020-05-06 10:23:19 +0100426 // A field annotated with a tag of `android:"arch_variant"` will be allowed to have
427 // values that differ by arch, fields not tagged as such must have common values across
428 // all variants.
429 //
Paul Duffinc459f892020-04-30 18:08:29 +0100430 // * Additional field tags can be specified on a field that will ignore certain values
431 // for the purpose of common value optimization. A value that is ignored must have the
432 // default value for the property type. This is to ensure that significant value are not
433 // ignored by accident. The purpose of this is to allow the snapshot generation to reflect
434 // the behavior of the runtime. e.g. if a property is ignored on the host then a property
435 // that is common for android can be treated as if it was common for android and host as
436 // the setting for host is ignored anyway.
437 // * `sdk:"ignored-on-host" - this indicates the property is ignored on the host variant.
438 //
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000439 // * The sdk module type populates the BpModule structure, creating the arch specific
440 // structure and calls AddToPropertySet(...) on the properties struct to add the member
441 // specific properties in the correct place in the structure.
442 //
Paul Duffin3a4eb502020-03-19 16:11:18 +0000443 AddPrebuiltModule(ctx SdkMemberContext, member SdkMember) BpModule
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000444
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000445 // Create a structure into which variant specific properties can be added.
446 CreateVariantPropertiesStruct() SdkMemberProperties
Paul Duffin13879572019-11-28 14:31:38 +0000447}
Paul Duffin255f18e2019-12-13 11:22:16 +0000448
Paul Duffine6029182019-12-16 17:43:48 +0000449// Base type for SdkMemberType implementations.
Paul Duffin255f18e2019-12-13 11:22:16 +0000450type SdkMemberTypeBase struct {
Paul Duffin2d3da312021-05-06 12:02:27 +0100451 PropertyName string
452 SupportsSdk bool
453 HostOsDependent bool
Paul Duffin255f18e2019-12-13 11:22:16 +0000454}
455
456func (b *SdkMemberTypeBase) SdkPropertyName() string {
457 return b.PropertyName
458}
459
Paul Duffine6029182019-12-16 17:43:48 +0000460func (b *SdkMemberTypeBase) UsableWithSdkAndSdkSnapshot() bool {
461 return b.SupportsSdk
462}
463
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100464func (b *SdkMemberTypeBase) IsHostOsDependent() bool {
465 return b.HostOsDependent
466}
467
Paul Duffin255f18e2019-12-13 11:22:16 +0000468// Encapsulates the information about registered SdkMemberTypes.
469type SdkMemberTypesRegistry struct {
470 // The list of types sorted by property name.
471 list []SdkMemberType
472
473 // The key that uniquely identifies this registry instance.
474 key OnceKey
475}
476
Paul Duffine6029182019-12-16 17:43:48 +0000477func (r *SdkMemberTypesRegistry) copyAndAppend(memberType SdkMemberType) *SdkMemberTypesRegistry {
478 oldList := r.list
Paul Duffin255f18e2019-12-13 11:22:16 +0000479
480 // Copy the slice just in case this is being read while being modified, e.g. when testing.
481 list := make([]SdkMemberType, 0, len(oldList)+1)
482 list = append(list, oldList...)
483 list = append(list, memberType)
484
485 // Sort the member types by their property name to ensure that registry order has no effect
486 // on behavior.
487 sort.Slice(list, func(i1, i2 int) bool {
488 t1 := list[i1]
489 t2 := list[i2]
490
491 return t1.SdkPropertyName() < t2.SdkPropertyName()
492 })
493
494 // Generate a key that identifies the slice of SdkMemberTypes by joining the property names
495 // from all the SdkMemberType .
496 var properties []string
497 for _, t := range list {
498 properties = append(properties, t.SdkPropertyName())
499 }
500 key := NewOnceKey(strings.Join(properties, "|"))
501
502 // Create a new registry so the pointer uniquely identifies the set of registered types.
Paul Duffine6029182019-12-16 17:43:48 +0000503 return &SdkMemberTypesRegistry{
Paul Duffin255f18e2019-12-13 11:22:16 +0000504 list: list,
505 key: key,
506 }
507}
Paul Duffine6029182019-12-16 17:43:48 +0000508
509func (r *SdkMemberTypesRegistry) RegisteredTypes() []SdkMemberType {
510 return r.list
511}
512
513func (r *SdkMemberTypesRegistry) UniqueOnceKey() OnceKey {
514 // Use the pointer to the registry as the unique key.
515 return NewCustomOnceKey(r)
516}
517
518// The set of registered SdkMemberTypes, one for sdk module and one for module_exports.
519var ModuleExportsMemberTypes = &SdkMemberTypesRegistry{}
520var SdkMemberTypes = &SdkMemberTypesRegistry{}
521
522// Register an SdkMemberType object to allow them to be used in the sdk and sdk_snapshot module
523// types.
524func RegisterSdkMemberType(memberType SdkMemberType) {
525 // All member types are usable with module_exports.
526 ModuleExportsMemberTypes = ModuleExportsMemberTypes.copyAndAppend(memberType)
527
528 // Only those that explicitly indicate it are usable with sdk.
529 if memberType.UsableWithSdkAndSdkSnapshot() {
530 SdkMemberTypes = SdkMemberTypes.copyAndAppend(memberType)
531 }
532}
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000533
534// Base structure for all implementations of SdkMemberProperties.
535//
Martin Stjernholm89238f42020-07-10 00:14:03 +0100536// Contains common properties that apply across many different member types.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000537type SdkMemberPropertiesBase struct {
Paul Duffina04c1072020-03-02 10:16:35 +0000538 // The number of unique os types supported by the member variants.
Paul Duffina551a1c2020-03-17 21:04:24 +0000539 //
540 // If a member has a variant with more than one os type then it will need to differentiate
541 // the locations of any of their prebuilt files in the snapshot by os type to prevent them
542 // from colliding. See OsPrefix().
543 //
544 // This property is the same for all variants of a member and so would be optimized away
545 // if it was not explicitly kept.
Paul Duffinb07fa512020-03-10 22:17:04 +0000546 Os_count int `sdk:"keep"`
Paul Duffina04c1072020-03-02 10:16:35 +0000547
548 // The os type for which these properties refer.
Paul Duffina551a1c2020-03-17 21:04:24 +0000549 //
550 // Provided to allow a member to differentiate between os types in the locations of their
551 // prebuilt files when it supports more than one os type.
552 //
553 // This property is the same for all os type specific variants of a member and so would be
554 // optimized away if it was not explicitly kept.
Paul Duffinb07fa512020-03-10 22:17:04 +0000555 Os OsType `sdk:"keep"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000556
557 // The setting to use for the compile_multilib property.
Martin Stjernholm89238f42020-07-10 00:14:03 +0100558 Compile_multilib string `android:"arch_variant"`
Paul Duffina04c1072020-03-02 10:16:35 +0000559}
560
561// The os prefix to use for any file paths in the sdk.
562//
563// Is an empty string if the member only provides variants for a single os type, otherwise
564// is the OsType.Name.
565func (b *SdkMemberPropertiesBase) OsPrefix() string {
566 if b.Os_count == 1 {
567 return ""
568 } else {
569 return b.Os.Name
570 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000571}
572
573func (b *SdkMemberPropertiesBase) Base() *SdkMemberPropertiesBase {
574 return b
575}
576
577// Interface to be implemented on top of a structure that contains variant specific
578// information.
579//
580// Struct fields that are capitalized are examined for common values to extract. Fields
581// that are not capitalized are assumed to be arch specific.
582type SdkMemberProperties interface {
583 // Access the base structure.
584 Base() *SdkMemberPropertiesBase
585
Paul Duffin3a4eb502020-03-19 16:11:18 +0000586 // Populate this structure with information from the variant.
587 PopulateFromVariant(ctx SdkMemberContext, variant Module)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000588
Paul Duffin3a4eb502020-03-19 16:11:18 +0000589 // Add the information from this structure to the property set.
590 AddToPropertySet(ctx SdkMemberContext, propertySet BpPropertySet)
591}
592
593// Provides access to information common to a specific member.
594type SdkMemberContext interface {
595
596 // The module context of the sdk common os variant which is creating the snapshot.
597 SdkModuleContext() ModuleContext
598
599 // The builder of the snapshot.
600 SnapshotBuilder() SnapshotBuilder
Paul Duffina551a1c2020-03-17 21:04:24 +0000601
602 // The type of the member.
603 MemberType() SdkMemberType
604
605 // The name of the member.
606 //
607 // Provided for use by sdk members to create a member specific location within the snapshot
608 // into which to copy the prebuilt files.
609 Name() string
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000610}