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