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" |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 29 | "android/soong/cc" |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 30 | "android/soong/java" |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | func init() { |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 34 | pctx.Import("android/soong/android") |
Paul Duffin | 375058f | 2019-11-29 20:17:53 +0000 | [diff] [blame] | 35 | pctx.Import("android/soong/java/config") |
| 36 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 37 | android.RegisterModuleType("sdk", ModuleFactory) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 38 | android.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 39 | android.PreDepsMutators(RegisterPreDepsMutators) |
| 40 | android.PostDepsMutators(RegisterPostDepsMutators) |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 41 | |
| 42 | // Populate the dependency tags for each member list property. This needs to |
| 43 | // be done here to break an initialization cycle. |
| 44 | for _, memberListProperty := range sdkMemberListProperties { |
| 45 | memberListProperty.dependencyTag = &sdkMemberDependencyTag{ |
| 46 | memberListProperty: memberListProperty, |
| 47 | } |
| 48 | } |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | type sdk struct { |
| 52 | android.ModuleBase |
| 53 | android.DefaultableModuleBase |
| 54 | |
| 55 | properties sdkProperties |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 56 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 57 | snapshotFile android.OptionalPath |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 58 | |
| 59 | // The builder, preserved for testing. |
| 60 | builderForTests *snapshotBuilder |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | type sdkProperties struct { |
Paul Duffin | a0dbf43 | 2019-12-05 11:25:53 +0000 | [diff] [blame^] | 64 | // The list of java header libraries in this SDK |
| 65 | // |
| 66 | // This should be used for java libraries that are provided separately at runtime, |
| 67 | // e.g. through an APEX. |
| 68 | Java_header_libs []string |
| 69 | // The list of java implementation libraries in this SDK |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 70 | Java_libs []string |
| 71 | // The list of native libraries in this SDK |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 72 | Native_shared_libs []string |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 73 | // The list of stub sources in this SDK |
| 74 | Stubs_sources []string |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 75 | |
| 76 | Snapshot bool `blueprint:"mutated"` |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 77 | } |
| 78 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 79 | type sdkMemberDependencyTag struct { |
| 80 | blueprint.BaseDependencyTag |
| 81 | memberListProperty *sdkMemberListProperty |
| 82 | } |
| 83 | |
| 84 | // Contains information about the sdk properties that list sdk members, e.g. |
Paul Duffin | a0dbf43 | 2019-12-05 11:25:53 +0000 | [diff] [blame^] | 85 | // Java_header_libs. |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 86 | type sdkMemberListProperty struct { |
| 87 | // the name of the property as used in a .bp file |
| 88 | name string |
| 89 | |
| 90 | // getter for the list of member names |
| 91 | getter func(properties *sdkProperties) []string |
| 92 | |
| 93 | // the type of member referenced in the list |
| 94 | memberType android.SdkMemberType |
| 95 | |
| 96 | // the dependency tag used for items in this list. |
| 97 | dependencyTag *sdkMemberDependencyTag |
| 98 | } |
| 99 | |
Paul Duffin | a6e737b | 2019-11-29 11:55:51 +0000 | [diff] [blame] | 100 | // Information about how to handle each member list property. |
| 101 | // |
| 102 | // It is organized first by package and then by name within the package. |
| 103 | // Packages are in alphabetical order and properties are in alphabetical order |
| 104 | // within each package. |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 105 | var sdkMemberListProperties = []*sdkMemberListProperty{ |
Paul Duffin | a6e737b | 2019-11-29 11:55:51 +0000 | [diff] [blame] | 106 | // Members from cc package. |
| 107 | { |
| 108 | name: "native_shared_libs", |
| 109 | getter: func(properties *sdkProperties) []string { return properties.Native_shared_libs }, |
| 110 | memberType: cc.LibrarySdkMemberType, |
| 111 | }, |
| 112 | // Members from java package. |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 113 | { |
Paul Duffin | a0dbf43 | 2019-12-05 11:25:53 +0000 | [diff] [blame^] | 114 | name: "java_header_libs", |
| 115 | getter: func(properties *sdkProperties) []string { return properties.Java_header_libs }, |
| 116 | memberType: java.HeaderLibrarySdkMemberType, |
| 117 | }, |
| 118 | { |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 119 | name: "java_libs", |
| 120 | getter: func(properties *sdkProperties) []string { return properties.Java_libs }, |
Paul Duffin | a0dbf43 | 2019-12-05 11:25:53 +0000 | [diff] [blame^] | 121 | memberType: java.ImplLibrarySdkMemberType, |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 122 | }, |
| 123 | { |
| 124 | name: "stubs_sources", |
| 125 | getter: func(properties *sdkProperties) []string { return properties.Stubs_sources }, |
| 126 | memberType: java.DroidStubsSdkMemberType, |
| 127 | }, |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 130 | // sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.) |
| 131 | // which Mainline modules like APEX can choose to build with. |
| 132 | func ModuleFactory() android.Module { |
| 133 | s := &sdk{} |
| 134 | s.AddProperties(&s.properties) |
| 135 | android.InitAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon) |
| 136 | android.InitDefaultableModule(s) |
Jiyong Park | 100f3fd | 2019-11-06 16:03:32 +0900 | [diff] [blame] | 137 | android.AddLoadHook(s, func(ctx android.LoadHookContext) { |
| 138 | type props struct { |
| 139 | Compile_multilib *string |
| 140 | } |
| 141 | p := &props{Compile_multilib: proptools.StringPtr("both")} |
| 142 | ctx.AppendProperties(p) |
| 143 | }) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 144 | return s |
| 145 | } |
| 146 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 147 | // sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module. |
| 148 | func SnapshotModuleFactory() android.Module { |
| 149 | s := ModuleFactory() |
| 150 | s.(*sdk).properties.Snapshot = true |
| 151 | return s |
| 152 | } |
| 153 | |
| 154 | func (s *sdk) snapshot() bool { |
| 155 | return s.properties.Snapshot |
| 156 | } |
| 157 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 158 | func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 159 | if !s.snapshot() { |
| 160 | // We don't need to create a snapshot out of sdk_snapshot. |
| 161 | // That doesn't make sense. We need a snapshot to create sdk_snapshot. |
| 162 | s.snapshotFile = android.OptionalPathForPath(s.buildSnapshot(ctx)) |
| 163 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | func (s *sdk) AndroidMkEntries() android.AndroidMkEntries { |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 167 | if !s.snapshotFile.Valid() { |
| 168 | return android.AndroidMkEntries{} |
| 169 | } |
| 170 | |
| 171 | return android.AndroidMkEntries{ |
| 172 | Class: "FAKE", |
| 173 | OutputFile: s.snapshotFile, |
| 174 | DistFile: s.snapshotFile, |
| 175 | Include: "$(BUILD_PHONY_PACKAGE)", |
Paul Duffin | 504b461 | 2019-11-22 14:52:29 +0000 | [diff] [blame] | 176 | ExtraFooters: []android.AndroidMkExtraFootersFunc{ |
| 177 | func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) { |
| 178 | // Allow the sdk to be built by simply passing its name on the command line. |
| 179 | fmt.Fprintln(w, ".PHONY:", s.Name()) |
| 180 | fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String()) |
| 181 | }, |
| 182 | }, |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 183 | } |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware |
| 187 | // interface and the sdk module type. This function has been made public to be called by tests |
| 188 | // outside of the sdk package |
| 189 | func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) { |
| 190 | ctx.BottomUp("SdkMember", memberMutator).Parallel() |
| 191 | ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel() |
| 192 | ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel() |
| 193 | } |
| 194 | |
| 195 | // RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware |
| 196 | // interface and the sdk module type. This function has been made public to be called by tests |
| 197 | // outside of the sdk package |
| 198 | func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) { |
| 199 | // These must run AFTER apexMutator. Note that the apex package is imported even though there is |
| 200 | // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an |
| 201 | // APEX to its dependents. Since different versions of the same SDK can be used by different |
| 202 | // APEXes, the apex and its dependents (which includes the dependencies to the sdk members) |
| 203 | // should have been mutated for the apex before the SDK requirements are set. |
| 204 | ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel() |
| 205 | ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel() |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 206 | ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel() |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | type dependencyTag struct { |
| 210 | blueprint.BaseDependencyTag |
| 211 | } |
| 212 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 213 | // For dependencies from an in-development version of an SDK member to frozen versions of the same member |
| 214 | // e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12 |
| 215 | type sdkMemberVesionedDepTag struct { |
| 216 | dependencyTag |
| 217 | member string |
| 218 | version string |
| 219 | } |
| 220 | |
| 221 | // Step 1: create dependencies from an SDK module to its members. |
| 222 | func memberMutator(mctx android.BottomUpMutatorContext) { |
| 223 | if m, ok := mctx.Module().(*sdk); ok { |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 224 | for _, memberListProperty := range sdkMemberListProperties { |
| 225 | names := memberListProperty.getter(&m.properties) |
| 226 | tag := memberListProperty.dependencyTag |
| 227 | memberListProperty.memberType.AddDependencies(mctx, tag, names) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // Step 2: record that dependencies of SDK modules are members of the SDK modules |
| 233 | func memberDepsMutator(mctx android.TopDownMutatorContext) { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 234 | if s, ok := mctx.Module().(*sdk); ok { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 235 | mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name") |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 236 | if s.snapshot() && mySdkRef.Unversioned() { |
| 237 | mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+ |
| 238 | "Did you manually modify Android.bp?") |
| 239 | } |
| 240 | if !s.snapshot() && !mySdkRef.Unversioned() { |
| 241 | mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.") |
| 242 | } |
| 243 | if mySdkRef.Version != "" && mySdkRef.Version != "current" { |
| 244 | if _, err := strconv.Atoi(mySdkRef.Version); err != nil { |
| 245 | mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version) |
| 246 | } |
| 247 | } |
| 248 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 249 | mctx.VisitDirectDeps(func(child android.Module) { |
| 250 | if member, ok := child.(android.SdkAware); ok { |
| 251 | member.MakeMemberOf(mySdkRef) |
| 252 | } |
| 253 | }) |
| 254 | } |
| 255 | } |
| 256 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 257 | // Step 3: create dependencies from the unversioned SDK member to snapshot versions |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 258 | // of the same member. By having these dependencies, they are mutated for multiple Mainline modules |
| 259 | // (apex and apk), each of which might want different sdks to be built with. For example, if both |
| 260 | // apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be |
| 261 | // built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are |
| 262 | // using. |
| 263 | func memberInterVersionMutator(mctx android.BottomUpMutatorContext) { |
| 264 | if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 265 | if !m.ContainingSdk().Unversioned() { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 266 | memberName := m.MemberName() |
| 267 | tag := sdkMemberVesionedDepTag{member: memberName, version: m.ContainingSdk().Version} |
| 268 | mctx.AddReverseDependency(mctx.Module(), tag, memberName) |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its |
| 274 | // descendants |
| 275 | func sdkDepsMutator(mctx android.TopDownMutatorContext) { |
| 276 | if m, ok := mctx.Module().(android.SdkAware); ok { |
| 277 | // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks() |
| 278 | // by reading its own properties like `uses_sdks`. |
| 279 | requiredSdks := m.RequiredSdks() |
| 280 | if len(requiredSdks) > 0 { |
| 281 | mctx.VisitDirectDeps(func(m android.Module) { |
| 282 | if dep, ok := m.(android.SdkAware); ok { |
| 283 | dep.BuildWithSdks(requiredSdks) |
| 284 | } |
| 285 | }) |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the |
| 291 | // versioned module is used instead of the un-versioned (in-development) module libfoo |
| 292 | func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) { |
| 293 | if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 294 | if sdk := m.ContainingSdk(); !sdk.Unversioned() { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 295 | if m.RequiredSdks().Contains(sdk) { |
| 296 | // Note that this replacement is done only for the modules that have the same |
| 297 | // variations as the current module. Since current module is already mutated for |
| 298 | // apex references in other APEXes are not affected by this replacement. |
| 299 | memberName := m.MemberName() |
| 300 | mctx.ReplaceDependencies(memberName) |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | } |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 305 | |
| 306 | // Step 6: ensure that the dependencies from outside of the APEX are all from the required SDKs |
| 307 | func sdkRequirementsMutator(mctx android.TopDownMutatorContext) { |
| 308 | if m, ok := mctx.Module().(interface { |
| 309 | DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool |
| 310 | RequiredSdks() android.SdkRefs |
| 311 | }); ok { |
| 312 | requiredSdks := m.RequiredSdks() |
| 313 | if len(requiredSdks) == 0 { |
| 314 | return |
| 315 | } |
| 316 | mctx.VisitDirectDeps(func(dep android.Module) { |
| 317 | if mctx.OtherModuleDependencyTag(dep) == android.DefaultsDepTag { |
| 318 | // dependency to defaults is always okay |
| 319 | return |
| 320 | } |
| 321 | |
| 322 | // If the dep is from outside of the APEX, but is not in any of the |
| 323 | // required SDKs, we know that the dep is a violation. |
| 324 | if sa, ok := dep.(android.SdkAware); ok { |
| 325 | if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) { |
| 326 | mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v", |
| 327 | sa.Name(), sa.ContainingSdk(), requiredSdks) |
| 328 | } |
| 329 | } |
| 330 | }) |
| 331 | } |
| 332 | } |