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 ( |
| 18 | "github.com/google/blueprint" |
| 19 | |
| 20 | "android/soong/android" |
| 21 | // This package doesn't depend on the apex package, but import it to make its mutators to be |
| 22 | // registered before mutators in this package. See RegisterPostDepsMutators for more details. |
| 23 | _ "android/soong/apex" |
| 24 | ) |
| 25 | |
| 26 | func init() { |
| 27 | android.RegisterModuleType("sdk", ModuleFactory) |
| 28 | android.PreDepsMutators(RegisterPreDepsMutators) |
| 29 | android.PostDepsMutators(RegisterPostDepsMutators) |
| 30 | } |
| 31 | |
| 32 | type sdk struct { |
| 33 | android.ModuleBase |
| 34 | android.DefaultableModuleBase |
| 35 | |
| 36 | properties sdkProperties |
| 37 | } |
| 38 | |
| 39 | type sdkProperties struct { |
| 40 | // The list of java_import modules that provide Java stubs for this SDK |
| 41 | Java_libs []string |
| 42 | Native_shared_libs []string |
| 43 | } |
| 44 | |
| 45 | // sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.) |
| 46 | // which Mainline modules like APEX can choose to build with. |
| 47 | func ModuleFactory() android.Module { |
| 48 | s := &sdk{} |
| 49 | s.AddProperties(&s.properties) |
| 50 | android.InitAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon) |
| 51 | android.InitDefaultableModule(s) |
| 52 | return s |
| 53 | } |
| 54 | |
| 55 | func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 56 | // TODO(jiyong): add build rules for creating stubs from members of this SDK |
| 57 | } |
| 58 | |
| 59 | // RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware |
| 60 | // interface and the sdk module type. This function has been made public to be called by tests |
| 61 | // outside of the sdk package |
| 62 | func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) { |
| 63 | ctx.BottomUp("SdkMember", memberMutator).Parallel() |
| 64 | ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel() |
| 65 | ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel() |
| 66 | } |
| 67 | |
| 68 | // RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware |
| 69 | // interface and the sdk module type. This function has been made public to be called by tests |
| 70 | // outside of the sdk package |
| 71 | func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) { |
| 72 | // These must run AFTER apexMutator. Note that the apex package is imported even though there is |
| 73 | // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an |
| 74 | // APEX to its dependents. Since different versions of the same SDK can be used by different |
| 75 | // APEXes, the apex and its dependents (which includes the dependencies to the sdk members) |
| 76 | // should have been mutated for the apex before the SDK requirements are set. |
| 77 | ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel() |
| 78 | ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel() |
| 79 | } |
| 80 | |
| 81 | type dependencyTag struct { |
| 82 | blueprint.BaseDependencyTag |
| 83 | } |
| 84 | |
| 85 | // For dependencies from an SDK module to its members |
| 86 | // e.g. mysdk -> libfoo and libbar |
| 87 | var sdkMemberDepTag dependencyTag |
| 88 | |
| 89 | // For dependencies from an in-development version of an SDK member to frozen versions of the same member |
| 90 | // e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12 |
| 91 | type sdkMemberVesionedDepTag struct { |
| 92 | dependencyTag |
| 93 | member string |
| 94 | version string |
| 95 | } |
| 96 | |
| 97 | // Step 1: create dependencies from an SDK module to its members. |
| 98 | func memberMutator(mctx android.BottomUpMutatorContext) { |
| 99 | if m, ok := mctx.Module().(*sdk); ok { |
| 100 | mctx.AddVariationDependencies(nil, sdkMemberDepTag, m.properties.Java_libs...) |
| 101 | |
| 102 | targets := mctx.MultiTargets() |
| 103 | for _, target := range targets { |
| 104 | mctx.AddFarVariationDependencies([]blueprint.Variation{ |
| 105 | {Mutator: "arch", Variation: target.String()}, |
| 106 | {Mutator: "image", Variation: "core"}, |
| 107 | {Mutator: "link", Variation: "shared"}, |
| 108 | }, sdkMemberDepTag, m.properties.Native_shared_libs...) |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Step 2: record that dependencies of SDK modules are members of the SDK modules |
| 114 | func memberDepsMutator(mctx android.TopDownMutatorContext) { |
| 115 | if _, ok := mctx.Module().(*sdk); ok { |
| 116 | mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name") |
| 117 | mctx.VisitDirectDeps(func(child android.Module) { |
| 118 | if member, ok := child.(android.SdkAware); ok { |
| 119 | member.MakeMemberOf(mySdkRef) |
| 120 | } |
| 121 | }) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Step 3: create dependencies from the in-development version of an SDK member to frozen versions |
| 126 | // of the same member. By having these dependencies, they are mutated for multiple Mainline modules |
| 127 | // (apex and apk), each of which might want different sdks to be built with. For example, if both |
| 128 | // apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be |
| 129 | // built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are |
| 130 | // using. |
| 131 | func memberInterVersionMutator(mctx android.BottomUpMutatorContext) { |
| 132 | if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() { |
| 133 | if !m.ContainingSdk().IsCurrentVersion() { |
| 134 | memberName := m.MemberName() |
| 135 | tag := sdkMemberVesionedDepTag{member: memberName, version: m.ContainingSdk().Version} |
| 136 | mctx.AddReverseDependency(mctx.Module(), tag, memberName) |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its |
| 142 | // descendants |
| 143 | func sdkDepsMutator(mctx android.TopDownMutatorContext) { |
| 144 | if m, ok := mctx.Module().(android.SdkAware); ok { |
| 145 | // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks() |
| 146 | // by reading its own properties like `uses_sdks`. |
| 147 | requiredSdks := m.RequiredSdks() |
| 148 | if len(requiredSdks) > 0 { |
| 149 | mctx.VisitDirectDeps(func(m android.Module) { |
| 150 | if dep, ok := m.(android.SdkAware); ok { |
| 151 | dep.BuildWithSdks(requiredSdks) |
| 152 | } |
| 153 | }) |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the |
| 159 | // versioned module is used instead of the un-versioned (in-development) module libfoo |
| 160 | func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) { |
| 161 | if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() { |
| 162 | if sdk := m.ContainingSdk(); !sdk.IsCurrentVersion() { |
| 163 | if m.RequiredSdks().Contains(sdk) { |
| 164 | // Note that this replacement is done only for the modules that have the same |
| 165 | // variations as the current module. Since current module is already mutated for |
| 166 | // apex references in other APEXes are not affected by this replacement. |
| 167 | memberName := m.MemberName() |
| 168 | mctx.ReplaceDependencies(memberName) |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |