blob: f28c39215703484f04a64e03b8edb6aa6dbc39ea [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
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000148func (s *SdkBase) BuildSnapshot(sdkModuleContext ModuleContext, builder SnapshotBuilder) {
149 sdkModuleContext.ModuleErrorf("module type " + sdkModuleContext.OtherModuleType(s.module) + " cannot be used in an sdk")
150}
151
Jiyong Parkd1063c12019-07-17 20:08:41 +0900152// InitSdkAwareModule initializes the SdkBase struct. This must be called by all modules including
153// SdkBase.
154func InitSdkAwareModule(m SdkAware) {
155 base := m.sdkBase()
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000156 base.module = m
Jiyong Parkd1063c12019-07-17 20:08:41 +0900157 m.AddProperties(&base.properties)
158}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000159
160// Provide support for generating the build rules which will build the snapshot.
161type SnapshotBuilder interface {
162 // Copy src to the dest (which is a snapshot relative path) and add the dest
163 // to the zip
164 CopyToSnapshot(src Path, dest string)
165
Paul Duffin91547182019-11-12 19:39:36 +0000166 // Unzip the supplied zip into the snapshot relative directory destDir.
167 UnzipToSnapshot(zipPath Path, destDir string)
168
Paul Duffinb645ec82019-11-27 17:43:54 +0000169 // Add a new prebuilt module to the snapshot. The returned module
170 // must be populated with the module type specific properties. The following
171 // properties will be automatically populated.
172 //
173 // * name
174 // * sdk_member_name
175 // * prefer
176 //
177 // This will result in two Soong modules being generated in the Android. One
178 // that is versioned, coupled to the snapshot version and marked as
179 // prefer=true. And one that is not versioned, not marked as prefer=true and
180 // will only be used if the equivalently named non-prebuilt module is not
181 // present.
Paul Duffin9d8d6092019-12-05 18:19:29 +0000182 AddPrebuiltModule(member SdkMember, moduleType string) BpModule
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000183
184 // The property tag to use when adding a property to a BpModule that contains
185 // references to other sdk members. Using this will ensure that the reference
186 // is correctly output for both versioned and unversioned prebuilts in the
187 // snapshot.
188 //
189 // e.g.
190 // bpPropertySet.AddPropertyWithTag("libs", []string{"member1", "member2"}, builder.SdkMemberReferencePropertyTag())
191 SdkMemberReferencePropertyTag() BpPropertyTag
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000192}
193
Paul Duffin5b511a22020-01-15 14:23:52 +0000194type BpPropertyTag interface{}
195
Paul Duffinb645ec82019-11-27 17:43:54 +0000196// A set of properties for use in a .bp file.
197type BpPropertySet interface {
198 // Add a property, the value can be one of the following types:
199 // * string
200 // * array of the above
201 // * bool
202 // * BpPropertySet
203 //
Paul Duffin5b511a22020-01-15 14:23:52 +0000204 // It is an error if multiple properties with the same name are added.
Paul Duffinb645ec82019-11-27 17:43:54 +0000205 AddProperty(name string, value interface{})
206
Paul Duffin5b511a22020-01-15 14:23:52 +0000207 // Add a property with an associated tag
208 AddPropertyWithTag(name string, value interface{}, tag BpPropertyTag)
209
Paul Duffinb645ec82019-11-27 17:43:54 +0000210 // Add a property set with the specified name and return so that additional
211 // properties can be added.
212 AddPropertySet(name string) BpPropertySet
213}
214
215// A .bp module definition.
216type BpModule interface {
217 BpPropertySet
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000218}
Paul Duffin13879572019-11-28 14:31:38 +0000219
220// An individual member of the SDK, includes all of the variants that the SDK
221// requires.
222type SdkMember interface {
223 // The name of the member.
224 Name() string
225
226 // All the variants required by the SDK.
227 Variants() []SdkAware
228}
229
Paul Duffinf8539922019-11-19 19:44:10 +0000230type SdkMemberTypeDependencyTag interface {
231 blueprint.DependencyTag
232
233 SdkMemberType() SdkMemberType
234}
235
236type sdkMemberDependencyTag struct {
237 blueprint.BaseDependencyTag
238 memberType SdkMemberType
239}
240
241func (t *sdkMemberDependencyTag) SdkMemberType() SdkMemberType {
242 return t.memberType
243}
244
245func DependencyTagForSdkMemberType(memberType SdkMemberType) SdkMemberTypeDependencyTag {
246 return &sdkMemberDependencyTag{memberType: memberType}
247}
248
Paul Duffin13879572019-11-28 14:31:38 +0000249// Interface that must be implemented for every type that can be a member of an
250// sdk.
251//
252// The basic implementation should look something like this, where ModuleType is
253// the name of the module type being supported.
254//
Paul Duffin255f18e2019-12-13 11:22:16 +0000255// type moduleTypeSdkMemberType struct {
256// android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +0000257// }
258//
Paul Duffin255f18e2019-12-13 11:22:16 +0000259// func init() {
260// android.RegisterSdkMemberType(&moduleTypeSdkMemberType{
261// SdkMemberTypeBase: android.SdkMemberTypeBase{
262// PropertyName: "module_types",
263// },
264// }
Paul Duffin13879572019-11-28 14:31:38 +0000265// }
266//
267// ...methods...
268//
269type SdkMemberType interface {
Paul Duffin255f18e2019-12-13 11:22:16 +0000270 // The name of the member type property on an sdk module.
271 SdkPropertyName() string
272
Paul Duffine6029182019-12-16 17:43:48 +0000273 // True if the member type supports the sdk/sdk_snapshot, false otherwise.
274 UsableWithSdkAndSdkSnapshot() bool
275
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000276 // Return true if modules of this type can have dependencies which should be
277 // treated as if they are sdk members.
278 //
279 // Any dependency that is to be treated as a member of the sdk needs to implement
280 // SdkAware and be added with an SdkMemberTypeDependencyTag tag.
281 HasTransitiveSdkMembers() bool
282
Paul Duffin13879572019-11-28 14:31:38 +0000283 // Add dependencies from the SDK module to all the variants the member
284 // contributes to the SDK. The exact set of variants required is determined
285 // by the SDK and its properties. The dependencies must be added with the
286 // supplied tag.
287 //
288 // The BottomUpMutatorContext provided is for the SDK module.
289 AddDependencies(mctx BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string)
290
291 // Return true if the supplied module is an instance of this member type.
292 //
293 // This is used to check the type of each variant before added to the
294 // SdkMember. Returning false will cause an error to be logged expaining that
295 // the module is not allowed in whichever sdk property it was added.
296 IsInstance(module Module) bool
297
298 // Build the snapshot for the SDK member
299 //
300 // The ModuleContext provided is for the SDK module, so information for
301 // variants in the supplied member can be accessed using the Other... methods.
302 //
303 // The SdkMember is guaranteed to contain variants for which the
304 // IsInstance(Module) method returned true.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000305 //
306 // deprecated Use AddPrebuiltModule() instead.
Paul Duffin13879572019-11-28 14:31:38 +0000307 BuildSnapshot(sdkModuleContext ModuleContext, builder SnapshotBuilder, member SdkMember)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000308
309 // Add a prebuilt module that the sdk will populate.
310 //
311 // Returning nil from this will cause the sdk module type to use the deprecated BuildSnapshot
312 // method to build the snapshot. That method is deprecated because it requires the SdkMemberType
313 // implementation to do all the word.
314 //
315 // Otherwise, returning a non-nil value from this will cause the sdk module type to do the
316 // majority of the work to generate the snapshot. The sdk module code generates the snapshot
317 // as follows:
318 //
319 // * A properties struct of type SdkMemberProperties is created for each variant and
320 // populated with information from the variant by calling PopulateFromVariant(SdkAware)
321 // on the struct.
322 //
323 // * An additional properties struct is created into which the common properties will be
324 // added.
325 //
326 // * The variant property structs are analysed to find exported (capitalized) fields which
327 // have common values. Those fields are cleared and the common value added to the common
328 // properties.
329 //
330 // * The sdk module type populates the BpModule structure, creating the arch specific
331 // structure and calls AddToPropertySet(...) on the properties struct to add the member
332 // specific properties in the correct place in the structure.
333 //
334 // * Finally, the FinalizeModule(...) method is called to add any additional properties.
335 // This was created to allow the property ordering in existing tests to be maintained so
336 // as to avoid having to change tests while refactoring.
337 //
338 AddPrebuiltModule(sdkModuleContext ModuleContext, builder SnapshotBuilder, member SdkMember) BpModule
339
340 // Add any additional properties to the end of the module.
341 FinalizeModule(sdkModuleContext ModuleContext, builder SnapshotBuilder, member SdkMember, bpModule BpModule)
342
343 // Create a structure into which variant specific properties can be added.
344 CreateVariantPropertiesStruct() SdkMemberProperties
Paul Duffin13879572019-11-28 14:31:38 +0000345}
Paul Duffin255f18e2019-12-13 11:22:16 +0000346
Paul Duffine6029182019-12-16 17:43:48 +0000347// Base type for SdkMemberType implementations.
Paul Duffin255f18e2019-12-13 11:22:16 +0000348type SdkMemberTypeBase struct {
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000349 PropertyName string
350 SupportsSdk bool
351 TransitiveSdkMembers bool
Paul Duffin255f18e2019-12-13 11:22:16 +0000352}
353
354func (b *SdkMemberTypeBase) SdkPropertyName() string {
355 return b.PropertyName
356}
357
Paul Duffine6029182019-12-16 17:43:48 +0000358func (b *SdkMemberTypeBase) UsableWithSdkAndSdkSnapshot() bool {
359 return b.SupportsSdk
360}
361
Paul Duffinf4ae4f12020-01-13 20:58:25 +0000362func (b *SdkMemberTypeBase) HasTransitiveSdkMembers() bool {
363 return b.TransitiveSdkMembers
364}
365
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000366func (b *SdkMemberTypeBase) BuildSnapshot(sdkModuleContext ModuleContext, builder SnapshotBuilder, member SdkMember) {
367 panic("override AddPrebuiltModule")
368}
369
370func (b *SdkMemberTypeBase) AddPrebuiltModule(sdkModuleContext ModuleContext, builder SnapshotBuilder, member SdkMember) BpModule {
371 // Returning nil causes the legacy BuildSnapshot method to be used.
372 return nil
373}
374
375func (b *SdkMemberTypeBase) FinalizeModule(sdkModuleContext ModuleContext, builder SnapshotBuilder, member SdkMember, module BpModule) {
376 // Do nothing by default
377}
378
379func (b *SdkMemberTypeBase) CreateVariantPropertiesStruct() SdkMemberProperties {
380 panic("override me")
381}
382
Paul Duffin255f18e2019-12-13 11:22:16 +0000383// Encapsulates the information about registered SdkMemberTypes.
384type SdkMemberTypesRegistry struct {
385 // The list of types sorted by property name.
386 list []SdkMemberType
387
388 // The key that uniquely identifies this registry instance.
389 key OnceKey
390}
391
Paul Duffine6029182019-12-16 17:43:48 +0000392func (r *SdkMemberTypesRegistry) copyAndAppend(memberType SdkMemberType) *SdkMemberTypesRegistry {
393 oldList := r.list
Paul Duffin255f18e2019-12-13 11:22:16 +0000394
395 // Copy the slice just in case this is being read while being modified, e.g. when testing.
396 list := make([]SdkMemberType, 0, len(oldList)+1)
397 list = append(list, oldList...)
398 list = append(list, memberType)
399
400 // Sort the member types by their property name to ensure that registry order has no effect
401 // on behavior.
402 sort.Slice(list, func(i1, i2 int) bool {
403 t1 := list[i1]
404 t2 := list[i2]
405
406 return t1.SdkPropertyName() < t2.SdkPropertyName()
407 })
408
409 // Generate a key that identifies the slice of SdkMemberTypes by joining the property names
410 // from all the SdkMemberType .
411 var properties []string
412 for _, t := range list {
413 properties = append(properties, t.SdkPropertyName())
414 }
415 key := NewOnceKey(strings.Join(properties, "|"))
416
417 // Create a new registry so the pointer uniquely identifies the set of registered types.
Paul Duffine6029182019-12-16 17:43:48 +0000418 return &SdkMemberTypesRegistry{
Paul Duffin255f18e2019-12-13 11:22:16 +0000419 list: list,
420 key: key,
421 }
422}
Paul Duffine6029182019-12-16 17:43:48 +0000423
424func (r *SdkMemberTypesRegistry) RegisteredTypes() []SdkMemberType {
425 return r.list
426}
427
428func (r *SdkMemberTypesRegistry) UniqueOnceKey() OnceKey {
429 // Use the pointer to the registry as the unique key.
430 return NewCustomOnceKey(r)
431}
432
433// The set of registered SdkMemberTypes, one for sdk module and one for module_exports.
434var ModuleExportsMemberTypes = &SdkMemberTypesRegistry{}
435var SdkMemberTypes = &SdkMemberTypesRegistry{}
436
437// Register an SdkMemberType object to allow them to be used in the sdk and sdk_snapshot module
438// types.
439func RegisterSdkMemberType(memberType SdkMemberType) {
440 // All member types are usable with module_exports.
441 ModuleExportsMemberTypes = ModuleExportsMemberTypes.copyAndAppend(memberType)
442
443 // Only those that explicitly indicate it are usable with sdk.
444 if memberType.UsableWithSdkAndSdkSnapshot() {
445 SdkMemberTypes = SdkMemberTypes.copyAndAppend(memberType)
446 }
447}
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000448
449// Base structure for all implementations of SdkMemberProperties.
450//
451// Contains common properties that apply across many different member types.
452type SdkMemberPropertiesBase struct {
453 // The setting to use for the compile_multilib property.
454 Compile_multilib string
455}
456
457func (b *SdkMemberPropertiesBase) Base() *SdkMemberPropertiesBase {
458 return b
459}
460
461// Interface to be implemented on top of a structure that contains variant specific
462// information.
463//
464// Struct fields that are capitalized are examined for common values to extract. Fields
465// that are not capitalized are assumed to be arch specific.
466type SdkMemberProperties interface {
467 // Access the base structure.
468 Base() *SdkMemberPropertiesBase
469
470 // Populate the structure with information from the variant.
471 PopulateFromVariant(variant SdkAware)
472
473 // Add the information from the structure to the property set.
474 AddToPropertySet(sdkModuleContext ModuleContext, builder SnapshotBuilder, propertySet BpPropertySet)
475}