blob: 8b9d5bc8055bc9af487cf36e79836c61d9985e82 [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 Duffin72910952020-01-20 18:16:30 +000053 // The set of exported members.
54 exportedMembers map[string]struct{}
55
Paul Duffin1356d8c2020-02-25 19:26:33 +000056 // Information about the OsType specific member variants associated with this variant.
57 //
58 // Set by OsType specific variants when their GenerateAndroidBuildActions is invoked
59 // and used by the CommonOS variant when its GenerateAndroidBuildActions is invoked, which
60 // is guaranteed to occur afterwards.
61 memberRefs []sdkMemberRef
62
Jiyong Parkd1063c12019-07-17 20:08:41 +090063 properties sdkProperties
Jiyong Park9b409bc2019-10-11 14:59:13 +090064
Jiyong Park232e7852019-11-04 12:23:40 +090065 snapshotFile android.OptionalPath
Paul Duffinac37c502019-11-26 18:02:20 +000066
67 // The builder, preserved for testing.
68 builderForTests *snapshotBuilder
Jiyong Parkd1063c12019-07-17 20:08:41 +090069}
70
71type sdkProperties struct {
Jiyong Park9b409bc2019-10-11 14:59:13 +090072 Snapshot bool `blueprint:"mutated"`
Paul Duffin8150da62019-12-16 17:21:27 +000073
74 // True if this is a module_exports (or module_exports_snapshot) module type.
75 Module_exports bool `blueprint:"mutated"`
Jiyong Parkd1063c12019-07-17 20:08:41 +090076}
77
Paul Duffin13879572019-11-28 14:31:38 +000078// Contains information about the sdk properties that list sdk members, e.g.
Paul Duffina0dbf432019-12-05 11:25:53 +000079// Java_header_libs.
Paul Duffin13879572019-11-28 14:31:38 +000080type sdkMemberListProperty struct {
Paul Duffin13879572019-11-28 14:31:38 +000081 // getter for the list of member names
Paul Duffin255f18e2019-12-13 11:22:16 +000082 getter func(properties interface{}) []string
Paul Duffin13879572019-11-28 14:31:38 +000083
84 // the type of member referenced in the list
85 memberType android.SdkMemberType
86
Paul Duffin255f18e2019-12-13 11:22:16 +000087 // the dependency tag used for items in this list that can be used to determine the memberType
88 // for a resolved dependency.
Paul Duffinf8539922019-11-19 19:44:10 +000089 dependencyTag android.SdkMemberTypeDependencyTag
Paul Duffin13879572019-11-28 14:31:38 +000090}
91
Paul Duffin255f18e2019-12-13 11:22:16 +000092func (p *sdkMemberListProperty) propertyName() string {
93 return p.memberType.SdkPropertyName()
94}
95
96// Cache of dynamically generated dynamicSdkMemberTypes objects. The key is the pointer
97// to a slice of SdkMemberType instances held in android.SdkMemberTypes.
98var dynamicSdkMemberTypesMap android.OncePer
99
100// A dynamically generated set of member list properties and associated structure type.
101type dynamicSdkMemberTypes struct {
102 // The dynamically generated structure type.
103 //
104 // Contains one []string exported field for each android.SdkMemberTypes. The name of the field
105 // is the exported form of the value returned by SdkMemberType.SdkPropertyName().
106 propertiesStructType reflect.Type
107
108 // Information about each of the member type specific list properties.
109 memberListProperties []*sdkMemberListProperty
110}
111
112func (d *dynamicSdkMemberTypes) createMemberListProperties() interface{} {
113 return reflect.New(d.propertiesStructType).Interface()
114}
115
116func getDynamicSdkMemberTypes(registry *android.SdkMemberTypesRegistry) *dynamicSdkMemberTypes {
117
118 // Get a key that uniquely identifies the registry contents.
119 key := registry.UniqueOnceKey()
120
121 // Get the registered types.
122 registeredTypes := registry.RegisteredTypes()
123
124 // Get the cached value, creating new instance if necessary.
125 return dynamicSdkMemberTypesMap.Once(key, func() interface{} {
126 return createDynamicSdkMemberTypes(registeredTypes)
127 }).(*dynamicSdkMemberTypes)
128}
129
130// Create the dynamicSdkMemberTypes from the list of registered member types.
Paul Duffina6e737b2019-11-29 11:55:51 +0000131//
Paul Duffin255f18e2019-12-13 11:22:16 +0000132// A struct is created which contains one exported field per member type corresponding to
133// the SdkMemberType.SdkPropertyName() value.
134//
135// A list of sdkMemberListProperty instances is created, one per member type that provides:
136// * a reference to the member type.
137// * a getter for the corresponding field in the properties struct.
138// * a dependency tag that identifies the member type of a resolved dependency.
139//
140func createDynamicSdkMemberTypes(sdkMemberTypes []android.SdkMemberType) *dynamicSdkMemberTypes {
Paul Duffine6029182019-12-16 17:43:48 +0000141
Paul Duffin255f18e2019-12-13 11:22:16 +0000142 var listProperties []*sdkMemberListProperty
143 var fields []reflect.StructField
144
145 // Iterate over the member types creating StructField and sdkMemberListProperty objects.
146 for f, memberType := range sdkMemberTypes {
147 p := memberType.SdkPropertyName()
148
149 // Create a dynamic exported field for the member type's property.
150 fields = append(fields, reflect.StructField{
151 Name: proptools.FieldNameForProperty(p),
152 Type: reflect.TypeOf([]string{}),
Paul Duffin865171e2020-03-02 18:38:15 +0000153 Tag: `android:"arch_variant"`,
Paul Duffin255f18e2019-12-13 11:22:16 +0000154 })
155
156 // Copy the field index for use in the getter func as using the loop variable directly will
157 // cause all funcs to use the last value.
158 fieldIndex := f
159
160 // Create an sdkMemberListProperty for the member type.
161 memberListProperty := &sdkMemberListProperty{
162 getter: func(properties interface{}) []string {
163 // The properties is expected to be of the following form (where
164 // <Module_types> is the name of an SdkMemberType.SdkPropertyName().
165 // properties *struct {<Module_types> []string, ....}
166 //
167 // Although it accesses the field by index the following reflection code is equivalent to:
168 // *properties.<Module_types>
169 //
170 list := reflect.ValueOf(properties).Elem().Field(fieldIndex).Interface().([]string)
171 return list
172 },
173
174 memberType: memberType,
175
Paul Duffinf8539922019-11-19 19:44:10 +0000176 dependencyTag: android.DependencyTagForSdkMemberType(memberType),
Paul Duffin255f18e2019-12-13 11:22:16 +0000177 }
178
179 listProperties = append(listProperties, memberListProperty)
180 }
181
182 // Create a dynamic struct from the collated fields.
183 propertiesStructType := reflect.StructOf(fields)
184
185 return &dynamicSdkMemberTypes{
186 memberListProperties: listProperties,
187 propertiesStructType: propertiesStructType,
188 }
Paul Duffin13879572019-11-28 14:31:38 +0000189}
190
Jiyong Parkd1063c12019-07-17 20:08:41 +0900191// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
192// which Mainline modules like APEX can choose to build with.
Paul Duffin8150da62019-12-16 17:21:27 +0000193func SdkModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000194 return newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000195}
Paul Duffin255f18e2019-12-13 11:22:16 +0000196
Paul Duffine6029182019-12-16 17:43:48 +0000197func newSdkModule(moduleExports bool) *sdk {
Paul Duffin8150da62019-12-16 17:21:27 +0000198 s := &sdk{}
Paul Duffine6029182019-12-16 17:43:48 +0000199 s.properties.Module_exports = moduleExports
Paul Duffin255f18e2019-12-13 11:22:16 +0000200 // Get the dynamic sdk member type data for the currently registered sdk member types.
Paul Duffine6029182019-12-16 17:43:48 +0000201 var registry *android.SdkMemberTypesRegistry
202 if moduleExports {
203 registry = android.ModuleExportsMemberTypes
204 } else {
205 registry = android.SdkMemberTypes
206 }
207 s.dynamicSdkMemberTypes = getDynamicSdkMemberTypes(registry)
Paul Duffin255f18e2019-12-13 11:22:16 +0000208 // Create an instance of the dynamically created struct that contains all the
209 // properties for the member type specific list properties.
210 s.dynamicMemberTypeListProperties = s.dynamicSdkMemberTypes.createMemberListProperties()
Paul Duffin255f18e2019-12-13 11:22:16 +0000211 s.AddProperties(&s.properties, s.dynamicMemberTypeListProperties)
Paul Duffin1356d8c2020-02-25 19:26:33 +0000212 android.InitCommonOSAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900213 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900214 android.AddLoadHook(s, func(ctx android.LoadHookContext) {
215 type props struct {
216 Compile_multilib *string
217 }
218 p := &props{Compile_multilib: proptools.StringPtr("both")}
219 ctx.AppendProperties(p)
220 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900221 return s
222}
223
Jiyong Park9b409bc2019-10-11 14:59:13 +0900224// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
225func SnapshotModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000226 s := newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000227 s.properties.Snapshot = true
Jiyong Park9b409bc2019-10-11 14:59:13 +0900228 return s
229}
230
Paul Duffin72910952020-01-20 18:16:30 +0000231func (s *sdk) memberListProperties() []*sdkMemberListProperty {
232 return s.dynamicSdkMemberTypes.memberListProperties
233}
234
235func (s *sdk) getExportedMembers() map[string]struct{} {
236 if s.exportedMembers == nil {
237 // Collect all the exported members.
238 s.exportedMembers = make(map[string]struct{})
239
240 for _, memberListProperty := range s.memberListProperties() {
241 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
242
243 // Every member specified explicitly in the properties is exported by the sdk.
244 for _, name := range names {
245 s.exportedMembers[name] = struct{}{}
246 }
247 }
248 }
249
250 return s.exportedMembers
251}
252
253func (s *sdk) isInternalMember(memberName string) bool {
254 _, ok := s.getExportedMembers()[memberName]
255 return !ok
256}
257
Jiyong Park9b409bc2019-10-11 14:59:13 +0900258func (s *sdk) snapshot() bool {
259 return s.properties.Snapshot
260}
261
Jiyong Parkd1063c12019-07-17 20:08:41 +0900262func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000263 if s.snapshot() {
Jiyong Park232e7852019-11-04 12:23:40 +0900264 // We don't need to create a snapshot out of sdk_snapshot.
265 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000266 return
267 }
268
269 // This method is guaranteed to be called on OsType specific variants before it is called
270 // on their corresponding CommonOS variant.
271 if !s.IsCommonOSVariant() {
272 // Collect the OsType specific members are add them to the OsType specific variant.
273 s.memberRefs = s.collectMembers(ctx)
274 } else {
275 // Get the OsType specific variants on which the CommonOS depends.
276 osSpecificVariants := android.GetOsSpecificVariantsOfCommonOSVariant(ctx)
277 var sdkVariants []*sdk
278 for _, m := range osSpecificVariants {
279 if sdkVariant, ok := m.(*sdk); ok {
280 sdkVariants = append(sdkVariants, sdkVariant)
281 }
282 }
283
284 // Generate the snapshot from the member info.
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000285 p := s.buildSnapshot(ctx, sdkVariants)
286 s.snapshotFile = android.OptionalPathForPath(p)
287 ctx.InstallFile(android.PathForMainlineSdksInstall(ctx), s.Name()+"-current.zip", p)
Jiyong Park232e7852019-11-04 12:23:40 +0900288 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900289}
290
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900291func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries {
Jiyong Park232e7852019-11-04 12:23:40 +0900292 if !s.snapshotFile.Valid() {
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900293 return []android.AndroidMkEntries{}
Jiyong Park232e7852019-11-04 12:23:40 +0900294 }
295
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900296 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park232e7852019-11-04 12:23:40 +0900297 Class: "FAKE",
298 OutputFile: s.snapshotFile,
299 DistFile: s.snapshotFile,
300 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000301 ExtraFooters: []android.AndroidMkExtraFootersFunc{
302 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
303 // Allow the sdk to be built by simply passing its name on the command line.
304 fmt.Fprintln(w, ".PHONY:", s.Name())
305 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
306 },
307 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900308 }}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900309}
310
311// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
312// interface and the sdk module type. This function has been made public to be called by tests
313// outside of the sdk package
314func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
315 ctx.BottomUp("SdkMember", memberMutator).Parallel()
316 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
317 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
318}
319
320// RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware
321// interface and the sdk module type. This function has been made public to be called by tests
322// outside of the sdk package
323func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
324 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
325 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
326 // APEX to its dependents. Since different versions of the same SDK can be used by different
327 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
328 // should have been mutated for the apex before the SDK requirements are set.
329 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
330 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900331 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900332}
333
334type dependencyTag struct {
335 blueprint.BaseDependencyTag
336}
337
Jiyong Parkd1063c12019-07-17 20:08:41 +0900338// For dependencies from an in-development version of an SDK member to frozen versions of the same member
339// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
Paul Duffinfe149222020-01-14 14:06:09 +0000340type sdkMemberVersionedDepTag struct {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900341 dependencyTag
342 member string
343 version string
344}
345
Paul Duffinfe149222020-01-14 14:06:09 +0000346// Mark this tag so dependencies that use it are excluded from visibility enforcement.
347func (t sdkMemberVersionedDepTag) ExcludeFromVisibilityEnforcement() {}
348
Jiyong Parkd1063c12019-07-17 20:08:41 +0900349// Step 1: create dependencies from an SDK module to its members.
350func memberMutator(mctx android.BottomUpMutatorContext) {
Paul Duffin255f18e2019-12-13 11:22:16 +0000351 if s, ok := mctx.Module().(*sdk); ok {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000352 // Add dependencies from enabled and non CommonOS variants to the sdk member variants.
353 if s.Enabled() && !s.IsCommonOSVariant() {
Paul Duffin583bf7e2020-02-20 13:39:41 +0000354 for _, memberListProperty := range s.memberListProperties() {
355 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
Paul Duffincc1b3da2020-02-27 13:29:56 +0000356 if len(names) > 0 {
357 tag := memberListProperty.dependencyTag
358 memberListProperty.memberType.AddDependencies(mctx, tag, names)
359 }
Paul Duffin583bf7e2020-02-20 13:39:41 +0000360 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900361 }
362 }
363}
364
365// Step 2: record that dependencies of SDK modules are members of the SDK modules
366func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900367 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900368 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900369 if s.snapshot() && mySdkRef.Unversioned() {
370 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
371 "Did you manually modify Android.bp?")
372 }
373 if !s.snapshot() && !mySdkRef.Unversioned() {
374 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
375 }
376 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
377 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
378 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
379 }
380 }
381
Jiyong Parkd1063c12019-07-17 20:08:41 +0900382 mctx.VisitDirectDeps(func(child android.Module) {
383 if member, ok := child.(android.SdkAware); ok {
384 member.MakeMemberOf(mySdkRef)
385 }
386 })
387 }
388}
389
Jiyong Park9b409bc2019-10-11 14:59:13 +0900390// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900391// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
392// (apex and apk), each of which might want different sdks to be built with. For example, if both
393// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
394// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
395// using.
396func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
397 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900398 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900399 memberName := m.MemberName()
Paul Duffinfe149222020-01-14 14:06:09 +0000400 tag := sdkMemberVersionedDepTag{member: memberName, version: m.ContainingSdk().Version}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900401 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
402 }
403 }
404}
405
406// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
407// descendants
408func sdkDepsMutator(mctx android.TopDownMutatorContext) {
409 if m, ok := mctx.Module().(android.SdkAware); ok {
410 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
411 // by reading its own properties like `uses_sdks`.
412 requiredSdks := m.RequiredSdks()
413 if len(requiredSdks) > 0 {
414 mctx.VisitDirectDeps(func(m android.Module) {
415 if dep, ok := m.(android.SdkAware); ok {
416 dep.BuildWithSdks(requiredSdks)
417 }
418 })
419 }
420 }
421}
422
423// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
424// versioned module is used instead of the un-versioned (in-development) module libfoo
425func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
426 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900427 if sdk := m.ContainingSdk(); !sdk.Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900428 if m.RequiredSdks().Contains(sdk) {
429 // Note that this replacement is done only for the modules that have the same
430 // variations as the current module. Since current module is already mutated for
431 // apex references in other APEXes are not affected by this replacement.
432 memberName := m.MemberName()
433 mctx.ReplaceDependencies(memberName)
434 }
435 }
436 }
437}
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900438
439// Step 6: ensure that the dependencies from outside of the APEX are all from the required SDKs
440func sdkRequirementsMutator(mctx android.TopDownMutatorContext) {
441 if m, ok := mctx.Module().(interface {
442 DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool
443 RequiredSdks() android.SdkRefs
444 }); ok {
445 requiredSdks := m.RequiredSdks()
446 if len(requiredSdks) == 0 {
447 return
448 }
449 mctx.VisitDirectDeps(func(dep android.Module) {
450 if mctx.OtherModuleDependencyTag(dep) == android.DefaultsDepTag {
451 // dependency to defaults is always okay
452 return
453 }
454
455 // If the dep is from outside of the APEX, but is not in any of the
456 // required SDKs, we know that the dep is a violation.
457 if sa, ok := dep.(android.SdkAware); ok {
458 if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) {
459 mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v",
460 sa.Name(), sa.ContainingSdk(), requiredSdks)
461 }
462 }
463 })
464 }
465}