blob: 66094cda0937b7bf0737f83e9e184d5f5defbfb8 [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
25// SdkAware is the interface that must be supported by any module to become a member of SDK or to be
26// built with SDK
27type SdkAware interface {
28 Module
29 sdkBase() *SdkBase
30 MakeMemberOf(sdk SdkRef)
31 IsInAnySdk() bool
32 ContainingSdk() SdkRef
33 MemberName() string
34 BuildWithSdks(sdks SdkRefs)
35 RequiredSdks() SdkRefs
36}
37
38// SdkRef refers to a version of an SDK
39type SdkRef struct {
40 Name string
41 Version string
42}
43
Jiyong Park9b409bc2019-10-11 14:59:13 +090044// Unversioned determines if the SdkRef is referencing to the unversioned SDK module
45func (s SdkRef) Unversioned() bool {
46 return s.Version == ""
Jiyong Parkd1063c12019-07-17 20:08:41 +090047}
48
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090049// String returns string representation of this SdkRef for debugging purpose
50func (s SdkRef) String() string {
51 if s.Name == "" {
52 return "(No Sdk)"
53 }
54 if s.Unversioned() {
55 return s.Name
56 }
57 return s.Name + string(SdkVersionSeparator) + s.Version
58}
59
Jiyong Park9b409bc2019-10-11 14:59:13 +090060// SdkVersionSeparator is a character used to separate an sdk name and its version
61const SdkVersionSeparator = '@'
Jiyong Parkd1063c12019-07-17 20:08:41 +090062
Jiyong Park9b409bc2019-10-11 14:59:13 +090063// ParseSdkRef parses a `name@version` style string into a corresponding SdkRef struct
Jiyong Parkd1063c12019-07-17 20:08:41 +090064func ParseSdkRef(ctx BaseModuleContext, str string, property string) SdkRef {
Jiyong Park9b409bc2019-10-11 14:59:13 +090065 tokens := strings.Split(str, string(SdkVersionSeparator))
Jiyong Parkd1063c12019-07-17 20:08:41 +090066 if len(tokens) < 1 || len(tokens) > 2 {
67 ctx.PropertyErrorf(property, "%q does not follow name#version syntax", str)
68 return SdkRef{Name: "invalid sdk name", Version: "invalid sdk version"}
69 }
70
71 name := tokens[0]
72
Jiyong Park9b409bc2019-10-11 14:59:13 +090073 var version string
Jiyong Parkd1063c12019-07-17 20:08:41 +090074 if len(tokens) == 2 {
75 version = tokens[1]
76 }
77
78 return SdkRef{Name: name, Version: version}
79}
80
81type SdkRefs []SdkRef
82
Jiyong Park9b409bc2019-10-11 14:59:13 +090083// Contains tells if the given SdkRef is in this list of SdkRef's
Jiyong Parkd1063c12019-07-17 20:08:41 +090084func (refs SdkRefs) Contains(s SdkRef) bool {
85 for _, r := range refs {
86 if r == s {
87 return true
88 }
89 }
90 return false
91}
92
93type sdkProperties struct {
94 // The SDK that this module is a member of. nil if it is not a member of any SDK
95 ContainingSdk *SdkRef `blueprint:"mutated"`
96
97 // The list of SDK names and versions that are used to build this module
98 RequiredSdks SdkRefs `blueprint:"mutated"`
99
100 // Name of the module that this sdk member is representing
101 Sdk_member_name *string
102}
103
104// SdkBase is a struct that is expected to be included in module types to implement the SdkAware
105// interface. InitSdkAwareModule should be called to initialize this struct.
106type SdkBase struct {
107 properties sdkProperties
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000108 module SdkAware
Jiyong Parkd1063c12019-07-17 20:08:41 +0900109}
110
111func (s *SdkBase) sdkBase() *SdkBase {
112 return s
113}
114
Jiyong Park9b409bc2019-10-11 14:59:13 +0900115// MakeMemberOf sets this module to be a member of a specific SDK
Jiyong Parkd1063c12019-07-17 20:08:41 +0900116func (s *SdkBase) MakeMemberOf(sdk SdkRef) {
117 s.properties.ContainingSdk = &sdk
118}
119
120// IsInAnySdk returns true if this module is a member of any SDK
121func (s *SdkBase) IsInAnySdk() bool {
122 return s.properties.ContainingSdk != nil
123}
124
125// ContainingSdk returns the SDK that this module is a member of
126func (s *SdkBase) ContainingSdk() SdkRef {
127 if s.properties.ContainingSdk != nil {
128 return *s.properties.ContainingSdk
129 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900130 return SdkRef{Name: "", Version: ""}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900131}
132
Jiyong Park9b409bc2019-10-11 14:59:13 +0900133// MemberName returns the name of the module that this SDK member is overriding
Jiyong Parkd1063c12019-07-17 20:08:41 +0900134func (s *SdkBase) MemberName() string {
135 return proptools.String(s.properties.Sdk_member_name)
136}
137
138// BuildWithSdks is used to mark that this module has to be built with the given SDK(s).
139func (s *SdkBase) BuildWithSdks(sdks SdkRefs) {
140 s.properties.RequiredSdks = sdks
141}
142
143// RequiredSdks returns the SDK(s) that this module has to be built with
144func (s *SdkBase) RequiredSdks() SdkRefs {
145 return s.properties.RequiredSdks
146}
147
148// InitSdkAwareModule initializes the SdkBase struct. This must be called by all modules including
149// SdkBase.
150func InitSdkAwareModule(m SdkAware) {
151 base := m.sdkBase()
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000152 base.module = m
Jiyong Parkd1063c12019-07-17 20:08:41 +0900153 m.AddProperties(&base.properties)
154}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000155
156// Provide support for generating the build rules which will build the snapshot.
157type SnapshotBuilder interface {
158 // Copy src to the dest (which is a snapshot relative path) and add the dest
159 // to the zip
160 CopyToSnapshot(src Path, dest string)
161
Paul Duffin91547182019-11-12 19:39:36 +0000162 // Unzip the supplied zip into the snapshot relative directory destDir.
163 UnzipToSnapshot(zipPath Path, destDir string)
164
Paul Duffinb645ec82019-11-27 17:43:54 +0000165 // Add a new prebuilt module to the snapshot. The returned module
166 // must be populated with the module type specific properties. The following
167 // properties will be automatically populated.
168 //
169 // * name
170 // * sdk_member_name
171 // * prefer
172 //
173 // This will result in two Soong modules being generated in the Android. One
174 // that is versioned, coupled to the snapshot version and marked as
175 // prefer=true. And one that is not versioned, not marked as prefer=true and
176 // will only be used if the equivalently named non-prebuilt module is not
177 // present.
Paul Duffin9d8d6092019-12-05 18:19:29 +0000178 AddPrebuiltModule(member SdkMember, moduleType string) BpModule
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000179
180 // The property tag to use when adding a property to a BpModule that contains
181 // references to other sdk members. Using this will ensure that the reference
182 // is correctly output for both versioned and unversioned prebuilts in the
183 // snapshot.
184 //
Paul Duffin13f02712020-03-06 12:30:43 +0000185 // "required: true" means that the property must only contain references
186 // to other members of the sdk. Passing a reference to a module that is not a
187 // member of the sdk will result in a build error.
188 //
189 // "required: false" means that the property can contain references to modules
190 // that are either members or not members of the sdk. If a reference is to a
191 // module that is a non member then the reference is left unchanged, i.e. it
192 // is not transformed as references to members are.
193 //
194 // The handling of the member names is dependent on whether it is an internal or
195 // exported member. An exported member is one whose name is specified in one of
196 // the member type specific properties. An internal member is one that is added
197 // due to being a part of an exported (or other internal) member and is not itself
198 // an exported member.
199 //
200 // Member names are handled as follows:
201 // * When creating the unversioned form of the module the name is left unchecked
202 // unless the member is internal in which case it is transformed into an sdk
203 // specific name, i.e. by prefixing with the sdk name.
204 //
205 // * When creating the versioned form of the module the name is transformed into
206 // a versioned sdk specific name, i.e. by prefixing with the sdk name and
207 // suffixing with the version.
208 //
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000209 // e.g.
Paul Duffin13f02712020-03-06 12:30:43 +0000210 // bpPropertySet.AddPropertyWithTag("libs", []string{"member1", "member2"}, builder.SdkMemberReferencePropertyTag(true))
211 SdkMemberReferencePropertyTag(required bool) BpPropertyTag
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000212}
213
Paul Duffin5b511a22020-01-15 14:23:52 +0000214type BpPropertyTag interface{}
215
Paul Duffinb645ec82019-11-27 17:43:54 +0000216// A set of properties for use in a .bp file.
217type BpPropertySet interface {
218 // Add a property, the value can be one of the following types:
219 // * string
220 // * array of the above
221 // * bool
222 // * BpPropertySet
223 //
Paul Duffin5b511a22020-01-15 14:23:52 +0000224 // It is an error if multiple properties with the same name are added.
Paul Duffinb645ec82019-11-27 17:43:54 +0000225 AddProperty(name string, value interface{})
226
Paul Duffin5b511a22020-01-15 14:23:52 +0000227 // Add a property with an associated tag
228 AddPropertyWithTag(name string, value interface{}, tag BpPropertyTag)
229
Paul Duffinb645ec82019-11-27 17:43:54 +0000230 // Add a property set with the specified name and return so that additional
231 // properties can be added.
232 AddPropertySet(name string) BpPropertySet
233}
234
235// A .bp module definition.
236type BpModule interface {
237 BpPropertySet
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000238}
Paul Duffin13879572019-11-28 14:31:38 +0000239
240// An individual member of the SDK, includes all of the variants that the SDK
241// requires.
242type SdkMember interface {
243 // The name of the member.
244 Name() string
245
246 // All the variants required by the SDK.
247 Variants() []SdkAware
248}
249
Paul Duffinf8539922019-11-19 19:44:10 +0000250type SdkMemberTypeDependencyTag interface {
251 blueprint.DependencyTag
252
253 SdkMemberType() SdkMemberType
254}
255
256type sdkMemberDependencyTag struct {
257 blueprint.BaseDependencyTag
258 memberType SdkMemberType
259}
260
261func (t *sdkMemberDependencyTag) SdkMemberType() SdkMemberType {
262 return t.memberType
263}
264
265func DependencyTagForSdkMemberType(memberType SdkMemberType) SdkMemberTypeDependencyTag {
266 return &sdkMemberDependencyTag{memberType: memberType}
267}
268
Paul Duffin13879572019-11-28 14:31:38 +0000269// Interface that must be implemented for every type that can be a member of an
270// sdk.
271//
272// The basic implementation should look something like this, where ModuleType is
273// the name of the module type being supported.
274//
Paul Duffin255f18e2019-12-13 11:22:16 +0000275// type moduleTypeSdkMemberType struct {
276// android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +0000277// }
278//
Paul Duffin255f18e2019-12-13 11:22:16 +0000279// func init() {
280// android.RegisterSdkMemberType(&moduleTypeSdkMemberType{
281// SdkMemberTypeBase: android.SdkMemberTypeBase{
282// PropertyName: "module_types",
283// },
284// }
Paul Duffin13879572019-11-28 14:31:38 +0000285// }
286//
287// ...methods...
288//
289type SdkMemberType interface {
Paul Duffin255f18e2019-12-13 11:22:16 +0000290 // The name of the member type property on an sdk module.
291 SdkPropertyName() string
292
Paul Duffine6029182019-12-16 17:43:48 +0000293 // True if the member type supports the sdk/sdk_snapshot, false otherwise.
294 UsableWithSdkAndSdkSnapshot() bool
295
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000296 // Return true if modules of this type can have dependencies which should be
297 // treated as if they are sdk members.
298 //
299 // Any dependency that is to be treated as a member of the sdk needs to implement
300 // SdkAware and be added with an SdkMemberTypeDependencyTag tag.
301 HasTransitiveSdkMembers() bool
302
Martin Stjernholmcd07bce2020-03-10 22:37:59 +0000303 // Add dependencies from the SDK module to all the module variants the member
304 // type contributes to the SDK. `names` is the list of module names given in
305 // the member type property (as returned by SdkPropertyName()) in the SDK
306 // module. The exact set of variants required is determined by the SDK and its
307 // properties. The dependencies must be added with the supplied tag.
Paul Duffin13879572019-11-28 14:31:38 +0000308 //
309 // The BottomUpMutatorContext provided is for the SDK module.
310 AddDependencies(mctx BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string)
311
312 // Return true if the supplied module is an instance of this member type.
313 //
314 // This is used to check the type of each variant before added to the
315 // SdkMember. Returning false will cause an error to be logged expaining that
316 // the module is not allowed in whichever sdk property it was added.
317 IsInstance(module Module) bool
318
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000319 // Add a prebuilt module that the sdk will populate.
320 //
321 // Returning nil from this will cause the sdk module type to use the deprecated BuildSnapshot
322 // method to build the snapshot. That method is deprecated because it requires the SdkMemberType
323 // implementation to do all the word.
324 //
325 // Otherwise, returning a non-nil value from this will cause the sdk module type to do the
326 // majority of the work to generate the snapshot. The sdk module code generates the snapshot
327 // as follows:
328 //
329 // * A properties struct of type SdkMemberProperties is created for each variant and
330 // populated with information from the variant by calling PopulateFromVariant(SdkAware)
331 // on the struct.
332 //
333 // * An additional properties struct is created into which the common properties will be
334 // added.
335 //
336 // * The variant property structs are analysed to find exported (capitalized) fields which
337 // have common values. Those fields are cleared and the common value added to the common
Paul Duffinb07fa512020-03-10 22:17:04 +0000338 // properties. A field annotated with a tag of `sdk:"keep"` will be treated as if it
339 // was not capitalized, i.e. not optimized for common values.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000340 //
341 // * The sdk module type populates the BpModule structure, creating the arch specific
342 // structure and calls AddToPropertySet(...) on the properties struct to add the member
343 // specific properties in the correct place in the structure.
344 //
Paul Duffin3a4eb502020-03-19 16:11:18 +0000345 AddPrebuiltModule(ctx SdkMemberContext, member SdkMember) BpModule
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000346
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000347 // Create a structure into which variant specific properties can be added.
348 CreateVariantPropertiesStruct() SdkMemberProperties
Paul Duffin13879572019-11-28 14:31:38 +0000349}
Paul Duffin255f18e2019-12-13 11:22:16 +0000350
Paul Duffine6029182019-12-16 17:43:48 +0000351// Base type for SdkMemberType implementations.
Paul Duffin255f18e2019-12-13 11:22:16 +0000352type SdkMemberTypeBase struct {
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000353 PropertyName string
354 SupportsSdk bool
355 TransitiveSdkMembers bool
Paul Duffin255f18e2019-12-13 11:22:16 +0000356}
357
358func (b *SdkMemberTypeBase) SdkPropertyName() string {
359 return b.PropertyName
360}
361
Paul Duffine6029182019-12-16 17:43:48 +0000362func (b *SdkMemberTypeBase) UsableWithSdkAndSdkSnapshot() bool {
363 return b.SupportsSdk
364}
365
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000366func (b *SdkMemberTypeBase) HasTransitiveSdkMembers() bool {
367 return b.TransitiveSdkMembers
368}
369
Paul Duffin255f18e2019-12-13 11:22:16 +0000370// Encapsulates the information about registered SdkMemberTypes.
371type SdkMemberTypesRegistry struct {
372 // The list of types sorted by property name.
373 list []SdkMemberType
374
375 // The key that uniquely identifies this registry instance.
376 key OnceKey
377}
378
Paul Duffine6029182019-12-16 17:43:48 +0000379func (r *SdkMemberTypesRegistry) copyAndAppend(memberType SdkMemberType) *SdkMemberTypesRegistry {
380 oldList := r.list
Paul Duffin255f18e2019-12-13 11:22:16 +0000381
382 // Copy the slice just in case this is being read while being modified, e.g. when testing.
383 list := make([]SdkMemberType, 0, len(oldList)+1)
384 list = append(list, oldList...)
385 list = append(list, memberType)
386
387 // Sort the member types by their property name to ensure that registry order has no effect
388 // on behavior.
389 sort.Slice(list, func(i1, i2 int) bool {
390 t1 := list[i1]
391 t2 := list[i2]
392
393 return t1.SdkPropertyName() < t2.SdkPropertyName()
394 })
395
396 // Generate a key that identifies the slice of SdkMemberTypes by joining the property names
397 // from all the SdkMemberType .
398 var properties []string
399 for _, t := range list {
400 properties = append(properties, t.SdkPropertyName())
401 }
402 key := NewOnceKey(strings.Join(properties, "|"))
403
404 // Create a new registry so the pointer uniquely identifies the set of registered types.
Paul Duffine6029182019-12-16 17:43:48 +0000405 return &SdkMemberTypesRegistry{
Paul Duffin255f18e2019-12-13 11:22:16 +0000406 list: list,
407 key: key,
408 }
409}
Paul Duffine6029182019-12-16 17:43:48 +0000410
411func (r *SdkMemberTypesRegistry) RegisteredTypes() []SdkMemberType {
412 return r.list
413}
414
415func (r *SdkMemberTypesRegistry) UniqueOnceKey() OnceKey {
416 // Use the pointer to the registry as the unique key.
417 return NewCustomOnceKey(r)
418}
419
420// The set of registered SdkMemberTypes, one for sdk module and one for module_exports.
421var ModuleExportsMemberTypes = &SdkMemberTypesRegistry{}
422var SdkMemberTypes = &SdkMemberTypesRegistry{}
423
424// Register an SdkMemberType object to allow them to be used in the sdk and sdk_snapshot module
425// types.
426func RegisterSdkMemberType(memberType SdkMemberType) {
427 // All member types are usable with module_exports.
428 ModuleExportsMemberTypes = ModuleExportsMemberTypes.copyAndAppend(memberType)
429
430 // Only those that explicitly indicate it are usable with sdk.
431 if memberType.UsableWithSdkAndSdkSnapshot() {
432 SdkMemberTypes = SdkMemberTypes.copyAndAppend(memberType)
433 }
434}
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000435
436// Base structure for all implementations of SdkMemberProperties.
437//
Paul Duffina04c1072020-03-02 10:16:35 +0000438// Contains common properties that apply across many different member types. These
439// are not affected by the optimization to extract common values.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000440type SdkMemberPropertiesBase struct {
Paul Duffina04c1072020-03-02 10:16:35 +0000441 // The number of unique os types supported by the member variants.
Paul Duffina551a1c2020-03-17 21:04:24 +0000442 //
443 // If a member has a variant with more than one os type then it will need to differentiate
444 // the locations of any of their prebuilt files in the snapshot by os type to prevent them
445 // from colliding. See OsPrefix().
446 //
447 // This property is the same for all variants of a member and so would be optimized away
448 // if it was not explicitly kept.
Paul Duffinb07fa512020-03-10 22:17:04 +0000449 Os_count int `sdk:"keep"`
Paul Duffina04c1072020-03-02 10:16:35 +0000450
451 // The os type for which these properties refer.
Paul Duffina551a1c2020-03-17 21:04:24 +0000452 //
453 // Provided to allow a member to differentiate between os types in the locations of their
454 // prebuilt files when it supports more than one os type.
455 //
456 // This property is the same for all os type specific variants of a member and so would be
457 // optimized away if it was not explicitly kept.
Paul Duffinb07fa512020-03-10 22:17:04 +0000458 Os OsType `sdk:"keep"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000459
460 // The setting to use for the compile_multilib property.
461 //
462 // This property is set after optimization so there is no point in trying to optimize it.
463 Compile_multilib string `sdk:"keep"`
Paul Duffina04c1072020-03-02 10:16:35 +0000464}
465
466// The os prefix to use for any file paths in the sdk.
467//
468// Is an empty string if the member only provides variants for a single os type, otherwise
469// is the OsType.Name.
470func (b *SdkMemberPropertiesBase) OsPrefix() string {
471 if b.Os_count == 1 {
472 return ""
473 } else {
474 return b.Os.Name
475 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000476}
477
478func (b *SdkMemberPropertiesBase) Base() *SdkMemberPropertiesBase {
479 return b
480}
481
482// Interface to be implemented on top of a structure that contains variant specific
483// information.
484//
485// Struct fields that are capitalized are examined for common values to extract. Fields
486// that are not capitalized are assumed to be arch specific.
487type SdkMemberProperties interface {
488 // Access the base structure.
489 Base() *SdkMemberPropertiesBase
490
Paul Duffin3a4eb502020-03-19 16:11:18 +0000491 // Populate this structure with information from the variant.
492 PopulateFromVariant(ctx SdkMemberContext, variant Module)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000493
Paul Duffin3a4eb502020-03-19 16:11:18 +0000494 // Add the information from this structure to the property set.
495 AddToPropertySet(ctx SdkMemberContext, propertySet BpPropertySet)
496}
497
498// Provides access to information common to a specific member.
499type SdkMemberContext interface {
500
501 // The module context of the sdk common os variant which is creating the snapshot.
502 SdkModuleContext() ModuleContext
503
504 // The builder of the snapshot.
505 SnapshotBuilder() SnapshotBuilder
Paul Duffina551a1c2020-03-17 21:04:24 +0000506
507 // The type of the member.
508 MemberType() SdkMemberType
509
510 // The name of the member.
511 //
512 // Provided for use by sdk members to create a member specific location within the snapshot
513 // into which to copy the prebuilt files.
514 Name() string
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000515}