blob: 2c84a2e8878a86754575a96c81c7d7e4ef8ff20e [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 sdk
16
17import (
Jiyong Park9b409bc2019-10-11 14:59:13 +090018 "fmt"
Paul Duffin504b4612019-11-22 14:52:29 +000019 "io"
Paul Duffin255f18e2019-12-13 11:22:16 +000020 "reflect"
Jiyong Park9b409bc2019-10-11 14:59:13 +090021 "strconv"
22
Jiyong Parkd1063c12019-07-17 20:08:41 +090023 "github.com/google/blueprint"
Jiyong Park100f3fd2019-11-06 16:03:32 +090024 "github.com/google/blueprint/proptools"
Jiyong Parkd1063c12019-07-17 20:08:41 +090025
26 "android/soong/android"
27 // This package doesn't depend on the apex package, but import it to make its mutators to be
28 // registered before mutators in this package. See RegisterPostDepsMutators for more details.
29 _ "android/soong/apex"
30)
31
32func init() {
Jiyong Park232e7852019-11-04 12:23:40 +090033 pctx.Import("android/soong/android")
Paul Duffin375058f2019-11-29 20:17:53 +000034 pctx.Import("android/soong/java/config")
35
Paul Duffin6d9108f02021-03-09 22:59:28 +000036 registerSdkBuildComponents(android.InitRegistrationContext)
37}
38
39func registerSdkBuildComponents(ctx android.RegistrationContext) {
40 ctx.RegisterModuleType("sdk", SdkModuleFactory)
41 ctx.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory)
42 ctx.PreDepsMutators(RegisterPreDepsMutators)
43 ctx.PostDepsMutators(RegisterPostDepsMutators)
Jiyong Parkd1063c12019-07-17 20:08:41 +090044}
45
46type sdk struct {
47 android.ModuleBase
48 android.DefaultableModuleBase
49
Paul Duffin255f18e2019-12-13 11:22:16 +000050 // The dynamically generated information about the registered SdkMemberType
51 dynamicSdkMemberTypes *dynamicSdkMemberTypes
52
53 // The dynamically created instance of the properties struct containing the sdk member
54 // list properties, e.g. java_libs.
55 dynamicMemberTypeListProperties interface{}
56
Paul Duffin1356d8c2020-02-25 19:26:33 +000057 // Information about the OsType specific member variants associated with this variant.
58 //
Paul Duffin6a7e9532020-03-20 17:50:07 +000059 // Set by OsType specific variants in the collectMembers() method and used by the
60 // CommonOS variant when building the snapshot. That work is all done on separate
61 // calls to the sdk.GenerateAndroidBuildActions method which is guaranteed to be
62 // called for the OsType specific variants before the CommonOS variant (because
63 // the latter depends on the former).
Paul Duffin1356d8c2020-02-25 19:26:33 +000064 memberRefs []sdkMemberRef
65
Paul Duffin6a7e9532020-03-20 17:50:07 +000066 // The multilib variants that are used by this sdk variant.
67 multilibUsages multilibUsage
68
Jiyong Parkd1063c12019-07-17 20:08:41 +090069 properties sdkProperties
Jiyong Park9b409bc2019-10-11 14:59:13 +090070
Jiyong Park232e7852019-11-04 12:23:40 +090071 snapshotFile android.OptionalPath
Paul Duffinac37c502019-11-26 18:02:20 +000072
73 // The builder, preserved for testing.
74 builderForTests *snapshotBuilder
Jiyong Parkd1063c12019-07-17 20:08:41 +090075}
76
77type sdkProperties struct {
Jiyong Park9b409bc2019-10-11 14:59:13 +090078 Snapshot bool `blueprint:"mutated"`
Paul Duffin8150da62019-12-16 17:21:27 +000079
80 // True if this is a module_exports (or module_exports_snapshot) module type.
81 Module_exports bool `blueprint:"mutated"`
Paul Duffin157f40f2020-09-29 16:01:08 +010082
83 // The additional visibility to add to the prebuilt modules to allow them to
84 // reference each other.
85 //
86 // This can only be used to widen the visibility of the members:
87 //
88 // * Specifying //visibility:public here will make all members visible and
89 // essentially ignore their own visibility.
90 // * Specifying //visibility:private here is an error.
91 // * Specifying any other rule here will add it to the members visibility and
92 // be output to the member prebuilt in the snapshot. Duplicates will be
93 // dropped. Adding a rule to members that have //visibility:private will
94 // cause the //visibility:private to be discarded.
95 Prebuilt_visibility []string
Jiyong Parkd1063c12019-07-17 20:08:41 +090096}
97
Paul Duffin13879572019-11-28 14:31:38 +000098// Contains information about the sdk properties that list sdk members, e.g.
Paul Duffina0dbf432019-12-05 11:25:53 +000099// Java_header_libs.
Paul Duffin13879572019-11-28 14:31:38 +0000100type sdkMemberListProperty struct {
Paul Duffin13879572019-11-28 14:31:38 +0000101 // getter for the list of member names
Paul Duffin255f18e2019-12-13 11:22:16 +0000102 getter func(properties interface{}) []string
Paul Duffin13879572019-11-28 14:31:38 +0000103
104 // the type of member referenced in the list
105 memberType android.SdkMemberType
106
Paul Duffin255f18e2019-12-13 11:22:16 +0000107 // the dependency tag used for items in this list that can be used to determine the memberType
108 // for a resolved dependency.
Paul Duffinf8539922019-11-19 19:44:10 +0000109 dependencyTag android.SdkMemberTypeDependencyTag
Paul Duffin13879572019-11-28 14:31:38 +0000110}
111
Paul Duffin255f18e2019-12-13 11:22:16 +0000112func (p *sdkMemberListProperty) propertyName() string {
113 return p.memberType.SdkPropertyName()
114}
115
116// Cache of dynamically generated dynamicSdkMemberTypes objects. The key is the pointer
117// to a slice of SdkMemberType instances held in android.SdkMemberTypes.
118var dynamicSdkMemberTypesMap android.OncePer
119
120// A dynamically generated set of member list properties and associated structure type.
121type dynamicSdkMemberTypes struct {
122 // The dynamically generated structure type.
123 //
124 // Contains one []string exported field for each android.SdkMemberTypes. The name of the field
125 // is the exported form of the value returned by SdkMemberType.SdkPropertyName().
126 propertiesStructType reflect.Type
127
128 // Information about each of the member type specific list properties.
129 memberListProperties []*sdkMemberListProperty
130}
131
132func (d *dynamicSdkMemberTypes) createMemberListProperties() interface{} {
133 return reflect.New(d.propertiesStructType).Interface()
134}
135
136func getDynamicSdkMemberTypes(registry *android.SdkMemberTypesRegistry) *dynamicSdkMemberTypes {
137
138 // Get a key that uniquely identifies the registry contents.
139 key := registry.UniqueOnceKey()
140
141 // Get the registered types.
142 registeredTypes := registry.RegisteredTypes()
143
144 // Get the cached value, creating new instance if necessary.
145 return dynamicSdkMemberTypesMap.Once(key, func() interface{} {
146 return createDynamicSdkMemberTypes(registeredTypes)
147 }).(*dynamicSdkMemberTypes)
148}
149
150// Create the dynamicSdkMemberTypes from the list of registered member types.
Paul Duffina6e737b2019-11-29 11:55:51 +0000151//
Paul Duffin255f18e2019-12-13 11:22:16 +0000152// A struct is created which contains one exported field per member type corresponding to
153// the SdkMemberType.SdkPropertyName() value.
154//
155// A list of sdkMemberListProperty instances is created, one per member type that provides:
156// * a reference to the member type.
157// * a getter for the corresponding field in the properties struct.
158// * a dependency tag that identifies the member type of a resolved dependency.
159//
160func createDynamicSdkMemberTypes(sdkMemberTypes []android.SdkMemberType) *dynamicSdkMemberTypes {
Paul Duffine6029182019-12-16 17:43:48 +0000161
Paul Duffin255f18e2019-12-13 11:22:16 +0000162 var listProperties []*sdkMemberListProperty
163 var fields []reflect.StructField
164
165 // Iterate over the member types creating StructField and sdkMemberListProperty objects.
166 for f, memberType := range sdkMemberTypes {
167 p := memberType.SdkPropertyName()
168
169 // Create a dynamic exported field for the member type's property.
170 fields = append(fields, reflect.StructField{
171 Name: proptools.FieldNameForProperty(p),
172 Type: reflect.TypeOf([]string{}),
Paul Duffin865171e2020-03-02 18:38:15 +0000173 Tag: `android:"arch_variant"`,
Paul Duffin255f18e2019-12-13 11:22:16 +0000174 })
175
176 // Copy the field index for use in the getter func as using the loop variable directly will
177 // cause all funcs to use the last value.
178 fieldIndex := f
179
180 // Create an sdkMemberListProperty for the member type.
181 memberListProperty := &sdkMemberListProperty{
182 getter: func(properties interface{}) []string {
183 // The properties is expected to be of the following form (where
184 // <Module_types> is the name of an SdkMemberType.SdkPropertyName().
185 // properties *struct {<Module_types> []string, ....}
186 //
187 // Although it accesses the field by index the following reflection code is equivalent to:
188 // *properties.<Module_types>
189 //
190 list := reflect.ValueOf(properties).Elem().Field(fieldIndex).Interface().([]string)
191 return list
192 },
193
194 memberType: memberType,
195
Paul Duffinf8539922019-11-19 19:44:10 +0000196 dependencyTag: android.DependencyTagForSdkMemberType(memberType),
Paul Duffin255f18e2019-12-13 11:22:16 +0000197 }
198
199 listProperties = append(listProperties, memberListProperty)
200 }
201
202 // Create a dynamic struct from the collated fields.
203 propertiesStructType := reflect.StructOf(fields)
204
205 return &dynamicSdkMemberTypes{
206 memberListProperties: listProperties,
207 propertiesStructType: propertiesStructType,
208 }
Paul Duffin13879572019-11-28 14:31:38 +0000209}
210
Jiyong Parkd1063c12019-07-17 20:08:41 +0900211// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
212// which Mainline modules like APEX can choose to build with.
Paul Duffin8150da62019-12-16 17:21:27 +0000213func SdkModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000214 return newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000215}
Paul Duffin255f18e2019-12-13 11:22:16 +0000216
Paul Duffine6029182019-12-16 17:43:48 +0000217func newSdkModule(moduleExports bool) *sdk {
Paul Duffin8150da62019-12-16 17:21:27 +0000218 s := &sdk{}
Paul Duffine6029182019-12-16 17:43:48 +0000219 s.properties.Module_exports = moduleExports
Paul Duffin255f18e2019-12-13 11:22:16 +0000220 // Get the dynamic sdk member type data for the currently registered sdk member types.
Paul Duffine6029182019-12-16 17:43:48 +0000221 var registry *android.SdkMemberTypesRegistry
222 if moduleExports {
223 registry = android.ModuleExportsMemberTypes
224 } else {
225 registry = android.SdkMemberTypes
226 }
227 s.dynamicSdkMemberTypes = getDynamicSdkMemberTypes(registry)
Paul Duffin255f18e2019-12-13 11:22:16 +0000228 // Create an instance of the dynamically created struct that contains all the
229 // properties for the member type specific list properties.
230 s.dynamicMemberTypeListProperties = s.dynamicSdkMemberTypes.createMemberListProperties()
Paul Duffin255f18e2019-12-13 11:22:16 +0000231 s.AddProperties(&s.properties, s.dynamicMemberTypeListProperties)
Paul Duffin157f40f2020-09-29 16:01:08 +0100232
233 // Make sure that the prebuilt visibility property is verified for errors.
234 android.AddVisibilityProperty(s, "prebuilt_visibility", &s.properties.Prebuilt_visibility)
Paul Duffin1356d8c2020-02-25 19:26:33 +0000235 android.InitCommonOSAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900236 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900237 android.AddLoadHook(s, func(ctx android.LoadHookContext) {
238 type props struct {
239 Compile_multilib *string
240 }
241 p := &props{Compile_multilib: proptools.StringPtr("both")}
Martin Stjernholm26ab8e82020-06-30 20:34:00 +0100242 ctx.PrependProperties(p)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900243 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900244 return s
245}
246
Jiyong Park9b409bc2019-10-11 14:59:13 +0900247// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
248func SnapshotModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000249 s := newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000250 s.properties.Snapshot = true
Jiyong Park9b409bc2019-10-11 14:59:13 +0900251 return s
252}
253
Paul Duffin72910952020-01-20 18:16:30 +0000254func (s *sdk) memberListProperties() []*sdkMemberListProperty {
255 return s.dynamicSdkMemberTypes.memberListProperties
256}
257
258func (s *sdk) getExportedMembers() map[string]struct{} {
Paul Duffin13f02712020-03-06 12:30:43 +0000259 // Collect all the exported members.
260 exportedMembers := make(map[string]struct{})
Paul Duffin72910952020-01-20 18:16:30 +0000261
Paul Duffin13f02712020-03-06 12:30:43 +0000262 for _, memberListProperty := range s.memberListProperties() {
263 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
Paul Duffin72910952020-01-20 18:16:30 +0000264
Paul Duffin13f02712020-03-06 12:30:43 +0000265 // Every member specified explicitly in the properties is exported by the sdk.
266 for _, name := range names {
267 exportedMembers[name] = struct{}{}
Paul Duffin72910952020-01-20 18:16:30 +0000268 }
269 }
270
Paul Duffin13f02712020-03-06 12:30:43 +0000271 return exportedMembers
Paul Duffin72910952020-01-20 18:16:30 +0000272}
273
Jiyong Park9b409bc2019-10-11 14:59:13 +0900274func (s *sdk) snapshot() bool {
275 return s.properties.Snapshot
276}
277
Jiyong Parkd1063c12019-07-17 20:08:41 +0900278func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000279 if s.snapshot() {
Jiyong Park232e7852019-11-04 12:23:40 +0900280 // We don't need to create a snapshot out of sdk_snapshot.
281 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000282 return
283 }
284
285 // This method is guaranteed to be called on OsType specific variants before it is called
286 // on their corresponding CommonOS variant.
287 if !s.IsCommonOSVariant() {
Paul Duffin6a7e9532020-03-20 17:50:07 +0000288 // Update the OsType specific sdk variant with information about its members.
289 s.collectMembers(ctx)
Paul Duffin1356d8c2020-02-25 19:26:33 +0000290 } else {
291 // Get the OsType specific variants on which the CommonOS depends.
292 osSpecificVariants := android.GetOsSpecificVariantsOfCommonOSVariant(ctx)
293 var sdkVariants []*sdk
294 for _, m := range osSpecificVariants {
295 if sdkVariant, ok := m.(*sdk); ok {
296 sdkVariants = append(sdkVariants, sdkVariant)
297 }
298 }
299
300 // Generate the snapshot from the member info.
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000301 p := s.buildSnapshot(ctx, sdkVariants)
302 s.snapshotFile = android.OptionalPathForPath(p)
303 ctx.InstallFile(android.PathForMainlineSdksInstall(ctx), s.Name()+"-current.zip", p)
Jiyong Park232e7852019-11-04 12:23:40 +0900304 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900305}
306
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900307func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries {
Jiyong Park232e7852019-11-04 12:23:40 +0900308 if !s.snapshotFile.Valid() {
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900309 return []android.AndroidMkEntries{}
Jiyong Park232e7852019-11-04 12:23:40 +0900310 }
311
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900312 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park232e7852019-11-04 12:23:40 +0900313 Class: "FAKE",
314 OutputFile: s.snapshotFile,
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000315 DistFiles: android.MakeDefaultDistFiles(s.snapshotFile.Path()),
Jiyong Park232e7852019-11-04 12:23:40 +0900316 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000317 ExtraFooters: []android.AndroidMkExtraFootersFunc{
Jaewoong Jung02b11a62020-12-07 10:23:54 -0800318 func(w io.Writer, name, prefix, moduleDir string) {
Paul Duffin504b4612019-11-22 14:52:29 +0000319 // Allow the sdk to be built by simply passing its name on the command line.
320 fmt.Fprintln(w, ".PHONY:", s.Name())
321 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
322 },
323 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900324 }}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900325}
326
327// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
328// interface and the sdk module type. This function has been made public to be called by tests
329// outside of the sdk package
330func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
331 ctx.BottomUp("SdkMember", memberMutator).Parallel()
332 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
333 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
334}
335
Paul Duffin65347702020-03-31 15:23:40 +0100336// RegisterPostDepsMutators registers post-deps mutators to support modules implementing SdkAware
Jiyong Parkd1063c12019-07-17 20:08:41 +0900337// interface and the sdk module type. This function has been made public to be called by tests
338// outside of the sdk package
339func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
340 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
341 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
342 // APEX to its dependents. Since different versions of the same SDK can be used by different
343 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
344 // should have been mutated for the apex before the SDK requirements are set.
345 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
346 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900347 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900348}
349
350type dependencyTag struct {
351 blueprint.BaseDependencyTag
352}
353
Martin Stjernholmcc776012020-07-07 03:22:21 +0100354// Mark this tag so dependencies that use it are excluded from APEX contents.
355func (t dependencyTag) ExcludeFromApexContents() {}
356
357var _ android.ExcludeFromApexContentsTag = dependencyTag{}
358
Jiyong Parkd1063c12019-07-17 20:08:41 +0900359// For dependencies from an in-development version of an SDK member to frozen versions of the same member
360// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
Paul Duffinfe149222020-01-14 14:06:09 +0000361type sdkMemberVersionedDepTag struct {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900362 dependencyTag
363 member string
364 version string
365}
366
Paul Duffinfe149222020-01-14 14:06:09 +0000367// Mark this tag so dependencies that use it are excluded from visibility enforcement.
368func (t sdkMemberVersionedDepTag) ExcludeFromVisibilityEnforcement() {}
369
Jiyong Parkd1063c12019-07-17 20:08:41 +0900370// Step 1: create dependencies from an SDK module to its members.
371func memberMutator(mctx android.BottomUpMutatorContext) {
Paul Duffin255f18e2019-12-13 11:22:16 +0000372 if s, ok := mctx.Module().(*sdk); ok {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000373 // Add dependencies from enabled and non CommonOS variants to the sdk member variants.
374 if s.Enabled() && !s.IsCommonOSVariant() {
Paul Duffin583bf7e2020-02-20 13:39:41 +0000375 for _, memberListProperty := range s.memberListProperties() {
376 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
Paul Duffincc1b3da2020-02-27 13:29:56 +0000377 if len(names) > 0 {
378 tag := memberListProperty.dependencyTag
379 memberListProperty.memberType.AddDependencies(mctx, tag, names)
380 }
Paul Duffin583bf7e2020-02-20 13:39:41 +0000381 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900382 }
383 }
384}
385
386// Step 2: record that dependencies of SDK modules are members of the SDK modules
387func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900388 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900389 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900390 if s.snapshot() && mySdkRef.Unversioned() {
391 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
392 "Did you manually modify Android.bp?")
393 }
394 if !s.snapshot() && !mySdkRef.Unversioned() {
395 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
396 }
397 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
398 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
399 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
400 }
401 }
402
Jiyong Parkd1063c12019-07-17 20:08:41 +0900403 mctx.VisitDirectDeps(func(child android.Module) {
404 if member, ok := child.(android.SdkAware); ok {
405 member.MakeMemberOf(mySdkRef)
406 }
407 })
408 }
409}
410
Jiyong Park9b409bc2019-10-11 14:59:13 +0900411// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900412// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
413// (apex and apk), each of which might want different sdks to be built with. For example, if both
414// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
415// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
416// using.
417func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
418 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900419 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900420 memberName := m.MemberName()
Paul Duffinfe149222020-01-14 14:06:09 +0000421 tag := sdkMemberVersionedDepTag{member: memberName, version: m.ContainingSdk().Version}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900422 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
423 }
424 }
425}
426
427// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
428// descendants
429func sdkDepsMutator(mctx android.TopDownMutatorContext) {
Paul Duffina37eca22020-07-22 13:00:54 +0100430 if parent, ok := mctx.Module().(interface {
431 android.DepIsInSameApex
432 android.RequiredSdks
433 }); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900434 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
435 // by reading its own properties like `uses_sdks`.
Paul Duffina37eca22020-07-22 13:00:54 +0100436 requiredSdks := parent.RequiredSdks()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900437 if len(requiredSdks) > 0 {
438 mctx.VisitDirectDeps(func(m android.Module) {
Paul Duffina37eca22020-07-22 13:00:54 +0100439 // Only propagate required sdks from the apex onto its contents.
440 if dep, ok := m.(android.SdkAware); ok && parent.DepIsInSameApex(mctx, dep) {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900441 dep.BuildWithSdks(requiredSdks)
442 }
443 })
444 }
445 }
446}
447
448// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
449// versioned module is used instead of the un-versioned (in-development) module libfoo
450func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
Paul Duffina37eca22020-07-22 13:00:54 +0100451 if versionedSdkMember, ok := mctx.Module().(android.SdkAware); ok && versionedSdkMember.IsInAnySdk() {
452 if sdk := versionedSdkMember.ContainingSdk(); !sdk.Unversioned() {
453 // Only replace dependencies to <sdkmember> with <sdkmember@required-version>
454 // if the depending module requires it. e.g.
455 // foo -> sdkmember
456 // will be transformed to:
457 // foo -> sdkmember@1
458 // if and only if foo is a member of an APEX that requires version 1 of the
459 // sdk containing sdkmember.
460 memberName := versionedSdkMember.MemberName()
461
462 // Replace dependencies on sdkmember with a dependency on the current module which
463 // is a versioned prebuilt of the sdkmember if required.
464 mctx.ReplaceDependenciesIf(memberName, func(from blueprint.Module, tag blueprint.DependencyTag, to blueprint.Module) bool {
465 // from - foo
466 // to - sdkmember
467 replace := false
468 if parent, ok := from.(android.RequiredSdks); ok {
469 replace = parent.RequiredSdks().Contains(sdk)
470 }
471 return replace
472 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900473 }
474 }
475}
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900476
Paul Duffin65347702020-03-31 15:23:40 +0100477// Step 6: ensure that the dependencies outside of the APEX are all from the required SDKs
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900478func sdkRequirementsMutator(mctx android.TopDownMutatorContext) {
479 if m, ok := mctx.Module().(interface {
Paul Duffin923e8a52020-03-30 15:33:32 +0100480 android.DepIsInSameApex
481 android.RequiredSdks
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900482 }); ok {
483 requiredSdks := m.RequiredSdks()
484 if len(requiredSdks) == 0 {
485 return
486 }
487 mctx.VisitDirectDeps(func(dep android.Module) {
Paul Duffin65347702020-03-31 15:23:40 +0100488 tag := mctx.OtherModuleDependencyTag(dep)
489 if tag == android.DefaultsDepTag {
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900490 // dependency to defaults is always okay
491 return
492 }
493
Paul Duffin65347702020-03-31 15:23:40 +0100494 // Ignore the dependency from the unversioned member to any versioned members as an
495 // apex that depends on the unversioned member will not also be depending on a versioned
496 // member.
497 if _, ok := tag.(sdkMemberVersionedDepTag); ok {
498 return
499 }
500
501 // If the dep is outside of the APEX, but is not in any of the
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900502 // required SDKs, we know that the dep is a violation.
503 if sa, ok := dep.(android.SdkAware); ok {
504 if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) {
505 mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v",
506 sa.Name(), sa.ContainingSdk(), requiredSdks)
507 }
508 }
509 })
510 }
511}