Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 1 | // 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 | |
| 15 | package sdk |
| 16 | |
| 17 | import ( |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 18 | "fmt" |
Paul Duffin | 504b461 | 2019-11-22 14:52:29 +0000 | [diff] [blame] | 19 | "io" |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 20 | "reflect" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 21 | "strconv" |
| 22 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 23 | "github.com/google/blueprint" |
Jiyong Park | 100f3fd | 2019-11-06 16:03:32 +0900 | [diff] [blame] | 24 | "github.com/google/blueprint/proptools" |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 25 | |
| 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 | |
| 32 | func init() { |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 33 | pctx.Import("android/soong/android") |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 34 | pctx.Import("android/soong/java/config") |
| 35 | |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 36 | android.RegisterModuleType("sdk", SdkModuleFactory) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 37 | android.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 38 | android.PreDepsMutators(RegisterPreDepsMutators) |
| 39 | android.PostDepsMutators(RegisterPostDepsMutators) |
| 40 | } |
| 41 | |
| 42 | type sdk struct { |
| 43 | android.ModuleBase |
| 44 | android.DefaultableModuleBase |
| 45 | |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 46 | // 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 Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 53 | // The set of exported members. |
| 54 | exportedMembers map[string]struct{} |
| 55 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 56 | properties sdkProperties |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 57 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 58 | snapshotFile android.OptionalPath |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 59 | |
| 60 | // The builder, preserved for testing. |
| 61 | builderForTests *snapshotBuilder |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | type sdkProperties struct { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 65 | Snapshot bool `blueprint:"mutated"` |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 66 | |
| 67 | // True if this is a module_exports (or module_exports_snapshot) module type. |
| 68 | Module_exports bool `blueprint:"mutated"` |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 69 | } |
| 70 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 71 | // Contains information about the sdk properties that list sdk members, e.g. |
Paul Duffin | a0dbf43 | 2019-12-05 11:25:53 +0000 | [diff] [blame] | 72 | // Java_header_libs. |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 73 | type sdkMemberListProperty struct { |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 74 | // getter for the list of member names |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 75 | getter func(properties interface{}) []string |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 76 | |
| 77 | // the type of member referenced in the list |
| 78 | memberType android.SdkMemberType |
| 79 | |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 80 | // the dependency tag used for items in this list that can be used to determine the memberType |
| 81 | // for a resolved dependency. |
Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 82 | dependencyTag android.SdkMemberTypeDependencyTag |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 85 | func (p *sdkMemberListProperty) propertyName() string { |
| 86 | return p.memberType.SdkPropertyName() |
| 87 | } |
| 88 | |
| 89 | // Cache of dynamically generated dynamicSdkMemberTypes objects. The key is the pointer |
| 90 | // to a slice of SdkMemberType instances held in android.SdkMemberTypes. |
| 91 | var dynamicSdkMemberTypesMap android.OncePer |
| 92 | |
| 93 | // A dynamically generated set of member list properties and associated structure type. |
| 94 | type dynamicSdkMemberTypes struct { |
| 95 | // The dynamically generated structure type. |
| 96 | // |
| 97 | // Contains one []string exported field for each android.SdkMemberTypes. The name of the field |
| 98 | // is the exported form of the value returned by SdkMemberType.SdkPropertyName(). |
| 99 | propertiesStructType reflect.Type |
| 100 | |
| 101 | // Information about each of the member type specific list properties. |
| 102 | memberListProperties []*sdkMemberListProperty |
| 103 | } |
| 104 | |
| 105 | func (d *dynamicSdkMemberTypes) createMemberListProperties() interface{} { |
| 106 | return reflect.New(d.propertiesStructType).Interface() |
| 107 | } |
| 108 | |
| 109 | func getDynamicSdkMemberTypes(registry *android.SdkMemberTypesRegistry) *dynamicSdkMemberTypes { |
| 110 | |
| 111 | // Get a key that uniquely identifies the registry contents. |
| 112 | key := registry.UniqueOnceKey() |
| 113 | |
| 114 | // Get the registered types. |
| 115 | registeredTypes := registry.RegisteredTypes() |
| 116 | |
| 117 | // Get the cached value, creating new instance if necessary. |
| 118 | return dynamicSdkMemberTypesMap.Once(key, func() interface{} { |
| 119 | return createDynamicSdkMemberTypes(registeredTypes) |
| 120 | }).(*dynamicSdkMemberTypes) |
| 121 | } |
| 122 | |
| 123 | // Create the dynamicSdkMemberTypes from the list of registered member types. |
Paul Duffin | a6e737b | 2019-11-29 11:55:51 +0000 | [diff] [blame] | 124 | // |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 125 | // A struct is created which contains one exported field per member type corresponding to |
| 126 | // the SdkMemberType.SdkPropertyName() value. |
| 127 | // |
| 128 | // A list of sdkMemberListProperty instances is created, one per member type that provides: |
| 129 | // * a reference to the member type. |
| 130 | // * a getter for the corresponding field in the properties struct. |
| 131 | // * a dependency tag that identifies the member type of a resolved dependency. |
| 132 | // |
| 133 | func createDynamicSdkMemberTypes(sdkMemberTypes []android.SdkMemberType) *dynamicSdkMemberTypes { |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 134 | |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 135 | var listProperties []*sdkMemberListProperty |
| 136 | var fields []reflect.StructField |
| 137 | |
| 138 | // Iterate over the member types creating StructField and sdkMemberListProperty objects. |
| 139 | for f, memberType := range sdkMemberTypes { |
| 140 | p := memberType.SdkPropertyName() |
| 141 | |
| 142 | // Create a dynamic exported field for the member type's property. |
| 143 | fields = append(fields, reflect.StructField{ |
| 144 | Name: proptools.FieldNameForProperty(p), |
| 145 | Type: reflect.TypeOf([]string{}), |
| 146 | }) |
| 147 | |
| 148 | // Copy the field index for use in the getter func as using the loop variable directly will |
| 149 | // cause all funcs to use the last value. |
| 150 | fieldIndex := f |
| 151 | |
| 152 | // Create an sdkMemberListProperty for the member type. |
| 153 | memberListProperty := &sdkMemberListProperty{ |
| 154 | getter: func(properties interface{}) []string { |
| 155 | // The properties is expected to be of the following form (where |
| 156 | // <Module_types> is the name of an SdkMemberType.SdkPropertyName(). |
| 157 | // properties *struct {<Module_types> []string, ....} |
| 158 | // |
| 159 | // Although it accesses the field by index the following reflection code is equivalent to: |
| 160 | // *properties.<Module_types> |
| 161 | // |
| 162 | list := reflect.ValueOf(properties).Elem().Field(fieldIndex).Interface().([]string) |
| 163 | return list |
| 164 | }, |
| 165 | |
| 166 | memberType: memberType, |
| 167 | |
Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 168 | dependencyTag: android.DependencyTagForSdkMemberType(memberType), |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | listProperties = append(listProperties, memberListProperty) |
| 172 | } |
| 173 | |
| 174 | // Create a dynamic struct from the collated fields. |
| 175 | propertiesStructType := reflect.StructOf(fields) |
| 176 | |
| 177 | return &dynamicSdkMemberTypes{ |
| 178 | memberListProperties: listProperties, |
| 179 | propertiesStructType: propertiesStructType, |
| 180 | } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 183 | // sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.) |
| 184 | // which Mainline modules like APEX can choose to build with. |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 185 | func SdkModuleFactory() android.Module { |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 186 | return newSdkModule(false) |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 187 | } |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 188 | |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 189 | func newSdkModule(moduleExports bool) *sdk { |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 190 | s := &sdk{} |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 191 | s.properties.Module_exports = moduleExports |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 192 | // Get the dynamic sdk member type data for the currently registered sdk member types. |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 193 | var registry *android.SdkMemberTypesRegistry |
| 194 | if moduleExports { |
| 195 | registry = android.ModuleExportsMemberTypes |
| 196 | } else { |
| 197 | registry = android.SdkMemberTypes |
| 198 | } |
| 199 | s.dynamicSdkMemberTypes = getDynamicSdkMemberTypes(registry) |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 200 | // Create an instance of the dynamically created struct that contains all the |
| 201 | // properties for the member type specific list properties. |
| 202 | s.dynamicMemberTypeListProperties = s.dynamicSdkMemberTypes.createMemberListProperties() |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 203 | s.AddProperties(&s.properties, s.dynamicMemberTypeListProperties) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 204 | android.InitAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon) |
| 205 | android.InitDefaultableModule(s) |
Jiyong Park | 100f3fd | 2019-11-06 16:03:32 +0900 | [diff] [blame] | 206 | android.AddLoadHook(s, func(ctx android.LoadHookContext) { |
| 207 | type props struct { |
| 208 | Compile_multilib *string |
| 209 | } |
| 210 | p := &props{Compile_multilib: proptools.StringPtr("both")} |
| 211 | ctx.AppendProperties(p) |
| 212 | }) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 213 | return s |
| 214 | } |
| 215 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 216 | // sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module. |
| 217 | func SnapshotModuleFactory() android.Module { |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 218 | s := newSdkModule(false) |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 219 | s.properties.Snapshot = true |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 220 | return s |
| 221 | } |
| 222 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 223 | func (s *sdk) memberListProperties() []*sdkMemberListProperty { |
| 224 | return s.dynamicSdkMemberTypes.memberListProperties |
| 225 | } |
| 226 | |
| 227 | func (s *sdk) getExportedMembers() map[string]struct{} { |
| 228 | if s.exportedMembers == nil { |
| 229 | // Collect all the exported members. |
| 230 | s.exportedMembers = make(map[string]struct{}) |
| 231 | |
| 232 | for _, memberListProperty := range s.memberListProperties() { |
| 233 | names := memberListProperty.getter(s.dynamicMemberTypeListProperties) |
| 234 | |
| 235 | // Every member specified explicitly in the properties is exported by the sdk. |
| 236 | for _, name := range names { |
| 237 | s.exportedMembers[name] = struct{}{} |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return s.exportedMembers |
| 243 | } |
| 244 | |
| 245 | func (s *sdk) isInternalMember(memberName string) bool { |
| 246 | _, ok := s.getExportedMembers()[memberName] |
| 247 | return !ok |
| 248 | } |
| 249 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 250 | func (s *sdk) snapshot() bool { |
| 251 | return s.properties.Snapshot |
| 252 | } |
| 253 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 254 | func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 255 | if !s.snapshot() { |
| 256 | // We don't need to create a snapshot out of sdk_snapshot. |
| 257 | // That doesn't make sense. We need a snapshot to create sdk_snapshot. |
| 258 | s.snapshotFile = android.OptionalPathForPath(s.buildSnapshot(ctx)) |
| 259 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 260 | } |
| 261 | |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 262 | func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries { |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 263 | if !s.snapshotFile.Valid() { |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 264 | return []android.AndroidMkEntries{} |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 265 | } |
| 266 | |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 267 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 268 | Class: "FAKE", |
| 269 | OutputFile: s.snapshotFile, |
| 270 | DistFile: s.snapshotFile, |
| 271 | Include: "$(BUILD_PHONY_PACKAGE)", |
Paul Duffin | 504b461 | 2019-11-22 14:52:29 +0000 | [diff] [blame] | 272 | ExtraFooters: []android.AndroidMkExtraFootersFunc{ |
| 273 | func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) { |
| 274 | // Allow the sdk to be built by simply passing its name on the command line. |
| 275 | fmt.Fprintln(w, ".PHONY:", s.Name()) |
| 276 | fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String()) |
| 277 | }, |
| 278 | }, |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 279 | }} |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | // RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware |
| 283 | // interface and the sdk module type. This function has been made public to be called by tests |
| 284 | // outside of the sdk package |
| 285 | func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) { |
| 286 | ctx.BottomUp("SdkMember", memberMutator).Parallel() |
| 287 | ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel() |
| 288 | ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel() |
| 289 | } |
| 290 | |
| 291 | // RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware |
| 292 | // interface and the sdk module type. This function has been made public to be called by tests |
| 293 | // outside of the sdk package |
| 294 | func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) { |
| 295 | // These must run AFTER apexMutator. Note that the apex package is imported even though there is |
| 296 | // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an |
| 297 | // APEX to its dependents. Since different versions of the same SDK can be used by different |
| 298 | // APEXes, the apex and its dependents (which includes the dependencies to the sdk members) |
| 299 | // should have been mutated for the apex before the SDK requirements are set. |
| 300 | ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel() |
| 301 | ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel() |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 302 | ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel() |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | type dependencyTag struct { |
| 306 | blueprint.BaseDependencyTag |
| 307 | } |
| 308 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 309 | // For dependencies from an in-development version of an SDK member to frozen versions of the same member |
| 310 | // e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12 |
Paul Duffin | fe14922 | 2020-01-14 14:06:09 +0000 | [diff] [blame] | 311 | type sdkMemberVersionedDepTag struct { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 312 | dependencyTag |
| 313 | member string |
| 314 | version string |
| 315 | } |
| 316 | |
Paul Duffin | fe14922 | 2020-01-14 14:06:09 +0000 | [diff] [blame] | 317 | // Mark this tag so dependencies that use it are excluded from visibility enforcement. |
| 318 | func (t sdkMemberVersionedDepTag) ExcludeFromVisibilityEnforcement() {} |
| 319 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 320 | // Step 1: create dependencies from an SDK module to its members. |
| 321 | func memberMutator(mctx android.BottomUpMutatorContext) { |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 322 | if s, ok := mctx.Module().(*sdk); ok { |
Paul Duffin | 583bf7e | 2020-02-20 13:39:41 +0000 | [diff] [blame^] | 323 | if s.Enabled() { |
| 324 | for _, memberListProperty := range s.memberListProperties() { |
| 325 | names := memberListProperty.getter(s.dynamicMemberTypeListProperties) |
| 326 | tag := memberListProperty.dependencyTag |
| 327 | memberListProperty.memberType.AddDependencies(mctx, tag, names) |
| 328 | } |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | // Step 2: record that dependencies of SDK modules are members of the SDK modules |
| 334 | func memberDepsMutator(mctx android.TopDownMutatorContext) { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 335 | if s, ok := mctx.Module().(*sdk); ok { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 336 | mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name") |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 337 | if s.snapshot() && mySdkRef.Unversioned() { |
| 338 | mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+ |
| 339 | "Did you manually modify Android.bp?") |
| 340 | } |
| 341 | if !s.snapshot() && !mySdkRef.Unversioned() { |
| 342 | mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.") |
| 343 | } |
| 344 | if mySdkRef.Version != "" && mySdkRef.Version != "current" { |
| 345 | if _, err := strconv.Atoi(mySdkRef.Version); err != nil { |
| 346 | mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version) |
| 347 | } |
| 348 | } |
| 349 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 350 | mctx.VisitDirectDeps(func(child android.Module) { |
| 351 | if member, ok := child.(android.SdkAware); ok { |
| 352 | member.MakeMemberOf(mySdkRef) |
| 353 | } |
| 354 | }) |
| 355 | } |
| 356 | } |
| 357 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 358 | // Step 3: create dependencies from the unversioned SDK member to snapshot versions |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 359 | // of the same member. By having these dependencies, they are mutated for multiple Mainline modules |
| 360 | // (apex and apk), each of which might want different sdks to be built with. For example, if both |
| 361 | // apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be |
| 362 | // built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are |
| 363 | // using. |
| 364 | func memberInterVersionMutator(mctx android.BottomUpMutatorContext) { |
| 365 | if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 366 | if !m.ContainingSdk().Unversioned() { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 367 | memberName := m.MemberName() |
Paul Duffin | fe14922 | 2020-01-14 14:06:09 +0000 | [diff] [blame] | 368 | tag := sdkMemberVersionedDepTag{member: memberName, version: m.ContainingSdk().Version} |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 369 | mctx.AddReverseDependency(mctx.Module(), tag, memberName) |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | // Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its |
| 375 | // descendants |
| 376 | func sdkDepsMutator(mctx android.TopDownMutatorContext) { |
| 377 | if m, ok := mctx.Module().(android.SdkAware); ok { |
| 378 | // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks() |
| 379 | // by reading its own properties like `uses_sdks`. |
| 380 | requiredSdks := m.RequiredSdks() |
| 381 | if len(requiredSdks) > 0 { |
| 382 | mctx.VisitDirectDeps(func(m android.Module) { |
| 383 | if dep, ok := m.(android.SdkAware); ok { |
| 384 | dep.BuildWithSdks(requiredSdks) |
| 385 | } |
| 386 | }) |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | // Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the |
| 392 | // versioned module is used instead of the un-versioned (in-development) module libfoo |
| 393 | func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) { |
| 394 | if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 395 | if sdk := m.ContainingSdk(); !sdk.Unversioned() { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 396 | if m.RequiredSdks().Contains(sdk) { |
| 397 | // Note that this replacement is done only for the modules that have the same |
| 398 | // variations as the current module. Since current module is already mutated for |
| 399 | // apex references in other APEXes are not affected by this replacement. |
| 400 | memberName := m.MemberName() |
| 401 | mctx.ReplaceDependencies(memberName) |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 406 | |
| 407 | // Step 6: ensure that the dependencies from outside of the APEX are all from the required SDKs |
| 408 | func sdkRequirementsMutator(mctx android.TopDownMutatorContext) { |
| 409 | if m, ok := mctx.Module().(interface { |
| 410 | DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool |
| 411 | RequiredSdks() android.SdkRefs |
| 412 | }); ok { |
| 413 | requiredSdks := m.RequiredSdks() |
| 414 | if len(requiredSdks) == 0 { |
| 415 | return |
| 416 | } |
| 417 | mctx.VisitDirectDeps(func(dep android.Module) { |
| 418 | if mctx.OtherModuleDependencyTag(dep) == android.DefaultsDepTag { |
| 419 | // dependency to defaults is always okay |
| 420 | return |
| 421 | } |
| 422 | |
| 423 | // If the dep is from outside of the APEX, but is not in any of the |
| 424 | // required SDKs, we know that the dep is a violation. |
| 425 | if sa, ok := dep.(android.SdkAware); ok { |
| 426 | if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) { |
| 427 | mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v", |
| 428 | sa.Name(), sa.ContainingSdk(), requiredSdks) |
| 429 | } |
| 430 | } |
| 431 | }) |
| 432 | } |
| 433 | } |