blob: 100f63b0e379229e9355b85aaf8197fefa8f28b2 [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 Duffin94289702021-09-09 15:38:32 +010025// RequiredSdks provides access to the set of SDKs required by an APEX and its contents.
26//
Paul Duffin923e8a52020-03-30 15:33:32 +010027// Extracted from SdkAware to make it easier to define custom subsets of the
28// SdkAware interface and improve code navigation within the IDE.
29//
30// In addition to its use in SdkAware this interface must also be implemented by
31// APEX to specify the SDKs required by that module and its contents. e.g. APEX
32// is expected to implement RequiredSdks() by reading its own properties like
33// `uses_sdks`.
34type RequiredSdks interface {
Paul Duffin94289702021-09-09 15:38:32 +010035 // RequiredSdks returns the set of SDKs required by an APEX and its contents.
Paul Duffin923e8a52020-03-30 15:33:32 +010036 RequiredSdks() SdkRefs
37}
38
Paul Duffin94289702021-09-09 15:38:32 +010039// sdkAwareWithoutModule is provided simply to improve code navigation with the IDE.
Paul Duffin50f0da42020-07-22 13:52:01 +010040type sdkAwareWithoutModule interface {
Paul Duffin923e8a52020-03-30 15:33:32 +010041 RequiredSdks
42
Paul Duffinb97b1572021-04-29 21:50:40 +010043 // SdkMemberComponentName will return the name to use for a component of this module based on the
44 // base name of this module.
45 //
46 // The baseName is the name returned by ModuleBase.BaseModuleName(), i.e. the name specified in
47 // the name property in the .bp file so will not include the prebuilt_ prefix.
48 //
49 // The componentNameCreator is a func for creating the name of a component from the base name of
50 // the module, e.g. it could just append ".component" to the name passed in.
51 //
52 // This is intended to be called by prebuilt modules that create component models. It is because
53 // prebuilt module base names come in a variety of different forms:
54 // * unversioned - this is the same as the source module.
55 // * internal to an sdk - this is the unversioned name prefixed by the base name of the sdk
56 // module.
57 // * versioned - this is the same as the internal with the addition of an "@<version>" suffix.
58 //
59 // While this can be called from a source module in that case it will behave the same way as the
60 // unversioned name and return the result of calling the componentNameCreator func on the supplied
61 // base name.
62 //
63 // e.g. Assuming the componentNameCreator func simply appends ".component" to the name passed in
64 // then this will work as follows:
65 // * An unversioned name of "foo" will return "foo.component".
66 // * An internal to the sdk name of "sdk_foo" will return "sdk_foo.component".
67 // * A versioned name of "sdk_foo@current" will return "sdk_foo.component@current".
68 //
69 // Note that in the latter case the ".component" suffix is added before the version. Adding it
70 // after would change the version.
71 SdkMemberComponentName(baseName string, componentNameCreator func(string) string) string
72
Jiyong Parkd1063c12019-07-17 20:08:41 +090073 sdkBase() *SdkBase
74 MakeMemberOf(sdk SdkRef)
75 IsInAnySdk() bool
Paul Duffinb9e7a3c2021-05-06 15:53:19 +010076
77 // IsVersioned determines whether the module is versioned, i.e. has a name of the form
78 // <name>@<version>
79 IsVersioned() bool
80
Jiyong Parkd1063c12019-07-17 20:08:41 +090081 ContainingSdk() SdkRef
82 MemberName() string
83 BuildWithSdks(sdks SdkRefs)
Jiyong Parkd1063c12019-07-17 20:08:41 +090084}
85
Paul Duffin50f0da42020-07-22 13:52:01 +010086// SdkAware is the interface that must be supported by any module to become a member of SDK or to be
87// built with SDK
88type SdkAware interface {
89 Module
90 sdkAwareWithoutModule
91}
92
Jiyong Parkd1063c12019-07-17 20:08:41 +090093// SdkRef refers to a version of an SDK
94type SdkRef struct {
95 Name string
96 Version string
97}
98
Jiyong Park9b409bc2019-10-11 14:59:13 +090099// Unversioned determines if the SdkRef is referencing to the unversioned SDK module
100func (s SdkRef) Unversioned() bool {
101 return s.Version == ""
Jiyong Parkd1063c12019-07-17 20:08:41 +0900102}
103
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900104// String returns string representation of this SdkRef for debugging purpose
105func (s SdkRef) String() string {
106 if s.Name == "" {
107 return "(No Sdk)"
108 }
109 if s.Unversioned() {
110 return s.Name
111 }
112 return s.Name + string(SdkVersionSeparator) + s.Version
113}
114
Jiyong Park9b409bc2019-10-11 14:59:13 +0900115// SdkVersionSeparator is a character used to separate an sdk name and its version
116const SdkVersionSeparator = '@'
Jiyong Parkd1063c12019-07-17 20:08:41 +0900117
Jiyong Park9b409bc2019-10-11 14:59:13 +0900118// ParseSdkRef parses a `name@version` style string into a corresponding SdkRef struct
Jiyong Parkd1063c12019-07-17 20:08:41 +0900119func ParseSdkRef(ctx BaseModuleContext, str string, property string) SdkRef {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900120 tokens := strings.Split(str, string(SdkVersionSeparator))
Jiyong Parkd1063c12019-07-17 20:08:41 +0900121 if len(tokens) < 1 || len(tokens) > 2 {
Paul Duffin525a5902021-05-06 16:33:52 +0100122 ctx.PropertyErrorf(property, "%q does not follow name@version syntax", str)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900123 return SdkRef{Name: "invalid sdk name", Version: "invalid sdk version"}
124 }
125
126 name := tokens[0]
127
Jiyong Park9b409bc2019-10-11 14:59:13 +0900128 var version string
Jiyong Parkd1063c12019-07-17 20:08:41 +0900129 if len(tokens) == 2 {
130 version = tokens[1]
131 }
132
133 return SdkRef{Name: name, Version: version}
134}
135
136type SdkRefs []SdkRef
137
Jiyong Park9b409bc2019-10-11 14:59:13 +0900138// Contains tells if the given SdkRef is in this list of SdkRef's
Jiyong Parkd1063c12019-07-17 20:08:41 +0900139func (refs SdkRefs) Contains(s SdkRef) bool {
140 for _, r := range refs {
141 if r == s {
142 return true
143 }
144 }
145 return false
146}
147
148type sdkProperties struct {
149 // The SDK that this module is a member of. nil if it is not a member of any SDK
150 ContainingSdk *SdkRef `blueprint:"mutated"`
151
152 // The list of SDK names and versions that are used to build this module
153 RequiredSdks SdkRefs `blueprint:"mutated"`
154
155 // Name of the module that this sdk member is representing
156 Sdk_member_name *string
157}
158
159// SdkBase is a struct that is expected to be included in module types to implement the SdkAware
160// interface. InitSdkAwareModule should be called to initialize this struct.
161type SdkBase struct {
162 properties sdkProperties
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000163 module SdkAware
Jiyong Parkd1063c12019-07-17 20:08:41 +0900164}
165
166func (s *SdkBase) sdkBase() *SdkBase {
167 return s
168}
169
Paul Duffinb97b1572021-04-29 21:50:40 +0100170func (s *SdkBase) SdkMemberComponentName(baseName string, componentNameCreator func(string) string) string {
171 if s.MemberName() == "" {
172 return componentNameCreator(baseName)
173 } else {
174 index := strings.LastIndex(baseName, "@")
175 unversionedName := baseName[:index]
176 unversionedComponentName := componentNameCreator(unversionedName)
177 versionSuffix := baseName[index:]
178 return unversionedComponentName + versionSuffix
179 }
180}
181
Jiyong Park9b409bc2019-10-11 14:59:13 +0900182// MakeMemberOf sets this module to be a member of a specific SDK
Jiyong Parkd1063c12019-07-17 20:08:41 +0900183func (s *SdkBase) MakeMemberOf(sdk SdkRef) {
184 s.properties.ContainingSdk = &sdk
185}
186
187// IsInAnySdk returns true if this module is a member of any SDK
188func (s *SdkBase) IsInAnySdk() bool {
189 return s.properties.ContainingSdk != nil
190}
191
Paul Duffinb9e7a3c2021-05-06 15:53:19 +0100192// IsVersioned returns true if this module is versioned.
193func (s *SdkBase) IsVersioned() bool {
194 return strings.Contains(s.module.Name(), "@")
195}
196
Jiyong Parkd1063c12019-07-17 20:08:41 +0900197// ContainingSdk returns the SDK that this module is a member of
198func (s *SdkBase) ContainingSdk() SdkRef {
199 if s.properties.ContainingSdk != nil {
200 return *s.properties.ContainingSdk
201 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900202 return SdkRef{Name: "", Version: ""}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900203}
204
Jiyong Park9b409bc2019-10-11 14:59:13 +0900205// MemberName returns the name of the module that this SDK member is overriding
Jiyong Parkd1063c12019-07-17 20:08:41 +0900206func (s *SdkBase) MemberName() string {
207 return proptools.String(s.properties.Sdk_member_name)
208}
209
210// BuildWithSdks is used to mark that this module has to be built with the given SDK(s).
211func (s *SdkBase) BuildWithSdks(sdks SdkRefs) {
212 s.properties.RequiredSdks = sdks
213}
214
215// RequiredSdks returns the SDK(s) that this module has to be built with
216func (s *SdkBase) RequiredSdks() SdkRefs {
217 return s.properties.RequiredSdks
218}
219
220// InitSdkAwareModule initializes the SdkBase struct. This must be called by all modules including
221// SdkBase.
222func InitSdkAwareModule(m SdkAware) {
223 base := m.sdkBase()
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000224 base.module = m
Jiyong Parkd1063c12019-07-17 20:08:41 +0900225 m.AddProperties(&base.properties)
226}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000227
Paul Duffin0c2e0832021-04-28 00:39:52 +0100228// IsModuleInVersionedSdk returns true if the module is an versioned sdk.
229func IsModuleInVersionedSdk(module Module) bool {
230 if s, ok := module.(SdkAware); ok {
231 if !s.ContainingSdk().Unversioned() {
232 return true
233 }
234 }
235 return false
236}
237
Paul Duffin94289702021-09-09 15:38:32 +0100238// SnapshotBuilder provides support for generating the build rules which will build the snapshot.
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000239type SnapshotBuilder interface {
Paul Duffin94289702021-09-09 15:38:32 +0100240 // CopyToSnapshot generates a rule that will copy the src to the dest (which is a snapshot
241 // relative path) and add the dest to the zip.
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000242 CopyToSnapshot(src Path, dest string)
243
Paul Duffin94289702021-09-09 15:38:32 +0100244 // EmptyFile returns the path to an empty file.
Paul Duffin5c211452021-07-15 12:42:44 +0100245 //
246 // This can be used by sdk member types that need to create an empty file in the snapshot, simply
247 // pass the value returned from this to the CopyToSnapshot() method.
248 EmptyFile() Path
249
Paul Duffin94289702021-09-09 15:38:32 +0100250 // UnzipToSnapshot generates a rule that will unzip the supplied zip into the snapshot relative
251 // directory destDir.
Paul Duffin91547182019-11-12 19:39:36 +0000252 UnzipToSnapshot(zipPath Path, destDir string)
253
Paul Duffin94289702021-09-09 15:38:32 +0100254 // AddPrebuiltModule adds a new prebuilt module to the snapshot.
255 //
256 // It is intended to be called from SdkMemberType.AddPrebuiltModule which can add module type
257 // specific properties that are not variant specific. The following properties will be
258 // automatically populated before returning.
Paul Duffinb645ec82019-11-27 17:43:54 +0000259 //
260 // * name
261 // * sdk_member_name
262 // * prefer
263 //
Paul Duffin94289702021-09-09 15:38:32 +0100264 // Properties that are variant specific will be handled by SdkMemberProperties structure.
265 //
266 // Each module created by this method can be output to the generated Android.bp file in two
267 // different forms, depending on the setting of the SOONG_SDK_SNAPSHOT_VERSION build property.
268 // The two forms are:
269 // 1. A versioned Soong module that is referenced from a corresponding similarly versioned
270 // snapshot module.
271 // 2. An unversioned Soong module that.
272 //
273 // See sdk/update.go for more information.
Paul Duffin9d8d6092019-12-05 18:19:29 +0000274 AddPrebuiltModule(member SdkMember, moduleType string) BpModule
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000275
Paul Duffin94289702021-09-09 15:38:32 +0100276 // SdkMemberReferencePropertyTag returns a property tag to use when adding a property to a
277 // BpModule that contains references to other sdk members.
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000278 //
Paul Duffin94289702021-09-09 15:38:32 +0100279 // Using this will ensure that the reference is correctly output for both versioned and
280 // unversioned prebuilts in the snapshot.
Paul Duffin13f02712020-03-06 12:30:43 +0000281 //
Paul Duffin94289702021-09-09 15:38:32 +0100282 // "required: true" means that the property must only contain references to other members of the
283 // sdk. Passing a reference to a module that is not a member of the sdk will result in a build
284 // error.
Paul Duffin13f02712020-03-06 12:30:43 +0000285 //
Paul Duffin94289702021-09-09 15:38:32 +0100286 // "required: false" means that the property can contain references to modules that are either
287 // members or not members of the sdk. If a reference is to a module that is a non member then the
288 // reference is left unchanged, i.e. it is not transformed as references to members are.
289 //
290 // The handling of the member names is dependent on whether it is an internal or exported member.
291 // An exported member is one whose name is specified in one of the member type specific
292 // properties. An internal member is one that is added due to being a part of an exported (or
293 // other internal) member and is not itself an exported member.
Paul Duffin13f02712020-03-06 12:30:43 +0000294 //
295 // Member names are handled as follows:
Paul Duffin94289702021-09-09 15:38:32 +0100296 // * When creating the unversioned form of the module the name is left unchecked unless the member
297 // is internal in which case it is transformed into an sdk specific name, i.e. by prefixing with
298 // the sdk name.
Paul Duffin13f02712020-03-06 12:30:43 +0000299 //
Paul Duffin94289702021-09-09 15:38:32 +0100300 // * When creating the versioned form of the module the name is transformed into a versioned sdk
301 // specific name, i.e. by prefixing with the sdk name and suffixing with the version.
Paul Duffin13f02712020-03-06 12:30:43 +0000302 //
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000303 // e.g.
Paul Duffin13f02712020-03-06 12:30:43 +0000304 // bpPropertySet.AddPropertyWithTag("libs", []string{"member1", "member2"}, builder.SdkMemberReferencePropertyTag(true))
305 SdkMemberReferencePropertyTag(required bool) BpPropertyTag
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000306}
307
Paul Duffin94289702021-09-09 15:38:32 +0100308// BpPropertyTag is a marker interface that can be associated with properties in a BpPropertySet to
309// provide additional information which can be used to customize their behavior.
Paul Duffin5b511a22020-01-15 14:23:52 +0000310type BpPropertyTag interface{}
311
Paul Duffin94289702021-09-09 15:38:32 +0100312// BpPropertySet is a set of properties for use in a .bp file.
Paul Duffinb645ec82019-11-27 17:43:54 +0000313type BpPropertySet interface {
Paul Duffin94289702021-09-09 15:38:32 +0100314 // AddProperty adds a property.
315 //
316 // The value can be one of the following types:
Paul Duffinb645ec82019-11-27 17:43:54 +0000317 // * string
318 // * array of the above
319 // * bool
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100320 // For these types it is an error if multiple properties with the same name
321 // are added.
322 //
323 // * pointer to a struct
Paul Duffinb645ec82019-11-27 17:43:54 +0000324 // * BpPropertySet
325 //
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100326 // A pointer to a Blueprint-style property struct is first converted into a
327 // BpPropertySet by traversing the fields and adding their values as
328 // properties in a BpPropertySet. A field with a struct value is itself
329 // converted into a BpPropertySet before adding.
330 //
331 // Adding a BpPropertySet is done as follows:
332 // * If no property with the name exists then the BpPropertySet is added
333 // directly to this property. Care must be taken to ensure that it does not
334 // introduce a cycle.
335 // * If a property exists with the name and the current value is a
336 // BpPropertySet then every property of the new BpPropertySet is added to
337 // the existing BpPropertySet.
338 // * Otherwise, if a property exists with the name then it is an error.
Paul Duffinb645ec82019-11-27 17:43:54 +0000339 AddProperty(name string, value interface{})
340
Paul Duffin94289702021-09-09 15:38:32 +0100341 // AddPropertyWithTag adds a property with an associated property tag.
Paul Duffin5b511a22020-01-15 14:23:52 +0000342 AddPropertyWithTag(name string, value interface{}, tag BpPropertyTag)
343
Paul Duffin94289702021-09-09 15:38:32 +0100344 // AddPropertySet adds a property set with the specified name and returns it so that additional
345 // properties can be added to it.
Paul Duffinb645ec82019-11-27 17:43:54 +0000346 AddPropertySet(name string) BpPropertySet
Paul Duffin0df49682021-05-07 01:10:01 +0100347
Paul Duffin94289702021-09-09 15:38:32 +0100348 // AddCommentForProperty adds a comment for the named property (or property set).
Paul Duffin0df49682021-05-07 01:10:01 +0100349 AddCommentForProperty(name, text string)
Paul Duffinb645ec82019-11-27 17:43:54 +0000350}
351
Paul Duffin94289702021-09-09 15:38:32 +0100352// BpModule represents a module definition in a .bp file.
Paul Duffinb645ec82019-11-27 17:43:54 +0000353type BpModule interface {
354 BpPropertySet
Paul Duffin0df49682021-05-07 01:10:01 +0100355
356 // ModuleType returns the module type of the module
357 ModuleType() string
358
359 // Name returns the name of the module or "" if no name has been specified.
360 Name() string
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000361}
Paul Duffin13879572019-11-28 14:31:38 +0000362
Paul Duffin51227d82021-05-18 12:54:27 +0100363// BpPrintable is a marker interface that must be implemented by any struct that is added as a
364// property value.
365type BpPrintable interface {
366 bpPrintable()
367}
368
369// BpPrintableBase must be embedded within any struct that is added as a
370// property value.
371type BpPrintableBase struct {
372}
373
374func (b BpPrintableBase) bpPrintable() {
375}
376
377var _ BpPrintable = BpPrintableBase{}
378
Paul Duffin94289702021-09-09 15:38:32 +0100379// SdkMember is an individual member of the SDK.
380//
381// It includes all of the variants that the SDK depends upon.
Paul Duffin13879572019-11-28 14:31:38 +0000382type SdkMember interface {
Paul Duffin94289702021-09-09 15:38:32 +0100383 // Name returns the name of the member.
Paul Duffin13879572019-11-28 14:31:38 +0000384 Name() string
385
Paul Duffin94289702021-09-09 15:38:32 +0100386 // Variants returns all the variants of this module depended upon by the SDK.
Paul Duffin13879572019-11-28 14:31:38 +0000387 Variants() []SdkAware
388}
389
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100390// SdkMemberDependencyTag is the interface that a tag must implement in order to allow the
Paul Duffin2d3da312021-05-06 12:02:27 +0100391// dependent module to be automatically added to the sdk.
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100392type SdkMemberDependencyTag interface {
Paul Duffinf8539922019-11-19 19:44:10 +0000393 blueprint.DependencyTag
394
Paul Duffina7208112021-04-23 21:20:20 +0100395 // SdkMemberType returns the SdkMemberType that will be used to automatically add the child module
396 // to the sdk.
Paul Duffin5cca7c42021-05-26 10:16:01 +0100397 //
398 // Returning nil will prevent the module being added to the sdk.
Paul Duffineee466e2021-04-27 23:17:56 +0100399 SdkMemberType(child Module) SdkMemberType
Paul Duffina7208112021-04-23 21:20:20 +0100400
401 // ExportMember determines whether a module added to the sdk through this tag will be exported
402 // from the sdk or not.
403 //
404 // An exported member is added to the sdk using its own name, e.g. if "foo" was exported from sdk
405 // "bar" then its prebuilt would be simply called "foo". A member can be added to the sdk via
406 // multiple tags and if any of those tags returns true from this method then the membe will be
407 // exported. Every module added directly to the sdk via one of the member type specific
408 // properties, e.g. java_libs, will automatically be exported.
409 //
410 // If a member is not exported then it is treated as an internal implementation detail of the
411 // sdk and so will be added with an sdk specific name. e.g. if "foo" was an internal member of sdk
412 // "bar" then its prebuilt would be called "bar_foo". Additionally its visibility will be set to
413 // "//visibility:private" so it will not be accessible from outside its Android.bp file.
414 ExportMember() bool
Paul Duffinf8539922019-11-19 19:44:10 +0000415}
416
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100417var _ SdkMemberDependencyTag = (*sdkMemberDependencyTag)(nil)
418var _ ReplaceSourceWithPrebuilt = (*sdkMemberDependencyTag)(nil)
Paul Duffincee7e662020-07-09 17:32:57 +0100419
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100420type sdkMemberDependencyTag struct {
Paul Duffinf8539922019-11-19 19:44:10 +0000421 blueprint.BaseDependencyTag
422 memberType SdkMemberType
Paul Duffina7208112021-04-23 21:20:20 +0100423 export bool
Paul Duffinf8539922019-11-19 19:44:10 +0000424}
425
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100426func (t *sdkMemberDependencyTag) SdkMemberType(_ Module) SdkMemberType {
Paul Duffinf8539922019-11-19 19:44:10 +0000427 return t.memberType
428}
429
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100430func (t *sdkMemberDependencyTag) ExportMember() bool {
Paul Duffina7208112021-04-23 21:20:20 +0100431 return t.export
432}
433
Paul Duffin94289702021-09-09 15:38:32 +0100434// ReplaceSourceWithPrebuilt prevents dependencies from the sdk/module_exports onto their members
435// from being replaced with a preferred prebuilt.
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100436func (t *sdkMemberDependencyTag) ReplaceSourceWithPrebuilt() bool {
Paul Duffincee7e662020-07-09 17:32:57 +0100437 return false
438}
439
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100440// DependencyTagForSdkMemberType creates an SdkMemberDependencyTag that will cause any
Paul Duffina7208112021-04-23 21:20:20 +0100441// dependencies added by the tag to be added to the sdk as the specified SdkMemberType and exported
442// (or not) as specified by the export parameter.
Paul Duffinf7b3d0d2021-09-02 14:29:21 +0100443func DependencyTagForSdkMemberType(memberType SdkMemberType, export bool) SdkMemberDependencyTag {
444 return &sdkMemberDependencyTag{memberType: memberType, export: export}
Paul Duffinf8539922019-11-19 19:44:10 +0000445}
446
Paul Duffin94289702021-09-09 15:38:32 +0100447// 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 +0000448// sdk.
449//
450// The basic implementation should look something like this, where ModuleType is
451// the name of the module type being supported.
452//
Paul Duffin255f18e2019-12-13 11:22:16 +0000453// type moduleTypeSdkMemberType struct {
454// android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +0000455// }
456//
Paul Duffin255f18e2019-12-13 11:22:16 +0000457// func init() {
458// android.RegisterSdkMemberType(&moduleTypeSdkMemberType{
459// SdkMemberTypeBase: android.SdkMemberTypeBase{
460// PropertyName: "module_types",
461// },
462// }
Paul Duffin13879572019-11-28 14:31:38 +0000463// }
464//
465// ...methods...
466//
467type SdkMemberType interface {
Paul Duffin94289702021-09-09 15:38:32 +0100468 // SdkPropertyName returns the name of the member type property on an sdk module.
Paul Duffin255f18e2019-12-13 11:22:16 +0000469 SdkPropertyName() string
470
Paul Duffin13082052021-05-11 00:31:38 +0100471 // RequiresBpProperty returns true if this member type requires its property to be usable within
472 // an Android.bp file.
473 RequiresBpProperty() bool
474
Paul Duffin94289702021-09-09 15:38:32 +0100475 // UsableWithSdkAndSdkSnapshot returns true if the member type supports the sdk/sdk_snapshot,
476 // false otherwise.
Paul Duffine6029182019-12-16 17:43:48 +0000477 UsableWithSdkAndSdkSnapshot() bool
478
Paul Duffin94289702021-09-09 15:38:32 +0100479 // IsHostOsDependent returns true if prebuilt host artifacts may be specific to the host OS. Only
480 // applicable to modules where HostSupported() is true. If this is true, snapshots will list each
481 // host OS variant explicitly and disable all other host OS'es.
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100482 IsHostOsDependent() bool
483
Paul Duffin94289702021-09-09 15:38:32 +0100484 // AddDependencies adds dependencies from the SDK module to all the module variants the member
485 // type contributes to the SDK. `names` is the list of module names given in the member type
486 // property (as returned by SdkPropertyName()) in the SDK module. The exact set of variants
487 // required is determined by the SDK and its properties. The dependencies must be added with the
488 // supplied tag.
Paul Duffin13879572019-11-28 14:31:38 +0000489 //
490 // The BottomUpMutatorContext provided is for the SDK module.
Paul Duffin296701e2021-07-14 10:29:36 +0100491 AddDependencies(ctx SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string)
Paul Duffin13879572019-11-28 14:31:38 +0000492
Paul Duffin94289702021-09-09 15:38:32 +0100493 // IsInstance returns true if the supplied module is an instance of this member type.
Paul Duffin13879572019-11-28 14:31:38 +0000494 //
Paul Duffin94289702021-09-09 15:38:32 +0100495 // This is used to check the type of each variant before added to the SdkMember. Returning false
496 // will cause an error to be logged explaining that the module is not allowed in whichever sdk
497 // property it was added.
Paul Duffin13879572019-11-28 14:31:38 +0000498 IsInstance(module Module) bool
499
Paul Duffin0d4ed0a2021-05-10 23:58:40 +0100500 // UsesSourceModuleTypeInSnapshot returns true when the AddPrebuiltModule() method returns a
501 // source module type.
502 UsesSourceModuleTypeInSnapshot() bool
503
Paul Duffin94289702021-09-09 15:38:32 +0100504 // AddPrebuiltModule is called to add a prebuilt module that the sdk will populate.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000505 //
Paul Duffin425b0ea2020-05-06 12:41:39 +0100506 // The sdk module code generates the snapshot as follows:
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000507 //
508 // * A properties struct of type SdkMemberProperties is created for each variant and
509 // populated with information from the variant by calling PopulateFromVariant(SdkAware)
510 // on the struct.
511 //
512 // * An additional properties struct is created into which the common properties will be
513 // added.
514 //
515 // * The variant property structs are analysed to find exported (capitalized) fields which
516 // have common values. Those fields are cleared and the common value added to the common
Paul Duffin864e1b42020-05-06 10:23:19 +0100517 // properties.
518 //
519 // A field annotated with a tag of `sdk:"keep"` will be treated as if it
Paul Duffinb07fa512020-03-10 22:17:04 +0000520 // was not capitalized, i.e. not optimized for common values.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000521 //
Paul Duffin864e1b42020-05-06 10:23:19 +0100522 // A field annotated with a tag of `android:"arch_variant"` will be allowed to have
523 // values that differ by arch, fields not tagged as such must have common values across
524 // all variants.
525 //
Paul Duffinc459f892020-04-30 18:08:29 +0100526 // * Additional field tags can be specified on a field that will ignore certain values
527 // for the purpose of common value optimization. A value that is ignored must have the
528 // default value for the property type. This is to ensure that significant value are not
529 // ignored by accident. The purpose of this is to allow the snapshot generation to reflect
530 // the behavior of the runtime. e.g. if a property is ignored on the host then a property
531 // that is common for android can be treated as if it was common for android and host as
532 // the setting for host is ignored anyway.
533 // * `sdk:"ignored-on-host" - this indicates the property is ignored on the host variant.
534 //
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000535 // * The sdk module type populates the BpModule structure, creating the arch specific
536 // structure and calls AddToPropertySet(...) on the properties struct to add the member
537 // specific properties in the correct place in the structure.
538 //
Paul Duffin3a4eb502020-03-19 16:11:18 +0000539 AddPrebuiltModule(ctx SdkMemberContext, member SdkMember) BpModule
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000540
Paul Duffin94289702021-09-09 15:38:32 +0100541 // CreateVariantPropertiesStruct creates a structure into which variant specific properties can be
542 // added.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000543 CreateVariantPropertiesStruct() SdkMemberProperties
Paul Duffin13879572019-11-28 14:31:38 +0000544}
Paul Duffin255f18e2019-12-13 11:22:16 +0000545
Paul Duffin296701e2021-07-14 10:29:36 +0100546// SdkDependencyContext provides access to information needed by the SdkMemberType.AddDependencies()
547// implementations.
548type SdkDependencyContext interface {
549 BottomUpMutatorContext
550}
551
Paul Duffin94289702021-09-09 15:38:32 +0100552// SdkMemberTypeBase is the base type for SdkMemberType implementations and must be embedded in any
553// struct that implements SdkMemberType.
Paul Duffin255f18e2019-12-13 11:22:16 +0000554type SdkMemberTypeBase struct {
Paul Duffin13082052021-05-11 00:31:38 +0100555 PropertyName string
556
557 // When set to true BpPropertyNotRequired indicates that the member type does not require the
558 // property to be specifiable in an Android.bp file.
559 BpPropertyNotRequired bool
560
Paul Duffin2d3da312021-05-06 12:02:27 +0100561 SupportsSdk bool
562 HostOsDependent bool
Paul Duffin0d4ed0a2021-05-10 23:58:40 +0100563
564 // When set to true UseSourceModuleTypeInSnapshot indicates that the member type creates a source
565 // module type in its SdkMemberType.AddPrebuiltModule() method. That prevents the sdk snapshot
566 // code from automatically adding a prefer: true flag.
567 UseSourceModuleTypeInSnapshot bool
Paul Duffin255f18e2019-12-13 11:22:16 +0000568}
569
570func (b *SdkMemberTypeBase) SdkPropertyName() string {
571 return b.PropertyName
572}
573
Paul Duffin13082052021-05-11 00:31:38 +0100574func (b *SdkMemberTypeBase) RequiresBpProperty() bool {
575 return !b.BpPropertyNotRequired
576}
577
Paul Duffine6029182019-12-16 17:43:48 +0000578func (b *SdkMemberTypeBase) UsableWithSdkAndSdkSnapshot() bool {
579 return b.SupportsSdk
580}
581
Martin Stjernholmcaa47d72020-07-11 04:52:24 +0100582func (b *SdkMemberTypeBase) IsHostOsDependent() bool {
583 return b.HostOsDependent
584}
585
Paul Duffin0d4ed0a2021-05-10 23:58:40 +0100586func (b *SdkMemberTypeBase) UsesSourceModuleTypeInSnapshot() bool {
587 return b.UseSourceModuleTypeInSnapshot
588}
589
Paul Duffin94289702021-09-09 15:38:32 +0100590// SdkMemberTypesRegistry encapsulates the information about registered SdkMemberTypes.
Paul Duffin255f18e2019-12-13 11:22:16 +0000591type SdkMemberTypesRegistry struct {
592 // The list of types sorted by property name.
593 list []SdkMemberType
Paul Duffin255f18e2019-12-13 11:22:16 +0000594}
595
Paul Duffine6029182019-12-16 17:43:48 +0000596func (r *SdkMemberTypesRegistry) copyAndAppend(memberType SdkMemberType) *SdkMemberTypesRegistry {
597 oldList := r.list
Paul Duffin255f18e2019-12-13 11:22:16 +0000598
599 // Copy the slice just in case this is being read while being modified, e.g. when testing.
600 list := make([]SdkMemberType, 0, len(oldList)+1)
601 list = append(list, oldList...)
602 list = append(list, memberType)
603
604 // Sort the member types by their property name to ensure that registry order has no effect
605 // on behavior.
606 sort.Slice(list, func(i1, i2 int) bool {
607 t1 := list[i1]
608 t2 := list[i2]
609
610 return t1.SdkPropertyName() < t2.SdkPropertyName()
611 })
612
Paul Duffin255f18e2019-12-13 11:22:16 +0000613 // Create a new registry so the pointer uniquely identifies the set of registered types.
Paul Duffine6029182019-12-16 17:43:48 +0000614 return &SdkMemberTypesRegistry{
Paul Duffin255f18e2019-12-13 11:22:16 +0000615 list: list,
Paul Duffin255f18e2019-12-13 11:22:16 +0000616 }
617}
Paul Duffine6029182019-12-16 17:43:48 +0000618
619func (r *SdkMemberTypesRegistry) RegisteredTypes() []SdkMemberType {
620 return r.list
621}
622
623func (r *SdkMemberTypesRegistry) UniqueOnceKey() OnceKey {
624 // Use the pointer to the registry as the unique key.
625 return NewCustomOnceKey(r)
626}
627
Paul Duffin94289702021-09-09 15:38:32 +0100628// ModuleExportsMemberTypes is the set of registered SdkMemberTypes for module_exports modules.
Paul Duffine6029182019-12-16 17:43:48 +0000629var ModuleExportsMemberTypes = &SdkMemberTypesRegistry{}
Paul Duffin62782de2021-07-14 12:05:16 +0100630
Paul Duffin94289702021-09-09 15:38:32 +0100631// SdkMemberTypes is the set of registered SdkMemberTypes for sdk modules.
Paul Duffine6029182019-12-16 17:43:48 +0000632var SdkMemberTypes = &SdkMemberTypesRegistry{}
633
Paul Duffin94289702021-09-09 15:38:32 +0100634// RegisterSdkMemberType registers an SdkMemberType object to allow them to be used in the
635// module_exports, module_exports_snapshot and (depending on the value returned from
636// SdkMemberType.UsableWithSdkAndSdkSnapshot) the sdk and sdk_snapshot module types.
Paul Duffine6029182019-12-16 17:43:48 +0000637func RegisterSdkMemberType(memberType SdkMemberType) {
638 // All member types are usable with module_exports.
639 ModuleExportsMemberTypes = ModuleExportsMemberTypes.copyAndAppend(memberType)
640
641 // Only those that explicitly indicate it are usable with sdk.
642 if memberType.UsableWithSdkAndSdkSnapshot() {
643 SdkMemberTypes = SdkMemberTypes.copyAndAppend(memberType)
644 }
645}
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000646
Paul Duffin94289702021-09-09 15:38:32 +0100647// SdkMemberPropertiesBase is the base structure for all implementations of SdkMemberProperties and
648// must be embedded in any struct that implements SdkMemberProperties.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000649//
Martin Stjernholm89238f42020-07-10 00:14:03 +0100650// Contains common properties that apply across many different member types.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000651type SdkMemberPropertiesBase struct {
Paul Duffina04c1072020-03-02 10:16:35 +0000652 // The number of unique os types supported by the member variants.
Paul Duffina551a1c2020-03-17 21:04:24 +0000653 //
654 // If a member has a variant with more than one os type then it will need to differentiate
655 // the locations of any of their prebuilt files in the snapshot by os type to prevent them
656 // from colliding. See OsPrefix().
657 //
658 // This property is the same for all variants of a member and so would be optimized away
659 // if it was not explicitly kept.
Paul Duffinb07fa512020-03-10 22:17:04 +0000660 Os_count int `sdk:"keep"`
Paul Duffina04c1072020-03-02 10:16:35 +0000661
662 // The os type for which these properties refer.
Paul Duffina551a1c2020-03-17 21:04:24 +0000663 //
664 // Provided to allow a member to differentiate between os types in the locations of their
665 // prebuilt files when it supports more than one os type.
666 //
667 // This property is the same for all os type specific variants of a member and so would be
668 // optimized away if it was not explicitly kept.
Paul Duffinb07fa512020-03-10 22:17:04 +0000669 Os OsType `sdk:"keep"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000670
671 // The setting to use for the compile_multilib property.
Martin Stjernholm89238f42020-07-10 00:14:03 +0100672 Compile_multilib string `android:"arch_variant"`
Paul Duffina04c1072020-03-02 10:16:35 +0000673}
674
Paul Duffin94289702021-09-09 15:38:32 +0100675// OsPrefix returns the os prefix to use for any file paths in the sdk.
Paul Duffina04c1072020-03-02 10:16:35 +0000676//
677// Is an empty string if the member only provides variants for a single os type, otherwise
678// is the OsType.Name.
679func (b *SdkMemberPropertiesBase) OsPrefix() string {
680 if b.Os_count == 1 {
681 return ""
682 } else {
683 return b.Os.Name
684 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000685}
686
687func (b *SdkMemberPropertiesBase) Base() *SdkMemberPropertiesBase {
688 return b
689}
690
Paul Duffin94289702021-09-09 15:38:32 +0100691// SdkMemberProperties is the interface to be implemented on top of a structure that contains
692// variant specific information.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000693//
Paul Duffin94289702021-09-09 15:38:32 +0100694// Struct fields that are capitalized are examined for common values to extract. Fields that are not
695// capitalized are assumed to be arch specific.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000696type SdkMemberProperties interface {
Paul Duffin94289702021-09-09 15:38:32 +0100697 // Base returns the base structure.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000698 Base() *SdkMemberPropertiesBase
699
Paul Duffin94289702021-09-09 15:38:32 +0100700 // PopulateFromVariant populates this structure with information from a module variant.
701 //
702 // 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 +0000703 PopulateFromVariant(ctx SdkMemberContext, variant Module)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000704
Paul Duffin94289702021-09-09 15:38:32 +0100705 // AddToPropertySet adds the information from this structure to the property set.
706 //
707 // This will be called for each instance of this structure on which the PopulateFromVariant method
708 // was called and also on a number of different instances of this structure into which properties
709 // common to one or more variants have been copied. Therefore, implementations of this must handle
710 // the case when this structure is only partially populated.
Paul Duffin3a4eb502020-03-19 16:11:18 +0000711 AddToPropertySet(ctx SdkMemberContext, propertySet BpPropertySet)
712}
713
Paul Duffin94289702021-09-09 15:38:32 +0100714// SdkMemberContext provides access to information common to a specific member.
Paul Duffin3a4eb502020-03-19 16:11:18 +0000715type SdkMemberContext interface {
716
Paul Duffin94289702021-09-09 15:38:32 +0100717 // SdkModuleContext returns the module context of the sdk common os variant which is creating the
718 // snapshot.
719 //
720 // This is common to all members of the sdk and is not specific to the member being processed.
721 // If information about the member being processed needs to be obtained from this ModuleContext it
722 // must be obtained using one of the OtherModule... methods not the Module... methods.
Paul Duffin3a4eb502020-03-19 16:11:18 +0000723 SdkModuleContext() ModuleContext
724
Paul Duffin94289702021-09-09 15:38:32 +0100725 // SnapshotBuilder the builder of the snapshot.
Paul Duffin3a4eb502020-03-19 16:11:18 +0000726 SnapshotBuilder() SnapshotBuilder
Paul Duffina551a1c2020-03-17 21:04:24 +0000727
Paul Duffin94289702021-09-09 15:38:32 +0100728 // MemberType returns the type of the member currently being processed.
Paul Duffina551a1c2020-03-17 21:04:24 +0000729 MemberType() SdkMemberType
730
Paul Duffin94289702021-09-09 15:38:32 +0100731 // Name returns the name of the member currently being processed.
Paul Duffina551a1c2020-03-17 21:04:24 +0000732 //
733 // Provided for use by sdk members to create a member specific location within the snapshot
734 // into which to copy the prebuilt files.
735 Name() string
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000736}
Paul Duffinb97b1572021-04-29 21:50:40 +0100737
738// ExportedComponentsInfo contains information about the components that this module exports to an
739// sdk snapshot.
740//
741// A component of a module is a child module that the module creates and which forms an integral
742// part of the functionality that the creating module provides. A component module is essentially
743// owned by its creator and is tightly coupled to the creator and other components.
744//
745// e.g. the child modules created by prebuilt_apis are not components because they are not tightly
746// coupled to the prebuilt_apis module. Once they are created the prebuilt_apis ignores them. The
747// child impl and stub library created by java_sdk_library (and corresponding import) are components
748// because the creating module depends upon them in order to provide some of its own functionality.
749//
750// A component is exported if it is part of an sdk snapshot. e.g. The xml and impl child modules are
751// components but they are not exported as they are not part of an sdk snapshot.
752//
753// This information is used by the sdk snapshot generation code to ensure that it does not create
754// an sdk snapshot that contains a declaration of the component module and the module that creates
755// it as that would result in duplicate modules when attempting to use the snapshot. e.g. a snapshot
756// that included the java_sdk_library_import "foo" and also a java_import "foo.stubs" would fail
757// as there would be two modules called "foo.stubs".
758type ExportedComponentsInfo struct {
759 // The names of the exported components.
760 Components []string
761}
762
763var ExportedComponentsInfoProvider = blueprint.NewProvider(ExportedComponentsInfo{})