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