blob: a972f31da04ad8b11e6e57729804f735defc15e5 [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 Duffin21827262021-04-24 12:16:36 +010057 // Information about the OsType specific member variants depended upon by this variant.
Paul Duffin1356d8c2020-02-25 19:26:33 +000058 //
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 Duffin21827262021-04-24 12:16:36 +010064 memberVariantDeps []sdkMemberVariantDep
Paul Duffin1356d8c2020-02-25 19:26:33 +000065
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
Paul Duffincd064672021-04-24 00:47:29 +0100104 // setter for the list of member names
105 setter func(properties interface{}, list []string)
106
Paul Duffin13879572019-11-28 14:31:38 +0000107 // the type of member referenced in the list
108 memberType android.SdkMemberType
109
Paul Duffin255f18e2019-12-13 11:22:16 +0000110 // the dependency tag used for items in this list that can be used to determine the memberType
111 // for a resolved dependency.
Paul Duffinf8539922019-11-19 19:44:10 +0000112 dependencyTag android.SdkMemberTypeDependencyTag
Paul Duffin13879572019-11-28 14:31:38 +0000113}
114
Paul Duffin255f18e2019-12-13 11:22:16 +0000115func (p *sdkMemberListProperty) propertyName() string {
116 return p.memberType.SdkPropertyName()
117}
118
119// Cache of dynamically generated dynamicSdkMemberTypes objects. The key is the pointer
120// to a slice of SdkMemberType instances held in android.SdkMemberTypes.
121var dynamicSdkMemberTypesMap android.OncePer
122
123// A dynamically generated set of member list properties and associated structure type.
124type dynamicSdkMemberTypes struct {
125 // The dynamically generated structure type.
126 //
127 // Contains one []string exported field for each android.SdkMemberTypes. The name of the field
128 // is the exported form of the value returned by SdkMemberType.SdkPropertyName().
129 propertiesStructType reflect.Type
130
131 // Information about each of the member type specific list properties.
132 memberListProperties []*sdkMemberListProperty
Paul Duffincd064672021-04-24 00:47:29 +0100133
134 memberTypeToProperty map[android.SdkMemberType]*sdkMemberListProperty
Paul Duffin255f18e2019-12-13 11:22:16 +0000135}
136
137func (d *dynamicSdkMemberTypes) createMemberListProperties() interface{} {
138 return reflect.New(d.propertiesStructType).Interface()
139}
140
141func getDynamicSdkMemberTypes(registry *android.SdkMemberTypesRegistry) *dynamicSdkMemberTypes {
142
143 // Get a key that uniquely identifies the registry contents.
144 key := registry.UniqueOnceKey()
145
146 // Get the registered types.
147 registeredTypes := registry.RegisteredTypes()
148
149 // Get the cached value, creating new instance if necessary.
150 return dynamicSdkMemberTypesMap.Once(key, func() interface{} {
151 return createDynamicSdkMemberTypes(registeredTypes)
152 }).(*dynamicSdkMemberTypes)
153}
154
155// Create the dynamicSdkMemberTypes from the list of registered member types.
Paul Duffina6e737b2019-11-29 11:55:51 +0000156//
Paul Duffin255f18e2019-12-13 11:22:16 +0000157// A struct is created which contains one exported field per member type corresponding to
158// the SdkMemberType.SdkPropertyName() value.
159//
160// A list of sdkMemberListProperty instances is created, one per member type that provides:
161// * a reference to the member type.
162// * a getter for the corresponding field in the properties struct.
163// * a dependency tag that identifies the member type of a resolved dependency.
164//
165func createDynamicSdkMemberTypes(sdkMemberTypes []android.SdkMemberType) *dynamicSdkMemberTypes {
Paul Duffine6029182019-12-16 17:43:48 +0000166
Paul Duffin255f18e2019-12-13 11:22:16 +0000167 var listProperties []*sdkMemberListProperty
Paul Duffincd064672021-04-24 00:47:29 +0100168 memberTypeToProperty := map[android.SdkMemberType]*sdkMemberListProperty{}
Paul Duffin255f18e2019-12-13 11:22:16 +0000169 var fields []reflect.StructField
170
171 // Iterate over the member types creating StructField and sdkMemberListProperty objects.
Paul Duffin13082052021-05-11 00:31:38 +0100172 nextFieldIndex := 0
173 for _, memberType := range sdkMemberTypes {
174
Paul Duffin255f18e2019-12-13 11:22:16 +0000175 p := memberType.SdkPropertyName()
176
Paul Duffin13082052021-05-11 00:31:38 +0100177 var getter func(properties interface{}) []string
178 var setter func(properties interface{}, list []string)
179 if memberType.RequiresBpProperty() {
180 // Create a dynamic exported field for the member type's property.
181 fields = append(fields, reflect.StructField{
182 Name: proptools.FieldNameForProperty(p),
183 Type: reflect.TypeOf([]string{}),
184 Tag: `android:"arch_variant"`,
185 })
Paul Duffin255f18e2019-12-13 11:22:16 +0000186
Paul Duffin13082052021-05-11 00:31:38 +0100187 // Copy the field index for use in the getter func as using the loop variable directly will
188 // cause all funcs to use the last value.
189 fieldIndex := nextFieldIndex
190 nextFieldIndex += 1
Paul Duffin255f18e2019-12-13 11:22:16 +0000191
Paul Duffin13082052021-05-11 00:31:38 +0100192 getter = func(properties interface{}) []string {
Paul Duffin255f18e2019-12-13 11:22:16 +0000193 // The properties is expected to be of the following form (where
194 // <Module_types> is the name of an SdkMemberType.SdkPropertyName().
195 // properties *struct {<Module_types> []string, ....}
196 //
197 // Although it accesses the field by index the following reflection code is equivalent to:
198 // *properties.<Module_types>
199 //
200 list := reflect.ValueOf(properties).Elem().Field(fieldIndex).Interface().([]string)
201 return list
Paul Duffin13082052021-05-11 00:31:38 +0100202 }
Paul Duffin255f18e2019-12-13 11:22:16 +0000203
Paul Duffin13082052021-05-11 00:31:38 +0100204 setter = func(properties interface{}, list []string) {
Paul Duffincd064672021-04-24 00:47:29 +0100205 // The properties is expected to be of the following form (where
206 // <Module_types> is the name of an SdkMemberType.SdkPropertyName().
207 // properties *struct {<Module_types> []string, ....}
208 //
209 // Although it accesses the field by index the following reflection code is equivalent to:
210 // *properties.<Module_types> = list
211 //
212 reflect.ValueOf(properties).Elem().Field(fieldIndex).Set(reflect.ValueOf(list))
Paul Duffin13082052021-05-11 00:31:38 +0100213 }
214 }
Paul Duffincd064672021-04-24 00:47:29 +0100215
Paul Duffin13082052021-05-11 00:31:38 +0100216 // Create an sdkMemberListProperty for the member type.
217 memberListProperty := &sdkMemberListProperty{
218 getter: getter,
219 setter: setter,
Paul Duffin255f18e2019-12-13 11:22:16 +0000220 memberType: memberType,
221
Paul Duffina7208112021-04-23 21:20:20 +0100222 // Dependencies added directly from member properties are always exported.
223 dependencyTag: android.DependencyTagForSdkMemberType(memberType, true),
Paul Duffin255f18e2019-12-13 11:22:16 +0000224 }
225
Paul Duffincd064672021-04-24 00:47:29 +0100226 memberTypeToProperty[memberType] = memberListProperty
Paul Duffin255f18e2019-12-13 11:22:16 +0000227 listProperties = append(listProperties, memberListProperty)
228 }
229
230 // Create a dynamic struct from the collated fields.
231 propertiesStructType := reflect.StructOf(fields)
232
233 return &dynamicSdkMemberTypes{
234 memberListProperties: listProperties,
Paul Duffincd064672021-04-24 00:47:29 +0100235 memberTypeToProperty: memberTypeToProperty,
Paul Duffin255f18e2019-12-13 11:22:16 +0000236 propertiesStructType: propertiesStructType,
237 }
Paul Duffin13879572019-11-28 14:31:38 +0000238}
239
Jiyong Parkd1063c12019-07-17 20:08:41 +0900240// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
241// which Mainline modules like APEX can choose to build with.
Paul Duffin8150da62019-12-16 17:21:27 +0000242func SdkModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000243 return newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000244}
Paul Duffin255f18e2019-12-13 11:22:16 +0000245
Paul Duffine6029182019-12-16 17:43:48 +0000246func newSdkModule(moduleExports bool) *sdk {
Paul Duffin8150da62019-12-16 17:21:27 +0000247 s := &sdk{}
Paul Duffine6029182019-12-16 17:43:48 +0000248 s.properties.Module_exports = moduleExports
Paul Duffin255f18e2019-12-13 11:22:16 +0000249 // Get the dynamic sdk member type data for the currently registered sdk member types.
Paul Duffine6029182019-12-16 17:43:48 +0000250 var registry *android.SdkMemberTypesRegistry
251 if moduleExports {
252 registry = android.ModuleExportsMemberTypes
253 } else {
254 registry = android.SdkMemberTypes
255 }
256 s.dynamicSdkMemberTypes = getDynamicSdkMemberTypes(registry)
Paul Duffin255f18e2019-12-13 11:22:16 +0000257 // Create an instance of the dynamically created struct that contains all the
258 // properties for the member type specific list properties.
259 s.dynamicMemberTypeListProperties = s.dynamicSdkMemberTypes.createMemberListProperties()
Paul Duffin255f18e2019-12-13 11:22:16 +0000260 s.AddProperties(&s.properties, s.dynamicMemberTypeListProperties)
Paul Duffin157f40f2020-09-29 16:01:08 +0100261
262 // Make sure that the prebuilt visibility property is verified for errors.
263 android.AddVisibilityProperty(s, "prebuilt_visibility", &s.properties.Prebuilt_visibility)
Paul Duffin1356d8c2020-02-25 19:26:33 +0000264 android.InitCommonOSAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900265 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900266 android.AddLoadHook(s, func(ctx android.LoadHookContext) {
267 type props struct {
268 Compile_multilib *string
269 }
270 p := &props{Compile_multilib: proptools.StringPtr("both")}
Martin Stjernholm26ab8e82020-06-30 20:34:00 +0100271 ctx.PrependProperties(p)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900272 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900273 return s
274}
275
Jiyong Park9b409bc2019-10-11 14:59:13 +0900276// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
277func SnapshotModuleFactory() android.Module {
Paul Duffine6029182019-12-16 17:43:48 +0000278 s := newSdkModule(false)
Paul Duffin8150da62019-12-16 17:21:27 +0000279 s.properties.Snapshot = true
Jiyong Park9b409bc2019-10-11 14:59:13 +0900280 return s
281}
282
Paul Duffin72910952020-01-20 18:16:30 +0000283func (s *sdk) memberListProperties() []*sdkMemberListProperty {
284 return s.dynamicSdkMemberTypes.memberListProperties
285}
286
Paul Duffincd064672021-04-24 00:47:29 +0100287func (s *sdk) memberListProperty(memberType android.SdkMemberType) *sdkMemberListProperty {
288 return s.dynamicSdkMemberTypes.memberTypeToProperty[memberType]
289}
290
Jiyong Park9b409bc2019-10-11 14:59:13 +0900291func (s *sdk) snapshot() bool {
292 return s.properties.Snapshot
293}
294
Jiyong Parkd1063c12019-07-17 20:08:41 +0900295func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000296 if s.snapshot() {
Jiyong Park232e7852019-11-04 12:23:40 +0900297 // We don't need to create a snapshot out of sdk_snapshot.
298 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000299 return
300 }
301
302 // This method is guaranteed to be called on OsType specific variants before it is called
303 // on their corresponding CommonOS variant.
304 if !s.IsCommonOSVariant() {
Paul Duffin6a7e9532020-03-20 17:50:07 +0000305 // Update the OsType specific sdk variant with information about its members.
306 s.collectMembers(ctx)
Paul Duffin1356d8c2020-02-25 19:26:33 +0000307 } else {
308 // Get the OsType specific variants on which the CommonOS depends.
309 osSpecificVariants := android.GetOsSpecificVariantsOfCommonOSVariant(ctx)
310 var sdkVariants []*sdk
311 for _, m := range osSpecificVariants {
312 if sdkVariant, ok := m.(*sdk); ok {
313 sdkVariants = append(sdkVariants, sdkVariant)
314 }
315 }
316
317 // Generate the snapshot from the member info.
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000318 p := s.buildSnapshot(ctx, sdkVariants)
Mathew Inwood60770e22021-05-05 17:00:29 +0100319 zip := ctx.InstallFile(android.PathForMainlineSdksInstall(ctx), p.Base(), p)
320 s.snapshotFile = android.OptionalPathForPath(zip)
Jiyong Park232e7852019-11-04 12:23:40 +0900321 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900322}
323
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900324func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries {
Jiyong Park232e7852019-11-04 12:23:40 +0900325 if !s.snapshotFile.Valid() {
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900326 return []android.AndroidMkEntries{}
Jiyong Park232e7852019-11-04 12:23:40 +0900327 }
328
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900329 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park232e7852019-11-04 12:23:40 +0900330 Class: "FAKE",
331 OutputFile: s.snapshotFile,
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000332 DistFiles: android.MakeDefaultDistFiles(s.snapshotFile.Path()),
Jiyong Park232e7852019-11-04 12:23:40 +0900333 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000334 ExtraFooters: []android.AndroidMkExtraFootersFunc{
Jaewoong Jung02b11a62020-12-07 10:23:54 -0800335 func(w io.Writer, name, prefix, moduleDir string) {
Paul Duffin504b4612019-11-22 14:52:29 +0000336 // Allow the sdk to be built by simply passing its name on the command line.
337 fmt.Fprintln(w, ".PHONY:", s.Name())
338 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
339 },
340 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900341 }}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900342}
343
Paul Duffin296701e2021-07-14 10:29:36 +0100344// newDependencyContext creates a new SdkDependencyContext for this sdk.
345func (s *sdk) newDependencyContext(mctx android.BottomUpMutatorContext) android.SdkDependencyContext {
346 return &dependencyContext{
347 BottomUpMutatorContext: mctx,
348 }
349}
350
351type dependencyContext struct {
352 android.BottomUpMutatorContext
353}
354
355var _ android.SdkDependencyContext = (*dependencyContext)(nil)
356
Jiyong Parkd1063c12019-07-17 20:08:41 +0900357// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
358// interface and the sdk module type. This function has been made public to be called by tests
359// outside of the sdk package
360func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
361 ctx.BottomUp("SdkMember", memberMutator).Parallel()
362 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
363 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
364}
365
Paul Duffin65347702020-03-31 15:23:40 +0100366// RegisterPostDepsMutators registers post-deps mutators to support modules implementing SdkAware
Jiyong Parkd1063c12019-07-17 20:08:41 +0900367// interface and the sdk module type. This function has been made public to be called by tests
368// outside of the sdk package
369func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
370 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
371 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
372 // APEX to its dependents. Since different versions of the same SDK can be used by different
373 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
374 // should have been mutated for the apex before the SDK requirements are set.
375 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
376 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900377 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900378}
379
380type dependencyTag struct {
381 blueprint.BaseDependencyTag
382}
383
Martin Stjernholmcc776012020-07-07 03:22:21 +0100384// Mark this tag so dependencies that use it are excluded from APEX contents.
385func (t dependencyTag) ExcludeFromApexContents() {}
386
387var _ android.ExcludeFromApexContentsTag = dependencyTag{}
388
Jiyong Parkd1063c12019-07-17 20:08:41 +0900389// For dependencies from an in-development version of an SDK member to frozen versions of the same member
390// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
Paul Duffin573989d2021-03-17 13:25:29 +0000391//
392// The dependency represented by this tag requires that for every APEX variant created for the
393// `from` module that an equivalent APEX variant is created for the 'to' module. This is because an
394// APEX that requires a specific version of an sdk (via the `uses_sdks` property will replace
395// dependencies on the unversioned sdk member with a dependency on the appropriate versioned sdk
396// member. In order for that to work the versioned sdk member needs to have a variant for that APEX.
397// As it is not known at the time that the APEX variants are created which specific APEX variants of
398// a versioned sdk members will be required it is necessary for the versioned sdk members to have
399// variants for any APEX that it could be used within.
400//
401// If the APEX selects a versioned sdk member then it will not have a dependency on the `from`
402// module at all so any dependencies of that module will not affect the APEX. However, if the APEX
403// selects the unversioned sdk member then it must exclude all the versioned sdk members. In no
404// situation would this dependency cause the `to` module to be added to the APEX hence why this tag
405// also excludes the `to` module from being added to the APEX contents.
Paul Duffinfe149222020-01-14 14:06:09 +0000406type sdkMemberVersionedDepTag struct {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900407 dependencyTag
408 member string
409 version string
410}
411
Paul Duffin573989d2021-03-17 13:25:29 +0000412func (t sdkMemberVersionedDepTag) AlwaysRequireApexVariant() bool {
413 return true
414}
415
Paul Duffinfe149222020-01-14 14:06:09 +0000416// Mark this tag so dependencies that use it are excluded from visibility enforcement.
417func (t sdkMemberVersionedDepTag) ExcludeFromVisibilityEnforcement() {}
418
Paul Duffin573989d2021-03-17 13:25:29 +0000419var _ android.AlwaysRequireApexVariantTag = sdkMemberVersionedDepTag{}
420
Jiyong Parkd1063c12019-07-17 20:08:41 +0900421// Step 1: create dependencies from an SDK module to its members.
422func memberMutator(mctx android.BottomUpMutatorContext) {
Paul Duffin255f18e2019-12-13 11:22:16 +0000423 if s, ok := mctx.Module().(*sdk); ok {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000424 // Add dependencies from enabled and non CommonOS variants to the sdk member variants.
425 if s.Enabled() && !s.IsCommonOSVariant() {
Paul Duffin296701e2021-07-14 10:29:36 +0100426 ctx := s.newDependencyContext(mctx)
Paul Duffin583bf7e2020-02-20 13:39:41 +0000427 for _, memberListProperty := range s.memberListProperties() {
Paul Duffin13082052021-05-11 00:31:38 +0100428 if memberListProperty.getter == nil {
429 continue
430 }
Paul Duffin583bf7e2020-02-20 13:39:41 +0000431 names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
Paul Duffincc1b3da2020-02-27 13:29:56 +0000432 if len(names) > 0 {
433 tag := memberListProperty.dependencyTag
Paul Duffin296701e2021-07-14 10:29:36 +0100434 memberListProperty.memberType.AddDependencies(ctx, tag, names)
Paul Duffincc1b3da2020-02-27 13:29:56 +0000435 }
Paul Duffin583bf7e2020-02-20 13:39:41 +0000436 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900437 }
438 }
439}
440
441// Step 2: record that dependencies of SDK modules are members of the SDK modules
442func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900443 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900444 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900445 if s.snapshot() && mySdkRef.Unversioned() {
446 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
447 "Did you manually modify Android.bp?")
448 }
449 if !s.snapshot() && !mySdkRef.Unversioned() {
450 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
451 }
452 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
453 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
454 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
455 }
456 }
457
Jiyong Parkd1063c12019-07-17 20:08:41 +0900458 mctx.VisitDirectDeps(func(child android.Module) {
459 if member, ok := child.(android.SdkAware); ok {
460 member.MakeMemberOf(mySdkRef)
461 }
462 })
463 }
464}
465
Jiyong Park9b409bc2019-10-11 14:59:13 +0900466// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900467// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
468// (apex and apk), each of which might want different sdks to be built with. For example, if both
469// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
470// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
471// using.
472func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
Paul Duffinb9e7a3c2021-05-06 15:53:19 +0100473 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() && m.IsVersioned() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900474 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900475 memberName := m.MemberName()
Paul Duffinfe149222020-01-14 14:06:09 +0000476 tag := sdkMemberVersionedDepTag{member: memberName, version: m.ContainingSdk().Version}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900477 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
478 }
479 }
480}
481
Paul Duffin4c3e8e22021-03-18 15:41:29 +0000482// An interface that encapsulates all the functionality needed to manage the sdk dependencies.
483//
484// It is a mixture of apex and sdk module functionality.
485type sdkAndApexModule interface {
486 android.Module
487 android.DepIsInSameApex
488 android.RequiredSdks
489}
490
Jiyong Parkd1063c12019-07-17 20:08:41 +0900491// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
492// descendants
493func sdkDepsMutator(mctx android.TopDownMutatorContext) {
Paul Duffin4c3e8e22021-03-18 15:41:29 +0000494 if parent, ok := mctx.Module().(sdkAndApexModule); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900495 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
496 // by reading its own properties like `uses_sdks`.
Paul Duffina37eca22020-07-22 13:00:54 +0100497 requiredSdks := parent.RequiredSdks()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900498 if len(requiredSdks) > 0 {
499 mctx.VisitDirectDeps(func(m android.Module) {
Paul Duffina37eca22020-07-22 13:00:54 +0100500 // Only propagate required sdks from the apex onto its contents.
Paul Duffin4c3e8e22021-03-18 15:41:29 +0000501 if dep, ok := m.(android.SdkAware); ok && android.IsDepInSameApex(mctx, parent, dep) {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900502 dep.BuildWithSdks(requiredSdks)
503 }
504 })
505 }
506 }
507}
508
509// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
510// versioned module is used instead of the un-versioned (in-development) module libfoo
511func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
Paul Duffinb9e7a3c2021-05-06 15:53:19 +0100512 if versionedSdkMember, ok := mctx.Module().(android.SdkAware); ok && versionedSdkMember.IsInAnySdk() && versionedSdkMember.IsVersioned() {
Paul Duffina37eca22020-07-22 13:00:54 +0100513 if sdk := versionedSdkMember.ContainingSdk(); !sdk.Unversioned() {
514 // Only replace dependencies to <sdkmember> with <sdkmember@required-version>
515 // if the depending module requires it. e.g.
516 // foo -> sdkmember
517 // will be transformed to:
518 // foo -> sdkmember@1
519 // if and only if foo is a member of an APEX that requires version 1 of the
520 // sdk containing sdkmember.
521 memberName := versionedSdkMember.MemberName()
522
Paul Duffin1822a0a2021-03-21 12:56:33 +0000523 // Convert a panic into a normal error to allow it to be more easily tested for. This is a
524 // temporary workaround, once http://b/183204176 has been fixed this can be removed.
525 // TODO(b/183204176): Remove this after fixing.
526 defer func() {
527 if r := recover(); r != nil {
Paul Duffinb9e7a3c2021-05-06 15:53:19 +0100528 mctx.ModuleErrorf("sdkDepsReplaceMutator %s", r)
Paul Duffin1822a0a2021-03-21 12:56:33 +0000529 }
530 }()
531
Paul Duffina37eca22020-07-22 13:00:54 +0100532 // Replace dependencies on sdkmember with a dependency on the current module which
533 // is a versioned prebuilt of the sdkmember if required.
534 mctx.ReplaceDependenciesIf(memberName, func(from blueprint.Module, tag blueprint.DependencyTag, to blueprint.Module) bool {
535 // from - foo
536 // to - sdkmember
537 replace := false
538 if parent, ok := from.(android.RequiredSdks); ok {
539 replace = parent.RequiredSdks().Contains(sdk)
540 }
541 return replace
542 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900543 }
544 }
545}
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900546
Paul Duffin65347702020-03-31 15:23:40 +0100547// Step 6: ensure that the dependencies outside of the APEX are all from the required SDKs
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900548func sdkRequirementsMutator(mctx android.TopDownMutatorContext) {
Paul Duffin4c3e8e22021-03-18 15:41:29 +0000549 if m, ok := mctx.Module().(sdkAndApexModule); ok {
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900550 requiredSdks := m.RequiredSdks()
551 if len(requiredSdks) == 0 {
552 return
553 }
554 mctx.VisitDirectDeps(func(dep android.Module) {
Paul Duffin65347702020-03-31 15:23:40 +0100555 tag := mctx.OtherModuleDependencyTag(dep)
556 if tag == android.DefaultsDepTag {
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900557 // dependency to defaults is always okay
558 return
559 }
560
Paul Duffin65347702020-03-31 15:23:40 +0100561 // Ignore the dependency from the unversioned member to any versioned members as an
562 // apex that depends on the unversioned member will not also be depending on a versioned
563 // member.
564 if _, ok := tag.(sdkMemberVersionedDepTag); ok {
565 return
566 }
567
Paul Duffin4c3e8e22021-03-18 15:41:29 +0000568 // If the dep is outside of the APEX, but is not in any of the required SDKs, we know that the
569 // dep is a violation.
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900570 if sa, ok := dep.(android.SdkAware); ok {
Paul Duffin4c3e8e22021-03-18 15:41:29 +0000571 // It is not an error if a dependency that is excluded from the apex due to the tag is not
572 // in one of the required SDKs. That is because all of the existing tags that implement it
573 // do not depend on modules which can or should belong to an sdk_snapshot.
574 if _, ok := tag.(android.ExcludeFromApexContentsTag); ok {
575 // The tag defines a dependency that never requires the child module to be part of the
576 // same apex.
577 return
578 }
579
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900580 if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) {
581 mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v",
582 sa.Name(), sa.ContainingSdk(), requiredSdks)
583 }
584 }
585 })
586 }
587}