blob: 42c8ffa320a07e4de69020f05300f105d7bf4688 [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 Duffina1aa7382021-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 Duffina1aa7382021-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 Duffin91547182019-11-12 19:39:36 +0000242 // Unzip the supplied zip into the snapshot relative directory destDir.
243 UnzipToSnapshot(zipPath Path, destDir string)
244
Paul Duffinb645ec82019-11-27 17:43:54 +0000245 // Add a new prebuilt module to the snapshot. The returned module
246 // must be populated with the module type specific properties. The following
247 // properties will be automatically populated.
248 //
249 // * name
250 // * sdk_member_name
251 // * prefer
252 //
253 // This will result in two Soong modules being generated in the Android. One
254 // that is versioned, coupled to the snapshot version and marked as
255 // prefer=true. And one that is not versioned, not marked as prefer=true and
256 // will only be used if the equivalently named non-prebuilt module is not
257 // present.
Paul Duffin9d8d6092019-12-05 18:19:29 +0000258 AddPrebuiltModule(member SdkMember, moduleType string) BpModule
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000259
260 // The property tag to use when adding a property to a BpModule that contains
261 // references to other sdk members. Using this will ensure that the reference
262 // is correctly output for both versioned and unversioned prebuilts in the
263 // snapshot.
264 //
Paul Duffin13f02712020-03-06 12:30:43 +0000265 // "required: true" means that the property must only contain references
266 // to other members of the sdk. Passing a reference to a module that is not a
267 // member of the sdk will result in a build error.
268 //
269 // "required: false" means that the property can contain references to modules
270 // that are either members or not members of the sdk. If a reference is to a
271 // module that is a non member then the reference is left unchanged, i.e. it
272 // is not transformed as references to members are.
273 //
274 // The handling of the member names is dependent on whether it is an internal or
275 // exported member. An exported member is one whose name is specified in one of
276 // the member type specific properties. An internal member is one that is added
277 // due to being a part of an exported (or other internal) member and is not itself
278 // an exported member.
279 //
280 // Member names are handled as follows:
281 // * When creating the unversioned form of the module the name is left unchecked
282 // unless the member is internal in which case it is transformed into an sdk
283 // specific name, i.e. by prefixing with the sdk name.
284 //
285 // * When creating the versioned form of the module the name is transformed into
286 // a versioned sdk specific name, i.e. by prefixing with the sdk name and
287 // suffixing with the version.
288 //
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000289 // e.g.
Paul Duffin13f02712020-03-06 12:30:43 +0000290 // bpPropertySet.AddPropertyWithTag("libs", []string{"member1", "member2"}, builder.SdkMemberReferencePropertyTag(true))
291 SdkMemberReferencePropertyTag(required bool) BpPropertyTag
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000292}
293
Paul Duffin5b511a22020-01-15 14:23:52 +0000294type BpPropertyTag interface{}
295
Paul Duffinb645ec82019-11-27 17:43:54 +0000296// A set of properties for use in a .bp file.
297type BpPropertySet interface {
298 // Add a property, the value can be one of the following types:
299 // * string
300 // * array of the above
301 // * bool
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100302 // For these types it is an error if multiple properties with the same name
303 // are added.
304 //
305 // * pointer to a struct
Paul Duffinb645ec82019-11-27 17:43:54 +0000306 // * BpPropertySet
307 //
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100308 // A pointer to a Blueprint-style property struct is first converted into a
309 // BpPropertySet by traversing the fields and adding their values as
310 // properties in a BpPropertySet. A field with a struct value is itself
311 // converted into a BpPropertySet before adding.
312 //
313 // Adding a BpPropertySet is done as follows:
314 // * If no property with the name exists then the BpPropertySet is added
315 // directly to this property. Care must be taken to ensure that it does not
316 // introduce a cycle.
317 // * If a property exists with the name and the current value is a
318 // BpPropertySet then every property of the new BpPropertySet is added to
319 // the existing BpPropertySet.
320 // * Otherwise, if a property exists with the name then it is an error.
Paul Duffinb645ec82019-11-27 17:43:54 +0000321 AddProperty(name string, value interface{})
322
Paul Duffin5b511a22020-01-15 14:23:52 +0000323 // Add a property with an associated tag
324 AddPropertyWithTag(name string, value interface{}, tag BpPropertyTag)
325
Paul Duffinb645ec82019-11-27 17:43:54 +0000326 // Add a property set with the specified name and return so that additional
327 // properties can be added.
328 AddPropertySet(name string) BpPropertySet
Paul Duffin0df49682021-05-07 01:10:01 +0100329
330 // Add comment for property (or property set).
331 AddCommentForProperty(name, text string)
Paul Duffinb645ec82019-11-27 17:43:54 +0000332}
333
334// A .bp module definition.
335type BpModule interface {
336 BpPropertySet
Paul Duffin0df49682021-05-07 01:10:01 +0100337
338 // ModuleType returns the module type of the module
339 ModuleType() string
340
341 // Name returns the name of the module or "" if no name has been specified.
342 Name() string
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000343}
Paul Duffin13879572019-11-28 14:31:38 +0000344
345// An individual member of the SDK, includes all of the variants that the SDK
346// requires.
347type SdkMember interface {
348 // The name of the member.
349 Name() string
350
351 // All the variants required by the SDK.
352 Variants() []SdkAware
353}
354
Paul Duffina7208112021-04-23 21:20:20 +0100355// SdkMemberTypeDependencyTag is the interface that a tag must implement in order to allow the
Paul Duffin2d3da312021-05-06 12:02:27 +0100356// dependent module to be automatically added to the sdk.
Paul Duffinf8539922019-11-19 19:44:10 +0000357type SdkMemberTypeDependencyTag interface {
358 blueprint.DependencyTag
359
Paul Duffina7208112021-04-23 21:20:20 +0100360 // SdkMemberType returns the SdkMemberType that will be used to automatically add the child module
361 // to the sdk.
Paul Duffinb3821fe2021-05-26 10:16:01 +0100362 //
363 // Returning nil will prevent the module being added to the sdk.
Paul Duffineee466e2021-04-27 23:17:56 +0100364 SdkMemberType(child Module) SdkMemberType
Paul Duffina7208112021-04-23 21:20:20 +0100365
366 // ExportMember determines whether a module added to the sdk through this tag will be exported
367 // from the sdk or not.
368 //
369 // An exported member is added to the sdk using its own name, e.g. if "foo" was exported from sdk
370 // "bar" then its prebuilt would be simply called "foo". A member can be added to the sdk via
371 // multiple tags and if any of those tags returns true from this method then the membe will be
372 // exported. Every module added directly to the sdk via one of the member type specific
373 // properties, e.g. java_libs, will automatically be exported.
374 //
375 // If a member is not exported then it is treated as an internal implementation detail of the
376 // sdk and so will be added with an sdk specific name. e.g. if "foo" was an internal member of sdk
377 // "bar" then its prebuilt would be called "bar_foo". Additionally its visibility will be set to
378 // "//visibility:private" so it will not be accessible from outside its Android.bp file.
379 ExportMember() bool
Paul Duffinf8539922019-11-19 19:44:10 +0000380}
381
Paul Duffincee7e662020-07-09 17:32:57 +0100382var _ SdkMemberTypeDependencyTag = (*sdkMemberDependencyTag)(nil)
383var _ ReplaceSourceWithPrebuilt = (*sdkMemberDependencyTag)(nil)
384
Paul Duffinf8539922019-11-19 19:44:10 +0000385type sdkMemberDependencyTag struct {
386 blueprint.BaseDependencyTag
387 memberType SdkMemberType
Paul Duffina7208112021-04-23 21:20:20 +0100388 export bool
Paul Duffinf8539922019-11-19 19:44:10 +0000389}
390
Paul Duffineee466e2021-04-27 23:17:56 +0100391func (t *sdkMemberDependencyTag) SdkMemberType(_ Module) SdkMemberType {
Paul Duffinf8539922019-11-19 19:44:10 +0000392 return t.memberType
393}
394
Paul Duffina7208112021-04-23 21:20:20 +0100395func (t *sdkMemberDependencyTag) ExportMember() bool {
396 return t.export
397}
398
Paul Duffincee7e662020-07-09 17:32:57 +0100399// Prevent dependencies from the sdk/module_exports onto their members from being
400// replaced with a preferred prebuilt.
401func (t *sdkMemberDependencyTag) ReplaceSourceWithPrebuilt() bool {
402 return false
403}
404
Paul Duffina7208112021-04-23 21:20:20 +0100405// DependencyTagForSdkMemberType creates an SdkMemberTypeDependencyTag that will cause any
406// dependencies added by the tag to be added to the sdk as the specified SdkMemberType and exported
407// (or not) as specified by the export parameter.
408func DependencyTagForSdkMemberType(memberType SdkMemberType, export bool) SdkMemberTypeDependencyTag {
409 return &sdkMemberDependencyTag{memberType: memberType, export: export}
Paul Duffinf8539922019-11-19 19:44:10 +0000410}
411
Paul Duffin13879572019-11-28 14:31:38 +0000412// Interface that must be implemented for every type that can be a member of an
413// sdk.
414//
415// The basic implementation should look something like this, where ModuleType is
416// the name of the module type being supported.
417//
Paul Duffin255f18e2019-12-13 11:22:16 +0000418// type moduleTypeSdkMemberType struct {
419// android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +0000420// }
421//
Paul Duffin255f18e2019-12-13 11:22:16 +0000422// func init() {
423// android.RegisterSdkMemberType(&moduleTypeSdkMemberType{
424// SdkMemberTypeBase: android.SdkMemberTypeBase{
425// PropertyName: "module_types",
426// },
427// }
Paul Duffin13879572019-11-28 14:31:38 +0000428// }
429//
430// ...methods...
431//
432type SdkMemberType interface {
Paul Duffin255f18e2019-12-13 11:22:16 +0000433 // The name of the member type property on an sdk module.
434 SdkPropertyName() string
435
Paul Duffin13082052021-05-11 00:31:38 +0100436 // RequiresBpProperty returns true if this member type requires its property to be usable within
437 // an Android.bp file.
438 RequiresBpProperty() bool
439
Paul Duffine6029182019-12-16 17:43:48 +0000440 // True if the member type supports the sdk/sdk_snapshot, false otherwise.
441 UsableWithSdkAndSdkSnapshot() bool
442
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100443 // Return true if prebuilt host artifacts may be specific to the host OS. Only
444 // applicable to modules where HostSupported() is true. If this is true,
445 // snapshots will list each host OS variant explicitly and disable all other
446 // host OS'es.
447 IsHostOsDependent() bool
448
Martin Stjernholmcd07bce2020-03-10 22:37:59 +0000449 // Add dependencies from the SDK module to all the module variants the member
450 // type contributes to the SDK. `names` is the list of module names given in
451 // the member type property (as returned by SdkPropertyName()) in the SDK
452 // module. The exact set of variants required is determined by the SDK and its
453 // properties. The dependencies must be added with the supplied tag.
Paul Duffin13879572019-11-28 14:31:38 +0000454 //
455 // The BottomUpMutatorContext provided is for the SDK module.
456 AddDependencies(mctx BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string)
457
458 // Return true if the supplied module is an instance of this member type.
459 //
460 // This is used to check the type of each variant before added to the
461 // SdkMember. Returning false will cause an error to be logged expaining that
462 // the module is not allowed in whichever sdk property it was added.
463 IsInstance(module Module) bool
464
Paul Duffin0d4ed0a2021-05-10 23:58:40 +0100465 // UsesSourceModuleTypeInSnapshot returns true when the AddPrebuiltModule() method returns a
466 // source module type.
467 UsesSourceModuleTypeInSnapshot() bool
468
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000469 // Add a prebuilt module that the sdk will populate.
470 //
Paul Duffin425b0ea2020-05-06 12:41:39 +0100471 // The sdk module code generates the snapshot as follows:
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000472 //
473 // * A properties struct of type SdkMemberProperties is created for each variant and
474 // populated with information from the variant by calling PopulateFromVariant(SdkAware)
475 // on the struct.
476 //
477 // * An additional properties struct is created into which the common properties will be
478 // added.
479 //
480 // * The variant property structs are analysed to find exported (capitalized) fields which
481 // have common values. Those fields are cleared and the common value added to the common
Paul Duffin864e1b42020-05-06 10:23:19 +0100482 // properties.
483 //
484 // A field annotated with a tag of `sdk:"keep"` will be treated as if it
Paul Duffinb07fa512020-03-10 22:17:04 +0000485 // was not capitalized, i.e. not optimized for common values.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000486 //
Paul Duffin864e1b42020-05-06 10:23:19 +0100487 // A field annotated with a tag of `android:"arch_variant"` will be allowed to have
488 // values that differ by arch, fields not tagged as such must have common values across
489 // all variants.
490 //
Paul Duffinc459f892020-04-30 18:08:29 +0100491 // * Additional field tags can be specified on a field that will ignore certain values
492 // for the purpose of common value optimization. A value that is ignored must have the
493 // default value for the property type. This is to ensure that significant value are not
494 // ignored by accident. The purpose of this is to allow the snapshot generation to reflect
495 // the behavior of the runtime. e.g. if a property is ignored on the host then a property
496 // that is common for android can be treated as if it was common for android and host as
497 // the setting for host is ignored anyway.
498 // * `sdk:"ignored-on-host" - this indicates the property is ignored on the host variant.
499 //
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000500 // * The sdk module type populates the BpModule structure, creating the arch specific
501 // structure and calls AddToPropertySet(...) on the properties struct to add the member
502 // specific properties in the correct place in the structure.
503 //
Paul Duffin3a4eb502020-03-19 16:11:18 +0000504 AddPrebuiltModule(ctx SdkMemberContext, member SdkMember) BpModule
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000505
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000506 // Create a structure into which variant specific properties can be added.
507 CreateVariantPropertiesStruct() SdkMemberProperties
Paul Duffin13879572019-11-28 14:31:38 +0000508}
Paul Duffin255f18e2019-12-13 11:22:16 +0000509
Paul Duffine6029182019-12-16 17:43:48 +0000510// Base type for SdkMemberType implementations.
Paul Duffin255f18e2019-12-13 11:22:16 +0000511type SdkMemberTypeBase struct {
Paul Duffin13082052021-05-11 00:31:38 +0100512 PropertyName string
513
514 // When set to true BpPropertyNotRequired indicates that the member type does not require the
515 // property to be specifiable in an Android.bp file.
516 BpPropertyNotRequired bool
517
Paul Duffin2d3da312021-05-06 12:02:27 +0100518 SupportsSdk bool
519 HostOsDependent bool
Paul Duffin0d4ed0a2021-05-10 23:58:40 +0100520
521 // When set to true UseSourceModuleTypeInSnapshot indicates that the member type creates a source
522 // module type in its SdkMemberType.AddPrebuiltModule() method. That prevents the sdk snapshot
523 // code from automatically adding a prefer: true flag.
524 UseSourceModuleTypeInSnapshot bool
Paul Duffin255f18e2019-12-13 11:22:16 +0000525}
526
527func (b *SdkMemberTypeBase) SdkPropertyName() string {
528 return b.PropertyName
529}
530
Paul Duffin13082052021-05-11 00:31:38 +0100531func (b *SdkMemberTypeBase) RequiresBpProperty() bool {
532 return !b.BpPropertyNotRequired
533}
534
Paul Duffine6029182019-12-16 17:43:48 +0000535func (b *SdkMemberTypeBase) UsableWithSdkAndSdkSnapshot() bool {
536 return b.SupportsSdk
537}
538
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100539func (b *SdkMemberTypeBase) IsHostOsDependent() bool {
540 return b.HostOsDependent
541}
542
Paul Duffin0d4ed0a2021-05-10 23:58:40 +0100543func (b *SdkMemberTypeBase) UsesSourceModuleTypeInSnapshot() bool {
544 return b.UseSourceModuleTypeInSnapshot
545}
546
Paul Duffin255f18e2019-12-13 11:22:16 +0000547// Encapsulates the information about registered SdkMemberTypes.
548type SdkMemberTypesRegistry struct {
549 // The list of types sorted by property name.
550 list []SdkMemberType
551
552 // The key that uniquely identifies this registry instance.
553 key OnceKey
554}
555
Paul Duffine6029182019-12-16 17:43:48 +0000556func (r *SdkMemberTypesRegistry) copyAndAppend(memberType SdkMemberType) *SdkMemberTypesRegistry {
557 oldList := r.list
Paul Duffin255f18e2019-12-13 11:22:16 +0000558
559 // Copy the slice just in case this is being read while being modified, e.g. when testing.
560 list := make([]SdkMemberType, 0, len(oldList)+1)
561 list = append(list, oldList...)
562 list = append(list, memberType)
563
564 // Sort the member types by their property name to ensure that registry order has no effect
565 // on behavior.
566 sort.Slice(list, func(i1, i2 int) bool {
567 t1 := list[i1]
568 t2 := list[i2]
569
570 return t1.SdkPropertyName() < t2.SdkPropertyName()
571 })
572
573 // Generate a key that identifies the slice of SdkMemberTypes by joining the property names
574 // from all the SdkMemberType .
575 var properties []string
576 for _, t := range list {
577 properties = append(properties, t.SdkPropertyName())
578 }
579 key := NewOnceKey(strings.Join(properties, "|"))
580
581 // Create a new registry so the pointer uniquely identifies the set of registered types.
Paul Duffine6029182019-12-16 17:43:48 +0000582 return &SdkMemberTypesRegistry{
Paul Duffin255f18e2019-12-13 11:22:16 +0000583 list: list,
584 key: key,
585 }
586}
Paul Duffine6029182019-12-16 17:43:48 +0000587
588func (r *SdkMemberTypesRegistry) RegisteredTypes() []SdkMemberType {
589 return r.list
590}
591
592func (r *SdkMemberTypesRegistry) UniqueOnceKey() OnceKey {
593 // Use the pointer to the registry as the unique key.
594 return NewCustomOnceKey(r)
595}
596
597// The set of registered SdkMemberTypes, one for sdk module and one for module_exports.
598var ModuleExportsMemberTypes = &SdkMemberTypesRegistry{}
599var SdkMemberTypes = &SdkMemberTypesRegistry{}
600
601// Register an SdkMemberType object to allow them to be used in the sdk and sdk_snapshot module
602// types.
603func RegisterSdkMemberType(memberType SdkMemberType) {
604 // All member types are usable with module_exports.
605 ModuleExportsMemberTypes = ModuleExportsMemberTypes.copyAndAppend(memberType)
606
607 // Only those that explicitly indicate it are usable with sdk.
608 if memberType.UsableWithSdkAndSdkSnapshot() {
609 SdkMemberTypes = SdkMemberTypes.copyAndAppend(memberType)
610 }
611}
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000612
613// Base structure for all implementations of SdkMemberProperties.
614//
Martin Stjernholm89238f42020-07-10 00:14:03 +0100615// Contains common properties that apply across many different member types.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000616type SdkMemberPropertiesBase struct {
Paul Duffina04c1072020-03-02 10:16:35 +0000617 // The number of unique os types supported by the member variants.
Paul Duffina551a1c2020-03-17 21:04:24 +0000618 //
619 // If a member has a variant with more than one os type then it will need to differentiate
620 // the locations of any of their prebuilt files in the snapshot by os type to prevent them
621 // from colliding. See OsPrefix().
622 //
623 // This property is the same for all variants of a member and so would be optimized away
624 // if it was not explicitly kept.
Paul Duffinb07fa512020-03-10 22:17:04 +0000625 Os_count int `sdk:"keep"`
Paul Duffina04c1072020-03-02 10:16:35 +0000626
627 // The os type for which these properties refer.
Paul Duffina551a1c2020-03-17 21:04:24 +0000628 //
629 // Provided to allow a member to differentiate between os types in the locations of their
630 // prebuilt files when it supports more than one os type.
631 //
632 // This property is the same for all os type specific variants of a member and so would be
633 // optimized away if it was not explicitly kept.
Paul Duffinb07fa512020-03-10 22:17:04 +0000634 Os OsType `sdk:"keep"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000635
636 // The setting to use for the compile_multilib property.
Martin Stjernholm89238f42020-07-10 00:14:03 +0100637 Compile_multilib string `android:"arch_variant"`
Paul Duffina04c1072020-03-02 10:16:35 +0000638}
639
640// The os prefix to use for any file paths in the sdk.
641//
642// Is an empty string if the member only provides variants for a single os type, otherwise
643// is the OsType.Name.
644func (b *SdkMemberPropertiesBase) OsPrefix() string {
645 if b.Os_count == 1 {
646 return ""
647 } else {
648 return b.Os.Name
649 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000650}
651
652func (b *SdkMemberPropertiesBase) Base() *SdkMemberPropertiesBase {
653 return b
654}
655
656// Interface to be implemented on top of a structure that contains variant specific
657// information.
658//
659// Struct fields that are capitalized are examined for common values to extract. Fields
660// that are not capitalized are assumed to be arch specific.
661type SdkMemberProperties interface {
662 // Access the base structure.
663 Base() *SdkMemberPropertiesBase
664
Paul Duffin3a4eb502020-03-19 16:11:18 +0000665 // Populate this structure with information from the variant.
666 PopulateFromVariant(ctx SdkMemberContext, variant Module)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000667
Paul Duffin3a4eb502020-03-19 16:11:18 +0000668 // Add the information from this structure to the property set.
669 AddToPropertySet(ctx SdkMemberContext, propertySet BpPropertySet)
670}
671
672// Provides access to information common to a specific member.
673type SdkMemberContext interface {
674
675 // The module context of the sdk common os variant which is creating the snapshot.
676 SdkModuleContext() ModuleContext
677
678 // The builder of the snapshot.
679 SnapshotBuilder() SnapshotBuilder
Paul Duffina551a1c2020-03-17 21:04:24 +0000680
681 // The type of the member.
682 MemberType() SdkMemberType
683
684 // The name of the member.
685 //
686 // Provided for use by sdk members to create a member specific location within the snapshot
687 // into which to copy the prebuilt files.
688 Name() string
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000689}
Paul Duffina1aa7382021-04-29 21:50:40 +0100690
691// ExportedComponentsInfo contains information about the components that this module exports to an
692// sdk snapshot.
693//
694// A component of a module is a child module that the module creates and which forms an integral
695// part of the functionality that the creating module provides. A component module is essentially
696// owned by its creator and is tightly coupled to the creator and other components.
697//
698// e.g. the child modules created by prebuilt_apis are not components because they are not tightly
699// coupled to the prebuilt_apis module. Once they are created the prebuilt_apis ignores them. The
700// child impl and stub library created by java_sdk_library (and corresponding import) are components
701// because the creating module depends upon them in order to provide some of its own functionality.
702//
703// A component is exported if it is part of an sdk snapshot. e.g. The xml and impl child modules are
704// components but they are not exported as they are not part of an sdk snapshot.
705//
706// This information is used by the sdk snapshot generation code to ensure that it does not create
707// an sdk snapshot that contains a declaration of the component module and the module that creates
708// it as that would result in duplicate modules when attempting to use the snapshot. e.g. a snapshot
709// that included the java_sdk_library_import "foo" and also a java_import "foo.stubs" would fail
710// as there would be two modules called "foo.stubs".
711type ExportedComponentsInfo struct {
712 // The names of the exported components.
713 Components []string
714}
715
716var ExportedComponentsInfoProvider = blueprint.NewProvider(ExportedComponentsInfo{})