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 | 6d9108f0 | 2021-03-09 22:59:28 +0000 | [diff] [blame] | 36 | registerSdkBuildComponents(android.InitRegistrationContext) |
| 37 | } |
| 38 | |
| 39 | func registerSdkBuildComponents(ctx android.RegistrationContext) { |
| 40 | ctx.RegisterModuleType("sdk", SdkModuleFactory) |
| 41 | ctx.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory) |
| 42 | ctx.PreDepsMutators(RegisterPreDepsMutators) |
| 43 | ctx.PostDepsMutators(RegisterPostDepsMutators) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | type sdk struct { |
| 47 | android.ModuleBase |
| 48 | android.DefaultableModuleBase |
| 49 | |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 50 | // 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 Duffin | 2182726 | 2021-04-24 12:16:36 +0100 | [diff] [blame] | 57 | // Information about the OsType specific member variants depended upon by this variant. |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 58 | // |
Paul Duffin | 6a7e953 | 2020-03-20 17:50:07 +0000 | [diff] [blame] | 59 | // 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 Duffin | 2182726 | 2021-04-24 12:16:36 +0100 | [diff] [blame] | 64 | memberVariantDeps []sdkMemberVariantDep |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 65 | |
Paul Duffin | 6a7e953 | 2020-03-20 17:50:07 +0000 | [diff] [blame] | 66 | // The multilib variants that are used by this sdk variant. |
| 67 | multilibUsages multilibUsage |
| 68 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 69 | properties sdkProperties |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 70 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 71 | snapshotFile android.OptionalPath |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 72 | |
| 73 | // The builder, preserved for testing. |
| 74 | builderForTests *snapshotBuilder |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | type sdkProperties struct { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 78 | Snapshot bool `blueprint:"mutated"` |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 79 | |
| 80 | // True if this is a module_exports (or module_exports_snapshot) module type. |
| 81 | Module_exports bool `blueprint:"mutated"` |
Paul Duffin | 157f40f | 2020-09-29 16:01:08 +0100 | [diff] [blame] | 82 | |
| 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 |
Mathew Inwood | d0b99ce | 2021-05-20 11:00:12 +0100 | [diff] [blame^] | 96 | |
| 97 | // Specifying whether the generated prebuilt SDK build rule should have the |
| 98 | // prefer flag set or not. |
| 99 | Prebuilts_prefer *bool // default: false |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 100 | } |
| 101 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 102 | // Contains information about the sdk properties that list sdk members, e.g. |
Paul Duffin | a0dbf43 | 2019-12-05 11:25:53 +0000 | [diff] [blame] | 103 | // Java_header_libs. |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 104 | type sdkMemberListProperty struct { |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 105 | // getter for the list of member names |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 106 | getter func(properties interface{}) []string |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 107 | |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 108 | // setter for the list of member names |
| 109 | setter func(properties interface{}, list []string) |
| 110 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 111 | // the type of member referenced in the list |
| 112 | memberType android.SdkMemberType |
| 113 | |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 114 | // the dependency tag used for items in this list that can be used to determine the memberType |
| 115 | // for a resolved dependency. |
Paul Duffin | f853992 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 116 | dependencyTag android.SdkMemberTypeDependencyTag |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 119 | func (p *sdkMemberListProperty) propertyName() string { |
| 120 | return p.memberType.SdkPropertyName() |
| 121 | } |
| 122 | |
| 123 | // Cache of dynamically generated dynamicSdkMemberTypes objects. The key is the pointer |
| 124 | // to a slice of SdkMemberType instances held in android.SdkMemberTypes. |
| 125 | var dynamicSdkMemberTypesMap android.OncePer |
| 126 | |
| 127 | // A dynamically generated set of member list properties and associated structure type. |
| 128 | type dynamicSdkMemberTypes struct { |
| 129 | // The dynamically generated structure type. |
| 130 | // |
| 131 | // Contains one []string exported field for each android.SdkMemberTypes. The name of the field |
| 132 | // is the exported form of the value returned by SdkMemberType.SdkPropertyName(). |
| 133 | propertiesStructType reflect.Type |
| 134 | |
| 135 | // Information about each of the member type specific list properties. |
| 136 | memberListProperties []*sdkMemberListProperty |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 137 | |
| 138 | memberTypeToProperty map[android.SdkMemberType]*sdkMemberListProperty |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | func (d *dynamicSdkMemberTypes) createMemberListProperties() interface{} { |
| 142 | return reflect.New(d.propertiesStructType).Interface() |
| 143 | } |
| 144 | |
| 145 | func getDynamicSdkMemberTypes(registry *android.SdkMemberTypesRegistry) *dynamicSdkMemberTypes { |
| 146 | |
| 147 | // Get a key that uniquely identifies the registry contents. |
| 148 | key := registry.UniqueOnceKey() |
| 149 | |
| 150 | // Get the registered types. |
| 151 | registeredTypes := registry.RegisteredTypes() |
| 152 | |
| 153 | // Get the cached value, creating new instance if necessary. |
| 154 | return dynamicSdkMemberTypesMap.Once(key, func() interface{} { |
| 155 | return createDynamicSdkMemberTypes(registeredTypes) |
| 156 | }).(*dynamicSdkMemberTypes) |
| 157 | } |
| 158 | |
| 159 | // Create the dynamicSdkMemberTypes from the list of registered member types. |
Paul Duffin | a6e737b | 2019-11-29 11:55:51 +0000 | [diff] [blame] | 160 | // |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 161 | // A struct is created which contains one exported field per member type corresponding to |
| 162 | // the SdkMemberType.SdkPropertyName() value. |
| 163 | // |
| 164 | // A list of sdkMemberListProperty instances is created, one per member type that provides: |
| 165 | // * a reference to the member type. |
| 166 | // * a getter for the corresponding field in the properties struct. |
| 167 | // * a dependency tag that identifies the member type of a resolved dependency. |
| 168 | // |
| 169 | func createDynamicSdkMemberTypes(sdkMemberTypes []android.SdkMemberType) *dynamicSdkMemberTypes { |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 170 | |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 171 | var listProperties []*sdkMemberListProperty |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 172 | memberTypeToProperty := map[android.SdkMemberType]*sdkMemberListProperty{} |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 173 | var fields []reflect.StructField |
| 174 | |
| 175 | // Iterate over the member types creating StructField and sdkMemberListProperty objects. |
Paul Duffin | 1308205 | 2021-05-11 00:31:38 +0100 | [diff] [blame] | 176 | nextFieldIndex := 0 |
| 177 | for _, memberType := range sdkMemberTypes { |
| 178 | |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 179 | p := memberType.SdkPropertyName() |
| 180 | |
Paul Duffin | 1308205 | 2021-05-11 00:31:38 +0100 | [diff] [blame] | 181 | var getter func(properties interface{}) []string |
| 182 | var setter func(properties interface{}, list []string) |
| 183 | if memberType.RequiresBpProperty() { |
| 184 | // Create a dynamic exported field for the member type's property. |
| 185 | fields = append(fields, reflect.StructField{ |
| 186 | Name: proptools.FieldNameForProperty(p), |
| 187 | Type: reflect.TypeOf([]string{}), |
| 188 | Tag: `android:"arch_variant"`, |
| 189 | }) |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 190 | |
Paul Duffin | 1308205 | 2021-05-11 00:31:38 +0100 | [diff] [blame] | 191 | // Copy the field index for use in the getter func as using the loop variable directly will |
| 192 | // cause all funcs to use the last value. |
| 193 | fieldIndex := nextFieldIndex |
| 194 | nextFieldIndex += 1 |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 195 | |
Paul Duffin | 1308205 | 2021-05-11 00:31:38 +0100 | [diff] [blame] | 196 | getter = func(properties interface{}) []string { |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 197 | // The properties is expected to be of the following form (where |
| 198 | // <Module_types> is the name of an SdkMemberType.SdkPropertyName(). |
| 199 | // properties *struct {<Module_types> []string, ....} |
| 200 | // |
| 201 | // Although it accesses the field by index the following reflection code is equivalent to: |
| 202 | // *properties.<Module_types> |
| 203 | // |
| 204 | list := reflect.ValueOf(properties).Elem().Field(fieldIndex).Interface().([]string) |
| 205 | return list |
Paul Duffin | 1308205 | 2021-05-11 00:31:38 +0100 | [diff] [blame] | 206 | } |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 207 | |
Paul Duffin | 1308205 | 2021-05-11 00:31:38 +0100 | [diff] [blame] | 208 | setter = func(properties interface{}, list []string) { |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 209 | // The properties is expected to be of the following form (where |
| 210 | // <Module_types> is the name of an SdkMemberType.SdkPropertyName(). |
| 211 | // properties *struct {<Module_types> []string, ....} |
| 212 | // |
| 213 | // Although it accesses the field by index the following reflection code is equivalent to: |
| 214 | // *properties.<Module_types> = list |
| 215 | // |
| 216 | reflect.ValueOf(properties).Elem().Field(fieldIndex).Set(reflect.ValueOf(list)) |
Paul Duffin | 1308205 | 2021-05-11 00:31:38 +0100 | [diff] [blame] | 217 | } |
| 218 | } |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 219 | |
Paul Duffin | 1308205 | 2021-05-11 00:31:38 +0100 | [diff] [blame] | 220 | // Create an sdkMemberListProperty for the member type. |
| 221 | memberListProperty := &sdkMemberListProperty{ |
| 222 | getter: getter, |
| 223 | setter: setter, |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 224 | memberType: memberType, |
| 225 | |
Paul Duffin | a720811 | 2021-04-23 21:20:20 +0100 | [diff] [blame] | 226 | // Dependencies added directly from member properties are always exported. |
| 227 | dependencyTag: android.DependencyTagForSdkMemberType(memberType, true), |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 230 | memberTypeToProperty[memberType] = memberListProperty |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 231 | listProperties = append(listProperties, memberListProperty) |
| 232 | } |
| 233 | |
| 234 | // Create a dynamic struct from the collated fields. |
| 235 | propertiesStructType := reflect.StructOf(fields) |
| 236 | |
| 237 | return &dynamicSdkMemberTypes{ |
| 238 | memberListProperties: listProperties, |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 239 | memberTypeToProperty: memberTypeToProperty, |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 240 | propertiesStructType: propertiesStructType, |
| 241 | } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 244 | // sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.) |
| 245 | // which Mainline modules like APEX can choose to build with. |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 246 | func SdkModuleFactory() android.Module { |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 247 | return newSdkModule(false) |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 248 | } |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 249 | |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 250 | func newSdkModule(moduleExports bool) *sdk { |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 251 | s := &sdk{} |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 252 | s.properties.Module_exports = moduleExports |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 253 | // 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] | 254 | var registry *android.SdkMemberTypesRegistry |
| 255 | if moduleExports { |
| 256 | registry = android.ModuleExportsMemberTypes |
| 257 | } else { |
| 258 | registry = android.SdkMemberTypes |
| 259 | } |
| 260 | s.dynamicSdkMemberTypes = getDynamicSdkMemberTypes(registry) |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 261 | // Create an instance of the dynamically created struct that contains all the |
| 262 | // properties for the member type specific list properties. |
| 263 | s.dynamicMemberTypeListProperties = s.dynamicSdkMemberTypes.createMemberListProperties() |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 264 | s.AddProperties(&s.properties, s.dynamicMemberTypeListProperties) |
Paul Duffin | 157f40f | 2020-09-29 16:01:08 +0100 | [diff] [blame] | 265 | |
| 266 | // Make sure that the prebuilt visibility property is verified for errors. |
| 267 | android.AddVisibilityProperty(s, "prebuilt_visibility", &s.properties.Prebuilt_visibility) |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 268 | android.InitCommonOSAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 269 | android.InitDefaultableModule(s) |
Jiyong Park | 100f3fd | 2019-11-06 16:03:32 +0900 | [diff] [blame] | 270 | android.AddLoadHook(s, func(ctx android.LoadHookContext) { |
| 271 | type props struct { |
| 272 | Compile_multilib *string |
| 273 | } |
| 274 | p := &props{Compile_multilib: proptools.StringPtr("both")} |
Martin Stjernholm | 26ab8e8 | 2020-06-30 20:34:00 +0100 | [diff] [blame] | 275 | ctx.PrependProperties(p) |
Jiyong Park | 100f3fd | 2019-11-06 16:03:32 +0900 | [diff] [blame] | 276 | }) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 277 | return s |
| 278 | } |
| 279 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 280 | // sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module. |
| 281 | func SnapshotModuleFactory() android.Module { |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 282 | s := newSdkModule(false) |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 283 | s.properties.Snapshot = true |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 284 | return s |
| 285 | } |
| 286 | |
Paul Duffin | 7291095 | 2020-01-20 18:16:30 +0000 | [diff] [blame] | 287 | func (s *sdk) memberListProperties() []*sdkMemberListProperty { |
| 288 | return s.dynamicSdkMemberTypes.memberListProperties |
| 289 | } |
| 290 | |
Paul Duffin | cd06467 | 2021-04-24 00:47:29 +0100 | [diff] [blame] | 291 | func (s *sdk) memberListProperty(memberType android.SdkMemberType) *sdkMemberListProperty { |
| 292 | return s.dynamicSdkMemberTypes.memberTypeToProperty[memberType] |
| 293 | } |
| 294 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 295 | func (s *sdk) snapshot() bool { |
| 296 | return s.properties.Snapshot |
| 297 | } |
| 298 | |
Mathew Inwood | d0b99ce | 2021-05-20 11:00:12 +0100 | [diff] [blame^] | 299 | func (s *sdk) PreferPrebuilts() bool { |
| 300 | return proptools.BoolDefault(s.properties.Prebuilts_prefer, false) |
| 301 | } |
| 302 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 303 | func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 304 | if s.snapshot() { |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 305 | // We don't need to create a snapshot out of sdk_snapshot. |
| 306 | // 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] | 307 | return |
| 308 | } |
| 309 | |
| 310 | // This method is guaranteed to be called on OsType specific variants before it is called |
| 311 | // on their corresponding CommonOS variant. |
| 312 | if !s.IsCommonOSVariant() { |
Paul Duffin | 6a7e953 | 2020-03-20 17:50:07 +0000 | [diff] [blame] | 313 | // Update the OsType specific sdk variant with information about its members. |
| 314 | s.collectMembers(ctx) |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 315 | } else { |
| 316 | // Get the OsType specific variants on which the CommonOS depends. |
| 317 | osSpecificVariants := android.GetOsSpecificVariantsOfCommonOSVariant(ctx) |
| 318 | var sdkVariants []*sdk |
| 319 | for _, m := range osSpecificVariants { |
| 320 | if sdkVariant, ok := m.(*sdk); ok { |
| 321 | sdkVariants = append(sdkVariants, sdkVariant) |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // Generate the snapshot from the member info. |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 326 | p := s.buildSnapshot(ctx, sdkVariants) |
Mathew Inwood | 60770e2 | 2021-05-05 17:00:29 +0100 | [diff] [blame] | 327 | zip := ctx.InstallFile(android.PathForMainlineSdksInstall(ctx), p.Base(), p) |
| 328 | s.snapshotFile = android.OptionalPathForPath(zip) |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 329 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 330 | } |
| 331 | |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 332 | func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries { |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 333 | if !s.snapshotFile.Valid() { |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 334 | return []android.AndroidMkEntries{} |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 335 | } |
| 336 | |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 337 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 338 | Class: "FAKE", |
| 339 | OutputFile: s.snapshotFile, |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 340 | DistFiles: android.MakeDefaultDistFiles(s.snapshotFile.Path()), |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 341 | Include: "$(BUILD_PHONY_PACKAGE)", |
Paul Duffin | 504b461 | 2019-11-22 14:52:29 +0000 | [diff] [blame] | 342 | ExtraFooters: []android.AndroidMkExtraFootersFunc{ |
Jaewoong Jung | 02b11a6 | 2020-12-07 10:23:54 -0800 | [diff] [blame] | 343 | func(w io.Writer, name, prefix, moduleDir string) { |
Paul Duffin | 504b461 | 2019-11-22 14:52:29 +0000 | [diff] [blame] | 344 | // Allow the sdk to be built by simply passing its name on the command line. |
| 345 | fmt.Fprintln(w, ".PHONY:", s.Name()) |
| 346 | fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String()) |
| 347 | }, |
| 348 | }, |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 349 | }} |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | // RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware |
| 353 | // interface and the sdk module type. This function has been made public to be called by tests |
| 354 | // outside of the sdk package |
| 355 | func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) { |
| 356 | ctx.BottomUp("SdkMember", memberMutator).Parallel() |
| 357 | ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel() |
| 358 | ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel() |
| 359 | } |
| 360 | |
Paul Duffin | 6534770 | 2020-03-31 15:23:40 +0100 | [diff] [blame] | 361 | // RegisterPostDepsMutators registers post-deps mutators to support modules implementing SdkAware |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 362 | // interface and the sdk module type. This function has been made public to be called by tests |
| 363 | // outside of the sdk package |
| 364 | func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) { |
| 365 | // These must run AFTER apexMutator. Note that the apex package is imported even though there is |
| 366 | // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an |
| 367 | // APEX to its dependents. Since different versions of the same SDK can be used by different |
| 368 | // APEXes, the apex and its dependents (which includes the dependencies to the sdk members) |
| 369 | // should have been mutated for the apex before the SDK requirements are set. |
| 370 | ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel() |
| 371 | ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel() |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 372 | ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel() |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | type dependencyTag struct { |
| 376 | blueprint.BaseDependencyTag |
| 377 | } |
| 378 | |
Martin Stjernholm | cc77601 | 2020-07-07 03:22:21 +0100 | [diff] [blame] | 379 | // Mark this tag so dependencies that use it are excluded from APEX contents. |
| 380 | func (t dependencyTag) ExcludeFromApexContents() {} |
| 381 | |
| 382 | var _ android.ExcludeFromApexContentsTag = dependencyTag{} |
| 383 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 384 | // For dependencies from an in-development version of an SDK member to frozen versions of the same member |
| 385 | // e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12 |
Paul Duffin | 573989d | 2021-03-17 13:25:29 +0000 | [diff] [blame] | 386 | // |
| 387 | // The dependency represented by this tag requires that for every APEX variant created for the |
| 388 | // `from` module that an equivalent APEX variant is created for the 'to' module. This is because an |
| 389 | // APEX that requires a specific version of an sdk (via the `uses_sdks` property will replace |
| 390 | // dependencies on the unversioned sdk member with a dependency on the appropriate versioned sdk |
| 391 | // member. In order for that to work the versioned sdk member needs to have a variant for that APEX. |
| 392 | // As it is not known at the time that the APEX variants are created which specific APEX variants of |
| 393 | // a versioned sdk members will be required it is necessary for the versioned sdk members to have |
| 394 | // variants for any APEX that it could be used within. |
| 395 | // |
| 396 | // If the APEX selects a versioned sdk member then it will not have a dependency on the `from` |
| 397 | // module at all so any dependencies of that module will not affect the APEX. However, if the APEX |
| 398 | // selects the unversioned sdk member then it must exclude all the versioned sdk members. In no |
| 399 | // situation would this dependency cause the `to` module to be added to the APEX hence why this tag |
| 400 | // also excludes the `to` module from being added to the APEX contents. |
Paul Duffin | fe14922 | 2020-01-14 14:06:09 +0000 | [diff] [blame] | 401 | type sdkMemberVersionedDepTag struct { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 402 | dependencyTag |
| 403 | member string |
| 404 | version string |
| 405 | } |
| 406 | |
Paul Duffin | 573989d | 2021-03-17 13:25:29 +0000 | [diff] [blame] | 407 | func (t sdkMemberVersionedDepTag) AlwaysRequireApexVariant() bool { |
| 408 | return true |
| 409 | } |
| 410 | |
Paul Duffin | fe14922 | 2020-01-14 14:06:09 +0000 | [diff] [blame] | 411 | // Mark this tag so dependencies that use it are excluded from visibility enforcement. |
| 412 | func (t sdkMemberVersionedDepTag) ExcludeFromVisibilityEnforcement() {} |
| 413 | |
Paul Duffin | 573989d | 2021-03-17 13:25:29 +0000 | [diff] [blame] | 414 | var _ android.AlwaysRequireApexVariantTag = sdkMemberVersionedDepTag{} |
| 415 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 416 | // Step 1: create dependencies from an SDK module to its members. |
| 417 | func memberMutator(mctx android.BottomUpMutatorContext) { |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 418 | if s, ok := mctx.Module().(*sdk); ok { |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 419 | // Add dependencies from enabled and non CommonOS variants to the sdk member variants. |
| 420 | if s.Enabled() && !s.IsCommonOSVariant() { |
Paul Duffin | 583bf7e | 2020-02-20 13:39:41 +0000 | [diff] [blame] | 421 | for _, memberListProperty := range s.memberListProperties() { |
Paul Duffin | 1308205 | 2021-05-11 00:31:38 +0100 | [diff] [blame] | 422 | if memberListProperty.getter == nil { |
| 423 | continue |
| 424 | } |
Paul Duffin | 583bf7e | 2020-02-20 13:39:41 +0000 | [diff] [blame] | 425 | names := memberListProperty.getter(s.dynamicMemberTypeListProperties) |
Paul Duffin | cc1b3da | 2020-02-27 13:29:56 +0000 | [diff] [blame] | 426 | if len(names) > 0 { |
| 427 | tag := memberListProperty.dependencyTag |
| 428 | memberListProperty.memberType.AddDependencies(mctx, tag, names) |
| 429 | } |
Paul Duffin | 583bf7e | 2020-02-20 13:39:41 +0000 | [diff] [blame] | 430 | } |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | // Step 2: record that dependencies of SDK modules are members of the SDK modules |
| 436 | func memberDepsMutator(mctx android.TopDownMutatorContext) { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 437 | if s, ok := mctx.Module().(*sdk); ok { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 438 | mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name") |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 439 | if s.snapshot() && mySdkRef.Unversioned() { |
| 440 | mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+ |
| 441 | "Did you manually modify Android.bp?") |
| 442 | } |
| 443 | if !s.snapshot() && !mySdkRef.Unversioned() { |
| 444 | mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.") |
| 445 | } |
| 446 | if mySdkRef.Version != "" && mySdkRef.Version != "current" { |
| 447 | if _, err := strconv.Atoi(mySdkRef.Version); err != nil { |
| 448 | mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version) |
| 449 | } |
| 450 | } |
| 451 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 452 | mctx.VisitDirectDeps(func(child android.Module) { |
| 453 | if member, ok := child.(android.SdkAware); ok { |
| 454 | member.MakeMemberOf(mySdkRef) |
| 455 | } |
| 456 | }) |
| 457 | } |
| 458 | } |
| 459 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 460 | // Step 3: create dependencies from the unversioned SDK member to snapshot versions |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 461 | // of the same member. By having these dependencies, they are mutated for multiple Mainline modules |
| 462 | // (apex and apk), each of which might want different sdks to be built with. For example, if both |
| 463 | // apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be |
| 464 | // built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are |
| 465 | // using. |
| 466 | func memberInterVersionMutator(mctx android.BottomUpMutatorContext) { |
Paul Duffin | b9e7a3c | 2021-05-06 15:53:19 +0100 | [diff] [blame] | 467 | if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() && m.IsVersioned() { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 468 | if !m.ContainingSdk().Unversioned() { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 469 | memberName := m.MemberName() |
Paul Duffin | fe14922 | 2020-01-14 14:06:09 +0000 | [diff] [blame] | 470 | tag := sdkMemberVersionedDepTag{member: memberName, version: m.ContainingSdk().Version} |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 471 | mctx.AddReverseDependency(mctx.Module(), tag, memberName) |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
Paul Duffin | 4c3e8e2 | 2021-03-18 15:41:29 +0000 | [diff] [blame] | 476 | // An interface that encapsulates all the functionality needed to manage the sdk dependencies. |
| 477 | // |
| 478 | // It is a mixture of apex and sdk module functionality. |
| 479 | type sdkAndApexModule interface { |
| 480 | android.Module |
| 481 | android.DepIsInSameApex |
| 482 | android.RequiredSdks |
| 483 | } |
| 484 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 485 | // Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its |
| 486 | // descendants |
| 487 | func sdkDepsMutator(mctx android.TopDownMutatorContext) { |
Paul Duffin | 4c3e8e2 | 2021-03-18 15:41:29 +0000 | [diff] [blame] | 488 | if parent, ok := mctx.Module().(sdkAndApexModule); ok { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 489 | // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks() |
| 490 | // by reading its own properties like `uses_sdks`. |
Paul Duffin | a37eca2 | 2020-07-22 13:00:54 +0100 | [diff] [blame] | 491 | requiredSdks := parent.RequiredSdks() |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 492 | if len(requiredSdks) > 0 { |
| 493 | mctx.VisitDirectDeps(func(m android.Module) { |
Paul Duffin | a37eca2 | 2020-07-22 13:00:54 +0100 | [diff] [blame] | 494 | // Only propagate required sdks from the apex onto its contents. |
Paul Duffin | 4c3e8e2 | 2021-03-18 15:41:29 +0000 | [diff] [blame] | 495 | if dep, ok := m.(android.SdkAware); ok && android.IsDepInSameApex(mctx, parent, dep) { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 496 | dep.BuildWithSdks(requiredSdks) |
| 497 | } |
| 498 | }) |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | // Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the |
| 504 | // versioned module is used instead of the un-versioned (in-development) module libfoo |
| 505 | func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) { |
Paul Duffin | b9e7a3c | 2021-05-06 15:53:19 +0100 | [diff] [blame] | 506 | if versionedSdkMember, ok := mctx.Module().(android.SdkAware); ok && versionedSdkMember.IsInAnySdk() && versionedSdkMember.IsVersioned() { |
Paul Duffin | a37eca2 | 2020-07-22 13:00:54 +0100 | [diff] [blame] | 507 | if sdk := versionedSdkMember.ContainingSdk(); !sdk.Unversioned() { |
| 508 | // Only replace dependencies to <sdkmember> with <sdkmember@required-version> |
| 509 | // if the depending module requires it. e.g. |
| 510 | // foo -> sdkmember |
| 511 | // will be transformed to: |
| 512 | // foo -> sdkmember@1 |
| 513 | // if and only if foo is a member of an APEX that requires version 1 of the |
| 514 | // sdk containing sdkmember. |
| 515 | memberName := versionedSdkMember.MemberName() |
| 516 | |
Paul Duffin | 1822a0a | 2021-03-21 12:56:33 +0000 | [diff] [blame] | 517 | // Convert a panic into a normal error to allow it to be more easily tested for. This is a |
| 518 | // temporary workaround, once http://b/183204176 has been fixed this can be removed. |
| 519 | // TODO(b/183204176): Remove this after fixing. |
| 520 | defer func() { |
| 521 | if r := recover(); r != nil { |
Paul Duffin | b9e7a3c | 2021-05-06 15:53:19 +0100 | [diff] [blame] | 522 | mctx.ModuleErrorf("sdkDepsReplaceMutator %s", r) |
Paul Duffin | 1822a0a | 2021-03-21 12:56:33 +0000 | [diff] [blame] | 523 | } |
| 524 | }() |
| 525 | |
Paul Duffin | a37eca2 | 2020-07-22 13:00:54 +0100 | [diff] [blame] | 526 | // Replace dependencies on sdkmember with a dependency on the current module which |
| 527 | // is a versioned prebuilt of the sdkmember if required. |
| 528 | mctx.ReplaceDependenciesIf(memberName, func(from blueprint.Module, tag blueprint.DependencyTag, to blueprint.Module) bool { |
| 529 | // from - foo |
| 530 | // to - sdkmember |
| 531 | replace := false |
| 532 | if parent, ok := from.(android.RequiredSdks); ok { |
| 533 | replace = parent.RequiredSdks().Contains(sdk) |
| 534 | } |
| 535 | return replace |
| 536 | }) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | } |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 540 | |
Paul Duffin | 6534770 | 2020-03-31 15:23:40 +0100 | [diff] [blame] | 541 | // 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] | 542 | func sdkRequirementsMutator(mctx android.TopDownMutatorContext) { |
Paul Duffin | 4c3e8e2 | 2021-03-18 15:41:29 +0000 | [diff] [blame] | 543 | if m, ok := mctx.Module().(sdkAndApexModule); ok { |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 544 | requiredSdks := m.RequiredSdks() |
| 545 | if len(requiredSdks) == 0 { |
| 546 | return |
| 547 | } |
| 548 | mctx.VisitDirectDeps(func(dep android.Module) { |
Paul Duffin | 6534770 | 2020-03-31 15:23:40 +0100 | [diff] [blame] | 549 | tag := mctx.OtherModuleDependencyTag(dep) |
| 550 | if tag == android.DefaultsDepTag { |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 551 | // dependency to defaults is always okay |
| 552 | return |
| 553 | } |
| 554 | |
Paul Duffin | 6534770 | 2020-03-31 15:23:40 +0100 | [diff] [blame] | 555 | // Ignore the dependency from the unversioned member to any versioned members as an |
| 556 | // apex that depends on the unversioned member will not also be depending on a versioned |
| 557 | // member. |
| 558 | if _, ok := tag.(sdkMemberVersionedDepTag); ok { |
| 559 | return |
| 560 | } |
| 561 | |
Paul Duffin | 4c3e8e2 | 2021-03-18 15:41:29 +0000 | [diff] [blame] | 562 | // If the dep is outside of the APEX, but is not in any of the required SDKs, we know that the |
| 563 | // dep is a violation. |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 564 | if sa, ok := dep.(android.SdkAware); ok { |
Paul Duffin | 4c3e8e2 | 2021-03-18 15:41:29 +0000 | [diff] [blame] | 565 | // It is not an error if a dependency that is excluded from the apex due to the tag is not |
| 566 | // in one of the required SDKs. That is because all of the existing tags that implement it |
| 567 | // do not depend on modules which can or should belong to an sdk_snapshot. |
| 568 | if _, ok := tag.(android.ExcludeFromApexContentsTag); ok { |
| 569 | // The tag defines a dependency that never requires the child module to be part of the |
| 570 | // same apex. |
| 571 | return |
| 572 | } |
| 573 | |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 574 | if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) { |
| 575 | mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v", |
| 576 | sa.Name(), sa.ContainingSdk(), requiredSdks) |
| 577 | } |
| 578 | } |
| 579 | }) |
| 580 | } |
| 581 | } |