blob: 95924561a90b1d77f2a8039c8e9eca6e69377760 [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
305// dependent module to be automatically added to the sdk. In order for this to work the
306// SdkMemberType of the depending module must return true from
307// SdkMemberType.HasTransitiveSdkMembers.
Paul Duffinf8539922019-11-19 19:44:10 +0000308type SdkMemberTypeDependencyTag interface {
309 blueprint.DependencyTag
310
Paul Duffina7208112021-04-23 21:20:20 +0100311 // SdkMemberType returns the SdkMemberType that will be used to automatically add the child module
312 // to the sdk.
Paul Duffineee466e2021-04-27 23:17:56 +0100313 SdkMemberType(child Module) SdkMemberType
Paul Duffina7208112021-04-23 21:20:20 +0100314
315 // ExportMember determines whether a module added to the sdk through this tag will be exported
316 // from the sdk or not.
317 //
318 // An exported member is added to the sdk using its own name, e.g. if "foo" was exported from sdk
319 // "bar" then its prebuilt would be simply called "foo". A member can be added to the sdk via
320 // multiple tags and if any of those tags returns true from this method then the membe will be
321 // exported. Every module added directly to the sdk via one of the member type specific
322 // properties, e.g. java_libs, will automatically be exported.
323 //
324 // If a member is not exported then it is treated as an internal implementation detail of the
325 // sdk and so will be added with an sdk specific name. e.g. if "foo" was an internal member of sdk
326 // "bar" then its prebuilt would be called "bar_foo". Additionally its visibility will be set to
327 // "//visibility:private" so it will not be accessible from outside its Android.bp file.
328 ExportMember() bool
Paul Duffinf8539922019-11-19 19:44:10 +0000329}
330
Paul Duffincee7e662020-07-09 17:32:57 +0100331var _ SdkMemberTypeDependencyTag = (*sdkMemberDependencyTag)(nil)
332var _ ReplaceSourceWithPrebuilt = (*sdkMemberDependencyTag)(nil)
333
Paul Duffinf8539922019-11-19 19:44:10 +0000334type sdkMemberDependencyTag struct {
335 blueprint.BaseDependencyTag
336 memberType SdkMemberType
Paul Duffina7208112021-04-23 21:20:20 +0100337 export bool
Paul Duffinf8539922019-11-19 19:44:10 +0000338}
339
Paul Duffineee466e2021-04-27 23:17:56 +0100340func (t *sdkMemberDependencyTag) SdkMemberType(_ Module) SdkMemberType {
Paul Duffinf8539922019-11-19 19:44:10 +0000341 return t.memberType
342}
343
Paul Duffina7208112021-04-23 21:20:20 +0100344func (t *sdkMemberDependencyTag) ExportMember() bool {
345 return t.export
346}
347
Paul Duffincee7e662020-07-09 17:32:57 +0100348// Prevent dependencies from the sdk/module_exports onto their members from being
349// replaced with a preferred prebuilt.
350func (t *sdkMemberDependencyTag) ReplaceSourceWithPrebuilt() bool {
351 return false
352}
353
Paul Duffina7208112021-04-23 21:20:20 +0100354// DependencyTagForSdkMemberType creates an SdkMemberTypeDependencyTag that will cause any
355// dependencies added by the tag to be added to the sdk as the specified SdkMemberType and exported
356// (or not) as specified by the export parameter.
357func DependencyTagForSdkMemberType(memberType SdkMemberType, export bool) SdkMemberTypeDependencyTag {
358 return &sdkMemberDependencyTag{memberType: memberType, export: export}
Paul Duffinf8539922019-11-19 19:44:10 +0000359}
360
Paul Duffin13879572019-11-28 14:31:38 +0000361// Interface that must be implemented for every type that can be a member of an
362// sdk.
363//
364// The basic implementation should look something like this, where ModuleType is
365// the name of the module type being supported.
366//
Paul Duffin255f18e2019-12-13 11:22:16 +0000367// type moduleTypeSdkMemberType struct {
368// android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +0000369// }
370//
Paul Duffin255f18e2019-12-13 11:22:16 +0000371// func init() {
372// android.RegisterSdkMemberType(&moduleTypeSdkMemberType{
373// SdkMemberTypeBase: android.SdkMemberTypeBase{
374// PropertyName: "module_types",
375// },
376// }
Paul Duffin13879572019-11-28 14:31:38 +0000377// }
378//
379// ...methods...
380//
381type SdkMemberType interface {
Paul Duffin255f18e2019-12-13 11:22:16 +0000382 // The name of the member type property on an sdk module.
383 SdkPropertyName() string
384
Paul Duffine6029182019-12-16 17:43:48 +0000385 // True if the member type supports the sdk/sdk_snapshot, false otherwise.
386 UsableWithSdkAndSdkSnapshot() bool
387
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000388 // Return true if modules of this type can have dependencies which should be
389 // treated as if they are sdk members.
390 //
391 // Any dependency that is to be treated as a member of the sdk needs to implement
392 // SdkAware and be added with an SdkMemberTypeDependencyTag tag.
393 HasTransitiveSdkMembers() bool
394
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100395 // Return true if prebuilt host artifacts may be specific to the host OS. Only
396 // applicable to modules where HostSupported() is true. If this is true,
397 // snapshots will list each host OS variant explicitly and disable all other
398 // host OS'es.
399 IsHostOsDependent() bool
400
Martin Stjernholmcd07bce2020-03-10 22:37:59 +0000401 // Add dependencies from the SDK module to all the module variants the member
402 // type contributes to the SDK. `names` is the list of module names given in
403 // the member type property (as returned by SdkPropertyName()) in the SDK
404 // module. The exact set of variants required is determined by the SDK and its
405 // properties. The dependencies must be added with the supplied tag.
Paul Duffin13879572019-11-28 14:31:38 +0000406 //
407 // The BottomUpMutatorContext provided is for the SDK module.
408 AddDependencies(mctx BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string)
409
410 // Return true if the supplied module is an instance of this member type.
411 //
412 // This is used to check the type of each variant before added to the
413 // SdkMember. Returning false will cause an error to be logged expaining that
414 // the module is not allowed in whichever sdk property it was added.
415 IsInstance(module Module) bool
416
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000417 // Add a prebuilt module that the sdk will populate.
418 //
Paul Duffin425b0ea2020-05-06 12:41:39 +0100419 // The sdk module code generates the snapshot as follows:
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000420 //
421 // * A properties struct of type SdkMemberProperties is created for each variant and
422 // populated with information from the variant by calling PopulateFromVariant(SdkAware)
423 // on the struct.
424 //
425 // * An additional properties struct is created into which the common properties will be
426 // added.
427 //
428 // * The variant property structs are analysed to find exported (capitalized) fields which
429 // have common values. Those fields are cleared and the common value added to the common
Paul Duffin864e1b42020-05-06 10:23:19 +0100430 // properties.
431 //
432 // A field annotated with a tag of `sdk:"keep"` will be treated as if it
Paul Duffinb07fa512020-03-10 22:17:04 +0000433 // was not capitalized, i.e. not optimized for common values.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000434 //
Paul Duffin864e1b42020-05-06 10:23:19 +0100435 // A field annotated with a tag of `android:"arch_variant"` will be allowed to have
436 // values that differ by arch, fields not tagged as such must have common values across
437 // all variants.
438 //
Paul Duffinc459f892020-04-30 18:08:29 +0100439 // * Additional field tags can be specified on a field that will ignore certain values
440 // for the purpose of common value optimization. A value that is ignored must have the
441 // default value for the property type. This is to ensure that significant value are not
442 // ignored by accident. The purpose of this is to allow the snapshot generation to reflect
443 // the behavior of the runtime. e.g. if a property is ignored on the host then a property
444 // that is common for android can be treated as if it was common for android and host as
445 // the setting for host is ignored anyway.
446 // * `sdk:"ignored-on-host" - this indicates the property is ignored on the host variant.
447 //
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000448 // * The sdk module type populates the BpModule structure, creating the arch specific
449 // structure and calls AddToPropertySet(...) on the properties struct to add the member
450 // specific properties in the correct place in the structure.
451 //
Paul Duffin3a4eb502020-03-19 16:11:18 +0000452 AddPrebuiltModule(ctx SdkMemberContext, member SdkMember) BpModule
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000453
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000454 // Create a structure into which variant specific properties can be added.
455 CreateVariantPropertiesStruct() SdkMemberProperties
Paul Duffin13879572019-11-28 14:31:38 +0000456}
Paul Duffin255f18e2019-12-13 11:22:16 +0000457
Paul Duffine6029182019-12-16 17:43:48 +0000458// Base type for SdkMemberType implementations.
Paul Duffin255f18e2019-12-13 11:22:16 +0000459type SdkMemberTypeBase struct {
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000460 PropertyName string
461 SupportsSdk bool
462 TransitiveSdkMembers bool
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100463 HostOsDependent bool
Paul Duffin255f18e2019-12-13 11:22:16 +0000464}
465
466func (b *SdkMemberTypeBase) SdkPropertyName() string {
467 return b.PropertyName
468}
469
Paul Duffine6029182019-12-16 17:43:48 +0000470func (b *SdkMemberTypeBase) UsableWithSdkAndSdkSnapshot() bool {
471 return b.SupportsSdk
472}
473
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000474func (b *SdkMemberTypeBase) HasTransitiveSdkMembers() bool {
475 return b.TransitiveSdkMembers
476}
477
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100478func (b *SdkMemberTypeBase) IsHostOsDependent() bool {
479 return b.HostOsDependent
480}
481
Paul Duffin255f18e2019-12-13 11:22:16 +0000482// Encapsulates the information about registered SdkMemberTypes.
483type SdkMemberTypesRegistry struct {
484 // The list of types sorted by property name.
485 list []SdkMemberType
486
487 // The key that uniquely identifies this registry instance.
488 key OnceKey
489}
490
Paul Duffine6029182019-12-16 17:43:48 +0000491func (r *SdkMemberTypesRegistry) copyAndAppend(memberType SdkMemberType) *SdkMemberTypesRegistry {
492 oldList := r.list
Paul Duffin255f18e2019-12-13 11:22:16 +0000493
494 // Copy the slice just in case this is being read while being modified, e.g. when testing.
495 list := make([]SdkMemberType, 0, len(oldList)+1)
496 list = append(list, oldList...)
497 list = append(list, memberType)
498
499 // Sort the member types by their property name to ensure that registry order has no effect
500 // on behavior.
501 sort.Slice(list, func(i1, i2 int) bool {
502 t1 := list[i1]
503 t2 := list[i2]
504
505 return t1.SdkPropertyName() < t2.SdkPropertyName()
506 })
507
508 // Generate a key that identifies the slice of SdkMemberTypes by joining the property names
509 // from all the SdkMemberType .
510 var properties []string
511 for _, t := range list {
512 properties = append(properties, t.SdkPropertyName())
513 }
514 key := NewOnceKey(strings.Join(properties, "|"))
515
516 // Create a new registry so the pointer uniquely identifies the set of registered types.
Paul Duffine6029182019-12-16 17:43:48 +0000517 return &SdkMemberTypesRegistry{
Paul Duffin255f18e2019-12-13 11:22:16 +0000518 list: list,
519 key: key,
520 }
521}
Paul Duffine6029182019-12-16 17:43:48 +0000522
523func (r *SdkMemberTypesRegistry) RegisteredTypes() []SdkMemberType {
524 return r.list
525}
526
527func (r *SdkMemberTypesRegistry) UniqueOnceKey() OnceKey {
528 // Use the pointer to the registry as the unique key.
529 return NewCustomOnceKey(r)
530}
531
532// The set of registered SdkMemberTypes, one for sdk module and one for module_exports.
533var ModuleExportsMemberTypes = &SdkMemberTypesRegistry{}
534var SdkMemberTypes = &SdkMemberTypesRegistry{}
535
536// Register an SdkMemberType object to allow them to be used in the sdk and sdk_snapshot module
537// types.
538func RegisterSdkMemberType(memberType SdkMemberType) {
539 // All member types are usable with module_exports.
540 ModuleExportsMemberTypes = ModuleExportsMemberTypes.copyAndAppend(memberType)
541
542 // Only those that explicitly indicate it are usable with sdk.
543 if memberType.UsableWithSdkAndSdkSnapshot() {
544 SdkMemberTypes = SdkMemberTypes.copyAndAppend(memberType)
545 }
546}
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000547
548// Base structure for all implementations of SdkMemberProperties.
549//
Martin Stjernholm89238f42020-07-10 00:14:03 +0100550// Contains common properties that apply across many different member types.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000551type SdkMemberPropertiesBase struct {
Paul Duffina04c1072020-03-02 10:16:35 +0000552 // The number of unique os types supported by the member variants.
Paul Duffina551a1c2020-03-17 21:04:24 +0000553 //
554 // If a member has a variant with more than one os type then it will need to differentiate
555 // the locations of any of their prebuilt files in the snapshot by os type to prevent them
556 // from colliding. See OsPrefix().
557 //
558 // This property is the same for all variants of a member and so would be optimized away
559 // if it was not explicitly kept.
Paul Duffinb07fa512020-03-10 22:17:04 +0000560 Os_count int `sdk:"keep"`
Paul Duffina04c1072020-03-02 10:16:35 +0000561
562 // The os type for which these properties refer.
Paul Duffina551a1c2020-03-17 21:04:24 +0000563 //
564 // Provided to allow a member to differentiate between os types in the locations of their
565 // prebuilt files when it supports more than one os type.
566 //
567 // This property is the same for all os type specific variants of a member and so would be
568 // optimized away if it was not explicitly kept.
Paul Duffinb07fa512020-03-10 22:17:04 +0000569 Os OsType `sdk:"keep"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000570
571 // The setting to use for the compile_multilib property.
Martin Stjernholm89238f42020-07-10 00:14:03 +0100572 Compile_multilib string `android:"arch_variant"`
Paul Duffina04c1072020-03-02 10:16:35 +0000573}
574
575// The os prefix to use for any file paths in the sdk.
576//
577// Is an empty string if the member only provides variants for a single os type, otherwise
578// is the OsType.Name.
579func (b *SdkMemberPropertiesBase) OsPrefix() string {
580 if b.Os_count == 1 {
581 return ""
582 } else {
583 return b.Os.Name
584 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000585}
586
587func (b *SdkMemberPropertiesBase) Base() *SdkMemberPropertiesBase {
588 return b
589}
590
591// Interface to be implemented on top of a structure that contains variant specific
592// information.
593//
594// Struct fields that are capitalized are examined for common values to extract. Fields
595// that are not capitalized are assumed to be arch specific.
596type SdkMemberProperties interface {
597 // Access the base structure.
598 Base() *SdkMemberPropertiesBase
599
Paul Duffin3a4eb502020-03-19 16:11:18 +0000600 // Populate this structure with information from the variant.
601 PopulateFromVariant(ctx SdkMemberContext, variant Module)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000602
Paul Duffin3a4eb502020-03-19 16:11:18 +0000603 // Add the information from this structure to the property set.
604 AddToPropertySet(ctx SdkMemberContext, propertySet BpPropertySet)
605}
606
607// Provides access to information common to a specific member.
608type SdkMemberContext interface {
609
610 // The module context of the sdk common os variant which is creating the snapshot.
611 SdkModuleContext() ModuleContext
612
613 // The builder of the snapshot.
614 SnapshotBuilder() SnapshotBuilder
Paul Duffina551a1c2020-03-17 21:04:24 +0000615
616 // The type of the member.
617 MemberType() SdkMemberType
618
619 // The name of the member.
620 //
621 // Provided for use by sdk members to create a member specific location within the snapshot
622 // into which to copy the prebuilt files.
623 Name() string
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000624}