blob: fc0a84eb8d876abbc0c9b4421a73000f1e1dd677 [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 Duffind19f8942021-07-14 12:08:37 +010018 "fmt"
Paul Duffin255f18e2019-12-13 11:22:16 +000019 "sort"
Jiyong Parkd1063c12019-07-17 20:08:41 +090020 "strings"
21
Paul Duffin13879572019-11-28 14:31:38 +000022 "github.com/google/blueprint"
Jiyong Parkd1063c12019-07-17 20:08:41 +090023 "github.com/google/blueprint/proptools"
24)
25
Paul Duffin94289702021-09-09 15:38:32 +010026// sdkAwareWithoutModule is provided simply to improve code navigation with the IDE.
Paul Duffin50f0da42020-07-22 13:52:01 +010027type sdkAwareWithoutModule interface {
Paul Duffinb97b1572021-04-29 21:50:40 +010028 // SdkMemberComponentName will return the name to use for a component of this module based on the
29 // base name of this module.
30 //
31 // The baseName is the name returned by ModuleBase.BaseModuleName(), i.e. the name specified in
32 // the name property in the .bp file so will not include the prebuilt_ prefix.
33 //
34 // The componentNameCreator is a func for creating the name of a component from the base name of
35 // the module, e.g. it could just append ".component" to the name passed in.
36 //
37 // This is intended to be called by prebuilt modules that create component models. It is because
38 // prebuilt module base names come in a variety of different forms:
39 // * unversioned - this is the same as the source module.
40 // * internal to an sdk - this is the unversioned name prefixed by the base name of the sdk
41 // module.
42 // * versioned - this is the same as the internal with the addition of an "@<version>" suffix.
43 //
44 // While this can be called from a source module in that case it will behave the same way as the
45 // unversioned name and return the result of calling the componentNameCreator func on the supplied
46 // base name.
47 //
48 // e.g. Assuming the componentNameCreator func simply appends ".component" to the name passed in
49 // then this will work as follows:
50 // * An unversioned name of "foo" will return "foo.component".
51 // * An internal to the sdk name of "sdk_foo" will return "sdk_foo.component".
52 // * A versioned name of "sdk_foo@current" will return "sdk_foo.component@current".
53 //
54 // Note that in the latter case the ".component" suffix is added before the version. Adding it
55 // after would change the version.
56 SdkMemberComponentName(baseName string, componentNameCreator func(string) string) string
57
Jiyong Parkd1063c12019-07-17 20:08:41 +090058 sdkBase() *SdkBase
59 MakeMemberOf(sdk SdkRef)
60 IsInAnySdk() bool
Paul Duffinb9e7a3c2021-05-06 15:53:19 +010061
62 // IsVersioned determines whether the module is versioned, i.e. has a name of the form
63 // <name>@<version>
64 IsVersioned() bool
65
Jiyong Parkd1063c12019-07-17 20:08:41 +090066 ContainingSdk() SdkRef
67 MemberName() string
Jiyong Parkd1063c12019-07-17 20:08:41 +090068}
69
Paul Duffin50f0da42020-07-22 13:52:01 +010070// SdkAware is the interface that must be supported by any module to become a member of SDK or to be
71// built with SDK
72type SdkAware interface {
73 Module
74 sdkAwareWithoutModule
75}
76
Paul Duffin1938dba2022-07-26 23:53:00 +000077// minApiLevelForSdkSnapshot provides access to the min_sdk_version for MinApiLevelForSdkSnapshot
78type minApiLevelForSdkSnapshot interface {
79 MinSdkVersion(ctx EarlyModuleContext) SdkSpec
80}
81
82// MinApiLevelForSdkSnapshot returns the ApiLevel of the min_sdk_version of the supplied module.
83//
84// If the module does not provide a min_sdk_version then it defaults to 1.
85func MinApiLevelForSdkSnapshot(ctx EarlyModuleContext, module Module) ApiLevel {
86 minApiLevel := NoneApiLevel
87 if m, ok := module.(minApiLevelForSdkSnapshot); ok {
88 minApiLevel = m.MinSdkVersion(ctx).ApiLevel
89 }
90 if minApiLevel == NoneApiLevel {
91 // The default min API level is 1.
92 minApiLevel = uncheckedFinalApiLevel(1)
93 }
94 return minApiLevel
95}
96
Jiyong Parkd1063c12019-07-17 20:08:41 +090097// SdkRef refers to a version of an SDK
98type SdkRef struct {
99 Name string
100 Version string
101}
102
Jiyong Park9b409bc2019-10-11 14:59:13 +0900103// Unversioned determines if the SdkRef is referencing to the unversioned SDK module
104func (s SdkRef) Unversioned() bool {
105 return s.Version == ""
Jiyong Parkd1063c12019-07-17 20:08:41 +0900106}
107
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900108// String returns string representation of this SdkRef for debugging purpose
109func (s SdkRef) String() string {
110 if s.Name == "" {
111 return "(No Sdk)"
112 }
113 if s.Unversioned() {
114 return s.Name
115 }
116 return s.Name + string(SdkVersionSeparator) + s.Version
117}
118
Jiyong Park9b409bc2019-10-11 14:59:13 +0900119// SdkVersionSeparator is a character used to separate an sdk name and its version
120const SdkVersionSeparator = '@'
Jiyong Parkd1063c12019-07-17 20:08:41 +0900121
Jiyong Park9b409bc2019-10-11 14:59:13 +0900122// ParseSdkRef parses a `name@version` style string into a corresponding SdkRef struct
Jiyong Parkd1063c12019-07-17 20:08:41 +0900123func ParseSdkRef(ctx BaseModuleContext, str string, property string) SdkRef {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900124 tokens := strings.Split(str, string(SdkVersionSeparator))
Jiyong Parkd1063c12019-07-17 20:08:41 +0900125 if len(tokens) < 1 || len(tokens) > 2 {
Paul Duffin525a5902021-05-06 16:33:52 +0100126 ctx.PropertyErrorf(property, "%q does not follow name@version syntax", str)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900127 return SdkRef{Name: "invalid sdk name", Version: "invalid sdk version"}
128 }
129
130 name := tokens[0]
131
Jiyong Park9b409bc2019-10-11 14:59:13 +0900132 var version string
Jiyong Parkd1063c12019-07-17 20:08:41 +0900133 if len(tokens) == 2 {
134 version = tokens[1]
135 }
136
137 return SdkRef{Name: name, Version: version}
138}
139
140type SdkRefs []SdkRef
141
Jiyong Park9b409bc2019-10-11 14:59:13 +0900142// Contains tells if the given SdkRef is in this list of SdkRef's
Jiyong Parkd1063c12019-07-17 20:08:41 +0900143func (refs SdkRefs) Contains(s SdkRef) bool {
144 for _, r := range refs {
145 if r == s {
146 return true
147 }
148 }
149 return false
150}
151
152type sdkProperties struct {
153 // The SDK that this module is a member of. nil if it is not a member of any SDK
154 ContainingSdk *SdkRef `blueprint:"mutated"`
155
Jiyong Parkd1063c12019-07-17 20:08:41 +0900156 // Name of the module that this sdk member is representing
157 Sdk_member_name *string
158}
159
160// SdkBase is a struct that is expected to be included in module types to implement the SdkAware
161// interface. InitSdkAwareModule should be called to initialize this struct.
162type SdkBase struct {
163 properties sdkProperties
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000164 module SdkAware
Jiyong Parkd1063c12019-07-17 20:08:41 +0900165}
166
167func (s *SdkBase) sdkBase() *SdkBase {
168 return s
169}
170
Paul Duffinb97b1572021-04-29 21:50:40 +0100171func (s *SdkBase) SdkMemberComponentName(baseName string, componentNameCreator func(string) string) string {
172 if s.MemberName() == "" {
173 return componentNameCreator(baseName)
174 } else {
175 index := strings.LastIndex(baseName, "@")
176 unversionedName := baseName[:index]
177 unversionedComponentName := componentNameCreator(unversionedName)
178 versionSuffix := baseName[index:]
179 return unversionedComponentName + versionSuffix
180 }
181}
182
Jiyong Park9b409bc2019-10-11 14:59:13 +0900183// MakeMemberOf sets this module to be a member of a specific SDK
Jiyong Parkd1063c12019-07-17 20:08:41 +0900184func (s *SdkBase) MakeMemberOf(sdk SdkRef) {
185 s.properties.ContainingSdk = &sdk
186}
187
188// IsInAnySdk returns true if this module is a member of any SDK
189func (s *SdkBase) IsInAnySdk() bool {
190 return s.properties.ContainingSdk != nil
191}
192
Paul Duffinb9e7a3c2021-05-06 15:53:19 +0100193// IsVersioned returns true if this module is versioned.
194func (s *SdkBase) IsVersioned() bool {
195 return strings.Contains(s.module.Name(), "@")
196}
197
Jiyong Parkd1063c12019-07-17 20:08:41 +0900198// ContainingSdk returns the SDK that this module is a member of
199func (s *SdkBase) ContainingSdk() SdkRef {
200 if s.properties.ContainingSdk != nil {
201 return *s.properties.ContainingSdk
202 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900203 return SdkRef{Name: "", Version: ""}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900204}
205
Jiyong Park9b409bc2019-10-11 14:59:13 +0900206// MemberName returns the name of the module that this SDK member is overriding
Jiyong Parkd1063c12019-07-17 20:08:41 +0900207func (s *SdkBase) MemberName() string {
208 return proptools.String(s.properties.Sdk_member_name)
209}
210
Jiyong Parkd1063c12019-07-17 20:08:41 +0900211// InitSdkAwareModule initializes the SdkBase struct. This must be called by all modules including
212// SdkBase.
213func InitSdkAwareModule(m SdkAware) {
214 base := m.sdkBase()
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000215 base.module = m
Jiyong Parkd1063c12019-07-17 20:08:41 +0900216 m.AddProperties(&base.properties)
217}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000218
Paul Duffin0c2e0832021-04-28 00:39:52 +0100219// IsModuleInVersionedSdk returns true if the module is an versioned sdk.
220func IsModuleInVersionedSdk(module Module) bool {
221 if s, ok := module.(SdkAware); ok {
222 if !s.ContainingSdk().Unversioned() {
223 return true
224 }
225 }
226 return false
227}
228
Paul Duffin94289702021-09-09 15:38:32 +0100229// SnapshotBuilder provides support for generating the build rules which will build the snapshot.
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000230type SnapshotBuilder interface {
Paul Duffin94289702021-09-09 15:38:32 +0100231 // CopyToSnapshot generates a rule that will copy the src to the dest (which is a snapshot
232 // relative path) and add the dest to the zip.
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000233 CopyToSnapshot(src Path, dest string)
234
Paul Duffin94289702021-09-09 15:38:32 +0100235 // UnzipToSnapshot generates a rule that will unzip the supplied zip into the snapshot relative
236 // directory destDir.
Paul Duffin91547182019-11-12 19:39:36 +0000237 UnzipToSnapshot(zipPath Path, destDir string)
238
Paul Duffin94289702021-09-09 15:38:32 +0100239 // AddPrebuiltModule adds a new prebuilt module to the snapshot.
240 //
241 // It is intended to be called from SdkMemberType.AddPrebuiltModule which can add module type
242 // specific properties that are not variant specific. The following properties will be
243 // automatically populated before returning.
Paul Duffinb645ec82019-11-27 17:43:54 +0000244 //
245 // * name
246 // * sdk_member_name
247 // * prefer
248 //
Paul Duffin94289702021-09-09 15:38:32 +0100249 // Properties that are variant specific will be handled by SdkMemberProperties structure.
250 //
251 // Each module created by this method can be output to the generated Android.bp file in two
252 // different forms, depending on the setting of the SOONG_SDK_SNAPSHOT_VERSION build property.
253 // The two forms are:
254 // 1. A versioned Soong module that is referenced from a corresponding similarly versioned
255 // snapshot module.
256 // 2. An unversioned Soong module that.
257 //
258 // See sdk/update.go for more information.
Paul Duffin9d8d6092019-12-05 18:19:29 +0000259 AddPrebuiltModule(member SdkMember, moduleType string) BpModule
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000260
Paul Duffinc61783b2022-10-20 17:21:40 +0100261 // AddInternalModule creates a new module in the generated Android.bp file that can only be
262 // referenced by one of the other modules in the snapshot.
263 //
264 // The created module's name is constructed by concatenating the name of this member and the
265 // nameSuffix, separated by "-". It also has the visibility property set to "//visibility:private"
266 // to prevent it from being inadvertently accessed from outside the snapshot.
267 AddInternalModule(properties SdkMemberProperties, moduleType string, nameSuffix string) BpModule
268
Paul Duffin94289702021-09-09 15:38:32 +0100269 // SdkMemberReferencePropertyTag returns a property tag to use when adding a property to a
270 // BpModule that contains references to other sdk members.
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000271 //
Paul Duffin94289702021-09-09 15:38:32 +0100272 // Using this will ensure that the reference is correctly output for both versioned and
273 // unversioned prebuilts in the snapshot.
Paul Duffin13f02712020-03-06 12:30:43 +0000274 //
Paul Duffin94289702021-09-09 15:38:32 +0100275 // "required: true" means that the property must only contain references to other members of the
276 // sdk. Passing a reference to a module that is not a member of the sdk will result in a build
277 // error.
Paul Duffin13f02712020-03-06 12:30:43 +0000278 //
Paul Duffin94289702021-09-09 15:38:32 +0100279 // "required: false" means that the property can contain references to modules that are either
280 // members or not members of the sdk. If a reference is to a module that is a non member then the
281 // reference is left unchanged, i.e. it is not transformed as references to members are.
282 //
283 // The handling of the member names is dependent on whether it is an internal or exported member.
284 // An exported member is one whose name is specified in one of the member type specific
285 // properties. An internal member is one that is added due to being a part of an exported (or
286 // other internal) member and is not itself an exported member.
Paul Duffin13f02712020-03-06 12:30:43 +0000287 //
288 // Member names are handled as follows:
Paul Duffin94289702021-09-09 15:38:32 +0100289 // * When creating the unversioned form of the module the name is left unchecked unless the member
290 // is internal in which case it is transformed into an sdk specific name, i.e. by prefixing with
291 // the sdk name.
Paul Duffin13f02712020-03-06 12:30:43 +0000292 //
Paul Duffin94289702021-09-09 15:38:32 +0100293 // * When creating the versioned form of the module the name is transformed into a versioned sdk
294 // specific name, i.e. by prefixing with the sdk name and suffixing with the version.
Paul Duffin13f02712020-03-06 12:30:43 +0000295 //
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000296 // e.g.
Paul Duffin13f02712020-03-06 12:30:43 +0000297 // bpPropertySet.AddPropertyWithTag("libs", []string{"member1", "member2"}, builder.SdkMemberReferencePropertyTag(true))
298 SdkMemberReferencePropertyTag(required bool) BpPropertyTag
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000299}
300
Paul Duffin94289702021-09-09 15:38:32 +0100301// BpPropertyTag is a marker interface that can be associated with properties in a BpPropertySet to
302// provide additional information which can be used to customize their behavior.
Paul Duffin5b511a22020-01-15 14:23:52 +0000303type BpPropertyTag interface{}
304
Paul Duffin94289702021-09-09 15:38:32 +0100305// BpPropertySet is a set of properties for use in a .bp file.
Paul Duffinb645ec82019-11-27 17:43:54 +0000306type BpPropertySet interface {
Paul Duffin94289702021-09-09 15:38:32 +0100307 // AddProperty adds a property.
308 //
309 // The value can be one of the following types:
Paul Duffinb645ec82019-11-27 17:43:54 +0000310 // * string
311 // * array of the above
312 // * bool
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100313 // For these types it is an error if multiple properties with the same name
314 // are added.
315 //
316 // * pointer to a struct
Paul Duffinb645ec82019-11-27 17:43:54 +0000317 // * BpPropertySet
318 //
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100319 // A pointer to a Blueprint-style property struct is first converted into a
320 // BpPropertySet by traversing the fields and adding their values as
321 // properties in a BpPropertySet. A field with a struct value is itself
322 // converted into a BpPropertySet before adding.
323 //
324 // Adding a BpPropertySet is done as follows:
325 // * If no property with the name exists then the BpPropertySet is added
326 // directly to this property. Care must be taken to ensure that it does not
327 // introduce a cycle.
328 // * If a property exists with the name and the current value is a
329 // BpPropertySet then every property of the new BpPropertySet is added to
330 // the existing BpPropertySet.
331 // * Otherwise, if a property exists with the name then it is an error.
Paul Duffinb645ec82019-11-27 17:43:54 +0000332 AddProperty(name string, value interface{})
333
Paul Duffin94289702021-09-09 15:38:32 +0100334 // AddPropertyWithTag adds a property with an associated property tag.
Paul Duffin5b511a22020-01-15 14:23:52 +0000335 AddPropertyWithTag(name string, value interface{}, tag BpPropertyTag)
336
Paul Duffin94289702021-09-09 15:38:32 +0100337 // AddPropertySet adds a property set with the specified name and returns it so that additional
338 // properties can be added to it.
Paul Duffinb645ec82019-11-27 17:43:54 +0000339 AddPropertySet(name string) BpPropertySet
Paul Duffin0df49682021-05-07 01:10:01 +0100340
Paul Duffin94289702021-09-09 15:38:32 +0100341 // AddCommentForProperty adds a comment for the named property (or property set).
Paul Duffin0df49682021-05-07 01:10:01 +0100342 AddCommentForProperty(name, text string)
Paul Duffinb645ec82019-11-27 17:43:54 +0000343}
344
Paul Duffin94289702021-09-09 15:38:32 +0100345// BpModule represents a module definition in a .bp file.
Paul Duffinb645ec82019-11-27 17:43:54 +0000346type BpModule interface {
347 BpPropertySet
Paul Duffin0df49682021-05-07 01:10:01 +0100348
349 // ModuleType returns the module type of the module
350 ModuleType() string
351
352 // Name returns the name of the module or "" if no name has been specified.
353 Name() string
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000354}
Paul Duffin13879572019-11-28 14:31:38 +0000355
Paul Duffin51227d82021-05-18 12:54:27 +0100356// BpPrintable is a marker interface that must be implemented by any struct that is added as a
357// property value.
358type BpPrintable interface {
359 bpPrintable()
360}
361
362// BpPrintableBase must be embedded within any struct that is added as a
363// property value.
364type BpPrintableBase struct {
365}
366
367func (b BpPrintableBase) bpPrintable() {
368}
369
370var _ BpPrintable = BpPrintableBase{}
371
Paul Duffinf04033b2021-09-22 11:51:09 +0100372// sdkRegisterable defines the interface that must be implemented by objects that can be registered
373// in an sdkRegistry.
374type sdkRegisterable interface {
375 // SdkPropertyName returns the name of the corresponding property on an sdk module.
376 SdkPropertyName() string
377}
378
379// sdkRegistry provides support for registering and retrieving objects that define properties for
380// use by sdk and module_exports module types.
381type sdkRegistry struct {
382 // The list of registered objects sorted by property name.
383 list []sdkRegisterable
384}
385
386// copyAndAppend creates a new sdkRegistry that includes all the traits registered in
387// this registry plus the supplied trait.
388func (r *sdkRegistry) copyAndAppend(registerable sdkRegisterable) *sdkRegistry {
389 oldList := r.list
390
Paul Duffin581f2e52021-09-22 13:25:23 +0100391 // Make sure that list does not already contain the property. Uses a simple linear search instead
392 // of a binary search even though the list is sorted. That is because the number of items in the
393 // list is small and so not worth the overhead of a binary search.
394 found := false
395 newPropertyName := registerable.SdkPropertyName()
396 for _, r := range oldList {
397 if r.SdkPropertyName() == newPropertyName {
398 found = true
399 break
400 }
401 }
402 if found {
403 names := []string{}
404 for _, r := range oldList {
405 names = append(names, r.SdkPropertyName())
406 }
407 panic(fmt.Errorf("duplicate properties found, %q already exists in %q", newPropertyName, names))
408 }
409
Paul Duffinf04033b2021-09-22 11:51:09 +0100410 // Copy the slice just in case this is being read while being modified, e.g. when testing.
411 list := make([]sdkRegisterable, 0, len(oldList)+1)
412 list = append(list, oldList...)
413 list = append(list, registerable)
414
415 // Sort the registered objects by their property name to ensure that registry order has no effect
416 // on behavior.
417 sort.Slice(list, func(i1, i2 int) bool {
418 t1 := list[i1]
419 t2 := list[i2]
420
421 return t1.SdkPropertyName() < t2.SdkPropertyName()
422 })
423
424 // Create a new registry so the pointer uniquely identifies the set of registered types.
425 return &sdkRegistry{
426 list: list,
427 }
428}
429
430// registeredObjects returns the list of registered instances.
431func (r *sdkRegistry) registeredObjects() []sdkRegisterable {
432 return r.list
433}
434
435// uniqueOnceKey returns a key that uniquely identifies this instance and can be used with
436// OncePer.Once
437func (r *sdkRegistry) uniqueOnceKey() OnceKey {
438 // Use the pointer to the registry as the unique key. The pointer is used because it is guaranteed
439 // to uniquely identify the contained list. The list itself cannot be used as slices are not
440 // comparable. Using the pointer does mean that two separate registries with identical lists would
441 // have different keys and so cause whatever information is cached to be created multiple times.
442 // However, that is not an issue in practice as it should not occur outside tests. Constructing a
443 // string representation of the list to use instead would avoid that but is an unnecessary
444 // complication that provides no significant benefit.
445 return NewCustomOnceKey(r)
446}
447
Paul Duffind19f8942021-07-14 12:08:37 +0100448// SdkMemberTrait represents a trait that members of an sdk module can contribute to the sdk
449// snapshot.
450//
451// A trait is simply a characteristic of sdk member that is not required by default which may be
452// required for some members but not others. Traits can cause additional information to be output
453// to the sdk snapshot or replace the default information exported for a member with something else.
454// e.g.
Colin Crossd079e0b2022-08-16 10:27:33 -0700455// - By default cc libraries only export the default image variants to the SDK. However, for some
456// members it may be necessary to export specific image variants, e.g. vendor, or recovery.
457// - By default cc libraries export all the configured architecture variants except for the native
458// bridge architecture variants. However, for some members it may be necessary to export the
459// native bridge architecture variants as well.
460// - By default cc libraries export the platform variant (i.e. sdk:). However, for some members it
461// may be necessary to export the sdk variant (i.e. sdk:sdk).
Paul Duffind19f8942021-07-14 12:08:37 +0100462//
463// A sdk can request a module to provide no traits, one trait or a collection of traits. The exact
464// behavior of a trait is determined by how SdkMemberType implementations handle the traits. A trait
465// could be specific to one SdkMemberType or many. Some trait combinations could be incompatible.
466//
467// The sdk module type will create a special traits structure that contains a property for each
468// trait registered with RegisterSdkMemberTrait(). The property names are those returned from
469// SdkPropertyName(). Each property contains a list of modules that are required to have that trait.
470// e.g. something like this:
471//
Colin Crossd079e0b2022-08-16 10:27:33 -0700472// sdk {
473// name: "sdk",
474// ...
475// traits: {
476// recovery_image: ["module1", "module4", "module5"],
477// native_bridge: ["module1", "module2"],
478// native_sdk: ["module1", "module3"],
479// ...
480// },
481// ...
482// }
Paul Duffind19f8942021-07-14 12:08:37 +0100483type SdkMemberTrait interface {
484 // SdkPropertyName returns the name of the traits property on an sdk module.
485 SdkPropertyName() string
486}
487
Paul Duffinf04033b2021-09-22 11:51:09 +0100488var _ sdkRegisterable = (SdkMemberTrait)(nil)
489
Paul Duffind19f8942021-07-14 12:08:37 +0100490// SdkMemberTraitBase is the base struct that must be embedded within any type that implements
491// SdkMemberTrait.
492type SdkMemberTraitBase struct {
493 // PropertyName is the name of the property
494 PropertyName string
495}
496
497func (b *SdkMemberTraitBase) SdkPropertyName() string {
498 return b.PropertyName
499}
500
501// SdkMemberTraitSet is a set of SdkMemberTrait instances.
502type SdkMemberTraitSet interface {
503 // Empty returns true if this set is empty.
504 Empty() bool
505
506 // Contains returns true if this set contains the specified trait.
507 Contains(trait SdkMemberTrait) bool
508
509 // Subtract returns a new set containing all elements of this set except for those in the
510 // other set.
511 Subtract(other SdkMemberTraitSet) SdkMemberTraitSet
512
513 // String returns a string representation of the set and its contents.
514 String() string
515}
516
517func NewSdkMemberTraitSet(traits []SdkMemberTrait) SdkMemberTraitSet {
518 if len(traits) == 0 {
519 return EmptySdkMemberTraitSet()
520 }
521
522 m := sdkMemberTraitSet{}
523 for _, trait := range traits {
524 m[trait] = true
525 }
526 return m
527}
528
529func EmptySdkMemberTraitSet() SdkMemberTraitSet {
530 return (sdkMemberTraitSet)(nil)
531}
532
533type sdkMemberTraitSet map[SdkMemberTrait]bool
534
535var _ SdkMemberTraitSet = (sdkMemberTraitSet{})
536
537func (s sdkMemberTraitSet) Empty() bool {
538 return len(s) == 0
539}
540
541func (s sdkMemberTraitSet) Contains(trait SdkMemberTrait) bool {
542 return s[trait]
543}
544
545func (s sdkMemberTraitSet) Subtract(other SdkMemberTraitSet) SdkMemberTraitSet {
546 if other.Empty() {
547 return s
548 }
549
550 var remainder []SdkMemberTrait
551 for trait, _ := range s {
552 if !other.Contains(trait) {
553 remainder = append(remainder, trait)
554 }
555 }
556
557 return NewSdkMemberTraitSet(remainder)
558}
559
560func (s sdkMemberTraitSet) String() string {
561 list := []string{}
562 for trait, _ := range s {
563 list = append(list, trait.SdkPropertyName())
564 }
565 sort.Strings(list)
566 return fmt.Sprintf("[%s]", strings.Join(list, ","))
567}
568
Paul Duffinf04033b2021-09-22 11:51:09 +0100569var registeredSdkMemberTraits = &sdkRegistry{}
Paul Duffin30c830b2021-09-22 11:49:47 +0100570
571// RegisteredSdkMemberTraits returns a OnceKey and a sorted list of registered traits.
572//
573// The key uniquely identifies the array of traits and can be used with OncePer.Once() to cache
574// information derived from the array of traits.
575func RegisteredSdkMemberTraits() (OnceKey, []SdkMemberTrait) {
Paul Duffinf04033b2021-09-22 11:51:09 +0100576 registerables := registeredSdkMemberTraits.registeredObjects()
577 traits := make([]SdkMemberTrait, len(registerables))
578 for i, registerable := range registerables {
579 traits[i] = registerable.(SdkMemberTrait)
580 }
581 return registeredSdkMemberTraits.uniqueOnceKey(), traits
Paul Duffin30c830b2021-09-22 11:49:47 +0100582}
Paul Duffind19f8942021-07-14 12:08:37 +0100583
584// RegisterSdkMemberTrait registers an SdkMemberTrait object to allow them to be used in the
585// module_exports, module_exports_snapshot, sdk and sdk_snapshot module types.
586func RegisterSdkMemberTrait(trait SdkMemberTrait) {
Paul Duffin30c830b2021-09-22 11:49:47 +0100587 registeredSdkMemberTraits = registeredSdkMemberTraits.copyAndAppend(trait)
Paul Duffind19f8942021-07-14 12:08:37 +0100588}
589
Paul Duffin94289702021-09-09 15:38:32 +0100590// SdkMember is an individual member of the SDK.
591//
592// It includes all of the variants that the SDK depends upon.
Paul Duffin13879572019-11-28 14:31:38 +0000593type SdkMember interface {
Paul Duffin94289702021-09-09 15:38:32 +0100594 // Name returns the name of the member.
Paul Duffin13879572019-11-28 14:31:38 +0000595 Name() string
596
Paul Duffin94289702021-09-09 15:38:32 +0100597 // Variants returns all the variants of this module depended upon by the SDK.
Paul Duffin13879572019-11-28 14:31:38 +0000598 Variants() []SdkAware
599}
600
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100601// SdkMemberDependencyTag is the interface that a tag must implement in order to allow the
Paul Duffin2d3da312021-05-06 12:02:27 +0100602// dependent module to be automatically added to the sdk.
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100603type SdkMemberDependencyTag interface {
Paul Duffinf8539922019-11-19 19:44:10 +0000604 blueprint.DependencyTag
605
Paul Duffina7208112021-04-23 21:20:20 +0100606 // SdkMemberType returns the SdkMemberType that will be used to automatically add the child module
607 // to the sdk.
Paul Duffin5cca7c42021-05-26 10:16:01 +0100608 //
609 // Returning nil will prevent the module being added to the sdk.
Paul Duffineee466e2021-04-27 23:17:56 +0100610 SdkMemberType(child Module) SdkMemberType
Paul Duffina7208112021-04-23 21:20:20 +0100611
612 // ExportMember determines whether a module added to the sdk through this tag will be exported
613 // from the sdk or not.
614 //
615 // An exported member is added to the sdk using its own name, e.g. if "foo" was exported from sdk
616 // "bar" then its prebuilt would be simply called "foo". A member can be added to the sdk via
617 // multiple tags and if any of those tags returns true from this method then the membe will be
618 // exported. Every module added directly to the sdk via one of the member type specific
619 // properties, e.g. java_libs, will automatically be exported.
620 //
621 // If a member is not exported then it is treated as an internal implementation detail of the
622 // sdk and so will be added with an sdk specific name. e.g. if "foo" was an internal member of sdk
623 // "bar" then its prebuilt would be called "bar_foo". Additionally its visibility will be set to
624 // "//visibility:private" so it will not be accessible from outside its Android.bp file.
625 ExportMember() bool
Paul Duffinf8539922019-11-19 19:44:10 +0000626}
627
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100628var _ SdkMemberDependencyTag = (*sdkMemberDependencyTag)(nil)
629var _ ReplaceSourceWithPrebuilt = (*sdkMemberDependencyTag)(nil)
Paul Duffincee7e662020-07-09 17:32:57 +0100630
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100631type sdkMemberDependencyTag struct {
Paul Duffinf8539922019-11-19 19:44:10 +0000632 blueprint.BaseDependencyTag
633 memberType SdkMemberType
Paul Duffina7208112021-04-23 21:20:20 +0100634 export bool
Paul Duffinf8539922019-11-19 19:44:10 +0000635}
636
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100637func (t *sdkMemberDependencyTag) SdkMemberType(_ Module) SdkMemberType {
Paul Duffinf8539922019-11-19 19:44:10 +0000638 return t.memberType
639}
640
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100641func (t *sdkMemberDependencyTag) ExportMember() bool {
Paul Duffina7208112021-04-23 21:20:20 +0100642 return t.export
643}
644
Paul Duffin94289702021-09-09 15:38:32 +0100645// ReplaceSourceWithPrebuilt prevents dependencies from the sdk/module_exports onto their members
646// from being replaced with a preferred prebuilt.
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100647func (t *sdkMemberDependencyTag) ReplaceSourceWithPrebuilt() bool {
Paul Duffincee7e662020-07-09 17:32:57 +0100648 return false
649}
650
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100651// DependencyTagForSdkMemberType creates an SdkMemberDependencyTag that will cause any
Paul Duffina7208112021-04-23 21:20:20 +0100652// dependencies added by the tag to be added to the sdk as the specified SdkMemberType and exported
653// (or not) as specified by the export parameter.
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100654func DependencyTagForSdkMemberType(memberType SdkMemberType, export bool) SdkMemberDependencyTag {
655 return &sdkMemberDependencyTag{memberType: memberType, export: export}
Paul Duffinf8539922019-11-19 19:44:10 +0000656}
657
Paul Duffin94289702021-09-09 15:38:32 +0100658// SdkMemberType is the interface that must be implemented for every type that can be a member of an
Paul Duffin13879572019-11-28 14:31:38 +0000659// sdk.
660//
661// The basic implementation should look something like this, where ModuleType is
662// the name of the module type being supported.
663//
Colin Crossd079e0b2022-08-16 10:27:33 -0700664// type moduleTypeSdkMemberType struct {
665// android.SdkMemberTypeBase
666// }
Paul Duffin13879572019-11-28 14:31:38 +0000667//
Colin Crossd079e0b2022-08-16 10:27:33 -0700668// func init() {
669// android.RegisterSdkMemberType(&moduleTypeSdkMemberType{
670// SdkMemberTypeBase: android.SdkMemberTypeBase{
671// PropertyName: "module_types",
672// },
673// }
674// }
Paul Duffin13879572019-11-28 14:31:38 +0000675//
Colin Crossd079e0b2022-08-16 10:27:33 -0700676// ...methods...
Paul Duffin13879572019-11-28 14:31:38 +0000677type SdkMemberType interface {
Paul Duffin94289702021-09-09 15:38:32 +0100678 // SdkPropertyName returns the name of the member type property on an sdk module.
Paul Duffin255f18e2019-12-13 11:22:16 +0000679 SdkPropertyName() string
680
Paul Duffin13082052021-05-11 00:31:38 +0100681 // RequiresBpProperty returns true if this member type requires its property to be usable within
682 // an Android.bp file.
683 RequiresBpProperty() bool
684
Paul Duffinf861df72022-07-01 15:56:06 +0000685 // SupportedBuildReleases returns the string representation of a set of target build releases that
686 // support this member type.
687 SupportedBuildReleases() string
688
Paul Duffin94289702021-09-09 15:38:32 +0100689 // UsableWithSdkAndSdkSnapshot returns true if the member type supports the sdk/sdk_snapshot,
690 // false otherwise.
Paul Duffine6029182019-12-16 17:43:48 +0000691 UsableWithSdkAndSdkSnapshot() bool
692
Paul Duffin94289702021-09-09 15:38:32 +0100693 // IsHostOsDependent returns true if prebuilt host artifacts may be specific to the host OS. Only
694 // applicable to modules where HostSupported() is true. If this is true, snapshots will list each
695 // host OS variant explicitly and disable all other host OS'es.
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100696 IsHostOsDependent() bool
697
Liz Kammer96320df2022-05-12 20:40:00 -0400698 // SupportedLinkages returns the names of the linkage variants supported by this module.
699 SupportedLinkages() []string
700
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000701 // ArePrebuiltsRequired returns true if prebuilts are required in the sdk snapshot, false
702 // otherwise.
703 ArePrebuiltsRequired() bool
704
Paul Duffin94289702021-09-09 15:38:32 +0100705 // AddDependencies adds dependencies from the SDK module to all the module variants the member
706 // type contributes to the SDK. `names` is the list of module names given in the member type
707 // property (as returned by SdkPropertyName()) in the SDK module. The exact set of variants
708 // required is determined by the SDK and its properties. The dependencies must be added with the
709 // supplied tag.
Paul Duffin13879572019-11-28 14:31:38 +0000710 //
711 // The BottomUpMutatorContext provided is for the SDK module.
Paul Duffin296701e2021-07-14 10:29:36 +0100712 AddDependencies(ctx SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string)
Paul Duffin13879572019-11-28 14:31:38 +0000713
Paul Duffin94289702021-09-09 15:38:32 +0100714 // IsInstance returns true if the supplied module is an instance of this member type.
Paul Duffin13879572019-11-28 14:31:38 +0000715 //
Paul Duffin94289702021-09-09 15:38:32 +0100716 // This is used to check the type of each variant before added to the SdkMember. Returning false
717 // will cause an error to be logged explaining that the module is not allowed in whichever sdk
718 // property it was added.
Paul Duffin13879572019-11-28 14:31:38 +0000719 IsInstance(module Module) bool
720
Paul Duffin0d4ed0a2021-05-10 23:58:40 +0100721 // UsesSourceModuleTypeInSnapshot returns true when the AddPrebuiltModule() method returns a
722 // source module type.
723 UsesSourceModuleTypeInSnapshot() bool
724
Paul Duffin94289702021-09-09 15:38:32 +0100725 // AddPrebuiltModule is called to add a prebuilt module that the sdk will populate.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000726 //
Paul Duffin425b0ea2020-05-06 12:41:39 +0100727 // The sdk module code generates the snapshot as follows:
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000728 //
729 // * A properties struct of type SdkMemberProperties is created for each variant and
730 // populated with information from the variant by calling PopulateFromVariant(SdkAware)
731 // on the struct.
732 //
733 // * An additional properties struct is created into which the common properties will be
734 // added.
735 //
736 // * The variant property structs are analysed to find exported (capitalized) fields which
737 // have common values. Those fields are cleared and the common value added to the common
Paul Duffin864e1b42020-05-06 10:23:19 +0100738 // properties.
739 //
Paul Duffin02e25c82022-09-22 15:30:58 +0100740 // A field annotated with a tag of `sdk:"ignore"` will be treated as if it
741 // was not capitalized, i.e. ignored and not optimized for common values.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000742 //
Paul Duffinbfdca962022-09-22 16:21:54 +0100743 // A field annotated with a tag of `sdk:"keep"` will not be cleared even if the value is common
744 // across multiple structs. Common values will still be copied into the common property struct.
745 // So, if the same value is placed in all structs populated from variants that value would be
746 // copied into all common property structs and so be available in every instance.
747 //
Paul Duffin864e1b42020-05-06 10:23:19 +0100748 // A field annotated with a tag of `android:"arch_variant"` will be allowed to have
749 // values that differ by arch, fields not tagged as such must have common values across
750 // all variants.
751 //
Paul Duffinc459f892020-04-30 18:08:29 +0100752 // * Additional field tags can be specified on a field that will ignore certain values
753 // for the purpose of common value optimization. A value that is ignored must have the
754 // default value for the property type. This is to ensure that significant value are not
755 // ignored by accident. The purpose of this is to allow the snapshot generation to reflect
756 // the behavior of the runtime. e.g. if a property is ignored on the host then a property
757 // that is common for android can be treated as if it was common for android and host as
758 // the setting for host is ignored anyway.
759 // * `sdk:"ignored-on-host" - this indicates the property is ignored on the host variant.
760 //
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000761 // * The sdk module type populates the BpModule structure, creating the arch specific
762 // structure and calls AddToPropertySet(...) on the properties struct to add the member
763 // specific properties in the correct place in the structure.
764 //
Paul Duffin3a4eb502020-03-19 16:11:18 +0000765 AddPrebuiltModule(ctx SdkMemberContext, member SdkMember) BpModule
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000766
Paul Duffin94289702021-09-09 15:38:32 +0100767 // CreateVariantPropertiesStruct creates a structure into which variant specific properties can be
768 // added.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000769 CreateVariantPropertiesStruct() SdkMemberProperties
Paul Duffind19f8942021-07-14 12:08:37 +0100770
771 // SupportedTraits returns the set of traits supported by this member type.
772 SupportedTraits() SdkMemberTraitSet
Liz Kammer96320df2022-05-12 20:40:00 -0400773
774 // Overrides returns whether type overrides other SdkMemberType
775 Overrides(SdkMemberType) bool
Paul Duffin13879572019-11-28 14:31:38 +0000776}
Paul Duffin255f18e2019-12-13 11:22:16 +0000777
Paul Duffinf04033b2021-09-22 11:51:09 +0100778var _ sdkRegisterable = (SdkMemberType)(nil)
779
Paul Duffin296701e2021-07-14 10:29:36 +0100780// SdkDependencyContext provides access to information needed by the SdkMemberType.AddDependencies()
781// implementations.
782type SdkDependencyContext interface {
783 BottomUpMutatorContext
Paul Duffind19f8942021-07-14 12:08:37 +0100784
785 // RequiredTraits returns the set of SdkMemberTrait instances that the sdk requires the named
786 // member to provide.
787 RequiredTraits(name string) SdkMemberTraitSet
788
789 // RequiresTrait returns true if the sdk requires the member with the supplied name to provide the
790 // supplied trait.
791 RequiresTrait(name string, trait SdkMemberTrait) bool
Paul Duffin296701e2021-07-14 10:29:36 +0100792}
793
Paul Duffin94289702021-09-09 15:38:32 +0100794// SdkMemberTypeBase is the base type for SdkMemberType implementations and must be embedded in any
795// struct that implements SdkMemberType.
Paul Duffin255f18e2019-12-13 11:22:16 +0000796type SdkMemberTypeBase struct {
Paul Duffin13082052021-05-11 00:31:38 +0100797 PropertyName string
798
Liz Kammer96320df2022-05-12 20:40:00 -0400799 // Property names that this SdkMemberTypeBase can override, this is useful when a module type is a
800 // superset of another module type.
801 OverridesPropertyNames map[string]bool
802
803 // The names of linkage variants supported by this module.
804 SupportedLinkageNames []string
805
Paul Duffin13082052021-05-11 00:31:38 +0100806 // When set to true BpPropertyNotRequired indicates that the member type does not require the
807 // property to be specifiable in an Android.bp file.
808 BpPropertyNotRequired bool
809
Paul Duffinf861df72022-07-01 15:56:06 +0000810 // The name of the first targeted build release.
811 //
812 // If not specified then it is assumed to be available on all targeted build releases.
813 SupportedBuildReleaseSpecification string
814
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000815 // Set to true if this must be usable with the sdk/sdk_snapshot module types. Otherwise, it will
816 // only be usable with module_exports/module_exports_snapshots module types.
817 SupportsSdk bool
818
819 // Set to true if prebuilt host artifacts of this member may be specific to the host OS. Only
820 // applicable to modules where HostSupported() is true.
Paul Duffin2d3da312021-05-06 12:02:27 +0100821 HostOsDependent bool
Paul Duffin0d4ed0a2021-05-10 23:58:40 +0100822
823 // When set to true UseSourceModuleTypeInSnapshot indicates that the member type creates a source
824 // module type in its SdkMemberType.AddPrebuiltModule() method. That prevents the sdk snapshot
825 // code from automatically adding a prefer: true flag.
826 UseSourceModuleTypeInSnapshot bool
Paul Duffind19f8942021-07-14 12:08:37 +0100827
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000828 // Set to proptools.BoolPtr(false) if this member does not generate prebuilts but is only provided
829 // to allow the sdk to gather members from this member's dependencies. If not specified then
830 // defaults to true.
831 PrebuiltsRequired *bool
832
Paul Duffind19f8942021-07-14 12:08:37 +0100833 // The list of supported traits.
834 Traits []SdkMemberTrait
Paul Duffin255f18e2019-12-13 11:22:16 +0000835}
836
837func (b *SdkMemberTypeBase) SdkPropertyName() string {
838 return b.PropertyName
839}
840
Paul Duffin13082052021-05-11 00:31:38 +0100841func (b *SdkMemberTypeBase) RequiresBpProperty() bool {
842 return !b.BpPropertyNotRequired
843}
844
Paul Duffinf861df72022-07-01 15:56:06 +0000845func (b *SdkMemberTypeBase) SupportedBuildReleases() string {
846 return b.SupportedBuildReleaseSpecification
847}
848
Paul Duffine6029182019-12-16 17:43:48 +0000849func (b *SdkMemberTypeBase) UsableWithSdkAndSdkSnapshot() bool {
850 return b.SupportsSdk
851}
852
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100853func (b *SdkMemberTypeBase) IsHostOsDependent() bool {
854 return b.HostOsDependent
855}
856
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000857func (b *SdkMemberTypeBase) ArePrebuiltsRequired() bool {
858 return proptools.BoolDefault(b.PrebuiltsRequired, true)
859}
860
Paul Duffin0d4ed0a2021-05-10 23:58:40 +0100861func (b *SdkMemberTypeBase) UsesSourceModuleTypeInSnapshot() bool {
862 return b.UseSourceModuleTypeInSnapshot
863}
864
Paul Duffind19f8942021-07-14 12:08:37 +0100865func (b *SdkMemberTypeBase) SupportedTraits() SdkMemberTraitSet {
866 return NewSdkMemberTraitSet(b.Traits)
867}
868
Liz Kammer96320df2022-05-12 20:40:00 -0400869func (b *SdkMemberTypeBase) Overrides(other SdkMemberType) bool {
870 return b.OverridesPropertyNames[other.SdkPropertyName()]
871}
872
873func (b *SdkMemberTypeBase) SupportedLinkages() []string {
874 return b.SupportedLinkageNames
875}
876
Paul Duffin30c830b2021-09-22 11:49:47 +0100877// registeredModuleExportsMemberTypes is the set of registered SdkMemberTypes for module_exports
878// modules.
Paul Duffinf04033b2021-09-22 11:51:09 +0100879var registeredModuleExportsMemberTypes = &sdkRegistry{}
Paul Duffin62782de2021-07-14 12:05:16 +0100880
Paul Duffinf04033b2021-09-22 11:51:09 +0100881// registeredSdkMemberTypes is the set of registered registeredSdkMemberTypes for sdk modules.
882var registeredSdkMemberTypes = &sdkRegistry{}
Paul Duffin30c830b2021-09-22 11:49:47 +0100883
884// RegisteredSdkMemberTypes returns a OnceKey and a sorted list of registered types.
885//
886// If moduleExports is true then the slice of types includes all registered types that can be used
887// with the module_exports and module_exports_snapshot module types. Otherwise, the slice of types
888// only includes those registered types that can be used with the sdk and sdk_snapshot module
889// types.
890//
891// The key uniquely identifies the array of types and can be used with OncePer.Once() to cache
892// information derived from the array of types.
893func RegisteredSdkMemberTypes(moduleExports bool) (OnceKey, []SdkMemberType) {
Paul Duffinf04033b2021-09-22 11:51:09 +0100894 var registry *sdkRegistry
Paul Duffin30c830b2021-09-22 11:49:47 +0100895 if moduleExports {
896 registry = registeredModuleExportsMemberTypes
897 } else {
898 registry = registeredSdkMemberTypes
899 }
900
Paul Duffinf04033b2021-09-22 11:51:09 +0100901 registerables := registry.registeredObjects()
902 types := make([]SdkMemberType, len(registerables))
903 for i, registerable := range registerables {
904 types[i] = registerable.(SdkMemberType)
905 }
906 return registry.uniqueOnceKey(), types
Paul Duffin30c830b2021-09-22 11:49:47 +0100907}
Paul Duffine6029182019-12-16 17:43:48 +0000908
Paul Duffin94289702021-09-09 15:38:32 +0100909// RegisterSdkMemberType registers an SdkMemberType object to allow them to be used in the
910// module_exports, module_exports_snapshot and (depending on the value returned from
911// SdkMemberType.UsableWithSdkAndSdkSnapshot) the sdk and sdk_snapshot module types.
Paul Duffine6029182019-12-16 17:43:48 +0000912func RegisterSdkMemberType(memberType SdkMemberType) {
913 // All member types are usable with module_exports.
Paul Duffin30c830b2021-09-22 11:49:47 +0100914 registeredModuleExportsMemberTypes = registeredModuleExportsMemberTypes.copyAndAppend(memberType)
Paul Duffine6029182019-12-16 17:43:48 +0000915
916 // Only those that explicitly indicate it are usable with sdk.
917 if memberType.UsableWithSdkAndSdkSnapshot() {
Paul Duffin30c830b2021-09-22 11:49:47 +0100918 registeredSdkMemberTypes = registeredSdkMemberTypes.copyAndAppend(memberType)
Paul Duffine6029182019-12-16 17:43:48 +0000919 }
920}
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000921
Paul Duffin94289702021-09-09 15:38:32 +0100922// SdkMemberPropertiesBase is the base structure for all implementations of SdkMemberProperties and
923// must be embedded in any struct that implements SdkMemberProperties.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000924//
Martin Stjernholm89238f42020-07-10 00:14:03 +0100925// Contains common properties that apply across many different member types.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000926type SdkMemberPropertiesBase struct {
Paul Duffinc61783b2022-10-20 17:21:40 +0100927 // The name of the member.
928 //
929 // Ignore this property during optimization. This is needed because this property is the same for
930 // all variants of a member and so would be optimized away if it was not ignored.
931 MemberName string `sdk:"ignore"`
932
Paul Duffina04c1072020-03-02 10:16:35 +0000933 // The number of unique os types supported by the member variants.
Paul Duffina551a1c2020-03-17 21:04:24 +0000934 //
935 // If a member has a variant with more than one os type then it will need to differentiate
936 // the locations of any of their prebuilt files in the snapshot by os type to prevent them
937 // from colliding. See OsPrefix().
938 //
Paul Duffin02e25c82022-09-22 15:30:58 +0100939 // Ignore this property during optimization. This is needed because this property is the same for
940 // all variants of a member and so would be optimized away if it was not ignored.
941 Os_count int `sdk:"ignore"`
Paul Duffina04c1072020-03-02 10:16:35 +0000942
943 // The os type for which these properties refer.
Paul Duffina551a1c2020-03-17 21:04:24 +0000944 //
945 // Provided to allow a member to differentiate between os types in the locations of their
946 // prebuilt files when it supports more than one os type.
947 //
Paul Duffin02e25c82022-09-22 15:30:58 +0100948 // Ignore this property during optimization. This is needed because this property is the same for
949 // all variants of a member and so would be optimized away if it was not ignored.
950 Os OsType `sdk:"ignore"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000951
952 // The setting to use for the compile_multilib property.
Martin Stjernholm89238f42020-07-10 00:14:03 +0100953 Compile_multilib string `android:"arch_variant"`
Paul Duffina04c1072020-03-02 10:16:35 +0000954}
955
Paul Duffinc61783b2022-10-20 17:21:40 +0100956func (b *SdkMemberPropertiesBase) Name() string {
957 return b.MemberName
958}
959
Paul Duffin94289702021-09-09 15:38:32 +0100960// OsPrefix returns the os prefix to use for any file paths in the sdk.
Paul Duffina04c1072020-03-02 10:16:35 +0000961//
962// Is an empty string if the member only provides variants for a single os type, otherwise
963// is the OsType.Name.
964func (b *SdkMemberPropertiesBase) OsPrefix() string {
965 if b.Os_count == 1 {
966 return ""
967 } else {
968 return b.Os.Name
969 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000970}
971
972func (b *SdkMemberPropertiesBase) Base() *SdkMemberPropertiesBase {
973 return b
974}
975
Paul Duffin94289702021-09-09 15:38:32 +0100976// SdkMemberProperties is the interface to be implemented on top of a structure that contains
977// variant specific information.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000978//
Paul Duffin94289702021-09-09 15:38:32 +0100979// Struct fields that are capitalized are examined for common values to extract. Fields that are not
980// capitalized are assumed to be arch specific.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000981type SdkMemberProperties interface {
Paul Duffin94289702021-09-09 15:38:32 +0100982 // Base returns the base structure.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000983 Base() *SdkMemberPropertiesBase
984
Paul Duffinc61783b2022-10-20 17:21:40 +0100985 Name() string
986
Paul Duffin94289702021-09-09 15:38:32 +0100987 // PopulateFromVariant populates this structure with information from a module variant.
988 //
989 // It will typically be called once for each variant of a member module that the SDK depends upon.
Paul Duffin3a4eb502020-03-19 16:11:18 +0000990 PopulateFromVariant(ctx SdkMemberContext, variant Module)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000991
Paul Duffin94289702021-09-09 15:38:32 +0100992 // AddToPropertySet adds the information from this structure to the property set.
993 //
994 // This will be called for each instance of this structure on which the PopulateFromVariant method
995 // was called and also on a number of different instances of this structure into which properties
996 // common to one or more variants have been copied. Therefore, implementations of this must handle
997 // the case when this structure is only partially populated.
Paul Duffin3a4eb502020-03-19 16:11:18 +0000998 AddToPropertySet(ctx SdkMemberContext, propertySet BpPropertySet)
999}
1000
Paul Duffin94289702021-09-09 15:38:32 +01001001// SdkMemberContext provides access to information common to a specific member.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001002type SdkMemberContext interface {
1003
Paul Duffin94289702021-09-09 15:38:32 +01001004 // SdkModuleContext returns the module context of the sdk common os variant which is creating the
1005 // snapshot.
1006 //
1007 // This is common to all members of the sdk and is not specific to the member being processed.
1008 // If information about the member being processed needs to be obtained from this ModuleContext it
1009 // must be obtained using one of the OtherModule... methods not the Module... methods.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001010 SdkModuleContext() ModuleContext
1011
Paul Duffin94289702021-09-09 15:38:32 +01001012 // SnapshotBuilder the builder of the snapshot.
Paul Duffin3a4eb502020-03-19 16:11:18 +00001013 SnapshotBuilder() SnapshotBuilder
Paul Duffina551a1c2020-03-17 21:04:24 +00001014
Paul Duffin94289702021-09-09 15:38:32 +01001015 // MemberType returns the type of the member currently being processed.
Paul Duffina551a1c2020-03-17 21:04:24 +00001016 MemberType() SdkMemberType
1017
Paul Duffin94289702021-09-09 15:38:32 +01001018 // Name returns the name of the member currently being processed.
Paul Duffina551a1c2020-03-17 21:04:24 +00001019 //
1020 // Provided for use by sdk members to create a member specific location within the snapshot
1021 // into which to copy the prebuilt files.
1022 Name() string
Paul Duffind19f8942021-07-14 12:08:37 +01001023
1024 // RequiresTrait returns true if this member is expected to provide the specified trait.
1025 RequiresTrait(trait SdkMemberTrait) bool
Paul Duffin13648912022-07-15 13:12:35 +00001026
1027 // IsTargetBuildBeforeTiramisu return true if the target build release for which this snapshot is
1028 // being generated is before Tiramisu, i.e. S.
1029 IsTargetBuildBeforeTiramisu() bool
Paul Duffin88f2fbe2020-02-27 16:00:53 +00001030}
Paul Duffinb97b1572021-04-29 21:50:40 +01001031
1032// ExportedComponentsInfo contains information about the components that this module exports to an
1033// sdk snapshot.
1034//
1035// A component of a module is a child module that the module creates and which forms an integral
1036// part of the functionality that the creating module provides. A component module is essentially
1037// owned by its creator and is tightly coupled to the creator and other components.
1038//
1039// e.g. the child modules created by prebuilt_apis are not components because they are not tightly
1040// coupled to the prebuilt_apis module. Once they are created the prebuilt_apis ignores them. The
1041// child impl and stub library created by java_sdk_library (and corresponding import) are components
1042// because the creating module depends upon them in order to provide some of its own functionality.
1043//
1044// A component is exported if it is part of an sdk snapshot. e.g. The xml and impl child modules are
1045// components but they are not exported as they are not part of an sdk snapshot.
1046//
1047// This information is used by the sdk snapshot generation code to ensure that it does not create
1048// an sdk snapshot that contains a declaration of the component module and the module that creates
1049// it as that would result in duplicate modules when attempting to use the snapshot. e.g. a snapshot
1050// that included the java_sdk_library_import "foo" and also a java_import "foo.stubs" would fail
1051// as there would be two modules called "foo.stubs".
1052type ExportedComponentsInfo struct {
1053 // The names of the exported components.
1054 Components []string
1055}
1056
1057var ExportedComponentsInfoProvider = blueprint.NewProvider(ExportedComponentsInfo{})
Paul Duffin958806b2022-05-16 13:10:47 +00001058
1059// AdditionalSdkInfo contains additional properties to add to the generated SDK info file.
1060type AdditionalSdkInfo struct {
1061 Properties map[string]interface{}
1062}
1063
1064var AdditionalSdkInfoProvider = blueprint.NewProvider(AdditionalSdkInfo{})