blob: 93beb6e214c5e19ae526bad240ea061412388a5a [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
Paul Duffin0df49682021-05-07 01:10:01 +0100287
288 // Add comment for property (or property set).
289 AddCommentForProperty(name, text string)
Paul Duffinb645ec82019-11-27 17:43:54 +0000290}
291
292// A .bp module definition.
293type BpModule interface {
294 BpPropertySet
Paul Duffin0df49682021-05-07 01:10:01 +0100295
296 // ModuleType returns the module type of the module
297 ModuleType() string
298
299 // Name returns the name of the module or "" if no name has been specified.
300 Name() string
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000301}
Paul Duffin13879572019-11-28 14:31:38 +0000302
Paul Duffin51227d82021-05-18 12:54:27 +0100303// BpPrintable is a marker interface that must be implemented by any struct that is added as a
304// property value.
305type BpPrintable interface {
306 bpPrintable()
307}
308
309// BpPrintableBase must be embedded within any struct that is added as a
310// property value.
311type BpPrintableBase struct {
312}
313
314func (b BpPrintableBase) bpPrintable() {
315}
316
317var _ BpPrintable = BpPrintableBase{}
318
Paul Duffin13879572019-11-28 14:31:38 +0000319// An individual member of the SDK, includes all of the variants that the SDK
320// requires.
321type SdkMember interface {
322 // The name of the member.
323 Name() string
324
325 // All the variants required by the SDK.
326 Variants() []SdkAware
327}
328
Paul Duffina7208112021-04-23 21:20:20 +0100329// SdkMemberTypeDependencyTag is the interface that a tag must implement in order to allow the
Paul Duffin2d3da312021-05-06 12:02:27 +0100330// dependent module to be automatically added to the sdk.
Paul Duffinf8539922019-11-19 19:44:10 +0000331type SdkMemberTypeDependencyTag interface {
332 blueprint.DependencyTag
333
Paul Duffina7208112021-04-23 21:20:20 +0100334 // SdkMemberType returns the SdkMemberType that will be used to automatically add the child module
335 // to the sdk.
Paul Duffineee466e2021-04-27 23:17:56 +0100336 SdkMemberType(child Module) SdkMemberType
Paul Duffina7208112021-04-23 21:20:20 +0100337
338 // ExportMember determines whether a module added to the sdk through this tag will be exported
339 // from the sdk or not.
340 //
341 // An exported member is added to the sdk using its own name, e.g. if "foo" was exported from sdk
342 // "bar" then its prebuilt would be simply called "foo". A member can be added to the sdk via
343 // multiple tags and if any of those tags returns true from this method then the membe will be
344 // exported. Every module added directly to the sdk via one of the member type specific
345 // properties, e.g. java_libs, will automatically be exported.
346 //
347 // If a member is not exported then it is treated as an internal implementation detail of the
348 // sdk and so will be added with an sdk specific name. e.g. if "foo" was an internal member of sdk
349 // "bar" then its prebuilt would be called "bar_foo". Additionally its visibility will be set to
350 // "//visibility:private" so it will not be accessible from outside its Android.bp file.
351 ExportMember() bool
Paul Duffinf8539922019-11-19 19:44:10 +0000352}
353
Paul Duffincee7e662020-07-09 17:32:57 +0100354var _ SdkMemberTypeDependencyTag = (*sdkMemberDependencyTag)(nil)
355var _ ReplaceSourceWithPrebuilt = (*sdkMemberDependencyTag)(nil)
356
Paul Duffinf8539922019-11-19 19:44:10 +0000357type sdkMemberDependencyTag struct {
358 blueprint.BaseDependencyTag
359 memberType SdkMemberType
Paul Duffina7208112021-04-23 21:20:20 +0100360 export bool
Paul Duffinf8539922019-11-19 19:44:10 +0000361}
362
Paul Duffineee466e2021-04-27 23:17:56 +0100363func (t *sdkMemberDependencyTag) SdkMemberType(_ Module) SdkMemberType {
Paul Duffinf8539922019-11-19 19:44:10 +0000364 return t.memberType
365}
366
Paul Duffina7208112021-04-23 21:20:20 +0100367func (t *sdkMemberDependencyTag) ExportMember() bool {
368 return t.export
369}
370
Paul Duffincee7e662020-07-09 17:32:57 +0100371// Prevent dependencies from the sdk/module_exports onto their members from being
372// replaced with a preferred prebuilt.
373func (t *sdkMemberDependencyTag) ReplaceSourceWithPrebuilt() bool {
374 return false
375}
376
Paul Duffina7208112021-04-23 21:20:20 +0100377// DependencyTagForSdkMemberType creates an SdkMemberTypeDependencyTag that will cause any
378// dependencies added by the tag to be added to the sdk as the specified SdkMemberType and exported
379// (or not) as specified by the export parameter.
380func DependencyTagForSdkMemberType(memberType SdkMemberType, export bool) SdkMemberTypeDependencyTag {
381 return &sdkMemberDependencyTag{memberType: memberType, export: export}
Paul Duffinf8539922019-11-19 19:44:10 +0000382}
383
Paul Duffin13879572019-11-28 14:31:38 +0000384// Interface that must be implemented for every type that can be a member of an
385// sdk.
386//
387// The basic implementation should look something like this, where ModuleType is
388// the name of the module type being supported.
389//
Paul Duffin255f18e2019-12-13 11:22:16 +0000390// type moduleTypeSdkMemberType struct {
391// android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +0000392// }
393//
Paul Duffin255f18e2019-12-13 11:22:16 +0000394// func init() {
395// android.RegisterSdkMemberType(&moduleTypeSdkMemberType{
396// SdkMemberTypeBase: android.SdkMemberTypeBase{
397// PropertyName: "module_types",
398// },
399// }
Paul Duffin13879572019-11-28 14:31:38 +0000400// }
401//
402// ...methods...
403//
404type SdkMemberType interface {
Paul Duffin255f18e2019-12-13 11:22:16 +0000405 // The name of the member type property on an sdk module.
406 SdkPropertyName() string
407
Paul Duffin13082052021-05-11 00:31:38 +0100408 // RequiresBpProperty returns true if this member type requires its property to be usable within
409 // an Android.bp file.
410 RequiresBpProperty() bool
411
Paul Duffine6029182019-12-16 17:43:48 +0000412 // True if the member type supports the sdk/sdk_snapshot, false otherwise.
413 UsableWithSdkAndSdkSnapshot() bool
414
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100415 // Return true if prebuilt host artifacts may be specific to the host OS. Only
416 // applicable to modules where HostSupported() is true. If this is true,
417 // snapshots will list each host OS variant explicitly and disable all other
418 // host OS'es.
419 IsHostOsDependent() bool
420
Martin Stjernholmcd07bce2020-03-10 22:37:59 +0000421 // Add dependencies from the SDK module to all the module variants the member
422 // type contributes to the SDK. `names` is the list of module names given in
423 // the member type property (as returned by SdkPropertyName()) in the SDK
424 // module. The exact set of variants required is determined by the SDK and its
425 // properties. The dependencies must be added with the supplied tag.
Paul Duffin13879572019-11-28 14:31:38 +0000426 //
427 // The BottomUpMutatorContext provided is for the SDK module.
428 AddDependencies(mctx BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string)
429
430 // Return true if the supplied module is an instance of this member type.
431 //
432 // This is used to check the type of each variant before added to the
433 // SdkMember. Returning false will cause an error to be logged expaining that
434 // the module is not allowed in whichever sdk property it was added.
435 IsInstance(module Module) bool
436
Paul Duffin0d4ed0a2021-05-10 23:58:40 +0100437 // UsesSourceModuleTypeInSnapshot returns true when the AddPrebuiltModule() method returns a
438 // source module type.
439 UsesSourceModuleTypeInSnapshot() bool
440
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000441 // Add a prebuilt module that the sdk will populate.
442 //
Paul Duffin425b0ea2020-05-06 12:41:39 +0100443 // The sdk module code generates the snapshot as follows:
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000444 //
445 // * A properties struct of type SdkMemberProperties is created for each variant and
446 // populated with information from the variant by calling PopulateFromVariant(SdkAware)
447 // on the struct.
448 //
449 // * An additional properties struct is created into which the common properties will be
450 // added.
451 //
452 // * The variant property structs are analysed to find exported (capitalized) fields which
453 // have common values. Those fields are cleared and the common value added to the common
Paul Duffin864e1b42020-05-06 10:23:19 +0100454 // properties.
455 //
456 // A field annotated with a tag of `sdk:"keep"` will be treated as if it
Paul Duffinb07fa512020-03-10 22:17:04 +0000457 // was not capitalized, i.e. not optimized for common values.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000458 //
Paul Duffin864e1b42020-05-06 10:23:19 +0100459 // A field annotated with a tag of `android:"arch_variant"` will be allowed to have
460 // values that differ by arch, fields not tagged as such must have common values across
461 // all variants.
462 //
Paul Duffinc459f892020-04-30 18:08:29 +0100463 // * Additional field tags can be specified on a field that will ignore certain values
464 // for the purpose of common value optimization. A value that is ignored must have the
465 // default value for the property type. This is to ensure that significant value are not
466 // ignored by accident. The purpose of this is to allow the snapshot generation to reflect
467 // the behavior of the runtime. e.g. if a property is ignored on the host then a property
468 // that is common for android can be treated as if it was common for android and host as
469 // the setting for host is ignored anyway.
470 // * `sdk:"ignored-on-host" - this indicates the property is ignored on the host variant.
471 //
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000472 // * The sdk module type populates the BpModule structure, creating the arch specific
473 // structure and calls AddToPropertySet(...) on the properties struct to add the member
474 // specific properties in the correct place in the structure.
475 //
Paul Duffin3a4eb502020-03-19 16:11:18 +0000476 AddPrebuiltModule(ctx SdkMemberContext, member SdkMember) BpModule
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000477
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000478 // Create a structure into which variant specific properties can be added.
479 CreateVariantPropertiesStruct() SdkMemberProperties
Paul Duffin13879572019-11-28 14:31:38 +0000480}
Paul Duffin255f18e2019-12-13 11:22:16 +0000481
Paul Duffine6029182019-12-16 17:43:48 +0000482// Base type for SdkMemberType implementations.
Paul Duffin255f18e2019-12-13 11:22:16 +0000483type SdkMemberTypeBase struct {
Paul Duffin13082052021-05-11 00:31:38 +0100484 PropertyName string
485
486 // When set to true BpPropertyNotRequired indicates that the member type does not require the
487 // property to be specifiable in an Android.bp file.
488 BpPropertyNotRequired bool
489
Paul Duffin2d3da312021-05-06 12:02:27 +0100490 SupportsSdk bool
491 HostOsDependent bool
Paul Duffin0d4ed0a2021-05-10 23:58:40 +0100492
493 // When set to true UseSourceModuleTypeInSnapshot indicates that the member type creates a source
494 // module type in its SdkMemberType.AddPrebuiltModule() method. That prevents the sdk snapshot
495 // code from automatically adding a prefer: true flag.
496 UseSourceModuleTypeInSnapshot bool
Paul Duffin255f18e2019-12-13 11:22:16 +0000497}
498
499func (b *SdkMemberTypeBase) SdkPropertyName() string {
500 return b.PropertyName
501}
502
Paul Duffin13082052021-05-11 00:31:38 +0100503func (b *SdkMemberTypeBase) RequiresBpProperty() bool {
504 return !b.BpPropertyNotRequired
505}
506
Paul Duffine6029182019-12-16 17:43:48 +0000507func (b *SdkMemberTypeBase) UsableWithSdkAndSdkSnapshot() bool {
508 return b.SupportsSdk
509}
510
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100511func (b *SdkMemberTypeBase) IsHostOsDependent() bool {
512 return b.HostOsDependent
513}
514
Paul Duffin0d4ed0a2021-05-10 23:58:40 +0100515func (b *SdkMemberTypeBase) UsesSourceModuleTypeInSnapshot() bool {
516 return b.UseSourceModuleTypeInSnapshot
517}
518
Paul Duffin255f18e2019-12-13 11:22:16 +0000519// Encapsulates the information about registered SdkMemberTypes.
520type SdkMemberTypesRegistry struct {
521 // The list of types sorted by property name.
522 list []SdkMemberType
523
524 // The key that uniquely identifies this registry instance.
525 key OnceKey
526}
527
Paul Duffine6029182019-12-16 17:43:48 +0000528func (r *SdkMemberTypesRegistry) copyAndAppend(memberType SdkMemberType) *SdkMemberTypesRegistry {
529 oldList := r.list
Paul Duffin255f18e2019-12-13 11:22:16 +0000530
531 // Copy the slice just in case this is being read while being modified, e.g. when testing.
532 list := make([]SdkMemberType, 0, len(oldList)+1)
533 list = append(list, oldList...)
534 list = append(list, memberType)
535
536 // Sort the member types by their property name to ensure that registry order has no effect
537 // on behavior.
538 sort.Slice(list, func(i1, i2 int) bool {
539 t1 := list[i1]
540 t2 := list[i2]
541
542 return t1.SdkPropertyName() < t2.SdkPropertyName()
543 })
544
545 // Generate a key that identifies the slice of SdkMemberTypes by joining the property names
546 // from all the SdkMemberType .
547 var properties []string
548 for _, t := range list {
549 properties = append(properties, t.SdkPropertyName())
550 }
551 key := NewOnceKey(strings.Join(properties, "|"))
552
553 // Create a new registry so the pointer uniquely identifies the set of registered types.
Paul Duffine6029182019-12-16 17:43:48 +0000554 return &SdkMemberTypesRegistry{
Paul Duffin255f18e2019-12-13 11:22:16 +0000555 list: list,
556 key: key,
557 }
558}
Paul Duffine6029182019-12-16 17:43:48 +0000559
560func (r *SdkMemberTypesRegistry) RegisteredTypes() []SdkMemberType {
561 return r.list
562}
563
564func (r *SdkMemberTypesRegistry) UniqueOnceKey() OnceKey {
565 // Use the pointer to the registry as the unique key.
566 return NewCustomOnceKey(r)
567}
568
569// The set of registered SdkMemberTypes, one for sdk module and one for module_exports.
570var ModuleExportsMemberTypes = &SdkMemberTypesRegistry{}
571var SdkMemberTypes = &SdkMemberTypesRegistry{}
572
573// Register an SdkMemberType object to allow them to be used in the sdk and sdk_snapshot module
574// types.
575func RegisterSdkMemberType(memberType SdkMemberType) {
576 // All member types are usable with module_exports.
577 ModuleExportsMemberTypes = ModuleExportsMemberTypes.copyAndAppend(memberType)
578
579 // Only those that explicitly indicate it are usable with sdk.
580 if memberType.UsableWithSdkAndSdkSnapshot() {
581 SdkMemberTypes = SdkMemberTypes.copyAndAppend(memberType)
582 }
583}
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000584
585// Base structure for all implementations of SdkMemberProperties.
586//
Martin Stjernholm89238f42020-07-10 00:14:03 +0100587// Contains common properties that apply across many different member types.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000588type SdkMemberPropertiesBase struct {
Paul Duffina04c1072020-03-02 10:16:35 +0000589 // The number of unique os types supported by the member variants.
Paul Duffina551a1c2020-03-17 21:04:24 +0000590 //
591 // If a member has a variant with more than one os type then it will need to differentiate
592 // the locations of any of their prebuilt files in the snapshot by os type to prevent them
593 // from colliding. See OsPrefix().
594 //
595 // This property is the same for all variants of a member and so would be optimized away
596 // if it was not explicitly kept.
Paul Duffinb07fa512020-03-10 22:17:04 +0000597 Os_count int `sdk:"keep"`
Paul Duffina04c1072020-03-02 10:16:35 +0000598
599 // The os type for which these properties refer.
Paul Duffina551a1c2020-03-17 21:04:24 +0000600 //
601 // Provided to allow a member to differentiate between os types in the locations of their
602 // prebuilt files when it supports more than one os type.
603 //
604 // This property is the same for all os type specific variants of a member and so would be
605 // optimized away if it was not explicitly kept.
Paul Duffinb07fa512020-03-10 22:17:04 +0000606 Os OsType `sdk:"keep"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000607
608 // The setting to use for the compile_multilib property.
Martin Stjernholm89238f42020-07-10 00:14:03 +0100609 Compile_multilib string `android:"arch_variant"`
Paul Duffina04c1072020-03-02 10:16:35 +0000610}
611
612// The os prefix to use for any file paths in the sdk.
613//
614// Is an empty string if the member only provides variants for a single os type, otherwise
615// is the OsType.Name.
616func (b *SdkMemberPropertiesBase) OsPrefix() string {
617 if b.Os_count == 1 {
618 return ""
619 } else {
620 return b.Os.Name
621 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000622}
623
624func (b *SdkMemberPropertiesBase) Base() *SdkMemberPropertiesBase {
625 return b
626}
627
628// Interface to be implemented on top of a structure that contains variant specific
629// information.
630//
631// Struct fields that are capitalized are examined for common values to extract. Fields
632// that are not capitalized are assumed to be arch specific.
633type SdkMemberProperties interface {
634 // Access the base structure.
635 Base() *SdkMemberPropertiesBase
636
Paul Duffin3a4eb502020-03-19 16:11:18 +0000637 // Populate this structure with information from the variant.
638 PopulateFromVariant(ctx SdkMemberContext, variant Module)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000639
Paul Duffin3a4eb502020-03-19 16:11:18 +0000640 // Add the information from this structure to the property set.
641 AddToPropertySet(ctx SdkMemberContext, propertySet BpPropertySet)
642}
643
644// Provides access to information common to a specific member.
645type SdkMemberContext interface {
646
647 // The module context of the sdk common os variant which is creating the snapshot.
648 SdkModuleContext() ModuleContext
649
650 // The builder of the snapshot.
651 SnapshotBuilder() SnapshotBuilder
Paul Duffina551a1c2020-03-17 21:04:24 +0000652
653 // The type of the member.
654 MemberType() SdkMemberType
655
656 // The name of the member.
657 //
658 // Provided for use by sdk members to create a member specific location within the snapshot
659 // into which to copy the prebuilt files.
660 Name() string
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000661}