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" |
| 19 | "strconv" |
| 20 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 21 | "github.com/google/blueprint" |
Jiyong Park | 100f3fd | 2019-11-06 16:03:32 +0900 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 23 | |
| 24 | "android/soong/android" |
| 25 | // This package doesn't depend on the apex package, but import it to make its mutators to be |
| 26 | // registered before mutators in this package. See RegisterPostDepsMutators for more details. |
| 27 | _ "android/soong/apex" |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 28 | "android/soong/cc" |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | func init() { |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame^] | 32 | pctx.Import("android/soong/android") |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 33 | android.RegisterModuleType("sdk", ModuleFactory) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 34 | android.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 35 | android.PreDepsMutators(RegisterPreDepsMutators) |
| 36 | android.PostDepsMutators(RegisterPostDepsMutators) |
| 37 | } |
| 38 | |
| 39 | type sdk struct { |
| 40 | android.ModuleBase |
| 41 | android.DefaultableModuleBase |
| 42 | |
| 43 | properties sdkProperties |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 44 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame^] | 45 | snapshotFile android.OptionalPath |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | type sdkProperties struct { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 49 | // The list of java libraries in this SDK |
| 50 | Java_libs []string |
| 51 | // The list of native libraries in this SDK |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 52 | Native_shared_libs []string |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 53 | |
| 54 | Snapshot bool `blueprint:"mutated"` |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.) |
| 58 | // which Mainline modules like APEX can choose to build with. |
| 59 | func ModuleFactory() android.Module { |
| 60 | s := &sdk{} |
| 61 | s.AddProperties(&s.properties) |
| 62 | android.InitAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon) |
| 63 | android.InitDefaultableModule(s) |
Jiyong Park | 100f3fd | 2019-11-06 16:03:32 +0900 | [diff] [blame] | 64 | android.AddLoadHook(s, func(ctx android.LoadHookContext) { |
| 65 | type props struct { |
| 66 | Compile_multilib *string |
| 67 | } |
| 68 | p := &props{Compile_multilib: proptools.StringPtr("both")} |
| 69 | ctx.AppendProperties(p) |
| 70 | }) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 71 | return s |
| 72 | } |
| 73 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 74 | // sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module. |
| 75 | func SnapshotModuleFactory() android.Module { |
| 76 | s := ModuleFactory() |
| 77 | s.(*sdk).properties.Snapshot = true |
| 78 | return s |
| 79 | } |
| 80 | |
| 81 | func (s *sdk) snapshot() bool { |
| 82 | return s.properties.Snapshot |
| 83 | } |
| 84 | |
| 85 | func (s *sdk) frozenVersions(ctx android.BaseModuleContext) []string { |
| 86 | if s.snapshot() { |
| 87 | panic(fmt.Errorf("frozenVersions() called for sdk_snapshot %q", ctx.ModuleName())) |
| 88 | } |
| 89 | versions := []string{} |
| 90 | ctx.WalkDeps(func(child android.Module, parent android.Module) bool { |
| 91 | depTag := ctx.OtherModuleDependencyTag(child) |
| 92 | if depTag == sdkMemberDepTag { |
| 93 | return true |
| 94 | } |
| 95 | if versionedDepTag, ok := depTag.(sdkMemberVesionedDepTag); ok { |
| 96 | v := versionedDepTag.version |
| 97 | if v != "current" && !android.InList(v, versions) { |
| 98 | versions = append(versions, versionedDepTag.version) |
| 99 | } |
| 100 | } |
| 101 | return false |
| 102 | }) |
| 103 | return android.SortedUniqueStrings(versions) |
| 104 | } |
| 105 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 106 | func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame^] | 107 | if !s.snapshot() { |
| 108 | // We don't need to create a snapshot out of sdk_snapshot. |
| 109 | // That doesn't make sense. We need a snapshot to create sdk_snapshot. |
| 110 | s.snapshotFile = android.OptionalPathForPath(s.buildSnapshot(ctx)) |
| 111 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | func (s *sdk) AndroidMkEntries() android.AndroidMkEntries { |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame^] | 115 | if !s.snapshotFile.Valid() { |
| 116 | return android.AndroidMkEntries{} |
| 117 | } |
| 118 | |
| 119 | return android.AndroidMkEntries{ |
| 120 | Class: "FAKE", |
| 121 | OutputFile: s.snapshotFile, |
| 122 | DistFile: s.snapshotFile, |
| 123 | Include: "$(BUILD_PHONY_PACKAGE)", |
| 124 | } |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware |
| 128 | // interface and the sdk module type. This function has been made public to be called by tests |
| 129 | // outside of the sdk package |
| 130 | func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) { |
| 131 | ctx.BottomUp("SdkMember", memberMutator).Parallel() |
| 132 | ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel() |
| 133 | ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel() |
| 134 | } |
| 135 | |
| 136 | // RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware |
| 137 | // interface and the sdk module type. This function has been made public to be called by tests |
| 138 | // outside of the sdk package |
| 139 | func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) { |
| 140 | // These must run AFTER apexMutator. Note that the apex package is imported even though there is |
| 141 | // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an |
| 142 | // APEX to its dependents. Since different versions of the same SDK can be used by different |
| 143 | // APEXes, the apex and its dependents (which includes the dependencies to the sdk members) |
| 144 | // should have been mutated for the apex before the SDK requirements are set. |
| 145 | ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel() |
| 146 | ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel() |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 147 | ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel() |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | type dependencyTag struct { |
| 151 | blueprint.BaseDependencyTag |
| 152 | } |
| 153 | |
| 154 | // For dependencies from an SDK module to its members |
| 155 | // e.g. mysdk -> libfoo and libbar |
| 156 | var sdkMemberDepTag dependencyTag |
| 157 | |
| 158 | // For dependencies from an in-development version of an SDK member to frozen versions of the same member |
| 159 | // e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12 |
| 160 | type sdkMemberVesionedDepTag struct { |
| 161 | dependencyTag |
| 162 | member string |
| 163 | version string |
| 164 | } |
| 165 | |
| 166 | // Step 1: create dependencies from an SDK module to its members. |
| 167 | func memberMutator(mctx android.BottomUpMutatorContext) { |
| 168 | if m, ok := mctx.Module().(*sdk); ok { |
| 169 | mctx.AddVariationDependencies(nil, sdkMemberDepTag, m.properties.Java_libs...) |
| 170 | |
| 171 | targets := mctx.MultiTargets() |
| 172 | for _, target := range targets { |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 173 | for _, lib := range m.properties.Native_shared_libs { |
| 174 | name, version := cc.StubsLibNameAndVersion(lib) |
| 175 | if version == "" { |
| 176 | version = cc.LatestStubsVersionFor(mctx.Config(), name) |
| 177 | } |
| 178 | mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{ |
| 179 | {Mutator: "image", Variation: "core"}, |
| 180 | {Mutator: "link", Variation: "shared"}, |
| 181 | {Mutator: "version", Variation: version}, |
| 182 | }...), sdkMemberDepTag, name) |
| 183 | } |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Step 2: record that dependencies of SDK modules are members of the SDK modules |
| 189 | func memberDepsMutator(mctx android.TopDownMutatorContext) { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 190 | if s, ok := mctx.Module().(*sdk); ok { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 191 | mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name") |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 192 | if s.snapshot() && mySdkRef.Unversioned() { |
| 193 | mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+ |
| 194 | "Did you manually modify Android.bp?") |
| 195 | } |
| 196 | if !s.snapshot() && !mySdkRef.Unversioned() { |
| 197 | mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.") |
| 198 | } |
| 199 | if mySdkRef.Version != "" && mySdkRef.Version != "current" { |
| 200 | if _, err := strconv.Atoi(mySdkRef.Version); err != nil { |
| 201 | mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version) |
| 202 | } |
| 203 | } |
| 204 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 205 | mctx.VisitDirectDeps(func(child android.Module) { |
| 206 | if member, ok := child.(android.SdkAware); ok { |
| 207 | member.MakeMemberOf(mySdkRef) |
| 208 | } |
| 209 | }) |
| 210 | } |
| 211 | } |
| 212 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 213 | // Step 3: create dependencies from the unversioned SDK member to snapshot versions |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 214 | // of the same member. By having these dependencies, they are mutated for multiple Mainline modules |
| 215 | // (apex and apk), each of which might want different sdks to be built with. For example, if both |
| 216 | // apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be |
| 217 | // built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are |
| 218 | // using. |
| 219 | func memberInterVersionMutator(mctx android.BottomUpMutatorContext) { |
| 220 | if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 221 | if !m.ContainingSdk().Unversioned() { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 222 | memberName := m.MemberName() |
| 223 | tag := sdkMemberVesionedDepTag{member: memberName, version: m.ContainingSdk().Version} |
| 224 | mctx.AddReverseDependency(mctx.Module(), tag, memberName) |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its |
| 230 | // descendants |
| 231 | func sdkDepsMutator(mctx android.TopDownMutatorContext) { |
| 232 | if m, ok := mctx.Module().(android.SdkAware); ok { |
| 233 | // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks() |
| 234 | // by reading its own properties like `uses_sdks`. |
| 235 | requiredSdks := m.RequiredSdks() |
| 236 | if len(requiredSdks) > 0 { |
| 237 | mctx.VisitDirectDeps(func(m android.Module) { |
| 238 | if dep, ok := m.(android.SdkAware); ok { |
| 239 | dep.BuildWithSdks(requiredSdks) |
| 240 | } |
| 241 | }) |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the |
| 247 | // versioned module is used instead of the un-versioned (in-development) module libfoo |
| 248 | func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) { |
| 249 | if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 250 | if sdk := m.ContainingSdk(); !sdk.Unversioned() { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 251 | if m.RequiredSdks().Contains(sdk) { |
| 252 | // Note that this replacement is done only for the modules that have the same |
| 253 | // variations as the current module. Since current module is already mutated for |
| 254 | // apex references in other APEXes are not affected by this replacement. |
| 255 | memberName := m.MemberName() |
| 256 | mctx.ReplaceDependencies(memberName) |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 261 | |
| 262 | // Step 6: ensure that the dependencies from outside of the APEX are all from the required SDKs |
| 263 | func sdkRequirementsMutator(mctx android.TopDownMutatorContext) { |
| 264 | if m, ok := mctx.Module().(interface { |
| 265 | DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool |
| 266 | RequiredSdks() android.SdkRefs |
| 267 | }); ok { |
| 268 | requiredSdks := m.RequiredSdks() |
| 269 | if len(requiredSdks) == 0 { |
| 270 | return |
| 271 | } |
| 272 | mctx.VisitDirectDeps(func(dep android.Module) { |
| 273 | if mctx.OtherModuleDependencyTag(dep) == android.DefaultsDepTag { |
| 274 | // dependency to defaults is always okay |
| 275 | return |
| 276 | } |
| 277 | |
| 278 | // If the dep is from outside of the APEX, but is not in any of the |
| 279 | // required SDKs, we know that the dep is a violation. |
| 280 | if sa, ok := dep.(android.SdkAware); ok { |
| 281 | if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) { |
| 282 | mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v", |
| 283 | sa.Name(), sa.ContainingSdk(), requiredSdks) |
| 284 | } |
| 285 | } |
| 286 | }) |
| 287 | } |
| 288 | } |