blob: 984ed7a1dae2ac70a5b10617e9581e03a2c1a7e8 [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 //
55 // Set by OsType specific variants when their GenerateAndroidBuildActions is invoked
56 // and used by the CommonOS variant when its GenerateAndroidBuildActions is invoked, which
57 // is guaranteed to occur afterwards.
58 memberRefs []sdkMemberRef
59
Jiyong Parkd1063c12019-07-17 20:08:41 +090060 properties sdkProperties
Jiyong Park9b409bc2019-10-11 14:59:13 +090061
Jiyong Park232e7852019-11-04 12:23:40 +090062 snapshotFile android.OptionalPath
Paul Duffinac37c502019-11-26 18:02:20 +000063
64 // The builder, preserved for testing.
65 builderForTests *snapshotBuilder
Jiyong Parkd1063c12019-07-17 20:08:41 +090066}
67
68type sdkProperties struct {
Jiyong Park9b409bc2019-10-11 14:59:13 +090069 Snapshot bool `blueprint:"mutated"`
Paul Duffin8150da62019-12-16 17:21:27 +000070
71 // True if this is a module_exports (or module_exports_snapshot) module type.
72 Module_exports bool `blueprint:"mutated"`
Jiyong Parkd1063c12019-07-17 20:08:41 +090073}
74
Paul Duffin13879572019-11-28 14:31:38 +000075// Contains information about the sdk properties that list sdk members, e.g.
Paul Duffina0dbf432019-12-05 11:25:53 +000076// Java_header_libs.
Paul Duffin13879572019-11-28 14:31:38 +000077type sdkMemberListProperty struct {
Paul Duffin13879572019-11-28 14:31:38 +000078 // getter for the list of member names
Paul Duffin255f18e2019-12-13 11:22:16 +000079 getter func(properties interface{}) []string
Paul Duffin13879572019-11-28 14:31:38 +000080
81 // the type of member referenced in the list
82 memberType android.SdkMemberType
83
Paul Duffin255f18e2019-12-13 11:22:16 +000084 // the dependency tag used for items in this list that can be used to determine the memberType
85 // for a resolved dependency.
Paul Duffinf8539922019-11-19 19:44:10 +000086 dependencyTag android.SdkMemberTypeDependencyTag
Paul Duffin13879572019-11-28 14:31:38 +000087}
88
Paul Duffin255f18e2019-12-13 11:22:16 +000089func (p *sdkMemberListProperty) propertyName() string {
90 return p.memberType.SdkPropertyName()
91}
92
93// Cache of dynamically generated dynamicSdkMemberTypes objects. The key is the pointer
94// to a slice of SdkMemberType instances held in android.SdkMemberTypes.
95var dynamicSdkMemberTypesMap android.OncePer
96
97// A dynamically generated set of member list properties and associated structure type.
98type dynamicSdkMemberTypes struct {
99 // The dynamically generated structure type.
100 //
101 // Contains one []string exported field for each android.SdkMemberTypes. The name of the field
102 // is the exported form of the value returned by SdkMemberType.SdkPropertyName().
103 propertiesStructType reflect.Type
104
105 // Information about each of the member type specific list properties.
106 memberListProperties []*sdkMemberListProperty
107}
108
109func (d *dynamicSdkMemberTypes) createMemberListProperties() interface{} {
110 return reflect.New(d.propertiesStructType).Interface()
111}
112
113func getDynamicSdkMemberTypes(registry *android.SdkMemberTypesRegistry) *dynamicSdkMemberTypes {
114
115 // Get a key that uniquely identifies the registry contents.
116 key := registry.UniqueOnceKey()
117
118 // Get the registered types.
119 registeredTypes := registry.RegisteredTypes()
120
121 // Get the cached value, creating new instance if necessary.
122 return dynamicSdkMemberTypesMap.Once(key, func() interface{} {
123 return createDynamicSdkMemberTypes(registeredTypes)
124 }).(*dynamicSdkMemberTypes)
125}
126
127// Create the dynamicSdkMemberTypes from the list of registered member types.
Paul Duffina6e737b2019-11-29 11:55:51 +0000128//
Paul Duffin255f18e2019-12-13 11:22:16 +0000129// A struct is created which contains one exported field per member type corresponding to
130// the SdkMemberType.SdkPropertyName() value.
131//
132// A list of sdkMemberListProperty instances is created, one per member type that provides:
133// * a reference to the member type.
134// * a getter for the corresponding field in the properties struct.
135// * a dependency tag that identifies the member type of a resolved dependency.
136//
137func createDynamicSdkMemberTypes(sdkMemberTypes []android.SdkMemberType) *dynamicSdkMemberTypes {
Paul Duffine6029182019-12-16 17:43:48 +0000138
Paul Duffin255f18e2019-12-13 11:22:16 +0000139 var listProperties []*sdkMemberListProperty
140 var fields []reflect.StructField
141
142 // Iterate over the member types creating StructField and sdkMemberListProperty objects.
143 for f, memberType := range sdkMemberTypes {
144 p := memberType.SdkPropertyName()
145
146 // Create a dynamic exported field for the member type's property.
147 fields = append(fields, reflect.StructField{
148 Name: proptools.FieldNameForProperty(p),
149 Type: reflect.TypeOf([]string{}),
Paul Duffin865171e2020-03-02 18:38:15 +0000150 Tag: `android:"arch_variant"`,
Paul Duffin255f18e2019-12-13 11:22:16 +0000151 })
152
153 // Copy the field index for use in the getter func as using the loop variable directly will
154 // cause all funcs to use the last value.
155 fieldIndex := f
156
157 // Create an sdkMemberListProperty for the member type.
158 memberListProperty := &sdkMemberListProperty{
159 getter: func(properties interface{}) []string {
160 // The properties is expected to be of the following form (where
161 // <Module_types> is the name of an SdkMemberType.SdkPropertyName().
162 // properties *struct {<Module_types> []string, ....}
163 //
164 // Although it accesses the field by index the following reflection code is equivalent to:
165 // *properties.<Module_types>
166 //
167 list := reflect.ValueOf(properties).Elem().Field(fieldIndex).Interface().([]string)
168 return list
169 },
170
171 memberType: memberType,
172
Paul Duffinf8539922019-11-19 19:44:10 +0000173 dependencyTag: android.DependencyTagForSdkMemberType(memberType),
Paul Duffin255f18e2019-12-13 11:22:16 +0000174 }
175
176 listProperties = append(listProperties, memberListProperty)
177 }
178
179 // Create a dynamic struct from the collated fields.
180 propertiesStructType := reflect.StructOf(fields)
181
182 return &dynamicSdkMemberTypes{
183 memberListProperties: listProperties,
184 propertiesStructType: propertiesStructType,
185 }
Paul Duffin13879572019-11-28 14:31:38 +0000186}
187
Jiyong Parkd1063c12019-07-17 20:08:41 +0900188// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
189// which Mainline modules like APEX can choose to build with.
Paul Duffin8150da62019-12-16 17:21:27 +0000190func SdkModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000191 return newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000192}
Paul Duffin255f18e2019-12-13 11:22:16 +0000193
Paul Duffine6029182019-12-16 17:43:48 +0000194func newSdkModule(moduleExports bool) *sdk {
Paul Duffin8150da62019-12-16 17:21:27 +0000195 s := &sdk{}
Paul Duffine6029182019-12-16 17:43:48 +0000196 s.properties.Module_exports = moduleExports
Paul Duffin255f18e2019-12-13 11:22:16 +0000197 // Get the dynamic sdk member type data for the currently registered sdk member types.
Paul Duffine6029182019-12-16 17:43:48 +0000198 var registry *android.SdkMemberTypesRegistry
199 if moduleExports {
200 registry = android.ModuleExportsMemberTypes
201 } else {
202 registry = android.SdkMemberTypes
203 }
204 s.dynamicSdkMemberTypes = getDynamicSdkMemberTypes(registry)
Paul Duffin255f18e2019-12-13 11:22:16 +0000205 // Create an instance of the dynamically created struct that contains all the
206 // properties for the member type specific list properties.
207 s.dynamicMemberTypeListProperties = s.dynamicSdkMemberTypes.createMemberListProperties()
Paul Duffin255f18e2019-12-13 11:22:16 +0000208 s.AddProperties(&s.properties, s.dynamicMemberTypeListProperties)
Paul Duffin1356d8c2020-02-25 19:26:33 +0000209 android.InitCommonOSAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900210 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900211 android.AddLoadHook(s, func(ctx android.LoadHookContext) {
212 type props struct {
213 Compile_multilib *string
214 }
215 p := &props{Compile_multilib: proptools.StringPtr("both")}
216 ctx.AppendProperties(p)
217 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900218 return s
219}
220
Jiyong Park9b409bc2019-10-11 14:59:13 +0900221// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
222func SnapshotModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000223 s := newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000224 s.properties.Snapshot = true
Jiyong Park9b409bc2019-10-11 14:59:13 +0900225 return s
226}
227
Paul Duffin72910952020-01-20 18:16:30 +0000228func (s *sdk) memberListProperties() []*sdkMemberListProperty {
229 return s.dynamicSdkMemberTypes.memberListProperties
230}
231
232func (s *sdk) getExportedMembers() map[string]struct{} {
Paul Duffin13f02712020-03-06 12:30:43 +0000233 // Collect all the exported members.
234 exportedMembers := make(map[string]struct{})
Paul Duffin72910952020-01-20 18:16:30 +0000235
Paul Duffin13f02712020-03-06 12:30:43 +0000236 for _, memberListProperty := range s.memberListProperties() {
237 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
Paul Duffin72910952020-01-20 18:16:30 +0000238
Paul Duffin13f02712020-03-06 12:30:43 +0000239 // Every member specified explicitly in the properties is exported by the sdk.
240 for _, name := range names {
241 exportedMembers[name] = struct{}{}
Paul Duffin72910952020-01-20 18:16:30 +0000242 }
243 }
244
Paul Duffin13f02712020-03-06 12:30:43 +0000245 return exportedMembers
Paul Duffin72910952020-01-20 18:16:30 +0000246}
247
Jiyong Park9b409bc2019-10-11 14:59:13 +0900248func (s *sdk) snapshot() bool {
249 return s.properties.Snapshot
250}
251
Jiyong Parkd1063c12019-07-17 20:08:41 +0900252func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000253 if s.snapshot() {
Jiyong Park232e7852019-11-04 12:23:40 +0900254 // We don't need to create a snapshot out of sdk_snapshot.
255 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000256 return
257 }
258
259 // This method is guaranteed to be called on OsType specific variants before it is called
260 // on their corresponding CommonOS variant.
261 if !s.IsCommonOSVariant() {
262 // Collect the OsType specific members are add them to the OsType specific variant.
263 s.memberRefs = s.collectMembers(ctx)
264 } else {
265 // Get the OsType specific variants on which the CommonOS depends.
266 osSpecificVariants := android.GetOsSpecificVariantsOfCommonOSVariant(ctx)
267 var sdkVariants []*sdk
268 for _, m := range osSpecificVariants {
269 if sdkVariant, ok := m.(*sdk); ok {
270 sdkVariants = append(sdkVariants, sdkVariant)
271 }
272 }
273
274 // Generate the snapshot from the member info.
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000275 p := s.buildSnapshot(ctx, sdkVariants)
276 s.snapshotFile = android.OptionalPathForPath(p)
277 ctx.InstallFile(android.PathForMainlineSdksInstall(ctx), s.Name()+"-current.zip", p)
Jiyong Park232e7852019-11-04 12:23:40 +0900278 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900279}
280
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900281func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries {
Jiyong Park232e7852019-11-04 12:23:40 +0900282 if !s.snapshotFile.Valid() {
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900283 return []android.AndroidMkEntries{}
Jiyong Park232e7852019-11-04 12:23:40 +0900284 }
285
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900286 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park232e7852019-11-04 12:23:40 +0900287 Class: "FAKE",
288 OutputFile: s.snapshotFile,
289 DistFile: s.snapshotFile,
290 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000291 ExtraFooters: []android.AndroidMkExtraFootersFunc{
292 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
293 // Allow the sdk to be built by simply passing its name on the command line.
294 fmt.Fprintln(w, ".PHONY:", s.Name())
295 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
296 },
297 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900298 }}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900299}
300
301// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
302// interface and the sdk module type. This function has been made public to be called by tests
303// outside of the sdk package
304func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
305 ctx.BottomUp("SdkMember", memberMutator).Parallel()
306 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
307 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
308}
309
310// RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware
311// interface and the sdk module type. This function has been made public to be called by tests
312// outside of the sdk package
313func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
314 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
315 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
316 // APEX to its dependents. Since different versions of the same SDK can be used by different
317 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
318 // should have been mutated for the apex before the SDK requirements are set.
319 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
320 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900321 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900322}
323
324type dependencyTag struct {
325 blueprint.BaseDependencyTag
326}
327
Jiyong Parkd1063c12019-07-17 20:08:41 +0900328// For dependencies from an in-development version of an SDK member to frozen versions of the same member
329// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
Paul Duffinfe149222020-01-14 14:06:09 +0000330type sdkMemberVersionedDepTag struct {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900331 dependencyTag
332 member string
333 version string
334}
335
Paul Duffinfe149222020-01-14 14:06:09 +0000336// Mark this tag so dependencies that use it are excluded from visibility enforcement.
337func (t sdkMemberVersionedDepTag) ExcludeFromVisibilityEnforcement() {}
338
Jiyong Parkd1063c12019-07-17 20:08:41 +0900339// Step 1: create dependencies from an SDK module to its members.
340func memberMutator(mctx android.BottomUpMutatorContext) {
Paul Duffin255f18e2019-12-13 11:22:16 +0000341 if s, ok := mctx.Module().(*sdk); ok {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000342 // Add dependencies from enabled and non CommonOS variants to the sdk member variants.
343 if s.Enabled() && !s.IsCommonOSVariant() {
Paul Duffin583bf7e2020-02-20 13:39:41 +0000344 for _, memberListProperty := range s.memberListProperties() {
345 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
Paul Duffincc1b3da2020-02-27 13:29:56 +0000346 if len(names) > 0 {
347 tag := memberListProperty.dependencyTag
348 memberListProperty.memberType.AddDependencies(mctx, tag, names)
349 }
Paul Duffin583bf7e2020-02-20 13:39:41 +0000350 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900351 }
352 }
353}
354
355// Step 2: record that dependencies of SDK modules are members of the SDK modules
356func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900357 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900358 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900359 if s.snapshot() && mySdkRef.Unversioned() {
360 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
361 "Did you manually modify Android.bp?")
362 }
363 if !s.snapshot() && !mySdkRef.Unversioned() {
364 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
365 }
366 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
367 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
368 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
369 }
370 }
371
Jiyong Parkd1063c12019-07-17 20:08:41 +0900372 mctx.VisitDirectDeps(func(child android.Module) {
373 if member, ok := child.(android.SdkAware); ok {
374 member.MakeMemberOf(mySdkRef)
375 }
376 })
377 }
378}
379
Jiyong Park9b409bc2019-10-11 14:59:13 +0900380// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900381// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
382// (apex and apk), each of which might want different sdks to be built with. For example, if both
383// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
384// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
385// using.
386func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
387 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900388 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900389 memberName := m.MemberName()
Paul Duffinfe149222020-01-14 14:06:09 +0000390 tag := sdkMemberVersionedDepTag{member: memberName, version: m.ContainingSdk().Version}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900391 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
392 }
393 }
394}
395
396// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
397// descendants
398func sdkDepsMutator(mctx android.TopDownMutatorContext) {
399 if m, ok := mctx.Module().(android.SdkAware); ok {
400 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
401 // by reading its own properties like `uses_sdks`.
402 requiredSdks := m.RequiredSdks()
403 if len(requiredSdks) > 0 {
404 mctx.VisitDirectDeps(func(m android.Module) {
405 if dep, ok := m.(android.SdkAware); ok {
406 dep.BuildWithSdks(requiredSdks)
407 }
408 })
409 }
410 }
411}
412
413// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
414// versioned module is used instead of the un-versioned (in-development) module libfoo
415func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
416 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900417 if sdk := m.ContainingSdk(); !sdk.Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900418 if m.RequiredSdks().Contains(sdk) {
419 // Note that this replacement is done only for the modules that have the same
420 // variations as the current module. Since current module is already mutated for
421 // apex references in other APEXes are not affected by this replacement.
422 memberName := m.MemberName()
423 mctx.ReplaceDependencies(memberName)
424 }
425 }
426 }
427}
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900428
429// Step 6: ensure that the dependencies from outside of the APEX are all from the required SDKs
430func sdkRequirementsMutator(mctx android.TopDownMutatorContext) {
431 if m, ok := mctx.Module().(interface {
432 DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool
433 RequiredSdks() android.SdkRefs
434 }); ok {
435 requiredSdks := m.RequiredSdks()
436 if len(requiredSdks) == 0 {
437 return
438 }
439 mctx.VisitDirectDeps(func(dep android.Module) {
440 if mctx.OtherModuleDependencyTag(dep) == android.DefaultsDepTag {
441 // dependency to defaults is always okay
442 return
443 }
444
445 // If the dep is from outside of the APEX, but is not in any of the
446 // required SDKs, we know that the dep is a violation.
447 if sa, ok := dep.(android.SdkAware); ok {
448 if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) {
449 mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v",
450 sa.Name(), sa.ContainingSdk(), requiredSdks)
451 }
452 }
453 })
454 }
455}