blob: 5f4b387c20f72bbc689220cd9cb6c3b7a49148e9 [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
Paul Duffinb97b1572021-04-29 21:50:40 +010041 // SdkMemberComponentName will return the name to use for a component of this module based on the
42 // base name of this module.
43 //
44 // The baseName is the name returned by ModuleBase.BaseModuleName(), i.e. the name specified in
45 // the name property in the .bp file so will not include the prebuilt_ prefix.
46 //
47 // The componentNameCreator is a func for creating the name of a component from the base name of
48 // the module, e.g. it could just append ".component" to the name passed in.
49 //
50 // This is intended to be called by prebuilt modules that create component models. It is because
51 // prebuilt module base names come in a variety of different forms:
52 // * unversioned - this is the same as the source module.
53 // * internal to an sdk - this is the unversioned name prefixed by the base name of the sdk
54 // module.
55 // * versioned - this is the same as the internal with the addition of an "@<version>" suffix.
56 //
57 // While this can be called from a source module in that case it will behave the same way as the
58 // unversioned name and return the result of calling the componentNameCreator func on the supplied
59 // base name.
60 //
61 // e.g. Assuming the componentNameCreator func simply appends ".component" to the name passed in
62 // then this will work as follows:
63 // * An unversioned name of "foo" will return "foo.component".
64 // * An internal to the sdk name of "sdk_foo" will return "sdk_foo.component".
65 // * A versioned name of "sdk_foo@current" will return "sdk_foo.component@current".
66 //
67 // Note that in the latter case the ".component" suffix is added before the version. Adding it
68 // after would change the version.
69 SdkMemberComponentName(baseName string, componentNameCreator func(string) string) string
70
Jiyong Parkd1063c12019-07-17 20:08:41 +090071 sdkBase() *SdkBase
72 MakeMemberOf(sdk SdkRef)
73 IsInAnySdk() bool
Paul Duffinb9e7a3c2021-05-06 15:53:19 +010074
75 // IsVersioned determines whether the module is versioned, i.e. has a name of the form
76 // <name>@<version>
77 IsVersioned() bool
78
Jiyong Parkd1063c12019-07-17 20:08:41 +090079 ContainingSdk() SdkRef
80 MemberName() string
81 BuildWithSdks(sdks SdkRefs)
Jiyong Parkd1063c12019-07-17 20:08:41 +090082}
83
Paul Duffin50f0da42020-07-22 13:52:01 +010084// SdkAware is the interface that must be supported by any module to become a member of SDK or to be
85// built with SDK
86type SdkAware interface {
87 Module
88 sdkAwareWithoutModule
89}
90
Jiyong Parkd1063c12019-07-17 20:08:41 +090091// SdkRef refers to a version of an SDK
92type SdkRef struct {
93 Name string
94 Version string
95}
96
Jiyong Park9b409bc2019-10-11 14:59:13 +090097// Unversioned determines if the SdkRef is referencing to the unversioned SDK module
98func (s SdkRef) Unversioned() bool {
99 return s.Version == ""
Jiyong Parkd1063c12019-07-17 20:08:41 +0900100}
101
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900102// String returns string representation of this SdkRef for debugging purpose
103func (s SdkRef) String() string {
104 if s.Name == "" {
105 return "(No Sdk)"
106 }
107 if s.Unversioned() {
108 return s.Name
109 }
110 return s.Name + string(SdkVersionSeparator) + s.Version
111}
112
Jiyong Park9b409bc2019-10-11 14:59:13 +0900113// SdkVersionSeparator is a character used to separate an sdk name and its version
114const SdkVersionSeparator = '@'
Jiyong Parkd1063c12019-07-17 20:08:41 +0900115
Jiyong Park9b409bc2019-10-11 14:59:13 +0900116// ParseSdkRef parses a `name@version` style string into a corresponding SdkRef struct
Jiyong Parkd1063c12019-07-17 20:08:41 +0900117func ParseSdkRef(ctx BaseModuleContext, str string, property string) SdkRef {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900118 tokens := strings.Split(str, string(SdkVersionSeparator))
Jiyong Parkd1063c12019-07-17 20:08:41 +0900119 if len(tokens) < 1 || len(tokens) > 2 {
Paul Duffin525a5902021-05-06 16:33:52 +0100120 ctx.PropertyErrorf(property, "%q does not follow name@version syntax", str)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900121 return SdkRef{Name: "invalid sdk name", Version: "invalid sdk version"}
122 }
123
124 name := tokens[0]
125
Jiyong Park9b409bc2019-10-11 14:59:13 +0900126 var version string
Jiyong Parkd1063c12019-07-17 20:08:41 +0900127 if len(tokens) == 2 {
128 version = tokens[1]
129 }
130
131 return SdkRef{Name: name, Version: version}
132}
133
134type SdkRefs []SdkRef
135
Jiyong Park9b409bc2019-10-11 14:59:13 +0900136// Contains tells if the given SdkRef is in this list of SdkRef's
Jiyong Parkd1063c12019-07-17 20:08:41 +0900137func (refs SdkRefs) Contains(s SdkRef) bool {
138 for _, r := range refs {
139 if r == s {
140 return true
141 }
142 }
143 return false
144}
145
146type sdkProperties struct {
147 // The SDK that this module is a member of. nil if it is not a member of any SDK
148 ContainingSdk *SdkRef `blueprint:"mutated"`
149
150 // The list of SDK names and versions that are used to build this module
151 RequiredSdks SdkRefs `blueprint:"mutated"`
152
153 // Name of the module that this sdk member is representing
154 Sdk_member_name *string
155}
156
157// SdkBase is a struct that is expected to be included in module types to implement the SdkAware
158// interface. InitSdkAwareModule should be called to initialize this struct.
159type SdkBase struct {
160 properties sdkProperties
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000161 module SdkAware
Jiyong Parkd1063c12019-07-17 20:08:41 +0900162}
163
164func (s *SdkBase) sdkBase() *SdkBase {
165 return s
166}
167
Paul Duffinb97b1572021-04-29 21:50:40 +0100168func (s *SdkBase) SdkMemberComponentName(baseName string, componentNameCreator func(string) string) string {
169 if s.MemberName() == "" {
170 return componentNameCreator(baseName)
171 } else {
172 index := strings.LastIndex(baseName, "@")
173 unversionedName := baseName[:index]
174 unversionedComponentName := componentNameCreator(unversionedName)
175 versionSuffix := baseName[index:]
176 return unversionedComponentName + versionSuffix
177 }
178}
179
Jiyong Park9b409bc2019-10-11 14:59:13 +0900180// MakeMemberOf sets this module to be a member of a specific SDK
Jiyong Parkd1063c12019-07-17 20:08:41 +0900181func (s *SdkBase) MakeMemberOf(sdk SdkRef) {
182 s.properties.ContainingSdk = &sdk
183}
184
185// IsInAnySdk returns true if this module is a member of any SDK
186func (s *SdkBase) IsInAnySdk() bool {
187 return s.properties.ContainingSdk != nil
188}
189
Paul Duffinb9e7a3c2021-05-06 15:53:19 +0100190// IsVersioned returns true if this module is versioned.
191func (s *SdkBase) IsVersioned() bool {
192 return strings.Contains(s.module.Name(), "@")
193}
194
Jiyong Parkd1063c12019-07-17 20:08:41 +0900195// ContainingSdk returns the SDK that this module is a member of
196func (s *SdkBase) ContainingSdk() SdkRef {
197 if s.properties.ContainingSdk != nil {
198 return *s.properties.ContainingSdk
199 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900200 return SdkRef{Name: "", Version: ""}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900201}
202
Jiyong Park9b409bc2019-10-11 14:59:13 +0900203// MemberName returns the name of the module that this SDK member is overriding
Jiyong Parkd1063c12019-07-17 20:08:41 +0900204func (s *SdkBase) MemberName() string {
205 return proptools.String(s.properties.Sdk_member_name)
206}
207
208// BuildWithSdks is used to mark that this module has to be built with the given SDK(s).
209func (s *SdkBase) BuildWithSdks(sdks SdkRefs) {
210 s.properties.RequiredSdks = sdks
211}
212
213// RequiredSdks returns the SDK(s) that this module has to be built with
214func (s *SdkBase) RequiredSdks() SdkRefs {
215 return s.properties.RequiredSdks
216}
217
218// InitSdkAwareModule initializes the SdkBase struct. This must be called by all modules including
219// SdkBase.
220func InitSdkAwareModule(m SdkAware) {
221 base := m.sdkBase()
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000222 base.module = m
Jiyong Parkd1063c12019-07-17 20:08:41 +0900223 m.AddProperties(&base.properties)
224}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000225
Paul Duffin0c2e0832021-04-28 00:39:52 +0100226// IsModuleInVersionedSdk returns true if the module is an versioned sdk.
227func IsModuleInVersionedSdk(module Module) bool {
228 if s, ok := module.(SdkAware); ok {
229 if !s.ContainingSdk().Unversioned() {
230 return true
231 }
232 }
233 return false
234}
235
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000236// Provide support for generating the build rules which will build the snapshot.
237type SnapshotBuilder interface {
238 // Copy src to the dest (which is a snapshot relative path) and add the dest
239 // to the zip
240 CopyToSnapshot(src Path, dest string)
241
Paul Duffin5c211452021-07-15 12:42:44 +0100242 // Return the path to an empty file.
243 //
244 // This can be used by sdk member types that need to create an empty file in the snapshot, simply
245 // pass the value returned from this to the CopyToSnapshot() method.
246 EmptyFile() Path
247
Paul Duffin91547182019-11-12 19:39:36 +0000248 // Unzip the supplied zip into the snapshot relative directory destDir.
249 UnzipToSnapshot(zipPath Path, destDir string)
250
Paul Duffinb645ec82019-11-27 17:43:54 +0000251 // Add a new prebuilt module to the snapshot. The returned module
252 // must be populated with the module type specific properties. The following
253 // properties will be automatically populated.
254 //
255 // * name
256 // * sdk_member_name
257 // * prefer
258 //
259 // This will result in two Soong modules being generated in the Android. One
260 // that is versioned, coupled to the snapshot version and marked as
261 // prefer=true. And one that is not versioned, not marked as prefer=true and
262 // will only be used if the equivalently named non-prebuilt module is not
263 // present.
Paul Duffin9d8d6092019-12-05 18:19:29 +0000264 AddPrebuiltModule(member SdkMember, moduleType string) BpModule
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000265
266 // The property tag to use when adding a property to a BpModule that contains
267 // references to other sdk members. Using this will ensure that the reference
268 // is correctly output for both versioned and unversioned prebuilts in the
269 // snapshot.
270 //
Paul Duffin13f02712020-03-06 12:30:43 +0000271 // "required: true" means that the property must only contain references
272 // to other members of the sdk. Passing a reference to a module that is not a
273 // member of the sdk will result in a build error.
274 //
275 // "required: false" means that the property can contain references to modules
276 // that are either members or not members of the sdk. If a reference is to a
277 // module that is a non member then the reference is left unchanged, i.e. it
278 // is not transformed as references to members are.
279 //
280 // The handling of the member names is dependent on whether it is an internal or
281 // exported member. An exported member is one whose name is specified in one of
282 // the member type specific properties. An internal member is one that is added
283 // due to being a part of an exported (or other internal) member and is not itself
284 // an exported member.
285 //
286 // Member names are handled as follows:
287 // * When creating the unversioned form of the module the name is left unchecked
288 // unless the member is internal in which case it is transformed into an sdk
289 // specific name, i.e. by prefixing with the sdk name.
290 //
291 // * When creating the versioned form of the module the name is transformed into
292 // a versioned sdk specific name, i.e. by prefixing with the sdk name and
293 // suffixing with the version.
294 //
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000295 // e.g.
Paul Duffin13f02712020-03-06 12:30:43 +0000296 // bpPropertySet.AddPropertyWithTag("libs", []string{"member1", "member2"}, builder.SdkMemberReferencePropertyTag(true))
297 SdkMemberReferencePropertyTag(required bool) BpPropertyTag
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000298}
299
Paul Duffin5b511a22020-01-15 14:23:52 +0000300type BpPropertyTag interface{}
301
Paul Duffinb645ec82019-11-27 17:43:54 +0000302// A set of properties for use in a .bp file.
303type BpPropertySet interface {
304 // Add a property, the value can be one of the following types:
305 // * string
306 // * array of the above
307 // * bool
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100308 // For these types it is an error if multiple properties with the same name
309 // are added.
310 //
311 // * pointer to a struct
Paul Duffinb645ec82019-11-27 17:43:54 +0000312 // * BpPropertySet
313 //
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100314 // A pointer to a Blueprint-style property struct is first converted into a
315 // BpPropertySet by traversing the fields and adding their values as
316 // properties in a BpPropertySet. A field with a struct value is itself
317 // converted into a BpPropertySet before adding.
318 //
319 // Adding a BpPropertySet is done as follows:
320 // * If no property with the name exists then the BpPropertySet is added
321 // directly to this property. Care must be taken to ensure that it does not
322 // introduce a cycle.
323 // * If a property exists with the name and the current value is a
324 // BpPropertySet then every property of the new BpPropertySet is added to
325 // the existing BpPropertySet.
326 // * Otherwise, if a property exists with the name then it is an error.
Paul Duffinb645ec82019-11-27 17:43:54 +0000327 AddProperty(name string, value interface{})
328
Paul Duffin5b511a22020-01-15 14:23:52 +0000329 // Add a property with an associated tag
330 AddPropertyWithTag(name string, value interface{}, tag BpPropertyTag)
331
Paul Duffinb645ec82019-11-27 17:43:54 +0000332 // Add a property set with the specified name and return so that additional
333 // properties can be added.
334 AddPropertySet(name string) BpPropertySet
Paul Duffin0df49682021-05-07 01:10:01 +0100335
336 // Add comment for property (or property set).
337 AddCommentForProperty(name, text string)
Paul Duffinb645ec82019-11-27 17:43:54 +0000338}
339
340// A .bp module definition.
341type BpModule interface {
342 BpPropertySet
Paul Duffin0df49682021-05-07 01:10:01 +0100343
344 // ModuleType returns the module type of the module
345 ModuleType() string
346
347 // Name returns the name of the module or "" if no name has been specified.
348 Name() string
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000349}
Paul Duffin13879572019-11-28 14:31:38 +0000350
Paul Duffin51227d82021-05-18 12:54:27 +0100351// BpPrintable is a marker interface that must be implemented by any struct that is added as a
352// property value.
353type BpPrintable interface {
354 bpPrintable()
355}
356
357// BpPrintableBase must be embedded within any struct that is added as a
358// property value.
359type BpPrintableBase struct {
360}
361
362func (b BpPrintableBase) bpPrintable() {
363}
364
365var _ BpPrintable = BpPrintableBase{}
366
Paul Duffin13879572019-11-28 14:31:38 +0000367// An individual member of the SDK, includes all of the variants that the SDK
368// requires.
369type SdkMember interface {
370 // The name of the member.
371 Name() string
372
373 // All the variants required by the SDK.
374 Variants() []SdkAware
375}
376
Paul Duffina7208112021-04-23 21:20:20 +0100377// SdkMemberTypeDependencyTag is the interface that a tag must implement in order to allow the
Paul Duffin2d3da312021-05-06 12:02:27 +0100378// dependent module to be automatically added to the sdk.
Paul Duffinf8539922019-11-19 19:44:10 +0000379type SdkMemberTypeDependencyTag interface {
380 blueprint.DependencyTag
381
Paul Duffina7208112021-04-23 21:20:20 +0100382 // SdkMemberType returns the SdkMemberType that will be used to automatically add the child module
383 // to the sdk.
Paul Duffin5cca7c42021-05-26 10:16:01 +0100384 //
385 // Returning nil will prevent the module being added to the sdk.
Paul Duffineee466e2021-04-27 23:17:56 +0100386 SdkMemberType(child Module) SdkMemberType
Paul Duffina7208112021-04-23 21:20:20 +0100387
388 // ExportMember determines whether a module added to the sdk through this tag will be exported
389 // from the sdk or not.
390 //
391 // An exported member is added to the sdk using its own name, e.g. if "foo" was exported from sdk
392 // "bar" then its prebuilt would be simply called "foo". A member can be added to the sdk via
393 // multiple tags and if any of those tags returns true from this method then the membe will be
394 // exported. Every module added directly to the sdk via one of the member type specific
395 // properties, e.g. java_libs, will automatically be exported.
396 //
397 // If a member is not exported then it is treated as an internal implementation detail of the
398 // sdk and so will be added with an sdk specific name. e.g. if "foo" was an internal member of sdk
399 // "bar" then its prebuilt would be called "bar_foo". Additionally its visibility will be set to
400 // "//visibility:private" so it will not be accessible from outside its Android.bp file.
401 ExportMember() bool
Paul Duffinf8539922019-11-19 19:44:10 +0000402}
403
Paul Duffincee7e662020-07-09 17:32:57 +0100404var _ SdkMemberTypeDependencyTag = (*sdkMemberDependencyTag)(nil)
405var _ ReplaceSourceWithPrebuilt = (*sdkMemberDependencyTag)(nil)
406
Paul Duffinf8539922019-11-19 19:44:10 +0000407type sdkMemberDependencyTag struct {
408 blueprint.BaseDependencyTag
409 memberType SdkMemberType
Paul Duffina7208112021-04-23 21:20:20 +0100410 export bool
Paul Duffinf8539922019-11-19 19:44:10 +0000411}
412
Paul Duffineee466e2021-04-27 23:17:56 +0100413func (t *sdkMemberDependencyTag) SdkMemberType(_ Module) SdkMemberType {
Paul Duffinf8539922019-11-19 19:44:10 +0000414 return t.memberType
415}
416
Paul Duffina7208112021-04-23 21:20:20 +0100417func (t *sdkMemberDependencyTag) ExportMember() bool {
418 return t.export
419}
420
Paul Duffincee7e662020-07-09 17:32:57 +0100421// Prevent dependencies from the sdk/module_exports onto their members from being
422// replaced with a preferred prebuilt.
423func (t *sdkMemberDependencyTag) ReplaceSourceWithPrebuilt() bool {
424 return false
425}
426
Paul Duffina7208112021-04-23 21:20:20 +0100427// DependencyTagForSdkMemberType creates an SdkMemberTypeDependencyTag that will cause any
428// dependencies added by the tag to be added to the sdk as the specified SdkMemberType and exported
429// (or not) as specified by the export parameter.
430func DependencyTagForSdkMemberType(memberType SdkMemberType, export bool) SdkMemberTypeDependencyTag {
431 return &sdkMemberDependencyTag{memberType: memberType, export: export}
Paul Duffinf8539922019-11-19 19:44:10 +0000432}
433
Paul Duffin13879572019-11-28 14:31:38 +0000434// Interface that must be implemented for every type that can be a member of an
435// sdk.
436//
437// The basic implementation should look something like this, where ModuleType is
438// the name of the module type being supported.
439//
Paul Duffin255f18e2019-12-13 11:22:16 +0000440// type moduleTypeSdkMemberType struct {
441// android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +0000442// }
443//
Paul Duffin255f18e2019-12-13 11:22:16 +0000444// func init() {
445// android.RegisterSdkMemberType(&moduleTypeSdkMemberType{
446// SdkMemberTypeBase: android.SdkMemberTypeBase{
447// PropertyName: "module_types",
448// },
449// }
Paul Duffin13879572019-11-28 14:31:38 +0000450// }
451//
452// ...methods...
453//
454type SdkMemberType interface {
Paul Duffin255f18e2019-12-13 11:22:16 +0000455 // The name of the member type property on an sdk module.
456 SdkPropertyName() string
457
Paul Duffin13082052021-05-11 00:31:38 +0100458 // RequiresBpProperty returns true if this member type requires its property to be usable within
459 // an Android.bp file.
460 RequiresBpProperty() bool
461
Paul Duffine6029182019-12-16 17:43:48 +0000462 // True if the member type supports the sdk/sdk_snapshot, false otherwise.
463 UsableWithSdkAndSdkSnapshot() bool
464
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100465 // Return true if prebuilt host artifacts may be specific to the host OS. Only
466 // applicable to modules where HostSupported() is true. If this is true,
467 // snapshots will list each host OS variant explicitly and disable all other
468 // host OS'es.
469 IsHostOsDependent() bool
470
Martin Stjernholmcd07bce2020-03-10 22:37:59 +0000471 // Add dependencies from the SDK module to all the module variants the member
472 // type contributes to the SDK. `names` is the list of module names given in
473 // the member type property (as returned by SdkPropertyName()) in the SDK
474 // module. The exact set of variants required is determined by the SDK and its
475 // properties. The dependencies must be added with the supplied tag.
Paul Duffin13879572019-11-28 14:31:38 +0000476 //
477 // The BottomUpMutatorContext provided is for the SDK module.
478 AddDependencies(mctx BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string)
479
480 // Return true if the supplied module is an instance of this member type.
481 //
482 // This is used to check the type of each variant before added to the
483 // SdkMember. Returning false will cause an error to be logged expaining that
484 // the module is not allowed in whichever sdk property it was added.
485 IsInstance(module Module) bool
486
Paul Duffin0d4ed0a2021-05-10 23:58:40 +0100487 // UsesSourceModuleTypeInSnapshot returns true when the AddPrebuiltModule() method returns a
488 // source module type.
489 UsesSourceModuleTypeInSnapshot() bool
490
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000491 // Add a prebuilt module that the sdk will populate.
492 //
Paul Duffin425b0ea2020-05-06 12:41:39 +0100493 // The sdk module code generates the snapshot as follows:
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000494 //
495 // * A properties struct of type SdkMemberProperties is created for each variant and
496 // populated with information from the variant by calling PopulateFromVariant(SdkAware)
497 // on the struct.
498 //
499 // * An additional properties struct is created into which the common properties will be
500 // added.
501 //
502 // * The variant property structs are analysed to find exported (capitalized) fields which
503 // have common values. Those fields are cleared and the common value added to the common
Paul Duffin864e1b42020-05-06 10:23:19 +0100504 // properties.
505 //
506 // A field annotated with a tag of `sdk:"keep"` will be treated as if it
Paul Duffinb07fa512020-03-10 22:17:04 +0000507 // was not capitalized, i.e. not optimized for common values.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000508 //
Paul Duffin864e1b42020-05-06 10:23:19 +0100509 // A field annotated with a tag of `android:"arch_variant"` will be allowed to have
510 // values that differ by arch, fields not tagged as such must have common values across
511 // all variants.
512 //
Paul Duffinc459f892020-04-30 18:08:29 +0100513 // * Additional field tags can be specified on a field that will ignore certain values
514 // for the purpose of common value optimization. A value that is ignored must have the
515 // default value for the property type. This is to ensure that significant value are not
516 // ignored by accident. The purpose of this is to allow the snapshot generation to reflect
517 // the behavior of the runtime. e.g. if a property is ignored on the host then a property
518 // that is common for android can be treated as if it was common for android and host as
519 // the setting for host is ignored anyway.
520 // * `sdk:"ignored-on-host" - this indicates the property is ignored on the host variant.
521 //
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000522 // * The sdk module type populates the BpModule structure, creating the arch specific
523 // structure and calls AddToPropertySet(...) on the properties struct to add the member
524 // specific properties in the correct place in the structure.
525 //
Paul Duffin3a4eb502020-03-19 16:11:18 +0000526 AddPrebuiltModule(ctx SdkMemberContext, member SdkMember) BpModule
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000527
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000528 // Create a structure into which variant specific properties can be added.
529 CreateVariantPropertiesStruct() SdkMemberProperties
Paul Duffin13879572019-11-28 14:31:38 +0000530}
Paul Duffin255f18e2019-12-13 11:22:16 +0000531
Paul Duffine6029182019-12-16 17:43:48 +0000532// Base type for SdkMemberType implementations.
Paul Duffin255f18e2019-12-13 11:22:16 +0000533type SdkMemberTypeBase struct {
Paul Duffin13082052021-05-11 00:31:38 +0100534 PropertyName string
535
536 // When set to true BpPropertyNotRequired indicates that the member type does not require the
537 // property to be specifiable in an Android.bp file.
538 BpPropertyNotRequired bool
539
Paul Duffin2d3da312021-05-06 12:02:27 +0100540 SupportsSdk bool
541 HostOsDependent bool
Paul Duffin0d4ed0a2021-05-10 23:58:40 +0100542
543 // When set to true UseSourceModuleTypeInSnapshot indicates that the member type creates a source
544 // module type in its SdkMemberType.AddPrebuiltModule() method. That prevents the sdk snapshot
545 // code from automatically adding a prefer: true flag.
546 UseSourceModuleTypeInSnapshot bool
Paul Duffin255f18e2019-12-13 11:22:16 +0000547}
548
549func (b *SdkMemberTypeBase) SdkPropertyName() string {
550 return b.PropertyName
551}
552
Paul Duffin13082052021-05-11 00:31:38 +0100553func (b *SdkMemberTypeBase) RequiresBpProperty() bool {
554 return !b.BpPropertyNotRequired
555}
556
Paul Duffine6029182019-12-16 17:43:48 +0000557func (b *SdkMemberTypeBase) UsableWithSdkAndSdkSnapshot() bool {
558 return b.SupportsSdk
559}
560
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100561func (b *SdkMemberTypeBase) IsHostOsDependent() bool {
562 return b.HostOsDependent
563}
564
Paul Duffin0d4ed0a2021-05-10 23:58:40 +0100565func (b *SdkMemberTypeBase) UsesSourceModuleTypeInSnapshot() bool {
566 return b.UseSourceModuleTypeInSnapshot
567}
568
Paul Duffin255f18e2019-12-13 11:22:16 +0000569// Encapsulates the information about registered SdkMemberTypes.
570type SdkMemberTypesRegistry struct {
571 // The list of types sorted by property name.
572 list []SdkMemberType
Paul Duffin255f18e2019-12-13 11:22:16 +0000573}
574
Paul Duffine6029182019-12-16 17:43:48 +0000575func (r *SdkMemberTypesRegistry) copyAndAppend(memberType SdkMemberType) *SdkMemberTypesRegistry {
576 oldList := r.list
Paul Duffin255f18e2019-12-13 11:22:16 +0000577
578 // Copy the slice just in case this is being read while being modified, e.g. when testing.
579 list := make([]SdkMemberType, 0, len(oldList)+1)
580 list = append(list, oldList...)
581 list = append(list, memberType)
582
583 // Sort the member types by their property name to ensure that registry order has no effect
584 // on behavior.
585 sort.Slice(list, func(i1, i2 int) bool {
586 t1 := list[i1]
587 t2 := list[i2]
588
589 return t1.SdkPropertyName() < t2.SdkPropertyName()
590 })
591
Paul Duffin255f18e2019-12-13 11:22:16 +0000592 // Create a new registry so the pointer uniquely identifies the set of registered types.
Paul Duffine6029182019-12-16 17:43:48 +0000593 return &SdkMemberTypesRegistry{
Paul Duffin255f18e2019-12-13 11:22:16 +0000594 list: list,
Paul Duffin255f18e2019-12-13 11:22:16 +0000595 }
596}
Paul Duffine6029182019-12-16 17:43:48 +0000597
598func (r *SdkMemberTypesRegistry) RegisteredTypes() []SdkMemberType {
599 return r.list
600}
601
602func (r *SdkMemberTypesRegistry) UniqueOnceKey() OnceKey {
603 // Use the pointer to the registry as the unique key.
604 return NewCustomOnceKey(r)
605}
606
607// The set of registered SdkMemberTypes, one for sdk module and one for module_exports.
608var ModuleExportsMemberTypes = &SdkMemberTypesRegistry{}
609var SdkMemberTypes = &SdkMemberTypesRegistry{}
610
611// Register an SdkMemberType object to allow them to be used in the sdk and sdk_snapshot module
612// types.
613func RegisterSdkMemberType(memberType SdkMemberType) {
614 // All member types are usable with module_exports.
615 ModuleExportsMemberTypes = ModuleExportsMemberTypes.copyAndAppend(memberType)
616
617 // Only those that explicitly indicate it are usable with sdk.
618 if memberType.UsableWithSdkAndSdkSnapshot() {
619 SdkMemberTypes = SdkMemberTypes.copyAndAppend(memberType)
620 }
621}
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000622
623// Base structure for all implementations of SdkMemberProperties.
624//
Martin Stjernholm89238f42020-07-10 00:14:03 +0100625// Contains common properties that apply across many different member types.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000626type SdkMemberPropertiesBase struct {
Paul Duffina04c1072020-03-02 10:16:35 +0000627 // The number of unique os types supported by the member variants.
Paul Duffina551a1c2020-03-17 21:04:24 +0000628 //
629 // If a member has a variant with more than one os type then it will need to differentiate
630 // the locations of any of their prebuilt files in the snapshot by os type to prevent them
631 // from colliding. See OsPrefix().
632 //
633 // This property is the same for all variants of a member and so would be optimized away
634 // if it was not explicitly kept.
Paul Duffinb07fa512020-03-10 22:17:04 +0000635 Os_count int `sdk:"keep"`
Paul Duffina04c1072020-03-02 10:16:35 +0000636
637 // The os type for which these properties refer.
Paul Duffina551a1c2020-03-17 21:04:24 +0000638 //
639 // Provided to allow a member to differentiate between os types in the locations of their
640 // prebuilt files when it supports more than one os type.
641 //
642 // This property is the same for all os type specific variants of a member and so would be
643 // optimized away if it was not explicitly kept.
Paul Duffinb07fa512020-03-10 22:17:04 +0000644 Os OsType `sdk:"keep"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000645
646 // The setting to use for the compile_multilib property.
Martin Stjernholm89238f42020-07-10 00:14:03 +0100647 Compile_multilib string `android:"arch_variant"`
Paul Duffina04c1072020-03-02 10:16:35 +0000648}
649
650// The os prefix to use for any file paths in the sdk.
651//
652// Is an empty string if the member only provides variants for a single os type, otherwise
653// is the OsType.Name.
654func (b *SdkMemberPropertiesBase) OsPrefix() string {
655 if b.Os_count == 1 {
656 return ""
657 } else {
658 return b.Os.Name
659 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000660}
661
662func (b *SdkMemberPropertiesBase) Base() *SdkMemberPropertiesBase {
663 return b
664}
665
666// Interface to be implemented on top of a structure that contains variant specific
667// information.
668//
669// Struct fields that are capitalized are examined for common values to extract. Fields
670// that are not capitalized are assumed to be arch specific.
671type SdkMemberProperties interface {
672 // Access the base structure.
673 Base() *SdkMemberPropertiesBase
674
Paul Duffin3a4eb502020-03-19 16:11:18 +0000675 // Populate this structure with information from the variant.
676 PopulateFromVariant(ctx SdkMemberContext, variant Module)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000677
Paul Duffin3a4eb502020-03-19 16:11:18 +0000678 // Add the information from this structure to the property set.
679 AddToPropertySet(ctx SdkMemberContext, propertySet BpPropertySet)
680}
681
682// Provides access to information common to a specific member.
683type SdkMemberContext interface {
684
685 // The module context of the sdk common os variant which is creating the snapshot.
686 SdkModuleContext() ModuleContext
687
688 // The builder of the snapshot.
689 SnapshotBuilder() SnapshotBuilder
Paul Duffina551a1c2020-03-17 21:04:24 +0000690
691 // The type of the member.
692 MemberType() SdkMemberType
693
694 // The name of the member.
695 //
696 // Provided for use by sdk members to create a member specific location within the snapshot
697 // into which to copy the prebuilt files.
698 Name() string
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000699}
Paul Duffinb97b1572021-04-29 21:50:40 +0100700
701// ExportedComponentsInfo contains information about the components that this module exports to an
702// sdk snapshot.
703//
704// A component of a module is a child module that the module creates and which forms an integral
705// part of the functionality that the creating module provides. A component module is essentially
706// owned by its creator and is tightly coupled to the creator and other components.
707//
708// e.g. the child modules created by prebuilt_apis are not components because they are not tightly
709// coupled to the prebuilt_apis module. Once they are created the prebuilt_apis ignores them. The
710// child impl and stub library created by java_sdk_library (and corresponding import) are components
711// because the creating module depends upon them in order to provide some of its own functionality.
712//
713// A component is exported if it is part of an sdk snapshot. e.g. The xml and impl child modules are
714// components but they are not exported as they are not part of an sdk snapshot.
715//
716// This information is used by the sdk snapshot generation code to ensure that it does not create
717// an sdk snapshot that contains a declaration of the component module and the module that creates
718// it as that would result in duplicate modules when attempting to use the snapshot. e.g. a snapshot
719// that included the java_sdk_library_import "foo" and also a java_import "foo.stubs" would fail
720// as there would be two modules called "foo.stubs".
721type ExportedComponentsInfo struct {
722 // The names of the exported components.
723 Components []string
724}
725
726var ExportedComponentsInfoProvider = blueprint.NewProvider(ExportedComponentsInfo{})