blob: 6fc1910d08c1d9b41547de8db521379ed8427db8 [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
44 ContainingSdk() SdkRef
45 MemberName() string
46 BuildWithSdks(sdks SdkRefs)
Jiyong Parkd1063c12019-07-17 20:08:41 +090047}
48
Paul Duffin50f0da42020-07-22 13:52:01 +010049// SdkAware is the interface that must be supported by any module to become a member of SDK or to be
50// built with SDK
51type SdkAware interface {
52 Module
53 sdkAwareWithoutModule
54}
55
Jiyong Parkd1063c12019-07-17 20:08:41 +090056// SdkRef refers to a version of an SDK
57type SdkRef struct {
58 Name string
59 Version string
60}
61
Jiyong Park9b409bc2019-10-11 14:59:13 +090062// Unversioned determines if the SdkRef is referencing to the unversioned SDK module
63func (s SdkRef) Unversioned() bool {
64 return s.Version == ""
Jiyong Parkd1063c12019-07-17 20:08:41 +090065}
66
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090067// String returns string representation of this SdkRef for debugging purpose
68func (s SdkRef) String() string {
69 if s.Name == "" {
70 return "(No Sdk)"
71 }
72 if s.Unversioned() {
73 return s.Name
74 }
75 return s.Name + string(SdkVersionSeparator) + s.Version
76}
77
Jiyong Park9b409bc2019-10-11 14:59:13 +090078// SdkVersionSeparator is a character used to separate an sdk name and its version
79const SdkVersionSeparator = '@'
Jiyong Parkd1063c12019-07-17 20:08:41 +090080
Jiyong Park9b409bc2019-10-11 14:59:13 +090081// ParseSdkRef parses a `name@version` style string into a corresponding SdkRef struct
Jiyong Parkd1063c12019-07-17 20:08:41 +090082func ParseSdkRef(ctx BaseModuleContext, str string, property string) SdkRef {
Jiyong Park9b409bc2019-10-11 14:59:13 +090083 tokens := strings.Split(str, string(SdkVersionSeparator))
Jiyong Parkd1063c12019-07-17 20:08:41 +090084 if len(tokens) < 1 || len(tokens) > 2 {
85 ctx.PropertyErrorf(property, "%q does not follow name#version syntax", str)
86 return SdkRef{Name: "invalid sdk name", Version: "invalid sdk version"}
87 }
88
89 name := tokens[0]
90
Jiyong Park9b409bc2019-10-11 14:59:13 +090091 var version string
Jiyong Parkd1063c12019-07-17 20:08:41 +090092 if len(tokens) == 2 {
93 version = tokens[1]
94 }
95
96 return SdkRef{Name: name, Version: version}
97}
98
99type SdkRefs []SdkRef
100
Jiyong Park9b409bc2019-10-11 14:59:13 +0900101// Contains tells if the given SdkRef is in this list of SdkRef's
Jiyong Parkd1063c12019-07-17 20:08:41 +0900102func (refs SdkRefs) Contains(s SdkRef) bool {
103 for _, r := range refs {
104 if r == s {
105 return true
106 }
107 }
108 return false
109}
110
111type sdkProperties struct {
112 // The SDK that this module is a member of. nil if it is not a member of any SDK
113 ContainingSdk *SdkRef `blueprint:"mutated"`
114
115 // The list of SDK names and versions that are used to build this module
116 RequiredSdks SdkRefs `blueprint:"mutated"`
117
118 // Name of the module that this sdk member is representing
119 Sdk_member_name *string
120}
121
122// SdkBase is a struct that is expected to be included in module types to implement the SdkAware
123// interface. InitSdkAwareModule should be called to initialize this struct.
124type SdkBase struct {
125 properties sdkProperties
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000126 module SdkAware
Jiyong Parkd1063c12019-07-17 20:08:41 +0900127}
128
129func (s *SdkBase) sdkBase() *SdkBase {
130 return s
131}
132
Jiyong Park9b409bc2019-10-11 14:59:13 +0900133// MakeMemberOf sets this module to be a member of a specific SDK
Jiyong Parkd1063c12019-07-17 20:08:41 +0900134func (s *SdkBase) MakeMemberOf(sdk SdkRef) {
135 s.properties.ContainingSdk = &sdk
136}
137
138// IsInAnySdk returns true if this module is a member of any SDK
139func (s *SdkBase) IsInAnySdk() bool {
140 return s.properties.ContainingSdk != nil
141}
142
143// ContainingSdk returns the SDK that this module is a member of
144func (s *SdkBase) ContainingSdk() SdkRef {
145 if s.properties.ContainingSdk != nil {
146 return *s.properties.ContainingSdk
147 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900148 return SdkRef{Name: "", Version: ""}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900149}
150
Jiyong Park9b409bc2019-10-11 14:59:13 +0900151// MemberName returns the name of the module that this SDK member is overriding
Jiyong Parkd1063c12019-07-17 20:08:41 +0900152func (s *SdkBase) MemberName() string {
153 return proptools.String(s.properties.Sdk_member_name)
154}
155
156// BuildWithSdks is used to mark that this module has to be built with the given SDK(s).
157func (s *SdkBase) BuildWithSdks(sdks SdkRefs) {
158 s.properties.RequiredSdks = sdks
159}
160
161// RequiredSdks returns the SDK(s) that this module has to be built with
162func (s *SdkBase) RequiredSdks() SdkRefs {
163 return s.properties.RequiredSdks
164}
165
166// InitSdkAwareModule initializes the SdkBase struct. This must be called by all modules including
167// SdkBase.
168func InitSdkAwareModule(m SdkAware) {
169 base := m.sdkBase()
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000170 base.module = m
Jiyong Parkd1063c12019-07-17 20:08:41 +0900171 m.AddProperties(&base.properties)
172}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000173
Paul Duffin0c2e0832021-04-28 00:39:52 +0100174// IsModuleInVersionedSdk returns true if the module is an versioned sdk.
175func IsModuleInVersionedSdk(module Module) bool {
176 if s, ok := module.(SdkAware); ok {
177 if !s.ContainingSdk().Unversioned() {
178 return true
179 }
180 }
181 return false
182}
183
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000184// Provide support for generating the build rules which will build the snapshot.
185type SnapshotBuilder interface {
186 // Copy src to the dest (which is a snapshot relative path) and add the dest
187 // to the zip
188 CopyToSnapshot(src Path, dest string)
189
Paul Duffin91547182019-11-12 19:39:36 +0000190 // Unzip the supplied zip into the snapshot relative directory destDir.
191 UnzipToSnapshot(zipPath Path, destDir string)
192
Paul Duffinb645ec82019-11-27 17:43:54 +0000193 // Add a new prebuilt module to the snapshot. The returned module
194 // must be populated with the module type specific properties. The following
195 // properties will be automatically populated.
196 //
197 // * name
198 // * sdk_member_name
199 // * prefer
200 //
201 // This will result in two Soong modules being generated in the Android. One
202 // that is versioned, coupled to the snapshot version and marked as
203 // prefer=true. And one that is not versioned, not marked as prefer=true and
204 // will only be used if the equivalently named non-prebuilt module is not
205 // present.
Paul Duffin9d8d6092019-12-05 18:19:29 +0000206 AddPrebuiltModule(member SdkMember, moduleType string) BpModule
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000207
208 // The property tag to use when adding a property to a BpModule that contains
209 // references to other sdk members. Using this will ensure that the reference
210 // is correctly output for both versioned and unversioned prebuilts in the
211 // snapshot.
212 //
Paul Duffin13f02712020-03-06 12:30:43 +0000213 // "required: true" means that the property must only contain references
214 // to other members of the sdk. Passing a reference to a module that is not a
215 // member of the sdk will result in a build error.
216 //
217 // "required: false" means that the property can contain references to modules
218 // that are either members or not members of the sdk. If a reference is to a
219 // module that is a non member then the reference is left unchanged, i.e. it
220 // is not transformed as references to members are.
221 //
222 // The handling of the member names is dependent on whether it is an internal or
223 // exported member. An exported member is one whose name is specified in one of
224 // the member type specific properties. An internal member is one that is added
225 // due to being a part of an exported (or other internal) member and is not itself
226 // an exported member.
227 //
228 // Member names are handled as follows:
229 // * When creating the unversioned form of the module the name is left unchecked
230 // unless the member is internal in which case it is transformed into an sdk
231 // specific name, i.e. by prefixing with the sdk name.
232 //
233 // * When creating the versioned form of the module the name is transformed into
234 // a versioned sdk specific name, i.e. by prefixing with the sdk name and
235 // suffixing with the version.
236 //
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000237 // e.g.
Paul Duffin13f02712020-03-06 12:30:43 +0000238 // bpPropertySet.AddPropertyWithTag("libs", []string{"member1", "member2"}, builder.SdkMemberReferencePropertyTag(true))
239 SdkMemberReferencePropertyTag(required bool) BpPropertyTag
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000240}
241
Paul Duffin5b511a22020-01-15 14:23:52 +0000242type BpPropertyTag interface{}
243
Paul Duffinb645ec82019-11-27 17:43:54 +0000244// A set of properties for use in a .bp file.
245type BpPropertySet interface {
246 // Add a property, the value can be one of the following types:
247 // * string
248 // * array of the above
249 // * bool
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100250 // For these types it is an error if multiple properties with the same name
251 // are added.
252 //
253 // * pointer to a struct
Paul Duffinb645ec82019-11-27 17:43:54 +0000254 // * BpPropertySet
255 //
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100256 // A pointer to a Blueprint-style property struct is first converted into a
257 // BpPropertySet by traversing the fields and adding their values as
258 // properties in a BpPropertySet. A field with a struct value is itself
259 // converted into a BpPropertySet before adding.
260 //
261 // Adding a BpPropertySet is done as follows:
262 // * If no property with the name exists then the BpPropertySet is added
263 // directly to this property. Care must be taken to ensure that it does not
264 // introduce a cycle.
265 // * If a property exists with the name and the current value is a
266 // BpPropertySet then every property of the new BpPropertySet is added to
267 // the existing BpPropertySet.
268 // * Otherwise, if a property exists with the name then it is an error.
Paul Duffinb645ec82019-11-27 17:43:54 +0000269 AddProperty(name string, value interface{})
270
Paul Duffin5b511a22020-01-15 14:23:52 +0000271 // Add a property with an associated tag
272 AddPropertyWithTag(name string, value interface{}, tag BpPropertyTag)
273
Paul Duffinb645ec82019-11-27 17:43:54 +0000274 // Add a property set with the specified name and return so that additional
275 // properties can be added.
276 AddPropertySet(name string) BpPropertySet
277}
278
279// A .bp module definition.
280type BpModule interface {
281 BpPropertySet
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000282}
Paul Duffin13879572019-11-28 14:31:38 +0000283
284// An individual member of the SDK, includes all of the variants that the SDK
285// requires.
286type SdkMember interface {
287 // The name of the member.
288 Name() string
289
290 // All the variants required by the SDK.
291 Variants() []SdkAware
292}
293
Paul Duffina7208112021-04-23 21:20:20 +0100294// SdkMemberTypeDependencyTag is the interface that a tag must implement in order to allow the
295// dependent module to be automatically added to the sdk. In order for this to work the
296// SdkMemberType of the depending module must return true from
297// SdkMemberType.HasTransitiveSdkMembers.
Paul Duffinf8539922019-11-19 19:44:10 +0000298type SdkMemberTypeDependencyTag interface {
299 blueprint.DependencyTag
300
Paul Duffina7208112021-04-23 21:20:20 +0100301 // SdkMemberType returns the SdkMemberType that will be used to automatically add the child module
302 // to the sdk.
Paul Duffineee466e2021-04-27 23:17:56 +0100303 SdkMemberType(child Module) SdkMemberType
Paul Duffina7208112021-04-23 21:20:20 +0100304
305 // ExportMember determines whether a module added to the sdk through this tag will be exported
306 // from the sdk or not.
307 //
308 // An exported member is added to the sdk using its own name, e.g. if "foo" was exported from sdk
309 // "bar" then its prebuilt would be simply called "foo". A member can be added to the sdk via
310 // multiple tags and if any of those tags returns true from this method then the membe will be
311 // exported. Every module added directly to the sdk via one of the member type specific
312 // properties, e.g. java_libs, will automatically be exported.
313 //
314 // If a member is not exported then it is treated as an internal implementation detail of the
315 // sdk and so will be added with an sdk specific name. e.g. if "foo" was an internal member of sdk
316 // "bar" then its prebuilt would be called "bar_foo". Additionally its visibility will be set to
317 // "//visibility:private" so it will not be accessible from outside its Android.bp file.
318 ExportMember() bool
Paul Duffinf8539922019-11-19 19:44:10 +0000319}
320
Paul Duffincee7e662020-07-09 17:32:57 +0100321var _ SdkMemberTypeDependencyTag = (*sdkMemberDependencyTag)(nil)
322var _ ReplaceSourceWithPrebuilt = (*sdkMemberDependencyTag)(nil)
323
Paul Duffinf8539922019-11-19 19:44:10 +0000324type sdkMemberDependencyTag struct {
325 blueprint.BaseDependencyTag
326 memberType SdkMemberType
Paul Duffina7208112021-04-23 21:20:20 +0100327 export bool
Paul Duffinf8539922019-11-19 19:44:10 +0000328}
329
Paul Duffineee466e2021-04-27 23:17:56 +0100330func (t *sdkMemberDependencyTag) SdkMemberType(_ Module) SdkMemberType {
Paul Duffinf8539922019-11-19 19:44:10 +0000331 return t.memberType
332}
333
Paul Duffina7208112021-04-23 21:20:20 +0100334func (t *sdkMemberDependencyTag) ExportMember() bool {
335 return t.export
336}
337
Paul Duffincee7e662020-07-09 17:32:57 +0100338// Prevent dependencies from the sdk/module_exports onto their members from being
339// replaced with a preferred prebuilt.
340func (t *sdkMemberDependencyTag) ReplaceSourceWithPrebuilt() bool {
341 return false
342}
343
Paul Duffina7208112021-04-23 21:20:20 +0100344// DependencyTagForSdkMemberType creates an SdkMemberTypeDependencyTag that will cause any
345// dependencies added by the tag to be added to the sdk as the specified SdkMemberType and exported
346// (or not) as specified by the export parameter.
347func DependencyTagForSdkMemberType(memberType SdkMemberType, export bool) SdkMemberTypeDependencyTag {
348 return &sdkMemberDependencyTag{memberType: memberType, export: export}
Paul Duffinf8539922019-11-19 19:44:10 +0000349}
350
Paul Duffin13879572019-11-28 14:31:38 +0000351// Interface that must be implemented for every type that can be a member of an
352// sdk.
353//
354// The basic implementation should look something like this, where ModuleType is
355// the name of the module type being supported.
356//
Paul Duffin255f18e2019-12-13 11:22:16 +0000357// type moduleTypeSdkMemberType struct {
358// android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +0000359// }
360//
Paul Duffin255f18e2019-12-13 11:22:16 +0000361// func init() {
362// android.RegisterSdkMemberType(&moduleTypeSdkMemberType{
363// SdkMemberTypeBase: android.SdkMemberTypeBase{
364// PropertyName: "module_types",
365// },
366// }
Paul Duffin13879572019-11-28 14:31:38 +0000367// }
368//
369// ...methods...
370//
371type SdkMemberType interface {
Paul Duffin255f18e2019-12-13 11:22:16 +0000372 // The name of the member type property on an sdk module.
373 SdkPropertyName() string
374
Paul Duffine6029182019-12-16 17:43:48 +0000375 // True if the member type supports the sdk/sdk_snapshot, false otherwise.
376 UsableWithSdkAndSdkSnapshot() bool
377
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000378 // Return true if modules of this type can have dependencies which should be
379 // treated as if they are sdk members.
380 //
381 // Any dependency that is to be treated as a member of the sdk needs to implement
382 // SdkAware and be added with an SdkMemberTypeDependencyTag tag.
383 HasTransitiveSdkMembers() bool
384
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100385 // Return true if prebuilt host artifacts may be specific to the host OS. Only
386 // applicable to modules where HostSupported() is true. If this is true,
387 // snapshots will list each host OS variant explicitly and disable all other
388 // host OS'es.
389 IsHostOsDependent() bool
390
Martin Stjernholmcd07bce2020-03-10 22:37:59 +0000391 // Add dependencies from the SDK module to all the module variants the member
392 // type contributes to the SDK. `names` is the list of module names given in
393 // the member type property (as returned by SdkPropertyName()) in the SDK
394 // module. The exact set of variants required is determined by the SDK and its
395 // properties. The dependencies must be added with the supplied tag.
Paul Duffin13879572019-11-28 14:31:38 +0000396 //
397 // The BottomUpMutatorContext provided is for the SDK module.
398 AddDependencies(mctx BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string)
399
400 // Return true if the supplied module is an instance of this member type.
401 //
402 // This is used to check the type of each variant before added to the
403 // SdkMember. Returning false will cause an error to be logged expaining that
404 // the module is not allowed in whichever sdk property it was added.
405 IsInstance(module Module) bool
406
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000407 // Add a prebuilt module that the sdk will populate.
408 //
Paul Duffin425b0ea2020-05-06 12:41:39 +0100409 // The sdk module code generates the snapshot as follows:
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000410 //
411 // * A properties struct of type SdkMemberProperties is created for each variant and
412 // populated with information from the variant by calling PopulateFromVariant(SdkAware)
413 // on the struct.
414 //
415 // * An additional properties struct is created into which the common properties will be
416 // added.
417 //
418 // * The variant property structs are analysed to find exported (capitalized) fields which
419 // have common values. Those fields are cleared and the common value added to the common
Paul Duffin864e1b42020-05-06 10:23:19 +0100420 // properties.
421 //
422 // A field annotated with a tag of `sdk:"keep"` will be treated as if it
Paul Duffinb07fa512020-03-10 22:17:04 +0000423 // was not capitalized, i.e. not optimized for common values.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000424 //
Paul Duffin864e1b42020-05-06 10:23:19 +0100425 // A field annotated with a tag of `android:"arch_variant"` will be allowed to have
426 // values that differ by arch, fields not tagged as such must have common values across
427 // all variants.
428 //
Paul Duffinc459f892020-04-30 18:08:29 +0100429 // * Additional field tags can be specified on a field that will ignore certain values
430 // for the purpose of common value optimization. A value that is ignored must have the
431 // default value for the property type. This is to ensure that significant value are not
432 // ignored by accident. The purpose of this is to allow the snapshot generation to reflect
433 // the behavior of the runtime. e.g. if a property is ignored on the host then a property
434 // that is common for android can be treated as if it was common for android and host as
435 // the setting for host is ignored anyway.
436 // * `sdk:"ignored-on-host" - this indicates the property is ignored on the host variant.
437 //
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000438 // * The sdk module type populates the BpModule structure, creating the arch specific
439 // structure and calls AddToPropertySet(...) on the properties struct to add the member
440 // specific properties in the correct place in the structure.
441 //
Paul Duffin3a4eb502020-03-19 16:11:18 +0000442 AddPrebuiltModule(ctx SdkMemberContext, member SdkMember) BpModule
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000443
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000444 // Create a structure into which variant specific properties can be added.
445 CreateVariantPropertiesStruct() SdkMemberProperties
Paul Duffin13879572019-11-28 14:31:38 +0000446}
Paul Duffin255f18e2019-12-13 11:22:16 +0000447
Paul Duffine6029182019-12-16 17:43:48 +0000448// Base type for SdkMemberType implementations.
Paul Duffin255f18e2019-12-13 11:22:16 +0000449type SdkMemberTypeBase struct {
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000450 PropertyName string
451 SupportsSdk bool
452 TransitiveSdkMembers bool
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100453 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
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000464func (b *SdkMemberTypeBase) HasTransitiveSdkMembers() bool {
465 return b.TransitiveSdkMembers
466}
467
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100468func (b *SdkMemberTypeBase) IsHostOsDependent() bool {
469 return b.HostOsDependent
470}
471
Paul Duffin255f18e2019-12-13 11:22:16 +0000472// Encapsulates the information about registered SdkMemberTypes.
473type SdkMemberTypesRegistry struct {
474 // The list of types sorted by property name.
475 list []SdkMemberType
476
477 // The key that uniquely identifies this registry instance.
478 key OnceKey
479}
480
Paul Duffine6029182019-12-16 17:43:48 +0000481func (r *SdkMemberTypesRegistry) copyAndAppend(memberType SdkMemberType) *SdkMemberTypesRegistry {
482 oldList := r.list
Paul Duffin255f18e2019-12-13 11:22:16 +0000483
484 // Copy the slice just in case this is being read while being modified, e.g. when testing.
485 list := make([]SdkMemberType, 0, len(oldList)+1)
486 list = append(list, oldList...)
487 list = append(list, memberType)
488
489 // Sort the member types by their property name to ensure that registry order has no effect
490 // on behavior.
491 sort.Slice(list, func(i1, i2 int) bool {
492 t1 := list[i1]
493 t2 := list[i2]
494
495 return t1.SdkPropertyName() < t2.SdkPropertyName()
496 })
497
498 // Generate a key that identifies the slice of SdkMemberTypes by joining the property names
499 // from all the SdkMemberType .
500 var properties []string
501 for _, t := range list {
502 properties = append(properties, t.SdkPropertyName())
503 }
504 key := NewOnceKey(strings.Join(properties, "|"))
505
506 // Create a new registry so the pointer uniquely identifies the set of registered types.
Paul Duffine6029182019-12-16 17:43:48 +0000507 return &SdkMemberTypesRegistry{
Paul Duffin255f18e2019-12-13 11:22:16 +0000508 list: list,
509 key: key,
510 }
511}
Paul Duffine6029182019-12-16 17:43:48 +0000512
513func (r *SdkMemberTypesRegistry) RegisteredTypes() []SdkMemberType {
514 return r.list
515}
516
517func (r *SdkMemberTypesRegistry) UniqueOnceKey() OnceKey {
518 // Use the pointer to the registry as the unique key.
519 return NewCustomOnceKey(r)
520}
521
522// The set of registered SdkMemberTypes, one for sdk module and one for module_exports.
523var ModuleExportsMemberTypes = &SdkMemberTypesRegistry{}
524var SdkMemberTypes = &SdkMemberTypesRegistry{}
525
526// Register an SdkMemberType object to allow them to be used in the sdk and sdk_snapshot module
527// types.
528func RegisterSdkMemberType(memberType SdkMemberType) {
529 // All member types are usable with module_exports.
530 ModuleExportsMemberTypes = ModuleExportsMemberTypes.copyAndAppend(memberType)
531
532 // Only those that explicitly indicate it are usable with sdk.
533 if memberType.UsableWithSdkAndSdkSnapshot() {
534 SdkMemberTypes = SdkMemberTypes.copyAndAppend(memberType)
535 }
536}
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000537
538// Base structure for all implementations of SdkMemberProperties.
539//
Martin Stjernholm89238f42020-07-10 00:14:03 +0100540// Contains common properties that apply across many different member types.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000541type SdkMemberPropertiesBase struct {
Paul Duffina04c1072020-03-02 10:16:35 +0000542 // The number of unique os types supported by the member variants.
Paul Duffina551a1c2020-03-17 21:04:24 +0000543 //
544 // If a member has a variant with more than one os type then it will need to differentiate
545 // the locations of any of their prebuilt files in the snapshot by os type to prevent them
546 // from colliding. See OsPrefix().
547 //
548 // This property is the same for all variants of a member and so would be optimized away
549 // if it was not explicitly kept.
Paul Duffinb07fa512020-03-10 22:17:04 +0000550 Os_count int `sdk:"keep"`
Paul Duffina04c1072020-03-02 10:16:35 +0000551
552 // The os type for which these properties refer.
Paul Duffina551a1c2020-03-17 21:04:24 +0000553 //
554 // Provided to allow a member to differentiate between os types in the locations of their
555 // prebuilt files when it supports more than one os type.
556 //
557 // This property is the same for all os type specific variants of a member and so would be
558 // optimized away if it was not explicitly kept.
Paul Duffinb07fa512020-03-10 22:17:04 +0000559 Os OsType `sdk:"keep"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000560
561 // The setting to use for the compile_multilib property.
Martin Stjernholm89238f42020-07-10 00:14:03 +0100562 Compile_multilib string `android:"arch_variant"`
Paul Duffina04c1072020-03-02 10:16:35 +0000563}
564
565// The os prefix to use for any file paths in the sdk.
566//
567// Is an empty string if the member only provides variants for a single os type, otherwise
568// is the OsType.Name.
569func (b *SdkMemberPropertiesBase) OsPrefix() string {
570 if b.Os_count == 1 {
571 return ""
572 } else {
573 return b.Os.Name
574 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000575}
576
577func (b *SdkMemberPropertiesBase) Base() *SdkMemberPropertiesBase {
578 return b
579}
580
581// Interface to be implemented on top of a structure that contains variant specific
582// information.
583//
584// Struct fields that are capitalized are examined for common values to extract. Fields
585// that are not capitalized are assumed to be arch specific.
586type SdkMemberProperties interface {
587 // Access the base structure.
588 Base() *SdkMemberPropertiesBase
589
Paul Duffin3a4eb502020-03-19 16:11:18 +0000590 // Populate this structure with information from the variant.
591 PopulateFromVariant(ctx SdkMemberContext, variant Module)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000592
Paul Duffin3a4eb502020-03-19 16:11:18 +0000593 // Add the information from this structure to the property set.
594 AddToPropertySet(ctx SdkMemberContext, propertySet BpPropertySet)
595}
596
597// Provides access to information common to a specific member.
598type SdkMemberContext interface {
599
600 // The module context of the sdk common os variant which is creating the snapshot.
601 SdkModuleContext() ModuleContext
602
603 // The builder of the snapshot.
604 SnapshotBuilder() SnapshotBuilder
Paul Duffina551a1c2020-03-17 21:04:24 +0000605
606 // The type of the member.
607 MemberType() SdkMemberType
608
609 // The name of the member.
610 //
611 // Provided for use by sdk members to create a member specific location within the snapshot
612 // into which to copy the prebuilt files.
613 Name() string
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000614}