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