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