blob: 50b0886d4c99fbbdc55f539473989170ee985ba3 [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 Duffin8150da62019-12-16 17:21:27 +000036 android.RegisterModuleType("sdk", SdkModuleFactory)
Jiyong Park9b409bc2019-10-11 14:59:13 +090037 android.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory)
Jiyong Parkd1063c12019-07-17 20:08:41 +090038 android.PreDepsMutators(RegisterPreDepsMutators)
39 android.PostDepsMutators(RegisterPostDepsMutators)
40}
41
42type sdk struct {
43 android.ModuleBase
44 android.DefaultableModuleBase
45
Paul Duffin255f18e2019-12-13 11:22:16 +000046 // The dynamically generated information about the registered SdkMemberType
47 dynamicSdkMemberTypes *dynamicSdkMemberTypes
48
49 // The dynamically created instance of the properties struct containing the sdk member
50 // list properties, e.g. java_libs.
51 dynamicMemberTypeListProperties interface{}
52
Paul Duffin1356d8c2020-02-25 19:26:33 +000053 // Information about the OsType specific member variants associated with this variant.
54 //
Paul Duffin6a7e9532020-03-20 17:50:07 +000055 // Set by OsType specific variants in the collectMembers() method and used by the
56 // CommonOS variant when building the snapshot. That work is all done on separate
57 // calls to the sdk.GenerateAndroidBuildActions method which is guaranteed to be
58 // called for the OsType specific variants before the CommonOS variant (because
59 // the latter depends on the former).
Paul Duffin1356d8c2020-02-25 19:26:33 +000060 memberRefs []sdkMemberRef
61
Paul Duffin6a7e9532020-03-20 17:50:07 +000062 // The multilib variants that are used by this sdk variant.
63 multilibUsages multilibUsage
64
Jiyong Parkd1063c12019-07-17 20:08:41 +090065 properties sdkProperties
Jiyong Park9b409bc2019-10-11 14:59:13 +090066
Jiyong Park232e7852019-11-04 12:23:40 +090067 snapshotFile android.OptionalPath
Paul Duffinac37c502019-11-26 18:02:20 +000068
69 // The builder, preserved for testing.
70 builderForTests *snapshotBuilder
Jiyong Parkd1063c12019-07-17 20:08:41 +090071}
72
73type sdkProperties struct {
Jiyong Park9b409bc2019-10-11 14:59:13 +090074 Snapshot bool `blueprint:"mutated"`
Paul Duffin8150da62019-12-16 17:21:27 +000075
76 // True if this is a module_exports (or module_exports_snapshot) module type.
77 Module_exports bool `blueprint:"mutated"`
Paul Duffin157f40f2020-09-29 16:01:08 +010078
79 // The additional visibility to add to the prebuilt modules to allow them to
80 // reference each other.
81 //
82 // This can only be used to widen the visibility of the members:
83 //
84 // * Specifying //visibility:public here will make all members visible and
85 // essentially ignore their own visibility.
86 // * Specifying //visibility:private here is an error.
87 // * Specifying any other rule here will add it to the members visibility and
88 // be output to the member prebuilt in the snapshot. Duplicates will be
89 // dropped. Adding a rule to members that have //visibility:private will
90 // cause the //visibility:private to be discarded.
91 Prebuilt_visibility []string
Jiyong Parkd1063c12019-07-17 20:08:41 +090092}
93
Paul Duffin13879572019-11-28 14:31:38 +000094// Contains information about the sdk properties that list sdk members, e.g.
Paul Duffina0dbf432019-12-05 11:25:53 +000095// Java_header_libs.
Paul Duffin13879572019-11-28 14:31:38 +000096type sdkMemberListProperty struct {
Paul Duffin13879572019-11-28 14:31:38 +000097 // getter for the list of member names
Paul Duffin255f18e2019-12-13 11:22:16 +000098 getter func(properties interface{}) []string
Paul Duffin13879572019-11-28 14:31:38 +000099
100 // the type of member referenced in the list
101 memberType android.SdkMemberType
102
Paul Duffin255f18e2019-12-13 11:22:16 +0000103 // the dependency tag used for items in this list that can be used to determine the memberType
104 // for a resolved dependency.
Paul Duffinf8539922019-11-19 19:44:10 +0000105 dependencyTag android.SdkMemberTypeDependencyTag
Paul Duffin13879572019-11-28 14:31:38 +0000106}
107
Paul Duffin255f18e2019-12-13 11:22:16 +0000108func (p *sdkMemberListProperty) propertyName() string {
109 return p.memberType.SdkPropertyName()
110}
111
112// Cache of dynamically generated dynamicSdkMemberTypes objects. The key is the pointer
113// to a slice of SdkMemberType instances held in android.SdkMemberTypes.
114var dynamicSdkMemberTypesMap android.OncePer
115
116// A dynamically generated set of member list properties and associated structure type.
117type dynamicSdkMemberTypes struct {
118 // The dynamically generated structure type.
119 //
120 // Contains one []string exported field for each android.SdkMemberTypes. The name of the field
121 // is the exported form of the value returned by SdkMemberType.SdkPropertyName().
122 propertiesStructType reflect.Type
123
124 // Information about each of the member type specific list properties.
125 memberListProperties []*sdkMemberListProperty
126}
127
128func (d *dynamicSdkMemberTypes) createMemberListProperties() interface{} {
129 return reflect.New(d.propertiesStructType).Interface()
130}
131
132func getDynamicSdkMemberTypes(registry *android.SdkMemberTypesRegistry) *dynamicSdkMemberTypes {
133
134 // Get a key that uniquely identifies the registry contents.
135 key := registry.UniqueOnceKey()
136
137 // Get the registered types.
138 registeredTypes := registry.RegisteredTypes()
139
140 // Get the cached value, creating new instance if necessary.
141 return dynamicSdkMemberTypesMap.Once(key, func() interface{} {
142 return createDynamicSdkMemberTypes(registeredTypes)
143 }).(*dynamicSdkMemberTypes)
144}
145
146// Create the dynamicSdkMemberTypes from the list of registered member types.
Paul Duffina6e737b2019-11-29 11:55:51 +0000147//
Paul Duffin255f18e2019-12-13 11:22:16 +0000148// A struct is created which contains one exported field per member type corresponding to
149// the SdkMemberType.SdkPropertyName() value.
150//
151// A list of sdkMemberListProperty instances is created, one per member type that provides:
152// * a reference to the member type.
153// * a getter for the corresponding field in the properties struct.
154// * a dependency tag that identifies the member type of a resolved dependency.
155//
156func createDynamicSdkMemberTypes(sdkMemberTypes []android.SdkMemberType) *dynamicSdkMemberTypes {
Paul Duffine6029182019-12-16 17:43:48 +0000157
Paul Duffin255f18e2019-12-13 11:22:16 +0000158 var listProperties []*sdkMemberListProperty
159 var fields []reflect.StructField
160
161 // Iterate over the member types creating StructField and sdkMemberListProperty objects.
162 for f, memberType := range sdkMemberTypes {
163 p := memberType.SdkPropertyName()
164
165 // Create a dynamic exported field for the member type's property.
166 fields = append(fields, reflect.StructField{
167 Name: proptools.FieldNameForProperty(p),
168 Type: reflect.TypeOf([]string{}),
Paul Duffin865171e2020-03-02 18:38:15 +0000169 Tag: `android:"arch_variant"`,
Paul Duffin255f18e2019-12-13 11:22:16 +0000170 })
171
172 // Copy the field index for use in the getter func as using the loop variable directly will
173 // cause all funcs to use the last value.
174 fieldIndex := f
175
176 // Create an sdkMemberListProperty for the member type.
177 memberListProperty := &sdkMemberListProperty{
178 getter: func(properties interface{}) []string {
179 // The properties is expected to be of the following form (where
180 // <Module_types> is the name of an SdkMemberType.SdkPropertyName().
181 // properties *struct {<Module_types> []string, ....}
182 //
183 // Although it accesses the field by index the following reflection code is equivalent to:
184 // *properties.<Module_types>
185 //
186 list := reflect.ValueOf(properties).Elem().Field(fieldIndex).Interface().([]string)
187 return list
188 },
189
190 memberType: memberType,
191
Paul Duffinf8539922019-11-19 19:44:10 +0000192 dependencyTag: android.DependencyTagForSdkMemberType(memberType),
Paul Duffin255f18e2019-12-13 11:22:16 +0000193 }
194
195 listProperties = append(listProperties, memberListProperty)
196 }
197
198 // Create a dynamic struct from the collated fields.
199 propertiesStructType := reflect.StructOf(fields)
200
201 return &dynamicSdkMemberTypes{
202 memberListProperties: listProperties,
203 propertiesStructType: propertiesStructType,
204 }
Paul Duffin13879572019-11-28 14:31:38 +0000205}
206
Jiyong Parkd1063c12019-07-17 20:08:41 +0900207// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
208// which Mainline modules like APEX can choose to build with.
Paul Duffin8150da62019-12-16 17:21:27 +0000209func SdkModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000210 return newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000211}
Paul Duffin255f18e2019-12-13 11:22:16 +0000212
Paul Duffine6029182019-12-16 17:43:48 +0000213func newSdkModule(moduleExports bool) *sdk {
Paul Duffin8150da62019-12-16 17:21:27 +0000214 s := &sdk{}
Paul Duffine6029182019-12-16 17:43:48 +0000215 s.properties.Module_exports = moduleExports
Paul Duffin255f18e2019-12-13 11:22:16 +0000216 // Get the dynamic sdk member type data for the currently registered sdk member types.
Paul Duffine6029182019-12-16 17:43:48 +0000217 var registry *android.SdkMemberTypesRegistry
218 if moduleExports {
219 registry = android.ModuleExportsMemberTypes
220 } else {
221 registry = android.SdkMemberTypes
222 }
223 s.dynamicSdkMemberTypes = getDynamicSdkMemberTypes(registry)
Paul Duffin255f18e2019-12-13 11:22:16 +0000224 // Create an instance of the dynamically created struct that contains all the
225 // properties for the member type specific list properties.
226 s.dynamicMemberTypeListProperties = s.dynamicSdkMemberTypes.createMemberListProperties()
Paul Duffin255f18e2019-12-13 11:22:16 +0000227 s.AddProperties(&s.properties, s.dynamicMemberTypeListProperties)
Paul Duffin157f40f2020-09-29 16:01:08 +0100228
229 // Make sure that the prebuilt visibility property is verified for errors.
230 android.AddVisibilityProperty(s, "prebuilt_visibility", &s.properties.Prebuilt_visibility)
Paul Duffin1356d8c2020-02-25 19:26:33 +0000231 android.InitCommonOSAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900232 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900233 android.AddLoadHook(s, func(ctx android.LoadHookContext) {
234 type props struct {
235 Compile_multilib *string
236 }
237 p := &props{Compile_multilib: proptools.StringPtr("both")}
Martin Stjernholm26ab8e82020-06-30 20:34:00 +0100238 ctx.PrependProperties(p)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900239 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900240 return s
241}
242
Jiyong Park9b409bc2019-10-11 14:59:13 +0900243// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
244func SnapshotModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000245 s := newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000246 s.properties.Snapshot = true
Jiyong Park9b409bc2019-10-11 14:59:13 +0900247 return s
248}
249
Paul Duffin72910952020-01-20 18:16:30 +0000250func (s *sdk) memberListProperties() []*sdkMemberListProperty {
251 return s.dynamicSdkMemberTypes.memberListProperties
252}
253
254func (s *sdk) getExportedMembers() map[string]struct{} {
Paul Duffin13f02712020-03-06 12:30:43 +0000255 // Collect all the exported members.
256 exportedMembers := make(map[string]struct{})
Paul Duffin72910952020-01-20 18:16:30 +0000257
Paul Duffin13f02712020-03-06 12:30:43 +0000258 for _, memberListProperty := range s.memberListProperties() {
259 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
Paul Duffin72910952020-01-20 18:16:30 +0000260
Paul Duffin13f02712020-03-06 12:30:43 +0000261 // Every member specified explicitly in the properties is exported by the sdk.
262 for _, name := range names {
263 exportedMembers[name] = struct{}{}
Paul Duffin72910952020-01-20 18:16:30 +0000264 }
265 }
266
Paul Duffin13f02712020-03-06 12:30:43 +0000267 return exportedMembers
Paul Duffin72910952020-01-20 18:16:30 +0000268}
269
Jiyong Park9b409bc2019-10-11 14:59:13 +0900270func (s *sdk) snapshot() bool {
271 return s.properties.Snapshot
272}
273
Jiyong Parkd1063c12019-07-17 20:08:41 +0900274func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000275 if s.snapshot() {
Jiyong Park232e7852019-11-04 12:23:40 +0900276 // We don't need to create a snapshot out of sdk_snapshot.
277 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000278 return
279 }
280
281 // This method is guaranteed to be called on OsType specific variants before it is called
282 // on their corresponding CommonOS variant.
283 if !s.IsCommonOSVariant() {
Paul Duffin6a7e9532020-03-20 17:50:07 +0000284 // Update the OsType specific sdk variant with information about its members.
285 s.collectMembers(ctx)
Paul Duffin1356d8c2020-02-25 19:26:33 +0000286 } else {
287 // Get the OsType specific variants on which the CommonOS depends.
288 osSpecificVariants := android.GetOsSpecificVariantsOfCommonOSVariant(ctx)
289 var sdkVariants []*sdk
290 for _, m := range osSpecificVariants {
291 if sdkVariant, ok := m.(*sdk); ok {
292 sdkVariants = append(sdkVariants, sdkVariant)
293 }
294 }
295
296 // Generate the snapshot from the member info.
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000297 p := s.buildSnapshot(ctx, sdkVariants)
298 s.snapshotFile = android.OptionalPathForPath(p)
299 ctx.InstallFile(android.PathForMainlineSdksInstall(ctx), s.Name()+"-current.zip", p)
Jiyong Park232e7852019-11-04 12:23:40 +0900300 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900301}
302
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900303func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries {
Jiyong Park232e7852019-11-04 12:23:40 +0900304 if !s.snapshotFile.Valid() {
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900305 return []android.AndroidMkEntries{}
Jiyong Park232e7852019-11-04 12:23:40 +0900306 }
307
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900308 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park232e7852019-11-04 12:23:40 +0900309 Class: "FAKE",
310 OutputFile: s.snapshotFile,
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000311 DistFiles: android.MakeDefaultDistFiles(s.snapshotFile.Path()),
Jiyong Park232e7852019-11-04 12:23:40 +0900312 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000313 ExtraFooters: []android.AndroidMkExtraFootersFunc{
314 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
315 // Allow the sdk to be built by simply passing its name on the command line.
316 fmt.Fprintln(w, ".PHONY:", s.Name())
317 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
318 },
319 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900320 }}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900321}
322
323// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
324// interface and the sdk module type. This function has been made public to be called by tests
325// outside of the sdk package
326func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
327 ctx.BottomUp("SdkMember", memberMutator).Parallel()
328 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
329 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
330}
331
Paul Duffin65347702020-03-31 15:23:40 +0100332// RegisterPostDepsMutators registers post-deps mutators to support modules implementing SdkAware
Jiyong Parkd1063c12019-07-17 20:08:41 +0900333// interface and the sdk module type. This function has been made public to be called by tests
334// outside of the sdk package
335func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
336 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
337 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
338 // APEX to its dependents. Since different versions of the same SDK can be used by different
339 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
340 // should have been mutated for the apex before the SDK requirements are set.
341 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
342 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900343 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900344}
345
346type dependencyTag struct {
347 blueprint.BaseDependencyTag
348}
349
Martin Stjernholmcc776012020-07-07 03:22:21 +0100350// Mark this tag so dependencies that use it are excluded from APEX contents.
351func (t dependencyTag) ExcludeFromApexContents() {}
352
353var _ android.ExcludeFromApexContentsTag = dependencyTag{}
354
Jiyong Parkd1063c12019-07-17 20:08:41 +0900355// For dependencies from an in-development version of an SDK member to frozen versions of the same member
356// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
Paul Duffinfe149222020-01-14 14:06:09 +0000357type sdkMemberVersionedDepTag struct {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900358 dependencyTag
359 member string
360 version string
361}
362
Paul Duffinfe149222020-01-14 14:06:09 +0000363// Mark this tag so dependencies that use it are excluded from visibility enforcement.
364func (t sdkMemberVersionedDepTag) ExcludeFromVisibilityEnforcement() {}
365
Jiyong Parkd1063c12019-07-17 20:08:41 +0900366// Step 1: create dependencies from an SDK module to its members.
367func memberMutator(mctx android.BottomUpMutatorContext) {
Paul Duffin255f18e2019-12-13 11:22:16 +0000368 if s, ok := mctx.Module().(*sdk); ok {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000369 // Add dependencies from enabled and non CommonOS variants to the sdk member variants.
370 if s.Enabled() && !s.IsCommonOSVariant() {
Paul Duffin583bf7e2020-02-20 13:39:41 +0000371 for _, memberListProperty := range s.memberListProperties() {
372 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
Paul Duffincc1b3da2020-02-27 13:29:56 +0000373 if len(names) > 0 {
374 tag := memberListProperty.dependencyTag
375 memberListProperty.memberType.AddDependencies(mctx, tag, names)
376 }
Paul Duffin583bf7e2020-02-20 13:39:41 +0000377 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900378 }
379 }
380}
381
382// Step 2: record that dependencies of SDK modules are members of the SDK modules
383func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900384 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900385 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900386 if s.snapshot() && mySdkRef.Unversioned() {
387 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
388 "Did you manually modify Android.bp?")
389 }
390 if !s.snapshot() && !mySdkRef.Unversioned() {
391 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
392 }
393 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
394 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
395 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
396 }
397 }
398
Jiyong Parkd1063c12019-07-17 20:08:41 +0900399 mctx.VisitDirectDeps(func(child android.Module) {
400 if member, ok := child.(android.SdkAware); ok {
401 member.MakeMemberOf(mySdkRef)
402 }
403 })
404 }
405}
406
Jiyong Park9b409bc2019-10-11 14:59:13 +0900407// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900408// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
409// (apex and apk), each of which might want different sdks to be built with. For example, if both
410// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
411// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
412// using.
413func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
414 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900415 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900416 memberName := m.MemberName()
Paul Duffinfe149222020-01-14 14:06:09 +0000417 tag := sdkMemberVersionedDepTag{member: memberName, version: m.ContainingSdk().Version}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900418 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
419 }
420 }
421}
422
423// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
424// descendants
425func sdkDepsMutator(mctx android.TopDownMutatorContext) {
Paul Duffina37eca22020-07-22 13:00:54 +0100426 if parent, ok := mctx.Module().(interface {
427 android.DepIsInSameApex
428 android.RequiredSdks
429 }); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900430 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
431 // by reading its own properties like `uses_sdks`.
Paul Duffina37eca22020-07-22 13:00:54 +0100432 requiredSdks := parent.RequiredSdks()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900433 if len(requiredSdks) > 0 {
434 mctx.VisitDirectDeps(func(m android.Module) {
Paul Duffina37eca22020-07-22 13:00:54 +0100435 // Only propagate required sdks from the apex onto its contents.
436 if dep, ok := m.(android.SdkAware); ok && parent.DepIsInSameApex(mctx, dep) {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900437 dep.BuildWithSdks(requiredSdks)
438 }
439 })
440 }
441 }
442}
443
444// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
445// versioned module is used instead of the un-versioned (in-development) module libfoo
446func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
Paul Duffina37eca22020-07-22 13:00:54 +0100447 if versionedSdkMember, ok := mctx.Module().(android.SdkAware); ok && versionedSdkMember.IsInAnySdk() {
448 if sdk := versionedSdkMember.ContainingSdk(); !sdk.Unversioned() {
449 // Only replace dependencies to <sdkmember> with <sdkmember@required-version>
450 // if the depending module requires it. e.g.
451 // foo -> sdkmember
452 // will be transformed to:
453 // foo -> sdkmember@1
454 // if and only if foo is a member of an APEX that requires version 1 of the
455 // sdk containing sdkmember.
456 memberName := versionedSdkMember.MemberName()
457
458 // Replace dependencies on sdkmember with a dependency on the current module which
459 // is a versioned prebuilt of the sdkmember if required.
460 mctx.ReplaceDependenciesIf(memberName, func(from blueprint.Module, tag blueprint.DependencyTag, to blueprint.Module) bool {
461 // from - foo
462 // to - sdkmember
463 replace := false
464 if parent, ok := from.(android.RequiredSdks); ok {
465 replace = parent.RequiredSdks().Contains(sdk)
466 }
467 return replace
468 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900469 }
470 }
471}
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900472
Paul Duffin65347702020-03-31 15:23:40 +0100473// Step 6: ensure that the dependencies outside of the APEX are all from the required SDKs
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900474func sdkRequirementsMutator(mctx android.TopDownMutatorContext) {
475 if m, ok := mctx.Module().(interface {
Paul Duffin923e8a52020-03-30 15:33:32 +0100476 android.DepIsInSameApex
477 android.RequiredSdks
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900478 }); ok {
479 requiredSdks := m.RequiredSdks()
480 if len(requiredSdks) == 0 {
481 return
482 }
483 mctx.VisitDirectDeps(func(dep android.Module) {
Paul Duffin65347702020-03-31 15:23:40 +0100484 tag := mctx.OtherModuleDependencyTag(dep)
485 if tag == android.DefaultsDepTag {
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900486 // dependency to defaults is always okay
487 return
488 }
489
Paul Duffin65347702020-03-31 15:23:40 +0100490 // Ignore the dependency from the unversioned member to any versioned members as an
491 // apex that depends on the unversioned member will not also be depending on a versioned
492 // member.
493 if _, ok := tag.(sdkMemberVersionedDepTag); ok {
494 return
495 }
496
497 // If the dep is outside of the APEX, but is not in any of the
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900498 // required SDKs, we know that the dep is a violation.
499 if sa, ok := dep.(android.SdkAware); ok {
500 if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) {
501 mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v",
502 sa.Name(), sa.ContainingSdk(), requiredSdks)
503 }
504 }
505 })
506 }
507}