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 | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 53 | // Information about the OsType specific member variants associated with this variant. |
| 54 | // |
Paul Duffin | 6a7e953 | 2020-03-20 17:50:07 +0000 | [diff] [blame] | 55 | // Set by OsType specific variants in the collectMembers() method and used by the |
| 56 | // CommonOS variant when building the snapshot. That work is all done on separate |
| 57 | // calls to the sdk.GenerateAndroidBuildActions method which is guaranteed to be |
| 58 | // called for the OsType specific variants before the CommonOS variant (because |
| 59 | // the latter depends on the former). |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 60 | memberRefs []sdkMemberRef |
| 61 | |
Paul Duffin | 6a7e953 | 2020-03-20 17:50:07 +0000 | [diff] [blame] | 62 | // The multilib variants that are used by this sdk variant. |
| 63 | multilibUsages multilibUsage |
| 64 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 65 | properties sdkProperties |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 66 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 67 | snapshotFile android.OptionalPath |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 68 | |
| 69 | // The builder, preserved for testing. |
| 70 | builderForTests *snapshotBuilder |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | type sdkProperties struct { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 74 | Snapshot bool `blueprint:"mutated"` |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 75 | |
| 76 | // True if this is a module_exports (or module_exports_snapshot) module type. |
| 77 | Module_exports bool `blueprint:"mutated"` |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 78 | } |
| 79 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 80 | // Contains information about the sdk properties that list sdk members, e.g. |
Paul Duffin | a0dbf43 | 2019-12-05 11:25:53 +0000 | [diff] [blame] | 81 | // Java_header_libs. |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 82 | type sdkMemberListProperty struct { |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 83 | // getter for the list of member names |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 84 | getter func(properties interface{}) []string |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 85 | |
| 86 | // the type of member referenced in the list |
| 87 | memberType android.SdkMemberType |
| 88 | |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 89 | // the dependency tag used for items in this list that can be used to determine the memberType |
| 90 | // for a resolved dependency. |
Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 91 | dependencyTag android.SdkMemberTypeDependencyTag |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 94 | func (p *sdkMemberListProperty) propertyName() string { |
| 95 | return p.memberType.SdkPropertyName() |
| 96 | } |
| 97 | |
| 98 | // Cache of dynamically generated dynamicSdkMemberTypes objects. The key is the pointer |
| 99 | // to a slice of SdkMemberType instances held in android.SdkMemberTypes. |
| 100 | var dynamicSdkMemberTypesMap android.OncePer |
| 101 | |
| 102 | // A dynamically generated set of member list properties and associated structure type. |
| 103 | type dynamicSdkMemberTypes struct { |
| 104 | // The dynamically generated structure type. |
| 105 | // |
| 106 | // Contains one []string exported field for each android.SdkMemberTypes. The name of the field |
| 107 | // is the exported form of the value returned by SdkMemberType.SdkPropertyName(). |
| 108 | propertiesStructType reflect.Type |
| 109 | |
| 110 | // Information about each of the member type specific list properties. |
| 111 | memberListProperties []*sdkMemberListProperty |
| 112 | } |
| 113 | |
| 114 | func (d *dynamicSdkMemberTypes) createMemberListProperties() interface{} { |
| 115 | return reflect.New(d.propertiesStructType).Interface() |
| 116 | } |
| 117 | |
| 118 | func getDynamicSdkMemberTypes(registry *android.SdkMemberTypesRegistry) *dynamicSdkMemberTypes { |
| 119 | |
| 120 | // Get a key that uniquely identifies the registry contents. |
| 121 | key := registry.UniqueOnceKey() |
| 122 | |
| 123 | // Get the registered types. |
| 124 | registeredTypes := registry.RegisteredTypes() |
| 125 | |
| 126 | // Get the cached value, creating new instance if necessary. |
| 127 | return dynamicSdkMemberTypesMap.Once(key, func() interface{} { |
| 128 | return createDynamicSdkMemberTypes(registeredTypes) |
| 129 | }).(*dynamicSdkMemberTypes) |
| 130 | } |
| 131 | |
| 132 | // Create the dynamicSdkMemberTypes from the list of registered member types. |
Paul Duffin | a6e737b | 2019-11-29 11:55:51 +0000 | [diff] [blame] | 133 | // |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 134 | // A struct is created which contains one exported field per member type corresponding to |
| 135 | // the SdkMemberType.SdkPropertyName() value. |
| 136 | // |
| 137 | // A list of sdkMemberListProperty instances is created, one per member type that provides: |
| 138 | // * a reference to the member type. |
| 139 | // * a getter for the corresponding field in the properties struct. |
| 140 | // * a dependency tag that identifies the member type of a resolved dependency. |
| 141 | // |
| 142 | func createDynamicSdkMemberTypes(sdkMemberTypes []android.SdkMemberType) *dynamicSdkMemberTypes { |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 143 | |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 144 | var listProperties []*sdkMemberListProperty |
| 145 | var fields []reflect.StructField |
| 146 | |
| 147 | // Iterate over the member types creating StructField and sdkMemberListProperty objects. |
| 148 | for f, memberType := range sdkMemberTypes { |
| 149 | p := memberType.SdkPropertyName() |
| 150 | |
| 151 | // Create a dynamic exported field for the member type's property. |
| 152 | fields = append(fields, reflect.StructField{ |
| 153 | Name: proptools.FieldNameForProperty(p), |
| 154 | Type: reflect.TypeOf([]string{}), |
Paul Duffin | 865171e | 2020-03-02 18:38:15 +0000 | [diff] [blame] | 155 | Tag: `android:"arch_variant"`, |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 156 | }) |
| 157 | |
| 158 | // Copy the field index for use in the getter func as using the loop variable directly will |
| 159 | // cause all funcs to use the last value. |
| 160 | fieldIndex := f |
| 161 | |
| 162 | // Create an sdkMemberListProperty for the member type. |
| 163 | memberListProperty := &sdkMemberListProperty{ |
| 164 | getter: func(properties interface{}) []string { |
| 165 | // The properties is expected to be of the following form (where |
| 166 | // <Module_types> is the name of an SdkMemberType.SdkPropertyName(). |
| 167 | // properties *struct {<Module_types> []string, ....} |
| 168 | // |
| 169 | // Although it accesses the field by index the following reflection code is equivalent to: |
| 170 | // *properties.<Module_types> |
| 171 | // |
| 172 | list := reflect.ValueOf(properties).Elem().Field(fieldIndex).Interface().([]string) |
| 173 | return list |
| 174 | }, |
| 175 | |
| 176 | memberType: memberType, |
| 177 | |
Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 178 | dependencyTag: android.DependencyTagForSdkMemberType(memberType), |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | listProperties = append(listProperties, memberListProperty) |
| 182 | } |
| 183 | |
| 184 | // Create a dynamic struct from the collated fields. |
| 185 | propertiesStructType := reflect.StructOf(fields) |
| 186 | |
| 187 | return &dynamicSdkMemberTypes{ |
| 188 | memberListProperties: listProperties, |
| 189 | propertiesStructType: propertiesStructType, |
| 190 | } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 193 | // sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.) |
| 194 | // which Mainline modules like APEX can choose to build with. |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 195 | func SdkModuleFactory() android.Module { |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 196 | return newSdkModule(false) |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 197 | } |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 198 | |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 199 | func newSdkModule(moduleExports bool) *sdk { |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 200 | s := &sdk{} |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 201 | s.properties.Module_exports = moduleExports |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 202 | // 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] | 203 | var registry *android.SdkMemberTypesRegistry |
| 204 | if moduleExports { |
| 205 | registry = android.ModuleExportsMemberTypes |
| 206 | } else { |
| 207 | registry = android.SdkMemberTypes |
| 208 | } |
| 209 | s.dynamicSdkMemberTypes = getDynamicSdkMemberTypes(registry) |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 210 | // Create an instance of the dynamically created struct that contains all the |
| 211 | // properties for the member type specific list properties. |
| 212 | s.dynamicMemberTypeListProperties = s.dynamicSdkMemberTypes.createMemberListProperties() |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 213 | s.AddProperties(&s.properties, s.dynamicMemberTypeListProperties) |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 214 | android.InitCommonOSAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 215 | android.InitDefaultableModule(s) |
Jiyong Park | 100f3fd | 2019-11-06 16:03:32 +0900 | [diff] [blame] | 216 | android.AddLoadHook(s, func(ctx android.LoadHookContext) { |
| 217 | type props struct { |
| 218 | Compile_multilib *string |
| 219 | } |
| 220 | p := &props{Compile_multilib: proptools.StringPtr("both")} |
| 221 | ctx.AppendProperties(p) |
| 222 | }) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 223 | return s |
| 224 | } |
| 225 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 226 | // sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module. |
| 227 | func SnapshotModuleFactory() android.Module { |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 228 | s := newSdkModule(false) |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 229 | s.properties.Snapshot = true |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 230 | return s |
| 231 | } |
| 232 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 233 | func (s *sdk) memberListProperties() []*sdkMemberListProperty { |
| 234 | return s.dynamicSdkMemberTypes.memberListProperties |
| 235 | } |
| 236 | |
| 237 | func (s *sdk) getExportedMembers() map[string]struct{} { |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 238 | // Collect all the exported members. |
| 239 | exportedMembers := make(map[string]struct{}) |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 240 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 241 | for _, memberListProperty := range s.memberListProperties() { |
| 242 | names := memberListProperty.getter(s.dynamicMemberTypeListProperties) |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 243 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 244 | // Every member specified explicitly in the properties is exported by the sdk. |
| 245 | for _, name := range names { |
| 246 | exportedMembers[name] = struct{}{} |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 250 | return exportedMembers |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 253 | func (s *sdk) snapshot() bool { |
| 254 | return s.properties.Snapshot |
| 255 | } |
| 256 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 257 | func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 258 | if s.snapshot() { |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 259 | // We don't need to create a snapshot out of sdk_snapshot. |
| 260 | // That doesn't make sense. We need a snapshot to create sdk_snapshot. |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 261 | return |
| 262 | } |
| 263 | |
| 264 | // This method is guaranteed to be called on OsType specific variants before it is called |
| 265 | // on their corresponding CommonOS variant. |
| 266 | if !s.IsCommonOSVariant() { |
Paul Duffin | 6a7e953 | 2020-03-20 17:50:07 +0000 | [diff] [blame] | 267 | // Update the OsType specific sdk variant with information about its members. |
| 268 | s.collectMembers(ctx) |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 269 | } else { |
| 270 | // Get the OsType specific variants on which the CommonOS depends. |
| 271 | osSpecificVariants := android.GetOsSpecificVariantsOfCommonOSVariant(ctx) |
| 272 | var sdkVariants []*sdk |
| 273 | for _, m := range osSpecificVariants { |
| 274 | if sdkVariant, ok := m.(*sdk); ok { |
| 275 | sdkVariants = append(sdkVariants, sdkVariant) |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // Generate the snapshot from the member info. |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 280 | p := s.buildSnapshot(ctx, sdkVariants) |
| 281 | s.snapshotFile = android.OptionalPathForPath(p) |
| 282 | ctx.InstallFile(android.PathForMainlineSdksInstall(ctx), s.Name()+"-current.zip", p) |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 283 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 284 | } |
| 285 | |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 286 | func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries { |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 287 | if !s.snapshotFile.Valid() { |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 288 | return []android.AndroidMkEntries{} |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 289 | } |
| 290 | |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 291 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 292 | Class: "FAKE", |
| 293 | OutputFile: s.snapshotFile, |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 294 | DistFiles: android.MakeDefaultDistFiles(s.snapshotFile.Path()), |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 295 | Include: "$(BUILD_PHONY_PACKAGE)", |
Paul Duffin | 504b461 | 2019-11-22 14:52:29 +0000 | [diff] [blame] | 296 | ExtraFooters: []android.AndroidMkExtraFootersFunc{ |
| 297 | func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) { |
| 298 | // Allow the sdk to be built by simply passing its name on the command line. |
| 299 | fmt.Fprintln(w, ".PHONY:", s.Name()) |
| 300 | fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String()) |
| 301 | }, |
| 302 | }, |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 303 | }} |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | // RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware |
| 307 | // interface and the sdk module type. This function has been made public to be called by tests |
| 308 | // outside of the sdk package |
| 309 | func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) { |
| 310 | ctx.BottomUp("SdkMember", memberMutator).Parallel() |
| 311 | ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel() |
| 312 | ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel() |
| 313 | } |
| 314 | |
Paul Duffin | 6534770 | 2020-03-31 15:23:40 +0100 | [diff] [blame] | 315 | // RegisterPostDepsMutators registers post-deps mutators to support modules implementing SdkAware |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 316 | // interface and the sdk module type. This function has been made public to be called by tests |
| 317 | // outside of the sdk package |
| 318 | func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) { |
| 319 | // These must run AFTER apexMutator. Note that the apex package is imported even though there is |
| 320 | // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an |
| 321 | // APEX to its dependents. Since different versions of the same SDK can be used by different |
| 322 | // APEXes, the apex and its dependents (which includes the dependencies to the sdk members) |
| 323 | // should have been mutated for the apex before the SDK requirements are set. |
| 324 | ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel() |
| 325 | ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel() |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 326 | ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel() |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | type dependencyTag struct { |
| 330 | blueprint.BaseDependencyTag |
| 331 | } |
| 332 | |
Martin Stjernholm | cc77601 | 2020-07-07 03:22:21 +0100 | [diff] [blame^] | 333 | // Mark this tag so dependencies that use it are excluded from APEX contents. |
| 334 | func (t dependencyTag) ExcludeFromApexContents() {} |
| 335 | |
| 336 | var _ android.ExcludeFromApexContentsTag = dependencyTag{} |
| 337 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 338 | // 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 Duffin | fe14922 | 2020-01-14 14:06:09 +0000 | [diff] [blame] | 340 | type sdkMemberVersionedDepTag struct { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 341 | dependencyTag |
| 342 | member string |
| 343 | version string |
| 344 | } |
| 345 | |
Paul Duffin | fe14922 | 2020-01-14 14:06:09 +0000 | [diff] [blame] | 346 | // Mark this tag so dependencies that use it are excluded from visibility enforcement. |
| 347 | func (t sdkMemberVersionedDepTag) ExcludeFromVisibilityEnforcement() {} |
| 348 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 349 | // Step 1: create dependencies from an SDK module to its members. |
| 350 | func memberMutator(mctx android.BottomUpMutatorContext) { |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 351 | if s, ok := mctx.Module().(*sdk); ok { |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 352 | // Add dependencies from enabled and non CommonOS variants to the sdk member variants. |
| 353 | if s.Enabled() && !s.IsCommonOSVariant() { |
Paul Duffin | 583bf7e | 2020-02-20 13:39:41 +0000 | [diff] [blame] | 354 | for _, memberListProperty := range s.memberListProperties() { |
| 355 | names := memberListProperty.getter(s.dynamicMemberTypeListProperties) |
Paul Duffin | cc1b3da | 2020-02-27 13:29:56 +0000 | [diff] [blame] | 356 | if len(names) > 0 { |
| 357 | tag := memberListProperty.dependencyTag |
| 358 | memberListProperty.memberType.AddDependencies(mctx, tag, names) |
| 359 | } |
Paul Duffin | 583bf7e | 2020-02-20 13:39:41 +0000 | [diff] [blame] | 360 | } |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // Step 2: record that dependencies of SDK modules are members of the SDK modules |
| 366 | func memberDepsMutator(mctx android.TopDownMutatorContext) { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 367 | if s, ok := mctx.Module().(*sdk); ok { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 368 | mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name") |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 369 | 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 Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 382 | mctx.VisitDirectDeps(func(child android.Module) { |
| 383 | if member, ok := child.(android.SdkAware); ok { |
| 384 | member.MakeMemberOf(mySdkRef) |
| 385 | } |
| 386 | }) |
| 387 | } |
| 388 | } |
| 389 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 390 | // Step 3: create dependencies from the unversioned SDK member to snapshot versions |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 391 | // 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. |
| 396 | func memberInterVersionMutator(mctx android.BottomUpMutatorContext) { |
| 397 | if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 398 | if !m.ContainingSdk().Unversioned() { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 399 | memberName := m.MemberName() |
Paul Duffin | fe14922 | 2020-01-14 14:06:09 +0000 | [diff] [blame] | 400 | tag := sdkMemberVersionedDepTag{member: memberName, version: m.ContainingSdk().Version} |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 401 | 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 |
| 408 | func 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 |
| 425 | func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) { |
| 426 | if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 427 | if sdk := m.ContainingSdk(); !sdk.Unversioned() { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 428 | 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 Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 438 | |
Paul Duffin | 6534770 | 2020-03-31 15:23:40 +0100 | [diff] [blame] | 439 | // Step 6: ensure that the dependencies outside of the APEX are all from the required SDKs |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 440 | func sdkRequirementsMutator(mctx android.TopDownMutatorContext) { |
| 441 | if m, ok := mctx.Module().(interface { |
Paul Duffin | 923e8a5 | 2020-03-30 15:33:32 +0100 | [diff] [blame] | 442 | android.DepIsInSameApex |
| 443 | android.RequiredSdks |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 444 | }); ok { |
| 445 | requiredSdks := m.RequiredSdks() |
| 446 | if len(requiredSdks) == 0 { |
| 447 | return |
| 448 | } |
| 449 | mctx.VisitDirectDeps(func(dep android.Module) { |
Paul Duffin | 6534770 | 2020-03-31 15:23:40 +0100 | [diff] [blame] | 450 | tag := mctx.OtherModuleDependencyTag(dep) |
| 451 | if tag == android.DefaultsDepTag { |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 452 | // dependency to defaults is always okay |
| 453 | return |
| 454 | } |
| 455 | |
Paul Duffin | 6534770 | 2020-03-31 15:23:40 +0100 | [diff] [blame] | 456 | // Ignore the dependency from the unversioned member to any versioned members as an |
| 457 | // apex that depends on the unversioned member will not also be depending on a versioned |
| 458 | // member. |
| 459 | if _, ok := tag.(sdkMemberVersionedDepTag); ok { |
| 460 | return |
| 461 | } |
| 462 | |
| 463 | // If the dep is outside of the APEX, but is not in any of the |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 464 | // required SDKs, we know that the dep is a violation. |
| 465 | if sa, ok := dep.(android.SdkAware); ok { |
| 466 | if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) { |
| 467 | mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v", |
| 468 | sa.Name(), sa.ContainingSdk(), requiredSdks) |
| 469 | } |
| 470 | } |
| 471 | }) |
| 472 | } |
| 473 | } |