blob: 4eb3665fbbeb0fd3660b32cb7990feb6064d1382 [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"
Jiyong Park100f3fd2019-11-06 16:03:32 +090022 "github.com/google/blueprint/proptools"
Jiyong Parkd1063c12019-07-17 20:08:41 +090023
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 Park73c54ee2019-10-22 20:31:18 +090028 "android/soong/cc"
Jiyong Parkd1063c12019-07-17 20:08:41 +090029)
30
31func init() {
Jiyong Park232e7852019-11-04 12:23:40 +090032 pctx.Import("android/soong/android")
Jiyong Parkd1063c12019-07-17 20:08:41 +090033 android.RegisterModuleType("sdk", ModuleFactory)
Jiyong Park9b409bc2019-10-11 14:59:13 +090034 android.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory)
Jiyong Parkd1063c12019-07-17 20:08:41 +090035 android.PreDepsMutators(RegisterPreDepsMutators)
36 android.PostDepsMutators(RegisterPostDepsMutators)
37}
38
39type sdk struct {
40 android.ModuleBase
41 android.DefaultableModuleBase
42
43 properties sdkProperties
Jiyong Park9b409bc2019-10-11 14:59:13 +090044
Jiyong Park232e7852019-11-04 12:23:40 +090045 snapshotFile android.OptionalPath
Jiyong Parkd1063c12019-07-17 20:08:41 +090046}
47
48type sdkProperties struct {
Jiyong Park9b409bc2019-10-11 14:59:13 +090049 // The list of java libraries in this SDK
50 Java_libs []string
51 // The list of native libraries in this SDK
Jiyong Parkd1063c12019-07-17 20:08:41 +090052 Native_shared_libs []string
Jiyong Park9b409bc2019-10-11 14:59:13 +090053
54 Snapshot bool `blueprint:"mutated"`
Jiyong Parkd1063c12019-07-17 20:08:41 +090055}
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.
59func ModuleFactory() android.Module {
60 s := &sdk{}
61 s.AddProperties(&s.properties)
62 android.InitAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
63 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +090064 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 Parkd1063c12019-07-17 20:08:41 +090071 return s
72}
73
Jiyong Park9b409bc2019-10-11 14:59:13 +090074// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
75func SnapshotModuleFactory() android.Module {
76 s := ModuleFactory()
77 s.(*sdk).properties.Snapshot = true
78 return s
79}
80
81func (s *sdk) snapshot() bool {
82 return s.properties.Snapshot
83}
84
85func (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 Parkd1063c12019-07-17 20:08:41 +0900106func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park232e7852019-11-04 12:23:40 +0900107 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 Park9b409bc2019-10-11 14:59:13 +0900112}
113
114func (s *sdk) AndroidMkEntries() android.AndroidMkEntries {
Jiyong Park232e7852019-11-04 12:23:40 +0900115 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 Parkd1063c12019-07-17 20:08:41 +0900125}
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
130func 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
139func 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 Parka7bc8ad2019-10-15 15:20:07 +0900147 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900148}
149
150type dependencyTag struct {
151 blueprint.BaseDependencyTag
152}
153
154// For dependencies from an SDK module to its members
155// e.g. mysdk -> libfoo and libbar
156var 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
160type sdkMemberVesionedDepTag struct {
161 dependencyTag
162 member string
163 version string
164}
165
166// Step 1: create dependencies from an SDK module to its members.
167func 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 Park73c54ee2019-10-22 20:31:18 +0900173 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 Parkd1063c12019-07-17 20:08:41 +0900184 }
185 }
186}
187
188// Step 2: record that dependencies of SDK modules are members of the SDK modules
189func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900190 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900191 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900192 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 Parkd1063c12019-07-17 20:08:41 +0900205 mctx.VisitDirectDeps(func(child android.Module) {
206 if member, ok := child.(android.SdkAware); ok {
207 member.MakeMemberOf(mySdkRef)
208 }
209 })
210 }
211}
212
Jiyong Park9b409bc2019-10-11 14:59:13 +0900213// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900214// 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.
219func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
220 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900221 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900222 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
231func 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
248func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
249 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900250 if sdk := m.ContainingSdk(); !sdk.Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900251 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 Parka7bc8ad2019-10-15 15:20:07 +0900261
262// Step 6: ensure that the dependencies from outside of the APEX are all from the required SDKs
263func 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}