blob: 2fdaf35aa8c3e3ff51c1ec75d04b73dbcfb97940 [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 Duffin03e7d0c2020-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
Jiyong Parkd1063c12019-07-17 20:08:41 +090037// SdkAware is the interface that must be supported by any module to become a member of SDK or to be
38// built with SDK
39type SdkAware interface {
40 Module
Paul Duffin03e7d0c2020-03-30 15:33:32 +010041 RequiredSdks
42
Jiyong Parkd1063c12019-07-17 20:08:41 +090043 sdkBase() *SdkBase
44 MakeMemberOf(sdk SdkRef)
45 IsInAnySdk() bool
46 ContainingSdk() SdkRef
47 MemberName() string
48 BuildWithSdks(sdks SdkRefs)
Jiyong Parkd1063c12019-07-17 20:08:41 +090049}
50
51// SdkRef refers to a version of an SDK
52type SdkRef struct {
53 Name string
54 Version string
55}
56
Jiyong Park9b409bc2019-10-11 14:59:13 +090057// Unversioned determines if the SdkRef is referencing to the unversioned SDK module
58func (s SdkRef) Unversioned() bool {
59 return s.Version == ""
Jiyong Parkd1063c12019-07-17 20:08:41 +090060}
61
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090062// String returns string representation of this SdkRef for debugging purpose
63func (s SdkRef) String() string {
64 if s.Name == "" {
65 return "(No Sdk)"
66 }
67 if s.Unversioned() {
68 return s.Name
69 }
70 return s.Name + string(SdkVersionSeparator) + s.Version
71}
72
Jiyong Park9b409bc2019-10-11 14:59:13 +090073// SdkVersionSeparator is a character used to separate an sdk name and its version
74const SdkVersionSeparator = '@'
Jiyong Parkd1063c12019-07-17 20:08:41 +090075
Jiyong Park9b409bc2019-10-11 14:59:13 +090076// ParseSdkRef parses a `name@version` style string into a corresponding SdkRef struct
Jiyong Parkd1063c12019-07-17 20:08:41 +090077func ParseSdkRef(ctx BaseModuleContext, str string, property string) SdkRef {
Jiyong Park9b409bc2019-10-11 14:59:13 +090078 tokens := strings.Split(str, string(SdkVersionSeparator))
Jiyong Parkd1063c12019-07-17 20:08:41 +090079 if len(tokens) < 1 || len(tokens) > 2 {
80 ctx.PropertyErrorf(property, "%q does not follow name#version syntax", str)
81 return SdkRef{Name: "invalid sdk name", Version: "invalid sdk version"}
82 }
83
84 name := tokens[0]
85
Jiyong Park9b409bc2019-10-11 14:59:13 +090086 var version string
Jiyong Parkd1063c12019-07-17 20:08:41 +090087 if len(tokens) == 2 {
88 version = tokens[1]
89 }
90
91 return SdkRef{Name: name, Version: version}
92}
93
94type SdkRefs []SdkRef
95
Jiyong Park9b409bc2019-10-11 14:59:13 +090096// Contains tells if the given SdkRef is in this list of SdkRef's
Jiyong Parkd1063c12019-07-17 20:08:41 +090097func (refs SdkRefs) Contains(s SdkRef) bool {
98 for _, r := range refs {
99 if r == s {
100 return true
101 }
102 }
103 return false
104}
105
106type sdkProperties struct {
107 // The SDK that this module is a member of. nil if it is not a member of any SDK
108 ContainingSdk *SdkRef `blueprint:"mutated"`
109
110 // The list of SDK names and versions that are used to build this module
111 RequiredSdks SdkRefs `blueprint:"mutated"`
112
113 // Name of the module that this sdk member is representing
114 Sdk_member_name *string
115}
116
117// SdkBase is a struct that is expected to be included in module types to implement the SdkAware
118// interface. InitSdkAwareModule should be called to initialize this struct.
119type SdkBase struct {
120 properties sdkProperties
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000121 module SdkAware
Jiyong Parkd1063c12019-07-17 20:08:41 +0900122}
123
124func (s *SdkBase) sdkBase() *SdkBase {
125 return s
126}
127
Jiyong Park9b409bc2019-10-11 14:59:13 +0900128// MakeMemberOf sets this module to be a member of a specific SDK
Jiyong Parkd1063c12019-07-17 20:08:41 +0900129func (s *SdkBase) MakeMemberOf(sdk SdkRef) {
130 s.properties.ContainingSdk = &sdk
131}
132
133// IsInAnySdk returns true if this module is a member of any SDK
134func (s *SdkBase) IsInAnySdk() bool {
135 return s.properties.ContainingSdk != nil
136}
137
138// ContainingSdk returns the SDK that this module is a member of
139func (s *SdkBase) ContainingSdk() SdkRef {
140 if s.properties.ContainingSdk != nil {
141 return *s.properties.ContainingSdk
142 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900143 return SdkRef{Name: "", Version: ""}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900144}
145
Jiyong Park9b409bc2019-10-11 14:59:13 +0900146// MemberName returns the name of the module that this SDK member is overriding
Jiyong Parkd1063c12019-07-17 20:08:41 +0900147func (s *SdkBase) MemberName() string {
148 return proptools.String(s.properties.Sdk_member_name)
149}
150
151// BuildWithSdks is used to mark that this module has to be built with the given SDK(s).
152func (s *SdkBase) BuildWithSdks(sdks SdkRefs) {
153 s.properties.RequiredSdks = sdks
154}
155
156// RequiredSdks returns the SDK(s) that this module has to be built with
157func (s *SdkBase) RequiredSdks() SdkRefs {
158 return s.properties.RequiredSdks
159}
160
161// InitSdkAwareModule initializes the SdkBase struct. This must be called by all modules including
162// SdkBase.
163func InitSdkAwareModule(m SdkAware) {
164 base := m.sdkBase()
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000165 base.module = m
Jiyong Parkd1063c12019-07-17 20:08:41 +0900166 m.AddProperties(&base.properties)
167}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000168
169// Provide support for generating the build rules which will build the snapshot.
170type SnapshotBuilder interface {
171 // Copy src to the dest (which is a snapshot relative path) and add the dest
172 // to the zip
173 CopyToSnapshot(src Path, dest string)
174
Paul Duffin91547182019-11-12 19:39:36 +0000175 // Unzip the supplied zip into the snapshot relative directory destDir.
176 UnzipToSnapshot(zipPath Path, destDir string)
177
Paul Duffinb645ec82019-11-27 17:43:54 +0000178 // Add a new prebuilt module to the snapshot. The returned module
179 // must be populated with the module type specific properties. The following
180 // properties will be automatically populated.
181 //
182 // * name
183 // * sdk_member_name
184 // * prefer
185 //
186 // This will result in two Soong modules being generated in the Android. One
187 // that is versioned, coupled to the snapshot version and marked as
188 // prefer=true. And one that is not versioned, not marked as prefer=true and
189 // will only be used if the equivalently named non-prebuilt module is not
190 // present.
Paul Duffin9d8d6092019-12-05 18:19:29 +0000191 AddPrebuiltModule(member SdkMember, moduleType string) BpModule
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000192
193 // The property tag to use when adding a property to a BpModule that contains
194 // references to other sdk members. Using this will ensure that the reference
195 // is correctly output for both versioned and unversioned prebuilts in the
196 // snapshot.
197 //
Paul Duffin206433a2020-03-06 12:30:43 +0000198 // "required: true" means that the property must only contain references
199 // to other members of the sdk. Passing a reference to a module that is not a
200 // member of the sdk will result in a build error.
201 //
202 // "required: false" means that the property can contain references to modules
203 // that are either members or not members of the sdk. If a reference is to a
204 // module that is a non member then the reference is left unchanged, i.e. it
205 // is not transformed as references to members are.
206 //
207 // The handling of the member names is dependent on whether it is an internal or
208 // exported member. An exported member is one whose name is specified in one of
209 // the member type specific properties. An internal member is one that is added
210 // due to being a part of an exported (or other internal) member and is not itself
211 // an exported member.
212 //
213 // Member names are handled as follows:
214 // * When creating the unversioned form of the module the name is left unchecked
215 // unless the member is internal in which case it is transformed into an sdk
216 // specific name, i.e. by prefixing with the sdk name.
217 //
218 // * When creating the versioned form of the module the name is transformed into
219 // a versioned sdk specific name, i.e. by prefixing with the sdk name and
220 // suffixing with the version.
221 //
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000222 // e.g.
Paul Duffin206433a2020-03-06 12:30:43 +0000223 // bpPropertySet.AddPropertyWithTag("libs", []string{"member1", "member2"}, builder.SdkMemberReferencePropertyTag(true))
224 SdkMemberReferencePropertyTag(required bool) BpPropertyTag
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000225}
226
Paul Duffin5b511a22020-01-15 14:23:52 +0000227type BpPropertyTag interface{}
228
Paul Duffinb645ec82019-11-27 17:43:54 +0000229// A set of properties for use in a .bp file.
230type BpPropertySet interface {
231 // Add a property, the value can be one of the following types:
232 // * string
233 // * array of the above
234 // * bool
235 // * BpPropertySet
236 //
Paul Duffin5b511a22020-01-15 14:23:52 +0000237 // It is an error if multiple properties with the same name are added.
Paul Duffinb645ec82019-11-27 17:43:54 +0000238 AddProperty(name string, value interface{})
239
Paul Duffin5b511a22020-01-15 14:23:52 +0000240 // Add a property with an associated tag
241 AddPropertyWithTag(name string, value interface{}, tag BpPropertyTag)
242
Paul Duffinb645ec82019-11-27 17:43:54 +0000243 // Add a property set with the specified name and return so that additional
244 // properties can be added.
245 AddPropertySet(name string) BpPropertySet
246}
247
248// A .bp module definition.
249type BpModule interface {
250 BpPropertySet
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000251}
Paul Duffin13879572019-11-28 14:31:38 +0000252
253// An individual member of the SDK, includes all of the variants that the SDK
254// requires.
255type SdkMember interface {
256 // The name of the member.
257 Name() string
258
259 // All the variants required by the SDK.
260 Variants() []SdkAware
261}
262
Paul Duffinf8539922019-11-19 19:44:10 +0000263type SdkMemberTypeDependencyTag interface {
264 blueprint.DependencyTag
265
266 SdkMemberType() SdkMemberType
267}
268
269type sdkMemberDependencyTag struct {
270 blueprint.BaseDependencyTag
271 memberType SdkMemberType
272}
273
274func (t *sdkMemberDependencyTag) SdkMemberType() SdkMemberType {
275 return t.memberType
276}
277
278func DependencyTagForSdkMemberType(memberType SdkMemberType) SdkMemberTypeDependencyTag {
279 return &sdkMemberDependencyTag{memberType: memberType}
280}
281
Paul Duffin13879572019-11-28 14:31:38 +0000282// Interface that must be implemented for every type that can be a member of an
283// sdk.
284//
285// The basic implementation should look something like this, where ModuleType is
286// the name of the module type being supported.
287//
Paul Duffin255f18e2019-12-13 11:22:16 +0000288// type moduleTypeSdkMemberType struct {
289// android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +0000290// }
291//
Paul Duffin255f18e2019-12-13 11:22:16 +0000292// func init() {
293// android.RegisterSdkMemberType(&moduleTypeSdkMemberType{
294// SdkMemberTypeBase: android.SdkMemberTypeBase{
295// PropertyName: "module_types",
296// },
297// }
Paul Duffin13879572019-11-28 14:31:38 +0000298// }
299//
300// ...methods...
301//
302type SdkMemberType interface {
Paul Duffin255f18e2019-12-13 11:22:16 +0000303 // The name of the member type property on an sdk module.
304 SdkPropertyName() string
305
Paul Duffine6029182019-12-16 17:43:48 +0000306 // True if the member type supports the sdk/sdk_snapshot, false otherwise.
307 UsableWithSdkAndSdkSnapshot() bool
308
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000309 // Return true if modules of this type can have dependencies which should be
310 // treated as if they are sdk members.
311 //
312 // Any dependency that is to be treated as a member of the sdk needs to implement
313 // SdkAware and be added with an SdkMemberTypeDependencyTag tag.
314 HasTransitiveSdkMembers() bool
315
Paul Duffin13879572019-11-28 14:31:38 +0000316 // Add dependencies from the SDK module to all the variants the member
317 // contributes to the SDK. The exact set of variants required is determined
318 // by the SDK and its properties. The dependencies must be added with the
319 // supplied tag.
320 //
321 // The BottomUpMutatorContext provided is for the SDK module.
322 AddDependencies(mctx BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string)
323
324 // Return true if the supplied module is an instance of this member type.
325 //
326 // This is used to check the type of each variant before added to the
327 // SdkMember. Returning false will cause an error to be logged expaining that
328 // the module is not allowed in whichever sdk property it was added.
329 IsInstance(module Module) bool
330
Paul Duffin33cedcc2020-02-27 16:00:53 +0000331 // Add a prebuilt module that the sdk will populate.
332 //
333 // Returning nil from this will cause the sdk module type to use the deprecated BuildSnapshot
334 // method to build the snapshot. That method is deprecated because it requires the SdkMemberType
335 // implementation to do all the word.
336 //
337 // Otherwise, returning a non-nil value from this will cause the sdk module type to do the
338 // majority of the work to generate the snapshot. The sdk module code generates the snapshot
339 // as follows:
340 //
341 // * A properties struct of type SdkMemberProperties is created for each variant and
342 // populated with information from the variant by calling PopulateFromVariant(SdkAware)
343 // on the struct.
344 //
345 // * An additional properties struct is created into which the common properties will be
346 // added.
347 //
348 // * The variant property structs are analysed to find exported (capitalized) fields which
349 // have common values. Those fields are cleared and the common value added to the common
Paul Duffin49096812020-03-10 22:17:04 +0000350 // properties. A field annotated with a tag of `sdk:"keep"` will be treated as if it
351 // was not capitalized, i.e. not optimized for common values.
Paul Duffin33cedcc2020-02-27 16:00:53 +0000352 //
353 // * The sdk module type populates the BpModule structure, creating the arch specific
354 // structure and calls AddToPropertySet(...) on the properties struct to add the member
355 // specific properties in the correct place in the structure.
356 //
Paul Duffin17ab8832020-03-19 16:11:18 +0000357 AddPrebuiltModule(ctx SdkMemberContext, member SdkMember) BpModule
Paul Duffin33cedcc2020-02-27 16:00:53 +0000358
Paul Duffin33cedcc2020-02-27 16:00:53 +0000359 // Create a structure into which variant specific properties can be added.
360 CreateVariantPropertiesStruct() SdkMemberProperties
Paul Duffin13879572019-11-28 14:31:38 +0000361}
Paul Duffin255f18e2019-12-13 11:22:16 +0000362
Paul Duffine6029182019-12-16 17:43:48 +0000363// Base type for SdkMemberType implementations.
Paul Duffin255f18e2019-12-13 11:22:16 +0000364type SdkMemberTypeBase struct {
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000365 PropertyName string
366 SupportsSdk bool
367 TransitiveSdkMembers bool
Paul Duffin255f18e2019-12-13 11:22:16 +0000368}
369
370func (b *SdkMemberTypeBase) SdkPropertyName() string {
371 return b.PropertyName
372}
373
Paul Duffine6029182019-12-16 17:43:48 +0000374func (b *SdkMemberTypeBase) UsableWithSdkAndSdkSnapshot() bool {
375 return b.SupportsSdk
376}
377
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000378func (b *SdkMemberTypeBase) HasTransitiveSdkMembers() bool {
379 return b.TransitiveSdkMembers
380}
381
Paul Duffin255f18e2019-12-13 11:22:16 +0000382// Encapsulates the information about registered SdkMemberTypes.
383type SdkMemberTypesRegistry struct {
384 // The list of types sorted by property name.
385 list []SdkMemberType
386
387 // The key that uniquely identifies this registry instance.
388 key OnceKey
389}
390
Paul Duffine6029182019-12-16 17:43:48 +0000391func (r *SdkMemberTypesRegistry) copyAndAppend(memberType SdkMemberType) *SdkMemberTypesRegistry {
392 oldList := r.list
Paul Duffin255f18e2019-12-13 11:22:16 +0000393
394 // Copy the slice just in case this is being read while being modified, e.g. when testing.
395 list := make([]SdkMemberType, 0, len(oldList)+1)
396 list = append(list, oldList...)
397 list = append(list, memberType)
398
399 // Sort the member types by their property name to ensure that registry order has no effect
400 // on behavior.
401 sort.Slice(list, func(i1, i2 int) bool {
402 t1 := list[i1]
403 t2 := list[i2]
404
405 return t1.SdkPropertyName() < t2.SdkPropertyName()
406 })
407
408 // Generate a key that identifies the slice of SdkMemberTypes by joining the property names
409 // from all the SdkMemberType .
410 var properties []string
411 for _, t := range list {
412 properties = append(properties, t.SdkPropertyName())
413 }
414 key := NewOnceKey(strings.Join(properties, "|"))
415
416 // Create a new registry so the pointer uniquely identifies the set of registered types.
Paul Duffine6029182019-12-16 17:43:48 +0000417 return &SdkMemberTypesRegistry{
Paul Duffin255f18e2019-12-13 11:22:16 +0000418 list: list,
419 key: key,
420 }
421}
Paul Duffine6029182019-12-16 17:43:48 +0000422
423func (r *SdkMemberTypesRegistry) RegisteredTypes() []SdkMemberType {
424 return r.list
425}
426
427func (r *SdkMemberTypesRegistry) UniqueOnceKey() OnceKey {
428 // Use the pointer to the registry as the unique key.
429 return NewCustomOnceKey(r)
430}
431
432// The set of registered SdkMemberTypes, one for sdk module and one for module_exports.
433var ModuleExportsMemberTypes = &SdkMemberTypesRegistry{}
434var SdkMemberTypes = &SdkMemberTypesRegistry{}
435
436// Register an SdkMemberType object to allow them to be used in the sdk and sdk_snapshot module
437// types.
438func RegisterSdkMemberType(memberType SdkMemberType) {
439 // All member types are usable with module_exports.
440 ModuleExportsMemberTypes = ModuleExportsMemberTypes.copyAndAppend(memberType)
441
442 // Only those that explicitly indicate it are usable with sdk.
443 if memberType.UsableWithSdkAndSdkSnapshot() {
444 SdkMemberTypes = SdkMemberTypes.copyAndAppend(memberType)
445 }
446}
Paul Duffin33cedcc2020-02-27 16:00:53 +0000447
448// Base structure for all implementations of SdkMemberProperties.
449//
Paul Duffin9b358d72020-03-02 10:16:35 +0000450// Contains common properties that apply across many different member types. These
451// are not affected by the optimization to extract common values.
Paul Duffin33cedcc2020-02-27 16:00:53 +0000452type SdkMemberPropertiesBase struct {
Paul Duffin9b358d72020-03-02 10:16:35 +0000453 // The number of unique os types supported by the member variants.
Paul Duffinc9103932020-03-17 21:04:24 +0000454 //
455 // If a member has a variant with more than one os type then it will need to differentiate
456 // the locations of any of their prebuilt files in the snapshot by os type to prevent them
457 // from colliding. See OsPrefix().
458 //
459 // This property is the same for all variants of a member and so would be optimized away
460 // if it was not explicitly kept.
Paul Duffin49096812020-03-10 22:17:04 +0000461 Os_count int `sdk:"keep"`
Paul Duffin9b358d72020-03-02 10:16:35 +0000462
463 // The os type for which these properties refer.
Paul Duffinc9103932020-03-17 21:04:24 +0000464 //
465 // Provided to allow a member to differentiate between os types in the locations of their
466 // prebuilt files when it supports more than one os type.
467 //
468 // This property is the same for all os type specific variants of a member and so would be
469 // optimized away if it was not explicitly kept.
Paul Duffin49096812020-03-10 22:17:04 +0000470 Os OsType `sdk:"keep"`
Paul Duffinc9103932020-03-17 21:04:24 +0000471
472 // The setting to use for the compile_multilib property.
473 //
474 // This property is set after optimization so there is no point in trying to optimize it.
475 Compile_multilib string `sdk:"keep"`
Paul Duffin9b358d72020-03-02 10:16:35 +0000476}
477
478// The os prefix to use for any file paths in the sdk.
479//
480// Is an empty string if the member only provides variants for a single os type, otherwise
481// is the OsType.Name.
482func (b *SdkMemberPropertiesBase) OsPrefix() string {
483 if b.Os_count == 1 {
484 return ""
485 } else {
486 return b.Os.Name
487 }
Paul Duffin33cedcc2020-02-27 16:00:53 +0000488}
489
490func (b *SdkMemberPropertiesBase) Base() *SdkMemberPropertiesBase {
491 return b
492}
493
494// Interface to be implemented on top of a structure that contains variant specific
495// information.
496//
497// Struct fields that are capitalized are examined for common values to extract. Fields
498// that are not capitalized are assumed to be arch specific.
499type SdkMemberProperties interface {
500 // Access the base structure.
501 Base() *SdkMemberPropertiesBase
502
Paul Duffin17ab8832020-03-19 16:11:18 +0000503 // Populate this structure with information from the variant.
504 PopulateFromVariant(ctx SdkMemberContext, variant Module)
Paul Duffin33cedcc2020-02-27 16:00:53 +0000505
Paul Duffin17ab8832020-03-19 16:11:18 +0000506 // Add the information from this structure to the property set.
507 AddToPropertySet(ctx SdkMemberContext, propertySet BpPropertySet)
508}
509
510// Provides access to information common to a specific member.
511type SdkMemberContext interface {
512
513 // The module context of the sdk common os variant which is creating the snapshot.
514 SdkModuleContext() ModuleContext
515
516 // The builder of the snapshot.
517 SnapshotBuilder() SnapshotBuilder
Paul Duffinc9103932020-03-17 21:04:24 +0000518
519 // The type of the member.
520 MemberType() SdkMemberType
521
522 // The name of the member.
523 //
524 // Provided for use by sdk members to create a member specific location within the snapshot
525 // into which to copy the prebuilt files.
526 Name() string
Paul Duffin33cedcc2020-02-27 16:00:53 +0000527}